@leitwerk-dev/process-sdk 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-I7U6HCLN.js → chunk-HT2AXXLH.js} +2 -1
- package/dist/chunk-HT2AXXLH.js.map +1 -0
- package/dist/core-capabilities.d.ts +57 -1
- package/dist/core-capabilities.d.ts.map +1 -1
- package/dist/define-process.d.ts +43 -26
- package/dist/define-process.d.ts.map +1 -1
- package/dist/{extension-api-3Vr3RE8S.d.ts → extension-api-Ljc4UTZ0.d.ts} +440 -49
- package/dist/extension-api.d.ts +66 -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 +234 -118
- package/dist/flow.d.ts.map +1 -1
- package/dist/index.d.ts +15 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4189 -3534
- package/dist/index.js.map +1 -1
- package/dist/mapped-turn.d.ts +169 -0
- package/dist/mapped-turn.d.ts.map +1 -0
- package/dist/outcome-contract.d.ts +19 -0
- package/dist/outcome-contract.d.ts.map +1 -0
- package/dist/outcome-schema.d.ts +23 -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 +45 -24
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/core-capabilities.ts +76 -1
- package/src/define-process.ts +447 -540
- package/src/extension-api.ts +78 -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 +888 -532
- package/src/index.ts +77 -2
- package/src/mapped-turn.ts +437 -0
- package/src/outcome-contract.ts +126 -0
- package/src/outcome-schema.ts +314 -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 +102 -116
- package/src/turn-wait.ts +76 -0
- package/src/types.ts +44 -22
- package/dist/chunk-I7U6HCLN.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,
|
|
@@ -23,15 +20,23 @@ import type {
|
|
|
23
20
|
WorkerCompleteInput,
|
|
24
21
|
WorkerProcessContext,
|
|
25
22
|
} from "./extension-api.js";
|
|
23
|
+
import { type MappedLlmTurnSpec, mappedCollectTrigger } from "./mapped-turn.js";
|
|
26
24
|
import {
|
|
27
25
|
getProcessTurnTransitions,
|
|
28
26
|
markDefinedProcess,
|
|
29
27
|
setProcessTurnTransitions,
|
|
30
28
|
} from "./process-definition-internals.js";
|
|
29
|
+
import {
|
|
30
|
+
type ProcessGraphSource,
|
|
31
|
+
toProcessGraphView,
|
|
32
|
+
validateProcessGraphProducts,
|
|
33
|
+
validateProcessGraphTurnTransitions,
|
|
34
|
+
} from "./process-graph.js";
|
|
31
35
|
import type { ServerExtensionEventMap } from "./server-events.js";
|
|
36
|
+
import { validateExternalActions, validateTurnDefinition } from "./turn-semantics.js";
|
|
37
|
+
import type { TurnWaitPredicate } from "./turn-wait.js";
|
|
32
38
|
import type {
|
|
33
39
|
HumanTurnActionView,
|
|
34
|
-
HumanTurnExternalActionView,
|
|
35
40
|
HumanTurnExternalTrigger,
|
|
36
41
|
HumanTurnNotesField,
|
|
37
42
|
OutcomeToolParameterSpec,
|
|
@@ -85,6 +90,14 @@ type NormalizedActionRoute = NormalizedRouteTarget & {
|
|
|
85
90
|
export interface ProcessRuntimeTurnContext<TParams = unknown, TState = unknown>
|
|
86
91
|
extends WorkerProcessContext<TParams, TState> {}
|
|
87
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
|
+
|
|
88
101
|
/** @internal */
|
|
89
102
|
export interface LlmTurnPreparationContext<TParams = unknown, TState = unknown>
|
|
90
103
|
extends ProcessRuntimeTurnContext<TParams, TState> {
|
|
@@ -153,30 +166,6 @@ export interface ProcessEffectPlan<TState = unknown> extends ProcessLifecycleEff
|
|
|
153
166
|
queueInput?: readonly ProcessQueuedInput[];
|
|
154
167
|
}
|
|
155
168
|
|
|
156
|
-
/** @internal */
|
|
157
|
-
export interface SavePlanResultLifecycleIntent<TParams = unknown, TState = unknown> {
|
|
158
|
-
/** @internal */
|
|
159
|
-
kind: "save_plan_result";
|
|
160
|
-
/** @internal */
|
|
161
|
-
summaryParam: string;
|
|
162
|
-
/** @internal */
|
|
163
|
-
acceptanceCriteriaParam: string;
|
|
164
|
-
/** @internal */
|
|
165
|
-
planMarkdownParam: string;
|
|
166
|
-
/** @internal */
|
|
167
|
-
emitEventType: ProcessEventName;
|
|
168
|
-
/** @internal */
|
|
169
|
-
broadcastType: string;
|
|
170
|
-
/** @internal */
|
|
171
|
-
state?: ProcessOutcomeEffect<TParams, TState>;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/** @internal */
|
|
175
|
-
export type ProcessOutcomeLifecycleIntent<
|
|
176
|
-
TParams = unknown,
|
|
177
|
-
TState = unknown,
|
|
178
|
-
> = SavePlanResultLifecycleIntent<TParams, TState>;
|
|
179
|
-
|
|
180
169
|
/** @public */
|
|
181
170
|
export interface ProcessActionExecution<TParams = unknown, TState = unknown> {
|
|
182
171
|
/** @public */
|
|
@@ -223,10 +212,14 @@ export type ProcessOutcomeBranchSelector<TParams = unknown, TState = unknown> =
|
|
|
223
212
|
input: ProcessOutcomeExecution<TParams, TState>,
|
|
224
213
|
) => MaybePromise<string>;
|
|
225
214
|
|
|
226
|
-
/** @
|
|
215
|
+
/** Branch route and display metadata for state-routed outcomes. @public */
|
|
227
216
|
export interface ProcessOutcomeBranchSpec extends StaticRouteTarget {
|
|
228
217
|
/** @internal */
|
|
229
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;
|
|
230
223
|
}
|
|
231
224
|
|
|
232
225
|
/** @internal */
|
|
@@ -317,8 +310,22 @@ export type ProcessHumanTurnActionSpec<TParams = unknown, TState = unknown> = Ba
|
|
|
317
310
|
externalTriggers?: readonly ProcessActionExternalTrigger[];
|
|
318
311
|
} & (StaticActionRouting | BranchingActionRouting<TParams, TState>);
|
|
319
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
|
+
|
|
320
321
|
/** @public */
|
|
321
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;
|
|
322
329
|
/** @internal */
|
|
323
330
|
description: string;
|
|
324
331
|
/** @internal */
|
|
@@ -331,8 +338,6 @@ interface ProcessToolOutcomeBaseSpec<TParams = unknown, TState = unknown> {
|
|
|
331
338
|
resultSummaryParameter?: string;
|
|
332
339
|
/** @internal */
|
|
333
340
|
effect?: ProcessOutcomeEffect<TParams, TState>;
|
|
334
|
-
/** @internal */
|
|
335
|
-
lifecycleIntent?: ProcessOutcomeLifecycleIntent<TParams, TState>;
|
|
336
341
|
}
|
|
337
342
|
|
|
338
343
|
/** @public */
|
|
@@ -411,8 +416,14 @@ export interface LlmTurnDefinition<
|
|
|
411
416
|
kind: "llm";
|
|
412
417
|
/** @public */
|
|
413
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>>;
|
|
414
423
|
/** Code-defined model policy purpose. Purpose selections cannot be overridden per launch/action. @internal */
|
|
415
424
|
modelPurpose?: LlmModelPurpose;
|
|
425
|
+
/** Extension-defined scoped settings consumed by this turn. @public */
|
|
426
|
+
executionPurpose?: string;
|
|
416
427
|
/** Built-in Pi tools active while this turn runs. @public */
|
|
417
428
|
availableTools: readonly PiBuiltInToolName[];
|
|
418
429
|
/** Server-owned integration tools proxied over authenticated worker IPC. @internal */
|
|
@@ -421,8 +432,15 @@ export interface LlmTurnDefinition<
|
|
|
421
432
|
resolveIntegrationTools?: (params: TParams, state: TState) => readonly string[];
|
|
422
433
|
/** Opt in to the durable, operator-facing ask_questions custom tool. @internal */
|
|
423
434
|
askQuestions?: boolean;
|
|
435
|
+
/** Constrain questions using captured process data and completed preparation before prompting. @public */
|
|
436
|
+
resolveAskQuestions?: (ctx: ProcessRuntimeTurnContext<TParams, TState>) => boolean;
|
|
424
437
|
/** Deterministic worker preparation that must complete before Pi is prompted. @internal */
|
|
425
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[]>;
|
|
426
444
|
/** @public */
|
|
427
445
|
completionMode?: TurnCompletionMode;
|
|
428
446
|
/** @public */
|
|
@@ -455,6 +473,8 @@ export interface LlmTurnDefinition<
|
|
|
455
473
|
requiredSemanticMarkdownRefs?: readonly ProcessSemanticEntryRefKey[];
|
|
456
474
|
/** @internal */
|
|
457
475
|
optionalSemanticMarkdownRefs?: readonly ProcessSemanticEntryRefKey[];
|
|
476
|
+
/** Runs this turn once per frozen item, then collects and routes once. @public */
|
|
477
|
+
forEach?: MappedLlmTurnSpec<TParams, TState>;
|
|
458
478
|
}
|
|
459
479
|
|
|
460
480
|
/** @public */
|
|
@@ -467,6 +487,8 @@ export interface AutomaticTurnDefinition<
|
|
|
467
487
|
kind: "automatic";
|
|
468
488
|
/** @public */
|
|
469
489
|
description: string;
|
|
490
|
+
/** Server-side entry condition, evaluated before worker allocation. @public */
|
|
491
|
+
waitFor?: TurnWaitPredicate<TParams, TState>;
|
|
470
492
|
/** Server-owned integration tools callable by this deterministic worker turn. @internal */
|
|
471
493
|
integrationTools?: readonly string[];
|
|
472
494
|
/** External events armed while this automatic turn is selected and waiting. @public */
|
|
@@ -623,58 +645,26 @@ type BranchRouteSpec = StaticRouteTarget & { trigger?: string };
|
|
|
623
645
|
function compileBranchRoutes(input: {
|
|
624
646
|
branches: Record<string, BranchRouteSpec>;
|
|
625
647
|
knownTurnIds?: ReadonlySet<TurnId>;
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
routes: Record<string, NormalizedActionRoute>;
|
|
631
|
-
transitions: NormalizedActionRoute[];
|
|
632
|
-
} {
|
|
633
|
-
const routes: Record<string, NormalizedActionRoute> = {};
|
|
634
|
-
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`);
|
|
635
652
|
const usedTriggers = new Set<string>();
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
function resolveOutcomeRouting<TParams, TState>(input: {
|
|
654
|
-
turnId: TurnId;
|
|
655
|
-
outcome: string;
|
|
656
|
-
spec: ProcessToolOutcomeSpec<TParams, TState>;
|
|
657
|
-
knownTurnIds: ReadonlySet<TurnId>;
|
|
658
|
-
}): CompiledOutcomeRouting<TParams, TState> | null {
|
|
659
|
-
if (!("branches" in input.spec)) {
|
|
660
|
-
return null;
|
|
661
|
-
}
|
|
662
|
-
const entries = Object.entries(input.spec.branches);
|
|
663
|
-
if (entries.length === 0) {
|
|
664
|
-
throw new Error(
|
|
665
|
-
`Turn '${input.turnId}' outcome '${input.outcome}' must declare at least one branch`,
|
|
666
|
-
);
|
|
667
|
-
}
|
|
668
|
-
const { routes } = compileBranchRoutes({
|
|
669
|
-
branches: input.spec.branches,
|
|
670
|
-
knownTurnIds: input.knownTurnIds,
|
|
671
|
-
targetContext: (branchId) =>
|
|
672
|
-
`Turn '${input.turnId}' outcome '${input.outcome}' branch '${branchId}'`,
|
|
673
|
-
emptyBranchError: `Turn '${input.turnId}' outcome '${input.outcome}' contains an empty branch id`,
|
|
674
|
-
duplicateTriggerError: (trigger) =>
|
|
675
|
-
`Turn '${input.turnId}' outcome '${input.outcome}' contains duplicate branch trigger '${trigger}'`,
|
|
676
|
-
});
|
|
677
|
-
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
|
+
);
|
|
678
668
|
}
|
|
679
669
|
|
|
680
670
|
function resolveActionRouting<TParams, TState>(input: {
|
|
@@ -687,20 +677,10 @@ function resolveActionRouting<TParams, TState>(input: {
|
|
|
687
677
|
transitions: readonly NormalizedActionRoute[];
|
|
688
678
|
} {
|
|
689
679
|
if ("branches" in input.spec) {
|
|
690
|
-
const
|
|
691
|
-
if (entries.length === 0) {
|
|
692
|
-
throw new Error(
|
|
693
|
-
`Action '${input.actionId}' on turn '${input.turnId}' must declare at least one branch`,
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
const { routes, transitions } = compileBranchRoutes({
|
|
680
|
+
const routes = compileBranchRoutes({
|
|
697
681
|
branches: input.spec.branches,
|
|
698
682
|
knownTurnIds: input.knownTurnIds,
|
|
699
|
-
|
|
700
|
-
`Action '${input.actionId}' branch '${branchId}' on turn '${input.turnId}'`,
|
|
701
|
-
emptyBranchError: `Action '${input.actionId}' on turn '${input.turnId}' contains an empty branch id`,
|
|
702
|
-
duplicateTriggerError: (trigger) =>
|
|
703
|
-
`Action '${input.actionId}' on turn '${input.turnId}' contains duplicate branch trigger '${trigger}'`,
|
|
683
|
+
context: `Action '${input.actionId}' on turn '${input.turnId}'`,
|
|
704
684
|
});
|
|
705
685
|
return {
|
|
706
686
|
routing: {
|
|
@@ -708,7 +688,7 @@ function resolveActionRouting<TParams, TState>(input: {
|
|
|
708
688
|
routes,
|
|
709
689
|
choose: input.spec.choose,
|
|
710
690
|
},
|
|
711
|
-
transitions,
|
|
691
|
+
transitions: Object.values(routes),
|
|
712
692
|
};
|
|
713
693
|
}
|
|
714
694
|
const target = normalizeStaticRouteTarget(
|
|
@@ -716,11 +696,7 @@ function resolveActionRouting<TParams, TState>(input: {
|
|
|
716
696
|
input.spec,
|
|
717
697
|
input.knownTurnIds,
|
|
718
698
|
);
|
|
719
|
-
const route = {
|
|
720
|
-
...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
|
|
721
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
722
|
-
trigger: input.spec.trigger ?? input.actionId,
|
|
723
|
-
};
|
|
699
|
+
const route = { ...target, trigger: input.spec.trigger ?? input.actionId };
|
|
724
700
|
return {
|
|
725
701
|
routing: {
|
|
726
702
|
kind: "static",
|
|
@@ -765,23 +741,14 @@ function compileExternalSourceTransitions<TParams, TState>(input: {
|
|
|
765
741
|
spec: ExternalTurnDefinition<TParams, TState>;
|
|
766
742
|
knownTurnIds: ReadonlySet<TurnId>;
|
|
767
743
|
}): readonly ProcessTurnTransition[] {
|
|
768
|
-
|
|
769
|
-
throw new Error(`External turn '${input.turnId}' must declare at least one source transition`);
|
|
770
|
-
}
|
|
771
|
-
return input.spec.transitions.map((transition, index) => {
|
|
772
|
-
if (transition.source.kind.trim() === "") {
|
|
773
|
-
throw new Error(
|
|
774
|
-
`External turn '${input.turnId}' source transition ${index} has an empty kind`,
|
|
775
|
-
);
|
|
776
|
-
}
|
|
744
|
+
return compileDeclarations(input.spec.transitions, (transition, index) => {
|
|
777
745
|
const target = normalizeStaticRouteTarget(
|
|
778
746
|
`External turn '${input.turnId}' source '${transition.source.kind}'`,
|
|
779
747
|
transition,
|
|
780
748
|
input.knownTurnIds,
|
|
781
749
|
);
|
|
782
750
|
return {
|
|
783
|
-
...
|
|
784
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
751
|
+
...target,
|
|
785
752
|
trigger: getExternalSourceTransitionId({
|
|
786
753
|
turnId: input.turnId,
|
|
787
754
|
source: transition.source,
|
|
@@ -837,44 +804,21 @@ function buildActionPreviewAndScheduling(
|
|
|
837
804
|
preview?: ProcessActionPreviewDefinition;
|
|
838
805
|
scheduling?: ProcessActionSchedulingDefinition;
|
|
839
806
|
} {
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
const [transition] = transitions;
|
|
848
|
-
if (!transition) {
|
|
849
|
-
return undefined;
|
|
850
|
-
}
|
|
851
|
-
if (transition.lifecycleStatus !== undefined) {
|
|
852
|
-
return {
|
|
853
|
-
kind: "terminal",
|
|
854
|
-
lifecycleStatus: transition.lifecycleStatus,
|
|
855
|
-
} satisfies ProcessActionPreviewDefinition;
|
|
856
|
-
}
|
|
857
|
-
return {
|
|
858
|
-
kind: "trigger",
|
|
859
|
-
trigger: transition.trigger,
|
|
860
|
-
} satisfies ProcessActionPreviewDefinition;
|
|
861
|
-
})();
|
|
862
|
-
if (spec.preview && preview) {
|
|
863
|
-
validateActionPreviewMatchesTransitions({
|
|
864
|
-
turnId,
|
|
865
|
-
actionId,
|
|
866
|
-
preview,
|
|
867
|
-
transitions,
|
|
868
|
-
context: "preview",
|
|
869
|
-
});
|
|
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 };
|
|
870
814
|
}
|
|
871
|
-
if (spec.
|
|
815
|
+
if (preview && (spec.preview || spec.schedulable)) {
|
|
872
816
|
validateActionPreviewMatchesTransitions({
|
|
873
817
|
turnId,
|
|
874
818
|
actionId,
|
|
875
819
|
preview,
|
|
876
820
|
transitions,
|
|
877
|
-
context: "scheduling",
|
|
821
|
+
context: spec.preview ? "preview" : "scheduling",
|
|
878
822
|
});
|
|
879
823
|
}
|
|
880
824
|
if (spec.schedulable && !preview) {
|
|
@@ -888,93 +832,17 @@ function buildActionPreviewAndScheduling(
|
|
|
888
832
|
};
|
|
889
833
|
}
|
|
890
834
|
|
|
891
|
-
function
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
): ProcessEffectPlan<TState>
|
|
895
|
-
if (!left) {
|
|
896
|
-
return right;
|
|
897
|
-
}
|
|
898
|
-
if (!right) {
|
|
899
|
-
return left;
|
|
900
|
-
}
|
|
835
|
+
function withFormState<TState>(
|
|
836
|
+
effect: ProcessEffectPlan<TState> | undefined,
|
|
837
|
+
state: TState,
|
|
838
|
+
): ProcessEffectPlan<TState> {
|
|
901
839
|
return {
|
|
902
|
-
...
|
|
903
|
-
|
|
904
|
-
...(
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
...(
|
|
908
|
-
? { broadcasts: [...(left.broadcasts ?? []), ...(right.broadcasts ?? [])] }
|
|
909
|
-
: {}),
|
|
910
|
-
...(left.emit || right.emit ? { emit: [...(left.emit ?? []), ...(right.emit ?? [])] } : {}),
|
|
911
|
-
...(left.queueInput || right.queueInput
|
|
912
|
-
? { queueInput: [...(left.queueInput ?? []), ...(right.queueInput ?? [])] }
|
|
913
|
-
: {}),
|
|
914
|
-
};
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
async function buildSavePlanResultEffect<TParams, TState>(input: {
|
|
918
|
-
intent: SavePlanResultLifecycleIntent<TParams, TState>;
|
|
919
|
-
execution: ProcessOutcomeExecution<TParams, TState>;
|
|
920
|
-
}): Promise<ProcessEffectPlan<TState>> {
|
|
921
|
-
const statePlan = input.intent.state ? await input.intent.state(input.execution) : undefined;
|
|
922
|
-
const planRevision = input.execution.ctx.process.planRevision + 1;
|
|
923
|
-
const summary = normalizeEffectString(input.execution.event.params[input.intent.summaryParam]);
|
|
924
|
-
const acceptanceCriteria = normalizeEffectStringArray(
|
|
925
|
-
input.execution.event.params[input.intent.acceptanceCriteriaParam],
|
|
926
|
-
);
|
|
927
|
-
const planMarkdown =
|
|
928
|
-
typeof input.execution.event.turnResultMarkdown === "string"
|
|
929
|
-
? input.execution.event.turnResultMarkdown
|
|
930
|
-
: normalizeEffectString(input.execution.event.params[input.intent.planMarkdownParam]);
|
|
931
|
-
|
|
932
|
-
return mergeProcessEffectPlans(statePlan, {
|
|
933
|
-
processPatch: { planRevision },
|
|
934
|
-
broadcasts: [
|
|
935
|
-
{
|
|
936
|
-
type: input.intent.broadcastType,
|
|
937
|
-
payload: {
|
|
938
|
-
planRevision,
|
|
939
|
-
reviewState: "awaiting_approval",
|
|
940
|
-
approved: false,
|
|
941
|
-
summary,
|
|
942
|
-
},
|
|
943
|
-
},
|
|
944
|
-
],
|
|
945
|
-
emit: [
|
|
946
|
-
{
|
|
947
|
-
type: input.intent.emitEventType,
|
|
948
|
-
data: {
|
|
949
|
-
planRevision,
|
|
950
|
-
summary,
|
|
951
|
-
planMarkdown,
|
|
952
|
-
acceptanceCriteria,
|
|
953
|
-
},
|
|
954
|
-
},
|
|
955
|
-
],
|
|
956
|
-
}) as ProcessEffectPlan<TState>;
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
function resolveOutcomeEffect<TParams, TState>(input: {
|
|
960
|
-
spec: ProcessToolOutcomeSpec<TParams, TState> | ProcessTurnEndSpec<TParams, TState, string>;
|
|
961
|
-
}): ProcessOutcomeEffect<TParams, TState> | undefined {
|
|
962
|
-
const lifecycleIntent = "lifecycleIntent" in input.spec ? input.spec.lifecycleIntent : undefined;
|
|
963
|
-
if (!lifecycleIntent) {
|
|
964
|
-
return input.spec.effect;
|
|
965
|
-
}
|
|
966
|
-
return async (execution) => {
|
|
967
|
-
const explicitPlan = input.spec.effect ? await input.spec.effect(execution) : undefined;
|
|
968
|
-
let lifecyclePlan: ProcessEffectPlan<TState> | undefined;
|
|
969
|
-
switch (lifecycleIntent.kind) {
|
|
970
|
-
case "save_plan_result":
|
|
971
|
-
lifecyclePlan = await buildSavePlanResultEffect({
|
|
972
|
-
intent: lifecycleIntent,
|
|
973
|
-
execution,
|
|
974
|
-
});
|
|
975
|
-
break;
|
|
976
|
-
}
|
|
977
|
-
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] } : {}),
|
|
978
846
|
};
|
|
979
847
|
}
|
|
980
848
|
|
|
@@ -1119,52 +987,30 @@ function compileTurnOutcomeDefinitions<TParams, TState>(input: {
|
|
|
1119
987
|
const outcomeEntries = Object.entries(input.outcomes ?? {}) as Array<
|
|
1120
988
|
[string, ProcessToolOutcomeSpec<TParams, TState>]
|
|
1121
989
|
>;
|
|
1122
|
-
if (outcomeEntries.length > 0 && input.turnEnd) {
|
|
1123
|
-
throw new Error(
|
|
1124
|
-
`Turn '${input.turnId}' cannot declare both tool outcomes and a turnEnd result`,
|
|
1125
|
-
);
|
|
1126
|
-
}
|
|
1127
|
-
if (outcomeEntries.length === 0 && !input.turnEnd) {
|
|
1128
|
-
throw new Error(
|
|
1129
|
-
`Turn '${input.turnId}' must declare at least one tool outcome or a turnEnd result`,
|
|
1130
|
-
);
|
|
1131
|
-
}
|
|
1132
990
|
|
|
1133
991
|
const transitions: ProcessTurnTransition[] = [];
|
|
1134
992
|
const effects = new Map<string, ProcessOutcomeEffect<TParams, TState> | undefined>();
|
|
1135
993
|
const routings = new Map<string, CompiledOutcomeRouting<TParams, TState>>();
|
|
1136
|
-
|
|
994
|
+
compileDeclarations(outcomeEntries, ([outcome, spec]) => {
|
|
1137
995
|
if (outcome.trim() === "") {
|
|
1138
996
|
throw new Error(`Turn '${input.turnId}' declares an empty outcome id`);
|
|
1139
997
|
}
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
});
|
|
1146
|
-
const target = routing
|
|
1147
|
-
? null
|
|
1148
|
-
: buildOutcomeTransition(input.turnId, outcome, spec, input.knownTurnIds);
|
|
1149
|
-
if (routing) {
|
|
1150
|
-
routings.set(outcome, routing);
|
|
1151
|
-
for (const route of Object.values(routing.routes)) {
|
|
1152
|
-
transitions.push({
|
|
1153
|
-
...(route.nextTurnId ? { nextTurnId: route.nextTurnId } : {}),
|
|
1154
|
-
...(route.lifecycleStatus ? { lifecycleStatus: route.lifecycleStatus } : {}),
|
|
1155
|
-
trigger: route.trigger,
|
|
1156
|
-
outcome,
|
|
1157
|
-
});
|
|
1158
|
-
}
|
|
1159
|
-
} else if (target) {
|
|
1160
|
-
transitions.push({
|
|
1161
|
-
...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
|
|
1162
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
1163
|
-
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}'`,
|
|
1164
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 });
|
|
1165
1011
|
}
|
|
1166
|
-
effects.set(outcome,
|
|
1167
|
-
}
|
|
1012
|
+
effects.set(outcome, spec.effect);
|
|
1013
|
+
});
|
|
1168
1014
|
|
|
1169
1015
|
if (input.turnEnd) {
|
|
1170
1016
|
if (input.turnEnd.outcome.trim() === "") {
|
|
@@ -1176,12 +1022,8 @@ function compileTurnOutcomeDefinitions<TParams, TState>(input: {
|
|
|
1176
1022
|
input.turnEnd,
|
|
1177
1023
|
input.knownTurnIds,
|
|
1178
1024
|
);
|
|
1179
|
-
transitions.push({
|
|
1180
|
-
|
|
1181
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
1182
|
-
outcome: input.turnEnd.outcome,
|
|
1183
|
-
});
|
|
1184
|
-
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);
|
|
1185
1027
|
}
|
|
1186
1028
|
|
|
1187
1029
|
return {
|
|
@@ -1191,37 +1033,43 @@ function compileTurnOutcomeDefinitions<TParams, TState>(input: {
|
|
|
1191
1033
|
};
|
|
1192
1034
|
}
|
|
1193
1035
|
|
|
1194
|
-
function
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
}) {
|
|
1199
|
-
const
|
|
1200
|
-
if (
|
|
1201
|
-
|
|
1202
|
-
|
|
1036
|
+
function compileMappedTurnTransitions<TParams, TState>(input: {
|
|
1037
|
+
turnId: TurnId;
|
|
1038
|
+
spec: LlmTurnDefinition<string, TParams, TState>;
|
|
1039
|
+
knownTurnIds: ReadonlySet<TurnId>;
|
|
1040
|
+
}): readonly ProcessTurnTransition[] {
|
|
1041
|
+
const mapped = input.spec.forEach;
|
|
1042
|
+
if (!mapped) return [];
|
|
1043
|
+
const routing = mapped.routing;
|
|
1044
|
+
if (routing.kind === "static") {
|
|
1045
|
+
const target = normalizeStaticRouteTarget(
|
|
1046
|
+
`Mapped turn '${input.turnId}' collection`,
|
|
1047
|
+
routing,
|
|
1048
|
+
input.knownTurnIds,
|
|
1203
1049
|
);
|
|
1050
|
+
return [{ ...target, trigger: mappedCollectTrigger() }];
|
|
1204
1051
|
}
|
|
1205
|
-
|
|
1206
|
-
|
|
1052
|
+
return compileDeclarations(Object.entries(routing.branches), ([branchId, to]) => {
|
|
1053
|
+
if (branchId.trim() === "") {
|
|
1054
|
+
throw new Error(`Mapped turn '${input.turnId}' collection contains an empty branch id`);
|
|
1055
|
+
}
|
|
1056
|
+
const target = normalizeStaticRouteTarget(
|
|
1057
|
+
`Mapped turn '${input.turnId}' collection branch '${branchId}'`,
|
|
1058
|
+
{ to },
|
|
1059
|
+
input.knownTurnIds,
|
|
1060
|
+
);
|
|
1061
|
+
return { ...target, trigger: mappedCollectTrigger(branchId) };
|
|
1062
|
+
});
|
|
1207
1063
|
}
|
|
1208
1064
|
|
|
1209
1065
|
type DerivedHumanTurnAction<TParams, TState> = {
|
|
1210
1066
|
actionId: string;
|
|
1211
1067
|
actionSpec: ProcessHumanTurnActionSpec<TParams, TState>;
|
|
1212
1068
|
routing: CompiledActionRouting<TParams, TState>;
|
|
1213
|
-
transitions: readonly NormalizedActionRoute[];
|
|
1214
1069
|
actionView: HumanTurnActionView;
|
|
1215
1070
|
externalTriggers: readonly HumanTurnExternalTrigger[];
|
|
1216
1071
|
};
|
|
1217
1072
|
|
|
1218
|
-
type DerivedHumanTurnExternalAction<TParams, TState> = {
|
|
1219
|
-
externalActionId: string;
|
|
1220
|
-
actionSpec: ProcessHumanTurnExternalActionSpec<TParams, TState>;
|
|
1221
|
-
transition: NormalizedActionRoute;
|
|
1222
|
-
view: HumanTurnExternalActionView;
|
|
1223
|
-
};
|
|
1224
|
-
|
|
1225
1073
|
function hasDeclaredStaticRouteTarget(target: StaticRouteTarget): boolean {
|
|
1226
1074
|
return (
|
|
1227
1075
|
target.to !== undefined || target.complete === true || target.lifecycleStatus !== undefined
|
|
@@ -1234,27 +1082,13 @@ function resolveExternalActionRoute<TParams, TState>(input: {
|
|
|
1234
1082
|
spec: ProcessHumanTurnExternalActionSpec<TParams, TState>;
|
|
1235
1083
|
knownTurnIds?: ReadonlySet<TurnId>;
|
|
1236
1084
|
}): NormalizedActionRoute {
|
|
1237
|
-
if (input.externalActionId.trim() === "") {
|
|
1238
|
-
throw new Error(`Human turn '${input.turnId}' declares an empty external action id`);
|
|
1239
|
-
}
|
|
1240
|
-
if (input.spec.id !== input.externalActionId) {
|
|
1241
|
-
throw new Error(
|
|
1242
|
-
`Human turn '${input.turnId}' external action '${input.externalActionId}' has mismatched spec id '${input.spec.id}'`,
|
|
1243
|
-
);
|
|
1244
|
-
}
|
|
1245
|
-
if (input.spec.source.kind.trim() === "") {
|
|
1246
|
-
throw new Error(
|
|
1247
|
-
`Human turn '${input.turnId}' external action '${input.externalActionId}' must declare a non-empty source kind`,
|
|
1248
|
-
);
|
|
1249
|
-
}
|
|
1250
1085
|
const target = normalizeStaticRouteTarget(
|
|
1251
1086
|
`Human turn '${input.turnId}' external action '${input.externalActionId}'`,
|
|
1252
1087
|
input.spec,
|
|
1253
1088
|
input.knownTurnIds,
|
|
1254
1089
|
);
|
|
1255
1090
|
return {
|
|
1256
|
-
...
|
|
1257
|
-
...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
|
|
1091
|
+
...target,
|
|
1258
1092
|
trigger: getExternalActionTransitionTrigger({ externalActionId: input.externalActionId }),
|
|
1259
1093
|
};
|
|
1260
1094
|
}
|
|
@@ -1323,44 +1157,25 @@ function deriveHumanTurnActions<TParams, TState>(input: {
|
|
|
1323
1157
|
spec: HumanTurnDefinition<TParams, TState>;
|
|
1324
1158
|
knownTurnIds?: ReadonlySet<TurnId>;
|
|
1325
1159
|
turnDefinitionsById?: ReadonlyMap<TurnId, TurnDefinition<unknown, unknown>>;
|
|
1326
|
-
requireHumanActions?: boolean;
|
|
1327
1160
|
}): {
|
|
1328
1161
|
actions: readonly DerivedHumanTurnAction<TParams, TState>[];
|
|
1329
|
-
externalActions: readonly DerivedHumanTurnExternalAction<TParams, TState>[];
|
|
1330
1162
|
transitions: readonly ProcessTurnTransition[];
|
|
1331
1163
|
} {
|
|
1332
|
-
const actionEntries = Object.entries(input.spec.actions)
|
|
1333
|
-
[string, ProcessHumanTurnActionSpec<TParams, TState>]
|
|
1334
|
-
>;
|
|
1335
|
-
if (actionEntries.length === 0 && input.knownTurnIds && input.requireHumanActions !== false) {
|
|
1336
|
-
throw new Error(`Human turn '${input.turnId}' must declare at least one action`);
|
|
1337
|
-
}
|
|
1338
|
-
|
|
1164
|
+
const actionEntries = Object.entries(input.spec.actions);
|
|
1339
1165
|
const derivedActions: DerivedHumanTurnAction<TParams, TState>[] = [];
|
|
1340
1166
|
const transitions: ProcessTurnTransition[] = [];
|
|
1341
1167
|
const actionIdByTrigger = new Map<string, string>();
|
|
1342
|
-
|
|
1168
|
+
compileDeclarations(actionEntries, ([actionId, actionSpec]) => {
|
|
1343
1169
|
if (actionId.trim() === "") {
|
|
1344
1170
|
throw new Error(`Human turn '${input.turnId}' declares an empty action id`);
|
|
1345
1171
|
}
|
|
1346
1172
|
validateActionPrimaryPrompt(input.turnId, actionId, actionSpec);
|
|
1347
|
-
const resolved =
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
kind: "static",
|
|
1354
|
-
route: { trigger: actionSpec.trigger ?? actionId },
|
|
1355
|
-
} satisfies CompiledActionRouting<TParams, TState>,
|
|
1356
|
-
transitions: [] as readonly NormalizedActionRoute[],
|
|
1357
|
-
}
|
|
1358
|
-
: resolveActionRouting({
|
|
1359
|
-
turnId: input.turnId,
|
|
1360
|
-
actionId,
|
|
1361
|
-
spec: actionSpec,
|
|
1362
|
-
knownTurnIds: input.knownTurnIds,
|
|
1363
|
-
});
|
|
1173
|
+
const resolved = resolveActionRouting({
|
|
1174
|
+
turnId: input.turnId,
|
|
1175
|
+
actionId,
|
|
1176
|
+
spec: actionSpec,
|
|
1177
|
+
knownTurnIds: input.knownTurnIds,
|
|
1178
|
+
});
|
|
1364
1179
|
for (const transition of resolved.transitions) {
|
|
1365
1180
|
const existingActionId = actionIdByTrigger.get(transition.trigger);
|
|
1366
1181
|
if (existingActionId && existingActionId !== actionId) {
|
|
@@ -1369,26 +1184,18 @@ function deriveHumanTurnActions<TParams, TState>(input: {
|
|
|
1369
1184
|
);
|
|
1370
1185
|
}
|
|
1371
1186
|
actionIdByTrigger.set(transition.trigger, actionId);
|
|
1372
|
-
transitions.push({
|
|
1373
|
-
...(transition.nextTurnId ? { nextTurnId: transition.nextTurnId } : {}),
|
|
1374
|
-
...(transition.lifecycleStatus ? { lifecycleStatus: transition.lifecycleStatus } : {}),
|
|
1375
|
-
trigger: transition.trigger,
|
|
1376
|
-
});
|
|
1187
|
+
transitions.push({ ...transition });
|
|
1377
1188
|
}
|
|
1378
|
-
const previewAndScheduling =
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
: {}),
|
|
1385
|
-
}
|
|
1386
|
-
: buildActionPreviewAndScheduling(input.turnId, actionId, actionSpec, resolved.transitions);
|
|
1189
|
+
const previewAndScheduling = buildActionPreviewAndScheduling(
|
|
1190
|
+
input.turnId,
|
|
1191
|
+
actionId,
|
|
1192
|
+
actionSpec,
|
|
1193
|
+
resolved.transitions,
|
|
1194
|
+
);
|
|
1387
1195
|
derivedActions.push({
|
|
1388
1196
|
actionId,
|
|
1389
1197
|
actionSpec,
|
|
1390
1198
|
routing: resolved.routing,
|
|
1391
|
-
transitions: resolved.transitions,
|
|
1392
1199
|
actionView: {
|
|
1393
1200
|
actionId,
|
|
1394
1201
|
label: actionSpec.label,
|
|
@@ -1404,40 +1211,30 @@ function deriveHumanTurnActions<TParams, TState>(input: {
|
|
|
1404
1211
|
description: trigger.description,
|
|
1405
1212
|
})) ?? [],
|
|
1406
1213
|
});
|
|
1407
|
-
}
|
|
1214
|
+
});
|
|
1408
1215
|
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
);
|
|
1427
|
-
}
|
|
1428
|
-
}
|
|
1429
|
-
validateExternalActionPublishedInputTarget({
|
|
1430
|
-
turnId: input.turnId,
|
|
1431
|
-
externalActionId,
|
|
1432
|
-
spec: actionSpec,
|
|
1433
|
-
turnDefinitionsById: input.turnDefinitionsById,
|
|
1434
|
-
});
|
|
1435
|
-
const transition = resolveExternalActionRoute({
|
|
1436
|
-
turnId: input.turnId,
|
|
1437
|
-
externalActionId,
|
|
1438
|
-
spec: actionSpec,
|
|
1439
|
-
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,
|
|
1440
1233
|
});
|
|
1234
|
+
if (error) throw new Error(error);
|
|
1235
|
+
const route = { ...input, externalActionId, spec: actionSpec };
|
|
1236
|
+
validateExternalActionPublishedInputTarget(route);
|
|
1237
|
+
const transition = resolveExternalActionRoute(route);
|
|
1441
1238
|
const existingActionId = actionIdByTrigger.get(transition.trigger);
|
|
1442
1239
|
if (existingActionId) {
|
|
1443
1240
|
throw new Error(
|
|
@@ -1445,30 +1242,8 @@ function deriveHumanTurnActions<TParams, TState>(input: {
|
|
|
1445
1242
|
);
|
|
1446
1243
|
}
|
|
1447
1244
|
actionIdByTrigger.set(transition.trigger, externalActionId);
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
...(transition.lifecycleStatus ? { lifecycleStatus: transition.lifecycleStatus } : {}),
|
|
1451
|
-
trigger: transition.trigger,
|
|
1452
|
-
});
|
|
1453
|
-
derivedExternalActions.push({
|
|
1454
|
-
externalActionId,
|
|
1455
|
-
actionSpec,
|
|
1456
|
-
transition,
|
|
1457
|
-
view: {
|
|
1458
|
-
id: getExternalActionArmingId({ turnId: input.turnId, externalActionId }),
|
|
1459
|
-
externalActionId,
|
|
1460
|
-
sourceKind: actionSpec.source.kind,
|
|
1461
|
-
label: actionSpec.label ?? actionSpec.source.label ?? null,
|
|
1462
|
-
description: actionSpec.description ?? actionSpec.source.description ?? null,
|
|
1463
|
-
},
|
|
1464
|
-
});
|
|
1465
|
-
}
|
|
1466
|
-
|
|
1467
|
-
return {
|
|
1468
|
-
actions: derivedActions,
|
|
1469
|
-
externalActions: derivedExternalActions,
|
|
1470
|
-
transitions,
|
|
1471
|
-
};
|
|
1245
|
+
return transition;
|
|
1246
|
+
});
|
|
1472
1247
|
}
|
|
1473
1248
|
|
|
1474
1249
|
/** @internal */
|
|
@@ -1482,8 +1257,6 @@ export function resolveHumanTurnView<TParams, TState>(input: {
|
|
|
1482
1257
|
actions: readonly HumanTurnActionView[];
|
|
1483
1258
|
/** @internal */
|
|
1484
1259
|
externalTriggers: readonly HumanTurnExternalTrigger[];
|
|
1485
|
-
/** @internal */
|
|
1486
|
-
externalActions: readonly HumanTurnExternalActionView[];
|
|
1487
1260
|
} {
|
|
1488
1261
|
const derived = deriveHumanTurnActions({
|
|
1489
1262
|
turnId: input.turnId,
|
|
@@ -1492,7 +1265,6 @@ export function resolveHumanTurnView<TParams, TState>(input: {
|
|
|
1492
1265
|
return {
|
|
1493
1266
|
actions: derived.actions.map((action) => action.actionView),
|
|
1494
1267
|
externalTriggers: derived.actions.flatMap((action) => action.externalTriggers),
|
|
1495
|
-
externalActions: derived.externalActions.map((action) => action.view),
|
|
1496
1268
|
};
|
|
1497
1269
|
}
|
|
1498
1270
|
|
|
@@ -1502,49 +1274,21 @@ function compileHumanTurn<TParams, TState>(input: {
|
|
|
1502
1274
|
knownTurnIds: ReadonlySet<TurnId>;
|
|
1503
1275
|
turnDefinitionsById: ReadonlyMap<TurnId, TurnDefinition<unknown, unknown>>;
|
|
1504
1276
|
actionUses: Map<string, CompiledActionUse<TParams, TState>[]>;
|
|
1505
|
-
}): {
|
|
1506
|
-
|
|
1507
|
-
} {
|
|
1508
|
-
const derived = deriveHumanTurnActions({
|
|
1509
|
-
turnId: input.turnId,
|
|
1510
|
-
spec: input.spec,
|
|
1511
|
-
knownTurnIds: input.knownTurnIds,
|
|
1512
|
-
turnDefinitionsById: input.turnDefinitionsById,
|
|
1513
|
-
});
|
|
1277
|
+
}): ProcessTurnBinding<HumanTurnDefinition<TParams, TState>> {
|
|
1278
|
+
const derived = deriveHumanTurnActions(input);
|
|
1514
1279
|
for (const action of derived.actions) {
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
...(action.actionSpec.effect ? { effect: action.actionSpec.effect } : {}),
|
|
1523
|
-
routing: action.routing,
|
|
1524
|
-
},
|
|
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,
|
|
1525
1287
|
});
|
|
1288
|
+
input.actionUses.set(action.actionId, uses);
|
|
1526
1289
|
}
|
|
1527
1290
|
|
|
1528
|
-
return
|
|
1529
|
-
binding: createProcessTurnBinding(input.spec, derived.transitions),
|
|
1530
|
-
};
|
|
1531
|
-
}
|
|
1532
|
-
|
|
1533
|
-
function compileExternalTurn<TParams, TState>(input: {
|
|
1534
|
-
turnId: TurnId;
|
|
1535
|
-
spec: ExternalTurnDefinition<TParams, TState>;
|
|
1536
|
-
knownTurnIds: ReadonlySet<TurnId>;
|
|
1537
|
-
}): {
|
|
1538
|
-
binding: ProcessTurnBinding<ExternalTurnDefinition<TParams, TState>>;
|
|
1539
|
-
} {
|
|
1540
|
-
const transitions = compileExternalSourceTransitions({
|
|
1541
|
-
turnId: input.turnId,
|
|
1542
|
-
spec: input.spec,
|
|
1543
|
-
knownTurnIds: input.knownTurnIds,
|
|
1544
|
-
});
|
|
1545
|
-
return {
|
|
1546
|
-
binding: createProcessTurnBinding(input.spec, transitions),
|
|
1547
|
-
};
|
|
1291
|
+
return createProcessTurnBinding(input.spec, derived.transitions);
|
|
1548
1292
|
}
|
|
1549
1293
|
|
|
1550
1294
|
function resolveActionRouteForExecution<TParams, TState>(input: {
|
|
@@ -1567,12 +1311,7 @@ function resolveActionRouteForExecution<TParams, TState>(input: {
|
|
|
1567
1311
|
};
|
|
1568
1312
|
const routes = input.use.routing.routes;
|
|
1569
1313
|
return Promise.resolve(input.use.routing.choose(actionExecution)).then((selectedBranchId) => {
|
|
1570
|
-
|
|
1571
|
-
throw new Error(
|
|
1572
|
-
`Action '${input.actionId}' on turn '${input.use.turnId}' selected unknown branch '${selectedBranchId}'`,
|
|
1573
|
-
);
|
|
1574
|
-
}
|
|
1575
|
-
const route = routes[selectedBranchId];
|
|
1314
|
+
const route = Object.hasOwn(routes, selectedBranchId) ? routes[selectedBranchId] : undefined;
|
|
1576
1315
|
if (!route) {
|
|
1577
1316
|
throw new Error(
|
|
1578
1317
|
`Action '${input.actionId}' on turn '${input.use.turnId}' selected unknown branch '${selectedBranchId}'`,
|
|
@@ -1597,14 +1336,13 @@ function buildActionTransitionRequest<TState>(route: NormalizedActionRoute) {
|
|
|
1597
1336
|
}
|
|
1598
1337
|
|
|
1599
1338
|
function validateHappyPathConnectivity<TParams, TState>(input: {
|
|
1600
|
-
processId: string;
|
|
1601
1339
|
happyPath: readonly TurnId[] | undefined;
|
|
1602
1340
|
turns: ReadonlyMap<TurnId, ProcessTurnBinding<TurnDefinition<TParams, TState>>>;
|
|
1603
|
-
|
|
1604
|
-
|
|
1341
|
+
}): string[] {
|
|
1342
|
+
const errors: string[] = [];
|
|
1605
1343
|
const happyPath = input.happyPath;
|
|
1606
1344
|
if (!happyPath || happyPath.length < 2) {
|
|
1607
|
-
return;
|
|
1345
|
+
return errors;
|
|
1608
1346
|
}
|
|
1609
1347
|
const happyPathTurns = new Set<TurnId>(happyPath);
|
|
1610
1348
|
for (let index = 0; index < happyPath.length - 1; index += 1) {
|
|
@@ -1617,7 +1355,7 @@ function validateHappyPathConnectivity<TParams, TState>(input: {
|
|
|
1617
1355
|
const current = queue.shift() as TurnId;
|
|
1618
1356
|
for (const transition of getProcessTurnTransitions(input.turns.get(current))) {
|
|
1619
1357
|
const next = transition.nextTurnId;
|
|
1620
|
-
if (next === undefined || !input.
|
|
1358
|
+
if (next === undefined || !input.turns.has(next)) {
|
|
1621
1359
|
continue;
|
|
1622
1360
|
}
|
|
1623
1361
|
if (next === target) {
|
|
@@ -1627,7 +1365,7 @@ function validateHappyPathConnectivity<TParams, TState>(input: {
|
|
|
1627
1365
|
if (visited.has(next) || happyPathTurns.has(next)) {
|
|
1628
1366
|
continue;
|
|
1629
1367
|
}
|
|
1630
|
-
const definition = input.
|
|
1368
|
+
const definition = input.turns.get(next)?.definition;
|
|
1631
1369
|
if (definition?.kind !== "human") {
|
|
1632
1370
|
continue;
|
|
1633
1371
|
}
|
|
@@ -1636,60 +1374,122 @@ function validateHappyPathConnectivity<TParams, TState>(input: {
|
|
|
1636
1374
|
}
|
|
1637
1375
|
}
|
|
1638
1376
|
if (!connected) {
|
|
1639
|
-
|
|
1640
|
-
`
|
|
1377
|
+
errors.push(
|
|
1378
|
+
`happy path segment '${source}' -> '${target}' is not connected by declared transitions`,
|
|
1641
1379
|
);
|
|
1642
1380
|
}
|
|
1643
1381
|
}
|
|
1382
|
+
return errors;
|
|
1644
1383
|
}
|
|
1645
1384
|
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
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}`);
|
|
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
|
+
}
|
|
1652
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);
|
|
1653
1450
|
const knownTurnIds = new Set(turnEntries.map(([turnId]) => turnId));
|
|
1654
1451
|
const turnDefinitionsById = new Map<TurnId, TurnDefinition<unknown, unknown>>(
|
|
1655
1452
|
turnEntries.map(([turnId, turnSpec]) => [turnId, turnSpec as TurnDefinition<unknown, unknown>]),
|
|
1656
1453
|
);
|
|
1657
|
-
if (
|
|
1658
|
-
|
|
1454
|
+
if (turnEntries.length === 0) {
|
|
1455
|
+
diagnostics.add("must declare at least one turn");
|
|
1659
1456
|
}
|
|
1660
1457
|
const seenEntries = new Set<TurnId>();
|
|
1661
1458
|
for (const turnId of [input.entry, ...(input.alternateEntries ?? [])]) {
|
|
1662
1459
|
if (seenEntries.has(turnId)) {
|
|
1663
|
-
|
|
1460
|
+
diagnostics.add(`declares duplicate entry turn '${turnId}'`);
|
|
1664
1461
|
}
|
|
1665
1462
|
if (!knownTurnIds.has(turnId)) {
|
|
1666
|
-
|
|
1463
|
+
diagnostics.add(`entry turn '${turnId}' is not declared in turns`);
|
|
1667
1464
|
}
|
|
1668
1465
|
seenEntries.add(turnId);
|
|
1669
1466
|
}
|
|
1670
1467
|
|
|
1468
|
+
let validHappyPath = true;
|
|
1671
1469
|
if (input.happyPath) {
|
|
1470
|
+
const before = diagnostics.errors.length;
|
|
1672
1471
|
if (input.happyPath.length === 0) {
|
|
1673
|
-
|
|
1472
|
+
diagnostics.add("declares an empty happy path");
|
|
1674
1473
|
}
|
|
1675
1474
|
const seen = new Set<TurnId>();
|
|
1676
1475
|
for (const turnId of input.happyPath) {
|
|
1677
1476
|
if (!knownTurnIds.has(turnId)) {
|
|
1678
|
-
|
|
1477
|
+
diagnostics.add(`happy path references undeclared turn '${turnId}'`);
|
|
1679
1478
|
}
|
|
1680
1479
|
if (seen.has(turnId)) {
|
|
1681
|
-
|
|
1480
|
+
diagnostics.add(`happy path repeats turn '${turnId}'`);
|
|
1682
1481
|
}
|
|
1683
1482
|
seen.add(turnId);
|
|
1684
1483
|
}
|
|
1685
|
-
if (input.happyPath[0] !== input.entry) {
|
|
1686
|
-
|
|
1687
|
-
`Process '${input.id}' happy path must start at the entry turn '${input.entry}'`,
|
|
1688
|
-
);
|
|
1484
|
+
if (input.happyPath.length > 0 && input.happyPath[0] !== input.entry) {
|
|
1485
|
+
diagnostics.add(`happy path must start at the entry turn '${input.entry}'`);
|
|
1689
1486
|
}
|
|
1487
|
+
validHappyPath = before === diagnostics.errors.length;
|
|
1690
1488
|
}
|
|
1691
1489
|
|
|
1692
|
-
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
|
+
);
|
|
1693
1493
|
const actionUses = new Map<string, CompiledActionUse<TParams, TState>[]>();
|
|
1694
1494
|
const executableTurns = new Map<
|
|
1695
1495
|
TurnId,
|
|
@@ -1697,64 +1497,174 @@ function buildDefinedProcess<TParams, TState>(
|
|
|
1697
1497
|
spec: RoutableTurnDefinition<string, TParams, TState>;
|
|
1698
1498
|
}
|
|
1699
1499
|
>();
|
|
1700
|
-
|
|
1500
|
+
let validTurns = true;
|
|
1501
|
+
let inspectableProducts = true;
|
|
1701
1502
|
for (const [turnId, turnSpec] of turnEntries) {
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1503
|
+
const errors = diagnostics.capture(() => validateTurnDefinition(turnId, turnSpec), turnId);
|
|
1504
|
+
if (errors === undefined) {
|
|
1505
|
+
validTurns = false;
|
|
1506
|
+
inspectableProducts = false;
|
|
1507
|
+
continue;
|
|
1508
|
+
}
|
|
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
|
+
}
|
|
1709
1529
|
const externalTransitions =
|
|
1710
|
-
turnSpec.kind === "automatic" && turnSpec.externalActions
|
|
1711
|
-
?
|
|
1530
|
+
(turnSpec.kind === "llm" || turnSpec.kind === "automatic") && turnSpec.externalActions
|
|
1531
|
+
? compileExternalActionTransitions({
|
|
1712
1532
|
turnId,
|
|
1713
|
-
spec:
|
|
1714
|
-
kind: "human",
|
|
1715
|
-
description: turnSpec.description,
|
|
1716
|
-
actions: {},
|
|
1717
|
-
externalActions: turnSpec.externalActions,
|
|
1718
|
-
},
|
|
1533
|
+
spec: turnSpec,
|
|
1719
1534
|
knownTurnIds,
|
|
1720
1535
|
turnDefinitionsById,
|
|
1721
|
-
|
|
1722
|
-
}).transitions
|
|
1536
|
+
})
|
|
1723
1537
|
: [];
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
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
|
+
}
|
|
1731
1567
|
|
|
1732
|
-
|
|
1733
|
-
|
|
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
|
+
}
|
|
1579
|
+
|
|
1580
|
+
const transitions = compileExternalSourceTransitions({
|
|
1734
1581
|
turnId,
|
|
1735
1582
|
spec: turnSpec,
|
|
1736
1583
|
knownTurnIds,
|
|
1737
|
-
turnDefinitionsById,
|
|
1738
|
-
actionUses,
|
|
1739
1584
|
});
|
|
1740
|
-
turns.set(turnId,
|
|
1741
|
-
|
|
1585
|
+
turns.set(turnId, createProcessTurnBinding(turnSpec, transitions));
|
|
1586
|
+
} catch (error) {
|
|
1587
|
+
validTurns = false;
|
|
1588
|
+
diagnostics.record(error, turnId);
|
|
1742
1589
|
}
|
|
1590
|
+
}
|
|
1743
1591
|
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
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
|
+
}
|
|
1750
1602
|
}
|
|
1751
1603
|
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
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("; "));
|
|
1758
1668
|
|
|
1759
1669
|
const worker: ExtensionProcessDefinition<TParams, TState>["worker"] = (api) => {
|
|
1760
1670
|
api.start(input.entry);
|
|
@@ -1788,13 +1698,6 @@ function buildDefinedProcess<TParams, TState>(
|
|
|
1788
1698
|
for (const [actionId, uses] of actionUses) {
|
|
1789
1699
|
const [firstUse] = uses;
|
|
1790
1700
|
const form = firstUse?.form;
|
|
1791
|
-
for (const use of uses) {
|
|
1792
|
-
if (!areFormsEquivalent(form, use.form)) {
|
|
1793
|
-
throw new Error(
|
|
1794
|
-
`Action '${actionId}' must use the same form on every turn that references it`,
|
|
1795
|
-
);
|
|
1796
|
-
}
|
|
1797
|
-
}
|
|
1798
1701
|
api.action({
|
|
1799
1702
|
id: actionId,
|
|
1800
1703
|
label: firstUse?.label ?? actionId,
|
|
@@ -1829,9 +1732,7 @@ function buildDefinedProcess<TParams, TState>(
|
|
|
1829
1732
|
inputValue,
|
|
1830
1733
|
state: effectResult?.state ?? ctx.state,
|
|
1831
1734
|
});
|
|
1832
|
-
const result = formState
|
|
1833
|
-
? mergeProcessEffectPlans(effectResult, { state: formState })
|
|
1834
|
-
: effectResult;
|
|
1735
|
+
const result = formState ? withFormState(effectResult, formState) : effectResult;
|
|
1835
1736
|
await applyProcessEffectPlan({
|
|
1836
1737
|
ctx,
|
|
1837
1738
|
result,
|
|
@@ -1857,7 +1758,13 @@ function buildDefinedProcess<TParams, TState>(
|
|
|
1857
1758
|
const result = effect ? await effect(execution) : undefined;
|
|
1858
1759
|
let transition: Omit<ServerTransitionRequest<TState>, "state"> | undefined;
|
|
1859
1760
|
if (routing) {
|
|
1860
|
-
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
|
+
});
|
|
1861
1768
|
const route = routing.routes[branchId];
|
|
1862
1769
|
if (!route) {
|
|
1863
1770
|
throw new Error(
|