iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / core / lib / src / agents / api.tsmain at aacbc74

// api.ts — `itx.agents`, the agents app's published type. A mind installs the app (install.ts
// mounts the collection facet as the `itx.agents` rewrite rule), so iterate/api does not name it.
// Importing `iterate/agents` registers the root on iterate/api's `InstalledAppRoots`, and a caller
// that knows agents are installed writes `itx as IterateContextApiWith<"agents">`. catalog.ts and
// collection.ts implement it.
import type { StreamEvent } from "../stream/processor.ts";

/** What `agents.get(path).message(input)` takes: the words, or the words with attachments (each
 *  stored under the agent's path in `itx.files` and named on the event). */
export type AgentMessageInput =
  | string
  | {
      message: string;
      files?: { contentType: string; filename: string; data: Uint8Array | ArrayBuffer | string }[];
    };

/** How `message` delivers the words. `trigger: false` makes them context only: the agent reads
 *  them on its next turn but does not wake for them (an FYI that needs nothing now). The default,
 *  `trigger: true`, starts a turn, unless the mind gates words between agents (message.ts).
 *  `key` names this one message, by the sender's choice (at most 200 characters): sent again with the
 *  same key and the same words, it lands once, and the call answers the event the log already
 *  holds. A sender that cannot know whether its first try arrived (a page after a lost
 *  connection) sends again with the key it used. The key's name space is the agent's context,
 *  whoever sends: make keys that nobody else would make. A file of a keyed message is never
 *  replaced; the same key with other words or other file content is refused (message.ts). */
export type AgentMessageOptions = { trigger?: boolean; key?: string };

/** `itx.agents.get(path)`: one agent. Anything else on its context is a plain
 *  `itx.cd(path).append(…)`, stamped with where it came from. */
export interface AgentHandleApi {
  /** Words for the agent: ONE `events.iterate.com/agent/message-received` on its context, the
   *  input every kind of agent reads, answered so a caller can wait for what follows it. The
   *  mind's config transcribes it into the input of the mind's own agent, and that input starts
   *  the agent's next request (or, with `{ trigger: false }`, is context that it reads then). The
   *  fact names the sender when another agent sent it: the collection's base, which the sender's
   *  own `itx.agents` row pins (collection.ts). An agent of another kind gets the same fact, for
   *  its own subscription to deliver. A deleted agent refuses, and so does one never created
   *  (NOT_CREATED). */
  message(input: AgentMessageInput, options?: AgentMessageOptions): Promise<StreamEvent>;
  /** What the agent did lately, from its own durable log: the last `minutes` (5 by default, at
   *  most 1,440), at most `limit` lines (30 by default, at most 200), oldest first, beside its
   *  catalog row. It wakes no agent: it reads the log, which wakes only the agent's context. The
   *  read starts a few thousand offsets before the newest event the catalog has copied from that
   *  log and reads at most 4,000 events, so a window older than that is cut short. */
  recent(options?: { minutes?: number; limit?: number }): Promise<AgentRecent>;
}

/** What `itx.agents.get(path).recent()` answers. */
export type AgentRecent = Pick<
  AgentListing,
  "path" | "title" | "status" | "activity" | "description"
> & {
  /** One line per thing it did in the window, oldest first: `status` its run's status label,
   *  `summary` what it said about itself (`setSummary`), `message-in` words it received,
   *  `message-out` words it sent, `script` the first line of a script it ran. Each `text` is one
   *  line, cut to 300 characters. */
  did: { at: string; what: AgentDidKind; text: string }[];
  /** The newest script it ran in the window: its status label, its whole source and, when it
   *  failed, the error. */
  lastScript: { at: string; status: string; script: string; error?: string } | null;
};
export type AgentDidKind = "status" | "summary" | "message-in" | "message-out" | "script";

/** One row of `itx.agents.list()`. */
export type AgentListing = {
  path: string;
  createdAt: string;
  parent: string | null;
  kind: string | null;
  host: string | null;
  status: "working" | "idle" | "waiting" | "ended" | null;
  activity: string | null;
  description: string | null;
  title: string | null;
  /** True when `title` is only the first line a person sent, and no name the agent has. */
  titleFromWords: boolean;
  lastActivityAt: string | null;
};

/** `itx.agents` — installed by rewrite rule on the mind's root and on each agent's context.
 *  `create` starts the mind's own agent, whose code is in the mind's config repo. A deleted agent
 *  is not re-creatable, and a `delete` of an agent never created refuses (NOT_CREATED). `delete`
 *  of an agent of another kind only records its death on `/agents`. */
export interface AgentsApi {
  /** Every agent, with what a list shows about it. An agent is any context under `/agents`, at any
   *  depth: an agent `create` made, a coding session a bridge appends to, anything. `parent`
   *  is the nearest agent above it (null at the top). `kind` is what runs it (`"agent"`,
   *  `"claude"`, …) and `host` the machine it runs on. `status` is `working`, `idle`, `waiting` (on
   *  someone) or `ended`; `activity` one line on what it is doing now, and `description` a few
   *  sentences on its purpose or conclusions. `title` is the name it gave itself, or else the first
   *  line a person sent it, and then `titleFromWords` is true. `lastActivityAt` is when it was last active. Each is null until the
   *  agent has said it. It reads only `/agents` and wakes no agent, so use it to show many agents
   *  rather than reading each one. */
  list(): Promise<AgentListing[]>;
  get(path: string): AgentHandleApi;
  create(path: string): Promise<{ path: string }>;
  delete(path: string): Promise<{ path: string }>;
}

/** The root's own `itx.agents` (catalog.ts `AgentCollectionDurableObject`), as code holding the
 *  root's scope reaches it. */
export interface AgentsRootApi extends AgentsApi {
  /** The collection as the context `base` sees it: what it creates is linked to `base`, and a
   *  relative path means `base`'s (collection.ts). Each agent's own `itx.agents` row is the root's
   *  `at(<agent>)`; root code acting for another context passes that context, as voice's
   *  `setupVoiceAgent` does. */
  at(base: string): AgentsApi;
}

declare module "iterate/api" {
  interface InstalledAppRoots {
    agents: AgentsApi;
  }
}