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

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