reasoning-level-migration.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { fileURLToPath } from "node:url";
  2. import { beforeAll, afterAll, describe, expect, it } from "vitest";
  3. import {
  4. ModelConfigRules,
  5. ModelConfig,
  6. ModelOptionSpecsConfig,
  7. ProviderConfigMap,
  8. ProviderConfigResolver,
  9. ProviderConfig,
  10. ZhipuAccountAccessConfig,
  11. createRegistryModelConfig,
  12. createRegistryProviderConfig,
  13. type ModelSelection,
  14. type ProviderRegistryServiceSnapshot,
  15. } from "@zcode/provider";
  16. import { NodeZCodeBuiltinProviderConfigSource } from "../src/index.js";
  17. import { createNodeModelSelectionFacade } from "../src/model-selection-facade.js";
  18. import { legacyReasoningLevelRenames } from "../src/legacy-reasoning-level-renames.js";
  19. const source = new NodeZCodeBuiltinProviderConfigSource({
  20. bundledFilePath: fileURLToPath(
  21. new URL("../../../config/provider/zcode-builtin.json", import.meta.url),
  22. ),
  23. watch: false,
  24. });
  25. let builtin: Awaited<ReturnType<typeof source.read>>;
  26. beforeAll(async () => {
  27. builtin = await source.read();
  28. });
  29. afterAll(() => source.dispose());
  30. function view(
  31. modelId: string,
  32. level: string,
  33. personalModels = ModelConfigRules.empty(),
  34. modelRules = builtin.models,
  35. ) {
  36. const config = {
  37. revision: "c",
  38. zcodeBuiltinRevision: "b",
  39. personalRevision: "p",
  40. zcodeBuiltinProviders: builtin.providers,
  41. zcodeBuiltinProviderTemplates: builtin.templates,
  42. personalProviders: ProviderConfigMap.empty(),
  43. zcodeBuiltinModelRules: modelRules,
  44. personalModels,
  45. };
  46. const account = {
  47. revision: "a",
  48. basedOnZCodeBuiltinRevision: "b",
  49. providers: new ProviderConfigMap([
  50. [
  51. "account:bigmodel-individual-coding-plan",
  52. new ProviderConfig({ access: new ZhipuAccountAccessConfig({ entitled: true }) }),
  53. ],
  54. ]),
  55. states: {
  56. "account:bigmodel-individual-coding-plan": {
  57. availability: "available" as const,
  58. entitled: true,
  59. current: true,
  60. },
  61. },
  62. };
  63. // 真实 Built-in 规则和 Resolver;补入模型成员用于独立覆盖 GLM 的不同代际。
  64. const resolution = new ProviderConfigResolver().resolve({
  65. ...config,
  66. accountProviders: account.providers,
  67. accountStates: account.states,
  68. });
  69. const providerId = "account:bigmodel-individual-coding-plan";
  70. const providerConfig = resolution.effectiveProviders.get(providerId)!;
  71. const modelConfig = ModelConfigRules.composeEffective(modelRules, personalModels).resolve({
  72. providerId,
  73. modelId,
  74. apiType: providerConfig.api?.type,
  75. baseURL: providerConfig.api?.baseUrl,
  76. });
  77. const checkedModel = createRegistryModelConfig(modelConfig);
  78. const checkedProvider = createRegistryProviderConfig(providerConfig);
  79. if (!checkedModel.ok || !checkedProvider.ok)
  80. throw new Error(
  81. `fixture must be complete: ${JSON.stringify({ checkedModel, checkedProvider })}`,
  82. );
  83. const registry = {
  84. revision: 1,
  85. providers: [
  86. {
  87. providerId,
  88. config: checkedProvider.config,
  89. models: [{ modelId, config: checkedModel.config }],
  90. },
  91. ],
  92. };
  93. const snapshot: ProviderRegistryServiceSnapshot = {
  94. config,
  95. account,
  96. resolution,
  97. registry,
  98. sourceRevisions: { config: "c", account: "a" },
  99. };
  100. const facade = createNodeModelSelectionFacade({
  101. getSnapshot: () => snapshot,
  102. getView: () => registry,
  103. refresh: async () => snapshot,
  104. onDidChange: () => () => {},
  105. });
  106. const selection: ModelSelection = Object.freeze({
  107. providerId,
  108. modelId,
  109. options: Object.freeze({ reasoningLevel: level }),
  110. });
  111. return { result: facade.getView(undefined, undefined, { selection }), selection, facade };
  112. }
  113. describe("Todo95C 来源限定的只读关闭档位改名", () => {
  114. it("保留 25 条历史来源,只恢复当前规则仍合法的关闭值", () => {
  115. expect(legacyReasoningLevelRenames).toHaveLength(25);
  116. for (const entry of legacyReasoningLevelRenames) {
  117. const rule = builtin.models
  118. .rules()
  119. .find(
  120. (rule) =>
  121. rule.type === "model" &&
  122. (rule.modelMatch === entry.modelMatch ||
  123. rule.modelMatch === entry.simplifiedModelMatch),
  124. );
  125. expect(rule?.config.optionSpecs?.reasoningLevel?.values).not.toContain(entry.oldLevel);
  126. // 仅测试生成代表型号;实际迁移从不从正则反推名字。
  127. const modelId = entry.modelMatch.slice(2).split("(?:")[0]!.replaceAll("\\.", ".");
  128. // Todo113 核实 K2.7 Code 不支持关闭;旧改名表不能越过当前值域重新启用关闭档。
  129. if (modelId === "kimi-k2.7-code") {
  130. expect(rule?.config.optionSpecs?.reasoningLevel?.values).toEqual(["enabled"]);
  131. expect(view(modelId, entry.oldLevel).result.selectionIssue).toBe(
  132. "reasoning-level-not-supported",
  133. );
  134. continue;
  135. }
  136. expect(rule?.config.optionSpecs?.reasoningLevel?.values).toContain("disabled");
  137. expect(
  138. view(modelId, entry.oldLevel).result.effectiveSelection?.options?.reasoningLevel,
  139. modelId,
  140. ).toBe("disabled");
  141. }
  142. });
  143. it.each([
  144. ["GLM-5-Turbo", "off"],
  145. ["GLM-5.2", "nothink"],
  146. ["vendor/GLM-5.2:free", "nothink"],
  147. ])("%s / %s", (id, old) => {
  148. const { result, selection, facade } = view(id!, old!);
  149. expect(result.selectionIssue).toBeUndefined();
  150. expect(result.effectiveSelection?.options?.reasoningLevel).toBe("disabled");
  151. expect(selection.options?.reasoningLevel).toBe(old);
  152. expect(facade.getView(selection).preferredSelection?.options?.reasoningLevel).toBe("disabled");
  153. });
  154. it.each([
  155. ["unknown-future", "off"],
  156. ["GLM-5.3", "nothink"],
  157. ["GLM-5.2", "off"],
  158. ["GLM-5.3", "none"],
  159. ["GLM-5.3", "typo"],
  160. ["GLM-5.2_custom", "nothink"],
  161. ])("不猜 %s / %s", (id, old) => {
  162. expect(view(id!, old!).result.selectionIssue).toBe("reasoning-level-not-supported");
  163. });
  164. it("旧缓存仍使用原 matcher 时也能恢复历史关闭选择", () => {
  165. const oldRules = new ModelConfigRules(
  166. builtin.models.rules().map((rule) => {
  167. const rename =
  168. rule.type === "model"
  169. ? legacyReasoningLevelRenames.find(
  170. (entry) => entry.simplifiedModelMatch === rule.modelMatch,
  171. )
  172. : undefined;
  173. return rename ? { ...rule, modelMatch: rename.modelMatch } : rule;
  174. }),
  175. );
  176. expect(
  177. view("GLM-5.2", "nothink", ModelConfigRules.empty(), oldRules).result.effectiveSelection
  178. ?.options?.reasoningLevel,
  179. ).toBe("disabled");
  180. });
  181. it("后续 Built-in values 覆盖旧规则时不恢复被覆盖的关闭值", () => {
  182. expect(view("GLM-5.3-Flash", "off").result.selectionIssue).toBe(
  183. "reasoning-level-not-supported",
  184. );
  185. });
  186. it("Personal 原值合法则保留;自定义值域不套用内置别名", () => {
  187. const personal = (values: string[]) =>
  188. new ModelConfigRules([
  189. {
  190. type: "provider-model",
  191. providerId: "account:bigmodel-individual-coding-plan",
  192. modelId: "GLM-5.2",
  193. config: new ModelConfig({
  194. optionSpecs: new ModelOptionSpecsConfig({ reasoningLevel: { values } }),
  195. }),
  196. },
  197. ]);
  198. expect(
  199. view("GLM-5.2", "nothink", personal(["nothink", "high"])).result.effectiveSelection?.options
  200. ?.reasoningLevel,
  201. ).toBe("nothink");
  202. expect(view("GLM-5.2", "nothink", personal(["disabled", "high"])).result.selectionIssue).toBe(
  203. "reasoning-level-not-supported",
  204. );
  205. });
  206. });