Operation

Operations: work that outlives a request (a human's answer, a connect link, an agent turn). op.wait() returns the typed outcome for its kind.

class Operation

A long-running piece of work. T is what wait() resolves to.

Attributes
idstring
kindstring
statusOperationStatus
stepOperationStep | null

Where a running operation is: an agent turn on a CLI harness delivering, then working; a number's port submitted, then scheduled. Null otherwise.

resultunknown

The raw result once succeeded.

createdAtDate | null
updatedAtDate | null
summaryTextstring | null

A short line saying what it is (a message preview, an addressee, a command).

userIdstring | null
keyIdstring | null

The key that started it.

agentIdstring | null
inputJsonObject | null

What it was started with (on a single operation's view, for account principals).

workstreamNamedWorkstream | null

The workstream its work belongs to, as it reads now (a renamed one by its new name, a merged one as the one it joined); null when its work named none. A single operation's view and Operation.list() name it.

causestring | null

Why a failed one failed, as one key: its error's type and reason and what its fix acts on (the cap that stopped it, the connection it couldn't use). Operations that failed the same way share it; Operation.failures() groups by it and Operation.list({ cause }) takes it. On the list and a read of one; null unless it failed.

failedOnFailedOn | null

What a failed one's fix acts on (FailedOn); null when it's NoClick's own to fix, or it didn't fail. On the list and a read of one.

startedAtDate | null
finishedAtDate | null
waitMsnumber | null

Time queued before it ran.

runMsnumber | null

Time running.

heldMsnumber | null

Time waiting on a person.

progressOperationProgress | null

What the work reported as it went (null before it reported anything). An agent that started it hears each report as an operation event.

undoableboolean | null

Whether undo() can take it back now: a reversible write (a deletion that went to the trash, a dict's clear, a database migration) not undone yet. null for kinds that record no undo.

undoneAtDate | null

When it was undone, if it was.

undoRewindsstring | null

On a promote that bound databases, while it can be undone: what undoing it takes back from them (each goes back to the moment before the promote; what was written since is kept in the trash as a database copy). null otherwise.

keptCopyIdstring | null

On a database's restore: the copy it keeps of what the database held just before, in the trash (a TrashItem of kind database_copy whose resourceId this is) once it's done. undo() puts it back. null on other kinds.

transitionsOperationTransition[]

Every status change, oldest first (empty for operations recorded before they were kept).

consoleUrlstring | null

This operation in the developer console (null off the hosted API).

errorNoClickError | null

The failure as a NoClickError (null unless it failed).

doneboolean

Whether the operation reached a terminal status.

Types
type OperationStatus =
  | "queued" | "running" | "awaiting_human"
  | "succeeded" | "failed" | "canceled" | "expired";

/** Where a running operation is: an agent turn's `TurnStep`, or a port's `PortStep`. */
type OperationStep = TurnStep | PortStep;

/**
 * Where a running turn of an agent on a CLI harness is: `delivering` (starting its sandbox and
 * handing it the message) or `working` (the harness is on it).
 */
type TurnStep = "delivering" | "working";

/**
 * Invite-only beta. Where a number's port (`phone.port_in`) is: `submitted` (with the
 * current carrier) or `scheduled` (it moves at `portAt`).
 */
type PortStep = "submitted" | "scheduled";

type JsonObject = { [key: string]: unknown };

/** The workstream an operation's work belongs to, as it reads now. */
interface NamedWorkstream {
  id: string;
  name: string;
}

/**
 * What a failure's fix acts on, by id: the cap that stopped it (`object` `"cap"`, its `scope` `key`, `user`, `agent`,
 * `project` or `account`), a `connection`, the `account`'s funds or plan, a paused `user`, an `app`, a `workflow` or
 * an `agent`.
 */
interface FailedOn {
  object: "cap" | "connection" | "account" | "user" | "app" | "workflow" | "agent";
  id: string | null;
  scope?: "key" | "user" | "agent" | "project" | "account" | null;
}

/**
 * What an operation's work reported as it went: its `step`, its `stages`, the last 50 `lines` it printed (a build's
 * log, a call's turns, an agent's tool calls), `lineCount` (every line so far) and when it last said something.
 */
interface OperationProgress {
  step: string | null;
  stages: OperationStage[];
  lines: string[];
  lineCount: number;
  updatedAt: Date | null;
  raw: JsonObject;
}

/** One stage of what an operation's work planned (a deploy's: Queued, Building, Shipping, Live). */
interface OperationStage {
  name: string;
  status: "done" | "current" | "pending";
  startedAt: Date | null;
  endedAt: Date | null;
}

liststatic

static list(
  options?: OperationFilters & { limit?: number },
): Promise<Operation[]>

Operations matching the filters, newest first (or the first limit).

Parameters
options.kindstring | readonly string[]
options.statusOperationStatus | "live" | readonly (OperationStatus | "live")[]

Statuses, or "live" for every non-terminal one.

options.statusNotOperationStatus | readonly OperationStatus[]
options.agentstring | ApiObject

An Agent or agent id.

options.keystring

The requesting key: its id or its prefix (nk_live_ab12).

options.createdAfterDate | string
options.createdBeforeDate | string
options.updatedAfterDate | string

Rows changed since (for polling).

options.causestring

The failed ones of one cause (an OperationFailure's cause, from Operation.failures()).

options.limitnumber

Return at most this many.

options.userFilterUserFilter

Which users the read covers (user_id query), instead of acting as one (user).

options.userUserOption

undefined = the ambient user (asUser), null = the account itself.

(defaults to the current user)
options.clientNoClick

The client to send with; defaults to the module client built from NOCLICK_API_KEY.

Returns

Every matching operation, fetched page by page.

Types
type OperationStatus =
  | "queued" | "running" | "awaiting_human"
  | "succeeded" | "failed" | "canceled" | "expired";

/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

listPagestatic

static listPage(
  options?: OperationFilters & PageOptions,
): Promise<Page<Operation>>

One page of matching operations; pass nextCursor back as cursor.

Parameters
options.cursorstring | null

The next_cursor of the page before; omit it for the first page.

options.limitnumber

Return at most this many.

options.kindstring | readonly string[]
options.statusOperationStatus | "live" | readonly (OperationStatus | "live")[]

Statuses, or "live" for every non-terminal one.

options.statusNotOperationStatus | readonly OperationStatus[]
options.agentstring | ApiObject

An Agent or agent id.

options.keystring

The requesting key: its id or its prefix (nk_live_ab12).

options.createdAfterDate | string
options.createdBeforeDate | string
options.updatedAfterDate | string

Rows changed since (for polling).

options.causestring

The failed ones of one cause (an OperationFailure's cause, from Operation.failures()).

options.userFilterUserFilter

Which users the read covers (user_id query), instead of acting as one (user).

options.userUserOption

undefined = the ambient user (asUser), null = the account itself.

(defaults to the current user)
options.clientNoClick

The client to send with; defaults to the module client built from NOCLICK_API_KEY.

Returns

A page whose data holds the operations and whose next_cursor is None on the last page.

Types
type OperationStatus =
  | "queued" | "running" | "awaiting_human"
  | "succeeded" | "failed" | "canceled" | "expired";

/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

summarystatic

static summary(options?: OperationFilters): Promise<OperationSummary>

How many operations match, by status.

Parameters
options.kindstring | readonly string[]
options.statusOperationStatus | "live" | readonly (OperationStatus | "live")[]

Statuses, or "live" for every non-terminal one.

options.statusNotOperationStatus | readonly OperationStatus[]
options.agentstring | ApiObject

An Agent or agent id.

options.keystring

The requesting key: its id or its prefix (nk_live_ab12).

options.createdAfterDate | string
options.createdBeforeDate | string
options.updatedAfterDate | string

Rows changed since (for polling).

options.causestring

The failed ones of one cause (an OperationFailure's cause, from Operation.failures()).

options.userFilterUserFilter

Which users the read covers (user_id query), instead of acting as one (user).

options.userUserOption

undefined = the ambient user (asUser), null = the account itself.

(defaults to the current user)
options.clientNoClick

The client to send with; defaults to the module client built from NOCLICK_API_KEY.

Returns

The total, and in by_status a count for every status, zeros included.

Types
type OperationStatus =
  | "queued" | "running" | "awaiting_human"
  | "succeeded" | "failed" | "canceled" | "expired";

/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

failuresstatic

static failures(options?: FailureFilters): Promise<OperationFailures>

The failed operations matching the filters, grouped by why they failed: one cap used up or one connection disconnected fails every run behind it the same way, and each is one OperationFailure with how many, since when, the latest error and what it touched. limit caps how many causes (50).

const yesterday = new Date(Date.now() - 86_400_000);
const found = await Operation.failures({ createdAfter: yesterday, userFilter: ALL_USERS });
for (const cause of found.causes) console.log(cause.count, cause.error?.message, cause.error?.nextStep);
const runs = await Operation.list({ cause: found.causes[0].cause, userFilter: ALL_USERS });
Parameters
options.kindstring | readonly string[]
options.agentstring | ApiObject

An Agent or agent id.

options.keystring

The requesting key: its id or its prefix (nk_live_ab12).

options.createdAfterDate | string
options.createdBeforeDate | string
options.limitnumber

Return at most this many.

options.userFilterUserFilter

Which users the read covers (user_id query), instead of acting as one (user).

options.userUserOption

undefined = the ambient user (asUser), null = the account itself.

(defaults to the current user)
options.clientNoClick

The client to send with; defaults to the module client built from NOCLICK_API_KEY.

Returns

Each cause, the one that failed most lately first; total counts every failure matched.

Types
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

fromIdstatic

static fromId(operationId: string, options?: CallOptions): Operation<T>

A lazy reference to an operation by id.

Parameters
operationIdstringrequired
options.clientNoClick

The client to send with; defaults to the module client built from NOCLICK_API_KEY.

options.userUserOption

undefined = the ambient user (asUser), null = the account itself.

(defaults to the current user)
Returns

An operation that loads the first time you read a field. Nothing is sent yet.

Types
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

wait

wait(options?: { timeoutS?: number }): Promise<T>

Wait until the operation finishes and return its outcome (typed per kind: Connection, Answer, boolean, AgentReply, ...). Throws the operation's error if it failed, OperationCanceled / Expired, or OperationTimeout after timeoutS seconds.

Parameters
options.timeoutSnumber
Returns

The outcome, typed for the operation's kind: an AgentReply from agent.send, True or False from human.approve, the connected Connection from a connect link. A kind the client has no type for gives the raw result.

toolCalls

toolCalls(): Promise<ToolCall[]>

The tool calls an agent.message turn made, oldest first (none for other kinds).

Returns

Each call's tool, arguments, status, duration and a preview of its result.

events

async *events(options?: { intervalMs?: number }): AsyncGenerator<OperationEvent>

Yield an event for each status or step change until the operation finishes.

Parameters
options.intervalMsnumberdefault 1000
Returns

The changes, polled every interval seconds: first the status (and step) it has now, last its final one. An event's result is the raw result, not the typed outcome wait() returns.

cancel

cancel(): Promise<this>

Ask the server to cancel; returns the updated operation. A coding agent's turn (agent.message on a coding harness) stops in its sandbox first, its harness going on from the same session, so the next turn never runs beside it.

Returns

This operation, now canceled.

undo

undo(): Promise<JsonObject>

Take back a reversible write, once: a deletion comes out of the trash, a dict's cleared keys come back, a migrated database is restored to the moment before, a restored database gets back what the restore rewound, a promote puts the release before back with the app's databases as they were (each restore keeping what it rewinds in the trash, named in the result's copies). What reached the world (an email sent, a payment) has no undo: that throws Conflict, as does undoing twice. Returns what the undo put back, as its own API shows it.

Returns

What the undo put back, as its own API shows it (a restore of a database is an operation of its own).

Types
type JsonObject = { [key: string]: unknown };

OperationEvent

interface OperationEvent

A status or step change observed by Operation.events(): step is where a running operation is (delivering or working for a CLI agent's turn, submitted or scheduled for a port; None otherwise).

Attributes
statusOperationStatus
stepOperationStep | null

Where a running operation is (delivering or working for a CLI agent's turn, submitted or scheduled for a port); null otherwise.

updatedAtDate | null
resultunknown
errorNoClickError | null
rawJsonObject

The response JSON exactly as the API sent it.

Types
type OperationStatus =
  | "queued" | "running" | "awaiting_human"
  | "succeeded" | "failed" | "canceled" | "expired";

/** Where a running operation is: an agent turn's `TurnStep`, or a port's `PortStep`. */
type OperationStep = TurnStep | PortStep;

/**
 * Where a running turn of an agent on a CLI harness is: `delivering` (starting its sandbox and
 * handing it the message) or `working` (the harness is on it).
 */
type TurnStep = "delivering" | "working";

/**
 * Invite-only beta. Where a number's port (`phone.port_in`) is: `submitted` (with the
 * current carrier) or `scheduled` (it moves at `portAt`).
 */
type PortStep = "submitted" | "scheduled";

type JsonObject = { [key: string]: unknown };

OperationSummary

interface OperationSummary

How many operations match, by status.

Attributes
totalnumber
byStatusRecord<OperationStatus, number>
rawJsonObject

The response JSON exactly as the API sent it.

Types
type OperationStatus =
  | "queued" | "running" | "awaiting_human"
  | "succeeded" | "failed" | "canceled" | "expired";

type JsonObject = { [key: string]: unknown };

OperationFailure

interface OperationFailure

One cause of failures (Operation.failures()): count failed operations between firstAt and lastAt that failed the same way, on the same thing (one cap used up, one connection disconnected). failedOn is what their fix acts on (null when it's NoClick's own to fix). error is the latest one's, operationId that operation; cause is the key their views carry and Operation.list({ cause }) takes. Up to ten of the agents, users and keys they touched, beside how many.

Attributes
causestring
countnumber
firstAtDate | null
lastAtDate | null
failedOnFailedOn | null
errorNoClickError | null
operationIdstring
kindsstring[]
agentIdsstring[]
agentCountnumber
userIds(string | null)[]
userCountnumber
keyIdsstring[]
keyCountnumber
rawJsonObject

The response JSON exactly as the API sent it.

Types
/**
 * What a failure's fix acts on, by id: the cap that stopped it (`object` `"cap"`, its `scope` `key`, `user`, `agent`,
 * `project` or `account`), a `connection`, the `account`'s funds or plan, a paused `user`, an `app`, a `workflow` or
 * an `agent`.
 */
interface FailedOn {
  object: "cap" | "connection" | "account" | "user" | "app" | "workflow" | "agent";
  id: string | null;
  scope?: "key" | "user" | "agent" | "project" | "account" | null;
}

type JsonObject = { [key: string]: unknown };

OperationFailures

interface OperationFailures

Operation.failures(): each cause, the one that failed most lately first; total counts every failure matched.

Attributes
causesOperationFailure[]
totalnumber
hasMoreboolean

Causes past the limit were left out.

rawJsonObject

The response JSON exactly as the API sent it.

Types
type JsonObject = { [key: string]: unknown };

OperationTransition

interface OperationTransition

One status change the server recorded (by names who canceled).

Attributes
statusOperationStatus
atDate | null
bystring | null
Types
type OperationStatus =
  | "queued" | "running" | "awaiting_human"
  | "succeeded" | "failed" | "canceled" | "expired";