@leitwerk-dev/process-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LICENSE +13 -0
- 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/chunk-5RJZ2E7D.js +58 -0
- package/dist/chunk-5RJZ2E7D.js.map +1 -0
- package/dist/chunk-ITPH65G6.js +197 -0
- package/dist/chunk-ITPH65G6.js.map +1 -0
- package/dist/chunk-LIIP7FMU.js +48 -0
- package/dist/chunk-LIIP7FMU.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-CnlL69Tu.d.ts +1281 -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 +11 -0
- package/dist/git-binary.d.ts.map +1 -0
- package/dist/git-binary.js +38 -0
- package/dist/git-binary.js.map +1 -0
- 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 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- 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 +48 -0
- package/dist/leaf-outcome-renderer.d.ts.map +1 -0
- package/dist/leaf-outcome-renderer.js +69 -0
- package/dist/leaf-outcome-renderer.js.map +1 -0
- 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 +59 -0
- package/dist/pi-config.d.ts.map +1 -0
- package/dist/pi-config.js +182 -0
- package/dist/pi-config.js.map +1 -0
- 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 +10 -0
- package/dist/review-flow.d.ts.map +1 -0
- package/dist/review-flow.js +44 -0
- package/dist/review-flow.js.map +1 -0
- package/dist/runtime-internals.d.ts +3 -0
- package/dist/runtime-internals.d.ts.map +1 -0
- package/dist/runtime-internals.js +3 -0
- package/dist/runtime-internals.js.map +1 -0
- 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 +72 -0
- package/src/browser-ui-extension.ts +32 -0
- package/src/capabilities.ts +16 -0
- package/src/codecs.ts +10 -0
- package/src/commit-message.ts +9 -0
- package/src/core-capabilities.ts +526 -0
- package/src/define-process.ts +1879 -0
- package/src/event-bus.ts +35 -0
- package/src/extension-api.ts +600 -0
- package/src/flow.ts +2268 -0
- package/src/form-contract.ts +11 -0
- package/src/fs-utils.ts +28 -0
- package/src/git-binary.ts +40 -0
- package/src/graph-fragments.ts +110 -0
- package/src/host-capabilities.ts +60 -0
- package/src/index.ts +468 -0
- package/src/launcher-contract.ts +8 -0
- package/src/leaf-outcome-renderer.ts +112 -0
- package/src/leaf-outcomes.ts +74 -0
- package/src/model-provider.ts +461 -0
- package/src/pi-config.ts +268 -0
- package/src/process-definition-internals.ts +46 -0
- package/src/process-flow-view.ts +479 -0
- package/src/process-graph.ts +309 -0
- package/src/process-launcher-builder.ts +59 -0
- package/src/process-watcher-builder.ts +72 -0
- package/src/registry-utils.ts +31 -0
- package/src/repository-git-env.ts +35 -0
- package/src/review-flow.ts +66 -0
- package/src/runtime-internals.ts +7 -0
- package/src/server-events.ts +105 -0
- package/src/server-process-builder.ts +90 -0
- package/src/state-helpers.ts +36 -0
- package/src/subprocess-env.ts +48 -0
- package/src/tool-renderers.ts +92 -0
- package/src/turn-semantics.ts +554 -0
- package/src/types.ts +391 -0
- package/src/ui-process-builder.ts +46 -0
- package/src/worker-process-builder.ts +33 -0
package/src/flow.ts
ADDED
|
@@ -0,0 +1,2268 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
assertValidProcessProductName,
|
|
4
|
+
createReviewSubject,
|
|
5
|
+
humanizeProcessLabel,
|
|
6
|
+
type ProcessInstance,
|
|
7
|
+
type ProcessProject,
|
|
8
|
+
type ProcessSemanticEntryRefKey,
|
|
9
|
+
type ProcessTurnStartSelection,
|
|
10
|
+
type ProcessTurnTerminalLifecycleStatus,
|
|
11
|
+
type ReviewSubject,
|
|
12
|
+
type TurnId,
|
|
13
|
+
} from "@leitwerk-dev/domain";
|
|
14
|
+
import type {
|
|
15
|
+
AutomaticTurnDefinition,
|
|
16
|
+
DefinedProcessInput,
|
|
17
|
+
ExternalTurnDefinition,
|
|
18
|
+
HumanTurnDefinition,
|
|
19
|
+
HumanTurnOperatorAttention,
|
|
20
|
+
LlmModelPurpose,
|
|
21
|
+
LlmTurnDefinition,
|
|
22
|
+
ProcessDefinition,
|
|
23
|
+
ProcessEffectPlan,
|
|
24
|
+
ProcessHumanTurnActionSpec,
|
|
25
|
+
ProcessHumanTurnExternalActionSpec,
|
|
26
|
+
ProcessOutcomeEffect,
|
|
27
|
+
ProcessOutcomeExecution,
|
|
28
|
+
ProcessRuntimeTurnContext,
|
|
29
|
+
ProcessToolOutcomeSpec,
|
|
30
|
+
ProcessTurnEndSpec,
|
|
31
|
+
ServerAutomaticTurnDefinition,
|
|
32
|
+
ServerAutomaticTurnRestartBehavior,
|
|
33
|
+
TurnDefinition,
|
|
34
|
+
TurnDefinitionRecord,
|
|
35
|
+
} from "./define-process.js";
|
|
36
|
+
import { defineProcess } from "./define-process.js";
|
|
37
|
+
import type {
|
|
38
|
+
Codec,
|
|
39
|
+
ExternalActionSource,
|
|
40
|
+
ExternalSourceEffect,
|
|
41
|
+
ExternalSourceEffectContext,
|
|
42
|
+
FormDefinition,
|
|
43
|
+
ProcessActionDefinition,
|
|
44
|
+
ProcessLauncherAPI,
|
|
45
|
+
ProcessLauncherDefinition,
|
|
46
|
+
ProcessWatcherAPI,
|
|
47
|
+
ProcessWatcherDefinition,
|
|
48
|
+
RepositoryCredentialProject,
|
|
49
|
+
RepositoryCredentialRequirement,
|
|
50
|
+
ServerProcessAPI,
|
|
51
|
+
UiProcessAPI,
|
|
52
|
+
WorkerCompleteInput,
|
|
53
|
+
} from "./extension-api.js";
|
|
54
|
+
import type { OutcomeToolParameterSpec, PiBuiltInToolName, ProcessPiConfig } from "./types.js";
|
|
55
|
+
|
|
56
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
57
|
+
|
|
58
|
+
export const DEFAULT_PLAN_RESULT_OUTCOME_ID = "plan_saved" as const;
|
|
59
|
+
export const DEFAULT_FLOW_PRODUCT_NAME = "default" as const;
|
|
60
|
+
const ASSISTANT_OUTPUT_TURN_RESULT = { mode: "assistant_output", required: true } as const;
|
|
61
|
+
const OUTCOME_TOOL_MARKDOWN_PARAMETER_NAME = "markdown" as const;
|
|
62
|
+
const OUTCOME_TOOL_ARGUMENT_TURN_RESULT = {
|
|
63
|
+
mode: "outcome_tool_argument",
|
|
64
|
+
parameterName: OUTCOME_TOOL_MARKDOWN_PARAMETER_NAME,
|
|
65
|
+
required: true,
|
|
66
|
+
} as const;
|
|
67
|
+
|
|
68
|
+
export interface FlowLlmTurn<
|
|
69
|
+
TParams = unknown,
|
|
70
|
+
TState = unknown,
|
|
71
|
+
TOutcome extends string = string,
|
|
72
|
+
> {
|
|
73
|
+
id: TurnId;
|
|
74
|
+
definition: LlmTurnDefinition<TOutcome, TParams, TState>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface FlowAutomaticTurn<
|
|
78
|
+
TParams = unknown,
|
|
79
|
+
TState = unknown,
|
|
80
|
+
TOutcome extends string = string,
|
|
81
|
+
> {
|
|
82
|
+
id: TurnId;
|
|
83
|
+
definition: AutomaticTurnDefinition<TOutcome, TParams, TState>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface FlowServerAutomaticTurn<
|
|
87
|
+
TParams = unknown,
|
|
88
|
+
TState = unknown,
|
|
89
|
+
TOutcome extends string = string,
|
|
90
|
+
> {
|
|
91
|
+
id: TurnId;
|
|
92
|
+
definition: ServerAutomaticTurnDefinition<TOutcome, TParams, TState>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface FlowHumanTurn<TParams = unknown, TState = unknown> {
|
|
96
|
+
id: TurnId;
|
|
97
|
+
definition: HumanTurnDefinition<TParams, TState>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface FlowExternalTurn<TParams = unknown, TState = unknown> {
|
|
101
|
+
id: TurnId;
|
|
102
|
+
definition: ExternalTurnDefinition<TParams, TState>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type FlowTurn<TParams = unknown, TState = unknown> =
|
|
106
|
+
| FlowLlmTurn<TParams, TState, string>
|
|
107
|
+
| FlowAutomaticTurn<TParams, TState, string>
|
|
108
|
+
| FlowServerAutomaticTurn<TParams, TState, string>
|
|
109
|
+
| FlowHumanTurn<TParams, TState>
|
|
110
|
+
| FlowExternalTurn<TParams, TState>
|
|
111
|
+
| { id: TurnId; definition: TurnDefinition<TParams, TState> };
|
|
112
|
+
|
|
113
|
+
export interface PlanFieldOptions {
|
|
114
|
+
description?: string;
|
|
115
|
+
requiredErrorCode?: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface PlanAcceptanceCriteriaOptions extends PlanFieldOptions {
|
|
119
|
+
minItems?: number;
|
|
120
|
+
minItemsErrorCode?: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface PlanSavedStateInput<TParams = unknown, TState = unknown> {
|
|
124
|
+
effect: ProcessOutcomeEffect<TParams, TState>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface FlowRepoContext {
|
|
128
|
+
key: string;
|
|
129
|
+
fsPath: string;
|
|
130
|
+
workspaceClonePath: string;
|
|
131
|
+
baseBranch: string;
|
|
132
|
+
workBranch: string;
|
|
133
|
+
locator: string;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export interface FlowRepoLookup {
|
|
137
|
+
get(key: string): FlowRepoContext;
|
|
138
|
+
optional(key: string): FlowRepoContext | undefined;
|
|
139
|
+
all(): FlowRepoContext[];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface FlowPromptContext<
|
|
143
|
+
TParams = unknown,
|
|
144
|
+
TState = unknown,
|
|
145
|
+
TConsumedProducts extends string = string,
|
|
146
|
+
> {
|
|
147
|
+
process: ProcessInstance;
|
|
148
|
+
projects: readonly ProcessProject[];
|
|
149
|
+
params: TParams;
|
|
150
|
+
state: TState;
|
|
151
|
+
workspaceRoot?: string;
|
|
152
|
+
prompts: {
|
|
153
|
+
initial: string;
|
|
154
|
+
};
|
|
155
|
+
input: Readonly<Partial<Record<TConsumedProducts, string>>>;
|
|
156
|
+
repo: FlowRepoLookup;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface FlowAutomaticRunContext<TParams = unknown, TState = unknown> {
|
|
160
|
+
process: ProcessInstance;
|
|
161
|
+
projects: readonly ProcessProject[];
|
|
162
|
+
params: TParams;
|
|
163
|
+
state: TState;
|
|
164
|
+
workspaceRoot?: string;
|
|
165
|
+
repo: FlowRepoLookup;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface FlowLlmOutcomeEffectContext<TParams = unknown, TState = unknown> {
|
|
169
|
+
process: ProcessInstance;
|
|
170
|
+
projects: readonly ProcessProject[];
|
|
171
|
+
params: TParams;
|
|
172
|
+
state: TState;
|
|
173
|
+
output: { content: string | null } | null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export interface FlowAutomaticOutcomeEffectContext<TParams = unknown, TState = unknown> {
|
|
177
|
+
process: ProcessInstance;
|
|
178
|
+
projects: readonly ProcessProject[];
|
|
179
|
+
params: TParams;
|
|
180
|
+
state: TState;
|
|
181
|
+
output: { content: string | null } | null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export interface FlowExternalSourceContext<
|
|
185
|
+
TParams = unknown,
|
|
186
|
+
TState = unknown,
|
|
187
|
+
TEvent = unknown,
|
|
188
|
+
TInput extends Record<string, unknown> = Record<string, unknown>,
|
|
189
|
+
> extends ExternalSourceEffectContext<TParams, TState, TEvent, TInput> {}
|
|
190
|
+
|
|
191
|
+
type FlowOutcomeEffectInput<TParams, TState, TContext> = {
|
|
192
|
+
ctx: TContext;
|
|
193
|
+
event: ProcessOutcomeExecution<TParams, TState>["event"];
|
|
194
|
+
turnId: TurnId;
|
|
195
|
+
outcome: string;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
type FlowOutcomeEffect<TParams, TState, TContext> = (
|
|
199
|
+
input: FlowOutcomeEffectInput<TParams, TState, TContext>,
|
|
200
|
+
) => MaybePromise<ProcessEffectPlan<TState> | undefined>;
|
|
201
|
+
|
|
202
|
+
type FlowOutcomeStateEffect<TParams, TState, TContext> = (
|
|
203
|
+
input: FlowOutcomeEffectInput<TParams, TState, TContext>,
|
|
204
|
+
) => MaybePromise<TState>;
|
|
205
|
+
|
|
206
|
+
type SnapshotContext<TParams, TState> = {
|
|
207
|
+
process: ProcessInstance;
|
|
208
|
+
projects: readonly ProcessProject[];
|
|
209
|
+
params: TParams;
|
|
210
|
+
state: TState;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
function textField(input: {
|
|
214
|
+
description: string;
|
|
215
|
+
requiredErrorCode: string;
|
|
216
|
+
}): OutcomeToolParameterSpec {
|
|
217
|
+
return {
|
|
218
|
+
type: "string",
|
|
219
|
+
description: input.description,
|
|
220
|
+
required: true,
|
|
221
|
+
requiredErrorCode: input.requiredErrorCode,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function stringArrayField(input: {
|
|
226
|
+
description: string;
|
|
227
|
+
requiredErrorCode: string;
|
|
228
|
+
minItems: number;
|
|
229
|
+
minItemsErrorCode: string;
|
|
230
|
+
}): OutcomeToolParameterSpec {
|
|
231
|
+
return {
|
|
232
|
+
type: "array",
|
|
233
|
+
description: input.description,
|
|
234
|
+
items: { type: "string" },
|
|
235
|
+
required: true,
|
|
236
|
+
requiredErrorCode: input.requiredErrorCode,
|
|
237
|
+
minItems: input.minItems,
|
|
238
|
+
minItemsErrorCode: input.minItemsErrorCode,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function normalizeProductName(productName: string): string {
|
|
243
|
+
assertValidProcessProductName(productName);
|
|
244
|
+
return productName;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function toRequiredErrorCode(name: string): string {
|
|
248
|
+
const snake = name
|
|
249
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
250
|
+
.replace(/[^a-zA-Z0-9]+/g, "_")
|
|
251
|
+
.replace(/^_+|_+$/g, "")
|
|
252
|
+
.toLowerCase();
|
|
253
|
+
return `${snake || "value"}_required`;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function readInitialPrompt<TParams>(params: TParams): string {
|
|
257
|
+
if (typeof params === "object" && params !== null && "prompt" in params) {
|
|
258
|
+
const value = (params as { prompt?: unknown }).prompt;
|
|
259
|
+
if (typeof value === "string") {
|
|
260
|
+
return value;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return "";
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function isPathInside(parent: string, child: string): boolean {
|
|
267
|
+
const relative = path.relative(parent, child);
|
|
268
|
+
return (
|
|
269
|
+
relative !== "" &&
|
|
270
|
+
relative !== ".." &&
|
|
271
|
+
!relative.startsWith(`..${path.sep}`) &&
|
|
272
|
+
!path.isAbsolute(relative)
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function safeJoinInside(parent: string, childPath: string, context: string): string {
|
|
277
|
+
const parentPath = path.resolve(parent);
|
|
278
|
+
const targetPath = path.resolve(parentPath, childPath);
|
|
279
|
+
if (!isPathInside(parentPath, targetPath)) {
|
|
280
|
+
throw new Error(`${context} resolves outside '${parentPath}'`);
|
|
281
|
+
}
|
|
282
|
+
return targetPath;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function toWorkspaceClonePath(relativePath: string): string {
|
|
286
|
+
return `./${relativePath.split(path.sep).join("/")}`;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function resolveSafeProjectWorkspacePath(key: string): {
|
|
290
|
+
relativePath: string;
|
|
291
|
+
workspaceClonePath: string;
|
|
292
|
+
} {
|
|
293
|
+
if (path.isAbsolute(key)) {
|
|
294
|
+
throw new Error(`Flow context project key '${key}' must be a relative workspace path`);
|
|
295
|
+
}
|
|
296
|
+
const virtualWorkspaceRoot = path.resolve(path.sep, "__leitwerk_process_workspace__");
|
|
297
|
+
const virtualTarget = safeJoinInside(
|
|
298
|
+
virtualWorkspaceRoot,
|
|
299
|
+
key,
|
|
300
|
+
`Flow context project key '${key}'`,
|
|
301
|
+
);
|
|
302
|
+
const relativePath = path.relative(virtualWorkspaceRoot, virtualTarget);
|
|
303
|
+
return {
|
|
304
|
+
relativePath,
|
|
305
|
+
workspaceClonePath: toWorkspaceClonePath(relativePath),
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function resolveRepoByKey(input: {
|
|
310
|
+
projects: readonly ProcessProject[];
|
|
311
|
+
workspaceRoot?: string;
|
|
312
|
+
key: string;
|
|
313
|
+
required: true;
|
|
314
|
+
}): FlowRepoContext;
|
|
315
|
+
function resolveRepoByKey(input: {
|
|
316
|
+
projects: readonly ProcessProject[];
|
|
317
|
+
workspaceRoot?: string;
|
|
318
|
+
key: string;
|
|
319
|
+
required: false;
|
|
320
|
+
}): FlowRepoContext | undefined;
|
|
321
|
+
function resolveRepoByKey(input: {
|
|
322
|
+
projects: readonly ProcessProject[];
|
|
323
|
+
workspaceRoot?: string;
|
|
324
|
+
key: string;
|
|
325
|
+
required: boolean;
|
|
326
|
+
}): FlowRepoContext | undefined {
|
|
327
|
+
const requestedKey = input.key.trim();
|
|
328
|
+
if (!requestedKey) {
|
|
329
|
+
throw new Error("Flow repo lookup requires a non-empty project key");
|
|
330
|
+
}
|
|
331
|
+
const matches = input.projects.filter((project) => project.key.trim() === requestedKey);
|
|
332
|
+
if (matches.length === 0) {
|
|
333
|
+
if (!input.required) {
|
|
334
|
+
return undefined;
|
|
335
|
+
}
|
|
336
|
+
const availableKeys = input.projects
|
|
337
|
+
.map((project) => project.key.trim())
|
|
338
|
+
.filter((key) => key !== "")
|
|
339
|
+
.join(", ");
|
|
340
|
+
throw new Error(
|
|
341
|
+
`Flow context requires project '${requestedKey}', but this process has no project with that key${
|
|
342
|
+
availableKeys ? ` (available: ${availableKeys})` : ""
|
|
343
|
+
}`,
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
if (matches.length > 1) {
|
|
347
|
+
throw new Error(`Flow context found duplicate project key '${requestedKey}'`);
|
|
348
|
+
}
|
|
349
|
+
const project = matches[0];
|
|
350
|
+
const key = project.key.trim();
|
|
351
|
+
if (!key) {
|
|
352
|
+
throw new Error("Flow context requires projects to have non-empty keys");
|
|
353
|
+
}
|
|
354
|
+
const baseBranch = project.baseBranch.trim();
|
|
355
|
+
if (!baseBranch) {
|
|
356
|
+
throw new Error(`Flow context project '${key}' has no base branch`);
|
|
357
|
+
}
|
|
358
|
+
const workBranch = (project.workBranch ?? project.baseBranch).trim();
|
|
359
|
+
if (!workBranch) {
|
|
360
|
+
throw new Error(`Flow context project '${key}' has no work branch`);
|
|
361
|
+
}
|
|
362
|
+
const workspacePath = resolveSafeProjectWorkspacePath(key);
|
|
363
|
+
return {
|
|
364
|
+
key,
|
|
365
|
+
workspaceClonePath: workspacePath.workspaceClonePath,
|
|
366
|
+
fsPath:
|
|
367
|
+
typeof input.workspaceRoot === "string" && input.workspaceRoot.trim() !== ""
|
|
368
|
+
? safeJoinInside(
|
|
369
|
+
input.workspaceRoot,
|
|
370
|
+
workspacePath.relativePath,
|
|
371
|
+
`Flow context project '${key}' workspace path`,
|
|
372
|
+
)
|
|
373
|
+
: workspacePath.workspaceClonePath,
|
|
374
|
+
baseBranch,
|
|
375
|
+
workBranch,
|
|
376
|
+
locator: project.repoLocator,
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function createFlowRepoLookup(input: {
|
|
381
|
+
projects: readonly ProcessProject[];
|
|
382
|
+
workspaceRoot?: string;
|
|
383
|
+
}): FlowRepoLookup {
|
|
384
|
+
return {
|
|
385
|
+
get: (key) =>
|
|
386
|
+
resolveRepoByKey({
|
|
387
|
+
projects: input.projects,
|
|
388
|
+
workspaceRoot: input.workspaceRoot,
|
|
389
|
+
key,
|
|
390
|
+
required: true,
|
|
391
|
+
}),
|
|
392
|
+
optional: (key) =>
|
|
393
|
+
resolveRepoByKey({
|
|
394
|
+
projects: input.projects,
|
|
395
|
+
workspaceRoot: input.workspaceRoot,
|
|
396
|
+
key,
|
|
397
|
+
required: false,
|
|
398
|
+
}),
|
|
399
|
+
all: () =>
|
|
400
|
+
input.projects.map((project) =>
|
|
401
|
+
resolveRepoByKey({
|
|
402
|
+
projects: input.projects,
|
|
403
|
+
workspaceRoot: input.workspaceRoot,
|
|
404
|
+
key: project.key,
|
|
405
|
+
required: true,
|
|
406
|
+
}),
|
|
407
|
+
),
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export function createFlowPromptContext<TParams, TState, TConsumedProducts extends string = string>(
|
|
412
|
+
ctx: ProcessRuntimeTurnContext<TParams, TState>,
|
|
413
|
+
consumedProducts: readonly TConsumedProducts[],
|
|
414
|
+
optionalConsumedProducts: readonly string[] = [],
|
|
415
|
+
): FlowPromptContext<TParams, TState, TConsumedProducts> {
|
|
416
|
+
const productInput: Record<string, string> = {};
|
|
417
|
+
for (const productName of consumedProducts) {
|
|
418
|
+
const markdown = ctx.turnResultMarkdownByProduct?.[productName];
|
|
419
|
+
if (typeof markdown !== "string" || markdown.trim() === "") {
|
|
420
|
+
throw new Error(
|
|
421
|
+
`Flow prompt context requires product '${productName}' markdown, but the worker payload did not provide it`,
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
productInput[productName] = markdown;
|
|
425
|
+
}
|
|
426
|
+
for (const productName of optionalConsumedProducts) {
|
|
427
|
+
if (productName in productInput) {
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
const markdown = ctx.turnResultMarkdownByProduct?.[productName];
|
|
431
|
+
if (typeof markdown === "string" && markdown.trim() !== "") {
|
|
432
|
+
productInput[productName] = markdown;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return {
|
|
436
|
+
process: ctx.process,
|
|
437
|
+
projects: ctx.projects,
|
|
438
|
+
params: ctx.params,
|
|
439
|
+
state: ctx.state,
|
|
440
|
+
...(ctx.workspaceRoot ? { workspaceRoot: ctx.workspaceRoot } : {}),
|
|
441
|
+
prompts: {
|
|
442
|
+
initial: readInitialPrompt(ctx.params),
|
|
443
|
+
},
|
|
444
|
+
input: productInput as Partial<Record<TConsumedProducts, string>>,
|
|
445
|
+
repo: createFlowRepoLookup({
|
|
446
|
+
projects: ctx.projects,
|
|
447
|
+
workspaceRoot: ctx.workspaceRoot,
|
|
448
|
+
}),
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
export function createFlowAutomaticRunContext<TParams, TState>(
|
|
453
|
+
ctx: ProcessRuntimeTurnContext<TParams, TState>,
|
|
454
|
+
): FlowAutomaticRunContext<TParams, TState> {
|
|
455
|
+
return {
|
|
456
|
+
process: ctx.process,
|
|
457
|
+
projects: ctx.projects,
|
|
458
|
+
params: ctx.params,
|
|
459
|
+
state: ctx.state,
|
|
460
|
+
...(ctx.workspaceRoot ? { workspaceRoot: ctx.workspaceRoot } : {}),
|
|
461
|
+
repo: createFlowRepoLookup({
|
|
462
|
+
projects: ctx.projects,
|
|
463
|
+
workspaceRoot: ctx.workspaceRoot,
|
|
464
|
+
}),
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function createFlowLlmOutcomeEffectContext<TParams, TState>(input: {
|
|
469
|
+
ctx: SnapshotContext<TParams, TState>;
|
|
470
|
+
event: ProcessOutcomeExecution<TParams, TState>["event"];
|
|
471
|
+
}): FlowLlmOutcomeEffectContext<TParams, TState> {
|
|
472
|
+
return {
|
|
473
|
+
process: input.ctx.process,
|
|
474
|
+
projects: input.ctx.projects,
|
|
475
|
+
params: input.ctx.params,
|
|
476
|
+
state: input.ctx.state,
|
|
477
|
+
output:
|
|
478
|
+
typeof input.event.turnResultMarkdown === "string"
|
|
479
|
+
? { content: input.event.turnResultMarkdown }
|
|
480
|
+
: null,
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function createFlowAutomaticOutcomeEffectContext<TParams, TState>(input: {
|
|
485
|
+
ctx: SnapshotContext<TParams, TState>;
|
|
486
|
+
event: ProcessOutcomeExecution<TParams, TState>["event"];
|
|
487
|
+
}): FlowAutomaticOutcomeEffectContext<TParams, TState> {
|
|
488
|
+
return {
|
|
489
|
+
process: input.ctx.process,
|
|
490
|
+
projects: input.ctx.projects,
|
|
491
|
+
params: input.ctx.params,
|
|
492
|
+
state: input.ctx.state,
|
|
493
|
+
output:
|
|
494
|
+
typeof input.event.turnResultMarkdown === "string"
|
|
495
|
+
? { content: input.event.turnResultMarkdown }
|
|
496
|
+
: null,
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function wrapLlmOutcomeEffect<TParams, TState>(
|
|
501
|
+
effect: FlowOutcomeEffect<TParams, TState, FlowLlmOutcomeEffectContext<TParams, TState>>,
|
|
502
|
+
): ProcessOutcomeEffect<TParams, TState> {
|
|
503
|
+
return (execution) =>
|
|
504
|
+
effect({
|
|
505
|
+
ctx: createFlowLlmOutcomeEffectContext({ ctx: execution.ctx, event: execution.event }),
|
|
506
|
+
event: execution.event,
|
|
507
|
+
turnId: execution.turnId,
|
|
508
|
+
outcome: execution.outcome,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function wrapAutomaticOutcomeEffect<TParams, TState>(
|
|
513
|
+
effect: FlowOutcomeEffect<TParams, TState, FlowAutomaticOutcomeEffectContext<TParams, TState>>,
|
|
514
|
+
): ProcessOutcomeEffect<TParams, TState> {
|
|
515
|
+
return (execution) =>
|
|
516
|
+
effect({
|
|
517
|
+
ctx: createFlowAutomaticOutcomeEffectContext({ ctx: execution.ctx, event: execution.event }),
|
|
518
|
+
event: execution.event,
|
|
519
|
+
turnId: execution.turnId,
|
|
520
|
+
outcome: execution.outcome,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
class RouteAndEffectBuilder<TParams, TState, TContext> {
|
|
525
|
+
protected target:
|
|
526
|
+
| { kind: "to"; turnId: TurnId }
|
|
527
|
+
| { kind: "complete" }
|
|
528
|
+
| { kind: "lifecycleStatus"; status: ProcessTurnTerminalLifecycleStatus }
|
|
529
|
+
| null = null;
|
|
530
|
+
protected flowEffect: FlowOutcomeEffect<TParams, TState, TContext> | undefined;
|
|
531
|
+
|
|
532
|
+
protected setTarget(
|
|
533
|
+
next:
|
|
534
|
+
| { kind: "to"; turnId: TurnId }
|
|
535
|
+
| { kind: "complete" }
|
|
536
|
+
| { kind: "lifecycleStatus"; status: ProcessTurnTerminalLifecycleStatus }
|
|
537
|
+
| null,
|
|
538
|
+
): this {
|
|
539
|
+
if (this.target && next) {
|
|
540
|
+
throw new Error("Flow route already declares a target");
|
|
541
|
+
}
|
|
542
|
+
this.target = next;
|
|
543
|
+
return this;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
to(turnId: TurnId): this {
|
|
547
|
+
return this.setTarget({ kind: "to", turnId });
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
complete(): this {
|
|
551
|
+
return this.setTarget({ kind: "complete" });
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
lifecycleStatus(status: ProcessTurnTerminalLifecycleStatus): this {
|
|
555
|
+
return this.setTarget({ kind: "lifecycleStatus", status });
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
stay(): this {
|
|
559
|
+
this.target = null;
|
|
560
|
+
return this;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
state(fn: FlowOutcomeStateEffect<TParams, TState, TContext>): this {
|
|
564
|
+
this.flowEffect = async (input) => ({ state: await fn(input) });
|
|
565
|
+
return this;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
effect(fn: FlowOutcomeEffect<TParams, TState, TContext>): this {
|
|
569
|
+
this.flowEffect = fn;
|
|
570
|
+
return this;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
hasRoute(): boolean {
|
|
574
|
+
return this.target !== null;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
protected buildRouteTarget(): {
|
|
578
|
+
to?: TurnId;
|
|
579
|
+
complete?: boolean;
|
|
580
|
+
lifecycleStatus?: ProcessTurnTerminalLifecycleStatus;
|
|
581
|
+
} {
|
|
582
|
+
return buildRouteTargetSpec(this.target);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
export type FlowTargetSpec =
|
|
587
|
+
| { kind: "to"; turnId: TurnId }
|
|
588
|
+
| { kind: "complete" }
|
|
589
|
+
| { kind: "lifecycleStatus"; status: ProcessTurnTerminalLifecycleStatus }
|
|
590
|
+
| null;
|
|
591
|
+
|
|
592
|
+
export function buildRouteTargetSpec(target: FlowTargetSpec): {
|
|
593
|
+
to?: TurnId;
|
|
594
|
+
complete?: boolean;
|
|
595
|
+
lifecycleStatus?: ProcessTurnTerminalLifecycleStatus;
|
|
596
|
+
} {
|
|
597
|
+
if (!target) {
|
|
598
|
+
return {};
|
|
599
|
+
}
|
|
600
|
+
if (target.kind === "to") {
|
|
601
|
+
return { to: target.turnId };
|
|
602
|
+
}
|
|
603
|
+
if (target.kind === "complete") {
|
|
604
|
+
return { complete: true };
|
|
605
|
+
}
|
|
606
|
+
return { lifecycleStatus: target.status };
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
interface ParameterOptions extends Partial<Omit<OutcomeToolParameterSpec, "type" | "description">> {
|
|
610
|
+
description?: string;
|
|
611
|
+
requiredErrorCode?: string;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
interface ArrayParameterOptions extends ParameterOptions {
|
|
615
|
+
items?: OutcomeToolParameterSpec["items"];
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
interface EnumParameterOptions extends ParameterOptions {
|
|
619
|
+
values?: readonly string[];
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
interface MarkdownParameterOptions extends ParameterOptions {
|
|
623
|
+
publish?: true;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function isStringArray(value: unknown): value is readonly string[] {
|
|
627
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
class ParameterizedOutcomeBuilder<TParams, TState, TContext> extends RouteAndEffectBuilder<
|
|
631
|
+
TParams,
|
|
632
|
+
TState,
|
|
633
|
+
TContext
|
|
634
|
+
> {
|
|
635
|
+
protected outcomeDescription: string | null = null;
|
|
636
|
+
protected parameters: Record<string, OutcomeToolParameterSpec> = {};
|
|
637
|
+
protected publishedMarkdownParameter: string | null = null;
|
|
638
|
+
|
|
639
|
+
description(text: string): this {
|
|
640
|
+
this.outcomeDescription = text;
|
|
641
|
+
return this;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
parameter(name: string, spec: OutcomeToolParameterSpec): this {
|
|
645
|
+
if (name.trim() === "") {
|
|
646
|
+
throw new Error("Outcome parameter name must be non-empty");
|
|
647
|
+
}
|
|
648
|
+
this.parameters[name] = {
|
|
649
|
+
...spec,
|
|
650
|
+
description: spec.description ?? "",
|
|
651
|
+
};
|
|
652
|
+
return this;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
private withRequired(
|
|
656
|
+
name: string,
|
|
657
|
+
options: string | ParameterOptions,
|
|
658
|
+
): ParameterOptions & { description?: string } {
|
|
659
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
660
|
+
return {
|
|
661
|
+
...resolved,
|
|
662
|
+
required: true,
|
|
663
|
+
requiredErrorCode: resolved.requiredErrorCode ?? toRequiredErrorCode(name),
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
string(name: string, options: string | ParameterOptions = {}): this {
|
|
668
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
669
|
+
return this.parameter(name, {
|
|
670
|
+
...resolved,
|
|
671
|
+
type: "string",
|
|
672
|
+
description: resolved.description ?? "",
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
markdown(name: string, options: string | MarkdownParameterOptions = {}): this {
|
|
677
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
678
|
+
const { publish, ...parameterOptions } = resolved;
|
|
679
|
+
if (publish) {
|
|
680
|
+
if (this.publishedMarkdownParameter && this.publishedMarkdownParameter !== name) {
|
|
681
|
+
throw new Error("Outcome can publish only one markdown parameter");
|
|
682
|
+
}
|
|
683
|
+
normalizeProductName(name);
|
|
684
|
+
this.publishedMarkdownParameter = name;
|
|
685
|
+
}
|
|
686
|
+
return this.parameter(name, {
|
|
687
|
+
...parameterOptions,
|
|
688
|
+
required: publish ? true : resolved.required,
|
|
689
|
+
requiredErrorCode: resolved.requiredErrorCode ?? (publish ? `${name}_required` : undefined),
|
|
690
|
+
type: "string",
|
|
691
|
+
description: resolved.description ?? "",
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
requiredString(name: string, options: string | ParameterOptions = {}): this {
|
|
696
|
+
const resolved = this.withRequired(name, options);
|
|
697
|
+
return this.parameter(name, {
|
|
698
|
+
...resolved,
|
|
699
|
+
type: "string",
|
|
700
|
+
description: resolved.description ?? "",
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
number(name: string, options: string | ParameterOptions = {}): this {
|
|
705
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
706
|
+
return this.parameter(name, {
|
|
707
|
+
...resolved,
|
|
708
|
+
type: "number",
|
|
709
|
+
description: resolved.description ?? "",
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
requiredNumber(name: string, options: string | ParameterOptions = {}): this {
|
|
714
|
+
const resolved = this.withRequired(name, options);
|
|
715
|
+
return this.parameter(name, {
|
|
716
|
+
...resolved,
|
|
717
|
+
type: "number",
|
|
718
|
+
description: resolved.description ?? "",
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
boolean(name: string, options: string | ParameterOptions = {}): this {
|
|
723
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
724
|
+
return this.parameter(name, {
|
|
725
|
+
...resolved,
|
|
726
|
+
type: "boolean",
|
|
727
|
+
description: resolved.description ?? "",
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
requiredBoolean(name: string, options: string | ParameterOptions = {}): this {
|
|
732
|
+
const resolved = this.withRequired(name, options);
|
|
733
|
+
return this.parameter(name, {
|
|
734
|
+
...resolved,
|
|
735
|
+
type: "boolean",
|
|
736
|
+
description: resolved.description ?? "",
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
stringArray(name: string, options: string | ArrayParameterOptions = {}): this {
|
|
741
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
742
|
+
return this.parameter(name, {
|
|
743
|
+
...resolved,
|
|
744
|
+
type: "array",
|
|
745
|
+
description: resolved.description ?? "",
|
|
746
|
+
items: resolved.items ?? { type: "string" },
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
requiredStringArray(name: string, options: string | ArrayParameterOptions = {}): this {
|
|
751
|
+
const resolved = this.withRequired(name, options);
|
|
752
|
+
return this.parameter(name, {
|
|
753
|
+
...resolved,
|
|
754
|
+
type: "array",
|
|
755
|
+
description: resolved.description ?? "",
|
|
756
|
+
items: (resolved as ArrayParameterOptions).items ?? { type: "string" },
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
enum(name: string, valuesOrOptions: readonly string[] | EnumParameterOptions): this {
|
|
761
|
+
const resolved: EnumParameterOptions = isStringArray(valuesOrOptions)
|
|
762
|
+
? { values: [...valuesOrOptions] }
|
|
763
|
+
: valuesOrOptions;
|
|
764
|
+
const { values = [], ...options } = resolved;
|
|
765
|
+
return this.parameter(name, {
|
|
766
|
+
...options,
|
|
767
|
+
type: "string",
|
|
768
|
+
description: options.description ?? "",
|
|
769
|
+
enum: [...values],
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
object(name: string, options: string | ParameterOptions = {}): this {
|
|
774
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
775
|
+
return this.parameter(name, {
|
|
776
|
+
...resolved,
|
|
777
|
+
type: "object",
|
|
778
|
+
description: resolved.description ?? "",
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
array(name: string, options: string | ArrayParameterOptions = {}): this {
|
|
783
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
784
|
+
return this.parameter(name, {
|
|
785
|
+
...resolved,
|
|
786
|
+
type: "array",
|
|
787
|
+
description: resolved.description ?? "",
|
|
788
|
+
items: resolved.items ?? { type: "string" },
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
export class OutcomeToolBuilder<
|
|
794
|
+
TParams = unknown,
|
|
795
|
+
TState = unknown,
|
|
796
|
+
> extends ParameterizedOutcomeBuilder<
|
|
797
|
+
TParams,
|
|
798
|
+
TState,
|
|
799
|
+
FlowLlmOutcomeEffectContext<TParams, TState>
|
|
800
|
+
> {
|
|
801
|
+
build(): ProcessToolOutcomeSpec<TParams, TState> {
|
|
802
|
+
if (!this.outcomeDescription) {
|
|
803
|
+
throw new Error("LLM outcome tool must declare .description(...)");
|
|
804
|
+
}
|
|
805
|
+
return {
|
|
806
|
+
description: this.outcomeDescription,
|
|
807
|
+
parameters: this.parameters,
|
|
808
|
+
...(this.publishedMarkdownParameter
|
|
809
|
+
? {
|
|
810
|
+
publishedProduct: this.publishedMarkdownParameter,
|
|
811
|
+
turnResultMarkdownParameter: this.publishedMarkdownParameter,
|
|
812
|
+
}
|
|
813
|
+
: {}),
|
|
814
|
+
...this.buildRouteTarget(),
|
|
815
|
+
...(this.flowEffect ? { effect: wrapLlmOutcomeEffect(this.flowEffect) } : {}),
|
|
816
|
+
};
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
export class AutomaticOutcomeBuilder<
|
|
821
|
+
TParams = unknown,
|
|
822
|
+
TState = unknown,
|
|
823
|
+
> extends ParameterizedOutcomeBuilder<
|
|
824
|
+
TParams,
|
|
825
|
+
TState,
|
|
826
|
+
FlowAutomaticOutcomeEffectContext<TParams, TState>
|
|
827
|
+
> {
|
|
828
|
+
build(): ProcessToolOutcomeSpec<TParams, TState> {
|
|
829
|
+
if (!this.outcomeDescription) {
|
|
830
|
+
throw new Error("Automatic outcome must declare .description(...)");
|
|
831
|
+
}
|
|
832
|
+
return {
|
|
833
|
+
description: this.outcomeDescription,
|
|
834
|
+
parameters: this.parameters,
|
|
835
|
+
...(this.publishedMarkdownParameter
|
|
836
|
+
? {
|
|
837
|
+
publishedProduct: this.publishedMarkdownParameter,
|
|
838
|
+
turnResultMarkdownParameter: this.publishedMarkdownParameter,
|
|
839
|
+
}
|
|
840
|
+
: {}),
|
|
841
|
+
...this.buildRouteTarget(),
|
|
842
|
+
...(this.flowEffect ? { effect: wrapAutomaticOutcomeEffect(this.flowEffect) } : {}),
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
export class PlanResultBuilder<TParams = unknown, TState = unknown> extends RouteAndEffectBuilder<
|
|
848
|
+
TParams,
|
|
849
|
+
TState,
|
|
850
|
+
FlowLlmOutcomeEffectContext<TParams, TState>
|
|
851
|
+
> {
|
|
852
|
+
private resultDescription = "The candidate plan is ready for operator review";
|
|
853
|
+
private summarySpec: OutcomeToolParameterSpec | null = null;
|
|
854
|
+
private acceptanceCriteriaSpec: OutcomeToolParameterSpec | null = null;
|
|
855
|
+
private reviewTurnId: TurnId | null = null;
|
|
856
|
+
|
|
857
|
+
description(text: string): this {
|
|
858
|
+
this.resultDescription = text;
|
|
859
|
+
return this;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
summary(options: string | PlanFieldOptions = {}): this {
|
|
863
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
864
|
+
this.summarySpec = textField({
|
|
865
|
+
description: resolved.description ?? "Short summary of the proposed plan",
|
|
866
|
+
requiredErrorCode: resolved.requiredErrorCode ?? "summary_required",
|
|
867
|
+
});
|
|
868
|
+
return this;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
acceptanceCriteria(options: string | PlanAcceptanceCriteriaOptions = {}): this {
|
|
872
|
+
const resolved = typeof options === "string" ? { description: options } : options;
|
|
873
|
+
this.acceptanceCriteriaSpec = stringArrayField({
|
|
874
|
+
description: resolved.description ?? "Acceptance criteria for the requested change",
|
|
875
|
+
requiredErrorCode: resolved.requiredErrorCode ?? "acceptance_criteria_required",
|
|
876
|
+
minItems: resolved.minItems ?? 1,
|
|
877
|
+
minItemsErrorCode: resolved.minItemsErrorCode ?? "acceptance_criteria_required",
|
|
878
|
+
});
|
|
879
|
+
return this;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
review(turnId: TurnId): this {
|
|
883
|
+
this.reviewTurnId = turnId;
|
|
884
|
+
return this.setTarget({ kind: "to", turnId });
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
buildOutcome(): ProcessToolOutcomeSpec<TParams, TState> {
|
|
888
|
+
if (!this.summarySpec) {
|
|
889
|
+
throw new Error("plan result must declare .summary(...)");
|
|
890
|
+
}
|
|
891
|
+
if (!this.acceptanceCriteriaSpec) {
|
|
892
|
+
throw new Error("plan result must declare .acceptanceCriteria(...)");
|
|
893
|
+
}
|
|
894
|
+
if (!this.reviewTurnId) {
|
|
895
|
+
throw new Error("plan result must declare .review(turnId)");
|
|
896
|
+
}
|
|
897
|
+
const stateEffect = this.flowEffect ? wrapLlmOutcomeEffect(this.flowEffect) : undefined;
|
|
898
|
+
return {
|
|
899
|
+
description: this.resultDescription,
|
|
900
|
+
parameters: {
|
|
901
|
+
summary: this.summarySpec,
|
|
902
|
+
acceptanceCriteria: this.acceptanceCriteriaSpec,
|
|
903
|
+
},
|
|
904
|
+
to: this.reviewTurnId,
|
|
905
|
+
lifecycleIntent: {
|
|
906
|
+
kind: "save_plan_result",
|
|
907
|
+
summaryParam: "summary",
|
|
908
|
+
acceptanceCriteriaParam: "acceptanceCriteria",
|
|
909
|
+
planMarkdownParam: "planMarkdown",
|
|
910
|
+
emitEventType: DEFAULT_PLAN_RESULT_OUTCOME_ID,
|
|
911
|
+
broadcastType: "plan.updated",
|
|
912
|
+
...(stateEffect ? { state: stateEffect } : {}),
|
|
913
|
+
},
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
export class PublishedResultBuilder<TParams = unknown, TState = unknown>
|
|
919
|
+
extends RouteAndEffectBuilder<TParams, TState, FlowLlmOutcomeEffectContext<TParams, TState>>
|
|
920
|
+
implements FlowLlmTurn<TParams, TState, string>
|
|
921
|
+
{
|
|
922
|
+
constructor(
|
|
923
|
+
private readonly productName: string,
|
|
924
|
+
private readonly parent?: FlowLlmTurn<TParams, TState, string>,
|
|
925
|
+
) {
|
|
926
|
+
super();
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
get id(): TurnId {
|
|
930
|
+
if (!this.parent) {
|
|
931
|
+
throw new Error("Published result builder is not attached to a turn");
|
|
932
|
+
}
|
|
933
|
+
return this.parent.id;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
get definition(): LlmTurnDefinition<string, TParams, TState> {
|
|
937
|
+
if (!this.parent) {
|
|
938
|
+
throw new Error("Published result builder is not attached to a turn");
|
|
939
|
+
}
|
|
940
|
+
return this.parent.definition;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
buildTurnEnd(): ProcessTurnEndSpec<TParams, TState, string> {
|
|
944
|
+
const effect = this.flowEffect ? wrapLlmOutcomeEffect(this.flowEffect) : undefined;
|
|
945
|
+
return {
|
|
946
|
+
outcome: this.productName,
|
|
947
|
+
...this.buildRouteTarget(),
|
|
948
|
+
...(effect ? { effect } : {}),
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
export class LlmTurnEndBuilder<TParams = unknown, TState = unknown>
|
|
954
|
+
extends RouteAndEffectBuilder<TParams, TState, FlowLlmOutcomeEffectContext<TParams, TState>>
|
|
955
|
+
implements FlowLlmTurn<TParams, TState, string>
|
|
956
|
+
{
|
|
957
|
+
constructor(
|
|
958
|
+
private readonly outcomeId: string,
|
|
959
|
+
private readonly parent?: FlowLlmTurn<TParams, TState, string>,
|
|
960
|
+
) {
|
|
961
|
+
super();
|
|
962
|
+
if (outcomeId.trim() === "") {
|
|
963
|
+
throw new Error("LLM turn end outcome must be non-empty");
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
get id(): TurnId {
|
|
968
|
+
if (!this.parent) {
|
|
969
|
+
throw new Error("LLM turn end builder is not attached to a turn");
|
|
970
|
+
}
|
|
971
|
+
return this.parent.id;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
get definition(): LlmTurnDefinition<string, TParams, TState> {
|
|
975
|
+
if (!this.parent) {
|
|
976
|
+
throw new Error("LLM turn end builder is not attached to a turn");
|
|
977
|
+
}
|
|
978
|
+
return this.parent.definition;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
build(): ProcessTurnEndSpec<TParams, TState, string> {
|
|
982
|
+
const effect = this.flowEffect ? wrapLlmOutcomeEffect(this.flowEffect) : undefined;
|
|
983
|
+
return {
|
|
984
|
+
outcome: this.outcomeId,
|
|
985
|
+
...this.buildRouteTarget(),
|
|
986
|
+
...(effect ? { effect } : {}),
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
type LlmPromptBuilder<TParams, TState, TConsumedProducts extends string> = (
|
|
992
|
+
ctx: FlowPromptContext<TParams, TState, TConsumedProducts>,
|
|
993
|
+
) => MaybePromise<string>;
|
|
994
|
+
|
|
995
|
+
export class LlmFlowBuilder<
|
|
996
|
+
TParams = unknown,
|
|
997
|
+
TState = unknown,
|
|
998
|
+
TConsumedProducts extends string = never,
|
|
999
|
+
> implements FlowLlmTurn<TParams, TState, string>
|
|
1000
|
+
{
|
|
1001
|
+
private readonly turnId: TurnId;
|
|
1002
|
+
private turnDescription: string | null = null;
|
|
1003
|
+
private modelPurposeValue: LlmModelPurpose | undefined;
|
|
1004
|
+
private availableTools: readonly PiBuiltInToolName[] = [];
|
|
1005
|
+
private promptBuilder:
|
|
1006
|
+
| ((ctx: ProcessRuntimeTurnContext<TParams, TState>) => MaybePromise<string>)
|
|
1007
|
+
| null = null;
|
|
1008
|
+
private branchType: LlmTurnDefinition<string, TParams, TState>["branchType"] = "primary";
|
|
1009
|
+
private context: LlmTurnDefinition<string, TParams, TState>["context"] = "fresh";
|
|
1010
|
+
private completionMode: LlmTurnDefinition<string, TParams, TState>["completionMode"] = "turn_end";
|
|
1011
|
+
private questionsEnabled = false;
|
|
1012
|
+
private startFrom: LlmTurnDefinition<string, TParams, TState>["startFrom"] | undefined;
|
|
1013
|
+
private restorePrimaryLeafAfterTurn: boolean | undefined;
|
|
1014
|
+
private reviewSubject: ReviewSubject | undefined;
|
|
1015
|
+
private consumedProductNames: string[] = [];
|
|
1016
|
+
private optionalConsumedProductNames: string[] = [];
|
|
1017
|
+
private publishedResult: {
|
|
1018
|
+
productName: string;
|
|
1019
|
+
builder: PublishedResultBuilder<TParams, TState>;
|
|
1020
|
+
} | null = null;
|
|
1021
|
+
private endResult: LlmTurnEndBuilder<TParams, TState> | null = null;
|
|
1022
|
+
private outcomeToolBuilders = new Map<string, OutcomeToolBuilder<TParams, TState>>();
|
|
1023
|
+
|
|
1024
|
+
constructor(turnId: TurnId) {
|
|
1025
|
+
this.turnId = turnId;
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
get id(): TurnId {
|
|
1029
|
+
return this.turnId;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
get definition(): LlmTurnDefinition<string, TParams, TState> {
|
|
1033
|
+
return this.buildDefinition();
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
description(description: string): this {
|
|
1037
|
+
this.turnDescription = description;
|
|
1038
|
+
return this;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
modelPurpose(purpose: LlmModelPurpose): this {
|
|
1042
|
+
this.modelPurposeValue = purpose;
|
|
1043
|
+
return this;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
tools(...tools: readonly PiBuiltInToolName[]): this {
|
|
1047
|
+
this.availableTools = tools;
|
|
1048
|
+
return this;
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
/** Enable durable operator questions for this LLM turn. */
|
|
1052
|
+
askQuestions(): this {
|
|
1053
|
+
this.questionsEnabled = true;
|
|
1054
|
+
return this;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
freshPrimary(): this {
|
|
1058
|
+
this.branchType = "primary";
|
|
1059
|
+
this.context = "fresh";
|
|
1060
|
+
this.completionMode = "turn_end";
|
|
1061
|
+
this.startFrom = undefined;
|
|
1062
|
+
this.restorePrimaryLeafAfterTurn = undefined;
|
|
1063
|
+
return this;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
freshSeededPrimary(): this {
|
|
1067
|
+
this.branchType = "primary";
|
|
1068
|
+
this.context = "fresh_seeded";
|
|
1069
|
+
this.completionMode = "turn_end";
|
|
1070
|
+
this.startFrom = undefined;
|
|
1071
|
+
this.restorePrimaryLeafAfterTurn = undefined;
|
|
1072
|
+
return this;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
fullPrimary(): this {
|
|
1076
|
+
this.branchType = "primary";
|
|
1077
|
+
this.context = "full";
|
|
1078
|
+
this.completionMode = "turn_end";
|
|
1079
|
+
return this;
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
rootBranchReview(): this {
|
|
1083
|
+
this.branchType = "root_branch";
|
|
1084
|
+
this.context = "full";
|
|
1085
|
+
this.completionMode = "turn_end";
|
|
1086
|
+
this.restorePrimaryLeafAfterTurn = true;
|
|
1087
|
+
return this;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
continueFromPrimaryLeaf(): this {
|
|
1091
|
+
this.startFrom = {
|
|
1092
|
+
kind: "semantic_ref",
|
|
1093
|
+
ref: "currentPrimaryPathLeaf",
|
|
1094
|
+
fallback: { kind: "current_leaf" },
|
|
1095
|
+
};
|
|
1096
|
+
return this;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
continueFromReviewBranch(): this {
|
|
1100
|
+
this.startFrom = {
|
|
1101
|
+
kind: "semantic_ref",
|
|
1102
|
+
ref: "review",
|
|
1103
|
+
fallback: { kind: "current_leaf" },
|
|
1104
|
+
};
|
|
1105
|
+
return this;
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
continueFromProductBranch(
|
|
1109
|
+
productName: string,
|
|
1110
|
+
fallback: ProcessTurnStartSelection = { kind: "current_leaf" },
|
|
1111
|
+
): this {
|
|
1112
|
+
this.startFrom = {
|
|
1113
|
+
kind: "product_ref",
|
|
1114
|
+
productName: normalizeProductName(productName),
|
|
1115
|
+
fallback,
|
|
1116
|
+
};
|
|
1117
|
+
return this;
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
startFromReviewBranch(): this {
|
|
1121
|
+
this.startFrom = {
|
|
1122
|
+
kind: "semantic_ref",
|
|
1123
|
+
ref: "review",
|
|
1124
|
+
fallback: { kind: "session_root" },
|
|
1125
|
+
};
|
|
1126
|
+
return this;
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
startFromProductBranch(
|
|
1130
|
+
productName: string,
|
|
1131
|
+
fallback: ProcessTurnStartSelection = { kind: "session_root" },
|
|
1132
|
+
): this {
|
|
1133
|
+
this.startFrom = {
|
|
1134
|
+
kind: "product_ref",
|
|
1135
|
+
productName: normalizeProductName(productName),
|
|
1136
|
+
fallback,
|
|
1137
|
+
};
|
|
1138
|
+
return this;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
startFromRoot(): this {
|
|
1142
|
+
this.startFrom = { kind: "session_root" };
|
|
1143
|
+
return this;
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
reviews(subject: ReviewSubject): this {
|
|
1147
|
+
this.reviewSubject = subject;
|
|
1148
|
+
return this;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
consume<TProductName extends string>(
|
|
1152
|
+
productName: TProductName,
|
|
1153
|
+
): LlmFlowBuilder<TParams, TState, TConsumedProducts | TProductName> {
|
|
1154
|
+
const normalized = normalizeProductName(productName);
|
|
1155
|
+
if (!this.consumedProductNames.includes(normalized)) {
|
|
1156
|
+
this.consumedProductNames.push(normalized);
|
|
1157
|
+
}
|
|
1158
|
+
return this as unknown as LlmFlowBuilder<TParams, TState, TConsumedProducts | TProductName>;
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
optionalConsume<TProductName extends string>(
|
|
1162
|
+
productName: TProductName,
|
|
1163
|
+
): LlmFlowBuilder<TParams, TState, TConsumedProducts | TProductName> {
|
|
1164
|
+
const normalized = normalizeProductName(productName);
|
|
1165
|
+
if (!this.optionalConsumedProductNames.includes(normalized)) {
|
|
1166
|
+
this.optionalConsumedProductNames.push(normalized);
|
|
1167
|
+
}
|
|
1168
|
+
return this as unknown as LlmFlowBuilder<TParams, TState, TConsumedProducts | TProductName>;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
publish(productName: string): PublishedResultBuilder<TParams, TState> {
|
|
1172
|
+
const normalized = normalizeProductName(productName);
|
|
1173
|
+
if (this.publishedResult) {
|
|
1174
|
+
throw new Error(`LLM turn '${this.turnId}' can publish only one product in flow v1`);
|
|
1175
|
+
}
|
|
1176
|
+
if (this.endResult) {
|
|
1177
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare both .publish(...) and .end(...)`);
|
|
1178
|
+
}
|
|
1179
|
+
const builder = new PublishedResultBuilder<TParams, TState>(normalized, this);
|
|
1180
|
+
this.publishedResult = { productName: normalized, builder };
|
|
1181
|
+
return builder;
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
end(outcomeId: string): LlmTurnEndBuilder<TParams, TState> {
|
|
1185
|
+
if (this.endResult) {
|
|
1186
|
+
throw new Error(`LLM turn '${this.turnId}' declares duplicate .end(...) completion`);
|
|
1187
|
+
}
|
|
1188
|
+
if (this.publishedResult) {
|
|
1189
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare both .publish(...) and .end(...)`);
|
|
1190
|
+
}
|
|
1191
|
+
if (this.outcomeToolBuilders.size > 0) {
|
|
1192
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare both .end(...) and outcome tools`);
|
|
1193
|
+
}
|
|
1194
|
+
this.endResult = new LlmTurnEndBuilder<TParams, TState>(outcomeId, this);
|
|
1195
|
+
return this.endResult;
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
buildPrompt(fn: LlmPromptBuilder<TParams, TState, TConsumedProducts>): this {
|
|
1199
|
+
this.promptBuilder = (ctx) =>
|
|
1200
|
+
fn(
|
|
1201
|
+
createFlowPromptContext(
|
|
1202
|
+
ctx,
|
|
1203
|
+
this.consumedProductNames as TConsumedProducts[],
|
|
1204
|
+
this.optionalConsumedProductNames,
|
|
1205
|
+
),
|
|
1206
|
+
);
|
|
1207
|
+
return this;
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
prompt(prompt: (ctx: ProcessRuntimeTurnContext<TParams, TState>) => MaybePromise<string>): this {
|
|
1211
|
+
this.promptBuilder = prompt;
|
|
1212
|
+
return this;
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
outcomeTool(
|
|
1216
|
+
id: string,
|
|
1217
|
+
configure: (
|
|
1218
|
+
tool: OutcomeToolBuilder<TParams, TState>,
|
|
1219
|
+
) => OutcomeToolBuilder<TParams, TState> | undefined,
|
|
1220
|
+
): this {
|
|
1221
|
+
if (id.trim() === "") {
|
|
1222
|
+
throw new Error(`LLM turn '${this.turnId}' declares an empty outcome tool id`);
|
|
1223
|
+
}
|
|
1224
|
+
if (this.endResult) {
|
|
1225
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare both .end(...) and outcome tools`);
|
|
1226
|
+
}
|
|
1227
|
+
if (this.publishedResult) {
|
|
1228
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare outcome tools after .publish(...)`);
|
|
1229
|
+
}
|
|
1230
|
+
if (this.outcomeToolBuilders.has(id)) {
|
|
1231
|
+
throw new Error(`LLM turn '${this.turnId}' declares duplicate outcome tool '${id}'`);
|
|
1232
|
+
}
|
|
1233
|
+
const builder = new OutcomeToolBuilder<TParams, TState>();
|
|
1234
|
+
configure(builder);
|
|
1235
|
+
this.outcomeToolBuilders.set(id, builder);
|
|
1236
|
+
return this;
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
private buildBaseDefinition() {
|
|
1240
|
+
if (!this.turnDescription) {
|
|
1241
|
+
throw new Error(`LLM turn '${this.turnId}' must declare .description(...)`);
|
|
1242
|
+
}
|
|
1243
|
+
const prompt = this.promptBuilder;
|
|
1244
|
+
if (!prompt) {
|
|
1245
|
+
throw new Error(`LLM turn '${this.turnId}' must declare .buildPrompt(...)`);
|
|
1246
|
+
}
|
|
1247
|
+
return {
|
|
1248
|
+
kind: "llm" as const,
|
|
1249
|
+
description: this.turnDescription,
|
|
1250
|
+
...(this.modelPurposeValue ? { modelPurpose: this.modelPurposeValue } : {}),
|
|
1251
|
+
availableTools: this.availableTools,
|
|
1252
|
+
...(this.questionsEnabled ? { askQuestions: true as const } : {}),
|
|
1253
|
+
completionMode: this.completionMode,
|
|
1254
|
+
branchType: this.branchType,
|
|
1255
|
+
context: this.context,
|
|
1256
|
+
...(this.startFrom ? { startFrom: this.startFrom } : {}),
|
|
1257
|
+
...(this.restorePrimaryLeafAfterTurn !== undefined
|
|
1258
|
+
? { restorePrimaryLeafAfterTurn: this.restorePrimaryLeafAfterTurn }
|
|
1259
|
+
: {}),
|
|
1260
|
+
prompt,
|
|
1261
|
+
};
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
producesPlan(
|
|
1265
|
+
configure: (plan: PlanResultBuilder<TParams, TState>) => PlanResultBuilder<TParams, TState>,
|
|
1266
|
+
): FlowLlmTurn<TParams, TState, typeof DEFAULT_PLAN_RESULT_OUTCOME_ID> {
|
|
1267
|
+
const definition = this.buildBaseDefinition();
|
|
1268
|
+
const plan = configure(new PlanResultBuilder<TParams, TState>());
|
|
1269
|
+
const outcome = plan.buildOutcome();
|
|
1270
|
+
return {
|
|
1271
|
+
id: this.turnId,
|
|
1272
|
+
definition: {
|
|
1273
|
+
...definition,
|
|
1274
|
+
outcomes: {
|
|
1275
|
+
[DEFAULT_PLAN_RESULT_OUTCOME_ID]: outcome,
|
|
1276
|
+
},
|
|
1277
|
+
turnResultMarkdown: OUTCOME_TOOL_ARGUMENT_TURN_RESULT,
|
|
1278
|
+
resultSemanticRef: "plan",
|
|
1279
|
+
publishedProduct: "plan",
|
|
1280
|
+
},
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
private buildDefinition(): LlmTurnDefinition<string, TParams, TState> {
|
|
1285
|
+
const definition = this.buildBaseDefinition();
|
|
1286
|
+
const hasOutcomeTools = this.outcomeToolBuilders.size > 0;
|
|
1287
|
+
const explicitTurnEnd = this.endResult;
|
|
1288
|
+
const published = this.publishedResult;
|
|
1289
|
+
const outcomes: Record<string, ProcessToolOutcomeSpec<TParams, TState>> = {};
|
|
1290
|
+
for (const [outcomeId, builder] of this.outcomeToolBuilders) {
|
|
1291
|
+
outcomes[outcomeId] = builder.build();
|
|
1292
|
+
}
|
|
1293
|
+
const hasOutcomePublishedProduct = Object.values(outcomes).some(
|
|
1294
|
+
(outcome) => outcome.publishedProduct,
|
|
1295
|
+
);
|
|
1296
|
+
let turnEnd: ProcessTurnEndSpec<TParams, TState, string> | undefined;
|
|
1297
|
+
let resultSemanticRef: "plan" | "review" | undefined = this.reviewSubject
|
|
1298
|
+
? "review"
|
|
1299
|
+
: undefined;
|
|
1300
|
+
let turnResultMarkdown: LlmTurnDefinition<string, TParams, TState>["turnResultMarkdown"];
|
|
1301
|
+
let publishedProduct: string | undefined;
|
|
1302
|
+
|
|
1303
|
+
if (published) {
|
|
1304
|
+
publishedProduct = published.productName;
|
|
1305
|
+
if (published.productName === "plan") {
|
|
1306
|
+
resultSemanticRef = "plan";
|
|
1307
|
+
}
|
|
1308
|
+
if (published.productName === "review") {
|
|
1309
|
+
resultSemanticRef = "review";
|
|
1310
|
+
}
|
|
1311
|
+
const publishHasRoute = published.builder.hasRoute();
|
|
1312
|
+
if (publishHasRoute && hasOutcomeTools) {
|
|
1313
|
+
throw new Error(
|
|
1314
|
+
`LLM turn '${this.turnId}' cannot route published product '${published.productName}' and declare outcome tools in flow v1`,
|
|
1315
|
+
);
|
|
1316
|
+
}
|
|
1317
|
+
if (publishHasRoute && !hasOutcomeTools) {
|
|
1318
|
+
turnResultMarkdown = ASSISTANT_OUTPUT_TURN_RESULT;
|
|
1319
|
+
turnEnd = published.builder.buildTurnEnd();
|
|
1320
|
+
}
|
|
1321
|
+
if (!publishHasRoute && hasOutcomeTools) {
|
|
1322
|
+
turnResultMarkdown = OUTCOME_TOOL_ARGUMENT_TURN_RESULT;
|
|
1323
|
+
}
|
|
1324
|
+
if (!publishHasRoute && !hasOutcomeTools) {
|
|
1325
|
+
throw new Error(
|
|
1326
|
+
`LLM turn '${this.turnId}' publishes product '${published.productName}' but declares no route or outcome tool completion path`,
|
|
1327
|
+
);
|
|
1328
|
+
}
|
|
1329
|
+
} else if (hasOutcomeTools && !hasOutcomePublishedProduct) {
|
|
1330
|
+
turnResultMarkdown = OUTCOME_TOOL_ARGUMENT_TURN_RESULT;
|
|
1331
|
+
} else if (explicitTurnEnd) {
|
|
1332
|
+
turnResultMarkdown = ASSISTANT_OUTPUT_TURN_RESULT;
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
if (hasOutcomeTools) {
|
|
1336
|
+
for (const [outcomeId, outcome] of Object.entries(outcomes)) {
|
|
1337
|
+
if (OUTCOME_TOOL_MARKDOWN_PARAMETER_NAME in outcome.parameters) {
|
|
1338
|
+
throw new Error(
|
|
1339
|
+
`LLM turn '${this.turnId}' outcome tool '${outcomeId}' cannot declare reserved markdown parameter '${OUTCOME_TOOL_MARKDOWN_PARAMETER_NAME}'`,
|
|
1340
|
+
);
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
if (explicitTurnEnd) {
|
|
1346
|
+
if (turnEnd || Object.keys(outcomes).length > 0) {
|
|
1347
|
+
throw new Error(`LLM turn '${this.turnId}' cannot declare multiple completion paths`);
|
|
1348
|
+
}
|
|
1349
|
+
turnEnd = explicitTurnEnd.build();
|
|
1350
|
+
}
|
|
1351
|
+
if (Object.keys(outcomes).length === 0 && !turnEnd) {
|
|
1352
|
+
throw new Error(`LLM turn '${this.turnId}' must declare a completion path`);
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
return {
|
|
1356
|
+
...definition,
|
|
1357
|
+
...(this.reviewSubject ? { reviewSubject: this.reviewSubject } : {}),
|
|
1358
|
+
...(Object.keys(outcomes).length > 0 ? { outcomes } : {}),
|
|
1359
|
+
...(turnEnd ? { turnEnd } : {}),
|
|
1360
|
+
...(turnResultMarkdown ? { turnResultMarkdown } : {}),
|
|
1361
|
+
...(resultSemanticRef ? { resultSemanticRef } : {}),
|
|
1362
|
+
...(publishedProduct ? { publishedProduct } : {}),
|
|
1363
|
+
...(this.consumedProductNames.length > 0
|
|
1364
|
+
? { consumedProducts: [...this.consumedProductNames] }
|
|
1365
|
+
: {}),
|
|
1366
|
+
...(this.optionalConsumedProductNames.length > 0
|
|
1367
|
+
? { optionalConsumedProducts: [...this.optionalConsumedProductNames] }
|
|
1368
|
+
: {}),
|
|
1369
|
+
};
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
export class AutomaticFlowBuilder<TParams = unknown, TState = unknown>
|
|
1374
|
+
implements FlowAutomaticTurn<TParams, TState, string>
|
|
1375
|
+
{
|
|
1376
|
+
private readonly turnId: TurnId;
|
|
1377
|
+
private turnDescription: string | null = null;
|
|
1378
|
+
private runFn:
|
|
1379
|
+
| ((ctx: FlowAutomaticRunContext<TParams, TState>) => MaybePromise<WorkerCompleteInput<string>>)
|
|
1380
|
+
| null = null;
|
|
1381
|
+
private outcomeBuilders = new Map<string, AutomaticOutcomeBuilder<TParams, TState>>();
|
|
1382
|
+
|
|
1383
|
+
constructor(turnId: TurnId) {
|
|
1384
|
+
this.turnId = turnId;
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
get id(): TurnId {
|
|
1388
|
+
return this.turnId;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
get definition(): AutomaticTurnDefinition<string, TParams, TState> {
|
|
1392
|
+
return this.buildDefinition();
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
description(description: string): this {
|
|
1396
|
+
this.turnDescription = description;
|
|
1397
|
+
return this;
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
run(
|
|
1401
|
+
fn: (
|
|
1402
|
+
ctx: FlowAutomaticRunContext<TParams, TState>,
|
|
1403
|
+
) => MaybePromise<WorkerCompleteInput<string>>,
|
|
1404
|
+
): this {
|
|
1405
|
+
this.runFn = fn;
|
|
1406
|
+
return this;
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
outcome(
|
|
1410
|
+
id: string,
|
|
1411
|
+
configure: (
|
|
1412
|
+
outcome: AutomaticOutcomeBuilder<TParams, TState>,
|
|
1413
|
+
) => AutomaticOutcomeBuilder<TParams, TState> | undefined,
|
|
1414
|
+
): this {
|
|
1415
|
+
if (id.trim() === "") {
|
|
1416
|
+
throw new Error(`Automatic turn '${this.turnId}' declares an empty outcome id`);
|
|
1417
|
+
}
|
|
1418
|
+
if (this.outcomeBuilders.has(id)) {
|
|
1419
|
+
throw new Error(`Automatic turn '${this.turnId}' declares duplicate outcome '${id}'`);
|
|
1420
|
+
}
|
|
1421
|
+
const builder = new AutomaticOutcomeBuilder<TParams, TState>();
|
|
1422
|
+
configure(builder);
|
|
1423
|
+
this.outcomeBuilders.set(id, builder);
|
|
1424
|
+
return this;
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
private buildDefinition(): AutomaticTurnDefinition<string, TParams, TState> {
|
|
1428
|
+
if (!this.turnDescription) {
|
|
1429
|
+
throw new Error(`Automatic turn '${this.turnId}' must declare .description(...)`);
|
|
1430
|
+
}
|
|
1431
|
+
if (!this.runFn) {
|
|
1432
|
+
throw new Error(`Automatic turn '${this.turnId}' must declare .run(...)`);
|
|
1433
|
+
}
|
|
1434
|
+
if (this.outcomeBuilders.size === 0) {
|
|
1435
|
+
throw new Error(`Automatic turn '${this.turnId}' must declare at least one outcome`);
|
|
1436
|
+
}
|
|
1437
|
+
const outcomes: Record<string, ProcessToolOutcomeSpec<TParams, TState>> = {};
|
|
1438
|
+
for (const [outcomeId, builder] of this.outcomeBuilders) {
|
|
1439
|
+
outcomes[outcomeId] = builder.build();
|
|
1440
|
+
}
|
|
1441
|
+
const runFn = this.runFn;
|
|
1442
|
+
return {
|
|
1443
|
+
kind: "automatic",
|
|
1444
|
+
description: this.turnDescription,
|
|
1445
|
+
outcomes,
|
|
1446
|
+
run: (ctx) => runFn(createFlowAutomaticRunContext(ctx)),
|
|
1447
|
+
};
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
export class ServerAutomaticFlowBuilder<TParams = unknown, TState = unknown>
|
|
1452
|
+
implements FlowServerAutomaticTurn<TParams, TState, string>
|
|
1453
|
+
{
|
|
1454
|
+
private readonly turnId: TurnId;
|
|
1455
|
+
private turnDescription: string | null = null;
|
|
1456
|
+
private restartBehaviorValue: ServerAutomaticTurnRestartBehavior | undefined;
|
|
1457
|
+
private runFn:
|
|
1458
|
+
| ((ctx: FlowAutomaticRunContext<TParams, TState>) => MaybePromise<WorkerCompleteInput<string>>)
|
|
1459
|
+
| null = null;
|
|
1460
|
+
private outcomeBuilders = new Map<string, AutomaticOutcomeBuilder<TParams, TState>>();
|
|
1461
|
+
|
|
1462
|
+
constructor(turnId: TurnId) {
|
|
1463
|
+
this.turnId = turnId;
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
get id(): TurnId {
|
|
1467
|
+
return this.turnId;
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
get definition(): ServerAutomaticTurnDefinition<string, TParams, TState> {
|
|
1471
|
+
return this.buildDefinition();
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
description(description: string): this {
|
|
1475
|
+
this.turnDescription = description;
|
|
1476
|
+
return this;
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
run(
|
|
1480
|
+
fn: (
|
|
1481
|
+
ctx: FlowAutomaticRunContext<TParams, TState>,
|
|
1482
|
+
) => MaybePromise<WorkerCompleteInput<string> & { state?: TState }>,
|
|
1483
|
+
): this {
|
|
1484
|
+
this.runFn = fn;
|
|
1485
|
+
return this;
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
restartBehavior(behavior: ServerAutomaticTurnRestartBehavior): this {
|
|
1489
|
+
this.restartBehaviorValue = behavior;
|
|
1490
|
+
return this;
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
outcome(
|
|
1494
|
+
id: string,
|
|
1495
|
+
configure: (
|
|
1496
|
+
outcome: AutomaticOutcomeBuilder<TParams, TState>,
|
|
1497
|
+
) => AutomaticOutcomeBuilder<TParams, TState> | undefined,
|
|
1498
|
+
): this {
|
|
1499
|
+
if (id.trim() === "") {
|
|
1500
|
+
throw new Error(`Server automatic turn '${this.turnId}' declares an empty outcome id`);
|
|
1501
|
+
}
|
|
1502
|
+
if (this.outcomeBuilders.has(id)) {
|
|
1503
|
+
throw new Error(`Server automatic turn '${this.turnId}' declares duplicate outcome '${id}'`);
|
|
1504
|
+
}
|
|
1505
|
+
const builder = new AutomaticOutcomeBuilder<TParams, TState>();
|
|
1506
|
+
configure(builder);
|
|
1507
|
+
this.outcomeBuilders.set(id, builder);
|
|
1508
|
+
return this;
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
private buildDefinition(): ServerAutomaticTurnDefinition<string, TParams, TState> {
|
|
1512
|
+
if (!this.turnDescription) {
|
|
1513
|
+
throw new Error(`Server automatic turn '${this.turnId}' must declare .description(...)`);
|
|
1514
|
+
}
|
|
1515
|
+
if (!this.runFn) {
|
|
1516
|
+
throw new Error(`Server automatic turn '${this.turnId}' must declare .run(...)`);
|
|
1517
|
+
}
|
|
1518
|
+
if (this.outcomeBuilders.size === 0) {
|
|
1519
|
+
throw new Error(`Server automatic turn '${this.turnId}' must declare at least one outcome`);
|
|
1520
|
+
}
|
|
1521
|
+
const outcomes: Record<string, ProcessToolOutcomeSpec<TParams, TState>> = {};
|
|
1522
|
+
for (const [outcomeId, builder] of this.outcomeBuilders) {
|
|
1523
|
+
outcomes[outcomeId] = builder.build();
|
|
1524
|
+
}
|
|
1525
|
+
const runFn = this.runFn;
|
|
1526
|
+
return {
|
|
1527
|
+
kind: "server_automatic",
|
|
1528
|
+
description: this.turnDescription,
|
|
1529
|
+
outcomes,
|
|
1530
|
+
...(this.restartBehaviorValue ? { restartBehavior: this.restartBehaviorValue } : {}),
|
|
1531
|
+
run: (ctx) =>
|
|
1532
|
+
runFn(createFlowAutomaticRunContext(ctx as ProcessRuntimeTurnContext<TParams, TState>)),
|
|
1533
|
+
};
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
function formFieldPublishedProductName(field: FormDefinition["fields"][number]): string | null {
|
|
1538
|
+
if (!field.publish) {
|
|
1539
|
+
return null;
|
|
1540
|
+
}
|
|
1541
|
+
return normalizeProductName(
|
|
1542
|
+
typeof field.publish === "object" && field.publish.product ? field.publish.product : field.id,
|
|
1543
|
+
);
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
function inferActionLabel(actionId: string): string {
|
|
1547
|
+
return humanizeProcessLabel(actionId);
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
function inferAcceptanceState(actionId: string): ProcessHumanTurnActionSpec["acceptanceState"] {
|
|
1551
|
+
const normalized = actionId.toLowerCase();
|
|
1552
|
+
if (/approve|accept|complete|merged/.test(normalized)) {
|
|
1553
|
+
return "accepted";
|
|
1554
|
+
}
|
|
1555
|
+
if (/request|revision|feedback|change|reject/.test(normalized)) {
|
|
1556
|
+
return "requires_changes";
|
|
1557
|
+
}
|
|
1558
|
+
return "neutral";
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
export class HumanActionBuilder<TParams = unknown, TState = unknown> {
|
|
1562
|
+
private spec: Record<string, unknown> = {};
|
|
1563
|
+
|
|
1564
|
+
constructor(private readonly actionId: string) {}
|
|
1565
|
+
|
|
1566
|
+
label(label: string): this {
|
|
1567
|
+
this.spec.label = label;
|
|
1568
|
+
return this;
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
description(description: string): this {
|
|
1572
|
+
this.spec.description = description;
|
|
1573
|
+
return this;
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
acceptanceState(state: ProcessHumanTurnActionSpec<TParams, TState>["acceptanceState"]): this {
|
|
1577
|
+
this.spec.acceptanceState = state;
|
|
1578
|
+
return this;
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
form(form: FormDefinition): this {
|
|
1582
|
+
this.spec.form = form;
|
|
1583
|
+
return this;
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
to(turnId: TurnId): this {
|
|
1587
|
+
this.spec.to = turnId;
|
|
1588
|
+
return this;
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
complete(): this {
|
|
1592
|
+
this.spec.complete = true;
|
|
1593
|
+
return this;
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
lifecycleStatus(status: ProcessTurnTerminalLifecycleStatus): this {
|
|
1597
|
+
this.spec.lifecycleStatus = status;
|
|
1598
|
+
return this;
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
trigger(trigger: string): this {
|
|
1602
|
+
this.spec.trigger = trigger;
|
|
1603
|
+
return this;
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
schedulable(value = true): this {
|
|
1607
|
+
this.spec.schedulable = value;
|
|
1608
|
+
return this;
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
effect(effect: ProcessHumanTurnActionSpec<TParams, TState>["effect"]): this {
|
|
1612
|
+
this.spec.effect = effect;
|
|
1613
|
+
return this;
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
build(): ProcessHumanTurnActionSpec<TParams, TState> {
|
|
1617
|
+
return {
|
|
1618
|
+
label:
|
|
1619
|
+
typeof this.spec.label === "string" ? this.spec.label : inferActionLabel(this.actionId),
|
|
1620
|
+
acceptanceState:
|
|
1621
|
+
(this.spec.acceptanceState as
|
|
1622
|
+
| ProcessHumanTurnActionSpec<TParams, TState>["acceptanceState"]
|
|
1623
|
+
| undefined) ?? inferAcceptanceState(this.actionId),
|
|
1624
|
+
...this.spec,
|
|
1625
|
+
} as ProcessHumanTurnActionSpec<TParams, TState>;
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
export class ExternalActionBuilder<
|
|
1630
|
+
TParams = unknown,
|
|
1631
|
+
TState = unknown,
|
|
1632
|
+
TEvent = unknown,
|
|
1633
|
+
TInput extends Record<string, unknown> = Record<string, unknown>,
|
|
1634
|
+
> {
|
|
1635
|
+
private spec: Partial<ProcessHumanTurnExternalActionSpec<TParams, TState, TEvent, TInput>>;
|
|
1636
|
+
|
|
1637
|
+
constructor(actionId: string, source: ExternalActionSource<TParams, TState, TEvent, TInput>) {
|
|
1638
|
+
this.spec = { id: actionId, source };
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
label(label: string): this {
|
|
1642
|
+
this.spec.label = label;
|
|
1643
|
+
return this;
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
description(description: string): this {
|
|
1647
|
+
this.spec.description = description;
|
|
1648
|
+
return this;
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
to(turnId: TurnId): this {
|
|
1652
|
+
this.spec.to = turnId;
|
|
1653
|
+
return this;
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
complete(): this {
|
|
1657
|
+
this.spec.complete = true;
|
|
1658
|
+
return this;
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
lifecycleStatus(status: ProcessTurnTerminalLifecycleStatus): this {
|
|
1662
|
+
this.spec.lifecycleStatus = status;
|
|
1663
|
+
return this;
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
publishInput(productName: string, options: { inputField: string }): this {
|
|
1667
|
+
this.spec.publishInput = {
|
|
1668
|
+
productName: normalizeProductName(productName),
|
|
1669
|
+
inputField: options.inputField,
|
|
1670
|
+
};
|
|
1671
|
+
return this;
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
build(): ProcessHumanTurnExternalActionSpec<TParams, TState, TEvent, TInput> {
|
|
1675
|
+
return this.spec as ProcessHumanTurnExternalActionSpec<TParams, TState, TEvent, TInput>;
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
export class HumanFlowBuilder<TParams = unknown, TState = unknown>
|
|
1680
|
+
implements FlowHumanTurn<TParams, TState>
|
|
1681
|
+
{
|
|
1682
|
+
private turnDescription: string | null = null;
|
|
1683
|
+
private reviewSubject: ReviewSubject | null = null;
|
|
1684
|
+
private reviewProductName: string | undefined;
|
|
1685
|
+
private reviewSemanticRef: ProcessSemanticEntryRefKey | undefined;
|
|
1686
|
+
private operatorAttentionValue: HumanTurnOperatorAttention | undefined;
|
|
1687
|
+
private turnCommentary: string | undefined;
|
|
1688
|
+
private notes: HumanTurnDefinition<TParams, TState>["notesFields"] | undefined;
|
|
1689
|
+
private actions = new Map<string, HumanActionBuilder<TParams, TState>>();
|
|
1690
|
+
private externalActionBuilders = new Map<
|
|
1691
|
+
string,
|
|
1692
|
+
ExternalActionBuilder<TParams, TState, unknown, Record<string, unknown>>
|
|
1693
|
+
>();
|
|
1694
|
+
|
|
1695
|
+
constructor(private readonly turnId: TurnId) {}
|
|
1696
|
+
|
|
1697
|
+
get id(): TurnId {
|
|
1698
|
+
return this.turnId;
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
get definition(): HumanTurnDefinition<TParams, TState> {
|
|
1702
|
+
if (!this.turnDescription) {
|
|
1703
|
+
throw new Error(`Human turn '${this.turnId}' must declare .description(...)`);
|
|
1704
|
+
}
|
|
1705
|
+
return {
|
|
1706
|
+
kind: "human",
|
|
1707
|
+
description: this.turnDescription,
|
|
1708
|
+
...(this.reviewSubject ? { reviewSubject: this.reviewSubject } : {}),
|
|
1709
|
+
...(this.reviewProductName ? { reviewProduct: this.reviewProductName } : {}),
|
|
1710
|
+
...(this.reviewSemanticRef ? { reviewSemanticRef: this.reviewSemanticRef } : {}),
|
|
1711
|
+
...(this.operatorAttentionValue ? { operatorAttention: this.operatorAttentionValue } : {}),
|
|
1712
|
+
...(this.notes ? { notesFields: this.notes } : {}),
|
|
1713
|
+
...(this.turnCommentary ? { commentary: this.turnCommentary } : {}),
|
|
1714
|
+
actions: Object.fromEntries(
|
|
1715
|
+
[...this.actions.entries()].map(([actionId, builder]) => [actionId, builder.build()]),
|
|
1716
|
+
),
|
|
1717
|
+
...(this.externalActionBuilders.size > 0
|
|
1718
|
+
? {
|
|
1719
|
+
externalActions: Object.fromEntries(
|
|
1720
|
+
[...this.externalActionBuilders.entries()].map(([actionId, builder]) => [
|
|
1721
|
+
actionId,
|
|
1722
|
+
builder.build(),
|
|
1723
|
+
]),
|
|
1724
|
+
),
|
|
1725
|
+
}
|
|
1726
|
+
: {}),
|
|
1727
|
+
};
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1730
|
+
description(description: string): this {
|
|
1731
|
+
this.turnDescription = description;
|
|
1732
|
+
return this;
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
review(subject: ReviewSubject | "plan" | "implementation"): this {
|
|
1736
|
+
this.reviewSubject = typeof subject === "string" ? createReviewSubject(subject) : subject;
|
|
1737
|
+
this.reviewProductName = undefined;
|
|
1738
|
+
this.reviewSemanticRef = typeof subject === "string" && subject === "plan" ? "plan" : undefined;
|
|
1739
|
+
return this;
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
reviewProduct(
|
|
1743
|
+
productName: string,
|
|
1744
|
+
options: { subject: ReviewSubject | "plan" | "implementation" },
|
|
1745
|
+
): this {
|
|
1746
|
+
const normalized = normalizeProductName(productName);
|
|
1747
|
+
this.reviewProductName = normalized;
|
|
1748
|
+
this.reviewSubject =
|
|
1749
|
+
typeof options.subject === "string" ? createReviewSubject(options.subject) : options.subject;
|
|
1750
|
+
this.reviewSemanticRef =
|
|
1751
|
+
normalized === "plan" ? "plan" : normalized === "review" ? "review" : undefined;
|
|
1752
|
+
return this;
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
operatorAttention(attention: HumanTurnOperatorAttention): this {
|
|
1756
|
+
this.operatorAttentionValue = attention;
|
|
1757
|
+
return this;
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
commentary(commentary: string): this {
|
|
1761
|
+
this.turnCommentary = commentary;
|
|
1762
|
+
return this;
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
notesFields(notes: HumanTurnDefinition<TParams, TState>["notesFields"]): this {
|
|
1766
|
+
this.notes = notes;
|
|
1767
|
+
return this;
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
action(
|
|
1771
|
+
actionId: string,
|
|
1772
|
+
configure: (
|
|
1773
|
+
action: HumanActionBuilder<TParams, TState>,
|
|
1774
|
+
) => HumanActionBuilder<TParams, TState> | undefined,
|
|
1775
|
+
): this {
|
|
1776
|
+
if (this.actions.has(actionId)) {
|
|
1777
|
+
throw new Error(`Human turn '${this.turnId}' declares duplicate action '${actionId}'`);
|
|
1778
|
+
}
|
|
1779
|
+
const builder = new HumanActionBuilder<TParams, TState>(actionId);
|
|
1780
|
+
configure(builder);
|
|
1781
|
+
this.actions.set(actionId, builder);
|
|
1782
|
+
return this;
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
externalAction<
|
|
1786
|
+
TEvent = unknown,
|
|
1787
|
+
TInput extends Record<string, unknown> = Record<string, unknown>,
|
|
1788
|
+
>(
|
|
1789
|
+
externalActionId: string,
|
|
1790
|
+
source: ExternalActionSource<TParams, TState, TEvent, TInput>,
|
|
1791
|
+
configure: (
|
|
1792
|
+
external: ExternalActionBuilder<TParams, TState, TEvent, TInput>,
|
|
1793
|
+
) => ExternalActionBuilder<TParams, TState, TEvent, TInput> | undefined,
|
|
1794
|
+
): this {
|
|
1795
|
+
if (this.externalActionBuilders.has(externalActionId)) {
|
|
1796
|
+
throw new Error(
|
|
1797
|
+
`Human turn '${this.turnId}' declares duplicate external action '${externalActionId}'`,
|
|
1798
|
+
);
|
|
1799
|
+
}
|
|
1800
|
+
const builder = new ExternalActionBuilder<TParams, TState, TEvent, TInput>(
|
|
1801
|
+
externalActionId,
|
|
1802
|
+
source,
|
|
1803
|
+
);
|
|
1804
|
+
configure(builder);
|
|
1805
|
+
this.externalActionBuilders.set(
|
|
1806
|
+
externalActionId,
|
|
1807
|
+
builder as unknown as ExternalActionBuilder<
|
|
1808
|
+
TParams,
|
|
1809
|
+
TState,
|
|
1810
|
+
unknown,
|
|
1811
|
+
Record<string, unknown>
|
|
1812
|
+
>,
|
|
1813
|
+
);
|
|
1814
|
+
return this;
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
export class ExternalRouteBuilder<
|
|
1819
|
+
TParams = unknown,
|
|
1820
|
+
TState = unknown,
|
|
1821
|
+
TEvent = unknown,
|
|
1822
|
+
TInput extends Record<string, unknown> = Record<string, unknown>,
|
|
1823
|
+
> implements FlowExternalTurn<TParams, TState>
|
|
1824
|
+
{
|
|
1825
|
+
private externalEffect: ExternalSourceEffect<TParams, TState, TEvent, TInput> | undefined;
|
|
1826
|
+
private target:
|
|
1827
|
+
| { kind: "to"; turnId: TurnId }
|
|
1828
|
+
| { kind: "complete" }
|
|
1829
|
+
| { kind: "lifecycleStatus"; status: ProcessTurnTerminalLifecycleStatus }
|
|
1830
|
+
| null = null;
|
|
1831
|
+
constructor(
|
|
1832
|
+
private readonly parent: ExternalFlowBuilder<TParams, TState>,
|
|
1833
|
+
private readonly source: ExternalActionSource<TParams, TState, TEvent, TInput>,
|
|
1834
|
+
) {}
|
|
1835
|
+
|
|
1836
|
+
get id(): TurnId {
|
|
1837
|
+
return this.parent.id;
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
get definition(): ExternalTurnDefinition<TParams, TState> {
|
|
1841
|
+
return this.parent.definition;
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
private setTarget(
|
|
1845
|
+
next:
|
|
1846
|
+
| { kind: "to"; turnId: TurnId }
|
|
1847
|
+
| { kind: "complete" }
|
|
1848
|
+
| { kind: "lifecycleStatus"; status: ProcessTurnTerminalLifecycleStatus },
|
|
1849
|
+
): this {
|
|
1850
|
+
if (this.target) {
|
|
1851
|
+
throw new Error("External source route already declares a target");
|
|
1852
|
+
}
|
|
1853
|
+
this.target = next;
|
|
1854
|
+
return this;
|
|
1855
|
+
}
|
|
1856
|
+
|
|
1857
|
+
to(turnId: TurnId): this {
|
|
1858
|
+
return this.setTarget({ kind: "to", turnId });
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
complete(): this {
|
|
1862
|
+
return this.setTarget({ kind: "complete" });
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1865
|
+
lifecycleStatus(status: ProcessTurnTerminalLifecycleStatus): this {
|
|
1866
|
+
return this.setTarget({ kind: "lifecycleStatus", status });
|
|
1867
|
+
}
|
|
1868
|
+
|
|
1869
|
+
state(
|
|
1870
|
+
fn: (input: {
|
|
1871
|
+
ctx: FlowExternalSourceContext<TParams, TState, TEvent, TInput>;
|
|
1872
|
+
event: TEvent;
|
|
1873
|
+
input: TInput;
|
|
1874
|
+
}) => MaybePromise<TState>,
|
|
1875
|
+
): this {
|
|
1876
|
+
this.externalEffect = async (ctx) => ({
|
|
1877
|
+
state: await fn({ ctx, event: ctx.event, input: ctx.input }),
|
|
1878
|
+
});
|
|
1879
|
+
return this;
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
effect(
|
|
1883
|
+
fn: (input: {
|
|
1884
|
+
ctx: FlowExternalSourceContext<TParams, TState, TEvent, TInput>;
|
|
1885
|
+
event: TEvent;
|
|
1886
|
+
input: TInput;
|
|
1887
|
+
}) => MaybePromise<ProcessEffectPlan<TState> | undefined>,
|
|
1888
|
+
): this {
|
|
1889
|
+
this.externalEffect = (ctx) => fn({ ctx, event: ctx.event, input: ctx.input });
|
|
1890
|
+
return this;
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1893
|
+
private buildRouteTarget(): {
|
|
1894
|
+
to?: TurnId;
|
|
1895
|
+
complete?: boolean;
|
|
1896
|
+
lifecycleStatus?: ProcessTurnTerminalLifecycleStatus;
|
|
1897
|
+
} {
|
|
1898
|
+
return buildRouteTargetSpec(this.target);
|
|
1899
|
+
}
|
|
1900
|
+
|
|
1901
|
+
build() {
|
|
1902
|
+
return {
|
|
1903
|
+
source: this.source,
|
|
1904
|
+
...this.buildRouteTarget(),
|
|
1905
|
+
...(this.externalEffect ? { effect: this.externalEffect } : {}),
|
|
1906
|
+
};
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
|
|
1910
|
+
export class ExternalFlowBuilder<TParams = unknown, TState = unknown>
|
|
1911
|
+
implements FlowExternalTurn<TParams, TState>
|
|
1912
|
+
{
|
|
1913
|
+
private turnDescription: string | null = null;
|
|
1914
|
+
private routeBuilders: ExternalRouteBuilder<TParams, TState, unknown, Record<string, unknown>>[] =
|
|
1915
|
+
[];
|
|
1916
|
+
|
|
1917
|
+
constructor(private readonly turnId: TurnId) {}
|
|
1918
|
+
|
|
1919
|
+
get id(): TurnId {
|
|
1920
|
+
return this.turnId;
|
|
1921
|
+
}
|
|
1922
|
+
|
|
1923
|
+
get definition(): ExternalTurnDefinition<TParams, TState> {
|
|
1924
|
+
if (!this.turnDescription) {
|
|
1925
|
+
this.turnDescription =
|
|
1926
|
+
this.routeBuilders[0]?.build().source.description ?? "Wait for an external source";
|
|
1927
|
+
}
|
|
1928
|
+
if (this.routeBuilders.length === 0) {
|
|
1929
|
+
throw new Error(`External turn '${this.turnId}' must declare at least one .from(...) source`);
|
|
1930
|
+
}
|
|
1931
|
+
return {
|
|
1932
|
+
kind: "external",
|
|
1933
|
+
description: this.turnDescription,
|
|
1934
|
+
transitions: this.routeBuilders.map((builder) => builder.build()),
|
|
1935
|
+
};
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
description(description: string): this {
|
|
1939
|
+
this.turnDescription = description;
|
|
1940
|
+
return this;
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
from<TEvent = unknown, TInput extends Record<string, unknown> = Record<string, unknown>>(
|
|
1944
|
+
source: ExternalActionSource<TParams, TState, TEvent, TInput>,
|
|
1945
|
+
): ExternalRouteBuilder<TParams, TState, TEvent, TInput> {
|
|
1946
|
+
const builder = new ExternalRouteBuilder<TParams, TState, TEvent, TInput>(this, source);
|
|
1947
|
+
this.routeBuilders.push(
|
|
1948
|
+
builder as unknown as ExternalRouteBuilder<TParams, TState, unknown, Record<string, unknown>>,
|
|
1949
|
+
);
|
|
1950
|
+
return builder;
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
type Hook<TApi> = (api: TApi) => void;
|
|
1955
|
+
|
|
1956
|
+
export abstract class FlowComponentBuilder<TParams = unknown, TState = unknown> {
|
|
1957
|
+
readonly turns: FlowTurn<TParams, TState>[] = [];
|
|
1958
|
+
readonly serverHooks: Hook<ServerProcessAPI<TParams, TState>>[] = [];
|
|
1959
|
+
readonly uiHooks: Hook<UiProcessAPI<TParams, TState>>[] = [];
|
|
1960
|
+
readonly launcherHooks: Hook<ProcessLauncherAPI<TParams>>[] = [];
|
|
1961
|
+
readonly watcherHooks: Hook<ProcessWatcherAPI<TParams>>[] = [];
|
|
1962
|
+
|
|
1963
|
+
turn(turn: FlowTurn<TParams, TState>): this {
|
|
1964
|
+
this.turns.push(turn);
|
|
1965
|
+
return this;
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
server(hook: Hook<ServerProcessAPI<TParams, TState>>): this {
|
|
1969
|
+
this.serverHooks.push(hook);
|
|
1970
|
+
return this;
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
action(definition: ProcessActionDefinition<TParams, TState>): this {
|
|
1974
|
+
this.serverHooks.push((api) => api.action(definition));
|
|
1975
|
+
return this;
|
|
1976
|
+
}
|
|
1977
|
+
|
|
1978
|
+
ui(hook: Hook<UiProcessAPI<TParams, TState>>): this {
|
|
1979
|
+
this.uiHooks.push(hook);
|
|
1980
|
+
return this;
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
launcher(
|
|
1984
|
+
definitionOrHook: ProcessLauncherDefinition<TParams> | Hook<ProcessLauncherAPI<TParams>>,
|
|
1985
|
+
): this {
|
|
1986
|
+
if (typeof definitionOrHook === "function") {
|
|
1987
|
+
this.launcherHooks.push(definitionOrHook);
|
|
1988
|
+
} else {
|
|
1989
|
+
this.launcherHooks.push((api) => api.launcher(definitionOrHook));
|
|
1990
|
+
}
|
|
1991
|
+
return this;
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
watcher<TEvent = unknown>(
|
|
1995
|
+
definitionOrHook: ProcessWatcherDefinition<TParams, TEvent> | Hook<ProcessWatcherAPI<TParams>>,
|
|
1996
|
+
): this {
|
|
1997
|
+
if (typeof definitionOrHook === "function") {
|
|
1998
|
+
this.watcherHooks.push(definitionOrHook);
|
|
1999
|
+
} else {
|
|
2000
|
+
this.watcherHooks.push((api) => api.watcher(definitionOrHook));
|
|
2001
|
+
}
|
|
2002
|
+
return this;
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
export class FlowFragmentBuilder<TParams = unknown, TState = unknown> extends FlowComponentBuilder<
|
|
2007
|
+
TParams,
|
|
2008
|
+
TState
|
|
2009
|
+
> {
|
|
2010
|
+
readonly name: string;
|
|
2011
|
+
|
|
2012
|
+
constructor(name: string) {
|
|
2013
|
+
super();
|
|
2014
|
+
if (name.trim() === "") {
|
|
2015
|
+
throw new Error("Flow fragment name must be non-empty");
|
|
2016
|
+
}
|
|
2017
|
+
this.name = name;
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
export class FlowProcessBuilder<TParams = unknown, TState = unknown> extends FlowComponentBuilder<
|
|
2022
|
+
TParams,
|
|
2023
|
+
TState
|
|
2024
|
+
> {
|
|
2025
|
+
private readonly processId: string;
|
|
2026
|
+
private processDisplayName: string | null = null;
|
|
2027
|
+
private entryTurnId: TurnId | null = null;
|
|
2028
|
+
private readonly alternateEntryTurnIds: TurnId[] = [];
|
|
2029
|
+
private happyPathTurnIds: readonly TurnId[] | null = null;
|
|
2030
|
+
private paramsCodec: Codec<TParams> | null = null;
|
|
2031
|
+
private stateCodec: Codec<TState> | null = null;
|
|
2032
|
+
private initialStateFn: ((params: TParams) => TState) | null = null;
|
|
2033
|
+
private repositoryCredentialsFn:
|
|
2034
|
+
| ((input: {
|
|
2035
|
+
params: TParams;
|
|
2036
|
+
projects: readonly RepositoryCredentialProject[];
|
|
2037
|
+
}) => readonly RepositoryCredentialRequirement[])
|
|
2038
|
+
| undefined;
|
|
2039
|
+
private processPiConfig: ProcessPiConfig | undefined;
|
|
2040
|
+
|
|
2041
|
+
constructor(processId: string) {
|
|
2042
|
+
super();
|
|
2043
|
+
if (processId.trim() === "") {
|
|
2044
|
+
throw new Error("Flow process id must be non-empty");
|
|
2045
|
+
}
|
|
2046
|
+
this.processId = processId;
|
|
2047
|
+
}
|
|
2048
|
+
|
|
2049
|
+
displayName(displayName: string): this {
|
|
2050
|
+
this.processDisplayName = displayName;
|
|
2051
|
+
return this;
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
entry(turnId: TurnId): this {
|
|
2055
|
+
this.entryTurnId = turnId;
|
|
2056
|
+
return this;
|
|
2057
|
+
}
|
|
2058
|
+
|
|
2059
|
+
/** Declare an additional turn that launchers may select as the first turn. */
|
|
2060
|
+
alternateEntry(turnId: TurnId): this {
|
|
2061
|
+
this.alternateEntryTurnIds.push(turnId);
|
|
2062
|
+
return this;
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
/**
|
|
2066
|
+
* Declare the happy path: the ordered spine of turns a successful run walks
|
|
2067
|
+
* through. Flow diagrams render this as the main line. Must start at the entry
|
|
2068
|
+
* turn and reference only declared turns without repeats.
|
|
2069
|
+
*/
|
|
2070
|
+
happyPath(...turnIds: readonly TurnId[]): this {
|
|
2071
|
+
this.happyPathTurnIds = turnIds;
|
|
2072
|
+
return this;
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
codecs(input: { params: Codec<TParams>; state: Codec<TState> }): this {
|
|
2076
|
+
this.paramsCodec = input.params;
|
|
2077
|
+
this.stateCodec = input.state;
|
|
2078
|
+
return this;
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2081
|
+
initialState(fn: (params: TParams) => TState): this {
|
|
2082
|
+
this.initialStateFn = fn;
|
|
2083
|
+
return this;
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
repositoryCredentials(
|
|
2087
|
+
fn: (input: {
|
|
2088
|
+
params: TParams;
|
|
2089
|
+
projects: readonly RepositoryCredentialProject[];
|
|
2090
|
+
}) => readonly RepositoryCredentialRequirement[],
|
|
2091
|
+
): this {
|
|
2092
|
+
this.repositoryCredentialsFn = fn;
|
|
2093
|
+
return this;
|
|
2094
|
+
}
|
|
2095
|
+
|
|
2096
|
+
piConfig(config: ProcessPiConfig): this {
|
|
2097
|
+
this.processPiConfig = config;
|
|
2098
|
+
return this;
|
|
2099
|
+
}
|
|
2100
|
+
|
|
2101
|
+
use(fragment: FlowFragmentBuilder<TParams, TState>): this {
|
|
2102
|
+
this.turns.push(...fragment.turns);
|
|
2103
|
+
this.serverHooks.push(...fragment.serverHooks);
|
|
2104
|
+
this.uiHooks.push(...fragment.uiHooks);
|
|
2105
|
+
this.launcherHooks.push(...fragment.launcherHooks);
|
|
2106
|
+
this.watcherHooks.push(...fragment.watcherHooks);
|
|
2107
|
+
return this;
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2110
|
+
define(): ProcessDefinition<TParams, TState> {
|
|
2111
|
+
if (!this.processDisplayName) {
|
|
2112
|
+
throw new Error(`Flow process '${this.processId}' must declare .displayName(...)`);
|
|
2113
|
+
}
|
|
2114
|
+
if (!this.entryTurnId) {
|
|
2115
|
+
throw new Error(`Flow process '${this.processId}' must declare .entry(turnId)`);
|
|
2116
|
+
}
|
|
2117
|
+
if (!this.paramsCodec || !this.stateCodec) {
|
|
2118
|
+
throw new Error(`Flow process '${this.processId}' must declare .codecs(...)`);
|
|
2119
|
+
}
|
|
2120
|
+
if (!this.initialStateFn) {
|
|
2121
|
+
throw new Error(`Flow process '${this.processId}' must declare .initialState(...)`);
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2124
|
+
const turns: TurnDefinitionRecord<TParams, TState> = {};
|
|
2125
|
+
for (const turn of this.turns) {
|
|
2126
|
+
if (Object.hasOwn(turns, turn.id)) {
|
|
2127
|
+
throw new Error(`Flow process '${this.processId}' declares duplicate turn '${turn.id}'`);
|
|
2128
|
+
}
|
|
2129
|
+
turns[turn.id] = turn.definition;
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
const publishedProducts = new Set<string>();
|
|
2133
|
+
for (const turnDef of Object.values(turns)) {
|
|
2134
|
+
if (turnDef.kind === "llm") {
|
|
2135
|
+
if (turnDef.publishedProduct) {
|
|
2136
|
+
normalizeProductName(turnDef.publishedProduct);
|
|
2137
|
+
publishedProducts.add(turnDef.publishedProduct);
|
|
2138
|
+
}
|
|
2139
|
+
for (const outcome of Object.values(turnDef.outcomes ?? {})) {
|
|
2140
|
+
if (outcome?.publishedProduct) {
|
|
2141
|
+
normalizeProductName(outcome.publishedProduct);
|
|
2142
|
+
publishedProducts.add(outcome.publishedProduct);
|
|
2143
|
+
}
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
if (turnDef.kind === "human") {
|
|
2147
|
+
for (const action of Object.values(turnDef.actions)) {
|
|
2148
|
+
for (const field of action.form?.fields ?? []) {
|
|
2149
|
+
const productName = formFieldPublishedProductName(field);
|
|
2150
|
+
if (productName) {
|
|
2151
|
+
publishedProducts.add(productName);
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
for (const externalAction of Object.values(turnDef.externalActions ?? {})) {
|
|
2156
|
+
if (externalAction.publishInput) {
|
|
2157
|
+
publishedProducts.add(normalizeProductName(externalAction.publishInput.productName));
|
|
2158
|
+
}
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
for (const [turnId, turnDef] of Object.entries(turns)) {
|
|
2163
|
+
if (turnDef.kind !== "llm") {
|
|
2164
|
+
continue;
|
|
2165
|
+
}
|
|
2166
|
+
for (const productName of [
|
|
2167
|
+
...(turnDef.consumedProducts ?? []),
|
|
2168
|
+
...(turnDef.optionalConsumedProducts ?? []),
|
|
2169
|
+
]) {
|
|
2170
|
+
normalizeProductName(productName);
|
|
2171
|
+
if (!publishedProducts.has(productName)) {
|
|
2172
|
+
throw new Error(
|
|
2173
|
+
`Flow process '${this.processId}' turn '${turnId}' consumes product '${productName}' that is never published by this process`,
|
|
2174
|
+
);
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
const input: DefinedProcessInput<TParams, TState> = {
|
|
2180
|
+
id: this.processId,
|
|
2181
|
+
displayName: this.processDisplayName,
|
|
2182
|
+
entry: this.entryTurnId,
|
|
2183
|
+
...(this.alternateEntryTurnIds.length > 0
|
|
2184
|
+
? { alternateEntries: [...this.alternateEntryTurnIds] }
|
|
2185
|
+
: {}),
|
|
2186
|
+
...(this.happyPathTurnIds ? { happyPath: [...this.happyPathTurnIds] } : {}),
|
|
2187
|
+
paramsCodec: this.paramsCodec,
|
|
2188
|
+
stateCodec: this.stateCodec,
|
|
2189
|
+
initialState: this.initialStateFn,
|
|
2190
|
+
...(this.repositoryCredentialsFn
|
|
2191
|
+
? { repositoryCredentials: this.repositoryCredentialsFn }
|
|
2192
|
+
: {}),
|
|
2193
|
+
...(this.processPiConfig ? { piConfig: this.processPiConfig } : {}),
|
|
2194
|
+
turns,
|
|
2195
|
+
...(this.serverHooks.length > 0
|
|
2196
|
+
? {
|
|
2197
|
+
server: (api) => {
|
|
2198
|
+
for (const hook of this.serverHooks) {
|
|
2199
|
+
hook(api);
|
|
2200
|
+
}
|
|
2201
|
+
},
|
|
2202
|
+
}
|
|
2203
|
+
: {}),
|
|
2204
|
+
...(this.uiHooks.length > 0
|
|
2205
|
+
? {
|
|
2206
|
+
ui: (api) => {
|
|
2207
|
+
for (const hook of this.uiHooks) {
|
|
2208
|
+
hook(api);
|
|
2209
|
+
}
|
|
2210
|
+
},
|
|
2211
|
+
}
|
|
2212
|
+
: {}),
|
|
2213
|
+
...(this.launcherHooks.length > 0
|
|
2214
|
+
? {
|
|
2215
|
+
launchers: (api) => {
|
|
2216
|
+
for (const hook of this.launcherHooks) {
|
|
2217
|
+
hook(api);
|
|
2218
|
+
}
|
|
2219
|
+
},
|
|
2220
|
+
}
|
|
2221
|
+
: {}),
|
|
2222
|
+
...(this.watcherHooks.length > 0
|
|
2223
|
+
? {
|
|
2224
|
+
watchers: (api) => {
|
|
2225
|
+
for (const hook of this.watcherHooks) {
|
|
2226
|
+
hook(api);
|
|
2227
|
+
}
|
|
2228
|
+
},
|
|
2229
|
+
}
|
|
2230
|
+
: {}),
|
|
2231
|
+
};
|
|
2232
|
+
return defineProcess(input);
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
export const flow = {
|
|
2237
|
+
llm<TParams = unknown, TState = unknown>(turnId: TurnId): LlmFlowBuilder<TParams, TState> {
|
|
2238
|
+
return new LlmFlowBuilder<TParams, TState>(turnId);
|
|
2239
|
+
},
|
|
2240
|
+
automatic<TParams = unknown, TState = unknown>(
|
|
2241
|
+
turnId: TurnId,
|
|
2242
|
+
): AutomaticFlowBuilder<TParams, TState> {
|
|
2243
|
+
return new AutomaticFlowBuilder<TParams, TState>(turnId);
|
|
2244
|
+
},
|
|
2245
|
+
serverAutomatic<TParams = unknown, TState = unknown>(
|
|
2246
|
+
turnId: TurnId,
|
|
2247
|
+
): ServerAutomaticFlowBuilder<TParams, TState> {
|
|
2248
|
+
return new ServerAutomaticFlowBuilder<TParams, TState>(turnId);
|
|
2249
|
+
},
|
|
2250
|
+
human<TParams = unknown, TState = unknown>(turnId: TurnId): HumanFlowBuilder<TParams, TState> {
|
|
2251
|
+
return new HumanFlowBuilder<TParams, TState>(turnId);
|
|
2252
|
+
},
|
|
2253
|
+
external<TParams = unknown, TState = unknown>(
|
|
2254
|
+
turnId: TurnId,
|
|
2255
|
+
): ExternalFlowBuilder<TParams, TState> {
|
|
2256
|
+
return new ExternalFlowBuilder<TParams, TState>(turnId);
|
|
2257
|
+
},
|
|
2258
|
+
fragment<TParams = unknown, TState = unknown>(
|
|
2259
|
+
name: string,
|
|
2260
|
+
): FlowFragmentBuilder<TParams, TState> {
|
|
2261
|
+
return new FlowFragmentBuilder<TParams, TState>(name);
|
|
2262
|
+
},
|
|
2263
|
+
process<TParams = unknown, TState = unknown>(
|
|
2264
|
+
processId: string,
|
|
2265
|
+
): FlowProcessBuilder<TParams, TState> {
|
|
2266
|
+
return new FlowProcessBuilder<TParams, TState>(processId);
|
|
2267
|
+
},
|
|
2268
|
+
};
|