runtime-driver.mjs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {
  2. createZCodeApp,
  3. startProcessProviderRegistryRuntime,
  4. } from "../../../../../../apps/zcode-cli/packages/bootstrap/dist/index.js";
  5. // 当前产品不提供 MCP 热更新。此 driver 只用于 Runtime/Adapter HTTP 集成测试:
  6. // 注入目录变化,不替换模型 adapter、compact、持久化或公告逻辑,不计为 MCP 用户流程 E2E。
  7. const providers = await startProcessProviderRegistryRuntime(process.env);
  8. const options = JSON.parse(process.argv[3] ?? "{}");
  9. const recordingTool = "mcp__catalog__lookup";
  10. const recordedRequests = [];
  11. const app = await createZCodeApp({
  12. providerRegistry: providers.runtime.registryService,
  13. configuredDefaultModelSelection: providers.configuredDefaultModelSelection,
  14. env: process.env,
  15. // 已结束 turn 的内存事件会裁掉 messages;在真实事件到达时取证,不能事后把统计当正文。
  16. eventSink: {
  17. onSessionEvent(event) {
  18. if (event.type === "model_request") recordedRequests.push(structuredClone(event.payload));
  19. },
  20. },
  21. runtimeConfig: {
  22. workingDirectory: process.cwd(),
  23. toolSearch: { mode: options.mode ?? "native" },
  24. modelStreaming: process.argv[2] ?? "on",
  25. titleGeneration: { enabled: false },
  26. toolAllowlist: [
  27. "Read",
  28. "ToolSearch",
  29. "TaskOutput",
  30. "TodoRead",
  31. "DeferredToolCall",
  32. ...(options.recording ? [recordingTool] : []),
  33. ],
  34. ...(options.autoCompact
  35. ? { compact: { contextWindow: 40000, bufferTokens: 10, summaryReserveTokens: 0 } }
  36. : {}),
  37. },
  38. });
  39. // 首次用户执行会初始化 shell 并重建内置工具;先完成该边界,再注入目录场景。
  40. await app.runtime.ensureSessionPersistedForExternalActivity("E2E_TOOLSEARCH_CATALOG_SETUP");
  41. await app.resume();
  42. const registry = app.runtime.getToolRegistry();
  43. const original = new Map(registry.list().map((name) => [name, registry.get(name)]));
  44. process.on("message", async ({ id, command, name, state, prompt }) => {
  45. try {
  46. let value;
  47. if (command === "catalog") {
  48. if (state === "absent") registry.unregister(name);
  49. else {
  50. const entry = original.get(name);
  51. if (!entry) throw new Error(`Unknown test tool: ${name}`);
  52. registry.register(
  53. { ...entry, metadata: { ...entry.metadata, alwaysLoad: state === "resident" } },
  54. { silentDuplicateWarning: true },
  55. );
  56. }
  57. app.runtime.invalidateToolCache();
  58. } else if (command === "turn") {
  59. await app.submitPrompt(prompt);
  60. } else if (command === "history") {
  61. value = app.runtime.messageHistory.toRuntimeEntries();
  62. } else if (command === "records") {
  63. value = recordedRequests;
  64. } else if (command === "close") {
  65. await app.close();
  66. providers.dispose();
  67. process.send({ id, value });
  68. process.disconnect();
  69. return;
  70. } else throw new Error(`Unknown driver command: ${command}`);
  71. process.send({ id, value });
  72. } catch (error) {
  73. process.send({ id, error: error.stack ?? String(error) });
  74. }
  75. });
  76. process.send({ ready: true });