@leitwerk-dev/process-sdk 0.1.9 → 0.2.1

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