Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.
JavaScript 3d3d-assets3d-modelingagent-skillsagent-toolsai-agentsasset-pipelinecligame-assetsglbgltfmcpmcp-servermodel-context-protocolprocedural-generationprocedural-modelingtext-to-3dthreejstypescript
feat(kiln): KilnDraftBuffer + makeKilnUnifiedTools (6-tool unified surface)
Matt Kissinger6a9dc09cbc12
diff --git a/agent/index.ts b/agent/index.tsindex a24d5d69..9110ffe0 100644--- a/agent/index.ts+++ b/agent/index.ts@@ -13,6 +13,7 @@* - {@link resolveKilnAgentModel} — model-id string -> Strands provider descriptor* - {@link makeKilnTools} — in-process tool skin over the shared registry* - {@link makeKilnEditTools} — surgical edit-tool skin for refining an existing asset+ * - {@link makeKilnUnifiedTools} — buffer-based 6-tool surface (KILN_TOOL_SURFACE='unified')* - {@link unifiedDiff} — dependency-free unified diff (the refine patch artifact)* - {@link ensureStreamStart} — the OpenRouter/Vercel-bridge stream fix* - {@link makeOpenRouterModel} — build an OpenRouter Strands model with the fix@@ -24,8 +25,15 @@ export type { RunKilnAgentOptions, RunKilnAgentResult, KilnKnowhow, RefineMode,export { generateKilnAsset, generateKilnCodeAgent, DEFAULT_KILN_AGENT_MODEL } from './generate';export type { GenerateKilnAssetOptions, GenerateKilnAssetResult } from './generate';-export { makeKilnTools, makeKilnEditTools, KilnEditBuffer, KILN_SUBMIT_TOOL_NAME } from './tools';-export type { SubmitSink, EditSink, EditRecord, EditResult } from './tools';+export {+ makeKilnTools,+ makeKilnEditTools,+ makeKilnUnifiedTools,+ KilnDraftBuffer,+ KilnEditBuffer,+ KILN_SUBMIT_TOOL_NAME,+} from './tools';+export type { SubmitSink, EditSink, UnifiedSink, EditRecord, EditResult } from './tools';export { unifiedDiff } from './diff';export type { UnifiedDiffOptions } from './diff';diff --git a/agent/tools-unified.test.ts b/agent/tools-unified.test.tsnew file mode 100644index 00000000..0977de86--- /dev/null+++ b/agent/tools-unified.test.ts@@ -0,0 +1,156 @@+/**+ * Unit tests for the unified buffer surface (KilnDraftBuffer.draft ++ * makeKilnUnifiedTools). Mirrors tools-edit.test.ts: the buffer is pure string+ * ops (tested directly) and the factory is checked for its 6-tool wiring,+ * capture into the sink, the kiln_draft escape-hatch hint, and the collapsed+ * kiln_render media contract.+ */+import { describe, expect, test } from 'bun:test';+import { ImageBlock, JsonBlock } from '@strands-agents/sdk';++import {+ KilnDraftBuffer,+ KilnEditBuffer,+ makeKilnUnifiedTools,+ type UnifiedSink,+} from './tools';++const BOX_CODE = `+const meta = { name: 'test-box', category: 'prop' };+function build() {+ const root = createRoot('Root');+ createPart('Mesh_Box', boxGeo(1, 1, 1), gameMaterial('#ff0000'), { parent: root, position: [0, 0.5, 0] });+ return root;+}+`;++function findTool(tools: ReturnType<typeof makeKilnUnifiedTools>, name: string) {+ const t = tools.find((x) => x.name === name) as+ | { invoke(input: unknown): Promise<unknown> }+ | undefined;+ if (!t) throw new Error(`tool ${name} not found`);+ return t;+}++describe('KilnDraftBuffer', () => {+ test('seeds empty by default and KilnEditBuffer is the same class (alias)', () => {+ expect(KilnEditBuffer).toBe(KilnDraftBuffer);+ const buf = new KilnDraftBuffer();+ expect(buf.code).toBe('');+ expect(buf.edits).toHaveLength(0);+ });++ test('draft replaces the whole buffer and is NOT recorded as an edit', () => {+ const buf = new KilnDraftBuffer();+ const r = buf.draft(BOX_CODE);+ expect(r.ok).toBe(true);+ expect(r.bytes).toBe(BOX_CODE.length);+ expect(r.lines).toBe(BOX_CODE.split('\n').length);+ expect(buf.code).toBe(BOX_CODE);+ expect(buf.edits).toHaveLength(0); // drafting is authoring, not a diff step+ });++ test('draft then surgical edit: edit is recorded, draft is not', () => {+ const buf = new KilnDraftBuffer();+ buf.draft(BOX_CODE);+ const r = buf.apply({ oldString: 'boxGeo(1, 1, 1)', newString: 'boxGeo(2, 1, 1)' });+ expect(r.ok).toBe(true);+ expect(buf.code).toContain('boxGeo(2, 1, 1)');+ expect(buf.edits).toHaveLength(1);+ });+});++describe('makeKilnUnifiedTools', () => {+ test('exposes exactly the six unified tools in order', () => {+ const sink: UnifiedSink = { edits: [] };+ const tools = makeKilnUnifiedTools({ sink });+ expect(tools.map((t) => t.name)).toEqual([+ 'kiln_draft',+ 'kiln_view',+ 'kiln_edit',+ 'kiln_validate',+ 'kiln_render',+ 'kiln_finalize',+ ]);+ // No legacy verbs leak into the unified surface.+ expect(tools.map((t) => t.name)).not.toContain('kiln_list_primitives');+ expect(tools.map((t) => t.name)).not.toContain('kiln_screenshot');+ expect(tools.map((t) => t.name)).not.toContain('kiln_submit');+ expect(sink.edits).toHaveLength(0);+ });++ test('kiln_draft writes the buffer + captures into sink.code; kiln_view reads it back', async () => {+ const sink: UnifiedSink = { edits: [] };+ const tools = makeKilnUnifiedTools({ sink });+ const drafted = (await findTool(tools, 'kiln_draft').invoke({ code: BOX_CODE })) as {+ ok: boolean;+ bytes: number;+ };+ expect(drafted.ok).toBe(true);+ expect(sink.code).toBe(BOX_CODE); // captured even before finalize+ const viewed = (await findTool(tools, 'kiln_view').invoke({})) as { code: string; lines: number };+ expect(viewed.code).toBe(BOX_CODE);+ });++ test('refine seed: kiln_edit updates sink.code; the shared edit trace records it', async () => {+ const sink: UnifiedSink = { edits: [] };+ const tools = makeKilnUnifiedTools({ seedCode: BOX_CODE, sink });+ const edited = (await findTool(tools, 'kiln_edit').invoke({+ oldString: 'boxGeo(1, 1, 1)',+ newString: 'boxGeo(2, 1, 1)',+ })) as { ok: boolean };+ expect(edited.ok).toBe(true);+ expect(sink.code).toContain('boxGeo(2, 1, 1)');+ expect(sink.edits).toHaveLength(1); // live trace shared into the sink+ });++ test('a failed kiln_edit points the model at kiln_draft as the escape hatch', async () => {+ const sink: UnifiedSink = { edits: [] };+ const tools = makeKilnUnifiedTools({ seedCode: BOX_CODE, sink });+ const failed = (await findTool(tools, 'kiln_edit').invoke({+ oldString: 'sphereGeo(9)',+ newString: 'boxGeo(9,9,9)',+ })) as { ok: boolean; hint?: string };+ expect(failed.ok).toBe(false);+ expect(failed.hint).toContain('kiln_draft');+ expect(sink.edits).toHaveLength(0); // nothing applied+ });++ test('kiln_render returns [ImageBlock, JsonBlock] for a valid buffer', async () => {+ const sink: UnifiedSink = { edits: [] };+ const tools = makeKilnUnifiedTools({ seedCode: BOX_CODE, sink });+ const out = (await findTool(tools, 'kiln_render').invoke({})) as unknown[];+ expect(Array.isArray(out)).toBe(true);+ expect(out).toHaveLength(2);+ expect(out[0]).toBeInstanceOf(ImageBlock);+ expect(out[1]).toBeInstanceOf(JsonBlock);+ const json = (out[1] as JsonBlock).json as Record<string, unknown>;+ expect(json['ok']).toBe(true);+ expect(json['tris']).toBeGreaterThan(0); // metrics ride alongside the image+ expect('pngBase64' in json).toBe(false);+ });++ test('kiln_render on a broken buffer is image-free (plain JSON error)', async () => {+ const sink: UnifiedSink = { edits: [] };+ const tools = makeKilnUnifiedTools({ seedCode: 'not a kiln program (', sink });+ const out = (await findTool(tools, 'kiln_render').invoke({})) as { ok: boolean; error?: string };+ expect(Array.isArray(out)).toBe(false);+ expect(out.ok).toBe(false);+ expect(out.error).toBeDefined();+ });++ test('kiln_finalize captures the buffer and marks finalized', async () => {+ const sink: UnifiedSink = { edits: [] };+ const tools = makeKilnUnifiedTools({ sink });+ await findTool(tools, 'kiln_draft').invoke({ code: BOX_CODE });+ const fin = (await findTool(tools, 'kiln_finalize').invoke({})) as {+ ok: boolean;+ recorded: boolean;+ bytes: number;+ };+ expect(fin.ok).toBe(true);+ expect(fin.recorded).toBe(true);+ expect(sink.code).toBe(BOX_CODE);+ expect(sink.finalized).toBe(true);+ });+});diff --git a/agent/tools.ts b/agent/tools.tsindex 0f47e75b..116e755b 100644--- a/agent/tools.ts+++ b/agent/tools.ts@@ -10,11 +10,17 @@* An explicit submit tool is preferred over `structuredOutputSchema` because the* latter's coexistence with a full tool set is provider-dependent; a submit tool* is unambiguous and works across every provider.+ *+ * Three factories: {@link makeKilnTools} (the default `current` generate surface),+ * {@link makeKilnEditTools} (the `current` surgical-refine surface), and+ * {@link makeKilnUnifiedTools} (the buffer-based 6-tool `unified` surface, gated+ * behind KILN_TOOL_SURFACE='unified'). The first two stay byte-identical so the+ * bench baseline and production default do not move.*/import { tool, ImageBlock, JsonBlock, type Tool, type JSONValue } from '@strands-agents/sdk';import { z } from 'zod';-import { kilnToolRegistry } from '../tools/registry';+import { kilnToolRegistry, kilnRenderViewsDef } from '../tools/registry';/** Name of the terminal tool the agent calls to record its final program. */export const KILN_SUBMIT_TOOL_NAME = 'kiln_submit';@@ -126,14 +132,21 @@ export interface EditSink {}/**- * The in-memory working copy of the asset being refined. Pure string ops - no- * Strands / THREE involvement - so it is trivially unit-testable on its own.+ * The in-memory working copy of the program being authored or refined. Pure+ * string ops - no Strands / THREE involvement - so it is trivially unit-testable+ * on its own.+ *+ * Two ways to write it: {@link draft} replaces the whole buffer (the authoring+ * verb — generate starts from an empty buffer and drafts the first program),+ * and {@link apply} does a surgical exact-string edit (recorded in `edits` for+ * the refine diff). Seed defaults to empty for the from-scratch generate path;+ * pass the parent's code to seed a refine.*/-export class KilnEditBuffer {+export class KilnDraftBuffer {private buf: string;readonly edits: EditRecord[] = [];- constructor(seedCode: string) {+ constructor(seedCode: string = '') {this.buf = seedCode;}@@ -152,6 +165,17 @@ export class KilnEditBuffer {return { code: this.buf, lines: this.lineCount };}+ /**+ * Replace the entire working buffer (the authoring verb). NOT recorded as an+ * edit — drafting is wholesale authoring, not a surgical diff step. The refine+ * diff is always computed from the seed to the final buffer, so a draft is+ * captured there regardless.+ */+ draft(code: string): { ok: true; bytes: number; lines: number } {+ this.buf = code;+ return { ok: true, bytes: this.buf.length, lines: this.lineCount };+ }+/** Apply one exact-string replacement. Mirrors the Edit tool's contract. */apply(input: { oldString: string; newString: string; replaceAll?: boolean }): EditResult {const { oldString, newString } = input;@@ -197,6 +221,10 @@ export class KilnEditBuffer {}}+/** Back-compat alias: the edit-mode refine path and external importers (kiln-studio+ * mock, tests) still refer to this as `KilnEditBuffer`. Same class. */+export { KilnDraftBuffer as KilnEditBuffer };+const viewInput = z.object({});const editInput = z.object({@@ -239,7 +267,7 @@ const submitEditInput = z.object({* still captures the edits).*/export function makeKilnEditTools(opts: { seedCode: string; sink: EditSink }): Tool[] {- const buffer = new KilnEditBuffer(opts.seedCode);+ const buffer = new KilnDraftBuffer(opts.seedCode);opts.sink.edits = buffer.edits; // share the live traceconst find = (name: string): (typeof kilnToolRegistry)[number] => {@@ -320,3 +348,137 @@ export function makeKilnEditTools(opts: { seedCode: string; sink: EditSink }): Treturn [listTool, viewTool, editTool, validateTool, renderTool, screenshotTool, submitTool];}++// =============================================================================+// Unified tool surface (KILN_TOOL_SURFACE='unified')+// =============================================================================+//+// One buffer-based surface that unifies the from-scratch generate path and the+// refine path behind a single working buffer. Six tools:+//+// kiln_draft write/replace the whole buffer (authoring + capture point)+// kiln_view read the current buffer (anchors edits)+// kiln_edit surgical exact-string replace on the buffer+// kiln_validate static check of the buffer (no execution — the cheap loop)+// kiln_render execute ONCE -> metrics + the six-view image (render+screenshot)+// kiln_finalize terminal: lock in the current buffer as the answer (arg-less)+//+// The program is HELD in the buffer, so capture is unambiguous (the final buffer)+// rather than "whichever copy landed in kiln_submit". Generate seeds an empty+// buffer; refine seeds the parent's code. `kiln_list_primitives` is dropped — the+// primitive catalog (incl. examples) lives in the cached system prompt instead.+//+// Selected only when the flag is on; the legacy `makeKilnTools` /+// `makeKilnEditTools` surfaces stay byte-identical for the default `current`+// surface and the bench baseline.++/**+ * A mutable sink the unified tools write into. Create one per run with+ * `edits: []`, pass it to {@link makeKilnUnifiedTools}, and after invoke read+ * `.code` (the final working buffer), `.edits` (the applied surgical-edit trace),+ * and `.finalized` (whether kiln_finalize fired).+ */+export interface UnifiedSink {+ code?: string;+ edits: EditRecord[];+ finalized?: boolean;+}++const draftInput = z.object({+ code: z+ .string()+ .describe('The complete Kiln program (defines `meta` + `build()`, optional `animate()`).'),+});++/**+ * Build the unified 6-tool surface over a working buffer seeded with `seedCode`+ * (empty for generate, the parent's code for refine). The buffer's live edit+ * trace is shared into `sink.edits`; `sink.code` tracks the latest buffer after+ * every draft/edit (so an un-finalized run still captures the work). `kiln_edit`+ * failures point at `kiln_draft` as the always-reliable rewrite escape hatch.+ */+export function makeKilnUnifiedTools(opts: { seedCode?: string; sink: UnifiedSink }): Tool[] {+ const buffer = new KilnDraftBuffer(opts.seedCode ?? '');+ opts.sink.edits = buffer.edits; // share the live trace++ const validateDef = kilnToolRegistry.find((d) => d.name === 'kiln_validate');+ if (!validateDef) throw new Error('makeKilnUnifiedTools: missing registry tool kiln_validate');++ const draftTool: Tool = tool({+ name: 'kiln_draft',+ description:+ 'Write the complete Kiln program into your working buffer. This is how you author — call it first ' ++ 'to lay down your program, and call it again any time to rewrite it wholesale. The argument `code` ' ++ 'is the full program (meta + build(), optional animate()). For small fixes prefer kiln_edit. ' ++ 'Returns { ok, bytes, lines }.',+ inputSchema: draftInput,+ callback: (input) => {+ const r = buffer.draft((input as { code: string }).code);+ opts.sink.code = buffer.code; // capture the live buffer even if finalize is skipped+ return r as JSONValue;+ },+ });++ const viewTool: Tool = tool({+ name: 'kiln_view',+ description:+ 'Read the current working code (the program you are building). Returns the full raw source and ' ++ 'its line count. kiln_edit matches against exactly this text - copy spans from here.',+ inputSchema: viewInput,+ callback: () => buffer.view() as JSONValue,+ });++ const editTool: Tool = tool({+ name: 'kiln_edit',+ description:+ 'Make a surgical change to the working code: replace an exact span (oldString) with newString. ' ++ 'oldString must appear verbatim in the current code (call kiln_view first) and be unique unless ' ++ 'replaceAll is set. Make the smallest edits that satisfy the request; do not rewrite the whole ' ++ 'program. If an edit cannot anchor, fall back to kiln_draft to rewrite. Returns ' ++ '{ ok, occurrences, newBytes } or { ok:false, error, hint }.',+ inputSchema: editInput,+ callback: (input) => {+ const r = buffer.apply(input as { oldString: string; newString: string; replaceAll?: boolean });+ if (r.ok) {+ opts.sink.code = buffer.code; // capture the live buffer even if finalize is skipped+ return r as JSONValue;+ }+ // Point the model at kiln_draft as the always-reliable escape hatch.+ const hint = r.hint+ ? `${r.hint} Or call kiln_draft to rewrite the whole program.`+ : 'Call kiln_draft to rewrite the whole program.';+ return { ...r, hint } as JSONValue;+ },+ });++ const validateTool: Tool = tool({+ name: 'kiln_validate',+ description: `${validateDef.description} Operates on your current working buffer (no argument).`,+ inputSchema: viewInput,+ callback: async () => (await validateDef.run({ code: buffer.code })) as JSONValue,+ });++ const renderTool: Tool = tool({+ name: 'kiln_render',+ description: kilnRenderViewsDef.description + ' Operates on your current working buffer (no argument).',+ inputSchema: viewInput,+ callback: async () =>+ toCallbackResult(kilnRenderViewsDef, await kilnRenderViewsDef.run({ code: buffer.code })),+ });++ const finalizeTool: Tool = tool({+ name: 'kiln_finalize',+ description:+ 'Lock in your current working buffer as the FINAL answer. Call this exactly once, after the render ' ++ 'looks right across all six views. No argument — it finalizes exactly what you have drafted and ' ++ 'edited. Returns { ok, recorded, bytes, edits }.',+ inputSchema: viewInput,+ callback: () => {+ opts.sink.code = buffer.code;+ opts.sink.finalized = true;+ return { ok: true, recorded: true, bytes: buffer.code.length, edits: buffer.edits.length } as JSONValue;+ },+ });++ return [draftTool, viewTool, editTool, validateTool, renderTool, finalizeTool];+}