storageMigration.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {
  2. decodeCustomModelValue,
  3. migrateLegacyModelProviderId,
  4. migrateLegacyOfficialGlmModelId,
  5. modelSelectionSchema,
  6. ZCODE_AGENT_PROVIDER,
  7. type ModelSelection,
  8. } from "@zcode/shared";
  9. import { normalizeBotCurrentOptions, normalizeBotDraftOptions } from "./config.js";
  10. function isRecord(value: unknown): value is Record<string, unknown> {
  11. return typeof value === "object" && value !== null && !Array.isArray(value);
  12. }
  13. /** 只在 v3 文件不存在时调用;不是当前 Bot Options 的兼容读取器。 */
  14. function migrateSelection(options: Record<string, unknown>): ModelSelection | undefined {
  15. if (Object.hasOwn(options, "modelSelection")) {
  16. // Bug 根因:旧 thoughtLevel 曾覆盖已保存的新档位;新字段存在时禁止回读旧字段。
  17. const parsed = modelSelectionSchema.safeParse(options.modelSelection);
  18. if (!parsed.success) return undefined;
  19. const selection = parsed.data;
  20. if (!selection.providerId.startsWith("builtin:")) return selection;
  21. const providerId = migrateLegacyModelProviderId(selection.providerId);
  22. return providerId
  23. ? {
  24. ...selection,
  25. providerId,
  26. modelId: migrateLegacyOfficialGlmModelId(selection.providerId, selection.modelId),
  27. }
  28. : undefined;
  29. }
  30. const value = typeof options.model === "string" ? options.model.trim() : "";
  31. const custom = decodeCustomModelValue(value);
  32. const separator = value.indexOf("/");
  33. const oldProviderId =
  34. custom?.providerId ?? (separator > 0 ? value.slice(0, separator) : undefined);
  35. const modelId = custom?.modelName ?? (separator > 0 ? value.slice(separator + 1) : undefined);
  36. if (!oldProviderId || !modelId) return undefined;
  37. const providerId = migrateLegacyModelProviderId(oldProviderId);
  38. // 仅接受明确身份,不再按模型名唯一匹配其他供应商,也不把裸模型名解释为 Agent Provider。
  39. // Bug 根因:候选为空时曾把明确旧选择永久写空到 v3;迁移只搬意图,不能检查当前可用性。
  40. if (!providerId) return undefined;
  41. const reasoningLevel =
  42. typeof options.thoughtLevel === "string" ? options.thoughtLevel.trim() : "";
  43. return {
  44. providerId,
  45. modelId: migrateLegacyOfficialGlmModelId(oldProviderId, modelId),
  46. ...(reasoningLevel ? { options: { reasoningLevel } } : {}),
  47. };
  48. }
  49. export function importLegacyBotConfig(value: unknown): unknown {
  50. if (!isRecord(value) || !Array.isArray(value.bots)) return value;
  51. return {
  52. ...value,
  53. version: 3,
  54. bots: value.bots.map((bot) => {
  55. if (!isRecord(bot)) return bot;
  56. const options = isRecord(bot.currentOptions) ? bot.currentOptions : {};
  57. return {
  58. ...bot,
  59. currentOptions: normalizeBotCurrentOptions({
  60. ...options,
  61. modelSelection: migrateSelection(options),
  62. }),
  63. };
  64. }),
  65. };
  66. }
  67. export function importLegacyBotState(value: unknown): unknown {
  68. if (!isRecord(value) || !isRecord(value.bots)) return value;
  69. const bots = Object.entries(value.bots).map(([id, state]) => {
  70. if (!isRecord(state) || !isRecord(state.draftOptions)) return [id, state];
  71. const options = state.draftOptions;
  72. return [
  73. id,
  74. {
  75. ...state,
  76. draftOptions: normalizeBotDraftOptions({
  77. provider: ZCODE_AGENT_PROVIDER,
  78. modelSelection: migrateSelection(options),
  79. ...(typeof options.mode === "string" ? { mode: options.mode } : {}),
  80. }),
  81. },
  82. ];
  83. });
  84. return { ...value, version: 3, bots: Object.fromEntries(bots) };
  85. }