Skip to content
API reference

mule

Import
import * as api from "@fungi.computer/mule";
Kind Export Description
type alias MuleStreamFunction Pi’s model stream function, supplied explicitly by the caller.
type alias MuleIdentityKind Causal roles added around Pi’s model and tool loop.
type alias MuleIdentity One run-local causal identity.
type alias MuleError Expected failure from Mule’s model, tool, control, or listener work.
type alias MuleToolResult Result produced by a tool before Mule forms Pi’s correlated result message.
type alias MuleToolExecutionPart Progress or terminal output from one tool execution.
type alias MuleToolRejection A real parser or authorization rejection produced before tool execution.
type alias MuleCompletedTurn Facts from a completed Pi turn supplied to the retained stop hook.
type alias MuleEvent Lifecycle emitted around Pi values. Pi messages and events are not rewritten.
type alias MuleToolCheckpoint Durable evidence for a call in an interrupted assistant turn.
type alias MuleResume Completed assistant turn recovered by the transcript owner, before its next model call.
type alias MuleRunSettings Run inputs that are identical on the Promise and Effect roots.
type alias MuleRunOutcome Honest terminal result of one Mule invocation.
type alias MulePromiseListener Promise listener adapted once into Mule’s canonical Effect program.
type alias MuleToolPreparation Outcome of preparing one exact Pi tool call.
type alias MuleTool Pi tool declaration paired with its Promise preparation door.
type alias MuleRunInput Inputs for one complete Pi model/tool loop through the Promise facade.
type alias Mule Promise-facing Mule interface.
type alias ProviderRejection Bounded provider evidence, never response bodies, headers or error prose.
type alias ProviderStatusRecorder One provider call’s transport and parsed-event observations.
constant mule Default Promise-facing Mule interface.
constant streamModel Pi’s maintained stream behind Mule’s structural provider port.
function decodeProviderData() Decode Pi’s parsed request/event data before it enters an owned hook.
function normalizeModel() Validate a catalog model’s opaque sampling fields before it enters Mule.
function isRetryableProviderRejection() Retry only typed transient failures, never usage exhaustion or rejection.
function observeProviderStatus() Observe one call’s typed HTTP and parsed-event evidence. Pi owns stream decoding; this module retains no provider prose.

MuleStreamFunctiontype alias

Section titled “MuleStreamFunction”
Signature
type MuleStreamFunction = (
model,
context,
options?,
) => AssistantMessageEventStream | Promise<AssistantMessageEventStream>;

Pi’s model stream function, supplied explicitly by the caller.

Parameter Type
model Model<Api>
context Context
options? SimpleStreamOptions

| AssistantMessageEventStream | Promise<AssistantMessageEventStream>


MuleIdentityKindtype alias

Section titled “MuleIdentityKind”
Signature
type MuleIdentityKind =
"run" | "turn" | "message" | "tool_call" | "tool_result" | "terminal";

Causal roles added around Pi’s model and tool loop.


MuleIdentitytype alias

Section titled “MuleIdentity”
Signature
type MuleIdentity = object;

One run-local causal identity.

Signature
readonly kind: MuleIdentityKind;
Signature
readonly value: string;
Signature
readonly optional parent?: MuleIdentity;

MuleErrortype alias

Section titled “MuleError”
Signature
type MuleError = object;

Expected failure from Mule’s model, tool, control, or listener work.

Signature
readonly _tag: "MuleError";
Signature
readonly phase: "model" | "tool" | "control" | "listener";
Signature
readonly message: string;
Signature
readonly optional cause?: unknown;

The underlying rejection or thrown value, when one exists.


MuleToolResulttype alias

Section titled “MuleToolResult”
Signature
type MuleToolResult = object;

Result produced by a tool before Mule forms Pi’s correlated result message.

Signature
readonly content: (
| TextContent
| ImageContent)[];
Signature
readonly optional data?: JsonValue;

Application payload for listeners, never part of a model message.

Signature
readonly optional usage?: Usage;
Signature
readonly isError: boolean;
Signature
readonly optional terminate?: boolean;

MuleToolExecutionParttype alias

Section titled “MuleToolExecutionPart”
Signature
type MuleToolExecutionPart =
| {
type: "update";
result: MuleToolResult;
}
| {
type: "result";
result: MuleToolResult;
};

Progress or terminal output from one tool execution.


MuleToolRejectiontype alias

Section titled “MuleToolRejection”
Signature
type MuleToolRejection = object;

A real parser or authorization rejection produced before tool execution.

Signature
readonly message: string;
Signature
readonly optional terminate?: boolean;

MuleCompletedTurntype alias

Section titled “MuleCompletedTurn”
Signature
type MuleCompletedTurn = object;

Facts from a completed Pi turn supplied to the retained stop hook.

Signature
readonly message: AssistantMessage;
Signature
readonly toolResults: readonly ToolResultMessage[];
Signature
readonly context: readonly Message[];
Signature
readonly newMessages: readonly Message[];

MuleEventtype alias

Section titled “MuleEvent”
Signature
type MuleEvent =
| {
type: "run_start";
run: MuleIdentity;
}
| {
type: "model_prompt";
run: MuleIdentity;
turn: MuleIdentity;
attempt: number;
systemPrompt: string;
}
| {
type: "turn_start";
run: MuleIdentity;
turn: MuleIdentity;
}
| {
type: "message_start";
run: MuleIdentity;
turn: MuleIdentity;
message: MuleIdentity;
value: Message;
}
| {
type: "message_update";
run: MuleIdentity;
turn: MuleIdentity;
message: MuleIdentity;
value: AssistantMessage;
event: AssistantMessageEvent;
}
| {
type: "message_end";
run: MuleIdentity;
turn: MuleIdentity;
message: MuleIdentity;
value: Message;
data?: JsonValue;
}
| {
type: "tool_execution_start";
replay: "safe" | "unsafe";
run: MuleIdentity;
turn: MuleIdentity;
toolCall: MuleIdentity;
call: ToolCall;
}
| {
type: "tool_execution_update";
run: MuleIdentity;
turn: MuleIdentity;
toolCall: MuleIdentity;
call: ToolCall;
result: MuleToolResult;
}
| {
type: "tool_execution_end";
run: MuleIdentity;
turn: MuleIdentity;
toolCall: MuleIdentity;
call: ToolCall;
result: MuleToolResult;
}
| {
type: "turn_end";
run: MuleIdentity;
turn: MuleIdentity;
message: AssistantMessage;
toolResults: readonly ToolResultMessage[];
}
| {
type: "run_end";
run: MuleIdentity;
terminal: MuleIdentity;
outcome: "completed" | "cancelled" | "failed";
};

Lifecycle emitted around Pi values. Pi messages and events are not rewritten.

Signature
{
type: "run_start";
run: MuleIdentity;
}

Signature
{
type: "model_prompt";
run: MuleIdentity;
turn: MuleIdentity;
attempt: number;
systemPrompt: string;
}
Name Type Description
type "model_prompt" Exact prompt sent on this model attempt, including after a retry.
run MuleIdentity -
turn MuleIdentity -
attempt number -
systemPrompt string -

Signature
{
type: "turn_start";
run: MuleIdentity;
turn: MuleIdentity;
}

Signature
{
type: "message_start";
run: MuleIdentity;
turn: MuleIdentity;
message: MuleIdentity;
value: Message;
}

Signature
{
type: "message_update";
run: MuleIdentity;
turn: MuleIdentity;
message: MuleIdentity;
value: AssistantMessage;
event: AssistantMessageEvent;
}

Signature
{
type: "message_end";
run: MuleIdentity;
turn: MuleIdentity;
message: MuleIdentity;
value: Message;
data?: JsonValue;
}
Name Type Description
type "message_end" -
run MuleIdentity -
turn MuleIdentity -
message MuleIdentity -
value Message -
data? JsonValue Application payload independent of the completed model message.

Signature
{
type: "tool_execution_start";
replay: "safe" | "unsafe";
run: MuleIdentity;
turn: MuleIdentity;
toolCall: MuleIdentity;
call: ToolCall;
}

Signature
{
type: "tool_execution_update";
run: MuleIdentity;
turn: MuleIdentity;
toolCall: MuleIdentity;
call: ToolCall;
result: MuleToolResult;
}

Signature
{
type: "tool_execution_end";
run: MuleIdentity;
turn: MuleIdentity;
toolCall: MuleIdentity;
call: ToolCall;
result: MuleToolResult;
}

Signature
{
type: "turn_end";
run: MuleIdentity;
turn: MuleIdentity;
message: AssistantMessage;
toolResults: readonly ToolResultMessage[];
}

Signature
{
type: "run_end";
run: MuleIdentity;
terminal: MuleIdentity;
outcome: "completed" | "cancelled" | "failed";
}
Name Type Description
type "run_end" The run’s one terminal. A run interrupted after run_start ends cancelled; a run that fails with a non-listener MuleError ends failed. A listener failure ends the run without this event.
run MuleIdentity -
terminal MuleIdentity -
outcome "completed" | "cancelled" | "failed" -

MuleToolCheckpointtype alias

Section titled “MuleToolCheckpoint”
Signature
type MuleToolCheckpoint =
| {
call: ToolCall;
state: "pending";
}
| {
call: ToolCall;
state: "started";
replay: "safe" | "unsafe";
}
| {
call: ToolCall;
state: "completed";
result: MuleToolResult;
message?: ToolResultMessage;
};

Durable evidence for a call in an interrupted assistant turn.

Signature
{
call: ToolCall;
state: "pending";
}

Signature
{
call: ToolCall;
state: "started";
replay: "safe" | "unsafe";
}

Signature
{
call: ToolCall;
state: "completed";
result: MuleToolResult;
message?: ToolResultMessage;
}
Name Type Description
call ToolCall -
state "completed" -
result MuleToolResult -
message? ToolResultMessage Present when the transcript message was committed too.

MuleResumetype alias

Section titled “MuleResume”
Signature
type MuleResume = object;

Completed assistant turn recovered by the transcript owner, before its next model call.

Signature
readonly message: AssistantMessage;
Signature
readonly tools: readonly MuleToolCheckpoint[];

MuleRunSettingstype alias

Section titled “MuleRunSettings”
Signature
type MuleRunSettings = object;

Run inputs that are identical on the Promise and Effect roots.

Signature
readonly model: Model<Api>;
Signature
readonly optional stream?: MuleStreamFunction;

Override Pi’s default streamSimple, primarily for explicit providers and tests.

Signature
readonly systemPrompt: string;
Signature
readonly messages: readonly Message[];
Signature
readonly optional resume?: MuleResume;

Continue a durable, completed assistant turn before requesting another response.

Signature
readonly optional streamOptions?: Omit<SimpleStreamOptions, "signal" | "maxRetries">;

Provider-native retries are disabled; the typed host policy owns retry.

Signature
readonly optional modelStreamIdleTimeoutMs?: number;

Maximum silence between provider stream events, in milliseconds.

Signature
readonly optional modelStreamLimits?: Partial<{
firstEventMs: number;
attemptMs: number;
outputBytes: number;
}>;

Trusted per-attempt safety limits may only tighten Mule’s defaults.

Signature
readonly optional toolExecution?: "parallel" | "sequential";
Signature
readonly optional retry?: RetryPolicy;

Bounded retry around each assistant call, using Pi’s exponential delay. Undefined means no retry, matching pi-ai’s own default. Hosts that want Pi coding-agent’s numbers pass piCodingAgentRetryDefaults. Enabled policy counts and delays must be non-negative safe integers. Each assistant turn captures its policy before requesting the model.

Signature
readonly retryable: (message) => boolean;

Host-owned typed decision for this exact assistant message. Only true permits retry; unknown messages return false, never a decision from prose.

Parameter Type
message AssistantMessage

boolean

Signature
readonly optional retryAfterMs?: (message) => number | undefined;

Provider’s bounded Retry-After evidence for the last assistant error.

Parameter Type
message AssistantMessage

number | undefined


MuleRunOutcometype alias

Section titled “MuleRunOutcome”
Signature
type MuleRunOutcome = object;

Honest terminal result of one Mule invocation.

Signature
readonly status: "completed" | "cancelled" | "failed";
Signature
readonly run: MuleIdentity;
Signature
readonly terminal: MuleIdentity;
Signature
readonly message: AssistantMessage;
Signature
readonly messages: readonly Message[];
Signature
readonly optional modelLimitReason?: string;

Mule-owned limit diagnostic, not untrusted provider prose.


MulePromiseListenertype alias

Section titled “MulePromiseListener”
Signature
type MulePromiseListener = (event, signal) => Promise<void> | void;

Promise listener adapted once into Mule’s canonical Effect program.

Parameter Type
event MuleEvent
signal AbortSignal

Promise<void> | void


MuleToolPreparationtype alias

Section titled “MuleToolPreparation”
Signature
type MuleToolPreparation =
| {
status: "prepared";
execute: (signal) => AsyncIterable<MuleToolExecutionPart>;
}
| (object & MuleToolRejection);

Outcome of preparing one exact Pi tool call.

Signature
{
status: "prepared";
execute: (signal) => AsyncIterable<MuleToolExecutionPart>;
}
Name Type Description
status "prepared" -
execute() (signal) => AsyncIterable<MuleToolExecutionPart> Run the already-decoded and authorized call; abort ends it early.

object & MuleToolRejection


Signature
type MuleTool = object;

Pi tool declaration paired with its Promise preparation door.

Signature
readonly descriptor: Tool;
Signature
readonly optional replay?: "safe" | "unsafe";

Only read-only or explicitly idempotent tools may opt into crash replay.

Signature
readonly optional executionMode?: "parallel" | "sequential";
Signature
readonly prepare: (call, signal) => Promise<MuleToolPreparation>;

Decode and authorize one exact Pi tool call before it can execute.

Parameter Type
call ToolCall
signal AbortSignal

Promise<MuleToolPreparation>


MuleRunInputtype alias

Section titled “MuleRunInput”
Signature
type MuleRunInput = MuleRunSettings & object;

Inputs for one complete Pi model/tool loop through the Promise facade.

Name Type Description
tools? readonly MuleTool[] -
resolveApiKey()? (provider, signal) => Promise<string | undefined> Resolve a fresh provider credential immediately before each Pi call.
getSteeringMessages()? (signal) => Promise<readonly Message[]> Pull messages that should steer the active run at Pi’s safe point.
getFollowUpMessages()? (signal) => Promise<readonly Message[]> Pull messages queued until the run would otherwise finish.
shouldStopAfterTurn()? (turn, signal) => Promise<boolean> Request a graceful stop after the current Pi turn has fully settled.

Signature
type Mule = object;

Promise-facing Mule interface.

Signature
readonly run: (input, listener?, signal?) => Promise<MuleRunOutcome>;

Run one complete Pi model/tool loop. Rejects with the MuleError that failed the run, or with the abort reason (an AbortError when the reason is not an Error) after the caller aborts signal.

Parameter Type
input MuleRunInput
listener? MulePromiseListener
signal? AbortSignal

Promise<MuleRunOutcome>


ProviderRejectiontype alias

Section titled “ProviderRejection”
Signature
type ProviderRejection =
| Readonly<{
code: "usage-limit-reached";
resetAt?: number;
limit?: "five_hour" | "seven_day" | "seven_day_opus" | "seven_day_sonnet";
}>
| Readonly<{
code: "rate-limit";
retryAfterMs?: number;
}>
| Readonly<{
code: "overloaded";
}>
| Readonly<{
code: "provider_unavailable";
status?: number;
}>
| Readonly<{
code: "provider_rejected";
status: number;
}>;

Bounded provider evidence, never response bodies, headers or error prose.


ProviderStatusRecordertype alias

Section titled “ProviderStatusRecorder”
Signature
type ProviderStatusRecorder = Readonly<{
fetch: typeof globalThis.fetch;
onProviderStreamEvent: NonNullable<
SimpleStreamOptions["onProviderStreamEvent"]
>;
rejection: (message?) => ProviderRejection | undefined;
}>;

One provider call’s transport and parsed-event observations.

muleconstant

Section titled “mule”
Signature
const mule: Mule;

Default Promise-facing Mule interface.


streamModelconstant

Section titled “streamModel”
Signature
const streamModel: MuleStreamFunction;

Pi’s maintained stream behind Mule’s structural provider port.

decodeProviderData()function

Section titled “decodeProviderData()”
Signature
function decodeProviderData(data): ProviderData;

Decode Pi’s parsed request/event data before it enters an owned hook.

Parameter Type
data unknown

ProviderData


normalizeModel()function

Section titled “normalizeModel()”
Signature
function normalizeModel<TApi>(model): Model<TApi>;

Validate a catalog model’s opaque sampling fields before it enters Mule.

Type Parameter
TApi extends Api
Parameter Type
model Omit<Model<TApi>, "samplingParams" | "samplingParamsByThinkingLevel">

Model<TApi>


isRetryableProviderRejection()function

Section titled “isRetryableProviderRejection()”
Signature
function isRetryableProviderRejection(rejection): boolean;

Retry only typed transient failures, never usage exhaustion or rejection.

Parameter Type
rejection ProviderRejection | undefined

boolean


observeProviderStatus()function

Section titled “observeProviderStatus()”
Signature
function observeProviderStatus(options?): ProviderStatusRecorder;

Observe one call’s typed HTTP and parsed-event evidence. Pi owns stream decoding; this module retains no provider prose.

Parameter Type
options? Pick<SimpleStreamOptions, "onProviderStreamEvent" | "fetch">

ProviderStatusRecorder