model-reference.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { describe, expect, it } from "vitest";
  2. import type { ModelCatalogEntry } from "@zcode/contracts";
  3. import {
  4. describeWorkflowSubagentModel,
  5. formatModelCatalogId,
  6. parseWorkflowSubagentModel,
  7. resolveModelReference,
  8. } from "../src/tool/handlers/model-reference.js";
  9. /**
  10. * 目录条目的最小构造器。`reasoningLevels` 与 `current` 在端口契约上是**必填**(空数组与
  11. * false 各有意义),所以这里给的是默认值而不是让它们缺席。
  12. */
  13. function entry(
  14. providerId: string,
  15. modelId: string,
  16. extra: Partial<ModelCatalogEntry> = {},
  17. ): ModelCatalogEntry {
  18. return { providerId, modelId, reasoningLevels: [], current: false, ...extra };
  19. }
  20. function expectOk(resolution: ReturnType<typeof resolveModelReference>) {
  21. expect(resolution.ok).toBe(true);
  22. if (!resolution.ok) throw new Error(`expected a resolution, got ${resolution.reason}`);
  23. return resolution;
  24. }
  25. function expectFailure(resolution: ReturnType<typeof resolveModelReference>) {
  26. expect(resolution.ok).toBe(false);
  27. if (resolution.ok) throw new Error("expected a failure");
  28. return resolution;
  29. }
  30. describe("resolveModelReference — 三档匹配", () => {
  31. it("第 1 档:全称精确命中,哪怕裸名在别处也唯一", () => {
  32. const entries = [entry("zhipu", "glm-4.6"), entry("openai", "glm-4.6")];
  33. const resolved = expectOk(resolveModelReference("openai/glm-4.6", entries));
  34. expect(resolved.canonical).toBe("openai/glm-4.6");
  35. expect(resolved.selection).toEqual({ providerId: "openai", modelId: "glm-4.6" });
  36. expect(resolved.entry).toBe(entries[1]);
  37. });
  38. it("第 2 档:裸模型名只挂在一个 provider 下就直接命中", () => {
  39. const entries = [entry("zhipu", "glm-4.6"), entry("openai", "gpt-5")];
  40. expect(expectOk(resolveModelReference("gpt-5", entries)).canonical).toBe("openai/gpt-5");
  41. });
  42. it("第 3 档:裸名撞多个 provider 时,当前会话那一条胜出", () => {
  43. const entries = [
  44. entry("zhipu", "glm-4.6"),
  45. entry("bigmodel", "glm-4.6", { current: true }),
  46. entry("openrouter", "glm-4.6"),
  47. ];
  48. const resolved = expectOk(resolveModelReference("glm-4.6", entries));
  49. expect(resolved.canonical).toBe("bigmodel/glm-4.6");
  50. });
  51. it("第 3 档无当前项:回 ambiguous 并列出撞上的全部全称", () => {
  52. const entries = [entry("zhipu", "glm-4.6"), entry("openrouter", "glm-4.6")];
  53. const failed = expectFailure(resolveModelReference("glm-4.6", entries));
  54. expect(failed.reason).toBe("ambiguous");
  55. expect(failed.message).toContain("zhipu/glm-4.6");
  56. expect(failed.message).toContain("openrouter/glm-4.6");
  57. expect(failed.message).toContain("providerId/modelId");
  58. expect(failed.candidates).toHaveLength(2);
  59. });
  60. it("三档比较一律大小写不敏感,且规范形用注册表自己的拼写", () => {
  61. const entries = [entry("BigModel", "GLM-4.6")];
  62. // 用户打什么大小写都行;回填进 `subagent_model` 的那一个必须是目录里的原样拼写。
  63. expect(expectOk(resolveModelReference("bigmodel/glm-4.6", entries)).canonical).toBe(
  64. "BigModel/GLM-4.6",
  65. );
  66. expect(expectOk(resolveModelReference("GLM-4.6", entries)).canonical).toBe("BigModel/GLM-4.6");
  67. expect(expectOk(resolveModelReference(" glm-4.6 ", entries)).canonical).toBe(
  68. "BigModel/GLM-4.6",
  69. );
  70. });
  71. });
  72. describe("resolveModelReference — 禁用条目", () => {
  73. it("只匹配到禁用条目时回 disabled,并把每条的理由说出来", () => {
  74. const entries = [entry("zhipu", "glm-4.6", { disabledReason: "no API key configured" })];
  75. const failed = expectFailure(resolveModelReference("glm-4.6", entries));
  76. expect(failed.reason).toBe("disabled");
  77. expect(failed.message).toContain("no API key configured");
  78. expect(failed.message).toContain("zhipu/glm-4.6");
  79. expect(failed.candidates).toEqual(entries);
  80. });
  81. it("禁用条目绝不参与第 3 档的歧义:同名的可用条目照常胜出", () => {
  82. const entries = [
  83. entry("zhipu", "glm-4.6", { disabledReason: "disabled by policy" }),
  84. entry("bigmodel", "glm-4.6"),
  85. ];
  86. // 只有一条可用,所以不需要 current 兜底也不该是 ambiguous。
  87. expect(expectOk(resolveModelReference("glm-4.6", entries)).canonical).toBe("bigmodel/glm-4.6");
  88. });
  89. it("not_found 的清单里不出现禁用条目:挑了也用不了的名字只会换来第二次失败", () => {
  90. const entries = [
  91. entry("zhipu", "glm-4.6", { disabledReason: "no API key configured" }),
  92. entry("openai", "gpt-5"),
  93. ];
  94. const failed = expectFailure(resolveModelReference("claude-opus-5", entries));
  95. expect(failed.message).toContain("openai/gpt-5");
  96. expect(failed.message).not.toContain("zhipu/glm-4.6");
  97. expect(failed.candidates).toEqual([entries[1]]);
  98. });
  99. });
  100. describe("resolveModelReference — not_found 的文案", () => {
  101. it("点名给的那个字符串、列出可用 id 并标出当前项,末尾给出 ListModels 这条路", () => {
  102. const entries = [entry("zhipu", "glm-4.6", { current: true }), entry("openai", "gpt-5")];
  103. const failed = expectFailure(resolveModelReference("gemini-3", entries));
  104. expect(failed.reason).toBe("not_found");
  105. expect(failed.message).toContain("`gemini-3`");
  106. expect(failed.message).toContain("zhipu/glm-4.6 [current]");
  107. expect(failed.message).toContain("openai/gpt-5");
  108. expect(failed.message).not.toContain("openai/gpt-5 [current]");
  109. expect(failed.message).toContain("Pass one of these ids, or call ListModels.");
  110. });
  111. it("目录本身为空时说「这台机器没配模型」,而不是给一份空清单", () => {
  112. const failed = expectFailure(resolveModelReference("gpt-5", []));
  113. expect(failed.reason).toBe("not_found");
  114. expect(failed.message).toContain("No models are configured on this host.");
  115. expect(failed.candidates).toEqual([]);
  116. });
  117. it("清单封顶在 40 行,余下的换成一句可以拿到全量的话", () => {
  118. const entries = Array.from({ length: 60 }, (_, index) => entry("p", `m-${index}`));
  119. const failed = expectFailure(resolveModelReference("nope", entries));
  120. const listed = failed.message.split("\n").filter((line) => line.startsWith("p/m-"));
  121. expect(listed).toHaveLength(40);
  122. expect(failed.message).toContain("… and 20 more.");
  123. // candidates 不截断:它是给调用方的结构化事实,截断的只有面向模型的那份文案。
  124. expect(failed.candidates).toHaveLength(60);
  125. });
  126. });
  127. describe("resolveModelReference — $reasoningLevel", () => {
  128. const LEVELED = entry("bigmodel", "glm-4.6", {
  129. reasoningLevels: ["low", "medium", "high"],
  130. defaultReasoningLevel: "high",
  131. });
  132. it("给了合法档位就带进选型,并用注册表的拼写", () => {
  133. const resolved = expectOk(resolveModelReference("bigmodel/glm-4.6$LOW", [LEVELED]));
  134. expect(resolved.selection.options).toEqual({ reasoningLevel: "low" });
  135. expect(resolved.canonical).toBe("bigmodel/glm-4.6$low");
  136. });
  137. it("裸名加档位同样走第 2 档", () => {
  138. expect(expectOk(resolveModelReference("glm-4.6$medium", [LEVELED])).canonical).toBe(
  139. "bigmodel/glm-4.6$medium",
  140. );
  141. });
  142. it("档位不认识时回 reasoning_level_unknown,并列出这个模型的档位与默认档", () => {
  143. const failed = expectFailure(resolveModelReference("glm-4.6$ultra", [LEVELED]));
  144. expect(failed.reason).toBe("reasoning_level_unknown");
  145. expect(failed.message).toContain("`ultra`");
  146. expect(failed.message).toContain("low, medium, high");
  147. expect(failed.message).toContain("Omit the suffix to use high.");
  148. // 整张目录不进这条文案:模型名是认对的,错的只有档位。
  149. expect(failed.candidates).toEqual([LEVELED]);
  150. });
  151. it("给没有档位的模型接 `$` 时说清该模型根本没有档位", () => {
  152. const failed = expectFailure(resolveModelReference("gpt-5$high", [entry("openai", "gpt-5")]));
  153. expect(failed.reason).toBe("reasoning_level_unknown");
  154. expect(failed.message).toContain("has no reasoning levels");
  155. });
  156. it("不给档位时用注册表默认档", () => {
  157. expect(expectOk(resolveModelReference("glm-4.6", [LEVELED])).selection.options).toEqual({
  158. reasoningLevel: "high",
  159. });
  160. });
  161. it("模型没有档位时不造 options", () => {
  162. const resolved = expectOk(resolveModelReference("gpt-5", [entry("openai", "gpt-5")]));
  163. expect(resolved.selection.options).toBeUndefined();
  164. expect(resolved.canonical).toBe("openai/gpt-5");
  165. });
  166. it("有档位但注册表没给默认档时同样不造 options:没有可用的那一个就不猜", () => {
  167. const noDefault = entry("openai", "o5", { reasoningLevels: ["low", "high"] });
  168. expect(expectOk(resolveModelReference("o5", [noDefault])).selection.options).toBeUndefined();
  169. });
  170. });
  171. describe("规范形的读写", () => {
  172. it("describeWorkflowSubagentModel 只在设了模型时出现,并说明主代理没换", () => {
  173. expect(describeWorkflowSubagentModel(undefined)).toBe("");
  174. expect(describeWorkflowSubagentModel("bigmodel/glm-4.6$high")).toBe(
  175. " Subagents run on bigmodel/glm-4.6$high (the main agent stays on the session model).",
  176. );
  177. });
  178. it("parseWorkflowSubagentModel 把规范形拆回选型,缺席即缺席", () => {
  179. expect(parseWorkflowSubagentModel(undefined)).toBeUndefined();
  180. expect(parseWorkflowSubagentModel("bigmodel/glm-4.6$high")).toEqual({
  181. providerId: "bigmodel",
  182. modelId: "glm-4.6",
  183. options: { reasoningLevel: "high" },
  184. });
  185. });
  186. it("parseWorkflowSubagentModel 对没归一化过的字符串喊出来,而不是静默丢掉用户要的模型", () => {
  187. // 走到 handler 的字符串一定过了 resolveInput,所以这里的失败是接线故障。
  188. expect(() => parseWorkflowSubagentModel("glm-4.6")).toThrow(/un-canonicalised/u);
  189. });
  190. it("formatModelCatalogId 不带档位:id 是可以逐字抄进 subagent_model 的那一半", () => {
  191. expect(formatModelCatalogId(entry("bigmodel", "glm-4.6", { reasoningLevels: ["high"] }))).toBe(
  192. "bigmodel/glm-4.6",
  193. );
  194. });
  195. });