UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

278 lines (277 loc) • 19.5 kB
/** * The workflow CATALOG — the saved workflows a deployment offers by name, apart from the per-run scripts the script store keeps * ({@link import("./workflow-script-store.js").WorkflowScriptStore}: what each run persisted, read back by `scriptPath`). The two * never share an interface: a run's script is never a saved name, and a saved name is never a run's. * * A catalog is built from LAYERS, one per source, in one fixed order (highest first): `deployment` > `project` > `user` > * `builtin` ({@link WORKFLOW_SOURCES}). A layer is one or more directories read in order (a project layer: the nearest level's * directories first). Every file a layer reads is a candidate for one name — a workflow's name is its `meta.name`, a preset's is * its file name — and for each name the first candidate down that order is the entry; every later one is shadowed (a * `shadowed` notice names who shadows it). A candidate that cannot be run (its meta does not parse, two files in one directory * claim the name) still claims its name: in the `deployment` layer it HOLDS the name — nothing lower is listed or run under * it, and `resolve` refuses it ({@link WorkflowCatalogHeldError}); in any lower layer the next candidate is used instead * (`shadow_unparseable`). What the catalog could not use is told as a notice, never dropped. * * A PRESET (`<name>.preset.json`) is a saved set of arguments for a workflow, run by its own name: the workflow it names is * resolved through this same catalog (the entry in force now), and its `args` merge under the call's. A preset whose workflow * is not a listed workflow is not listed and keeps its name (`preset_target_missing`). * * A TEAM (`<name>.team.json`) is a saved manager team, named by its file name: it claims its name like any other entry and * is listed, but it is not run by the Workflow tool — each kind of entry has exactly one door * ({@link WORKFLOW_CATALOG_ENTRY_DOORS}); `resolve` and `readByHash` answer only what the Workflow tool runs, and `resolveTeam` * only a team (what a run naming it, `TaskSpec.team`, is made from). * * `list`, `resolve` and {@link WorkflowCatalog.readByHash} read the layers afresh on every call, so a workflow saved through * {@link WorkflowCatalog.saveRunAs} is listed and resolvable at once. {@link WorkflowCatalog.snapshot} is ONE read kept — what a * Workflow tool mount lists on its card and runs every name of that read by, for as long as the mount lasts; a name that read * does not know is read at its call. * * A layer that is not read whole — its directory cannot be listed, any file of it cannot be read or is too large, its path runs * through a link that points nowhere, or a host's layer answers a read that is not a listing — cannot be read: it is told * (`unreadable`, no `name`), the layers above it are read as usual, and every name a layer below it claims is held, since * whether the unread layer shadows that name is not known. */ import { type WorkflowJournalStore } from "../core/workflow-journal-store.js"; import type { WorkflowMeta, WorkflowRequires } from "./workflow-script-runner.js"; /** Where a saved workflow comes from. CLOSED; the array order is the shadowing order, highest first. */ export declare const WORKFLOW_SOURCES: readonly ["deployment", "project", "user", "builtin"]; export type WorkflowSource = (typeof WORKFLOW_SOURCES)[number]; /** The two layers a person can save into. */ export declare const WORKFLOW_SAVE_LAYERS: readonly ["project", "user"]; export type WorkflowSaveLayer = (typeof WORKFLOW_SAVE_LAYERS)[number]; /** What one catalog entry is. CLOSED. */ export type WorkflowCatalogEntryKind = "workflow" | "preset" | "team"; /** The one door each kind of entry is run through: the Workflow tool (`workflow_tool`), or a run of its own a host starts by * the entry's name (`run`). Every reader of "who runs this entry" reads this table. */ export declare const WORKFLOW_CATALOG_ENTRY_DOORS: Readonly<{ readonly workflow: "workflow_tool"; readonly preset: "workflow_tool"; readonly team: "run"; }>; /** The largest file a directory layer holds, in bytes as the file is written (UTF-8): its read does not read a larger one (its * layer is then one that cannot be read), and its save does not write one. A host's own layer sets its own bounds. */ export declare const WORKFLOW_CATALOG_MAX_FILE_BYTES = 1000000; /** A workflow name: what `meta.name` must be for a workflow to be listed or saved, and what a preset's file name must be — * letters, digits, `.`, `_`, `-`, at most 120, not starting with `.` (the file name a saved workflow is written under). */ export declare function isWorkflowName(name: unknown): name is string; export interface WorkflowCatalogEntry { readonly name: string; readonly kind: WorkflowCatalogEntryKind; readonly description: string; readonly whenToUse?: string; /** A workflow's `meta.phases`; a preset's are its workflow's. */ readonly phases?: WorkflowMeta["phases"]; readonly source: WorkflowSource; /** The file the entry was read from (a layer that is not a directory names its own coordinate). */ readonly location: string; /** sha-256 of everything a run of this entry runs: the script's bytes, and for a preset its `args` and `stringArgKey` too * (`sha256:<hex>`). The key {@link WorkflowCatalog.readByHash} answers by. */ readonly contentHash: string; /** What it declares it needs (a preset's: its own merged with its workflow's). */ readonly requires?: WorkflowRequires; } /** What runs: the script and, for a preset, the arguments that merge under the call's. */ export interface WorkflowCatalogContent { readonly contentHash: string; readonly script: string; readonly defaultArgs?: unknown; readonly stringArgKey?: string; } export interface WorkflowCatalogResolution extends WorkflowCatalogContent { readonly entry: WorkflowCatalogEntry; } /** CLOSED. `unreadable` is always a whole layer (no `name`): a layer is read whole or not at all. */ export type WorkflowCatalogNoticeCode = "invalid_meta" | "unreadable" | "near_miss_extension" | "duplicate_in_layer" | "shadowed" | "shadow_unparseable" | "name_differs_from_file" | "args_sidecar_retired" | "preset_target_missing" | "official_dir_missing"; export interface WorkflowCatalogNotice { readonly code: WorkflowCatalogNoticeCode; readonly source: WorkflowSource; readonly location: string; readonly name?: string; readonly detail: string; } export interface WorkflowCatalogListing { readonly entries: readonly WorkflowCatalogEntry[]; readonly notices: readonly WorkflowCatalogNotice[]; } /** One file a layer read, with its text. `stem` = the file name without `.js` / `.preset.json` / `.team.json`. A file the * layer could not read has no row: the layer's read fails instead. */ export type WorkflowCatalogLayerFile = { readonly location: string; readonly stem: string; readonly kind: WorkflowCatalogEntryKind; readonly text: string; }; export interface WorkflowCatalogLayerRead { /** One list per directory, in the order the layer searches them (the first shadows the rest). Saving writes into the first. */ readonly dirs: ReadonlyArray<ReadonlyArray<WorkflowCatalogLayerFile>>; readonly notices: ReadonlyArray<Omit<WorkflowCatalogNotice, "source">>; } /** * One source's saved workflows. `read` answers every file the layer holds, each with its text; a read that cannot be done * whole throws (a file it cannot read included), and so does an answer that is not a listing of this shape: the catalog tells * it and holds every name below this layer (a layer half-read could let a lower layer's file stand in for one it hides). * `save` writes `<name>.js` into the first directory: absent ⇒ nothing can be saved here. It refuses a link in the way * (`workflow.save_symlink`), and an existing file unless `overwrite` or anything there that is not a plain file * (`workflow.save_exists`). `dirs`: the directories it reads, when it reads directories — two layers over one directory are * refused when the catalog is built. */ export interface WorkflowCatalogLayer { readonly source: WorkflowSource; readonly dirs?: readonly string[]; read(): WorkflowCatalogLayerRead | Promise<WorkflowCatalogLayerRead>; save?(req: { readonly name: string; readonly script: string; readonly overwrite: boolean; }): { location: string; } | Promise<{ location: string; }>; } export interface WorkflowCatalogSaveRequest { /** Must be the recorded script's `meta.name` (a workflow is found by it). */ readonly name: string; readonly layer: WorkflowSaveLayer; /** Replace that layer's `<name>.js` when it exists. */ readonly overwrite?: boolean; } export interface WorkflowCatalogSaved { readonly name: string; readonly layer: WorkflowSaveLayer; readonly location: string; /** The saved bytes' hash — the same bytes the run was launched with. */ readonly contentHash: string; /** Present when the name, called now, does NOT run what was just saved: a higher layer's entry has it, a higher layer's preset * whose workflow is not listed claims it (the name then runs nothing), or the deployment layer holds it — the file is written, * and this says who answers the name instead. */ readonly shadowedBy?: { readonly source: WorkflowSource; readonly location: string; }; /** Present when `overwrite` replaced a file that was there: the name that file was listed under (its `meta.name`; its file * name when its meta did not read) — another workflow's name when the file held another workflow. */ readonly replaced?: string; /** When a Workflow tool mount that is running now runs what was just saved — present exactly when `shadowedBy` is not (with * it, the name runs something else, now and later). A mount answers every name its one read knew from that read, and reads * any other name at its call: `"next_mount"` — the catalog answered this name right before the save, so a mount whose read * answered it keeps running what it read until its next mount; `"at_once"` — it did not, so a running mount reads the name * at its call and runs the saved bytes. Read from the catalog as it stood right before the save: a mount whose read still * holds a name that was removed or broken on disk by hand since keeps that read, whatever this says. */ readonly takesEffect?: "at_once" | "next_mount"; } /** One read of the catalog, kept for as long as its holder lasts: `list` and `resolve` answer from it, whatever is edited or * saved since. A name that read neither lists nor holds is resolved by a read made at that call. */ export interface WorkflowCatalogSnapshot { list(): Promise<WorkflowCatalogListing>; resolve(name: string): Promise<WorkflowCatalogResolution | undefined>; } export interface WorkflowCatalog { list(): Promise<WorkflowCatalogListing>; /** The entry in force for `name` with what it runs, when the Workflow tool runs it (a workflow or a preset); `undefined` for a * name nothing lists or a team. A held name is refused ({@link WorkflowCatalogHeldError}): the deployment layer's without a * runnable entry, or one below a layer that cannot be read. */ resolve(name: string): Promise<WorkflowCatalogResolution | undefined>; /** Read every layer once and keep it ({@link WorkflowCatalogSnapshot}) — a Workflow tool mount's card and its `{name}` calls. */ snapshot(): Promise<WorkflowCatalogSnapshot>; /** The team in force for `name` as its file reads (its entry, the workers it names, its prompt), when the entry under `name` is a * team; `undefined` for a name nothing lists or that is not a team. A held name is refused ({@link WorkflowCatalogHeldError}), as * {@link resolve} refuses it. What a run that names a team (`TaskSpec.team`) is made from; a catalog without it runs no team. */ resolveTeam?(name: string): Promise<{ readonly entry: WorkflowCatalogEntry; readonly workers: ReadonlyArray<{ readonly agent: string; }>; readonly appendSystemPrompt?: string; } | undefined>; /** The content of the listed entry whose `contentHash` this is, or `undefined` when no listed entry has it now. */ readByHash(contentHash: string): Promise<WorkflowCatalogContent | undefined>; /** * Save what run `runId` was launched with (its launch record, read under `scope`) as a workflow in `layer`. Present only on a * catalog given a journal that keeps launch records. Refusals ({@link WorkflowCatalogSaveError}, code in * {@link WORKFLOW_SAVE_REFUSAL_CODES}): no such writable layer, no launch record for the run, or a layer that cannot be read * whole now, or — by a directory layer's own save — a script over {@link WORKFLOW_CATALOG_MAX_FILE_BYTES} (UTF-8), which that * layer's read would not read → `workflow.save_unavailable` (`reason`: `layer_not_writable` / `no_launch_record` / * `layer_unreadable` / `script_too_large`); * `name` is not the script's `meta.name` (or that is not a workflow name) → `workflow.save_name_mismatch`; the name is * taken in that directory → `workflow.save_exists`; a link where the file or its directory would be, in a layer that reads * whole → `workflow.save_symlink` (a link that makes the layer's read fail — to nothing, or to a file it cannot read whole — * is `layer_unreadable`: the layer's read is judged first). A store fault reading the record throws as the store threw it. */ saveRunAs?(runId: string, scope: string, req: WorkflowCatalogSaveRequest): Promise<WorkflowCatalogSaved>; } /** CLOSED: every refusal {@link WorkflowCatalog.saveRunAs} answers (the error's `code`). */ export declare const WORKFLOW_SAVE_REFUSAL_CODES: readonly ["workflow.save_exists", "workflow.save_name_mismatch", "workflow.save_symlink", "workflow.save_unavailable"]; export type WorkflowSaveRefusalCode = (typeof WORKFLOW_SAVE_REFUSAL_CODES)[number]; /** CLOSED: why a save is unavailable — `layer_not_writable`: this catalog has no writable layer of that word; `no_launch_record`: * none for that run under that scope; `layer_unreadable`: the layer's own read fails now (a file it cannot read, an oversized * one, a link to nothing, an unlistable directory), so what a save would collide with is not known — `details.location` is the * location the layer's `unreadable` notice names, and nothing was written; `script_too_large`: a directory layer's save was asked * to write a script over {@link WORKFLOW_CATALOG_MAX_FILE_BYTES} in UTF-8 bytes, a file its own read would not read (the layer * would then be one that cannot be read) — the message names the limit and the script's size, and nothing was written. A host's * own layer is not judged by it: its save decides what it holds. */ export type WorkflowSaveUnavailableReason = "layer_not_writable" | "no_launch_record" | "layer_unreadable" | "script_too_large"; export declare class WorkflowCatalogSaveError extends Error { readonly code: WorkflowSaveRefusalCode; readonly details: { readonly code: WorkflowSaveRefusalCode; readonly reason?: WorkflowSaveUnavailableReason; readonly location?: string; }; constructor(code: WorkflowSaveRefusalCode, message: string, reason?: WorkflowSaveUnavailableReason, location?: string); } export declare const WORKFLOW_CATALOG_HELD_CODE = "workflow.catalog_held"; /** A held name: the deployment layer's with nothing runnable under it (`by: "deployment"`), or one claimed below a layer that * cannot be read (`by: "unreadable_layer"`; `location` = that layer) — refused, never answered from a lower layer. */ export declare class WorkflowCatalogHeldError extends Error { readonly code = "workflow.catalog_held"; readonly details: { readonly code: typeof WORKFLOW_CATALOG_HELD_CODE; readonly name: string; readonly location: string; }; constructor(name: string, location: string, why: string, by?: "deployment" | "unreadable_layer"); } /** * The one migration of a retired `<file>.args.json` sidecar, as each place that teaches it words it (a catalog notice for one * file, naming `preset`; the integration guide with placeholders): a preset under a name of its own, pointing at the * workflow's `meta.name` — a preset under the workflow's own name would be a second claimant of that name in its directory, * and the name would be held. */ export declare function workflowArgsSidecarMigration(file: string, preset: string, workflow: string): string; /** * A layer of directories (`dirs`, searched in order; the first is where a save writes). Reads each directory's own `*.js` * (workflows), `*.preset.json` (presets) and `*.team.json` (teams) — never a subdirectory, a dot-file, or another extension; a linked file is read as * the file it links to. A directory that does not exist holds nothing; one that exists and cannot be listed, one whose path * runs through a link that points nowhere, and one holding a file it cannot read (or one over * {@link WORKFLOW_CATALOG_MAX_FILE_BYTES}) fail the read — a file gone since the listing is simply not there. * `<name>.args.json` is not read (`args_sidecar_retired`), a near-miss extension is told (`near_miss_extension`), and with * `expectEntries` (a host's official directory) a layer that holds no file at all is told (`official_dir_missing`). * Only a `project` or `user` layer saves. */ export declare function createDirectoryWorkflowLayer(opts: { readonly source: WorkflowSource; readonly dirs: readonly string[]; readonly expectEntries?: boolean; }): WorkflowCatalogLayer; /** * Build a catalog over `layers` (one per source; their order is {@link WORKFLOW_SOURCES}', whatever order they are passed in). * `journalStore`: the journal whose launch records {@link WorkflowCatalog.saveRunAs} saves from — only a journal that keeps * launch records mounts `saveRunAs`. Two layers of one source, a layer whose source is not one, and two layers whose `dirs` * name one directory are refused (`config.invalid_argument`). */ export declare function createWorkflowCatalog(opts: { readonly layers: readonly WorkflowCatalogLayer[]; readonly journalStore?: WorkflowJournalStore; }): WorkflowCatalog; /** Whether `v` answers every read a catalog answers — what both doors that take a host's catalog (the factory, the Workflow * tool's `catalog` seat) check before they mount it. */ export declare function isWorkflowCatalog(v: unknown): v is WorkflowCatalog; /** * A task's catalog from a deployment's factory (`RunnerDeps.workflowCatalog`), called once with the task's coordinates. A factory * that is not a function, throws, or answers no catalog yields a catalog that answers every read with that fault — the Workflow * tool still mounts, its card lists nothing, a `{name}` call is refused naming the fault (never read as "nothing is saved") — and * `onFault` hears it at once, so the operator learns it at the mount rather than from a model's call. A layer that cannot be * read is heard the same way when the mount takes its snapshot. */ export declare function workflowCatalogOfFactory<C>(factory: unknown, ctx: C, onFault: (e: Error) => void): WorkflowCatalog;