@leitwerk-dev/process-sdk 0.1.2 → 0.1.4
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/browser-ui-extension.d.ts +27 -0
- package/dist/browser-ui-extension.d.ts.map +1 -0
- package/dist/browser-ui-extension.js +2 -0
- package/dist/browser-ui-extension.js.map +1 -0
- package/dist/capabilities.d.ts +9 -0
- package/dist/capabilities.d.ts.map +1 -0
- package/dist/capabilities.js +7 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/codecs.d.ts +3 -0
- package/dist/codecs.d.ts.map +1 -0
- package/dist/codecs.js +9 -0
- package/dist/codecs.js.map +1 -0
- package/dist/commit-message.d.ts +7 -0
- package/dist/commit-message.d.ts.map +1 -0
- package/dist/commit-message.js +3 -0
- package/dist/commit-message.js.map +1 -0
- package/dist/core-capabilities.d.ts +393 -0
- package/dist/core-capabilities.d.ts.map +1 -0
- package/dist/core-capabilities.js +6 -0
- package/dist/core-capabilities.js.map +1 -0
- package/dist/define-process.d.ts +269 -0
- package/dist/define-process.d.ts.map +1 -0
- package/dist/define-process.js +1040 -0
- package/dist/define-process.js.map +1 -0
- package/dist/event-bus.d.ts +3 -0
- package/dist/event-bus.d.ts.map +1 -0
- package/dist/event-bus.js +31 -0
- package/dist/event-bus.js.map +1 -0
- package/dist/extension-api.d.ts +425 -0
- package/dist/extension-api.d.ts.map +1 -0
- package/dist/extension-api.js +5 -0
- package/dist/extension-api.js.map +1 -0
- package/dist/flow.d.ts +456 -0
- package/dist/flow.d.ts.map +1 -0
- package/dist/flow.js +1557 -0
- package/dist/flow.js.map +1 -0
- package/dist/form-contract.d.ts +2 -0
- package/dist/form-contract.d.ts.map +1 -0
- package/dist/form-contract.js +2 -0
- package/dist/form-contract.js.map +1 -0
- package/dist/fs-utils.d.ts +3 -0
- package/dist/fs-utils.d.ts.map +1 -0
- package/dist/fs-utils.js +23 -0
- package/dist/fs-utils.js.map +1 -0
- package/dist/git-binary.d.ts +3 -4
- package/dist/git-binary.d.ts.map +1 -0
- package/dist/git-binary.js +33 -25
- package/dist/git-binary.js.map +1 -1
- package/dist/graph-fragments.d.ts +23 -0
- package/dist/graph-fragments.d.ts.map +1 -0
- package/dist/graph-fragments.js +57 -0
- package/dist/graph-fragments.js.map +1 -0
- package/dist/host-capabilities.d.ts +12 -0
- package/dist/host-capabilities.d.ts.map +1 -0
- package/dist/host-capabilities.js +42 -0
- package/dist/host-capabilities.js.map +1 -0
- package/dist/index.d.ts +35 -1066
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -4488
- package/dist/index.js.map +1 -1
- package/dist/launcher-contract.d.ts +2 -0
- package/dist/launcher-contract.d.ts.map +1 -0
- package/dist/launcher-contract.js +2 -0
- package/dist/launcher-contract.js.map +1 -0
- package/dist/leaf-outcome-renderer.d.ts +7 -7
- package/dist/leaf-outcome-renderer.d.ts.map +1 -0
- package/dist/leaf-outcome-renderer.js +64 -74
- package/dist/leaf-outcome-renderer.js.map +1 -1
- package/dist/leaf-outcomes.d.ts +5 -0
- package/dist/leaf-outcomes.d.ts.map +1 -0
- package/dist/leaf-outcomes.js +52 -0
- package/dist/leaf-outcomes.js.map +1 -0
- package/dist/model-provider.d.ts +210 -0
- package/dist/model-provider.d.ts.map +1 -0
- package/dist/model-provider.js +171 -0
- package/dist/model-provider.js.map +1 -0
- package/dist/pi-config.d.ts +15 -19
- package/dist/pi-config.d.ts.map +1 -0
- package/dist/pi-config.js +180 -21
- package/dist/pi-config.js.map +1 -1
- package/dist/process-definition-internals.d.ts +7 -0
- package/dist/process-definition-internals.d.ts.map +1 -0
- package/dist/process-definition-internals.js +25 -0
- package/dist/process-definition-internals.js.map +1 -0
- package/dist/process-flow-view.d.ts +20 -0
- package/dist/process-flow-view.d.ts.map +1 -0
- package/dist/process-flow-view.js +418 -0
- package/dist/process-flow-view.js.map +1 -0
- package/dist/process-graph.d.ts +45 -0
- package/dist/process-graph.d.ts.map +1 -0
- package/dist/process-graph.js +213 -0
- package/dist/process-graph.js.map +1 -0
- package/dist/process-launcher-builder.d.ts +9 -0
- package/dist/process-launcher-builder.d.ts.map +1 -0
- package/dist/process-launcher-builder.js +43 -0
- package/dist/process-launcher-builder.js.map +1 -0
- package/dist/process-watcher-builder.d.ts +10 -0
- package/dist/process-watcher-builder.d.ts.map +1 -0
- package/dist/process-watcher-builder.js +40 -0
- package/dist/process-watcher-builder.js.map +1 -0
- package/dist/registry-utils.d.ts +8 -0
- package/dist/registry-utils.d.ts.map +1 -0
- package/dist/registry-utils.js +18 -0
- package/dist/registry-utils.js.map +1 -0
- package/dist/repository-git-env.d.ts +8 -0
- package/dist/repository-git-env.d.ts.map +1 -0
- package/dist/repository-git-env.js +26 -0
- package/dist/repository-git-env.js.map +1 -0
- package/dist/review-flow.d.ts +7 -13
- package/dist/review-flow.d.ts.map +1 -0
- package/dist/review-flow.js +43 -12
- package/dist/review-flow.js.map +1 -1
- package/dist/runtime-internals.d.ts +3 -21
- package/dist/runtime-internals.d.ts.map +1 -0
- package/dist/runtime-internals.js +2 -18
- package/dist/runtime-internals.js.map +1 -1
- package/dist/server-events.d.ts +84 -0
- package/dist/server-events.d.ts.map +1 -0
- package/dist/server-events.js +2 -0
- package/dist/server-events.js.map +1 -0
- package/dist/server-process-builder.d.ts +11 -0
- package/dist/server-process-builder.d.ts.map +1 -0
- package/dist/server-process-builder.js +52 -0
- package/dist/server-process-builder.js.map +1 -0
- package/dist/state-helpers.d.ts +9 -0
- package/dist/state-helpers.d.ts.map +1 -0
- package/dist/state-helpers.js +20 -0
- package/dist/state-helpers.js.map +1 -0
- package/dist/subprocess-env.d.ts +12 -0
- package/dist/subprocess-env.d.ts.map +1 -0
- package/dist/subprocess-env.js +34 -0
- package/dist/subprocess-env.js.map +1 -0
- package/dist/tool-renderers.d.ts +10 -0
- package/dist/tool-renderers.d.ts.map +1 -0
- package/dist/tool-renderers.js +63 -0
- package/dist/tool-renderers.js.map +1 -0
- package/dist/turn-semantics.d.ts +22 -0
- package/dist/turn-semantics.d.ts.map +1 -0
- package/dist/turn-semantics.js +339 -0
- package/dist/turn-semantics.js.map +1 -0
- package/dist/types.d.ts +295 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/dist/ui-process-builder.d.ts +9 -0
- package/dist/ui-process-builder.d.ts.map +1 -0
- package/dist/ui-process-builder.js +30 -0
- package/dist/ui-process-builder.js.map +1 -0
- package/dist/worker-process-builder.d.ts +10 -0
- package/dist/worker-process-builder.d.ts.map +1 -0
- package/dist/worker-process-builder.js +22 -0
- package/dist/worker-process-builder.js.map +1 -0
- package/package.json +4 -4
package/dist/flow.js
ADDED
|
@@ -0,0 +1,1557 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { assertValidProcessProductName, createReviewSubject, humanizeProcessLabel, } from "@leitwerk-dev/domain";
|
|
3
|
+
import { defineProcess } from "./define-process.js";
|
|
4
|
+
export const DEFAULT_PLAN_RESULT_OUTCOME_ID = "plan_saved";
|
|
5
|
+
export const DEFAULT_FLOW_PRODUCT_NAME = "default";
|
|
6
|
+
const ASSISTANT_OUTPUT_TURN_RESULT = { mode: "assistant_output", required: true };
|
|
7
|
+
const OUTCOME_TOOL_MARKDOWN_PARAMETER_NAME = "markdown";
|
|
8
|
+
const OUTCOME_TOOL_ARGUMENT_TURN_RESULT = {
|
|
9
|
+
mode: "outcome_tool_argument",
|
|
10
|
+
parameterName: OUTCOME_TOOL_MARKDOWN_PARAMETER_NAME,
|
|
11
|
+
required: true,
|
|
12
|
+
};
|
|
13
|
+
function textField(input) {
|
|
14
|
+
return {
|
|
15
|
+
type: "string",
|
|
16
|
+
description: input.description,
|
|
17
|
+
required: true,
|
|
18
|
+
requiredErrorCode: input.requiredErrorCode,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function stringArrayField(input) {
|
|
22
|
+
return {
|
|
23
|
+
type: "array",
|
|
24
|
+
description: input.description,
|
|
25
|
+
items: { type: "string" },
|
|
26
|
+
required: true,
|
|
27
|
+
requiredErrorCode: input.requiredErrorCode,
|
|
28
|
+
minItems: input.minItems,
|
|
29
|
+
minItemsErrorCode: input.minItemsErrorCode,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function normalizeProductName(productName) {
|
|
33
|
+
assertValidProcessProductName(productName);
|
|
34
|
+
return productName;
|
|
35
|
+
}
|
|
36
|
+
function toRequiredErrorCode(name) {
|
|
37
|
+
const snake = name
|
|
38
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
39
|
+
.replace(/[^a-zA-Z0-9]+/g, "_")
|
|
40
|
+
.replace(/^_+|_+$/g, "")
|
|
41
|
+
.toLowerCase();
|
|
42
|
+
return `${snake || "value"}_required`;
|
|
43
|
+
}
|
|
44
|
+
function readInitialPrompt(params) {
|
|
45
|
+
if (typeof params === "object" && params !== null && "prompt" in params) {
|
|
46
|
+
const value = params.prompt;
|
|
47
|
+
if (typeof value === "string") {
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
function isPathInside(parent, child) {
|
|
54
|
+
const relative = path.relative(parent, child);
|
|
55
|
+
return (relative !== "" &&
|
|
56
|
+
relative !== ".." &&
|
|
57
|
+
!relative.startsWith(`..${path.sep}`) &&
|
|
58
|
+
!path.isAbsolute(relative));
|
|
59
|
+
}
|
|
60
|
+
function safeJoinInside(parent, childPath, context) {
|
|
61
|
+
const parentPath = path.resolve(parent);
|
|
62
|
+
const targetPath = path.resolve(parentPath, childPath);
|
|
63
|
+
if (!isPathInside(parentPath, targetPath)) {
|
|
64
|
+
throw new Error(`${context} resolves outside '${parentPath}'`);
|
|
65
|
+
}
|
|
66
|
+
return targetPath;
|
|
67
|
+
}
|
|
68
|
+
function toWorkspaceClonePath(relativePath) {
|
|
69
|
+
return `./${relativePath.split(path.sep).join("/")}`;
|
|
70
|
+
}
|
|
71
|
+
function resolveSafeProjectWorkspacePath(key) {
|
|
72
|
+
if (path.isAbsolute(key)) {
|
|
73
|
+
throw new Error(`Flow context project key '${key}' must be a relative workspace path`);
|
|
74
|
+
}
|
|
75
|
+
const virtualWorkspaceRoot = path.resolve(path.sep, "__leitwerk_process_workspace__");
|
|
76
|
+
const virtualTarget = safeJoinInside(virtualWorkspaceRoot, key, `Flow context project key '${key}'`);
|
|
77
|
+
const relativePath = path.relative(virtualWorkspaceRoot, virtualTarget);
|
|
78
|
+
return {
|
|
79
|
+
relativePath,
|
|
80
|
+
workspaceClonePath: toWorkspaceClonePath(relativePath),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function resolveRepoByKey(input) {
|
|
84
|
+
const requestedKey = input.key.trim();
|
|
85
|
+
if (!requestedKey) {
|
|
86
|
+
throw new Error("Flow repo lookup requires a non-empty project key");
|
|
87
|
+
}
|
|
88
|
+
const matches = input.projects.filter((project) => project.key.trim() === requestedKey);
|
|
89
|
+
if (matches.length === 0) {
|
|
90
|
+
if (!input.required) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
const availableKeys = input.projects
|
|
94
|
+
.map((project) => project.key.trim())
|
|
95
|
+
.filter((key) => key !== "")
|
|
96
|
+
.join(", ");
|
|
97
|
+
throw new Error(`Flow context requires project '${requestedKey}', but this process has no project with that key${availableKeys ? ` (available: ${availableKeys})` : ""}`);
|
|
98
|
+
}
|
|
99
|
+
if (matches.length > 1) {
|
|
100
|
+
throw new Error(`Flow context found duplicate project key '${requestedKey}'`);
|
|
101
|
+
}
|
|
102
|
+
const project = matches[0];
|
|
103
|
+
const key = project.key.trim();
|
|
104
|
+
if (!key) {
|
|
105
|
+
throw new Error("Flow context requires projects to have non-empty keys");
|
|
106
|
+
}
|
|
107
|
+
const baseBranch = project.baseBranch.trim();
|
|
108
|
+
if (!baseBranch) {
|
|
109
|
+
throw new Error(`Flow context project '${key}' has no base branch`);
|
|
110
|
+
}
|
|
111
|
+
const workBranch = (project.workBranch ?? project.baseBranch).trim();
|
|
112
|
+
if (!workBranch) {
|
|
113
|
+
throw new Error(`Flow context project '${key}' has no work branch`);
|
|
114
|
+
}
|
|
115
|
+
const workspacePath = resolveSafeProjectWorkspacePath(key);
|
|
116
|
+
return {
|
|
117
|
+
key,
|
|
118
|
+
workspaceClonePath: workspacePath.workspaceClonePath,
|
|
119
|
+
fsPath: typeof input.workspaceRoot === "string" && input.workspaceRoot.trim() !== ""
|
|
120
|
+
? safeJoinInside(input.workspaceRoot, workspacePath.relativePath, `Flow context project '${key}' workspace path`)
|
|
121
|
+
: workspacePath.workspaceClonePath,
|
|
122
|
+
baseBranch,
|
|
123
|
+
workBranch,
|
|
124
|
+
locator: project.repoLocator,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function createFlowRepoLookup(input) {
|
|
128
|
+
return {
|
|
129
|
+
get: (key) => resolveRepoByKey({
|
|
130
|
+
projects: input.projects,
|
|
131
|
+
workspaceRoot: input.workspaceRoot,
|
|
132
|
+
key,
|
|
133
|
+
required: true,
|
|
134
|
+
}),
|
|
135
|
+
optional: (key) => resolveRepoByKey({
|
|
136
|
+
projects: input.projects,
|
|
137
|
+
workspaceRoot: input.workspaceRoot,
|
|
138
|
+
key,
|
|
139
|
+
required: false,
|
|
140
|
+
}),
|
|
141
|
+
all: () => input.projects.map((project) => resolveRepoByKey({
|
|
142
|
+
projects: input.projects,
|
|
143
|
+
workspaceRoot: input.workspaceRoot,
|
|
144
|
+
key: project.key,
|
|
145
|
+
required: true,
|
|
146
|
+
})),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
export function createFlowPromptContext(ctx, consumedProducts, optionalConsumedProducts = []) {
|
|
150
|
+
const productInput = {};
|
|
151
|
+
for (const productName of consumedProducts) {
|
|
152
|
+
const markdown = ctx.turnResultMarkdownByProduct?.[productName];
|
|
153
|
+
if (typeof markdown !== "string" || markdown.trim() === "") {
|
|
154
|
+
throw new Error(`Flow prompt context requires product '${productName}' markdown, but the worker payload did not provide it`);
|
|
155
|
+
}
|
|
156
|
+
productInput[productName] = markdown;
|
|
157
|
+
}
|
|
158
|
+
for (const productName of optionalConsumedProducts) {
|
|
159
|
+
if (productName in productInput) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
const markdown = ctx.turnResultMarkdownByProduct?.[productName];
|
|
163
|
+
if (typeof markdown === "string" && markdown.trim() !== "") {
|
|
164
|
+
productInput[productName] = markdown;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
process: ctx.process,
|
|
169
|
+
projects: ctx.projects,
|
|
170
|
+
params: ctx.params,
|
|
171
|
+
state: ctx.state,
|
|
172
|
+
...(ctx.workspaceRoot ? { workspaceRoot: ctx.workspaceRoot } : {}),
|
|
173
|
+
prompts: {
|
|
174
|
+
initial: readInitialPrompt(ctx.params),
|
|
175
|
+
},
|
|
176
|
+
input: productInput,
|
|
177
|
+
repo: createFlowRepoLookup({
|
|
178
|
+
projects: ctx.projects,
|
|
179
|
+
workspaceRoot: ctx.workspaceRoot,
|
|
180
|
+
}),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
export function createFlowAutomaticRunContext(ctx) {
|
|
184
|
+
return {
|
|
185
|
+
process: ctx.process,
|
|
186
|
+
projects: ctx.projects,
|
|
187
|
+
params: ctx.params,
|
|
188
|
+
state: ctx.state,
|
|
189
|
+
...(ctx.workspaceRoot ? { workspaceRoot: ctx.workspaceRoot } : {}),
|
|
190
|
+
repo: createFlowRepoLookup({
|
|
191
|
+
projects: ctx.projects,
|
|
192
|
+
workspaceRoot: ctx.workspaceRoot,
|
|
193
|
+
}),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function createFlowLlmOutcomeEffectContext(input) {
|
|
197
|
+
return {
|
|
198
|
+
process: input.ctx.process,
|
|
199
|
+
projects: input.ctx.projects,
|
|
200
|
+
params: input.ctx.params,
|
|
201
|
+
state: input.ctx.state,
|
|
202
|
+
output: typeof input.event.turnResultMarkdown === "string"
|
|
203
|
+
? { content: input.event.turnResultMarkdown }
|
|
204
|
+
: null,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
function createFlowAutomaticOutcomeEffectContext(input) {
|
|
208
|
+
return {
|
|
209
|
+
process: input.ctx.process,
|
|
210
|
+
projects: input.ctx.projects,
|
|
211
|
+
params: input.ctx.params,
|
|
212
|
+
state: input.ctx.state,
|
|
213
|
+
output: typeof input.event.turnResultMarkdown === "string"
|
|
214
|
+
? { content: input.event.turnResultMarkdown }
|
|
215
|
+
: null,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function wrapLlmOutcomeEffect(effect) {
|
|
219
|
+
return (execution) => effect({
|
|
220
|
+
ctx: createFlowLlmOutcomeEffectContext({ ctx: execution.ctx, event: execution.event }),
|
|
221
|
+
event: execution.event,
|
|
222
|
+
turnId: execution.turnId,
|
|
223
|
+
outcome: execution.outcome,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
function wrapAutomaticOutcomeEffect(effect) {
|
|
227
|
+
return (execution) => effect({
|
|
228
|
+
ctx: createFlowAutomaticOutcomeEffectContext({ ctx: execution.ctx, event: execution.event }),
|
|
229
|
+
event: execution.event,
|
|
230
|
+
turnId: execution.turnId,
|
|
231
|
+
outcome: execution.outcome,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
class RouteAndEffectBuilder {
|
|
235
|
+
target = null;
|
|
236
|
+
flowEffect;
|
|
237
|
+
setTarget(next) {
|
|
238
|
+
if (this.target && next) {
|
|
239
|
+
throw new Error("Flow route already declares a target");
|
|
240
|
+
}
|
|
241
|
+
this.target = next;
|
|
242
|
+
return this;
|
|
243
|
+
}
|
|
244
|
+
to(turnId) {
|
|
245
|
+
return this.setTarget({ kind: "to", turnId });
|
|
246
|
+
}
|
|
247
|
+
complete() {
|
|
248
|
+
return this.setTarget({ kind: "complete" });
|
|
249
|
+
}
|
|
250
|
+
lifecycleStatus(status) {
|
|
251
|
+
return this.setTarget({ kind: "lifecycleStatus", status });
|
|
252
|
+
}
|
|
253
|
+
stay() {
|
|
254
|
+
this.target = null;
|
|
255
|
+
return this;
|
|
256
|
+
}
|
|
257
|
+
state(fn) {
|
|
258
|
+
this.flowEffect = async (input) => ({ state: await fn(input) });
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
effect(fn) {
|
|
262
|
+
this.flowEffect = fn;
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
hasRoute() {
|
|
266
|
+
return this.target !== null;
|
|
267
|
+
}
|
|
268
|
+
buildRouteTarget() {
|
|
269
|
+
return buildRouteTargetSpec(this.target);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
export function buildRouteTargetSpec(target) {
|
|
273
|
+
if (!target) {
|
|
274
|
+
return {};
|
|
275
|
+
}
|
|
276
|
+
if (target.kind === "to") {
|
|
277
|
+
return { to: target.turnId };
|
|
278
|
+
}
|
|
279
|
+
if (target.kind === "complete") {
|
|
280
|
+
return { complete: true };
|
|
281
|
+
}
|
|
282
|
+
return { lifecycleStatus: target.status };
|
|
283
|
+
}
|
|
284
|
+
function isStringArray(value) {
|
|
285
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
286
|
+
}
|
|
287
|
+
class ParameterizedOutcomeBuilder extends RouteAndEffectBuilder {
|
|
288
|
+
outcomeDescription = null;
|
|
289
|
+
parameters = {};
|
|
290
|
+
publishedMarkdownParameter = null;
|
|
291
|
+
description(text) {
|
|
292
|
+
this.outcomeDescription = text;
|
|
293
|
+
return this;
|
|
294
|
+
}
|
|
295
|
+
parameter(name, spec) {
|
|
296
|
+
if (name.trim() === "") {
|
|
297
|
+
throw new Error("Outcome parameter name must be non-empty");
|
|
298
|
+
}
|
|
299
|
+
this.parameters[name] = {
|
|
300
|
+
...spec,
|
|
301
|
+
description: spec.description ?? "",
|
|
302
|
+
};
|
|
303
|
+
return this;
|
|
304
|
+
}
|
|
305
|
+
withRequired(name, options) {
|
|
306
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
307
|
+
return {
|
|
308
|
+
...resolved,
|
|
309
|
+
required: true,
|
|
310
|
+
requiredErrorCode: resolved.requiredErrorCode ?? toRequiredErrorCode(name),
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
string(name, options = {}) {
|
|
314
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
315
|
+
return this.parameter(name, {
|
|
316
|
+
...resolved,
|
|
317
|
+
type: "string",
|
|
318
|
+
description: resolved.description ?? "",
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
markdown(name, options = {}) {
|
|
322
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
323
|
+
const { publish, ...parameterOptions } = resolved;
|
|
324
|
+
if (publish) {
|
|
325
|
+
if (this.publishedMarkdownParameter && this.publishedMarkdownParameter !== name) {
|
|
326
|
+
throw new Error("Outcome can publish only one markdown parameter");
|
|
327
|
+
}
|
|
328
|
+
normalizeProductName(name);
|
|
329
|
+
this.publishedMarkdownParameter = name;
|
|
330
|
+
}
|
|
331
|
+
return this.parameter(name, {
|
|
332
|
+
...parameterOptions,
|
|
333
|
+
required: publish ? true : resolved.required,
|
|
334
|
+
requiredErrorCode: resolved.requiredErrorCode ?? (publish ? `${name}_required` : undefined),
|
|
335
|
+
type: "string",
|
|
336
|
+
description: resolved.description ?? "",
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
requiredString(name, options = {}) {
|
|
340
|
+
const resolved = this.withRequired(name, options);
|
|
341
|
+
return this.parameter(name, {
|
|
342
|
+
...resolved,
|
|
343
|
+
type: "string",
|
|
344
|
+
description: resolved.description ?? "",
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
number(name, options = {}) {
|
|
348
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
349
|
+
return this.parameter(name, {
|
|
350
|
+
...resolved,
|
|
351
|
+
type: "number",
|
|
352
|
+
description: resolved.description ?? "",
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
requiredNumber(name, options = {}) {
|
|
356
|
+
const resolved = this.withRequired(name, options);
|
|
357
|
+
return this.parameter(name, {
|
|
358
|
+
...resolved,
|
|
359
|
+
type: "number",
|
|
360
|
+
description: resolved.description ?? "",
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
boolean(name, options = {}) {
|
|
364
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
365
|
+
return this.parameter(name, {
|
|
366
|
+
...resolved,
|
|
367
|
+
type: "boolean",
|
|
368
|
+
description: resolved.description ?? "",
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
requiredBoolean(name, options = {}) {
|
|
372
|
+
const resolved = this.withRequired(name, options);
|
|
373
|
+
return this.parameter(name, {
|
|
374
|
+
...resolved,
|
|
375
|
+
type: "boolean",
|
|
376
|
+
description: resolved.description ?? "",
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
stringArray(name, options = {}) {
|
|
380
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
381
|
+
return this.parameter(name, {
|
|
382
|
+
...resolved,
|
|
383
|
+
type: "array",
|
|
384
|
+
description: resolved.description ?? "",
|
|
385
|
+
items: resolved.items ?? { type: "string" },
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
requiredStringArray(name, options = {}) {
|
|
389
|
+
const resolved = this.withRequired(name, options);
|
|
390
|
+
return this.parameter(name, {
|
|
391
|
+
...resolved,
|
|
392
|
+
type: "array",
|
|
393
|
+
description: resolved.description ?? "",
|
|
394
|
+
items: resolved.items ?? { type: "string" },
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
enum(name, valuesOrOptions) {
|
|
398
|
+
const resolved = isStringArray(valuesOrOptions)
|
|
399
|
+
? { values: [...valuesOrOptions] }
|
|
400
|
+
: valuesOrOptions;
|
|
401
|
+
const { values = [], ...options } = resolved;
|
|
402
|
+
return this.parameter(name, {
|
|
403
|
+
...options,
|
|
404
|
+
type: "string",
|
|
405
|
+
description: options.description ?? "",
|
|
406
|
+
enum: [...values],
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
object(name, options = {}) {
|
|
410
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
411
|
+
return this.parameter(name, {
|
|
412
|
+
...resolved,
|
|
413
|
+
type: "object",
|
|
414
|
+
description: resolved.description ?? "",
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
array(name, options = {}) {
|
|
418
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
419
|
+
return this.parameter(name, {
|
|
420
|
+
...resolved,
|
|
421
|
+
type: "array",
|
|
422
|
+
description: resolved.description ?? "",
|
|
423
|
+
items: resolved.items ?? { type: "string" },
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
export class OutcomeToolBuilder extends ParameterizedOutcomeBuilder {
|
|
428
|
+
build() {
|
|
429
|
+
if (!this.outcomeDescription) {
|
|
430
|
+
throw new Error("LLM outcome tool must declare .description(...)");
|
|
431
|
+
}
|
|
432
|
+
return {
|
|
433
|
+
description: this.outcomeDescription,
|
|
434
|
+
parameters: this.parameters,
|
|
435
|
+
...(this.publishedMarkdownParameter
|
|
436
|
+
? {
|
|
437
|
+
publishedProduct: this.publishedMarkdownParameter,
|
|
438
|
+
turnResultMarkdownParameter: this.publishedMarkdownParameter,
|
|
439
|
+
}
|
|
440
|
+
: {}),
|
|
441
|
+
...this.buildRouteTarget(),
|
|
442
|
+
...(this.flowEffect ? { effect: wrapLlmOutcomeEffect(this.flowEffect) } : {}),
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
export class AutomaticOutcomeBuilder extends ParameterizedOutcomeBuilder {
|
|
447
|
+
build() {
|
|
448
|
+
if (!this.outcomeDescription) {
|
|
449
|
+
throw new Error("Automatic outcome must declare .description(...)");
|
|
450
|
+
}
|
|
451
|
+
return {
|
|
452
|
+
description: this.outcomeDescription,
|
|
453
|
+
parameters: this.parameters,
|
|
454
|
+
...(this.publishedMarkdownParameter
|
|
455
|
+
? {
|
|
456
|
+
publishedProduct: this.publishedMarkdownParameter,
|
|
457
|
+
turnResultMarkdownParameter: this.publishedMarkdownParameter,
|
|
458
|
+
}
|
|
459
|
+
: {}),
|
|
460
|
+
...this.buildRouteTarget(),
|
|
461
|
+
...(this.flowEffect ? { effect: wrapAutomaticOutcomeEffect(this.flowEffect) } : {}),
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
export class PlanResultBuilder extends RouteAndEffectBuilder {
|
|
466
|
+
resultDescription = "The candidate plan is ready for operator review";
|
|
467
|
+
summarySpec = null;
|
|
468
|
+
acceptanceCriteriaSpec = null;
|
|
469
|
+
reviewTurnId = null;
|
|
470
|
+
description(text) {
|
|
471
|
+
this.resultDescription = text;
|
|
472
|
+
return this;
|
|
473
|
+
}
|
|
474
|
+
summary(options = {}) {
|
|
475
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
476
|
+
this.summarySpec = textField({
|
|
477
|
+
description: resolved.description ?? "Short summary of the proposed plan",
|
|
478
|
+
requiredErrorCode: resolved.requiredErrorCode ?? "summary_required",
|
|
479
|
+
});
|
|
480
|
+
return this;
|
|
481
|
+
}
|
|
482
|
+
acceptanceCriteria(options = {}) {
|
|
483
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
484
|
+
this.acceptanceCriteriaSpec = stringArrayField({
|
|
485
|
+
description: resolved.description ?? "Acceptance criteria for the requested change",
|
|
486
|
+
requiredErrorCode: resolved.requiredErrorCode ?? "acceptance_criteria_required",
|
|
487
|
+
minItems: resolved.minItems ?? 1,
|
|
488
|
+
minItemsErrorCode: resolved.minItemsErrorCode ?? "acceptance_criteria_required",
|
|
489
|
+
});
|
|
490
|
+
return this;
|
|
491
|
+
}
|
|
492
|
+
review(turnId) {
|
|
493
|
+
this.reviewTurnId = turnId;
|
|
494
|
+
return this.setTarget({ kind: "to", turnId });
|
|
495
|
+
}
|
|
496
|
+
buildOutcome() {
|
|
497
|
+
if (!this.summarySpec) {
|
|
498
|
+
throw new Error("plan result must declare .summary(...)");
|
|
499
|
+
}
|
|
500
|
+
if (!this.acceptanceCriteriaSpec) {
|
|
501
|
+
throw new Error("plan result must declare .acceptanceCriteria(...)");
|
|
502
|
+
}
|
|
503
|
+
if (!this.reviewTurnId) {
|
|
504
|
+
throw new Error("plan result must declare .review(turnId)");
|
|
505
|
+
}
|
|
506
|
+
const stateEffect = this.flowEffect ? wrapLlmOutcomeEffect(this.flowEffect) : undefined;
|
|
507
|
+
return {
|
|
508
|
+
description: this.resultDescription,
|
|
509
|
+
parameters: {
|
|
510
|
+
summary: this.summarySpec,
|
|
511
|
+
acceptanceCriteria: this.acceptanceCriteriaSpec,
|
|
512
|
+
},
|
|
513
|
+
to: this.reviewTurnId,
|
|
514
|
+
lifecycleIntent: {
|
|
515
|
+
kind: "save_plan_result",
|
|
516
|
+
summaryParam: "summary",
|
|
517
|
+
acceptanceCriteriaParam: "acceptanceCriteria",
|
|
518
|
+
planMarkdownParam: "planMarkdown",
|
|
519
|
+
emitEventType: DEFAULT_PLAN_RESULT_OUTCOME_ID,
|
|
520
|
+
broadcastType: "plan.updated",
|
|
521
|
+
...(stateEffect ? { state: stateEffect } : {}),
|
|
522
|
+
},
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
export class PublishedResultBuilder extends RouteAndEffectBuilder {
|
|
527
|
+
productName;
|
|
528
|
+
parent;
|
|
529
|
+
constructor(productName, parent) {
|
|
530
|
+
super();
|
|
531
|
+
this.productName = productName;
|
|
532
|
+
this.parent = parent;
|
|
533
|
+
}
|
|
534
|
+
get id() {
|
|
535
|
+
if (!this.parent) {
|
|
536
|
+
throw new Error("Published result builder is not attached to a turn");
|
|
537
|
+
}
|
|
538
|
+
return this.parent.id;
|
|
539
|
+
}
|
|
540
|
+
get definition() {
|
|
541
|
+
if (!this.parent) {
|
|
542
|
+
throw new Error("Published result builder is not attached to a turn");
|
|
543
|
+
}
|
|
544
|
+
return this.parent.definition;
|
|
545
|
+
}
|
|
546
|
+
buildTurnEnd() {
|
|
547
|
+
const effect = this.flowEffect ? wrapLlmOutcomeEffect(this.flowEffect) : undefined;
|
|
548
|
+
return {
|
|
549
|
+
outcome: this.productName,
|
|
550
|
+
...this.buildRouteTarget(),
|
|
551
|
+
...(effect ? { effect } : {}),
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
export class LlmTurnEndBuilder extends RouteAndEffectBuilder {
|
|
556
|
+
outcomeId;
|
|
557
|
+
parent;
|
|
558
|
+
constructor(outcomeId, parent) {
|
|
559
|
+
super();
|
|
560
|
+
this.outcomeId = outcomeId;
|
|
561
|
+
this.parent = parent;
|
|
562
|
+
if (outcomeId.trim() === "") {
|
|
563
|
+
throw new Error("LLM turn end outcome must be non-empty");
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
get id() {
|
|
567
|
+
if (!this.parent) {
|
|
568
|
+
throw new Error("LLM turn end builder is not attached to a turn");
|
|
569
|
+
}
|
|
570
|
+
return this.parent.id;
|
|
571
|
+
}
|
|
572
|
+
get definition() {
|
|
573
|
+
if (!this.parent) {
|
|
574
|
+
throw new Error("LLM turn end builder is not attached to a turn");
|
|
575
|
+
}
|
|
576
|
+
return this.parent.definition;
|
|
577
|
+
}
|
|
578
|
+
build() {
|
|
579
|
+
const effect = this.flowEffect ? wrapLlmOutcomeEffect(this.flowEffect) : undefined;
|
|
580
|
+
return {
|
|
581
|
+
outcome: this.outcomeId,
|
|
582
|
+
...this.buildRouteTarget(),
|
|
583
|
+
...(effect ? { effect } : {}),
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
export class LlmFlowBuilder {
|
|
588
|
+
turnId;
|
|
589
|
+
turnDescription = null;
|
|
590
|
+
modelPurposeValue;
|
|
591
|
+
availableTools = [];
|
|
592
|
+
promptBuilder = null;
|
|
593
|
+
branchType = "primary";
|
|
594
|
+
context = "fresh";
|
|
595
|
+
completionMode = "turn_end";
|
|
596
|
+
questionsEnabled = false;
|
|
597
|
+
startFrom;
|
|
598
|
+
restorePrimaryLeafAfterTurn;
|
|
599
|
+
reviewSubject;
|
|
600
|
+
consumedProductNames = [];
|
|
601
|
+
optionalConsumedProductNames = [];
|
|
602
|
+
publishedResult = null;
|
|
603
|
+
endResult = null;
|
|
604
|
+
outcomeToolBuilders = new Map();
|
|
605
|
+
constructor(turnId) {
|
|
606
|
+
this.turnId = turnId;
|
|
607
|
+
}
|
|
608
|
+
get id() {
|
|
609
|
+
return this.turnId;
|
|
610
|
+
}
|
|
611
|
+
get definition() {
|
|
612
|
+
return this.buildDefinition();
|
|
613
|
+
}
|
|
614
|
+
description(description) {
|
|
615
|
+
this.turnDescription = description;
|
|
616
|
+
return this;
|
|
617
|
+
}
|
|
618
|
+
modelPurpose(purpose) {
|
|
619
|
+
this.modelPurposeValue = purpose;
|
|
620
|
+
return this;
|
|
621
|
+
}
|
|
622
|
+
tools(...tools) {
|
|
623
|
+
this.availableTools = tools;
|
|
624
|
+
return this;
|
|
625
|
+
}
|
|
626
|
+
/** Enable durable operator questions for this LLM turn. */
|
|
627
|
+
askQuestions() {
|
|
628
|
+
this.questionsEnabled = true;
|
|
629
|
+
return this;
|
|
630
|
+
}
|
|
631
|
+
freshPrimary() {
|
|
632
|
+
this.branchType = "primary";
|
|
633
|
+
this.context = "fresh";
|
|
634
|
+
this.completionMode = "turn_end";
|
|
635
|
+
this.startFrom = undefined;
|
|
636
|
+
this.restorePrimaryLeafAfterTurn = undefined;
|
|
637
|
+
return this;
|
|
638
|
+
}
|
|
639
|
+
freshSeededPrimary() {
|
|
640
|
+
this.branchType = "primary";
|
|
641
|
+
this.context = "fresh_seeded";
|
|
642
|
+
this.completionMode = "turn_end";
|
|
643
|
+
this.startFrom = undefined;
|
|
644
|
+
this.restorePrimaryLeafAfterTurn = undefined;
|
|
645
|
+
return this;
|
|
646
|
+
}
|
|
647
|
+
fullPrimary() {
|
|
648
|
+
this.branchType = "primary";
|
|
649
|
+
this.context = "full";
|
|
650
|
+
this.completionMode = "turn_end";
|
|
651
|
+
return this;
|
|
652
|
+
}
|
|
653
|
+
rootBranchReview() {
|
|
654
|
+
this.branchType = "root_branch";
|
|
655
|
+
this.context = "full";
|
|
656
|
+
this.completionMode = "turn_end";
|
|
657
|
+
this.restorePrimaryLeafAfterTurn = true;
|
|
658
|
+
return this;
|
|
659
|
+
}
|
|
660
|
+
continueFromPrimaryLeaf() {
|
|
661
|
+
this.startFrom = {
|
|
662
|
+
kind: "semantic_ref",
|
|
663
|
+
ref: "currentPrimaryPathLeaf",
|
|
664
|
+
fallback: { kind: "current_leaf" },
|
|
665
|
+
};
|
|
666
|
+
return this;
|
|
667
|
+
}
|
|
668
|
+
continueFromReviewBranch() {
|
|
669
|
+
this.startFrom = {
|
|
670
|
+
kind: "semantic_ref",
|
|
671
|
+
ref: "review",
|
|
672
|
+
fallback: { kind: "current_leaf" },
|
|
673
|
+
};
|
|
674
|
+
return this;
|
|
675
|
+
}
|
|
676
|
+
continueFromProductBranch(productName, fallback = { kind: "current_leaf" }) {
|
|
677
|
+
this.startFrom = {
|
|
678
|
+
kind: "product_ref",
|
|
679
|
+
productName: normalizeProductName(productName),
|
|
680
|
+
fallback,
|
|
681
|
+
};
|
|
682
|
+
return this;
|
|
683
|
+
}
|
|
684
|
+
startFromReviewBranch() {
|
|
685
|
+
this.startFrom = {
|
|
686
|
+
kind: "semantic_ref",
|
|
687
|
+
ref: "review",
|
|
688
|
+
fallback: { kind: "session_root" },
|
|
689
|
+
};
|
|
690
|
+
return this;
|
|
691
|
+
}
|
|
692
|
+
startFromProductBranch(productName, fallback = { kind: "session_root" }) {
|
|
693
|
+
this.startFrom = {
|
|
694
|
+
kind: "product_ref",
|
|
695
|
+
productName: normalizeProductName(productName),
|
|
696
|
+
fallback,
|
|
697
|
+
};
|
|
698
|
+
return this;
|
|
699
|
+
}
|
|
700
|
+
startFromRoot() {
|
|
701
|
+
this.startFrom = { kind: "session_root" };
|
|
702
|
+
return this;
|
|
703
|
+
}
|
|
704
|
+
reviews(subject) {
|
|
705
|
+
this.reviewSubject = subject;
|
|
706
|
+
return this;
|
|
707
|
+
}
|
|
708
|
+
consume(productName) {
|
|
709
|
+
const normalized = normalizeProductName(productName);
|
|
710
|
+
if (!this.consumedProductNames.includes(normalized)) {
|
|
711
|
+
this.consumedProductNames.push(normalized);
|
|
712
|
+
}
|
|
713
|
+
return this;
|
|
714
|
+
}
|
|
715
|
+
optionalConsume(productName) {
|
|
716
|
+
const normalized = normalizeProductName(productName);
|
|
717
|
+
if (!this.optionalConsumedProductNames.includes(normalized)) {
|
|
718
|
+
this.optionalConsumedProductNames.push(normalized);
|
|
719
|
+
}
|
|
720
|
+
return this;
|
|
721
|
+
}
|
|
722
|
+
publish(productName) {
|
|
723
|
+
const normalized = normalizeProductName(productName);
|
|
724
|
+
if (this.publishedResult) {
|
|
725
|
+
throw new Error(`LLM turn '${this.turnId}' can publish only one product in flow v1`);
|
|
726
|
+
}
|
|
727
|
+
if (this.endResult) {
|
|
728
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare both .publish(...) and .end(...)`);
|
|
729
|
+
}
|
|
730
|
+
const builder = new PublishedResultBuilder(normalized, this);
|
|
731
|
+
this.publishedResult = { productName: normalized, builder };
|
|
732
|
+
return builder;
|
|
733
|
+
}
|
|
734
|
+
end(outcomeId) {
|
|
735
|
+
if (this.endResult) {
|
|
736
|
+
throw new Error(`LLM turn '${this.turnId}' declares duplicate .end(...) completion`);
|
|
737
|
+
}
|
|
738
|
+
if (this.publishedResult) {
|
|
739
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare both .publish(...) and .end(...)`);
|
|
740
|
+
}
|
|
741
|
+
if (this.outcomeToolBuilders.size > 0) {
|
|
742
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare both .end(...) and outcome tools`);
|
|
743
|
+
}
|
|
744
|
+
this.endResult = new LlmTurnEndBuilder(outcomeId, this);
|
|
745
|
+
return this.endResult;
|
|
746
|
+
}
|
|
747
|
+
buildPrompt(fn) {
|
|
748
|
+
this.promptBuilder = (ctx) => fn(createFlowPromptContext(ctx, this.consumedProductNames, this.optionalConsumedProductNames));
|
|
749
|
+
return this;
|
|
750
|
+
}
|
|
751
|
+
prompt(prompt) {
|
|
752
|
+
this.promptBuilder = prompt;
|
|
753
|
+
return this;
|
|
754
|
+
}
|
|
755
|
+
outcomeTool(id, configure) {
|
|
756
|
+
if (id.trim() === "") {
|
|
757
|
+
throw new Error(`LLM turn '${this.turnId}' declares an empty outcome tool id`);
|
|
758
|
+
}
|
|
759
|
+
if (this.endResult) {
|
|
760
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare both .end(...) and outcome tools`);
|
|
761
|
+
}
|
|
762
|
+
if (this.publishedResult) {
|
|
763
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare outcome tools after .publish(...)`);
|
|
764
|
+
}
|
|
765
|
+
if (this.outcomeToolBuilders.has(id)) {
|
|
766
|
+
throw new Error(`LLM turn '${this.turnId}' declares duplicate outcome tool '${id}'`);
|
|
767
|
+
}
|
|
768
|
+
const builder = new OutcomeToolBuilder();
|
|
769
|
+
configure(builder);
|
|
770
|
+
this.outcomeToolBuilders.set(id, builder);
|
|
771
|
+
return this;
|
|
772
|
+
}
|
|
773
|
+
buildBaseDefinition() {
|
|
774
|
+
if (!this.turnDescription) {
|
|
775
|
+
throw new Error(`LLM turn '${this.turnId}' must declare .description(...)`);
|
|
776
|
+
}
|
|
777
|
+
const prompt = this.promptBuilder;
|
|
778
|
+
if (!prompt) {
|
|
779
|
+
throw new Error(`LLM turn '${this.turnId}' must declare .buildPrompt(...)`);
|
|
780
|
+
}
|
|
781
|
+
return {
|
|
782
|
+
kind: "llm",
|
|
783
|
+
description: this.turnDescription,
|
|
784
|
+
...(this.modelPurposeValue ? { modelPurpose: this.modelPurposeValue } : {}),
|
|
785
|
+
availableTools: this.availableTools,
|
|
786
|
+
...(this.questionsEnabled ? { askQuestions: true } : {}),
|
|
787
|
+
completionMode: this.completionMode,
|
|
788
|
+
branchType: this.branchType,
|
|
789
|
+
context: this.context,
|
|
790
|
+
...(this.startFrom ? { startFrom: this.startFrom } : {}),
|
|
791
|
+
...(this.restorePrimaryLeafAfterTurn !== undefined
|
|
792
|
+
? { restorePrimaryLeafAfterTurn: this.restorePrimaryLeafAfterTurn }
|
|
793
|
+
: {}),
|
|
794
|
+
prompt,
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
producesPlan(configure) {
|
|
798
|
+
const definition = this.buildBaseDefinition();
|
|
799
|
+
const plan = configure(new PlanResultBuilder());
|
|
800
|
+
const outcome = plan.buildOutcome();
|
|
801
|
+
return {
|
|
802
|
+
id: this.turnId,
|
|
803
|
+
definition: {
|
|
804
|
+
...definition,
|
|
805
|
+
outcomes: {
|
|
806
|
+
[DEFAULT_PLAN_RESULT_OUTCOME_ID]: outcome,
|
|
807
|
+
},
|
|
808
|
+
turnResultMarkdown: OUTCOME_TOOL_ARGUMENT_TURN_RESULT,
|
|
809
|
+
resultSemanticRef: "plan",
|
|
810
|
+
publishedProduct: "plan",
|
|
811
|
+
},
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
buildDefinition() {
|
|
815
|
+
const definition = this.buildBaseDefinition();
|
|
816
|
+
const hasOutcomeTools = this.outcomeToolBuilders.size > 0;
|
|
817
|
+
const explicitTurnEnd = this.endResult;
|
|
818
|
+
const published = this.publishedResult;
|
|
819
|
+
const outcomes = {};
|
|
820
|
+
for (const [outcomeId, builder] of this.outcomeToolBuilders) {
|
|
821
|
+
outcomes[outcomeId] = builder.build();
|
|
822
|
+
}
|
|
823
|
+
const hasOutcomePublishedProduct = Object.values(outcomes).some((outcome) => outcome.publishedProduct);
|
|
824
|
+
let turnEnd;
|
|
825
|
+
let resultSemanticRef = this.reviewSubject
|
|
826
|
+
? "review"
|
|
827
|
+
: undefined;
|
|
828
|
+
let turnResultMarkdown;
|
|
829
|
+
let publishedProduct;
|
|
830
|
+
if (published) {
|
|
831
|
+
publishedProduct = published.productName;
|
|
832
|
+
if (published.productName === "plan") {
|
|
833
|
+
resultSemanticRef = "plan";
|
|
834
|
+
}
|
|
835
|
+
if (published.productName === "review") {
|
|
836
|
+
resultSemanticRef = "review";
|
|
837
|
+
}
|
|
838
|
+
const publishHasRoute = published.builder.hasRoute();
|
|
839
|
+
if (publishHasRoute && hasOutcomeTools) {
|
|
840
|
+
throw new Error(`LLM turn '${this.turnId}' cannot route published product '${published.productName}' and declare outcome tools in flow v1`);
|
|
841
|
+
}
|
|
842
|
+
if (publishHasRoute && !hasOutcomeTools) {
|
|
843
|
+
turnResultMarkdown = ASSISTANT_OUTPUT_TURN_RESULT;
|
|
844
|
+
turnEnd = published.builder.buildTurnEnd();
|
|
845
|
+
}
|
|
846
|
+
if (!publishHasRoute && hasOutcomeTools) {
|
|
847
|
+
turnResultMarkdown = OUTCOME_TOOL_ARGUMENT_TURN_RESULT;
|
|
848
|
+
}
|
|
849
|
+
if (!publishHasRoute && !hasOutcomeTools) {
|
|
850
|
+
throw new Error(`LLM turn '${this.turnId}' publishes product '${published.productName}' but declares no route or outcome tool completion path`);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
else if (hasOutcomeTools && !hasOutcomePublishedProduct) {
|
|
854
|
+
turnResultMarkdown = OUTCOME_TOOL_ARGUMENT_TURN_RESULT;
|
|
855
|
+
}
|
|
856
|
+
else if (explicitTurnEnd) {
|
|
857
|
+
turnResultMarkdown = ASSISTANT_OUTPUT_TURN_RESULT;
|
|
858
|
+
}
|
|
859
|
+
if (hasOutcomeTools) {
|
|
860
|
+
for (const [outcomeId, outcome] of Object.entries(outcomes)) {
|
|
861
|
+
if (OUTCOME_TOOL_MARKDOWN_PARAMETER_NAME in outcome.parameters) {
|
|
862
|
+
throw new Error(`LLM turn '${this.turnId}' outcome tool '${outcomeId}' cannot declare reserved markdown parameter '${OUTCOME_TOOL_MARKDOWN_PARAMETER_NAME}'`);
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
if (explicitTurnEnd) {
|
|
867
|
+
if (turnEnd || Object.keys(outcomes).length > 0) {
|
|
868
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare multiple completion paths`);
|
|
869
|
+
}
|
|
870
|
+
turnEnd = explicitTurnEnd.build();
|
|
871
|
+
}
|
|
872
|
+
if (Object.keys(outcomes).length === 0 && !turnEnd) {
|
|
873
|
+
throw new Error(`LLM turn '${this.turnId}' must declare a completion path`);
|
|
874
|
+
}
|
|
875
|
+
return {
|
|
876
|
+
...definition,
|
|
877
|
+
...(this.reviewSubject ? { reviewSubject: this.reviewSubject } : {}),
|
|
878
|
+
...(Object.keys(outcomes).length > 0 ? { outcomes } : {}),
|
|
879
|
+
...(turnEnd ? { turnEnd } : {}),
|
|
880
|
+
...(turnResultMarkdown ? { turnResultMarkdown } : {}),
|
|
881
|
+
...(resultSemanticRef ? { resultSemanticRef } : {}),
|
|
882
|
+
...(publishedProduct ? { publishedProduct } : {}),
|
|
883
|
+
...(this.consumedProductNames.length > 0
|
|
884
|
+
? { consumedProducts: [...this.consumedProductNames] }
|
|
885
|
+
: {}),
|
|
886
|
+
...(this.optionalConsumedProductNames.length > 0
|
|
887
|
+
? { optionalConsumedProducts: [...this.optionalConsumedProductNames] }
|
|
888
|
+
: {}),
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
export class AutomaticFlowBuilder {
|
|
893
|
+
turnId;
|
|
894
|
+
turnDescription = null;
|
|
895
|
+
runFn = null;
|
|
896
|
+
outcomeBuilders = new Map();
|
|
897
|
+
constructor(turnId) {
|
|
898
|
+
this.turnId = turnId;
|
|
899
|
+
}
|
|
900
|
+
get id() {
|
|
901
|
+
return this.turnId;
|
|
902
|
+
}
|
|
903
|
+
get definition() {
|
|
904
|
+
return this.buildDefinition();
|
|
905
|
+
}
|
|
906
|
+
description(description) {
|
|
907
|
+
this.turnDescription = description;
|
|
908
|
+
return this;
|
|
909
|
+
}
|
|
910
|
+
run(fn) {
|
|
911
|
+
this.runFn = fn;
|
|
912
|
+
return this;
|
|
913
|
+
}
|
|
914
|
+
outcome(id, configure) {
|
|
915
|
+
if (id.trim() === "") {
|
|
916
|
+
throw new Error(`Automatic turn '${this.turnId}' declares an empty outcome id`);
|
|
917
|
+
}
|
|
918
|
+
if (this.outcomeBuilders.has(id)) {
|
|
919
|
+
throw new Error(`Automatic turn '${this.turnId}' declares duplicate outcome '${id}'`);
|
|
920
|
+
}
|
|
921
|
+
const builder = new AutomaticOutcomeBuilder();
|
|
922
|
+
configure(builder);
|
|
923
|
+
this.outcomeBuilders.set(id, builder);
|
|
924
|
+
return this;
|
|
925
|
+
}
|
|
926
|
+
buildDefinition() {
|
|
927
|
+
if (!this.turnDescription) {
|
|
928
|
+
throw new Error(`Automatic turn '${this.turnId}' must declare .description(...)`);
|
|
929
|
+
}
|
|
930
|
+
if (!this.runFn) {
|
|
931
|
+
throw new Error(`Automatic turn '${this.turnId}' must declare .run(...)`);
|
|
932
|
+
}
|
|
933
|
+
if (this.outcomeBuilders.size === 0) {
|
|
934
|
+
throw new Error(`Automatic turn '${this.turnId}' must declare at least one outcome`);
|
|
935
|
+
}
|
|
936
|
+
const outcomes = {};
|
|
937
|
+
for (const [outcomeId, builder] of this.outcomeBuilders) {
|
|
938
|
+
outcomes[outcomeId] = builder.build();
|
|
939
|
+
}
|
|
940
|
+
const runFn = this.runFn;
|
|
941
|
+
return {
|
|
942
|
+
kind: "automatic",
|
|
943
|
+
description: this.turnDescription,
|
|
944
|
+
outcomes,
|
|
945
|
+
run: (ctx) => runFn(createFlowAutomaticRunContext(ctx)),
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
export class ServerAutomaticFlowBuilder {
|
|
950
|
+
turnId;
|
|
951
|
+
turnDescription = null;
|
|
952
|
+
restartBehaviorValue;
|
|
953
|
+
runFn = null;
|
|
954
|
+
outcomeBuilders = new Map();
|
|
955
|
+
constructor(turnId) {
|
|
956
|
+
this.turnId = turnId;
|
|
957
|
+
}
|
|
958
|
+
get id() {
|
|
959
|
+
return this.turnId;
|
|
960
|
+
}
|
|
961
|
+
get definition() {
|
|
962
|
+
return this.buildDefinition();
|
|
963
|
+
}
|
|
964
|
+
description(description) {
|
|
965
|
+
this.turnDescription = description;
|
|
966
|
+
return this;
|
|
967
|
+
}
|
|
968
|
+
run(fn) {
|
|
969
|
+
this.runFn = fn;
|
|
970
|
+
return this;
|
|
971
|
+
}
|
|
972
|
+
restartBehavior(behavior) {
|
|
973
|
+
this.restartBehaviorValue = behavior;
|
|
974
|
+
return this;
|
|
975
|
+
}
|
|
976
|
+
outcome(id, configure) {
|
|
977
|
+
if (id.trim() === "") {
|
|
978
|
+
throw new Error(`Server automatic turn '${this.turnId}' declares an empty outcome id`);
|
|
979
|
+
}
|
|
980
|
+
if (this.outcomeBuilders.has(id)) {
|
|
981
|
+
throw new Error(`Server automatic turn '${this.turnId}' declares duplicate outcome '${id}'`);
|
|
982
|
+
}
|
|
983
|
+
const builder = new AutomaticOutcomeBuilder();
|
|
984
|
+
configure(builder);
|
|
985
|
+
this.outcomeBuilders.set(id, builder);
|
|
986
|
+
return this;
|
|
987
|
+
}
|
|
988
|
+
buildDefinition() {
|
|
989
|
+
if (!this.turnDescription) {
|
|
990
|
+
throw new Error(`Server automatic turn '${this.turnId}' must declare .description(...)`);
|
|
991
|
+
}
|
|
992
|
+
if (!this.runFn) {
|
|
993
|
+
throw new Error(`Server automatic turn '${this.turnId}' must declare .run(...)`);
|
|
994
|
+
}
|
|
995
|
+
if (this.outcomeBuilders.size === 0) {
|
|
996
|
+
throw new Error(`Server automatic turn '${this.turnId}' must declare at least one outcome`);
|
|
997
|
+
}
|
|
998
|
+
const outcomes = {};
|
|
999
|
+
for (const [outcomeId, builder] of this.outcomeBuilders) {
|
|
1000
|
+
outcomes[outcomeId] = builder.build();
|
|
1001
|
+
}
|
|
1002
|
+
const runFn = this.runFn;
|
|
1003
|
+
return {
|
|
1004
|
+
kind: "server_automatic",
|
|
1005
|
+
description: this.turnDescription,
|
|
1006
|
+
outcomes,
|
|
1007
|
+
...(this.restartBehaviorValue ? { restartBehavior: this.restartBehaviorValue } : {}),
|
|
1008
|
+
run: (ctx) => runFn(createFlowAutomaticRunContext(ctx)),
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
function formFieldPublishedProductName(field) {
|
|
1013
|
+
if (!field.publish) {
|
|
1014
|
+
return null;
|
|
1015
|
+
}
|
|
1016
|
+
return normalizeProductName(typeof field.publish === "object" && field.publish.product ? field.publish.product : field.id);
|
|
1017
|
+
}
|
|
1018
|
+
function inferActionLabel(actionId) {
|
|
1019
|
+
return humanizeProcessLabel(actionId);
|
|
1020
|
+
}
|
|
1021
|
+
function inferAcceptanceState(actionId) {
|
|
1022
|
+
const normalized = actionId.toLowerCase();
|
|
1023
|
+
if (/approve|accept|complete|merged/.test(normalized)) {
|
|
1024
|
+
return "accepted";
|
|
1025
|
+
}
|
|
1026
|
+
if (/request|revision|feedback|change|reject/.test(normalized)) {
|
|
1027
|
+
return "requires_changes";
|
|
1028
|
+
}
|
|
1029
|
+
return "neutral";
|
|
1030
|
+
}
|
|
1031
|
+
export class HumanActionBuilder {
|
|
1032
|
+
actionId;
|
|
1033
|
+
spec = {};
|
|
1034
|
+
constructor(actionId) {
|
|
1035
|
+
this.actionId = actionId;
|
|
1036
|
+
}
|
|
1037
|
+
label(label) {
|
|
1038
|
+
this.spec.label = label;
|
|
1039
|
+
return this;
|
|
1040
|
+
}
|
|
1041
|
+
description(description) {
|
|
1042
|
+
this.spec.description = description;
|
|
1043
|
+
return this;
|
|
1044
|
+
}
|
|
1045
|
+
acceptanceState(state) {
|
|
1046
|
+
this.spec.acceptanceState = state;
|
|
1047
|
+
return this;
|
|
1048
|
+
}
|
|
1049
|
+
form(form) {
|
|
1050
|
+
this.spec.form = form;
|
|
1051
|
+
return this;
|
|
1052
|
+
}
|
|
1053
|
+
to(turnId) {
|
|
1054
|
+
this.spec.to = turnId;
|
|
1055
|
+
return this;
|
|
1056
|
+
}
|
|
1057
|
+
complete() {
|
|
1058
|
+
this.spec.complete = true;
|
|
1059
|
+
return this;
|
|
1060
|
+
}
|
|
1061
|
+
lifecycleStatus(status) {
|
|
1062
|
+
this.spec.lifecycleStatus = status;
|
|
1063
|
+
return this;
|
|
1064
|
+
}
|
|
1065
|
+
trigger(trigger) {
|
|
1066
|
+
this.spec.trigger = trigger;
|
|
1067
|
+
return this;
|
|
1068
|
+
}
|
|
1069
|
+
schedulable(value = true) {
|
|
1070
|
+
this.spec.schedulable = value;
|
|
1071
|
+
return this;
|
|
1072
|
+
}
|
|
1073
|
+
effect(effect) {
|
|
1074
|
+
this.spec.effect = effect;
|
|
1075
|
+
return this;
|
|
1076
|
+
}
|
|
1077
|
+
build() {
|
|
1078
|
+
return {
|
|
1079
|
+
label: typeof this.spec.label === "string" ? this.spec.label : inferActionLabel(this.actionId),
|
|
1080
|
+
acceptanceState: this.spec.acceptanceState ?? inferAcceptanceState(this.actionId),
|
|
1081
|
+
...this.spec,
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
export class ExternalActionBuilder {
|
|
1086
|
+
spec;
|
|
1087
|
+
constructor(actionId, source) {
|
|
1088
|
+
this.spec = { id: actionId, source };
|
|
1089
|
+
}
|
|
1090
|
+
label(label) {
|
|
1091
|
+
this.spec.label = label;
|
|
1092
|
+
return this;
|
|
1093
|
+
}
|
|
1094
|
+
description(description) {
|
|
1095
|
+
this.spec.description = description;
|
|
1096
|
+
return this;
|
|
1097
|
+
}
|
|
1098
|
+
to(turnId) {
|
|
1099
|
+
this.spec.to = turnId;
|
|
1100
|
+
return this;
|
|
1101
|
+
}
|
|
1102
|
+
complete() {
|
|
1103
|
+
this.spec.complete = true;
|
|
1104
|
+
return this;
|
|
1105
|
+
}
|
|
1106
|
+
lifecycleStatus(status) {
|
|
1107
|
+
this.spec.lifecycleStatus = status;
|
|
1108
|
+
return this;
|
|
1109
|
+
}
|
|
1110
|
+
publishInput(productName, options) {
|
|
1111
|
+
this.spec.publishInput = {
|
|
1112
|
+
productName: normalizeProductName(productName),
|
|
1113
|
+
inputField: options.inputField,
|
|
1114
|
+
};
|
|
1115
|
+
return this;
|
|
1116
|
+
}
|
|
1117
|
+
build() {
|
|
1118
|
+
return this.spec;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
export class HumanFlowBuilder {
|
|
1122
|
+
turnId;
|
|
1123
|
+
turnDescription = null;
|
|
1124
|
+
reviewSubject = null;
|
|
1125
|
+
reviewProductName;
|
|
1126
|
+
reviewSemanticRef;
|
|
1127
|
+
operatorAttentionValue;
|
|
1128
|
+
turnCommentary;
|
|
1129
|
+
notes;
|
|
1130
|
+
actions = new Map();
|
|
1131
|
+
externalActionBuilders = new Map();
|
|
1132
|
+
constructor(turnId) {
|
|
1133
|
+
this.turnId = turnId;
|
|
1134
|
+
}
|
|
1135
|
+
get id() {
|
|
1136
|
+
return this.turnId;
|
|
1137
|
+
}
|
|
1138
|
+
get definition() {
|
|
1139
|
+
if (!this.turnDescription) {
|
|
1140
|
+
throw new Error(`Human turn '${this.turnId}' must declare .description(...)`);
|
|
1141
|
+
}
|
|
1142
|
+
return {
|
|
1143
|
+
kind: "human",
|
|
1144
|
+
description: this.turnDescription,
|
|
1145
|
+
...(this.reviewSubject ? { reviewSubject: this.reviewSubject } : {}),
|
|
1146
|
+
...(this.reviewProductName ? { reviewProduct: this.reviewProductName } : {}),
|
|
1147
|
+
...(this.reviewSemanticRef ? { reviewSemanticRef: this.reviewSemanticRef } : {}),
|
|
1148
|
+
...(this.operatorAttentionValue ? { operatorAttention: this.operatorAttentionValue } : {}),
|
|
1149
|
+
...(this.notes ? { notesFields: this.notes } : {}),
|
|
1150
|
+
...(this.turnCommentary ? { commentary: this.turnCommentary } : {}),
|
|
1151
|
+
actions: Object.fromEntries([...this.actions.entries()].map(([actionId, builder]) => [actionId, builder.build()])),
|
|
1152
|
+
...(this.externalActionBuilders.size > 0
|
|
1153
|
+
? {
|
|
1154
|
+
externalActions: Object.fromEntries([...this.externalActionBuilders.entries()].map(([actionId, builder]) => [
|
|
1155
|
+
actionId,
|
|
1156
|
+
builder.build(),
|
|
1157
|
+
])),
|
|
1158
|
+
}
|
|
1159
|
+
: {}),
|
|
1160
|
+
};
|
|
1161
|
+
}
|
|
1162
|
+
description(description) {
|
|
1163
|
+
this.turnDescription = description;
|
|
1164
|
+
return this;
|
|
1165
|
+
}
|
|
1166
|
+
review(subject) {
|
|
1167
|
+
this.reviewSubject = typeof subject === "string" ? createReviewSubject(subject) : subject;
|
|
1168
|
+
this.reviewProductName = undefined;
|
|
1169
|
+
this.reviewSemanticRef = typeof subject === "string" && subject === "plan" ? "plan" : undefined;
|
|
1170
|
+
return this;
|
|
1171
|
+
}
|
|
1172
|
+
reviewProduct(productName, options) {
|
|
1173
|
+
const normalized = normalizeProductName(productName);
|
|
1174
|
+
this.reviewProductName = normalized;
|
|
1175
|
+
this.reviewSubject =
|
|
1176
|
+
typeof options.subject === "string" ? createReviewSubject(options.subject) : options.subject;
|
|
1177
|
+
this.reviewSemanticRef =
|
|
1178
|
+
normalized === "plan" ? "plan" : normalized === "review" ? "review" : undefined;
|
|
1179
|
+
return this;
|
|
1180
|
+
}
|
|
1181
|
+
operatorAttention(attention) {
|
|
1182
|
+
this.operatorAttentionValue = attention;
|
|
1183
|
+
return this;
|
|
1184
|
+
}
|
|
1185
|
+
commentary(commentary) {
|
|
1186
|
+
this.turnCommentary = commentary;
|
|
1187
|
+
return this;
|
|
1188
|
+
}
|
|
1189
|
+
notesFields(notes) {
|
|
1190
|
+
this.notes = notes;
|
|
1191
|
+
return this;
|
|
1192
|
+
}
|
|
1193
|
+
action(actionId, configure) {
|
|
1194
|
+
if (this.actions.has(actionId)) {
|
|
1195
|
+
throw new Error(`Human turn '${this.turnId}' declares duplicate action '${actionId}'`);
|
|
1196
|
+
}
|
|
1197
|
+
const builder = new HumanActionBuilder(actionId);
|
|
1198
|
+
configure(builder);
|
|
1199
|
+
this.actions.set(actionId, builder);
|
|
1200
|
+
return this;
|
|
1201
|
+
}
|
|
1202
|
+
externalAction(externalActionId, source, configure) {
|
|
1203
|
+
if (this.externalActionBuilders.has(externalActionId)) {
|
|
1204
|
+
throw new Error(`Human turn '${this.turnId}' declares duplicate external action '${externalActionId}'`);
|
|
1205
|
+
}
|
|
1206
|
+
const builder = new ExternalActionBuilder(externalActionId, source);
|
|
1207
|
+
configure(builder);
|
|
1208
|
+
this.externalActionBuilders.set(externalActionId, builder);
|
|
1209
|
+
return this;
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
export class ExternalRouteBuilder {
|
|
1213
|
+
parent;
|
|
1214
|
+
source;
|
|
1215
|
+
externalEffect;
|
|
1216
|
+
target = null;
|
|
1217
|
+
constructor(parent, source) {
|
|
1218
|
+
this.parent = parent;
|
|
1219
|
+
this.source = source;
|
|
1220
|
+
}
|
|
1221
|
+
get id() {
|
|
1222
|
+
return this.parent.id;
|
|
1223
|
+
}
|
|
1224
|
+
get definition() {
|
|
1225
|
+
return this.parent.definition;
|
|
1226
|
+
}
|
|
1227
|
+
setTarget(next) {
|
|
1228
|
+
if (this.target) {
|
|
1229
|
+
throw new Error("External source route already declares a target");
|
|
1230
|
+
}
|
|
1231
|
+
this.target = next;
|
|
1232
|
+
return this;
|
|
1233
|
+
}
|
|
1234
|
+
to(turnId) {
|
|
1235
|
+
return this.setTarget({ kind: "to", turnId });
|
|
1236
|
+
}
|
|
1237
|
+
complete() {
|
|
1238
|
+
return this.setTarget({ kind: "complete" });
|
|
1239
|
+
}
|
|
1240
|
+
lifecycleStatus(status) {
|
|
1241
|
+
return this.setTarget({ kind: "lifecycleStatus", status });
|
|
1242
|
+
}
|
|
1243
|
+
state(fn) {
|
|
1244
|
+
this.externalEffect = async (ctx) => ({
|
|
1245
|
+
state: await fn({ ctx, event: ctx.event, input: ctx.input }),
|
|
1246
|
+
});
|
|
1247
|
+
return this;
|
|
1248
|
+
}
|
|
1249
|
+
effect(fn) {
|
|
1250
|
+
this.externalEffect = (ctx) => fn({ ctx, event: ctx.event, input: ctx.input });
|
|
1251
|
+
return this;
|
|
1252
|
+
}
|
|
1253
|
+
buildRouteTarget() {
|
|
1254
|
+
return buildRouteTargetSpec(this.target);
|
|
1255
|
+
}
|
|
1256
|
+
build() {
|
|
1257
|
+
return {
|
|
1258
|
+
source: this.source,
|
|
1259
|
+
...this.buildRouteTarget(),
|
|
1260
|
+
...(this.externalEffect ? { effect: this.externalEffect } : {}),
|
|
1261
|
+
};
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
export class ExternalFlowBuilder {
|
|
1265
|
+
turnId;
|
|
1266
|
+
turnDescription = null;
|
|
1267
|
+
routeBuilders = [];
|
|
1268
|
+
constructor(turnId) {
|
|
1269
|
+
this.turnId = turnId;
|
|
1270
|
+
}
|
|
1271
|
+
get id() {
|
|
1272
|
+
return this.turnId;
|
|
1273
|
+
}
|
|
1274
|
+
get definition() {
|
|
1275
|
+
if (!this.turnDescription) {
|
|
1276
|
+
this.turnDescription =
|
|
1277
|
+
this.routeBuilders[0]?.build().source.description ?? "Wait for an external source";
|
|
1278
|
+
}
|
|
1279
|
+
if (this.routeBuilders.length === 0) {
|
|
1280
|
+
throw new Error(`External turn '${this.turnId}' must declare at least one .from(...) source`);
|
|
1281
|
+
}
|
|
1282
|
+
return {
|
|
1283
|
+
kind: "external",
|
|
1284
|
+
description: this.turnDescription,
|
|
1285
|
+
transitions: this.routeBuilders.map((builder) => builder.build()),
|
|
1286
|
+
};
|
|
1287
|
+
}
|
|
1288
|
+
description(description) {
|
|
1289
|
+
this.turnDescription = description;
|
|
1290
|
+
return this;
|
|
1291
|
+
}
|
|
1292
|
+
from(source) {
|
|
1293
|
+
const builder = new ExternalRouteBuilder(this, source);
|
|
1294
|
+
this.routeBuilders.push(builder);
|
|
1295
|
+
return builder;
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
export class FlowComponentBuilder {
|
|
1299
|
+
turns = [];
|
|
1300
|
+
serverHooks = [];
|
|
1301
|
+
uiHooks = [];
|
|
1302
|
+
launcherHooks = [];
|
|
1303
|
+
watcherHooks = [];
|
|
1304
|
+
turn(turn) {
|
|
1305
|
+
this.turns.push(turn);
|
|
1306
|
+
return this;
|
|
1307
|
+
}
|
|
1308
|
+
server(hook) {
|
|
1309
|
+
this.serverHooks.push(hook);
|
|
1310
|
+
return this;
|
|
1311
|
+
}
|
|
1312
|
+
action(definition) {
|
|
1313
|
+
this.serverHooks.push((api) => api.action(definition));
|
|
1314
|
+
return this;
|
|
1315
|
+
}
|
|
1316
|
+
ui(hook) {
|
|
1317
|
+
this.uiHooks.push(hook);
|
|
1318
|
+
return this;
|
|
1319
|
+
}
|
|
1320
|
+
launcher(definitionOrHook) {
|
|
1321
|
+
if (typeof definitionOrHook === "function") {
|
|
1322
|
+
this.launcherHooks.push(definitionOrHook);
|
|
1323
|
+
}
|
|
1324
|
+
else {
|
|
1325
|
+
this.launcherHooks.push((api) => api.launcher(definitionOrHook));
|
|
1326
|
+
}
|
|
1327
|
+
return this;
|
|
1328
|
+
}
|
|
1329
|
+
watcher(definitionOrHook) {
|
|
1330
|
+
if (typeof definitionOrHook === "function") {
|
|
1331
|
+
this.watcherHooks.push(definitionOrHook);
|
|
1332
|
+
}
|
|
1333
|
+
else {
|
|
1334
|
+
this.watcherHooks.push((api) => api.watcher(definitionOrHook));
|
|
1335
|
+
}
|
|
1336
|
+
return this;
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
export class FlowFragmentBuilder extends FlowComponentBuilder {
|
|
1340
|
+
name;
|
|
1341
|
+
constructor(name) {
|
|
1342
|
+
super();
|
|
1343
|
+
if (name.trim() === "") {
|
|
1344
|
+
throw new Error("Flow fragment name must be non-empty");
|
|
1345
|
+
}
|
|
1346
|
+
this.name = name;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
export class FlowProcessBuilder extends FlowComponentBuilder {
|
|
1350
|
+
processId;
|
|
1351
|
+
processDisplayName = null;
|
|
1352
|
+
entryTurnId = null;
|
|
1353
|
+
alternateEntryTurnIds = [];
|
|
1354
|
+
happyPathTurnIds = null;
|
|
1355
|
+
paramsCodec = null;
|
|
1356
|
+
stateCodec = null;
|
|
1357
|
+
initialStateFn = null;
|
|
1358
|
+
repositoryCredentialsFn;
|
|
1359
|
+
processPiConfig;
|
|
1360
|
+
constructor(processId) {
|
|
1361
|
+
super();
|
|
1362
|
+
if (processId.trim() === "") {
|
|
1363
|
+
throw new Error("Flow process id must be non-empty");
|
|
1364
|
+
}
|
|
1365
|
+
this.processId = processId;
|
|
1366
|
+
}
|
|
1367
|
+
displayName(displayName) {
|
|
1368
|
+
this.processDisplayName = displayName;
|
|
1369
|
+
return this;
|
|
1370
|
+
}
|
|
1371
|
+
entry(turnId) {
|
|
1372
|
+
this.entryTurnId = turnId;
|
|
1373
|
+
return this;
|
|
1374
|
+
}
|
|
1375
|
+
/** Declare an additional turn that launchers may select as the first turn. */
|
|
1376
|
+
alternateEntry(turnId) {
|
|
1377
|
+
this.alternateEntryTurnIds.push(turnId);
|
|
1378
|
+
return this;
|
|
1379
|
+
}
|
|
1380
|
+
/**
|
|
1381
|
+
* Declare the happy path: the ordered spine of turns a successful run walks
|
|
1382
|
+
* through. Flow diagrams render this as the main line. Must start at the entry
|
|
1383
|
+
* turn and reference only declared turns without repeats.
|
|
1384
|
+
*/
|
|
1385
|
+
happyPath(...turnIds) {
|
|
1386
|
+
this.happyPathTurnIds = turnIds;
|
|
1387
|
+
return this;
|
|
1388
|
+
}
|
|
1389
|
+
codecs(input) {
|
|
1390
|
+
this.paramsCodec = input.params;
|
|
1391
|
+
this.stateCodec = input.state;
|
|
1392
|
+
return this;
|
|
1393
|
+
}
|
|
1394
|
+
initialState(fn) {
|
|
1395
|
+
this.initialStateFn = fn;
|
|
1396
|
+
return this;
|
|
1397
|
+
}
|
|
1398
|
+
repositoryCredentials(fn) {
|
|
1399
|
+
this.repositoryCredentialsFn = fn;
|
|
1400
|
+
return this;
|
|
1401
|
+
}
|
|
1402
|
+
piConfig(config) {
|
|
1403
|
+
this.processPiConfig = config;
|
|
1404
|
+
return this;
|
|
1405
|
+
}
|
|
1406
|
+
use(fragment) {
|
|
1407
|
+
this.turns.push(...fragment.turns);
|
|
1408
|
+
this.serverHooks.push(...fragment.serverHooks);
|
|
1409
|
+
this.uiHooks.push(...fragment.uiHooks);
|
|
1410
|
+
this.launcherHooks.push(...fragment.launcherHooks);
|
|
1411
|
+
this.watcherHooks.push(...fragment.watcherHooks);
|
|
1412
|
+
return this;
|
|
1413
|
+
}
|
|
1414
|
+
define() {
|
|
1415
|
+
if (!this.processDisplayName) {
|
|
1416
|
+
throw new Error(`Flow process '${this.processId}' must declare .displayName(...)`);
|
|
1417
|
+
}
|
|
1418
|
+
if (!this.entryTurnId) {
|
|
1419
|
+
throw new Error(`Flow process '${this.processId}' must declare .entry(turnId)`);
|
|
1420
|
+
}
|
|
1421
|
+
if (!this.paramsCodec || !this.stateCodec) {
|
|
1422
|
+
throw new Error(`Flow process '${this.processId}' must declare .codecs(...)`);
|
|
1423
|
+
}
|
|
1424
|
+
if (!this.initialStateFn) {
|
|
1425
|
+
throw new Error(`Flow process '${this.processId}' must declare .initialState(...)`);
|
|
1426
|
+
}
|
|
1427
|
+
const turns = {};
|
|
1428
|
+
for (const turn of this.turns) {
|
|
1429
|
+
if (Object.hasOwn(turns, turn.id)) {
|
|
1430
|
+
throw new Error(`Flow process '${this.processId}' declares duplicate turn '${turn.id}'`);
|
|
1431
|
+
}
|
|
1432
|
+
turns[turn.id] = turn.definition;
|
|
1433
|
+
}
|
|
1434
|
+
const publishedProducts = new Set();
|
|
1435
|
+
for (const turnDef of Object.values(turns)) {
|
|
1436
|
+
if (turnDef.kind === "llm") {
|
|
1437
|
+
if (turnDef.publishedProduct) {
|
|
1438
|
+
normalizeProductName(turnDef.publishedProduct);
|
|
1439
|
+
publishedProducts.add(turnDef.publishedProduct);
|
|
1440
|
+
}
|
|
1441
|
+
for (const outcome of Object.values(turnDef.outcomes ?? {})) {
|
|
1442
|
+
if (outcome?.publishedProduct) {
|
|
1443
|
+
normalizeProductName(outcome.publishedProduct);
|
|
1444
|
+
publishedProducts.add(outcome.publishedProduct);
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
if (turnDef.kind === "human") {
|
|
1449
|
+
for (const action of Object.values(turnDef.actions)) {
|
|
1450
|
+
for (const field of action.form?.fields ?? []) {
|
|
1451
|
+
const productName = formFieldPublishedProductName(field);
|
|
1452
|
+
if (productName) {
|
|
1453
|
+
publishedProducts.add(productName);
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
for (const externalAction of Object.values(turnDef.externalActions ?? {})) {
|
|
1458
|
+
if (externalAction.publishInput) {
|
|
1459
|
+
publishedProducts.add(normalizeProductName(externalAction.publishInput.productName));
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
for (const [turnId, turnDef] of Object.entries(turns)) {
|
|
1465
|
+
if (turnDef.kind !== "llm") {
|
|
1466
|
+
continue;
|
|
1467
|
+
}
|
|
1468
|
+
for (const productName of [
|
|
1469
|
+
...(turnDef.consumedProducts ?? []),
|
|
1470
|
+
...(turnDef.optionalConsumedProducts ?? []),
|
|
1471
|
+
]) {
|
|
1472
|
+
normalizeProductName(productName);
|
|
1473
|
+
if (!publishedProducts.has(productName)) {
|
|
1474
|
+
throw new Error(`Flow process '${this.processId}' turn '${turnId}' consumes product '${productName}' that is never published by this process`);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
const input = {
|
|
1479
|
+
id: this.processId,
|
|
1480
|
+
displayName: this.processDisplayName,
|
|
1481
|
+
entry: this.entryTurnId,
|
|
1482
|
+
...(this.alternateEntryTurnIds.length > 0
|
|
1483
|
+
? { alternateEntries: [...this.alternateEntryTurnIds] }
|
|
1484
|
+
: {}),
|
|
1485
|
+
...(this.happyPathTurnIds ? { happyPath: [...this.happyPathTurnIds] } : {}),
|
|
1486
|
+
paramsCodec: this.paramsCodec,
|
|
1487
|
+
stateCodec: this.stateCodec,
|
|
1488
|
+
initialState: this.initialStateFn,
|
|
1489
|
+
...(this.repositoryCredentialsFn
|
|
1490
|
+
? { repositoryCredentials: this.repositoryCredentialsFn }
|
|
1491
|
+
: {}),
|
|
1492
|
+
...(this.processPiConfig ? { piConfig: this.processPiConfig } : {}),
|
|
1493
|
+
turns,
|
|
1494
|
+
...(this.serverHooks.length > 0
|
|
1495
|
+
? {
|
|
1496
|
+
server: (api) => {
|
|
1497
|
+
for (const hook of this.serverHooks) {
|
|
1498
|
+
hook(api);
|
|
1499
|
+
}
|
|
1500
|
+
},
|
|
1501
|
+
}
|
|
1502
|
+
: {}),
|
|
1503
|
+
...(this.uiHooks.length > 0
|
|
1504
|
+
? {
|
|
1505
|
+
ui: (api) => {
|
|
1506
|
+
for (const hook of this.uiHooks) {
|
|
1507
|
+
hook(api);
|
|
1508
|
+
}
|
|
1509
|
+
},
|
|
1510
|
+
}
|
|
1511
|
+
: {}),
|
|
1512
|
+
...(this.launcherHooks.length > 0
|
|
1513
|
+
? {
|
|
1514
|
+
launchers: (api) => {
|
|
1515
|
+
for (const hook of this.launcherHooks) {
|
|
1516
|
+
hook(api);
|
|
1517
|
+
}
|
|
1518
|
+
},
|
|
1519
|
+
}
|
|
1520
|
+
: {}),
|
|
1521
|
+
...(this.watcherHooks.length > 0
|
|
1522
|
+
? {
|
|
1523
|
+
watchers: (api) => {
|
|
1524
|
+
for (const hook of this.watcherHooks) {
|
|
1525
|
+
hook(api);
|
|
1526
|
+
}
|
|
1527
|
+
},
|
|
1528
|
+
}
|
|
1529
|
+
: {}),
|
|
1530
|
+
};
|
|
1531
|
+
return defineProcess(input);
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
export const flow = {
|
|
1535
|
+
llm(turnId) {
|
|
1536
|
+
return new LlmFlowBuilder(turnId);
|
|
1537
|
+
},
|
|
1538
|
+
automatic(turnId) {
|
|
1539
|
+
return new AutomaticFlowBuilder(turnId);
|
|
1540
|
+
},
|
|
1541
|
+
serverAutomatic(turnId) {
|
|
1542
|
+
return new ServerAutomaticFlowBuilder(turnId);
|
|
1543
|
+
},
|
|
1544
|
+
human(turnId) {
|
|
1545
|
+
return new HumanFlowBuilder(turnId);
|
|
1546
|
+
},
|
|
1547
|
+
external(turnId) {
|
|
1548
|
+
return new ExternalFlowBuilder(turnId);
|
|
1549
|
+
},
|
|
1550
|
+
fragment(name) {
|
|
1551
|
+
return new FlowFragmentBuilder(name);
|
|
1552
|
+
},
|
|
1553
|
+
process(processId) {
|
|
1554
|
+
return new FlowProcessBuilder(processId);
|
|
1555
|
+
},
|
|
1556
|
+
};
|
|
1557
|
+
//# sourceMappingURL=flow.js.map
|