|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import assert from 'node:assert/strict'; |
| 21 | +import test from 'node:test'; |
| 22 | +import { |
| 23 | + applyModelFactOverride, |
| 24 | + applyModelFactOverridesToConnection, |
| 25 | + decodeModelFactsDocument, |
| 26 | + modelFactKey, |
| 27 | +} from '../model-facts.js'; |
| 28 | + |
| 29 | +test('model facts use provider:model keys and merge fields without replacing provider facts', () => { |
| 30 | + const key = modelFactKey('openai', 'o4-mini'); |
| 31 | + const document = decodeModelFactsDocument({ |
| 32 | + schemaVersion: 1, |
| 33 | + overrides: { [key]: { contextWindow: 200_000, capabilities: { vision: false } } }, |
| 34 | + }); |
| 35 | + const model = applyModelFactOverride( |
| 36 | + { |
| 37 | + id: 'o4-mini', |
| 38 | + displayName: 'Provider name', |
| 39 | + maxOutputTokens: 4_000, |
| 40 | + capabilities: { chat: true, vision: true }, |
| 41 | + }, |
| 42 | + document.overrides[key], |
| 43 | + ); |
| 44 | + assert.equal(model.displayName, 'Provider name'); |
| 45 | + assert.equal(model.contextWindow, 200_000); |
| 46 | + assert.deepEqual(model.capabilities, { chat: true, vision: false }); |
| 47 | +}); |
| 48 | + |
| 49 | +test('malformed and unknown model fact fields are rejected', () => { |
| 50 | + assert.throws(() => |
| 51 | + decodeModelFactsDocument({ schemaVersion: 1, overrides: { 'openai:o4-mini': {} } }), |
| 52 | + ); |
| 53 | + assert.throws(() => |
| 54 | + decodeModelFactsDocument({ schemaVersion: 1, overrides: { 'openai:o4-mini': { nope: true } } }), |
| 55 | + ); |
| 56 | + assert.throws(() => |
| 57 | + decodeModelFactsDocument({ schemaVersion: 1, overrides: { 'o4-mini': { contextWindow: 1 } } }), |
| 58 | + ); |
| 59 | + assert.throws(() => |
| 60 | + decodeModelFactsDocument({ |
| 61 | + schemaVersion: 1, |
| 62 | + overrides: { 'openai:o4-mini': { contextWindow: 0 } }, |
| 63 | + }), |
| 64 | + ); |
| 65 | + assert.throws(() => |
| 66 | + decodeModelFactsDocument({ |
| 67 | + schemaVersion: 1, |
| 68 | + overrides: { 'openai:o4-mini': { capabilities: { toString: true } } }, |
| 69 | + }), |
| 70 | + ); |
| 71 | + assert.throws(() => |
| 72 | + decodeModelFactsDocument({ |
| 73 | + schemaVersion: 1, |
| 74 | + overrides: { 'toString:model': { contextWindow: 1 } }, |
| 75 | + }), |
| 76 | + ); |
| 77 | +}); |
| 78 | + |
| 79 | +test('model fact keys preserve colons in provider model ids', () => { |
| 80 | + const key = modelFactKey('ollama-cloud', 'gpt-oss:120b'); |
| 81 | + assert.equal(key, 'ollama-cloud:gpt-oss:120b'); |
| 82 | + const document = decodeModelFactsDocument({ |
| 83 | + schemaVersion: 1, |
| 84 | + overrides: { [key]: { contextWindow: 131_072 } }, |
| 85 | + }); |
| 86 | + assert.equal(document.overrides[key]?.contextWindow, 131_072); |
| 87 | +}); |
| 88 | + |
| 89 | +test('override-only models are projected only when enabled', () => { |
| 90 | + const connection = { |
| 91 | + slug: 'openai', |
| 92 | + providerType: 'openai' as const, |
| 93 | + defaultModel: 'custom', |
| 94 | + enabledModelIds: ['custom'], |
| 95 | + models: [{ id: 'provider-model' }], |
| 96 | + }; |
| 97 | + const result = applyModelFactOverridesToConnection(connection, { |
| 98 | + 'openai:custom': { contextWindow: 64_000 }, |
| 99 | + 'openai:hidden': { contextWindow: 1_000 }, |
| 100 | + }); |
| 101 | + assert.deepEqual(result.models, [ |
| 102 | + { id: 'provider-model' }, |
| 103 | + { |
| 104 | + id: 'custom', |
| 105 | + contextWindow: 64_000, |
| 106 | + inputLimit: 64_000, |
| 107 | + factOverriddenFields: ['contextWindow', 'inputLimit'], |
| 108 | + }, |
| 109 | + ]); |
| 110 | +}); |
| 111 | + |
| 112 | +test('context window facts cannot be truncated by an older input limit', () => { |
| 113 | + const result = applyModelFactOverride( |
| 114 | + { id: 'model', contextWindow: 8_192, inputLimit: 8_192 }, |
| 115 | + { contextWindow: 200_000 }, |
| 116 | + ); |
| 117 | + assert.equal(result.contextWindow, 200_000); |
| 118 | + assert.equal(result.inputLimit, 200_000); |
| 119 | +}); |
| 120 | + |
| 121 | +test('overrides replace fields on discovered models while preserving untouched provider facts', () => { |
| 122 | + const result = applyModelFactOverridesToConnection( |
| 123 | + { |
| 124 | + providerType: 'openai', |
| 125 | + defaultModel: 'provider-model', |
| 126 | + enabledModelIds: ['provider-model'], |
| 127 | + models: [ |
| 128 | + { id: 'provider-model', contextWindow: 8_000, capabilities: { chat: true, vision: true } }, |
| 129 | + ], |
| 130 | + }, |
| 131 | + { 'openai:provider-model': { contextWindow: 64_000, capabilities: { vision: false } } }, |
| 132 | + ); |
| 133 | + assert.equal(result.models?.[0]?.contextWindow, 64_000); |
| 134 | + assert.deepEqual(result.models?.[0]?.capabilities, { chat: true, vision: false }); |
| 135 | +}); |
| 136 | + |
| 137 | +test('modality overrides merge directions independently', () => { |
| 138 | + const result = applyModelFactOverride( |
| 139 | + { |
| 140 | + id: 'multimodal', |
| 141 | + modalities: { input: ['text', 'image', 'pdf'], output: ['text', 'audio'] }, |
| 142 | + }, |
| 143 | + { modalities: { input: ['text'] } }, |
| 144 | + ); |
| 145 | + assert.deepEqual(result.modalities, { |
| 146 | + input: ['text'], |
| 147 | + output: ['text', 'audio'], |
| 148 | + }); |
| 149 | +}); |
| 150 | + |
| 151 | +test('model capabilities preserve web search facts from metadata and overrides', () => { |
| 152 | + const result = applyModelFactOverride( |
| 153 | + { id: 'web-model', capabilities: { webSearch: true } }, |
| 154 | + { capabilities: { chat: true } }, |
| 155 | + ); |
| 156 | + assert.deepEqual(result.capabilities, { webSearch: true, chat: true }); |
| 157 | +}); |
| 158 | + |
| 159 | +test('model fact overrides accept parallel tool-call capability metadata', () => { |
| 160 | + const document = decodeModelFactsDocument({ |
| 161 | + schemaVersion: 1, |
| 162 | + overrides: { 'openai:tool-model': { capabilities: { parallelToolCalls: false } } }, |
| 163 | + }); |
| 164 | + assert.deepEqual(document.overrides['openai:tool-model']?.capabilities, { |
| 165 | + parallelToolCalls: false, |
| 166 | + }); |
| 167 | +}); |
0 commit comments