session-ready-main.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { app, protocol } from "electron";
  2. import { mkdir, writeFile } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { PLUGIN_SANDBOX_PRIVILEGED_SCHEME } from "../../src/main/pluginSandbox/index.js";
  5. import { managedEnvironment } from "./managed-environment.js";
  6. import { checkSessionReady } from "./managed-late-checks.js";
  7. const [root, profile, node, cli] = process.argv.slice(2) as [string, string, string, string];
  8. app.setPath("userData", profile);
  9. protocol.registerSchemesAsPrivileged([PLUGIN_SANDBOX_PRIVILEGED_SCHEME]);
  10. app.on("window-all-closed", () => {});
  11. app
  12. .whenReady()
  13. .then(async () => {
  14. // 单例回归只使用显式注入的 MCP;默认内置插件会引入认证和网络等待,污染恢复时序。
  15. const configDir = join(root, "agent-data/.zcode/cli");
  16. await mkdir(configDir, { recursive: true });
  17. await writeFile(
  18. join(configDir, "config.json"),
  19. JSON.stringify({ plugins: { enabled: false } }),
  20. );
  21. const env = await managedEnvironment(root, node, cli);
  22. const checks: { name: string; ms: number }[] = [];
  23. try {
  24. await checkSessionReady(env, root, async (name, run) => {
  25. const start = performance.now();
  26. await run();
  27. checks.push({ name, ms: performance.now() - start });
  28. });
  29. await writeFile(
  30. join(root, "session-ready-results.json"),
  31. JSON.stringify(
  32. {
  33. caseId: "PLM-UI-SESSION-READY",
  34. checks,
  35. },
  36. null,
  37. 2,
  38. ),
  39. );
  40. } finally {
  41. await writeFile(
  42. join(root, "session-ready-events.json"),
  43. JSON.stringify(
  44. {
  45. phases: env.events.history.filter((e) => e.type === "phase"),
  46. lifecycle: env.agent.notifications.history.filter(
  47. (e) => e.method === "session/lifecycle/changed",
  48. ),
  49. mcp: env.agent.events.history,
  50. },
  51. null,
  52. 2,
  53. ),
  54. );
  55. await env.close();
  56. }
  57. app.exit(0);
  58. })
  59. .catch((error) => {
  60. process.stderr.write(String(error.stack ?? error) + "\n");
  61. app.exit(1);
  62. });