@leitwerk-dev/process-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LICENSE +13 -0
- package/dist/browser-ui-extension.d.ts +27 -0
- package/dist/browser-ui-extension.d.ts.map +1 -0
- package/dist/browser-ui-extension.js +2 -0
- package/dist/browser-ui-extension.js.map +1 -0
- package/dist/capabilities.d.ts +9 -0
- package/dist/capabilities.d.ts.map +1 -0
- package/dist/capabilities.js +7 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/chunk-5RJZ2E7D.js +58 -0
- package/dist/chunk-5RJZ2E7D.js.map +1 -0
- package/dist/chunk-ITPH65G6.js +197 -0
- package/dist/chunk-ITPH65G6.js.map +1 -0
- package/dist/chunk-LIIP7FMU.js +48 -0
- package/dist/chunk-LIIP7FMU.js.map +1 -0
- package/dist/codecs.d.ts +3 -0
- package/dist/codecs.d.ts.map +1 -0
- package/dist/codecs.js +9 -0
- package/dist/codecs.js.map +1 -0
- package/dist/commit-message.d.ts +7 -0
- package/dist/commit-message.d.ts.map +1 -0
- package/dist/commit-message.js +3 -0
- package/dist/commit-message.js.map +1 -0
- package/dist/core-capabilities.d.ts +393 -0
- package/dist/core-capabilities.d.ts.map +1 -0
- package/dist/core-capabilities.js +6 -0
- package/dist/core-capabilities.js.map +1 -0
- package/dist/define-process.d.ts +269 -0
- package/dist/define-process.d.ts.map +1 -0
- package/dist/define-process.js +1040 -0
- package/dist/define-process.js.map +1 -0
- package/dist/event-bus.d.ts +3 -0
- package/dist/event-bus.d.ts.map +1 -0
- package/dist/event-bus.js +31 -0
- package/dist/event-bus.js.map +1 -0
- package/dist/extension-api-CnlL69Tu.d.ts +1281 -0
- package/dist/extension-api.d.ts +425 -0
- package/dist/extension-api.d.ts.map +1 -0
- package/dist/extension-api.js +5 -0
- package/dist/extension-api.js.map +1 -0
- package/dist/flow.d.ts +456 -0
- package/dist/flow.d.ts.map +1 -0
- package/dist/flow.js +1557 -0
- package/dist/flow.js.map +1 -0
- package/dist/form-contract.d.ts +2 -0
- package/dist/form-contract.d.ts.map +1 -0
- package/dist/form-contract.js +2 -0
- package/dist/form-contract.js.map +1 -0
- package/dist/fs-utils.d.ts +3 -0
- package/dist/fs-utils.d.ts.map +1 -0
- package/dist/fs-utils.js +23 -0
- package/dist/fs-utils.js.map +1 -0
- package/dist/git-binary.d.ts +11 -0
- package/dist/git-binary.d.ts.map +1 -0
- package/dist/git-binary.js +38 -0
- package/dist/git-binary.js.map +1 -0
- package/dist/graph-fragments.d.ts +23 -0
- package/dist/graph-fragments.d.ts.map +1 -0
- package/dist/graph-fragments.js +57 -0
- package/dist/graph-fragments.js.map +1 -0
- package/dist/host-capabilities.d.ts +12 -0
- package/dist/host-capabilities.d.ts.map +1 -0
- package/dist/host-capabilities.js +42 -0
- package/dist/host-capabilities.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/launcher-contract.d.ts +2 -0
- package/dist/launcher-contract.d.ts.map +1 -0
- package/dist/launcher-contract.js +2 -0
- package/dist/launcher-contract.js.map +1 -0
- package/dist/leaf-outcome-renderer.d.ts +48 -0
- package/dist/leaf-outcome-renderer.d.ts.map +1 -0
- package/dist/leaf-outcome-renderer.js +69 -0
- package/dist/leaf-outcome-renderer.js.map +1 -0
- package/dist/leaf-outcomes.d.ts +5 -0
- package/dist/leaf-outcomes.d.ts.map +1 -0
- package/dist/leaf-outcomes.js +52 -0
- package/dist/leaf-outcomes.js.map +1 -0
- package/dist/model-provider.d.ts +210 -0
- package/dist/model-provider.d.ts.map +1 -0
- package/dist/model-provider.js +171 -0
- package/dist/model-provider.js.map +1 -0
- package/dist/pi-config.d.ts +59 -0
- package/dist/pi-config.d.ts.map +1 -0
- package/dist/pi-config.js +182 -0
- package/dist/pi-config.js.map +1 -0
- package/dist/process-definition-internals.d.ts +7 -0
- package/dist/process-definition-internals.d.ts.map +1 -0
- package/dist/process-definition-internals.js +25 -0
- package/dist/process-definition-internals.js.map +1 -0
- package/dist/process-flow-view.d.ts +20 -0
- package/dist/process-flow-view.d.ts.map +1 -0
- package/dist/process-flow-view.js +418 -0
- package/dist/process-flow-view.js.map +1 -0
- package/dist/process-graph.d.ts +45 -0
- package/dist/process-graph.d.ts.map +1 -0
- package/dist/process-graph.js +213 -0
- package/dist/process-graph.js.map +1 -0
- package/dist/process-launcher-builder.d.ts +9 -0
- package/dist/process-launcher-builder.d.ts.map +1 -0
- package/dist/process-launcher-builder.js +43 -0
- package/dist/process-launcher-builder.js.map +1 -0
- package/dist/process-watcher-builder.d.ts +10 -0
- package/dist/process-watcher-builder.d.ts.map +1 -0
- package/dist/process-watcher-builder.js +40 -0
- package/dist/process-watcher-builder.js.map +1 -0
- package/dist/registry-utils.d.ts +8 -0
- package/dist/registry-utils.d.ts.map +1 -0
- package/dist/registry-utils.js +18 -0
- package/dist/registry-utils.js.map +1 -0
- package/dist/repository-git-env.d.ts +8 -0
- package/dist/repository-git-env.d.ts.map +1 -0
- package/dist/repository-git-env.js +26 -0
- package/dist/repository-git-env.js.map +1 -0
- package/dist/review-flow.d.ts +10 -0
- package/dist/review-flow.d.ts.map +1 -0
- package/dist/review-flow.js +44 -0
- package/dist/review-flow.js.map +1 -0
- package/dist/runtime-internals.d.ts +3 -0
- package/dist/runtime-internals.d.ts.map +1 -0
- package/dist/runtime-internals.js +3 -0
- package/dist/runtime-internals.js.map +1 -0
- package/dist/server-events.d.ts +84 -0
- package/dist/server-events.d.ts.map +1 -0
- package/dist/server-events.js +2 -0
- package/dist/server-events.js.map +1 -0
- package/dist/server-process-builder.d.ts +11 -0
- package/dist/server-process-builder.d.ts.map +1 -0
- package/dist/server-process-builder.js +52 -0
- package/dist/server-process-builder.js.map +1 -0
- package/dist/state-helpers.d.ts +9 -0
- package/dist/state-helpers.d.ts.map +1 -0
- package/dist/state-helpers.js +20 -0
- package/dist/state-helpers.js.map +1 -0
- package/dist/subprocess-env.d.ts +12 -0
- package/dist/subprocess-env.d.ts.map +1 -0
- package/dist/subprocess-env.js +34 -0
- package/dist/subprocess-env.js.map +1 -0
- package/dist/tool-renderers.d.ts +10 -0
- package/dist/tool-renderers.d.ts.map +1 -0
- package/dist/tool-renderers.js +63 -0
- package/dist/tool-renderers.js.map +1 -0
- package/dist/turn-semantics.d.ts +22 -0
- package/dist/turn-semantics.d.ts.map +1 -0
- package/dist/turn-semantics.js +339 -0
- package/dist/turn-semantics.js.map +1 -0
- package/dist/types.d.ts +295 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/dist/ui-process-builder.d.ts +9 -0
- package/dist/ui-process-builder.d.ts.map +1 -0
- package/dist/ui-process-builder.js +30 -0
- package/dist/ui-process-builder.js.map +1 -0
- package/dist/worker-process-builder.d.ts +10 -0
- package/dist/worker-process-builder.d.ts.map +1 -0
- package/dist/worker-process-builder.js +22 -0
- package/dist/worker-process-builder.js.map +1 -0
- package/package.json +72 -0
- package/src/browser-ui-extension.ts +32 -0
- package/src/capabilities.ts +16 -0
- package/src/codecs.ts +10 -0
- package/src/commit-message.ts +9 -0
- package/src/core-capabilities.ts +526 -0
- package/src/define-process.ts +1879 -0
- package/src/event-bus.ts +35 -0
- package/src/extension-api.ts +600 -0
- package/src/flow.ts +2268 -0
- package/src/form-contract.ts +11 -0
- package/src/fs-utils.ts +28 -0
- package/src/git-binary.ts +40 -0
- package/src/graph-fragments.ts +110 -0
- package/src/host-capabilities.ts +60 -0
- package/src/index.ts +468 -0
- package/src/launcher-contract.ts +8 -0
- package/src/leaf-outcome-renderer.ts +112 -0
- package/src/leaf-outcomes.ts +74 -0
- package/src/model-provider.ts +461 -0
- package/src/pi-config.ts +268 -0
- package/src/process-definition-internals.ts +46 -0
- package/src/process-flow-view.ts +479 -0
- package/src/process-graph.ts +309 -0
- package/src/process-launcher-builder.ts +59 -0
- package/src/process-watcher-builder.ts +72 -0
- package/src/registry-utils.ts +31 -0
- package/src/repository-git-env.ts +35 -0
- package/src/review-flow.ts +66 -0
- package/src/runtime-internals.ts +7 -0
- package/src/server-events.ts +105 -0
- package/src/server-process-builder.ts +90 -0
- package/src/state-helpers.ts +36 -0
- package/src/subprocess-env.ts +48 -0
- package/src/tool-renderers.ts +92 -0
- package/src/turn-semantics.ts +554 -0
- package/src/types.ts +391 -0
- package/src/ui-process-builder.ts +46 -0
- package/src/worker-process-builder.ts +33 -0
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertValidProcessProductName,
|
|
3
|
+
defaultProcessTurnStartSelection,
|
|
4
|
+
type ProcessTurnStartFallback,
|
|
5
|
+
type ProcessTurnStartSelection,
|
|
6
|
+
resolveProcessTurnStartSelection,
|
|
7
|
+
} from "@leitwerk-dev/domain";
|
|
8
|
+
import type {
|
|
9
|
+
AutomaticTurnDefinition,
|
|
10
|
+
ExternalTurnDefinition,
|
|
11
|
+
HumanTurnDefinition,
|
|
12
|
+
LlmTurnDefinition,
|
|
13
|
+
ProcessToolOutcomeSpec,
|
|
14
|
+
ServerAutomaticTurnDefinition,
|
|
15
|
+
TurnDefinition,
|
|
16
|
+
} from "./define-process.js";
|
|
17
|
+
import { llmTurn, resolveHumanTurnView } from "./define-process.js";
|
|
18
|
+
import { validatePiBuiltInToolArray } from "./pi-config.js";
|
|
19
|
+
import { REQUIRED_MARKDOWN_RESULT_TURN_RESULT } from "./tool-renderers.js";
|
|
20
|
+
import type {
|
|
21
|
+
OutcomeToolParameterSpec,
|
|
22
|
+
ProcessActionPreviewDefinition,
|
|
23
|
+
ProcessActionSchedulingDefinition,
|
|
24
|
+
TurnBranchType,
|
|
25
|
+
} from "./types.js";
|
|
26
|
+
|
|
27
|
+
export function isLlmTurnDefinition<TParams = unknown, TState = unknown>(
|
|
28
|
+
turnDef: TurnDefinition<TParams, TState>,
|
|
29
|
+
): turnDef is LlmTurnDefinition<string, TParams, TState> {
|
|
30
|
+
return turnDef.kind === "llm";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function isAutomaticTurnDefinition<TParams = unknown, TState = unknown>(
|
|
34
|
+
turnDef: TurnDefinition<TParams, TState>,
|
|
35
|
+
): turnDef is AutomaticTurnDefinition<string, TParams, TState> {
|
|
36
|
+
return turnDef.kind === "automatic";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function isServerAutomaticTurnDefinition<TParams = unknown, TState = unknown>(
|
|
40
|
+
turnDef: TurnDefinition<TParams, TState>,
|
|
41
|
+
): turnDef is ServerAutomaticTurnDefinition<string, TParams, TState> {
|
|
42
|
+
return turnDef.kind === "server_automatic";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function isHumanTurnDefinition<TParams = unknown, TState = unknown>(
|
|
46
|
+
turnDef: TurnDefinition<TParams, TState>,
|
|
47
|
+
): turnDef is HumanTurnDefinition<TParams, TState> {
|
|
48
|
+
return turnDef.kind === "human";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function isExternalTurnDefinition<TParams = unknown, TState = unknown>(
|
|
52
|
+
turnDef: TurnDefinition<TParams, TState>,
|
|
53
|
+
): turnDef is ExternalTurnDefinition<TParams, TState> {
|
|
54
|
+
return turnDef.kind === "external";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function defaultTurnStartSelection(branchType: TurnBranchType): ProcessTurnStartFallback {
|
|
58
|
+
return defaultProcessTurnStartSelection(branchType);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function resolveLlmTurnStartSelection<TOutcome extends string = string>(
|
|
62
|
+
turnDef: Pick<LlmTurnDefinition<TOutcome>, "branchType" | "startFrom">,
|
|
63
|
+
): ProcessTurnStartSelection {
|
|
64
|
+
return resolveProcessTurnStartSelection(turnDef);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function resolveLlmTurnRestorePrimaryLeafAfterTurn<TOutcome extends string = string>(
|
|
68
|
+
turnDef: Pick<LlmTurnDefinition<TOutcome>, "branchType" | "restorePrimaryLeafAfterTurn">,
|
|
69
|
+
): boolean {
|
|
70
|
+
return turnDef.restorePrimaryLeafAfterTurn ?? turnDef.branchType !== "primary";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function validateProcessActionPreviewDefinition(
|
|
74
|
+
preview: ProcessActionPreviewDefinition | undefined,
|
|
75
|
+
context: string,
|
|
76
|
+
): string[] {
|
|
77
|
+
if (!preview) {
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
if (preview.kind === "trigger") {
|
|
81
|
+
return preview.trigger.trim() ? [] : [`${context} must declare a non-empty trigger preview`];
|
|
82
|
+
}
|
|
83
|
+
if (preview.kind === "terminal") {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
if (preview.turnId === null || preview.turnId.trim()) {
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
return [`${context} must declare a non-empty fixed turn id when turnId is not null`];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function validateProcessActionSchedulingDefinition(
|
|
93
|
+
scheduling: ProcessActionSchedulingDefinition | undefined,
|
|
94
|
+
context: string,
|
|
95
|
+
): string[] {
|
|
96
|
+
return validateProcessActionPreviewDefinition(scheduling?.preview, context);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function hasDuplicateBy<TItem>(
|
|
100
|
+
items: readonly TItem[],
|
|
101
|
+
selectKey: (item: TItem) => string,
|
|
102
|
+
): boolean {
|
|
103
|
+
const ids = new Set<string>();
|
|
104
|
+
for (const item of items) {
|
|
105
|
+
const key = selectKey(item);
|
|
106
|
+
if (ids.has(key)) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
ids.add(key);
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function validateOutcomeTurnResultContract<TParams = unknown, TState = unknown>(
|
|
115
|
+
turnKindLabel: string,
|
|
116
|
+
turnId: string,
|
|
117
|
+
outcomes: Partial<Record<string, ProcessToolOutcomeSpec<TParams, TState>>> | undefined,
|
|
118
|
+
turnEnd: unknown,
|
|
119
|
+
): string[] {
|
|
120
|
+
const errors: string[] = [];
|
|
121
|
+
const hasOutcomeTools = Object.keys(outcomes ?? {}).length > 0;
|
|
122
|
+
const hasTurnEndResult = turnEnd !== undefined;
|
|
123
|
+
if (!hasOutcomeTools && !hasTurnEndResult) {
|
|
124
|
+
errors.push(
|
|
125
|
+
`${turnKindLabel} turn '${turnId}' must declare at least one outcome tool or a turnEnd result`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
if (hasOutcomeTools && hasTurnEndResult) {
|
|
129
|
+
errors.push(`${turnKindLabel} turn '${turnId}' cannot declare both outcomes and turnEnd`);
|
|
130
|
+
}
|
|
131
|
+
return errors;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function isValidOutcomeToolArrayItemType(type: unknown): boolean {
|
|
135
|
+
return type === "string" || type === "number" || type === "boolean" || type === "object";
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function validateOutcomeToolParameters<TParams = unknown, TState = unknown>(
|
|
139
|
+
turnKindLabel: string,
|
|
140
|
+
turnId: string,
|
|
141
|
+
outcomes: Partial<Record<string, ProcessToolOutcomeSpec<TParams, TState>>> | undefined,
|
|
142
|
+
): string[] {
|
|
143
|
+
const errors: string[] = [];
|
|
144
|
+
for (const [outcome, outcomeSpec] of Object.entries(outcomes ?? {}) as Array<
|
|
145
|
+
[string, ProcessToolOutcomeSpec<TParams, TState>]
|
|
146
|
+
>) {
|
|
147
|
+
if (outcomeSpec.publishedProduct) {
|
|
148
|
+
try {
|
|
149
|
+
assertValidProcessProductName(outcomeSpec.publishedProduct);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
errors.push(error instanceof Error ? error.message : String(error));
|
|
152
|
+
}
|
|
153
|
+
const parameterName = outcomeSpec.turnResultMarkdownParameter?.trim() ?? "";
|
|
154
|
+
if (!parameterName) {
|
|
155
|
+
errors.push(
|
|
156
|
+
`${turnKindLabel} turn '${turnId}' outcome '${outcome}' publishes product '${outcomeSpec.publishedProduct}' but does not declare a turn-result markdown parameter`,
|
|
157
|
+
);
|
|
158
|
+
} else if (!Object.hasOwn(outcomeSpec.parameters, parameterName)) {
|
|
159
|
+
errors.push(
|
|
160
|
+
`${turnKindLabel} turn '${turnId}' outcome '${outcome}' publishes product '${outcomeSpec.publishedProduct}' from missing parameter '${parameterName}'`,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
for (const [paramName, paramSpec] of Object.entries(outcomeSpec.parameters) as Array<
|
|
165
|
+
[string, OutcomeToolParameterSpec]
|
|
166
|
+
>) {
|
|
167
|
+
if (paramSpec.minItems !== undefined && paramSpec.type !== "array") {
|
|
168
|
+
errors.push(
|
|
169
|
+
`${turnKindLabel} turn '${turnId}' outcome '${outcome}' parameter '${paramName}' uses minItems but is not an array`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
if (paramSpec.minimum !== undefined && paramSpec.type !== "number") {
|
|
173
|
+
errors.push(
|
|
174
|
+
`${turnKindLabel} turn '${turnId}' outcome '${outcome}' parameter '${paramName}' uses minimum but is not a number`,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
if (paramSpec.items !== undefined && paramSpec.type !== "array") {
|
|
178
|
+
errors.push(
|
|
179
|
+
`${turnKindLabel} turn '${turnId}' outcome '${outcome}' parameter '${paramName}' declares array items but is not an array`,
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
if (paramSpec.type === "array") {
|
|
183
|
+
if (!paramSpec.items) {
|
|
184
|
+
errors.push(
|
|
185
|
+
`${turnKindLabel} turn '${turnId}' outcome '${outcome}' parameter '${paramName}' is an array and must declare items`,
|
|
186
|
+
);
|
|
187
|
+
} else if (!isValidOutcomeToolArrayItemType(paramSpec.items.type)) {
|
|
188
|
+
errors.push(
|
|
189
|
+
`${turnKindLabel} turn '${turnId}' outcome '${outcome}' parameter '${paramName}' declares unsupported array item type '${String(paramSpec.items.type)}'`,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return errors;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function assertNoValidationErrors(errors: readonly string[]): void {
|
|
199
|
+
if (errors.length > 0) {
|
|
200
|
+
throw new Error(errors.join("; "));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function validateTurnStartSelection(
|
|
205
|
+
turnId: string,
|
|
206
|
+
startFrom: ProcessTurnStartSelection,
|
|
207
|
+
context = "startFrom",
|
|
208
|
+
): string[] {
|
|
209
|
+
const errors: string[] = [];
|
|
210
|
+
if (startFrom.kind === "entry" && startFrom.entryId.trim() === "") {
|
|
211
|
+
errors.push(`LLM turn '${turnId}' ${context} entry id must be non-empty`);
|
|
212
|
+
}
|
|
213
|
+
if (startFrom.kind === "product_ref") {
|
|
214
|
+
try {
|
|
215
|
+
assertValidProcessProductName(startFrom.productName);
|
|
216
|
+
} catch (error) {
|
|
217
|
+
errors.push(error instanceof Error ? error.message : String(error));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if ("fallback" in startFrom && startFrom.fallback) {
|
|
221
|
+
errors.push(...validateTurnStartSelection(turnId, startFrom.fallback, `${context} fallback`));
|
|
222
|
+
}
|
|
223
|
+
return errors;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function validateLlmTurnDefinition<
|
|
227
|
+
TOutcome extends string = string,
|
|
228
|
+
TParams = unknown,
|
|
229
|
+
TState = unknown,
|
|
230
|
+
>(turnId: string, turnDef: LlmTurnDefinition<TOutcome, TParams, TState>): string[] {
|
|
231
|
+
const errors: string[] = [];
|
|
232
|
+
const declaredOutcomes = turnDef.outcomes;
|
|
233
|
+
const startFrom = resolveLlmTurnStartSelection(turnDef);
|
|
234
|
+
|
|
235
|
+
if ((turnDef.completionMode ?? "turn_end") !== "turn_end") {
|
|
236
|
+
errors.push(`LLM turn '${turnId}' must use completionMode 'turn_end'`);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const declaredTurnEnd = turnDef.turnEnd;
|
|
240
|
+
errors.push(
|
|
241
|
+
...validateOutcomeTurnResultContract("LLM", turnId, declaredOutcomes, declaredTurnEnd),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
if (turnDef.branchType === "root_branch" && startFrom.kind === "current_leaf") {
|
|
245
|
+
errors.push(
|
|
246
|
+
`LLM turn '${turnId}' uses branchType 'root_branch' but starts from the current leaf`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (turnDef.branchType === "leaf_branch" && startFrom.kind === "session_root") {
|
|
251
|
+
errors.push(
|
|
252
|
+
`LLM turn '${turnId}' uses branchType 'leaf_branch' but starts from the session root`,
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
errors.push(...validateTurnStartSelection(turnId, startFrom));
|
|
257
|
+
|
|
258
|
+
if (turnDef.askQuestions !== undefined && turnDef.askQuestions !== true) {
|
|
259
|
+
errors.push(`LLM turn '${turnId}' askQuestions must be true when declared`);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (!Array.isArray(turnDef.availableTools)) {
|
|
263
|
+
errors.push(`LLM turn '${turnId}' must declare availableTools as an array`);
|
|
264
|
+
} else {
|
|
265
|
+
errors.push(
|
|
266
|
+
...validatePiBuiltInToolArray(turnDef.availableTools, `LLM turn '${turnId}' availableTools`),
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (turnDef.turnResultMarkdown?.mode === "tool_call") {
|
|
271
|
+
if (turnDef.turnResultMarkdown.toolName.trim() === "") {
|
|
272
|
+
errors.push(
|
|
273
|
+
`LLM turn '${turnId}' tool-call turnResultMarkdown must use a non-empty toolName`,
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
if (turnDef.turnResultMarkdown.path.trim() === "") {
|
|
277
|
+
errors.push(`LLM turn '${turnId}' tool-call turnResultMarkdown must use a non-empty path`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (turnDef.turnResultMarkdown?.mode === "outcome_tool_argument") {
|
|
281
|
+
const parameterName = turnDef.turnResultMarkdown.parameterName.trim();
|
|
282
|
+
const outcomeEntries = Object.entries(declaredOutcomes ?? {}) as Array<
|
|
283
|
+
[string, ProcessToolOutcomeSpec<TParams, TState>]
|
|
284
|
+
>;
|
|
285
|
+
if (parameterName === "") {
|
|
286
|
+
errors.push(
|
|
287
|
+
`LLM turn '${turnId}' outcome-tool turnResultMarkdown must use a non-empty parameterName`,
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
if (outcomeEntries.length === 0) {
|
|
291
|
+
errors.push(
|
|
292
|
+
`LLM turn '${turnId}' outcome-tool turnResultMarkdown requires at least one outcome tool`,
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
for (const [outcomeId, spec] of outcomeEntries) {
|
|
296
|
+
if (parameterName in spec.parameters) {
|
|
297
|
+
errors.push(
|
|
298
|
+
`LLM turn '${turnId}' outcome tool '${outcomeId}' cannot declare reserved markdown parameter '${parameterName}'`,
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
errors.push(...validateOutcomeToolParameters("LLM", turnId, declaredOutcomes));
|
|
305
|
+
|
|
306
|
+
return errors;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export function validateAutomaticTurnDefinition<
|
|
310
|
+
TOutcome extends string = string,
|
|
311
|
+
TParams = unknown,
|
|
312
|
+
TState = unknown,
|
|
313
|
+
>(
|
|
314
|
+
turnId: string,
|
|
315
|
+
turnDef:
|
|
316
|
+
| AutomaticTurnDefinition<TOutcome, TParams, TState>
|
|
317
|
+
| ServerAutomaticTurnDefinition<TOutcome, TParams, TState>,
|
|
318
|
+
): string[] {
|
|
319
|
+
const errors: string[] = [];
|
|
320
|
+
const declaredOutcomes = turnDef.outcomes;
|
|
321
|
+
const declaredTurnEnd = turnDef.turnEnd;
|
|
322
|
+
errors.push(
|
|
323
|
+
...validateOutcomeTurnResultContract("Automatic", turnId, declaredOutcomes, declaredTurnEnd),
|
|
324
|
+
);
|
|
325
|
+
errors.push(...validateOutcomeToolParameters("Automatic", turnId, declaredOutcomes));
|
|
326
|
+
|
|
327
|
+
return errors;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function validateHumanTurnDefinition<TParams = unknown, TState = unknown>(
|
|
331
|
+
turnId: string,
|
|
332
|
+
turnDef: HumanTurnDefinition<TParams, TState>,
|
|
333
|
+
): string[] {
|
|
334
|
+
const errors: string[] = [];
|
|
335
|
+
|
|
336
|
+
try {
|
|
337
|
+
resolveHumanTurnView({ turnId, turn: turnDef });
|
|
338
|
+
} catch (error: unknown) {
|
|
339
|
+
errors.push(error instanceof Error ? error.message : String(error));
|
|
340
|
+
}
|
|
341
|
+
if (turnDef.reviewProduct) {
|
|
342
|
+
try {
|
|
343
|
+
assertValidProcessProductName(turnDef.reviewProduct);
|
|
344
|
+
} catch (error) {
|
|
345
|
+
errors.push(error instanceof Error ? error.message : String(error));
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const actionEntries = Object.entries(turnDef.actions) as Array<
|
|
350
|
+
[string, HumanTurnDefinition<TParams, TState>["actions"][string]]
|
|
351
|
+
>;
|
|
352
|
+
if (actionEntries.length === 0) {
|
|
353
|
+
errors.push(`Human turn '${turnId}' must declare at least one action`);
|
|
354
|
+
}
|
|
355
|
+
for (const [actionId, action] of actionEntries) {
|
|
356
|
+
if (!action.acceptanceState) {
|
|
357
|
+
errors.push(`Human turn '${turnId}' is missing an acceptance state for action '${actionId}'`);
|
|
358
|
+
}
|
|
359
|
+
errors.push(
|
|
360
|
+
...validateProcessActionPreviewDefinition(
|
|
361
|
+
action.preview,
|
|
362
|
+
`Human turn '${turnId}' action '${actionId}' preview`,
|
|
363
|
+
),
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const externalTriggers = actionEntries.flatMap(([actionId, action]) =>
|
|
368
|
+
(action.externalTriggers ?? []).map((trigger) => ({ actionId, trigger })),
|
|
369
|
+
);
|
|
370
|
+
if (hasDuplicateBy(externalTriggers, ({ trigger }) => trigger.id)) {
|
|
371
|
+
errors.push(`Human turn '${turnId}' contains duplicate external trigger ids`);
|
|
372
|
+
}
|
|
373
|
+
if (hasDuplicateBy(externalTriggers, ({ actionId }) => actionId)) {
|
|
374
|
+
errors.push(`Human turn '${turnId}' contains duplicate external trigger action ids`);
|
|
375
|
+
}
|
|
376
|
+
for (const { actionId, trigger } of externalTriggers) {
|
|
377
|
+
if (trigger.id.trim() === "") {
|
|
378
|
+
errors.push(`Human turn '${turnId}' contains an external trigger with an empty id`);
|
|
379
|
+
}
|
|
380
|
+
if (actionId.trim() === "") {
|
|
381
|
+
errors.push(
|
|
382
|
+
`Human turn '${turnId}' external trigger '${trigger.id}' must declare a non-empty actionId`,
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
if (trigger.label.trim() === "") {
|
|
386
|
+
errors.push(
|
|
387
|
+
`Human turn '${turnId}' external trigger '${trigger.id}' must declare a non-empty label`,
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
if (trigger.description.trim() === "") {
|
|
391
|
+
errors.push(
|
|
392
|
+
`Human turn '${turnId}' external trigger '${trigger.id}' must declare a non-empty description`,
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const externalActionEntries = Object.entries(turnDef.externalActions ?? {});
|
|
398
|
+
if (hasDuplicateBy(externalActionEntries, ([externalActionId]) => externalActionId)) {
|
|
399
|
+
errors.push(`Human turn '${turnId}' contains duplicate external action ids`);
|
|
400
|
+
}
|
|
401
|
+
for (const [externalActionId, externalAction] of externalActionEntries) {
|
|
402
|
+
if (externalActionId.trim() === "") {
|
|
403
|
+
errors.push(`Human turn '${turnId}' contains an external action with an empty id`);
|
|
404
|
+
}
|
|
405
|
+
if (externalAction.id !== externalActionId) {
|
|
406
|
+
errors.push(
|
|
407
|
+
`Human turn '${turnId}' external action '${externalActionId}' has mismatched id '${externalAction.id}'`,
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
if (externalAction.source.kind.trim() === "") {
|
|
411
|
+
errors.push(
|
|
412
|
+
`Human turn '${turnId}' external action '${externalActionId}' must declare a non-empty source kind`,
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
const targetCount =
|
|
416
|
+
(externalAction.to !== undefined ? 1 : 0) +
|
|
417
|
+
(externalAction.complete === true ? 1 : 0) +
|
|
418
|
+
(externalAction.lifecycleStatus !== undefined ? 1 : 0);
|
|
419
|
+
if (targetCount !== 1) {
|
|
420
|
+
errors.push(
|
|
421
|
+
`Human turn '${turnId}' external action '${externalActionId}' must declare exactly one target`,
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
if (externalAction.publishInput) {
|
|
425
|
+
try {
|
|
426
|
+
assertValidProcessProductName(externalAction.publishInput.productName);
|
|
427
|
+
} catch (error) {
|
|
428
|
+
errors.push(error instanceof Error ? error.message : String(error));
|
|
429
|
+
}
|
|
430
|
+
if (externalAction.publishInput.inputField.trim() === "") {
|
|
431
|
+
errors.push(
|
|
432
|
+
`Human turn '${turnId}' external action '${externalActionId}' publishInput must declare a non-empty inputField`,
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
if (externalAction.complete === true || externalAction.lifecycleStatus !== undefined) {
|
|
436
|
+
errors.push(
|
|
437
|
+
`Human turn '${turnId}' external action '${externalActionId}' cannot publish input on a terminal route`,
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const notesFields = turnDef.notesFields ?? [];
|
|
444
|
+
if (hasDuplicateBy(notesFields, (field) => field.id)) {
|
|
445
|
+
const noteIds = new Set<string>();
|
|
446
|
+
for (const field of notesFields) {
|
|
447
|
+
if (noteIds.has(field.id)) {
|
|
448
|
+
errors.push(`Human turn '${turnId}' contains duplicate notes field id '${field.id}'`);
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
noteIds.add(field.id);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return errors;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export function validateExternalTurnDefinition<TParams = unknown, TState = unknown>(
|
|
459
|
+
turnId: string,
|
|
460
|
+
turnDef: ExternalTurnDefinition<TParams, TState>,
|
|
461
|
+
): string[] {
|
|
462
|
+
const errors: string[] = [];
|
|
463
|
+
if (turnDef.transitions.length === 0) {
|
|
464
|
+
errors.push(`External turn '${turnId}' must declare at least one source transition`);
|
|
465
|
+
}
|
|
466
|
+
for (const [index, transition] of turnDef.transitions.entries()) {
|
|
467
|
+
if (transition.source.kind.trim() === "") {
|
|
468
|
+
errors.push(
|
|
469
|
+
`External turn '${turnId}' source transition ${index} must declare a non-empty kind`,
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
const targetCount =
|
|
473
|
+
(transition.to !== undefined ? 1 : 0) +
|
|
474
|
+
(transition.complete === true ? 1 : 0) +
|
|
475
|
+
(transition.lifecycleStatus !== undefined ? 1 : 0);
|
|
476
|
+
if (targetCount !== 1) {
|
|
477
|
+
errors.push(
|
|
478
|
+
`External turn '${turnId}' source transition ${index} must declare exactly one target`,
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return errors;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
export function validateTurnDefinition<TParams = unknown, TState = unknown>(
|
|
486
|
+
turnId: string,
|
|
487
|
+
turnDef: TurnDefinition<TParams, TState>,
|
|
488
|
+
): string[];
|
|
489
|
+
export function validateTurnDefinition<TParams = unknown, TState = unknown>(
|
|
490
|
+
turnDef: TurnDefinition<TParams, TState>,
|
|
491
|
+
): string[];
|
|
492
|
+
export function validateTurnDefinition<TParams = unknown, TState = unknown>(
|
|
493
|
+
turnIdOrDef: string | TurnDefinition<TParams, TState>,
|
|
494
|
+
turnDef?: TurnDefinition<TParams, TState>,
|
|
495
|
+
): string[] {
|
|
496
|
+
const effectiveTurnDef = typeof turnIdOrDef === "string" ? turnDef : turnIdOrDef;
|
|
497
|
+
const effectiveTurnId =
|
|
498
|
+
typeof turnIdOrDef === "string"
|
|
499
|
+
? turnIdOrDef
|
|
500
|
+
: ((turnIdOrDef as { id?: string }).id ?? "unknown_turn");
|
|
501
|
+
if (!effectiveTurnDef) {
|
|
502
|
+
return ["Turn definition is required"];
|
|
503
|
+
}
|
|
504
|
+
return isLlmTurnDefinition(effectiveTurnDef)
|
|
505
|
+
? validateLlmTurnDefinition(effectiveTurnId, effectiveTurnDef)
|
|
506
|
+
: isAutomaticTurnDefinition(effectiveTurnDef) ||
|
|
507
|
+
isServerAutomaticTurnDefinition(effectiveTurnDef)
|
|
508
|
+
? validateAutomaticTurnDefinition(effectiveTurnId, effectiveTurnDef)
|
|
509
|
+
: isHumanTurnDefinition(effectiveTurnDef)
|
|
510
|
+
? validateHumanTurnDefinition(effectiveTurnId, effectiveTurnDef)
|
|
511
|
+
: validateExternalTurnDefinition(effectiveTurnId, effectiveTurnDef);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export function assertValidLlmTurnDefinition<
|
|
515
|
+
TOutcome extends string = string,
|
|
516
|
+
TParams = unknown,
|
|
517
|
+
TState = unknown,
|
|
518
|
+
>(turnId: string, turnDef: LlmTurnDefinition<TOutcome, TParams, TState>): void {
|
|
519
|
+
assertNoValidationErrors(validateLlmTurnDefinition(turnId, turnDef));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
export function createRootBranchReviewTurn<
|
|
523
|
+
TOutcome extends string,
|
|
524
|
+
TParams = unknown,
|
|
525
|
+
TState = unknown,
|
|
526
|
+
>(
|
|
527
|
+
def: Omit<
|
|
528
|
+
LlmTurnDefinition<TOutcome, TParams, TState>,
|
|
529
|
+
"kind" | "branchType" | "completionMode" | "startFrom" | "restorePrimaryLeafAfterTurn"
|
|
530
|
+
> &
|
|
531
|
+
Partial<
|
|
532
|
+
Pick<
|
|
533
|
+
LlmTurnDefinition<TOutcome, TParams, TState>,
|
|
534
|
+
"completionMode" | "startFrom" | "restorePrimaryLeafAfterTurn"
|
|
535
|
+
>
|
|
536
|
+
>,
|
|
537
|
+
): LlmTurnDefinition<TOutcome, TParams, TState> {
|
|
538
|
+
const {
|
|
539
|
+
completionMode = "turn_end",
|
|
540
|
+
startFrom = { kind: "session_root" },
|
|
541
|
+
restorePrimaryLeafAfterTurn = true,
|
|
542
|
+
turnResultMarkdown = REQUIRED_MARKDOWN_RESULT_TURN_RESULT,
|
|
543
|
+
...rest
|
|
544
|
+
} = def;
|
|
545
|
+
|
|
546
|
+
return llmTurn({
|
|
547
|
+
completionMode,
|
|
548
|
+
branchType: "root_branch",
|
|
549
|
+
startFrom,
|
|
550
|
+
restorePrimaryLeafAfterTurn,
|
|
551
|
+
turnResultMarkdown,
|
|
552
|
+
...rest,
|
|
553
|
+
});
|
|
554
|
+
}
|