@leitwerk-dev/process-sdk 0.3.1 → 0.4.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.
- package/dist/{chunk-GVBLXKSR.js → chunk-HT2AXXLH.js} +2 -1
- package/dist/chunk-HT2AXXLH.js.map +1 -0
- package/dist/core-capabilities.d.ts +63 -2
- package/dist/core-capabilities.d.ts.map +1 -1
- package/dist/define-process.d.ts +40 -26
- package/dist/define-process.d.ts.map +1 -1
- package/dist/{extension-api-DM-AvWOW.d.ts → extension-api-Ljc4UTZ0.d.ts} +258 -64
- package/dist/extension-api.d.ts +52 -0
- package/dist/extension-api.d.ts.map +1 -1
- package/dist/external-source-poll.d.ts +1 -1
- package/dist/external-source-poll.d.ts.map +1 -1
- package/dist/file-external-source.d.ts +32 -0
- package/dist/file-external-source.d.ts.map +1 -0
- package/dist/flow-mapped.typecheck.d.ts +2 -0
- package/dist/flow-mapped.typecheck.d.ts.map +1 -0
- package/dist/flow.d.ts +188 -177
- package/dist/flow.d.ts.map +1 -1
- package/dist/index.d.ts +11 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4387 -4313
- package/dist/index.js.map +1 -1
- package/dist/mapped-turn.d.ts +6 -22
- package/dist/mapped-turn.d.ts.map +1 -1
- package/dist/outcome-contract.d.ts +19 -0
- package/dist/outcome-contract.d.ts.map +1 -0
- package/dist/outcome-schema.d.ts +25 -0
- package/dist/outcome-schema.d.ts.map +1 -0
- package/dist/pi-config-public.js +1 -1
- package/dist/pi-config.js +1 -1
- package/dist/process-flow-view.d.ts +3 -14
- package/dist/process-flow-view.d.ts.map +1 -1
- package/dist/process-graph.d.ts +10 -4
- package/dist/process-graph.d.ts.map +1 -1
- package/dist/scoped-settings.d.ts +87 -0
- package/dist/scoped-settings.d.ts.map +1 -0
- package/dist/ticket-creation.d.ts +24 -0
- package/dist/ticket-creation.d.ts.map +1 -0
- package/dist/turn-semantics.d.ts +3 -3
- package/dist/turn-semantics.d.ts.map +1 -1
- package/dist/turn-wait.d.ts +53 -0
- package/dist/turn-wait.d.ts.map +1 -0
- package/dist/types.d.ts +31 -18
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/core-capabilities.ts +87 -1
- package/src/define-process.ts +420 -596
- package/src/extension-api.ts +60 -0
- package/src/external-source-poll.ts +6 -24
- package/src/file-external-source.ts +135 -0
- package/src/flow-mapped.typecheck.ts +136 -0
- package/src/flow.ts +746 -815
- package/src/index.ts +48 -1
- package/src/mapped-turn.ts +74 -23
- package/src/outcome-contract.ts +119 -0
- package/src/outcome-schema.ts +315 -0
- package/src/process-flow-view.ts +84 -346
- package/src/process-graph.ts +95 -45
- package/src/scoped-settings.ts +113 -0
- package/src/ticket-creation.ts +113 -0
- package/src/turn-semantics.ts +85 -193
- package/src/turn-wait.ts +76 -0
- package/src/types.ts +30 -16
- package/dist/chunk-GVBLXKSR.js.map +0 -1
package/src/define-process.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
type ProcessTurnTransition,
|
|
10
|
-
type TurnId,
|
|
11
|
-
type TurnProgressReport,
|
|
1
|
+
import type {
|
|
2
|
+
ProcessInstance,
|
|
3
|
+
ProcessSemanticEntryRefKey,
|
|
4
|
+
ProcessTurnStartSelection,
|
|
5
|
+
ProcessTurnTerminalLifecycleStatus,
|
|
6
|
+
ProcessTurnTransition,
|
|
7
|
+
TurnId,
|
|
8
|
+
TurnProgressReport,
|
|
12
9
|
} from "@leitwerk-dev/domain";
|
|
13
10
|
import type {
|
|
14
11
|
ExtensionProcessDefinition,
|
|
@@ -29,10 +26,17 @@ import {
|
|
|
29
26
|
markDefinedProcess,
|
|
30
27
|
setProcessTurnTransitions,
|
|
31
28
|
} from "./process-definition-internals.js";
|
|
29
|
+
import {
|
|
30
|
+
type ProcessGraphSource,
|
|
31
|
+
toProcessGraphView,
|
|
32
|
+
validateProcessGraphProducts,
|
|
33
|
+
validateProcessGraphTurnTransitions,
|
|
34
|
+
} from "./process-graph.js";
|
|
32
35
|
import type { ServerExtensionEventMap } from "./server-events.js";
|
|
36
|
+
import { validateExternalActions, validateTurnDefinition } from "./turn-semantics.js";
|
|
37
|
+
import type { TurnWaitPredicate } from "./turn-wait.js";
|
|
33
38
|
import type {
|
|
34
39
|
HumanTurnActionView,
|
|
35
|
-
HumanTurnExternalActionView,
|
|
36
40
|
HumanTurnExternalTrigger,
|
|
37
41
|
HumanTurnNotesField,
|
|
38
42
|
OutcomeToolParameterSpec,
|
|
@@ -86,6 +90,14 @@ type NormalizedActionRoute = NormalizedRouteTarget & {
|
|
|
86
90
|
export interface ProcessRuntimeTurnContext<TParams = unknown, TState = unknown>
|
|
87
91
|
extends WorkerProcessContext<TParams, TState> {}
|
|
88
92
|
|
|
93
|
+
/** An outcome-tool submission awaiting acceptance. @public */
|
|
94
|
+
export interface LlmOutcomeSubmission {
|
|
95
|
+
/** @public */
|
|
96
|
+
outcome: string;
|
|
97
|
+
/** @public */
|
|
98
|
+
params: Record<string, unknown>;
|
|
99
|
+
}
|
|
100
|
+
|
|
89
101
|
/** @internal */
|
|
90
102
|
export interface LlmTurnPreparationContext<TParams = unknown, TState = unknown>
|
|
91
103
|
extends ProcessRuntimeTurnContext<TParams, TState> {
|
|
@@ -154,30 +166,6 @@ export interface ProcessEffectPlan<TState = unknown> extends ProcessLifecycleEff
|
|
|
154
166
|
queueInput?: readonly ProcessQueuedInput[];
|
|
155
167
|
}
|
|
156
168
|
|
|
157
|
-
/** @internal */
|
|
158
|
-
export interface SavePlanResultLifecycleIntent<TParams = unknown, TState = unknown> {
|
|
159
|
-
/** @internal */
|
|
160
|
-
kind: "save_plan_result";
|
|
161
|
-
/** @internal */
|
|
162
|
-
summaryParam: string;
|
|
163
|
-
/** @internal */
|
|
164
|
-
acceptanceCriteriaParam: string;
|
|
165
|
-
/** @internal */
|
|
166
|
-
planMarkdownParam: string;
|
|
167
|
-
/** @internal */
|
|
168
|
-
emitEventType: ProcessEventName;
|
|
169
|
-
/** @internal */
|
|
170
|
-
broadcastType: string;
|
|
171
|
-
/** @internal */
|
|
172
|
-
state?: ProcessOutcomeEffect<TParams, TState>;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/** @internal */
|
|
176
|
-
export type ProcessOutcomeLifecycleIntent<
|
|
177
|
-
TParams = unknown,
|
|
178
|
-
TState = unknown,
|
|
179
|
-
> = SavePlanResultLifecycleIntent<TParams, TState>;
|
|
180
|
-
|
|
181
169
|
/** @public */
|
|
182
170
|
export interface ProcessActionExecution<TParams = unknown, TState = unknown> {
|
|
183
171
|
/** @public */
|
|
@@ -224,10 +212,14 @@ export type ProcessOutcomeBranchSelector<TParams = unknown, TState = unknown> =
|
|
|
224
212
|
input: ProcessOutcomeExecution<TParams, TState>,
|
|
225
213
|
) => MaybePromise<string>;
|
|
226
214
|
|
|
227
|
-
/** @
|
|
215
|
+
/** Branch route and display metadata for state-routed outcomes. @public */
|
|
228
216
|
export interface ProcessOutcomeBranchSpec extends StaticRouteTarget {
|
|
229
217
|
/** @internal */
|
|
230
218
|
trigger?: string;
|
|
219
|
+
/** Display name for the branch condition in the workflow graph. @public */
|
|
220
|
+
label?: string;
|
|
221
|
+
/** Explains the condition without evaluating runtime state. @public */
|
|
222
|
+
description?: string;
|
|
231
223
|
}
|
|
232
224
|
|
|
233
225
|
/** @internal */
|
|
@@ -318,8 +310,22 @@ export type ProcessHumanTurnActionSpec<TParams = unknown, TState = unknown> = Ba
|
|
|
318
310
|
externalTriggers?: readonly ProcessActionExternalTrigger[];
|
|
319
311
|
} & (StaticActionRouting | BranchingActionRouting<TParams, TState>);
|
|
320
312
|
|
|
313
|
+
/** Pure inputs for resolving a tool contract before worker preparation. @public */
|
|
314
|
+
export interface OutcomeParameterContext<TParams = unknown, TState = unknown> {
|
|
315
|
+
/** @public */
|
|
316
|
+
readonly params: TParams;
|
|
317
|
+
/** @public */
|
|
318
|
+
readonly state: TState;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
321
|
/** @public */
|
|
322
322
|
interface ProcessToolOutcomeBaseSpec<TParams = unknown, TState = unknown> {
|
|
323
|
+
/** Resolve parameters synchronously from captured process data. @public */
|
|
324
|
+
resolveParameters?: (
|
|
325
|
+
ctx: OutcomeParameterContext<TParams, TState>,
|
|
326
|
+
) => Record<string, OutcomeToolParameterSpec>;
|
|
327
|
+
/** Opt in to provider schema enforcement. @public */
|
|
328
|
+
strictSchema?: boolean;
|
|
323
329
|
/** @internal */
|
|
324
330
|
description: string;
|
|
325
331
|
/** @internal */
|
|
@@ -332,8 +338,6 @@ interface ProcessToolOutcomeBaseSpec<TParams = unknown, TState = unknown> {
|
|
|
332
338
|
resultSummaryParameter?: string;
|
|
333
339
|
/** @internal */
|
|
334
340
|
effect?: ProcessOutcomeEffect<TParams, TState>;
|
|
335
|
-
/** @internal */
|
|
336
|
-
lifecycleIntent?: ProcessOutcomeLifecycleIntent<TParams, TState>;
|
|
337
341
|
}
|
|
338
342
|
|
|
339
343
|
/** @public */
|
|
@@ -412,8 +416,14 @@ export interface LlmTurnDefinition<
|
|
|
412
416
|
kind: "llm";
|
|
413
417
|
/** @public */
|
|
414
418
|
description: string;
|
|
419
|
+
/** Server-side entry condition, evaluated before worker allocation. @public */
|
|
420
|
+
waitFor?: TurnWaitPredicate<TParams, TState>;
|
|
421
|
+
/** External events armed while this LLM turn is selected and waiting. @public */
|
|
422
|
+
externalActions?: Record<string, ProcessHumanTurnExternalActionSpec<TParams, TState>>;
|
|
415
423
|
/** Code-defined model policy purpose. Purpose selections cannot be overridden per launch/action. @internal */
|
|
416
424
|
modelPurpose?: LlmModelPurpose;
|
|
425
|
+
/** Extension-defined scoped settings consumed by this turn. @public */
|
|
426
|
+
executionPurpose?: string;
|
|
417
427
|
/** Built-in Pi tools active while this turn runs. @public */
|
|
418
428
|
availableTools: readonly PiBuiltInToolName[];
|
|
419
429
|
/** Server-owned integration tools proxied over authenticated worker IPC. @internal */
|
|
@@ -422,8 +432,15 @@ export interface LlmTurnDefinition<
|
|
|
422
432
|
resolveIntegrationTools?: (params: TParams, state: TState) => readonly string[];
|
|
423
433
|
/** Opt in to the durable, operator-facing ask_questions custom tool. @internal */
|
|
424
434
|
askQuestions?: boolean;
|
|
435
|
+
/** Constrain questions using captured process data and completed preparation before prompting. @public */
|
|
436
|
+
resolveAskQuestions?: (ctx: ProcessRuntimeTurnContext<TParams, TState>) => boolean;
|
|
425
437
|
/** Deterministic worker preparation that must complete before Pi is prompted. @internal */
|
|
426
438
|
prepare?(ctx: LlmTurnPreparationContext<TParams, TState>): MaybePromise<unknown>;
|
|
439
|
+
/** Return actionable errors before an outcome is accepted; no durable effects. @internal */
|
|
440
|
+
validateOutcome?(
|
|
441
|
+
ctx: LlmTurnPreparationContext<TParams, TState>,
|
|
442
|
+
outcome: LlmOutcomeSubmission,
|
|
443
|
+
): MaybePromise<readonly string[]>;
|
|
427
444
|
/** @public */
|
|
428
445
|
completionMode?: TurnCompletionMode;
|
|
429
446
|
/** @public */
|
|
@@ -470,6 +487,8 @@ export interface AutomaticTurnDefinition<
|
|
|
470
487
|
kind: "automatic";
|
|
471
488
|
/** @public */
|
|
472
489
|
description: string;
|
|
490
|
+
/** Server-side entry condition, evaluated before worker allocation. @public */
|
|
491
|
+
waitFor?: TurnWaitPredicate<TParams, TState>;
|
|
473
492
|
/** Server-owned integration tools callable by this deterministic worker turn. @internal */
|
|
474
493
|
integrationTools?: readonly string[];
|
|
475
494
|
/** External events armed while this automatic turn is selected and waiting. @public */
|
|
@@ -626,58 +645,26 @@ type BranchRouteSpec = StaticRouteTarget & { trigger?: string };
|
|
|
626
645
|
function compileBranchRoutes(input: {
|
|
627
646
|
branches: Record<string, BranchRouteSpec>;
|
|
628
647
|
knownTurnIds?: ReadonlySet<TurnId>;
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
routes: Record<string, NormalizedActionRoute>;
|
|
634
|
-
transitions: NormalizedActionRoute[];
|
|
635
|
-
} {
|
|
636
|
-
const routes: Record<string, NormalizedActionRoute> = {};
|
|
637
|
-
const transitions: NormalizedActionRoute[] = [];
|
|
648
|
+
context: string;
|
|
649
|
+
}): Record<string, NormalizedActionRoute> {
|
|
650
|
+
const entries = Object.entries(input.branches);
|
|
651
|
+
if (entries.length === 0) throw new Error(`${input.context} must declare at least one branch`);
|
|
638
652
|
const usedTriggers = new Set<string>();
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
function resolveOutcomeRouting<TParams, TState>(input: {
|
|
657
|
-
turnId: TurnId;
|
|
658
|
-
outcome: string;
|
|
659
|
-
spec: ProcessToolOutcomeSpec<TParams, TState>;
|
|
660
|
-
knownTurnIds: ReadonlySet<TurnId>;
|
|
661
|
-
}): CompiledOutcomeRouting<TParams, TState> | null {
|
|
662
|
-
if (!("branches" in input.spec)) {
|
|
663
|
-
return null;
|
|
664
|
-
}
|
|
665
|
-
const entries = Object.entries(input.spec.branches);
|
|
666
|
-
if (entries.length === 0) {
|
|
667
|
-
throw new Error(
|
|
668
|
-
`Turn '${input.turnId}' outcome '${input.outcome}' must declare at least one branch`,
|
|
669
|
-
);
|
|
670
|
-
}
|
|
671
|
-
const { routes } = compileBranchRoutes({
|
|
672
|
-
branches: input.spec.branches,
|
|
673
|
-
knownTurnIds: input.knownTurnIds,
|
|
674
|
-
targetContext: (branchId) =>
|
|
675
|
-
`Turn '${input.turnId}' outcome '${input.outcome}' branch '${branchId}'`,
|
|
676
|
-
emptyBranchError: `Turn '${input.turnId}' outcome '${input.outcome}' contains an empty branch id`,
|
|
677
|
-
duplicateTriggerError: (trigger) =>
|
|
678
|
-
`Turn '${input.turnId}' outcome '${input.outcome}' contains duplicate branch trigger '${trigger}'`,
|
|
679
|
-
});
|
|
680
|
-
return { routes, choose: input.spec.choose };
|
|
653
|
+
return Object.fromEntries(
|
|
654
|
+
compileDeclarations(entries, ([branchId, branchSpec]) => {
|
|
655
|
+
if (branchId.trim() === "") throw new Error(`${input.context} contains an empty branch id`);
|
|
656
|
+
const target = normalizeStaticRouteTarget(
|
|
657
|
+
`${input.context} branch '${branchId}'`,
|
|
658
|
+
branchSpec,
|
|
659
|
+
input.knownTurnIds,
|
|
660
|
+
);
|
|
661
|
+
const trigger = branchSpec.trigger ?? branchId;
|
|
662
|
+
if (usedTriggers.has(trigger))
|
|
663
|
+
throw new Error(`${input.context} contains duplicate branch trigger '${trigger}'`);
|
|
664
|
+
usedTriggers.add(trigger);
|
|
665
|
+
return [branchId, { ...target, trigger }] as const;
|
|
666
|
+
}),
|
|
667
|
+
);
|
|
681
668
|
}
|
|
682
669
|
|
|
683
670
|
function resolveActionRouting<TParams, TState>(input: {
|
|
@@ -690,20 +677,10 @@ function resolveActionRouting<TParams, TState>(input: {
|
|
|
690
677
|
transitions: readonly NormalizedActionRoute[];
|
|
691
678
|
} {
|
|
692
679
|
if ("branches" in input.spec) {
|
|
693
|
-
const
|
|
694
|
-
if (entries.length === 0) {
|
|
695
|
-
throw new Error(
|
|
696
|
-
`Action '${input.actionId}' on turn '${input.turnId}' must declare at least one branch`,
|
|
697
|
-
);
|
|
698
|
-
}
|
|
699
|
-
const { routes, transitions } = compileBranchRoutes({
|
|
680
|
+
const routes = compileBranchRoutes({
|
|
700
681
|
branches: input.spec.branches,
|
|
701
682
|
knownTurnIds: input.knownTurnIds,
|
|
702
|
-
|
|
703
|
-
`Action '${input.actionId}' branch '${branchId}' on turn '${input.turnId}'`,
|
|
704
|
-
emptyBranchError: `Action '${input.actionId}' on turn '${input.turnId}' contains an empty branch id`,
|
|
705
|
-
duplicateTriggerError: (trigger) =>
|
|
706
|
-
`Action '${input.actionId}' on turn '${input.turnId}' contains duplicate branch trigger '${trigger}'`,
|
|
683
|
+
context: `Action '${input.actionId}' on turn '${input.turnId}'`,
|
|
707
684
|
});
|
|
708
685
|
return {
|
|
709
686
|
routing: {
|
|
@@ -711,7 +688,7 @@ function resolveActionRouting<TParams, TState>(input: {
|
|
|
711
688
|
routes,
|
|
712
689
|
choose: input.spec.choose,
|
|
713
690
|
},
|
|
714
|
-
transitions,
|
|
691
|
+
transitions: Object.values(routes),
|
|
715
692
|
};
|
|
716
693
|
}
|
|
717
694
|
const target = normalizeStaticRouteTarget(
|
|
@@ -719,11 +696,7 @@ function resolveActionRouting<TParams, TState>(input: {
|
|
|
719
696
|
input.spec,
|
|
720
697
|
input.knownTurnIds,
|
|
721
698
|
);
|
|
722
|
-
const route = {
|
|
723
|
-
...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
|
|
724
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
725
|
-
trigger: input.spec.trigger ?? input.actionId,
|
|
726
|
-
};
|
|
699
|
+
const route = { ...target, trigger: input.spec.trigger ?? input.actionId };
|
|
727
700
|
return {
|
|
728
701
|
routing: {
|
|
729
702
|
kind: "static",
|
|
@@ -768,23 +741,14 @@ function compileExternalSourceTransitions<TParams, TState>(input: {
|
|
|
768
741
|
spec: ExternalTurnDefinition<TParams, TState>;
|
|
769
742
|
knownTurnIds: ReadonlySet<TurnId>;
|
|
770
743
|
}): readonly ProcessTurnTransition[] {
|
|
771
|
-
|
|
772
|
-
throw new Error(`External turn '${input.turnId}' must declare at least one source transition`);
|
|
773
|
-
}
|
|
774
|
-
return input.spec.transitions.map((transition, index) => {
|
|
775
|
-
if (transition.source.kind.trim() === "") {
|
|
776
|
-
throw new Error(
|
|
777
|
-
`External turn '${input.turnId}' source transition ${index} has an empty kind`,
|
|
778
|
-
);
|
|
779
|
-
}
|
|
744
|
+
return compileDeclarations(input.spec.transitions, (transition, index) => {
|
|
780
745
|
const target = normalizeStaticRouteTarget(
|
|
781
746
|
`External turn '${input.turnId}' source '${transition.source.kind}'`,
|
|
782
747
|
transition,
|
|
783
748
|
input.knownTurnIds,
|
|
784
749
|
);
|
|
785
750
|
return {
|
|
786
|
-
...
|
|
787
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
751
|
+
...target,
|
|
788
752
|
trigger: getExternalSourceTransitionId({
|
|
789
753
|
turnId: input.turnId,
|
|
790
754
|
source: transition.source,
|
|
@@ -840,44 +804,21 @@ function buildActionPreviewAndScheduling(
|
|
|
840
804
|
preview?: ProcessActionPreviewDefinition;
|
|
841
805
|
scheduling?: ProcessActionSchedulingDefinition;
|
|
842
806
|
} {
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
return undefined;
|
|
853
|
-
}
|
|
854
|
-
if (transition.lifecycleStatus !== undefined) {
|
|
855
|
-
return {
|
|
856
|
-
kind: "terminal",
|
|
857
|
-
lifecycleStatus: transition.lifecycleStatus,
|
|
858
|
-
} satisfies ProcessActionPreviewDefinition;
|
|
859
|
-
}
|
|
860
|
-
return {
|
|
861
|
-
kind: "trigger",
|
|
862
|
-
trigger: transition.trigger,
|
|
863
|
-
} satisfies ProcessActionPreviewDefinition;
|
|
864
|
-
})();
|
|
865
|
-
if (spec.preview && preview) {
|
|
807
|
+
let preview = spec.preview;
|
|
808
|
+
const [transition] = transitions;
|
|
809
|
+
if (!preview && transitions.length === 1 && transition) {
|
|
810
|
+
preview =
|
|
811
|
+
transition.lifecycleStatus !== undefined
|
|
812
|
+
? { kind: "terminal", lifecycleStatus: transition.lifecycleStatus }
|
|
813
|
+
: { kind: "trigger", trigger: transition.trigger };
|
|
814
|
+
}
|
|
815
|
+
if (preview && (spec.preview || spec.schedulable)) {
|
|
866
816
|
validateActionPreviewMatchesTransitions({
|
|
867
817
|
turnId,
|
|
868
818
|
actionId,
|
|
869
819
|
preview,
|
|
870
820
|
transitions,
|
|
871
|
-
context: "preview",
|
|
872
|
-
});
|
|
873
|
-
}
|
|
874
|
-
if (spec.schedulable && preview) {
|
|
875
|
-
validateActionPreviewMatchesTransitions({
|
|
876
|
-
turnId,
|
|
877
|
-
actionId,
|
|
878
|
-
preview,
|
|
879
|
-
transitions,
|
|
880
|
-
context: "scheduling",
|
|
821
|
+
context: spec.preview ? "preview" : "scheduling",
|
|
881
822
|
});
|
|
882
823
|
}
|
|
883
824
|
if (spec.schedulable && !preview) {
|
|
@@ -891,93 +832,17 @@ function buildActionPreviewAndScheduling(
|
|
|
891
832
|
};
|
|
892
833
|
}
|
|
893
834
|
|
|
894
|
-
function
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
): ProcessEffectPlan<TState>
|
|
898
|
-
if (!left) {
|
|
899
|
-
return right;
|
|
900
|
-
}
|
|
901
|
-
if (!right) {
|
|
902
|
-
return left;
|
|
903
|
-
}
|
|
835
|
+
function withFormState<TState>(
|
|
836
|
+
effect: ProcessEffectPlan<TState> | undefined,
|
|
837
|
+
state: TState,
|
|
838
|
+
): ProcessEffectPlan<TState> {
|
|
904
839
|
return {
|
|
905
|
-
...
|
|
906
|
-
|
|
907
|
-
...(
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
...(
|
|
911
|
-
? { broadcasts: [...(left.broadcasts ?? []), ...(right.broadcasts ?? [])] }
|
|
912
|
-
: {}),
|
|
913
|
-
...(left.emit || right.emit ? { emit: [...(left.emit ?? []), ...(right.emit ?? [])] } : {}),
|
|
914
|
-
...(left.queueInput || right.queueInput
|
|
915
|
-
? { queueInput: [...(left.queueInput ?? []), ...(right.queueInput ?? [])] }
|
|
916
|
-
: {}),
|
|
917
|
-
};
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
async function buildSavePlanResultEffect<TParams, TState>(input: {
|
|
921
|
-
intent: SavePlanResultLifecycleIntent<TParams, TState>;
|
|
922
|
-
execution: ProcessOutcomeExecution<TParams, TState>;
|
|
923
|
-
}): Promise<ProcessEffectPlan<TState>> {
|
|
924
|
-
const statePlan = input.intent.state ? await input.intent.state(input.execution) : undefined;
|
|
925
|
-
const planRevision = input.execution.ctx.process.planRevision + 1;
|
|
926
|
-
const summary = normalizeEffectString(input.execution.event.params[input.intent.summaryParam]);
|
|
927
|
-
const acceptanceCriteria = normalizeEffectStringArray(
|
|
928
|
-
input.execution.event.params[input.intent.acceptanceCriteriaParam],
|
|
929
|
-
);
|
|
930
|
-
const planMarkdown =
|
|
931
|
-
typeof input.execution.event.turnResultMarkdown === "string"
|
|
932
|
-
? input.execution.event.turnResultMarkdown
|
|
933
|
-
: normalizeEffectString(input.execution.event.params[input.intent.planMarkdownParam]);
|
|
934
|
-
|
|
935
|
-
return mergeProcessEffectPlans(statePlan, {
|
|
936
|
-
processPatch: { planRevision },
|
|
937
|
-
broadcasts: [
|
|
938
|
-
{
|
|
939
|
-
type: input.intent.broadcastType,
|
|
940
|
-
payload: {
|
|
941
|
-
planRevision,
|
|
942
|
-
reviewState: "awaiting_approval",
|
|
943
|
-
approved: false,
|
|
944
|
-
summary,
|
|
945
|
-
},
|
|
946
|
-
},
|
|
947
|
-
],
|
|
948
|
-
emit: [
|
|
949
|
-
{
|
|
950
|
-
type: input.intent.emitEventType,
|
|
951
|
-
data: {
|
|
952
|
-
planRevision,
|
|
953
|
-
summary,
|
|
954
|
-
planMarkdown,
|
|
955
|
-
acceptanceCriteria,
|
|
956
|
-
},
|
|
957
|
-
},
|
|
958
|
-
],
|
|
959
|
-
}) as ProcessEffectPlan<TState>;
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
function resolveOutcomeEffect<TParams, TState>(input: {
|
|
963
|
-
spec: ProcessToolOutcomeSpec<TParams, TState> | ProcessTurnEndSpec<TParams, TState, string>;
|
|
964
|
-
}): ProcessOutcomeEffect<TParams, TState> | undefined {
|
|
965
|
-
const lifecycleIntent = "lifecycleIntent" in input.spec ? input.spec.lifecycleIntent : undefined;
|
|
966
|
-
if (!lifecycleIntent) {
|
|
967
|
-
return input.spec.effect;
|
|
968
|
-
}
|
|
969
|
-
return async (execution) => {
|
|
970
|
-
const explicitPlan = input.spec.effect ? await input.spec.effect(execution) : undefined;
|
|
971
|
-
let lifecyclePlan: ProcessEffectPlan<TState> | undefined;
|
|
972
|
-
switch (lifecycleIntent.kind) {
|
|
973
|
-
case "save_plan_result":
|
|
974
|
-
lifecyclePlan = await buildSavePlanResultEffect({
|
|
975
|
-
intent: lifecycleIntent,
|
|
976
|
-
execution,
|
|
977
|
-
});
|
|
978
|
-
break;
|
|
979
|
-
}
|
|
980
|
-
return mergeProcessEffectPlans(explicitPlan, lifecyclePlan);
|
|
840
|
+
...effect,
|
|
841
|
+
state,
|
|
842
|
+
...(effect?.processPatch ? { processPatch: { ...effect.processPatch } } : {}),
|
|
843
|
+
...(effect?.broadcasts ? { broadcasts: [...effect.broadcasts] } : {}),
|
|
844
|
+
...(effect?.emit ? { emit: [...effect.emit] } : {}),
|
|
845
|
+
...(effect?.queueInput ? { queueInput: [...effect.queueInput] } : {}),
|
|
981
846
|
};
|
|
982
847
|
}
|
|
983
848
|
|
|
@@ -1122,52 +987,30 @@ function compileTurnOutcomeDefinitions<TParams, TState>(input: {
|
|
|
1122
987
|
const outcomeEntries = Object.entries(input.outcomes ?? {}) as Array<
|
|
1123
988
|
[string, ProcessToolOutcomeSpec<TParams, TState>]
|
|
1124
989
|
>;
|
|
1125
|
-
if (outcomeEntries.length > 0 && input.turnEnd) {
|
|
1126
|
-
throw new Error(
|
|
1127
|
-
`Turn '${input.turnId}' cannot declare both tool outcomes and a turnEnd result`,
|
|
1128
|
-
);
|
|
1129
|
-
}
|
|
1130
|
-
if (outcomeEntries.length === 0 && !input.turnEnd) {
|
|
1131
|
-
throw new Error(
|
|
1132
|
-
`Turn '${input.turnId}' must declare at least one tool outcome or a turnEnd result`,
|
|
1133
|
-
);
|
|
1134
|
-
}
|
|
1135
990
|
|
|
1136
991
|
const transitions: ProcessTurnTransition[] = [];
|
|
1137
992
|
const effects = new Map<string, ProcessOutcomeEffect<TParams, TState> | undefined>();
|
|
1138
993
|
const routings = new Map<string, CompiledOutcomeRouting<TParams, TState>>();
|
|
1139
|
-
|
|
994
|
+
compileDeclarations(outcomeEntries, ([outcome, spec]) => {
|
|
1140
995
|
if (outcome.trim() === "") {
|
|
1141
996
|
throw new Error(`Turn '${input.turnId}' declares an empty outcome id`);
|
|
1142
997
|
}
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
});
|
|
1149
|
-
const target = routing
|
|
1150
|
-
? null
|
|
1151
|
-
: buildOutcomeTransition(input.turnId, outcome, spec, input.knownTurnIds);
|
|
1152
|
-
if (routing) {
|
|
1153
|
-
routings.set(outcome, routing);
|
|
1154
|
-
for (const route of Object.values(routing.routes)) {
|
|
1155
|
-
transitions.push({
|
|
1156
|
-
...(route.nextTurnId ? { nextTurnId: route.nextTurnId } : {}),
|
|
1157
|
-
...(route.lifecycleStatus ? { lifecycleStatus: route.lifecycleStatus } : {}),
|
|
1158
|
-
trigger: route.trigger,
|
|
1159
|
-
outcome,
|
|
1160
|
-
});
|
|
1161
|
-
}
|
|
1162
|
-
} else if (target) {
|
|
1163
|
-
transitions.push({
|
|
1164
|
-
...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
|
|
1165
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
1166
|
-
outcome,
|
|
998
|
+
if ("branches" in spec) {
|
|
999
|
+
const routes = compileBranchRoutes({
|
|
1000
|
+
branches: spec.branches,
|
|
1001
|
+
knownTurnIds: input.knownTurnIds,
|
|
1002
|
+
context: `Turn '${input.turnId}' outcome '${outcome}'`,
|
|
1167
1003
|
});
|
|
1004
|
+
routings.set(outcome, { routes, choose: spec.choose });
|
|
1005
|
+
for (const route of Object.values(routes)) {
|
|
1006
|
+
transitions.push({ ...route, outcome });
|
|
1007
|
+
}
|
|
1008
|
+
} else {
|
|
1009
|
+
const target = buildOutcomeTransition(input.turnId, outcome, spec, input.knownTurnIds);
|
|
1010
|
+
transitions.push({ ...target, outcome });
|
|
1168
1011
|
}
|
|
1169
|
-
effects.set(outcome,
|
|
1170
|
-
}
|
|
1012
|
+
effects.set(outcome, spec.effect);
|
|
1013
|
+
});
|
|
1171
1014
|
|
|
1172
1015
|
if (input.turnEnd) {
|
|
1173
1016
|
if (input.turnEnd.outcome.trim() === "") {
|
|
@@ -1179,12 +1022,8 @@ function compileTurnOutcomeDefinitions<TParams, TState>(input: {
|
|
|
1179
1022
|
input.turnEnd,
|
|
1180
1023
|
input.knownTurnIds,
|
|
1181
1024
|
);
|
|
1182
|
-
transitions.push({
|
|
1183
|
-
|
|
1184
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
1185
|
-
outcome: input.turnEnd.outcome,
|
|
1186
|
-
});
|
|
1187
|
-
effects.set(input.turnEnd.outcome, resolveOutcomeEffect({ spec: input.turnEnd }));
|
|
1025
|
+
transitions.push({ ...target, outcome: input.turnEnd.outcome });
|
|
1026
|
+
effects.set(input.turnEnd.outcome, input.turnEnd.effect);
|
|
1188
1027
|
}
|
|
1189
1028
|
|
|
1190
1029
|
return {
|
|
@@ -1201,52 +1040,16 @@ function compileMappedTurnTransitions<TParams, TState>(input: {
|
|
|
1201
1040
|
}): readonly ProcessTurnTransition[] {
|
|
1202
1041
|
const mapped = input.spec.forEach;
|
|
1203
1042
|
if (!mapped) return [];
|
|
1204
|
-
const outcomes = Object.entries(input.spec.outcomes ?? {}) as Array<
|
|
1205
|
-
[string, ProcessToolOutcomeSpec<TParams, TState>]
|
|
1206
|
-
>;
|
|
1207
|
-
if (outcomes.length === 0) {
|
|
1208
|
-
throw new Error(`Mapped turn '${input.turnId}' must declare at least one outcome tool`);
|
|
1209
|
-
}
|
|
1210
|
-
if (input.spec.turnEnd) {
|
|
1211
|
-
throw new Error(`Mapped turn '${input.turnId}' cannot declare a turnEnd result`);
|
|
1212
|
-
}
|
|
1213
|
-
for (const [outcome, spec] of outcomes) {
|
|
1214
|
-
if (
|
|
1215
|
-
"branches" in spec ||
|
|
1216
|
-
hasDeclaredStaticRouteTarget(spec) ||
|
|
1217
|
-
spec.effect ||
|
|
1218
|
-
spec.lifecycleIntent ||
|
|
1219
|
-
spec.publishedProduct
|
|
1220
|
-
) {
|
|
1221
|
-
throw new Error(
|
|
1222
|
-
`Mapped turn '${input.turnId}' outcome '${outcome}' cannot route, change state, or publish; collect results instead`,
|
|
1223
|
-
);
|
|
1224
|
-
}
|
|
1225
|
-
if (!Object.hasOwn(mapped.yields, outcome)) {
|
|
1226
|
-
throw new Error(
|
|
1227
|
-
`Mapped turn '${input.turnId}' outcome '${outcome}' must declare .yield(...)`,
|
|
1228
|
-
);
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
1043
|
const routing = mapped.routing;
|
|
1232
1044
|
if (routing.kind === "static") {
|
|
1233
1045
|
const target = normalizeStaticRouteTarget(
|
|
1234
1046
|
`Mapped turn '${input.turnId}' collection`,
|
|
1235
|
-
|
|
1236
|
-
...(routing.to !== undefined ? { to: routing.to } : {}),
|
|
1237
|
-
...(routing.lifecycleStatus !== undefined
|
|
1238
|
-
? { lifecycleStatus: routing.lifecycleStatus }
|
|
1239
|
-
: {}),
|
|
1240
|
-
},
|
|
1047
|
+
routing,
|
|
1241
1048
|
input.knownTurnIds,
|
|
1242
1049
|
);
|
|
1243
1050
|
return [{ ...target, trigger: mappedCollectTrigger() }];
|
|
1244
1051
|
}
|
|
1245
|
-
|
|
1246
|
-
if (branches.length === 0) {
|
|
1247
|
-
throw new Error(`Mapped turn '${input.turnId}' collection must declare at least one branch`);
|
|
1248
|
-
}
|
|
1249
|
-
return branches.map(([branchId, to]) => {
|
|
1052
|
+
return compileDeclarations(Object.entries(routing.branches), ([branchId, to]) => {
|
|
1250
1053
|
if (branchId.trim() === "") {
|
|
1251
1054
|
throw new Error(`Mapped turn '${input.turnId}' collection contains an empty branch id`);
|
|
1252
1055
|
}
|
|
@@ -1259,37 +1062,14 @@ function compileMappedTurnTransitions<TParams, TState>(input: {
|
|
|
1259
1062
|
});
|
|
1260
1063
|
}
|
|
1261
1064
|
|
|
1262
|
-
function addActionUse<TParams, TState>(input: {
|
|
1263
|
-
actionUses: Map<string, CompiledActionUse<TParams, TState>[]>;
|
|
1264
|
-
actionId: string;
|
|
1265
|
-
use: CompiledActionUse<TParams, TState>;
|
|
1266
|
-
}) {
|
|
1267
|
-
const existing = input.actionUses.get(input.actionId) ?? [];
|
|
1268
|
-
if (existing.some((candidate) => candidate.turnId === input.use.turnId)) {
|
|
1269
|
-
throw new Error(
|
|
1270
|
-
`Turn '${input.use.turnId}' declares action '${input.actionId}' more than once`,
|
|
1271
|
-
);
|
|
1272
|
-
}
|
|
1273
|
-
existing.push(input.use);
|
|
1274
|
-
input.actionUses.set(input.actionId, existing);
|
|
1275
|
-
}
|
|
1276
|
-
|
|
1277
1065
|
type DerivedHumanTurnAction<TParams, TState> = {
|
|
1278
1066
|
actionId: string;
|
|
1279
1067
|
actionSpec: ProcessHumanTurnActionSpec<TParams, TState>;
|
|
1280
1068
|
routing: CompiledActionRouting<TParams, TState>;
|
|
1281
|
-
transitions: readonly NormalizedActionRoute[];
|
|
1282
1069
|
actionView: HumanTurnActionView;
|
|
1283
1070
|
externalTriggers: readonly HumanTurnExternalTrigger[];
|
|
1284
1071
|
};
|
|
1285
1072
|
|
|
1286
|
-
type DerivedHumanTurnExternalAction<TParams, TState> = {
|
|
1287
|
-
externalActionId: string;
|
|
1288
|
-
actionSpec: ProcessHumanTurnExternalActionSpec<TParams, TState>;
|
|
1289
|
-
transition: NormalizedActionRoute;
|
|
1290
|
-
view: HumanTurnExternalActionView;
|
|
1291
|
-
};
|
|
1292
|
-
|
|
1293
1073
|
function hasDeclaredStaticRouteTarget(target: StaticRouteTarget): boolean {
|
|
1294
1074
|
return (
|
|
1295
1075
|
target.to !== undefined || target.complete === true || target.lifecycleStatus !== undefined
|
|
@@ -1302,27 +1082,13 @@ function resolveExternalActionRoute<TParams, TState>(input: {
|
|
|
1302
1082
|
spec: ProcessHumanTurnExternalActionSpec<TParams, TState>;
|
|
1303
1083
|
knownTurnIds?: ReadonlySet<TurnId>;
|
|
1304
1084
|
}): NormalizedActionRoute {
|
|
1305
|
-
if (input.externalActionId.trim() === "") {
|
|
1306
|
-
throw new Error(`Human turn '${input.turnId}' declares an empty external action id`);
|
|
1307
|
-
}
|
|
1308
|
-
if (input.spec.id !== input.externalActionId) {
|
|
1309
|
-
throw new Error(
|
|
1310
|
-
`Human turn '${input.turnId}' external action '${input.externalActionId}' has mismatched spec id '${input.spec.id}'`,
|
|
1311
|
-
);
|
|
1312
|
-
}
|
|
1313
|
-
if (input.spec.source.kind.trim() === "") {
|
|
1314
|
-
throw new Error(
|
|
1315
|
-
`Human turn '${input.turnId}' external action '${input.externalActionId}' must declare a non-empty source kind`,
|
|
1316
|
-
);
|
|
1317
|
-
}
|
|
1318
1085
|
const target = normalizeStaticRouteTarget(
|
|
1319
1086
|
`Human turn '${input.turnId}' external action '${input.externalActionId}'`,
|
|
1320
1087
|
input.spec,
|
|
1321
1088
|
input.knownTurnIds,
|
|
1322
1089
|
);
|
|
1323
1090
|
return {
|
|
1324
|
-
...
|
|
1325
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
1091
|
+
...target,
|
|
1326
1092
|
trigger: getExternalActionTransitionTrigger({ externalActionId: input.externalActionId }),
|
|
1327
1093
|
};
|
|
1328
1094
|
}
|
|
@@ -1391,44 +1157,25 @@ function deriveHumanTurnActions<TParams, TState>(input: {
|
|
|
1391
1157
|
spec: HumanTurnDefinition<TParams, TState>;
|
|
1392
1158
|
knownTurnIds?: ReadonlySet<TurnId>;
|
|
1393
1159
|
turnDefinitionsById?: ReadonlyMap<TurnId, TurnDefinition<unknown, unknown>>;
|
|
1394
|
-
requireHumanActions?: boolean;
|
|
1395
1160
|
}): {
|
|
1396
1161
|
actions: readonly DerivedHumanTurnAction<TParams, TState>[];
|
|
1397
|
-
externalActions: readonly DerivedHumanTurnExternalAction<TParams, TState>[];
|
|
1398
1162
|
transitions: readonly ProcessTurnTransition[];
|
|
1399
1163
|
} {
|
|
1400
|
-
const actionEntries = Object.entries(input.spec.actions)
|
|
1401
|
-
[string, ProcessHumanTurnActionSpec<TParams, TState>]
|
|
1402
|
-
>;
|
|
1403
|
-
if (actionEntries.length === 0 && input.knownTurnIds && input.requireHumanActions !== false) {
|
|
1404
|
-
throw new Error(`Human turn '${input.turnId}' must declare at least one action`);
|
|
1405
|
-
}
|
|
1406
|
-
|
|
1164
|
+
const actionEntries = Object.entries(input.spec.actions);
|
|
1407
1165
|
const derivedActions: DerivedHumanTurnAction<TParams, TState>[] = [];
|
|
1408
1166
|
const transitions: ProcessTurnTransition[] = [];
|
|
1409
1167
|
const actionIdByTrigger = new Map<string, string>();
|
|
1410
|
-
|
|
1168
|
+
compileDeclarations(actionEntries, ([actionId, actionSpec]) => {
|
|
1411
1169
|
if (actionId.trim() === "") {
|
|
1412
1170
|
throw new Error(`Human turn '${input.turnId}' declares an empty action id`);
|
|
1413
1171
|
}
|
|
1414
1172
|
validateActionPrimaryPrompt(input.turnId, actionId, actionSpec);
|
|
1415
|
-
const resolved =
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
kind: "static",
|
|
1422
|
-
route: { trigger: actionSpec.trigger ?? actionId },
|
|
1423
|
-
} satisfies CompiledActionRouting<TParams, TState>,
|
|
1424
|
-
transitions: [] as readonly NormalizedActionRoute[],
|
|
1425
|
-
}
|
|
1426
|
-
: resolveActionRouting({
|
|
1427
|
-
turnId: input.turnId,
|
|
1428
|
-
actionId,
|
|
1429
|
-
spec: actionSpec,
|
|
1430
|
-
knownTurnIds: input.knownTurnIds,
|
|
1431
|
-
});
|
|
1173
|
+
const resolved = resolveActionRouting({
|
|
1174
|
+
turnId: input.turnId,
|
|
1175
|
+
actionId,
|
|
1176
|
+
spec: actionSpec,
|
|
1177
|
+
knownTurnIds: input.knownTurnIds,
|
|
1178
|
+
});
|
|
1432
1179
|
for (const transition of resolved.transitions) {
|
|
1433
1180
|
const existingActionId = actionIdByTrigger.get(transition.trigger);
|
|
1434
1181
|
if (existingActionId && existingActionId !== actionId) {
|
|
@@ -1437,26 +1184,18 @@ function deriveHumanTurnActions<TParams, TState>(input: {
|
|
|
1437
1184
|
);
|
|
1438
1185
|
}
|
|
1439
1186
|
actionIdByTrigger.set(transition.trigger, actionId);
|
|
1440
|
-
transitions.push({
|
|
1441
|
-
...(transition.nextTurnId ? { nextTurnId: transition.nextTurnId } : {}),
|
|
1442
|
-
...(transition.lifecycleStatus ? { lifecycleStatus: transition.lifecycleStatus } : {}),
|
|
1443
|
-
trigger: transition.trigger,
|
|
1444
|
-
});
|
|
1187
|
+
transitions.push({ ...transition });
|
|
1445
1188
|
}
|
|
1446
|
-
const previewAndScheduling =
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
: {}),
|
|
1453
|
-
}
|
|
1454
|
-
: buildActionPreviewAndScheduling(input.turnId, actionId, actionSpec, resolved.transitions);
|
|
1189
|
+
const previewAndScheduling = buildActionPreviewAndScheduling(
|
|
1190
|
+
input.turnId,
|
|
1191
|
+
actionId,
|
|
1192
|
+
actionSpec,
|
|
1193
|
+
resolved.transitions,
|
|
1194
|
+
);
|
|
1455
1195
|
derivedActions.push({
|
|
1456
1196
|
actionId,
|
|
1457
1197
|
actionSpec,
|
|
1458
1198
|
routing: resolved.routing,
|
|
1459
|
-
transitions: resolved.transitions,
|
|
1460
1199
|
actionView: {
|
|
1461
1200
|
actionId,
|
|
1462
1201
|
label: actionSpec.label,
|
|
@@ -1472,40 +1211,30 @@ function deriveHumanTurnActions<TParams, TState>(input: {
|
|
|
1472
1211
|
description: trigger.description,
|
|
1473
1212
|
})) ?? [],
|
|
1474
1213
|
});
|
|
1475
|
-
}
|
|
1214
|
+
});
|
|
1476
1215
|
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
);
|
|
1495
|
-
}
|
|
1496
|
-
}
|
|
1497
|
-
validateExternalActionPublishedInputTarget({
|
|
1498
|
-
turnId: input.turnId,
|
|
1499
|
-
externalActionId,
|
|
1500
|
-
spec: actionSpec,
|
|
1501
|
-
turnDefinitionsById: input.turnDefinitionsById,
|
|
1502
|
-
});
|
|
1503
|
-
const transition = resolveExternalActionRoute({
|
|
1504
|
-
turnId: input.turnId,
|
|
1505
|
-
externalActionId,
|
|
1506
|
-
spec: actionSpec,
|
|
1507
|
-
knownTurnIds: input.knownTurnIds,
|
|
1216
|
+
transitions.push(...compileExternalActionTransitions(input, actionIdByTrigger));
|
|
1217
|
+
return { actions: derivedActions, transitions };
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
function compileExternalActionTransitions<TParams, TState>(
|
|
1221
|
+
input: {
|
|
1222
|
+
turnId: TurnId;
|
|
1223
|
+
spec: Pick<HumanTurnDefinition<TParams, TState>, "externalActions">;
|
|
1224
|
+
knownTurnIds?: ReadonlySet<TurnId>;
|
|
1225
|
+
turnDefinitionsById?: ReadonlyMap<TurnId, TurnDefinition<unknown, unknown>>;
|
|
1226
|
+
},
|
|
1227
|
+
actionIdByTrigger = new Map<string, string>(),
|
|
1228
|
+
): readonly ProcessTurnTransition[] {
|
|
1229
|
+
const entries = Object.entries(input.spec.externalActions ?? {});
|
|
1230
|
+
return compileDeclarations(entries, ([externalActionId, actionSpec]) => {
|
|
1231
|
+
const [error] = validateExternalActions("Human", input.turnId, {
|
|
1232
|
+
[externalActionId]: actionSpec,
|
|
1508
1233
|
});
|
|
1234
|
+
if (error) throw new Error(error);
|
|
1235
|
+
const route = { ...input, externalActionId, spec: actionSpec };
|
|
1236
|
+
validateExternalActionPublishedInputTarget(route);
|
|
1237
|
+
const transition = resolveExternalActionRoute(route);
|
|
1509
1238
|
const existingActionId = actionIdByTrigger.get(transition.trigger);
|
|
1510
1239
|
if (existingActionId) {
|
|
1511
1240
|
throw new Error(
|
|
@@ -1513,30 +1242,8 @@ function deriveHumanTurnActions<TParams, TState>(input: {
|
|
|
1513
1242
|
);
|
|
1514
1243
|
}
|
|
1515
1244
|
actionIdByTrigger.set(transition.trigger, externalActionId);
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
...(transition.lifecycleStatus ? { lifecycleStatus: transition.lifecycleStatus } : {}),
|
|
1519
|
-
trigger: transition.trigger,
|
|
1520
|
-
});
|
|
1521
|
-
derivedExternalActions.push({
|
|
1522
|
-
externalActionId,
|
|
1523
|
-
actionSpec,
|
|
1524
|
-
transition,
|
|
1525
|
-
view: {
|
|
1526
|
-
id: getExternalActionArmingId({ turnId: input.turnId, externalActionId }),
|
|
1527
|
-
externalActionId,
|
|
1528
|
-
sourceKind: actionSpec.source.kind,
|
|
1529
|
-
label: actionSpec.label ?? actionSpec.source.label ?? null,
|
|
1530
|
-
description: actionSpec.description ?? actionSpec.source.description ?? null,
|
|
1531
|
-
},
|
|
1532
|
-
});
|
|
1533
|
-
}
|
|
1534
|
-
|
|
1535
|
-
return {
|
|
1536
|
-
actions: derivedActions,
|
|
1537
|
-
externalActions: derivedExternalActions,
|
|
1538
|
-
transitions,
|
|
1539
|
-
};
|
|
1245
|
+
return transition;
|
|
1246
|
+
});
|
|
1540
1247
|
}
|
|
1541
1248
|
|
|
1542
1249
|
/** @internal */
|
|
@@ -1550,8 +1257,6 @@ export function resolveHumanTurnView<TParams, TState>(input: {
|
|
|
1550
1257
|
actions: readonly HumanTurnActionView[];
|
|
1551
1258
|
/** @internal */
|
|
1552
1259
|
externalTriggers: readonly HumanTurnExternalTrigger[];
|
|
1553
|
-
/** @internal */
|
|
1554
|
-
externalActions: readonly HumanTurnExternalActionView[];
|
|
1555
1260
|
} {
|
|
1556
1261
|
const derived = deriveHumanTurnActions({
|
|
1557
1262
|
turnId: input.turnId,
|
|
@@ -1560,7 +1265,6 @@ export function resolveHumanTurnView<TParams, TState>(input: {
|
|
|
1560
1265
|
return {
|
|
1561
1266
|
actions: derived.actions.map((action) => action.actionView),
|
|
1562
1267
|
externalTriggers: derived.actions.flatMap((action) => action.externalTriggers),
|
|
1563
|
-
externalActions: derived.externalActions.map((action) => action.view),
|
|
1564
1268
|
};
|
|
1565
1269
|
}
|
|
1566
1270
|
|
|
@@ -1570,49 +1274,21 @@ function compileHumanTurn<TParams, TState>(input: {
|
|
|
1570
1274
|
knownTurnIds: ReadonlySet<TurnId>;
|
|
1571
1275
|
turnDefinitionsById: ReadonlyMap<TurnId, TurnDefinition<unknown, unknown>>;
|
|
1572
1276
|
actionUses: Map<string, CompiledActionUse<TParams, TState>[]>;
|
|
1573
|
-
}): {
|
|
1574
|
-
|
|
1575
|
-
} {
|
|
1576
|
-
const derived = deriveHumanTurnActions({
|
|
1577
|
-
turnId: input.turnId,
|
|
1578
|
-
spec: input.spec,
|
|
1579
|
-
knownTurnIds: input.knownTurnIds,
|
|
1580
|
-
turnDefinitionsById: input.turnDefinitionsById,
|
|
1581
|
-
});
|
|
1277
|
+
}): ProcessTurnBinding<HumanTurnDefinition<TParams, TState>> {
|
|
1278
|
+
const derived = deriveHumanTurnActions(input);
|
|
1582
1279
|
for (const action of derived.actions) {
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
...(action.actionSpec.effect ? { effect: action.actionSpec.effect } : {}),
|
|
1591
|
-
routing: action.routing,
|
|
1592
|
-
},
|
|
1280
|
+
const uses = input.actionUses.get(action.actionId) ?? [];
|
|
1281
|
+
uses.push({
|
|
1282
|
+
turnId: input.turnId,
|
|
1283
|
+
label: action.actionSpec.label,
|
|
1284
|
+
...(action.actionSpec.form ? { form: action.actionSpec.form } : {}),
|
|
1285
|
+
...(action.actionSpec.effect ? { effect: action.actionSpec.effect } : {}),
|
|
1286
|
+
routing: action.routing,
|
|
1593
1287
|
});
|
|
1288
|
+
input.actionUses.set(action.actionId, uses);
|
|
1594
1289
|
}
|
|
1595
1290
|
|
|
1596
|
-
return
|
|
1597
|
-
binding: createProcessTurnBinding(input.spec, derived.transitions),
|
|
1598
|
-
};
|
|
1599
|
-
}
|
|
1600
|
-
|
|
1601
|
-
function compileExternalTurn<TParams, TState>(input: {
|
|
1602
|
-
turnId: TurnId;
|
|
1603
|
-
spec: ExternalTurnDefinition<TParams, TState>;
|
|
1604
|
-
knownTurnIds: ReadonlySet<TurnId>;
|
|
1605
|
-
}): {
|
|
1606
|
-
binding: ProcessTurnBinding<ExternalTurnDefinition<TParams, TState>>;
|
|
1607
|
-
} {
|
|
1608
|
-
const transitions = compileExternalSourceTransitions({
|
|
1609
|
-
turnId: input.turnId,
|
|
1610
|
-
spec: input.spec,
|
|
1611
|
-
knownTurnIds: input.knownTurnIds,
|
|
1612
|
-
});
|
|
1613
|
-
return {
|
|
1614
|
-
binding: createProcessTurnBinding(input.spec, transitions),
|
|
1615
|
-
};
|
|
1291
|
+
return createProcessTurnBinding(input.spec, derived.transitions);
|
|
1616
1292
|
}
|
|
1617
1293
|
|
|
1618
1294
|
function resolveActionRouteForExecution<TParams, TState>(input: {
|
|
@@ -1635,12 +1311,7 @@ function resolveActionRouteForExecution<TParams, TState>(input: {
|
|
|
1635
1311
|
};
|
|
1636
1312
|
const routes = input.use.routing.routes;
|
|
1637
1313
|
return Promise.resolve(input.use.routing.choose(actionExecution)).then((selectedBranchId) => {
|
|
1638
|
-
|
|
1639
|
-
throw new Error(
|
|
1640
|
-
`Action '${input.actionId}' on turn '${input.use.turnId}' selected unknown branch '${selectedBranchId}'`,
|
|
1641
|
-
);
|
|
1642
|
-
}
|
|
1643
|
-
const route = routes[selectedBranchId];
|
|
1314
|
+
const route = Object.hasOwn(routes, selectedBranchId) ? routes[selectedBranchId] : undefined;
|
|
1644
1315
|
if (!route) {
|
|
1645
1316
|
throw new Error(
|
|
1646
1317
|
`Action '${input.actionId}' on turn '${input.use.turnId}' selected unknown branch '${selectedBranchId}'`,
|
|
@@ -1665,14 +1336,13 @@ function buildActionTransitionRequest<TState>(route: NormalizedActionRoute) {
|
|
|
1665
1336
|
}
|
|
1666
1337
|
|
|
1667
1338
|
function validateHappyPathConnectivity<TParams, TState>(input: {
|
|
1668
|
-
processId: string;
|
|
1669
1339
|
happyPath: readonly TurnId[] | undefined;
|
|
1670
1340
|
turns: ReadonlyMap<TurnId, ProcessTurnBinding<TurnDefinition<TParams, TState>>>;
|
|
1671
|
-
|
|
1672
|
-
|
|
1341
|
+
}): string[] {
|
|
1342
|
+
const errors: string[] = [];
|
|
1673
1343
|
const happyPath = input.happyPath;
|
|
1674
1344
|
if (!happyPath || happyPath.length < 2) {
|
|
1675
|
-
return;
|
|
1345
|
+
return errors;
|
|
1676
1346
|
}
|
|
1677
1347
|
const happyPathTurns = new Set<TurnId>(happyPath);
|
|
1678
1348
|
for (let index = 0; index < happyPath.length - 1; index += 1) {
|
|
@@ -1685,7 +1355,7 @@ function validateHappyPathConnectivity<TParams, TState>(input: {
|
|
|
1685
1355
|
const current = queue.shift() as TurnId;
|
|
1686
1356
|
for (const transition of getProcessTurnTransitions(input.turns.get(current))) {
|
|
1687
1357
|
const next = transition.nextTurnId;
|
|
1688
|
-
if (next === undefined || !input.
|
|
1358
|
+
if (next === undefined || !input.turns.has(next)) {
|
|
1689
1359
|
continue;
|
|
1690
1360
|
}
|
|
1691
1361
|
if (next === target) {
|
|
@@ -1695,7 +1365,7 @@ function validateHappyPathConnectivity<TParams, TState>(input: {
|
|
|
1695
1365
|
if (visited.has(next) || happyPathTurns.has(next)) {
|
|
1696
1366
|
continue;
|
|
1697
1367
|
}
|
|
1698
|
-
const definition = input.
|
|
1368
|
+
const definition = input.turns.get(next)?.definition;
|
|
1699
1369
|
if (definition?.kind !== "human") {
|
|
1700
1370
|
continue;
|
|
1701
1371
|
}
|
|
@@ -1704,60 +1374,122 @@ function validateHappyPathConnectivity<TParams, TState>(input: {
|
|
|
1704
1374
|
}
|
|
1705
1375
|
}
|
|
1706
1376
|
if (!connected) {
|
|
1707
|
-
|
|
1708
|
-
`
|
|
1377
|
+
errors.push(
|
|
1378
|
+
`happy path segment '${source}' -> '${target}' is not connected by declared transitions`,
|
|
1709
1379
|
);
|
|
1710
1380
|
}
|
|
1711
1381
|
}
|
|
1382
|
+
return errors;
|
|
1712
1383
|
}
|
|
1713
1384
|
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1385
|
+
type ProcessDefinitionDeclaration<TParams, TState> = Pick<
|
|
1386
|
+
DefinedProcessInput<TParams, TState>,
|
|
1387
|
+
"id" | "entry" | "alternateEntries" | "happyPath" | "turns"
|
|
1388
|
+
>;
|
|
1389
|
+
|
|
1390
|
+
class DefinitionDiagnostics {
|
|
1391
|
+
readonly errors: string[] = [];
|
|
1392
|
+
|
|
1393
|
+
constructor(private readonly processId: string) {}
|
|
1394
|
+
|
|
1395
|
+
add(message: string, turnId?: TurnId): void {
|
|
1396
|
+
const context = `Process '${this.processId}'${turnId === undefined ? "" : `, turn '${turnId}'`}`;
|
|
1397
|
+
this.errors.push(`${context}: ${message}`);
|
|
1720
1398
|
}
|
|
1399
|
+
|
|
1400
|
+
record(error: unknown, turnId?: TurnId): void {
|
|
1401
|
+
if (error instanceof AggregateError) {
|
|
1402
|
+
for (const cause of error.errors) this.record(cause, turnId);
|
|
1403
|
+
} else {
|
|
1404
|
+
this.add(error instanceof Error ? error.message : String(error), turnId);
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
capture<T>(inspect: () => T, turnId?: TurnId): T | undefined {
|
|
1409
|
+
try {
|
|
1410
|
+
return inspect();
|
|
1411
|
+
} catch (error) {
|
|
1412
|
+
this.record(error, turnId);
|
|
1413
|
+
return undefined;
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
// Siblings can be inspected independently; their dependent compilation cannot
|
|
1419
|
+
// proceed until all of them are valid.
|
|
1420
|
+
function compileDeclarations<TEntry, TResult>(
|
|
1421
|
+
entries: readonly TEntry[],
|
|
1422
|
+
compile: (entry: TEntry, index: number) => TResult,
|
|
1423
|
+
): TResult[] {
|
|
1424
|
+
const results: TResult[] = [];
|
|
1425
|
+
const errors: unknown[] = [];
|
|
1426
|
+
for (const [index, entry] of entries.entries()) {
|
|
1427
|
+
try {
|
|
1428
|
+
results.push(compile(entry, index));
|
|
1429
|
+
} catch (error) {
|
|
1430
|
+
errors.push(error);
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
if (errors.length > 0) {
|
|
1434
|
+
throw new AggregateError(
|
|
1435
|
+
errors,
|
|
1436
|
+
errors.map((error) => (error instanceof Error ? error.message : String(error))).join("; "),
|
|
1437
|
+
);
|
|
1438
|
+
}
|
|
1439
|
+
return results;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
// Compilation and catalog admission share this inspection. Author callbacks are
|
|
1443
|
+
// retained as values; inspecting a declaration never executes a process.
|
|
1444
|
+
function compileProcessDefinition<TParams, TState>(
|
|
1445
|
+
input: ProcessDefinitionDeclaration<TParams, TState>,
|
|
1446
|
+
retainedTurns?: ReadonlyMap<TurnId, ProcessTurnBinding<TurnDefinition<TParams, TState>>>,
|
|
1447
|
+
) {
|
|
1448
|
+
const diagnostics = new DefinitionDiagnostics(input.id);
|
|
1449
|
+
const turnEntries = Object.entries(input.turns);
|
|
1721
1450
|
const knownTurnIds = new Set(turnEntries.map(([turnId]) => turnId));
|
|
1722
1451
|
const turnDefinitionsById = new Map<TurnId, TurnDefinition<unknown, unknown>>(
|
|
1723
1452
|
turnEntries.map(([turnId, turnSpec]) => [turnId, turnSpec as TurnDefinition<unknown, unknown>]),
|
|
1724
1453
|
);
|
|
1725
|
-
if (
|
|
1726
|
-
|
|
1454
|
+
if (turnEntries.length === 0) {
|
|
1455
|
+
diagnostics.add("must declare at least one turn");
|
|
1727
1456
|
}
|
|
1728
1457
|
const seenEntries = new Set<TurnId>();
|
|
1729
1458
|
for (const turnId of [input.entry, ...(input.alternateEntries ?? [])]) {
|
|
1730
1459
|
if (seenEntries.has(turnId)) {
|
|
1731
|
-
|
|
1460
|
+
diagnostics.add(`declares duplicate entry turn '${turnId}'`);
|
|
1732
1461
|
}
|
|
1733
1462
|
if (!knownTurnIds.has(turnId)) {
|
|
1734
|
-
|
|
1463
|
+
diagnostics.add(`entry turn '${turnId}' is not declared in turns`);
|
|
1735
1464
|
}
|
|
1736
1465
|
seenEntries.add(turnId);
|
|
1737
1466
|
}
|
|
1738
1467
|
|
|
1468
|
+
let validHappyPath = true;
|
|
1739
1469
|
if (input.happyPath) {
|
|
1470
|
+
const before = diagnostics.errors.length;
|
|
1740
1471
|
if (input.happyPath.length === 0) {
|
|
1741
|
-
|
|
1472
|
+
diagnostics.add("declares an empty happy path");
|
|
1742
1473
|
}
|
|
1743
1474
|
const seen = new Set<TurnId>();
|
|
1744
1475
|
for (const turnId of input.happyPath) {
|
|
1745
1476
|
if (!knownTurnIds.has(turnId)) {
|
|
1746
|
-
|
|
1477
|
+
diagnostics.add(`happy path references undeclared turn '${turnId}'`);
|
|
1747
1478
|
}
|
|
1748
1479
|
if (seen.has(turnId)) {
|
|
1749
|
-
|
|
1480
|
+
diagnostics.add(`happy path repeats turn '${turnId}'`);
|
|
1750
1481
|
}
|
|
1751
1482
|
seen.add(turnId);
|
|
1752
1483
|
}
|
|
1753
|
-
if (input.happyPath[0] !== input.entry) {
|
|
1754
|
-
|
|
1755
|
-
`Process '${input.id}' happy path must start at the entry turn '${input.entry}'`,
|
|
1756
|
-
);
|
|
1484
|
+
if (input.happyPath.length > 0 && input.happyPath[0] !== input.entry) {
|
|
1485
|
+
diagnostics.add(`happy path must start at the entry turn '${input.entry}'`);
|
|
1757
1486
|
}
|
|
1487
|
+
validHappyPath = before === diagnostics.errors.length;
|
|
1758
1488
|
}
|
|
1759
1489
|
|
|
1760
|
-
const turns = new Map<TurnId, ProcessTurnBinding<TurnDefinition<TParams, TState>>>(
|
|
1490
|
+
const turns = new Map<TurnId, ProcessTurnBinding<TurnDefinition<TParams, TState>>>(
|
|
1491
|
+
turnEntries.map(([turnId, definition]) => [turnId, { definition }]),
|
|
1492
|
+
);
|
|
1761
1493
|
const actionUses = new Map<string, CompiledActionUse<TParams, TState>[]>();
|
|
1762
1494
|
const executableTurns = new Map<
|
|
1763
1495
|
TurnId,
|
|
@@ -1765,79 +1497,174 @@ function buildDefinedProcess<TParams, TState>(
|
|
|
1765
1497
|
spec: RoutableTurnDefinition<string, TParams, TState>;
|
|
1766
1498
|
}
|
|
1767
1499
|
>();
|
|
1768
|
-
|
|
1500
|
+
let validTurns = true;
|
|
1501
|
+
let inspectableProducts = true;
|
|
1769
1502
|
for (const [turnId, turnSpec] of turnEntries) {
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
turnSpec,
|
|
1775
|
-
compileMappedTurnTransitions({ turnId, spec: turnSpec, knownTurnIds }),
|
|
1776
|
-
),
|
|
1777
|
-
);
|
|
1778
|
-
executableTurns.set(turnId, {
|
|
1779
|
-
spec: turnSpec,
|
|
1780
|
-
effects: new Map(),
|
|
1781
|
-
routings: new Map(),
|
|
1782
|
-
});
|
|
1503
|
+
const errors = diagnostics.capture(() => validateTurnDefinition(turnId, turnSpec), turnId);
|
|
1504
|
+
if (errors === undefined) {
|
|
1505
|
+
validTurns = false;
|
|
1506
|
+
inspectableProducts = false;
|
|
1783
1507
|
continue;
|
|
1784
1508
|
}
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1509
|
+
for (const error of errors) diagnostics.add(error, turnId);
|
|
1510
|
+
if (errors.length > 0) {
|
|
1511
|
+
validTurns = false;
|
|
1512
|
+
continue;
|
|
1513
|
+
}
|
|
1514
|
+
try {
|
|
1515
|
+
const external =
|
|
1516
|
+
turnSpec.kind === "external"
|
|
1517
|
+
? turnSpec.transitions
|
|
1518
|
+
: Object.values(turnSpec.externalActions ?? {});
|
|
1519
|
+
for (const route of external) {
|
|
1520
|
+
const target = route.to ? input.turns[route.to] : undefined;
|
|
1521
|
+
if ((target?.kind === "llm" || target?.kind === "automatic") && !target.waitFor) {
|
|
1522
|
+
validTurns = false;
|
|
1523
|
+
diagnostics.add(
|
|
1524
|
+
`External transition from '${turnId}' to worker turn '${route.to}' requires .waitFor(...) on the target`,
|
|
1525
|
+
turnId,
|
|
1526
|
+
);
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1792
1529
|
const externalTransitions =
|
|
1793
|
-
turnSpec.kind === "automatic" && turnSpec.externalActions
|
|
1794
|
-
?
|
|
1530
|
+
(turnSpec.kind === "llm" || turnSpec.kind === "automatic") && turnSpec.externalActions
|
|
1531
|
+
? compileExternalActionTransitions({
|
|
1795
1532
|
turnId,
|
|
1796
|
-
spec:
|
|
1797
|
-
kind: "human",
|
|
1798
|
-
description: turnSpec.description,
|
|
1799
|
-
actions: {},
|
|
1800
|
-
externalActions: turnSpec.externalActions,
|
|
1801
|
-
},
|
|
1533
|
+
spec: turnSpec,
|
|
1802
1534
|
knownTurnIds,
|
|
1803
1535
|
turnDefinitionsById,
|
|
1804
|
-
|
|
1805
|
-
}).transitions
|
|
1536
|
+
})
|
|
1806
1537
|
: [];
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1538
|
+
if (turnSpec.kind === "llm" && turnSpec.forEach) {
|
|
1539
|
+
turns.set(
|
|
1540
|
+
turnId,
|
|
1541
|
+
createProcessTurnBinding(turnSpec, [
|
|
1542
|
+
...compileMappedTurnTransitions({ turnId, spec: turnSpec, knownTurnIds }),
|
|
1543
|
+
...externalTransitions,
|
|
1544
|
+
]),
|
|
1545
|
+
);
|
|
1546
|
+
executableTurns.set(turnId, {
|
|
1547
|
+
spec: turnSpec,
|
|
1548
|
+
effects: new Map(),
|
|
1549
|
+
routings: new Map(),
|
|
1550
|
+
});
|
|
1551
|
+
continue;
|
|
1552
|
+
}
|
|
1553
|
+
if (turnSpec.kind === "llm" || turnSpec.kind === "automatic") {
|
|
1554
|
+
const { transitions, effects, routings } = compileTurnOutcomeDefinitions({
|
|
1555
|
+
turnId,
|
|
1556
|
+
outcomes: turnSpec.outcomes,
|
|
1557
|
+
turnEnd: turnSpec.turnEnd,
|
|
1558
|
+
knownTurnIds,
|
|
1559
|
+
});
|
|
1560
|
+
turns.set(
|
|
1561
|
+
turnId,
|
|
1562
|
+
createProcessTurnBinding(turnSpec, [...transitions, ...externalTransitions]),
|
|
1563
|
+
);
|
|
1564
|
+
executableTurns.set(turnId, { spec: turnSpec, effects, routings });
|
|
1565
|
+
continue;
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
if (turnSpec.kind === "human") {
|
|
1569
|
+
const compiled = compileHumanTurn({
|
|
1570
|
+
turnId,
|
|
1571
|
+
spec: turnSpec,
|
|
1572
|
+
knownTurnIds,
|
|
1573
|
+
turnDefinitionsById,
|
|
1574
|
+
actionUses,
|
|
1575
|
+
});
|
|
1576
|
+
turns.set(turnId, compiled);
|
|
1577
|
+
continue;
|
|
1578
|
+
}
|
|
1814
1579
|
|
|
1815
|
-
|
|
1816
|
-
const compiled = compileHumanTurn({
|
|
1580
|
+
const transitions = compileExternalSourceTransitions({
|
|
1817
1581
|
turnId,
|
|
1818
1582
|
spec: turnSpec,
|
|
1819
1583
|
knownTurnIds,
|
|
1820
|
-
turnDefinitionsById,
|
|
1821
|
-
actionUses,
|
|
1822
1584
|
});
|
|
1823
|
-
turns.set(turnId,
|
|
1824
|
-
|
|
1585
|
+
turns.set(turnId, createProcessTurnBinding(turnSpec, transitions));
|
|
1586
|
+
} catch (error) {
|
|
1587
|
+
validTurns = false;
|
|
1588
|
+
diagnostics.record(error, turnId);
|
|
1825
1589
|
}
|
|
1590
|
+
}
|
|
1826
1591
|
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1592
|
+
for (const [actionId, uses] of actionUses) {
|
|
1593
|
+
const [firstUse] = uses;
|
|
1594
|
+
for (const use of uses.slice(1)) {
|
|
1595
|
+
if (!areFormsEquivalent(firstUse.form, use.form)) {
|
|
1596
|
+
diagnostics.add(
|
|
1597
|
+
`Action '${actionId}' must use the same form on every turn that references it (first declared on '${firstUse.turnId}')`,
|
|
1598
|
+
use.turnId,
|
|
1599
|
+
);
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1833
1602
|
}
|
|
1834
1603
|
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1604
|
+
if (retainedTurns) {
|
|
1605
|
+
for (const [turnId, binding] of retainedTurns) {
|
|
1606
|
+
if ("transitions" in binding) {
|
|
1607
|
+
diagnostics.add(
|
|
1608
|
+
"must declare routing on the turn definition instead of authored transitions",
|
|
1609
|
+
turnId,
|
|
1610
|
+
);
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
// Recompiled declarations and retained transitions must each form a valid graph.
|
|
1615
|
+
// Retained routes cannot stand in for metadata that changed after definition.
|
|
1616
|
+
for (const graphTurns of retainedTurns ? [turns, retainedTurns] : [turns]) {
|
|
1617
|
+
if (!inspectableProducts) continue;
|
|
1618
|
+
const graph = diagnostics.capture(() =>
|
|
1619
|
+
toProcessGraphView({
|
|
1620
|
+
id: input.id,
|
|
1621
|
+
entryTurnId: input.entry,
|
|
1622
|
+
alternateEntryTurnIds: input.alternateEntries,
|
|
1623
|
+
happyPath: input.happyPath,
|
|
1624
|
+
turns: graphTurns,
|
|
1625
|
+
} as unknown as ProcessGraphSource),
|
|
1626
|
+
);
|
|
1627
|
+
if (!graph) continue;
|
|
1628
|
+
for (const error of validateProcessGraphProducts(graph)) diagnostics.add(error);
|
|
1629
|
+
const transitionErrors = validateProcessGraphTurnTransitions(graph);
|
|
1630
|
+
for (const error of transitionErrors) diagnostics.add(error);
|
|
1631
|
+
// Connectivity depends on successful routing compilation. Do not manufacture
|
|
1632
|
+
// disconnected-path errors from turns whose declarations already failed.
|
|
1633
|
+
if (validTurns && validHappyPath && transitionErrors.length === 0) {
|
|
1634
|
+
for (const error of validateHappyPathConnectivity({
|
|
1635
|
+
happyPath: input.happyPath,
|
|
1636
|
+
turns: graphTurns,
|
|
1637
|
+
}))
|
|
1638
|
+
diagnostics.add(error);
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
return { turns, actionUses, executableTurns, errors: [...new Set(diagnostics.errors)] };
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
/** Recheck declaration metadata and retained graph facts without executing author callbacks. @internal */
|
|
1646
|
+
export function validateProcessDefinition<TParams, TState>(
|
|
1647
|
+
process: ExtensionProcessDefinition<TParams, TState>,
|
|
1648
|
+
): readonly string[] {
|
|
1649
|
+
return compileProcessDefinition(
|
|
1650
|
+
{
|
|
1651
|
+
id: process.id,
|
|
1652
|
+
entry: process.entryTurnId,
|
|
1653
|
+
alternateEntries: process.alternateEntryTurnIds,
|
|
1654
|
+
happyPath: process.happyPath,
|
|
1655
|
+
turns: Object.fromEntries(
|
|
1656
|
+
[...process.turns].map(([id, binding]) => [id, binding.definition]),
|
|
1657
|
+
),
|
|
1658
|
+
},
|
|
1659
|
+
process.turns,
|
|
1660
|
+
).errors;
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
function buildDefinedProcess<TParams, TState>(
|
|
1664
|
+
input: DefinedProcessInput<TParams, TState>,
|
|
1665
|
+
): Pick<ExtensionProcessDefinition<TParams, TState>, "turns" | "worker" | "server"> {
|
|
1666
|
+
const { turns, actionUses, executableTurns, errors } = compileProcessDefinition(input);
|
|
1667
|
+
if (errors.length > 0) throw new Error(errors.join("; "));
|
|
1841
1668
|
|
|
1842
1669
|
const worker: ExtensionProcessDefinition<TParams, TState>["worker"] = (api) => {
|
|
1843
1670
|
api.start(input.entry);
|
|
@@ -1871,13 +1698,6 @@ function buildDefinedProcess<TParams, TState>(
|
|
|
1871
1698
|
for (const [actionId, uses] of actionUses) {
|
|
1872
1699
|
const [firstUse] = uses;
|
|
1873
1700
|
const form = firstUse?.form;
|
|
1874
|
-
for (const use of uses) {
|
|
1875
|
-
if (!areFormsEquivalent(form, use.form)) {
|
|
1876
|
-
throw new Error(
|
|
1877
|
-
`Action '${actionId}' must use the same form on every turn that references it`,
|
|
1878
|
-
);
|
|
1879
|
-
}
|
|
1880
|
-
}
|
|
1881
1701
|
api.action({
|
|
1882
1702
|
id: actionId,
|
|
1883
1703
|
label: firstUse?.label ?? actionId,
|
|
@@ -1912,9 +1732,7 @@ function buildDefinedProcess<TParams, TState>(
|
|
|
1912
1732
|
inputValue,
|
|
1913
1733
|
state: effectResult?.state ?? ctx.state,
|
|
1914
1734
|
});
|
|
1915
|
-
const result = formState
|
|
1916
|
-
? mergeProcessEffectPlans(effectResult, { state: formState })
|
|
1917
|
-
: effectResult;
|
|
1735
|
+
const result = formState ? withFormState(effectResult, formState) : effectResult;
|
|
1918
1736
|
await applyProcessEffectPlan({
|
|
1919
1737
|
ctx,
|
|
1920
1738
|
result,
|
|
@@ -1940,7 +1758,13 @@ function buildDefinedProcess<TParams, TState>(
|
|
|
1940
1758
|
const result = effect ? await effect(execution) : undefined;
|
|
1941
1759
|
let transition: Omit<ServerTransitionRequest<TState>, "state"> | undefined;
|
|
1942
1760
|
if (routing) {
|
|
1943
|
-
const branchId = await routing.choose(
|
|
1761
|
+
const branchId = await routing.choose({
|
|
1762
|
+
...execution,
|
|
1763
|
+
ctx: {
|
|
1764
|
+
...execution.ctx,
|
|
1765
|
+
state: result?.state !== undefined ? result.state : execution.ctx.state,
|
|
1766
|
+
},
|
|
1767
|
+
});
|
|
1944
1768
|
const route = routing.routes[branchId];
|
|
1945
1769
|
if (!route) {
|
|
1946
1770
|
throw new Error(
|