@anvia/core 0.6.2 → 0.7.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/dist/agent/index.d.ts +16 -9
- package/dist/agent/index.js +10 -6
- package/dist/{agent-BXsuZ7pi.d.ts → agent-BW19m79B.d.ts} +10 -4
- package/dist/audio-generation/index.d.ts +1 -1
- package/dist/{chunk-I47EHI45.js → chunk-66IJDP6L.js} +8 -6
- package/dist/chunk-66IJDP6L.js.map +1 -0
- package/dist/{chunk-SWG6AORE.js → chunk-6OR42QTG.js} +245 -131
- package/dist/chunk-6OR42QTG.js.map +1 -0
- package/dist/{chunk-EFGX3EX5.js → chunk-FTO3W4UP.js} +2 -2
- package/dist/chunk-FTO3W4UP.js.map +1 -0
- package/dist/{chunk-AER5FGPK.js → chunk-IA47SXLS.js} +14 -30
- package/dist/chunk-IA47SXLS.js.map +1 -0
- package/dist/chunk-MMHG7WAM.js +19 -0
- package/dist/chunk-MMHG7WAM.js.map +1 -0
- package/dist/{chunk-WZR3ZPGM.js → chunk-NCZRYTA6.js} +9 -3
- package/dist/chunk-NCZRYTA6.js.map +1 -0
- package/dist/chunk-S5IGJ4RB.js +224 -0
- package/dist/chunk-S5IGJ4RB.js.map +1 -0
- package/dist/{chunk-PZN27R7H.js → chunk-SHCKZZQH.js} +10 -11
- package/dist/chunk-SHCKZZQH.js.map +1 -0
- package/dist/{chunk-ZQQYFVO5.js → chunk-WJO6NVE2.js} +4 -217
- package/dist/chunk-WJO6NVE2.js.map +1 -0
- package/dist/{chunk-SHHSMV7O.js → chunk-ZT2YH2GA.js} +5 -5
- package/dist/chunk-ZT2YH2GA.js.map +1 -0
- package/dist/completion/index.d.ts +3 -3
- package/dist/completion/index.js +15 -7
- package/dist/{create-completion-B7-k4LsF.d.ts → create-completion-BEbtU5pX.d.ts} +3 -2
- package/dist/embeddings/index.js +1 -1
- package/dist/evals/index.d.ts +4 -4
- package/dist/evals/index.js +30 -26
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.d.ts +4 -4
- package/dist/extractor/index.js +9 -7
- package/dist/image-generation/index.d.ts +1 -1
- package/dist/index.d.ts +9 -9
- package/dist/index.js +16 -11
- package/dist/internal/agent.d.ts +4 -4
- package/dist/internal/agent.js +7 -5
- package/dist/loaders/index.d.ts +1 -1
- package/dist/mcp/index.d.ts +4 -4
- package/dist/mcp/index.js +5 -4
- package/dist/mcp/index.js.map +1 -1
- package/dist/memory/index.d.ts +1 -1
- package/dist/{middleware-D6Wu0AcC.d.ts → middleware-CZocYAtZ.d.ts} +2 -2
- package/dist/observability/index.d.ts +2 -2
- package/dist/pipeline/index.d.ts +4 -4
- package/dist/pipeline/index.js +17 -12
- package/dist/pipeline/index.js.map +1 -1
- package/dist/skills/index.d.ts +4 -4
- package/dist/skills/index.js +6 -4
- package/dist/{think-tool-DTDGH1Q1.d.ts → think-tool-B8Rqw2i_.d.ts} +1 -1
- package/dist/tool/index.d.ts +4 -4
- package/dist/tool/index.js +9 -6
- package/dist/{tool-OU7OZS0t.d.ts → tool-BLypI31e.d.ts} +18 -4
- package/dist/transcription/index.d.ts +1 -1
- package/dist/{types-_boSLqrx.d.ts → types-C8loR4il.d.ts} +3 -1
- package/dist/{types-DXkaLs4s.d.ts → types-CIwm9p4w.d.ts} +2 -2
- package/dist/{types-Bpjngcxq.d.ts → types-D-YTJdLj.d.ts} +1 -1
- package/dist/vector-store/index.d.ts +2 -2
- package/dist/vector-store/index.js +3 -2
- package/package.json +1 -1
- package/dist/chunk-AER5FGPK.js.map +0 -1
- package/dist/chunk-EFGX3EX5.js.map +0 -1
- package/dist/chunk-I47EHI45.js.map +0 -1
- package/dist/chunk-PZN27R7H.js.map +0 -1
- package/dist/chunk-SHHSMV7O.js.map +0 -1
- package/dist/chunk-SWG6AORE.js.map +0 -1
- package/dist/chunk-WZR3ZPGM.js.map +0 -1
- package/dist/chunk-ZQQYFVO5.js.map +0 -1
|
@@ -6,19 +6,26 @@ import {
|
|
|
6
6
|
} from "./chunk-YK4WAAS4.js";
|
|
7
7
|
import {
|
|
8
8
|
ToolSet,
|
|
9
|
+
parseToolArgs,
|
|
9
10
|
toolResultContentToText
|
|
10
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-IA47SXLS.js";
|
|
11
12
|
import {
|
|
12
13
|
createTool
|
|
13
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-66IJDP6L.js";
|
|
15
|
+
import {
|
|
16
|
+
compact
|
|
17
|
+
} from "./chunk-MMHG7WAM.js";
|
|
14
18
|
import {
|
|
15
19
|
CompletionRequestBuilder,
|
|
20
|
+
isStreamingCompletionModel
|
|
21
|
+
} from "./chunk-S5IGJ4RB.js";
|
|
22
|
+
import {
|
|
16
23
|
Message,
|
|
17
24
|
ToolContent,
|
|
18
25
|
Usage,
|
|
19
26
|
assertCompletionRequestSupported,
|
|
20
27
|
textFromAssistantContent
|
|
21
|
-
} from "./chunk-
|
|
28
|
+
} from "./chunk-WJO6NVE2.js";
|
|
22
29
|
import {
|
|
23
30
|
mapWithConcurrency
|
|
24
31
|
} from "./chunk-OIMLU4SF.js";
|
|
@@ -46,6 +53,14 @@ var PromptCancelledError = class extends Error {
|
|
|
46
53
|
chatHistory;
|
|
47
54
|
reason;
|
|
48
55
|
};
|
|
56
|
+
var ToolApprovalRequiredError = class extends Error {
|
|
57
|
+
constructor(request) {
|
|
58
|
+
super(`Tool approval required for ${request.toolName}, but no approval handler is configured.`);
|
|
59
|
+
this.request = request;
|
|
60
|
+
this.name = "ToolApprovalRequiredError";
|
|
61
|
+
}
|
|
62
|
+
request;
|
|
63
|
+
};
|
|
49
64
|
|
|
50
65
|
// src/agent/hooks.ts
|
|
51
66
|
function createHook(hook) {
|
|
@@ -58,11 +73,11 @@ function skipTool(reason) {
|
|
|
58
73
|
return { type: "skip", reason };
|
|
59
74
|
}
|
|
60
75
|
function requestToolApproval(options = {}) {
|
|
61
|
-
return {
|
|
76
|
+
return compact({
|
|
62
77
|
type: "approval_request",
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
};
|
|
78
|
+
reason: options.reason,
|
|
79
|
+
rejectMessage: options.rejectMessage
|
|
80
|
+
});
|
|
66
81
|
}
|
|
67
82
|
var runControl = {
|
|
68
83
|
continue() {
|
|
@@ -430,6 +445,24 @@ var PromptRequestMemory = class {
|
|
|
430
445
|
}
|
|
431
446
|
};
|
|
432
447
|
|
|
448
|
+
// src/agent/request-types.ts
|
|
449
|
+
function addTurn(turn, event) {
|
|
450
|
+
if (event.type === "text_delta") {
|
|
451
|
+
return { type: "text_delta", turn, delta: event.delta };
|
|
452
|
+
}
|
|
453
|
+
if (event.type === "reasoning_delta") {
|
|
454
|
+
const mapped = { type: "reasoning_delta", turn, delta: event.delta };
|
|
455
|
+
if (event.id !== void 0) mapped.id = event.id;
|
|
456
|
+
if (event.contentType !== void 0) mapped.contentType = event.contentType;
|
|
457
|
+
if (event.signature !== void 0) mapped.signature = event.signature;
|
|
458
|
+
return mapped;
|
|
459
|
+
}
|
|
460
|
+
return { type: "tool_call", turn, toolCall: event.toolCall };
|
|
461
|
+
}
|
|
462
|
+
function isGenerationDeltaEvent(type) {
|
|
463
|
+
return type === "text_delta" || type === "reasoning_delta" || type === "tool_call_delta" || type === "tool_call";
|
|
464
|
+
}
|
|
465
|
+
|
|
433
466
|
// src/agent/retrieval.ts
|
|
434
467
|
async function fetchDynamicContext(agent, ragText) {
|
|
435
468
|
if (ragText === void 0 || ragText.length === 0 || agent.dynamicContexts.length === 0) {
|
|
@@ -449,11 +482,11 @@ async function fetchDynamicContext(agent, ragText) {
|
|
|
449
482
|
documents.push(formatted);
|
|
450
483
|
} else {
|
|
451
484
|
const metadata = formatMetadata(result.metadata);
|
|
452
|
-
documents.push({
|
|
485
|
+
documents.push(compact({
|
|
453
486
|
id: result.id,
|
|
454
487
|
text: typeof result.document === "string" ? result.document : JSON.stringify(result.document, null, 2),
|
|
455
|
-
|
|
456
|
-
});
|
|
488
|
+
additionalProps: metadata
|
|
489
|
+
}));
|
|
457
490
|
}
|
|
458
491
|
}
|
|
459
492
|
}
|
|
@@ -491,9 +524,6 @@ function formatMetadata(metadata) {
|
|
|
491
524
|
}
|
|
492
525
|
|
|
493
526
|
// src/agent/utils.ts
|
|
494
|
-
function isStreamingCompletionModel(model) {
|
|
495
|
-
return "streamCompletion" in model && typeof model.streamCompletion === "function";
|
|
496
|
-
}
|
|
497
527
|
function extractRagText(message) {
|
|
498
528
|
if (message.role === "user") {
|
|
499
529
|
return message.content.flatMap((item) => item.type === "text" ? [item.text] : []).join("\n");
|
|
@@ -745,15 +775,19 @@ function isEmptyToolArguments(value) {
|
|
|
745
775
|
// src/agent/tool-execution.ts
|
|
746
776
|
var MCP_TOOL_METADATA_KEY = /* @__PURE__ */ Symbol.for("anvia.mcp.tool.metadata");
|
|
747
777
|
var ToolCallExecutor = class {
|
|
748
|
-
constructor(agent, activeHook, concurrency, requestMiddlewares, cancel) {
|
|
778
|
+
constructor(agent, activeHook, approvals, runContext, concurrency, requestMiddlewares, cancel) {
|
|
749
779
|
this.agent = agent;
|
|
750
780
|
this.activeHook = activeHook;
|
|
781
|
+
this.approvals = approvals;
|
|
782
|
+
this.runContext = runContext;
|
|
751
783
|
this.concurrency = concurrency;
|
|
752
784
|
this.requestMiddlewares = requestMiddlewares;
|
|
753
785
|
this.cancel = cancel;
|
|
754
786
|
}
|
|
755
787
|
agent;
|
|
756
788
|
activeHook;
|
|
789
|
+
approvals;
|
|
790
|
+
runContext;
|
|
757
791
|
concurrency;
|
|
758
792
|
requestMiddlewares;
|
|
759
793
|
cancel;
|
|
@@ -774,16 +808,18 @@ var ToolCallExecutor = class {
|
|
|
774
808
|
(definition) => definition.name === toolCall.function.name
|
|
775
809
|
);
|
|
776
810
|
const toolMetadata = toolTraceMetadata(tool);
|
|
777
|
-
const toolObservers = await observation?.runObservers.startTool(
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
811
|
+
const toolObservers = await observation?.runObservers.startTool(
|
|
812
|
+
compact({
|
|
813
|
+
turn: observation.turn,
|
|
814
|
+
toolCall,
|
|
815
|
+
toolName: toolCall.function.name,
|
|
816
|
+
internalCallId,
|
|
817
|
+
args,
|
|
818
|
+
toolCallId: toolCall.callId,
|
|
819
|
+
toolDefinition,
|
|
820
|
+
toolMetadata
|
|
821
|
+
})
|
|
822
|
+
);
|
|
787
823
|
const callAction = await this.activeHook?.onToolCall?.({
|
|
788
824
|
...hookArgs,
|
|
789
825
|
tool: toolCallControl
|
|
@@ -799,18 +835,6 @@ var ToolCallExecutor = class {
|
|
|
799
835
|
);
|
|
800
836
|
throw this.cancel(callAction.reason);
|
|
801
837
|
}
|
|
802
|
-
if (callAction?.type === "approval_request") {
|
|
803
|
-
const reason = `Tool approval was requested for ${toolCall.function.name}, but no approval handler is installed.`;
|
|
804
|
-
await recordToolError(
|
|
805
|
-
toolObservers,
|
|
806
|
-
observation?.turn,
|
|
807
|
-
toolCall,
|
|
808
|
-
internalCallId,
|
|
809
|
-
args,
|
|
810
|
-
reason
|
|
811
|
-
);
|
|
812
|
-
throw this.cancel(reason);
|
|
813
|
-
}
|
|
814
838
|
let output;
|
|
815
839
|
let skipped = false;
|
|
816
840
|
let effectiveArgs = args;
|
|
@@ -818,49 +842,70 @@ var ToolCallExecutor = class {
|
|
|
818
842
|
output = callAction.reason;
|
|
819
843
|
skipped = true;
|
|
820
844
|
} else {
|
|
821
|
-
|
|
822
|
-
...hookArgs,
|
|
823
|
-
turn: observation?.turn ?? 0,
|
|
824
|
-
originalArgs: args
|
|
825
|
-
});
|
|
845
|
+
let approvalDecision;
|
|
826
846
|
try {
|
|
827
|
-
|
|
828
|
-
emitStreamEvent: async (event) => {
|
|
829
|
-
await toolObservers?.streamEvent({
|
|
830
|
-
turn: observation?.turn ?? 0,
|
|
831
|
-
toolCall,
|
|
832
|
-
toolName: toolCall.function.name,
|
|
833
|
-
internalCallId,
|
|
834
|
-
args: effectiveArgs,
|
|
835
|
-
...toolCall.callId === void 0 ? {} : { toolCallId: toolCall.callId },
|
|
836
|
-
event
|
|
837
|
-
});
|
|
838
|
-
const payload = agentToolEventPayload(toolCall, internalCallId, event);
|
|
839
|
-
if (payload !== void 0) {
|
|
840
|
-
onStreamEvent?.(payload);
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
});
|
|
847
|
+
approvalDecision = callAction?.type === "approval_request" ? await this.requestApproval(tool, hookArgs, callAction) : await this.evaluateToolApproval(tool, hookArgs) ?? { approved: true };
|
|
844
848
|
} catch (error) {
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
error,
|
|
849
|
-
run: runControl
|
|
850
|
-
});
|
|
851
|
-
await toolObservers?.error({
|
|
852
|
-
turn: observation?.turn ?? 0,
|
|
849
|
+
await recordToolError(
|
|
850
|
+
toolObservers,
|
|
851
|
+
observation?.turn,
|
|
853
852
|
toolCall,
|
|
854
|
-
toolName: toolCall.function.name,
|
|
855
853
|
internalCallId,
|
|
856
|
-
args
|
|
857
|
-
...toolCall.callId === void 0 ? {} : { toolCallId: toolCall.callId },
|
|
854
|
+
args,
|
|
858
855
|
error
|
|
856
|
+
);
|
|
857
|
+
throw error;
|
|
858
|
+
}
|
|
859
|
+
if (!approvalDecision.approved) {
|
|
860
|
+
output = approvalDecision.result;
|
|
861
|
+
skipped = true;
|
|
862
|
+
} else {
|
|
863
|
+
effectiveArgs = await this.runToolInputMiddlewares({
|
|
864
|
+
...hookArgs,
|
|
865
|
+
turn: observation?.turn ?? 0,
|
|
866
|
+
originalArgs: args
|
|
859
867
|
});
|
|
860
|
-
|
|
861
|
-
|
|
868
|
+
try {
|
|
869
|
+
output = await this.agent.callTool(toolCall.function.name, effectiveArgs, {
|
|
870
|
+
emitStreamEvent: async (event) => {
|
|
871
|
+
await toolObservers?.streamEvent(
|
|
872
|
+
compact({
|
|
873
|
+
turn: observation?.turn ?? 0,
|
|
874
|
+
toolCall,
|
|
875
|
+
toolName: toolCall.function.name,
|
|
876
|
+
internalCallId,
|
|
877
|
+
args: effectiveArgs,
|
|
878
|
+
toolCallId: toolCall.callId,
|
|
879
|
+
event
|
|
880
|
+
})
|
|
881
|
+
);
|
|
882
|
+
const payload = agentToolEventPayload(toolCall, internalCallId, event);
|
|
883
|
+
if (payload !== void 0) {
|
|
884
|
+
onStreamEvent?.(payload);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
});
|
|
888
|
+
} catch (error) {
|
|
889
|
+
const errorAction = await this.activeHook?.onToolError?.({
|
|
890
|
+
...hookArgs,
|
|
891
|
+
args: effectiveArgs,
|
|
892
|
+
error,
|
|
893
|
+
run: runControl
|
|
894
|
+
});
|
|
895
|
+
await toolObservers?.error({
|
|
896
|
+
turn: observation?.turn ?? 0,
|
|
897
|
+
toolCall,
|
|
898
|
+
toolName: toolCall.function.name,
|
|
899
|
+
internalCallId,
|
|
900
|
+
args: effectiveArgs,
|
|
901
|
+
...toolCall.callId !== void 0 && { toolCallId: toolCall.callId },
|
|
902
|
+
error
|
|
903
|
+
});
|
|
904
|
+
if (errorAction?.type === "terminate") {
|
|
905
|
+
throw this.cancel(errorAction.reason);
|
|
906
|
+
}
|
|
907
|
+
output = error instanceof Error ? error.toString() : String(error);
|
|
862
908
|
}
|
|
863
|
-
output = error instanceof Error ? error.toString() : String(error);
|
|
864
909
|
}
|
|
865
910
|
}
|
|
866
911
|
let result = toolOutputToText(output);
|
|
@@ -968,7 +1013,65 @@ var ToolCallExecutor = class {
|
|
|
968
1013
|
activeMiddlewares() {
|
|
969
1014
|
return [...this.agent.middlewares, ...this.requestMiddlewares];
|
|
970
1015
|
}
|
|
1016
|
+
async evaluateToolApproval(tool, hookArgs) {
|
|
1017
|
+
if (tool?.approval === void 0) {
|
|
1018
|
+
return void 0;
|
|
1019
|
+
}
|
|
1020
|
+
const policy = tool.approval;
|
|
1021
|
+
const context = approvalContext(tool, hookArgs, this.agent, this.runContext);
|
|
1022
|
+
const required = await policy.when(context);
|
|
1023
|
+
if (!required) {
|
|
1024
|
+
return { approved: true };
|
|
1025
|
+
}
|
|
1026
|
+
const reason = await resolveApprovalText(policy.reason, context);
|
|
1027
|
+
const rejectMessage = await resolveApprovalText(policy.rejectMessage, context);
|
|
1028
|
+
return this.requestApproval(tool, hookArgs, compact({ reason, rejectMessage }));
|
|
1029
|
+
}
|
|
1030
|
+
async requestApproval(tool, hookArgs, options) {
|
|
1031
|
+
const request = compact({
|
|
1032
|
+
...approvalContext(tool, hookArgs, this.agent, this.runContext),
|
|
1033
|
+
reason: options.reason,
|
|
1034
|
+
rejectMessage: options.rejectMessage
|
|
1035
|
+
});
|
|
1036
|
+
if (this.approvals === void 0) {
|
|
1037
|
+
throw new ToolApprovalRequiredError(request);
|
|
1038
|
+
}
|
|
1039
|
+
const decision = normalizeApprovalDecision(await this.approvals.handler(request));
|
|
1040
|
+
if (decision.approved) {
|
|
1041
|
+
return { approved: true };
|
|
1042
|
+
}
|
|
1043
|
+
return {
|
|
1044
|
+
approved: false,
|
|
1045
|
+
result: decision.rejectMessage ?? decision.reason ?? request.rejectMessage ?? "Tool approval was rejected."
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
971
1048
|
};
|
|
1049
|
+
function approvalContext(tool, hookArgs, agent, run) {
|
|
1050
|
+
const rawParsedArgs = parseToolArgs(hookArgs.args);
|
|
1051
|
+
const parsedArgs = tool?.parseApprovalArgs?.(rawParsedArgs) ?? rawParsedArgs;
|
|
1052
|
+
return compact({
|
|
1053
|
+
toolName: hookArgs.toolName,
|
|
1054
|
+
args: parsedArgs,
|
|
1055
|
+
rawArgs: hookArgs.args,
|
|
1056
|
+
toolCallId: hookArgs.toolCallId,
|
|
1057
|
+
internalCallId: hookArgs.internalCallId,
|
|
1058
|
+
run: compact({
|
|
1059
|
+
agentId: agent.id,
|
|
1060
|
+
runId: run.runId,
|
|
1061
|
+
sessionId: run.sessionId,
|
|
1062
|
+
metadata: run.metadata
|
|
1063
|
+
})
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
1066
|
+
function normalizeApprovalDecision(decision) {
|
|
1067
|
+
if (typeof decision === "boolean") {
|
|
1068
|
+
return { approved: decision };
|
|
1069
|
+
}
|
|
1070
|
+
return decision;
|
|
1071
|
+
}
|
|
1072
|
+
async function resolveApprovalText(value, context) {
|
|
1073
|
+
return typeof value === "function" ? value(context) : value;
|
|
1074
|
+
}
|
|
972
1075
|
function normalizeToolOutputMiddlewareResult(result) {
|
|
973
1076
|
if (typeof result === "string") {
|
|
974
1077
|
return { result };
|
|
@@ -1007,15 +1110,15 @@ function agentToolEventPayload(toolCall, internalCallId, event) {
|
|
|
1007
1110
|
if (typeof event.agentId !== "string" || event.agentId.length === 0) {
|
|
1008
1111
|
return void 0;
|
|
1009
1112
|
}
|
|
1010
|
-
return {
|
|
1113
|
+
return compact({
|
|
1011
1114
|
type: "agent_tool_event",
|
|
1012
1115
|
toolName: toolCall.function.name,
|
|
1013
|
-
|
|
1116
|
+
toolCallId: toolCall.callId,
|
|
1014
1117
|
internalCallId,
|
|
1015
1118
|
agentId: event.agentId,
|
|
1016
|
-
|
|
1119
|
+
agentName: event.agentName,
|
|
1017
1120
|
event: event.event
|
|
1018
|
-
};
|
|
1121
|
+
});
|
|
1019
1122
|
}
|
|
1020
1123
|
|
|
1021
1124
|
// src/agent/request.ts
|
|
@@ -1023,16 +1126,20 @@ var PromptRequest = class _PromptRequest {
|
|
|
1023
1126
|
constructor(agent, promptMessage, initialHistory = [], memoryContext = void 0) {
|
|
1024
1127
|
this.agent = agent;
|
|
1025
1128
|
this.promptMessage = promptMessage;
|
|
1129
|
+
this.memoryContext = memoryContext;
|
|
1026
1130
|
this.chatHistory = initialHistory;
|
|
1027
1131
|
this.maxTurnCount = agent.defaultMaxTurns ?? 0;
|
|
1028
1132
|
this.activeHook = agent.hook;
|
|
1133
|
+
this.approvalOptions = agent.approvals;
|
|
1029
1134
|
this.memoryRecorder = new PromptRequestMemory(agent, memoryContext, initialHistory);
|
|
1030
1135
|
}
|
|
1031
1136
|
agent;
|
|
1032
1137
|
promptMessage;
|
|
1138
|
+
memoryContext;
|
|
1033
1139
|
chatHistory;
|
|
1034
1140
|
maxTurnCount;
|
|
1035
1141
|
activeHook;
|
|
1142
|
+
approvalOptions;
|
|
1036
1143
|
concurrency = 1;
|
|
1037
1144
|
traceOptions;
|
|
1038
1145
|
requestMiddlewares = [];
|
|
@@ -1051,6 +1158,10 @@ var PromptRequest = class _PromptRequest {
|
|
|
1051
1158
|
this.activeHook = hook;
|
|
1052
1159
|
return this;
|
|
1053
1160
|
}
|
|
1161
|
+
approvals(options) {
|
|
1162
|
+
this.approvalOptions = options;
|
|
1163
|
+
return this;
|
|
1164
|
+
}
|
|
1054
1165
|
/**
|
|
1055
1166
|
* @deprecated Use `withHook` instead.
|
|
1056
1167
|
*/
|
|
@@ -1164,6 +1275,7 @@ var PromptRequest = class _PromptRequest {
|
|
|
1164
1275
|
return result;
|
|
1165
1276
|
}
|
|
1166
1277
|
const toolResults = await this.executeToolCalls(
|
|
1278
|
+
runId,
|
|
1167
1279
|
toolCalls,
|
|
1168
1280
|
newMessages,
|
|
1169
1281
|
void 0,
|
|
@@ -1236,16 +1348,18 @@ var PromptRequest = class _PromptRequest {
|
|
|
1236
1348
|
request = await this.runCompletionRequestMiddlewares(request, currentTurns);
|
|
1237
1349
|
assertCompletionRequestSupported(this.agent.model, request, { streaming: true });
|
|
1238
1350
|
const providerRequest = this.providerTraceRequest(request, { stream: true });
|
|
1239
|
-
const generationObservers = await runObservers.startGeneration(
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1351
|
+
const generationObservers = await runObservers.startGeneration(
|
|
1352
|
+
compact({
|
|
1353
|
+
turn: currentTurns,
|
|
1354
|
+
request,
|
|
1355
|
+
providerRequest,
|
|
1356
|
+
modelInfo: {
|
|
1357
|
+
provider: this.agent.model.provider,
|
|
1358
|
+
defaultModel: this.agent.model.defaultModel,
|
|
1359
|
+
capabilities: this.agent.model.capabilities
|
|
1360
|
+
}
|
|
1361
|
+
})
|
|
1362
|
+
);
|
|
1249
1363
|
const accumulator = new CompletionStreamAccumulator();
|
|
1250
1364
|
const generationStartedAt = Date.now();
|
|
1251
1365
|
let firstDeltaMs;
|
|
@@ -1268,11 +1382,13 @@ var PromptRequest = class _PromptRequest {
|
|
|
1268
1382
|
throw error;
|
|
1269
1383
|
}
|
|
1270
1384
|
let response = accumulator.response();
|
|
1271
|
-
await generationObservers.end(
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1385
|
+
await generationObservers.end(
|
|
1386
|
+
compact({
|
|
1387
|
+
turn: currentTurns,
|
|
1388
|
+
response,
|
|
1389
|
+
firstDeltaMs
|
|
1390
|
+
})
|
|
1391
|
+
);
|
|
1276
1392
|
response = await this.runCompletionResponseMiddlewares(request, response, currentTurns);
|
|
1277
1393
|
usage = Usage.add(usage, response.usage);
|
|
1278
1394
|
await this.runCompletionResponseHook(prompt, response, newMessages);
|
|
@@ -1319,6 +1435,7 @@ var PromptRequest = class _PromptRequest {
|
|
|
1319
1435
|
}
|
|
1320
1436
|
const toolResultEvents = createAsyncQueue();
|
|
1321
1437
|
const toolResultsPromise = this.executeToolCalls(
|
|
1438
|
+
runId,
|
|
1322
1439
|
toolCalls,
|
|
1323
1440
|
newMessages,
|
|
1324
1441
|
(result) => {
|
|
@@ -1368,16 +1485,18 @@ var PromptRequest = class _PromptRequest {
|
|
|
1368
1485
|
async runCompletion(request, turn, runObservers) {
|
|
1369
1486
|
assertCompletionRequestSupported(this.agent.model, request);
|
|
1370
1487
|
const providerRequest = this.providerTraceRequest(request);
|
|
1371
|
-
const generationObservers = await runObservers.startGeneration(
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1488
|
+
const generationObservers = await runObservers.startGeneration(
|
|
1489
|
+
compact({
|
|
1490
|
+
turn,
|
|
1491
|
+
request,
|
|
1492
|
+
providerRequest,
|
|
1493
|
+
modelInfo: {
|
|
1494
|
+
provider: this.agent.model.provider,
|
|
1495
|
+
defaultModel: this.agent.model.defaultModel,
|
|
1496
|
+
capabilities: this.agent.model.capabilities
|
|
1497
|
+
}
|
|
1498
|
+
})
|
|
1499
|
+
);
|
|
1381
1500
|
try {
|
|
1382
1501
|
const response = await this.agent.model.completion(request);
|
|
1383
1502
|
await generationObservers.end({ turn, response });
|
|
@@ -1396,10 +1515,16 @@ var PromptRequest = class _PromptRequest {
|
|
|
1396
1515
|
};
|
|
1397
1516
|
}
|
|
1398
1517
|
}
|
|
1399
|
-
async executeToolCalls(toolCalls, newMessages, onResult, onStreamEvent, observation) {
|
|
1518
|
+
async executeToolCalls(runId, toolCalls, newMessages, onResult, onStreamEvent, observation) {
|
|
1400
1519
|
const executor = new ToolCallExecutor(
|
|
1401
1520
|
this.agent,
|
|
1402
1521
|
this.activeHook,
|
|
1522
|
+
this.approvalOptions,
|
|
1523
|
+
{
|
|
1524
|
+
runId,
|
|
1525
|
+
sessionId: this.memoryContext?.sessionId,
|
|
1526
|
+
metadata: this.memoryContext?.metadata
|
|
1527
|
+
},
|
|
1403
1528
|
this.concurrency,
|
|
1404
1529
|
this.requestMiddlewares,
|
|
1405
1530
|
(reason) => this.cancelled(newMessages, reason)
|
|
@@ -1436,13 +1561,12 @@ var PromptRequest = class _PromptRequest {
|
|
|
1436
1561
|
await registration.store.append({
|
|
1437
1562
|
runId,
|
|
1438
1563
|
agentId,
|
|
1439
|
-
...
|
|
1440
|
-
...
|
|
1441
|
-
...event.type === "agent_tool_event" ? {
|
|
1564
|
+
...compact({ agentName, turn }),
|
|
1565
|
+
...event.type === "agent_tool_event" ? compact({
|
|
1442
1566
|
toolName: event.toolName,
|
|
1443
|
-
|
|
1567
|
+
toolCallId: event.toolCallId,
|
|
1444
1568
|
internalCallId: event.internalCallId
|
|
1445
|
-
} : {},
|
|
1569
|
+
}) : {},
|
|
1446
1570
|
event
|
|
1447
1571
|
});
|
|
1448
1572
|
}
|
|
@@ -1609,22 +1733,6 @@ function normalizeSteeringInput(input) {
|
|
|
1609
1733
|
}
|
|
1610
1734
|
return Array.isArray(input) ? [...input] : [input];
|
|
1611
1735
|
}
|
|
1612
|
-
function addTurn(turn, event) {
|
|
1613
|
-
if (event.type === "text_delta") {
|
|
1614
|
-
return { type: "text_delta", turn, delta: event.delta };
|
|
1615
|
-
}
|
|
1616
|
-
if (event.type === "reasoning_delta") {
|
|
1617
|
-
const mapped = { type: "reasoning_delta", turn, delta: event.delta };
|
|
1618
|
-
if (event.id !== void 0) mapped.id = event.id;
|
|
1619
|
-
if (event.contentType !== void 0) mapped.contentType = event.contentType;
|
|
1620
|
-
if (event.signature !== void 0) mapped.signature = event.signature;
|
|
1621
|
-
return mapped;
|
|
1622
|
-
}
|
|
1623
|
-
return { type: "tool_call", turn, toolCall: event.toolCall };
|
|
1624
|
-
}
|
|
1625
|
-
function isGenerationDeltaEvent(type) {
|
|
1626
|
-
return type === "text_delta" || type === "reasoning_delta" || type === "tool_call_delta" || type === "tool_call";
|
|
1627
|
-
}
|
|
1628
1736
|
|
|
1629
1737
|
// src/agent/agent.ts
|
|
1630
1738
|
var DEFAULT_MAX_TURNS = 20;
|
|
@@ -1644,6 +1752,7 @@ var Agent = class {
|
|
|
1644
1752
|
hook;
|
|
1645
1753
|
outputSchema;
|
|
1646
1754
|
observers;
|
|
1755
|
+
approvals;
|
|
1647
1756
|
dynamicContexts;
|
|
1648
1757
|
dynamicTools;
|
|
1649
1758
|
middlewares;
|
|
@@ -1669,6 +1778,7 @@ var Agent = class {
|
|
|
1669
1778
|
this.hook = options.hook;
|
|
1670
1779
|
this.outputSchema = options.outputSchema;
|
|
1671
1780
|
this.observers = options.observers ?? [];
|
|
1781
|
+
this.approvals = options.approvals;
|
|
1672
1782
|
this.dynamicContexts = options.dynamicContexts ?? [];
|
|
1673
1783
|
this.dynamicTools = options.dynamicTools ?? [];
|
|
1674
1784
|
this.middlewares = options.middlewares ?? options.toolMiddlewares ?? [];
|
|
@@ -1689,8 +1799,8 @@ var Agent = class {
|
|
|
1689
1799
|
}
|
|
1690
1800
|
return new AgentSession(this, {
|
|
1691
1801
|
sessionId: normalized,
|
|
1692
|
-
...options.userId
|
|
1693
|
-
...options.metadata
|
|
1802
|
+
...options.userId !== void 0 && { userId: options.userId },
|
|
1803
|
+
...options.metadata !== void 0 && { metadata: options.metadata }
|
|
1694
1804
|
});
|
|
1695
1805
|
}
|
|
1696
1806
|
asTool(options) {
|
|
@@ -1708,11 +1818,13 @@ var Agent = class {
|
|
|
1708
1818
|
if (options.stream === true && context.emitStreamEvent !== void 0 && this.model.capabilities.streaming && isStreamingCompletionModel(this.model)) {
|
|
1709
1819
|
let output = "";
|
|
1710
1820
|
for await (const event of childRequest.stream()) {
|
|
1711
|
-
await context.emitStreamEvent(
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1821
|
+
await context.emitStreamEvent(
|
|
1822
|
+
compact({
|
|
1823
|
+
agentId: this.id,
|
|
1824
|
+
agentName: this.name,
|
|
1825
|
+
event
|
|
1826
|
+
})
|
|
1827
|
+
);
|
|
1716
1828
|
if (event.type === "final") {
|
|
1717
1829
|
output = event.output;
|
|
1718
1830
|
}
|
|
@@ -1790,14 +1902,16 @@ export {
|
|
|
1790
1902
|
normalizeAgentId,
|
|
1791
1903
|
MaxTurnsError,
|
|
1792
1904
|
PromptCancelledError,
|
|
1905
|
+
ToolApprovalRequiredError,
|
|
1793
1906
|
createHook,
|
|
1794
1907
|
cancelPrompt,
|
|
1795
1908
|
skipTool,
|
|
1796
1909
|
requestToolApproval,
|
|
1797
1910
|
runControl,
|
|
1798
1911
|
toolCallControl,
|
|
1912
|
+
extractRagText,
|
|
1799
1913
|
DEFAULT_MAX_TURNS,
|
|
1800
1914
|
Agent,
|
|
1801
1915
|
AgentSession
|
|
1802
1916
|
};
|
|
1803
|
-
//# sourceMappingURL=chunk-
|
|
1917
|
+
//# sourceMappingURL=chunk-6OR42QTG.js.map
|