service-input.test.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import { describe, expect, it } from 'vitest';
  2. import { LocalModelProviderError, type LocalModelConfig } from '../contracts.js';
  3. import {
  4. assertValidRawApiKey,
  5. mergeModelsFromInputs,
  6. modelsFromInputs,
  7. normalizeApiFormat,
  8. normalizeApiKeyUpdate,
  9. normalizeHeaderNames,
  10. normalizeHeaders,
  11. removeHeaderCaseInsensitive,
  12. } from './service-input.js';
  13. describe('model provider input normalization', () => {
  14. it('preserves declared capabilities on create and merges updates without losing limits', () => {
  15. const created = modelsFromInputs([
  16. {
  17. modelId: 'vision-model',
  18. capabilities: { support_image: true, max_image_bytes_inline: 1024 },
  19. },
  20. ]);
  21. expect(created['vision-model']?.capabilities).toEqual({
  22. support_image: true,
  23. max_image_bytes_inline: 1024,
  24. });
  25. const patch = [{ modelId: 'vision-model', capabilities: { support_image: false } }];
  26. for (const updated of [modelsFromInputs(patch, created), mergeModelsFromInputs(created, patch)]) {
  27. expect(updated['vision-model']?.capabilities).toEqual({
  28. support_image: false,
  29. max_image_bytes_inline: 1024,
  30. });
  31. const renamed = mergeModelsFromInputs(updated, [
  32. { modelId: 'vision-model', displayName: 'Renamed' },
  33. ]);
  34. expect(renamed['vision-model']?.capabilities).toEqual(updated['vision-model']?.capabilities);
  35. }
  36. expect(created['vision-model']?.capabilities?.support_image).toBe(true);
  37. });
  38. it('normalizes API keys and formats', () => {
  39. expect(() => assertValidRawApiKey(undefined as unknown as string)).toThrow(
  40. LocalModelProviderError,
  41. );
  42. expect(() => assertValidRawApiKey('sk-a****z')).toThrow(LocalModelProviderError);
  43. expect(assertValidRawApiKey(' sk-raw ')).toBe('sk-raw');
  44. expect(normalizeApiKeyUpdate(undefined)).toEqual({ kind: 'keep' });
  45. expect(normalizeApiKeyUpdate(' ')).toEqual({ kind: 'clear' });
  46. expect(normalizeApiKeyUpdate(' sk-next ')).toEqual({ kind: 'set', apiKey: 'sk-next' });
  47. expect(normalizeApiFormat(undefined)).toBeUndefined();
  48. expect(normalizeApiFormat(' ')).toBeUndefined();
  49. expect(normalizeApiFormat(' openai-responses ')).toBe('openai-responses');
  50. expect(() => normalizeApiFormat('unsupported')).toThrowError(
  51. expect.objectContaining({ code: 'INVALID_API_FORMAT' }),
  52. );
  53. });
  54. it('normalizes and validates headers case-insensitively', () => {
  55. expect(normalizeHeaders(undefined)).toBeUndefined();
  56. expect(normalizeHeaders({})).toBeUndefined();
  57. expect(normalizeHeaders({ ' X-Tenant ': ' tenant-a ' })).toEqual({ 'X-Tenant': 'tenant-a' });
  58. expect(() => normalizeHeaders({ '': 'value' })).toThrowError(
  59. expect.objectContaining({ code: 'INVALID_HEADERS' }),
  60. );
  61. expect(() => normalizeHeaders({ 'X-Number': 1 as unknown as string })).toThrowError(
  62. expect.objectContaining({ code: 'INVALID_HEADERS' }),
  63. );
  64. expect(() => normalizeHeaders({ 'X-Test': 'one', 'x-test': 'two' })).toThrowError(
  65. expect.objectContaining({ code: 'INVALID_HEADERS' }),
  66. );
  67. expect(normalizeHeaderNames(undefined)).toEqual([]);
  68. expect(normalizeHeaderNames([' X-A ', '', 'X-B'])).toEqual(['X-A', 'X-B']);
  69. expect(() => normalizeHeaderNames(['X-A', 'x-a'])).toThrowError(
  70. expect.objectContaining({ code: 'INVALID_HEADERS' }),
  71. );
  72. const headers = { Authorization: 'Bearer test', 'X-Tenant': 'tenant-a' };
  73. removeHeaderCaseInsensitive(headers, 'authorization');
  74. expect(headers).toEqual({ 'X-Tenant': 'tenant-a' });
  75. });
  76. it('maps a fully populated model input and preserves existing metadata', () => {
  77. const existing: Record<string, LocalModelConfig> = {
  78. 'model-1': { name: 'Old name', capabilities: { experimental: true } },
  79. };
  80. const models = modelsFromInputs(
  81. [
  82. {
  83. modelId: ' model-1 ',
  84. displayName: 'Model 1',
  85. configurationSource: 'discovered',
  86. enabled: false,
  87. attachment: true,
  88. reasoning: true,
  89. toolCall: true,
  90. temperature: true,
  91. modalities: { input: ['text', 'image'], output: ['text'] },
  92. limit: { context: 200_000, output: 32_000 },
  93. thinkingConfig: { mode: 'switchable', defaultValue: 'true' },
  94. effortOptions: ['low', 'high'],
  95. },
  96. ],
  97. existing,
  98. );
  99. expect(models['model-1']).toMatchObject({
  100. name: 'Model 1',
  101. configuration_source: 'discovered',
  102. enabled: false,
  103. attachment: true,
  104. reasoning: true,
  105. tool_call: true,
  106. temperature: true,
  107. modalities: { input: ['text', 'image'], output: ['text'] },
  108. limit: { context: 200_000, output: 32_000 },
  109. thinking_config: { mode: 'switchable', default_value: 'true' },
  110. thinking: { effortOptions: ['low', 'high'] },
  111. capabilities: { experimental: true },
  112. });
  113. });
  114. it('persists declared Kimi K3 effort options as thinking metadata', () => {
  115. expect(
  116. modelsFromInputs([
  117. {
  118. modelId: 'kimi-k3',
  119. displayName: 'Kimi K3',
  120. reasoning: true,
  121. effortOptions: ['low', 'high', 'max'],
  122. },
  123. ])['kimi-k3'],
  124. ).toEqual({
  125. name: 'Kimi K3',
  126. reasoning: true,
  127. thinking: { effortOptions: ['low', 'high', 'max'] },
  128. });
  129. });
  130. it('applies implicit thinking only to eligible new models', () => {
  131. expect(modelsFromInputs([{ modelId: 'implicit' }], undefined, true).implicit).toMatchObject({
  132. reasoning: true,
  133. thinking_config: { mode: 'switchable', default_value: 'true' },
  134. });
  135. expect(
  136. modelsFromInputs([{ modelId: 'explicit-off', reasoning: false }], undefined, true)[
  137. 'explicit-off'
  138. ],
  139. ).toEqual({ reasoning: false });
  140. expect(
  141. modelsFromInputs(
  142. [{ modelId: 'explicit-thinking', thinkingConfig: { mode: 'forced_on' } }],
  143. undefined,
  144. true,
  145. )['explicit-thinking'],
  146. ).toEqual({ thinking_config: { mode: 'forced_on' } });
  147. expect(
  148. modelsFromInputs(
  149. [{ modelId: 'effort-options', effortOptions: ['low', 'high'] }],
  150. undefined,
  151. true,
  152. )['effort-options'],
  153. ).toEqual({ reasoning: true, thinking: { effortOptions: ['low', 'high'] } });
  154. expect(
  155. modelsFromInputs(
  156. [{ modelId: 'existing' }],
  157. { existing: { thinking_config: { mode: 'switchable', default_value: 'false' } } },
  158. true,
  159. ).existing,
  160. ).toEqual({ thinking_config: { mode: 'switchable', default_value: 'false' } });
  161. expect(modelsFromInputs([{ modelId: 'old' }], { old: {} }, true).old).toEqual({});
  162. });
  163. it('merges nested model fields and cleans replaced thinking state', () => {
  164. const existing: Record<string, LocalModelConfig> = {
  165. 'model-1': {
  166. limit: { context: 100_000, output: 8_000 },
  167. modalities: { input: ['text'], output: ['text'] },
  168. thinking_config: { mode: 'switchable', default_value: 'false' },
  169. thinking: {
  170. effort: 'medium',
  171. defaultEffort: 'medium',
  172. effortOptions: ['medium'],
  173. } as unknown as NonNullable<LocalModelConfig['thinking']>,
  174. },
  175. };
  176. const merged = mergeModelsFromInputs(existing, [
  177. {
  178. modelId: 'model-1',
  179. limit: { context: 200_000 },
  180. modalities: { input: ['text', 'image'] },
  181. thinkingConfig: { mode: 'forced_on' },
  182. effortOptions: ['high'],
  183. },
  184. { modelId: 'new-model', displayName: 'New model' },
  185. ]);
  186. expect(merged['model-1']).toMatchObject({
  187. limit: { context: 200_000, output: 8_000 },
  188. modalities: { input: ['text', 'image'], output: ['text'] },
  189. thinking_config: { mode: 'forced_on', default_value: 'false' },
  190. thinking: { effortOptions: ['high'] },
  191. });
  192. expect(merged['model-1']?.thinking).not.toHaveProperty('effort');
  193. expect(merged['model-1']?.thinking).not.toHaveProperty('defaultEffort');
  194. expect(merged['new-model']).toEqual({ name: 'New model' });
  195. expect(
  196. mergeModelsFromInputs(
  197. {
  198. cleared: {
  199. thinking: { effort: 'low' } as unknown as NonNullable<LocalModelConfig['thinking']>,
  200. },
  201. },
  202. [{ modelId: 'cleared', effortOptions: [] }],
  203. ).cleared,
  204. ).not.toHaveProperty('thinking');
  205. });
  206. it('removes incompatible MiniMax M3 state and rejects invalid model fields', () => {
  207. const merged = mergeModelsFromInputs(
  208. {
  209. 'MiniMax-M3': {
  210. thinking_config: { mode: 'switchable' },
  211. variants: { thinking: {}, 'none-thinking': {} },
  212. },
  213. },
  214. [{ modelId: 'MiniMax-M3', reasoning: false }],
  215. );
  216. expect(merged['MiniMax-M3']).toEqual({ reasoning: false });
  217. expect(() => modelsFromInputs([{ modelId: ' ' }])).toThrowError(
  218. expect.objectContaining({ code: 'VALIDATION_ERROR' }),
  219. );
  220. expect(() =>
  221. modelsFromInputs([{ modelId: 'model', configurationSource: 'generated' }]),
  222. ).toThrowError(expect.objectContaining({ code: 'VALIDATION_ERROR' }));
  223. expect(() => modelsFromInputs([{ modelId: 'model', limit: { context: 0 } }])).toThrowError(
  224. expect.objectContaining({ code: 'VALIDATION_ERROR' }),
  225. );
  226. expect(() => modelsFromInputs([{ modelId: 'model', limit: { output: 1.5 } }])).toThrowError(
  227. expect.objectContaining({ code: 'VALIDATION_ERROR' }),
  228. );
  229. });
  230. });