@finalai/sandbox-router-agent-provider 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/router-computer-use.ts"],
4
+ "sourcesContent": ["import { InMemoryTransport, parseJSONRPCMessage } from '@modelcontextprotocol/client';\nimport { McpServer } from '@modelcontextprotocol/server';\nimport { jsonValueSchema, type JsonValue } from '@finalai/agent-provider';\nimport type { McpChannelEndpointIface, McpChannelIface } from '@finalai/agent-provider/sandbox';\nimport { bytesToBase64 } from '@finalai/agent-provider/sandbox/bytes';\nimport { DesktopState, type SandboxHandleIface } from '@finalai/sandbox-router';\nimport * as z from 'zod';\n\n// Computer use through the router's provider-neutral API (capabilities `desktop` and\n// `computerUse`): an MCP server in this process whose tools take screenshots of the sandbox's\n// desktop and send it mouse and keyboard input through the router, so the same agent drives an\n// E2B, Daytona or Final desktop alike. The desktop is started on first use when it is stopped.\n\n// The tools it serves, as the agent sees them; coordinates are screenshot pixels.\nexport enum RouterComputerUseTool {\n Screenshot = 'screenshot',\n Click = 'click',\n Move = 'move',\n Drag = 'drag',\n Type = 'type',\n Key = 'key',\n Scroll = 'scroll',\n}\n\nconst READY_WAIT_MS = 120_000;\nconst READY_POLL_MS = 500;\n// How long one `receive` waits for a message before answering none.\nconst RECEIVE_WAIT_MS = 25_000;\n\nconst sleep = (ms: number, signal?: AbortSignal) =>\n new Promise<void>((resolve, reject) => {\n const timer = setTimeout(resolve, ms);\n signal?.addEventListener(\n 'abort',\n () => {\n clearTimeout(timer);\n reject(signal.reason);\n },\n { once: true },\n );\n });\n\n// Starts the desktop when it is stopped and waits for it to run.\nexport const ensureDesktopRunning = async (\n sandbox: SandboxHandleIface,\n signal?: AbortSignal,\n): Promise<void> => {\n const deadline = Date.now() + READY_WAIT_MS;\n let status = await sandbox.desktop.status();\n if (status.state === DesktopState.Stopped) status = await sandbox.desktop.start();\n for (;;) {\n if (status.state === DesktopState.Running) return;\n if (status.state !== DesktopState.Starting)\n throw new Error(\n `the sandbox's desktop is ${status.state}${status.error == null ? '' : `: ${status.error}`}`,\n );\n if (Date.now() >= deadline)\n throw new Error(\n `the desktop did not start within ${Math.round(READY_WAIT_MS / 1000)} seconds`,\n );\n await sleep(READY_POLL_MS, signal);\n status = await sandbox.desktop.status();\n }\n};\n\nconst point = {\n x: z.number().describe('Pixels from the left'),\n y: z.number().describe('Pixels from the top'),\n};\n\nconst done = (text: string) => ({ content: [{ type: 'text' as const, text }] });\n\nconst registerTools = (server: McpServer, sandbox: SandboxHandleIface): void => {\n server.registerTool(\n RouterComputerUseTool.Screenshot,\n {\n description:\n 'A PNG of the desktop. Every other tool takes coordinates in this image\u2019s pixels.',\n inputSchema: z.object({}),\n },\n async () => ({\n content: [\n {\n type: 'image' as const,\n data: bytesToBase64(await sandbox.computerUse.screenshot()),\n mimeType: 'image/png',\n },\n ],\n }),\n );\n server.registerTool(\n RouterComputerUseTool.Click,\n {\n description: 'Clicks at a point.',\n inputSchema: z.object({\n ...point,\n button: z.enum(['left', 'right', 'middle']).optional(),\n double: z.boolean().optional(),\n }),\n },\n async (input) => {\n await sandbox.computerUse.act({ type: 'click', ...input });\n return done(`clicked at ${input.x},${input.y}`);\n },\n );\n server.registerTool(\n RouterComputerUseTool.Move,\n { description: 'Moves the pointer to a point.', inputSchema: z.object(point) },\n async (input) => {\n await sandbox.computerUse.act({ type: 'move', ...input });\n return done(`moved to ${input.x},${input.y}`);\n },\n );\n server.registerTool(\n RouterComputerUseTool.Drag,\n {\n description:\n 'Presses the left button at the first point, moves through the rest and releases.',\n inputSchema: z.object({ path: z.array(z.object(point)).min(2) }),\n },\n async (input) => {\n await sandbox.computerUse.act({ type: 'drag', path: input.path });\n return done(`dragged through ${input.path.length} points`);\n },\n );\n server.registerTool(\n RouterComputerUseTool.Type,\n { description: 'Types text.', inputSchema: z.object({ text: z.string() }) },\n async (input) => {\n await sandbox.computerUse.act({ type: 'type', text: input.text });\n return done(`typed ${input.text.length} characters`);\n },\n );\n server.registerTool(\n RouterComputerUseTool.Key,\n {\n description: 'Presses keys together, as a chord (`[\"ctrl\", \"c\"]`), then releases them.',\n inputSchema: z.object({ keys: z.array(z.string().min(1)).min(1) }),\n },\n async (input) => {\n await sandbox.computerUse.act({ type: 'key', keys: input.keys });\n return done(`pressed ${input.keys.join('+')}`);\n },\n );\n server.registerTool(\n RouterComputerUseTool.Scroll,\n {\n description: 'Scrolls at a point by dx and dy pixels.',\n inputSchema: z.object({ ...point, dx: z.number(), dy: z.number() }),\n },\n async (input) => {\n await sandbox.computerUse.act({ type: 'scroll', ...input });\n return done(`scrolled by ${input.dx},${input.dy}`);\n },\n );\n};\n\nexport interface RouterComputerUseEndpointIface {\n endpoint: McpChannelEndpointIface;\n close(): Promise<void>;\n}\n\nexport const createRouterComputerUseEndpoint = (args: {\n sandbox: SandboxHandleIface;\n}): RouterComputerUseEndpointIface => {\n let closed = false;\n const channels = new Set<McpChannelIface>();\n const connect = async (options?: { abortSignal?: AbortSignal }): Promise<McpChannelIface> => {\n if (closed) throw new Error('computer-use endpoint is closed');\n await ensureDesktopRunning(args.sandbox, options?.abortSignal);\n options?.abortSignal?.throwIfAborted();\n const server = new McpServer({ name: 'computer_use', version: '1' });\n registerTools(server, args.sandbox);\n const [ours, theirs] = InMemoryTransport.createLinkedPair();\n await server.connect(theirs);\n const inbox: JsonValue[] = [];\n const waiters = new Set<() => void>();\n const wake = () => {\n for (const waiter of waiters) waiter();\n waiters.clear();\n };\n ours.onmessage = (message) => {\n inbox.push(jsonValueSchema.parse(JSON.parse(JSON.stringify(message))));\n wake();\n };\n await ours.start();\n let open = true;\n const channel: McpChannelIface = {\n send: async (message) => {\n if (!open) throw new Error('computer-use channel is closed');\n await ours.send(parseJSONRPCMessage(message));\n },\n receive: async (receiveOptions) => {\n if (inbox.length === 0 && open)\n await new Promise<void>((resolve) => {\n const finish = (): void => {\n clearTimeout(timer);\n waiters.delete(finish);\n receiveOptions?.abortSignal?.removeEventListener('abort', finish);\n resolve();\n };\n const timer = setTimeout(finish, RECEIVE_WAIT_MS);\n waiters.add(finish);\n receiveOptions?.abortSignal?.addEventListener('abort', finish, { once: true });\n });\n receiveOptions?.abortSignal?.throwIfAborted();\n return inbox.splice(0);\n },\n close: async () => {\n if (!open) return;\n open = false;\n channels.delete(channel);\n wake();\n await ours.close();\n await server.close();\n },\n };\n if (closed) {\n await channel.close();\n throw new Error('computer-use endpoint is closed');\n }\n channels.add(channel);\n return channel;\n };\n return {\n endpoint: { name: 'computer_use', connect },\n close: async () => {\n closed = true;\n const results = await Promise.allSettled([...channels].map((channel) => channel.close()));\n channels.clear();\n for (const result of results) if (result.status === 'rejected') throw result.reason;\n },\n };\n};\n"],
5
+ "mappings": "AAAA,SAAS,mBAAmB,2BAA2B;AACvD,SAAS,iBAAiB;AAC1B,SAAS,uBAAuC;AAEhD,SAAS,qBAAqB;AAC9B,SAAS,oBAA6C;AACtD,YAAY,OAAO;AAQZ,IAAK,wBAAL,kBAAKA,2BAAL;AACL,EAAAA,uBAAA,gBAAa;AACb,EAAAA,uBAAA,WAAQ;AACR,EAAAA,uBAAA,UAAO;AACP,EAAAA,uBAAA,UAAO;AACP,EAAAA,uBAAA,UAAO;AACP,EAAAA,uBAAA,SAAM;AACN,EAAAA,uBAAA,YAAS;AAPC,SAAAA;AAAA,GAAA;AAUZ,MAAM,gBAAgB;AACtB,MAAM,gBAAgB;AAEtB,MAAM,kBAAkB;AAExB,MAAM,QAAQ,CAAC,IAAY,WACzB,IAAI,QAAc,CAAC,SAAS,WAAW;AACrC,QAAM,QAAQ,WAAW,SAAS,EAAE;AACpC,UAAQ;AAAA,IACN;AAAA,IACA,MAAM;AACJ,mBAAa,KAAK;AAClB,aAAO,OAAO,MAAM;AAAA,IACtB;AAAA,IACA,EAAE,MAAM,KAAK;AAAA,EACf;AACF,CAAC;AAGI,MAAM,uBAAuB,OAClC,SACA,WAC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C;AACxF,eAAS,MAAM;AACf,iBAAW,UAAU,QAAS,KAAI,OAAO,WAAW,WAAY,OAAM,OAAO;AAAA,IAC/E;AAAA,EACF;AACF;",
6
+ "names": ["RouterComputerUseTool"]
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@finalai/sandbox-router-agent-provider",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "private": false,
5
5
  "description": "A sandbox provider for the agent stack over the Sandbox Router: agent sandboxes on any provider the router reaches, implementing the public sandbox provider contract.",
6
6
  "license": "Apache-2.0",
@@ -18,6 +18,11 @@
18
18
  "import": "./dist/modules/index.mjs",
19
19
  "default": "./dist/modules/index.mjs"
20
20
  },
21
+ "./environment": {
22
+ "types": "./dist/modules/environment.d.mts",
23
+ "import": "./dist/modules/environment.mjs",
24
+ "default": "./dist/modules/environment.mjs"
25
+ },
21
26
  "./package.json": "./package.json"
22
27
  },
23
28
  "publishConfig": {
@@ -25,9 +30,11 @@
25
30
  "registry": "https://registry.npmjs.org/"
26
31
  },
27
32
  "dependencies": {
28
- "@finalai/agent-protocol": "0.23.3",
29
- "@finalai/agent-provider": "0.20.3",
30
- "@finalai/sandbox-router": "0.2.3",
33
+ "@finalai/agent-protocol": "0.24.0",
34
+ "@finalai/agent-provider": "0.21.0",
35
+ "@finalai/sandbox-router": "0.3.0",
36
+ "@modelcontextprotocol/client": "^2.1.0",
37
+ "@modelcontextprotocol/server": "^2.4.0",
31
38
  "zod": "4.6.6"
32
39
  },
33
40
  "engines": {