@leitwerk-dev/process-sdk 0.1.8 → 0.2.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.
- package/dist/{chunk-TRQJ5PRM.js → chunk-TA4KO53Q.js} +1 -5
- package/dist/chunk-TA4KO53Q.js.map +1 -0
- package/dist/{chunk-5RJZ2E7D.js → chunk-WFY2WWZE.js} +2 -6
- package/dist/{chunk-5RJZ2E7D.js.map → chunk-WFY2WWZE.js.map} +1 -1
- package/dist/core-capabilities.d.ts +76 -34
- package/dist/core-capabilities.d.ts.map +1 -1
- package/dist/define-process.d.ts +36 -55
- package/dist/define-process.d.ts.map +1 -1
- package/dist/{extension-api-CqPGRqc9.d.ts → extension-api-CcqZCYJx.d.ts} +160 -74
- package/dist/extension-api.d.ts +120 -5
- package/dist/extension-api.d.ts.map +1 -1
- package/dist/external-action-source.d.ts +7 -0
- package/dist/external-action-source.d.ts.map +1 -0
- package/dist/external-source-poll.d.ts +15 -0
- package/dist/external-source-poll.d.ts.map +1 -0
- package/dist/flow.d.ts +92 -110
- package/dist/flow.d.ts.map +1 -1
- package/dist/fs-utils.d.ts +2 -0
- package/dist/fs-utils.d.ts.map +1 -1
- package/dist/git-binary.js +25 -33
- package/dist/git-binary.js.map +1 -1
- package/dist/index.d.ts +21 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4694 -29
- package/dist/index.js.map +1 -1
- package/dist/integration-http.d.ts +21 -0
- package/dist/integration-http.d.ts.map +1 -0
- package/dist/json-data.d.ts +3 -0
- package/dist/json-data.d.ts.map +1 -0
- package/dist/leaf-outcome-renderer.js +74 -64
- package/dist/leaf-outcome-renderer.js.map +1 -1
- package/dist/leaf-outcomes.d.ts.map +1 -1
- package/dist/model-provider.d.ts.map +1 -1
- package/dist/pi-config.d.ts +1 -5
- package/dist/pi-config.d.ts.map +1 -1
- package/dist/pi-config.js +20 -180
- package/dist/pi-config.js.map +1 -1
- package/dist/process-flow-view.d.ts.map +1 -1
- package/dist/process-graph.d.ts +1 -2
- package/dist/process-graph.d.ts.map +1 -1
- package/dist/process-watcher-source.d.ts +3 -1
- package/dist/process-watcher-source.d.ts.map +1 -1
- package/dist/project-binding.d.ts +9 -0
- package/dist/project-binding.d.ts.map +1 -0
- package/dist/registry-utils.d.ts.map +1 -1
- package/dist/repository-feedback.d.ts +29 -0
- package/dist/repository-feedback.d.ts.map +1 -0
- package/dist/repository-git-env.d.ts +2 -0
- package/dist/repository-git-env.d.ts.map +1 -1
- package/dist/repository-http.d.ts +19 -0
- package/dist/repository-http.d.ts.map +1 -0
- package/dist/repository-https.d.ts +3 -0
- package/dist/repository-https.d.ts.map +1 -0
- package/dist/repository-issue-watcher.d.ts +31 -0
- package/dist/repository-issue-watcher.d.ts.map +1 -0
- package/dist/repository-source-config.d.ts +32 -0
- package/dist/repository-source-config.d.ts.map +1 -0
- package/dist/repository-types.d.ts +36 -0
- package/dist/repository-types.d.ts.map +1 -0
- package/dist/review-flow.js +12 -43
- package/dist/review-flow.js.map +1 -1
- package/dist/runtime-internals.js +18 -2
- package/dist/runtime-internals.js.map +1 -1
- package/dist/state-helpers.d.ts +3 -2
- package/dist/state-helpers.d.ts.map +1 -1
- package/dist/subprocess-env.d.ts +1 -0
- package/dist/subprocess-env.d.ts.map +1 -1
- package/dist/tool-arguments.d.ts +7 -0
- package/dist/tool-arguments.d.ts.map +1 -0
- package/dist/turn-semantics.d.ts +5 -6
- package/dist/turn-semantics.d.ts.map +1 -1
- package/dist/types.d.ts +5 -15
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/core-capabilities.ts +90 -37
- package/src/define-process.ts +231 -277
- package/src/extension-api.ts +148 -4
- package/src/external-action-source.ts +12 -0
- package/src/external-source-poll.ts +49 -0
- package/src/flow.ts +427 -646
- package/src/fs-utils.ts +17 -2
- package/src/index.ts +56 -13
- package/src/integration-http.ts +58 -0
- package/src/json-data.ts +25 -0
- package/src/leaf-outcomes.ts +1 -9
- package/src/model-provider.ts +10 -23
- package/src/pi-config.ts +1 -6
- package/src/process-flow-view.ts +32 -88
- package/src/process-graph.ts +25 -54
- package/src/process-watcher-source.ts +13 -8
- package/src/project-binding.ts +36 -0
- package/src/registry-utils.ts +1 -5
- package/src/repository-feedback.ts +59 -0
- package/src/repository-git-env.ts +34 -0
- package/src/repository-http.ts +71 -0
- package/src/repository-https.ts +22 -0
- package/src/repository-issue-watcher.ts +92 -0
- package/src/repository-source-config.ts +60 -0
- package/src/repository-types.ts +25 -0
- package/src/state-helpers.ts +8 -9
- package/src/subprocess-env.ts +6 -0
- package/src/tool-arguments.ts +37 -0
- package/src/turn-semantics.ts +31 -75
- package/src/types.ts +5 -15
- package/dist/browser-ui-extension.js +0 -2
- package/dist/browser-ui-extension.js.map +0 -1
- package/dist/capabilities.js +0 -7
- package/dist/capabilities.js.map +0 -1
- package/dist/chunk-TRQJ5PRM.js.map +0 -1
- package/dist/codecs.js +0 -9
- package/dist/codecs.js.map +0 -1
- package/dist/commit-message.js +0 -3
- package/dist/commit-message.js.map +0 -1
- package/dist/core-capabilities.js +0 -6
- package/dist/core-capabilities.js.map +0 -1
- package/dist/define-process.js +0 -1040
- package/dist/define-process.js.map +0 -1
- package/dist/event-bus.js +0 -31
- package/dist/event-bus.js.map +0 -1
- package/dist/extension-api.js +0 -5
- package/dist/extension-api.js.map +0 -1
- package/dist/flow.js +0 -1565
- package/dist/flow.js.map +0 -1
- package/dist/form-contract.js +0 -2
- package/dist/form-contract.js.map +0 -1
- package/dist/fs-utils.js +0 -23
- package/dist/fs-utils.js.map +0 -1
- package/dist/graph-fragments.js +0 -57
- package/dist/graph-fragments.js.map +0 -1
- package/dist/host-capabilities.js +0 -42
- package/dist/host-capabilities.js.map +0 -1
- package/dist/launcher-contract.js +0 -2
- package/dist/launcher-contract.js.map +0 -1
- package/dist/leaf-outcomes.js +0 -52
- package/dist/leaf-outcomes.js.map +0 -1
- package/dist/model-provider.js +0 -171
- package/dist/model-provider.js.map +0 -1
- package/dist/process-definition-internals.js +0 -25
- package/dist/process-definition-internals.js.map +0 -1
- package/dist/process-flow-view.js +0 -418
- package/dist/process-flow-view.js.map +0 -1
- package/dist/process-graph.js +0 -213
- package/dist/process-graph.js.map +0 -1
- package/dist/process-launcher-builder.js +0 -43
- package/dist/process-launcher-builder.js.map +0 -1
- package/dist/process-watcher-builder.js +0 -40
- package/dist/process-watcher-builder.js.map +0 -1
- package/dist/process-watcher-source.js +0 -45
- package/dist/process-watcher-source.js.map +0 -1
- package/dist/registry-utils.js +0 -18
- package/dist/registry-utils.js.map +0 -1
- package/dist/repository-git-env.js +0 -26
- package/dist/repository-git-env.js.map +0 -1
- package/dist/server-events.js +0 -2
- package/dist/server-events.js.map +0 -1
- package/dist/server-process-builder.js +0 -52
- package/dist/server-process-builder.js.map +0 -1
- package/dist/state-helpers.js +0 -20
- package/dist/state-helpers.js.map +0 -1
- package/dist/subprocess-env.js +0 -34
- package/dist/subprocess-env.js.map +0 -1
- package/dist/tool-renderers.js +0 -63
- package/dist/tool-renderers.js.map +0 -1
- package/dist/turn-semantics.js +0 -365
- package/dist/turn-semantics.js.map +0 -1
- package/dist/types.js +0 -23
- package/dist/types.js.map +0 -1
- package/dist/ui-process-builder.js +0 -30
- package/dist/ui-process-builder.js.map +0 -1
- package/dist/worker-process-builder.js +0 -22
- package/dist/worker-process-builder.js.map +0 -1
package/dist/define-process.js
DELETED
|
@@ -1,1040 +0,0 @@
|
|
|
1
|
-
import { assertValidProcessProductName, } from "@leitwerk-dev/domain";
|
|
2
|
-
import { getProcessTurnTransitions, markDefinedProcess, setProcessTurnTransitions, } from "./process-definition-internals.js";
|
|
3
|
-
function createTransitionTargetError(context, detail) {
|
|
4
|
-
return new Error(`${context} ${detail}`);
|
|
5
|
-
}
|
|
6
|
-
function createProcessTurnBinding(definition, transitions) {
|
|
7
|
-
const binding = { definition };
|
|
8
|
-
if (transitions && transitions.length > 0) {
|
|
9
|
-
setProcessTurnTransitions(binding, transitions);
|
|
10
|
-
}
|
|
11
|
-
return binding;
|
|
12
|
-
}
|
|
13
|
-
function normalizeStaticRouteTarget(context, target, knownTurnIds) {
|
|
14
|
-
const declaredTargetCount = (target.to !== undefined ? 1 : 0) +
|
|
15
|
-
(target.complete === true ? 1 : 0) +
|
|
16
|
-
(target.lifecycleStatus !== undefined ? 1 : 0);
|
|
17
|
-
if (declaredTargetCount !== 1) {
|
|
18
|
-
throw createTransitionTargetError(context, "must declare exactly one target via 'to', 'complete', or 'lifecycleStatus'");
|
|
19
|
-
}
|
|
20
|
-
if (target.to !== undefined) {
|
|
21
|
-
if (knownTurnIds && !knownTurnIds.has(target.to)) {
|
|
22
|
-
throw createTransitionTargetError(context, `references unknown turn '${target.to}'`);
|
|
23
|
-
}
|
|
24
|
-
return { nextTurnId: target.to };
|
|
25
|
-
}
|
|
26
|
-
if (target.complete === true) {
|
|
27
|
-
return { lifecycleStatus: "completed" };
|
|
28
|
-
}
|
|
29
|
-
return { lifecycleStatus: target.lifecycleStatus };
|
|
30
|
-
}
|
|
31
|
-
function buildOutcomeTransition(turnId, outcome, target, knownTurnIds) {
|
|
32
|
-
if (!hasDeclaredStaticRouteTarget(target)) {
|
|
33
|
-
return { nextTurnId: turnId };
|
|
34
|
-
}
|
|
35
|
-
return normalizeStaticRouteTarget(`Turn '${turnId}' outcome '${outcome}'`, target, knownTurnIds);
|
|
36
|
-
}
|
|
37
|
-
function resolveActionRouting(input) {
|
|
38
|
-
if ("branches" in input.spec) {
|
|
39
|
-
const entries = Object.entries(input.spec.branches);
|
|
40
|
-
if (entries.length === 0) {
|
|
41
|
-
throw new Error(`Action '${input.actionId}' on turn '${input.turnId}' must declare at least one branch`);
|
|
42
|
-
}
|
|
43
|
-
const routes = {};
|
|
44
|
-
const resolvedTransitions = [];
|
|
45
|
-
const usedTriggers = new Set();
|
|
46
|
-
for (const [branchId, branchSpec] of entries) {
|
|
47
|
-
if (branchId.trim() === "") {
|
|
48
|
-
throw new Error(`Action '${input.actionId}' on turn '${input.turnId}' contains an empty branch id`);
|
|
49
|
-
}
|
|
50
|
-
const target = normalizeStaticRouteTarget(`Action '${input.actionId}' branch '${branchId}' on turn '${input.turnId}'`, branchSpec, input.knownTurnIds);
|
|
51
|
-
const trigger = branchSpec.trigger ?? branchId;
|
|
52
|
-
if (usedTriggers.has(trigger)) {
|
|
53
|
-
throw new Error(`Action '${input.actionId}' on turn '${input.turnId}' contains duplicate branch trigger '${trigger}'`);
|
|
54
|
-
}
|
|
55
|
-
usedTriggers.add(trigger);
|
|
56
|
-
const normalized = {
|
|
57
|
-
...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
|
|
58
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
59
|
-
trigger,
|
|
60
|
-
};
|
|
61
|
-
routes[branchId] = normalized;
|
|
62
|
-
resolvedTransitions.push(normalized);
|
|
63
|
-
}
|
|
64
|
-
return {
|
|
65
|
-
routing: {
|
|
66
|
-
kind: "branches",
|
|
67
|
-
routes,
|
|
68
|
-
choose: input.spec.choose,
|
|
69
|
-
},
|
|
70
|
-
transitions: resolvedTransitions,
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
const target = normalizeStaticRouteTarget(`Action '${input.actionId}' on turn '${input.turnId}'`, input.spec, input.knownTurnIds);
|
|
74
|
-
const route = {
|
|
75
|
-
...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
|
|
76
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
77
|
-
trigger: input.spec.trigger ?? input.actionId,
|
|
78
|
-
};
|
|
79
|
-
return {
|
|
80
|
-
routing: {
|
|
81
|
-
kind: "static",
|
|
82
|
-
route,
|
|
83
|
-
},
|
|
84
|
-
transitions: [route],
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
function externalSourceTransitionId(input) {
|
|
88
|
-
return `${input.turnId}:${input.source.kind}:${input.index}`;
|
|
89
|
-
}
|
|
90
|
-
function externalActionArmingId(input) {
|
|
91
|
-
return `${input.turnId}:${input.externalActionId}`;
|
|
92
|
-
}
|
|
93
|
-
function externalActionTransitionTrigger(input) {
|
|
94
|
-
return `external:${input.externalActionId}`;
|
|
95
|
-
}
|
|
96
|
-
function compileExternalSourceTransitions(input) {
|
|
97
|
-
if (input.spec.transitions.length === 0) {
|
|
98
|
-
throw new Error(`External turn '${input.turnId}' must declare at least one source transition`);
|
|
99
|
-
}
|
|
100
|
-
return input.spec.transitions.map((transition, index) => {
|
|
101
|
-
if (transition.source.kind.trim() === "") {
|
|
102
|
-
throw new Error(`External turn '${input.turnId}' source transition ${index} has an empty kind`);
|
|
103
|
-
}
|
|
104
|
-
const target = normalizeStaticRouteTarget(`External turn '${input.turnId}' source '${transition.source.kind}'`, transition, input.knownTurnIds);
|
|
105
|
-
return {
|
|
106
|
-
...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
|
|
107
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
108
|
-
trigger: externalSourceTransitionId({
|
|
109
|
-
turnId: input.turnId,
|
|
110
|
-
source: transition.source,
|
|
111
|
-
index,
|
|
112
|
-
}),
|
|
113
|
-
};
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
export function getExternalSourceTransitionId(input) {
|
|
117
|
-
return externalSourceTransitionId(input);
|
|
118
|
-
}
|
|
119
|
-
export function getExternalActionArmingId(input) {
|
|
120
|
-
return externalActionArmingId(input);
|
|
121
|
-
}
|
|
122
|
-
export function getExternalActionTransitionTrigger(input) {
|
|
123
|
-
return externalActionTransitionTrigger(input);
|
|
124
|
-
}
|
|
125
|
-
function previewMatchesTransition(preview, transition) {
|
|
126
|
-
if (preview.kind === "trigger") {
|
|
127
|
-
return transition.trigger === preview.trigger;
|
|
128
|
-
}
|
|
129
|
-
if (preview.kind === "terminal") {
|
|
130
|
-
return transition.lifecycleStatus === preview.lifecycleStatus;
|
|
131
|
-
}
|
|
132
|
-
return transition.nextTurnId === preview.turnId;
|
|
133
|
-
}
|
|
134
|
-
function validateActionPreviewMatchesTransitions(input) {
|
|
135
|
-
const matches = input.transitions.filter((transition) => previewMatchesTransition(input.preview, transition));
|
|
136
|
-
if (matches.length === 1) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
const previewLabel = input.preview.kind === "trigger"
|
|
140
|
-
? `trigger '${input.preview.trigger}'`
|
|
141
|
-
: input.preview.kind === "terminal"
|
|
142
|
-
? `terminal '${input.preview.lifecycleStatus}'`
|
|
143
|
-
: `fixed turn '${String(input.preview.turnId)}'`;
|
|
144
|
-
throw new Error(`Action '${input.actionId}' on turn '${input.turnId}' ${input.context} does not match a compiled transition for ${previewLabel}`);
|
|
145
|
-
}
|
|
146
|
-
function buildActionPreviewAndScheduling(turnId, actionId, spec, transitions) {
|
|
147
|
-
const preview = (() => {
|
|
148
|
-
if (spec.preview) {
|
|
149
|
-
return spec.preview;
|
|
150
|
-
}
|
|
151
|
-
if (transitions.length !== 1) {
|
|
152
|
-
return undefined;
|
|
153
|
-
}
|
|
154
|
-
const [transition] = transitions;
|
|
155
|
-
if (!transition) {
|
|
156
|
-
return undefined;
|
|
157
|
-
}
|
|
158
|
-
if (transition.lifecycleStatus !== undefined) {
|
|
159
|
-
return {
|
|
160
|
-
kind: "terminal",
|
|
161
|
-
lifecycleStatus: transition.lifecycleStatus,
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
return {
|
|
165
|
-
kind: "trigger",
|
|
166
|
-
trigger: transition.trigger,
|
|
167
|
-
};
|
|
168
|
-
})();
|
|
169
|
-
if (spec.preview && preview) {
|
|
170
|
-
validateActionPreviewMatchesTransitions({
|
|
171
|
-
turnId,
|
|
172
|
-
actionId,
|
|
173
|
-
preview,
|
|
174
|
-
transitions,
|
|
175
|
-
context: "preview",
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
if (spec.schedulable && preview) {
|
|
179
|
-
validateActionPreviewMatchesTransitions({
|
|
180
|
-
turnId,
|
|
181
|
-
actionId,
|
|
182
|
-
preview,
|
|
183
|
-
transitions,
|
|
184
|
-
context: "scheduling",
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
if (spec.schedulable && !preview) {
|
|
188
|
-
throw new Error(`Action '${actionId}' is schedulable but does not resolve to a unique preview transition`);
|
|
189
|
-
}
|
|
190
|
-
return {
|
|
191
|
-
...(preview ? { preview } : {}),
|
|
192
|
-
...(spec.schedulable && preview ? { scheduling: { preview } } : {}),
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
function mergeProcessEffectPlans(left, right) {
|
|
196
|
-
if (!left) {
|
|
197
|
-
return right;
|
|
198
|
-
}
|
|
199
|
-
if (!right) {
|
|
200
|
-
return left;
|
|
201
|
-
}
|
|
202
|
-
return {
|
|
203
|
-
...left,
|
|
204
|
-
...right,
|
|
205
|
-
...(left.processPatch || right.processPatch
|
|
206
|
-
? { processPatch: { ...(left.processPatch ?? {}), ...(right.processPatch ?? {}) } }
|
|
207
|
-
: {}),
|
|
208
|
-
...(left.broadcasts || right.broadcasts
|
|
209
|
-
? { broadcasts: [...(left.broadcasts ?? []), ...(right.broadcasts ?? [])] }
|
|
210
|
-
: {}),
|
|
211
|
-
...(left.emit || right.emit ? { emit: [...(left.emit ?? []), ...(right.emit ?? [])] } : {}),
|
|
212
|
-
...(left.queueInput || right.queueInput
|
|
213
|
-
? { queueInput: [...(left.queueInput ?? []), ...(right.queueInput ?? [])] }
|
|
214
|
-
: {}),
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
function normalizeEffectString(value) {
|
|
218
|
-
return typeof value === "string" ? value.trim() : "";
|
|
219
|
-
}
|
|
220
|
-
function normalizeEffectStringArray(value) {
|
|
221
|
-
return Array.isArray(value)
|
|
222
|
-
? value
|
|
223
|
-
.map((entry) => (typeof entry === "string" ? entry.trim() : ""))
|
|
224
|
-
.filter((entry) => entry !== "")
|
|
225
|
-
: [];
|
|
226
|
-
}
|
|
227
|
-
function getTurnReviewSubject(turnDef) {
|
|
228
|
-
if (!turnDef || !("reviewSubject" in turnDef)) {
|
|
229
|
-
return null;
|
|
230
|
-
}
|
|
231
|
-
return turnDef.reviewSubject ?? null;
|
|
232
|
-
}
|
|
233
|
-
function patchReviewSubjectIntoState(state, reviewSubject) {
|
|
234
|
-
if (typeof state !== "object" || state === null || Array.isArray(state)) {
|
|
235
|
-
return state;
|
|
236
|
-
}
|
|
237
|
-
return {
|
|
238
|
-
...state,
|
|
239
|
-
reviewSubject,
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
async function buildSavePlanResultEffect(input) {
|
|
243
|
-
const statePlan = input.intent.state ? await input.intent.state(input.execution) : undefined;
|
|
244
|
-
const targetReviewSubject = getTurnReviewSubject(input.targetTurn);
|
|
245
|
-
const baseState = statePlan?.state ?? input.execution.ctx.state;
|
|
246
|
-
const planRevision = input.execution.ctx.process.planRevision + 1;
|
|
247
|
-
const summary = normalizeEffectString(input.execution.event.params[input.intent.summaryParam]);
|
|
248
|
-
const acceptanceCriteria = normalizeEffectStringArray(input.execution.event.params[input.intent.acceptanceCriteriaParam]);
|
|
249
|
-
const planMarkdown = typeof input.execution.event.turnResultMarkdown === "string"
|
|
250
|
-
? input.execution.event.turnResultMarkdown
|
|
251
|
-
: normalizeEffectString(input.execution.event.params[input.intent.planMarkdownParam]);
|
|
252
|
-
return mergeProcessEffectPlans(statePlan, {
|
|
253
|
-
state: patchReviewSubjectIntoState(baseState, targetReviewSubject),
|
|
254
|
-
processPatch: { planRevision },
|
|
255
|
-
broadcasts: [
|
|
256
|
-
{
|
|
257
|
-
type: input.intent.broadcastType,
|
|
258
|
-
payload: {
|
|
259
|
-
planRevision,
|
|
260
|
-
reviewState: "awaiting_approval",
|
|
261
|
-
approved: false,
|
|
262
|
-
summary,
|
|
263
|
-
},
|
|
264
|
-
},
|
|
265
|
-
],
|
|
266
|
-
emit: [
|
|
267
|
-
{
|
|
268
|
-
type: input.intent.emitEventType,
|
|
269
|
-
data: {
|
|
270
|
-
planRevision,
|
|
271
|
-
summary,
|
|
272
|
-
planMarkdown,
|
|
273
|
-
acceptanceCriteria,
|
|
274
|
-
},
|
|
275
|
-
},
|
|
276
|
-
],
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
function resolveOutcomeEffect(input) {
|
|
280
|
-
const lifecycleIntent = "lifecycleIntent" in input.spec ? input.spec.lifecycleIntent : undefined;
|
|
281
|
-
if (!lifecycleIntent) {
|
|
282
|
-
return input.spec.effect;
|
|
283
|
-
}
|
|
284
|
-
return async (execution) => {
|
|
285
|
-
const explicitPlan = input.spec.effect ? await input.spec.effect(execution) : undefined;
|
|
286
|
-
let lifecyclePlan;
|
|
287
|
-
switch (lifecycleIntent.kind) {
|
|
288
|
-
case "save_plan_result":
|
|
289
|
-
lifecyclePlan = await buildSavePlanResultEffect({
|
|
290
|
-
intent: lifecycleIntent,
|
|
291
|
-
execution,
|
|
292
|
-
targetTurn: input.targetTurn,
|
|
293
|
-
});
|
|
294
|
-
break;
|
|
295
|
-
}
|
|
296
|
-
return mergeProcessEffectPlans(explicitPlan, lifecyclePlan);
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
function areFormsEquivalent(left, right) {
|
|
300
|
-
if (left === right) {
|
|
301
|
-
return true;
|
|
302
|
-
}
|
|
303
|
-
return JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
|
|
304
|
-
}
|
|
305
|
-
function cloneStateForFormPatch(state) {
|
|
306
|
-
if (typeof state !== "object" || state === null) {
|
|
307
|
-
throw new Error("Declarative form state fields require object process state");
|
|
308
|
-
}
|
|
309
|
-
return structuredClone(state);
|
|
310
|
-
}
|
|
311
|
-
function normalizeFormStateValue(field, value) {
|
|
312
|
-
if (field.kind === "number") {
|
|
313
|
-
const parsed = typeof value === "number" ? value : Number(String(value).trim());
|
|
314
|
-
if (!Number.isFinite(parsed)) {
|
|
315
|
-
throw new Error(`Field '${field.id}' must be a number`);
|
|
316
|
-
}
|
|
317
|
-
return parsed;
|
|
318
|
-
}
|
|
319
|
-
return value;
|
|
320
|
-
}
|
|
321
|
-
function applyFormStateFields(input) {
|
|
322
|
-
const stateFields = (input.form?.fields ?? []).filter((field) => field.state);
|
|
323
|
-
if (stateFields.length === 0) {
|
|
324
|
-
return undefined;
|
|
325
|
-
}
|
|
326
|
-
const nextState = cloneStateForFormPatch(input.state);
|
|
327
|
-
for (const field of stateFields) {
|
|
328
|
-
const rawValue = input.inputValue[field.id];
|
|
329
|
-
if (rawValue === undefined) {
|
|
330
|
-
continue;
|
|
331
|
-
}
|
|
332
|
-
const path = field.state?.path.trim();
|
|
333
|
-
if (!path) {
|
|
334
|
-
throw new Error(`Form field '${field.id}' declares an empty state path`);
|
|
335
|
-
}
|
|
336
|
-
const segments = path.split(".").map((segment) => segment.trim());
|
|
337
|
-
if (segments.some((segment) => segment === "")) {
|
|
338
|
-
throw new Error(`Form field '${field.id}' declares invalid state path '${path}'`);
|
|
339
|
-
}
|
|
340
|
-
let target = nextState;
|
|
341
|
-
for (const segment of segments.slice(0, -1)) {
|
|
342
|
-
const existing = target[segment];
|
|
343
|
-
if (typeof existing !== "object" || existing === null || Array.isArray(existing)) {
|
|
344
|
-
target[segment] = {};
|
|
345
|
-
}
|
|
346
|
-
target = target[segment];
|
|
347
|
-
}
|
|
348
|
-
target[segments[segments.length - 1]] = normalizeFormStateValue(field, rawValue);
|
|
349
|
-
}
|
|
350
|
-
return nextState;
|
|
351
|
-
}
|
|
352
|
-
function makeProcessServerRuntimeContext(ctx) {
|
|
353
|
-
return {
|
|
354
|
-
process: ctx.process,
|
|
355
|
-
projects: ctx.projects,
|
|
356
|
-
params: ctx.params,
|
|
357
|
-
state: ctx.state,
|
|
358
|
-
readSemanticTurnResultMarkdown: (ref) => ctx.readSemanticTurnResultMarkdown(ref),
|
|
359
|
-
readProductTurnResultMarkdown: (productName) => ctx.readProductTurnResultMarkdown(productName),
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
function mergeTransitionWithState(input) {
|
|
363
|
-
const next = {
|
|
364
|
-
...(input.transition ?? {}),
|
|
365
|
-
...(input.result?.state !== undefined ? { state: input.result.state } : {}),
|
|
366
|
-
};
|
|
367
|
-
const hasFields = next.turnId !== undefined ||
|
|
368
|
-
next.lifecycleStatus !== undefined ||
|
|
369
|
-
next.trigger !== undefined ||
|
|
370
|
-
next.effect !== undefined ||
|
|
371
|
-
next.state !== undefined;
|
|
372
|
-
return hasFields ? next : null;
|
|
373
|
-
}
|
|
374
|
-
function hasLifecycleEffects(result) {
|
|
375
|
-
return Boolean(result?.processPatch !== undefined ||
|
|
376
|
-
(result?.broadcasts !== undefined && result.broadcasts.length > 0));
|
|
377
|
-
}
|
|
378
|
-
async function applyProcessEffectPlan(input) {
|
|
379
|
-
const transition = mergeTransitionWithState({
|
|
380
|
-
result: input.result,
|
|
381
|
-
transition: input.transition,
|
|
382
|
-
});
|
|
383
|
-
if (transition) {
|
|
384
|
-
await input.ctx.transition(transition);
|
|
385
|
-
}
|
|
386
|
-
if (hasLifecycleEffects(input.result)) {
|
|
387
|
-
if (!input.ctx.applyLifecycleEffects) {
|
|
388
|
-
throw new Error("Process lifecycle effects are not supported by this server context");
|
|
389
|
-
}
|
|
390
|
-
input.ctx.applyLifecycleEffects({
|
|
391
|
-
...(input.result.processPatch ? { processPatch: input.result.processPatch } : {}),
|
|
392
|
-
...(input.result.broadcasts ? { broadcasts: input.result.broadcasts } : {}),
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
for (const emitted of input.result?.emit ?? []) {
|
|
396
|
-
input.ctx.emitEvent(emitted.type, emitted.data);
|
|
397
|
-
}
|
|
398
|
-
for (const queuedInput of input.result?.queueInput ?? []) {
|
|
399
|
-
input.ctx.queueInput(queuedInput);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
function compileTurnOutcomeDefinitions(input) {
|
|
403
|
-
const outcomeEntries = Object.entries(input.outcomes ?? {});
|
|
404
|
-
if (outcomeEntries.length > 0 && input.turnEnd) {
|
|
405
|
-
throw new Error(`Turn '${input.turnId}' cannot declare both tool outcomes and a turnEnd result`);
|
|
406
|
-
}
|
|
407
|
-
if (outcomeEntries.length === 0 && !input.turnEnd) {
|
|
408
|
-
throw new Error(`Turn '${input.turnId}' must declare at least one tool outcome or a turnEnd result`);
|
|
409
|
-
}
|
|
410
|
-
const transitions = [];
|
|
411
|
-
const effects = new Map();
|
|
412
|
-
for (const [outcome, spec] of outcomeEntries) {
|
|
413
|
-
if (outcome.trim() === "") {
|
|
414
|
-
throw new Error(`Turn '${input.turnId}' declares an empty outcome id`);
|
|
415
|
-
}
|
|
416
|
-
const target = buildOutcomeTransition(input.turnId, outcome, spec, input.knownTurnIds);
|
|
417
|
-
transitions.push({
|
|
418
|
-
...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
|
|
419
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
420
|
-
outcome,
|
|
421
|
-
});
|
|
422
|
-
effects.set(outcome, resolveOutcomeEffect({
|
|
423
|
-
spec,
|
|
424
|
-
targetTurn: target.nextTurnId
|
|
425
|
-
? input.turnDefinitionsById.get(target.nextTurnId)
|
|
426
|
-
: undefined,
|
|
427
|
-
}));
|
|
428
|
-
}
|
|
429
|
-
if (input.turnEnd) {
|
|
430
|
-
if (input.turnEnd.outcome.trim() === "") {
|
|
431
|
-
throw new Error(`Turn '${input.turnId}' declares an empty turnEnd outcome id`);
|
|
432
|
-
}
|
|
433
|
-
const target = buildOutcomeTransition(input.turnId, input.turnEnd.outcome, input.turnEnd, input.knownTurnIds);
|
|
434
|
-
transitions.push({
|
|
435
|
-
...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
|
|
436
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
437
|
-
outcome: input.turnEnd.outcome,
|
|
438
|
-
});
|
|
439
|
-
effects.set(input.turnEnd.outcome, resolveOutcomeEffect({
|
|
440
|
-
spec: input.turnEnd,
|
|
441
|
-
targetTurn: target.nextTurnId
|
|
442
|
-
? input.turnDefinitionsById.get(target.nextTurnId)
|
|
443
|
-
: undefined,
|
|
444
|
-
}));
|
|
445
|
-
}
|
|
446
|
-
return {
|
|
447
|
-
transitions,
|
|
448
|
-
effects,
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
function addActionUse(input) {
|
|
452
|
-
const existing = input.actionUses.get(input.actionId) ?? [];
|
|
453
|
-
if (existing.some((candidate) => candidate.turnId === input.use.turnId)) {
|
|
454
|
-
throw new Error(`Turn '${input.use.turnId}' declares action '${input.actionId}' more than once`);
|
|
455
|
-
}
|
|
456
|
-
existing.push(input.use);
|
|
457
|
-
input.actionUses.set(input.actionId, existing);
|
|
458
|
-
}
|
|
459
|
-
function hasDeclaredStaticRouteTarget(target) {
|
|
460
|
-
return (target.to !== undefined || target.complete === true || target.lifecycleStatus !== undefined);
|
|
461
|
-
}
|
|
462
|
-
function resolveExternalActionRoute(input) {
|
|
463
|
-
if (input.externalActionId.trim() === "") {
|
|
464
|
-
throw new Error(`Human turn '${input.turnId}' declares an empty external action id`);
|
|
465
|
-
}
|
|
466
|
-
if (input.spec.id !== input.externalActionId) {
|
|
467
|
-
throw new Error(`Human turn '${input.turnId}' external action '${input.externalActionId}' has mismatched spec id '${input.spec.id}'`);
|
|
468
|
-
}
|
|
469
|
-
if (input.spec.source.kind.trim() === "") {
|
|
470
|
-
throw new Error(`Human turn '${input.turnId}' external action '${input.externalActionId}' must declare a non-empty source kind`);
|
|
471
|
-
}
|
|
472
|
-
const target = normalizeStaticRouteTarget(`Human turn '${input.turnId}' external action '${input.externalActionId}'`, input.spec, input.knownTurnIds);
|
|
473
|
-
return {
|
|
474
|
-
...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
|
|
475
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
476
|
-
trigger: externalActionTransitionTrigger({ externalActionId: input.externalActionId }),
|
|
477
|
-
};
|
|
478
|
-
}
|
|
479
|
-
function validateExternalActionPublishedInputTarget(input) {
|
|
480
|
-
const publication = input.spec.publishInput;
|
|
481
|
-
if (!publication || !input.turnDefinitionsById) {
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
if (input.spec.to === undefined) {
|
|
485
|
-
throw new Error(`Human turn '${input.turnId}' external action '${input.externalActionId}' publishes input '${publication.productName}' but does not route to a consuming turn`);
|
|
486
|
-
}
|
|
487
|
-
const targetTurn = input.turnDefinitionsById.get(input.spec.to);
|
|
488
|
-
if (!targetTurn || targetTurn.kind !== "llm") {
|
|
489
|
-
throw new Error(`Human turn '${input.turnId}' external action '${input.externalActionId}' publishes input '${publication.productName}' but target turn '${input.spec.to}' is not an LLM turn`);
|
|
490
|
-
}
|
|
491
|
-
const consumed = new Set([
|
|
492
|
-
...(targetTurn.consumedProducts ?? []),
|
|
493
|
-
...(targetTurn.optionalConsumedProducts ?? []),
|
|
494
|
-
]);
|
|
495
|
-
if (!consumed.has(publication.productName)) {
|
|
496
|
-
throw new Error(`Human turn '${input.turnId}' external action '${input.externalActionId}' publishes input '${publication.productName}' but target turn '${input.spec.to}' does not consume it`);
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
function validateActionPrimaryPrompt(turnId, actionId, action) {
|
|
500
|
-
const fields = action.form?.fields ?? [];
|
|
501
|
-
const textualFields = fields.filter((field) => field.kind === "text" || field.kind === "textarea");
|
|
502
|
-
const primaryPrompts = fields.filter((field) => field.primaryPrompt);
|
|
503
|
-
if (primaryPrompts.some((field) => field.kind !== "text" && field.kind !== "textarea")) {
|
|
504
|
-
throw new Error(`Human turn '${turnId}' action '${actionId}' primary prompt must be a text or textarea field`);
|
|
505
|
-
}
|
|
506
|
-
if (primaryPrompts.length > 1) {
|
|
507
|
-
throw new Error(`Human turn '${turnId}' action '${actionId}' must declare at most one primary prompt`);
|
|
508
|
-
}
|
|
509
|
-
if (textualFields.length > 0 && primaryPrompts.length === 0) {
|
|
510
|
-
throw new Error(`Human turn '${turnId}' action '${actionId}' with textual fields must declare a primary prompt`);
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
function deriveHumanTurnActions(input) {
|
|
514
|
-
const actionEntries = Object.entries(input.spec.actions);
|
|
515
|
-
if (actionEntries.length === 0 && input.knownTurnIds) {
|
|
516
|
-
throw new Error(`Human turn '${input.turnId}' must declare at least one action`);
|
|
517
|
-
}
|
|
518
|
-
const derivedActions = [];
|
|
519
|
-
const transitions = [];
|
|
520
|
-
const actionIdByTrigger = new Map();
|
|
521
|
-
for (const [actionId, actionSpec] of actionEntries) {
|
|
522
|
-
if (actionId.trim() === "") {
|
|
523
|
-
throw new Error(`Human turn '${input.turnId}' declares an empty action id`);
|
|
524
|
-
}
|
|
525
|
-
validateActionPrimaryPrompt(input.turnId, actionId, actionSpec);
|
|
526
|
-
const resolved = !input.knownTurnIds &&
|
|
527
|
-
!("branches" in actionSpec) &&
|
|
528
|
-
!hasDeclaredStaticRouteTarget(actionSpec)
|
|
529
|
-
? {
|
|
530
|
-
routing: {
|
|
531
|
-
kind: "static",
|
|
532
|
-
route: { trigger: actionSpec.trigger ?? actionId },
|
|
533
|
-
},
|
|
534
|
-
transitions: [],
|
|
535
|
-
}
|
|
536
|
-
: resolveActionRouting({
|
|
537
|
-
turnId: input.turnId,
|
|
538
|
-
actionId,
|
|
539
|
-
spec: actionSpec,
|
|
540
|
-
knownTurnIds: input.knownTurnIds,
|
|
541
|
-
});
|
|
542
|
-
for (const transition of resolved.transitions) {
|
|
543
|
-
const existingActionId = actionIdByTrigger.get(transition.trigger);
|
|
544
|
-
if (existingActionId && existingActionId !== actionId) {
|
|
545
|
-
throw new Error(`Human turn '${input.turnId}' contains duplicate action trigger '${transition.trigger}' across actions '${existingActionId}' and '${actionId}'`);
|
|
546
|
-
}
|
|
547
|
-
actionIdByTrigger.set(transition.trigger, actionId);
|
|
548
|
-
transitions.push({
|
|
549
|
-
...(transition.nextTurnId ? { nextTurnId: transition.nextTurnId } : {}),
|
|
550
|
-
...(transition.lifecycleStatus ? { lifecycleStatus: transition.lifecycleStatus } : {}),
|
|
551
|
-
trigger: transition.trigger,
|
|
552
|
-
});
|
|
553
|
-
}
|
|
554
|
-
const previewAndScheduling = !input.knownTurnIds && resolved.transitions.length === 0
|
|
555
|
-
? {
|
|
556
|
-
...(actionSpec.preview ? { preview: actionSpec.preview } : {}),
|
|
557
|
-
...(actionSpec.schedulable && actionSpec.preview
|
|
558
|
-
? { scheduling: { preview: actionSpec.preview } }
|
|
559
|
-
: {}),
|
|
560
|
-
}
|
|
561
|
-
: buildActionPreviewAndScheduling(input.turnId, actionId, actionSpec, resolved.transitions);
|
|
562
|
-
derivedActions.push({
|
|
563
|
-
actionId,
|
|
564
|
-
actionSpec,
|
|
565
|
-
routing: resolved.routing,
|
|
566
|
-
transitions: resolved.transitions,
|
|
567
|
-
actionView: {
|
|
568
|
-
actionId,
|
|
569
|
-
label: actionSpec.label,
|
|
570
|
-
description: actionSpec.description,
|
|
571
|
-
acceptanceState: actionSpec.acceptanceState,
|
|
572
|
-
...previewAndScheduling,
|
|
573
|
-
},
|
|
574
|
-
externalTriggers: actionSpec.externalTriggers?.map((trigger) => ({
|
|
575
|
-
id: trigger.id,
|
|
576
|
-
actionId,
|
|
577
|
-
label: trigger.label,
|
|
578
|
-
description: trigger.description,
|
|
579
|
-
})) ?? [],
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
const externalActionEntries = Object.entries(input.spec.externalActions ?? {});
|
|
583
|
-
const derivedExternalActions = [];
|
|
584
|
-
const externalActionIds = new Set();
|
|
585
|
-
for (const [externalActionId, actionSpec] of externalActionEntries) {
|
|
586
|
-
if (externalActionIds.has(externalActionId)) {
|
|
587
|
-
throw new Error(`Human turn '${input.turnId}' declares duplicate external action '${externalActionId}'`);
|
|
588
|
-
}
|
|
589
|
-
externalActionIds.add(externalActionId);
|
|
590
|
-
if (actionSpec.publishInput) {
|
|
591
|
-
assertValidProcessProductName(actionSpec.publishInput.productName);
|
|
592
|
-
if (actionSpec.publishInput.inputField.trim() === "") {
|
|
593
|
-
throw new Error(`Human turn '${input.turnId}' external action '${externalActionId}' publishInput must declare a non-empty inputField`);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
validateExternalActionPublishedInputTarget({
|
|
597
|
-
turnId: input.turnId,
|
|
598
|
-
externalActionId,
|
|
599
|
-
spec: actionSpec,
|
|
600
|
-
turnDefinitionsById: input.turnDefinitionsById,
|
|
601
|
-
});
|
|
602
|
-
const transition = resolveExternalActionRoute({
|
|
603
|
-
turnId: input.turnId,
|
|
604
|
-
externalActionId,
|
|
605
|
-
spec: actionSpec,
|
|
606
|
-
knownTurnIds: input.knownTurnIds,
|
|
607
|
-
});
|
|
608
|
-
const existingActionId = actionIdByTrigger.get(transition.trigger);
|
|
609
|
-
if (existingActionId) {
|
|
610
|
-
throw new Error(`Human turn '${input.turnId}' contains duplicate trigger '${transition.trigger}' across action '${existingActionId}' and external action '${externalActionId}'`);
|
|
611
|
-
}
|
|
612
|
-
actionIdByTrigger.set(transition.trigger, externalActionId);
|
|
613
|
-
transitions.push({
|
|
614
|
-
...(transition.nextTurnId ? { nextTurnId: transition.nextTurnId } : {}),
|
|
615
|
-
...(transition.lifecycleStatus ? { lifecycleStatus: transition.lifecycleStatus } : {}),
|
|
616
|
-
trigger: transition.trigger,
|
|
617
|
-
});
|
|
618
|
-
derivedExternalActions.push({
|
|
619
|
-
externalActionId,
|
|
620
|
-
actionSpec,
|
|
621
|
-
transition,
|
|
622
|
-
view: {
|
|
623
|
-
id: externalActionArmingId({ turnId: input.turnId, externalActionId }),
|
|
624
|
-
externalActionId,
|
|
625
|
-
sourceKind: actionSpec.source.kind,
|
|
626
|
-
label: actionSpec.label ?? actionSpec.source.label ?? null,
|
|
627
|
-
description: actionSpec.description ?? actionSpec.source.description ?? null,
|
|
628
|
-
},
|
|
629
|
-
});
|
|
630
|
-
}
|
|
631
|
-
return {
|
|
632
|
-
actions: derivedActions,
|
|
633
|
-
externalActions: derivedExternalActions,
|
|
634
|
-
transitions,
|
|
635
|
-
};
|
|
636
|
-
}
|
|
637
|
-
export function resolveHumanTurnView(input) {
|
|
638
|
-
const derived = deriveHumanTurnActions({
|
|
639
|
-
turnId: input.turnId,
|
|
640
|
-
spec: input.turn,
|
|
641
|
-
});
|
|
642
|
-
return {
|
|
643
|
-
actions: derived.actions.map((action) => action.actionView),
|
|
644
|
-
externalTriggers: derived.actions.flatMap((action) => action.externalTriggers),
|
|
645
|
-
externalActions: derived.externalActions.map((action) => action.view),
|
|
646
|
-
};
|
|
647
|
-
}
|
|
648
|
-
function compileHumanTurn(input) {
|
|
649
|
-
const derived = deriveHumanTurnActions({
|
|
650
|
-
turnId: input.turnId,
|
|
651
|
-
spec: input.spec,
|
|
652
|
-
knownTurnIds: input.knownTurnIds,
|
|
653
|
-
turnDefinitionsById: input.turnDefinitionsById,
|
|
654
|
-
});
|
|
655
|
-
for (const action of derived.actions) {
|
|
656
|
-
addActionUse({
|
|
657
|
-
actionUses: input.actionUses,
|
|
658
|
-
actionId: action.actionId,
|
|
659
|
-
use: {
|
|
660
|
-
turnId: input.turnId,
|
|
661
|
-
label: action.actionSpec.label,
|
|
662
|
-
...(action.actionSpec.form ? { form: action.actionSpec.form } : {}),
|
|
663
|
-
...(action.actionSpec.effect ? { effect: action.actionSpec.effect } : {}),
|
|
664
|
-
routing: action.routing,
|
|
665
|
-
},
|
|
666
|
-
});
|
|
667
|
-
}
|
|
668
|
-
return {
|
|
669
|
-
binding: createProcessTurnBinding(input.spec, derived.transitions),
|
|
670
|
-
};
|
|
671
|
-
}
|
|
672
|
-
function compileExternalTurn(input) {
|
|
673
|
-
const transitions = compileExternalSourceTransitions({
|
|
674
|
-
turnId: input.turnId,
|
|
675
|
-
spec: input.spec,
|
|
676
|
-
knownTurnIds: input.knownTurnIds,
|
|
677
|
-
});
|
|
678
|
-
return {
|
|
679
|
-
binding: createProcessTurnBinding(input.spec, transitions),
|
|
680
|
-
};
|
|
681
|
-
}
|
|
682
|
-
function getTurnEntries(turns) {
|
|
683
|
-
return Object.entries(turns);
|
|
684
|
-
}
|
|
685
|
-
function resolveActionRouteForExecution(input) {
|
|
686
|
-
if (input.use.routing.kind === "static") {
|
|
687
|
-
return { route: input.use.routing.route };
|
|
688
|
-
}
|
|
689
|
-
const actionExecution = {
|
|
690
|
-
ctx: makeProcessServerRuntimeContext(input.ctx),
|
|
691
|
-
input: input.inputValue,
|
|
692
|
-
turnId: input.use.turnId,
|
|
693
|
-
actionId: input.actionId,
|
|
694
|
-
};
|
|
695
|
-
const routes = input.use.routing.routes;
|
|
696
|
-
return Promise.resolve(input.use.routing.choose(actionExecution)).then((selectedBranchId) => {
|
|
697
|
-
if (!Object.hasOwn(routes, selectedBranchId)) {
|
|
698
|
-
throw new Error(`Action '${input.actionId}' on turn '${input.use.turnId}' selected unknown branch '${selectedBranchId}'`);
|
|
699
|
-
}
|
|
700
|
-
const route = routes[selectedBranchId];
|
|
701
|
-
if (!route) {
|
|
702
|
-
throw new Error(`Action '${input.actionId}' on turn '${input.use.turnId}' selected unknown branch '${selectedBranchId}'`);
|
|
703
|
-
}
|
|
704
|
-
return { route, selectedBranchId };
|
|
705
|
-
});
|
|
706
|
-
}
|
|
707
|
-
function buildActionTransitionRequest(route) {
|
|
708
|
-
if (route.nextTurnId !== undefined) {
|
|
709
|
-
return {
|
|
710
|
-
turnId: route.nextTurnId,
|
|
711
|
-
trigger: route.trigger,
|
|
712
|
-
};
|
|
713
|
-
}
|
|
714
|
-
return {
|
|
715
|
-
turnId: null,
|
|
716
|
-
lifecycleStatus: route.lifecycleStatus,
|
|
717
|
-
trigger: route.trigger,
|
|
718
|
-
};
|
|
719
|
-
}
|
|
720
|
-
function validateHappyPathConnectivity(input) {
|
|
721
|
-
const happyPath = input.happyPath;
|
|
722
|
-
if (!happyPath || happyPath.length < 2) {
|
|
723
|
-
return;
|
|
724
|
-
}
|
|
725
|
-
const happyPathTurns = new Set(happyPath);
|
|
726
|
-
for (let index = 0; index < happyPath.length - 1; index += 1) {
|
|
727
|
-
const source = happyPath[index];
|
|
728
|
-
const target = happyPath[index + 1];
|
|
729
|
-
let connected = false;
|
|
730
|
-
const visited = new Set([source]);
|
|
731
|
-
const queue = [source];
|
|
732
|
-
while (queue.length > 0 && !connected) {
|
|
733
|
-
const current = queue.shift();
|
|
734
|
-
for (const transition of getProcessTurnTransitions(input.turns.get(current))) {
|
|
735
|
-
const next = transition.nextTurnId;
|
|
736
|
-
if (next === undefined || !input.turnDefinitionsById.has(next)) {
|
|
737
|
-
continue;
|
|
738
|
-
}
|
|
739
|
-
if (next === target) {
|
|
740
|
-
connected = true;
|
|
741
|
-
break;
|
|
742
|
-
}
|
|
743
|
-
if (visited.has(next) || happyPathTurns.has(next)) {
|
|
744
|
-
continue;
|
|
745
|
-
}
|
|
746
|
-
const definition = input.turnDefinitionsById.get(next);
|
|
747
|
-
if (definition?.kind !== "human") {
|
|
748
|
-
continue;
|
|
749
|
-
}
|
|
750
|
-
visited.add(next);
|
|
751
|
-
queue.push(next);
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
if (!connected) {
|
|
755
|
-
throw new Error(`Process '${input.processId}' happy path segment '${source}' -> '${target}' is not connected by declared transitions`);
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
function buildDefinedProcess(input) {
|
|
760
|
-
const turnEntries = getTurnEntries(input.turns);
|
|
761
|
-
if (turnEntries.length === 0) {
|
|
762
|
-
throw new Error(`Process '${input.id}' must declare at least one turn`);
|
|
763
|
-
}
|
|
764
|
-
const knownTurnIds = new Set(turnEntries.map(([turnId]) => turnId));
|
|
765
|
-
const turnDefinitionsById = new Map(turnEntries.map(([turnId, turnSpec]) => [turnId, turnSpec]));
|
|
766
|
-
if (!knownTurnIds.has(input.entry)) {
|
|
767
|
-
throw new Error(`Process '${input.id}' entry turn '${input.entry}' is not declared`);
|
|
768
|
-
}
|
|
769
|
-
const seenEntries = new Set();
|
|
770
|
-
for (const turnId of [input.entry, ...(input.alternateEntries ?? [])]) {
|
|
771
|
-
if (seenEntries.has(turnId)) {
|
|
772
|
-
throw new Error(`Process '${input.id}' declares duplicate entry turn '${turnId}'`);
|
|
773
|
-
}
|
|
774
|
-
if (!knownTurnIds.has(turnId)) {
|
|
775
|
-
throw new Error(`Process '${input.id}' entry turn '${turnId}' is not declared`);
|
|
776
|
-
}
|
|
777
|
-
seenEntries.add(turnId);
|
|
778
|
-
}
|
|
779
|
-
if (input.happyPath) {
|
|
780
|
-
if (input.happyPath.length === 0) {
|
|
781
|
-
throw new Error(`Process '${input.id}' declares an empty happy path`);
|
|
782
|
-
}
|
|
783
|
-
const seen = new Set();
|
|
784
|
-
for (const turnId of input.happyPath) {
|
|
785
|
-
if (!knownTurnIds.has(turnId)) {
|
|
786
|
-
throw new Error(`Process '${input.id}' happy path references undeclared turn '${turnId}'`);
|
|
787
|
-
}
|
|
788
|
-
if (seen.has(turnId)) {
|
|
789
|
-
throw new Error(`Process '${input.id}' happy path repeats turn '${turnId}'`);
|
|
790
|
-
}
|
|
791
|
-
seen.add(turnId);
|
|
792
|
-
}
|
|
793
|
-
if (input.happyPath[0] !== input.entry) {
|
|
794
|
-
throw new Error(`Process '${input.id}' happy path must start at the entry turn '${input.entry}'`);
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
const turns = new Map();
|
|
798
|
-
const actionUses = new Map();
|
|
799
|
-
const outcomeEffects = new Map();
|
|
800
|
-
const executableTurns = new Map();
|
|
801
|
-
for (const [turnId, turnSpec] of turnEntries) {
|
|
802
|
-
if (turnSpec.kind === "llm") {
|
|
803
|
-
const compiledOutcomes = compileTurnOutcomeDefinitions({
|
|
804
|
-
turnId,
|
|
805
|
-
outcomes: turnSpec.outcomes,
|
|
806
|
-
turnEnd: turnSpec.turnEnd,
|
|
807
|
-
knownTurnIds,
|
|
808
|
-
turnDefinitionsById,
|
|
809
|
-
});
|
|
810
|
-
turns.set(turnId, createProcessTurnBinding(turnSpec, compiledOutcomes.transitions));
|
|
811
|
-
outcomeEffects.set(turnId, compiledOutcomes.effects);
|
|
812
|
-
executableTurns.set(turnId, {
|
|
813
|
-
kind: "llm",
|
|
814
|
-
id: turnId,
|
|
815
|
-
spec: turnSpec,
|
|
816
|
-
});
|
|
817
|
-
continue;
|
|
818
|
-
}
|
|
819
|
-
if (turnSpec.kind === "automatic" || turnSpec.kind === "server_automatic") {
|
|
820
|
-
const compiledOutcomes = compileTurnOutcomeDefinitions({
|
|
821
|
-
turnId,
|
|
822
|
-
outcomes: turnSpec.outcomes,
|
|
823
|
-
turnEnd: turnSpec.turnEnd,
|
|
824
|
-
knownTurnIds,
|
|
825
|
-
turnDefinitionsById,
|
|
826
|
-
});
|
|
827
|
-
turns.set(turnId, createProcessTurnBinding(turnSpec, compiledOutcomes.transitions));
|
|
828
|
-
outcomeEffects.set(turnId, compiledOutcomes.effects);
|
|
829
|
-
if (turnSpec.kind === "automatic") {
|
|
830
|
-
executableTurns.set(turnId, {
|
|
831
|
-
kind: "automatic",
|
|
832
|
-
id: turnId,
|
|
833
|
-
spec: turnSpec,
|
|
834
|
-
});
|
|
835
|
-
}
|
|
836
|
-
continue;
|
|
837
|
-
}
|
|
838
|
-
if (turnSpec.kind === "human") {
|
|
839
|
-
const compiled = compileHumanTurn({
|
|
840
|
-
turnId,
|
|
841
|
-
spec: turnSpec,
|
|
842
|
-
knownTurnIds,
|
|
843
|
-
turnDefinitionsById,
|
|
844
|
-
actionUses,
|
|
845
|
-
});
|
|
846
|
-
turns.set(turnId, compiled.binding);
|
|
847
|
-
continue;
|
|
848
|
-
}
|
|
849
|
-
const compiled = compileExternalTurn({
|
|
850
|
-
turnId,
|
|
851
|
-
spec: turnSpec,
|
|
852
|
-
knownTurnIds,
|
|
853
|
-
});
|
|
854
|
-
turns.set(turnId, compiled.binding);
|
|
855
|
-
}
|
|
856
|
-
validateHappyPathConnectivity({
|
|
857
|
-
processId: input.id,
|
|
858
|
-
happyPath: input.happyPath,
|
|
859
|
-
turns,
|
|
860
|
-
turnDefinitionsById,
|
|
861
|
-
});
|
|
862
|
-
const worker = (api) => {
|
|
863
|
-
api.start(input.entry);
|
|
864
|
-
const manuallyRegisteredTurns = new Set();
|
|
865
|
-
input.worker?.({
|
|
866
|
-
start(turnId) {
|
|
867
|
-
api.start(turnId);
|
|
868
|
-
},
|
|
869
|
-
turn(turnId, handler) {
|
|
870
|
-
manuallyRegisteredTurns.add(turnId);
|
|
871
|
-
api.turn(turnId, handler);
|
|
872
|
-
},
|
|
873
|
-
});
|
|
874
|
-
for (const executable of executableTurns.values()) {
|
|
875
|
-
if (manuallyRegisteredTurns.has(executable.id)) {
|
|
876
|
-
continue;
|
|
877
|
-
}
|
|
878
|
-
if (executable.kind === "llm") {
|
|
879
|
-
api.turn(executable.id, async (run) => {
|
|
880
|
-
await run.turn(executable.spec);
|
|
881
|
-
});
|
|
882
|
-
continue;
|
|
883
|
-
}
|
|
884
|
-
api.turn(executable.id, async (run) => {
|
|
885
|
-
await run.complete(await executable.spec.run(run.ctx));
|
|
886
|
-
});
|
|
887
|
-
}
|
|
888
|
-
};
|
|
889
|
-
const server = (api) => {
|
|
890
|
-
for (const [actionId, uses] of actionUses) {
|
|
891
|
-
const [firstUse] = uses;
|
|
892
|
-
const form = firstUse?.form;
|
|
893
|
-
for (const use of uses) {
|
|
894
|
-
if (!areFormsEquivalent(form, use.form)) {
|
|
895
|
-
throw new Error(`Action '${actionId}' must use the same form on every turn that references it`);
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
api.action({
|
|
899
|
-
id: actionId,
|
|
900
|
-
label: firstUse?.label ?? actionId,
|
|
901
|
-
...(form ? { form } : {}),
|
|
902
|
-
async plan(inputValue, ctx) {
|
|
903
|
-
const turnId = ctx.process.selectedTurnId;
|
|
904
|
-
const use = uses.find((candidate) => candidate.turnId === turnId);
|
|
905
|
-
if (!use || !turnId) {
|
|
906
|
-
throw new Error(`Action '${actionId}' is not available for selected turn '${String(turnId)}'`);
|
|
907
|
-
}
|
|
908
|
-
const resolvedRoute = await resolveActionRouteForExecution({
|
|
909
|
-
use,
|
|
910
|
-
actionId,
|
|
911
|
-
ctx,
|
|
912
|
-
inputValue,
|
|
913
|
-
});
|
|
914
|
-
const effectResult = use.effect
|
|
915
|
-
? await use.effect({
|
|
916
|
-
ctx: makeProcessServerRuntimeContext(ctx),
|
|
917
|
-
input: inputValue,
|
|
918
|
-
turnId,
|
|
919
|
-
actionId,
|
|
920
|
-
...(resolvedRoute.selectedBranchId
|
|
921
|
-
? { selectedBranchId: resolvedRoute.selectedBranchId }
|
|
922
|
-
: {}),
|
|
923
|
-
})
|
|
924
|
-
: undefined;
|
|
925
|
-
const formState = applyFormStateFields({
|
|
926
|
-
form: use.form,
|
|
927
|
-
inputValue,
|
|
928
|
-
state: effectResult?.state ?? ctx.state,
|
|
929
|
-
});
|
|
930
|
-
const result = formState
|
|
931
|
-
? mergeProcessEffectPlans(effectResult, { state: formState })
|
|
932
|
-
: effectResult;
|
|
933
|
-
await applyProcessEffectPlan({
|
|
934
|
-
ctx,
|
|
935
|
-
result,
|
|
936
|
-
transition: buildActionTransitionRequest(resolvedRoute.route),
|
|
937
|
-
});
|
|
938
|
-
},
|
|
939
|
-
});
|
|
940
|
-
}
|
|
941
|
-
for (const [turnId, effects] of outcomeEffects) {
|
|
942
|
-
api.onTurnOutcome(turnId, async (event, ctx) => {
|
|
943
|
-
const effect = effects.get(event.outcome);
|
|
944
|
-
if (!effect) {
|
|
945
|
-
return;
|
|
946
|
-
}
|
|
947
|
-
const result = await effect({
|
|
948
|
-
ctx: makeProcessServerRuntimeContext(ctx),
|
|
949
|
-
event,
|
|
950
|
-
turnId,
|
|
951
|
-
outcome: event.outcome,
|
|
952
|
-
});
|
|
953
|
-
await applyProcessEffectPlan({ ctx, result });
|
|
954
|
-
});
|
|
955
|
-
}
|
|
956
|
-
input.server?.(api);
|
|
957
|
-
};
|
|
958
|
-
return {
|
|
959
|
-
turns,
|
|
960
|
-
worker,
|
|
961
|
-
server,
|
|
962
|
-
};
|
|
963
|
-
}
|
|
964
|
-
export function defineProcess(input) {
|
|
965
|
-
const compiled = buildDefinedProcess(input);
|
|
966
|
-
const process = {
|
|
967
|
-
id: input.id,
|
|
968
|
-
displayName: input.displayName,
|
|
969
|
-
entryTurnId: input.entry,
|
|
970
|
-
alternateEntryTurnIds: [...(input.alternateEntries ?? [])],
|
|
971
|
-
...(input.happyPath ? { happyPath: [...input.happyPath] } : {}),
|
|
972
|
-
turns: compiled.turns,
|
|
973
|
-
paramsCodec: input.paramsCodec,
|
|
974
|
-
stateCodec: input.stateCodec,
|
|
975
|
-
initialState: input.initialState,
|
|
976
|
-
...(input.repositoryCredentials ? { repositoryCredentials: input.repositoryCredentials } : {}),
|
|
977
|
-
...(input.piConfig ? { piConfig: input.piConfig } : {}),
|
|
978
|
-
server: compiled.server,
|
|
979
|
-
worker: compiled.worker,
|
|
980
|
-
ui: input.ui,
|
|
981
|
-
launchers: input.launchers,
|
|
982
|
-
watchers: input.watchers,
|
|
983
|
-
};
|
|
984
|
-
markDefinedProcess(process);
|
|
985
|
-
return process;
|
|
986
|
-
}
|
|
987
|
-
export function routeTurnOutcomes(baseTurn, routes, options = {}) {
|
|
988
|
-
const baseOutcomes = (baseTurn.outcomes ?? {});
|
|
989
|
-
for (const outcomeId of Object.keys(routes)) {
|
|
990
|
-
if (!baseOutcomes[outcomeId]) {
|
|
991
|
-
throw new Error(`Cannot route unknown outcome '${outcomeId}' on reusable turn`);
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
if (options.strict) {
|
|
995
|
-
for (const outcomeId of Object.keys(baseOutcomes)) {
|
|
996
|
-
if (!routes[outcomeId]) {
|
|
997
|
-
throw new Error(`Strict routing omitted outcome '${outcomeId}' on reusable turn`);
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
return {
|
|
1002
|
-
...baseTurn,
|
|
1003
|
-
outcomes: Object.fromEntries(Object.entries(baseOutcomes).map(([outcomeId, baseOutcome]) => [
|
|
1004
|
-
outcomeId,
|
|
1005
|
-
{ ...baseOutcome, ...(routes[outcomeId] ?? {}) },
|
|
1006
|
-
])),
|
|
1007
|
-
};
|
|
1008
|
-
}
|
|
1009
|
-
// biome-ignore-end lint/suspicious/noExplicitAny: end routable turn instantiation helper suppression
|
|
1010
|
-
export function llmTurn(input) {
|
|
1011
|
-
return {
|
|
1012
|
-
...input,
|
|
1013
|
-
kind: "llm",
|
|
1014
|
-
};
|
|
1015
|
-
}
|
|
1016
|
-
export function automaticTurn(input) {
|
|
1017
|
-
return {
|
|
1018
|
-
...input,
|
|
1019
|
-
kind: "automatic",
|
|
1020
|
-
};
|
|
1021
|
-
}
|
|
1022
|
-
export function serverAutomaticTurn(input) {
|
|
1023
|
-
return {
|
|
1024
|
-
...input,
|
|
1025
|
-
kind: "server_automatic",
|
|
1026
|
-
};
|
|
1027
|
-
}
|
|
1028
|
-
export function humanTurn(input) {
|
|
1029
|
-
return {
|
|
1030
|
-
...input,
|
|
1031
|
-
kind: "human",
|
|
1032
|
-
};
|
|
1033
|
-
}
|
|
1034
|
-
export function externalTurn(input) {
|
|
1035
|
-
return {
|
|
1036
|
-
...input,
|
|
1037
|
-
kind: "external",
|
|
1038
|
-
};
|
|
1039
|
-
}
|
|
1040
|
-
//# sourceMappingURL=define-process.js.map
|