iterate.com / config-template

git clone https://git.iterate.com/repos/config-template

config-template / api.tsmain at 3b8ad1b

// api.ts — THE MIND'S OWN API: `itx.config.<name>(…)`, for its agents and its pages.
//
// `itx.config.<name>(…)` reaches every public method of the class that worker.ts exports: flat
// names alone, and never an arrow field, which is no method of the class. worker.ts's class
// extends this one. Add a method here, or to worker.ts's class.
import { IterateConfigEntrypoint } from "iterate/sdk";
import { agentCatalogOf, type CatalogItx, isAgentOf } from "@repos/dash/agent-catalog.ts";
import { mergePull } from "@repos/git/pulls.ts";
import { runStep, stepContext } from "@repos/onboarding/steps.ts";
// @ts-expect-error -- a plain module of the Tasks app, with no types
import { taskAskers } from "@repos/tasks/askers.js";
// @ts-expect-error -- a plain module of the Tasks app, with no types
import { answerTask } from "@repos/tasks/asks.js";
// @ts-expect-error -- a plain module of the Tasks app, with no types
import { taskBoard } from "@repos/tasks/board.js";
// @ts-expect-error -- a plain module of the Tasks app, with no types
import { taskAssign, taskOpen, taskUpdate } from "@repos/tasks/cards.js";
// @ts-expect-error -- a plain module of the Tasks app, with no types
import { followTasks, taskFollowers, unfollowTasks } from "@repos/tasks/feed.js";
import { profileOf } from "./profile.ts";
import profileFiles from "./profile/";

/** THE REPO OF THE MIND'S TASKS: the board shows the markdown files under its `todo/`
 *  (worker.ts, `tasksApp({ repo })`), and the task calls below read and write it when a call
 *  names no repo. It holds nothing else, and a commit to it publishes nothing. */
export const TASKS_REPO = "/repos/task-board";

export abstract class ConfigApi extends IterateConfigEntrypoint {
  /** `itx.config.profile()`: what the mind says of itself to the world, `{ name, description,
   *  icon }`, as its website answers it at `/.well-known/iterate-profile` (profile.ts). `problem`
   *  is there when a file of the folder profile/ is broken, and says which. To change the
   *  profile, commit `profile/profile.json` (and `profile/icon.svg`) to this repo. */
  async profile() {
    using itx = this.getItx();
    return profileOf(profileFiles, new URL(await itx.url()).hostname);
  }

  /** `itx.config.pullMerge("/repos/git", 7)`: squash that pull of that repo into main, as the
   *  git app's page does when a member presses "Squash and merge". One commit lands on main,
   *  named `<title> (#7)` with the pull's body under it, and answers `{ commitOid }`. It throws,
   *  in words for the one who asked, when the pull is not open or main has moved past what the
   *  branch has (pulls.ts of /repos/git says what a pull is, and how to open one or comment on one:
   *  each is one event). */
  async pullMerge(repo: string, number: number) {
    using itx = this.getItx();
    return await mergePull(itx, repo, number);
  }

  /** `itx.config.onboardingStep(step, action)`: one press of a button of the onboarding app, as
   *  the onboarding agent makes it from the owner's plain words (steps.ts and
   *  onboarding/connect-iterate-cloud.md of /repos/onboarding). It answers
   *  `{ status, words, link? }`. */
  async onboardingStep(step: string, action: string) {
    using itx = this.getItx();
    return await runStep(await stepContext(itx), step, action);
  }

  // ── THE TASK CALLS: how an agent keeps a card of the board, with no YAML by hand. README.md of
  // /repos/tasks is their guide; the code is that repo's cards.js, feed.js, asks.js, askers.js
  // and board.js. Each takes `repo: "<name>"` for another repo of the mind than the tasks repo.

  /** `itx.config.taskOpen({ title, text?, status?, owner?, agent?, tags?, ask?, decision?,
   *  noChoice?, repo?, by? })`: a new card on the board: `text` is the ask in the person's own
   *  words and where it came from, `owner` the person who asked. It answers
   *  `{ repo, file, commitOid, link }`. */
  async taskOpen(input: Record<string, unknown>) {
    using itx = this.getItx();
    return await taskOpen(itx, { ...input, repo: input?.repo || TASKS_REPO }, this.taskDeps(itx));
  }

  /** `itx.config.taskUpdate({ file, status?, title?, owner?, agent?, tags?, blockedOn?, ask?,
   *  decision?, tell?, noChoice?, withdraw?, reword?, note?, repo?, by? })`: one commit that keeps
   *  a card true. `blockedOn: ""` takes the blocker away; `ask` is one more question for a person,
   *  with its `decision`, its `tell: { agent, words? }` (the agent that acts on the answer) or
   *  `noChoice`; `withdraw: { ask, why }` takes an ask off, and `reword: { ask, to }` writes one
   *  again; `note` is one line under `## Log`. `by`: the agent that makes the change, when the
   *  call comes from no agent's context (the MCP `run` tool). It answers
   *  `{ repo, file, commitOid, changed, card }`. */
  async taskUpdate(input: Record<string, unknown>) {
    using itx = this.getItx();
    return await taskUpdate(itx, { ...input, repo: input?.repo || TASKS_REPO }, this.taskDeps(itx));
  }

  /** `itx.config.taskAssign({ file, agent, message?, repo?, by? })`: gives a card to an agent that
   *  runs. The card names the agent and is in progress, and the agent gets one message that makes
   *  the card its brief. It answers `{ repo, file, agent, model, effort, starting, running, card }`. */
  async taskAssign(input: Record<string, unknown>) {
    using itx = this.getItx();
    return await taskAssign(itx, { ...input, repo: input?.repo || TASKS_REPO }, this.taskDeps(itx));
  }

  /** `itx.config.answerTask({ file, ask, answer, by, repo? })`: an answer that a person gave to
   *  an ask of a task (one line of its `human-ask:`), written into the task file as the board
   *  writes it: the ask's line goes, and the ask with the answer, `by` and the time is kept under
   *  `## Answers`. For an answer that reached an agent in a chat or on a call; a person at the
   *  board answers there ("Needs a person"). `ask`: the ask's whole text. It answers
   *  `{ repo, file, commitOid, stillAsked }`. */
  async answerTask(input: { file: string; ask: string; answer: string; by: string; repo?: string }) {
    using itx = this.getItx();
    return await answerTask(itx, { ...input, repo: input?.repo || TASKS_REPO });
  }

  /** `itx.config.followTasks({ file?, agent?, wake?, only?, repo? })`: from now on an agent is
   *  told of each change of one task file (`file`, like "todo/a-task.md"), or with no `file` of
   *  every task, as a message with what changed and a small diff. `agent`: the calling agent's
   *  own context when not given; a member's script and the MCP `run` tool name it. `wake: false`:
   *  the words are context only. `only`: the changes that moved one of the fields it names
   *  (`["status", "blocked-on", "human-ask"]`), and no other. It answers
   *  `{ agent, follows, only, repo, log, row }`. */
  async followTasks(input?: {
    file?: string;
    agent?: string;
    wake?: boolean;
    only?: string[];
    repo?: string;
  }) {
    using itx = this.getItx();
    return await followTasks(itx, { ...input, repo: input?.repo || TASKS_REPO }, this.taskDeps(itx));
  }

  /** `itx.config.unfollowTasks({ file?, agent?, repo? })`: ends what `followTasks` with the same
   *  `file` (or with none) and the same repo began. It answers `{ agent, follows, repo, ended }`. */
  async unfollowTasks(input?: { file?: string; agent?: string; repo?: string }) {
    using itx = this.getItx();
    return await unfollowTasks(itx, { ...input, repo: input?.repo || TASKS_REPO }, this.taskDeps(itx));
  }

  /** `itx.config.taskFollowers({ repo? })`: which agent follows which task file, or "every task". */
  async taskFollowers(input?: { repo?: string }) {
    using itx = this.getItx();
    return await taskFollowers(itx, { ...input, repo: input?.repo || TASKS_REPO });
  }

  /** `itx.config.taskAskers({ repo? })`: who asked each ask that a task still has (a line of its
   *  `human-ask:`), read from the change feed: the agent of the commit that added the ask, by its
   *  `Via:` line or the platform's stamp. The board's "Needs a person" links each ask to that
   *  agent's chat. It answers `{ repo, asks, readFrom }`, each ask as
   *  `{ file, ask, agent, by, page, at, message, model, blocking, assignee }`. `agent` is null
   *  when nothing says who asked; `assignee` is the task's own agent, which is not the asker. */
  async taskAskers(input?: { repo?: string }) {
    using itx = this.getItx();
    return await taskAskers(itx, { ...input, repo: input?.repo || TASKS_REPO });
  }

  /** `itx.config.taskBoard({ repo? })`: the whole board as data, in one call, for an agent that
   *  manages work or starts with no history: every task as its card with the answers people gave
   *  to its asks, its last change and whether the agent it names has work now, the tasks that
   *  need a person, the board's newest changes and who follows what. It answers
   *  `{ repo, commitOid, board, tasks, needsAPerson, recentChanges, followers }`. */
  async taskBoard(input?: { repo?: string }) {
    using itx = this.getItx();
    return await taskBoard(itx, { ...input, repo: input?.repo || TASKS_REPO });
  }

  /** What the task calls ask of the mind (cards.js and feed.js of /repos/tasks): where a call came
   *  from, and the one gate that says a path is an agent (agent-catalog.ts of /repos/dash), since a
   *  follower's row writes to the path it names. A mind adds its own parts by overriding this in
   *  worker.ts's class: cards.js lists them (`linkNote`, `briefRules`, `startWorker`,
   *  `sessionState`). */
  protected taskDeps(itx: object) {
    // Two casts, both of shapes that this file cannot name more closely: `itx` is the root's
    // handle, which has `cd(path).facets` as `CatalogItx` asks; and the catalog's live state is
    // `unknown` to its reader, with `agents` a map by path (iterate/agents writes it).
    const catalog = async () =>
      (await agentCatalogOf(itx as CatalogItx)) as { agents?: Record<string, unknown> } | null;
    return {
      callerPath: () => this.callerPath(),
      isAgent: async (path: string) => isAgentOf(await catalog(), path),
      /** the agent's row in the catalog (`kind`, `status`, `lastActivityAt`), or null */
      agentRow: async (path: string) => {
        const agents = (await catalog())?.agents;
        return agents && Object.hasOwn(agents, path) ? agents[path] : null;
      },
    };
  }
}