opencode-go.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { readFile } from "node:fs/promises";
  2. import { beforeAll, describe, expect, it } from "vitest";
  3. import { compileModelOptionMaps } from "@zcode/model-option-map";
  4. import { ModelConfig, ModelConfigRules, parseZCodeBuiltinModelConfigRules } from "@zcode/provider";
  5. const url = "https://opencode.ai/zen/go/v1";
  6. const members = {
  7. "opencode-go-chat": [
  8. "glm-5.3-flash",
  9. "glm-5.3",
  10. "glm-5.2",
  11. "glm-5.1",
  12. "kimi-k3",
  13. "kimi-k2.7-code",
  14. "kimi-k2.6",
  15. "deepseek-v4.1-flash",
  16. "deepseek-v4-pro",
  17. "deepseek-v4-flash",
  18. "deepseek-v4-flash-vision-exp",
  19. "mimo-v2.5",
  20. "mimo-v2.5-pro",
  21. "hy4-preview",
  22. "hy3",
  23. ],
  24. "opencode-go-messages": [
  25. "minimax-m3",
  26. "minimax-m2.7",
  27. "minimax-m2.5",
  28. "qwen3.8-max",
  29. "qwen3.8-flash",
  30. "qwen3.7-max",
  31. "qwen3.7-plus",
  32. "qwen3.6-plus",
  33. ],
  34. "opencode-go-responses": ["gpt-5.6-luna", "grok-4.6"],
  35. };
  36. const enabled = new Set([
  37. "glm-5.3-flash",
  38. "glm-5.3",
  39. "kimi-k3",
  40. "kimi-k2.7-code",
  41. "deepseek-v4.1-flash",
  42. "deepseek-v4-pro",
  43. "mimo-v2.5",
  44. "mimo-v2.5-pro",
  45. "minimax-m3",
  46. "qwen3.8-max",
  47. "qwen3.8-flash",
  48. "gpt-5.6-luna",
  49. "grok-4.6",
  50. ]);
  51. let rules: ModelConfigRules;
  52. let templates: {
  53. templateId: string;
  54. config: {
  55. api: { type: string; baseUrl: string };
  56. access: unknown;
  57. logo: unknown;
  58. builtinModelIds: string[];
  59. };
  60. }[];
  61. beforeAll(async () => {
  62. const release = JSON.parse(
  63. await readFile(new URL("../../../config/provider/zcode-builtin.json", import.meta.url), "utf8"),
  64. );
  65. rules = parseZCodeBuiltinModelConfigRules(release.config.modelConfigRules);
  66. templates = release.config.providerConfigRules.templateRules;
  67. });
  68. const input = (modelId: string, apiType = "openai-chat-completions", baseUrl = url) => ({
  69. providerId: "personal-go",
  70. modelId,
  71. apiType,
  72. baseUrl,
  73. });
  74. const patch = (config: ModelConfig, level: string) =>
  75. compileModelOptionMaps({
  76. reasoningLevel: { map: config.optionSpecs!.reasoningLevel!.map! },
  77. maxOutputTokens: { map: config.optionSpecs!.maxOutputTokens!.map! },
  78. }).apply({}, { reasoningLevel: level, maxOutputTokens: 4096 });
  79. describe("Todo149 Go 模板与自建供应商共用站点规则", () => {
  80. it.each(Object.entries(members))("%s 按官方协议列成员,模板只决定默认启停", (templateId, ids) => {
  81. const t = templates.find((t) => t.templateId === templateId)!;
  82. expect(t).toBeDefined();
  83. expect(t.config.builtinModelIds).toEqual(
  84. [...ids].sort((a, b) => Number(enabled.has(b)) - Number(enabled.has(a))),
  85. );
  86. expect(t.config.api).toEqual({
  87. type: templateId.endsWith("chat")
  88. ? "openai-chat-completions"
  89. : templateId.endsWith("messages")
  90. ? "anthropic-messages"
  91. : "openai-responses",
  92. baseUrl: url,
  93. });
  94. expect(t.config.access).toEqual({
  95. type: "api-key",
  96. apiKeyManagementUrl: "https://opencode.ai/auth",
  97. });
  98. expect(t.config.logo).toEqual({ type: "builtin", key: "opencode" });
  99. for (const modelId of ids) {
  100. const i = input(modelId, t.config.api.type);
  101. const fromTemplate = rules.resolve({ ...i, templateId });
  102. const manual = rules.resolve(i);
  103. expect(fromTemplate.validateComplete(), modelId).toEqual([]);
  104. expect(fromTemplate.enabled, modelId).toBe(enabled.has(modelId));
  105. expect(fromTemplate.properties?.toJSON()).toEqual(manual.properties?.toJSON());
  106. expect(fromTemplate.optionSpecs?.toJSON()).toEqual(manual.optionSpecs?.toJSON());
  107. expect(manual.properties?.supportsNativeWebSearch, modelId).toBe(false);
  108. for (const level of fromTemplate.optionSpecs!.reasoningLevel!.values!)
  109. expect(() => patch(fromTemplate, level), `${modelId}/${level}`).not.toThrow();
  110. }
  111. });
  112. // 原因:GLM 落入通用四组参数兜底,Go 上游严格校验并拒绝 enable_thinking/reasoning。
  113. it.each(["glm-5.3", "glm-5.3-flash"])(
  114. "%s 每个档位仅发送 reasoning_effort,不修改其他站点",
  115. (modelId) => {
  116. for (const level of ["low", "high", "max"]) {
  117. expect(patch(rules.resolve(input(modelId)), level)).toEqual({
  118. reasoning_effort: level,
  119. max_tokens: 4096,
  120. });
  121. expect(patch(rules.resolve(input(modelId, undefined, `${url}/`)), level)).toEqual({
  122. reasoning_effort: level,
  123. max_tokens: 4096,
  124. });
  125. }
  126. for (const other of [
  127. "https://opencode.ai/zen/v1",
  128. "https://opencode.ai.evil.test/zen/go/v1",
  129. "https://opencode.ai/zen/go/v10",
  130. "https://other.test/zen/go/v1",
  131. ]) {
  132. expect(patch(rules.resolve(input(modelId, undefined, other)), "high")).toHaveProperty(
  133. "enable_thinking",
  134. true,
  135. );
  136. }
  137. expect(patch(rules.resolve(input(modelId, "anthropic-messages")), "high")).toHaveProperty(
  138. "thinking",
  139. );
  140. },
  141. );
  142. it.each([
  143. ["glm-5.2", ["high", "max"], 1000000, 131072],
  144. ["glm-5.1", ["enabled"], 202752, 32768],
  145. ["kimi-k3", ["max"], 1048576, 131072],
  146. ["kimi-k2.7-code", ["enabled"], 262144, 262144],
  147. ["deepseek-v4-pro", ["high", "max"], 1000000, 384000],
  148. ["hy3", ["none", "low", "high"], 256000, 128000],
  149. ["hy4-preview", ["none", "high"], 1024000, 64000],
  150. ] as const)("%s 使用 Go 能力而不是同名原厂默认", (id, levels, context, max) => {
  151. const c = rules.resolve(input(id));
  152. expect(c.optionSpecs?.reasoningLevel?.values).toEqual(levels);
  153. expect(c.properties?.contextWindow).toBe(context);
  154. expect(c.optionSpecs?.maxOutputTokens?.max).toBe(max);
  155. });
  156. it("视觉与桥接分离;Qwen 的 Go 输出上限不照搬原厂", () => {
  157. expect(rules.resolve(input("glm-5.3")).properties?.inputFormat.supportsImage).toBe(false);
  158. expect(rules.resolve(input("glm-5.3-flash")).properties?.inputFormat.toJSON()).toMatchObject({
  159. supportsImage: true,
  160. supportsVideo: true,
  161. supportsPdf: true,
  162. });
  163. expect(rules.resolve(input("deepseek-v4-flash")).properties?.inputFormat.supportsImage).toBe(
  164. false,
  165. );
  166. expect(
  167. rules.resolve(input("deepseek-v4-flash-vision-exp")).properties?.inputFormat.supportsImage,
  168. ).toBe(true);
  169. for (const id of ["qwen3.7-max", "qwen3.7-plus"])
  170. expect(rules.resolve(input(id, "anthropic-messages")).optionSpecs?.maxOutputTokens?.max).toBe(
  171. 65536,
  172. );
  173. });
  174. it.each(["provider-model", "manual-provider-model"] as const)(
  175. "%s 的个人 Map 仍然优先",
  176. (type) => {
  177. const personal = new ModelConfigRules([
  178. {
  179. type,
  180. providerId: "personal-go",
  181. modelId: "glm-5.3",
  182. config: new ModelConfig({
  183. optionSpecs: {
  184. reasoningLevel: { values: ["high"], map: '{"custom_effort": reasoningLevel}' },
  185. maxOutputTokens: { max: 8192, map: '{"max_tokens": maxOutputTokens}' },
  186. },
  187. }),
  188. },
  189. ]);
  190. const c = ModelConfigRules.composeEffective(rules, personal).resolve(input("glm-5.3"));
  191. expect(patch(c, "high")).toEqual({ custom_effort: "high", max_tokens: 4096 });
  192. if (type === "manual-provider-model") expect(c.properties?.contextWindow).toBeUndefined();
  193. },
  194. );
  195. });