Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

workflow.ts

168 lines6.6 KB

1import * as Schema from "effect/Schema";
2​
3import { TrimmedNonEmptyString } from "./baseSchemas.ts";
4​
5/**
6 * Contracts for Claude Agent SDK workflow-run visibility.
7 *
8 * A "workflow" is a background orchestration task the Claude Agent SDK runs
9 * in-process (the `Workflow` tool). The SDK streams a cumulative progress
10 * snapshot on every `task_progress` message via the (currently undocumented)
11 * `workflow_progress` field, and writes per-agent transcripts plus a result
12 * journal to a transcript directory on disk. These schemas model the subset
13 * the server forwards to clients.
14 *
15 * Every field that originates from the undocumented SDK surface is optional:
16 * the adapter normalizes entries defensively, and clients must tolerate
17 * absent fields so an SDK upgrade degrades to less detail, never to a
18 * decode failure.
19 */
20​
21/**
22 * One `agent()` call inside a workflow run. `index` is the SDK's stable
23 * per-run agent ordinal; snapshots are merged last-write-wins by `index`.
24 * `state` is an open string ("start" | "done" | "error" today) — clients
25 * must render unknown states as "running".
26 */
27export const WorkflowAgentProgressEntry = Schema.Struct({
28 type: Schema.Literal("workflow_agent"),
29 index: Schema.Number,
30 state: Schema.String,
31 label: Schema.optional(Schema.String),
32 phaseIndex: Schema.optional(Schema.Number),
33 phaseTitle: Schema.optional(Schema.String),
34 agentId: Schema.optional(Schema.String),
35 agentType: Schema.optional(Schema.String),
36 model: Schema.optional(Schema.String),
37 fallbackModel: Schema.optional(Schema.String),
38 isolation: Schema.optional(Schema.Literals(["worktree", "remote"])),
39 attempt: Schema.optional(Schema.Number),
40 queuedAt: Schema.optional(Schema.Number),
41 startedAt: Schema.optional(Schema.Number),
42 lastProgressAt: Schema.optional(Schema.Number),
43 cached: Schema.optional(Schema.Boolean),
44 remoteSessionId: Schema.optional(Schema.String),
45 lastToolName: Schema.optional(Schema.String),
46 lastToolSummary: Schema.optional(Schema.String),
47 promptPreview: Schema.optional(Schema.String),
48 resultPreview: Schema.optional(Schema.String),
49 error: Schema.optional(Schema.String),
50 /** Cumulative output tokens, tool calls, and wall-clock duration for this
51 * agent, as reported by the SDK snapshot. */
52 tokens: Schema.optional(Schema.Number),
53 toolCalls: Schema.optional(Schema.Number),
54 durationMs: Schema.optional(Schema.Number),
55});
56export type WorkflowAgentProgressEntry = typeof WorkflowAgentProgressEntry.Type;
57​
58export const WorkflowPhaseProgressEntry = Schema.Struct({
59 type: Schema.Literal("workflow_phase"),
60 index: Schema.Number,
61 title: Schema.String,
62 kind: Schema.optional(Schema.String),
63});
64export type WorkflowPhaseProgressEntry = typeof WorkflowPhaseProgressEntry.Type;
65​
66/** A `log()` narration line emitted by the workflow script. */
67export const WorkflowLogProgressEntry = Schema.Struct({
68 type: Schema.Literal("workflow_log"),
69 message: Schema.String,
70});
71export type WorkflowLogProgressEntry = typeof WorkflowLogProgressEntry.Type;
72​
73export const WorkflowProgressEntry = Schema.Union([
74 WorkflowAgentProgressEntry,
75 WorkflowPhaseProgressEntry,
76 WorkflowLogProgressEntry,
77]);
78export type WorkflowProgressEntry = typeof WorkflowProgressEntry.Type;
79​
80/**
81 * Handles returned by the Workflow tool result. `transcriptDir` and
82 * `scriptPath` are server-local paths — clients echo them back to the
83 * workflow inspection RPCs, which re-validate them structurally before
84 * touching disk. `sessionUrl` replaces the local handles for remote runs.
85 */
86export const WorkflowRunHandles = Schema.Struct({
87 taskId: TrimmedNonEmptyString,
88 runId: Schema.optional(TrimmedNonEmptyString),
89 workflowName: Schema.optional(TrimmedNonEmptyString),
90 taskType: Schema.optional(TrimmedNonEmptyString),
91 scriptPath: Schema.optional(TrimmedNonEmptyString),
92 transcriptDir: Schema.optional(TrimmedNonEmptyString),
93 sessionUrl: Schema.optional(TrimmedNonEmptyString),
94 warning: Schema.optional(TrimmedNonEmptyString),
95});
96export type WorkflowRunHandles = typeof WorkflowRunHandles.Type;
97​
98export class WorkflowInspectionError extends Schema.TaggedErrorClass<WorkflowInspectionError>()(
99 "WorkflowInspectionError",
100 {
101 operation: Schema.String,
102 reason: Schema.Literals(["invalid-path", "not-found", "read-failed", "unsupported"]),
103 detail: Schema.String,
104 /** Underlying failure for read-failed/not-found wraps; absent for pure
105 * validation reasons. Matches the GitCommandError convention. */
106 cause: Schema.optional(Schema.Defect()),
107 },
108) {
109 override get message(): string {
110 return `Workflow inspection failed in ${this.operation}: ${this.detail}`;
111 }
113​
114export const WorkflowReadScriptInput = Schema.Struct({
115 scriptPath: TrimmedNonEmptyString,
116});
117export type WorkflowReadScriptInput = typeof WorkflowReadScriptInput.Type;
118​
119export const WorkflowReadScriptResult = Schema.Struct({
120 source: Schema.String,
121 truncated: Schema.Boolean,
122});
123export type WorkflowReadScriptResult = typeof WorkflowReadScriptResult.Type;
124​
125export const WorkflowReadJournalInput = Schema.Struct({
126 transcriptDir: TrimmedNonEmptyString,
127});
128export type WorkflowReadJournalInput = typeof WorkflowReadJournalInput.Type;
129​
130/**
131 * One journal record per agent. `resultJson` is the agent's return value
132 * re-serialized as JSON, truncated server-side; `resultTruncated` marks the
133 * clip. Agents with a `started` record but no `result` yet report
134 * `hasResult: false`.
135 */
136export const WorkflowJournalEntry = Schema.Struct({
137 agentId: Schema.String,
138 hasResult: Schema.Boolean,
139 resultJson: Schema.optional(Schema.String),
140 resultTruncated: Schema.optional(Schema.Boolean),
141});
142export type WorkflowJournalEntry = typeof WorkflowJournalEntry.Type;
143​
144export const WorkflowReadJournalResult = Schema.Struct({
145 entries: Schema.Array(WorkflowJournalEntry),
146 truncated: Schema.Boolean,
147});
148export type WorkflowReadJournalResult = typeof WorkflowReadJournalResult.Type;
149​
150export const WorkflowReadAgentTranscriptInput = Schema.Struct({
151 transcriptDir: TrimmedNonEmptyString,
152 agentId: TrimmedNonEmptyString,
153 /** Zero-based line cursor; omit to read from the start. */
154 afterLine: Schema.optional(Schema.Int),
155});
156export type WorkflowReadAgentTranscriptInput = typeof WorkflowReadAgentTranscriptInput.Type;
157​
158/**
159 * Raw transcript JSONL lines starting after the cursor. `nextLine` is the
160 * cursor for the next page; `complete` means the read reached end-of-file
161 * (more lines may still be appended while the agent runs — poll again).
162 */
163export const WorkflowReadAgentTranscriptResult = Schema.Struct({
164 lines: Schema.Array(Schema.String),
165 nextLine: Schema.Int,
166 complete: Schema.Boolean,
167});
168export type WorkflowReadAgentTranscriptResult = typeof WorkflowReadAgentTranscriptResult.Type;