Fifth revision: share-link AI safety, sturdier generation, cleaner UI
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 1m0s
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 1m0s
- Malformed model JSON is sent back for repair, then retried; a failed job starts over once before it fails. Visitors can dismiss failed sets. - Generated sets get a short distinct name and description; flashcards are atomic and have no hints; standard study shows known/learning counts. - Share links: "In progress" list, finished sets join the normal lists with a "New" tag, plain progress steps, newest exam first. - Share-link AI expires after a week, can be limited to the US or Arizona (DB-IP Lite or Cloudflare headers) and is locked by abuse protection (migration 8). - The assistant never answers from general knowledge; MCP and web search tools load without a manual test. - Plain-text copy, no question count in exam mode, filler text removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d8df5edf30
commit
85d5c9186d
46 changed files with 1242 additions and 283 deletions
|
|
@ -661,21 +661,54 @@ export async function generateStructured(app: AppContext, opts: StructuredOption
|
|||
return { data: parseLooseJson(res.text), usage: res.usage, model: res.model, truncated: res.stopReason === 'max_tokens' };
|
||||
};
|
||||
if (!resolved.model.capabilities.tools) return promptJson();
|
||||
try {
|
||||
const chatOpts: ChatOptions = { ...opts, tools: [tool], toolChoice: { name: opts.toolName } };
|
||||
const chatOpts: ChatOptions = { ...opts, tools: [tool], toolChoice: { name: opts.toolName } };
|
||||
const once = async () => {
|
||||
const res = opts.mcpArea ? await runChatWithMcp(app, chatOpts, opts.mcpArea) : await runChat(app, chatOpts);
|
||||
const call = res.message.content.find((p) => p.type === 'tool_call');
|
||||
if (call && call.type === 'tool_call') return { data: call.input, usage: res.usage, model: res.model, truncated: res.stopReason === 'max_tokens' };
|
||||
if (call && call.type === 'tool_call') return { data: call.input as unknown, usage: res.usage, model: res.model, truncated: res.stopReason === 'max_tokens' };
|
||||
if (res.text.trim()) return { data: parseLooseJson(res.text), usage: res.usage, model: res.model, truncated: res.stopReason === 'max_tokens' };
|
||||
if (res.stopReason === 'refusal') throw new AIError('content_filter', 'The model declined this request.');
|
||||
if (res.stopReason === 'max_tokens') throw new AIError('invalid_response', 'The model ran out of output space before finishing. Ask for fewer items at a time, or raise the output limit in Settings.');
|
||||
throw new AIError('invalid_response', 'The model returned nothing usable.', { retryable: true });
|
||||
};
|
||||
try {
|
||||
return await once();
|
||||
} catch (err) {
|
||||
if (err instanceof AIError && err.code === 'unsupported') return promptJson();
|
||||
throw err;
|
||||
if (!(err instanceof AIError) || err.code !== 'invalid_response' || opts.signal?.aborted) throw err;
|
||||
// Malformed output (jsonrepair could not fix it): the model repairs its own JSON when we
|
||||
// have it, otherwise it gets one more try at the same request.
|
||||
if (err.rawOutput) {
|
||||
try {
|
||||
return await repairStructured(app, opts, tool, err.rawOutput);
|
||||
} catch (repairErr) {
|
||||
app.log('warn', `Repairing malformed ${opts.toolName} output failed; asking again`, repairErr instanceof Error ? repairErr.message : repairErr);
|
||||
}
|
||||
}
|
||||
return await once();
|
||||
}
|
||||
}
|
||||
|
||||
/** Sends malformed tool-call arguments back to the model to be rewritten as valid JSON. */
|
||||
async function repairStructured(app: AppContext, opts: StructuredOptions, tool: ToolDef, raw: string) {
|
||||
const res = await runChat(app, {
|
||||
task: opts.task,
|
||||
purpose: `${opts.purpose ?? 'structured output'} (JSON repair)`,
|
||||
modelId: opts.modelId,
|
||||
system: 'You repair malformed JSON. Call the tool once with the same content, corrected so it is valid JSON that matches the tool schema. Keep every item and all of its wording; do not add, drop or rewrite content.',
|
||||
messages: [{ role: 'user', content: [{ type: 'text', text: `These arguments for "${tool.name}" are not valid JSON:
|
||||
|
||||
${raw}` }] }],
|
||||
tools: [tool],
|
||||
toolChoice: { name: tool.name },
|
||||
maxOutputTokens: opts.maxOutputTokens,
|
||||
signal: opts.signal,
|
||||
});
|
||||
const call = res.message.content.find((p) => p.type === 'tool_call');
|
||||
if (!call || call.type !== 'tool_call') throw new AIError('invalid_response', 'The repair returned no tool call.');
|
||||
return { data: call.input as unknown, usage: res.usage, model: res.model, truncated: res.stopReason === 'max_tokens' };
|
||||
}
|
||||
|
||||
/* ------------------------------ reporting ------------------------------ */
|
||||
|
||||
export function usageSummary(db: DB, days: number) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue