modelSelectionGroups.test.ts 5.9 KB

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