computer-use-onboarding.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, expect, it, vi } from "vitest";
  2. /**
  3. * CUA 的输出策略:参考按需取用,宿主不主动推。
  4. *
  5. * Bug(2026-09-11 真机):首次 CUA 调用前 SDK 会把 docs/computer-use.md 自动写进输出,
  6. * 而模型刚通过 Skill 工具拿到大面积重合的 SKILL——同一份内容进上下文两遍(13.1KB +
  7. * 13.8KB,小节重合过半)。口径改成与 Browser Use 一致:文档由模型自己
  8. * `nodeRepl.write(await agent.documentation.get("computer-use"))` 取。
  9. *
  10. * 观察结果仍由 SDK 自动展示(`Target`):v1 曾把它写成 SKILL 里的纪律,
  11. * 模型忘写就整段观察丢失,那次回退的结论保留在 computer-use-client.mjs 的注释里。
  12. */
  13. const WRITTEN: string[] = [];
  14. async function makeGlobals(bridgeOverrides: Record<string, unknown> = {}) {
  15. const { join, dirname } = await import("node:path");
  16. const { fileURLToPath } = await import("node:url");
  17. const clientUrl = fileURLToPath(new URL("../../zcode-cua-plugin/scripts/computer-use-client.mjs", import.meta.url));
  18. const documentationRoot = join(dirname(clientUrl), "..", "docs");
  19. const bridge = {
  20. call: vi.fn(async () => ({ content: [{ type: "text", text: "ok" }] })),
  21. assertAvailable: () => {},
  22. documentationRoot,
  23. ...bridgeOverrides,
  24. };
  25. const globals = {
  26. [Symbol.for("zcode.node-repl.computer-use-bridge")]: bridge,
  27. nodeRepl: { write: (text: string) => { WRITTEN.push(text); } },
  28. };
  29. const mod = await import(clientUrl);
  30. await mod.setupComputerUseRuntime({ globals });
  31. return { globals, bridge, mod };
  32. }
  33. const referenceOf = (writes: readonly string[]) =>
  34. writes.find((text) => text.startsWith("# Computer Use"));
  35. describe("computer-use output policy", () => {
  36. it("never pushes the reference, on the first call or any later one", async () => {
  37. WRITTEN.length = 0;
  38. const { globals } = await makeGlobals();
  39. await globals.agent.computerUse.computer.list_apps({});
  40. expect(referenceOf(WRITTEN)).toBeUndefined();
  41. await globals.agent.computerUse.computer.list_apps({});
  42. expect(referenceOf(WRITTEN)).toBeUndefined();
  43. });
  44. it("serves the reference through agent.documentation for the model to write", async () => {
  45. const { globals } = await makeGlobals();
  46. const doc = await (
  47. globals.agent as { documentation: { get: (name: string) => Promise<string> } }
  48. ).documentation.get("computer-use");
  49. expect(doc).toMatch(/^# Computer Use/u);
  50. // 按需取用的那份仍是完整参考:模型需要参数形状时才付这份预算。
  51. expect(doc).toMatch(/## Tool arguments/u);
  52. });
  53. });