|
| 1 | +// Public task summaries keep task-registry internals and unbounded status text |
| 2 | +// out of gateway responses and events. |
| 3 | +import type { TaskSummary } from "../../../packages/gateway-protocol/src/index.js"; |
| 4 | +import type { TaskRecord, TaskStatus } from "../../tasks/task-registry.types.js"; |
| 5 | +import { |
| 6 | + TASK_STATUS_DETAIL_MAX_CHARS, |
| 7 | + formatTaskStatusTitle, |
| 8 | + sanitizeTaskStatusText, |
| 9 | +} from "../../tasks/task-status.js"; |
| 10 | + |
| 11 | +type TaskLedgerStatus = TaskSummary["status"]; |
| 12 | + |
| 13 | +export const TASK_STATUS_TO_LEDGER_STATUS: Record<TaskStatus, TaskLedgerStatus> = { |
| 14 | + queued: "queued", |
| 15 | + running: "running", |
| 16 | + succeeded: "completed", |
| 17 | + failed: "failed", |
| 18 | + timed_out: "timed_out", |
| 19 | + cancelled: "cancelled", |
| 20 | + lost: "failed", |
| 21 | +}; |
| 22 | + |
| 23 | +export type TaskEventPayload = |
| 24 | + | { action: "upserted"; task: TaskSummary } |
| 25 | + | { action: "deleted"; taskId: string } |
| 26 | + | { action: "restored" }; |
| 27 | + |
| 28 | +export function taskUpdatedAt(task: TaskRecord): number { |
| 29 | + return task.lastEventAt ?? task.endedAt ?? task.startedAt ?? task.createdAt; |
| 30 | +} |
| 31 | + |
| 32 | +function sanitizeOptionalTaskText( |
| 33 | + value: unknown, |
| 34 | + opts?: { errorContext?: boolean }, |
| 35 | +): string | undefined { |
| 36 | + const sanitized = sanitizeTaskStatusText(value, { |
| 37 | + errorContext: opts?.errorContext, |
| 38 | + maxChars: TASK_STATUS_DETAIL_MAX_CHARS, |
| 39 | + }); |
| 40 | + return sanitized || undefined; |
| 41 | +} |
| 42 | + |
| 43 | +export function mapTaskSummary(task: TaskRecord): TaskSummary { |
| 44 | + const progressSummary = sanitizeOptionalTaskText(task.progressSummary); |
| 45 | + const terminalSummary = sanitizeOptionalTaskText(task.terminalSummary, { errorContext: true }); |
| 46 | + const error = sanitizeOptionalTaskText(task.error, { errorContext: true }); |
| 47 | + return { |
| 48 | + id: task.taskId, |
| 49 | + taskId: task.taskId, |
| 50 | + kind: task.taskKind ?? task.runtime, |
| 51 | + runtime: task.runtime, |
| 52 | + status: TASK_STATUS_TO_LEDGER_STATUS[task.status], |
| 53 | + title: formatTaskStatusTitle(task), |
| 54 | + ...(task.agentId ? { agentId: task.agentId } : {}), |
| 55 | + sessionKey: task.requesterSessionKey, |
| 56 | + ...(task.childSessionKey ? { childSessionKey: task.childSessionKey } : {}), |
| 57 | + ownerKey: task.ownerKey, |
| 58 | + ...(task.runId ? { runId: task.runId } : {}), |
| 59 | + ...(task.parentFlowId ? { flowId: task.parentFlowId } : {}), |
| 60 | + ...(task.parentTaskId ? { parentTaskId: task.parentTaskId } : {}), |
| 61 | + ...(task.sourceId ? { sourceId: task.sourceId } : {}), |
| 62 | + createdAt: task.createdAt, |
| 63 | + updatedAt: taskUpdatedAt(task), |
| 64 | + ...(task.startedAt !== undefined ? { startedAt: task.startedAt } : {}), |
| 65 | + ...(task.endedAt !== undefined ? { endedAt: task.endedAt } : {}), |
| 66 | + ...(progressSummary ? { progressSummary } : {}), |
| 67 | + ...(terminalSummary ? { terminalSummary } : {}), |
| 68 | + ...(error ? { error } : {}), |
| 69 | + }; |
| 70 | +} |
0 commit comments