Skip to main content

Functions

The free functions that take any SessionBase: loop, tools, findTools.

1 min read
View MarkdownEdit on GitHub

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

Writeloop(session: SessionBase, config: LoopConfig): Promise<LoopResult>
Can move moneynot in the Python package

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.

ParameterTypeRequiredDescription
sessionSessionBaseyesAnything with execute
configLoopConfigyesSteps, callbacks, retry policy, abort policy
LoopConfig fieldTypeRequiredDescription
stepsLoopStep[]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) => voidnoAfter each successful step
onStepError(step, error, index) => voidnoAfter a step fails past its retries
retryPolicy{ maxRetries?, backoff?: "linear" | "exponential", baseDelay? }noDefault: no retries; baseDelay 1000 ms
abortOnErrorbooleannoDefault true: stop at the first failed step
Example
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", .);
Result: LoopResult
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

Readtools(session: SessionBase): Promise<Tool[]>
Pythonsession.tools()

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.

ParameterTypeRequiredDescription
sessionSessionBaseyes
Example
const  = await ();
.(.(() => .));
Result: Tool[]
// The same Tool as session.tools()
Tool[]

Throws nothing beyond what the session's own tools() throws.

Related session.tools, Framework adapters.

findTools

ReadfindTools(session: SessionBase, query: string): Promise<Tool[]>
Pythonsession.find_tools(intent)

tools(session) filtered to names and descriptions containing query, case-insensitively.

ParameterTypeRequiredDescription
sessionSessionBaseyes
querystringyesThe substring to look for
Example
const  = await (, "ship");
Result: Tool[]
Tool[]

Throws nothing beyond what tools throws.

Related session.findTools, discover for a catalog-wide search.

Functions | CodeSpar