Serving API: /v1/models also says context_length, so clients need not use the engine's private port (#5893)

#5893: the reporter connected Unsloth Studio to the engine's private port (key new
at every load) because only there did /v1/models report the context window; every
reload broke the connection. a82132337 (PR #4370, Miquel Alsas) gives the app's own
/v1/models meta.n_ctx (the engine's live window first, the configured one next,
left out when unknown). This adds the same number as context_length, the field
OpenAI-style clients read, so a client on the app's address and the app's API key
(which does not change with a load) has what it needs. The private engine key
stays per load by design. No new text.
Test: tests/native-models-context.test.mjs (live window first, configured next,
unknown left out, a loading model not listed, the host route): fails on
a82132337, passes; the PR's native-models test keeps its key checks with
context_length added; with the API route tests 35/35.
This commit is contained in:
Niko1221 committed 2026-11-16 16:04:39 +01:00
1 parent a821323374
commit e81a74af3f
3 files changed
+35 -3

No files matched your search

+3 -1
View File
@@ -4,6 +4,8 @@ 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 } : {}) } };
// #5893: also as context_length, the field OpenAI-style clients read; a client that needs the window then has no reason to use the
// engine's private port, whose key is new at every load
return { id: i.apiModelId, object: 'model', owned_by: 'strata', ...(n_ctx ? { context_length: n_ctx } : {}), meta: { instanceId: i.id, gpus: i.gpus, label: i.label, aliases: i.aliases, ...(n_ctx ? { n_ctx } : {}) } };
});
}
@@ -0,0 +1,30 @@
// #5893: a client that needs the loaded context window (Unsloth Studio sizes its compaction from it) used the engine's private port, whose key
// is new at every load, so every reload broke it. The app's own /v1/models carries the window (PR #4370: meta.n_ctx, the engine's live value
// first) and, here, also as context_length, the field OpenAI-style clients read; the app's API key stays the one the person set.
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { nativeModelEntries } from '../server/native-models.mjs';
const instances = [
{ id: 'n1', apiModelId: 'qwen3.8-flash-next', status: 'ready', context: 131072, metrics: { engine: { max_context: 120000 } }, gpus: [0], label: 'Qwen3.8 Flash Next', aliases: ['local'] },
{ id: 'n2', apiModelId: 'other', status: 'loading', context: 32768, gpus: [1], label: 'Other', aliases: [] },
{ id: 'n3', apiModelId: 'configured-only', status: 'ready', context: 65536, gpus: [], label: 'C', aliases: [] },
{ id: 'n4', apiModelId: 'unknown-window', status: 'ready', context: 0, gpus: [], label: 'U', aliases: [] },
];
test('ready models carry their window as context_length and meta.n_ctx; an unknown window is left out', () => {
const list = nativeModelEntries(instances);
assert.deepEqual(list.map(m => m.id), ['qwen3.8-flash-next', 'configured-only', 'unknown-window'], 'a model still loading is not listed');
const [live, configured, unknown] = list;
assert.equal(live.context_length, 120000, 'the engine\'s live window first');
assert.equal(live.meta.n_ctx, 120000);
assert.equal(configured.context_length, 65536);
assert.equal(configured.meta.n_ctx, 65536);
assert.ok(!('context_length' in unknown) && !('n_ctx' in unknown.meta), 'never reported as 0');
});
test('the host\'s /v1/models answers with these entries', () => {
const src = readFileSync(new URL('../server/index.mjs', import.meta.url), 'utf8');
assert.match(src, /if\(path==='\/v1\/models'&&method==='GET'\)return json\(res,200,\{object:'list',data:nativeModelEntries\(native\.state\(\)\.instances\)\}\);/);
});
+2 -2
View File
@@ -4,7 +4,7 @@ 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 } }]);
assert.deepEqual(nativeModelEntries([ready()]), [{ id: 'qwen-q2_0', object: 'model', owned_by: 'strata', context_length: 65536, 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', () => {
@@ -56,6 +56,6 @@ test('the input is not changed', () => {
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).sort(), ['context_length', 'id', 'meta', 'object', 'owned_by'], 'context_length (#5893) beside the old keys');
assert.deepEqual(Object.keys(entry.meta).sort(), ['aliases', 'gpus', 'instanceId', 'label', 'n_ctx']);
});