@sema-agent/core
Version:
Stateless, task-oriented AI agent core
81 lines (80 loc) • 5.24 kB
JavaScript
import { MANAGER_RECIPE } from "../prompts/manager-recipe.js";
import { readCallerSpec, rereadWith } from "../core/task-spec-members.js";
import { describeRejectedValue } from "../core/tool-search-setting.js";
import { thrownText } from "../core/thrown-value.js";
import { readRunLedgerParked } from "../core/run-ledger.js";
import { receiptWith } from "../core/runner/leg-receipt.js";
import { isWorkflowName } from "./workflow-catalog.js";
const CLAIMED = new WeakMap();
const RESOLVED = new WeakMap();
function teamClaimOf(receipt) {
return receipt === undefined ? undefined : CLAIMED.get(receipt);
}
export function legReadsTeam(receipt) {
return teamClaimOf(receipt) !== undefined;
}
export function teamAppendOf(receipt) {
return receipt === undefined ? undefined : RESOLVED.get(receipt);
}
const RECIPE_SEATS = Object.keys(MANAGER_RECIPE);
function refused(message) {
return Object.assign(new Error(message), { code: "config.invalid_argument" });
}
const noCatalog = (name) => refused(`team "${name}" cannot be run: this deployment has no workflow catalog (RunnerDeps.workflowCatalog), which is where saved teams are read from. Nothing was started.`);
export function legRunsTeam(spec, row) {
return spec.team !== undefined || (row !== undefined && readRunLedgerParked(row.state.runLedger)?.team !== undefined);
}
export function teamLeg(spec, receipt, deps, row) {
const recorded = row !== undefined ? readRunLedgerParked(row.state.runLedger)?.team : undefined;
const named = spec.team;
if ((named === undefined && recorded === undefined) || readCallerSpec(spec).refusal !== undefined || !("read" in receipt.roster))
return { spec, receivedPeers: receipt };
if (named !== undefined && !isWorkflowName(named))
throw refused(`TaskSpec.team must name a saved team (letters, digits, ".", "_", "-"; at most 120; not starting with ".") — got ${describeRejectedValue(named)}.`);
if (row !== undefined && recorded === undefined) {
throw refused(`this task did not start as a run of a saved team, so a leg that resumes it cannot name one (TaskSpec.team "${String(named)}"): whether a task runs a team is fixed when it starts. Resume it without TaskSpec.team; the row stays pending.`);
}
if (recorded !== undefined && named !== undefined && named !== recorded.name) {
throw refused(`this task started as a run of team "${recorded.name}", so a leg that resumes it runs that team — the resume names team "${String(named)}". Name "${recorded.name}" or no team; the row stays pending.`);
}
const name = recorded !== undefined ? recorded.name : String(named);
const stated = RECIPE_SEATS.filter((seat) => spec[seat] !== undefined);
if (stated.length > 0) {
throw refused(`a run of team "${name}" runs on the manager recipe's seats, so ${stated.map((s) => `TaskSpec.${s}`).join(", ")} cannot be stated beside TaskSpec.team — leave ${stated.length === 1 ? "it" : "them"} out (the team's own prompt is its file's appendSystemPrompt). Nothing was started.`);
}
if (deps.workflowCatalog === undefined)
throw noCatalog(name);
const { appendSystemPrompt: _duties, ...seats } = MANAGER_RECIPE;
const receivedPeers = receiptWith(receipt, seats);
CLAIMED.set(receivedPeers, Object.freeze({ name, ...(recorded !== undefined ? { recorded: { contentHash: recorded.contentHash } } : {}) }));
return { spec: rereadWith(spec, seats), receivedPeers };
}
export async function resolveLegTeam(receipt, catalog, book) {
const claim = teamClaimOf(receipt);
if (claim === undefined || receipt === undefined)
return;
const { name, recorded } = claim;
const read = catalog();
if (read === undefined)
throw noCatalog(name);
if (typeof read.resolveTeam !== "function")
throw refused(`team "${name}" cannot be run: this deployment's workflow catalog answers no team by name (it has no resolveTeam). Nothing was started.`);
let team;
try {
team = await read.resolveTeam(name);
}
catch (err) {
throw refused(`team "${name}" cannot be run: ${thrownText(err)}. Nothing was started.`);
}
if (team === undefined) {
const other = await read.resolve(name).catch(() => undefined);
throw refused(other !== undefined
? `"${name}" is a ${other.entry.kind === "preset" ? "preset" : "workflow"}, not a team: the Workflow tool runs it. Nothing was started.`
: `the workflow catalog lists no team "${name}". Nothing was started.`);
}
if (recorded !== undefined && team.entry.contentHash !== recorded.contentHash) {
throw refused(`team "${name}" changed since this task started: it started on ${recorded.contentHash}, and the file the catalog reads now (${team.entry.source}) is ${team.entry.contentHash} — the task resumes only on the team it started with. Put that file back to resume it; the row stays pending.`);
}
RESOLVED.set(receipt, [MANAGER_RECIPE.appendSystemPrompt, ...(team.appendSystemPrompt !== undefined && team.appendSystemPrompt !== "" ? [team.appendSystemPrompt] : [])].join("\n\n"));
book?.setTeam({ name, source: team.entry.source, contentHash: team.entry.contentHash });
}