@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/index.ts
CHANGED
|
@@ -50,6 +50,7 @@ export {
|
|
|
50
50
|
type ProcessLaunchPlanServiceLike,
|
|
51
51
|
type ProcessModelSelectionPreviewResultLike,
|
|
52
52
|
type ProcessModelSelectionServiceLike,
|
|
53
|
+
type ProcessObservationUpdate,
|
|
53
54
|
type ProcessProjectRepoLike,
|
|
54
55
|
type ProcessQuestionServiceLike,
|
|
55
56
|
type ProcessWatcherServiceLike,
|
|
@@ -57,8 +58,10 @@ export {
|
|
|
57
58
|
type ProgrammaticLaunchResultLike,
|
|
58
59
|
type QueuedProcessInputLike,
|
|
59
60
|
type RegisteredProcessWatcherLike,
|
|
61
|
+
type WaitingProcessRestart,
|
|
60
62
|
type WatcherLaunchResultLike,
|
|
61
63
|
} from "./core-capabilities.js";
|
|
64
|
+
export type { OutcomeParameterContext } from "./define-process.js";
|
|
62
65
|
export {
|
|
63
66
|
type AutomaticTurnDefinition,
|
|
64
67
|
automaticTurn,
|
|
@@ -69,7 +72,9 @@ export {
|
|
|
69
72
|
getExternalSourceTransitionId,
|
|
70
73
|
type HumanTurnDefinition,
|
|
71
74
|
humanTurn,
|
|
75
|
+
type LlmOutcomeSubmission,
|
|
72
76
|
type LlmTurnDefinition,
|
|
77
|
+
type LlmTurnPreparationContext,
|
|
73
78
|
llmTurn,
|
|
74
79
|
type ProcessEffectPlan,
|
|
75
80
|
type ProcessHumanTurnExternalActionSpec,
|
|
@@ -78,6 +83,7 @@ export {
|
|
|
78
83
|
resolveHumanTurnView,
|
|
79
84
|
routeTurnOutcomes,
|
|
80
85
|
type TurnDefinition,
|
|
86
|
+
validateProcessDefinition,
|
|
81
87
|
} from "./define-process.js";
|
|
82
88
|
export { createEventBus } from "./event-bus.js";
|
|
83
89
|
export type {
|
|
@@ -124,6 +130,7 @@ export type {
|
|
|
124
130
|
TicketCreationDestinationSnapshot,
|
|
125
131
|
TicketCreationDestinationSummary,
|
|
126
132
|
TicketCreationReceipt,
|
|
133
|
+
TurnDiagnosticReport,
|
|
127
134
|
UiLauncherDefinition,
|
|
128
135
|
UiLauncherSummary,
|
|
129
136
|
WorkerCompleteInput,
|
|
@@ -132,21 +139,38 @@ export type {
|
|
|
132
139
|
WorkerRunHandle,
|
|
133
140
|
WorkerTurnHandler,
|
|
134
141
|
} from "./extension-api.js";
|
|
135
|
-
export {
|
|
142
|
+
export {
|
|
143
|
+
findUiLauncherById,
|
|
144
|
+
SafeLaunchPreparationError,
|
|
145
|
+
SafeOutcomePlanningError,
|
|
146
|
+
} from "./extension-api.js";
|
|
136
147
|
export { defineExternalActionSource } from "./external-action-source.js";
|
|
137
148
|
export {
|
|
138
149
|
createExternalSourcePollReporter,
|
|
139
150
|
type ExternalSourcePollReporter,
|
|
140
151
|
} from "./external-source-poll.js";
|
|
152
|
+
export {
|
|
153
|
+
consumeTriggerFile,
|
|
154
|
+
createFileExternalSourceProvider,
|
|
155
|
+
defineFileExternalSource,
|
|
156
|
+
type FileExternalInput,
|
|
157
|
+
readTriggerFile,
|
|
158
|
+
} from "./file-external-source.js";
|
|
141
159
|
export {
|
|
142
160
|
AutomaticOutcomeBuilder,
|
|
143
161
|
createFlowPromptContext,
|
|
144
162
|
type FlowAutomaticRunContext,
|
|
163
|
+
type FlowForEachOptions,
|
|
145
164
|
FlowFragmentBuilder,
|
|
146
165
|
type FlowLlmPreparationContext,
|
|
166
|
+
type FlowMappedPreparationContext,
|
|
167
|
+
type FlowMappedPromptContext,
|
|
147
168
|
type FlowPromptContext,
|
|
148
169
|
flow,
|
|
149
170
|
HumanFlowBuilder,
|
|
171
|
+
MappedCollectBuilder,
|
|
172
|
+
MappedLlmFlowBuilder,
|
|
173
|
+
MappedOutcomeBuilder,
|
|
150
174
|
} from "./flow.js";
|
|
151
175
|
export { atomicWriteUtf8, hasErrorCode, isEnoent, isPathInside } from "./fs-utils.js";
|
|
152
176
|
export {
|
|
@@ -168,6 +192,23 @@ export {
|
|
|
168
192
|
normalizeLeafOutcomeCaptureResult,
|
|
169
193
|
validateLeafOutcomeCaptureResult,
|
|
170
194
|
} from "./leaf-outcomes.js";
|
|
195
|
+
export {
|
|
196
|
+
collectMappedResults,
|
|
197
|
+
type FrozenMappedItem,
|
|
198
|
+
freezeMappedItems,
|
|
199
|
+
MAPPED_COLLECT_TRIGGER,
|
|
200
|
+
type MappedCollect,
|
|
201
|
+
type MappedItemInput,
|
|
202
|
+
type MappedItemYield,
|
|
203
|
+
type MappedLlmTurnSpec,
|
|
204
|
+
type MappedOutcomeContext,
|
|
205
|
+
type MappedSnapshotInput,
|
|
206
|
+
type MappedTurnItemContext,
|
|
207
|
+
type MappedTurnIteration,
|
|
208
|
+
type MappedTurnServerContext,
|
|
209
|
+
type MappedYieldInput,
|
|
210
|
+
yieldMappedItemResult,
|
|
211
|
+
} from "./mapped-turn.js";
|
|
171
212
|
export {
|
|
172
213
|
builtinPiProvider,
|
|
173
214
|
type ConfiguredProviderModel,
|
|
@@ -198,6 +239,20 @@ export {
|
|
|
198
239
|
piWorker,
|
|
199
240
|
resolveProviderOptions,
|
|
200
241
|
} from "./model-provider.js";
|
|
242
|
+
export {
|
|
243
|
+
applyOutcomeContract,
|
|
244
|
+
type ResolvedOutcomeContract,
|
|
245
|
+
resolveOutcomeContract,
|
|
246
|
+
} from "./outcome-contract.js";
|
|
247
|
+
export {
|
|
248
|
+
assertOutcomeSchema,
|
|
249
|
+
compileOutcomeParameters,
|
|
250
|
+
type OutcomeSchemaIssue,
|
|
251
|
+
outcomeCallParameters,
|
|
252
|
+
outcomeParameterSchema,
|
|
253
|
+
validateOutcomeParameters,
|
|
254
|
+
validateOutcomeValue,
|
|
255
|
+
} from "./outcome-schema.js";
|
|
201
256
|
export { buildProcessFlowView } from "./process-flow-view.js";
|
|
202
257
|
export {
|
|
203
258
|
getProcessGraph,
|
|
@@ -210,7 +265,6 @@ export {
|
|
|
210
265
|
type ProcessGraphView,
|
|
211
266
|
serializeProcessGraph,
|
|
212
267
|
toProcessGraphView,
|
|
213
|
-
validateProcessGraphEntryTurns,
|
|
214
268
|
validateProcessGraphProducts,
|
|
215
269
|
validateProcessGraphTurnTransitions,
|
|
216
270
|
} from "./process-graph.js";
|
|
@@ -263,6 +317,7 @@ export {
|
|
|
263
317
|
parseRepositoryPullRequestConfig,
|
|
264
318
|
} from "./repository-source-config.js";
|
|
265
319
|
export type { RepositoryIssue, RepositoryPullRequest } from "./repository-types.js";
|
|
320
|
+
export * from "./scoped-settings.js";
|
|
266
321
|
export type {
|
|
267
322
|
ServerExtensionEventMap,
|
|
268
323
|
ServerExtensionEventName,
|
|
@@ -279,6 +334,15 @@ export {
|
|
|
279
334
|
structuralStateCodec,
|
|
280
335
|
} from "./state-helpers.js";
|
|
281
336
|
export { sanitizeWorkerSubprocessEnv } from "./subprocess-env.js";
|
|
337
|
+
export {
|
|
338
|
+
listTicketDestinations,
|
|
339
|
+
markdownTicketCreationDefinition,
|
|
340
|
+
parseTicketCreationConfig,
|
|
341
|
+
parseTicketDestinationId,
|
|
342
|
+
type TicketCreationConfig,
|
|
343
|
+
ticketDestinationId,
|
|
344
|
+
ticketLabelNames,
|
|
345
|
+
} from "./ticket-creation.js";
|
|
282
346
|
export { numberArg, objectArg, projectParameters, stringArg } from "./tool-arguments.js";
|
|
283
347
|
export type { ToolCallRendererDefinition } from "./tool-renderers.js";
|
|
284
348
|
export {
|
|
@@ -297,8 +361,19 @@ export {
|
|
|
297
361
|
resolveLlmTurnRestorePrimaryLeafAfterTurn,
|
|
298
362
|
validateTurnDefinition,
|
|
299
363
|
} from "./turn-semantics.js";
|
|
364
|
+
export {
|
|
365
|
+
type InitialProcessBinding,
|
|
366
|
+
RetryableWaitError,
|
|
367
|
+
type TurnWaitBinding,
|
|
368
|
+
type TurnWaitCompletion,
|
|
369
|
+
type TurnWaitContext,
|
|
370
|
+
type TurnWaitDecision,
|
|
371
|
+
type TurnWaitNotice,
|
|
372
|
+
type TurnWaitPredicate,
|
|
373
|
+
} from "./turn-wait.js";
|
|
300
374
|
export type {
|
|
301
375
|
EventBus,
|
|
376
|
+
OutcomeToolArrayItemSpec,
|
|
302
377
|
OutcomeToolParameterSpec,
|
|
303
378
|
OutcomeToolSpec,
|
|
304
379
|
PiCustomMessageInput,
|
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
MappedItem,
|
|
3
|
+
ProcessInstance,
|
|
4
|
+
ProcessProject,
|
|
5
|
+
ProcessTurnTerminalLifecycleStatus,
|
|
6
|
+
TurnId,
|
|
7
|
+
} from "@leitwerk-dev/domain";
|
|
8
|
+
import type { LlmTurnDefinition, ProcessToolOutcomeSpec } from "./define-process.js";
|
|
9
|
+
import type { Codec, ProcessTurnOutcomeEvent } from "./extension-api.js";
|
|
10
|
+
import { SafeOutcomePlanningError } from "./extension-api.js";
|
|
11
|
+
|
|
12
|
+
/** @public */
|
|
13
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
14
|
+
|
|
15
|
+
/** Maximum length of a mapped item key. @internal */
|
|
16
|
+
export const MAPPED_ITEM_KEY_MAX_LENGTH = 200;
|
|
17
|
+
/** Maximum length of a mapped item label. @internal */
|
|
18
|
+
export const MAPPED_ITEM_LABEL_MAX_LENGTH = 200;
|
|
19
|
+
/** Maximum number of items frozen by one mapped run. @internal */
|
|
20
|
+
export const MAPPED_ITEM_MAX_COUNT = 500;
|
|
21
|
+
/** Graph trigger used by a mapped turn's collection route. @internal */
|
|
22
|
+
export const MAPPED_COLLECT_TRIGGER = "collect" as const;
|
|
23
|
+
|
|
24
|
+
/** @internal */
|
|
25
|
+
export function mappedCollectTrigger(branchId?: string): string {
|
|
26
|
+
return branchId === undefined ? MAPPED_COLLECT_TRIGGER : `${MAPPED_COLLECT_TRIGGER}:${branchId}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Durable snapshot of process data available to mapped-turn server callbacks. @public */
|
|
30
|
+
export interface MappedTurnServerContext<TParams = unknown, TState = unknown> {
|
|
31
|
+
/** @public */
|
|
32
|
+
readonly process: ProcessInstance;
|
|
33
|
+
/** @internal */
|
|
34
|
+
readonly projects: readonly ProcessProject[];
|
|
35
|
+
/** @public */
|
|
36
|
+
readonly params: TParams;
|
|
37
|
+
/** @public */
|
|
38
|
+
readonly state: TState;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Active item of a mapped LLM turn, as delivered to one worker turn. @public */
|
|
42
|
+
export interface MappedTurnItemContext<TItem = unknown> {
|
|
43
|
+
/** Frozen, codec-validated item value. @public */
|
|
44
|
+
readonly item: TItem;
|
|
45
|
+
/** Stable key unique within the run. @public */
|
|
46
|
+
readonly itemKey: string;
|
|
47
|
+
/** Short plain-text label shown to operators. @public */
|
|
48
|
+
readonly itemLabel: string;
|
|
49
|
+
/** Zero-based position in the frozen order. @public */
|
|
50
|
+
readonly itemIndex: number;
|
|
51
|
+
/** Number of frozen items in the run. @public */
|
|
52
|
+
readonly itemCount: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Serialized active item carried by worker payloads and turn records. @internal */
|
|
56
|
+
export interface MappedTurnIteration extends MappedTurnItemContext<unknown> {
|
|
57
|
+
/** @internal */
|
|
58
|
+
readonly runId: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @public */
|
|
62
|
+
export interface MappedItemInput<TItem = unknown> {
|
|
63
|
+
/** @public */
|
|
64
|
+
readonly item: TItem;
|
|
65
|
+
/** @public */
|
|
66
|
+
readonly index: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @public */
|
|
70
|
+
export interface MappedSnapshotInput<TParams = unknown, TState = unknown, TItem = unknown> {
|
|
71
|
+
/** @public */
|
|
72
|
+
readonly params: TParams;
|
|
73
|
+
/** @public */
|
|
74
|
+
readonly state: TState;
|
|
75
|
+
/** @public */
|
|
76
|
+
readonly items: readonly TItem[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Context passed to a mapped outcome's `yield` callback. @public */
|
|
80
|
+
export interface MappedOutcomeContext<TParams = unknown, TState = unknown, TItem = unknown>
|
|
81
|
+
extends MappedTurnServerContext<TParams, TState>,
|
|
82
|
+
MappedTurnItemContext<TItem> {}
|
|
83
|
+
|
|
84
|
+
/** @public */
|
|
85
|
+
export interface MappedYieldInput<TParams = unknown, TState = unknown, TItem = unknown> {
|
|
86
|
+
/** @public */
|
|
87
|
+
readonly ctx: MappedOutcomeContext<TParams, TState, TItem>;
|
|
88
|
+
/** @public */
|
|
89
|
+
readonly event: ProcessTurnOutcomeEvent;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** @public */
|
|
93
|
+
export type MappedItemYield<
|
|
94
|
+
TParams = unknown,
|
|
95
|
+
TState = unknown,
|
|
96
|
+
TItem = unknown,
|
|
97
|
+
TResult = unknown,
|
|
98
|
+
> = (input: MappedYieldInput<TParams, TState, TItem>) => MaybePromise<TResult>;
|
|
99
|
+
|
|
100
|
+
/** @public */
|
|
101
|
+
export type MappedCollect<TParams = unknown, TState = unknown, TResult = unknown> = (
|
|
102
|
+
ctx: MappedTurnServerContext<TParams, TState>,
|
|
103
|
+
orderedResults: readonly TResult[],
|
|
104
|
+
) => MaybePromise<TState>;
|
|
105
|
+
|
|
106
|
+
/** Route applied once, after collection. @internal */
|
|
107
|
+
export type MappedCollectRouting<TParams = unknown, TState = unknown> =
|
|
108
|
+
| {
|
|
109
|
+
/** @internal */
|
|
110
|
+
kind: "static";
|
|
111
|
+
/** @internal */
|
|
112
|
+
to?: TurnId;
|
|
113
|
+
/** @internal */
|
|
114
|
+
lifecycleStatus?: ProcessTurnTerminalLifecycleStatus;
|
|
115
|
+
}
|
|
116
|
+
| {
|
|
117
|
+
/** @internal */
|
|
118
|
+
kind: "branches";
|
|
119
|
+
/** @internal */
|
|
120
|
+
branches: Readonly<Record<string, TurnId>>;
|
|
121
|
+
/** @internal */
|
|
122
|
+
choose(ctx: MappedTurnServerContext<TParams, TState>): MaybePromise<string>;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Durable sequential iteration of one LLM turn over frozen items. Items run one
|
|
127
|
+
* at a time; each item yields a typed result, and business state and routing
|
|
128
|
+
* change once, when all results are collected.
|
|
129
|
+
* @public
|
|
130
|
+
*/
|
|
131
|
+
export interface MappedLlmTurnSpec<
|
|
132
|
+
TParams = unknown,
|
|
133
|
+
TState = unknown,
|
|
134
|
+
TItem = unknown,
|
|
135
|
+
TResult = unknown,
|
|
136
|
+
> {
|
|
137
|
+
/** Evaluated once when the turn is entered. @public */
|
|
138
|
+
items(ctx: MappedTurnServerContext<TParams, TState>): readonly unknown[] | null | undefined;
|
|
139
|
+
/** @public */
|
|
140
|
+
itemCodec: Codec<TItem>;
|
|
141
|
+
/** @public */
|
|
142
|
+
resultCodec: Codec<TResult>;
|
|
143
|
+
/** @public */
|
|
144
|
+
key(input: MappedItemInput<TItem>): string;
|
|
145
|
+
/** Defaults to the item key. @public */
|
|
146
|
+
label?(input: MappedItemInput<TItem>): string;
|
|
147
|
+
/** Applied in the freezing transaction, before any item starts. @public */
|
|
148
|
+
stateAfterSnapshot?(input: MappedSnapshotInput<TParams, TState, TItem>): TState;
|
|
149
|
+
/** @internal */
|
|
150
|
+
yields: Readonly<Record<string, MappedItemYield<TParams, TState, TItem, TResult>>>;
|
|
151
|
+
/** @internal */
|
|
152
|
+
collect: MappedCollect<TParams, TState, TResult>;
|
|
153
|
+
/** @internal */
|
|
154
|
+
routing: MappedCollectRouting<TParams, TState>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** @internal */
|
|
158
|
+
export type FrozenMappedItem = Pick<MappedItem, "itemIndex" | "itemKey" | "label" | "itemJson">;
|
|
159
|
+
|
|
160
|
+
/** @internal */
|
|
161
|
+
export interface MappedFreezeResult<TState = unknown> {
|
|
162
|
+
/** @internal */
|
|
163
|
+
items: FrozenMappedItem[];
|
|
164
|
+
/** State to persist in the freezing transaction, when `stateAfterSnapshot` is declared. @internal */
|
|
165
|
+
state?: TState;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** @internal */
|
|
169
|
+
export interface MappedCollectResult<TState = unknown> {
|
|
170
|
+
/** @internal */
|
|
171
|
+
state: TState;
|
|
172
|
+
/** @internal */
|
|
173
|
+
route: {
|
|
174
|
+
/** @internal */
|
|
175
|
+
trigger: string;
|
|
176
|
+
/** @internal */
|
|
177
|
+
nextTurnId?: TurnId;
|
|
178
|
+
/** @internal */
|
|
179
|
+
lifecycleStatus?: ProcessTurnTerminalLifecycleStatus;
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** @internal */
|
|
184
|
+
export function validateMappedTurn<TOutcome extends string, TParams, TState>(
|
|
185
|
+
turnId: string,
|
|
186
|
+
turnDef: LlmTurnDefinition<TOutcome, TParams, TState>,
|
|
187
|
+
): string[] {
|
|
188
|
+
const mapped = turnDef.forEach;
|
|
189
|
+
if (mapped === undefined) return [];
|
|
190
|
+
const errors: string[] = [];
|
|
191
|
+
for (const name of ["items", "key", "collect"] as const) {
|
|
192
|
+
if (typeof mapped[name] !== "function") {
|
|
193
|
+
errors.push(`Mapped turn '${turnId}' must declare ${name}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
for (const name of ["itemCodec", "resultCodec"] as const) {
|
|
197
|
+
const codec = mapped[name];
|
|
198
|
+
if (typeof codec?.parse !== "function" || typeof codec?.serialize !== "function") {
|
|
199
|
+
errors.push(`Mapped turn '${turnId}' must declare ${name}`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (turnDef.turnEnd) {
|
|
203
|
+
errors.push(`Mapped turn '${turnId}' must finish items through outcome tools`);
|
|
204
|
+
}
|
|
205
|
+
if (turnDef.publishedProduct) {
|
|
206
|
+
errors.push(`Mapped turn '${turnId}' cannot publish a product from item turns`);
|
|
207
|
+
}
|
|
208
|
+
const outcomes = Object.entries(turnDef.outcomes ?? {}) as Array<
|
|
209
|
+
[string, ProcessToolOutcomeSpec<TParams, TState>]
|
|
210
|
+
>;
|
|
211
|
+
if (outcomes.length === 0) {
|
|
212
|
+
errors.push(`Mapped turn '${turnId}' must declare at least one outcome tool`);
|
|
213
|
+
}
|
|
214
|
+
for (const [outcomeId, spec] of outcomes) {
|
|
215
|
+
if (
|
|
216
|
+
"branches" in spec ||
|
|
217
|
+
spec.to !== undefined ||
|
|
218
|
+
spec.complete !== undefined ||
|
|
219
|
+
spec.lifecycleStatus !== undefined ||
|
|
220
|
+
spec.effect !== undefined ||
|
|
221
|
+
spec.publishedProduct !== undefined
|
|
222
|
+
) {
|
|
223
|
+
errors.push(
|
|
224
|
+
`Mapped turn '${turnId}' outcome '${outcomeId}' cannot route, change state, or publish`,
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
if (typeof mapped.yields?.[outcomeId] !== "function") {
|
|
228
|
+
errors.push(`Mapped turn '${turnId}' outcome '${outcomeId}' must declare a yield`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
for (const outcomeId of Object.keys(mapped.yields ?? {})) {
|
|
232
|
+
if (!Object.hasOwn(turnDef.outcomes ?? {}, outcomeId)) {
|
|
233
|
+
errors.push(`Mapped turn '${turnId}' yields undeclared outcome '${outcomeId}'`);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const routing = mapped.routing;
|
|
237
|
+
if (routing?.kind === "static") {
|
|
238
|
+
if ((routing.to === undefined) === (routing.lifecycleStatus === undefined)) {
|
|
239
|
+
errors.push(`Mapped turn '${turnId}' collection must declare exactly one target`);
|
|
240
|
+
}
|
|
241
|
+
} else if (routing?.kind === "branches") {
|
|
242
|
+
if (Object.keys(routing.branches).length === 0 || typeof routing.choose !== "function") {
|
|
243
|
+
errors.push(`Mapped turn '${turnId}' collection routing needs branches and a chooser`);
|
|
244
|
+
}
|
|
245
|
+
} else {
|
|
246
|
+
errors.push(`Mapped turn '${turnId}' must declare a collection route`);
|
|
247
|
+
}
|
|
248
|
+
return errors;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function toJsonValue(value: unknown, context: string): string {
|
|
252
|
+
let json: string | undefined;
|
|
253
|
+
try {
|
|
254
|
+
json = JSON.stringify(value);
|
|
255
|
+
} catch {
|
|
256
|
+
json = undefined;
|
|
257
|
+
}
|
|
258
|
+
if (json === undefined) {
|
|
259
|
+
throw new SafeOutcomePlanningError("invalid_mapped_value", `${context} is not JSON`);
|
|
260
|
+
}
|
|
261
|
+
return json;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function plainText(value: string): string {
|
|
265
|
+
return value.replace(/\s+/g, " ").trim();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function errorMessage(error: unknown): string {
|
|
269
|
+
return error instanceof Error ? error.message : String(error);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Evaluates, validates, and serializes the items of a mapped turn. @internal */
|
|
273
|
+
export function freezeMappedItems<TParams, TState>(
|
|
274
|
+
turnId: TurnId,
|
|
275
|
+
spec: MappedLlmTurnSpec<TParams, TState>,
|
|
276
|
+
ctx: MappedTurnServerContext<TParams, TState>,
|
|
277
|
+
): MappedFreezeResult<TState> {
|
|
278
|
+
const raw = spec.items(ctx) ?? [];
|
|
279
|
+
if (!Array.isArray(raw)) {
|
|
280
|
+
throw new SafeOutcomePlanningError(
|
|
281
|
+
"invalid_mapped_items",
|
|
282
|
+
`Mapped turn '${turnId}' items must be an array`,
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
if (raw.length > MAPPED_ITEM_MAX_COUNT) {
|
|
286
|
+
throw new SafeOutcomePlanningError(
|
|
287
|
+
"invalid_mapped_items",
|
|
288
|
+
`Mapped turn '${turnId}' may freeze at most ${MAPPED_ITEM_MAX_COUNT} items`,
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
const parsed: unknown[] = [];
|
|
292
|
+
const items: FrozenMappedItem[] = [];
|
|
293
|
+
const keys = new Set<string>();
|
|
294
|
+
for (const [index, value] of raw.entries()) {
|
|
295
|
+
let item: unknown;
|
|
296
|
+
try {
|
|
297
|
+
item = spec.itemCodec.parse(value);
|
|
298
|
+
} catch (error) {
|
|
299
|
+
throw new SafeOutcomePlanningError(
|
|
300
|
+
"invalid_mapped_item",
|
|
301
|
+
`Mapped turn '${turnId}' item ${index} is invalid: ${errorMessage(error)}`,
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
const key = spec.key({ item, index });
|
|
305
|
+
if (typeof key !== "string" || key.trim() === "" || key.length > MAPPED_ITEM_KEY_MAX_LENGTH) {
|
|
306
|
+
throw new SafeOutcomePlanningError(
|
|
307
|
+
"invalid_mapped_item_key",
|
|
308
|
+
`Mapped turn '${turnId}' item ${index} needs a non-empty key of at most ${MAPPED_ITEM_KEY_MAX_LENGTH} characters`,
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
if (keys.has(key)) {
|
|
312
|
+
throw new SafeOutcomePlanningError(
|
|
313
|
+
"duplicate_mapped_item_key",
|
|
314
|
+
`Mapped turn '${turnId}' repeats item key '${key}'`,
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
keys.add(key);
|
|
318
|
+
const rawLabel = spec.label ? spec.label({ item, index }) : key;
|
|
319
|
+
const label = plainText(typeof rawLabel === "string" ? rawLabel : "") || key;
|
|
320
|
+
parsed.push(item);
|
|
321
|
+
items.push({
|
|
322
|
+
itemIndex: index,
|
|
323
|
+
itemKey: key,
|
|
324
|
+
label: label.slice(0, MAPPED_ITEM_LABEL_MAX_LENGTH),
|
|
325
|
+
itemJson: toJsonValue(
|
|
326
|
+
spec.itemCodec.serialize(item),
|
|
327
|
+
`Mapped turn '${turnId}' item '${key}'`,
|
|
328
|
+
),
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
items,
|
|
333
|
+
...(spec.stateAfterSnapshot
|
|
334
|
+
? {
|
|
335
|
+
state: spec.stateAfterSnapshot({
|
|
336
|
+
params: ctx.params,
|
|
337
|
+
state: ctx.state,
|
|
338
|
+
items: parsed,
|
|
339
|
+
}),
|
|
340
|
+
}
|
|
341
|
+
: {}),
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Maps one item outcome to its serialized, codec-validated result. @internal */
|
|
346
|
+
export async function yieldMappedItemResult<TParams, TState>(input: {
|
|
347
|
+
/** @internal */
|
|
348
|
+
turnId: TurnId;
|
|
349
|
+
/** @internal */
|
|
350
|
+
spec: MappedLlmTurnSpec<TParams, TState>;
|
|
351
|
+
/** @internal */
|
|
352
|
+
ctx: MappedTurnServerContext<TParams, TState>;
|
|
353
|
+
/** @internal */
|
|
354
|
+
iteration: MappedTurnIteration;
|
|
355
|
+
/** @internal */
|
|
356
|
+
event: ProcessTurnOutcomeEvent;
|
|
357
|
+
}): Promise<string> {
|
|
358
|
+
const yieldResult = input.spec.yields[input.event.outcome];
|
|
359
|
+
if (!yieldResult) {
|
|
360
|
+
throw new SafeOutcomePlanningError(
|
|
361
|
+
"unknown_mapped_outcome",
|
|
362
|
+
`Mapped turn '${input.turnId}' has no result for outcome '${input.event.outcome}'`,
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
const item = input.spec.itemCodec.parse(input.iteration.item);
|
|
366
|
+
const value = await yieldResult({
|
|
367
|
+
ctx: {
|
|
368
|
+
...input.ctx,
|
|
369
|
+
item,
|
|
370
|
+
itemKey: input.iteration.itemKey,
|
|
371
|
+
itemLabel: input.iteration.itemLabel,
|
|
372
|
+
itemIndex: input.iteration.itemIndex,
|
|
373
|
+
itemCount: input.iteration.itemCount,
|
|
374
|
+
},
|
|
375
|
+
event: input.event,
|
|
376
|
+
});
|
|
377
|
+
let result: unknown;
|
|
378
|
+
try {
|
|
379
|
+
result = input.spec.resultCodec.parse(value);
|
|
380
|
+
} catch (error) {
|
|
381
|
+
throw new SafeOutcomePlanningError(
|
|
382
|
+
"invalid_mapped_result",
|
|
383
|
+
`Mapped turn '${input.turnId}' produced an invalid result for item '${input.iteration.itemKey}': ${errorMessage(error)}`,
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
return toJsonValue(
|
|
387
|
+
input.spec.resultCodec.serialize(result),
|
|
388
|
+
`Mapped turn '${input.turnId}' result for item '${input.iteration.itemKey}'`,
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** Collects ordered results once and resolves the turn's single business route. @internal */
|
|
393
|
+
export async function collectMappedResults<TParams, TState>(input: {
|
|
394
|
+
/** @internal */
|
|
395
|
+
turnId: TurnId;
|
|
396
|
+
/** @internal */
|
|
397
|
+
spec: MappedLlmTurnSpec<TParams, TState>;
|
|
398
|
+
/** @internal */
|
|
399
|
+
ctx: MappedTurnServerContext<TParams, TState>;
|
|
400
|
+
/** Serialized results in frozen item order. @internal */
|
|
401
|
+
resultJsons: readonly string[];
|
|
402
|
+
}): Promise<MappedCollectResult<TState>> {
|
|
403
|
+
const results = input.resultJsons.map((json, index) => {
|
|
404
|
+
try {
|
|
405
|
+
return input.spec.resultCodec.parse(JSON.parse(json));
|
|
406
|
+
} catch (error) {
|
|
407
|
+
throw new SafeOutcomePlanningError(
|
|
408
|
+
"invalid_mapped_result",
|
|
409
|
+
`Mapped turn '${input.turnId}' stored an invalid result at item ${index}: ${errorMessage(error)}`,
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
const state = await input.spec.collect(input.ctx, results);
|
|
414
|
+
const routing = input.spec.routing;
|
|
415
|
+
if (routing.kind === "static") {
|
|
416
|
+
return {
|
|
417
|
+
state,
|
|
418
|
+
route: {
|
|
419
|
+
trigger: mappedCollectTrigger(),
|
|
420
|
+
...(routing.to !== undefined ? { nextTurnId: routing.to } : {}),
|
|
421
|
+
...(routing.lifecycleStatus !== undefined
|
|
422
|
+
? { lifecycleStatus: routing.lifecycleStatus }
|
|
423
|
+
: {}),
|
|
424
|
+
},
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
const branchId = await routing.choose({ ...input.ctx, state });
|
|
428
|
+
if (!Object.hasOwn(routing.branches, branchId)) {
|
|
429
|
+
throw new Error(
|
|
430
|
+
`Mapped turn '${input.turnId}' collection selected unknown branch '${branchId}'`,
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
state,
|
|
435
|
+
route: { trigger: mappedCollectTrigger(branchId), nextTurnId: routing.branches[branchId] },
|
|
436
|
+
};
|
|
437
|
+
}
|