-
Notifications
You must be signed in to change notification settings - Fork 12
/
teamwork.ts
32 lines (30 loc) · 949 Bytes
/
teamwork.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { iterate } from './iterate.js'
import { rootState } from './state.js'
import { WorkflowState } from './state.js'
import { Workflow } from './workflow.js'
/**
* Teamwork runs given workflow and continues iterating over the workflow until it finishes.
* If you handle running tools manually, you can set runTools to false.
*/
export async function teamwork(
workflow: Workflow,
state: WorkflowState = rootState(workflow),
runTools: boolean = true
): Promise<WorkflowState> {
if (state.status === 'finished') {
return state
}
if (runTools === false && hasPausedStatus(state)) {
return state
}
return teamwork(workflow, await iterate(workflow, state), runTools)
}
/**
* Recursively checks if any state or nested state has a 'paused' status
*/
export const hasPausedStatus = (state: WorkflowState): boolean => {
if (state.status === 'paused') {
return true
}
return state.children.some(hasPausedStatus)
}