feat: phase 2 of AI pipeline hardening — tool-based structured outputs + prompt caching
Every structured-output call now uses an Anthropic tool instead of
parsing JSON out of free-form prose, and stable system prompts are
sent as cacheable blocks. Behaviour-equivalent to phase 1 from the
caller's point of view; the savings show up in token usage and in the
absence of "AI returned non-JSON response" failure modes.
* src/lib/llmTools.js — single source of truth for tool definitions:
emit_knowledge_graph, emit_handbook_delta, emit_learning_article /
_slides / _infographic / _all, emit_custom_topic, emit_quiz_questions,
emit_graph_actions, plus five article-patch tools (set_intro,
set_section, add_section, remove_section, replace_takeaways).
* src/lib/articlePatches.js — pure applyArticlePatches +
applyAndValidate; rebuilds the article from a sequence of patch tool
calls and re-validates against learningArticleSchema. set_section
falls back to appending when no matching heading exists so the
model's intent is preserved rather than silently dropped.
* src/lib/llmSchemas.js — Zod schemas for the five patch ops,
registered in toolSchemaRegistry so callLLM validates them
automatically.
* src/lib/llm.js — simulation mode now returns a tool_use stub matching
toolChoice.name, so the UI keeps working with Simulation Mode on
after the structured-output migration.
* src/lib/extractionPipeline.js — processSourceText and
analyzeHandbookDelta migrated to callLLM + tool use. System prompts
sent as { cache_control: ephemeral } blocks. Handbook results pass
through normalizeHandbookResult to collapse legacy "executes"
relations into executed_by with swapped source/target.
* src/lib/learningService.js — generateLearningContent picks the right
tool per selectedType; generateCustomTopic uses emit_custom_topic;
refineLearningContent now drives the five patch tools with
toolChoice 'any' and rejects the whole turn if the patched article
fails validation. Article-only refinement is intentional for phase 2;
refining a topic without an article surfaces a clear error.
* src/lib/testService.js — quiz generation via emit_quiz_questions.
* src/components/admin/KnowledgeGraph.jsx — analyzeGraph routed through
the reasoning tier (Opus) since graph-wide consolidation benefits
from a stronger reasoner.
* src/components/chat/prompts.js — buildSystemPrompt now returns three
text blocks: stable preamble (cached), KB context (cached, hash-bust
deferred to phase 5), per-turn user/admin tail (uncached).
* src/lib/__tests__/ — 13 new tests covering each patch op, multi-op
sequencing, post-patch validation failure, and tool/registry shape.
Acceptance: lint and 45/45 tests green; build succeeds; no
`match(/\{[\s\S]*\}/)` JSON extraction left in src/. Live verification
of cache hits on a second extraction within 5 minutes is deferred to
manual smoke testing — needs real `/api/anthropic` traffic.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
44
src/lib/__tests__/llmTools.test.js
Normal file
44
src/lib/__tests__/llmTools.test.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
EMIT_KNOWLEDGE_GRAPH_TOOL,
|
||||
EMIT_HANDBOOK_DELTA_TOOL,
|
||||
EMIT_LEARNING_ARTICLE_TOOL,
|
||||
EMIT_LEARNING_SLIDES_TOOL,
|
||||
EMIT_LEARNING_INFOGRAPHIC_TOOL,
|
||||
EMIT_LEARNING_ALL_TOOL,
|
||||
EMIT_CUSTOM_TOPIC_TOOL,
|
||||
EMIT_QUIZ_QUESTIONS_TOOL,
|
||||
EMIT_GRAPH_ACTIONS_TOOL,
|
||||
ARTICLE_PATCH_TOOLS,
|
||||
} from '../llmTools';
|
||||
import { toolSchemaRegistry } from '../llmSchemas';
|
||||
|
||||
const allTools = [
|
||||
EMIT_KNOWLEDGE_GRAPH_TOOL,
|
||||
EMIT_HANDBOOK_DELTA_TOOL,
|
||||
EMIT_LEARNING_ARTICLE_TOOL,
|
||||
EMIT_LEARNING_SLIDES_TOOL,
|
||||
EMIT_LEARNING_INFOGRAPHIC_TOOL,
|
||||
EMIT_LEARNING_ALL_TOOL,
|
||||
EMIT_CUSTOM_TOPIC_TOOL,
|
||||
EMIT_QUIZ_QUESTIONS_TOOL,
|
||||
EMIT_GRAPH_ACTIONS_TOOL,
|
||||
...ARTICLE_PATCH_TOOLS,
|
||||
];
|
||||
|
||||
describe('llmTools', () => {
|
||||
it('every tool has a name, description, and object input_schema', () => {
|
||||
for (const t of allTools) {
|
||||
expect(typeof t.name).toBe('string');
|
||||
expect(t.name.length).toBeGreaterThan(0);
|
||||
expect(typeof t.description).toBe('string');
|
||||
expect(t.input_schema).toMatchObject({ type: 'object' });
|
||||
}
|
||||
});
|
||||
|
||||
it('every tool has a matching Zod validator in toolSchemaRegistry', () => {
|
||||
for (const t of allTools) {
|
||||
expect(toolSchemaRegistry[t.name]).toBeTruthy();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user