@usestratus/sdk 0.15.0 → 1.1.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 +0 -1
- package/dist/azure/responses-model.d.ts.map +1 -1
- package/dist/azure/responses-model.js +59 -9
- package/dist/azure/responses-model.js.map +1 -1
- package/dist/core/builtin-tools.d.ts +1 -0
- package/dist/core/builtin-tools.d.ts.map +1 -1
- package/dist/core/builtin-tools.js.map +1 -1
- package/dist/core/index.d.ts +9 -6
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +5 -3
- package/dist/core/index.js.map +1 -1
- package/dist/core/mcp-client.d.ts +57 -0
- package/dist/core/mcp-client.d.ts.map +1 -0
- package/dist/core/mcp-client.js +181 -0
- package/dist/core/mcp-client.js.map +1 -0
- package/dist/core/memory-store.d.ts +9 -0
- package/dist/core/memory-store.d.ts.map +1 -0
- package/dist/core/memory-store.js +16 -0
- package/dist/core/memory-store.js.map +1 -0
- package/dist/core/model.d.ts +15 -0
- package/dist/core/model.d.ts.map +1 -1
- package/dist/core/result.d.ts +24 -0
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +18 -0
- package/dist/core/result.js.map +1 -1
- package/dist/core/run.d.ts +12 -2
- package/dist/core/run.d.ts.map +1 -1
- package/dist/core/run.js +267 -13
- package/dist/core/run.js.map +1 -1
- package/dist/core/session.d.ts +32 -0
- package/dist/core/session.d.ts.map +1 -1
- package/dist/core/session.js +37 -2
- package/dist/core/session.js.map +1 -1
- package/dist/core/subagent.d.ts.map +1 -1
- package/dist/core/subagent.js +26 -2
- package/dist/core/subagent.js.map +1 -1
- package/dist/core/tool.d.ts +18 -0
- package/dist/core/tool.d.ts.map +1 -1
- package/dist/core/tool.js +3 -1
- package/dist/core/tool.js.map +1 -1
- package/dist/core/types.d.ts +19 -1
- package/dist/core/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-store.js","sourceRoot":"","sources":["../../src/core/memory-store.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,kBAAkB;IACtB,KAAK,GAAG,IAAI,GAAG,EAA2B,CAAC;IAEnD,KAAK,CAAC,IAAI,CAAC,SAAiB,EAAE,QAAyB;QACtD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IACrC,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,SAAiB;QAC3B,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAClC,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,SAAiB;QAC7B,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,IAAI;QACT,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IACtC,CAAC;CACD"}
|
package/dist/core/model.d.ts
CHANGED
|
@@ -21,6 +21,10 @@ export interface ModelResponse {
|
|
|
21
21
|
usage?: UsageInfo;
|
|
22
22
|
finishReason?: FinishReason;
|
|
23
23
|
responseId?: string;
|
|
24
|
+
incompleteDetails?: {
|
|
25
|
+
reason?: string;
|
|
26
|
+
};
|
|
27
|
+
outputItems?: Record<string, unknown>[];
|
|
24
28
|
}
|
|
25
29
|
export type StreamEvent = {
|
|
26
30
|
type: "content_delta";
|
|
@@ -42,6 +46,17 @@ export type StreamEvent = {
|
|
|
42
46
|
type: "hosted_tool_call";
|
|
43
47
|
toolType: string;
|
|
44
48
|
status: "in_progress" | "completed" | "searching" | "generating" | "interpreting";
|
|
49
|
+
} | {
|
|
50
|
+
type: "subagent_start";
|
|
51
|
+
agentName: string;
|
|
52
|
+
} | {
|
|
53
|
+
type: "subagent_delta";
|
|
54
|
+
agentName: string;
|
|
55
|
+
content: string;
|
|
56
|
+
} | {
|
|
57
|
+
type: "subagent_end";
|
|
58
|
+
agentName: string;
|
|
59
|
+
result: string;
|
|
45
60
|
} | {
|
|
46
61
|
type: "done";
|
|
47
62
|
response: ModelResponse;
|
package/dist/core/model.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/core/model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,WAAW,EACX,oBAAoB,EACpB,aAAa,EACb,cAAc,EACd,QAAQ,EACR,cAAc,EACd,MAAM,SAAS,CAAC;AAEjB,MAAM,WAAW,YAAY;IAC5B,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,KAAK,CAAC,EAAE,CAAC,cAAc,GAAG,oBAAoB,CAAC,EAAE,CAAC;IAClD,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,cAAc,CAAC,EAAE,cAAc,CAAC;IAChC,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,SAAS;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,gBAAgB,EAAE,MAAM,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,eAAe,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,QAAQ,GAAG,YAAY,GAAG,gBAAgB,CAAC;AAE/E,MAAM,WAAW,aAAa;IAC7B,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,SAAS,EAAE,QAAQ,EAAE,CAAC;IACtB,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/core/model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,WAAW,EACX,oBAAo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AAE,GACnE;IAAE,IAAI,EAAE,iBAAiB,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAClE;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,GAC9C;IACA,IAAI,EAAE,kBAAkB,CAAC;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,aAAa,GAAG,WAAW,GAAG,WAAW,GAAG,YAAY,GAAG,cAAc,CAAC;CACjF,GACD;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAC7C;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAC9D;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC3D;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,aAAa,CAAA;CAAE,CAAC;AAE7C,MAAM,WAAW,mBAAmB;IACnC,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,KAAK;IACrB,WAAW,CAAC,OAAO,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IAC1F,mBAAmB,CAClB,OAAO,EAAE,YAAY,EACrB,OAAO,CAAC,EAAE,mBAAmB,GAC3B,aAAa,CAAC,WAAW,CAAC,CAAC;CAC9B"}
|
package/dist/core/result.d.ts
CHANGED
|
@@ -2,6 +2,29 @@ import type { Agent } from "./agent";
|
|
|
2
2
|
import type { GuardrailRunResult } from "./guardrails";
|
|
3
3
|
import type { FinishReason, UsageInfo } from "./model";
|
|
4
4
|
import type { ChatMessage } from "./types";
|
|
5
|
+
export interface PendingToolCall {
|
|
6
|
+
toolCallId: string;
|
|
7
|
+
toolName: string;
|
|
8
|
+
arguments: string;
|
|
9
|
+
parsedArguments: unknown;
|
|
10
|
+
}
|
|
11
|
+
export declare class InterruptedRunResult<TOutput = undefined> {
|
|
12
|
+
readonly interrupted: true;
|
|
13
|
+
readonly pendingToolCalls: PendingToolCall[];
|
|
14
|
+
readonly messages: ChatMessage[];
|
|
15
|
+
readonly currentAgent: Agent<any, TOutput>;
|
|
16
|
+
readonly context: any;
|
|
17
|
+
readonly numTurns: number;
|
|
18
|
+
readonly usage: UsageInfo | undefined;
|
|
19
|
+
constructor(options: {
|
|
20
|
+
pendingToolCalls: PendingToolCall[];
|
|
21
|
+
messages: ChatMessage[];
|
|
22
|
+
currentAgent: Agent<any, TOutput>;
|
|
23
|
+
context: any;
|
|
24
|
+
numTurns: number;
|
|
25
|
+
usage?: UsageInfo;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
5
28
|
export interface RunResultOptions<TOutput = undefined> {
|
|
6
29
|
output: string;
|
|
7
30
|
messages: ChatMessage[];
|
|
@@ -16,6 +39,7 @@ export interface RunResultOptions<TOutput = undefined> {
|
|
|
16
39
|
outputGuardrailResults?: GuardrailRunResult[];
|
|
17
40
|
}
|
|
18
41
|
export declare class RunResult<TOutput = undefined> {
|
|
42
|
+
readonly interrupted: false;
|
|
19
43
|
readonly output: string;
|
|
20
44
|
readonly finalOutput: TOutput extends undefined ? undefined : TOutput;
|
|
21
45
|
readonly messages: ChatMessage[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/core/result.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAA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 -1,4 +1,22 @@
|
|
|
1
|
+
export class InterruptedRunResult {
|
|
2
|
+
interrupted = true;
|
|
3
|
+
pendingToolCalls;
|
|
4
|
+
messages;
|
|
5
|
+
currentAgent;
|
|
6
|
+
context;
|
|
7
|
+
numTurns;
|
|
8
|
+
usage;
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.pendingToolCalls = options.pendingToolCalls;
|
|
11
|
+
this.messages = options.messages;
|
|
12
|
+
this.currentAgent = options.currentAgent;
|
|
13
|
+
this.context = options.context;
|
|
14
|
+
this.numTurns = options.numTurns;
|
|
15
|
+
this.usage = options.usage;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
1
18
|
export class RunResult {
|
|
19
|
+
interrupted = false;
|
|
2
20
|
output;
|
|
3
21
|
finalOutput;
|
|
4
22
|
messages;
|
package/dist/core/result.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/core/result.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/core/result.ts"],"names":[],"mappings":"AAYA,MAAM,OAAO,oBAAoB;IACvB,WAAW,GAAG,IAAa,CAAC;IAC5B,gBAAgB,CAAoB;IACpC,QAAQ,CAAgB;IACxB,YAAY,CAAsB;IAClC,OAAO,CAAM;IACb,QAAQ,CAAS;IACjB,KAAK,CAAwB;IAEtC,YAAY,OAOX;QACA,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;QACjD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;QACzC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAC/B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;IAC5B,CA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ToolInputGuardrail, ToolOutputGuardrail } from "./guardrails";
|
|
4
4
|
import type { RunHooks } from "./hooks";
|
|
5
5
|
import type { Model, ModelRequest, StreamEvent } from "./model";
|
|
6
|
-
import { RunResult } from "./result";
|
|
6
|
+
import { InterruptedRunResult, RunResult } from "./result";
|
|
7
|
+
import type { SubAgent } from "./subagent";
|
|
7
8
|
import type { ChatMessage } from "./types";
|
|
9
|
+
export interface ToolApproval {
|
|
10
|
+
toolCallId: string;
|
|
11
|
+
decision: "approve" | "deny";
|
|
12
|
+
/** Message sent to LLM when denied */
|
|
13
|
+
denyMessage?: string;
|
|
14
|
+
}
|
|
8
15
|
export type ToolErrorFormatter = (toolName: string, error: unknown) => string;
|
|
9
16
|
export type CallModelInputFilter<TContext> = (params: {
|
|
10
17
|
agent: Agent<TContext, any>;
|
|
@@ -40,8 +47,10 @@ export interface RunOptions<TContext, TOutput = undefined> {
|
|
|
40
47
|
toolOutputGuardrails?: ToolOutputGuardrail<TContext>[];
|
|
41
48
|
/** Reset tool_choice to "auto" after the first LLM call to prevent infinite loops */
|
|
42
49
|
resetToolChoice?: boolean;
|
|
50
|
+
/** Additional subagents available at runtime beyond those defined on the agent */
|
|
51
|
+
dynamicSubagents?: SubAgent<TContext>[];
|
|
43
52
|
}
|
|
44
|
-
export declare function run<TContext, TOutput = undefined>(agent: Agent<TContext, TOutput>, input: string | ChatMessage[], options?: RunOptions<TContext, TOutput>): Promise<RunResult<TOutput>>;
|
|
53
|
+
export declare function run<TContext, TOutput = undefined>(agent: Agent<TContext, TOutput>, input: string | ChatMessage[], options?: RunOptions<TContext, TOutput>): Promise<RunResult<TOutput> | InterruptedRunResult<TOutput>>;
|
|
45
54
|
export interface StreamOptions<TContext, TOutput = undefined> extends RunOptions<TContext, TOutput> {
|
|
46
55
|
}
|
|
47
56
|
export interface StreamedRunResult<TOutput = undefined> {
|
|
@@ -49,4 +58,5 @@ export interface StreamedRunResult<TOutput = undefined> {
|
|
|
49
58
|
result: Promise<RunResult<TOutput>>;
|
|
50
59
|
}
|
|
51
60
|
export declare function stream<TContext, TOutput = undefined>(agent: Agent<TContext, TOutput>, input: string | ChatMessage[], options?: StreamOptions<TContext, TOutput>): StreamedRunResult<TOutput>;
|
|
61
|
+
export declare function resumeRun<TContext, TOutput = undefined>(interrupted: InterruptedRunResult<TOutput>, approvals: ToolApproval[], options?: RunOptions<TContext, TOutput>): Promise<RunResult<TOutput> | InterruptedRunResult<TOutput>>;
|
|
52
62
|
//# sourceMappingURL=run.d.ts.map
|
package/dist/core/run.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../../src/core/run.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAe5C,OAAO,KAAK,EAAsB,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEhG,OAAO,KAAK,EAMX,QAAQ,EAGR,MAAM,SAAS,CAAC;AAEjB,OAAO,KAAK,EAEX,KAAK,EACL,YAAY,EAEZ,WAAW,EAEX,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../../src/core/run.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAe5C,OAAO,KAAK,EAAsB,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEhG,OAAO,KAAK,EAMX,QAAQ,EAGR,MAAM,SAAS,CAAC;AAEjB,OAAO,KAAK,EAEX,KAAK,EACL,YAAY,EAEZ,WAAW,EAEX,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,oBAAoB,E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gB,MAAM,CAAC,QAAQ,EAAE,OAAO,GAAG,SAAS,EACnD,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,EAC/B,KAAK,EAAE,MAAM,GAAG,WAAW,EAAE,EAC7B,OAAO,CAAC,EAAE,aAAa,CAAC,QAAQ,EAAE,OAAO,CAAC,GACxC,iBAAiB,CAAC,OAAO,CAAC,CAW5B;AA4yBD,wBAAsB,SAAS,CAAC,QAAQ,EAAE,OAAO,GAAG,SAAS,EAC5D,WAAW,EAAE,oBAAoB,CAAC,OAAO,CAAC,EAC1C,SAAS,EAAE,YAAY,EAAE,EACzB,OAAO,CAAC,EAAE,UAAU,CAAC,QAAQ,EAAE,OAAO,CAAC,GACrC,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAAC,CAyH7D"}
|
package/dist/core/run.js
CHANGED
|
@@ -3,11 +3,52 @@ import { MaxBudgetExceededError, MaxTurnsExceededError, OutputParseError, RunAbo
|
|
|
3
3
|
import { runInputGuardrails, runOutputGuardrails, runToolInputGuardrails, runToolOutputGuardrails, } from "./guardrails";
|
|
4
4
|
import { handoffToDefinition } from "./handoff";
|
|
5
5
|
import { isFunctionTool, isHostedTool } from "./hosted-tool";
|
|
6
|
-
import { RunResult } from "./result";
|
|
6
|
+
import { InterruptedRunResult, RunResult } from "./result";
|
|
7
7
|
import { subagentToDefinition, subagentToTool } from "./subagent";
|
|
8
8
|
import { toolToDefinition } from "./tool";
|
|
9
9
|
import { getCurrentTrace } from "./tracing";
|
|
10
10
|
const DEFAULT_MAX_TURNS = 10;
|
|
11
|
+
/**
|
|
12
|
+
* A simple async channel for pushing events from callbacks and pulling them
|
|
13
|
+
* from an async generator. Used to relay subagent stream events through the
|
|
14
|
+
* parent stream in real time.
|
|
15
|
+
*/
|
|
16
|
+
function createStreamEventChannel() {
|
|
17
|
+
const queue = [];
|
|
18
|
+
let resolve;
|
|
19
|
+
let finished = false;
|
|
20
|
+
return {
|
|
21
|
+
push(event) {
|
|
22
|
+
queue.push(event);
|
|
23
|
+
resolve?.();
|
|
24
|
+
resolve = undefined;
|
|
25
|
+
},
|
|
26
|
+
done() {
|
|
27
|
+
finished = true;
|
|
28
|
+
resolve?.();
|
|
29
|
+
resolve = undefined;
|
|
30
|
+
},
|
|
31
|
+
[Symbol.asyncIterator]() {
|
|
32
|
+
const iter = {
|
|
33
|
+
async next() {
|
|
34
|
+
while (queue.length === 0 && !finished) {
|
|
35
|
+
await new Promise((r) => {
|
|
36
|
+
resolve = r;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (queue.length > 0) {
|
|
40
|
+
return { value: queue.shift(), done: false };
|
|
41
|
+
}
|
|
42
|
+
return { value: undefined, done: true };
|
|
43
|
+
},
|
|
44
|
+
[Symbol.asyncIterator]() {
|
|
45
|
+
return iter;
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
return iter;
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
11
52
|
function getErrorMessage(error) {
|
|
12
53
|
if (error instanceof Error)
|
|
13
54
|
return error.message;
|
|
@@ -97,6 +138,31 @@ async function executeWithTimeout(fn, timeout, toolName) {
|
|
|
97
138
|
});
|
|
98
139
|
});
|
|
99
140
|
}
|
|
141
|
+
/** Execute a tool with optional timeout and retry logic */
|
|
142
|
+
async function executeToolWithRetry(fn, toolConfig) {
|
|
143
|
+
const maxAttempts = (toolConfig.retries?.limit ?? 0) + 1;
|
|
144
|
+
const baseDelay = toolConfig.retries?.delay ?? 1000;
|
|
145
|
+
const backoff = toolConfig.retries?.backoff ?? "exponential";
|
|
146
|
+
const shouldRetry = toolConfig.retries?.shouldRetry ?? (() => true);
|
|
147
|
+
let lastError;
|
|
148
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
149
|
+
try {
|
|
150
|
+
return await executeWithTimeout(fn, toolConfig.timeout, toolConfig.name);
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
lastError = error;
|
|
154
|
+
if (error instanceof ToolTimeoutError)
|
|
155
|
+
throw error; // Don't retry timeouts
|
|
156
|
+
if (attempt < maxAttempts - 1 && shouldRetry(error)) {
|
|
157
|
+
const delay = backoff === "exponential" ? baseDelay * 2 ** attempt : baseDelay;
|
|
158
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
throw lastError;
|
|
165
|
+
}
|
|
100
166
|
function checkAborted(signal) {
|
|
101
167
|
if (signal?.aborted) {
|
|
102
168
|
throw new RunAbortedError();
|
|
@@ -140,6 +206,7 @@ export async function run(agent, input, options) {
|
|
|
140
206
|
const callModelInputFilter = options?.callModelInputFilter;
|
|
141
207
|
const toolInputGuardrails = options?.toolInputGuardrails ?? [];
|
|
142
208
|
const toolOutputGuardrails = options?.toolOutputGuardrails ?? [];
|
|
209
|
+
const dynamicSubagents = options?.dynamicSubagents;
|
|
143
210
|
// Fire beforeRun hook on the entry agent
|
|
144
211
|
const inputText = typeof input === "string" ? input : extractUserText(input);
|
|
145
212
|
if (agent.hooks.beforeRun) {
|
|
@@ -181,7 +248,7 @@ export async function run(agent, input, options) {
|
|
|
181
248
|
}
|
|
182
249
|
for (let turn = 0; turn < maxTurns; turn++) {
|
|
183
250
|
checkAborted(signal);
|
|
184
|
-
const toolDefs = await buildToolDefs(currentAgent, ctx.context);
|
|
251
|
+
const toolDefs = await buildToolDefs(currentAgent, ctx.context, dynamicSubagents);
|
|
185
252
|
let request = {
|
|
186
253
|
messages,
|
|
187
254
|
tools: toolDefs.length > 0 ? toolDefs : undefined,
|
|
@@ -273,7 +340,19 @@ export async function run(agent, input, options) {
|
|
|
273
340
|
}
|
|
274
341
|
return buildFinalResult(agent, currentAgent, messages, ctx, trace, lastFinishReason, lastResponseId, inputGuardrailResults);
|
|
275
342
|
}
|
|
276
|
-
|
|
343
|
+
// Check for tools that need approval before executing
|
|
344
|
+
const pendingApprovals = await collectPendingApprovals(currentAgent, response.toolCalls, ctx.context);
|
|
345
|
+
if (pendingApprovals.length > 0) {
|
|
346
|
+
return new InterruptedRunResult({
|
|
347
|
+
pendingToolCalls: pendingApprovals,
|
|
348
|
+
messages: [...messages],
|
|
349
|
+
currentAgent,
|
|
350
|
+
context: ctx.context,
|
|
351
|
+
numTurns: ctx.numTurns,
|
|
352
|
+
usage: ctx.usage,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
const { toolMessages, handoffAgent } = await executeToolCallsWithHandoffs(currentAgent, ctx, response.toolCalls, trace, signal, toolErrorFmt, runHooks, toolInputGuardrails, toolOutputGuardrails, undefined, dynamicSubagents);
|
|
277
356
|
messages.push(...toolMessages);
|
|
278
357
|
// Check toolUseBehavior — should we stop instead of calling the LLM again?
|
|
279
358
|
if (await shouldStopAfterToolCalls(currentAgent, response.toolCalls, toolMessages)) {
|
|
@@ -410,6 +489,7 @@ async function* streamInternal(agent, input, options, resolveResult, rejectResul
|
|
|
410
489
|
const callModelInputFilter = options?.callModelInputFilter;
|
|
411
490
|
const toolInputGuardrails = options?.toolInputGuardrails ?? [];
|
|
412
491
|
const toolOutputGuardrails = options?.toolOutputGuardrails ?? [];
|
|
492
|
+
const dynamicSubagents = options?.dynamicSubagents;
|
|
413
493
|
// Fire beforeRun hook on the entry agent
|
|
414
494
|
const inputText = typeof input === "string" ? input : extractUserText(input);
|
|
415
495
|
if (agent.hooks.beforeRun) {
|
|
@@ -451,7 +531,7 @@ async function* streamInternal(agent, input, options, resolveResult, rejectResul
|
|
|
451
531
|
}
|
|
452
532
|
for (let turn = 0; turn < maxTurns; turn++) {
|
|
453
533
|
checkAborted(signal);
|
|
454
|
-
const toolDefs = await buildToolDefs(currentAgent, ctx.context);
|
|
534
|
+
const toolDefs = await buildToolDefs(currentAgent, ctx.context, dynamicSubagents);
|
|
455
535
|
let request = {
|
|
456
536
|
messages,
|
|
457
537
|
tools: toolDefs.length > 0 ? toolDefs : undefined,
|
|
@@ -546,7 +626,33 @@ async function* streamInternal(agent, input, options, resolveResult, rejectResul
|
|
|
546
626
|
resolveResult(result);
|
|
547
627
|
return;
|
|
548
628
|
}
|
|
549
|
-
|
|
629
|
+
// Check for tools that need approval before executing
|
|
630
|
+
const pendingApprovals = await collectPendingApprovals(currentAgent, finalResponse.toolCalls, ctx.context);
|
|
631
|
+
if (pendingApprovals.length > 0) {
|
|
632
|
+
const interrupted = new InterruptedRunResult({
|
|
633
|
+
pendingToolCalls: pendingApprovals,
|
|
634
|
+
messages: [...messages],
|
|
635
|
+
currentAgent,
|
|
636
|
+
context: ctx.context,
|
|
637
|
+
numTurns: ctx.numTurns,
|
|
638
|
+
usage: ctx.usage,
|
|
639
|
+
});
|
|
640
|
+
resolveResult(interrupted);
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
// Use a channel to relay subagent stream events in real time
|
|
644
|
+
const channel = createStreamEventChannel();
|
|
645
|
+
const toolExecPromise = executeToolCallsWithHandoffs(currentAgent, ctx, finalResponse.toolCalls, trace, signal, toolErrorFmt, runHooks, toolInputGuardrails, toolOutputGuardrails, (event) => channel.push(event), dynamicSubagents).then((result) => {
|
|
646
|
+
channel.done();
|
|
647
|
+
return result;
|
|
648
|
+
}, (error) => {
|
|
649
|
+
channel.done();
|
|
650
|
+
throw error;
|
|
651
|
+
});
|
|
652
|
+
for await (const event of channel) {
|
|
653
|
+
yield event;
|
|
654
|
+
}
|
|
655
|
+
const { toolMessages, handoffAgent } = await toolExecPromise;
|
|
550
656
|
messages.push(...toolMessages);
|
|
551
657
|
// Check toolUseBehavior
|
|
552
658
|
if (await shouldStopAfterToolCalls(currentAgent, finalResponse.toolCalls, toolMessages)) {
|
|
@@ -702,7 +808,7 @@ async function buildFinalResult(entryAgent, currentAgent, messages, ctx, trace,
|
|
|
702
808
|
}
|
|
703
809
|
return result;
|
|
704
810
|
}
|
|
705
|
-
async function buildToolDefs(agent, context) {
|
|
811
|
+
async function buildToolDefs(agent, context, extraSubagents) {
|
|
706
812
|
const defs = [];
|
|
707
813
|
for (const t of agent.tools) {
|
|
708
814
|
if (isHostedTool(t)) {
|
|
@@ -715,7 +821,8 @@ async function buildToolDefs(agent, context) {
|
|
|
715
821
|
defs.push(toolToDefinition(t));
|
|
716
822
|
}
|
|
717
823
|
}
|
|
718
|
-
|
|
824
|
+
const allSubagents = [...agent.subagents, ...(extraSubagents ?? [])];
|
|
825
|
+
for (const sa of allSubagents) {
|
|
719
826
|
defs.push(subagentToDefinition(sa));
|
|
720
827
|
}
|
|
721
828
|
for (const h of agent.handoffs) {
|
|
@@ -773,11 +880,12 @@ function applyResetToolChoice(settings, turn, resetToolChoice) {
|
|
|
773
880
|
}
|
|
774
881
|
return settings;
|
|
775
882
|
}
|
|
776
|
-
async function executeToolCallsWithHandoffs(agent, ctx, toolCalls, trace, signal, toolErrorFmt, runHooks, toolInputGuardrails, toolOutputGuardrails) {
|
|
883
|
+
async function executeToolCallsWithHandoffs(agent, ctx, toolCalls, trace, signal, toolErrorFmt, runHooks, toolInputGuardrails, toolOutputGuardrails, onStreamEvent, extraSubagents) {
|
|
777
884
|
let handoffAgent;
|
|
778
885
|
// Build O(1) lookup maps
|
|
779
886
|
const handoffsByName = new Map(agent.handoffs.map((h) => [h.toolName, h]));
|
|
780
|
-
const
|
|
887
|
+
const allSubagents = [...agent.subagents, ...(extraSubagents ?? [])];
|
|
888
|
+
const subagentsByName = new Map(allSubagents.map((sa) => [sa.toolName, sa]));
|
|
781
889
|
const functionTools = agent.tools.filter(isFunctionTool);
|
|
782
890
|
const toolsByName = new Map(functionTools.map((t) => [t.name, t]));
|
|
783
891
|
const results = await Promise.all(toolCalls.map(async (tc) => {
|
|
@@ -826,20 +934,21 @@ async function executeToolCallsWithHandoffs(agent, ctx, toolCalls, trace, signal
|
|
|
826
934
|
if (runHooks?.onToolStart) {
|
|
827
935
|
await runHooks.onToolStart({ agent, toolName: tcName, context: ctx.context });
|
|
828
936
|
}
|
|
937
|
+
const toolOpts = { signal, onStreamEvent };
|
|
829
938
|
let result;
|
|
830
939
|
if (trace) {
|
|
831
940
|
const span = trace.startSpan(`subagent:${matchedSubagent.agent.name}`, "subagent", {
|
|
832
941
|
toolName: tcName,
|
|
833
942
|
});
|
|
834
943
|
try {
|
|
835
|
-
result = await saTool.execute(ctx.context, params,
|
|
944
|
+
result = await saTool.execute(ctx.context, params, toolOpts);
|
|
836
945
|
}
|
|
837
946
|
finally {
|
|
838
947
|
trace.endSpan(span);
|
|
839
948
|
}
|
|
840
949
|
}
|
|
841
950
|
else {
|
|
842
|
-
result = await saTool.execute(ctx.context, params,
|
|
951
|
+
result = await saTool.execute(ctx.context, params, toolOpts);
|
|
843
952
|
}
|
|
844
953
|
// Fire onSubagentStop
|
|
845
954
|
if (agent.hooks.onSubagentStop) {
|
|
@@ -925,14 +1034,18 @@ async function executeToolCallsWithHandoffs(agent, ctx, toolCalls, trace, signal
|
|
|
925
1034
|
toolName: tcName,
|
|
926
1035
|
});
|
|
927
1036
|
try {
|
|
928
|
-
result = await
|
|
1037
|
+
result = await executeToolWithRetry(() => tool.execute(ctx.context, params, { signal }), { timeout: tool.timeout, retries: tool.retries, name: tcName });
|
|
929
1038
|
}
|
|
930
1039
|
finally {
|
|
931
1040
|
trace.endSpan(span);
|
|
932
1041
|
}
|
|
933
1042
|
}
|
|
934
1043
|
else {
|
|
935
|
-
result = await
|
|
1044
|
+
result = await executeToolWithRetry(() => tool.execute(ctx.context, params, { signal }), {
|
|
1045
|
+
timeout: tool.timeout,
|
|
1046
|
+
retries: tool.retries,
|
|
1047
|
+
name: tcName,
|
|
1048
|
+
});
|
|
936
1049
|
}
|
|
937
1050
|
// Run tool output guardrails
|
|
938
1051
|
if (toolOutputGuardrails && toolOutputGuardrails.length > 0) {
|
|
@@ -969,4 +1082,145 @@ async function executeToolCallsWithHandoffs(agent, ctx, toolCalls, trace, signal
|
|
|
969
1082
|
}));
|
|
970
1083
|
return { toolMessages: results, handoffAgent };
|
|
971
1084
|
}
|
|
1085
|
+
async function collectPendingApprovals(agent, toolCalls, context) {
|
|
1086
|
+
const functionTools = agent.tools.filter(isFunctionTool);
|
|
1087
|
+
const toolsByName = new Map(functionTools.map((t) => [t.name, t]));
|
|
1088
|
+
const pending = [];
|
|
1089
|
+
for (const tc of toolCalls) {
|
|
1090
|
+
const matchedTool = toolsByName.get(tc.function.name);
|
|
1091
|
+
if (!matchedTool?.needsApproval)
|
|
1092
|
+
continue;
|
|
1093
|
+
let parsedArgs;
|
|
1094
|
+
try {
|
|
1095
|
+
parsedArgs = JSON.parse(tc.function.arguments);
|
|
1096
|
+
}
|
|
1097
|
+
catch {
|
|
1098
|
+
parsedArgs = undefined;
|
|
1099
|
+
}
|
|
1100
|
+
let needsApproval;
|
|
1101
|
+
if (typeof matchedTool.needsApproval === "function") {
|
|
1102
|
+
needsApproval = await matchedTool.needsApproval(parsedArgs, context);
|
|
1103
|
+
}
|
|
1104
|
+
else {
|
|
1105
|
+
needsApproval = matchedTool.needsApproval;
|
|
1106
|
+
}
|
|
1107
|
+
if (needsApproval) {
|
|
1108
|
+
pending.push({
|
|
1109
|
+
toolCallId: tc.id,
|
|
1110
|
+
toolName: tc.function.name,
|
|
1111
|
+
arguments: tc.function.arguments,
|
|
1112
|
+
parsedArguments: parsedArgs,
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
return pending;
|
|
1117
|
+
}
|
|
1118
|
+
export async function resumeRun(interrupted, approvals, options) {
|
|
1119
|
+
const agent = interrupted.currentAgent;
|
|
1120
|
+
const messages = [...interrupted.messages];
|
|
1121
|
+
// The last message should be the assistant message with tool_calls
|
|
1122
|
+
const lastMsg = messages[messages.length - 1];
|
|
1123
|
+
if (!lastMsg || lastMsg.role !== "assistant" || !lastMsg.tool_calls) {
|
|
1124
|
+
throw new StratusError("Cannot resume: last message is not an assistant message with tool_calls");
|
|
1125
|
+
}
|
|
1126
|
+
const approvalMap = new Map(approvals.map((a) => [a.toolCallId, a]));
|
|
1127
|
+
const pendingIds = new Set(interrupted.pendingToolCalls.map((p) => p.toolCallId));
|
|
1128
|
+
const functionTools = agent.tools.filter(isFunctionTool);
|
|
1129
|
+
const subagentTools = (interrupted.currentAgent.subagents ?? []).map(subagentToTool);
|
|
1130
|
+
const toolsByName = new Map([...functionTools, ...subagentTools].map((t) => [t.name, t]));
|
|
1131
|
+
const model = options?.model ?? agent.model;
|
|
1132
|
+
if (!model) {
|
|
1133
|
+
throw new StratusError("No model provided. Pass a model to the agent or to run().");
|
|
1134
|
+
}
|
|
1135
|
+
const signal = options?.signal;
|
|
1136
|
+
checkAborted(signal);
|
|
1137
|
+
const context = (options?.context ?? interrupted.context);
|
|
1138
|
+
// Process each tool call from the assistant message
|
|
1139
|
+
const toolMessages = [];
|
|
1140
|
+
for (const tc of lastMsg.tool_calls) {
|
|
1141
|
+
if (pendingIds.has(tc.id)) {
|
|
1142
|
+
// This was a pending approval tool call
|
|
1143
|
+
const approval = approvalMap.get(tc.id);
|
|
1144
|
+
if (!approval || approval.decision === "deny") {
|
|
1145
|
+
const denyMsg = approval?.denyMessage ?? "Tool call denied by user";
|
|
1146
|
+
toolMessages.push({
|
|
1147
|
+
role: "tool",
|
|
1148
|
+
tool_call_id: tc.id,
|
|
1149
|
+
content: denyMsg,
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
else {
|
|
1153
|
+
// Approved — execute the tool
|
|
1154
|
+
const matchedTool = toolsByName.get(tc.function.name);
|
|
1155
|
+
if (!matchedTool) {
|
|
1156
|
+
toolMessages.push({
|
|
1157
|
+
role: "tool",
|
|
1158
|
+
tool_call_id: tc.id,
|
|
1159
|
+
content: `Error: Unknown tool "${tc.function.name}"`,
|
|
1160
|
+
});
|
|
1161
|
+
continue;
|
|
1162
|
+
}
|
|
1163
|
+
try {
|
|
1164
|
+
const params = JSON.parse(tc.function.arguments);
|
|
1165
|
+
const result = await executeToolWithRetry(() => matchedTool.execute(context, params, { signal }), {
|
|
1166
|
+
timeout: matchedTool.timeout,
|
|
1167
|
+
retries: matchedTool.retries,
|
|
1168
|
+
name: tc.function.name,
|
|
1169
|
+
});
|
|
1170
|
+
toolMessages.push({
|
|
1171
|
+
role: "tool",
|
|
1172
|
+
tool_call_id: tc.id,
|
|
1173
|
+
content: result,
|
|
1174
|
+
});
|
|
1175
|
+
}
|
|
1176
|
+
catch (error) {
|
|
1177
|
+
toolMessages.push({
|
|
1178
|
+
role: "tool",
|
|
1179
|
+
tool_call_id: tc.id,
|
|
1180
|
+
content: formatToolError(tc.function.name, error, options?.toolErrorFormatter),
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
else {
|
|
1186
|
+
// Non-pending tool call — execute it now (it wasn't executed before the interrupt)
|
|
1187
|
+
const matchedTool = toolsByName.get(tc.function.name);
|
|
1188
|
+
if (!matchedTool) {
|
|
1189
|
+
toolMessages.push({
|
|
1190
|
+
role: "tool",
|
|
1191
|
+
tool_call_id: tc.id,
|
|
1192
|
+
content: `Error: Unknown tool "${tc.function.name}"`,
|
|
1193
|
+
});
|
|
1194
|
+
continue;
|
|
1195
|
+
}
|
|
1196
|
+
try {
|
|
1197
|
+
const params = JSON.parse(tc.function.arguments);
|
|
1198
|
+
const result = await executeToolWithRetry(() => matchedTool.execute(context, params, { signal }), {
|
|
1199
|
+
timeout: matchedTool.timeout,
|
|
1200
|
+
retries: matchedTool.retries,
|
|
1201
|
+
name: tc.function.name,
|
|
1202
|
+
});
|
|
1203
|
+
toolMessages.push({
|
|
1204
|
+
role: "tool",
|
|
1205
|
+
tool_call_id: tc.id,
|
|
1206
|
+
content: result,
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1209
|
+
catch (error) {
|
|
1210
|
+
toolMessages.push({
|
|
1211
|
+
role: "tool",
|
|
1212
|
+
tool_call_id: tc.id,
|
|
1213
|
+
content: formatToolError(tc.function.name, error, options?.toolErrorFormatter),
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
messages.push(...toolMessages);
|
|
1219
|
+
// Continue the run from where we left off
|
|
1220
|
+
return run(agent, messages, {
|
|
1221
|
+
...options,
|
|
1222
|
+
context,
|
|
1223
|
+
model,
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
972
1226
|
//# sourceMappingURL=run.js.map
|