@anvia/core 0.11.3 → 0.12.1
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 +6 -3
- package/dist/agent/index.js +8 -7
- package/dist/{agent-Bcpuodu2.d.ts → agent-BBj3jvBi.d.ts} +11 -2
- package/dist/{chunk-7CQAERL3.js → chunk-CO7YK4JZ.js} +333 -103
- package/dist/chunk-CO7YK4JZ.js.map +1 -0
- package/dist/chunk-CWUJUSOS.js +291 -0
- package/dist/chunk-CWUJUSOS.js.map +1 -0
- package/dist/{chunk-FV5B4SGK.js → chunk-GISFLLNL.js} +5 -5
- package/dist/{chunk-V3WTT7TE.js → chunk-HF4KHHNM.js} +2 -2
- package/dist/{chunk-5EDHXYH5.js → chunk-IZM6FNCT.js} +8 -5
- package/dist/chunk-IZM6FNCT.js.map +1 -0
- package/dist/{chunk-MLMXQ2OV.js → chunk-J4XZCE4A.js} +9 -7
- package/dist/chunk-J4XZCE4A.js.map +1 -0
- package/dist/{chunk-2GXGB6U5.js → chunk-L62HQ5Y4.js} +4 -4
- package/dist/{chunk-NI637AGP.js → chunk-MX6ATKGT.js} +12 -3
- package/dist/chunk-MX6ATKGT.js.map +1 -0
- package/dist/evals/index.d.ts +3 -2
- package/dist/evals/index.js +11 -10
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.d.ts +3 -2
- package/dist/extractor/index.js +9 -8
- package/dist/guardrails/index.d.ts +133 -0
- package/dist/guardrails/index.js +27 -0
- package/dist/guardrails/index.js.map +1 -0
- package/dist/{index-CD9jb9Or.d.ts → index-Oucb3Ljj.d.ts} +7 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +28 -14
- package/dist/internal/agent.d.ts +3 -2
- package/dist/internal/agent.js +7 -6
- package/dist/observability/index.d.ts +2 -1
- package/dist/pipeline/index.d.ts +3 -2
- package/dist/pipeline/index.js +3 -3
- package/dist/request/index.d.ts +3 -2
- package/dist/request/index.js +6 -5
- package/dist/skills/index.js +4 -4
- package/dist/tool/index.js +3 -3
- package/dist/vector-store/index.js +2 -2
- package/package.json +6 -2
- package/dist/chunk-5EDHXYH5.js.map +0 -1
- package/dist/chunk-7CQAERL3.js.map +0 -1
- package/dist/chunk-MLMXQ2OV.js.map +0 -1
- package/dist/chunk-NI637AGP.js.map +0 -1
- /package/dist/{chunk-FV5B4SGK.js.map → chunk-GISFLLNL.js.map} +0 -0
- /package/dist/{chunk-V3WTT7TE.js.map → chunk-HF4KHHNM.js.map} +0 -0
- /package/dist/{chunk-2GXGB6U5.js.map → chunk-L62HQ5Y4.js.map} +0 -0
|
@@ -4,7 +4,14 @@ import {
|
|
|
4
4
|
import {
|
|
5
5
|
parseToolArgs,
|
|
6
6
|
toolResultContentToText
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-GISFLLNL.js";
|
|
8
|
+
import {
|
|
9
|
+
runControl,
|
|
10
|
+
toolCallControl
|
|
11
|
+
} from "./chunk-NPZDYOE6.js";
|
|
12
|
+
import {
|
|
13
|
+
compact
|
|
14
|
+
} from "./chunk-MMHG7WAM.js";
|
|
8
15
|
import {
|
|
9
16
|
mapWithConcurrency
|
|
10
17
|
} from "./chunk-OIMLU4SF.js";
|
|
@@ -13,6 +20,7 @@ import {
|
|
|
13
20
|
isStreamingCompletionModel
|
|
14
21
|
} from "./chunk-SRBW3LD7.js";
|
|
15
22
|
import {
|
|
23
|
+
AssistantContent,
|
|
16
24
|
Message,
|
|
17
25
|
ToolContent,
|
|
18
26
|
Usage,
|
|
@@ -20,12 +28,11 @@ import {
|
|
|
20
28
|
textFromAssistantContent
|
|
21
29
|
} from "./chunk-5LFGEMIW.js";
|
|
22
30
|
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
} from "./chunk-MMHG7WAM.js";
|
|
31
|
+
appendGuardrailPolicies,
|
|
32
|
+
hasEnforcedOutputGuardrails,
|
|
33
|
+
runInputGuardrails,
|
|
34
|
+
runOutputGuardrails
|
|
35
|
+
} from "./chunk-CWUJUSOS.js";
|
|
29
36
|
|
|
30
37
|
// src/request/errors.ts
|
|
31
38
|
var MaxTurnsError = class extends Error {
|
|
@@ -585,6 +592,9 @@ var ToolCallExecutor = class {
|
|
|
585
592
|
toolMetadata
|
|
586
593
|
})
|
|
587
594
|
);
|
|
595
|
+
let output;
|
|
596
|
+
let skipped = false;
|
|
597
|
+
let effectiveArgs = args;
|
|
588
598
|
const callAction = await this.activeHook?.onToolCall?.({
|
|
589
599
|
...hookArgs,
|
|
590
600
|
tool: toolCallControl
|
|
@@ -595,14 +605,11 @@ var ToolCallExecutor = class {
|
|
|
595
605
|
observation?.turn,
|
|
596
606
|
toolCall,
|
|
597
607
|
internalCallId,
|
|
598
|
-
|
|
608
|
+
effectiveArgs,
|
|
599
609
|
callAction.reason
|
|
600
610
|
);
|
|
601
611
|
throw this.cancel(callAction.reason);
|
|
602
612
|
}
|
|
603
|
-
let output;
|
|
604
|
-
let skipped = false;
|
|
605
|
-
let effectiveArgs = args;
|
|
606
613
|
if (callAction?.type === "skip") {
|
|
607
614
|
output = callAction.reason;
|
|
608
615
|
skipped = true;
|
|
@@ -614,8 +621,24 @@ var ToolCallExecutor = class {
|
|
|
614
621
|
turn: observation?.turn ?? 0,
|
|
615
622
|
originalArgs: args
|
|
616
623
|
});
|
|
617
|
-
|
|
618
|
-
approvalDecision = callAction?.type === "approval_request" ? await this.requestApproval(tool,
|
|
624
|
+
hookArgs.args = effectiveArgs;
|
|
625
|
+
approvalDecision = callAction?.type === "approval_request" ? await this.requestApproval(tool, hookArgs, callAction) : await this.evaluateToolApproval(tool, hookArgs) ?? { approved: true };
|
|
626
|
+
if (!approvalDecision.approved) {
|
|
627
|
+
output = approvalDecision.result;
|
|
628
|
+
skipped = true;
|
|
629
|
+
} else {
|
|
630
|
+
output = await this.runApprovedToolCall(
|
|
631
|
+
toolCall,
|
|
632
|
+
hookArgs,
|
|
633
|
+
effectiveArgs,
|
|
634
|
+
args,
|
|
635
|
+
toolObservers,
|
|
636
|
+
observation,
|
|
637
|
+
onStreamEvent,
|
|
638
|
+
false
|
|
639
|
+
);
|
|
640
|
+
effectiveArgs = hookArgs.args;
|
|
641
|
+
}
|
|
619
642
|
} catch (error) {
|
|
620
643
|
await recordToolError(
|
|
621
644
|
toolObservers,
|
|
@@ -627,52 +650,6 @@ var ToolCallExecutor = class {
|
|
|
627
650
|
);
|
|
628
651
|
throw error;
|
|
629
652
|
}
|
|
630
|
-
if (!approvalDecision.approved) {
|
|
631
|
-
output = approvalDecision.result;
|
|
632
|
-
skipped = true;
|
|
633
|
-
} else {
|
|
634
|
-
try {
|
|
635
|
-
output = await this.agent.callTool(toolCall.function.name, effectiveArgs, {
|
|
636
|
-
emitStreamEvent: async (event) => {
|
|
637
|
-
await toolObservers?.streamEvent(
|
|
638
|
-
compact({
|
|
639
|
-
turn: observation?.turn ?? 0,
|
|
640
|
-
toolCall,
|
|
641
|
-
toolName: toolCall.function.name,
|
|
642
|
-
internalCallId,
|
|
643
|
-
args: effectiveArgs,
|
|
644
|
-
toolCallId: toolCall.callId,
|
|
645
|
-
event
|
|
646
|
-
})
|
|
647
|
-
);
|
|
648
|
-
const payload = agentToolEventPayload(toolCall, internalCallId, event);
|
|
649
|
-
if (payload !== void 0) {
|
|
650
|
-
onStreamEvent?.(payload);
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
});
|
|
654
|
-
} catch (error) {
|
|
655
|
-
const errorAction = await this.activeHook?.onToolError?.({
|
|
656
|
-
...hookArgs,
|
|
657
|
-
args: effectiveArgs,
|
|
658
|
-
error,
|
|
659
|
-
run: runControl
|
|
660
|
-
});
|
|
661
|
-
await toolObservers?.error({
|
|
662
|
-
turn: observation?.turn ?? 0,
|
|
663
|
-
toolCall,
|
|
664
|
-
toolName: toolCall.function.name,
|
|
665
|
-
internalCallId,
|
|
666
|
-
args: effectiveArgs,
|
|
667
|
-
...toolCall.callId !== void 0 && { toolCallId: toolCall.callId },
|
|
668
|
-
error
|
|
669
|
-
});
|
|
670
|
-
if (errorAction?.type === "terminate") {
|
|
671
|
-
throw this.cancel(errorAction.reason);
|
|
672
|
-
}
|
|
673
|
-
output = error instanceof Error ? error.toString() : String(error);
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
653
|
}
|
|
677
654
|
let result = toolOutputToText(output);
|
|
678
655
|
let structuredResult = toolOutputToStructuredResult(output);
|
|
@@ -728,6 +705,56 @@ var ToolCallExecutor = class {
|
|
|
728
705
|
return ToolContent.toolResult(toolCall.id, output, toolCall.callId);
|
|
729
706
|
});
|
|
730
707
|
}
|
|
708
|
+
async runApprovedToolCall(toolCall, hookArgs, effectiveArgs, originalArgs, toolObservers, observation, onStreamEvent, applyInputMiddleware = true) {
|
|
709
|
+
const middlewareArgs = applyInputMiddleware ? await this.runToolInputMiddlewares({
|
|
710
|
+
...hookArgs,
|
|
711
|
+
args: effectiveArgs,
|
|
712
|
+
turn: observation?.turn ?? 0,
|
|
713
|
+
originalArgs
|
|
714
|
+
}) : effectiveArgs;
|
|
715
|
+
hookArgs.args = middlewareArgs;
|
|
716
|
+
try {
|
|
717
|
+
return await this.agent.callTool(toolCall.function.name, middlewareArgs, {
|
|
718
|
+
emitStreamEvent: async (event) => {
|
|
719
|
+
await toolObservers?.streamEvent(
|
|
720
|
+
compact({
|
|
721
|
+
turn: observation?.turn ?? 0,
|
|
722
|
+
toolCall,
|
|
723
|
+
toolName: toolCall.function.name,
|
|
724
|
+
internalCallId: hookArgs.internalCallId,
|
|
725
|
+
args: middlewareArgs,
|
|
726
|
+
toolCallId: toolCall.callId,
|
|
727
|
+
event
|
|
728
|
+
})
|
|
729
|
+
);
|
|
730
|
+
const payload = agentToolEventPayload(toolCall, hookArgs.internalCallId, event);
|
|
731
|
+
if (payload !== void 0) {
|
|
732
|
+
onStreamEvent?.(payload);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
} catch (error) {
|
|
737
|
+
const errorAction = await this.activeHook?.onToolError?.({
|
|
738
|
+
...hookArgs,
|
|
739
|
+
args: middlewareArgs,
|
|
740
|
+
error,
|
|
741
|
+
run: runControl
|
|
742
|
+
});
|
|
743
|
+
await toolObservers?.error({
|
|
744
|
+
turn: observation?.turn ?? 0,
|
|
745
|
+
toolCall,
|
|
746
|
+
toolName: toolCall.function.name,
|
|
747
|
+
internalCallId: hookArgs.internalCallId,
|
|
748
|
+
args: middlewareArgs,
|
|
749
|
+
...toolCall.callId !== void 0 && { toolCallId: toolCall.callId },
|
|
750
|
+
error
|
|
751
|
+
});
|
|
752
|
+
if (errorAction?.type === "terminate") {
|
|
753
|
+
throw this.cancel(errorAction.reason);
|
|
754
|
+
}
|
|
755
|
+
return error instanceof Error ? error.toString() : String(error);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
731
758
|
async runToolResultMiddlewares(args) {
|
|
732
759
|
let result = args.result;
|
|
733
760
|
let structuredResult = args.structuredResult;
|
|
@@ -1100,6 +1127,7 @@ var PromptRequest = class _PromptRequest {
|
|
|
1100
1127
|
this.maxTurnCount = agent.defaultMaxTurns ?? 0;
|
|
1101
1128
|
this.activeHook = agent.hook;
|
|
1102
1129
|
this.approvalOptions = agent.approvals;
|
|
1130
|
+
this.guardrailPolicies = [...agent.guardrails];
|
|
1103
1131
|
this.memoryRecorder = new PromptRequestMemory(agent, memoryContext, initialHistory);
|
|
1104
1132
|
}
|
|
1105
1133
|
agent;
|
|
@@ -1109,6 +1137,8 @@ var PromptRequest = class _PromptRequest {
|
|
|
1109
1137
|
maxTurnCount;
|
|
1110
1138
|
activeHook;
|
|
1111
1139
|
approvalOptions;
|
|
1140
|
+
guardrailPolicies;
|
|
1141
|
+
guardrailDecisions = [];
|
|
1112
1142
|
concurrency = 1;
|
|
1113
1143
|
traceOptions;
|
|
1114
1144
|
requestMiddlewares = [];
|
|
@@ -1131,6 +1161,10 @@ var PromptRequest = class _PromptRequest {
|
|
|
1131
1161
|
this.approvalOptions = options;
|
|
1132
1162
|
return this;
|
|
1133
1163
|
}
|
|
1164
|
+
guardrails(policies) {
|
|
1165
|
+
this.guardrailPolicies = appendGuardrailPolicies(this.guardrailPolicies, policies);
|
|
1166
|
+
return this;
|
|
1167
|
+
}
|
|
1134
1168
|
/**
|
|
1135
1169
|
* @deprecated Use `withHook` instead.
|
|
1136
1170
|
*/
|
|
@@ -1175,14 +1209,37 @@ var PromptRequest = class _PromptRequest {
|
|
|
1175
1209
|
async send() {
|
|
1176
1210
|
this.startRun();
|
|
1177
1211
|
const runId = globalThis.crypto.randomUUID();
|
|
1178
|
-
const newMessages = [this.promptMessage];
|
|
1179
|
-
this.chatHistory = await this.memoryRecorder.prepareRun(runId, newMessages);
|
|
1180
|
-
const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
1181
1212
|
let usage = Usage.empty();
|
|
1182
1213
|
let currentTurns = 0;
|
|
1183
1214
|
let lastPrompt = this.promptMessage;
|
|
1215
|
+
let newMessages = [this.promptMessage];
|
|
1184
1216
|
const runObservers = await this.startRunObservers();
|
|
1185
1217
|
try {
|
|
1218
|
+
const inputResult = await runInputGuardrails(this.guardrailPolicies, {
|
|
1219
|
+
prompt: this.promptMessage,
|
|
1220
|
+
history: this.chatHistory,
|
|
1221
|
+
inputText: textFromMessage(this.promptMessage),
|
|
1222
|
+
run: this.guardrailRunContext(runId)
|
|
1223
|
+
});
|
|
1224
|
+
for (const decision of inputResult.decisions) {
|
|
1225
|
+
await this.recordGuardrailDecision(decision, runObservers);
|
|
1226
|
+
}
|
|
1227
|
+
this.promptMessage = inputResult.prompt;
|
|
1228
|
+
if (inputResult.blocked) {
|
|
1229
|
+
const output = inputResult.message ?? "The request was blocked by a guardrail.";
|
|
1230
|
+
const result = {
|
|
1231
|
+
output,
|
|
1232
|
+
usage: Usage.empty(),
|
|
1233
|
+
messages: [this.promptMessage, Message.assistant(output)],
|
|
1234
|
+
guardrails: [...this.guardrailDecisions]
|
|
1235
|
+
};
|
|
1236
|
+
await runObservers.end(result);
|
|
1237
|
+
this.runState = "completed";
|
|
1238
|
+
return result;
|
|
1239
|
+
}
|
|
1240
|
+
newMessages = [this.promptMessage];
|
|
1241
|
+
this.chatHistory = await this.memoryRecorder.prepareRun(runId, newMessages);
|
|
1242
|
+
const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
1186
1243
|
await this.runRunStartHook(newMessages);
|
|
1187
1244
|
while (currentTurns <= this.maxTurnCount + 1) {
|
|
1188
1245
|
const prompt = newMessages.at(-1);
|
|
@@ -1213,27 +1270,46 @@ var PromptRequest = class _PromptRequest {
|
|
|
1213
1270
|
usage = Usage.add(usage, response.usage);
|
|
1214
1271
|
await this.runCompletionResponseHook(prompt, response, newMessages);
|
|
1215
1272
|
await this.runTurnEndHook(currentTurns, response, newMessages);
|
|
1216
|
-
const assistantMessage = Message.assistant(response.choice, response.messageId);
|
|
1217
|
-
newMessages.push(assistantMessage);
|
|
1218
|
-
await this.memoryRecorder.commitMessages(
|
|
1219
|
-
runId,
|
|
1220
|
-
currentTurns,
|
|
1221
|
-
[assistantMessage],
|
|
1222
|
-
pendingTurnMessages
|
|
1223
|
-
);
|
|
1224
1273
|
const toolCalls = response.choice.filter(
|
|
1225
1274
|
(item) => item.type === "tool_call"
|
|
1226
1275
|
);
|
|
1276
|
+
const assistantMessage = Message.assistant(response.choice, response.messageId);
|
|
1277
|
+
newMessages.push(assistantMessage);
|
|
1227
1278
|
if (toolCalls.length === 0) {
|
|
1279
|
+
if (this.steeringMessages.length > 0) {
|
|
1280
|
+
await this.memoryRecorder.commitMessages(
|
|
1281
|
+
runId,
|
|
1282
|
+
currentTurns,
|
|
1283
|
+
[assistantMessage],
|
|
1284
|
+
pendingTurnMessages
|
|
1285
|
+
);
|
|
1286
|
+
}
|
|
1228
1287
|
if (await this.drainSteeringMessages(runId, currentTurns, newMessages, pendingTurnMessages)) {
|
|
1229
1288
|
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
1230
1289
|
continue;
|
|
1231
1290
|
}
|
|
1291
|
+
const guardedOutput = await this.runOutputGuardrailsForResponse(
|
|
1292
|
+
runId,
|
|
1293
|
+
usage,
|
|
1294
|
+
response,
|
|
1295
|
+
newMessages,
|
|
1296
|
+
runObservers
|
|
1297
|
+
);
|
|
1298
|
+
response = guardedOutput.response;
|
|
1299
|
+
const finalAssistantMessage = Message.assistant(response.choice, response.messageId);
|
|
1300
|
+
newMessages[newMessages.length - 1] = finalAssistantMessage;
|
|
1301
|
+
await this.memoryRecorder.commitMessages(
|
|
1302
|
+
runId,
|
|
1303
|
+
currentTurns,
|
|
1304
|
+
[finalAssistantMessage],
|
|
1305
|
+
pendingTurnMessages
|
|
1306
|
+
);
|
|
1232
1307
|
const result = {
|
|
1233
|
-
output:
|
|
1308
|
+
output: guardedOutput.output,
|
|
1234
1309
|
usage,
|
|
1235
1310
|
messages: [...newMessages],
|
|
1236
|
-
trace: runObservers.trace
|
|
1311
|
+
trace: runObservers.trace,
|
|
1312
|
+
guardrails: [...this.guardrailDecisions]
|
|
1237
1313
|
};
|
|
1238
1314
|
await this.runRunEndHook(result, newMessages);
|
|
1239
1315
|
await runObservers.end(result);
|
|
@@ -1246,6 +1322,12 @@ var PromptRequest = class _PromptRequest {
|
|
|
1246
1322
|
this.runState = "completed";
|
|
1247
1323
|
return result;
|
|
1248
1324
|
}
|
|
1325
|
+
await this.memoryRecorder.commitMessages(
|
|
1326
|
+
runId,
|
|
1327
|
+
currentTurns,
|
|
1328
|
+
[assistantMessage],
|
|
1329
|
+
pendingTurnMessages
|
|
1330
|
+
);
|
|
1249
1331
|
const toolResults = await this.executeToolCalls(
|
|
1250
1332
|
runId,
|
|
1251
1333
|
toolCalls,
|
|
@@ -1284,18 +1366,51 @@ var PromptRequest = class _PromptRequest {
|
|
|
1284
1366
|
}
|
|
1285
1367
|
this.startRun();
|
|
1286
1368
|
const runId = globalThis.crypto.randomUUID();
|
|
1287
|
-
const newMessages = [this.promptMessage];
|
|
1288
|
-
this.chatHistory = await this.memoryRecorder.prepareRun(runId, newMessages);
|
|
1289
|
-
const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
1290
|
-
let usage = Usage.empty();
|
|
1291
|
-
let currentTurns = 0;
|
|
1292
|
-
let lastPrompt = this.promptMessage;
|
|
1293
|
-
const runObservers = await this.startRunObservers();
|
|
1294
1369
|
const emit = async (event) => {
|
|
1295
1370
|
await this.recordAgentEvent(runId, event);
|
|
1296
1371
|
return event;
|
|
1297
1372
|
};
|
|
1373
|
+
let usage = Usage.empty();
|
|
1374
|
+
let currentTurns = 0;
|
|
1375
|
+
let lastPrompt = this.promptMessage;
|
|
1376
|
+
let newMessages = [this.promptMessage];
|
|
1377
|
+
const runObservers = await this.startRunObservers();
|
|
1378
|
+
const bufferOutputDeltas = hasEnforcedOutputGuardrails(this.guardrailPolicies);
|
|
1298
1379
|
try {
|
|
1380
|
+
const inputResult = await runInputGuardrails(this.guardrailPolicies, {
|
|
1381
|
+
prompt: this.promptMessage,
|
|
1382
|
+
history: this.chatHistory,
|
|
1383
|
+
inputText: textFromMessage(this.promptMessage),
|
|
1384
|
+
run: this.guardrailRunContext(runId)
|
|
1385
|
+
});
|
|
1386
|
+
for (const decision of inputResult.decisions) {
|
|
1387
|
+
await this.recordGuardrailDecision(decision, runObservers);
|
|
1388
|
+
yield await emit({ type: "guardrail_decision", decision });
|
|
1389
|
+
}
|
|
1390
|
+
this.promptMessage = inputResult.prompt;
|
|
1391
|
+
if (inputResult.blocked) {
|
|
1392
|
+
const output = inputResult.message ?? "The request was blocked by a guardrail.";
|
|
1393
|
+
const result = {
|
|
1394
|
+
output,
|
|
1395
|
+
usage: Usage.empty(),
|
|
1396
|
+
messages: [this.promptMessage, Message.assistant(output)],
|
|
1397
|
+
guardrails: [...this.guardrailDecisions]
|
|
1398
|
+
};
|
|
1399
|
+
await runObservers.end(result);
|
|
1400
|
+
this.runState = "completed";
|
|
1401
|
+
yield await emit({
|
|
1402
|
+
type: "final",
|
|
1403
|
+
runId,
|
|
1404
|
+
output: result.output,
|
|
1405
|
+
usage: result.usage,
|
|
1406
|
+
messages: result.messages,
|
|
1407
|
+
guardrails: result.guardrails
|
|
1408
|
+
});
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
newMessages = [this.promptMessage];
|
|
1412
|
+
this.chatHistory = await this.memoryRecorder.prepareRun(runId, newMessages);
|
|
1413
|
+
const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
1299
1414
|
await this.runRunStartHook(newMessages);
|
|
1300
1415
|
while (currentTurns <= this.maxTurnCount + 1) {
|
|
1301
1416
|
const prompt = newMessages.at(-1);
|
|
@@ -1354,7 +1469,8 @@ var PromptRequest = class _PromptRequest {
|
|
|
1354
1469
|
if (mapped.type === "tool_call") {
|
|
1355
1470
|
emittedToolCallIds.add(mapped.toolCall.id);
|
|
1356
1471
|
}
|
|
1357
|
-
|
|
1472
|
+
const shouldBuffer = bufferResponseEvents || bufferOutputDeltas && (mapped.type === "text_delta" || mapped.type === "reasoning_delta");
|
|
1473
|
+
if (!shouldBuffer) {
|
|
1358
1474
|
yield await emit(addTurn(currentTurns, mapped));
|
|
1359
1475
|
}
|
|
1360
1476
|
}
|
|
@@ -1376,39 +1492,67 @@ var PromptRequest = class _PromptRequest {
|
|
|
1376
1492
|
usage = Usage.add(usage, response.usage);
|
|
1377
1493
|
await this.runCompletionResponseHook(prompt, response, newMessages);
|
|
1378
1494
|
await this.runTurnEndHook(currentTurns, response, newMessages);
|
|
1379
|
-
const assistantMessage = Message.assistant(response.choice, response.messageId);
|
|
1380
|
-
newMessages.push(assistantMessage);
|
|
1381
|
-
await this.memoryRecorder.commitMessages(
|
|
1382
|
-
runId,
|
|
1383
|
-
currentTurns,
|
|
1384
|
-
[assistantMessage],
|
|
1385
|
-
pendingTurnMessages
|
|
1386
|
-
);
|
|
1387
1495
|
const toolCalls = response.choice.filter(
|
|
1388
1496
|
(item) => item.type === "tool_call"
|
|
1389
1497
|
);
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1498
|
+
let assistantMessage = Message.assistant(response.choice, response.messageId);
|
|
1499
|
+
newMessages.push(assistantMessage);
|
|
1500
|
+
if (toolCalls.length === 0) {
|
|
1501
|
+
let emittedTurnEnd = false;
|
|
1502
|
+
if (!bufferOutputDeltas) {
|
|
1503
|
+
if (bufferResponseEvents) {
|
|
1504
|
+
for (const event of responseStreamEvents(currentTurns, response)) {
|
|
1505
|
+
yield await emit(event);
|
|
1506
|
+
}
|
|
1398
1507
|
}
|
|
1508
|
+
yield await emit({ type: "turn_end", turn: currentTurns, response });
|
|
1509
|
+
emittedTurnEnd = true;
|
|
1510
|
+
}
|
|
1511
|
+
if (this.steeringMessages.length > 0) {
|
|
1512
|
+
await this.memoryRecorder.commitMessages(
|
|
1513
|
+
runId,
|
|
1514
|
+
currentTurns,
|
|
1515
|
+
[assistantMessage],
|
|
1516
|
+
pendingTurnMessages
|
|
1517
|
+
);
|
|
1399
1518
|
}
|
|
1400
|
-
}
|
|
1401
|
-
yield await emit({ type: "turn_end", turn: currentTurns, response });
|
|
1402
|
-
if (toolCalls.length === 0) {
|
|
1403
1519
|
if (await this.drainSteeringMessages(runId, currentTurns, newMessages, pendingTurnMessages)) {
|
|
1404
1520
|
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
1405
1521
|
continue;
|
|
1406
1522
|
}
|
|
1523
|
+
const guardedOutput = await this.runOutputGuardrailsForResponse(
|
|
1524
|
+
runId,
|
|
1525
|
+
usage,
|
|
1526
|
+
response,
|
|
1527
|
+
newMessages,
|
|
1528
|
+
runObservers
|
|
1529
|
+
);
|
|
1530
|
+
for (const decision of guardedOutput.decisions) {
|
|
1531
|
+
yield await emit({ type: "guardrail_decision", decision });
|
|
1532
|
+
}
|
|
1533
|
+
response = guardedOutput.response;
|
|
1534
|
+
assistantMessage = Message.assistant(response.choice, response.messageId);
|
|
1535
|
+
newMessages[newMessages.length - 1] = assistantMessage;
|
|
1536
|
+
await this.memoryRecorder.commitMessages(
|
|
1537
|
+
runId,
|
|
1538
|
+
currentTurns,
|
|
1539
|
+
[assistantMessage],
|
|
1540
|
+
pendingTurnMessages
|
|
1541
|
+
);
|
|
1542
|
+
if (!emittedTurnEnd && (bufferResponseEvents || bufferOutputDeltas)) {
|
|
1543
|
+
for (const event of responseStreamEvents(currentTurns, response)) {
|
|
1544
|
+
yield await emit(event);
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
if (!emittedTurnEnd) {
|
|
1548
|
+
yield await emit({ type: "turn_end", turn: currentTurns, response });
|
|
1549
|
+
}
|
|
1407
1550
|
const result = {
|
|
1408
|
-
output:
|
|
1551
|
+
output: guardedOutput.output,
|
|
1409
1552
|
usage,
|
|
1410
1553
|
messages: [...newMessages],
|
|
1411
|
-
trace: runObservers.trace
|
|
1554
|
+
trace: runObservers.trace,
|
|
1555
|
+
guardrails: [...this.guardrailDecisions]
|
|
1412
1556
|
};
|
|
1413
1557
|
await this.runRunEndHook(result, newMessages);
|
|
1414
1558
|
await runObservers.end(result);
|
|
@@ -1425,10 +1569,29 @@ var PromptRequest = class _PromptRequest {
|
|
|
1425
1569
|
output: result.output,
|
|
1426
1570
|
usage: result.usage,
|
|
1427
1571
|
messages: result.messages,
|
|
1428
|
-
trace: result.trace
|
|
1572
|
+
trace: result.trace,
|
|
1573
|
+
guardrails: result.guardrails
|
|
1429
1574
|
});
|
|
1430
1575
|
return;
|
|
1431
1576
|
}
|
|
1577
|
+
if (bufferResponseEvents) {
|
|
1578
|
+
for (const event of responseStreamEvents(currentTurns, response)) {
|
|
1579
|
+
yield await emit(event);
|
|
1580
|
+
}
|
|
1581
|
+
} else {
|
|
1582
|
+
for (const toolCall of toolCalls) {
|
|
1583
|
+
if (!emittedToolCallIds.has(toolCall.id)) {
|
|
1584
|
+
yield await emit({ type: "tool_call", turn: currentTurns, toolCall });
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
await this.memoryRecorder.commitMessages(
|
|
1589
|
+
runId,
|
|
1590
|
+
currentTurns,
|
|
1591
|
+
[assistantMessage],
|
|
1592
|
+
pendingTurnMessages
|
|
1593
|
+
);
|
|
1594
|
+
yield await emit({ type: "turn_end", turn: currentTurns, response });
|
|
1432
1595
|
const toolResultEvents = createAsyncQueue();
|
|
1433
1596
|
const toolResultsPromise = this.executeToolCalls(
|
|
1434
1597
|
runId,
|
|
@@ -1527,6 +1690,46 @@ var PromptRequest = class _PromptRequest {
|
|
|
1527
1690
|
);
|
|
1528
1691
|
return executor.execute(toolCalls, onResult, onStreamEvent, observation);
|
|
1529
1692
|
}
|
|
1693
|
+
async runOutputGuardrailsForResponse(runId, usage, response, messages, runObservers) {
|
|
1694
|
+
const originalOutput = textFromAssistantContent(response.choice);
|
|
1695
|
+
const result = await runOutputGuardrails(this.guardrailPolicies, {
|
|
1696
|
+
outputText: originalOutput,
|
|
1697
|
+
messages: [...this.chatHistory, ...messages],
|
|
1698
|
+
usage,
|
|
1699
|
+
run: this.guardrailRunContext(runId)
|
|
1700
|
+
});
|
|
1701
|
+
for (const decision of result.decisions) {
|
|
1702
|
+
await this.recordGuardrailDecision(decision, runObservers);
|
|
1703
|
+
}
|
|
1704
|
+
const output = result.blocked ? result.message ?? "The response was blocked by a guardrail." : result.outputText;
|
|
1705
|
+
if (output === originalOutput) {
|
|
1706
|
+
return { output, response, decisions: result.decisions };
|
|
1707
|
+
}
|
|
1708
|
+
return {
|
|
1709
|
+
output,
|
|
1710
|
+
response: {
|
|
1711
|
+
...response,
|
|
1712
|
+
choice: [AssistantContent.text(output)]
|
|
1713
|
+
},
|
|
1714
|
+
decisions: result.decisions
|
|
1715
|
+
};
|
|
1716
|
+
}
|
|
1717
|
+
async recordGuardrailDecision(decision, runObservers) {
|
|
1718
|
+
this.guardrailDecisions.push(decision);
|
|
1719
|
+
await runObservers.event({
|
|
1720
|
+
name: "guardrail.decision",
|
|
1721
|
+
level: decision.action === "block" ? "WARNING" : "DEFAULT",
|
|
1722
|
+
attributes: guardrailDecisionAttributes(decision)
|
|
1723
|
+
});
|
|
1724
|
+
}
|
|
1725
|
+
guardrailRunContext(runId) {
|
|
1726
|
+
return compact({
|
|
1727
|
+
agentId: this.agent.id,
|
|
1728
|
+
runId,
|
|
1729
|
+
sessionId: this.memoryContext?.sessionId,
|
|
1730
|
+
metadata: this.memoryContext?.metadata
|
|
1731
|
+
});
|
|
1732
|
+
}
|
|
1530
1733
|
async startRunObservers() {
|
|
1531
1734
|
const failOnObserverError = this.traceOptions?.failOnObserverError === true || this.agent.observers.some((registration) => registration.failOnObserverError === true);
|
|
1532
1735
|
return startAgentRunObservers(
|
|
@@ -1776,6 +1979,33 @@ function responseStreamEvents(turn, response) {
|
|
|
1776
1979
|
}
|
|
1777
1980
|
return events;
|
|
1778
1981
|
}
|
|
1982
|
+
function textFromMessage(message) {
|
|
1983
|
+
if (message.role === "system") {
|
|
1984
|
+
return message.content;
|
|
1985
|
+
}
|
|
1986
|
+
return message.content.flatMap((content) => {
|
|
1987
|
+
if (content.type === "text") {
|
|
1988
|
+
return [content.text];
|
|
1989
|
+
}
|
|
1990
|
+
if (content.type === "document" && content.source.type === "text") {
|
|
1991
|
+
return [content.source.text];
|
|
1992
|
+
}
|
|
1993
|
+
return [];
|
|
1994
|
+
}).join("\n");
|
|
1995
|
+
}
|
|
1996
|
+
function guardrailDecisionAttributes(decision) {
|
|
1997
|
+
return compact({
|
|
1998
|
+
policyId: decision.policyId,
|
|
1999
|
+
guardrailId: decision.guardrailId,
|
|
2000
|
+
boundary: decision.boundary,
|
|
2001
|
+
mode: decision.mode,
|
|
2002
|
+
action: decision.action,
|
|
2003
|
+
applied: decision.applied,
|
|
2004
|
+
reason: decision.reason,
|
|
2005
|
+
message: decision.message,
|
|
2006
|
+
latencyMs: decision.latencyMs
|
|
2007
|
+
});
|
|
2008
|
+
}
|
|
1779
2009
|
|
|
1780
2010
|
export {
|
|
1781
2011
|
MaxTurnsError,
|
|
@@ -1784,4 +2014,4 @@ export {
|
|
|
1784
2014
|
extractRagText,
|
|
1785
2015
|
PromptRequest
|
|
1786
2016
|
};
|
|
1787
|
-
//# sourceMappingURL=chunk-
|
|
2017
|
+
//# sourceMappingURL=chunk-CO7YK4JZ.js.map
|