legacy-cli-personal-provider-config-importer.test.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { describe, expect, it } from "vitest";
  2. import { ModelConfigRules, ProviderConfigMap } from "@zcode/provider";
  3. import {
  4. importLegacyCliConfiguredDefault,
  5. importLegacyCliPersonalProviderConfig,
  6. } from "../src/app/legacy-cli-personal-provider-config-importer.js";
  7. function encoded(update: { providers: ProviderConfigMap; models: ModelConfigRules }): any {
  8. return {
  9. schemaVersion: 1,
  10. providers: update.providers.toJSON(),
  11. models: update.models.toJSON(),
  12. };
  13. }
  14. // 新存储格式按规则种类分桶,Provider 名称与配置分离;不再断言已退场的扁平 DTO。
  15. const emptyModelRules = {
  16. builtinProviderModelRules: [],
  17. manualProviderModelRules: [],
  18. modelApiRules: [],
  19. modelRules: [],
  20. providerSiteRules: [],
  21. templateModelRules: [],
  22. };
  23. describe("importLegacyCliPersonalProviderConfig", () => {
  24. it("Standalone 与 Desktop 一样丢弃内置套餐配置,API 只迁 Key", () => {
  25. const update = importLegacyCliPersonalProviderConfig({
  26. input: {
  27. provider: {
  28. "builtin:bigmodel-coding-plan": {
  29. kind: "anthropic",
  30. source: "custom",
  31. options: { apiKey: "discard", apiKeyRequired: false },
  32. models: { old: {} },
  33. },
  34. "builtin:bigmodel": {
  35. kind: "anthropic",
  36. source: "custom",
  37. name: "old",
  38. options: { apiKey: " kept ", baseURL: "https://old.test" },
  39. models: { old: {} },
  40. },
  41. "builtin:zai": { kind: "anthropic", source: "builtin", options: { apiKey: "" } },
  42. "account:zai-start-plan": {
  43. kind: "anthropic",
  44. source: "custom",
  45. options: { apiKey: "discard" },
  46. },
  47. "legacy-builtin": {
  48. kind: "anthropic",
  49. source: "builtin",
  50. options: { apiKey: "discard" },
  51. },
  52. },
  53. },
  54. });
  55. expect(update.providers.keys()).toEqual(["bigmodel-api"]);
  56. expect(update.providers.get("bigmodel-api")?.toJSON()).toEqual({
  57. group: "standard-personal",
  58. access: { type: "api-key", apiKey: "kept" },
  59. });
  60. });
  61. it("只把旧用户文件显式声明的 Provider 投影为 Personal Config", () => {
  62. const update = importLegacyCliPersonalProviderConfig({
  63. input: {
  64. model: "custom/model-a",
  65. provider: {
  66. custom: {
  67. kind: "openai-compatible",
  68. name: "Custom API",
  69. options: {
  70. apiKey: "secret",
  71. baseURL: "https://custom.example.com/v1",
  72. includeUsage: false,
  73. },
  74. models: {
  75. "model-a": {
  76. headers: { "X-Model-Route": "legacy" },
  77. limit: { context: 128_000, output: 16_000 },
  78. modalities: { input: ["text", "image", "video"] },
  79. reasoning: {
  80. enabled: true,
  81. levels: ["low", "high"],
  82. defaultLevel: "high",
  83. providerOptionsByLevel: {
  84. low: { openaiCompatible: { reasoningEffort: "low" } },
  85. high: { openaiCompatible: { reasoningEffort: "high" } },
  86. },
  87. },
  88. structured_output: true,
  89. tool_call: true,
  90. },
  91. },
  92. },
  93. },
  94. },
  95. });
  96. expect(encoded(update)).toEqual({
  97. schemaVersion: 1,
  98. providers: [
  99. {
  100. providerId: "custom",
  101. providerName: "Custom API",
  102. config: {
  103. group: "standard-personal",
  104. access: { type: "api-key", apiKey: "secret" },
  105. api: {
  106. type: "openai-chat-completions",
  107. baseUrl: "https://custom.example.com/v1",
  108. },
  109. personalModelIds: ["model-a"],
  110. modelOrder: ["model-a"],
  111. },
  112. },
  113. ],
  114. models: {
  115. ...emptyModelRules,
  116. builtinProviderModelRules: [
  117. {
  118. providerId: "custom",
  119. modelId: "model-a",
  120. config: {
  121. properties: {
  122. contextWindow: 128_000,
  123. },
  124. optionSpecs: { maxOutputTokens: { max: 16_000 } },
  125. },
  126. },
  127. ],
  128. },
  129. });
  130. });
  131. it("不把只有 model 选择、没有 provider 定义的内容迁成 Provider", () => {
  132. const update = importLegacyCliPersonalProviderConfig({
  133. input: { model: "missing/model-a" },
  134. });
  135. expect(encoded(update)).toEqual({
  136. schemaVersion: 1,
  137. providers: [],
  138. models: emptyModelRules,
  139. });
  140. });
  141. it("不把旧 CLI 中已退出产品的 ZAPI 迁成 Personal Provider", () => {
  142. const update = importLegacyCliPersonalProviderConfig({
  143. input: {
  144. provider: {
  145. "builtin:zapi": {
  146. kind: "anthropic",
  147. options: { baseURL: "https://retired.example.com", apiKey: "retired" },
  148. models: { retired: {} },
  149. },
  150. custom: {
  151. kind: "openai-compatible",
  152. options: { baseURL: "https://custom.example.com", apiKey: "current" },
  153. models: { current: {} },
  154. },
  155. },
  156. },
  157. });
  158. expect(update.providers.keys()).toEqual(["custom"]);
  159. });
  160. it("忽略旧 model options 中没有正式语义的任意字段", () => {
  161. const update = importLegacyCliPersonalProviderConfig({
  162. input: {
  163. provider: {
  164. custom: {
  165. kind: "openai-compatible",
  166. options: {
  167. apiKey: "secret",
  168. baseURL: "https://custom.example.com/v1",
  169. },
  170. models: {
  171. "model-a": {
  172. limit: { context: 128_000, output: 16_000 },
  173. options: {
  174. openaiCompatible: {
  175. reasoningSummary: "auto",
  176. metadata: { source: "legacy" },
  177. },
  178. },
  179. },
  180. "model-b": {
  181. options: { reasoningSummary: "concise" },
  182. },
  183. },
  184. },
  185. },
  186. },
  187. });
  188. expect(encoded(update).models.builtinProviderModelRules[0]?.config).not.toHaveProperty(
  189. "requestParameters",
  190. );
  191. expect(encoded(update).models.builtinProviderModelRules).toHaveLength(1);
  192. });
  193. it("迁移 options.max_tokens,但不扩展任意 extra_body 字段", () => {
  194. const update = importLegacyCliPersonalProviderConfig({
  195. input: {
  196. provider: {
  197. custom: {
  198. kind: "openai-compatible",
  199. options: {
  200. apiKey: "secret",
  201. baseURL: "https://custom.example.com/v1",
  202. },
  203. models: {
  204. direct: {
  205. options: { max_tokens: 24_000 },
  206. },
  207. wrapped: {
  208. options: {
  209. extra_body: { max_tokens: 12_000, reasoning_format: "parsed" },
  210. },
  211. },
  212. },
  213. },
  214. },
  215. },
  216. });
  217. expect(update.models.getExact("custom", "direct")?.toJSON()).toEqual({
  218. optionSpecs: { maxOutputTokens: { max: 24_000 } },
  219. });
  220. expect(update.models.getExact("custom", "wrapped")).toBeUndefined();
  221. });
  222. it("忽略没有生产消费方的 reasoning replay 元数据", () => {
  223. const update = importLegacyCliPersonalProviderConfig({
  224. input: {
  225. provider: {
  226. custom: {
  227. kind: "openai-compatible",
  228. options: {
  229. apiKey: "secret",
  230. baseURL: "https://custom.example.com/v1",
  231. },
  232. models: {
  233. "model-a": {
  234. interleaved: { field: "reasoning_content" },
  235. reasoningContentField: "reasoning_content",
  236. },
  237. },
  238. },
  239. },
  240. },
  241. });
  242. expect(update.providers.get("custom")).toBeDefined();
  243. expect(encoded(update).models).toEqual(emptyModelRules);
  244. });
  245. it("无 API Key Provider 在正式类型建立前继续留在兼容链路", () => {
  246. expect(() =>
  247. importLegacyCliPersonalProviderConfig({
  248. input: {
  249. provider: {
  250. local: {
  251. kind: "openai-compatible",
  252. options: {
  253. apiKeyRequired: false,
  254. baseURL: "http://127.0.0.1:11434/v1",
  255. },
  256. models: { "model-a": {} },
  257. },
  258. },
  259. },
  260. }),
  261. ).toThrow(/local/);
  262. });
  263. });
  264. describe("importLegacyCliConfiguredDefault", () => {
  265. it("把旧用户文件的 main model 迁移为 Environment Configured Default", () => {
  266. expect({
  267. schemaVersion: 1,
  268. configuredDefault: importLegacyCliConfiguredDefault({
  269. model: {
  270. main: "custom/model-a",
  271. lite: "custom/model-b",
  272. },
  273. }),
  274. }).toEqual({
  275. schemaVersion: 1,
  276. configuredDefault: {
  277. providerId: "custom",
  278. modelId: "model-a",
  279. },
  280. });
  281. });
  282. it("没有显式 model 时不产生迁移文档", () => {
  283. expect(importLegacyCliConfiguredDefault({ provider: {} })).toBeNull();
  284. });
  285. it("不把已退出产品的 ZAPI 迁成 Environment Configured Default", () => {
  286. expect(importLegacyCliConfiguredDefault({ model: "builtin:zapi/retired" })).toBeNull();
  287. });
  288. });