|
| 1 | +// @ts-nocheck |
| 2 | + |
| 3 | +import "dotenv/config"; |
| 4 | +import { Kernel, type KernelContext } from "@onkernel/sdk"; |
| 5 | +import { chromium } from "playwright"; |
| 6 | +import { Agent } from "./lib/agent"; |
| 7 | +import computers from "./lib/computers"; |
| 8 | + |
| 9 | +const kernel = new Kernel(); |
| 10 | +const app = kernel.app("ts-cua"); |
| 11 | + |
| 12 | +// LLM API Keys are set in the environment during `kernel deploy <filename> -e ANTHROPIC_API_KEY=XXX` |
| 13 | +// See https://docs.onkernel.com/launch/deploy#environment-variables |
| 14 | +if (!process.env.OPENAI_API_KEY) throw new Error('OPENAI_API_KEY is not set'); |
| 15 | + |
| 16 | +/** |
| 17 | + * Example app that run an agent using openai CUA |
| 18 | + * Args: |
| 19 | + * ctx: Kernel context containing invocation information |
| 20 | + * payload: An object with a `query` property |
| 21 | + * Returns: |
| 22 | + * An answer to the query, elapsed time and optionally the messages stack |
| 23 | + * Invoke this via CLI: |
| 24 | + * export KERNEL_API_KEY=<your_api_key> |
| 25 | + * kernel deploy index.ts -e OPENAI_API_KEY=XXXXX --force |
| 26 | + * kernel invoke ts-cua agent-run -p "{\"query\":\"current market price range for a used dreamcast\"}" |
| 27 | + * kernel logs ts-cua -f # Open in separate tab |
| 28 | + */ |
| 29 | + |
| 30 | +interface CuaInput { |
| 31 | + query: string; |
| 32 | +} |
| 33 | + |
| 34 | +interface CuaOutput { |
| 35 | + elapsed: number; |
| 36 | + response: Array<object>; |
| 37 | + answer: object; |
| 38 | +} |
| 39 | + |
| 40 | +app.action<CuaInput, CuaOutput>( |
| 41 | + "agent-run", |
| 42 | + async (ctx: KernelContext, payload?: CuaInput): Promise<CuaOutput> => { |
| 43 | + const startTime = Date.now(); |
| 44 | + const kernelBrowser = await kernel.browsers.create({ |
| 45 | + invocation_id: ctx.invocation_id, |
| 46 | + }); |
| 47 | + console.log( |
| 48 | + "> Kernel browser live view url: ", |
| 49 | + kernelBrowser.browser_live_view_url, |
| 50 | + ); |
| 51 | + |
| 52 | + try { |
| 53 | + |
| 54 | + // kernel browser |
| 55 | + const { computer } = await computers.create({ |
| 56 | + type: "kernel", |
| 57 | + cdp_ws_url: kernelBrowser.cdp_ws_url, |
| 58 | + }); |
| 59 | + |
| 60 | + // setup agent |
| 61 | + const agent = new Agent( |
| 62 | + "computer-use-preview", |
| 63 | + computer, |
| 64 | + [], // additional tools |
| 65 | + (message: string) => { |
| 66 | + console.log(`> safety check: ${message}`); |
| 67 | + return true; // Auto-acknowledge all safety checks for testing |
| 68 | + }, |
| 69 | + ); |
| 70 | + |
| 71 | + // start agent run |
| 72 | + const response = await agent.runFullTurn( |
| 73 | + [ |
| 74 | + { |
| 75 | + role: "system", |
| 76 | + content: `- Current date and time: ${new Date().toISOString()} (${new Date().toLocaleDateString("en-US", { weekday: "long" })})`, |
| 77 | + }, |
| 78 | + { |
| 79 | + type: "message", |
| 80 | + role: "user", |
| 81 | + content: [ |
| 82 | + { |
| 83 | + type: "input_text", |
| 84 | + text: payload.query, |
| 85 | + // text: "go to https://news.ycombinator.com , open top article , describe the target website design (in yaml format)" |
| 86 | + }, |
| 87 | + ], |
| 88 | + }, |
| 89 | + ], |
| 90 | + true, // print_steps |
| 91 | + true, // debug |
| 92 | + false, // show_images |
| 93 | + ); |
| 94 | + |
| 95 | + console.log("> agent run done"); |
| 96 | + |
| 97 | + const endTime = Date.now(); |
| 98 | + const timeElapsed = (endTime - startTime) / 1000; // Convert to seconds |
| 99 | + |
| 100 | + return { |
| 101 | + // response, // full messages stack trace |
| 102 | + elapsed: parseFloat(timeElapsed.toFixed(2)), |
| 103 | + answer: response?.slice(-1)?.[0]?.content?.[0]?.text ?? null, |
| 104 | + }; |
| 105 | + } finally { |
| 106 | + // Note: KernelPlaywrightComputer handles browser cleanup internally |
| 107 | + // No need to manually close browser here |
| 108 | + } |
| 109 | + }, |
| 110 | +); |
0 commit comments