|
| 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.http.StreamResponse; |
| 7 | +import com.browserbase.api.models.sessions.ModelConfig; |
| 8 | +import com.browserbase.api.models.sessions.SessionActParams; |
| 9 | +import com.browserbase.api.models.sessions.SessionEndParams; |
| 10 | +import com.browserbase.api.models.sessions.SessionExecuteParams; |
| 11 | +import com.browserbase.api.models.sessions.SessionExtractParams; |
| 12 | +import com.browserbase.api.models.sessions.SessionNavigateParams; |
| 13 | +import com.browserbase.api.models.sessions.SessionObserveParams; |
| 14 | +import com.browserbase.api.models.sessions.SessionStartParams; |
| 15 | +import com.browserbase.api.models.sessions.SessionStartResponse; |
| 16 | +import com.browserbase.api.models.sessions.StreamEvent; |
| 17 | +import com.microsoft.playwright.Browser; |
| 18 | +import com.microsoft.playwright.BrowserContext; |
| 19 | +import com.microsoft.playwright.Page; |
| 20 | +import com.microsoft.playwright.Playwright; |
| 21 | + |
| 22 | +public class LocalBrowserPlaywrightExample { |
| 23 | + public static void main(String[] args) { |
| 24 | + StagehandClient client = StagehandOkHttpClient.fromEnv(); |
| 25 | + |
| 26 | + SessionStartParams startParams = SessionStartParams.builder() |
| 27 | + .modelName("openai/gpt-5-nano") |
| 28 | + .browser(SessionStartParams.Browser.builder() |
| 29 | + .type(SessionStartParams.Browser.Type.LOCAL) |
| 30 | + .launchOptions(SessionStartParams.Browser.LaunchOptions.builder() |
| 31 | + .headless(true) |
| 32 | + .build()) |
| 33 | + .build()) |
| 34 | + .build(); |
| 35 | + |
| 36 | + SessionStartResponse startResponse = client.sessions().start(startParams); |
| 37 | + String sessionId = startResponse.data().sessionId(); |
| 38 | + String cdpUrl = startResponse.data().cdpUrl().orElse(null); |
| 39 | + if (cdpUrl == null || cdpUrl.isEmpty()) { |
| 40 | + throw new IllegalStateException("No cdpUrl returned for local session."); |
| 41 | + } |
| 42 | + |
| 43 | + try (Playwright playwright = Playwright.create()) { |
| 44 | + Browser browser = playwright.chromium().connectOverCDP(cdpUrl); |
| 45 | + BrowserContext context = browser.contexts().isEmpty() |
| 46 | + ? browser.newContext() |
| 47 | + : browser.contexts().get(0); |
| 48 | + Page page = context.pages().isEmpty() |
| 49 | + ? context.newPage() |
| 50 | + : context.pages().get(0); |
| 51 | + |
| 52 | + try { |
| 53 | + client.sessions() |
| 54 | + .navigate(SessionNavigateParams.builder() |
| 55 | + .id(sessionId) |
| 56 | + .url("https://example.com") |
| 57 | + .build()); |
| 58 | + page.navigate("https://example.com"); |
| 59 | + |
| 60 | + try (StreamResponse<StreamEvent> observeStream = client.sessions() |
| 61 | + .observeStreaming(SessionObserveParams.builder() |
| 62 | + .id(sessionId) |
| 63 | + .instruction("Find the most relevant click target on this page") |
| 64 | + .xStreamResponse(SessionObserveParams.XStreamResponse.TRUE) |
| 65 | + .build())) { |
| 66 | + printStreamEvents("observe", observeStream); |
| 67 | + } |
| 68 | + |
| 69 | + try (StreamResponse<StreamEvent> actStream = client.sessions() |
| 70 | + .actStreaming(SessionActParams.builder() |
| 71 | + .id(sessionId) |
| 72 | + .input("Click the 'Learn more' link") |
| 73 | + .xStreamResponse(SessionActParams.XStreamResponse.TRUE) |
| 74 | + .build())) { |
| 75 | + printStreamEvents("act", actStream); |
| 76 | + } |
| 77 | + |
| 78 | + SessionExtractParams.Schema schema = SessionExtractParams.Schema.builder() |
| 79 | + .putAdditionalProperty("type", JsonValue.from("object")) |
| 80 | + .putAdditionalProperty( |
| 81 | + "properties", |
| 82 | + JsonValue.from(java.util.Map.of( |
| 83 | + "title", java.util.Map.of("type", "string"), |
| 84 | + "h1", java.util.Map.of("type", "string")))) |
| 85 | + .putAdditionalProperty("required", JsonValue.from(java.util.List.of("title", "h1"))) |
| 86 | + .build(); |
| 87 | + |
| 88 | + try (StreamResponse<StreamEvent> extractStream = client.sessions() |
| 89 | + .extractStreaming(SessionExtractParams.builder() |
| 90 | + .id(sessionId) |
| 91 | + .instruction("Extract the page title and the primary heading (h1) text") |
| 92 | + .schema(schema) |
| 93 | + .xStreamResponse(SessionExtractParams.XStreamResponse.TRUE) |
| 94 | + .build())) { |
| 95 | + printStreamEvents("extract", extractStream); |
| 96 | + } |
| 97 | + |
| 98 | + try (StreamResponse<StreamEvent> executeStream = client.sessions() |
| 99 | + .executeStreaming(SessionExecuteParams.builder() |
| 100 | + .id(sessionId) |
| 101 | + .executeOptions(SessionExecuteParams.ExecuteOptions.builder() |
| 102 | + .instruction("Click the 'Learn more' link if available") |
| 103 | + .maxSteps(3.0) |
| 104 | + .build()) |
| 105 | + .agentConfig(SessionExecuteParams.AgentConfig.builder() |
| 106 | + .model(ModelConfig.builder() |
| 107 | + .modelName("openai/gpt-5-nano") |
| 108 | + .apiKey(System.getenv("MODEL_API_KEY")) |
| 109 | + .build()) |
| 110 | + .cua(false) |
| 111 | + .build()) |
| 112 | + .xStreamResponse(SessionExecuteParams.XStreamResponse.TRUE) |
| 113 | + .build())) { |
| 114 | + printStreamEvents("execute", executeStream); |
| 115 | + } |
| 116 | + } finally { |
| 117 | + client.sessions().end(SessionEndParams.builder().id(sessionId).build()); |
| 118 | + browser.close(); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static void printStreamEvents(String label, StreamResponse<StreamEvent> stream) { |
| 124 | + stream.stream().forEach(event -> System.out.println("[" + label + "] " + event.type() + " " + event.data())); |
| 125 | + System.out.println("[" + label + "] stream complete"); |
| 126 | + } |
| 127 | +} |
0 commit comments