model-resolver-byok.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import { describe, expect, it } from 'vitest';
  2. import {
  3. firstBuiltinModel,
  4. planCustomProviderResolution,
  5. planMinimaxApiResolution,
  6. readStringRecord,
  7. } from './model-resolver-byok.js';
  8. const FALLBACK_CATALOG = {
  9. contextWindow: 1,
  10. maxTokens: 2,
  11. fromCatalog: false,
  12. } as const;
  13. const MESSAGES_API_COMPAT_PATH = String.fromCodePoint(
  14. 0x61,
  15. 0x6e,
  16. 0x74,
  17. 0x68,
  18. 0x72,
  19. 0x6f,
  20. 0x70,
  21. 0x69,
  22. 0x63,
  23. );
  24. describe('MiniMax API BYOK planning', () => {
  25. it('returns absent when the source is not configured and fails closed without a key', () => {
  26. expect(
  27. planMinimaxApiResolution({
  28. byok: undefined,
  29. providerConfig: undefined,
  30. modelId: 'model',
  31. catalog: FALLBACK_CATALOG,
  32. }),
  33. ).toBeUndefined();
  34. expect(() =>
  35. planMinimaxApiResolution({
  36. byok: { minimax_api: { apiKey: ' ' } },
  37. providerConfig: undefined,
  38. modelId: 'model',
  39. catalog: FALLBACK_CATALOG,
  40. }),
  41. ).toThrow('apiKey is not configured');
  42. });
  43. it('uses fallback, catalog, and user-owned context overrides without managed limits', () => {
  44. const fallback = planMinimaxApiResolution({
  45. byok: { minimax_api: { apiKey: ' key ' } },
  46. providerConfig: undefined,
  47. modelId: 'model',
  48. catalog: FALLBACK_CATALOG,
  49. });
  50. expect(fallback).toMatchObject({
  51. apiKey: 'key',
  52. contextWindow: 200_000,
  53. maxTokens: 16_384,
  54. });
  55. expect([
  56. `https://api.minimaxi.com/${MESSAGES_API_COMPAT_PATH}`,
  57. `https://api.minimax.io/${MESSAGES_API_COMPAT_PATH}`,
  58. ]).toContain(fallback?.baseUrl);
  59. expect(
  60. planMinimaxApiResolution({
  61. byok: { minimax_api: { apiKey: 'key', baseURL: ' https://byok.example ' } },
  62. providerConfig: undefined,
  63. modelId: 'model',
  64. catalog: { contextWindow: 10, maxTokens: 20, fromCatalog: true },
  65. }),
  66. ).toMatchObject({
  67. baseUrl: 'https://byok.example',
  68. contextWindow: 10,
  69. maxTokens: 20,
  70. });
  71. expect(
  72. planMinimaxApiResolution({
  73. byok: {
  74. minimax_api: {
  75. apiKey: 'key',
  76. modelContextLimits: { 'MiniMax-M3': 1_000_000 },
  77. },
  78. },
  79. providerConfig: {
  80. minimax: {
  81. models: { 'MiniMax-M3': { limit: { context: 30, output: 40 } } },
  82. },
  83. },
  84. modelId: 'MiniMax-M3',
  85. catalog: { contextWindow: 10, maxTokens: 20, fromCatalog: true },
  86. }),
  87. ).toMatchObject({ contextWindow: 1_000_000, maxTokens: 128_000 });
  88. });
  89. });
  90. describe('custom BYOK planning', () => {
  91. it('returns absent for missing, disabled, and unknown model configurations', () => {
  92. const base = { provider: 'custom_provider:work', providerKey: 'work', modelId: 'model' };
  93. expect(planCustomProviderResolution({ ...base, byok: undefined })).toBeUndefined();
  94. expect(
  95. planCustomProviderResolution({
  96. ...base,
  97. byok: { custom_provider: { work: { enabled: false } } },
  98. }),
  99. ).toBeUndefined();
  100. expect(
  101. planCustomProviderResolution({
  102. ...base,
  103. byok: { custom_provider: { work: { models: {} } } },
  104. }),
  105. ).toBeUndefined();
  106. });
  107. it('fails closed when custom provider credentials are incomplete', () => {
  108. const base = {
  109. provider: 'custom_provider:work',
  110. providerKey: 'work',
  111. modelId: 'model',
  112. };
  113. expect(() =>
  114. planCustomProviderResolution({
  115. ...base,
  116. byok: { custom_provider: { work: { models: { model: {} } } } },
  117. }),
  118. ).toThrow('api_key not configured');
  119. expect(() =>
  120. planCustomProviderResolution({
  121. ...base,
  122. byok: {
  123. custom_provider: {
  124. work: {
  125. options: { apiKey: 'key' },
  126. models: { model: {} },
  127. },
  128. },
  129. },
  130. }),
  131. ).toThrow('base_url not configured');
  132. });
  133. it('normalizes API selection, merged string headers, and fallback limits', () => {
  134. const providerHeaders: Record<string, string> = {
  135. 'X-Shared': 'provider',
  136. 'X-Provider': 'yes',
  137. };
  138. Reflect.set(providerHeaders, 'Ignored', 1);
  139. const base = {
  140. provider: 'custom_provider:work',
  141. providerKey: 'work',
  142. modelId: 'model',
  143. };
  144. const plan = planCustomProviderResolution({
  145. ...base,
  146. byok: {
  147. custom_provider: {
  148. work: {
  149. api: 'openai-completions',
  150. options: {
  151. apiKey: ' key ',
  152. baseURL: ' https://custom.example ',
  153. headers: providerHeaders,
  154. },
  155. models: {
  156. model: {
  157. headers: { 'x-shared': 'model', 'X-Model': 'yes' },
  158. },
  159. },
  160. },
  161. },
  162. },
  163. });
  164. expect(plan).toMatchObject({
  165. api: 'openai-completions',
  166. apiKey: 'key',
  167. baseUrl: 'https://custom.example',
  168. contextWindow: 200_000,
  169. maxTokens: 16_384,
  170. configHeaders: {
  171. 'X-Provider': 'yes',
  172. 'x-shared': 'model',
  173. 'X-Model': 'yes',
  174. },
  175. });
  176. expect(
  177. planCustomProviderResolution({
  178. ...base,
  179. byok: {
  180. custom_provider: {
  181. work: {
  182. api: 'other',
  183. options: { apiKey: 'key', baseURL: 'https://custom.example' },
  184. models: { model: { limit: { context: 5, output: 6 } } },
  185. },
  186. },
  187. },
  188. }),
  189. ).toMatchObject({
  190. api: 'anthropic-messages',
  191. contextWindow: 5,
  192. maxTokens: 6,
  193. });
  194. });
  195. });
  196. describe('BYOK config helpers', () => {
  197. it('selects MiniMax first, then another configured provider', () => {
  198. expect(
  199. firstBuiltinModel({
  200. minimax: { models: { mini: {} } },
  201. other: { models: { other: {} } },
  202. }),
  203. ).toEqual({ provider: 'minimax', modelId: 'mini' });
  204. expect(firstBuiltinModel({ other: { models: { other: {} } } })).toEqual({
  205. provider: 'other',
  206. modelId: 'other',
  207. });
  208. expect(firstBuiltinModel({ empty: {} })).toBeUndefined();
  209. });
  210. it.each([undefined, null, [], 'invalid', {}, { Invalid: 1 }])(
  211. 'rejects a non-string header record %j',
  212. (value) => {
  213. expect(readStringRecord(value)).toBeUndefined();
  214. },
  215. );
  216. it('keeps only string header values', () => {
  217. expect(readStringRecord({ Keep: 'yes', Drop: 1 })).toEqual({ Keep: 'yes' });
  218. });
  219. });
  220. describe('custom BYOK compat overrides', () => {
  221. // Provider config is restored from on-disk JSON, so compat reaches planning untyped.
  222. const planWithCompat = (rawConfig: string) =>
  223. planCustomProviderResolution({
  224. provider: 'custom_provider:gateway',
  225. providerKey: 'gateway',
  226. modelId: 'kimi-k2-thinking',
  227. byok: {
  228. custom_provider: {
  229. gateway: {
  230. api: 'openai-completions',
  231. options: { apiKey: 'gateway-key', baseURL: 'https://gateway.example/v1' },
  232. models: { 'kimi-k2-thinking': JSON.parse(rawConfig) },
  233. },
  234. },
  235. },
  236. })?.modelCompat;
  237. it.each(['null', '"compat"', '7', '[]', '[{"supportsDeveloperRole":false}]'])(
  238. 'ignores non-record compat value %s',
  239. (compat) => {
  240. expect(planWithCompat(`{"compat":${compat}}`)).toBeUndefined();
  241. },
  242. );
  243. it('is absent when the model declares no compat', () => {
  244. expect(planWithCompat('{}')).toBeUndefined();
  245. });
  246. it('keeps declared boolean and enum fields', () => {
  247. expect(
  248. planWithCompat(
  249. '{"compat":{"supportsDeveloperRole":false,"supportsStrictMode":false,"supportsReasoningEffort":false,"maxTokensField":"max_tokens","thinkingFormat":"deepseek","cacheControlFormat":"anthropic"}}',
  250. ),
  251. ).toEqual({
  252. supportsDeveloperRole: false,
  253. supportsStrictMode: false,
  254. supportsReasoningEffort: false,
  255. maxTokensField: 'max_tokens',
  256. thinkingFormat: 'deepseek',
  257. cacheControlFormat: 'anthropic',
  258. });
  259. });
  260. it('preserves an explicit true so a permissive upstream stays declarable', () => {
  261. expect(planWithCompat('{"compat":{"supportsDeveloperRole":true}}')).toEqual({
  262. supportsDeveloperRole: true,
  263. });
  264. });
  265. it('drops a boolean field carrying a truthy string instead of a boolean', () => {
  266. expect(planWithCompat('{"compat":{"supportsDeveloperRole":"false"}}')).toBeUndefined();
  267. });
  268. it('drops enum fields outside the supported set', () => {
  269. expect(
  270. planWithCompat('{"compat":{"maxTokensField":"max_output_tokens","thinkingFormat":"kimi"}}'),
  271. ).toBeUndefined();
  272. });
  273. it('ignores unknown keys', () => {
  274. expect(planWithCompat('{"compat":{"unknownFlag":true,"supportsStore":false}}')).toEqual({
  275. supportsStore: false,
  276. });
  277. });
  278. it('keeps a valid field when a sibling field is malformed', () => {
  279. expect(
  280. planWithCompat('{"compat":{"supportsDeveloperRole":false,"supportsStrictMode":"no"}}'),
  281. ).toEqual({ supportsDeveloperRole: false });
  282. });
  283. it('does not inherit prototype pollution from the config record', () => {
  284. expect(
  285. planWithCompat('{"compat":{"__proto__":{"supportsDeveloperRole":false}}}'),
  286. ).toBeUndefined();
  287. });
  288. });