feat(popup): validate model using structured JSON output

Replace the fire-and-forget model ping with a structured output
test that enforces a JSON schema (reply, success, data.number).
The parsed response is validated against the expected types before
showing a success message, providing stronger proof that the
selected model both responds and supports structured outputs.
This commit is contained in:
2026-04-25 20:11:02 +02:00
parent b0873f3ed3
commit 46c5b756a5
3 changed files with 52 additions and 6 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+50 -4
View File
@@ -81,12 +81,58 @@ export async function checkModel() {
project: projectId,
dangerouslyAllowBrowser: true
});
await client.chat.completions.create({
const completion = await client.chat.completions.create({
model,
messages: [{ role: 'user', content: 'reply just pong' }],
max_completion_tokens: maxTokens || 2000
messages: [
{
role: 'user',
content:
'reply just pong, set success to true, and provide a random number between 1 and 100.'
}
],
max_completion_tokens: maxTokens || 2000,
response_format: {
type: 'json_schema',
json_schema: {
name: 'model_test',
strict: true,
schema: {
type: 'object',
properties: {
reply: { type: 'string', description: 'The text reply' },
success: { type: 'boolean', description: 'Always true' },
data: {
type: 'object',
properties: {
number: { type: 'integer' }
},
required: ['number'],
additionalProperties: false
}
},
required: ['reply', 'success', 'data'],
additionalProperties: false
}
}
}
});
showMessage({ msg: 'The model is valid!' });
const content = completion.choices[0]?.message?.content;
if (!content) {
throw new Error('No content returned from the model.');
}
const parsed = JSON.parse(content);
if (
typeof parsed.reply !== 'string' ||
typeof parsed.success !== 'boolean' ||
typeof parsed.data !== 'object' ||
typeof parsed.data.number !== 'number'
) {
throw new Error('Model did not follow the JSON schema correctly.');
}
showMessage({ msg: 'The model is valid and supports structured outputs!' });
} catch (err: any) {
showMessage({ msg: err, isError: true });
}