Functions
The free functions that take any SessionBase: loop, tools, findTools.
Functions
Three functions exported from the package rather than hung on the session, so they work with anything that satisfies SessionBase: the managed session, a Managed Agents session from @codespar/managed-agents-adapter, or a custom runtime.
loop
loop(session: SessionBase, config: LoopConfig): Promise<LoopResult>Runs the steps you give it through execute(); a payment step moves money.
The Complete Loop: a list of tool steps run in order, each able to read the results before it, with retries and an abort policy. Client-side orchestration over session.execute; nothing new on the wire.
| Parameter | Type | Required | Description |
|---|---|---|---|
session | SessionBase | yes | Anything with execute |
config | LoopConfig | yes | Steps, callbacks, retry policy, abort policy |
LoopConfig field | Type | Required | Description |
|---|---|---|---|
steps | LoopStep[] | yes | { tool, params, when? }; params may be a function of the previous results, when skips the step when it returns false |
onStepComplete | (step, result, index) => void | no | After each successful step |
onStepError | (step, error, index) => void | no | After a step fails past its retries |
retryPolicy | { maxRetries?, backoff?: "linear" | "exponential", baseDelay? } | no | Default: no retries; baseDelay 1000 ms |
abortOnError | boolean | no | Default true: stop at the first failed step |
const = await (, {
: [
{
: "codespar_charge",
: { : 150, : "BRL", : "pix", : "Order 0000", : { : "Example Buyer" } },
},
{
: "codespar_notify",
: () => ({
: "whatsapp",
: "+5500000000000",
: `Pay here: ${([0]?. as { ?: string }).}`,
}),
: () => [0]?. === true,
},
],
: { : 2, : "exponential" },
});
.(., ., "of", .);interface LoopResult {
success: boolean;
/** One ToolResult per step that ran, in order */
results: ToolResult[];
/** ms */
duration: number;
completedSteps: number;
totalSteps: number;
}
interface LoopStep {
tool: string;
params: Record<string, unknown> | ((prevResults: ToolResult[]) => Record<string, unknown>);
when?: (prevResults: ToolResult[]) => boolean;
}Throws nothing of its own: a step that throws (a CodesparApiError from execute) is caught, retried per the policy, reported to onStepError, and ends the loop with success: false when abortOnError is on.
Related How it works for the loop in prose.
tools
tools(session: SessionBase): Promise<Tool[]>The session's tools, for a SessionBase that carries a tools() method (the managed session does). Returns [] for one that does not. What framework adapters call to build their tool definitions.
| Parameter | Type | Required | Description |
|---|---|---|---|
session | SessionBase | yes |
const = await ();
.(.(() => .));// The same Tool as session.tools()
Tool[]Throws nothing beyond what the session's own tools() throws.
Related session.tools, Framework adapters.
findTools
findTools(session: SessionBase, query: string): Promise<Tool[]>tools(session) filtered to names and descriptions containing query, case-insensitively.
| Parameter | Type | Required | Description |
|---|---|---|---|
session | SessionBase | yes | |
query | string | yes | The substring to look for |
const = await (, "ship");Tool[]Throws nothing beyond what tools throws.
Related session.findTools, discover for a catalog-wide search.