subagent-markdown-selection.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { decodeCustomModelValue, encodeCustomModelValue } from "./custom-model-value.js";
  2. import {
  3. migrateLegacyModelProviderId,
  4. migrateLegacyOfficialGlmModelId,
  5. } from "./legacy-model-provider-identity.js";
  6. import { parseModelPickerValue, type ModelSelection } from "./model-selection.js";
  7. const INHERIT_NAMES = new Set(["inherit", "main", "sonnet", "opus", "haiku"]);
  8. /** Markdown 的正式字段始终为字符串 model + thoughtLevel;不解释中间态字段。 */
  9. export function parseSubagentMarkdownSelection(
  10. frontmatter: Record<string, unknown>,
  11. ): ModelSelection | undefined {
  12. if (typeof frontmatter.model !== "string") return undefined;
  13. const value = frontmatter.model.trim();
  14. if (!value || INHERIT_NAMES.has(value)) return undefined;
  15. let selection: ModelSelection;
  16. const custom = decodeCustomModelValue(value);
  17. if (custom) {
  18. if (!custom.providerId.trim() || !custom.modelName?.trim()) return undefined;
  19. selection = { providerId: custom.providerId.trim(), modelId: custom.modelName.trim() };
  20. } else {
  21. try {
  22. selection = parseModelPickerValue(value);
  23. } catch {
  24. return undefined;
  25. }
  26. }
  27. const reasoningLevel =
  28. typeof frontmatter.thoughtLevel === "string" ? frontmatter.thoughtLevel.trim() : "";
  29. return reasoningLevel ? { ...selection, options: { reasoningLevel } } : selection;
  30. }
  31. /** 普通 ID 保持可读;分隔符或 custom: 前缀会与解析格式冲突,须用既有编码无损保存。 */
  32. export function formatSubagentMarkdownModel(selection: ModelSelection): string {
  33. return selection.providerId.startsWith("custom:") ||
  34. selection.providerId.includes("/") ||
  35. selection.modelId.includes("$")
  36. ? encodeCustomModelValue(selection.providerId, selection.modelId)
  37. : `${selection.providerId}/${selection.modelId}`;
  38. }
  39. function migrateModelValue(value: string): string {
  40. if (value.startsWith("custom:")) {
  41. const decoded = decodeCustomModelValue(value);
  42. if (!decoded?.modelName || !decoded.providerId.startsWith("builtin:")) return value;
  43. const providerId = migrateLegacyModelProviderId(decoded.providerId);
  44. if (!providerId || providerId === decoded.providerId) return value;
  45. // 未改名的模型保留编码原文(包括 %24 和 %2F),不把模型名再当 Picker 解析。
  46. const modelId = migrateLegacyOfficialGlmModelId(decoded.providerId, decoded.modelName);
  47. const body = value.slice("custom:".length);
  48. const separator = body.startsWith("builtin:")
  49. ? body.indexOf(":", "builtin:".length)
  50. : body.indexOf(":");
  51. return separator < 0
  52. ? value
  53. : `custom:${encodeURIComponent(providerId)}:${modelId === decoded.modelName ? body.slice(separator + 1) : encodeURIComponent(modelId)}`;
  54. }
  55. const separator = value.indexOf("/");
  56. if (separator < 1) return value;
  57. const oldProvider = value.slice(0, separator);
  58. if (!oldProvider.startsWith("builtin:")) return value;
  59. const providerId = migrateLegacyModelProviderId(oldProvider);
  60. const modelId = value.slice(separator + 1);
  61. // 旧 Picker 的 $ 后是档位,不是型号;仅普通编码拆开,custom 编码的 $ 仍属于型号。
  62. const reasoningIndex = modelId.indexOf("$");
  63. const name = reasoningIndex < 0 ? modelId : modelId.slice(0, reasoningIndex);
  64. const suffix = reasoningIndex < 0 ? "" : modelId.slice(reasoningIndex);
  65. return providerId
  66. ? providerId + "/" + migrateLegacyOfficialGlmModelId(oldProvider, name) + suffix
  67. : value;
  68. }
  69. /** 只替换 frontmatter 单行 model 的 Provider 值,不重建 YAML、正文或无关格式。 */
  70. export function migrateSubagentMarkdownProvider(content: string): string {
  71. const frontmatter = /^(?:\uFEFF)?---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/u.exec(
  72. content,
  73. );
  74. if (!frontmatter) return content;
  75. const body = frontmatter[1]!;
  76. const migrated = body.replace(
  77. /^(model[ \t]*:[ \t]*)([^\r\n]*)/gmu,
  78. (line, prefix: string, raw: string) => {
  79. const scalar = /^("(?:\\.|[^"\\])*"|'(?:''|[^'])*'|[^#]*?)([ \t]+#.*|[ \t]*)$/u.exec(raw);
  80. if (!scalar) return line;
  81. const token = scalar[1]!;
  82. let value: string;
  83. try {
  84. value = token.startsWith('"')
  85. ? (JSON.parse(token) as string)
  86. : token.startsWith("'")
  87. ? token.slice(1, -1).replace(/''/gu, "'")
  88. : token;
  89. } catch {
  90. return line;
  91. }
  92. const next = migrateModelValue(value);
  93. if (next === value) return line;
  94. const encoded = token.startsWith('"')
  95. ? JSON.stringify(next)
  96. : token.startsWith("'")
  97. ? `'${next.replace(/'/gu, "''")}'`
  98. : next;
  99. return prefix + encoded + scalar[2];
  100. },
  101. );
  102. const offset = frontmatter[0].indexOf("\n") + 1;
  103. return content.slice(0, offset) + migrated + content.slice(offset + body.length);
  104. }