fix(cua): preserve the installed PTC bridge initialization path

This commit is contained in:
turtle1999 committed 2026-11-09 19:03:14 +08:00
1 parent e4a2972627
commit 5dbc29e64e
4 files changed
+11 -7

No files matched your search

@@ -4,7 +4,7 @@ import { existsSync } from 'node:fs'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { fileURLToPath } from 'node:url'
import { Context } from '@deepseek-ai/cordis'
import Loader from '@deepseek-ai/cordis-plugin-loader'
import { mountAgentLoopTestDependencies, mountAgentLoopTestHarness } from '@deepseek-ai/dsh-agent-loop-testkit'
@@ -71,7 +71,9 @@ it.skipIf(process.platform === 'win32' || !existsSync(bridge) || !existsSync(boo
} } }],
}))
expect((await run('await import("@deepseek-ai/dsh-cua/bridge")', 'unresolved')).error?.message).toContain('Cannot find package')
const initialization = `var { connectCua } = await import(${JSON.stringify(pathToFileURL(bridge).href)});\nvar cua = connectCua(() => tools.cua, text => console.log(text));`
const section = (await ctx.systemPrompt.assemble()).sections.find(value => value.name === 'computer-use:dsh-cua')
const initialization = section?.text.match(/Persistent PTC initialization:\n\n```ts\n([\s\S]*?)\n```/u)?.[1]
expect(initialization).toContain('/lib/bridge.js')
const first = await run(`${initialization}\nvar app = await cua.getApp("test.fixture"); var win = await app.getWindow(7); await win.click("0"); var shot = await win.getScreenshot(); ({ mimeType: shot.mimeType, pixelWidth: shot.pixelWidth, pixelHeight: shot.pixelHeight, windowId: shot.windowId, byteLength: shot.bytes.length });`, 'first')
expect(first.error).toBeUndefined()
expect(first.value).toEqual({ mimeType: 'image/png', pixelWidth: 1, pixelHeight: 1, windowId: 7, byteLength: 90 })
@@ -901,7 +901,7 @@ it('adds an installation-owned bridge URL when persistent PTC is available', asy
await f.ctx.plugin(Sandbox)
await f.ctx.plugin(SandboxPolicy)
await f.ctx.plugin(NodeRuntime, { executionMode: 'persistent-in-memory' })
expect(await text()).toContain(new URL('../../cua/src/bridge.ts', import.meta.url).href)
expect(await text()).toContain(new URL('../../cua/lib/bridge.js', import.meta.url).href)
})
it('keeps Cordis loading until preparation succeeds and reports preparation failure through the fiber', async () => {
@@ -28,7 +28,9 @@ const programDescription = 'Run a TypeScript program to observe and operate desk
export function installCuaTools(
ctx: Context, config: CuaConfig, tool: ToolDefinition, guidance: string, programGuidance: string,
): () => void | Promise<void> {
const bridgeUrl = JSON.stringify(new URL(`./bridge${extname(new URL(import.meta.url).pathname)}`, import.meta.url).href)
// PTC publishes the installed JS entry; automatic CUA initialization follows the Host's code format.
const ptcBridgeUrl = JSON.stringify(new URL('../lib/bridge.js', import.meta.url).href)
const programBridgeUrl = JSON.stringify(new URL(`./bridge${extname(new URL(import.meta.url).pathname)}`, import.meta.url).href)
const prompt = { name: 'computer-use:dsh-cua', order: ctx.systemPrompt.getSectionOrder('TOOL_COMPUTER_USE') }
const rawPrompt: PromptSection = { ...prompt, text: () => {
const runtime = ctx.get('ptcRuntime')
@@ -37,7 +39,7 @@ export function installCuaTools(
Persistent PTC initialization:
\`\`\`ts
var { connectCua } = await import(${bridgeUrl});
var { connectCua } = await import(${ptcBridgeUrl});
var cua = connectCua(() => tools.cua, text => console.log(text));
\`\`\``
} }
@@ -59,7 +61,7 @@ var cua = connectCua(() => tools.cua, text => console.log(text));
const program = createRunCodeTool(consumer, {
name: 'cua_run_code', description: programDescription, runtime: consumer.ptcRuntime,
tools: [tool], limits: config.runCode.executor,
initialization: `globalThis.cua ??= (await import(${bridgeUrl})).connectCua(() => tools.cua, text => console.log(text));`,
initialization: `globalThis.cua ??= (await import(${programBridgeUrl})).connectCua(() => tools.cua, text => console.log(text));`,
})
const scopes = new Map<Agent, { scope: Scope; current?: { kind: 'raw' | 'program'; remove: () => void } }>()
consumer.effect(() => async () => {
@@ -92,7 +92,7 @@ Maximum click holdMs: 5000 ms.
Persistent PTC initialization:
```ts
var { connectCua } = await import("file://{{cwd}}/cua-runtime/src/bridge.ts");
var { connectCua } = await import("file://{{cwd}}/cua-runtime/lib/bridge.js");
var cua = connectCua(() => tools.cua, text => console.log(text));
```