mimo-templates.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { fileURLToPath } from "node:url";
  2. import { describe, expect, it } from "vitest";
  3. import { compileModelOptionMaps } from "@zcode/model-option-map";
  4. import {
  5. ApiKeyAccessConfig,
  6. ModelConfig,
  7. ModelConfigRules,
  8. ProviderApiConfig,
  9. ProviderConfig,
  10. ProviderConfigMap,
  11. ProviderConfigResolver,
  12. } from "@zcode/provider";
  13. import { NodeZCodeBuiltinProviderConfigSource } from "@zcode/provider-node";
  14. const currentModelIds = ["mimo-v2.6-pro", "mimo-v2.6-flash"];
  15. const apiModelIds = [...currentModelIds, "mimo-v2.5-pro", "mimo-v2.5"];
  16. const templates = [
  17. ["xiaomi-mimo", "Xiaomi MiMo API", "https://api.xiaomimimo.com/anthropic"],
  18. [
  19. "xiaomi-mimo-token-plan",
  20. "Xiaomi MiMo Token Plan",
  21. "https://token-plan-cn.xiaomimimo.com/anthropic",
  22. ],
  23. ] as const;
  24. describe("MiMo API 与 Token Plan 官方模板", () => {
  25. it.each(["zcode-builtin.json", "zcode-builtin.test.json"])(
  26. "%s 两模板复用模型规则,个人地址与旧身份不变",
  27. async (filename) => {
  28. const source = new NodeZCodeBuiltinProviderConfigSource({
  29. bundledFilePath: fileURLToPath(
  30. new URL(`../../../config/provider/${filename}`, import.meta.url),
  31. ),
  32. watch: false,
  33. });
  34. try {
  35. const snapshot = await source.read();
  36. for (const [templateId, name, baseUrl] of templates) {
  37. const template = snapshot.providerTemplates?.get(templateId);
  38. expect(template, templateId).toBeDefined();
  39. expect(template!.templateNameMap).toEqual({ "zh-CN": name, "en-US": name });
  40. expect(template!.config.api?.toJSON()).toEqual({ type: "anthropic-messages", baseUrl });
  41. expect(template!.config.access?.toJSON()).toEqual({
  42. type: "api-key",
  43. apiKeyManagementUrl: "https://platform.xiaomimimo.com/",
  44. });
  45. expect(template!.config.builtinModelIds).toEqual(
  46. templateId === "xiaomi-mimo" ? apiModelIds : currentModelIds,
  47. );
  48. expect(template!.config.logo).toEqual({ type: "builtin", key: "xiaomi-mimo" });
  49. for (const modelId of template!.config.builtinModelIds!) {
  50. const model = snapshot.models.resolve({
  51. providerId: "test",
  52. templateId,
  53. modelId,
  54. apiType: "anthropic-messages",
  55. baseUrl,
  56. });
  57. expect(model.validateComplete()).toEqual([]);
  58. expect(model.enabled).toBe(true);
  59. expect(model.properties?.contextWindow).toBe(1000000);
  60. expect(model.properties?.inputFormat.toJSON()).toMatchObject({
  61. supportsImage: modelId !== "mimo-v2.5-pro",
  62. supportsVideo: modelId !== "mimo-v2.5-pro",
  63. supportsAudio: modelId !== "mimo-v2.5-pro",
  64. });
  65. expect(model.optionSpecs?.maxOutputTokens?.max).toBe(131072);
  66. expect(model.optionSpecs?.reasoningLevel?.values).toEqual(["disabled", "enabled"]);
  67. for (const reasoningLevel of ["disabled", "enabled"]) {
  68. expect(
  69. compileModelOptionMaps({
  70. reasoningLevel: { map: model.optionSpecs!.reasoningLevel!.map! },
  71. maxOutputTokens: { map: model.optionSpecs!.maxOutputTokens!.map! },
  72. }).apply({}, { reasoningLevel, maxOutputTokens: 1024 }),
  73. ).toEqual({
  74. thinking: { type: reasoningLevel },
  75. max_tokens: 1024,
  76. });
  77. }
  78. }
  79. }
  80. // 旧实例可能已手动填 Token Plan 地址;拆模板不能重命名、重新启用或覆盖其地址。
  81. const personal = new ProviderConfigMap([
  82. ...templates.map(([templateId]) => ({
  83. providerId: `new-${templateId}`,
  84. templateId,
  85. config: new ProviderConfig({
  86. group: "standard-personal",
  87. access: new ApiKeyAccessConfig({ apiKey: `test-${templateId}` }),
  88. }),
  89. })),
  90. {
  91. providerId: "legacy-mimo",
  92. templateId: "xiaomi-mimo",
  93. providerName: "My MiMo",
  94. enabled: false,
  95. config: new ProviderConfig({
  96. api: new ProviderApiConfig({ baseUrl: templates[1][2] }),
  97. access: new ApiKeyAccessConfig({ apiKey: "old-test-only" }),
  98. }),
  99. },
  100. {
  101. providerId: "international-mimo",
  102. templateId: "xiaomi-mimo-token-plan",
  103. config: new ProviderConfig({
  104. group: "standard-personal",
  105. api: new ProviderApiConfig({
  106. baseUrl: "https://token-plan-sgp.xiaomimimo.com/anthropic",
  107. }),
  108. access: new ApiKeyAccessConfig({ apiKey: "test-only" }),
  109. }),
  110. },
  111. ]);
  112. const before = personal.toJSON();
  113. const resolution = new ProviderConfigResolver().resolve({
  114. zcodeBuiltinProviders: ProviderConfigMap.empty(),
  115. zcodeBuiltinProviderTemplates: snapshot.providerTemplates,
  116. accountProviders: ProviderConfigMap.empty(),
  117. personalProviders: personal,
  118. zcodeBuiltinModelRules: snapshot.models,
  119. personalModels: new ModelConfigRules([
  120. {
  121. type: "provider-model",
  122. providerId: "international-mimo",
  123. modelId: "mimo-v2.6-pro",
  124. config: new ModelConfig({ enabled: false }),
  125. },
  126. ]),
  127. });
  128. for (const [templateId, , baseUrl] of templates) {
  129. const provider = resolution.registryProviders.find(
  130. (p) => p.providerId === `new-${templateId}`,
  131. );
  132. expect(provider?.config.api?.baseUrl).toBe(baseUrl);
  133. expect(provider?.models.map((m) => m.modelId)).toEqual(
  134. templateId === "xiaomi-mimo" ? apiModelIds : currentModelIds,
  135. );
  136. expect(provider?.config.access?.toJSON()).toMatchObject({
  137. apiKey: `test-${templateId}`,
  138. });
  139. }
  140. expect(
  141. resolution.registryProviders.find((p) => p.providerId === "international-mimo")?.config
  142. .api?.baseUrl,
  143. ).toBe("https://token-plan-sgp.xiaomimimo.com/anthropic");
  144. expect(
  145. resolution.registryProviders
  146. .find((p) => p.providerId === "international-mimo")
  147. ?.models.map((m) => m.modelId),
  148. ).toEqual(["mimo-v2.6-flash"]);
  149. expect(resolution.registryProviders.some((p) => p.providerId === "legacy-mimo")).toBe(
  150. false,
  151. );
  152. const legacy = resolution.resolvedProviders.find((p) => p.providerId === "legacy-mimo");
  153. expect(legacy?.config.access?.toJSON()).toMatchObject({ apiKey: "old-test-only" });
  154. expect(legacy?.providerName).toBe("My MiMo");
  155. expect(legacy?.config.api?.baseUrl).toBe(templates[1][2]);
  156. expect(personal.toJSON()).toEqual(before);
  157. } finally {
  158. source.dispose();
  159. }
  160. },
  161. );
  162. });