|
| 1 | +package com.stagehand.api.example; |
| 2 | + |
| 3 | +import com.browserbase.api.client.StagehandClient; |
| 4 | +import com.browserbase.api.client.okhttp.StagehandOkHttpClient; |
| 5 | +import com.browserbase.api.core.JsonValue; |
| 6 | +import com.browserbase.api.core.RequestOptions; |
| 7 | +import com.browserbase.api.core.http.StreamResponse; |
| 8 | +import com.browserbase.api.models.sessions.ModelConfig; |
| 9 | +import com.browserbase.api.models.sessions.SessionActParams; |
| 10 | +import com.browserbase.api.models.sessions.SessionEndParams; |
| 11 | +import com.browserbase.api.models.sessions.SessionExecuteParams; |
| 12 | +import com.browserbase.api.models.sessions.SessionExtractParams; |
| 13 | +import com.browserbase.api.models.sessions.SessionNavigateParams; |
| 14 | +import com.browserbase.api.models.sessions.SessionObserveParams; |
| 15 | +import com.browserbase.api.models.sessions.SessionStartParams; |
| 16 | +import com.browserbase.api.models.sessions.SessionStartResponse; |
| 17 | +import com.browserbase.api.models.sessions.StreamEvent; |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +/** |
| 23 | + * Local Stagehand server + multiregion Browserbase example demonstrating SSE streaming. |
| 24 | + * |
| 25 | + * This example shows the full flow of: |
| 26 | + * 1. Starting a Browserbase session in eu-central-1 (via local Stagehand server) |
| 27 | + * 2. Navigating to a webpage |
| 28 | + * 3. Observing possible actions (streaming) |
| 29 | + * 4. Acting on an element (streaming) |
| 30 | + * 5. Extracting structured data (streaming) |
| 31 | + * 6. Running an autonomous agent (streaming) |
| 32 | + * 7. Ending the session |
| 33 | + * |
| 34 | + * Required environment variables: |
| 35 | + * - BROWSERBASE_API_KEY: Your Browserbase API key |
| 36 | + * - BROWSERBASE_PROJECT_ID: Your Browserbase project ID |
| 37 | + * - MODEL_API_KEY: Your OpenAI API key |
| 38 | + * - STAGEHAND_API_URL: Local Stagehand server base URL |
| 39 | + */ |
| 40 | +public class LocalServerMultiregionBrowserExample { |
| 41 | + public static void main(String[] args) { |
| 42 | + Env.load(); |
| 43 | + StagehandClient client = StagehandOkHttpClient.builder().fromEnv().build(); |
| 44 | + |
| 45 | + SessionStartResponse startResponse = client.sessions() |
| 46 | + .start(SessionStartParams.builder() |
| 47 | + .modelName("anthropic/claude-sonnet-4-6") |
| 48 | + .browser(SessionStartParams.Browser.builder() |
| 49 | + .type(SessionStartParams.Browser.Type.BROWSERBASE) |
| 50 | + .build()) |
| 51 | + .browserbaseSessionCreateParams(SessionStartParams.BrowserbaseSessionCreateParams.builder() |
| 52 | + .projectId(System.getProperty("stagehand.browserbaseProjectId")) |
| 53 | + .region(SessionStartParams.BrowserbaseSessionCreateParams.Region.EU_CENTRAL_1) |
| 54 | + .build()) |
| 55 | + .build()); |
| 56 | + |
| 57 | + String sessionId = startResponse.data().sessionId(); |
| 58 | + System.out.println("Session started: " + sessionId); |
| 59 | + |
| 60 | + try { |
| 61 | + client.sessions() |
| 62 | + .navigate(SessionNavigateParams.builder() |
| 63 | + .id(sessionId) |
| 64 | + .url("https://news.ycombinator.com") |
| 65 | + .build()); |
| 66 | + System.out.println("Navigated to Hacker News"); |
| 67 | + |
| 68 | + SessionObserveParams observeParams = SessionObserveParams.builder() |
| 69 | + .id(sessionId) |
| 70 | + .instruction("find the link to view comments for the top post") |
| 71 | + .xStreamResponse(SessionObserveParams.XStreamResponse.TRUE) |
| 72 | + .build(); |
| 73 | + try (StreamResponse<StreamEvent> observeStream = client.sessions().observeStreaming(observeParams)) { |
| 74 | + printStreamEvents("observe", observeStream); |
| 75 | + } |
| 76 | + |
| 77 | + SessionActParams actParams = SessionActParams.builder() |
| 78 | + .id(sessionId) |
| 79 | + .input("Click the comments link for the top post") |
| 80 | + .xStreamResponse(SessionActParams.XStreamResponse.TRUE) |
| 81 | + .build(); |
| 82 | + try (StreamResponse<StreamEvent> actStream = client.sessions().actStreaming(actParams)) { |
| 83 | + printStreamEvents("act", actStream); |
| 84 | + } |
| 85 | + |
| 86 | + SessionExtractParams extractParams = SessionExtractParams.builder() |
| 87 | + .id(sessionId) |
| 88 | + .instruction("extract the text of the top comment on this page") |
| 89 | + .schema(SessionExtractParams.Schema.builder() |
| 90 | + .putAdditionalProperty("type", JsonValue.from("object")) |
| 91 | + .putAdditionalProperty( |
| 92 | + "properties", |
| 93 | + JsonValue.from(Map.of( |
| 94 | + "commentText", |
| 95 | + Map.of( |
| 96 | + "type", |
| 97 | + "string", |
| 98 | + "description", |
| 99 | + "The text content of the top comment"), |
| 100 | + "author", |
| 101 | + Map.of( |
| 102 | + "type", |
| 103 | + "string", |
| 104 | + "description", |
| 105 | + "The username of the comment author")))) |
| 106 | + .putAdditionalProperty("required", JsonValue.from(List.of("commentText"))) |
| 107 | + .build()) |
| 108 | + .xStreamResponse(SessionExtractParams.XStreamResponse.TRUE) |
| 109 | + .build(); |
| 110 | + try (StreamResponse<StreamEvent> extractStream = client.sessions().extractStreaming(extractParams)) { |
| 111 | + printStreamEvents("extract", extractStream); |
| 112 | + } |
| 113 | + |
| 114 | + SessionExecuteParams executeParams = SessionExecuteParams.builder() |
| 115 | + .id(sessionId) |
| 116 | + .executeOptions(SessionExecuteParams.ExecuteOptions.builder() |
| 117 | + .instruction( |
| 118 | + "Find any personal website, GitHub, LinkedIn, or other best profile URL for the" |
| 119 | + + " top comment author on this page. Click on the username to go to their" |
| 120 | + + " profile page and look for any links they have shared. Use Google Search" |
| 121 | + + " with their username or other details from their profile if you don't find" |
| 122 | + + " any direct links.") |
| 123 | + .maxSteps(15.0) |
| 124 | + .build()) |
| 125 | + .agentConfig(SessionExecuteParams.AgentConfig.builder() |
| 126 | + .model(ModelConfig.builder() |
| 127 | + .modelName("anthropic/claude-opus-4-6") |
| 128 | + .apiKey(System.getProperty("stagehand.modelApiKey")) |
| 129 | + .build()) |
| 130 | + .cua(false) |
| 131 | + .build()) |
| 132 | + .xStreamResponse(SessionExecuteParams.XStreamResponse.TRUE) |
| 133 | + .build(); |
| 134 | + try (StreamResponse<StreamEvent> executeStream = client.sessions() |
| 135 | + .executeStreaming( |
| 136 | + executeParams, |
| 137 | + RequestOptions.builder() |
| 138 | + .timeout(Duration.ofMinutes(5)) |
| 139 | + .build())) { |
| 140 | + printStreamEvents("execute", executeStream); |
| 141 | + } |
| 142 | + } finally { |
| 143 | + client.sessions().end(SessionEndParams.builder().id(sessionId).build()); |
| 144 | + System.out.println("Session ended"); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static void printStreamEvents(String label, StreamResponse<StreamEvent> stream) { |
| 149 | + stream.stream().forEach(event -> System.out.println("[" + label + "] " + event.type() + " " + event.data())); |
| 150 | + System.out.println("[" + label + "] stream complete"); |
| 151 | + } |
| 152 | +} |
0 commit comments