ui / src / agent-kind-view / types.tsmain at d68d7e1
// AN AGENT KIND's view (iterate/agents/kinds `AgentKind` says what a kind is). A kind file's default
// export `satisfies AgentKind` from here, which is that one with `view` typed: the mind worker
// imports the file (`makeAgentKind`) and runs `processEvent`; the Agents app's sandboxed frame loads
// the same source for `view` (frame.ts), its server-only imports stubbed (load.ts).
//
// import type { AgentKind } from "@iterate-com/ui/agent-kind-view";
//
// export default {
// kind: "echo",
// view: ({ html, useMemo, ui }) => (props) => html`<${ui.AgentShell} ...${props} />`,
// } satisfies AgentKind;
//
// THE AGENT (the kind `agent`: its facts are agent-chat/agent-facts.ts) needs no adapter: `AgentChat`
// reads its facts, its live state and its request traces, and its composer appends the Dash's
// words and files as one `agent-ui/message-received` (`storeFiles`, then `onAppend`). Its file:
//
// export default {
// kind: "agent",
// processEvent,
// // the streamed windows of an answer, which a wildcard never matches
// ephemeralEvents: ["events.iterate.com/llm-request/response-frame-received"],
// newAgent: { default: true, choices: [{ id: "agent", label: "Agent", byPerson: true }], start },
// view: ({ html, ui }) => (props) => html`<${ui.AgentChat} ...${props} />`,
// } satisfies AgentKind;
//
// Types only: nothing here runs.
// Self-contained (React, react-query and iterate's types only), so a mind's config repo, a test
// program or a package without JSX can name them.
import type { ComponentType } from "react";
import type { useMutation, useQuery } from "@tanstack/react-query";
import type { AgentKind as SdkAgentKind } from "iterate/agents/kinds";
/** One event of the agent's log, as the view reads it. */
export type AgentKindViewEvent = {
type: string;
offset: number;
createdAt: string;
payload?: any;
path?: string;
ephemeral?: boolean;
source?: Record<string, unknown>;
};
/** The chat's state (agent-chat-search.ts `AgentChatState`): the page's URL search. `view:
* "events"` is the Events tab, and `event` the offset its inspector shows. */
export type AgentKindViewState = {
view?: "chat" | "events";
event?: number;
[key: string]: unknown;
};
/** What a view is handed: AgentChat's props. Authority over the agent reaches it only through
* these, scoped to this one agent. */
export type AgentKindViewProps = {
/** The agent's path. */
path: string;
/** The agent's log, live: every durable event, and the kind's `ephemeralEvents`. */
context: {
events: readonly AgentKindViewEvent[];
caughtUp: boolean;
error?: string;
/** Pass it on to `Feed` as it is. None: the whole log is held. `failed`: the read failed. */
older?: { loadOlder(): void; loading: boolean; exhausted: boolean; failed?: boolean };
head?: number;
processors: { rows: readonly any[]; loaded: boolean; error?: string };
presence: { actors: readonly any[]; rpcStubs: readonly string[] };
liveState: Record<string, any>;
/** The version of the agent's path that `events` are of, once a read has said it. A new
* version is a new conversation: `AgentChat` closes what it had open of the last one. */
version?: number;
};
/** The host's own failure (opening the agent's context). */
error: string | undefined;
/** The chat's state, which is the page's URL: the tab, the Events tab's choices. */
state: AgentKindViewState;
onStateChange: (patch: Partial<AgentKindViewState>) => void;
/** Say something to the agent: `itx.agents.get(path).message(…)`. */
onMessage: (input: {
message: string;
files: { contentType: string; filename: string; data: string }[];
/** The message's own key, from `AgentChat`: the host sends it on, and a send tried again
* lands once. */
key?: string;
}) => Promise<void>;
/** Append events to the agent's context, as a person wrote them. */
onAppend: ((events: unknown[]) => Promise<unknown>) | undefined;
/** Stores a message's files under the agent's path, as `message()` stores them (the same path
* form), and answers each as the mind's file store holds it: a view then names them in a fact
* it appends. `key`: the message's own key, so the same files sent again go to the same paths.
* None: the host cannot store files (a page older than this verb). */
storeFiles?: (
files: { contentType: string; filename: string; data: string }[],
key?: string,
) => Promise<{ contentType: string; filename: string; path: string; size: number }[]>;
/** A signed download URL for a file under the agent's path. */
signedUrl: (path: string) => Promise<string>;
/** A few durable events of the agent's own log, by offset: those after `after`, at most
* `limit` (the page gives 8 at most). For a view that knows where a fact lies and must not
* page down the log to it. It rejects on a page older than this verb: page on then, with
* `context.older`. */
readEvents?: (after: number, limit: number) => Promise<{ events: AgentKindViewEvent[] }>;
/** The viewer's actor id or address, when the page knows it: `AgentChat` draws no sender line
* under their own messages. */
me?: string;
/** The people who wrote the held messages, by the platform's id on a message
* (`source.principal.actor`): the name and the picture that the person's own context says
* (`/users/<id>`), the picture as an address that the page signed as the viewer. Only those
* with a name or a picture; none on a page whose viewer may not read them (a guest), and
* none on a page older than this field. A view draws initials for anyone who is not here. */
people?: Readonly<Record<string, { name?: string; picture?: string }>>;
/** The agents below this one that are at work now, by path, each with its row's own words: its
* `status` (`working`, `waiting`, or `idle` while a script of its last answer may still run),
* what it says it does (`activity`) and when it last did anything (below.ts). A view draws a
* thread's line with them. Empty when none works; none on a page older than this field. */
atWork?: Readonly<Record<string, { status: string; activity?: string; lastActivityAt?: string }>>;
/** Another server's MCP App in the feed calls its server through these, as this agent: pass
* them on to `AgentChat` as `widgetCalls`. None: a page older than these verbs. */
widgetCalls?: {
callTool(call: {
path: string;
key: string;
server: string;
headers?: Record<string, string>;
name: string;
args: Record<string, unknown>;
}): Promise<unknown>;
readResource(read: {
path: string;
server: string;
headers?: Record<string, string>;
uri: string;
}): Promise<unknown>;
/** Our own widget's read of a context that its fact names. None: a page older than it. */
readContext?(read: {
path: string;
key: string;
context: string;
after: number;
limit: number;
}): Promise<unknown>;
};
/** `true`: `AgentChat` shows who else is here and who is typing. A kind's view that wants it
* also names `PRESENCE_EVENT_TYPES` (the kit's `lib/presence`) in its `ephemeralEvents`: the
* host's row of the log is then a page, and the view hears the others' status. */
presence?: boolean;
};
/** iterate's components a view builds with (agent-chat/chat-parts.tsx and agent-shell.tsx say
* what each takes; `AgentChat` takes a view's own props). */
export type AgentKindViewUi = Record<
| "AgentShell"
| "AgentChat"
| "Feed"
| "MessageBubble"
| "Composer"
| "Markdown"
| "Badge"
| "Button"
| "Spinner",
ComponentType<any>
>;
/** What `view` builds with. `html` is htm over React's createElement: the worker strips a kind
* file's types and nothing else, so there is no JSX. No `useState`, no `useEffect`: what a view
* holds is derived (`useMemo`) or is the server's (`useQuery`, `useMutation`). */
export type AgentKindViewKit = {
html: (strings: TemplateStringsArray, ...values: unknown[]) => any;
useMemo: <T>(factory: () => T, deps: readonly unknown[]) => T;
useQuery: typeof useQuery;
useMutation: typeof useMutation;
ui: AgentKindViewUi;
};
/** How one of a kind's agents is started: its `choices`, the `fields` each choice takes, and
* `start`, the events appended to the new agent's path before its first message. The Dash's New
* agent starts iterate agents, or the kind that says `default: true`. */
export type AgentKindNewAgent = {
/** `true`: the Dash's New agent starts this kind's agent, on its first choice, with each
* field at its `defaultValue`: it appends `start(…)` to the new path, then the first message as
* `agent-ui/message-received`. One kind of a mind says it; with two, the first by kind name. */
default?: true;
/** `byPerson: true`: this choice's new agents may be born in the folder of the person who
* starts them, `/agents/people/<person>/<kind>/<moment>` (lib/agent-summary.ts
* `newAgentPath`). A kind says it when its own file takes that path. Without it they are born at
* `/agents/<kind>/<moment>`. */
choices: { id: string; label: string; byPerson?: boolean }[];
fields?: AgentKindNewAgentField[];
/** `path` is the new agent's; `fields` are the picked choice's alone, each by its name. */
start(input: { choice: string; fields: Record<string, string>; path: string }): unknown[];
/** A line about what the choice and its fields' values as typed will do (a
* runner that has to be started first, say), or null for none. `mind` is the mind's address.
* Text, where `backticks` mark code a person may copy. */
describe?(input: { choice: string; fields: Record<string, string>; mind: string }): string | null;
};
/** A field of a choice: a line of text, or with `type: "checkbox"` a tick box, whose value
* is "on" when ticked and "" when not (`defaultValue: "on"` starts it ticked). `choices`: the ids
* of the choices that show the field; none: every choice of the kind.
*
* `type: "select"` with `options`: a list to pick one of (the models a choice can run, say). Its
* value is always one of the options: `defaultValue` at first, or the first option when that is
* not among them. Two fields of one name, each for other `choices`, give each choice its own
* list. A page older than this type draws a line of text with `defaultValue` in it, where a
* person can type anything: `start` must refuse a value that is not an option.
*
* `viewerDefault`: a first value made for the person who looks, where `{name}` is their first
* name, from the address they sign in with (`"{name}.laptop"` opens as `misha.laptop`); for
* someone with no such name the field opens with `defaultValue`.
*
* `offWhen`: a tick box that another field turns off. While the field named `field` has the
* value `is`, the box's value is "" and it is drawn unticked and disabled. A page older than
* this key still draws the box as it was, so `start` decides by the other field's value too. */
export type AgentKindNewAgentField = {
name: string;
label: string;
defaultValue: string;
type?: "text" | "checkbox" | "select";
options?: string[];
choices?: string[];
viewerDefault?: string;
offWhen?: { field: string; is: string };
};
/** A kind of agent whose view is a React component: iterate/agents/kinds' `AgentKind` (`kind`,
* `processEvent`, `ephemeralEvents`), with the chat filled in, and how one is started. */
export type AgentKind = SdkAgentKind<
(kit: AgentKindViewKit) => ComponentType<AgentKindViewProps>
> & {
newAgent?: AgentKindNewAgent;
};