@polka-codes/core 0.9.37 → 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 +97 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +265 -45
- 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
|
|
@@ -235,6 +235,27 @@ export { BaseStepSpec }
|
|
|
235
235
|
export { BaseStepSpec as BaseStepSpec_alias_1 }
|
|
236
236
|
export { BaseStepSpec as BaseStepSpec_alias_2 }
|
|
237
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
|
+
|
|
238
259
|
declare const builder: <TInput extends Record<string, Json>>() => StepsBuilder<TInput, TInput>;
|
|
239
260
|
export { builder }
|
|
240
261
|
export { builder as builder_alias_1 }
|
|
@@ -1010,6 +1031,55 @@ export { Json }
|
|
|
1010
1031
|
export { Json as Json_alias_1 }
|
|
1011
1032
|
export { Json as Json_alias_2 }
|
|
1012
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
|
+
|
|
1013
1083
|
declare const makeAgentStepSpecHandler: (getModelFn: (step: AgentStepSpec, context: WorkflowContext) => Promise<LanguageModelV2>) => StepSpecHandler;
|
|
1014
1084
|
export { makeAgentStepSpecHandler }
|
|
1015
1085
|
export { makeAgentStepSpecHandler as makeAgentStepSpecHandler_alias_1 }
|
|
@@ -1311,9 +1381,12 @@ declare class StepsBuilder<TInput extends Record<string, Json> = Record<string,
|
|
|
1311
1381
|
build(): BaseStepSpec<TInput, TOutput>;
|
|
1312
1382
|
step<TStepOutput extends Record<string, Json>, TStepSpec extends BaseStepSpec<TOutput, TStepOutput>>(step: TStepSpec): StepsBuilder<TInput, TStepOutput>;
|
|
1313
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>>;
|
|
1314
1385
|
custom<TStepOutput extends Record<string, Json>>(spec: CustomStepSpec<TOutput, TStepOutput>): StepsBuilder<TInput, TStepOutput>;
|
|
1315
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>;
|
|
1316
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>;
|
|
1317
1390
|
}
|
|
1318
1391
|
|
|
1319
1392
|
declare type StepSpecHandler = {
|
|
@@ -2013,4 +2086,28 @@ export { WorkflowSpecRaw }
|
|
|
2013
2086
|
export { WorkflowSpecRaw as WorkflowSpecRaw_alias_1 }
|
|
2014
2087
|
export { WorkflowSpecRaw as WorkflowSpecRaw_alias_2 }
|
|
2015
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
|
+
|
|
2016
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
|
@@ -3727,7 +3727,13 @@ var makeAgentStepSpecHandler = (getModelFn) => ({
|
|
|
3727
3727
|
const handleExitReason = async (reason) => {
|
|
3728
3728
|
switch (reason.type) {
|
|
3729
3729
|
case "Pause":
|
|
3730
|
-
return {
|
|
3730
|
+
return {
|
|
3731
|
+
type: "paused",
|
|
3732
|
+
state: {
|
|
3733
|
+
messages: agent.messages,
|
|
3734
|
+
usage: usageMeter.usage
|
|
3735
|
+
}
|
|
3736
|
+
};
|
|
3731
3737
|
case "UsageExceeded":
|
|
3732
3738
|
return { type: "error", error: new Error("Usage limit exceeded") };
|
|
3733
3739
|
case "Exit" /* Exit */: {
|
|
@@ -3837,6 +3843,13 @@ var StepsBuilder = class {
|
|
|
3837
3843
|
step: step2
|
|
3838
3844
|
});
|
|
3839
3845
|
}
|
|
3846
|
+
loop(id, config) {
|
|
3847
|
+
return this.step({
|
|
3848
|
+
...config,
|
|
3849
|
+
id,
|
|
3850
|
+
type: "loop"
|
|
3851
|
+
});
|
|
3852
|
+
}
|
|
3840
3853
|
custom(idOrSpec, run2) {
|
|
3841
3854
|
if (typeof idOrSpec === "string") {
|
|
3842
3855
|
if (!run2) {
|
|
@@ -3850,6 +3863,13 @@ var StepsBuilder = class {
|
|
|
3850
3863
|
}
|
|
3851
3864
|
return this.step(idOrSpec);
|
|
3852
3865
|
}
|
|
3866
|
+
workflow(id, config) {
|
|
3867
|
+
return this.step({
|
|
3868
|
+
...config,
|
|
3869
|
+
id,
|
|
3870
|
+
type: "workflow"
|
|
3871
|
+
});
|
|
3872
|
+
}
|
|
3853
3873
|
agent(id, spec) {
|
|
3854
3874
|
return this.step({
|
|
3855
3875
|
...spec,
|
|
@@ -3857,6 +3877,13 @@ var StepsBuilder = class {
|
|
|
3857
3877
|
type: "agent"
|
|
3858
3878
|
});
|
|
3859
3879
|
}
|
|
3880
|
+
branch(id, config) {
|
|
3881
|
+
return this.step({
|
|
3882
|
+
...config,
|
|
3883
|
+
id,
|
|
3884
|
+
type: "branch"
|
|
3885
|
+
});
|
|
3886
|
+
}
|
|
3860
3887
|
};
|
|
3861
3888
|
var builder = () => {
|
|
3862
3889
|
return new StepsBuilder();
|
|
@@ -3934,6 +3961,50 @@ var runStep = async (step2, input, context, resumedState, allOutputs) => {
|
|
|
3934
3961
|
}
|
|
3935
3962
|
};
|
|
3936
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
|
+
|
|
3937
4008
|
// src/workflow/steps.ts
|
|
3938
4009
|
var combineHandlers = (...handlers) => {
|
|
3939
4010
|
const allHandlers = {};
|
|
@@ -4023,6 +4094,196 @@ var parallelStepSpecHandler = {
|
|
|
4023
4094
|
};
|
|
4024
4095
|
}
|
|
4025
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
|
+
};
|
|
4026
4287
|
var customStepSpecHandler = {
|
|
4027
4288
|
type: "custom",
|
|
4028
4289
|
handler(step2) {
|
|
@@ -4037,50 +4298,6 @@ var customStepSpecHandler = {
|
|
|
4037
4298
|
function step(id, run2) {
|
|
4038
4299
|
return { id, type: "custom", run: run2 };
|
|
4039
4300
|
}
|
|
4040
|
-
|
|
4041
|
-
// src/workflow/workflow.ts
|
|
4042
|
-
var run = async (workflow, context, handler15, input) => {
|
|
4043
|
-
const rootStep = handler15(workflow.step, handler15);
|
|
4044
|
-
const result = await runStep(rootStep, input, context, void 0, {});
|
|
4045
|
-
switch (result.type) {
|
|
4046
|
-
case "paused":
|
|
4047
|
-
return {
|
|
4048
|
-
type: "paused",
|
|
4049
|
-
state: result.state
|
|
4050
|
-
};
|
|
4051
|
-
case "error":
|
|
4052
|
-
return {
|
|
4053
|
-
type: "error",
|
|
4054
|
-
error: result.error
|
|
4055
|
-
};
|
|
4056
|
-
case "success":
|
|
4057
|
-
return {
|
|
4058
|
-
type: "success",
|
|
4059
|
-
output: result.output
|
|
4060
|
-
};
|
|
4061
|
-
}
|
|
4062
|
-
};
|
|
4063
|
-
var resume = async (workflow, context, handler15, state, input) => {
|
|
4064
|
-
const rootStep = handler15(workflow.step, handler15);
|
|
4065
|
-
const result = await runStep(rootStep, input, context, state, {});
|
|
4066
|
-
switch (result.type) {
|
|
4067
|
-
case "paused":
|
|
4068
|
-
return {
|
|
4069
|
-
type: "paused",
|
|
4070
|
-
state: result.state
|
|
4071
|
-
};
|
|
4072
|
-
case "error":
|
|
4073
|
-
return {
|
|
4074
|
-
type: "error",
|
|
4075
|
-
error: result.error
|
|
4076
|
-
};
|
|
4077
|
-
case "success":
|
|
4078
|
-
return {
|
|
4079
|
-
type: "success",
|
|
4080
|
-
output: result.output
|
|
4081
|
-
};
|
|
4082
|
-
}
|
|
4083
|
-
};
|
|
4084
4301
|
export {
|
|
4085
4302
|
AgentBase,
|
|
4086
4303
|
AnalyzerAgent,
|
|
@@ -4101,6 +4318,7 @@ export {
|
|
|
4101
4318
|
architectAgentInfo,
|
|
4102
4319
|
askFollowupQuestion_default as askFollowupQuestion,
|
|
4103
4320
|
attemptCompletion_default as attemptCompletion,
|
|
4321
|
+
branchStepSpecHandler,
|
|
4104
4322
|
builder,
|
|
4105
4323
|
capabilities,
|
|
4106
4324
|
codeFixerAgentInfo,
|
|
@@ -4123,6 +4341,7 @@ export {
|
|
|
4123
4341
|
getAvailableTools,
|
|
4124
4342
|
handOver_default as handOver,
|
|
4125
4343
|
listFiles_default as listFiles,
|
|
4344
|
+
loopStepSpecHandler,
|
|
4126
4345
|
makeAgentStepSpecHandler,
|
|
4127
4346
|
makeAgentTool,
|
|
4128
4347
|
makeMultiAgentTool,
|
|
@@ -4144,5 +4363,6 @@ export {
|
|
|
4144
4363
|
step,
|
|
4145
4364
|
systemInformation,
|
|
4146
4365
|
toolUsePrompt,
|
|
4366
|
+
workflowStepSpecHandler,
|
|
4147
4367
|
writeToFile_default as writeToFile
|
|
4148
4368
|
};
|