toolsearch-runtime-driver.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { fork } from "node:child_process";
  2. import { once } from "node:events";
  3. import { createRequire } from "node:module";
  4. import { join, resolve } from "node:path";
  5. import { pathToFileURL } from "node:url";
  6. import type { createToolSearchCliFixture } from "./toolsearch-cli-process.js";
  7. export async function startToolSearchRuntimeDriver(
  8. fixture: Awaited<ReturnType<typeof createToolSearchCliFixture>>,
  9. streaming: "on" | "off",
  10. options: { mode?: "native" | "local"; recording?: boolean; autoCompact?: boolean } = {},
  11. ) {
  12. // Windows 的 --import 不接受裸盘符路径,必须传 file URL。
  13. const tsx = pathToFileURL(createRequire(join(process.cwd(), "package.json")).resolve("tsx")).href;
  14. const child = fork(
  15. resolve("test/e2e/fixtures/toolsearch/runtime-driver.mjs"),
  16. [streaming, JSON.stringify(options)],
  17. {
  18. cwd: fixture.workspace,
  19. env: fixture.env,
  20. execArgv: ["--import", tsx],
  21. stdio: ["ignore", "pipe", "pipe", "ipc"],
  22. },
  23. );
  24. let stderr = "";
  25. child.stderr?.on("data", (chunk: Buffer) => {
  26. stderr += chunk.toString();
  27. });
  28. child.stdout?.resume();
  29. const pending = new Map<number, { resolve(value: unknown): void; reject(error: Error): void }>();
  30. let nextId = 0;
  31. let resolveReady: () => void;
  32. let rejectReady: (error: Error) => void;
  33. const ready = new Promise<void>((resolve, reject) => {
  34. resolveReady = resolve;
  35. rejectReady = reject;
  36. });
  37. child.on(
  38. "message",
  39. (message: { ready?: boolean; id: number; value?: unknown; error?: string }) => {
  40. if (message.ready) {
  41. resolveReady();
  42. return;
  43. }
  44. const waiter = pending.get(message.id);
  45. pending.delete(message.id);
  46. if (message.error) waiter?.reject(new Error(message.error));
  47. else waiter?.resolve(message.value);
  48. },
  49. );
  50. const closed = once(child, "close");
  51. child.on("close", () => {
  52. const error = new Error(`ToolSearch runtime driver exited: ${stderr}`);
  53. rejectReady(error);
  54. for (const waiter of pending.values()) waiter.reject(error);
  55. pending.clear();
  56. });
  57. const timer = setTimeout(() => {
  58. child.kill();
  59. }, 90_000);
  60. await ready.catch(async (error) => {
  61. clearTimeout(timer);
  62. await fixture.close();
  63. throw error;
  64. });
  65. function command(command: string, values: Record<string, string> = {}): Promise<unknown> {
  66. const id = nextId++;
  67. return new Promise((resolve, reject) => {
  68. pending.set(id, { resolve, reject });
  69. child.send({ id, command, ...values });
  70. });
  71. }
  72. return {
  73. turn: (prompt: string) => command("turn", { prompt }),
  74. catalog: (name: string, state: "absent" | "resident" | "deferred") =>
  75. command("catalog", { name, state }),
  76. history: () => command("history"),
  77. records: () => command("records"),
  78. async close() {
  79. try {
  80. if (child.connected) await command("close");
  81. } finally {
  82. clearTimeout(timer);
  83. if (child.connected) child.kill();
  84. await closed;
  85. }
  86. },
  87. };
  88. }