import { ChangeList } from '../service/transformPage'; import { ToolDefinition, ToolHandler } from '../models/types'; // --------------------------------------------------------------------------- // Change operations output format — text instruction for non-structured builders // --------------------------------------------------------------------------- /** * Text instruction that tells the model to return a JSON array of change operations. * Append this to for builders that don't support structured outputs. */ export const CHANGE_OPS_FORMAT_INSTRUCTION = `Return a JSON object with two fields: "message" and "changes". "message" is a brief (1 sentence) message to the user explaining what you changed. It is shown in the chat feed as the assistant's reply — write it directly to the user, not a description of the JSON. "changes" is an array of change operations to apply to the page. Do NOT return the full HTML page. Each change operation must be one of: { "op": "update", "nodeId": "", "html": "" } — replaces the innerHTML of the target element { "op": "replace", "nodeId": "", "html": "" } — replaces the entire element (outerHTML) with new markup { "op": "delete", "nodeId": "" } — removes the element from the page { "op": "insert", "parentId": "", "position": "prepend"|"append"|"before"|"after", "html": "" } — inserts new HTML relative to the parent element { "op": "style-element", "nodeId": "", "style": "" } — sets the style attribute of the target element (must be unlocked) { "op": "search-replace", "nodeId": "", "search": "", "replace": "" } — finds exact text within a script/style block and replaces it. Use empty string for replace to delete. { "op": "search-insert", "nodeId": "", "after": "", "content": "" } — inserts new content immediately after the matched text in a script/style block. For partial edits to large scripts/styles, use search-replace or search-insert instead of replacing the entire block with update. Copy the search/after text exactly as it appears in the source. When making multiple edits to the same block, ensure each search string targets distinct text. To delete code, use search-replace with an empty replace string. Return ONLY the JSON object. Example: { "message": "Changed the heading and added a new notice.", "changes": [ { "op": "update", "nodeId": "5", "html": "

Hello world

" }, { "op": "insert", "parentId": "3", "position": "append", "html": "
New message
" } ] }`; // --------------------------------------------------------------------------- // Change operations JSON schema — for structured output (constrained decoding) // --------------------------------------------------------------------------- const CHANGE_OP_MESSAGE_SCHEMA = { type: 'string', description: 'Brief (1 sentence) message to the user explaining the change. Written directly to the user; shown in the chat feed as the assistant reply.', }; /** Array schema for the full change-op union (ranged writes allowed). */ const CHANGE_OPS_ARRAY_SCHEMA: Record = { type: 'array', items: { anyOf: [ { type: 'object', properties: { op: { type: 'string', const: 'update' }, nodeId: { type: 'string' }, html: { type: 'string' }, }, required: ['op', 'nodeId', 'html'], additionalProperties: false, }, { type: 'object', properties: { op: { type: 'string', const: 'replace' }, nodeId: { type: 'string' }, html: { type: 'string' }, }, required: ['op', 'nodeId', 'html'], additionalProperties: false, }, { type: 'object', properties: { op: { type: 'string', const: 'delete' }, nodeId: { type: 'string' }, }, required: ['op', 'nodeId'], additionalProperties: false, }, { type: 'object', properties: { op: { type: 'string', const: 'insert' }, parentId: { type: 'string' }, position: { type: 'string', enum: ['prepend', 'append', 'before', 'after'] }, html: { type: 'string' }, }, required: ['op', 'parentId', 'position', 'html'], additionalProperties: false, }, { type: 'object', properties: { op: { type: 'string', const: 'style-element' }, nodeId: { type: 'string' }, style: { type: 'string' }, }, required: ['op', 'nodeId', 'style'], additionalProperties: false, }, { type: 'object', properties: { op: { type: 'string', const: 'search-replace' }, nodeId: { type: 'string' }, search: { type: 'string' }, replace: { type: 'string' }, }, required: ['op', 'nodeId', 'search', 'replace'], additionalProperties: false, }, { type: 'object', properties: { op: { type: 'string', const: 'search-insert' }, nodeId: { type: 'string' }, after: { type: 'string' }, content: { type: 'string' }, }, required: ['op', 'nodeId', 'after', 'content'], additionalProperties: false, }, ], }, }; /** Array schema with ranged write ops omitted. */ const CHANGE_OPS_ARRAY_SCHEMA_NO_RANGED: Record = { type: 'array', items: { anyOf: [ { type: 'object', properties: { op: { type: 'string', const: 'update' }, nodeId: { type: 'string' }, html: { type: 'string' }, }, required: ['op', 'nodeId', 'html'], additionalProperties: false, }, { type: 'object', properties: { op: { type: 'string', const: 'replace' }, nodeId: { type: 'string' }, html: { type: 'string' }, }, required: ['op', 'nodeId', 'html'], additionalProperties: false, }, { type: 'object', properties: { op: { type: 'string', const: 'delete' }, nodeId: { type: 'string' }, }, required: ['op', 'nodeId'], additionalProperties: false, }, { type: 'object', properties: { op: { type: 'string', const: 'insert' }, parentId: { type: 'string' }, position: { type: 'string', enum: ['prepend', 'append', 'before', 'after'] }, html: { type: 'string' }, }, required: ['op', 'parentId', 'position', 'html'], additionalProperties: false, }, { type: 'object', properties: { op: { type: 'string', const: 'style-element' }, nodeId: { type: 'string' }, style: { type: 'string' }, }, required: ['op', 'nodeId', 'style'], additionalProperties: false, }, ], }, }; /** * JSON schema for Anthropic structured outputs. * Wraps the change-ops array in `{ message, changes }` so the model always * emits a user-facing message alongside its page changes. */ export const CHANGE_OPS_SCHEMA: Record = { type: 'object', properties: { message: CHANGE_OP_MESSAGE_SCHEMA, changes: CHANGE_OPS_ARRAY_SCHEMA, }, required: ['message', 'changes'], additionalProperties: false, }; /** * Variant of CHANGE_OPS_SCHEMA that omits ranged write operations (search-replace, search-insert). * Used for medium/hard/re-write changes where the model should replace full blocks instead of * attempting partial edits. */ export const CHANGE_OPS_SCHEMA_NO_RANGED: Record = { type: 'object', properties: { message: CHANGE_OP_MESSAGE_SCHEMA, changes: CHANGE_OPS_ARRAY_SCHEMA_NO_RANGED, }, required: ['message', 'changes'], additionalProperties: false, }; /** * Text instruction variant for no-ranged-writes mode. * Tells the model to use `update` for full innerHTML replacement instead of partial edits. */ export const CHANGE_OPS_FORMAT_INSTRUCTION_NO_RANGED = `For script and style blocks, use "update" to replace the full innerHTML or "replace" to replace the full outerHTML. Do NOT use partial search-replace edits.`; /** * OpenAI structured outputs require a root-level object. * Matches the Anthropic shape: { message, changes }. */ export const OPENAI_CHANGE_OPS_SCHEMA: Record = { type: 'object', properties: { message: CHANGE_OP_MESSAGE_SCHEMA, changes: CHANGE_OPS_ARRAY_SCHEMA, }, required: ['message', 'changes'], additionalProperties: false, }; // --------------------------------------------------------------------------- // Context sections — structured blocks passed to the builder // --------------------------------------------------------------------------- export type SectionMode = /** Always rendered in full. Never sketched. Used for small/universally relevant sections. */ | 'always-full' /** Sketched by default; rendered in full when the classifier expands it. */ | 'classifier-decides' /** Skipped entirely when sketch === null; otherwise sketched/expanded like classifier-decides. */ | 'always-omit-when-empty'; export interface ContextSection { /** Section title, e.g. "", "". Used as the classifier expand-set key. */ title: string; /** Full render — what the model sees when this section is expanded. */ content: string; /** * Compact render — what the classifier sees, and what the model sees when this * section is NOT expanded. `null` means "omit entirely from the prompt when not * expanded" (used by always-omit-when-empty sections that have nothing to say, * or by always-full sections where sketch is irrelevant). */ sketch: string | null; /** Default behavior for this section. Producer-declared. */ mode: SectionMode; /** * If true, this section is always rendered in full on the initial page build, * regardless of classifier output. Defaults to false. */ forceFullOnInitial?: boolean; /** How the model should work with this section (appended to instructions) */ instructions: string; } // --------------------------------------------------------------------------- // Builder result — discriminated union returned by Builder.run() // --------------------------------------------------------------------------- export type BuilderResult = | { kind: 'transforms'; changes: ChangeList; message?: string } | { kind: 'reply'; text: string } | { kind: 'error'; error: Error }; // --------------------------------------------------------------------------- // Attachments — images/files sent alongside the user message // --------------------------------------------------------------------------- export interface Attachment { mediaType: string; data: string; name?: string; } // --------------------------------------------------------------------------- // Builder interface // --------------------------------------------------------------------------- export interface Builder { run( currentPage: ContextSection, additionalSections: ContextSection[], userMessage: string, newBuild: boolean, attachments?: Attachment[], tools?: ToolDefinition[], toolHandlers?: Record, onToolCall?: (names: string[]) => void, ): Promise; }