Skip to content

Project layout

An AI project grows into many small declarations: tools, processes, an MCP server or two. Lay them out so a file’s name tells you what it holds before you open it.

Name a file after what it declares and its kind

Section titled “Name a file after what it declares and its kind”

Every file holds one declaration, and its name is the declaration’s name followed by its kind:

SearchIssues.tool.ts
PostAnswer.tool.ts
Triage.process.ts
Answering.process.ts
Docs.mcp.ts

Never collect tools in a tools.ts. A tool is found by its name, and a file that holds several grows without limit.

Keep a tool’s contract and its Layer together

Section titled “Keep a tool’s contract and its Layer together”

A tool’s file holds its class and the Layer that implements it:

src/PostAnswer.tool.ts
import * as AI from "@alchemy.run/ai";
import * as GitHub from "@alchemy.run/github";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import { Repo } from "./Repo.ts";
export class PostAnswer extends AI.Tool<PostAnswer>()("post_answer", {
description: "Post your answer on the issue as a comment, in Markdown.",
parameters: Schema.Struct({ issue: Schema.Number, markdown: Schema.String }),
success: Schema.Struct({ url: Schema.String }),
}) {}
export const PostAnswerLive = PostAnswer.make(
Effect.gen(function* () {
const comment = yield* GitHub.CreateIssueComment(yield* Repo);
return Effect.fn(function* ({ issue, markdown }) {
const posted = yield* comment({ issue_number: issue, body: markdown }).pipe(Effect.orDie);
return { url: posted.html_url };
});
}),
).pipe(Layer.provide(GitHub.CreateIssueCommentHttp));

The Layer is named after the tool, with Live. A failure only this tool returns lives in its file too.

A process’s file holds the process, and imports what it mentions:

src/Triage.process.ts
import * as AI from "@alchemy.run/ai";
import { Answering } from "./Answering.process.ts";
import { PostAnswer } from "./PostAnswer.tool.ts";
export class Triage extends AI.Process<Triage>()("Triage", {
description: "Answer one issue of the repository.",
})`
You look after one issue. Follow ${Answering}, then call ${PostAnswer}.
` {}

A process declared by name alone keeps its toLayer in the same file, as TriageLive. A second way to carry it out gets a file of its own.

Model names share one file, Models.ts, with the Layers behind them: they are a line each, and .model.ts already means a Domain model. The Worker and the sandbox Layer keep plain names: Worker.ts, Sandbox.ts.

src/
Answering.process.ts
Models.ts
PostAnswer.tool.ts
Sandbox.ts
SearchIssues.tool.ts
Triage.process.ts
Worker.ts

A Domain model’s files follow its own layout, in a folder or a package of their own. A process is not one of them: it can work with several models, so it sits beside the tools, its name and its prose in one .process.ts. When the prose mentions a model whose policy holds the process, the mentions and the model’s members are thunks.

src/
Board/
Board.model.ts
Issue.aggregate.ts
IssueWork.policy.ts hands each issue to its Triage session
LabelIssue.command.ts
Answering.process.ts
PostAnswer.tool.ts
Triage.process.ts the name, and what a session is told
Worker.ts the Layers: harness, model, sandbox, hosts

A page reads the board through the model’s Api, and a session through a route the Worker serves from the session itself. The page is in web/, and imports only the model’s Api, ids and value Schemas, and the Schema of that route.

src/
Board/
WebApi.api.ts what a page may send and read
IssueList.view.ts every issue, for the page's list
SessionRead.ts what the page reads of a session: its status and new items
Worker.ts serves the Api at /api and a session at /sessions/<key>
web/
src/
Board.ts Model, Message, update, Commands, Subscriptions
View.ts
entry.ts
worker.ts serves the build, forwards /api to the board
test/
Board.verify.test.ts the model's rules
Board.story.test.ts the page's update
App.test.ts the page with the board, in memory
integ.test.ts the page with the deployed app

See Connect a front end for what each file holds.

examples/cloudflare-ai-issues is laid out this way.