legacyAccountConnectionSettings.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import type {
  2. ProviderFamilyConnectionSelectionSettings,
  3. ProviderFamilyDomain,
  4. } from "@zcode/shared";
  5. import { readFile } from "node:fs/promises";
  6. function record(value: unknown): Record<string, unknown> {
  7. return value && typeof value === "object" && !Array.isArray(value)
  8. ? (value as Record<string, unknown>)
  9. : {};
  10. }
  11. export interface LegacyTeamConnection {
  12. readonly family: ProviderFamilyDomain;
  13. readonly productId: string;
  14. readonly projectId: string;
  15. }
  16. /** 仅迁移器解释旧键;组织信息由 Host 的只读 OAuth 查询注入。退役旧版后删除。 */
  17. export function readIncompleteLegacyTeamConnections(value: unknown): LegacyTeamConnection[] {
  18. if (!needsLegacyAccountConnectionMigration(value)) return [];
  19. const raw = record(value);
  20. const modes = record(raw.modelProviderFamilyModes);
  21. const keys = record(raw.modelProviderFamilySelectedKeys);
  22. return (["zai", "bigmodel"] as const).flatMap((family) => {
  23. if (modes[family] === "apiKey") return [];
  24. const prefix = `team-plan:builtin:${family}-coding-plan:`;
  25. const key = typeof keys[family] === "string" ? keys[family].trim() : "";
  26. if (!key.startsWith(prefix)) return [];
  27. try {
  28. const parts = key
  29. .slice(prefix.length)
  30. .split(":")
  31. .map((part) => decodeURIComponent(part).trim());
  32. if (parts.length !== 2 || parts.some((part) => !part)) return [];
  33. return [{ family, productId: parts[0]!, projectId: parts[1]! }];
  34. } catch {
  35. return [];
  36. }
  37. });
  38. }
  39. export async function readLegacyAccountConnectionSettingsFile(
  40. filePath: string,
  41. ): Promise<Record<string, unknown>> {
  42. try {
  43. return record(JSON.parse(await readFile(filePath, "utf8")));
  44. } catch (error) {
  45. if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
  46. throw error;
  47. }
  48. }
  49. export function needsLegacyAccountConnectionMigration(value: unknown): boolean {
  50. const raw = record(value);
  51. return (
  52. !Object.hasOwn(raw, "providerFamilyConnectionSelections") &&
  53. (Object.hasOwn(raw, "modelProviderFamilySelectedKeys") ||
  54. Object.hasOwn(raw, "modelProviderFamilyModes"))
  55. );
  56. }
  57. /** 仅在 settings 文件读取边界导入旧连接;运行时代码不能再解释旧导航 key。 */
  58. export function migrateLegacyAccountConnectionSettings(value: unknown): unknown {
  59. if (!needsLegacyAccountConnectionMigration(value)) return value;
  60. const raw = record(value);
  61. const modes = record(raw.modelProviderFamilyModes);
  62. const keys = record(raw.modelProviderFamilySelectedKeys);
  63. const selections: ProviderFamilyConnectionSelectionSettings = {};
  64. for (const family of ["zai", "bigmodel"] as const) {
  65. // API 实例由 Provider 配置迁移负责;旧 API 模式不是一个账号套餐。
  66. if (modes[family] === "apiKey") continue;
  67. const key = typeof keys[family] === "string" ? keys[family].trim() : "";
  68. if (key === `coding-plan:builtin:${family}-start-plan`) {
  69. selections[family] = { kind: "start-plan" };
  70. } else if (key === `coding-plan:builtin:${family}-coding-plan`) {
  71. selections[family] = { kind: "individual-coding-plan" };
  72. } else {
  73. const prefix = `team-plan:builtin:${family}-coding-plan:`;
  74. if (!key.startsWith(prefix)) continue;
  75. try {
  76. const parts = key
  77. .slice(prefix.length)
  78. .split(":")
  79. .map((part) => decodeURIComponent(part).trim());
  80. // 旧 project-only key 无法确定组织,保留原字段而非捏造新版 Team 身份。
  81. if (parts.length !== 3 || parts.some((part) => !part)) continue;
  82. const [productId, organizationId, projectId] = parts as [string, string, string];
  83. selections[family] = { kind: "team-coding-plan", productId, organizationId, projectId };
  84. } catch {
  85. // 损坏的编码只影响这一项,不能让整个 setting.json 回退成默认值。
  86. }
  87. }
  88. }
  89. return { ...raw, providerFamilyConnectionSelections: selections };
  90. }
  91. /** 旧字段仅供回滚保留,禁止暴露回 AppSettings 或参与当前运行判断;退役旧版后删除。 */
  92. export function retainLegacyAccountConnectionFields(value: unknown): Record<string, unknown> {
  93. const raw = record(value);
  94. return Object.fromEntries(
  95. ["modelProviderFamilyModes", "modelProviderFamilySelectedKeys"]
  96. .filter((key) => Object.hasOwn(raw, key))
  97. .map((key) => [key, raw[key]]),
  98. );
  99. }