-
-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathworkflow-contracts.ts
More file actions
257 lines (234 loc) · 7.34 KB
/
Copy pathworkflow-contracts.ts
File metadata and controls
257 lines (234 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import type { FromSchema } from "json-schema-to-ts";
import * as z from "zod/v4";
import { LOCAL_AGENT_PROVIDERS } from "./local-agent-profiles.js";
import type { LocalAgentProvider } from "./local-agent-profiles.js";
import { jsonSchemaSchema, type JsonSchema, type JsonValue } from "./json-types.js";
export const localAgentProviderSchema = z.enum(LOCAL_AGENT_PROVIDERS);
export const workflowMetaSchema = z
.object({
name: z.string().trim().min(1).regex(/^[a-z0-9-]+$/),
description: z.string().trim().min(1),
phases: z
.array(
z
.object({
title: z.string().trim().min(1),
detail: z.string().trim().min(1).optional(),
})
.strict(),
)
.optional(),
whenToUse: z.string().trim().min(1).optional(),
defaultProvider: localAgentProviderSchema.optional(),
concurrency: z.number().finite().int().positive().optional(),
})
.strict();
export type WorkflowMeta = z.infer<typeof workflowMetaSchema>;
export type WorkflowPhaseMeta = NonNullable<WorkflowMeta["phases"]>[number];
export const agentIsolationModeSchema = z.enum(["shared", "worktree"]);
export type AgentIsolationMode = z.infer<typeof agentIsolationModeSchema>;
export const workflowRunStatusSchema = z.enum([
"starting",
"running",
"completed",
"failed",
"cancelled",
]);
export type WorkflowRunStatus = z.infer<typeof workflowRunStatusSchema>;
export const workflowAgentCallStatusSchema = z.enum([
"running",
"completed",
"failed",
"cancelled",
"from_cache",
]);
export type WorkflowAgentCallStatus = z.infer<typeof workflowAgentCallStatusSchema>;
export const workflowRunSourceSchema = z.enum(["inline", "named", "resume"]);
export type WorkflowRunSource = z.infer<typeof workflowRunSourceSchema>;
export const agentOptsSchema = z
.object({
label: z.string().trim().min(1).optional(),
phase: z.string().trim().min(1).optional(),
schema: jsonSchemaSchema.optional(),
model: z.string().trim().min(1).optional(),
effort: z.string().trim().min(1).optional(),
provider: localAgentProviderSchema.optional(),
isolation: z.literal("worktree").optional(),
})
.strict();
export type AgentOpts<S extends JsonSchema | undefined = JsonSchema | undefined> = Omit<
z.infer<typeof agentOptsSchema>,
"schema"
> & {
schema?: S;
};
export interface WorkflowAgent {
<const S extends JsonSchema>(
prompt: string,
opts: AgentOpts<S> & { schema: S },
): Promise<FromSchema<S>>;
(prompt: string, opts?: AgentOpts<undefined>): Promise<string>;
}
export type WorkflowTask<T = unknown> = () => T | Promise<T>;
export interface WorkflowParallel {
<const T extends readonly WorkflowTask[]>(
tasks: T,
): Promise<{
[K in keyof T]: Awaited<ReturnType<T[K]>> | null;
}>;
}
export interface WorkflowPipeline {
<T, R>(
items: readonly T[],
stage: (previous: T, item: T, index: number) => R | Promise<R>,
): Promise<Array<Awaited<R> | null>>;
<T, A, R>(
items: readonly T[],
first: (previous: T, item: T, index: number) => A | Promise<A>,
second: (previous: Awaited<A>, item: T, index: number) => R | Promise<R>,
): Promise<Array<Awaited<R> | null>>;
(...args: unknown[]): Promise<Array<unknown | null>>;
}
export interface WorkflowNested {
(nameOrRef: string | { scriptPath: string }, args?: JsonValue): Promise<unknown>;
}
export const workflowErrorKindSchema = z.enum([
"syntax",
"meta",
"determinism",
"provider_disabled",
"provider_unavailable",
"no_provider",
"provider",
"schema",
"cancelled",
"timeout",
"heartbeat",
"worktree",
"nest_depth",
"path",
"result_too_large",
"args_too_large",
"script_too_large",
"internal",
]);
export type WorkflowErrorKind = z.infer<typeof workflowErrorKindSchema>;
/** Stable provider-independent error shape exposed to workflow scripts. */
export interface WorkflowScriptThrownError extends Error {
kind: WorkflowErrorKind;
retryable: boolean;
}
export const WORKFLOW_EVENT_TYPES = [
"run_started",
"run_completed",
"run_failed",
"run_cancelled",
"phase_started",
"log",
"agent_call_started",
"agent_call_completed",
"agent_call_failed",
"agent_call_cached",
"schema_retry",
"worktree_created",
"worktree_finalized",
] as const;
export const workflowEventTypeSchema = z.enum(WORKFLOW_EVENT_TYPES);
export type WorkflowEventType = z.infer<typeof workflowEventTypeSchema>;
export const workflowEventPayloadSchemas = {
run_started: z
.object({
name: z.string(),
scriptHash: z.string(),
concurrency: z.number().int().positive(),
})
.strict(),
run_completed: z.object({ callCount: z.number().int().nonnegative() }).strict(),
run_failed: z
.object({ error: z.string(), errorKind: workflowErrorKindSchema })
.strict(),
run_cancelled: z.object({ reason: z.string().optional() }).strict(),
phase_started: z.object({ title: z.string().min(1) }).strict(),
log: z.object({ message: z.string() }).strict(),
agent_call_started: z
.object({
callIndex: z.number().int().nonnegative(),
cacheKey: z.string(),
provider: localAgentProviderSchema,
isolation: agentIsolationModeSchema,
worktreePath: z.string().optional(),
})
.strict(),
agent_call_completed: z
.object({
callIndex: z.number().int().nonnegative(),
provider: localAgentProviderSchema,
isolation: agentIsolationModeSchema,
worktreePath: z.string().optional(),
dirty: z.boolean().optional(),
fromCache: z.boolean(),
})
.strict(),
agent_call_failed: z
.object({
callIndex: z.number().int().nonnegative(),
error: z.string(),
cleanupError: z.string().optional(),
isolation: agentIsolationModeSchema,
worktreePath: z.string().optional(),
})
.strict(),
agent_call_cached: z
.object({
callIndex: z.number().int().nonnegative(),
cacheKey: z.string(),
provider: localAgentProviderSchema,
replayMatch: z.enum(["same_index", "compatible_key"]),
replayedFromRunId: z.string(),
replayedFromCallIndex: z.number().int().nonnegative(),
})
.strict(),
schema_retry: z
.object({
callIndex: z.number().int().nonnegative(),
attempt: z.number().int().positive(),
errors: z.string(),
mode: z.enum(["native", "prompt"]),
})
.strict(),
worktree_created: z
.object({
callIndex: z.number().int().nonnegative(),
worktreePath: z.string(),
isolation: z.literal("worktree"),
})
.strict(),
worktree_finalized: z
.object({
callIndex: z.number().int().nonnegative(),
worktreePath: z.string().optional(),
dirty: z.boolean(),
removed: z.boolean(),
outcome: z.literal("failure").optional(),
})
.strict(),
} as const satisfies Record<WorkflowEventType, z.ZodTypeAny>;
export type WorkflowEventPayloads = {
[K in WorkflowEventType]: z.infer<(typeof workflowEventPayloadSchemas)[K]>;
};
export type AppendWorkflowEventInput<K extends WorkflowEventType = WorkflowEventType> = {
[P in K]: {
runId: string;
type: P;
phase?: string;
label?: string;
data: WorkflowEventPayloads[P];
};
}[K];
export function parseWorkflowEventPayload<K extends WorkflowEventType>(
type: K,
data: unknown,
): WorkflowEventPayloads[K] {
return workflowEventPayloadSchemas[type].parse(data) as WorkflowEventPayloads[K];
}
export type WorkflowProviderId = LocalAgentProvider;