ai-workflow-contract 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +22 -0
  2. package/src/index.ts +301 -0
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "ai-workflow-contract",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "description": "Shared AI workflow contracts over WorkCtrlFlow and EagerDataFlow substrates",
6
+ "type": "module",
7
+ "main": "./src/index.ts",
8
+ "exports": {
9
+ ".": "./src/index.ts"
10
+ },
11
+ "dependencies": {
12
+ "eager-data-flow-contract": "0.1.0",
13
+ "work-ctrl-flow-contract": "0.1.0"
14
+ },
15
+ "files": [
16
+ "src"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public",
20
+ "registry": "https://registry.npmjs.com"
21
+ }
22
+ }
package/src/index.ts ADDED
@@ -0,0 +1,301 @@
1
+ import type { EagerDataFlowAuthoringPlan, EagerDataFlowValue } from 'eager-data-flow-contract';
2
+ import type { FlowBundleSpec, WorkCtrlFlowRuntime } from 'work-ctrl-flow-contract';
3
+
4
+ export type AIWorkflowKind = 'AICtrlWorkflow' | 'AIDataWorkflow';
5
+
6
+ export type AIWorkflowSubstrate = 'WorkCtrlFlow' | 'EagerDataFlow';
7
+
8
+ export type AIWorkflowReusePolicy = 'semantic-hash' | 'never';
9
+
10
+ export type AIWorkflowReusePolicySource = 'node' | 'node-type-default' | 'platform-default';
11
+
12
+ export type AIWorkflowResourceRefScheme = 'vfs' | 'resource' | 'config' | 'secret';
13
+
14
+ export const AI_WORKFLOW_ALLOWED_RESOURCE_REF_SCHEMES = Object.freeze([
15
+ 'vfs',
16
+ 'resource',
17
+ 'config',
18
+ 'secret',
19
+ ] as const satisfies readonly AIWorkflowResourceRefScheme[]);
20
+
21
+ export type AIWorkflowResourceRef = `${AIWorkflowResourceRefScheme}://${string}`;
22
+
23
+ export type AIWorkflowResourceKind =
24
+ | 'AIWorkflowAppBundle'
25
+ | 'AIWorkflowCatalog'
26
+ | 'AICtrlWorkflow'
27
+ | 'AIDataWorkflow'
28
+ | 'AIAgentDefinition'
29
+ | 'MaterialPort'
30
+ | 'MaterialBinding'
31
+ | 'MessageSchema'
32
+ | 'Config'
33
+ | 'SecretHandle'
34
+ | (string & {});
35
+
36
+ export interface AIWorkflowResourceIdentity {
37
+ readonly kind: AIWorkflowResourceKind;
38
+ readonly fqn: string;
39
+ readonly ref: `resource://${string}`;
40
+ readonly revision?: string;
41
+ }
42
+
43
+ export interface AIWorkflowCatalogEntry {
44
+ readonly id: string;
45
+ readonly kind: AIWorkflowResourceKind;
46
+ readonly ref: AIWorkflowResourceRef;
47
+ readonly fqn?: string;
48
+ }
49
+
50
+ export interface AIWorkflowResourceCatalog {
51
+ readonly id: string;
52
+ readonly entries: readonly AIWorkflowCatalogEntry[];
53
+ }
54
+
55
+ export interface AIWorkflowAppBundleManifest {
56
+ readonly kind: 'AIWorkflowAppBundle';
57
+ readonly fqn: string;
58
+ readonly catalog: AIWorkflowResourceCatalog;
59
+ readonly entrypoints?: readonly AIWorkflowResourceRef[];
60
+ readonly metadata?: Readonly<Record<string, unknown>>;
61
+ }
62
+
63
+ export type AIWorkflowNodeType =
64
+ | 'agent'
65
+ | 'data'
66
+ | 'effect'
67
+ | 'manual'
68
+ | 'subflow'
69
+ | 'tool'
70
+ | (string & {});
71
+
72
+ export const AI_WORKFLOW_MACHINE_NODE_DEFAULT_REUSE_POLICY = 'semantic-hash' as const;
73
+
74
+ export const AI_WORKFLOW_MANUAL_NODE_DEFAULT_REUSE_POLICY = 'never' as const;
75
+
76
+ export interface AIWorkflowProfileDescriptor<
77
+ Kind extends AIWorkflowKind = AIWorkflowKind,
78
+ Substrate extends AIWorkflowSubstrate = AIWorkflowSubstrate,
79
+ > {
80
+ readonly kind: Kind;
81
+ readonly substrate: Substrate;
82
+ readonly defaultReusePolicy: AIWorkflowReusePolicy;
83
+ readonly manualNodeDefaultReusePolicy: 'never';
84
+ }
85
+
86
+ export const AI_CTRL_WORKFLOW_DESCRIPTOR = Object.freeze({
87
+ kind: 'AICtrlWorkflow',
88
+ substrate: 'WorkCtrlFlow',
89
+ defaultReusePolicy: AI_WORKFLOW_MACHINE_NODE_DEFAULT_REUSE_POLICY,
90
+ manualNodeDefaultReusePolicy: AI_WORKFLOW_MANUAL_NODE_DEFAULT_REUSE_POLICY,
91
+ } satisfies AIWorkflowProfileDescriptor<'AICtrlWorkflow', 'WorkCtrlFlow'>);
92
+
93
+ export const AI_DATA_WORKFLOW_DESCRIPTOR = Object.freeze({
94
+ kind: 'AIDataWorkflow',
95
+ substrate: 'EagerDataFlow',
96
+ defaultReusePolicy: AI_WORKFLOW_MACHINE_NODE_DEFAULT_REUSE_POLICY,
97
+ manualNodeDefaultReusePolicy: AI_WORKFLOW_MANUAL_NODE_DEFAULT_REUSE_POLICY,
98
+ } satisfies AIWorkflowProfileDescriptor<'AIDataWorkflow', 'EagerDataFlow'>);
99
+
100
+ export type AIWorkflowDefinitionRefScheme = 'resource' | 'vfs' | (string & {});
101
+
102
+ export interface AIWorkflowDefinitionRef {
103
+ readonly ref: string;
104
+ readonly scheme?: AIWorkflowDefinitionRefScheme;
105
+ readonly fqn?: string;
106
+ readonly revision?: string;
107
+ }
108
+
109
+ export interface AIWorkflowRunRef {
110
+ readonly workflow: AIWorkflowDefinitionRef;
111
+ readonly runId: string;
112
+ readonly generation: number;
113
+ readonly parentGeneration?: number;
114
+ }
115
+
116
+ export interface AIWorkflowRuntimeRoots {
117
+ /**
118
+ * Wrapper-provided global AI workflow state root. The contract treats it as
119
+ * opaque; it may be a VFS URI, content-addressed root, or host-specific path
120
+ * resolved outside core logic.
121
+ */
122
+ readonly globalRoot: string;
123
+ /**
124
+ * Wrapper-provided workspace AI workflow state root. Core logic must not
125
+ * derive this value from process cwd or environment variables.
126
+ */
127
+ readonly workspaceRoot: string;
128
+ }
129
+
130
+ export interface AIWorkflowSemanticFingerprintPart {
131
+ readonly name: string;
132
+ readonly value: string;
133
+ }
134
+
135
+ export interface AIWorkflowSemanticFingerprint {
136
+ readonly algorithm: 'semantic-hash/v1' | (string & {});
137
+ readonly value: string;
138
+ readonly parts?: readonly AIWorkflowSemanticFingerprintPart[];
139
+ }
140
+
141
+ export interface AIWorkflowEffectiveReusePolicy {
142
+ readonly policy: AIWorkflowReusePolicy;
143
+ readonly source: AIWorkflowReusePolicySource;
144
+ readonly nodeType: AIWorkflowNodeType;
145
+ }
146
+
147
+ export type AIWorkflowRunStatus = 'Pending' | 'Running' | 'Waiting' | 'Succeeded' | 'Failed' | 'Cancelled';
148
+
149
+ export type AIWorkflowNodeStatus =
150
+ | 'Pending'
151
+ | 'Running'
152
+ | 'Waiting'
153
+ | 'Succeeded'
154
+ | 'Failed'
155
+ | 'Cancelled'
156
+ | 'Invalidated'
157
+ | 'Reused';
158
+
159
+ export interface AIWorkflowNodeResult {
160
+ readonly nodeId: string;
161
+ readonly generation: number;
162
+ readonly status: AIWorkflowNodeStatus;
163
+ readonly output?: unknown;
164
+ readonly semanticFingerprint?: AIWorkflowSemanticFingerprint;
165
+ readonly reusedFrom?: {
166
+ readonly runId: string;
167
+ readonly generation: number;
168
+ readonly nodeId: string;
169
+ };
170
+ }
171
+
172
+ export interface AIWorkflowRunEvent {
173
+ readonly eventId: string;
174
+ readonly runId: string;
175
+ readonly generation: number;
176
+ readonly type: string;
177
+ readonly atMs: number;
178
+ readonly nodeId?: string;
179
+ readonly payload?: unknown;
180
+ }
181
+
182
+ export interface AIWorkflowRunState {
183
+ readonly ref: AIWorkflowRunRef;
184
+ readonly status: AIWorkflowRunStatus;
185
+ readonly nodes: Readonly<Record<string, AIWorkflowNodeResult>>;
186
+ readonly events?: readonly AIWorkflowRunEvent[];
187
+ readonly metadata?: Readonly<Record<string, unknown>>;
188
+ }
189
+
190
+ export interface AIWorkflowReusableNodeResultRequest {
191
+ readonly run: AIWorkflowRunRef;
192
+ readonly nodeId: string;
193
+ readonly nodeType: AIWorkflowNodeType;
194
+ readonly fingerprint: AIWorkflowSemanticFingerprint;
195
+ readonly policy: AIWorkflowEffectiveReusePolicy;
196
+ }
197
+
198
+ export interface AIWorkflowStateStore {
199
+ loadRunState(ref: AIWorkflowRunRef): Promise<AIWorkflowRunState | undefined>;
200
+ saveRunState(state: AIWorkflowRunState): Promise<void>;
201
+ appendRunEvent(ref: AIWorkflowRunRef, event: AIWorkflowRunEvent): Promise<void>;
202
+ findReusableNodeResult?(
203
+ request: AIWorkflowReusableNodeResultRequest,
204
+ ): Promise<AIWorkflowNodeResult | undefined>;
205
+ }
206
+
207
+ export interface AIWorkflowEffectRequest<Input = unknown> {
208
+ readonly effectId: string;
209
+ readonly operation: string;
210
+ readonly input: Input;
211
+ readonly config?: Readonly<Record<string, EagerDataFlowValue>>;
212
+ readonly run: AIWorkflowRunRef;
213
+ readonly nodeId?: string;
214
+ readonly materialRefs?: readonly string[];
215
+ readonly signal?: AbortSignal;
216
+ }
217
+
218
+ export interface AIWorkflowEffectProvider {
219
+ invoke<Input = unknown, Output = unknown>(request: AIWorkflowEffectRequest<Input>): Promise<Output>;
220
+ }
221
+
222
+ export interface AIWorkflowEffectHandlerContext {
223
+ readonly roots: AIWorkflowRuntimeRoots;
224
+ readonly metadata?: Readonly<Record<string, unknown>>;
225
+ }
226
+
227
+ export type AIWorkflowEffectHandler<Input = unknown, Output = unknown> = (
228
+ request: AIWorkflowEffectRequest<Input>,
229
+ context: AIWorkflowEffectHandlerContext,
230
+ ) => Output | Promise<Output>;
231
+
232
+ export type AIWorkflowEffectHandlerMap = Readonly<Record<string, AIWorkflowEffectHandler>>;
233
+
234
+ export interface AIWorkflowEffectHandlerResolution {
235
+ readonly operation: string;
236
+ readonly handler?: AIWorkflowEffectHandler;
237
+ }
238
+
239
+ export interface AIWorkflowEffectHandlerRegistry {
240
+ hasHandler(operation: string): boolean;
241
+ resolveHandler(operation: string): AIWorkflowEffectHandlerResolution;
242
+ listOperations(): readonly string[];
243
+ }
244
+
245
+ export interface AIWorkflowMutableEffectHandlerRegistry extends AIWorkflowEffectHandlerRegistry {
246
+ registerHandler(operation: string, handler: AIWorkflowEffectHandler): void;
247
+ unregisterHandler(operation: string): boolean;
248
+ }
249
+
250
+ export interface AIWorkflowEmbeddedEffectProvider extends AIWorkflowEffectProvider {
251
+ readonly registry: AIWorkflowEffectHandlerRegistry;
252
+ readonly roots: AIWorkflowRuntimeRoots;
253
+ }
254
+
255
+ export interface AIWorkflowEffectResult<Output = unknown> {
256
+ readonly effectId: string;
257
+ readonly operation: string;
258
+ readonly output: Output;
259
+ }
260
+
261
+ export interface AIWorkflowRuntimeContext<
262
+ Effects extends AIWorkflowEffectProvider = AIWorkflowEffectProvider,
263
+ Store extends AIWorkflowStateStore = AIWorkflowStateStore,
264
+ > {
265
+ readonly roots: AIWorkflowRuntimeRoots;
266
+ readonly effects: Effects;
267
+ readonly stateStore: Store;
268
+ readonly metadata?: Readonly<Record<string, unknown>>;
269
+ }
270
+
271
+ export interface AICtrlWorkflowRuntime<
272
+ Effects extends AIWorkflowEffectProvider = AIWorkflowEffectProvider,
273
+ Store extends AIWorkflowStateStore = AIWorkflowStateStore,
274
+ > extends WorkCtrlFlowRuntime {
275
+ readonly ai: AIWorkflowRuntimeContext<Effects, Store>;
276
+ }
277
+
278
+ export interface AIDataWorkflowRuntime<
279
+ Effects extends AIWorkflowEffectProvider = AIWorkflowEffectProvider,
280
+ Store extends AIWorkflowStateStore = AIWorkflowStateStore,
281
+ > {
282
+ readonly ai: AIWorkflowRuntimeContext<Effects, Store>;
283
+ }
284
+
285
+ export interface AICtrlWorkflowDefinitionBinding {
286
+ readonly kind: 'AICtrlWorkflow';
287
+ readonly substrate: 'WorkCtrlFlow';
288
+ readonly definition: FlowBundleSpec;
289
+ readonly descriptor?: AIWorkflowProfileDescriptor<'AICtrlWorkflow', 'WorkCtrlFlow'>;
290
+ }
291
+
292
+ export interface AIDataWorkflowDefinitionBinding {
293
+ readonly kind: 'AIDataWorkflow';
294
+ readonly substrate: 'EagerDataFlow';
295
+ readonly definition: EagerDataFlowAuthoringPlan;
296
+ readonly descriptor?: AIWorkflowProfileDescriptor<'AIDataWorkflow', 'EagerDataFlow'>;
297
+ }
298
+
299
+ export type AIWorkflowDefinitionBinding =
300
+ | AICtrlWorkflowDefinitionBinding
301
+ | AIDataWorkflowDefinitionBinding;