@polka-codes/core 0.9.36 → 0.9.38
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/README.md +45 -0
- package/dist/_tsup-dts-rollup.d.ts +99 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +270 -46
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -53,6 +53,51 @@ class MyTool extends ToolBase {
|
|
|
53
53
|
}
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
+
### Nested workflows
|
|
57
|
+
|
|
58
|
+
You can reuse existing workflows inside other workflows with the `workflow` step. This is helpful when you want to repeat a
|
|
59
|
+
common CLI pattern such as `custom → agent → custom`.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import {
|
|
63
|
+
builder,
|
|
64
|
+
combineHandlers,
|
|
65
|
+
customStepSpecHandler,
|
|
66
|
+
makeAgentStepSpecHandler,
|
|
67
|
+
sequentialStepSpecHandler,
|
|
68
|
+
workflowStepSpecHandler,
|
|
69
|
+
} from '@polka-codes/core/workflow';
|
|
70
|
+
|
|
71
|
+
const reviewWorkflow = builder<{ code: string }>()
|
|
72
|
+
.custom('prepare', async ({ code }) => ({ prompt: `Review this snippet:\n${code}` }))
|
|
73
|
+
.agent('agent', {
|
|
74
|
+
messages: [{ type: 'function', fn: (input) => input.prompt }],
|
|
75
|
+
systemPrompt: 'You are a careful reviewer.',
|
|
76
|
+
parseOutput: (raw) => ({ success: true, data: { review: raw } }),
|
|
77
|
+
})
|
|
78
|
+
.custom('summarize', async (input) => ({ summary: input.review }))
|
|
79
|
+
.build();
|
|
80
|
+
|
|
81
|
+
const mainWorkflow = builder<{ code: string; notify: boolean }>()
|
|
82
|
+
.workflow('review', {
|
|
83
|
+
workflow: reviewWorkflow,
|
|
84
|
+
mapInput: (input) => ({ code: input.code }),
|
|
85
|
+
mapOutput: ({ workflowOutput, input }) => ({ summary: workflowOutput.summary, notify: input.notify }),
|
|
86
|
+
})
|
|
87
|
+
.custom('finalize', async ({ summary, notify }) => ({ summary, notified: notify }))
|
|
88
|
+
.build();
|
|
89
|
+
|
|
90
|
+
const handlers = combineHandlers(
|
|
91
|
+
sequentialStepSpecHandler,
|
|
92
|
+
customStepSpecHandler,
|
|
93
|
+
workflowStepSpecHandler,
|
|
94
|
+
makeAgentStepSpecHandler(async () => /* return a LanguageModelV2 */ throw new Error('model not configured')),
|
|
95
|
+
);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The optional `mapInput` and `mapOutput` helpers let you shape data for the child workflow and massage the result before the
|
|
99
|
+
next parent step runs.
|
|
100
|
+
|
|
56
101
|
## Development
|
|
57
102
|
|
|
58
103
|
### Building
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { FilePart } from 'ai';
|
|
2
2
|
import type { ImagePart } from 'ai';
|
|
3
3
|
import type { JSONValue } from '@ai-sdk/provider';
|
|
4
|
+
import { LanguageModelUsage } from 'ai';
|
|
4
5
|
import type { LanguageModelV2 } from '@ai-sdk/provider';
|
|
5
6
|
import type { LanguageModelV2ToolResultOutput } from '@ai-sdk/provider';
|
|
6
7
|
import type { LanguageModelV2Usage } from '@ai-sdk/provider';
|
|
@@ -234,6 +235,27 @@ export { BaseStepSpec }
|
|
|
234
235
|
export { BaseStepSpec as BaseStepSpec_alias_1 }
|
|
235
236
|
export { BaseStepSpec as BaseStepSpec_alias_2 }
|
|
236
237
|
|
|
238
|
+
declare interface BranchStepSpec<TInput extends Record<string, Json> = Record<string, Json>, TOutput extends Record<string, Json> = Record<string, Json>> extends BaseStepSpec<TInput, TOutput> {
|
|
239
|
+
type: 'branch';
|
|
240
|
+
branches: {
|
|
241
|
+
id: string;
|
|
242
|
+
when: (input: TInput & {
|
|
243
|
+
$: Record<string, Record<string, Json>>;
|
|
244
|
+
}, context: WorkflowContext) => boolean | Promise<boolean>;
|
|
245
|
+
step: BaseStepSpec<TInput, TOutput>;
|
|
246
|
+
}[];
|
|
247
|
+
otherwise?: BaseStepSpec<TInput, TOutput>;
|
|
248
|
+
}
|
|
249
|
+
export { BranchStepSpec }
|
|
250
|
+
export { BranchStepSpec as BranchStepSpec_alias_1 }
|
|
251
|
+
export { BranchStepSpec as BranchStepSpec_alias_2 }
|
|
252
|
+
|
|
253
|
+
declare const branchStepSpecHandler: StepSpecHandler;
|
|
254
|
+
export { branchStepSpecHandler }
|
|
255
|
+
export { branchStepSpecHandler as branchStepSpecHandler_alias_1 }
|
|
256
|
+
export { branchStepSpecHandler as branchStepSpecHandler_alias_2 }
|
|
257
|
+
export { branchStepSpecHandler as branchStepSpecHandler_alias_3 }
|
|
258
|
+
|
|
237
259
|
declare const builder: <TInput extends Record<string, Json>>() => StepsBuilder<TInput, TInput>;
|
|
238
260
|
export { builder }
|
|
239
261
|
export { builder as builder_alias_1 }
|
|
@@ -1009,6 +1031,55 @@ export { Json }
|
|
|
1009
1031
|
export { Json as Json_alias_1 }
|
|
1010
1032
|
export { Json as Json_alias_2 }
|
|
1011
1033
|
|
|
1034
|
+
declare interface LoopAccumulatorSpec<TInput extends Record<string, Json>, TIterationOutput extends Record<string, Json>, TAccumulator extends Record<string, Json>> {
|
|
1035
|
+
initial: TAccumulator | (() => TAccumulator);
|
|
1036
|
+
update: (state: LoopStepState<TInput, TIterationOutput, TAccumulator> & {
|
|
1037
|
+
result: TIterationOutput;
|
|
1038
|
+
}, context: WorkflowContext) => TAccumulator | Promise<TAccumulator>;
|
|
1039
|
+
}
|
|
1040
|
+
export { LoopAccumulatorSpec }
|
|
1041
|
+
export { LoopAccumulatorSpec as LoopAccumulatorSpec_alias_1 }
|
|
1042
|
+
export { LoopAccumulatorSpec as LoopAccumulatorSpec_alias_2 }
|
|
1043
|
+
|
|
1044
|
+
declare type LoopStepOutput<TIterationOutput extends Record<string, Json> = Record<string, Json>, TAccumulator extends Record<string, Json> = Record<string, Json>> = {
|
|
1045
|
+
results: TIterationOutput[];
|
|
1046
|
+
last?: TIterationOutput;
|
|
1047
|
+
accumulator?: TAccumulator;
|
|
1048
|
+
};
|
|
1049
|
+
export { LoopStepOutput }
|
|
1050
|
+
export { LoopStepOutput as LoopStepOutput_alias_1 }
|
|
1051
|
+
export { LoopStepOutput as LoopStepOutput_alias_2 }
|
|
1052
|
+
|
|
1053
|
+
declare interface LoopStepSpec<TInput extends Record<string, Json> = Record<string, Json>, TIterationInput extends Record<string, Json> = TInput, TIterationOutput extends Record<string, Json> = Record<string, Json>, TAccumulator extends Record<string, Json> = Record<string, Json>> extends BaseStepSpec<TInput, LoopStepOutput<TIterationOutput, TAccumulator>> {
|
|
1054
|
+
type: 'loop';
|
|
1055
|
+
step: BaseStepSpec<TIterationInput, TIterationOutput>;
|
|
1056
|
+
while?: (state: LoopStepState<TInput, TIterationOutput, TAccumulator>, context: WorkflowContext) => boolean | Promise<boolean>;
|
|
1057
|
+
until?: (state: LoopStepState<TInput, TIterationOutput, TAccumulator>, context: WorkflowContext) => boolean | Promise<boolean>;
|
|
1058
|
+
maxIterations?: number;
|
|
1059
|
+
mapInput?: (state: LoopStepState<TInput, TIterationOutput, TAccumulator>, context: WorkflowContext) => TIterationInput | Promise<TIterationInput>;
|
|
1060
|
+
accumulator?: LoopAccumulatorSpec<TInput, TIterationOutput, TAccumulator>;
|
|
1061
|
+
}
|
|
1062
|
+
export { LoopStepSpec }
|
|
1063
|
+
export { LoopStepSpec as LoopStepSpec_alias_1 }
|
|
1064
|
+
export { LoopStepSpec as LoopStepSpec_alias_2 }
|
|
1065
|
+
|
|
1066
|
+
declare const loopStepSpecHandler: StepSpecHandler;
|
|
1067
|
+
export { loopStepSpecHandler }
|
|
1068
|
+
export { loopStepSpecHandler as loopStepSpecHandler_alias_1 }
|
|
1069
|
+
export { loopStepSpecHandler as loopStepSpecHandler_alias_2 }
|
|
1070
|
+
export { loopStepSpecHandler as loopStepSpecHandler_alias_3 }
|
|
1071
|
+
|
|
1072
|
+
declare type LoopStepState<TInput extends Record<string, Json> = Record<string, Json>, TIterationOutput extends Record<string, Json> = Record<string, Json>, TAccumulator extends Record<string, Json> = Record<string, Json>> = {
|
|
1073
|
+
iteration: number;
|
|
1074
|
+
input: TInput;
|
|
1075
|
+
results: TIterationOutput[];
|
|
1076
|
+
last?: TIterationOutput;
|
|
1077
|
+
accumulator?: TAccumulator;
|
|
1078
|
+
};
|
|
1079
|
+
export { LoopStepState }
|
|
1080
|
+
export { LoopStepState as LoopStepState_alias_1 }
|
|
1081
|
+
export { LoopStepState as LoopStepState_alias_2 }
|
|
1082
|
+
|
|
1012
1083
|
declare const makeAgentStepSpecHandler: (getModelFn: (step: AgentStepSpec, context: WorkflowContext) => Promise<LanguageModelV2>) => StepSpecHandler;
|
|
1013
1084
|
export { makeAgentStepSpecHandler }
|
|
1014
1085
|
export { makeAgentStepSpecHandler as makeAgentStepSpecHandler_alias_1 }
|
|
@@ -1310,9 +1381,12 @@ declare class StepsBuilder<TInput extends Record<string, Json> = Record<string,
|
|
|
1310
1381
|
build(): BaseStepSpec<TInput, TOutput>;
|
|
1311
1382
|
step<TStepOutput extends Record<string, Json>, TStepSpec extends BaseStepSpec<TOutput, TStepOutput>>(step: TStepSpec): StepsBuilder<TInput, TStepOutput>;
|
|
1312
1383
|
parallel<TStepOutput extends Record<string, Json>>(id: string, step: BaseStepSpec<TOutput, TStepOutput>): StepsBuilder<TInput, TStepOutput>;
|
|
1384
|
+
loop<TIterationInput extends Record<string, Json> = TOutput, TIterationOutput extends Record<string, Json> = Record<string, Json>, TAccumulator extends Record<string, Json> = Record<string, Json>>(id: string, config: Omit<LoopStepSpec<TOutput, TIterationInput, TIterationOutput, TAccumulator>, 'id' | 'type'>): StepsBuilder<TInput, LoopStepOutput<TIterationOutput, TAccumulator>>;
|
|
1313
1385
|
custom<TStepOutput extends Record<string, Json>>(spec: CustomStepSpec<TOutput, TStepOutput>): StepsBuilder<TInput, TStepOutput>;
|
|
1314
1386
|
custom<TStepOutput extends Record<string, Json>>(id: string, run: CustomStepSpec<TOutput, TStepOutput>['run']): StepsBuilder<TInput, TStepOutput>;
|
|
1387
|
+
workflow<TWorkflowInput extends Record<string, Json> = TOutput, TWorkflowOutput extends Record<string, Json> = Record<string, Json>, TStepOutput extends Record<string, Json> = TWorkflowOutput>(id: string, config: Omit<WorkflowStepSpec<TOutput, TWorkflowInput, TWorkflowOutput, TStepOutput>, 'id' | 'type'>): StepsBuilder<TInput, TStepOutput>;
|
|
1315
1388
|
agent<TStepOutput extends Record<string, Json>>(id: string, spec: Omit<AgentStepSpec<TOutput, TStepOutput>, 'id' | 'type'>): StepsBuilder<TInput, TStepOutput>;
|
|
1389
|
+
branch<TStepOutput extends Record<string, Json>>(id: string, config: Omit<BranchStepSpec<TOutput, TStepOutput>, 'id' | 'type'>): StepsBuilder<TInput, TStepOutput>;
|
|
1316
1390
|
}
|
|
1317
1391
|
|
|
1318
1392
|
declare type StepSpecHandler = {
|
|
@@ -1509,6 +1583,7 @@ export { TaskEventToolUse as TaskEventToolUse_alias_2 }
|
|
|
1509
1583
|
*/
|
|
1510
1584
|
declare interface TaskEventUsage extends TaskEventBase {
|
|
1511
1585
|
kind: TaskEventKind.Usage;
|
|
1586
|
+
usage: LanguageModelUsage;
|
|
1512
1587
|
}
|
|
1513
1588
|
export { TaskEventUsage }
|
|
1514
1589
|
export { TaskEventUsage as TaskEventUsage_alias_1 }
|
|
@@ -2011,4 +2086,28 @@ export { WorkflowSpecRaw }
|
|
|
2011
2086
|
export { WorkflowSpecRaw as WorkflowSpecRaw_alias_1 }
|
|
2012
2087
|
export { WorkflowSpecRaw as WorkflowSpecRaw_alias_2 }
|
|
2013
2088
|
|
|
2089
|
+
declare interface WorkflowStepSpec<TInput extends Record<string, Json> = Record<string, Json>, TWorkflowInput extends Record<string, Json> = TInput, TWorkflowOutput extends Record<string, Json> = Record<string, Json>, TOutput extends Record<string, Json> = TWorkflowOutput> extends BaseStepSpec<TInput, TOutput> {
|
|
2090
|
+
type: 'workflow';
|
|
2091
|
+
workflow: WorkflowSpec<TWorkflowInput, TWorkflowOutput>;
|
|
2092
|
+
mapInput?: (input: TInput & {
|
|
2093
|
+
$: Record<string, Record<string, Json>>;
|
|
2094
|
+
}, context: WorkflowContext) => TWorkflowInput | Promise<TWorkflowInput>;
|
|
2095
|
+
mapOutput?: (params: {
|
|
2096
|
+
input: TInput & {
|
|
2097
|
+
$: Record<string, Record<string, Json>>;
|
|
2098
|
+
};
|
|
2099
|
+
workflowInput: TWorkflowInput;
|
|
2100
|
+
workflowOutput: TWorkflowOutput;
|
|
2101
|
+
}, context: WorkflowContext) => TOutput | Promise<TOutput>;
|
|
2102
|
+
}
|
|
2103
|
+
export { WorkflowStepSpec }
|
|
2104
|
+
export { WorkflowStepSpec as WorkflowStepSpec_alias_1 }
|
|
2105
|
+
export { WorkflowStepSpec as WorkflowStepSpec_alias_2 }
|
|
2106
|
+
|
|
2107
|
+
declare const workflowStepSpecHandler: StepSpecHandler;
|
|
2108
|
+
export { workflowStepSpecHandler }
|
|
2109
|
+
export { workflowStepSpecHandler as workflowStepSpecHandler_alias_1 }
|
|
2110
|
+
export { workflowStepSpecHandler as workflowStepSpecHandler_alias_2 }
|
|
2111
|
+
export { workflowStepSpecHandler as workflowStepSpecHandler_alias_3 }
|
|
2112
|
+
|
|
2014
2113
|
export { }
|
package/dist/index.d.ts
CHANGED
|
@@ -118,9 +118,12 @@ export { builder } from './_tsup-dts-rollup.js';
|
|
|
118
118
|
export { CommandStepSpec } from './_tsup-dts-rollup.js';
|
|
119
119
|
export { commandStepSpecHandler } from './_tsup-dts-rollup.js';
|
|
120
120
|
export { command } from './_tsup-dts-rollup.js';
|
|
121
|
+
export { branchStepSpecHandler } from './_tsup-dts-rollup.js';
|
|
121
122
|
export { customStepSpecHandler } from './_tsup-dts-rollup.js';
|
|
123
|
+
export { loopStepSpecHandler } from './_tsup-dts-rollup.js';
|
|
122
124
|
export { parallelStepSpecHandler } from './_tsup-dts-rollup.js';
|
|
123
125
|
export { sequentialStepSpecHandler } from './_tsup-dts-rollup.js';
|
|
126
|
+
export { workflowStepSpecHandler } from './_tsup-dts-rollup.js';
|
|
124
127
|
export { step } from './_tsup-dts-rollup.js';
|
|
125
128
|
export { combineHandlers } from './_tsup-dts-rollup.js';
|
|
126
129
|
export { Json } from './_tsup-dts-rollup.js';
|
|
@@ -130,6 +133,12 @@ export { WorkflowContext } from './_tsup-dts-rollup.js';
|
|
|
130
133
|
export { StepSpecRaw } from './_tsup-dts-rollup.js';
|
|
131
134
|
export { SequentialStepSpec } from './_tsup-dts-rollup.js';
|
|
132
135
|
export { ParallelStepSpec } from './_tsup-dts-rollup.js';
|
|
136
|
+
export { LoopStepState } from './_tsup-dts-rollup.js';
|
|
137
|
+
export { LoopAccumulatorSpec } from './_tsup-dts-rollup.js';
|
|
138
|
+
export { LoopStepOutput } from './_tsup-dts-rollup.js';
|
|
139
|
+
export { LoopStepSpec } from './_tsup-dts-rollup.js';
|
|
140
|
+
export { WorkflowStepSpec } from './_tsup-dts-rollup.js';
|
|
141
|
+
export { BranchStepSpec } from './_tsup-dts-rollup.js';
|
|
133
142
|
export { CustomStepSpec } from './_tsup-dts-rollup.js';
|
|
134
143
|
export { StepRunResultSuccess } from './_tsup-dts-rollup.js';
|
|
135
144
|
export { StepRunResultPaused } from './_tsup-dts-rollup.js';
|
package/dist/index.js
CHANGED
|
@@ -2065,6 +2065,7 @@ Request timeout after ${requestTimeoutSeconds} seconds. Canceling current reques
|
|
|
2065
2065
|
};
|
|
2066
2066
|
try {
|
|
2067
2067
|
resetTimeout();
|
|
2068
|
+
const usageMeterOnFinishHandler = this.config.usageMeter.onFinishHandler(this.ai);
|
|
2068
2069
|
const streamTextOptions = {
|
|
2069
2070
|
model: this.ai,
|
|
2070
2071
|
temperature: 0,
|
|
@@ -2083,7 +2084,10 @@ Request timeout after ${requestTimeoutSeconds} seconds. Canceling current reques
|
|
|
2083
2084
|
break;
|
|
2084
2085
|
}
|
|
2085
2086
|
},
|
|
2086
|
-
onFinish:
|
|
2087
|
+
onFinish: (evt) => {
|
|
2088
|
+
usageMeterOnFinishHandler(evt);
|
|
2089
|
+
this.#callback({ kind: "Usage" /* Usage */, agent: this, usage: evt.totalUsage });
|
|
2090
|
+
},
|
|
2087
2091
|
onError: async (error) => {
|
|
2088
2092
|
console.error("Error in stream:", error);
|
|
2089
2093
|
},
|
|
@@ -3723,7 +3727,13 @@ var makeAgentStepSpecHandler = (getModelFn) => ({
|
|
|
3723
3727
|
const handleExitReason = async (reason) => {
|
|
3724
3728
|
switch (reason.type) {
|
|
3725
3729
|
case "Pause":
|
|
3726
|
-
return {
|
|
3730
|
+
return {
|
|
3731
|
+
type: "paused",
|
|
3732
|
+
state: {
|
|
3733
|
+
messages: agent.messages,
|
|
3734
|
+
usage: usageMeter.usage
|
|
3735
|
+
}
|
|
3736
|
+
};
|
|
3727
3737
|
case "UsageExceeded":
|
|
3728
3738
|
return { type: "error", error: new Error("Usage limit exceeded") };
|
|
3729
3739
|
case "Exit" /* Exit */: {
|
|
@@ -3833,6 +3843,13 @@ var StepsBuilder = class {
|
|
|
3833
3843
|
step: step2
|
|
3834
3844
|
});
|
|
3835
3845
|
}
|
|
3846
|
+
loop(id, config) {
|
|
3847
|
+
return this.step({
|
|
3848
|
+
...config,
|
|
3849
|
+
id,
|
|
3850
|
+
type: "loop"
|
|
3851
|
+
});
|
|
3852
|
+
}
|
|
3836
3853
|
custom(idOrSpec, run2) {
|
|
3837
3854
|
if (typeof idOrSpec === "string") {
|
|
3838
3855
|
if (!run2) {
|
|
@@ -3846,6 +3863,13 @@ var StepsBuilder = class {
|
|
|
3846
3863
|
}
|
|
3847
3864
|
return this.step(idOrSpec);
|
|
3848
3865
|
}
|
|
3866
|
+
workflow(id, config) {
|
|
3867
|
+
return this.step({
|
|
3868
|
+
...config,
|
|
3869
|
+
id,
|
|
3870
|
+
type: "workflow"
|
|
3871
|
+
});
|
|
3872
|
+
}
|
|
3849
3873
|
agent(id, spec) {
|
|
3850
3874
|
return this.step({
|
|
3851
3875
|
...spec,
|
|
@@ -3853,6 +3877,13 @@ var StepsBuilder = class {
|
|
|
3853
3877
|
type: "agent"
|
|
3854
3878
|
});
|
|
3855
3879
|
}
|
|
3880
|
+
branch(id, config) {
|
|
3881
|
+
return this.step({
|
|
3882
|
+
...config,
|
|
3883
|
+
id,
|
|
3884
|
+
type: "branch"
|
|
3885
|
+
});
|
|
3886
|
+
}
|
|
3856
3887
|
};
|
|
3857
3888
|
var builder = () => {
|
|
3858
3889
|
return new StepsBuilder();
|
|
@@ -3930,6 +3961,50 @@ var runStep = async (step2, input, context, resumedState, allOutputs) => {
|
|
|
3930
3961
|
}
|
|
3931
3962
|
};
|
|
3932
3963
|
|
|
3964
|
+
// src/workflow/workflow.ts
|
|
3965
|
+
var run = async (workflow, context, handler15, input) => {
|
|
3966
|
+
const rootStep = handler15(workflow.step, handler15);
|
|
3967
|
+
const result = await runStep(rootStep, input, context, void 0, {});
|
|
3968
|
+
switch (result.type) {
|
|
3969
|
+
case "paused":
|
|
3970
|
+
return {
|
|
3971
|
+
type: "paused",
|
|
3972
|
+
state: result.state
|
|
3973
|
+
};
|
|
3974
|
+
case "error":
|
|
3975
|
+
return {
|
|
3976
|
+
type: "error",
|
|
3977
|
+
error: result.error
|
|
3978
|
+
};
|
|
3979
|
+
case "success":
|
|
3980
|
+
return {
|
|
3981
|
+
type: "success",
|
|
3982
|
+
output: result.output
|
|
3983
|
+
};
|
|
3984
|
+
}
|
|
3985
|
+
};
|
|
3986
|
+
var resume = async (workflow, context, handler15, state, input) => {
|
|
3987
|
+
const rootStep = handler15(workflow.step, handler15);
|
|
3988
|
+
const result = await runStep(rootStep, input, context, state, {});
|
|
3989
|
+
switch (result.type) {
|
|
3990
|
+
case "paused":
|
|
3991
|
+
return {
|
|
3992
|
+
type: "paused",
|
|
3993
|
+
state: result.state
|
|
3994
|
+
};
|
|
3995
|
+
case "error":
|
|
3996
|
+
return {
|
|
3997
|
+
type: "error",
|
|
3998
|
+
error: result.error
|
|
3999
|
+
};
|
|
4000
|
+
case "success":
|
|
4001
|
+
return {
|
|
4002
|
+
type: "success",
|
|
4003
|
+
output: result.output
|
|
4004
|
+
};
|
|
4005
|
+
}
|
|
4006
|
+
};
|
|
4007
|
+
|
|
3933
4008
|
// src/workflow/steps.ts
|
|
3934
4009
|
var combineHandlers = (...handlers) => {
|
|
3935
4010
|
const allHandlers = {};
|
|
@@ -4019,6 +4094,196 @@ var parallelStepSpecHandler = {
|
|
|
4019
4094
|
};
|
|
4020
4095
|
}
|
|
4021
4096
|
};
|
|
4097
|
+
var loopStepSpecHandler = {
|
|
4098
|
+
type: "loop",
|
|
4099
|
+
handler(step2, rootHandler) {
|
|
4100
|
+
const innerStep = rootHandler(step2.step, rootHandler);
|
|
4101
|
+
return {
|
|
4102
|
+
...step2,
|
|
4103
|
+
async run(input, context, resumedState) {
|
|
4104
|
+
const allOutputs = input.$ ?? {};
|
|
4105
|
+
const initialInput = { ...input };
|
|
4106
|
+
delete initialInput.$;
|
|
4107
|
+
const results = resumedState ? [...resumedState.results] : [];
|
|
4108
|
+
let iteration = resumedState?.iteration ?? results.length;
|
|
4109
|
+
let accumulator = resumedState?.accumulator;
|
|
4110
|
+
if (step2.accumulator && accumulator === void 0) {
|
|
4111
|
+
const initialAccumulator = step2.accumulator.initial;
|
|
4112
|
+
accumulator = typeof initialAccumulator === "function" ? initialAccumulator() : initialAccumulator;
|
|
4113
|
+
}
|
|
4114
|
+
const hasPredicates = Boolean(step2.while || step2.until);
|
|
4115
|
+
const effectiveMaxIterations = step2.maxIterations ?? (hasPredicates ? void 0 : 1);
|
|
4116
|
+
const createState = () => ({
|
|
4117
|
+
iteration,
|
|
4118
|
+
input: initialInput,
|
|
4119
|
+
results,
|
|
4120
|
+
last: results[results.length - 1],
|
|
4121
|
+
accumulator
|
|
4122
|
+
});
|
|
4123
|
+
const completeSuccess = () => {
|
|
4124
|
+
const output = { results };
|
|
4125
|
+
if (results.length > 0) {
|
|
4126
|
+
output.last = results[results.length - 1];
|
|
4127
|
+
}
|
|
4128
|
+
if (accumulator !== void 0 && step2.accumulator) {
|
|
4129
|
+
output.accumulator = accumulator;
|
|
4130
|
+
}
|
|
4131
|
+
return { type: "success", output };
|
|
4132
|
+
};
|
|
4133
|
+
const shouldContinue = async () => {
|
|
4134
|
+
const state = createState();
|
|
4135
|
+
if (step2.until) {
|
|
4136
|
+
const shouldStop = await step2.until(state, context);
|
|
4137
|
+
if (shouldStop) {
|
|
4138
|
+
return false;
|
|
4139
|
+
}
|
|
4140
|
+
}
|
|
4141
|
+
if (step2.while) {
|
|
4142
|
+
return Boolean(await step2.while(state, context));
|
|
4143
|
+
}
|
|
4144
|
+
return true;
|
|
4145
|
+
};
|
|
4146
|
+
let pendingResumeState = resumedState;
|
|
4147
|
+
while (true) {
|
|
4148
|
+
if (!hasPredicates && effectiveMaxIterations !== void 0 && iteration >= effectiveMaxIterations) {
|
|
4149
|
+
return completeSuccess();
|
|
4150
|
+
}
|
|
4151
|
+
if (hasPredicates) {
|
|
4152
|
+
const canContinue = await shouldContinue();
|
|
4153
|
+
if (!canContinue) {
|
|
4154
|
+
return completeSuccess();
|
|
4155
|
+
}
|
|
4156
|
+
if (step2.maxIterations !== void 0 && iteration >= step2.maxIterations) {
|
|
4157
|
+
return {
|
|
4158
|
+
type: "error",
|
|
4159
|
+
error: new Error(`Loop exceeded max iterations: ${step2.maxIterations}`)
|
|
4160
|
+
};
|
|
4161
|
+
}
|
|
4162
|
+
}
|
|
4163
|
+
const stateBeforeRun = createState();
|
|
4164
|
+
const iterationInput = step2.mapInput ? await step2.mapInput(stateBeforeRun, context) : stateBeforeRun.last ?? initialInput;
|
|
4165
|
+
const resumeStateForInner = pendingResumeState && pendingResumeState.iteration === iteration ? pendingResumeState.stepState : void 0;
|
|
4166
|
+
const result = await runStep(innerStep, iterationInput, context, resumeStateForInner, allOutputs);
|
|
4167
|
+
if (result.type === "paused") {
|
|
4168
|
+
return {
|
|
4169
|
+
type: "paused",
|
|
4170
|
+
state: {
|
|
4171
|
+
iteration,
|
|
4172
|
+
stepState: result.state,
|
|
4173
|
+
results,
|
|
4174
|
+
accumulator
|
|
4175
|
+
}
|
|
4176
|
+
};
|
|
4177
|
+
}
|
|
4178
|
+
if (result.type === "error") {
|
|
4179
|
+
return result;
|
|
4180
|
+
}
|
|
4181
|
+
results[iteration] = result.output;
|
|
4182
|
+
if (step2.accumulator) {
|
|
4183
|
+
const updated = await step2.accumulator.update(
|
|
4184
|
+
{
|
|
4185
|
+
...stateBeforeRun,
|
|
4186
|
+
results,
|
|
4187
|
+
last: result.output,
|
|
4188
|
+
accumulator,
|
|
4189
|
+
result: result.output
|
|
4190
|
+
},
|
|
4191
|
+
context
|
|
4192
|
+
);
|
|
4193
|
+
accumulator = updated;
|
|
4194
|
+
}
|
|
4195
|
+
iteration += 1;
|
|
4196
|
+
pendingResumeState = void 0;
|
|
4197
|
+
}
|
|
4198
|
+
}
|
|
4199
|
+
};
|
|
4200
|
+
}
|
|
4201
|
+
};
|
|
4202
|
+
var workflowStepSpecHandler = {
|
|
4203
|
+
type: "workflow",
|
|
4204
|
+
handler(step2, rootHandler) {
|
|
4205
|
+
return {
|
|
4206
|
+
...step2,
|
|
4207
|
+
async run(input, context, resumedState) {
|
|
4208
|
+
const { $, ...rest } = input;
|
|
4209
|
+
void $;
|
|
4210
|
+
const defaultWorkflowInput = rest;
|
|
4211
|
+
const workflowInput = step2.mapInput ? await step2.mapInput(input, context) : defaultWorkflowInput;
|
|
4212
|
+
const workflowState = resumedState && "workflowState" in resumedState ? resumedState.workflowState : resumedState;
|
|
4213
|
+
const result = workflowState ? await resume(step2.workflow, context, rootHandler, workflowState, workflowInput) : await run(step2.workflow, context, rootHandler, workflowInput);
|
|
4214
|
+
if (result.type === "paused") {
|
|
4215
|
+
return { type: "paused", state: { workflowState: result.state, workflowInput } };
|
|
4216
|
+
}
|
|
4217
|
+
if (result.type === "error") {
|
|
4218
|
+
return { type: "error", error: result.error };
|
|
4219
|
+
}
|
|
4220
|
+
const finalOutput = step2.mapOutput ? await step2.mapOutput(
|
|
4221
|
+
{
|
|
4222
|
+
input,
|
|
4223
|
+
workflowInput,
|
|
4224
|
+
workflowOutput: result.output
|
|
4225
|
+
},
|
|
4226
|
+
context
|
|
4227
|
+
) : result.output;
|
|
4228
|
+
return { type: "success", output: finalOutput };
|
|
4229
|
+
}
|
|
4230
|
+
};
|
|
4231
|
+
}
|
|
4232
|
+
};
|
|
4233
|
+
var branchStepSpecHandler = {
|
|
4234
|
+
type: "branch",
|
|
4235
|
+
handler(step2, rootHandler) {
|
|
4236
|
+
const branches = step2.branches.map((branch) => ({
|
|
4237
|
+
...branch,
|
|
4238
|
+
step: rootHandler(branch.step, rootHandler)
|
|
4239
|
+
}));
|
|
4240
|
+
const otherwise = step2.otherwise ? rootHandler(step2.otherwise, rootHandler) : void 0;
|
|
4241
|
+
return {
|
|
4242
|
+
...step2,
|
|
4243
|
+
async run(input, context, resumedState) {
|
|
4244
|
+
const allOutputs = input.$ ?? {};
|
|
4245
|
+
let branchIndex = resumedState?.branchIndex;
|
|
4246
|
+
let stepToRun;
|
|
4247
|
+
if (branchIndex === void 0) {
|
|
4248
|
+
for (let i = 0; i < branches.length; i++) {
|
|
4249
|
+
const shouldRun = await branches[i].when(input, context);
|
|
4250
|
+
if (shouldRun) {
|
|
4251
|
+
branchIndex = i;
|
|
4252
|
+
break;
|
|
4253
|
+
}
|
|
4254
|
+
}
|
|
4255
|
+
if (branchIndex === void 0 && otherwise) {
|
|
4256
|
+
branchIndex = branches.length;
|
|
4257
|
+
}
|
|
4258
|
+
}
|
|
4259
|
+
if (branchIndex !== void 0) {
|
|
4260
|
+
if (branchIndex < branches.length) {
|
|
4261
|
+
stepToRun = branches[branchIndex].step;
|
|
4262
|
+
} else if (branchIndex === branches.length && otherwise) {
|
|
4263
|
+
stepToRun = otherwise;
|
|
4264
|
+
}
|
|
4265
|
+
}
|
|
4266
|
+
if (!stepToRun) {
|
|
4267
|
+
return {
|
|
4268
|
+
type: "error",
|
|
4269
|
+
error: new Error("No branch predicate matched and no otherwise branch provided")
|
|
4270
|
+
};
|
|
4271
|
+
}
|
|
4272
|
+
const result = await runStep(
|
|
4273
|
+
stepToRun,
|
|
4274
|
+
input,
|
|
4275
|
+
context,
|
|
4276
|
+
branchIndex === resumedState?.branchIndex ? resumedState?.stepState : void 0,
|
|
4277
|
+
allOutputs
|
|
4278
|
+
);
|
|
4279
|
+
if (result.type === "paused") {
|
|
4280
|
+
return { type: "paused", state: { branchIndex, stepState: result.state } };
|
|
4281
|
+
}
|
|
4282
|
+
return result;
|
|
4283
|
+
}
|
|
4284
|
+
};
|
|
4285
|
+
}
|
|
4286
|
+
};
|
|
4022
4287
|
var customStepSpecHandler = {
|
|
4023
4288
|
type: "custom",
|
|
4024
4289
|
handler(step2) {
|
|
@@ -4033,50 +4298,6 @@ var customStepSpecHandler = {
|
|
|
4033
4298
|
function step(id, run2) {
|
|
4034
4299
|
return { id, type: "custom", run: run2 };
|
|
4035
4300
|
}
|
|
4036
|
-
|
|
4037
|
-
// src/workflow/workflow.ts
|
|
4038
|
-
var run = async (workflow, context, handler15, input) => {
|
|
4039
|
-
const rootStep = handler15(workflow.step, handler15);
|
|
4040
|
-
const result = await runStep(rootStep, input, context, void 0, {});
|
|
4041
|
-
switch (result.type) {
|
|
4042
|
-
case "paused":
|
|
4043
|
-
return {
|
|
4044
|
-
type: "paused",
|
|
4045
|
-
state: result.state
|
|
4046
|
-
};
|
|
4047
|
-
case "error":
|
|
4048
|
-
return {
|
|
4049
|
-
type: "error",
|
|
4050
|
-
error: result.error
|
|
4051
|
-
};
|
|
4052
|
-
case "success":
|
|
4053
|
-
return {
|
|
4054
|
-
type: "success",
|
|
4055
|
-
output: result.output
|
|
4056
|
-
};
|
|
4057
|
-
}
|
|
4058
|
-
};
|
|
4059
|
-
var resume = async (workflow, context, handler15, state, input) => {
|
|
4060
|
-
const rootStep = handler15(workflow.step, handler15);
|
|
4061
|
-
const result = await runStep(rootStep, input, context, state, {});
|
|
4062
|
-
switch (result.type) {
|
|
4063
|
-
case "paused":
|
|
4064
|
-
return {
|
|
4065
|
-
type: "paused",
|
|
4066
|
-
state: result.state
|
|
4067
|
-
};
|
|
4068
|
-
case "error":
|
|
4069
|
-
return {
|
|
4070
|
-
type: "error",
|
|
4071
|
-
error: result.error
|
|
4072
|
-
};
|
|
4073
|
-
case "success":
|
|
4074
|
-
return {
|
|
4075
|
-
type: "success",
|
|
4076
|
-
output: result.output
|
|
4077
|
-
};
|
|
4078
|
-
}
|
|
4079
|
-
};
|
|
4080
4301
|
export {
|
|
4081
4302
|
AgentBase,
|
|
4082
4303
|
AnalyzerAgent,
|
|
@@ -4097,6 +4318,7 @@ export {
|
|
|
4097
4318
|
architectAgentInfo,
|
|
4098
4319
|
askFollowupQuestion_default as askFollowupQuestion,
|
|
4099
4320
|
attemptCompletion_default as attemptCompletion,
|
|
4321
|
+
branchStepSpecHandler,
|
|
4100
4322
|
builder,
|
|
4101
4323
|
capabilities,
|
|
4102
4324
|
codeFixerAgentInfo,
|
|
@@ -4119,6 +4341,7 @@ export {
|
|
|
4119
4341
|
getAvailableTools,
|
|
4120
4342
|
handOver_default as handOver,
|
|
4121
4343
|
listFiles_default as listFiles,
|
|
4344
|
+
loopStepSpecHandler,
|
|
4122
4345
|
makeAgentStepSpecHandler,
|
|
4123
4346
|
makeAgentTool,
|
|
4124
4347
|
makeMultiAgentTool,
|
|
@@ -4140,5 +4363,6 @@ export {
|
|
|
4140
4363
|
step,
|
|
4141
4364
|
systemInformation,
|
|
4142
4365
|
toolUsePrompt,
|
|
4366
|
+
workflowStepSpecHandler,
|
|
4143
4367
|
writeToFile_default as writeToFile
|
|
4144
4368
|
};
|