Skip to content

S18: specToCode TypeScript generator - #858

Merged
LinuxDevil merged 1 commit into
mainfrom
fb-s18-spec-to-code
Nov 3, 2026
Merged

LinuxDevil merged 1 commit into
mainfrom
fb-s18-spec-to-code

Conversation

@LinuxDevil

Copy link
Copy Markdown
Owner

What changed

  • src/spec/generators/typescript.ts — new specToCode(spec, options?): renders a validated AgentSpec v2 as a readable, deterministic TypeScript ES module. Data keys are inlined; the reference sections (tools, mcpServers, skills, memory, subagents, handoffs, guardrails, permissions, hooks, output) render as calls to the package-root spec resolvers against a module-level host. instructions renders as a template literal with the instructionsByFamily tail resolved at generation time ({ file } → await readFile + conditional node:fs/promises import), promptRef → prompt, model stays a 'provider/model' string. Option order follows a fixed KEY_ORDER; only the resolvers actually used are imported (sorted). When handoffs exist, the subagents record is hoisted to const subagents and host.resolveHandoffAgent resolves subagent targets or throws "handoff target '' is not defined in this file". triggers/abortListeners/flow/evals end the module as a commented export const spec = { ... } as const;. Not exported from the package root (that is S23).
  • src/spec/generators/tsLiteral.ts — new toTsLiteral(value, indent) / tsObjectKey printer: single-quoted strings, identifier-safe keys bare, inline ≤ 80 cols else one element per line, 2-space indent.
  • src/utils/plainErrors.test.ts — ALLOWED entry for the generated throw new Error( literal (generated modules cannot import SDKError).
  • Tests: typescript.test.ts (15 tests: minimal inline snapshot, full-section file snapshot, determinism/key order, minimal imports, template-literal escape round-trip, readFile import, family tail, handoff resolvers, triggers trailer, transpileModule syntax check over 4 cases) and tsLiteral.test.ts (8 tests).

Notes: package default is 'lousho' (renamed from @lousho/build-ai-agent); resolveToolRef is emitted with its real signature (ref, { ...host, agentName }, 'tools.<i>').

How tested

  • npx vitest run src/spec/generators — 38 tests pass (incl. plainErrors)
  • npx tsc --noEmit, npm run typecheck:tests, npm run lint — clean
  • npm run test:coverage (with --coverage.reportOnFailure so the report writes despite the known failures) — 7179 pass; pre-existing failures only: templates.test.ts README checks (CRLF checkout on Windows; index is LF), one flaky connect.test.ts MCP timing test; runRegistry checkpoint flake did not reproduce
  • npm run fallow, npm run docs:ratchet, npm run api:check, npm run docs:llms:check, npm run size-check — clean

Acceptance criteria

  • Minimal spec output is exactly the header, host line and a createAgent({ name, model, instructions }) call
  • Deterministic; key order follows KEY_ORDER regardless of input order
  • Only the resolvers actually used are imported
  • Every generated module transpiles with no syntax diagnostics (ts.transpileModule, ESNext/ES2022)
  • Instructions containing ``, ${x} and `\n` survive (evaluating the template literal gives the original)
  • A spec with triggers ends with the commented export const spec = { triggers: [...] } as const; block

Generated with Devin

specToCode(spec) renders a validated AgentSpec v2 as a readable, deterministic TypeScript ES module: a createAgent({...}) call with plain data inlined via toTsLiteral and the reference sections written as calls to the package-root spec resolvers (resolveToolRef, resolveSkillRef, memoryFromSpec, subagentFromSpec, handoffFromSpec, guardrailsFromSpec, permissionsFromSpec, hooksFromSpec, outputFromSpec, resolveMcpServers, jsonSchemaToStandardSchema, specV2ToAgent) against a module-level host. Only the resolvers actually used are imported. Handoffs resolve subagent targets through a const subagents record and a host resolveHandoffAgent that throws for targets the file does not define; triggers/abortListeners/flow/evals end the module as a commented 'export const spec'.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@LinuxDevil
LinuxDevil merged commit a021b14 into main Nov 3, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant