diff --git a/apps/host/server/index.mjs b/apps/host/server/index.mjs index 8d40296321..763476a6db 100644 --- a/apps/host/server/index.mjs +++ b/apps/host/server/index.mjs @@ -80,6 +80,7 @@ import { listEngines, changeEngine } from './engine-library.mjs'; import { forbidden as acquisitionForbidden } from './acquisition.mjs'; import { randomBytes, timingSafeEqual } from 'node:crypto'; import { nativeGateway } from './native-gateway.mjs'; +import { nativeModelEntries } from './native-models.mjs'; import { proxyCompletion } from './inference.mjs'; import { apiPath } from './api-paths.mjs'; import { findFreePort, listenProblem } from './ports.mjs'; @@ -2131,7 +2132,7 @@ async function openai(req, res, path, method, via) { async function routeInference(req, res, path, method, via) { if(native.state().instances.length){ - if(path==='/v1/models'&&method==='GET')return json(res,200,{object:'list',data:native.state().instances.filter(i=>i.status==='ready').map(i=>({id:i.apiModelId,object:'model',owned_by:'strata',meta:{instanceId:i.id,gpus:i.gpus,label:i.label,aliases:i.aliases}}))}); + if(path==='/v1/models'&&method==='GET')return json(res,200,{object:'list',data:nativeModelEntries(native.state().instances)}); if(method==='POST'&&['/v1/chat/completions','/v1/messages','/v1/responses'].includes(path)){if(store.get().serving?.pauseServing)return error(res,503,'New API requests are paused.');if(path==='/v1/responses'&&!native.state().capabilities.responses)return error(res,501,'Update the native Strata installation to use the Responses API.');return nativeGateway({req,res,path,body:await readBody(req,12*1024*1024),control:native,serving,mcpAllowed:model=>serving.mcpToolsAllowed(req,via,model)});} } if(path==='/v1/responses'&&method==='POST')return error(res,503,'Load a native Strata model to use the Responses API.'); diff --git a/apps/host/server/native-models.mjs b/apps/host/server/native-models.mjs new file mode 100644 index 0000000000..909b4c4d68 --- /dev/null +++ b/apps/host/server/native-models.mjs @@ -0,0 +1,9 @@ +/** The native model list for GET /v1/models. meta.n_ctx is the context window in tokens, the key serve/server.py uses; it is left out when unknown, never 0 (#351, #3931). */ +const tokens = value => Number.isInteger(value) && value > 0 ? value : undefined; + +export function nativeModelEntries(instances) { + return (instances ?? []).filter(i => i.status === 'ready').map(i => { + const n_ctx = tokens(i.metrics?.engine?.max_context) ?? tokens(i.context); + return { id: i.apiModelId, object: 'model', owned_by: 'strata', meta: { instanceId: i.id, gpus: i.gpus, label: i.label, aliases: i.aliases, ...(n_ctx ? { n_ctx } : {}) } }; + }); +} diff --git a/apps/host/tests/native-models.test.mjs b/apps/host/tests/native-models.test.mjs new file mode 100644 index 0000000000..34bdb28b89 --- /dev/null +++ b/apps/host/tests/native-models.test.mjs @@ -0,0 +1,61 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { nativeModelEntries } from '../server/native-models.mjs'; +const ready = (patch = {}) => ({ id: 'inst-a', apiModelId: 'qwen-q2_0', status: 'ready', gpus: [0], label: 'Qwen 3.6', aliases: ['qwen-q2_0'], context: 65536, metrics: null, ...patch }); + +test('the configured context is reported as n_ctx and the other keys are unchanged', () => { + assert.deepEqual(nativeModelEntries([ready()]), [{ id: 'qwen-q2_0', object: 'model', owned_by: 'strata', meta: { instanceId: 'inst-a', gpus: [0], label: 'Qwen 3.6', aliases: ['qwen-q2_0'], n_ctx: 65536 } }]); +}); + +test('the engine-reported window wins over the config', () => { + const [entry] = nativeModelEntries([ready({ context: 65536, metrics: { engine: { max_context: 32768 } } })]); + assert.equal(entry.meta.n_ctx, 32768); +}); + +test('an unknown engine value falls back to the config', () => { + for (const max_context of [0, null, undefined, NaN, -1, '32768']) { + const [entry] = nativeModelEntries([ready({ context: 65536, metrics: { engine: { max_context } } })]); + assert.equal(entry.meta.n_ctx, 65536, `max_context=${String(max_context)}`); + } +}); + +test('unknown context is left out, not 0', () => { + for (const context of [0, undefined, null, NaN, Infinity, -4096, 1.5, '65536']) { + const [entry] = nativeModelEntries([ready({ context, metrics: null })]); + assert.equal('n_ctx' in entry.meta, false, `context=${String(context)}`); + assert.deepEqual(entry.meta, { instanceId: 'inst-a', gpus: [0], label: 'Qwen 3.6', aliases: ['qwen-q2_0'] }); + } + const [bothUnknown] = nativeModelEntries([ready({ context: 0, metrics: { engine: { max_context: 0 } } })]); + assert.equal('n_ctx' in bothUnknown.meta, false); +}); + +test('only ready instances are listed, in input order', () => { + const result = nativeModelEntries([ + ready({ id: 'a', apiModelId: 'm-a' }), + ready({ id: 'b', apiModelId: 'm-b', status: 'loading' }), + ready({ id: 'c', apiModelId: 'm-c', status: 'error' }), + ready({ id: 'd', apiModelId: 'm-d', status: 'unloading' }), + ready({ id: 'e', apiModelId: 'm-e', context: 131072 }), + ]); + assert.deepEqual(result.map(e => e.id), ['m-a', 'm-e']); + assert.equal(result[1].meta.n_ctx, 131072); +}); + +test('empty input', () => { + assert.deepEqual(nativeModelEntries([]), []); + assert.deepEqual(nativeModelEntries(undefined), []); + assert.deepEqual(nativeModelEntries(null), []); +}); + +test('the input is not changed', () => { + const input = [ready({ metrics: { engine: { max_context: 32768 } } })]; + const copy = structuredClone(input); + nativeModelEntries(input); + assert.deepEqual(input, copy); +}); + +test('n_ctx is the only key added to meta', () => { + const [entry] = nativeModelEntries([ready()]); + assert.deepEqual(Object.keys(entry).sort(), ['id', 'meta', 'object', 'owned_by']); + assert.deepEqual(Object.keys(entry.meta).sort(), ['aliases', 'gpus', 'instanceId', 'label', 'n_ctx']); +});