Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions bin/provider-catalog-sync.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,35 @@ async function fetchJson(url, timeoutMs = 15000) {
return res.json();
}

function modelIdsFromProxyResponse(data) {
if (!data || typeof data !== 'object' || Array.isArray(data)) {
throw new Error('response body must be a non-null object');
}
if (!Object.prototype.hasOwnProperty.call(data, 'data')) {
throw new Error('response body is missing data');
}
if (!Array.isArray(data.data)) {
throw new Error('response data must be an array');
}
return data.data.map((model, index) => {
if (!model || typeof model !== 'object' || Array.isArray(model)) {
throw new Error(`response data[${index}] must be an object`);
}
if (typeof model.id !== 'string' || model.id.trim().length === 0) {
throw new Error(`response data[${index}].id must be a non-empty string`);
}
return model.id;
});
}

async function enumerateProxy(port, sourceDir) {
if (sourceDir) {
const file = path.join(sourceDir, `${port}.json`);
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
return (data.data ?? []).map((m) => m.id);
return modelIdsFromProxyResponse(data);
}
const data = await fetchJson(`http://127.0.0.1:${port}/v1/models`, 10000);
return (data.data ?? []).map((m) => m.id);
return modelIdsFromProxyResponse(data);
}

async function loadModelsDev(spec) {
Expand Down
43 changes: 43 additions & 0 deletions bin/provider-catalog-sync.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,49 @@ const FIXTURE = `{
}
`;

const runSnapshot = async (snapshot) => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'provider-catalog-sync-'));
const sourceDir = path.join(dir, 'sources');
fs.mkdirSync(sourceDir);
const configPath = path.join(dir, 'config.jsonc');
fs.writeFileSync(configPath, JSON.stringify({
provider: {
'claude-code': { models: { 'model-a': {}, 'model-b': {} } },
},
}));
fs.writeFileSync(path.join(sourceDir, '18749.json'), JSON.stringify(snapshot));
try {
return await runSync({ configPath, modelsDevSpec: null, sourceDir });
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
};

for (const [name, snapshot] of [
['missing data', { unexpected: [] }],
['non-array data', { data: 'oops' }],
['entry without an id', { data: [{ name: 'no-id' }] }],
['null entry', { data: [null] }],
]) {
test(`runSync marks proxy snapshot with ${name} as not-enumerable`, async () => {
const { report } = await runSnapshot(snapshot);
const entry = report.providers['claude-code'];
assert.equal(entry.status, 'not-enumerable');
assert.match(entry.note, /proxy :18749 enumeration failed:/);
assert.equal(entry.live_count, undefined);
assert.equal(entry.vanishings_basis, undefined);
});
}

test('runSync preserves the valid proxy snapshot report', async () => {
const { report } = await runSnapshot({ data: [{ id: 'model-a' }, { id: 'model-b' }] });
const entry = report.providers['claude-code'];
assert.equal(entry.live_count, 2);
assert.deepEqual(entry.additions_validated, []);
assert.deepEqual(entry.vanishings, []);
assert.equal(entry.vanishings_basis, 'live');
});

test('parseJsonc strips comments and trailing commas', () => {
const cfg = parseJsonc(FIXTURE);
assert.deepEqual(Object.keys(cfg.provider), ['proxy-a', 'name-only-b']);
Expand Down