@leitwerk-dev/process-sdk 0.1.9 → 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 +70 -34
- package/dist/core-capabilities.d.ts.map +1 -1
- package/dist/define-process.d.ts +17 -49
- package/dist/define-process.d.ts.map +1 -1
- package/dist/{extension-api-BBcwktmc.d.ts → extension-api-CcqZCYJx.d.ts} +136 -70
- package/dist/extension-api.d.ts +115 -7
- 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 +80 -104
- 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.map +1 -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 +2 -0
- 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 +81 -37
- package/src/define-process.ts +190 -313
- package/src/extension-api.ts +142 -6
- package/src/external-action-source.ts +12 -0
- package/src/external-source-poll.ts +49 -0
- package/src/flow.ts +332 -664
- package/src/fs-utils.ts +17 -2
- package/src/index.ts +54 -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 -51
- package/src/process-watcher-source.ts +3 -6
- 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 -4
- 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 -1078
- 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 -1645
- 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 -212
- 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 -52
- 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 -18
- 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/src/flow.ts
CHANGED
|
@@ -27,8 +27,6 @@ import type {
|
|
|
27
27
|
ProcessRuntimeTurnContext,
|
|
28
28
|
ProcessToolOutcomeSpec,
|
|
29
29
|
ProcessTurnEndSpec,
|
|
30
|
-
ServerAutomaticTurnDefinition,
|
|
31
|
-
ServerAutomaticTurnRestartBehavior,
|
|
32
30
|
TurnDefinition,
|
|
33
31
|
TurnDefinitionRecord,
|
|
34
32
|
} from "./define-process.js";
|
|
@@ -44,12 +42,16 @@ import type {
|
|
|
44
42
|
ProcessLauncherDefinition,
|
|
45
43
|
ProcessWatcherAPI,
|
|
46
44
|
ProcessWatcherDefinition,
|
|
47
|
-
RepositoryCredentialProject,
|
|
48
|
-
RepositoryCredentialRequirement,
|
|
49
45
|
ServerProcessAPI,
|
|
50
46
|
UiProcessAPI,
|
|
51
47
|
WorkerCompleteInput,
|
|
52
48
|
} from "./extension-api.js";
|
|
49
|
+
import { isPathInside } from "./fs-utils.js";
|
|
50
|
+
import {
|
|
51
|
+
type ProcessGraphSource,
|
|
52
|
+
toProcessGraphView,
|
|
53
|
+
validateProcessGraphProducts,
|
|
54
|
+
} from "./process-graph.js";
|
|
53
55
|
import type { OutcomeToolParameterSpec, PiBuiltInToolName, ProcessPiConfig } from "./types.js";
|
|
54
56
|
|
|
55
57
|
type MaybePromise<T> = T | Promise<T>;
|
|
@@ -82,15 +84,6 @@ export interface FlowAutomaticTurn<
|
|
|
82
84
|
definition: AutomaticTurnDefinition<TOutcome, TParams, TState>;
|
|
83
85
|
}
|
|
84
86
|
|
|
85
|
-
export interface FlowServerAutomaticTurn<
|
|
86
|
-
TParams = unknown,
|
|
87
|
-
TState = unknown,
|
|
88
|
-
TOutcome extends string = string,
|
|
89
|
-
> {
|
|
90
|
-
id: TurnId;
|
|
91
|
-
definition: ServerAutomaticTurnDefinition<TOutcome, TParams, TState>;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
87
|
export interface FlowHumanTurn<TParams = unknown, TState = unknown> {
|
|
95
88
|
id: TurnId;
|
|
96
89
|
definition: HumanTurnDefinition<TParams, TState>;
|
|
@@ -104,7 +97,6 @@ export interface FlowExternalTurn<TParams = unknown, TState = unknown> {
|
|
|
104
97
|
export type FlowTurn<TParams = unknown, TState = unknown> =
|
|
105
98
|
| FlowLlmTurn<TParams, TState, string>
|
|
106
99
|
| FlowAutomaticTurn<TParams, TState, string>
|
|
107
|
-
| FlowServerAutomaticTurn<TParams, TState, string>
|
|
108
100
|
| FlowHumanTurn<TParams, TState>
|
|
109
101
|
| FlowExternalTurn<TParams, TState>
|
|
110
102
|
| { id: TurnId; definition: TurnDefinition<TParams, TState> };
|
|
@@ -138,11 +130,12 @@ export interface FlowRepoLookup {
|
|
|
138
130
|
all(): FlowRepoContext[];
|
|
139
131
|
}
|
|
140
132
|
|
|
141
|
-
export
|
|
133
|
+
export type FlowPromptContext<
|
|
142
134
|
TParams = unknown,
|
|
143
135
|
TState = unknown,
|
|
144
136
|
TConsumedProducts extends string = string,
|
|
145
|
-
|
|
137
|
+
TPrepared = undefined,
|
|
138
|
+
> = {
|
|
146
139
|
process: ProcessInstance;
|
|
147
140
|
projects: readonly ProcessProject[];
|
|
148
141
|
params: TParams;
|
|
@@ -153,7 +146,7 @@ export interface FlowPromptContext<
|
|
|
153
146
|
};
|
|
154
147
|
input: Readonly<Partial<Record<TConsumedProducts, string>>>;
|
|
155
148
|
repo: FlowRepoLookup;
|
|
156
|
-
}
|
|
149
|
+
} & ([TPrepared] extends [undefined] ? { prepared?: undefined } : { prepared: TPrepared });
|
|
157
150
|
|
|
158
151
|
export interface FlowAutomaticRunContext<TParams = unknown, TState = unknown> {
|
|
159
152
|
process: ProcessInstance;
|
|
@@ -166,15 +159,12 @@ export interface FlowAutomaticRunContext<TParams = unknown, TState = unknown> {
|
|
|
166
159
|
reportProgress(report: TurnProgressReport): void;
|
|
167
160
|
}
|
|
168
161
|
|
|
169
|
-
export
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
state: TState;
|
|
174
|
-
output: { content: string | null } | null;
|
|
175
|
-
}
|
|
162
|
+
export type FlowLlmPreparationContext<
|
|
163
|
+
TParams = unknown,
|
|
164
|
+
TState = unknown,
|
|
165
|
+
> = FlowAutomaticRunContext<TParams, TState>;
|
|
176
166
|
|
|
177
|
-
|
|
167
|
+
interface FlowOutcomeEffectContext<TParams = unknown, TState = unknown> {
|
|
178
168
|
process: ProcessInstance;
|
|
179
169
|
projects: readonly ProcessProject[];
|
|
180
170
|
params: TParams;
|
|
@@ -182,6 +172,12 @@ export interface FlowAutomaticOutcomeEffectContext<TParams = unknown, TState = u
|
|
|
182
172
|
output: { content: string | null } | null;
|
|
183
173
|
}
|
|
184
174
|
|
|
175
|
+
export interface FlowLlmOutcomeEffectContext<TParams = unknown, TState = unknown>
|
|
176
|
+
extends FlowOutcomeEffectContext<TParams, TState> {}
|
|
177
|
+
|
|
178
|
+
export interface FlowAutomaticOutcomeEffectContext<TParams = unknown, TState = unknown>
|
|
179
|
+
extends FlowOutcomeEffectContext<TParams, TState> {}
|
|
180
|
+
|
|
185
181
|
export interface FlowExternalSourceContext<
|
|
186
182
|
TParams = unknown,
|
|
187
183
|
TState = unknown,
|
|
@@ -204,13 +200,6 @@ type FlowOutcomeStateEffect<TParams, TState, TContext> = (
|
|
|
204
200
|
input: FlowOutcomeEffectInput<TParams, TState, TContext>,
|
|
205
201
|
) => MaybePromise<TState>;
|
|
206
202
|
|
|
207
|
-
type SnapshotContext<TParams, TState> = {
|
|
208
|
-
process: ProcessInstance;
|
|
209
|
-
projects: readonly ProcessProject[];
|
|
210
|
-
params: TParams;
|
|
211
|
-
state: TState;
|
|
212
|
-
};
|
|
213
|
-
|
|
214
203
|
function textField(input: {
|
|
215
204
|
description: string;
|
|
216
205
|
requiredErrorCode: string;
|
|
@@ -264,16 +253,6 @@ function readInitialPrompt<TParams>(params: TParams): string {
|
|
|
264
253
|
return "";
|
|
265
254
|
}
|
|
266
255
|
|
|
267
|
-
function isPathInside(parent: string, child: string): boolean {
|
|
268
|
-
const relative = path.relative(parent, child);
|
|
269
|
-
return (
|
|
270
|
-
relative !== "" &&
|
|
271
|
-
relative !== ".." &&
|
|
272
|
-
!relative.startsWith(`..${path.sep}`) &&
|
|
273
|
-
!path.isAbsolute(relative)
|
|
274
|
-
);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
256
|
function safeJoinInside(parent: string, childPath: string, context: string): string {
|
|
278
257
|
const parentPath = path.resolve(parent);
|
|
279
258
|
const targetPath = path.resolve(parentPath, childPath);
|
|
@@ -383,37 +362,37 @@ function createFlowRepoLookup(input: {
|
|
|
383
362
|
workspaceRoot?: string;
|
|
384
363
|
}): FlowRepoLookup {
|
|
385
364
|
return {
|
|
386
|
-
get: (key) =>
|
|
387
|
-
|
|
388
|
-
projects: input.projects,
|
|
389
|
-
workspaceRoot: input.workspaceRoot,
|
|
390
|
-
key,
|
|
391
|
-
required: true,
|
|
392
|
-
}),
|
|
393
|
-
optional: (key) =>
|
|
394
|
-
resolveRepoByKey({
|
|
395
|
-
projects: input.projects,
|
|
396
|
-
workspaceRoot: input.workspaceRoot,
|
|
397
|
-
key,
|
|
398
|
-
required: false,
|
|
399
|
-
}),
|
|
365
|
+
get: (key) => resolveRepoByKey({ ...input, key, required: true }),
|
|
366
|
+
optional: (key) => resolveRepoByKey({ ...input, key, required: false }),
|
|
400
367
|
all: () =>
|
|
401
368
|
input.projects.map((project) =>
|
|
402
|
-
resolveRepoByKey({
|
|
403
|
-
projects: input.projects,
|
|
404
|
-
workspaceRoot: input.workspaceRoot,
|
|
405
|
-
key: project.key,
|
|
406
|
-
required: true,
|
|
407
|
-
}),
|
|
369
|
+
resolveRepoByKey({ ...input, key: project.key, required: true }),
|
|
408
370
|
),
|
|
409
371
|
};
|
|
410
372
|
}
|
|
411
373
|
|
|
412
|
-
|
|
374
|
+
function createFlowContextBase<TParams, TState>(ctx: ProcessRuntimeTurnContext<TParams, TState>) {
|
|
375
|
+
const { process, projects, params, state, workspaceRoot } = ctx;
|
|
376
|
+
return {
|
|
377
|
+
process,
|
|
378
|
+
projects,
|
|
379
|
+
params,
|
|
380
|
+
state,
|
|
381
|
+
...(workspaceRoot ? { workspaceRoot } : {}),
|
|
382
|
+
repo: createFlowRepoLookup({ projects, workspaceRoot }),
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export function createFlowPromptContext<
|
|
387
|
+
TParams,
|
|
388
|
+
TState,
|
|
389
|
+
TConsumedProducts extends string = string,
|
|
390
|
+
TPrepared = undefined,
|
|
391
|
+
>(
|
|
413
392
|
ctx: ProcessRuntimeTurnContext<TParams, TState>,
|
|
414
393
|
consumedProducts: readonly TConsumedProducts[],
|
|
415
394
|
optionalConsumedProducts: readonly string[] = [],
|
|
416
|
-
): FlowPromptContext<TParams, TState, TConsumedProducts> {
|
|
395
|
+
): FlowPromptContext<TParams, TState, TConsumedProducts, TPrepared> {
|
|
417
396
|
const productInput: Record<string, string> = {};
|
|
418
397
|
for (const productName of consumedProducts) {
|
|
419
398
|
const markdown = ctx.turnResultMarkdownByProduct?.[productName];
|
|
@@ -434,35 +413,20 @@ export function createFlowPromptContext<TParams, TState, TConsumedProducts exten
|
|
|
434
413
|
}
|
|
435
414
|
}
|
|
436
415
|
return {
|
|
437
|
-
|
|
438
|
-
projects: ctx.projects,
|
|
439
|
-
params: ctx.params,
|
|
440
|
-
state: ctx.state,
|
|
441
|
-
...(ctx.workspaceRoot ? { workspaceRoot: ctx.workspaceRoot } : {}),
|
|
416
|
+
...createFlowContextBase(ctx),
|
|
442
417
|
prompts: {
|
|
443
418
|
initial: readInitialPrompt(ctx.params),
|
|
444
419
|
},
|
|
445
420
|
input: productInput as Partial<Record<TConsumedProducts, string>>,
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
workspaceRoot: ctx.workspaceRoot,
|
|
449
|
-
}),
|
|
450
|
-
};
|
|
421
|
+
prepared: ctx.prepared as TPrepared,
|
|
422
|
+
} as FlowPromptContext<TParams, TState, TConsumedProducts, TPrepared>;
|
|
451
423
|
}
|
|
452
424
|
|
|
453
425
|
export function createFlowAutomaticRunContext<TParams, TState>(
|
|
454
426
|
ctx: ProcessRuntimeTurnContext<TParams, TState>,
|
|
455
427
|
): FlowAutomaticRunContext<TParams, TState> {
|
|
456
428
|
return {
|
|
457
|
-
|
|
458
|
-
projects: ctx.projects,
|
|
459
|
-
params: ctx.params,
|
|
460
|
-
state: ctx.state,
|
|
461
|
-
...(ctx.workspaceRoot ? { workspaceRoot: ctx.workspaceRoot } : {}),
|
|
462
|
-
repo: createFlowRepoLookup({
|
|
463
|
-
projects: ctx.projects,
|
|
464
|
-
workspaceRoot: ctx.workspaceRoot,
|
|
465
|
-
}),
|
|
429
|
+
...createFlowContextBase(ctx),
|
|
466
430
|
callIntegrationTool(name, args) {
|
|
467
431
|
if (!ctx.callIntegrationTool) {
|
|
468
432
|
throw new Error(`Automatic integration tool '${name}' is unavailable`);
|
|
@@ -475,56 +439,23 @@ export function createFlowAutomaticRunContext<TParams, TState>(
|
|
|
475
439
|
};
|
|
476
440
|
}
|
|
477
441
|
|
|
478
|
-
function
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
process: input.ctx.process,
|
|
484
|
-
projects: input.ctx.projects,
|
|
485
|
-
params: input.ctx.params,
|
|
486
|
-
state: input.ctx.state,
|
|
487
|
-
output:
|
|
488
|
-
typeof input.event.turnResultMarkdown === "string"
|
|
489
|
-
? { content: input.event.turnResultMarkdown }
|
|
490
|
-
: null,
|
|
491
|
-
};
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
function createFlowAutomaticOutcomeEffectContext<TParams, TState>(input: {
|
|
495
|
-
ctx: SnapshotContext<TParams, TState>;
|
|
496
|
-
event: ProcessOutcomeExecution<TParams, TState>["event"];
|
|
497
|
-
}): FlowAutomaticOutcomeEffectContext<TParams, TState> {
|
|
498
|
-
return {
|
|
499
|
-
process: input.ctx.process,
|
|
500
|
-
projects: input.ctx.projects,
|
|
501
|
-
params: input.ctx.params,
|
|
502
|
-
state: input.ctx.state,
|
|
503
|
-
output:
|
|
504
|
-
typeof input.event.turnResultMarkdown === "string"
|
|
505
|
-
? { content: input.event.turnResultMarkdown }
|
|
506
|
-
: null,
|
|
507
|
-
};
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
function wrapLlmOutcomeEffect<TParams, TState>(
|
|
511
|
-
effect: FlowOutcomeEffect<TParams, TState, FlowLlmOutcomeEffectContext<TParams, TState>>,
|
|
512
|
-
): ProcessOutcomeEffect<TParams, TState> {
|
|
513
|
-
return (execution) =>
|
|
514
|
-
effect({
|
|
515
|
-
ctx: createFlowLlmOutcomeEffectContext({ ctx: execution.ctx, event: execution.event }),
|
|
516
|
-
event: execution.event,
|
|
517
|
-
turnId: execution.turnId,
|
|
518
|
-
outcome: execution.outcome,
|
|
519
|
-
});
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
function wrapAutomaticOutcomeEffect<TParams, TState>(
|
|
523
|
-
effect: FlowOutcomeEffect<TParams, TState, FlowAutomaticOutcomeEffectContext<TParams, TState>>,
|
|
524
|
-
): ProcessOutcomeEffect<TParams, TState> {
|
|
442
|
+
function wrapOutcomeCallback<TParams, TState, TResult>(
|
|
443
|
+
callback: (
|
|
444
|
+
input: FlowOutcomeEffectInput<TParams, TState, FlowOutcomeEffectContext<TParams, TState>>,
|
|
445
|
+
) => TResult,
|
|
446
|
+
): (execution: ProcessOutcomeExecution<TParams, TState>) => TResult {
|
|
525
447
|
return (execution) =>
|
|
526
|
-
|
|
527
|
-
ctx:
|
|
448
|
+
callback({
|
|
449
|
+
ctx: {
|
|
450
|
+
process: execution.ctx.process,
|
|
451
|
+
projects: execution.ctx.projects,
|
|
452
|
+
params: execution.ctx.params,
|
|
453
|
+
state: execution.ctx.state,
|
|
454
|
+
output:
|
|
455
|
+
typeof execution.event.turnResultMarkdown === "string"
|
|
456
|
+
? { content: execution.event.turnResultMarkdown }
|
|
457
|
+
: null,
|
|
458
|
+
},
|
|
528
459
|
event: execution.event,
|
|
529
460
|
turnId: execution.turnId,
|
|
530
461
|
outcome: execution.outcome,
|
|
@@ -532,20 +463,10 @@ function wrapAutomaticOutcomeEffect<TParams, TState>(
|
|
|
532
463
|
}
|
|
533
464
|
|
|
534
465
|
class RouteAndEffectBuilder<TParams, TState, TContext> {
|
|
535
|
-
protected target:
|
|
536
|
-
| { kind: "to"; turnId: TurnId }
|
|
537
|
-
| { kind: "complete" }
|
|
538
|
-
| { kind: "lifecycleStatus"; status: ProcessTurnTerminalLifecycleStatus }
|
|
539
|
-
| null = null;
|
|
466
|
+
protected target: FlowTargetSpec = null;
|
|
540
467
|
protected flowEffect: FlowOutcomeEffect<TParams, TState, TContext> | undefined;
|
|
541
468
|
|
|
542
|
-
protected setTarget(
|
|
543
|
-
next:
|
|
544
|
-
| { kind: "to"; turnId: TurnId }
|
|
545
|
-
| { kind: "complete" }
|
|
546
|
-
| { kind: "lifecycleStatus"; status: ProcessTurnTerminalLifecycleStatus }
|
|
547
|
-
| null,
|
|
548
|
-
): this {
|
|
469
|
+
protected setTarget(next: FlowTargetSpec): this {
|
|
549
470
|
if (this.target && next) {
|
|
550
471
|
throw new Error("Flow route already declares a target");
|
|
551
472
|
}
|
|
@@ -645,6 +566,25 @@ class ParameterizedOutcomeBuilder<TParams, TState, TContext> extends RouteAndEff
|
|
|
645
566
|
protected outcomeDescription: string | null = null;
|
|
646
567
|
protected parameters: Record<string, OutcomeToolParameterSpec> = {};
|
|
647
568
|
protected publishedMarkdownParameter: string | null = null;
|
|
569
|
+
protected summaryParameter: string | null = null;
|
|
570
|
+
|
|
571
|
+
/** Optional concise result publication, separate from the full Markdown. */
|
|
572
|
+
resultSummary(name = "resultSummary"): this {
|
|
573
|
+
this.summaryParameter = name;
|
|
574
|
+
return this.parameter(name, {
|
|
575
|
+
type: "string",
|
|
576
|
+
description: "Concise summary of this attempt’s outcome.",
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
protected publishedMarkdownFields() {
|
|
581
|
+
return this.publishedMarkdownParameter
|
|
582
|
+
? {
|
|
583
|
+
publishedProduct: this.publishedMarkdownParameter,
|
|
584
|
+
turnResultMarkdownParameter: this.publishedMarkdownParameter,
|
|
585
|
+
}
|
|
586
|
+
: {};
|
|
587
|
+
}
|
|
648
588
|
|
|
649
589
|
description(text: string): this {
|
|
650
590
|
this.outcomeDescription = text;
|
|
@@ -674,15 +614,24 @@ class ParameterizedOutcomeBuilder<TParams, TState, TContext> extends RouteAndEff
|
|
|
674
614
|
};
|
|
675
615
|
}
|
|
676
616
|
|
|
677
|
-
|
|
617
|
+
private typedParameter(
|
|
618
|
+
name: string,
|
|
619
|
+
type: OutcomeToolParameterSpec["type"],
|
|
620
|
+
options: string | ArrayParameterOptions = {},
|
|
621
|
+
): this {
|
|
678
622
|
const resolved = typeof options === "string" ? { description: options } : options;
|
|
679
623
|
return this.parameter(name, {
|
|
680
624
|
...resolved,
|
|
681
|
-
type
|
|
625
|
+
type,
|
|
682
626
|
description: resolved.description ?? "",
|
|
627
|
+
...(type === "array" ? { items: resolved.items ?? { type: "string" } } : {}),
|
|
683
628
|
});
|
|
684
629
|
}
|
|
685
630
|
|
|
631
|
+
string(name: string, options: string | ParameterOptions = {}): this {
|
|
632
|
+
return this.typedParameter(name, "string", options);
|
|
633
|
+
}
|
|
634
|
+
|
|
686
635
|
markdown(name: string, options: string | MarkdownParameterOptions = {}): this {
|
|
687
636
|
const resolved = typeof options === "string" ? { description: options } : options;
|
|
688
637
|
const { publish, ...parameterOptions } = resolved;
|
|
@@ -693,78 +642,39 @@ class ParameterizedOutcomeBuilder<TParams, TState, TContext> extends RouteAndEff
|
|
|
693
642
|
normalizeProductName(name);
|
|
694
643
|
this.publishedMarkdownParameter = name;
|
|
695
644
|
}
|
|
696
|
-
return this.
|
|
645
|
+
return this.typedParameter(name, "string", {
|
|
697
646
|
...parameterOptions,
|
|
698
647
|
required: publish ? true : resolved.required,
|
|
699
648
|
requiredErrorCode: resolved.requiredErrorCode ?? (publish ? `${name}_required` : undefined),
|
|
700
|
-
type: "string",
|
|
701
|
-
description: resolved.description ?? "",
|
|
702
649
|
});
|
|
703
650
|
}
|
|
704
651
|
|
|
705
652
|
requiredString(name: string, options: string | ParameterOptions = {}): this {
|
|
706
|
-
|
|
707
|
-
return this.parameter(name, {
|
|
708
|
-
...resolved,
|
|
709
|
-
type: "string",
|
|
710
|
-
description: resolved.description ?? "",
|
|
711
|
-
});
|
|
653
|
+
return this.typedParameter(name, "string", this.withRequired(name, options));
|
|
712
654
|
}
|
|
713
655
|
|
|
714
656
|
number(name: string, options: string | ParameterOptions = {}): this {
|
|
715
|
-
|
|
716
|
-
return this.parameter(name, {
|
|
717
|
-
...resolved,
|
|
718
|
-
type: "number",
|
|
719
|
-
description: resolved.description ?? "",
|
|
720
|
-
});
|
|
657
|
+
return this.typedParameter(name, "number", options);
|
|
721
658
|
}
|
|
722
659
|
|
|
723
660
|
requiredNumber(name: string, options: string | ParameterOptions = {}): this {
|
|
724
|
-
|
|
725
|
-
return this.parameter(name, {
|
|
726
|
-
...resolved,
|
|
727
|
-
type: "number",
|
|
728
|
-
description: resolved.description ?? "",
|
|
729
|
-
});
|
|
661
|
+
return this.typedParameter(name, "number", this.withRequired(name, options));
|
|
730
662
|
}
|
|
731
663
|
|
|
732
664
|
boolean(name: string, options: string | ParameterOptions = {}): this {
|
|
733
|
-
|
|
734
|
-
return this.parameter(name, {
|
|
735
|
-
...resolved,
|
|
736
|
-
type: "boolean",
|
|
737
|
-
description: resolved.description ?? "",
|
|
738
|
-
});
|
|
665
|
+
return this.typedParameter(name, "boolean", options);
|
|
739
666
|
}
|
|
740
667
|
|
|
741
668
|
requiredBoolean(name: string, options: string | ParameterOptions = {}): this {
|
|
742
|
-
|
|
743
|
-
return this.parameter(name, {
|
|
744
|
-
...resolved,
|
|
745
|
-
type: "boolean",
|
|
746
|
-
description: resolved.description ?? "",
|
|
747
|
-
});
|
|
669
|
+
return this.typedParameter(name, "boolean", this.withRequired(name, options));
|
|
748
670
|
}
|
|
749
671
|
|
|
750
672
|
stringArray(name: string, options: string | ArrayParameterOptions = {}): this {
|
|
751
|
-
|
|
752
|
-
return this.parameter(name, {
|
|
753
|
-
...resolved,
|
|
754
|
-
type: "array",
|
|
755
|
-
description: resolved.description ?? "",
|
|
756
|
-
items: resolved.items ?? { type: "string" },
|
|
757
|
-
});
|
|
673
|
+
return this.typedParameter(name, "array", options);
|
|
758
674
|
}
|
|
759
675
|
|
|
760
676
|
requiredStringArray(name: string, options: string | ArrayParameterOptions = {}): this {
|
|
761
|
-
|
|
762
|
-
return this.parameter(name, {
|
|
763
|
-
...resolved,
|
|
764
|
-
type: "array",
|
|
765
|
-
description: resolved.description ?? "",
|
|
766
|
-
items: (resolved as ArrayParameterOptions).items ?? { type: "string" },
|
|
767
|
-
});
|
|
677
|
+
return this.typedParameter(name, "array", this.withRequired(name, options));
|
|
768
678
|
}
|
|
769
679
|
|
|
770
680
|
enum(name: string, valuesOrOptions: readonly string[] | EnumParameterOptions): this {
|
|
@@ -781,22 +691,11 @@ class ParameterizedOutcomeBuilder<TParams, TState, TContext> extends RouteAndEff
|
|
|
781
691
|
}
|
|
782
692
|
|
|
783
693
|
object(name: string, options: string | ParameterOptions = {}): this {
|
|
784
|
-
|
|
785
|
-
return this.parameter(name, {
|
|
786
|
-
...resolved,
|
|
787
|
-
type: "object",
|
|
788
|
-
description: resolved.description ?? "",
|
|
789
|
-
});
|
|
694
|
+
return this.typedParameter(name, "object", options);
|
|
790
695
|
}
|
|
791
696
|
|
|
792
697
|
array(name: string, options: string | ArrayParameterOptions = {}): this {
|
|
793
|
-
|
|
794
|
-
return this.parameter(name, {
|
|
795
|
-
...resolved,
|
|
796
|
-
type: "array",
|
|
797
|
-
description: resolved.description ?? "",
|
|
798
|
-
items: resolved.items ?? { type: "string" },
|
|
799
|
-
});
|
|
698
|
+
return this.typedParameter(name, "array", options);
|
|
800
699
|
}
|
|
801
700
|
}
|
|
802
701
|
|
|
@@ -848,12 +747,8 @@ export class OutcomeToolBuilder<
|
|
|
848
747
|
return {
|
|
849
748
|
description: this.outcomeDescription,
|
|
850
749
|
parameters: this.parameters,
|
|
851
|
-
...(this.
|
|
852
|
-
|
|
853
|
-
publishedProduct: this.publishedMarkdownParameter,
|
|
854
|
-
turnResultMarkdownParameter: this.publishedMarkdownParameter,
|
|
855
|
-
}
|
|
856
|
-
: {}),
|
|
750
|
+
...(this.summaryParameter ? { resultSummaryParameter: this.summaryParameter } : {}),
|
|
751
|
+
...this.publishedMarkdownFields(),
|
|
857
752
|
...(stateRouting
|
|
858
753
|
? {
|
|
859
754
|
branches: Object.fromEntries(
|
|
@@ -862,19 +757,10 @@ export class OutcomeToolBuilder<
|
|
|
862
757
|
{ to: turnId },
|
|
863
758
|
]),
|
|
864
759
|
),
|
|
865
|
-
choose: (
|
|
866
|
-
stateRouting.choose({
|
|
867
|
-
ctx: createFlowLlmOutcomeEffectContext({
|
|
868
|
-
ctx: execution.ctx,
|
|
869
|
-
event: execution.event,
|
|
870
|
-
}),
|
|
871
|
-
event: execution.event,
|
|
872
|
-
turnId: execution.turnId,
|
|
873
|
-
outcome: execution.outcome,
|
|
874
|
-
}),
|
|
760
|
+
choose: wrapOutcomeCallback((input) => stateRouting.choose(input)),
|
|
875
761
|
}
|
|
876
762
|
: this.buildRouteTarget()),
|
|
877
|
-
...(this.flowEffect ? { effect:
|
|
763
|
+
...(this.flowEffect ? { effect: wrapOutcomeCallback(this.flowEffect) } : {}),
|
|
878
764
|
};
|
|
879
765
|
}
|
|
880
766
|
}
|
|
@@ -899,21 +785,24 @@ export class AutomaticOutcomeBuilder<
|
|
|
899
785
|
if (!this.outcomeDescription) {
|
|
900
786
|
throw new Error("Automatic outcome must declare .description(...)");
|
|
901
787
|
}
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
788
|
+
const configuredEffect = this.flowEffect ? wrapOutcomeCallback(this.flowEffect) : undefined;
|
|
789
|
+
const effect = this.waits
|
|
790
|
+
? async (
|
|
791
|
+
execution: Parameters<NonNullable<ProcessToolOutcomeSpec<TParams, TState>["effect"]>>[0],
|
|
792
|
+
) => {
|
|
793
|
+
const result = configuredEffect ? await configuredEffect(execution) : undefined;
|
|
794
|
+
return {
|
|
795
|
+
...result,
|
|
796
|
+
processPatch: { ...(result?.processPatch ?? {}), lifecycleStatus: "waiting" as const },
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
: configuredEffect;
|
|
906
800
|
return {
|
|
907
801
|
description: this.outcomeDescription,
|
|
908
802
|
parameters: this.parameters,
|
|
909
|
-
...(this.
|
|
910
|
-
|
|
911
|
-
publishedProduct: this.publishedMarkdownParameter,
|
|
912
|
-
turnResultMarkdownParameter: this.publishedMarkdownParameter,
|
|
913
|
-
}
|
|
914
|
-
: {}),
|
|
803
|
+
...(this.summaryParameter ? { resultSummaryParameter: this.summaryParameter } : {}),
|
|
804
|
+
...this.publishedMarkdownFields(),
|
|
915
805
|
...this.buildRouteTarget(),
|
|
916
|
-
...(this.waits ? { wait: true as const } : {}),
|
|
917
806
|
...(effect ? { effect } : {}),
|
|
918
807
|
};
|
|
919
808
|
}
|
|
@@ -969,7 +858,7 @@ export class PlanResultBuilder<TParams = unknown, TState = unknown> extends Rout
|
|
|
969
858
|
if (!this.reviewTurnId) {
|
|
970
859
|
throw new Error("plan result must declare .review(turnId)");
|
|
971
860
|
}
|
|
972
|
-
const stateEffect = this.flowEffect ?
|
|
861
|
+
const stateEffect = this.flowEffect ? wrapOutcomeCallback(this.flowEffect) : undefined;
|
|
973
862
|
return {
|
|
974
863
|
description: this.resultDescription,
|
|
975
864
|
parameters: {
|
|
@@ -990,105 +879,116 @@ export class PlanResultBuilder<TParams = unknown, TState = unknown> extends Rout
|
|
|
990
879
|
}
|
|
991
880
|
}
|
|
992
881
|
|
|
993
|
-
|
|
882
|
+
abstract class TurnEndBuilder<TParams, TState>
|
|
994
883
|
extends RouteAndEffectBuilder<TParams, TState, FlowLlmOutcomeEffectContext<TParams, TState>>
|
|
995
884
|
implements FlowLlmTurn<TParams, TState, string>
|
|
996
885
|
{
|
|
886
|
+
protected abstract readonly builderName: string;
|
|
887
|
+
|
|
997
888
|
constructor(
|
|
998
|
-
private readonly
|
|
889
|
+
private readonly outcomeId: string,
|
|
999
890
|
private readonly parent?: FlowLlmTurn<TParams, TState, string>,
|
|
1000
891
|
) {
|
|
1001
892
|
super();
|
|
1002
893
|
}
|
|
1003
894
|
|
|
895
|
+
private get turn(): FlowLlmTurn<TParams, TState, string> {
|
|
896
|
+
if (!this.parent) throw new Error(`${this.builderName} builder is not attached to a turn`);
|
|
897
|
+
return this.parent;
|
|
898
|
+
}
|
|
899
|
+
|
|
1004
900
|
get id(): TurnId {
|
|
1005
|
-
|
|
1006
|
-
throw new Error("Published result builder is not attached to a turn");
|
|
1007
|
-
}
|
|
1008
|
-
return this.parent.id;
|
|
901
|
+
return this.turn.id;
|
|
1009
902
|
}
|
|
1010
903
|
|
|
1011
904
|
get definition(): LlmTurnDefinition<string, TParams, TState> {
|
|
1012
|
-
|
|
1013
|
-
throw new Error("Published result builder is not attached to a turn");
|
|
1014
|
-
}
|
|
1015
|
-
return this.parent.definition;
|
|
905
|
+
return this.turn.definition;
|
|
1016
906
|
}
|
|
1017
907
|
|
|
1018
|
-
|
|
1019
|
-
const effect = this.flowEffect ?
|
|
908
|
+
protected buildResult(): ProcessTurnEndSpec<TParams, TState, string> {
|
|
909
|
+
const effect = this.flowEffect ? wrapOutcomeCallback(this.flowEffect) : undefined;
|
|
1020
910
|
return {
|
|
1021
|
-
outcome: this.
|
|
911
|
+
outcome: this.outcomeId,
|
|
1022
912
|
...this.buildRouteTarget(),
|
|
1023
913
|
...(effect ? { effect } : {}),
|
|
1024
914
|
};
|
|
1025
915
|
}
|
|
1026
916
|
}
|
|
1027
917
|
|
|
1028
|
-
export class
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
{
|
|
1032
|
-
|
|
1033
|
-
private readonly outcomeId: string,
|
|
1034
|
-
private readonly parent?: FlowLlmTurn<TParams, TState, string>,
|
|
1035
|
-
) {
|
|
1036
|
-
super();
|
|
1037
|
-
if (outcomeId.trim() === "") {
|
|
1038
|
-
throw new Error("LLM turn end outcome must be non-empty");
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
918
|
+
export class PublishedResultBuilder<TParams = unknown, TState = unknown> extends TurnEndBuilder<
|
|
919
|
+
TParams,
|
|
920
|
+
TState
|
|
921
|
+
> {
|
|
922
|
+
protected readonly builderName = "Published result";
|
|
1041
923
|
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
throw new Error("LLM turn end builder is not attached to a turn");
|
|
1045
|
-
}
|
|
1046
|
-
return this.parent.id;
|
|
924
|
+
buildTurnEnd(): ProcessTurnEndSpec<TParams, TState, string> {
|
|
925
|
+
return this.buildResult();
|
|
1047
926
|
}
|
|
927
|
+
}
|
|
1048
928
|
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
929
|
+
export class LlmTurnEndBuilder<TParams = unknown, TState = unknown> extends TurnEndBuilder<
|
|
930
|
+
TParams,
|
|
931
|
+
TState
|
|
932
|
+
> {
|
|
933
|
+
protected readonly builderName = "LLM turn end";
|
|
934
|
+
|
|
935
|
+
constructor(outcomeId: string, parent?: FlowLlmTurn<TParams, TState, string>) {
|
|
936
|
+
super(outcomeId, parent);
|
|
937
|
+
if (outcomeId.trim() === "") throw new Error("LLM turn end outcome must be non-empty");
|
|
1054
938
|
}
|
|
1055
939
|
|
|
1056
940
|
build(): ProcessTurnEndSpec<TParams, TState, string> {
|
|
1057
|
-
|
|
1058
|
-
return {
|
|
1059
|
-
outcome: this.outcomeId,
|
|
1060
|
-
...this.buildRouteTarget(),
|
|
1061
|
-
...(effect ? { effect } : {}),
|
|
1062
|
-
};
|
|
941
|
+
return this.buildResult();
|
|
1063
942
|
}
|
|
1064
943
|
}
|
|
1065
944
|
|
|
1066
|
-
type LlmPromptBuilder<TParams, TState, TConsumedProducts extends string> = (
|
|
1067
|
-
ctx: FlowPromptContext<TParams, TState, TConsumedProducts>,
|
|
945
|
+
type LlmPromptBuilder<TParams, TState, TConsumedProducts extends string, TPrepared> = (
|
|
946
|
+
ctx: FlowPromptContext<TParams, TState, TConsumedProducts, TPrepared>,
|
|
1068
947
|
) => MaybePromise<string>;
|
|
1069
948
|
|
|
949
|
+
abstract class DescribedTurnBuilder {
|
|
950
|
+
protected turnDescription: string | null = null;
|
|
951
|
+
|
|
952
|
+
constructor(protected readonly turnId: TurnId) {}
|
|
953
|
+
|
|
954
|
+
get id(): TurnId {
|
|
955
|
+
return this.turnId;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
description(description: string): this {
|
|
959
|
+
this.turnDescription = description;
|
|
960
|
+
return this;
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
|
|
1070
964
|
export class LlmFlowBuilder<
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
965
|
+
TParams = unknown,
|
|
966
|
+
TState = unknown,
|
|
967
|
+
TConsumedProducts extends string = never,
|
|
968
|
+
TPrepared = undefined,
|
|
969
|
+
>
|
|
970
|
+
extends DescribedTurnBuilder
|
|
971
|
+
implements FlowLlmTurn<TParams, TState, string>
|
|
1075
972
|
{
|
|
1076
|
-
private readonly turnId: TurnId;
|
|
1077
|
-
private turnDescription: string | null = null;
|
|
1078
973
|
private modelPurposeValue: LlmModelPurpose | undefined;
|
|
1079
974
|
private availableTools: readonly PiBuiltInToolName[] = [];
|
|
1080
975
|
private availableIntegrationTools: readonly string[] = [];
|
|
976
|
+
private integrationToolResolver:
|
|
977
|
+
| ((params: TParams, state: TState) => readonly string[])
|
|
978
|
+
| undefined;
|
|
979
|
+
private preparation:
|
|
980
|
+
| ((ctx: FlowLlmPreparationContext<TParams, TState>) => MaybePromise<unknown>)
|
|
981
|
+
| null = null;
|
|
1081
982
|
private promptBuilder:
|
|
1082
983
|
| ((ctx: ProcessRuntimeTurnContext<TParams, TState>) => MaybePromise<string>)
|
|
1083
984
|
| null = null;
|
|
1084
985
|
private branchType: LlmTurnDefinition<string, TParams, TState>["branchType"] = "primary";
|
|
1085
986
|
private context: LlmTurnDefinition<string, TParams, TState>["context"] = "fresh";
|
|
1086
|
-
private completionMode: LlmTurnDefinition<string, TParams, TState>["completionMode"] = "turn_end";
|
|
1087
987
|
private questionsEnabled = false;
|
|
1088
988
|
private startFrom: LlmTurnDefinition<string, TParams, TState>["startFrom"] | undefined;
|
|
1089
989
|
private restorePrimaryLeafAfterTurn: boolean | undefined;
|
|
1090
|
-
private consumedProductNames
|
|
1091
|
-
private optionalConsumedProductNames
|
|
990
|
+
private readonly consumedProductNames = new Set<string>();
|
|
991
|
+
private readonly optionalConsumedProductNames = new Set<string>();
|
|
1092
992
|
private publishedResult: {
|
|
1093
993
|
productName: string;
|
|
1094
994
|
builder: PublishedResultBuilder<TParams, TState>;
|
|
@@ -1096,23 +996,10 @@ export class LlmFlowBuilder<
|
|
|
1096
996
|
private endResult: LlmTurnEndBuilder<TParams, TState> | null = null;
|
|
1097
997
|
private outcomeToolBuilders = new Map<string, OutcomeToolBuilder<TParams, TState>>();
|
|
1098
998
|
|
|
1099
|
-
constructor(turnId: TurnId) {
|
|
1100
|
-
this.turnId = turnId;
|
|
1101
|
-
}
|
|
1102
|
-
|
|
1103
|
-
get id(): TurnId {
|
|
1104
|
-
return this.turnId;
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
999
|
get definition(): LlmTurnDefinition<string, TParams, TState> {
|
|
1108
1000
|
return this.buildDefinition();
|
|
1109
1001
|
}
|
|
1110
1002
|
|
|
1111
|
-
description(description: string): this {
|
|
1112
|
-
this.turnDescription = description;
|
|
1113
|
-
return this;
|
|
1114
|
-
}
|
|
1115
|
-
|
|
1116
1003
|
modelPurpose(purpose: LlmModelPurpose): this {
|
|
1117
1004
|
this.modelPurposeValue = purpose;
|
|
1118
1005
|
return this;
|
|
@@ -1128,6 +1015,18 @@ export class LlmFlowBuilder<
|
|
|
1128
1015
|
return this;
|
|
1129
1016
|
}
|
|
1130
1017
|
|
|
1018
|
+
resolveIntegrationTools(resolver: (params: TParams, state: TState) => readonly string[]): this {
|
|
1019
|
+
this.integrationToolResolver = resolver;
|
|
1020
|
+
return this;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
prepare<TNextPrepared>(
|
|
1024
|
+
fn: (ctx: FlowLlmPreparationContext<TParams, TState>) => MaybePromise<TNextPrepared>,
|
|
1025
|
+
): LlmFlowBuilder<TParams, TState, TConsumedProducts, TNextPrepared> {
|
|
1026
|
+
this.preparation = fn;
|
|
1027
|
+
return this as unknown as LlmFlowBuilder<TParams, TState, TConsumedProducts, TNextPrepared>;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1131
1030
|
/** Enable durable operator questions for this LLM turn. */
|
|
1132
1031
|
askQuestions(): this {
|
|
1133
1032
|
this.questionsEnabled = true;
|
|
@@ -1137,7 +1036,6 @@ export class LlmFlowBuilder<
|
|
|
1137
1036
|
freshPrimary(): this {
|
|
1138
1037
|
this.branchType = "primary";
|
|
1139
1038
|
this.context = "fresh";
|
|
1140
|
-
this.completionMode = "turn_end";
|
|
1141
1039
|
this.startFrom = undefined;
|
|
1142
1040
|
this.restorePrimaryLeafAfterTurn = undefined;
|
|
1143
1041
|
return this;
|
|
@@ -1146,7 +1044,6 @@ export class LlmFlowBuilder<
|
|
|
1146
1044
|
freshSeededPrimary(): this {
|
|
1147
1045
|
this.branchType = "primary";
|
|
1148
1046
|
this.context = "fresh_seeded";
|
|
1149
|
-
this.completionMode = "turn_end";
|
|
1150
1047
|
this.startFrom = undefined;
|
|
1151
1048
|
this.restorePrimaryLeafAfterTurn = undefined;
|
|
1152
1049
|
return this;
|
|
@@ -1155,14 +1052,12 @@ export class LlmFlowBuilder<
|
|
|
1155
1052
|
fullPrimary(): this {
|
|
1156
1053
|
this.branchType = "primary";
|
|
1157
1054
|
this.context = "full";
|
|
1158
|
-
this.completionMode = "turn_end";
|
|
1159
1055
|
return this;
|
|
1160
1056
|
}
|
|
1161
1057
|
|
|
1162
1058
|
rootBranchReview(): this {
|
|
1163
1059
|
this.branchType = "root_branch";
|
|
1164
1060
|
this.context = "full";
|
|
1165
|
-
this.completionMode = "turn_end";
|
|
1166
1061
|
this.restorePrimaryLeafAfterTurn = true;
|
|
1167
1062
|
return this;
|
|
1168
1063
|
}
|
|
@@ -1189,12 +1084,7 @@ export class LlmFlowBuilder<
|
|
|
1189
1084
|
productName: string,
|
|
1190
1085
|
fallback: ProcessTurnStartSelection = { kind: "current_leaf" },
|
|
1191
1086
|
): this {
|
|
1192
|
-
this.
|
|
1193
|
-
kind: "product_ref",
|
|
1194
|
-
productName: normalizeProductName(productName),
|
|
1195
|
-
fallback,
|
|
1196
|
-
};
|
|
1197
|
-
return this;
|
|
1087
|
+
return this.startFromProductBranch(productName, fallback);
|
|
1198
1088
|
}
|
|
1199
1089
|
|
|
1200
1090
|
startFromReviewBranch(): this {
|
|
@@ -1225,22 +1115,26 @@ export class LlmFlowBuilder<
|
|
|
1225
1115
|
|
|
1226
1116
|
consume<TProductName extends string>(
|
|
1227
1117
|
productName: TProductName,
|
|
1228
|
-
): LlmFlowBuilder<TParams, TState, TConsumedProducts | TProductName> {
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1118
|
+
): LlmFlowBuilder<TParams, TState, TConsumedProducts | TProductName, TPrepared> {
|
|
1119
|
+
this.consumedProductNames.add(normalizeProductName(productName));
|
|
1120
|
+
return this as unknown as LlmFlowBuilder<
|
|
1121
|
+
TParams,
|
|
1122
|
+
TState,
|
|
1123
|
+
TConsumedProducts | TProductName,
|
|
1124
|
+
TPrepared
|
|
1125
|
+
>;
|
|
1234
1126
|
}
|
|
1235
1127
|
|
|
1236
1128
|
optionalConsume<TProductName extends string>(
|
|
1237
1129
|
productName: TProductName,
|
|
1238
|
-
): LlmFlowBuilder<TParams, TState, TConsumedProducts | TProductName> {
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1130
|
+
): LlmFlowBuilder<TParams, TState, TConsumedProducts | TProductName, TPrepared> {
|
|
1131
|
+
this.optionalConsumedProductNames.add(normalizeProductName(productName));
|
|
1132
|
+
return this as unknown as LlmFlowBuilder<
|
|
1133
|
+
TParams,
|
|
1134
|
+
TState,
|
|
1135
|
+
TConsumedProducts | TProductName,
|
|
1136
|
+
TPrepared
|
|
1137
|
+
>;
|
|
1244
1138
|
}
|
|
1245
1139
|
|
|
1246
1140
|
publish(productName: string): PublishedResultBuilder<TParams, TState> {
|
|
@@ -1270,14 +1164,12 @@ export class LlmFlowBuilder<
|
|
|
1270
1164
|
return this.endResult;
|
|
1271
1165
|
}
|
|
1272
1166
|
|
|
1273
|
-
buildPrompt(fn: LlmPromptBuilder<TParams, TState, TConsumedProducts>): this {
|
|
1167
|
+
buildPrompt(fn: LlmPromptBuilder<TParams, TState, TConsumedProducts, TPrepared>): this {
|
|
1274
1168
|
this.promptBuilder = (ctx) =>
|
|
1275
1169
|
fn(
|
|
1276
|
-
createFlowPromptContext(
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
this.optionalConsumedProductNames,
|
|
1280
|
-
),
|
|
1170
|
+
createFlowPromptContext(ctx, [...this.consumedProductNames] as TConsumedProducts[], [
|
|
1171
|
+
...this.optionalConsumedProductNames,
|
|
1172
|
+
]),
|
|
1281
1173
|
);
|
|
1282
1174
|
return this;
|
|
1283
1175
|
}
|
|
@@ -1319,6 +1211,7 @@ export class LlmFlowBuilder<
|
|
|
1319
1211
|
if (!prompt) {
|
|
1320
1212
|
throw new Error(`LLM turn '${this.turnId}' must declare .buildPrompt(...)`);
|
|
1321
1213
|
}
|
|
1214
|
+
const preparation = this.preparation;
|
|
1322
1215
|
return {
|
|
1323
1216
|
kind: "llm" as const,
|
|
1324
1217
|
description: this.turnDescription,
|
|
@@ -1327,8 +1220,17 @@ export class LlmFlowBuilder<
|
|
|
1327
1220
|
...(this.availableIntegrationTools.length > 0
|
|
1328
1221
|
? { integrationTools: this.availableIntegrationTools }
|
|
1329
1222
|
: {}),
|
|
1223
|
+
...(this.integrationToolResolver
|
|
1224
|
+
? { resolveIntegrationTools: this.integrationToolResolver }
|
|
1225
|
+
: {}),
|
|
1330
1226
|
...(this.questionsEnabled ? { askQuestions: true as const } : {}),
|
|
1331
|
-
|
|
1227
|
+
...(preparation
|
|
1228
|
+
? {
|
|
1229
|
+
prepare: (ctx: ProcessRuntimeTurnContext<TParams, TState>) =>
|
|
1230
|
+
preparation(createFlowAutomaticRunContext(ctx)),
|
|
1231
|
+
}
|
|
1232
|
+
: {}),
|
|
1233
|
+
completionMode: "turn_end" as const,
|
|
1332
1234
|
branchType: this.branchType,
|
|
1333
1235
|
context: this.context,
|
|
1334
1236
|
...(this.startFrom ? { startFrom: this.startFrom } : {}),
|
|
@@ -1442,76 +1344,77 @@ export class LlmFlowBuilder<
|
|
|
1442
1344
|
...(turnResultMarkdown ? { turnResultMarkdown } : {}),
|
|
1443
1345
|
...(resultSemanticRef ? { resultSemanticRef } : {}),
|
|
1444
1346
|
...(publishedProduct ? { publishedProduct } : {}),
|
|
1445
|
-
...(this.consumedProductNames.
|
|
1347
|
+
...(this.consumedProductNames.size > 0
|
|
1446
1348
|
? { consumedProducts: [...this.consumedProductNames] }
|
|
1447
1349
|
: {}),
|
|
1448
|
-
...(this.optionalConsumedProductNames.
|
|
1350
|
+
...(this.optionalConsumedProductNames.size > 0
|
|
1449
1351
|
? { optionalConsumedProducts: [...this.optionalConsumedProductNames] }
|
|
1450
1352
|
: {}),
|
|
1451
1353
|
};
|
|
1452
1354
|
}
|
|
1453
1355
|
}
|
|
1454
1356
|
|
|
1455
|
-
type
|
|
1456
|
-
|
|
1457
|
-
|
|
1357
|
+
type StoredExternalActionBuilder<TParams, TState> = ExternalActionBuilder<
|
|
1358
|
+
TParams,
|
|
1359
|
+
TState,
|
|
1360
|
+
unknown,
|
|
1361
|
+
Record<string, unknown>
|
|
1458
1362
|
>;
|
|
1459
1363
|
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1364
|
+
abstract class ExternalActionTurnBuilder<TParams, TState> extends DescribedTurnBuilder {
|
|
1365
|
+
protected abstract readonly turnKind: "Automatic" | "Human";
|
|
1366
|
+
private externalActionBuilders = new Map<string, StoredExternalActionBuilder<TParams, TState>>();
|
|
1367
|
+
|
|
1368
|
+
externalAction<
|
|
1369
|
+
TEvent = unknown,
|
|
1370
|
+
TInput extends Record<string, unknown> = Record<string, unknown>,
|
|
1371
|
+
>(
|
|
1372
|
+
externalActionId: string,
|
|
1373
|
+
source: ExternalActionSource<TParams, TState, TEvent, TInput>,
|
|
1374
|
+
configure: (
|
|
1375
|
+
external: ExternalActionBuilder<TParams, TState, TEvent, TInput>,
|
|
1376
|
+
) => ExternalActionBuilder<TParams, TState, TEvent, TInput> | undefined,
|
|
1377
|
+
): this {
|
|
1378
|
+
if (this.externalActionBuilders.has(externalActionId)) {
|
|
1379
|
+
throw new Error(
|
|
1380
|
+
`${this.turnKind} turn '${this.turnId}' declares duplicate external action '${externalActionId}'`,
|
|
1381
|
+
);
|
|
1382
|
+
}
|
|
1383
|
+
const builder = new ExternalActionBuilder(externalActionId, source);
|
|
1384
|
+
configure(builder);
|
|
1385
|
+
this.externalActionBuilders.set(
|
|
1386
|
+
externalActionId,
|
|
1387
|
+
builder as unknown as StoredExternalActionBuilder<TParams, TState>,
|
|
1473
1388
|
);
|
|
1389
|
+
return this;
|
|
1474
1390
|
}
|
|
1475
|
-
const builder = new ExternalActionBuilder(input.externalActionId, input.source);
|
|
1476
|
-
input.configure(builder);
|
|
1477
|
-
input.builders.set(input.externalActionId, builder as ExternalActionBuilder<TParams, TState>);
|
|
1478
|
-
}
|
|
1479
1391
|
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1392
|
+
protected buildExternalActions():
|
|
1393
|
+
| Record<string, ProcessHumanTurnExternalActionSpec<TParams, TState>>
|
|
1394
|
+
| undefined {
|
|
1395
|
+
return this.externalActionBuilders.size > 0
|
|
1396
|
+
? Object.fromEntries(
|
|
1397
|
+
[...this.externalActionBuilders].map(([id, builder]) => [id, builder.build()]),
|
|
1398
|
+
)
|
|
1399
|
+
: undefined;
|
|
1400
|
+
}
|
|
1484
1401
|
}
|
|
1485
1402
|
|
|
1486
1403
|
export class AutomaticFlowBuilder<TParams = unknown, TState = unknown>
|
|
1404
|
+
extends ExternalActionTurnBuilder<TParams, TState>
|
|
1487
1405
|
implements FlowAutomaticTurn<TParams, TState, string>
|
|
1488
1406
|
{
|
|
1489
|
-
|
|
1490
|
-
private turnDescription: string | null = null;
|
|
1407
|
+
protected readonly turnKind = "Automatic";
|
|
1491
1408
|
private runFn:
|
|
1492
1409
|
| ((ctx: FlowAutomaticRunContext<TParams, TState>) => MaybePromise<WorkerCompleteInput<string>>)
|
|
1493
1410
|
| null = null;
|
|
1494
1411
|
private outcomeBuilders = new Map<string, AutomaticOutcomeBuilder<TParams, TState>>();
|
|
1495
1412
|
private availableIntegrationTools: readonly string[] = [];
|
|
1496
|
-
private externalActionBuilders: ExternalActionBuilderMap<TParams, TState> = new Map();
|
|
1497
|
-
|
|
1498
|
-
constructor(turnId: TurnId) {
|
|
1499
|
-
this.turnId = turnId;
|
|
1500
|
-
}
|
|
1501
|
-
|
|
1502
|
-
get id(): TurnId {
|
|
1503
|
-
return this.turnId;
|
|
1504
|
-
}
|
|
1505
1413
|
|
|
1506
1414
|
get definition(): AutomaticTurnDefinition<string, TParams, TState> {
|
|
1507
1415
|
return this.buildDefinition();
|
|
1508
1416
|
}
|
|
1509
1417
|
|
|
1510
|
-
description(description: string): this {
|
|
1511
|
-
this.turnDescription = description;
|
|
1512
|
-
return this;
|
|
1513
|
-
}
|
|
1514
|
-
|
|
1515
1418
|
run(
|
|
1516
1419
|
fn: (
|
|
1517
1420
|
ctx: FlowAutomaticRunContext<TParams, TState>,
|
|
@@ -1526,27 +1429,6 @@ export class AutomaticFlowBuilder<TParams = unknown, TState = unknown>
|
|
|
1526
1429
|
return this;
|
|
1527
1430
|
}
|
|
1528
1431
|
|
|
1529
|
-
externalAction<
|
|
1530
|
-
TEvent = unknown,
|
|
1531
|
-
TInput extends Record<string, unknown> = Record<string, unknown>,
|
|
1532
|
-
>(
|
|
1533
|
-
externalActionId: string,
|
|
1534
|
-
source: ExternalActionSource<TParams, TState, TEvent, TInput>,
|
|
1535
|
-
configure: (
|
|
1536
|
-
external: ExternalActionBuilder<TParams, TState, TEvent, TInput>,
|
|
1537
|
-
) => ExternalActionBuilder<TParams, TState, TEvent, TInput> | undefined,
|
|
1538
|
-
): this {
|
|
1539
|
-
addExternalAction({
|
|
1540
|
-
builders: this.externalActionBuilders,
|
|
1541
|
-
turnId: this.turnId,
|
|
1542
|
-
turnKind: "Automatic",
|
|
1543
|
-
externalActionId,
|
|
1544
|
-
source,
|
|
1545
|
-
configure,
|
|
1546
|
-
});
|
|
1547
|
-
return this;
|
|
1548
|
-
}
|
|
1549
|
-
|
|
1550
1432
|
outcome(
|
|
1551
1433
|
id: string,
|
|
1552
1434
|
configure: (
|
|
@@ -1580,120 +1462,20 @@ export class AutomaticFlowBuilder<TParams = unknown, TState = unknown>
|
|
|
1580
1462
|
outcomes[outcomeId] = builder.build();
|
|
1581
1463
|
}
|
|
1582
1464
|
const runFn = this.runFn;
|
|
1465
|
+
const externalActions = this.buildExternalActions();
|
|
1583
1466
|
return {
|
|
1584
1467
|
kind: "automatic",
|
|
1585
1468
|
description: this.turnDescription,
|
|
1586
1469
|
...(this.availableIntegrationTools.length > 0
|
|
1587
1470
|
? { integrationTools: this.availableIntegrationTools }
|
|
1588
1471
|
: {}),
|
|
1589
|
-
...(
|
|
1590
|
-
? { externalActions: buildExternalActions(this.externalActionBuilders) }
|
|
1591
|
-
: {}),
|
|
1472
|
+
...(externalActions ? { externalActions } : {}),
|
|
1592
1473
|
outcomes,
|
|
1593
1474
|
run: (ctx) => runFn(createFlowAutomaticRunContext(ctx)),
|
|
1594
1475
|
};
|
|
1595
1476
|
}
|
|
1596
1477
|
}
|
|
1597
1478
|
|
|
1598
|
-
export class ServerAutomaticFlowBuilder<TParams = unknown, TState = unknown>
|
|
1599
|
-
implements FlowServerAutomaticTurn<TParams, TState, string>
|
|
1600
|
-
{
|
|
1601
|
-
private readonly turnId: TurnId;
|
|
1602
|
-
private turnDescription: string | null = null;
|
|
1603
|
-
private restartBehaviorValue: ServerAutomaticTurnRestartBehavior | undefined;
|
|
1604
|
-
private runFn:
|
|
1605
|
-
| ((ctx: FlowAutomaticRunContext<TParams, TState>) => MaybePromise<WorkerCompleteInput<string>>)
|
|
1606
|
-
| null = null;
|
|
1607
|
-
private outcomeBuilders = new Map<string, AutomaticOutcomeBuilder<TParams, TState>>();
|
|
1608
|
-
|
|
1609
|
-
constructor(turnId: TurnId) {
|
|
1610
|
-
this.turnId = turnId;
|
|
1611
|
-
}
|
|
1612
|
-
|
|
1613
|
-
get id(): TurnId {
|
|
1614
|
-
return this.turnId;
|
|
1615
|
-
}
|
|
1616
|
-
|
|
1617
|
-
get definition(): ServerAutomaticTurnDefinition<string, TParams, TState> {
|
|
1618
|
-
return this.buildDefinition();
|
|
1619
|
-
}
|
|
1620
|
-
|
|
1621
|
-
description(description: string): this {
|
|
1622
|
-
this.turnDescription = description;
|
|
1623
|
-
return this;
|
|
1624
|
-
}
|
|
1625
|
-
|
|
1626
|
-
run(
|
|
1627
|
-
fn: (
|
|
1628
|
-
ctx: FlowAutomaticRunContext<TParams, TState>,
|
|
1629
|
-
) => MaybePromise<WorkerCompleteInput<string> & { state?: TState }>,
|
|
1630
|
-
): this {
|
|
1631
|
-
this.runFn = fn;
|
|
1632
|
-
return this;
|
|
1633
|
-
}
|
|
1634
|
-
|
|
1635
|
-
restartBehavior(behavior: ServerAutomaticTurnRestartBehavior): this {
|
|
1636
|
-
this.restartBehaviorValue = behavior;
|
|
1637
|
-
return this;
|
|
1638
|
-
}
|
|
1639
|
-
|
|
1640
|
-
outcome(
|
|
1641
|
-
id: string,
|
|
1642
|
-
configure: (
|
|
1643
|
-
outcome: AutomaticOutcomeBuilder<TParams, TState>,
|
|
1644
|
-
) => AutomaticOutcomeBuilder<TParams, TState> | undefined,
|
|
1645
|
-
): this {
|
|
1646
|
-
if (id.trim() === "") {
|
|
1647
|
-
throw new Error(`Server automatic turn '${this.turnId}' declares an empty outcome id`);
|
|
1648
|
-
}
|
|
1649
|
-
if (this.outcomeBuilders.has(id)) {
|
|
1650
|
-
throw new Error(`Server automatic turn '${this.turnId}' declares duplicate outcome '${id}'`);
|
|
1651
|
-
}
|
|
1652
|
-
const builder = new AutomaticOutcomeBuilder<TParams, TState>();
|
|
1653
|
-
configure(builder);
|
|
1654
|
-
this.outcomeBuilders.set(id, builder);
|
|
1655
|
-
return this;
|
|
1656
|
-
}
|
|
1657
|
-
|
|
1658
|
-
private buildDefinition(): ServerAutomaticTurnDefinition<string, TParams, TState> {
|
|
1659
|
-
if (!this.turnDescription) {
|
|
1660
|
-
throw new Error(`Server automatic turn '${this.turnId}' must declare .description(...)`);
|
|
1661
|
-
}
|
|
1662
|
-
if (!this.runFn) {
|
|
1663
|
-
throw new Error(`Server automatic turn '${this.turnId}' must declare .run(...)`);
|
|
1664
|
-
}
|
|
1665
|
-
if (this.outcomeBuilders.size === 0) {
|
|
1666
|
-
throw new Error(`Server automatic turn '${this.turnId}' must declare at least one outcome`);
|
|
1667
|
-
}
|
|
1668
|
-
const outcomes: Record<string, ProcessToolOutcomeSpec<TParams, TState>> = {};
|
|
1669
|
-
for (const [outcomeId, builder] of this.outcomeBuilders) {
|
|
1670
|
-
outcomes[outcomeId] = builder.build();
|
|
1671
|
-
}
|
|
1672
|
-
const runFn = this.runFn;
|
|
1673
|
-
return {
|
|
1674
|
-
kind: "server_automatic",
|
|
1675
|
-
description: this.turnDescription,
|
|
1676
|
-
outcomes,
|
|
1677
|
-
...(this.restartBehaviorValue ? { restartBehavior: this.restartBehaviorValue } : {}),
|
|
1678
|
-
run: (ctx) =>
|
|
1679
|
-
runFn(createFlowAutomaticRunContext(ctx as ProcessRuntimeTurnContext<TParams, TState>)),
|
|
1680
|
-
};
|
|
1681
|
-
}
|
|
1682
|
-
}
|
|
1683
|
-
|
|
1684
|
-
function formFieldPublishedProductName(field: FormDefinition["fields"][number]): string | null {
|
|
1685
|
-
if (!field.publish) {
|
|
1686
|
-
return null;
|
|
1687
|
-
}
|
|
1688
|
-
return normalizeProductName(
|
|
1689
|
-
typeof field.publish === "object" && field.publish.product ? field.publish.product : field.id,
|
|
1690
|
-
);
|
|
1691
|
-
}
|
|
1692
|
-
|
|
1693
|
-
function inferActionLabel(actionId: string): string {
|
|
1694
|
-
return humanizeProcessLabel(actionId);
|
|
1695
|
-
}
|
|
1696
|
-
|
|
1697
1479
|
function inferAcceptanceState(actionId: string): ProcessHumanTurnActionSpec["acceptanceState"] {
|
|
1698
1480
|
const normalized = actionId.toLowerCase();
|
|
1699
1481
|
if (/approve|accept|complete|merged/.test(normalized)) {
|
|
@@ -1763,7 +1545,7 @@ export class HumanActionBuilder<TParams = unknown, TState = unknown> {
|
|
|
1763
1545
|
build(): ProcessHumanTurnActionSpec<TParams, TState> {
|
|
1764
1546
|
return {
|
|
1765
1547
|
label:
|
|
1766
|
-
typeof this.spec.label === "string" ? this.spec.label :
|
|
1548
|
+
typeof this.spec.label === "string" ? this.spec.label : humanizeProcessLabel(this.actionId),
|
|
1767
1549
|
acceptanceState:
|
|
1768
1550
|
(this.spec.acceptanceState as
|
|
1769
1551
|
| ProcessHumanTurnActionSpec<TParams, TState>["acceptanceState"]
|
|
@@ -1834,27 +1616,22 @@ export class ExternalActionBuilder<
|
|
|
1834
1616
|
}
|
|
1835
1617
|
|
|
1836
1618
|
export class HumanFlowBuilder<TParams = unknown, TState = unknown>
|
|
1619
|
+
extends ExternalActionTurnBuilder<TParams, TState>
|
|
1837
1620
|
implements FlowHumanTurn<TParams, TState>
|
|
1838
1621
|
{
|
|
1839
|
-
|
|
1622
|
+
protected readonly turnKind = "Human";
|
|
1840
1623
|
private reviewProductName: string | undefined;
|
|
1841
1624
|
private reviewSemanticRef: ProcessSemanticEntryRefKey | undefined;
|
|
1842
1625
|
private operatorAttentionValue: HumanTurnOperatorAttention | undefined;
|
|
1843
1626
|
private turnCommentary: string | undefined;
|
|
1844
1627
|
private notes: HumanTurnDefinition<TParams, TState>["notesFields"] | undefined;
|
|
1845
1628
|
private actions = new Map<string, HumanActionBuilder<TParams, TState>>();
|
|
1846
|
-
private externalActionBuilders: ExternalActionBuilderMap<TParams, TState> = new Map();
|
|
1847
|
-
|
|
1848
|
-
constructor(private readonly turnId: TurnId) {}
|
|
1849
|
-
|
|
1850
|
-
get id(): TurnId {
|
|
1851
|
-
return this.turnId;
|
|
1852
|
-
}
|
|
1853
1629
|
|
|
1854
1630
|
get definition(): HumanTurnDefinition<TParams, TState> {
|
|
1855
1631
|
if (!this.turnDescription) {
|
|
1856
1632
|
throw new Error(`Human turn '${this.turnId}' must declare .description(...)`);
|
|
1857
1633
|
}
|
|
1634
|
+
const externalActions = this.buildExternalActions();
|
|
1858
1635
|
return {
|
|
1859
1636
|
kind: "human",
|
|
1860
1637
|
description: this.turnDescription,
|
|
@@ -1866,17 +1643,10 @@ export class HumanFlowBuilder<TParams = unknown, TState = unknown>
|
|
|
1866
1643
|
actions: Object.fromEntries(
|
|
1867
1644
|
[...this.actions.entries()].map(([actionId, builder]) => [actionId, builder.build()]),
|
|
1868
1645
|
),
|
|
1869
|
-
...(
|
|
1870
|
-
? { externalActions: buildExternalActions(this.externalActionBuilders) }
|
|
1871
|
-
: {}),
|
|
1646
|
+
...(externalActions ? { externalActions } : {}),
|
|
1872
1647
|
};
|
|
1873
1648
|
}
|
|
1874
1649
|
|
|
1875
|
-
description(description: string): this {
|
|
1876
|
-
this.turnDescription = description;
|
|
1877
|
-
return this;
|
|
1878
|
-
}
|
|
1879
|
-
|
|
1880
1650
|
reviewProduct(productName: string): this {
|
|
1881
1651
|
const normalized = normalizeProductName(productName);
|
|
1882
1652
|
this.reviewProductName = normalized;
|
|
@@ -1914,27 +1684,6 @@ export class HumanFlowBuilder<TParams = unknown, TState = unknown>
|
|
|
1914
1684
|
this.actions.set(actionId, builder);
|
|
1915
1685
|
return this;
|
|
1916
1686
|
}
|
|
1917
|
-
|
|
1918
|
-
externalAction<
|
|
1919
|
-
TEvent = unknown,
|
|
1920
|
-
TInput extends Record<string, unknown> = Record<string, unknown>,
|
|
1921
|
-
>(
|
|
1922
|
-
externalActionId: string,
|
|
1923
|
-
source: ExternalActionSource<TParams, TState, TEvent, TInput>,
|
|
1924
|
-
configure: (
|
|
1925
|
-
external: ExternalActionBuilder<TParams, TState, TEvent, TInput>,
|
|
1926
|
-
) => ExternalActionBuilder<TParams, TState, TEvent, TInput> | undefined,
|
|
1927
|
-
): this {
|
|
1928
|
-
addExternalAction({
|
|
1929
|
-
builders: this.externalActionBuilders,
|
|
1930
|
-
turnId: this.turnId,
|
|
1931
|
-
turnKind: "Human",
|
|
1932
|
-
externalActionId,
|
|
1933
|
-
source,
|
|
1934
|
-
configure,
|
|
1935
|
-
});
|
|
1936
|
-
return this;
|
|
1937
|
-
}
|
|
1938
1687
|
}
|
|
1939
1688
|
|
|
1940
1689
|
export class ExternalRouteBuilder<
|
|
@@ -1945,11 +1694,7 @@ export class ExternalRouteBuilder<
|
|
|
1945
1694
|
> implements FlowExternalTurn<TParams, TState>
|
|
1946
1695
|
{
|
|
1947
1696
|
private externalEffect: ExternalSourceEffect<TParams, TState, TEvent, TInput> | undefined;
|
|
1948
|
-
private target:
|
|
1949
|
-
| { kind: "to"; turnId: TurnId }
|
|
1950
|
-
| { kind: "complete" }
|
|
1951
|
-
| { kind: "lifecycleStatus"; status: ProcessTurnTerminalLifecycleStatus }
|
|
1952
|
-
| null = null;
|
|
1697
|
+
private target: FlowTargetSpec = null;
|
|
1953
1698
|
constructor(
|
|
1954
1699
|
private readonly parent: ExternalFlowBuilder<TParams, TState>,
|
|
1955
1700
|
private readonly source: ExternalActionSource<TParams, TState, TEvent, TInput>,
|
|
@@ -1963,12 +1708,7 @@ export class ExternalRouteBuilder<
|
|
|
1963
1708
|
return this.parent.definition;
|
|
1964
1709
|
}
|
|
1965
1710
|
|
|
1966
|
-
private setTarget(
|
|
1967
|
-
next:
|
|
1968
|
-
| { kind: "to"; turnId: TurnId }
|
|
1969
|
-
| { kind: "complete" }
|
|
1970
|
-
| { kind: "lifecycleStatus"; status: ProcessTurnTerminalLifecycleStatus },
|
|
1971
|
-
): this {
|
|
1711
|
+
private setTarget(next: Exclude<FlowTargetSpec, null>): this {
|
|
1972
1712
|
if (this.target) {
|
|
1973
1713
|
throw new Error("External source route already declares a target");
|
|
1974
1714
|
}
|
|
@@ -2012,36 +1752,22 @@ export class ExternalRouteBuilder<
|
|
|
2012
1752
|
return this;
|
|
2013
1753
|
}
|
|
2014
1754
|
|
|
2015
|
-
private buildRouteTarget(): {
|
|
2016
|
-
to?: TurnId;
|
|
2017
|
-
complete?: boolean;
|
|
2018
|
-
lifecycleStatus?: ProcessTurnTerminalLifecycleStatus;
|
|
2019
|
-
} {
|
|
2020
|
-
return buildRouteTargetSpec(this.target);
|
|
2021
|
-
}
|
|
2022
|
-
|
|
2023
1755
|
build() {
|
|
2024
1756
|
return {
|
|
2025
1757
|
source: this.source,
|
|
2026
|
-
...this.
|
|
1758
|
+
...buildRouteTargetSpec(this.target),
|
|
2027
1759
|
...(this.externalEffect ? { effect: this.externalEffect } : {}),
|
|
2028
1760
|
};
|
|
2029
1761
|
}
|
|
2030
1762
|
}
|
|
2031
1763
|
|
|
2032
1764
|
export class ExternalFlowBuilder<TParams = unknown, TState = unknown>
|
|
1765
|
+
extends DescribedTurnBuilder
|
|
2033
1766
|
implements FlowExternalTurn<TParams, TState>
|
|
2034
1767
|
{
|
|
2035
|
-
private turnDescription: string | null = null;
|
|
2036
1768
|
private routeBuilders: ExternalRouteBuilder<TParams, TState, unknown, Record<string, unknown>>[] =
|
|
2037
1769
|
[];
|
|
2038
1770
|
|
|
2039
|
-
constructor(private readonly turnId: TurnId) {}
|
|
2040
|
-
|
|
2041
|
-
get id(): TurnId {
|
|
2042
|
-
return this.turnId;
|
|
2043
|
-
}
|
|
2044
|
-
|
|
2045
1771
|
get definition(): ExternalTurnDefinition<TParams, TState> {
|
|
2046
1772
|
if (!this.turnDescription) {
|
|
2047
1773
|
this.turnDescription =
|
|
@@ -2057,11 +1783,6 @@ export class ExternalFlowBuilder<TParams = unknown, TState = unknown>
|
|
|
2057
1783
|
};
|
|
2058
1784
|
}
|
|
2059
1785
|
|
|
2060
|
-
description(description: string): this {
|
|
2061
|
-
this.turnDescription = description;
|
|
2062
|
-
return this;
|
|
2063
|
-
}
|
|
2064
|
-
|
|
2065
1786
|
from<TEvent = unknown, TInput extends Record<string, unknown> = Record<string, unknown>>(
|
|
2066
1787
|
source: ExternalActionSource<TParams, TState, TEvent, TInput>,
|
|
2067
1788
|
): ExternalRouteBuilder<TParams, TState, TEvent, TInput> {
|
|
@@ -2075,6 +1796,12 @@ export class ExternalFlowBuilder<TParams = unknown, TState = unknown>
|
|
|
2075
1796
|
|
|
2076
1797
|
type Hook<TApi> = (api: TApi) => void;
|
|
2077
1798
|
|
|
1799
|
+
function chainHooks<TApi>(hooks: readonly Hook<TApi>[]): Hook<TApi> {
|
|
1800
|
+
return (api) => {
|
|
1801
|
+
for (const hook of hooks) hook(api);
|
|
1802
|
+
};
|
|
1803
|
+
}
|
|
1804
|
+
|
|
2078
1805
|
export abstract class FlowComponentBuilder<TParams = unknown, TState = unknown> {
|
|
2079
1806
|
readonly turns: FlowTurn<TParams, TState>[] = [];
|
|
2080
1807
|
readonly serverHooks: Hook<ServerProcessAPI<TParams, TState>>[] = [];
|
|
@@ -2154,13 +1881,11 @@ export class FlowProcessBuilder<TParams = unknown, TState = unknown> extends Flo
|
|
|
2154
1881
|
private paramsCodec: Codec<TParams> | null = null;
|
|
2155
1882
|
private stateCodec: Codec<TState> | null = null;
|
|
2156
1883
|
private initialStateFn: ((params: TParams) => TState) | null = null;
|
|
2157
|
-
private repositoryCredentialsFn:
|
|
2158
|
-
|
|
2159
|
-
params: TParams;
|
|
2160
|
-
projects: readonly RepositoryCredentialProject[];
|
|
2161
|
-
}) => readonly RepositoryCredentialRequirement[])
|
|
2162
|
-
| undefined;
|
|
1884
|
+
private repositoryCredentialsFn: ProcessDefinition<TParams, TState>["repositoryCredentials"];
|
|
1885
|
+
private storageSizeResolver: ProcessDefinition<TParams, TState>["resolveStorageSize"];
|
|
2163
1886
|
private processPiConfig: ProcessPiConfig | undefined;
|
|
1887
|
+
private developmentTools = false;
|
|
1888
|
+
private docker = false;
|
|
2164
1889
|
|
|
2165
1890
|
constructor(processId: string) {
|
|
2166
1891
|
super();
|
|
@@ -2208,20 +1933,32 @@ export class FlowProcessBuilder<TParams = unknown, TState = unknown> extends Flo
|
|
|
2208
1933
|
}
|
|
2209
1934
|
|
|
2210
1935
|
repositoryCredentials(
|
|
2211
|
-
fn:
|
|
2212
|
-
params: TParams;
|
|
2213
|
-
projects: readonly RepositoryCredentialProject[];
|
|
2214
|
-
}) => readonly RepositoryCredentialRequirement[],
|
|
1936
|
+
fn: NonNullable<ProcessDefinition<TParams, TState>["repositoryCredentials"]>,
|
|
2215
1937
|
): this {
|
|
2216
1938
|
this.repositoryCredentialsFn = fn;
|
|
2217
1939
|
return this;
|
|
2218
1940
|
}
|
|
2219
1941
|
|
|
1942
|
+
/** Resolve new process-volume capacity on the server; operator configuration wins. */
|
|
1943
|
+
resolveStorageSize(
|
|
1944
|
+
fn: NonNullable<ProcessDefinition<TParams, TState>["resolveStorageSize"]>,
|
|
1945
|
+
): this {
|
|
1946
|
+
this.storageSizeResolver = fn;
|
|
1947
|
+
return this;
|
|
1948
|
+
}
|
|
1949
|
+
|
|
2220
1950
|
piConfig(config: ProcessPiConfig): this {
|
|
2221
1951
|
this.processPiConfig = config;
|
|
2222
1952
|
return this;
|
|
2223
1953
|
}
|
|
2224
1954
|
|
|
1955
|
+
/** Declare runner-provided process runtime capabilities. */
|
|
1956
|
+
runtime(capabilities: { developmentTools?: boolean; docker?: boolean }): this {
|
|
1957
|
+
this.developmentTools = capabilities.developmentTools === true;
|
|
1958
|
+
this.docker = capabilities.docker === true;
|
|
1959
|
+
return this;
|
|
1960
|
+
}
|
|
1961
|
+
|
|
2225
1962
|
use(fragment: FlowFragmentBuilder<TParams, TState>): this {
|
|
2226
1963
|
this.turns.push(...fragment.turns);
|
|
2227
1964
|
this.serverHooks.push(...fragment.serverHooks);
|
|
@@ -2253,53 +1990,6 @@ export class FlowProcessBuilder<TParams = unknown, TState = unknown> extends Flo
|
|
|
2253
1990
|
turns[turn.id] = turn.definition;
|
|
2254
1991
|
}
|
|
2255
1992
|
|
|
2256
|
-
const publishedProducts = new Set<string>();
|
|
2257
|
-
for (const turnDef of Object.values(turns)) {
|
|
2258
|
-
if (turnDef.kind === "llm") {
|
|
2259
|
-
if (turnDef.publishedProduct) {
|
|
2260
|
-
normalizeProductName(turnDef.publishedProduct);
|
|
2261
|
-
publishedProducts.add(turnDef.publishedProduct);
|
|
2262
|
-
}
|
|
2263
|
-
for (const outcome of Object.values(turnDef.outcomes ?? {})) {
|
|
2264
|
-
if (outcome?.publishedProduct) {
|
|
2265
|
-
normalizeProductName(outcome.publishedProduct);
|
|
2266
|
-
publishedProducts.add(outcome.publishedProduct);
|
|
2267
|
-
}
|
|
2268
|
-
}
|
|
2269
|
-
}
|
|
2270
|
-
if (turnDef.kind === "human") {
|
|
2271
|
-
for (const action of Object.values(turnDef.actions)) {
|
|
2272
|
-
for (const field of action.form?.fields ?? []) {
|
|
2273
|
-
const productName = formFieldPublishedProductName(field);
|
|
2274
|
-
if (productName) {
|
|
2275
|
-
publishedProducts.add(productName);
|
|
2276
|
-
}
|
|
2277
|
-
}
|
|
2278
|
-
}
|
|
2279
|
-
for (const externalAction of Object.values(turnDef.externalActions ?? {})) {
|
|
2280
|
-
if (externalAction.publishInput) {
|
|
2281
|
-
publishedProducts.add(normalizeProductName(externalAction.publishInput.productName));
|
|
2282
|
-
}
|
|
2283
|
-
}
|
|
2284
|
-
}
|
|
2285
|
-
}
|
|
2286
|
-
for (const [turnId, turnDef] of Object.entries(turns)) {
|
|
2287
|
-
if (turnDef.kind !== "llm") {
|
|
2288
|
-
continue;
|
|
2289
|
-
}
|
|
2290
|
-
for (const productName of [
|
|
2291
|
-
...(turnDef.consumedProducts ?? []),
|
|
2292
|
-
...(turnDef.optionalConsumedProducts ?? []),
|
|
2293
|
-
]) {
|
|
2294
|
-
normalizeProductName(productName);
|
|
2295
|
-
if (!publishedProducts.has(productName)) {
|
|
2296
|
-
throw new Error(
|
|
2297
|
-
`Flow process '${this.processId}' turn '${turnId}' consumes product '${productName}' that is never published by this process`,
|
|
2298
|
-
);
|
|
2299
|
-
}
|
|
2300
|
-
}
|
|
2301
|
-
}
|
|
2302
|
-
|
|
2303
1993
|
const input: DefinedProcessInput<TParams, TState> = {
|
|
2304
1994
|
id: this.processId,
|
|
2305
1995
|
displayName: this.processDisplayName,
|
|
@@ -2311,49 +2001,32 @@ export class FlowProcessBuilder<TParams = unknown, TState = unknown> extends Flo
|
|
|
2311
2001
|
paramsCodec: this.paramsCodec,
|
|
2312
2002
|
stateCodec: this.stateCodec,
|
|
2313
2003
|
initialState: this.initialStateFn,
|
|
2314
|
-
...(this.
|
|
2315
|
-
? { repositoryCredentials: this.repositoryCredentialsFn }
|
|
2316
|
-
: {}),
|
|
2317
|
-
...(this.processPiConfig ? { piConfig: this.processPiConfig } : {}),
|
|
2318
|
-
turns,
|
|
2319
|
-
...(this.serverHooks.length > 0
|
|
2320
|
-
? {
|
|
2321
|
-
server: (api) => {
|
|
2322
|
-
for (const hook of this.serverHooks) {
|
|
2323
|
-
hook(api);
|
|
2324
|
-
}
|
|
2325
|
-
},
|
|
2326
|
-
}
|
|
2327
|
-
: {}),
|
|
2328
|
-
...(this.uiHooks.length > 0
|
|
2329
|
-
? {
|
|
2330
|
-
ui: (api) => {
|
|
2331
|
-
for (const hook of this.uiHooks) {
|
|
2332
|
-
hook(api);
|
|
2333
|
-
}
|
|
2334
|
-
},
|
|
2335
|
-
}
|
|
2336
|
-
: {}),
|
|
2337
|
-
...(this.launcherHooks.length > 0
|
|
2004
|
+
...(this.developmentTools || this.docker
|
|
2338
2005
|
? {
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
}
|
|
2006
|
+
runtime: {
|
|
2007
|
+
...(this.developmentTools ? { developmentTools: true } : {}),
|
|
2008
|
+
...(this.docker ? { docker: true } : {}),
|
|
2343
2009
|
},
|
|
2344
2010
|
}
|
|
2345
2011
|
: {}),
|
|
2346
|
-
...(this.
|
|
2347
|
-
? {
|
|
2348
|
-
watchers: (api) => {
|
|
2349
|
-
for (const hook of this.watcherHooks) {
|
|
2350
|
-
hook(api);
|
|
2351
|
-
}
|
|
2352
|
-
},
|
|
2353
|
-
}
|
|
2012
|
+
...(this.repositoryCredentialsFn
|
|
2013
|
+
? { repositoryCredentials: this.repositoryCredentialsFn }
|
|
2354
2014
|
: {}),
|
|
2015
|
+
...(this.storageSizeResolver ? { resolveStorageSize: this.storageSizeResolver } : {}),
|
|
2016
|
+
...(this.processPiConfig ? { piConfig: this.processPiConfig } : {}),
|
|
2017
|
+
turns,
|
|
2018
|
+
...(this.serverHooks.length > 0 ? { server: chainHooks(this.serverHooks) } : {}),
|
|
2019
|
+
...(this.uiHooks.length > 0 ? { ui: chainHooks(this.uiHooks) } : {}),
|
|
2020
|
+
...(this.launcherHooks.length > 0 ? { launchers: chainHooks(this.launcherHooks) } : {}),
|
|
2021
|
+
...(this.watcherHooks.length > 0 ? { watchers: chainHooks(this.watcherHooks) } : {}),
|
|
2355
2022
|
};
|
|
2356
|
-
|
|
2023
|
+
const process = defineProcess(input);
|
|
2024
|
+
const errors = validateProcessGraphProducts(
|
|
2025
|
+
toProcessGraphView(process as unknown as ProcessGraphSource),
|
|
2026
|
+
);
|
|
2027
|
+
if (errors.length > 0)
|
|
2028
|
+
throw new Error(`Flow process '${this.processId}': ${errors.join("; ")}`);
|
|
2029
|
+
return process;
|
|
2357
2030
|
}
|
|
2358
2031
|
}
|
|
2359
2032
|
|
|
@@ -2366,11 +2039,6 @@ export const flow = {
|
|
|
2366
2039
|
): AutomaticFlowBuilder<TParams, TState> {
|
|
2367
2040
|
return new AutomaticFlowBuilder<TParams, TState>(turnId);
|
|
2368
2041
|
},
|
|
2369
|
-
serverAutomatic<TParams = unknown, TState = unknown>(
|
|
2370
|
-
turnId: TurnId,
|
|
2371
|
-
): ServerAutomaticFlowBuilder<TParams, TState> {
|
|
2372
|
-
return new ServerAutomaticFlowBuilder<TParams, TState>(turnId);
|
|
2373
|
-
},
|
|
2374
2042
|
human<TParams = unknown, TState = unknown>(turnId: TurnId): HumanFlowBuilder<TParams, TState> {
|
|
2375
2043
|
return new HumanFlowBuilder<TParams, TState>(turnId);
|
|
2376
2044
|
},
|