@contractspec/integration.workflow-devkit 0.1.1

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.
@@ -0,0 +1,181 @@
1
+ // @bun
2
+ // src/helpers.ts
3
+ import { evaluateExpression } from "@contractspec/lib.contracts-spec/workflow";
4
+ function inferWorkflowDevkitBehavior(step) {
5
+ return step.runtime?.workflowDevkit?.behavior ?? step.type;
6
+ }
7
+ function resolveWorkflowDevkitEntryStepId(spec) {
8
+ const entryStepId = spec.definition.entryStepId ?? spec.definition.steps[0]?.id;
9
+ if (!entryStepId) {
10
+ throw new Error(`Workflow ${spec.meta.key}.v${spec.meta.version} does not define an entry step.`);
11
+ }
12
+ return entryStepId;
13
+ }
14
+ function resolveWorkflowDevkitRunIdentity(spec, runIdentity) {
15
+ if (runIdentity) {
16
+ return runIdentity;
17
+ }
18
+ const strategy = spec.runtime?.workflowDevkit?.runIdentity?.strategy ?? "meta-key-version";
19
+ const prefix = spec.runtime?.workflowDevkit?.runIdentity?.prefix;
20
+ const baseIdentity = strategy === "meta-key-version" ? `${spec.meta.key}.v${spec.meta.version}` : `${spec.meta.key}.v${spec.meta.version}`;
21
+ return prefix ? `${prefix}:${baseIdentity}` : baseIdentity;
22
+ }
23
+ function resolveWorkflowDevkitWaitToken(spec, step, runIdentity) {
24
+ const runtime = step.runtime?.workflowDevkit;
25
+ if (!runtime) {
26
+ return;
27
+ }
28
+ const explicitToken = runtime.hookWait?.token ?? runtime.webhookWait?.token ?? runtime.approvalWait?.token ?? runtime.streamSession?.followUpToken;
29
+ if (explicitToken) {
30
+ return explicitToken;
31
+ }
32
+ const tokenStrategy = spec.runtime?.workflowDevkit?.hookTokens?.strategy ?? "deterministic";
33
+ const prefix = spec.runtime?.workflowDevkit?.hookTokens?.prefix ?? spec.meta.key;
34
+ const stableStepId = sanitizeIdentifier(step.id);
35
+ if (tokenStrategy === "session-scoped") {
36
+ const resolvedRunIdentity = sanitizeIdentifier(resolveWorkflowDevkitRunIdentity(spec, runIdentity));
37
+ return `${prefix}:${resolvedRunIdentity}:${stableStepId}`;
38
+ }
39
+ if (tokenStrategy === "step-scoped") {
40
+ return `${prefix}:v${spec.meta.version}:${stableStepId}`;
41
+ }
42
+ return `${prefix}:${stableStepId}`;
43
+ }
44
+ function resolveWorkflowDevkitNextStepId(spec, step, data, input, output) {
45
+ const transitions = spec.definition.transitions.filter((transition) => transition.from === step.id);
46
+ for (const transition of transitions) {
47
+ if (evaluateExpression(transition.condition, {
48
+ data,
49
+ input,
50
+ output
51
+ })) {
52
+ return transition.to;
53
+ }
54
+ }
55
+ return null;
56
+ }
57
+ function mergeWorkflowDevkitData(current, input, output) {
58
+ const next = { ...current };
59
+ if (isRecord(input)) {
60
+ Object.assign(next, input);
61
+ }
62
+ if (isRecord(output)) {
63
+ Object.assign(next, output);
64
+ }
65
+ return next;
66
+ }
67
+ function sanitizeIdentifier(value) {
68
+ return value.replace(/[^a-zA-Z0-9_-]+/g, "-");
69
+ }
70
+ function isRecord(value) {
71
+ return value != null && typeof value === "object" && !Array.isArray(value);
72
+ }
73
+
74
+ // src/runtime.ts
75
+ async function runWorkflowSpecWithWorkflowDevkit(options) {
76
+ const history = [];
77
+ let currentStepId = resolveWorkflowDevkitEntryStepId(options.spec);
78
+ let data = { ...options.initialData ?? {} };
79
+ let input = options.initialData;
80
+ while (currentStepId) {
81
+ const step = lookupStep(options.spec, currentStepId);
82
+ const runtime = step.runtime?.workflowDevkit;
83
+ const context = {
84
+ data,
85
+ input,
86
+ runIdentity: options.runIdentity,
87
+ runtime,
88
+ spec: options.spec,
89
+ step
90
+ };
91
+ const behavior = inferWorkflowDevkitBehavior(step);
92
+ const token = resolveWorkflowDevkitWaitToken(options.spec, step, options.runIdentity);
93
+ const output = await executeWorkflowDevkitBehavior(behavior, context, token, options.bridge, options.primitives);
94
+ history.push({
95
+ behavior,
96
+ input,
97
+ output,
98
+ stepId: step.id,
99
+ token
100
+ });
101
+ data = mergeWorkflowDevkitData(data, input, output);
102
+ const nextStepId = resolveWorkflowDevkitNextStepId(options.spec, step, data, input, output);
103
+ if (!nextStepId) {
104
+ return {
105
+ currentStep: null,
106
+ data,
107
+ history,
108
+ status: "completed"
109
+ };
110
+ }
111
+ currentStepId = nextStepId;
112
+ input = output;
113
+ }
114
+ return {
115
+ currentStep: null,
116
+ data,
117
+ history,
118
+ status: "completed"
119
+ };
120
+ }
121
+ async function executeWorkflowDevkitBehavior(behavior, context, token, bridge, primitives) {
122
+ switch (behavior) {
123
+ case "sleep":
124
+ if (!context.runtime?.sleep?.duration) {
125
+ throw new Error(`Step "${context.step.id}" is missing sleep.duration.`);
126
+ }
127
+ await primitives.sleep(context.runtime.sleep.duration);
128
+ return { sleptFor: context.runtime.sleep.duration };
129
+ case "hookWait":
130
+ return awaitExternalWait(context, token, bridge?.onExternalWait, primitives.createHook);
131
+ case "webhookWait":
132
+ return awaitExternalWait(context, token, bridge?.onExternalWait, primitives.createWebhook ?? primitives.createHook, context.runtime?.webhookWait?.path, context.runtime?.webhookWait?.method);
133
+ case "approvalWait":
134
+ return awaitExternalWait(context, token, bridge?.onApprovalRequested, primitives.createHook);
135
+ case "streamSession":
136
+ return awaitExternalWait(context, token, bridge?.onStreamSession, primitives.createHook);
137
+ case "automation":
138
+ if (!bridge?.executeAutomationStep) {
139
+ throw new Error("Workflow DevKit bridge requires executeAutomationStep for automation steps.");
140
+ }
141
+ return bridge.executeAutomationStep(context);
142
+ case "human":
143
+ if (!bridge?.awaitHumanInput) {
144
+ throw new Error("Workflow DevKit bridge requires awaitHumanInput for human steps without explicit wait behavior.");
145
+ }
146
+ return bridge.awaitHumanInput(context);
147
+ case "decision":
148
+ return bridge?.executeDecisionStep ? bridge.executeDecisionStep(context) : context.input;
149
+ }
150
+ }
151
+ async function awaitExternalWait(context, token, notifier, factory, path, method) {
152
+ if (!token) {
153
+ throw new Error(`Step "${context.step.id}" requires a Workflow DevKit wait token.`);
154
+ }
155
+ const behavior = context.runtime?.behavior;
156
+ if (behavior !== "approvalWait" && behavior !== "hookWait" && behavior !== "streamSession" && behavior !== "webhookWait") {
157
+ throw new Error(`Step "${context.step.id}" is not configured with an external wait behavior.`);
158
+ }
159
+ const waitContext = {
160
+ ...context,
161
+ behavior,
162
+ token
163
+ };
164
+ await notifier?.(waitContext);
165
+ const hook = factory({ method, path, token });
166
+ try {
167
+ return await hook;
168
+ } finally {
169
+ await hook.dispose?.();
170
+ }
171
+ }
172
+ function lookupStep(spec, stepId) {
173
+ const step = spec.definition.steps.find((candidate) => candidate.id === stepId);
174
+ if (!step) {
175
+ throw new Error(`Step "${stepId}" not found in workflow ${spec.meta.key}.v${spec.meta.version}.`);
176
+ }
177
+ return step;
178
+ }
179
+ export {
180
+ runWorkflowSpecWithWorkflowDevkit
181
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,97 @@
1
+ import type { Step, WorkflowDevkitHostTarget, WorkflowDevkitIntegrationMode, WorkflowDevkitStepBehavior, WorkflowDevkitStepRuntimeConfig, WorkflowSpec } from '@contractspec/lib.contracts-spec/workflow';
2
+ import type { UIMessageChunk } from 'ai';
3
+ export interface WorkflowDevkitCompiledTransition {
4
+ condition?: string;
5
+ to: string;
6
+ }
7
+ export interface WorkflowDevkitCompiledStep {
8
+ behavior: Step['type'] | WorkflowDevkitStepBehavior;
9
+ id: string;
10
+ label: string;
11
+ operationRef?: string;
12
+ runtime?: WorkflowDevkitStepRuntimeConfig;
13
+ transitions: WorkflowDevkitCompiledTransition[];
14
+ type: Step['type'];
15
+ waitToken?: string;
16
+ }
17
+ export interface WorkflowDevkitCompilation {
18
+ entryStepId: string;
19
+ hostTarget: WorkflowDevkitHostTarget;
20
+ hookTokenStrategy: string;
21
+ integrationMode: WorkflowDevkitIntegrationMode;
22
+ runIdentityStrategy: string;
23
+ specKey: string;
24
+ specVersion: string;
25
+ steps: WorkflowDevkitCompiledStep[];
26
+ }
27
+ export interface WorkflowDevkitGeneratedArtifacts {
28
+ genericBootstrap: string;
29
+ manifest: string;
30
+ nextFollowUpRoute: string;
31
+ nextStartRoute: string;
32
+ nextStreamRoute: string;
33
+ workflowModule: string;
34
+ }
35
+ export interface WorkflowDevkitRuntimeStepContext {
36
+ data: Record<string, unknown>;
37
+ input?: unknown;
38
+ runIdentity?: string;
39
+ runtime?: WorkflowDevkitStepRuntimeConfig;
40
+ spec: WorkflowSpec;
41
+ step: Step;
42
+ }
43
+ export interface WorkflowDevkitExternalWaitContext extends WorkflowDevkitRuntimeStepContext {
44
+ behavior: WorkflowDevkitStepBehavior;
45
+ token: string;
46
+ }
47
+ export interface WorkflowDevkitRuntimeBridge {
48
+ awaitHumanInput?: (context: WorkflowDevkitRuntimeStepContext) => Promise<unknown>;
49
+ executeAutomationStep?: (context: WorkflowDevkitRuntimeStepContext) => Promise<unknown>;
50
+ executeDecisionStep?: (context: WorkflowDevkitRuntimeStepContext) => Promise<unknown>;
51
+ onApprovalRequested?: (context: WorkflowDevkitExternalWaitContext) => Promise<void> | void;
52
+ onExternalWait?: (context: WorkflowDevkitExternalWaitContext) => Promise<void> | void;
53
+ onStreamSession?: (context: WorkflowDevkitExternalWaitContext) => Promise<void> | void;
54
+ }
55
+ export interface WorkflowDevkitHookLike<T = unknown> extends AsyncIterable<T>, PromiseLike<T> {
56
+ dispose?: () => Promise<void> | void;
57
+ token: string;
58
+ }
59
+ export interface WorkflowDevkitPrimitives {
60
+ createHook: <T = unknown>(options?: {
61
+ token?: string;
62
+ }) => WorkflowDevkitHookLike<T>;
63
+ createWebhook?: <T = unknown>(options?: {
64
+ method?: string;
65
+ path?: string;
66
+ token?: string;
67
+ }) => WorkflowDevkitHookLike<T>;
68
+ sleep: (duration: string) => Promise<void>;
69
+ }
70
+ export interface WorkflowDevkitExecutionRecord {
71
+ behavior: Step['type'] | WorkflowDevkitStepBehavior;
72
+ input?: unknown;
73
+ output?: unknown;
74
+ stepId: string;
75
+ token?: string;
76
+ }
77
+ export interface WorkflowDevkitRunResult {
78
+ currentStep: string | null;
79
+ data: Record<string, unknown>;
80
+ history: WorkflowDevkitExecutionRecord[];
81
+ status: 'completed';
82
+ }
83
+ export interface WorkflowRunReadableLike extends ReadableStream<UIMessageChunk> {
84
+ getTailIndex?: () => number | Promise<number>;
85
+ }
86
+ export interface WorkflowRunLike {
87
+ getReadable?: (options?: {
88
+ startIndex?: number;
89
+ }) => WorkflowRunReadableLike;
90
+ readable?: WorkflowRunReadableLike;
91
+ runId: string;
92
+ }
93
+ export interface WorkflowApiLike {
94
+ getRun: (runId: string) => WorkflowRunLike | null | undefined;
95
+ resumeHook: (token: string, payload: unknown) => Promise<unknown>;
96
+ start: (workflow: (...args: unknown[]) => Promise<unknown>, args: unknown[]) => Promise<WorkflowRunLike>;
97
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ // @bun
package/package.json ADDED
@@ -0,0 +1,173 @@
1
+ {
2
+ "name": "@contractspec/integration.workflow-devkit",
3
+ "version": "0.1.1",
4
+ "description": "Workflow DevKit compiler and runtime bridges for ContractSpec workflows",
5
+ "keywords": [
6
+ "contractspec",
7
+ "workflow",
8
+ "workflow-devkit",
9
+ "durable",
10
+ "typescript"
11
+ ],
12
+ "type": "module",
13
+ "main": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "browser": "./dist/browser/index.js",
19
+ "bun": "./dist/index.js",
20
+ "node": "./dist/node/index.js",
21
+ "default": "./dist/index.js"
22
+ },
23
+ "./agent-adapter": {
24
+ "types": "./dist/agent-adapter.d.ts",
25
+ "browser": "./dist/browser/agent-adapter.js",
26
+ "bun": "./dist/agent-adapter.js",
27
+ "node": "./dist/node/agent-adapter.js",
28
+ "default": "./dist/agent-adapter.js"
29
+ },
30
+ "./chat-routes": {
31
+ "types": "./dist/chat-routes.d.ts",
32
+ "browser": "./dist/browser/chat-routes.js",
33
+ "bun": "./dist/chat-routes.js",
34
+ "node": "./dist/node/chat-routes.js",
35
+ "default": "./dist/chat-routes.js"
36
+ },
37
+ "./compiler": {
38
+ "types": "./dist/compiler.d.ts",
39
+ "browser": "./dist/browser/compiler.js",
40
+ "bun": "./dist/compiler.js",
41
+ "node": "./dist/node/compiler.js",
42
+ "default": "./dist/compiler.js"
43
+ },
44
+ "./helpers": {
45
+ "types": "./dist/helpers.d.ts",
46
+ "browser": "./dist/browser/helpers.js",
47
+ "bun": "./dist/helpers.js",
48
+ "node": "./dist/node/helpers.js",
49
+ "default": "./dist/helpers.js"
50
+ },
51
+ "./next": {
52
+ "types": "./dist/next.d.ts",
53
+ "browser": "./dist/browser/next.js",
54
+ "bun": "./dist/next.js",
55
+ "node": "./dist/node/next.js",
56
+ "default": "./dist/next.js"
57
+ },
58
+ "./runtime": {
59
+ "types": "./dist/runtime.d.ts",
60
+ "browser": "./dist/browser/runtime.js",
61
+ "bun": "./dist/runtime.js",
62
+ "node": "./dist/node/runtime.js",
63
+ "default": "./dist/runtime.js"
64
+ },
65
+ "./types": {
66
+ "types": "./dist/types.d.ts",
67
+ "browser": "./dist/browser/types.js",
68
+ "bun": "./dist/types.js",
69
+ "node": "./dist/node/types.js",
70
+ "default": "./dist/types.js"
71
+ }
72
+ },
73
+ "files": [
74
+ "dist",
75
+ "README.md"
76
+ ],
77
+ "scripts": {
78
+ "publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
79
+ "publish:pkg:canary": "bun publish:pkg --tag canary",
80
+ "build": "bun run prebuild && bun run build:bundle && bun run build:types",
81
+ "build:bundle": "contractspec-bun-build transpile",
82
+ "build:types": "contractspec-bun-build types",
83
+ "dev": "contractspec-bun-build dev",
84
+ "clean": "rimraf dist .turbo",
85
+ "lint": "bun lint:fix",
86
+ "lint:fix": "biome check --write --unsafe --only=nursery/useSortedClasses . && biome check --write .",
87
+ "lint:check": "biome check .",
88
+ "test": "bun test",
89
+ "prebuild": "contractspec-bun-build prebuild",
90
+ "typecheck": "tsc --noEmit"
91
+ },
92
+ "dependencies": {
93
+ "@contractspec/lib.ai-agent": "8.0.3",
94
+ "@contractspec/lib.contracts-spec": "5.0.3",
95
+ "@workflow/ai": "^4.1.0-beta.58",
96
+ "ai": "6.0.138",
97
+ "workflow": "^4.2.0-beta.72"
98
+ },
99
+ "devDependencies": {
100
+ "@contractspec/tool.bun": "3.7.11",
101
+ "@contractspec/tool.typescript": "3.7.11",
102
+ "typescript": "^5.9.3"
103
+ },
104
+ "publishConfig": {
105
+ "access": "public",
106
+ "registry": "https://registry.npmjs.org/",
107
+ "exports": {
108
+ ".": {
109
+ "types": "./dist/index.d.ts",
110
+ "browser": "./dist/browser/index.js",
111
+ "bun": "./dist/index.js",
112
+ "node": "./dist/node/index.js",
113
+ "default": "./dist/index.js"
114
+ },
115
+ "./agent-adapter": {
116
+ "types": "./dist/agent-adapter.d.ts",
117
+ "browser": "./dist/browser/agent-adapter.js",
118
+ "bun": "./dist/agent-adapter.js",
119
+ "node": "./dist/node/agent-adapter.js",
120
+ "default": "./dist/agent-adapter.js"
121
+ },
122
+ "./chat-routes": {
123
+ "types": "./dist/chat-routes.d.ts",
124
+ "browser": "./dist/browser/chat-routes.js",
125
+ "bun": "./dist/chat-routes.js",
126
+ "node": "./dist/node/chat-routes.js",
127
+ "default": "./dist/chat-routes.js"
128
+ },
129
+ "./compiler": {
130
+ "types": "./dist/compiler.d.ts",
131
+ "browser": "./dist/browser/compiler.js",
132
+ "bun": "./dist/compiler.js",
133
+ "node": "./dist/node/compiler.js",
134
+ "default": "./dist/compiler.js"
135
+ },
136
+ "./helpers": {
137
+ "types": "./dist/helpers.d.ts",
138
+ "browser": "./dist/browser/helpers.js",
139
+ "bun": "./dist/helpers.js",
140
+ "node": "./dist/node/helpers.js",
141
+ "default": "./dist/helpers.js"
142
+ },
143
+ "./next": {
144
+ "types": "./dist/next.d.ts",
145
+ "browser": "./dist/browser/next.js",
146
+ "bun": "./dist/next.js",
147
+ "node": "./dist/node/next.js",
148
+ "default": "./dist/next.js"
149
+ },
150
+ "./runtime": {
151
+ "types": "./dist/runtime.d.ts",
152
+ "browser": "./dist/browser/runtime.js",
153
+ "bun": "./dist/runtime.js",
154
+ "node": "./dist/node/runtime.js",
155
+ "default": "./dist/runtime.js"
156
+ },
157
+ "./types": {
158
+ "types": "./dist/types.d.ts",
159
+ "browser": "./dist/browser/types.js",
160
+ "bun": "./dist/types.js",
161
+ "node": "./dist/node/types.js",
162
+ "default": "./dist/types.js"
163
+ }
164
+ }
165
+ },
166
+ "license": "MIT",
167
+ "repository": {
168
+ "type": "git",
169
+ "url": "https://github.com/lssm-tech/contractspec.git",
170
+ "directory": "packages/integrations/workflow-devkit"
171
+ },
172
+ "homepage": "https://contractspec.io"
173
+ }