Skip to content
Moku
@moku-labs/ai pages

compose

Natural language to a validated build file. A repair loop means it can never return an invalid spec.

On this page

What it does

compose asks an LLM for a build file and validates the answer through buildfile. An invalid answer goes back to the model with the validation issue. It writes no files and starts no run.

  1. Build a system prompt from buildfile.jsonSchema() and the input shapes of the tasks.
  2. Call promptGen.generate with the configured provider.
  3. Strip a Markdown fence, then validate with buildfile.compile.
  4. On failure, send the request, the last output and the issue again, up to maxRepairAttempts times.
  5. On success, apply name and emit the text.

Config

pluginConfigs.compose.

OptionTypeDefaultDescription
providerstring"openai"The prompt-gen provider for every attempt.
maxRepairAttemptsnumber2Extra attempts after the first.

API

Mounted at app.compose, reachable with ctx.require(composePlugin).

MethodSignatureDescription
compose(opts: { prompt, emit, name?, signal? }) => Promise<ComposeResult>Generates and validates.
OptionTypeDescription
promptstringWhat the build should make.
emit"build" | "script"YAML text, or a defineBuild() TypeScript module.
namestringReplaces the generated name.
signalAbortSignalGoes to every generate call.

ComposeResult is { spec, text, costUsd }. costUsd counts every attempt, the failed ones too.

compose.ts
import { writeFile } from "node:fs/promises";
import { app } from "./app";

await app.start();
const { text, costUsd } = await app.compose.compose({
  prompt: "Narrate a greeting in English, then translate it to Spanish.",
  emit: "build",
  name: "greeting"
});
await writeFile("greeting.moku.yaml", text);
console.log(costUsd);
await app.stop();

When every attempt fails it throws [ai] Compose could not produce a valid build file after <n> attempts.

Events

Emits

None.

Listens to

None.

Depends on

buildfile, promptGen.