modelSelectionGroups.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { describe, expect, it } from "vitest";
  2. import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
  3. import type { ModelSelectionView } from "@zcode/services";
  4. import { buildRegistryModelSelectGroups } from "../src/lib/modelSelectionGroups.js";
  5. describe("model selection groups", () => {
  6. it.each([
  7. [{ type: "zhipu-account", accountType: "zai", mode: "individual-coding-plan" }, false],
  8. [{ type: "zhipu-account", accountType: "bigmodel", mode: "team-coding-plan" }, false],
  9. [{ type: "zhipu-coding-plan-api-key" }, false],
  10. [{ type: "zhipu-account", accountType: "zai", mode: "start-plan" }, false],
  11. [{ type: "zhipu-account", accountType: "bigmodel", mode: "start-plan" }, false],
  12. [{ type: "zhipu-account", accountType: "bigmodel", mode: "off-peak" }, true],
  13. [{ type: "api-key" }, true],
  14. ])("仅套餐 GLM-5.3 隐藏视觉标,不影响 Flash/真实能力:%j", (access, expected) => {
  15. const view: ModelSelectionView = {
  16. revision: 1,
  17. providers: [
  18. {
  19. providerId: "fixture",
  20. config: {
  21. logo: { type: "builtin", key: "deepseek" },
  22. access: access as ModelSelectionView["providers"][number]["config"]["access"],
  23. api: { type: "anthropic-messages" },
  24. },
  25. models: ["GLM-5.3", "GLM-5.3-Flash", "GLM-4.7"].map((modelId) => ({
  26. modelId,
  27. config: { properties: { inputFormat: { supportsImage: true } } },
  28. })),
  29. },
  30. ],
  31. };
  32. const items = buildRegistryModelSelectGroups("glm", view)[0]!.items;
  33. expect(buildRegistryModelSelectGroups("glm", view)[0]!.logo).toEqual({
  34. type: "builtin",
  35. key: "deepseek",
  36. });
  37. expect(items[0]!.supportsVisionInput === true).toBe(expected);
  38. expect(items[1]!.supportsVisionInput).toBe(true);
  39. expect(items[2]!.supportsVisionInput).toBe(true);
  40. expect(view.providers[0]!.models[0]!.config.properties?.inputFormat?.supportsImage).toBe(true);
  41. });
  42. it("保留带前缀和多段斜杠的精确模型 ID;不从名称推断视觉能力", () => {
  43. const modelId = "upstream/family/vision-preview";
  44. const view: ModelSelectionView = {
  45. revision: 1,
  46. providers: [
  47. {
  48. providerId: "aggregator",
  49. config: { api: { type: "openai-chat-completions", baseUrl: "https://example.test/v1" } },
  50. models: [{ modelId, config: { properties: { inputFormat: { supportsImage: true } } } }],
  51. },
  52. ],
  53. };
  54. expect(buildRegistryModelSelectGroups("glm", view)[0]?.items).toEqual([
  55. {
  56. key: `registry-provider:aggregator:${modelId}`,
  57. value: "custom:aggregator:upstream%2Ffamily%2Fvision-preview",
  58. name: modelId,
  59. supportsVisionInput: true,
  60. },
  61. ]);
  62. view.providers[0]!.models[0]!.config = {};
  63. expect(buildRegistryModelSelectGroups("glm", view)[0]?.items[0]).not.toHaveProperty(
  64. "supportsVisionInput",
  65. );
  66. });
  67. it("直接把 Registry API Provider 投影为有序模型组", () => {
  68. const selectionView: ModelSelectionView = {
  69. revision: 7,
  70. providers: [
  71. {
  72. providerId: "anthropic-api",
  73. providerName: "Anthropic API",
  74. config: {
  75. access: { type: "api-key", apiKey: "secret" },
  76. api: {
  77. type: "anthropic-messages",
  78. baseUrl: "https://example.com/anthropic",
  79. },
  80. models: ["claude-a", "claude-b"],
  81. },
  82. models: [
  83. {
  84. modelId: "claude-a",
  85. config: { properties: { inputFormat: { supportsImage: true } } },
  86. },
  87. { modelId: "claude-b", config: {} },
  88. ],
  89. },
  90. {
  91. providerId: "openai-api",
  92. providerName: "OpenAI API",
  93. config: {
  94. access: { type: "api-key", apiKey: "secret" },
  95. api: {
  96. type: "openai-responses",
  97. baseUrl: "https://example.com/openai",
  98. },
  99. models: ["model-a"],
  100. },
  101. models: [{ modelId: "model-a", config: {} }],
  102. },
  103. ],
  104. };
  105. expect(buildRegistryModelSelectGroups("glm", selectionView)).toEqual([
  106. {
  107. key: "registry-provider:anthropic-api",
  108. label: "Anthropic API",
  109. labelBadge: "API",
  110. items: [
  111. {
  112. key: "registry-provider:anthropic-api:claude-a",
  113. value: "custom:anthropic-api:claude-a",
  114. name: "claude-a",
  115. supportsVisionInput: true,
  116. },
  117. {
  118. key: "registry-provider:anthropic-api:claude-b",
  119. value: "custom:anthropic-api:claude-b",
  120. name: "claude-b",
  121. },
  122. ],
  123. },
  124. {
  125. key: "registry-provider:openai-api",
  126. label: "OpenAI API",
  127. labelBadge: "API",
  128. items: [
  129. {
  130. key: "registry-provider:openai-api:model-a",
  131. value: "custom:openai-api:model-a",
  132. name: "model-a",
  133. },
  134. ],
  135. },
  136. ]);
  137. });
  138. it("直接把 Registry Account Provider 投影为已选连接,不重新裁决 Family", () => {
  139. const providerId = BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan;
  140. const selectionView: ModelSelectionView = {
  141. revision: 8,
  142. providers: [
  143. {
  144. providerId,
  145. providerName: "BigModel Coding Plan",
  146. config: {
  147. access: {
  148. type: "zhipu-account",
  149. accountType: "bigmodel",
  150. mode: "team-coding-plan",
  151. entitled: true,
  152. },
  153. api: {
  154. type: "anthropic-messages",
  155. baseUrl: "https://example.com/anthropic",
  156. },
  157. models: ["GLM-5.2"],
  158. },
  159. models: [{ modelId: "GLM-5.2", config: {} }],
  160. },
  161. ],
  162. };
  163. expect(buildRegistryModelSelectGroups("glm", selectionView)).toEqual([
  164. {
  165. key: `registry-provider:${providerId}`,
  166. label: "BigModel",
  167. labelBadge: "Team",
  168. directItems: true,
  169. items: [
  170. {
  171. key: `registry-provider:${providerId}:GLM-5.2`,
  172. value: "custom:account%3Abigmodel-team-coding-plan:GLM-5.2",
  173. name: "GLM-5.2",
  174. },
  175. ],
  176. },
  177. ]);
  178. });
  179. });