@cogitator-ai/workflows 0.11.10 → 0.12.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/README.md +7 -1
- package/dist/executor.d.ts +5 -2
- package/dist/executor.d.ts.map +1 -1
- package/dist/executor.js +18 -3
- package/dist/executor.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/manager/workflow-manager.d.ts.map +1 -1
- package/dist/manager/workflow-manager.js +1 -0
- package/dist/manager/workflow-manager.js.map +1 -1
- package/dist/nodes/adapters.d.ts +4 -2
- package/dist/nodes/adapters.d.ts.map +1 -1
- package/dist/nodes/adapters.js +6 -4
- package/dist/nodes/adapters.js.map +1 -1
- package/dist/nodes/agent.d.ts +16 -1
- package/dist/nodes/agent.d.ts.map +1 -1
- package/dist/nodes/agent.js +43 -2
- package/dist/nodes/agent.js.map +1 -1
- package/dist/nodes/approvals.d.ts +31 -0
- package/dist/nodes/approvals.d.ts.map +1 -0
- package/dist/nodes/approvals.js +51 -0
- package/dist/nodes/approvals.js.map +1 -0
- package/dist/nodes/base.d.ts +5 -0
- package/dist/nodes/base.d.ts.map +1 -1
- package/dist/nodes/tool.d.ts +13 -0
- package/dist/nodes/tool.d.ts.map +1 -1
- package/dist/nodes/tool.js +23 -9
- package/dist/nodes/tool.js.map +1 -1
- package/dist/patterns/index.d.ts +1 -1
- package/dist/patterns/index.d.ts.map +1 -1
- package/dist/patterns/index.js +1 -1
- package/dist/patterns/index.js.map +1 -1
- package/dist/patterns/map-reduce.d.ts +54 -6
- package/dist/patterns/map-reduce.d.ts.map +1 -1
- package/dist/patterns/map-reduce.js +173 -112
- package/dist/patterns/map-reduce.js.map +1 -1
- package/package.json +5 -4
package/dist/nodes/agent.d.ts
CHANGED
|
@@ -1,9 +1,24 @@
|
|
|
1
1
|
import type { WorkflowNode, WorkflowState } from '@cogitator-ai/types';
|
|
2
|
-
import type
|
|
2
|
+
import { type Agent, type RunOptions, type RunResult } from '@cogitator-ai/core';
|
|
3
|
+
import { type NodeApprovalOptions } from './approvals.js';
|
|
3
4
|
export interface AgentNodeOptions<S = WorkflowState> {
|
|
4
5
|
stateMapper?: (result: RunResult) => Partial<S>;
|
|
5
6
|
inputMapper?: (state: S, input?: unknown) => string;
|
|
6
7
|
runOptions?: Partial<RunOptions>;
|
|
8
|
+
/**
|
|
9
|
+
* Tool calls that need approval while the node runs. By default (and with an object) each
|
|
10
|
+
* waiting call becomes a request in the workflow's approval store, and the run goes on once it
|
|
11
|
+
* is answered. `false`, or no approval store at all, fails the node with an
|
|
12
|
+
* `AgentRunPausedError` that holds the checkpoint to resume the run with. An `onApproval` in
|
|
13
|
+
* `runOptions` decides calls before they ever pause.
|
|
14
|
+
*/
|
|
15
|
+
approvals?: NodeApprovalOptions | false;
|
|
7
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Run an agent as a workflow step. Its answer is the node's output; `stateMapper` copies what
|
|
19
|
+
* the next steps need into the state. A run that pauses for tool approvals never passes as an
|
|
20
|
+
* answer: the node asks for the calls through the approval store and waits, or fails with an
|
|
21
|
+
* `AgentRunPausedError` (see `approvals`).
|
|
22
|
+
*/
|
|
8
23
|
export declare function agentNode<S extends WorkflowState = WorkflowState>(agent: Agent, options?: AgentNodeOptions<S>): WorkflowNode<S>;
|
|
9
24
|
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/nodes/agent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/nodes/agent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKV,YAAY,EACZ,aAAa,EACd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAGL,KAAK,KAAK,EACV,KAAK,UAAU,EACf,KAAK,SAAS,EACf,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAmB,KAAK,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAExE,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,aAAa;IACjD,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;IAChD,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,CAAC,EAAE,OAAO,KAAK,MAAM,CAAC;IACpD,UAAU,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IACjC;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,mBAAmB,GAAG,KAAK,CAAC;CACzC;AAeD;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,aAAa,GAAG,aAAa,EAC/D,KAAK,EAAE,KAAK,EACZ,OAAO,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAC5B,YAAY,CAAC,CAAC,CAAC,CA2EjB"}
|
package/dist/nodes/agent.js
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
import { AgentRunPausedError, isPausedRun, } from '@cogitator-ai/core';
|
|
2
|
+
import { askToolApproval } from './approvals.js';
|
|
3
|
+
function resumeOptions(runOptions) {
|
|
4
|
+
const { input: _input, images: _images, audio: _audio, context: _context, threadId: _threadId, threadAccess: _threadAccess, ...rest } = runOptions ?? {};
|
|
5
|
+
return rest;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Run an agent as a workflow step. Its answer is the node's output; `stateMapper` copies what
|
|
9
|
+
* the next steps need into the state. A run that pauses for tool approvals never passes as an
|
|
10
|
+
* answer: the node asks for the calls through the approval store and waits, or fails with an
|
|
11
|
+
* `AgentRunPausedError` (see `approvals`).
|
|
12
|
+
*/
|
|
1
13
|
export function agentNode(agent, options) {
|
|
2
14
|
return {
|
|
3
15
|
name: agent.name,
|
|
@@ -19,12 +31,37 @@ export function agentNode(agent, options) {
|
|
|
19
31
|
else {
|
|
20
32
|
input = JSON.stringify(ctx.state);
|
|
21
33
|
}
|
|
34
|
+
const where = `workflow "${ctx.workflowId}", node "${ctx.nodeId}"`;
|
|
35
|
+
const askApprovals = async (pending) => {
|
|
36
|
+
const answers = await Promise.all(pending.map(async (request) => {
|
|
37
|
+
const decision = await askToolApproval(request, agent.name, options?.approvals, extCtx);
|
|
38
|
+
return decision && [request.toolCallId, decision];
|
|
39
|
+
}));
|
|
40
|
+
const decisions = {};
|
|
41
|
+
for (const answer of answers) {
|
|
42
|
+
if (!answer)
|
|
43
|
+
return undefined;
|
|
44
|
+
decisions[answer[0]] = answer[1];
|
|
45
|
+
}
|
|
46
|
+
return decisions;
|
|
47
|
+
};
|
|
22
48
|
try {
|
|
23
|
-
|
|
49
|
+
let result = await extCtx.cogitator.run(agent, {
|
|
24
50
|
input,
|
|
25
51
|
...(extCtx.signal && { signal: extCtx.signal }),
|
|
26
52
|
...options?.runOptions,
|
|
27
53
|
});
|
|
54
|
+
while (isPausedRun(result)) {
|
|
55
|
+
const decisions = await askApprovals(result.pendingApprovals);
|
|
56
|
+
if (!decisions || !result.checkpoint) {
|
|
57
|
+
throw new AgentRunPausedError(result, agent.name, where);
|
|
58
|
+
}
|
|
59
|
+
result = await extCtx.cogitator.resume(agent, result.checkpoint, {
|
|
60
|
+
...(extCtx.signal && { signal: extCtx.signal }),
|
|
61
|
+
...resumeOptions(options?.runOptions),
|
|
62
|
+
decisions,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
28
65
|
const stateUpdate = options?.stateMapper?.(result);
|
|
29
66
|
return {
|
|
30
67
|
state: stateUpdate,
|
|
@@ -32,8 +69,12 @@ export function agentNode(agent, options) {
|
|
|
32
69
|
};
|
|
33
70
|
}
|
|
34
71
|
catch (error) {
|
|
72
|
+
if (error instanceof AgentRunPausedError)
|
|
73
|
+
throw error;
|
|
35
74
|
const message = error instanceof Error ? error.message : String(error);
|
|
36
|
-
throw new Error(`agentNode "${agent.name}" failed in
|
|
75
|
+
throw new Error(`agentNode "${agent.name}" failed in ${where}: ${message}`, {
|
|
76
|
+
cause: error,
|
|
77
|
+
});
|
|
37
78
|
}
|
|
38
79
|
},
|
|
39
80
|
};
|
package/dist/nodes/agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/nodes/agent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/nodes/agent.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,mBAAmB,EACnB,WAAW,GAIZ,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,eAAe,EAA4B,MAAM,aAAa,CAAC;AAgBxE,SAAS,aAAa,CAAC,UAA2C;IAChE,MAAM,EACJ,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,QAAQ,EACjB,QAAQ,EAAE,SAAS,EACnB,YAAY,EAAE,aAAa,EAC3B,GAAG,IAAI,EACR,GAAG,UAAU,IAAI,EAAE,CAAC;IACrB,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CACvB,KAAY,EACZ,OAA6B;IAE7B,OAAO;QACL,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,EAAE,EAAE,KAAK,EAAE,GAAG,EAA0B,EAAE;YACxC,MAAM,MAAM,GAAG,GAA6B,CAAC;YAE7C,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;gBACtB,MAAM,IAAI,KAAK,CACb,cAAc,KAAK,CAAC,IAAI,qDAAqD,CAC9E,CAAC;YACJ,CAAC;YAED,IAAI,KAAa,CAAC;YAClB,IAAI,OAAO,EAAE,WAAW,EAAE,CAAC;gBACzB,KAAK,GAAG,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,KAA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1E,KAAK,EAAE,KAAK;iBACb,CAAC,CAAC;YACL,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ApprovalNotifier, ApprovalStore, ToolApprovalDecision, ToolApprovalRequest, WorkflowState } from '@cogitator-ai/types';
|
|
2
|
+
import type { ExtendedNodeContext } from './base.js';
|
|
3
|
+
/**
|
|
4
|
+
* Where a node asks for the tool calls that need approval, and how. Each call becomes an
|
|
5
|
+
* approve/reject request in the workflow's human-in-the-loop approval store, like an
|
|
6
|
+
* `approvalNode`: an approved call runs, any other is declined with the approver's comment.
|
|
7
|
+
*/
|
|
8
|
+
export interface NodeApprovalOptions {
|
|
9
|
+
/** Defaults to the run's `approvalStore` execute option */
|
|
10
|
+
approvalStore?: ApprovalStore;
|
|
11
|
+
/** Defaults to the run's `approvalNotifier` execute option */
|
|
12
|
+
approvalNotifier?: ApprovalNotifier;
|
|
13
|
+
assignee?: string;
|
|
14
|
+
assigneeGroup?: string[];
|
|
15
|
+
/** How long a request waits for an answer, in ms */
|
|
16
|
+
timeout?: number;
|
|
17
|
+
/** What a request that timed out decides: `'approve'`, else the call is declined */
|
|
18
|
+
timeoutAction?: 'approve' | 'reject' | 'fail' | 'escalate';
|
|
19
|
+
/** Who gets the request when it times out with `timeoutAction: 'escalate'` */
|
|
20
|
+
escalateTo?: string;
|
|
21
|
+
priority?: 'low' | 'normal' | 'high' | 'urgent';
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Asks the approval store whether `caller` may make the tool call in `request`, and waits for the
|
|
25
|
+
* answer. The request is identified by the call's id, so two identical calls of one turn get a
|
|
26
|
+
* request each, and a node that runs again after a restart finds the request of the same call.
|
|
27
|
+
* Undefined when there is nobody to ask: `approvals` is `false`, or neither it nor the run has
|
|
28
|
+
* an approval store.
|
|
29
|
+
*/
|
|
30
|
+
export declare function askToolApproval<S extends WorkflowState>(request: ToolApprovalRequest, caller: string, approvals: NodeApprovalOptions | false | undefined, ctx: ExtendedNodeContext<S>): Promise<ToolApprovalDecision | undefined>;
|
|
31
|
+
//# sourceMappingURL=approvals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"approvals.d.ts","sourceRoot":"","sources":["../../src/nodes/approvals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,oBAAoB,EACpB,mBAAmB,EACnB,aAAa,EACd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AAGlD;;;;GAIG;AACH,MAAM,WAAW,mBAAmB;IAClC,2DAA2D;IAC3D,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,8DAA8D;IAC9D,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,oDAAoD;IACpD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,oFAAoF;IACpF,aAAa,CAAC,EAAE,SAAS,GAAG,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAC;IAC3D,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,QAAQ,CAAC;CACjD;AAQD;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,CAAC,SAAS,aAAa,EAC3D,OAAO,EAAE,mBAAmB,EAC5B,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,mBAAmB,GAAG,KAAK,GAAG,SAAS,EAClD,GAAG,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAC1B,OAAO,CAAC,oBAAoB,GAAG,SAAS,CAAC,CAqC3C"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { executeHumanNode } from '../human/human-node.js';
|
|
2
|
+
function declineReason(result) {
|
|
3
|
+
if (result.timedOut)
|
|
4
|
+
return 'Nobody approved the call in time';
|
|
5
|
+
if (result.withdrawn)
|
|
6
|
+
return 'The approval request was withdrawn';
|
|
7
|
+
return result.response.comment ?? 'Declined in the workflow';
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Asks the approval store whether `caller` may make the tool call in `request`, and waits for the
|
|
11
|
+
* answer. The request is identified by the call's id, so two identical calls of one turn get a
|
|
12
|
+
* request each, and a node that runs again after a restart finds the request of the same call.
|
|
13
|
+
* Undefined when there is nobody to ask: `approvals` is `false`, or neither it nor the run has
|
|
14
|
+
* an approval store.
|
|
15
|
+
*/
|
|
16
|
+
export async function askToolApproval(request, caller, approvals, ctx) {
|
|
17
|
+
if (approvals === false)
|
|
18
|
+
return undefined;
|
|
19
|
+
const approvalStore = approvals?.approvalStore ?? ctx.approvalStore;
|
|
20
|
+
if (!approvalStore)
|
|
21
|
+
return undefined;
|
|
22
|
+
const result = await executeHumanNode({
|
|
23
|
+
caller,
|
|
24
|
+
toolCallId: request.toolCallId,
|
|
25
|
+
tool: request.toolName,
|
|
26
|
+
arguments: request.arguments,
|
|
27
|
+
...(request.sideEffects && { sideEffects: request.sideEffects }),
|
|
28
|
+
}, {
|
|
29
|
+
approval: {
|
|
30
|
+
type: 'approve-reject',
|
|
31
|
+
title: `Allow ${caller} to call ${request.toolName}?`,
|
|
32
|
+
description: `${request.description}\n\nArguments: ${JSON.stringify(request.arguments)}`,
|
|
33
|
+
assignee: approvals?.assignee,
|
|
34
|
+
assigneeGroup: approvals?.assigneeGroup,
|
|
35
|
+
timeout: approvals?.timeout,
|
|
36
|
+
timeoutAction: approvals?.timeoutAction,
|
|
37
|
+
escalateTo: approvals?.escalateTo,
|
|
38
|
+
priority: approvals?.priority,
|
|
39
|
+
},
|
|
40
|
+
}, {
|
|
41
|
+
workflowId: ctx.workflowId,
|
|
42
|
+
runId: ctx.workflowId,
|
|
43
|
+
nodeId: `${ctx.nodeId}:approval`,
|
|
44
|
+
approvalStore,
|
|
45
|
+
approvalNotifier: approvals?.approvalNotifier ?? ctx.approvalNotifier,
|
|
46
|
+
signal: ctx.signal,
|
|
47
|
+
onApprovalRequired: ctx.onApprovalRequired,
|
|
48
|
+
});
|
|
49
|
+
return result.approved ? { approved: true } : { approved: false, reason: declineReason(result) };
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=approvals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"approvals.js","sourceRoot":"","sources":["../../src/nodes/approvals.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,gBAAgB,EAAwB,MAAM,qBAAqB,CAAC;AAuB7E,SAAS,aAAa,CAAC,MAAsC;IAC3D,IAAI,MAAM,CAAC,QAAQ;QAAE,OAAO,kCAAkC,CAAC;IAC/D,IAAI,MAAM,CAAC,SAAS;QAAE,OAAO,oCAAoC,CAAC;IAClE,OAAO,MAAM,CAAC,QAAQ,CAAC,OAAO,IAAI,0BAA0B,CAAC;AAC/D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,OAA4B,EAC5B,MAAc,EACd,SAAkD,EAClD,GAA2B;IAE3B,IAAI,SAAS,KAAK,KAAK;QAAE,OAAO,SAAS,CAAC;IAC1C,MAAM,aAAa,GAAG,SAAS,EAAE,aAAa,IAAI,GAAG,CAAC,aAAa,CAAC;IACpE,IAAI,CAAC,aAAa;QAAE,OAAO,SAAS,CAAC;IAErC,MAAM,MAAM,GAAG,MAAM,gBAAgB,CACnC;QACE,MAAM;QACN,UAAU,EAAE,OAAO,CAAC,UAAU;QAC9B,IAAI,EAAE,OAAO,CAAC,QAAQ;QACtB,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,GAAG,CAAC,OAAO,CAAC,WAAW,IAAI,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC;KACjE,EACD;QACE,QAAQ,EAAE;YACR,IAAI,EAAE,gBAAgB;YACtB,KAAK,EAAE,SAAS,MAAM,YAAY,OAAO,CAAC,QAAQ,GAAG;YACrD,WAAW,EAAE,GAAG,OAAO,CAAC,WAAW,kBAAkB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE;YACxF,QAAQ,EAAE,SAAS,EAAE,QAAQ;YAC7B,aAAa,EAAE,SAAS,EAAE,aAAa;YACvC,OAAO,EAAE,SAAS,EAAE,OAAO;YAC3B,aAAa,EAAE,SAAS,EAAE,aAAa;YACvC,UAAU,EAAE,SAAS,EAAE,UAAU;YACjC,QAAQ,EAAE,SAAS,EAAE,QAAQ;SAC9B;KACF,EACD;QACE,UAAU,EAAE,GAAG,CAAC,UAAU;QAC1B,KAAK,EAAE,GAAG,CAAC,UAAU;QACrB,MAAM,EAAE,GAAG,GAAG,CAAC,MAAM,WAAW;QAChC,aAAa;QACb,gBAAgB,EAAE,SAAS,EAAE,gBAAgB,IAAI,GAAG,CAAC,gBAAgB;QACrE,MAAM,EAAE,GAAG,CAAC,MAAM;QAClB,kBAAkB,EAAE,GAAG,CAAC,kBAAkB;KAC3C,CACF,CAAC;IACF,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;AACnG,CAAC"}
|
package/dist/nodes/base.d.ts
CHANGED
|
@@ -7,6 +7,11 @@ export interface ExtendedNodeContext<S = WorkflowState> extends NodeContext<S> {
|
|
|
7
7
|
signal?: AbortSignal;
|
|
8
8
|
/** Subworkflow nesting depth (0 for top-level runs) */
|
|
9
9
|
depth?: number;
|
|
10
|
+
/**
|
|
11
|
+
* How many times the node has run in this workflow run, this time included. A resumed run
|
|
12
|
+
* counts on from its checkpoint, so it names the same visit as before the restart
|
|
13
|
+
*/
|
|
14
|
+
visit?: number;
|
|
10
15
|
/** Run-level defaults for human-in-the-loop and timer nodes */
|
|
11
16
|
approvalStore?: ApprovalStore;
|
|
12
17
|
approvalNotifier?: ApprovalNotifier;
|
package/dist/nodes/base.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/nodes/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EACV,WAAW,EACX,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,UAAU,EACV,UAAU,EACX,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEpD,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC;AAElE,MAAM,WAAW,mBAAmB,CAAC,CAAC,GAAG,aAAa,CAAE,SAAQ,WAAW,CAAC,CAAC,CAAC;IAC5E,SAAS,EAAE,SAAS,CAAC;IACrB,sEAAsE;IACtE,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,uDAAuD;IACvD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,+DAA+D;IAC/D,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,6DAA6D;IAC7D,kBAAkB,CAAC,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;IACxD,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,CAAC;CAChD"}
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/nodes/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EACV,WAAW,EACX,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,UAAU,EACV,UAAU,EACX,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEpD,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC;AAElE,MAAM,WAAW,mBAAmB,CAAC,CAAC,GAAG,aAAa,CAAE,SAAQ,WAAW,CAAC,CAAC,CAAC;IAC5E,SAAS,EAAE,SAAS,CAAC;IACrB,sEAAsE;IACtE,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,uDAAuD;IACvD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,+DAA+D;IAC/D,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,6DAA6D;IAC7D,kBAAkB,CAAC,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;IACxD,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,CAAC;CAChD"}
|
package/dist/nodes/tool.d.ts
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import type { WorkflowNode, WorkflowState, Tool } from '@cogitator-ai/types';
|
|
2
|
+
import { type NodeApprovalOptions } from './approvals.js';
|
|
2
3
|
export interface ToolNodeOptions<S = WorkflowState, TArgs = unknown> {
|
|
3
4
|
argsMapper: (state: S, input?: unknown) => TArgs;
|
|
4
5
|
stateMapper?: (result: unknown) => Partial<S>;
|
|
5
6
|
signal?: AbortSignal;
|
|
7
|
+
/**
|
|
8
|
+
* Where a call of a tool that needs approval is asked. By default (and with an object) it
|
|
9
|
+
* becomes a request in the workflow's approval store and the tool runs once it is approved.
|
|
10
|
+
* `false`, or no approval store at all, fails the node without running the tool.
|
|
11
|
+
*/
|
|
12
|
+
approvals?: NodeApprovalOptions | false;
|
|
6
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Run one tool as a workflow step, the way an agent run calls it: through the node context's
|
|
16
|
+
* `cogitator.invokeTool`, so the mapped arguments are validated against the tool's schema, a call
|
|
17
|
+
* that needs approval is asked for (see `approvals`), the guardrails apply, a sandboxed tool runs
|
|
18
|
+
* in the sandbox and `tool.timeout` is kept. A call that fails, or is declined, fails the node.
|
|
19
|
+
*/
|
|
7
20
|
export declare function toolNode<S extends WorkflowState = WorkflowState, TArgs = unknown>(tool: Tool<TArgs, unknown>, options: ToolNodeOptions<S, TArgs>): WorkflowNode<S>;
|
|
8
21
|
//# sourceMappingURL=tool.d.ts.map
|
package/dist/nodes/tool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/nodes/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/nodes/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAc,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAEzF,OAAO,EAAmB,KAAK,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAExE,MAAM,WAAW,eAAe,CAAC,CAAC,GAAG,aAAa,EAAE,KAAK,GAAG,OAAO;IACjE,UAAU,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,CAAC,EAAE,OAAO,KAAK,KAAK,CAAC;IACjD,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;IAC9C,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB;;;;OAIG;IACH,SAAS,CAAC,EAAE,mBAAmB,GAAG,KAAK,CAAC;CACzC;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,aAAa,GAAG,aAAa,EAAE,KAAK,GAAG,OAAO,EAC/E,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,OAAO,CAAC,EAC1B,OAAO,EAAE,eAAe,CAAC,CAAC,EAAE,KAAK,CAAC,GACjC,YAAY,CAAC,CAAC,CAAC,CAwCjB"}
|
package/dist/nodes/tool.js
CHANGED
|
@@ -1,20 +1,34 @@
|
|
|
1
|
+
import { askToolApproval } from './approvals.js';
|
|
2
|
+
/**
|
|
3
|
+
* Run one tool as a workflow step, the way an agent run calls it: through the node context's
|
|
4
|
+
* `cogitator.invokeTool`, so the mapped arguments are validated against the tool's schema, a call
|
|
5
|
+
* that needs approval is asked for (see `approvals`), the guardrails apply, a sandboxed tool runs
|
|
6
|
+
* in the sandbox and `tool.timeout` is kept. A call that fails, or is declined, fails the node.
|
|
7
|
+
*/
|
|
1
8
|
export function toolNode(tool, options) {
|
|
2
9
|
return {
|
|
3
10
|
name: tool.name,
|
|
4
11
|
fn: async (ctx) => {
|
|
12
|
+
const extCtx = ctx;
|
|
13
|
+
const cogitator = extCtx.cogitator;
|
|
14
|
+
if (!cogitator) {
|
|
15
|
+
throw new Error(`toolNode "${tool.name}" requires a Cogitator instance in the node context`);
|
|
16
|
+
}
|
|
5
17
|
const args = options.argsMapper(ctx.state, ctx.input);
|
|
6
|
-
const
|
|
18
|
+
const signal = options.signal ?? extCtx.signal;
|
|
19
|
+
const invocation = await cogitator.invokeTool(tool, args, {
|
|
20
|
+
toolCallId: `${ctx.nodeId}:visit:${extCtx.visit ?? 1}`,
|
|
7
21
|
agentId: `workflow:${ctx.workflowId}:${ctx.nodeId}`,
|
|
8
22
|
runId: ctx.workflowId,
|
|
9
|
-
signal
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
23
|
+
...(signal && { signal }),
|
|
24
|
+
onApproval: async (request) => (await askToolApproval(request, `workflow node "${ctx.nodeId}"`, options.approvals, extCtx)) ?? 'pause',
|
|
25
|
+
});
|
|
26
|
+
if (invocation.error !== undefined) {
|
|
27
|
+
throw new Error(`toolNode "${tool.name}" failed in workflow "${ctx.workflowId}", node "${ctx.nodeId}": ${invocation.error}`);
|
|
28
|
+
}
|
|
15
29
|
return {
|
|
16
|
-
state:
|
|
17
|
-
output: result,
|
|
30
|
+
state: options.stateMapper?.(invocation.result),
|
|
31
|
+
output: invocation.result,
|
|
18
32
|
};
|
|
19
33
|
},
|
|
20
34
|
};
|
package/dist/nodes/tool.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool.js","sourceRoot":"","sources":["../../src/nodes/tool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"tool.js","sourceRoot":"","sources":["../../src/nodes/tool.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAA4B,MAAM,aAAa,CAAC;AAcxE;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CACtB,IAA0B,EAC1B,OAAkC;IAElC,OAAO;QACL,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,EAAE,EAAE,KAAK,EAAE,GAAG,EAA0B,EAAE;YACxC,MAAM,MAAM,GAAG,GAAmD,CAAC;YACnE,MAAM,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;YACnC,IAAI,CAAC,SAAS,EAAE,CAAC;gBACf,MAAM,IAAI,KAAK,CACb,aAAa,IAAI,CAAC,IAAI,qDAAqD,CAC5E,CAAC;YACJ,CAAC;YAED,MAAM,IAAI,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;YACtD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC;YAC/C,MAAM,UAAU,GAAG,MAAM,SAAS,CAAC,UAAU,CAAC,IAAY,EAAE,IAAI,EAAE;gBAChE,UAAU,EAAE,GAAG,GAAG,CAAC,MAAM,UAAU,MAAM,CAAC,KAAK,IAAI,CAAC,EAAE;gBACtD,OAAO,EAAE,YAAY,GAAG,CAAC,UAAU,IAAI,GAAG,CAAC,MAAM,EAAE;gBACnD,KAAK,EAAE,GAAG,CAAC,UAAU;gBACrB,GAAG,CAAC,MAAM,IAAI,EAAE,MAAM,EAAE,CAAC;gBACzB,UAAU,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,CAC5B,CAAC,MAAM,eAAe,CACpB,OAAO,EACP,kBAAkB,GAAG,CAAC,MAAM,GAAG,EAC/B,OAAO,CAAC,SAAS,EACjB,MAAgC,CACjC,CAAC,IAAI,OAAO;aAChB,CAAC,CAAC;YAEH,IAAI,UAAU,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;gBACnC,MAAM,IAAI,KAAK,CACb,aAAa,IAAI,CAAC,IAAI,yBAAyB,GAAG,CAAC,UAAU,YAAY,GAAG,CAAC,MAAM,MAAM,UAAU,CAAC,KAAK,EAAE,CAC5G,CAAC;YACJ,CAAC;YAED,OAAO;gBACL,KAAK,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC;gBAC/C,MAAM,EAAE,UAAU,CAAC,MAAM;aAC1B,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/patterns/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { type MapItemResult, type MapProgressEvent, type MapNodeConfig, type ReduceNodeConfig, type MapReduceResult, type MapReduceNodeConfig, executeMap, executeReduce, executeMapReduce, mapNode, reduceNode, mapReduceNode, parallelMap, sequentialMap, batchedMap, collect, sum, count, first, last, groupBy, partition, flatMap, stats, } from './map-reduce.js';
|
|
1
|
+
export { type MapItemResult, type MapProgressEvent, type MapNodeConfig, type ReduceNodeConfig, type MapReduceResult, type MapReduceNodeConfig, type MapItemContext, type MapExecutionOptions, MapItemSkippedError, MapItemTimeoutError, executeMap, executeReduce, executeMapReduce, mapNode, reduceNode, mapReduceNode, parallelMap, sequentialMap, batchedMap, collect, sum, count, first, last, groupBy, partition, flatMap, stats, } from './map-reduce.js';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/patterns/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,OAAO,EACP,UAAU,EACV,aAAa,EACb,WAAW,EACX,aAAa,EACb,UAAU,EACV,OAAO,EACP,GAAG,EACH,KAAK,EACL,KAAK,EACL,IAAI,EACJ,OAAO,EACP,SAAS,EACT,OAAO,EACP,KAAK,GACN,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/patterns/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,mBAAmB,EACxB,mBAAmB,EACnB,mBAAmB,EACnB,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,OAAO,EACP,UAAU,EACV,aAAa,EACb,WAAW,EACX,aAAa,EACb,UAAU,EACV,OAAO,EACP,GAAG,EACH,KAAK,EACL,KAAK,EACL,IAAI,EACJ,OAAO,EACP,SAAS,EACT,OAAO,EACP,KAAK,GACN,MAAM,cAAc,CAAC"}
|
package/dist/patterns/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { executeMap, executeReduce, executeMapReduce, mapNode, reduceNode, mapReduceNode, parallelMap, sequentialMap, batchedMap, collect, sum, count, first, last, groupBy, partition, flatMap, stats, } from './map-reduce.js';
|
|
1
|
+
export { MapItemSkippedError, MapItemTimeoutError, executeMap, executeReduce, executeMapReduce, mapNode, reduceNode, mapReduceNode, parallelMap, sequentialMap, batchedMap, collect, sum, count, first, last, groupBy, partition, flatMap, stats, } from './map-reduce.js';
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/patterns/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/patterns/index.ts"],"names":[],"mappings":"AAAA,OAAO,EASL,mBAAmB,EACnB,mBAAmB,EACnB,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,OAAO,EACP,UAAU,EACV,aAAa,EACb,WAAW,EACX,aAAa,EACb,UAAU,EACV,OAAO,EACP,GAAG,EACH,KAAK,EACL,KAAK,EACL,IAAI,EACJ,OAAO,EACP,SAAS,EACT,OAAO,EACP,KAAK,GACN,MAAM,cAAc,CAAC"}
|
|
@@ -15,15 +15,34 @@ export interface MapProgressEvent<T> {
|
|
|
15
15
|
pending: number;
|
|
16
16
|
running: number;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* What a mapper gets besides the item: the signal to stop on and which attempt this is.
|
|
20
|
+
*/
|
|
21
|
+
export interface MapItemContext {
|
|
22
|
+
/**
|
|
23
|
+
* Aborted when this attempt times out, another item fails the map (without
|
|
24
|
+
* `continueOnError`), or the workflow run is cancelled or paused. Pass it on to agent runs,
|
|
25
|
+
* fetches and tools so their work stops too
|
|
26
|
+
*/
|
|
27
|
+
signal: AbortSignal;
|
|
28
|
+
/** 1 for the first attempt, 2 for the first retry, ... */
|
|
29
|
+
attempt: number;
|
|
30
|
+
}
|
|
18
31
|
export interface MapNodeConfig<S, T> {
|
|
19
32
|
name: string;
|
|
20
33
|
items: (state: S) => unknown[];
|
|
21
|
-
mapper: (item: unknown, index: number, state: S) => Promise<T> | T;
|
|
34
|
+
mapper: (item: unknown, index: number, state: S, ctx: MapItemContext) => Promise<T> | T;
|
|
22
35
|
concurrency?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Keep mapping when an item fails: failed items appear in the results with `success: false`.
|
|
38
|
+
* Without it the first failure stops the map: no further item starts, items in flight are
|
|
39
|
+
* aborted, and the map rejects with that failure
|
|
40
|
+
*/
|
|
23
41
|
continueOnError?: boolean;
|
|
24
42
|
onProgress?: (progress: MapProgressEvent<T>) => void;
|
|
25
43
|
filter?: (item: unknown, index: number, state: S) => boolean;
|
|
26
44
|
transform?: (item: unknown, index: number, state: S) => unknown;
|
|
45
|
+
/** Time limit of one attempt in ms: a late attempt is aborted (`ctx.signal`) and fails */
|
|
27
46
|
timeout?: number;
|
|
28
47
|
retry?: {
|
|
29
48
|
maxAttempts: number;
|
|
@@ -31,6 +50,25 @@ export interface MapNodeConfig<S, T> {
|
|
|
31
50
|
backoff?: 'linear' | 'exponential';
|
|
32
51
|
};
|
|
33
52
|
}
|
|
53
|
+
/** Options of a map run. */
|
|
54
|
+
export interface MapExecutionOptions {
|
|
55
|
+
/** Cancels the map: no further item starts and the items in flight are aborted */
|
|
56
|
+
signal?: AbortSignal;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Result error of an item that never ran because the map stopped first: another item failed
|
|
60
|
+
* or the run was cancelled. `cause` is what stopped the map.
|
|
61
|
+
*/
|
|
62
|
+
export declare class MapItemSkippedError extends Error {
|
|
63
|
+
readonly index: number;
|
|
64
|
+
constructor(index: number, cause: Error);
|
|
65
|
+
}
|
|
66
|
+
/** An attempt of an item that ran past the map's `timeout`. */
|
|
67
|
+
export declare class MapItemTimeoutError extends Error {
|
|
68
|
+
readonly index: number;
|
|
69
|
+
readonly timeoutMs: number;
|
|
70
|
+
constructor(index: number, timeoutMs: number);
|
|
71
|
+
}
|
|
34
72
|
export interface ReduceNodeConfig<S, T, R> {
|
|
35
73
|
name: string;
|
|
36
74
|
initial: R | ((state: S) => R);
|
|
@@ -55,23 +93,33 @@ export interface MapReduceNodeConfig<S, T, R> {
|
|
|
55
93
|
map: Omit<MapNodeConfig<S, T>, 'name'>;
|
|
56
94
|
reduce: Omit<ReduceNodeConfig<S, T, R>, 'name'>;
|
|
57
95
|
}
|
|
58
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Map the items of `state` with `config.mapper`, at most `concurrency` at a time. Without
|
|
98
|
+
* `continueOnError` the first failed item stops the map: no further item starts, the items in
|
|
99
|
+
* flight are aborted, and the map rejects with that item's error. Aborting `options.signal`
|
|
100
|
+
* stops it the same way. The rejection carries `partialResults`, one entry per item: what it
|
|
101
|
+
* returned, why it failed, or a `MapItemSkippedError` for an item that never ran.
|
|
102
|
+
*/
|
|
103
|
+
export declare function executeMap<S, T>(state: S, config: MapNodeConfig<S, T>, options?: MapExecutionOptions): Promise<MapItemResult<T>[]>;
|
|
59
104
|
export declare function executeReduce<S, T, R>(results: MapItemResult<T>[], state: S, config: ReduceNodeConfig<S, T, R>): R;
|
|
60
|
-
export declare function executeMapReduce<S, T, R>(state: S, config: MapReduceNodeConfig<S, T, R
|
|
105
|
+
export declare function executeMapReduce<S, T, R>(state: S, config: MapReduceNodeConfig<S, T, R>, options?: MapExecutionOptions): Promise<MapReduceResult<T, R>>;
|
|
61
106
|
export declare function mapNode<S, T>(name: string, config: Omit<MapNodeConfig<S, T>, 'name'>): MapNodeConfig<S, T>;
|
|
62
107
|
export declare function reduceNode<S, T, R>(name: string, config: Omit<ReduceNodeConfig<S, T, R>, 'name'>): ReduceNodeConfig<S, T, R>;
|
|
63
108
|
export declare function mapReduceNode<S, T, R>(name: string, config: Omit<MapReduceNodeConfig<S, T, R>, 'name'>): MapReduceNodeConfig<S, T, R>;
|
|
64
|
-
export declare function parallelMap<S, T>(state: S, items: (state: S) => unknown[], mapper: (item: unknown, index: number, state: S) => Promise<T> | T, options?: {
|
|
109
|
+
export declare function parallelMap<S, T>(state: S, items: (state: S) => unknown[], mapper: (item: unknown, index: number, state: S, ctx: MapItemContext) => Promise<T> | T, options?: {
|
|
65
110
|
continueOnError?: boolean;
|
|
66
111
|
onProgress?: (progress: MapProgressEvent<T>) => void;
|
|
112
|
+
signal?: AbortSignal;
|
|
67
113
|
}): Promise<MapItemResult<T>[]>;
|
|
68
|
-
export declare function sequentialMap<S, T>(state: S, items: (state: S) => unknown[], mapper: (item: unknown, index: number, state: S) => Promise<T> | T, options?: {
|
|
114
|
+
export declare function sequentialMap<S, T>(state: S, items: (state: S) => unknown[], mapper: (item: unknown, index: number, state: S, ctx: MapItemContext) => Promise<T> | T, options?: {
|
|
69
115
|
continueOnError?: boolean;
|
|
70
116
|
onProgress?: (progress: MapProgressEvent<T>) => void;
|
|
117
|
+
signal?: AbortSignal;
|
|
71
118
|
}): Promise<MapItemResult<T>[]>;
|
|
72
|
-
export declare function batchedMap<S, T>(state: S, items: (state: S) => unknown[], mapper: (item: unknown, index: number, state: S) => Promise<T> | T, batchSize: number, options?: {
|
|
119
|
+
export declare function batchedMap<S, T>(state: S, items: (state: S) => unknown[], mapper: (item: unknown, index: number, state: S, ctx: MapItemContext) => Promise<T> | T, batchSize: number, options?: {
|
|
73
120
|
continueOnError?: boolean;
|
|
74
121
|
onProgress?: (progress: MapProgressEvent<T>) => void;
|
|
122
|
+
signal?: AbortSignal;
|
|
75
123
|
}): Promise<MapItemResult<T>[]>;
|
|
76
124
|
export declare function collect<T>(): Omit<ReduceNodeConfig<unknown, T, T[]>, 'name'>;
|
|
77
125
|
export declare function sum(): Omit<ReduceNodeConfig<unknown, number, number>, 'name'>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map-reduce.d.ts","sourceRoot":"","sources":["../../src/patterns/map-reduce.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,CAAC,CAAC;IACV,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CA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