reasoning-level-migration.test.ts 6.5 KB

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