# @jigs-ai/jigs v0.110.4

APIs for implementing a factory-owned custom agent step. Most workflows use
`runAgent` and other generated routines instead.

Call these inside `"use step"` code. `createAgentRunner` provides a live provider
model with jigs policy applied.
See [Custom agent steps](https://salimhamed.github.io/jigs/guide/custom-agent-step).

## Agent runner

### AgentRunner

A harness ready to run in a worktree, from [createAgentRunner](#createagentrunner). Pass `model` to the AI
SDK's `generateText`, read the session reference from its result with `sessionFrom`, and call
`close` when the call is done.

#### Methods

##### close()

> **close**(): `Promise`\<`void`\>

Release the worktree lock and stop what the harness started. Safe to call twice.

###### Returns

`Promise`\<`void`\>

##### sessionFrom()

> **sessionFrom**(`result`): `AgentSessionRef` \| `undefined`

The session reference in a `generateText` result, for a later step to resume.

###### Parameters

###### result

###### providerMetadata?

`Record`\<`string`, `Record`\<`string`, `unknown`\>\> \| `null`

###### Returns

`AgentSessionRef` \| `undefined`

#### Properties

##### model

> **model**: `LanguageModel`

The live provider model, with jigs' policy already applied.

***

### AgentRunnerOptions

Where [createAgentRunner](#createagentrunner) runs a harness, and the session it resumes.

#### Properties

##### cwd

> **cwd**: `string`

The worktree the agent works in.

##### resume?

> `optional` **resume**: `AgentSessionRef`

A session reference from an earlier call to resume.

##### run

> **run**: [`RunMetadata`](#runmetadata)

The run the step belongs to: `getWorkflowMetadata()` inside the step.

***

### createAgentRunner()

> **createAgentRunner**(`harness`, `options`): `Promise`\<[`AgentRunner`](#agentrunner)\>

Open a Codex harness inside a factory's own step, the way the built-in agent step does: the
environment allowlist with the factory's `agents.env`, the request and just-in-time checks, the
worktree lock, and Codex's private home and app server. The returned `model` is the live
provider, ready for `generateText`.

#### Parameters

##### harness

`Harness`

##### options

[`AgentRunnerOptions`](#agentrunneroptions)

#### Returns

`Promise`\<[`AgentRunner`](#agentrunner)\>

#### Remarks

Call it inside a `"use step"` function, never in a workflow. The step can hand the provider a
function, such as a tool-approval hook or a logger, because a step runs where functions are
allowed; pass it through the AI SDK call.

It throws `JitCheckError` when a just-in-time check fails, and [AgentSessionError](#agentsessionerror) when
`resume` names a session this harness cannot resume. Claude Code and Pi have no AI SDK provider
model, so their descriptors throw: run them with `runAgent`.

It reads the run's status before opening the harness and watches it until `close`. It throws
a fatal error instead of opening on a cancelled run, and once the run is cancelled every
provider call through `model` is aborted and fails with that fatal error.

#### Example

```ts
import type { AgentSessionRef, Harness } from "@jigs-ai/jigs";
import { createAgentRunner } from "@jigs-ai/jigs/steps";
import { generateText } from "ai";
import { getWorkflowMetadata } from "workflow";

export async function runWithTemperature(request: {
  harness: Harness;
  cwd: string;
  prompt: string;
  resume?: AgentSessionRef | undefined;
}) {
  "use step";
  const runner = await createAgentRunner(request.harness, {
    cwd: request.cwd,
    run: getWorkflowMetadata(),
    resume: request.resume,
  });
  try {
    const result = await generateText({ model: runner.model, prompt: request.prompt, temperature: 0 });
    return { text: result.text, session: runner.sessionFrom(result) };
  } finally {
    await runner.close();
  }
}
```

## Errors

### AgentSessionError

A durable agent session is missing or cannot be resumed by this harness.

#### Extends

- `Error`

#### Properties

##### name

> `readonly` **name**: `"AgentSessionError"` = `"AgentSessionError"`

###### Overrides

`Error.name`

***

### ProviderApiError

A provider answered a call with a failure: an error status, or a refusal in its body.

#### Extends

- `JigsError`

#### Constructors

##### Constructor

> **new ProviderApiError**(`init`): [`ProviderApiError`](#providerapierror)

###### Parameters

###### init

`ProviderApiErrorInit`

###### Returns

[`ProviderApiError`](#providerapierror)

###### Overrides

`JigsError.constructor`

#### Properties

##### body

> `readonly` **body**: `string`

##### code?

> `readonly` `optional` **code**: `string`

##### hint?

> `readonly` `optional` **hint**: `string`

What the reader can do about it. Quote each command to run in backticks, as in
``stop the service first: `pnpm exec jigs service stop` ``: the CLI prints it on its own line.

###### Inherited from

`JigsError.hint`

##### provider

> `readonly` **provider**: `Provider`

##### status

> `readonly` **status**: `number`

## Runtime metadata

### RunMetadata

> **RunMetadata** = `Pick`\<`WorkflowMetadata`, `"workflowRunId"`\>

The run a step belongs to: `getWorkflowMetadata()` inside the step.
