session-pane-retention-fixture.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { mkdir, writeFile } from "node:fs/promises";
  2. import { join, resolve } from "node:path";
  3. import { homedir } from "node:os";
  4. import { DatabaseSync } from "node:sqlite";
  5. import { seedProviderReadinessHistoryFixture } from "./provider-readiness-history-fixture.js";
  6. import {
  7. seedPersonalProviderConfig,
  8. seedPersonalDefaultModelSelection,
  9. } from "./model-provider-restart-config.js";
  10. /** 在启动 App 前准备隔离历史;工具仅作为已完成的持久记录,不实际执行或调用模型。 */
  11. export async function seedSessionPaneRetentionFixture(options: {
  12. e2eHomeDir: string;
  13. cliEntryPoint: string;
  14. crossWorkspace?: boolean;
  15. }) {
  16. const home = resolve(options.e2eHomeDir);
  17. if (home === resolve(homedir())) throw new Error("不能向用户 HOME 写入合成历史");
  18. const workspacePath = join(home, "Workspace");
  19. await mkdir(workspacePath, { recursive: true });
  20. await mkdir(join(home, ".zcode/v2"), { recursive: true });
  21. const workspaceFor = (index: number) =>
  22. options.crossWorkspace ? join(home, `Workspace-${index}`) : workspacePath;
  23. for (let index = 0; index < 8; index++) {
  24. await mkdir(workspaceFor(index), { recursive: true });
  25. await seedProviderReadinessHistoryFixture({
  26. ...options,
  27. workspacePath: workspaceFor(index),
  28. history: {
  29. sessionId: `sess_probe_retention_${index}`,
  30. title: `Retention Probe ${index}`,
  31. userText: `RETENTION_USER_${index} Inspect the synthetic test file.`,
  32. assistantText: `RETENTION_REPLY_${index}\n\n${`Synthetic reply for session ${index}. This fixture tests retained history.\n`.repeat(160)}`,
  33. assistantInputTokens: 100,
  34. assistantOutputTokens: 100,
  35. modelId: "probe-model",
  36. runtimeProviderId: "probe-local",
  37. taskProviderId: "glm",
  38. },
  39. });
  40. }
  41. // node:sqlite 只有同步 API;此处仅离线播种测试 fixture,不在产品 IO 路径执行。
  42. const db = new DatabaseSync(join(home, ".zcode/cli/db/db.sqlite"));
  43. try {
  44. for (let index = 0; index < 8; index++) {
  45. const sessionId = `sess_probe_retention_${index}`;
  46. const at = Date.now() - 30_000;
  47. const content = `RETENTION_PAYLOAD_${index}\n${Array.from(
  48. { length: 2800 },
  49. (_, line) =>
  50. `line ${line} session ${index}: diagnostic synthetic content with a stable unique value.`,
  51. ).join("\n")}`;
  52. const part = {
  53. type: "tool",
  54. callID: `probe_call_${index}`,
  55. tool: "Write",
  56. state: {
  57. status: "completed",
  58. input: { file_path: join(workspaceFor(index), `fixture-${index}.txt`), content },
  59. output: `Synthetic write completed for session ${index}.`,
  60. title: "Write",
  61. time: { start: at, end: at + 10 },
  62. metadata: { fixture: true },
  63. },
  64. time: { start: at, end: at + 10 },
  65. };
  66. db.prepare(
  67. "INSERT INTO part(id,message_id,session_id,time_created,time_updated,data) VALUES(?,?,?,?,?,?)",
  68. ).run(
  69. `part_tool_${sessionId}`,
  70. `msg_assistant_${sessionId}`,
  71. sessionId,
  72. at,
  73. at + 10,
  74. JSON.stringify(part),
  75. );
  76. db.prepare(
  77. "INSERT INTO session_entry(id,session_id,type,time_created,time_updated,data) VALUES(?,?,?,?,?,?)",
  78. ).run(
  79. `model_${sessionId}`,
  80. sessionId,
  81. "runtime/model_selection",
  82. at,
  83. at,
  84. JSON.stringify({ modelSelection: { providerId: "probe-local", modelId: "probe-model" } }),
  85. );
  86. }
  87. } finally {
  88. db.close();
  89. }
  90. const providerFile = join(home, ".zcode/v2/provider_config.json");
  91. await seedPersonalProviderConfig(providerFile, {
  92. id: "probe-local",
  93. label: "Retention Local Fixture",
  94. apiFormat: "openai-chat-completions",
  95. baseURL: "http://127.0.0.1:19326/v1",
  96. apiKey: "synthetic-no-remote-access",
  97. enabled: true,
  98. models: [{ id: "probe-model", contextWindow: 200_000, maxOutputTokens: 4096 }],
  99. });
  100. await seedPersonalDefaultModelSelection(providerFile, {
  101. providerId: "probe-local",
  102. modelId: "probe-model",
  103. });
  104. await writeFile(
  105. join(home, ".zcode/v2/setting.json"),
  106. JSON.stringify({
  107. providerFamilyDomain: "zai",
  108. providerFamilyDomainMigrated: true,
  109. providerFamilyDomainUpdatedAt: Date.now(),
  110. providerFamilyConnectionSelections: {},
  111. recentProjects: [workspacePath],
  112. lastWorkspaceSession: [{ kind: "local", workspacePath, workspacePurpose: "project" }],
  113. lastActiveTabIndex: 0,
  114. }),
  115. );
  116. await writeFile(join(home, ".zcode/v2/credentials.json"), "{}");
  117. return { home, workspacePath, sessions: 8, messages: 16, parts: 24 };
  118. }