@sema-agent/core
Version:
Stateless, task-oriented AI agent core
69 lines (68 loc) • 2.78 kB
JavaScript
export const A2A_TASK_STATES = Object.freeze([
"submitted",
"working",
"input-required",
"completed",
"canceled",
"failed",
"rejected",
"auth-required",
"unknown",
]);
const CANCELLING_STOP_SOURCES = new Set(["user", "parent"]);
export function toA2ATaskState(record) {
switch (record.status) {
case "running":
return "working";
case "parked":
return "input-required";
case "completed":
return "completed";
case "failed":
return "failed";
case "killed":
return record.stoppedBy !== undefined && CANCELLING_STOP_SOURCES.has(record.stoppedBy) ? "canceled" : "failed";
}
}
export function fromA2ATaskState(state) {
switch (state) {
case "working":
return { lossy: false, status: "running" };
case "input-required":
return { lossy: false, status: "parked" };
case "completed":
return { lossy: false, status: "completed" };
case "failed":
return { lossy: false, status: "failed" };
case "submitted":
return {
lossy: true,
status: "running",
note: "the durable ledger has no accepted-but-not-yet-started state — a row exists only once its child is running, so the distinction between submitted and working is dropped.",
};
case "canceled":
return {
lossy: true,
status: "killed",
note: "the cancellation attribution lives in the row's stoppedBy field, which a state alone cannot carry: a killed row WITHOUT stoppedBy in (user, parent) projects back as failed, not canceled.",
};
case "rejected":
return {
lossy: true,
status: "failed",
note: "rejection happens before a row exists (admission-time), so it is recorded as a failed terminal — the fact that the task was never admitted at all is lost.",
};
case "auth-required":
return {
lossy: true,
status: "failed",
note: "auth-required is a resumable waiting state with no durable seat here (parked is bound to an approval-checkpoint token, which this state has none of), so it lands as a failed terminal and is NOT resumable through this mapping.",
};
case "unknown":
return {
lossy: true,
status: "failed",
note: "unknown is a read-projection state (an outcome this engine instance cannot see), not a durable outcome; writing it back asserts a terminal failure the row itself never recorded.",
};
}
}