import assert from 'assert'; import { createAnthropicBuilder, buildWithModel, TOOLS_INSTRUCTION_HINT } from '../src/builders/anthropic'; import { createBuilder } from '../src/builders/index'; import { ContextSection, BuilderResult } from '../src/builders/types'; import { completePrompt, AgentCompletion, PromptCompletionArgs, ToolDefinition, ToolHandler } from '../src/models/types'; // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- const PAGE_HTML = '

Hello

'; const currentPage: ContextSection = { title: '', content: PAGE_HTML, sketch: null, mode: 'always-full', instructions: '', }; const additionalSections: ContextSection[] = []; /** Creates a stub completePrompt that returns the given raw string. */ function stubComplete(raw: string): completePrompt { return async () => ({ completed: true, value: raw } as AgentCompletion); } /** Creates a stub completePrompt that records calls and returns a value. */ function trackingComplete(raw: string): { fn: completePrompt; calls: number } { const tracker = { fn: null as unknown as completePrompt, calls: 0 }; tracker.fn = async () => { tracker.calls++; return { completed: true, value: raw } as AgentCompletion; }; return tracker; } // --------------------------------------------------------------------------- // Tests // --------------------------------------------------------------------------- describe('Anthropic Builder — Haiku Classifier Routing', () => { describe('Non-Opus model → no classification', () => { it('calls the provided complete function directly for non-Opus models', async () => { const changes = JSON.stringify([{ op: 'replace', nodeId: '1', html: '

Hi

' }]); const tracker = trackingComplete(changes); const builder = createAnthropicBuilder(tracker.fn, undefined, 'SynthOS', { apiKey: 'test-key', model: 'claude-sonnet-4-6-20250514', }); const result = await builder.run(currentPage, additionalSections, 'change heading', false); assert.strictEqual(result.kind, 'transforms'); assert.strictEqual(tracker.calls, 1); }); }); describe('No apiKey → skip classification', () => { it('uses existing behavior when options.apiKey is undefined', async () => { const changes = JSON.stringify([{ op: 'replace', nodeId: '1', html: '

Hi

' }]); const tracker = trackingComplete(changes); const builder = createAnthropicBuilder(tracker.fn, undefined, 'SynthOS', { model: 'claude-opus-4-20250514', // no apiKey }); const result = await builder.run(currentPage, additionalSections, 'change heading', false); assert.strictEqual(result.kind, 'transforms'); assert.strictEqual(tracker.calls, 1); }); }); describe('buildWithModel', () => { it('constructs the prompt and parses a transforms response', async () => { const changes = [{ op: 'replace', nodeId: '1', html: '

Updated

' }]; const complete = stubComplete(JSON.stringify(changes)); const result = await buildWithModel(complete, currentPage, additionalSections, 'update heading', undefined, 'SynthOS'); assert.strictEqual(result.kind, 'transforms'); if (result.kind === 'transforms') { assert.strictEqual(result.changes.length, 1); } }); it('returns error when model call fails', async () => { const complete: completePrompt = async () => ({ completed: false, error: new Error('API error'), }); const result = await buildWithModel(complete, currentPage, additionalSections, 'test', undefined, 'SynthOS'); assert.strictEqual(result.kind, 'error'); }); it('returns a reply result for { kind: "reply" } responses', async () => { const reply = JSON.stringify({ kind: 'reply', text: 'The heading says Hello.' }); const complete = stubComplete(reply); const result = await buildWithModel(complete, currentPage, additionalSections, 'what does the heading say?', undefined, 'SynthOS'); assert.strictEqual(result.kind, 'reply'); if (result.kind === 'reply') { assert.strictEqual(result.text, 'The heading says Hello.'); } }); it('appends the tools instruction hint to system when tools are bound', async () => { let captured: PromptCompletionArgs | undefined; const complete: completePrompt = async (args) => { captured = args; return { completed: true, value: '[]' }; }; const tools: ToolDefinition[] = [{ name: 'get_thing', description: 'Returns a thing.', input_schema: { type: 'object', properties: {}, additionalProperties: false }, }]; const handlers: Record = { get_thing: async () => 'ok' }; await buildWithModel(complete, currentPage, additionalSections, 'test', undefined, 'SynthOS', undefined, undefined, tools, handlers); assert.ok(captured); assert.ok(captured!.system!.content.includes(TOOLS_INSTRUCTION_HINT), 'system should include tools hint'); assert.strictEqual(captured!.tools, tools); assert.strictEqual(captured!.toolHandlers, handlers); }); it('omits the tools instruction hint when tools are not bound', async () => { let captured: PromptCompletionArgs | undefined; const complete: completePrompt = async (args) => { captured = args; return { completed: true, value: '[]' }; }; await buildWithModel(complete, currentPage, additionalSections, 'test', undefined, 'SynthOS'); assert.ok(captured); assert.ok(!captured!.system!.content.includes(TOOLS_INSTRUCTION_HINT), 'system must not include tools hint'); assert.strictEqual(captured!.tools, undefined); }); }); describe('Builder.run() — tool passthrough', () => { it('passes tools, toolHandlers, and onToolCall through to the model', async () => { const changes = JSON.stringify([{ op: 'replace', nodeId: '1', html: '

Hi

' }]); let captured: PromptCompletionArgs | undefined; const complete: completePrompt = async (args) => { captured = args; return { completed: true, value: changes }; }; const tools: ToolDefinition[] = [{ name: 'get_connectors', description: 'Returns connectors.', input_schema: { type: 'object', properties: {}, additionalProperties: false }, }]; const handlers: Record = { get_connectors: async () => '[]' }; const onToolCall = (_names: string[]) => { /* noop */ }; const builder = createAnthropicBuilder(complete, undefined, 'SynthOS', { apiKey: 'test-key', model: 'claude-sonnet-4-6', }); const result = await builder.run(currentPage, additionalSections, 'change', false, undefined, tools, handlers, onToolCall); assert.strictEqual(result.kind, 'transforms'); assert.ok(captured); assert.strictEqual(captured!.tools, tools); assert.strictEqual(captured!.toolHandlers, handlers); assert.strictEqual(captured!.onToolCall, onToolCall); }); }); describe('createBuilder factory', () => { it('passes options through to Anthropic builder', async () => { const changes = JSON.stringify([{ op: 'replace', nodeId: '1', html: '

test

' }]); const tracker = trackingComplete(changes); // Non-Opus model should just work without classification const builder = createBuilder('Anthropic', tracker.fn, undefined, 'SynthOS', { apiKey: 'key', model: 'claude-sonnet-4-6-20250514', }); const result = await builder.run(currentPage, additionalSections, 'test', false); assert.strictEqual(result.kind, 'transforms'); assert.strictEqual(tracker.calls, 1); }); it('does not pass options to non-Anthropic builders', async () => { const changes = JSON.stringify([{ op: 'replace', nodeId: '1', html: '

test

' }]); const tracker = trackingComplete(changes); // OpenAI builder should work fine (options ignored) const builder = createBuilder('OpenAI', tracker.fn, undefined, 'SynthOS', { apiKey: 'key', model: 'gpt-4o', }); const result = await builder.run(currentPage, additionalSections, 'test', false); assert.strictEqual(result.kind, 'transforms'); assert.strictEqual(tracker.calls, 1); }); }); });