provider-migration-unrelated-tables.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import type { DatabaseSync, SQLInputValue } from "node:sqlite";
  2. // 独立于新版 Repo 写入,播种合法原始行;迁移不得改变这些表的任一列。
  3. export function seedUnrelatedProviderTables(db: DatabaseSync, sessionId: string): string[] {
  4. const json = JSON.stringify({
  5. keep: "正文/附件引用",
  6. model: "builtin:zai/legacy",
  7. file: "/asset/a",
  8. });
  9. const rows: Record<string, Record<string, SQLInputValue>> = {
  10. todo: {
  11. session_id: sessionId,
  12. content: "待办正文",
  13. status: "pending",
  14. priority: "high",
  15. position: 0,
  16. },
  17. permission: { project_id: "project", data: json },
  18. input_history: {
  19. id: "history",
  20. project_id: "project",
  21. session_id: sessionId,
  22. text: "输入正文",
  23. kind: "prompt",
  24. },
  25. local_setting: {
  26. scope: "user",
  27. scope_id: "default",
  28. namespace: "model",
  29. key: "reasoningLevel",
  30. value: '"high"',
  31. schema_version: 1,
  32. },
  33. session_target: {
  34. session_id: sessionId,
  35. target_id: "goal",
  36. objective: "目标正文",
  37. status: "active",
  38. },
  39. workflow_definition: {
  40. id: "definition",
  41. name: "workflow",
  42. source: "user",
  43. script_hash: "sha",
  44. meta_json: json,
  45. },
  46. workflow_run: {
  47. id: "run",
  48. definition_id: "definition",
  49. name: "workflow",
  50. parent_session_id: sessionId,
  51. cwd: "/ws",
  52. script_hash: "sha",
  53. args_json: json,
  54. status: "running",
  55. },
  56. workflow_activity: {
  57. id: "activity",
  58. run_id: "run",
  59. call_index: 1,
  60. call_path: "ask",
  61. type: "ask",
  62. input_hash: "sha",
  63. prompt: "工作流正文",
  64. opts_json: json,
  65. status: "completed",
  66. },
  67. workflow_event: {
  68. id: "event",
  69. run_id: "run",
  70. sequence: 1,
  71. type: "completed",
  72. activity_id: "activity",
  73. payload_json: json,
  74. },
  75. session_task_link: {
  76. id: "link",
  77. root_workflow_run_id: "run",
  78. activity_id: "activity",
  79. child_session_id: sessionId,
  80. role: "child",
  81. path: "1",
  82. model: "builtin:zai/legacy",
  83. status: "completed",
  84. },
  85. model_usage: {
  86. id: "usage",
  87. logical_request_id: "request",
  88. session_id: sessionId,
  89. query_source: "agent",
  90. provider_id: "builtin:zai",
  91. model_id: "legacy",
  92. variant: "high",
  93. status: "completed",
  94. started_at: 1,
  95. raw_usage_json: json,
  96. },
  97. turn_usage: { session_id: sessionId, turn_id: "turn", status: "completed", started_at: 1 },
  98. tool_usage: {
  99. id: "tool",
  100. session_id: sessionId,
  101. tool_call_id: "call",
  102. tool_name: "Read",
  103. status: "completed",
  104. started_at: 1,
  105. },
  106. session_input: {
  107. id: "input",
  108. session_id: sessionId,
  109. kind: "prompt",
  110. delivery: "queue",
  111. payload: json,
  112. admitted_sequence: 1,
  113. status: "admitted",
  114. },
  115. dwf_run: {
  116. id: "dwf",
  117. parent_session_id: sessionId,
  118. caps_max_concurrency: 1,
  119. status: "running",
  120. args_json: json,
  121. },
  122. dwf_actor: {
  123. run_id: "dwf",
  124. site_id: "actor",
  125. ordinal: 1,
  126. resolved_model: json,
  127. persona_json: json,
  128. },
  129. dwf_node: {
  130. run_id: "dwf",
  131. site_id: "node",
  132. ordinal: 1,
  133. kind: "ask",
  134. input_hash: "sha",
  135. input_json: json,
  136. status: "completed",
  137. },
  138. dwf_event: { run_id: "dwf", sequence: 1, type: "completed", payload_json: json },
  139. };
  140. for (const [table, input] of Object.entries(rows)) {
  141. const columns = db.prepare(`PRAGMA table_info(${table})`).all();
  142. for (const column of columns) {
  143. const name = String(column.name);
  144. if (
  145. name.startsWith("time_") &&
  146. column.notnull &&
  147. column.dflt_value === null &&
  148. !(name in input)
  149. )
  150. input[name] = 12;
  151. }
  152. const keys = Object.keys(input);
  153. db.prepare(
  154. `INSERT INTO ${table}(${keys.join(",")}) VALUES(${keys.map(() => "?").join(",")})`,
  155. ).run(...Object.values(input));
  156. }
  157. return Object.keys(rows);
  158. }