Skip to content

Extensions

An extension is a bundle a conversation selects by name: sections added to the system prompt, tools that come with it, and hooks on what the harness does. Use one for behavior that belongs to some conversations and not others, such as a review policy or a persona.

An extension has a name and what it declares up front: sections that never change, and the tools it brings.

src/extensions.ts
import * as Pi from "@alchemy.run/pi";
export class House extends Pi.Extension<House>()("house", {
sections: { style: "Answer in one short paragraph." },
}) {}

List it on the harness. Extensions on the harness are selected by every conversation unless it says otherwise:

const harness = yield* Pi.Harness({ models: [Fast], extensions: [House] });

An extension’s third argument is an Effect that returns its behavior. A beforeTool hook sees each call before it runs, and Pi.block refuses it with a reason the model reads:

src/extensions.ts
import * as Pi from "@alchemy.run/pi";
import * as Effect from "effect/Effect";
export class Guard extends Pi.Extension<Guard>()(
"guard",
{ sections: { policy: "Deploys need approval." } },
Effect.gen(function* () {
return {
hooks: {
beforeTool: Effect.fn(function* (call) {
return call.name === "deploy" ? Pi.block("Deploys need approval.") : undefined;
}),
},
};
}),
) {}

Returning undefined changes nothing. Returning { arguments } runs the call with those arguments instead.

A section can be computed before each model request, from the conversation it is for:

src/extensions.ts
import * as Pi from "@alchemy.run/pi";
import * as Effect from "effect/Effect";
export class Clock extends Pi.Extension<Clock>()(
"clock",
{},
Effect.gen(function* () {
return {
sections: {
now: Effect.fn(function* ({ conversationId }) {
return `Conversation ${conversationId}. The time is ${new Date().toISOString()}.`;
}),
},
};
}),
) {}

afterTool replaces a call’s result. beforeRequest replaces the messages of one model request. afterResponse sees each answer. onYield runs when the model stops, and can keep the run going with another message:

onYield: Effect.fn(function* (answer) {
const text = answer.content.map((part) => (part.type === "text" ? part.text : "")).join("");
return text.includes("DONE") ? undefined : { continue: "Keep going until the tests pass." };
}),

The Effect that builds an extension’s behavior runs where the harness is yielded, so it can declare bindings, as a tool’s does. An extension with behavior but no third argument is implemented by a Layer, with Guard.make(...).

A conversation selects exactly the extensions it names, or adds and removes from the harness’s:

yield* harness.createConversation({ name: "review", extensions: [Guard] });
yield* harness.configure("review", { extensions: { remove: [House] } });

Selecting an extension enables the tools it brings.