@leitwerk-dev/process-sdk 0.3.0 → 0.3.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/src/flow.ts CHANGED
@@ -47,6 +47,14 @@ import type {
47
47
  WorkerCompleteInput,
48
48
  } from "./extension-api.js";
49
49
  import { isPathInside } from "./fs-utils.js";
50
+ import type {
51
+ MappedCollect,
52
+ MappedCollectRouting,
53
+ MappedItemYield,
54
+ MappedLlmTurnSpec,
55
+ MappedTurnItemContext,
56
+ MappedTurnServerContext,
57
+ } from "./mapped-turn.js";
50
58
  import {
51
59
  type ProcessGraphSource,
52
60
  toProcessGraphView,
@@ -664,9 +672,9 @@ interface ParameterOptions extends Partial<Omit<OutcomeToolParameterSpec, "type"
664
672
  requiredErrorCode?: string;
665
673
  }
666
674
 
667
- /** @internal */
675
+ /** @public */
668
676
  interface ArrayParameterOptions extends ParameterOptions {
669
- /** @internal */
677
+ /** @public */
670
678
  items?: OutcomeToolParameterSpec["items"];
671
679
  }
672
680
 
@@ -711,15 +719,19 @@ class ParameterizedOutcomeBuilder<TParams, TState, TContext> extends RouteAndEff
711
719
  }
712
720
 
713
721
  /** @internal */
714
- protected publishedMarkdownFields() {
715
- return this.publishedMarkdownParameter
716
- ? {
717
- /** @internal */
718
- publishedProduct: this.publishedMarkdownParameter,
719
- /** @internal */
720
- turnResultMarkdownParameter: this.publishedMarkdownParameter,
721
- }
722
- : {};
722
+ protected buildParameterSpec(kind: string): ProcessToolOutcomeSpec<TParams, TState> {
723
+ if (!this.outcomeDescription) throw new Error(`${kind} must declare .description(...)`);
724
+ return {
725
+ description: this.outcomeDescription,
726
+ parameters: this.parameters,
727
+ ...(this.summaryParameter ? { resultSummaryParameter: this.summaryParameter } : {}),
728
+ ...(this.publishedMarkdownParameter
729
+ ? {
730
+ publishedProduct: this.publishedMarkdownParameter,
731
+ turnResultMarkdownParameter: this.publishedMarkdownParameter,
732
+ }
733
+ : {}),
734
+ };
723
735
  }
724
736
 
725
737
  /** @public */
@@ -847,6 +859,11 @@ class ParameterizedOutcomeBuilder<TParams, TState, TContext> extends RouteAndEff
847
859
  array(name: string, options: string | ArrayParameterOptions = {}): this {
848
860
  return this.typedParameter(name, "array", options);
849
861
  }
862
+
863
+ /** @public */
864
+ requiredArray(name: string, options: string | ArrayParameterOptions = {}): this {
865
+ return this.typedParameter(name, "array", this.withRequired(name, options));
866
+ }
850
867
  }
851
868
 
852
869
  /** @public */
@@ -890,18 +907,13 @@ export class OutcomeToolBuilder<
890
907
 
891
908
  /** @internal */
892
909
  build(): ProcessToolOutcomeSpec<TParams, TState> {
893
- if (!this.outcomeDescription) {
894
- throw new Error("LLM outcome tool must declare .description(...)");
895
- }
910
+ const parameters = this.buildParameterSpec("LLM outcome tool");
896
911
  if (this.stateRouting && this.hasRoute()) {
897
912
  throw new Error("State-routed outcome cannot declare another route");
898
913
  }
899
914
  const stateRouting = this.stateRouting;
900
915
  return {
901
- description: this.outcomeDescription,
902
- parameters: this.parameters,
903
- ...(this.summaryParameter ? { resultSummaryParameter: this.summaryParameter } : {}),
904
- ...this.publishedMarkdownFields(),
916
+ ...parameters,
905
917
  ...(stateRouting
906
918
  ? {
907
919
  branches: Object.fromEntries(
@@ -938,9 +950,7 @@ export class AutomaticOutcomeBuilder<
938
950
 
939
951
  /** @internal */
940
952
  build(): ProcessToolOutcomeSpec<TParams, TState> {
941
- if (!this.outcomeDescription) {
942
- throw new Error("Automatic outcome must declare .description(...)");
943
- }
953
+ const parameters = this.buildParameterSpec("Automatic outcome");
944
954
  const configuredEffect = this.flowEffect ? wrapOutcomeCallback(this.flowEffect) : undefined;
945
955
  const effect = this.waits
946
956
  ? async (
@@ -954,10 +964,7 @@ export class AutomaticOutcomeBuilder<
954
964
  }
955
965
  : configuredEffect;
956
966
  return {
957
- description: this.outcomeDescription,
958
- parameters: this.parameters,
959
- ...(this.summaryParameter ? { resultSummaryParameter: this.summaryParameter } : {}),
960
- ...this.publishedMarkdownFields(),
967
+ ...parameters,
961
968
  ...this.buildRouteTarget(),
962
969
  ...(effect ? { effect } : {}),
963
970
  };
@@ -1180,12 +1187,111 @@ export class LlmFlowBuilder<
1180
1187
  } | null = null;
1181
1188
  private endResult: LlmTurnEndBuilder<TParams, TState> | null = null;
1182
1189
  private outcomeToolBuilders = new Map<string, OutcomeToolBuilder<TParams, TState>>();
1190
+ private mapped: {
1191
+ options: FlowForEachOptions<TParams, TState, unknown, unknown>;
1192
+ yields: Map<string, MappedItemYield<TParams, TState, unknown, unknown>>;
1193
+ collect?: MappedCollect<TParams, TState, unknown>;
1194
+ routing?: MappedCollectRouting<TParams, TState>;
1195
+ } | null = null;
1183
1196
 
1184
1197
  /** @internal */
1185
1198
  get definition(): LlmTurnDefinition<string, TParams, TState> {
1186
1199
  return this.buildDefinition();
1187
1200
  }
1188
1201
 
1202
+ /**
1203
+ * Run this turn once per frozen item. Items run sequentially; each item's
1204
+ * outcome yields a typed result, and `collect` updates state and routes once.
1205
+ * @public
1206
+ */
1207
+ forEach<TItem, TResult>(
1208
+ options: FlowForEachOptions<TParams, TState, TItem, TResult>,
1209
+ ): MappedLlmFlowBuilder<TParams, TState, TItem, TResult, TConsumedProducts, TPrepared> {
1210
+ if (this.mapped) {
1211
+ throw new Error(`LLM turn '${this.turnId}' declares .forEach(...) more than once`);
1212
+ }
1213
+ if (this.outcomeToolBuilders.size > 0 || this.publishedResult || this.endResult) {
1214
+ throw new Error(
1215
+ `LLM turn '${this.turnId}' must declare .forEach(...) before its completion path`,
1216
+ );
1217
+ }
1218
+ this.mapped = {
1219
+ options: options as FlowForEachOptions<TParams, TState, unknown, unknown>,
1220
+ yields: new Map(),
1221
+ };
1222
+ return new MappedLlmFlowBuilder<TParams, TState, TItem, TResult, TConsumedProducts, TPrepared>(
1223
+ this,
1224
+ );
1225
+ }
1226
+
1227
+ /** @internal */
1228
+ addMappedOutcome(
1229
+ id: string,
1230
+ builder: OutcomeToolBuilder<TParams, TState>,
1231
+ yieldResult: MappedItemYield<TParams, TState, unknown, unknown>,
1232
+ ): void {
1233
+ if (!this.mapped) throw new Error(`LLM turn '${this.turnId}' is not mapped`);
1234
+ if (id.trim() === "") {
1235
+ throw new Error(`LLM turn '${this.turnId}' declares an empty outcome tool id`);
1236
+ }
1237
+ if (this.outcomeToolBuilders.has(id)) {
1238
+ throw new Error(`LLM turn '${this.turnId}' declares duplicate outcome tool '${id}'`);
1239
+ }
1240
+ this.outcomeToolBuilders.set(id, builder);
1241
+ this.mapped.yields.set(id, yieldResult);
1242
+ }
1243
+
1244
+ /** @internal */
1245
+ setMappedCollect(collect: MappedCollect<TParams, TState, unknown>): void {
1246
+ if (!this.mapped) throw new Error(`LLM turn '${this.turnId}' is not mapped`);
1247
+ if (this.mapped.collect) {
1248
+ throw new Error(`Mapped turn '${this.turnId}' declares .collect(...) more than once`);
1249
+ }
1250
+ this.mapped.collect = collect;
1251
+ }
1252
+
1253
+ /** @internal */
1254
+ setMappedRouting(routing: MappedCollectRouting<TParams, TState>): void {
1255
+ if (!this.mapped) throw new Error(`LLM turn '${this.turnId}' is not mapped`);
1256
+ if (this.mapped.routing) {
1257
+ throw new Error(`Mapped turn '${this.turnId}' collection already declares a route`);
1258
+ }
1259
+ this.mapped.routing = routing;
1260
+ }
1261
+
1262
+ private mappedItemContext(
1263
+ ctx: ProcessRuntimeTurnContext<TParams, TState>,
1264
+ ): Partial<MappedTurnItemContext<unknown>> {
1265
+ if (!this.mapped) return {};
1266
+ const iteration = ctx.iteration;
1267
+ if (!iteration) {
1268
+ throw new Error(`Mapped turn '${this.turnId}' requires an active item`);
1269
+ }
1270
+ return {
1271
+ item: this.mapped.options.itemCodec.parse(iteration.item),
1272
+ itemKey: iteration.itemKey,
1273
+ itemLabel: iteration.itemLabel,
1274
+ itemIndex: iteration.itemIndex,
1275
+ itemCount: iteration.itemCount,
1276
+ };
1277
+ }
1278
+
1279
+ private buildMappedSpec(): MappedLlmTurnSpec<TParams, TState> | undefined {
1280
+ if (!this.mapped) return undefined;
1281
+ if (!this.mapped.collect) {
1282
+ throw new Error(`Mapped turn '${this.turnId}' must declare .collect(...)`);
1283
+ }
1284
+ if (!this.mapped.routing) {
1285
+ throw new Error(`Mapped turn '${this.turnId}' must declare a collection route`);
1286
+ }
1287
+ return {
1288
+ ...this.mapped.options,
1289
+ yields: Object.fromEntries(this.mapped.yields),
1290
+ collect: this.mapped.collect,
1291
+ routing: this.mapped.routing,
1292
+ };
1293
+ }
1294
+
1189
1295
  /** @internal */
1190
1296
  modelPurpose(purpose: LlmModelPurpose): this {
1191
1297
  this.modelPurposeValue = purpose;
@@ -1342,6 +1448,7 @@ export class LlmFlowBuilder<
1342
1448
 
1343
1449
  /** @public */
1344
1450
  publish(productName: string): PublishedResultBuilder<TParams, TState> {
1451
+ this.assertNotMapped(".publish(...)");
1345
1452
  const normalized = normalizeProductName(productName);
1346
1453
  if (this.publishedResult) {
1347
1454
  throw new Error(`LLM turn '${this.turnId}' can publish only one product in flow v1`);
@@ -1356,6 +1463,7 @@ export class LlmFlowBuilder<
1356
1463
 
1357
1464
  /** @public */
1358
1465
  end(outcomeId: string): LlmTurnEndBuilder<TParams, TState> {
1466
+ this.assertNotMapped(".end(...)");
1359
1467
  if (this.endResult) {
1360
1468
  throw new Error(`LLM turn '${this.turnId}' declares duplicate .end(...) completion`);
1361
1469
  }
@@ -1372,14 +1480,25 @@ export class LlmFlowBuilder<
1372
1480
  /** @public */
1373
1481
  buildPrompt(fn: LlmPromptBuilder<TParams, TState, TConsumedProducts, TPrepared>): this {
1374
1482
  this.promptBuilder = (ctx) =>
1375
- fn(
1376
- createFlowPromptContext(ctx, [...this.consumedProductNames] as TConsumedProducts[], [
1377
- ...this.optionalConsumedProductNames,
1378
- ]),
1379
- );
1483
+ fn({
1484
+ ...createFlowPromptContext<TParams, TState, TConsumedProducts, TPrepared>(
1485
+ ctx,
1486
+ [...this.consumedProductNames] as TConsumedProducts[],
1487
+ [...this.optionalConsumedProductNames],
1488
+ ),
1489
+ ...this.mappedItemContext(ctx),
1490
+ });
1380
1491
  return this;
1381
1492
  }
1382
1493
 
1494
+ private assertNotMapped(feature: string): void {
1495
+ if (this.mapped) {
1496
+ throw new Error(
1497
+ `Mapped turn '${this.turnId}' cannot declare ${feature}; items yield results and .collect(...) routes`,
1498
+ );
1499
+ }
1500
+ }
1501
+
1383
1502
  /** @public */
1384
1503
  prompt(prompt: (ctx: ProcessRuntimeTurnContext<TParams, TState>) => MaybePromise<string>): this {
1385
1504
  this.promptBuilder = prompt;
@@ -1393,6 +1512,7 @@ export class LlmFlowBuilder<
1393
1512
  tool: OutcomeToolBuilder<TParams, TState>,
1394
1513
  ) => OutcomeToolBuilder<TParams, TState> | undefined,
1395
1514
  ): this {
1515
+ this.assertNotMapped("routed outcome tools");
1396
1516
  if (id.trim() === "") {
1397
1517
  throw new Error(`LLM turn '${this.turnId}' declares an empty outcome tool id`);
1398
1518
  }
@@ -1435,7 +1555,10 @@ export class LlmFlowBuilder<
1435
1555
  ...(preparation
1436
1556
  ? {
1437
1557
  prepare: (ctx: ProcessRuntimeTurnContext<TParams, TState>) =>
1438
- preparation(createFlowAutomaticRunContext(ctx)),
1558
+ preparation({
1559
+ ...createFlowAutomaticRunContext(ctx),
1560
+ ...this.mappedItemContext(ctx),
1561
+ }),
1439
1562
  }
1440
1563
  : {}),
1441
1564
  completionMode: "turn_end" as const,
@@ -1545,9 +1668,11 @@ export class LlmFlowBuilder<
1545
1668
  if (Object.keys(outcomes).length === 0 && !turnEnd) {
1546
1669
  throw new Error(`LLM turn '${this.turnId}' must declare a completion path`);
1547
1670
  }
1671
+ const forEach = this.buildMappedSpec();
1548
1672
 
1549
1673
  return {
1550
1674
  ...definition,
1675
+ ...(forEach ? { forEach } : {}),
1551
1676
  ...(Object.keys(outcomes).length > 0 ? { outcomes } : {}),
1552
1677
  ...(turnEnd ? { turnEnd } : {}),
1553
1678
  ...(turnResultMarkdown ? { turnResultMarkdown } : {}),
@@ -1563,6 +1688,298 @@ export class LlmFlowBuilder<
1563
1688
  }
1564
1689
  }
1565
1690
 
1691
+ /** Item selection for `flow.llm(...).forEach(...)`. @public */
1692
+ export type FlowForEachOptions<TParams, TState, TItem, TResult> = Pick<
1693
+ MappedLlmTurnSpec<TParams, TState, TItem, TResult>,
1694
+ "items" | "itemCodec" | "resultCodec" | "key" | "label" | "stateAfterSnapshot"
1695
+ >;
1696
+
1697
+ /** @public */
1698
+ export type FlowMappedPromptContext<
1699
+ TParams,
1700
+ TState,
1701
+ TItem,
1702
+ TConsumedProducts extends string,
1703
+ TPrepared,
1704
+ > = FlowPromptContext<TParams, TState, TConsumedProducts, TPrepared> & MappedTurnItemContext<TItem>;
1705
+
1706
+ /** @public */
1707
+ export type FlowMappedPreparationContext<TParams, TState, TItem> = FlowLlmPreparationContext<
1708
+ TParams,
1709
+ TState
1710
+ > &
1711
+ MappedTurnItemContext<TItem>;
1712
+
1713
+ /**
1714
+ * Outcome of one mapped item. It declares parameters and yields the item's
1715
+ * result; it cannot route, change process state, or publish products.
1716
+ * @public
1717
+ */
1718
+ export class MappedOutcomeBuilder<
1719
+ TParams = unknown,
1720
+ TState = unknown,
1721
+ TItem = unknown,
1722
+ TResult = unknown,
1723
+ > {
1724
+ private readonly tool = new OutcomeToolBuilder<TParams, TState>();
1725
+ private yieldResult: MappedItemYield<TParams, TState, TItem, TResult> | null = null;
1726
+
1727
+ /** @public */
1728
+ description(text: string): this {
1729
+ this.tool.description(text);
1730
+ return this;
1731
+ }
1732
+
1733
+ /** @public */
1734
+ resultSummary(name?: string): this {
1735
+ this.tool.resultSummary(name);
1736
+ return this;
1737
+ }
1738
+
1739
+ /** @public */
1740
+ requiredString(name: string, options: string | ParameterOptions = {}): this {
1741
+ this.tool.requiredString(name, options);
1742
+ return this;
1743
+ }
1744
+
1745
+ /** @public */
1746
+ string(name: string, options: string | ParameterOptions = {}): this {
1747
+ this.tool.string(name, options);
1748
+ return this;
1749
+ }
1750
+
1751
+ /** @public */
1752
+ requiredNumber(name: string, options: string | ParameterOptions = {}): this {
1753
+ this.tool.requiredNumber(name, options);
1754
+ return this;
1755
+ }
1756
+
1757
+ /** @public */
1758
+ requiredBoolean(name: string, options: string | ParameterOptions = {}): this {
1759
+ this.tool.requiredBoolean(name, options);
1760
+ return this;
1761
+ }
1762
+
1763
+ /** @public */
1764
+ requiredStringArray(name: string, options: string | ArrayParameterOptions = {}): this {
1765
+ this.tool.requiredStringArray(name, options);
1766
+ return this;
1767
+ }
1768
+
1769
+ /** @public */
1770
+ object(name: string, options: string | ParameterOptions = {}): this {
1771
+ this.tool.object(name, options);
1772
+ return this;
1773
+ }
1774
+
1775
+ /** @public */
1776
+ requiredArray(name: string, options: string | ArrayParameterOptions = {}): this {
1777
+ this.tool.requiredArray(name, options);
1778
+ return this;
1779
+ }
1780
+
1781
+ /** Map this outcome to the item's typed result. @public */
1782
+ yield(fn: MappedItemYield<TParams, TState, TItem, TResult>): this {
1783
+ this.yieldResult = fn;
1784
+ return this;
1785
+ }
1786
+
1787
+ /** @internal */
1788
+ build(): {
1789
+ /** @internal */
1790
+ tool: OutcomeToolBuilder<TParams, TState>;
1791
+ /** @internal */
1792
+ yieldResult: MappedItemYield<TParams, TState, TItem, TResult>;
1793
+ } {
1794
+ if (!this.yieldResult) {
1795
+ throw new Error("Mapped outcome must declare .yield(...)");
1796
+ }
1797
+ return { tool: this.tool, yieldResult: this.yieldResult };
1798
+ }
1799
+ }
1800
+
1801
+ /** @public */
1802
+ abstract class MappedTurnFacade<TParams, TState> implements FlowLlmTurn<TParams, TState, string> {
1803
+ /** @internal */
1804
+ constructor(
1805
+ /** @internal */
1806
+ protected readonly base: LlmFlowBuilder<TParams, TState, string, unknown>,
1807
+ ) {}
1808
+
1809
+ /** @internal */
1810
+ get id(): TurnId {
1811
+ return this.base.id;
1812
+ }
1813
+
1814
+ /** @internal */
1815
+ get definition(): LlmTurnDefinition<string, TParams, TState> {
1816
+ return this.base.definition;
1817
+ }
1818
+ }
1819
+
1820
+ /**
1821
+ * Routes a mapped turn once, after all item results are collected.
1822
+ * @public
1823
+ */
1824
+ export class MappedCollectBuilder<TParams = unknown, TState = unknown> extends MappedTurnFacade<
1825
+ TParams,
1826
+ TState
1827
+ > {
1828
+ /** @public */
1829
+ to(turnId: TurnId): this {
1830
+ this.base.setMappedRouting({ kind: "static", to: turnId });
1831
+ return this;
1832
+ }
1833
+
1834
+ /** @public */
1835
+ complete(): this {
1836
+ this.base.setMappedRouting({ kind: "static", lifecycleStatus: "completed" });
1837
+ return this;
1838
+ }
1839
+
1840
+ /** @public */
1841
+ lifecycleStatus(status: ProcessTurnTerminalLifecycleStatus): this {
1842
+ this.base.setMappedRouting({ kind: "static", lifecycleStatus: status });
1843
+ return this;
1844
+ }
1845
+
1846
+ /** Choose the next turn from the collected state. @public */
1847
+ routeByState(
1848
+ branches: Record<string, TurnId>,
1849
+ choose: (ctx: MappedTurnServerContext<TParams, TState>) => MaybePromise<string>,
1850
+ ): this {
1851
+ if (Object.keys(branches).length === 0) {
1852
+ throw new Error("Mapped collection routing must declare at least one branch");
1853
+ }
1854
+ this.base.setMappedRouting({ kind: "branches", branches: { ...branches }, choose });
1855
+ return this;
1856
+ }
1857
+ }
1858
+
1859
+ /**
1860
+ * LLM turn that runs once per frozen item. Business state and routing belong
1861
+ * to `.collect(...)`.
1862
+ * @public
1863
+ */
1864
+ export class MappedLlmFlowBuilder<
1865
+ TParams = unknown,
1866
+ TState = unknown,
1867
+ TItem = unknown,
1868
+ TResult = unknown,
1869
+ TConsumedProducts extends string = never,
1870
+ TPrepared = undefined,
1871
+ > extends MappedTurnFacade<TParams, TState> {
1872
+ /** @internal */
1873
+ constructor(base: LlmFlowBuilder<TParams, TState, TConsumedProducts, TPrepared>) {
1874
+ super(base as unknown as LlmFlowBuilder<TParams, TState, string, unknown>);
1875
+ }
1876
+
1877
+ /** @public */
1878
+ description(description: string): this {
1879
+ this.base.description(description);
1880
+ return this;
1881
+ }
1882
+
1883
+ /** @public */
1884
+ tools(...tools: readonly PiBuiltInToolName[]): this {
1885
+ this.base.tools(...tools);
1886
+ return this;
1887
+ }
1888
+
1889
+ /** @public */
1890
+ integrationTools(...tools: readonly string[]): this {
1891
+ this.base.integrationTools(...tools);
1892
+ return this;
1893
+ }
1894
+
1895
+ /** @public */
1896
+ freshPrimary(): this {
1897
+ this.base.freshPrimary();
1898
+ return this;
1899
+ }
1900
+
1901
+ /** @public */
1902
+ consume<TProductName extends string>(
1903
+ productName: TProductName,
1904
+ ): MappedLlmFlowBuilder<
1905
+ TParams,
1906
+ TState,
1907
+ TItem,
1908
+ TResult,
1909
+ TConsumedProducts | TProductName,
1910
+ TPrepared
1911
+ > {
1912
+ this.base.consume(productName);
1913
+ return this as unknown as MappedLlmFlowBuilder<
1914
+ TParams,
1915
+ TState,
1916
+ TItem,
1917
+ TResult,
1918
+ TConsumedProducts | TProductName,
1919
+ TPrepared
1920
+ >;
1921
+ }
1922
+
1923
+ /** Deterministic worker preparation for the active item. @public */
1924
+ prepare<TNextPrepared>(
1925
+ fn: (ctx: FlowMappedPreparationContext<TParams, TState, TItem>) => MaybePromise<TNextPrepared>,
1926
+ ): MappedLlmFlowBuilder<TParams, TState, TItem, TResult, TConsumedProducts, TNextPrepared> {
1927
+ this.base.prepare((ctx) => fn(ctx as FlowMappedPreparationContext<TParams, TState, TItem>));
1928
+ return this as unknown as MappedLlmFlowBuilder<
1929
+ TParams,
1930
+ TState,
1931
+ TItem,
1932
+ TResult,
1933
+ TConsumedProducts,
1934
+ TNextPrepared
1935
+ >;
1936
+ }
1937
+
1938
+ /** Build the prompt for the active item. @public */
1939
+ buildPrompt(
1940
+ fn: (
1941
+ ctx: FlowMappedPromptContext<TParams, TState, TItem, TConsumedProducts, TPrepared>,
1942
+ ) => MaybePromise<string>,
1943
+ ): this {
1944
+ this.base.buildPrompt((ctx) =>
1945
+ fn(
1946
+ ctx as unknown as FlowMappedPromptContext<
1947
+ TParams,
1948
+ TState,
1949
+ TItem,
1950
+ TConsumedProducts,
1951
+ TPrepared
1952
+ >,
1953
+ ),
1954
+ );
1955
+ return this;
1956
+ }
1957
+
1958
+ /** @public */
1959
+ outcomeTool(
1960
+ id: string,
1961
+ configure: (
1962
+ outcome: MappedOutcomeBuilder<TParams, TState, TItem, TResult>,
1963
+ ) => MappedOutcomeBuilder<TParams, TState, TItem, TResult> | undefined,
1964
+ ): this {
1965
+ const outcome = new MappedOutcomeBuilder<TParams, TState, TItem, TResult>();
1966
+ configure(outcome);
1967
+ const built = outcome.build();
1968
+ this.base.addMappedOutcome(
1969
+ id,
1970
+ built.tool,
1971
+ built.yieldResult as MappedItemYield<TParams, TState, unknown, unknown>,
1972
+ );
1973
+ return this;
1974
+ }
1975
+
1976
+ /** Combine ordered item results into process state, once. @public */
1977
+ collect(fn: MappedCollect<TParams, TState, TResult>): MappedCollectBuilder<TParams, TState> {
1978
+ this.base.setMappedCollect(fn as MappedCollect<TParams, TState, unknown>);
1979
+ return new MappedCollectBuilder<TParams, TState>(this.base);
1980
+ }
1981
+ }
1982
+
1566
1983
  type StoredExternalActionBuilder<TParams, TState> = ExternalActionBuilder<
1567
1984
  TParams,
1568
1985
  TState,
package/src/index.ts CHANGED
@@ -132,7 +132,11 @@ export type {
132
132
  WorkerRunHandle,
133
133
  WorkerTurnHandler,
134
134
  } from "./extension-api.js";
135
- export { findUiLauncherById, SafeLaunchPreparationError } from "./extension-api.js";
135
+ export {
136
+ findUiLauncherById,
137
+ SafeLaunchPreparationError,
138
+ SafeOutcomePlanningError,
139
+ } from "./extension-api.js";
136
140
  export { defineExternalActionSource } from "./external-action-source.js";
137
141
  export {
138
142
  createExternalSourcePollReporter,
@@ -142,11 +146,17 @@ export {
142
146
  AutomaticOutcomeBuilder,
143
147
  createFlowPromptContext,
144
148
  type FlowAutomaticRunContext,
149
+ type FlowForEachOptions,
145
150
  FlowFragmentBuilder,
146
151
  type FlowLlmPreparationContext,
152
+ type FlowMappedPreparationContext,
153
+ type FlowMappedPromptContext,
147
154
  type FlowPromptContext,
148
155
  flow,
149
156
  HumanFlowBuilder,
157
+ MappedCollectBuilder,
158
+ MappedLlmFlowBuilder,
159
+ MappedOutcomeBuilder,
150
160
  } from "./flow.js";
151
161
  export { atomicWriteUtf8, hasErrorCode, isEnoent, isPathInside } from "./fs-utils.js";
152
162
  export {
@@ -168,6 +178,23 @@ export {
168
178
  normalizeLeafOutcomeCaptureResult,
169
179
  validateLeafOutcomeCaptureResult,
170
180
  } from "./leaf-outcomes.js";
181
+ export {
182
+ collectMappedResults,
183
+ type FrozenMappedItem,
184
+ freezeMappedItems,
185
+ MAPPED_COLLECT_TRIGGER,
186
+ type MappedCollect,
187
+ type MappedItemInput,
188
+ type MappedItemYield,
189
+ type MappedLlmTurnSpec,
190
+ type MappedOutcomeContext,
191
+ type MappedSnapshotInput,
192
+ type MappedTurnItemContext,
193
+ type MappedTurnIteration,
194
+ type MappedTurnServerContext,
195
+ type MappedYieldInput,
196
+ yieldMappedItemResult,
197
+ } from "./mapped-turn.js";
171
198
  export {
172
199
  builtinPiProvider,
173
200
  type ConfiguredProviderModel,
@@ -299,6 +326,7 @@ export {
299
326
  } from "./turn-semantics.js";
300
327
  export type {
301
328
  EventBus,
329
+ OutcomeToolArrayItemSpec,
302
330
  OutcomeToolParameterSpec,
303
331
  OutcomeToolSpec,
304
332
  PiCustomMessageInput,