@botonic/plugin-ai-agents 0.48.1 → 0.49.0-alpha.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/lib/cjs/agents/base-agent.d.ts +28 -0
- package/lib/cjs/agents/base-agent.js +39 -0
- package/lib/cjs/agents/base-agent.js.map +1 -0
- package/lib/cjs/agents/index.d.ts +2 -0
- package/lib/cjs/agents/index.js +8 -0
- package/lib/cjs/agents/index.js.map +1 -0
- package/lib/cjs/agents/router-agent.d.ts +25 -0
- package/lib/cjs/agents/router-agent.js +33 -0
- package/lib/cjs/agents/router-agent.js.map +1 -0
- package/lib/cjs/agents/specialist-agent.d.ts +35 -0
- package/lib/cjs/{agent-builder.js → agents/specialist-agent.js} +48 -49
- package/lib/cjs/agents/specialist-agent.js.map +1 -0
- package/lib/cjs/bot-config-tools.js +3 -4
- package/lib/cjs/bot-config-tools.js.map +1 -1
- package/lib/cjs/debug-logger.d.ts +1 -1
- package/lib/cjs/debug-logger.js +4 -1
- package/lib/cjs/debug-logger.js.map +1 -1
- package/lib/cjs/guardrails/input.d.ts +1 -1
- package/lib/cjs/guardrails/input.js +20 -9
- package/lib/cjs/guardrails/input.js.map +1 -1
- package/lib/cjs/index.d.ts +4 -1
- package/lib/cjs/index.js +112 -51
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/llm-config.d.ts +2 -1
- package/lib/cjs/llm-config.js +3 -0
- package/lib/cjs/llm-config.js.map +1 -1
- package/lib/cjs/runners/base-runner.d.ts +26 -0
- package/lib/cjs/runners/base-runner.js +114 -0
- package/lib/cjs/runners/base-runner.js.map +1 -0
- package/lib/cjs/runners/index.d.ts +2 -0
- package/lib/cjs/runners/index.js +8 -0
- package/lib/cjs/runners/index.js.map +1 -0
- package/lib/cjs/runners/router-runner.d.ts +6 -0
- package/lib/cjs/runners/router-runner.js +29 -0
- package/lib/cjs/runners/router-runner.js.map +1 -0
- package/lib/cjs/runners/specialist-runner.d.ts +10 -0
- package/lib/cjs/runners/specialist-runner.js +78 -0
- package/lib/cjs/runners/specialist-runner.js.map +1 -0
- package/lib/cjs/structured-output/bot-executor.d.ts +6 -42
- package/lib/cjs/structured-output/bot-executor.js +9 -9
- package/lib/cjs/structured-output/bot-executor.js.map +1 -1
- package/lib/cjs/structured-output/carousel.d.ts +6 -78
- package/lib/cjs/structured-output/carousel.js +2 -1
- package/lib/cjs/structured-output/carousel.js.map +1 -1
- package/lib/cjs/structured-output/exit.d.ts +4 -8
- package/lib/cjs/structured-output/exit.js +4 -4
- package/lib/cjs/structured-output/exit.js.map +1 -1
- package/lib/cjs/structured-output/index.d.ts +48 -553
- package/lib/cjs/structured-output/index.js +15 -0
- package/lib/cjs/structured-output/index.js.map +1 -1
- package/lib/cjs/structured-output/text-with-buttons.d.ts +10 -49
- package/lib/cjs/structured-output/text-with-buttons.js +10 -10
- package/lib/cjs/structured-output/text-with-buttons.js.map +1 -1
- package/lib/cjs/structured-output/text.d.ts +4 -18
- package/lib/cjs/structured-output/text.js +2 -1
- package/lib/cjs/structured-output/text.js.map +1 -1
- package/lib/cjs/tools/index.d.ts +1 -1
- package/lib/cjs/tools/index.js +3 -2
- package/lib/cjs/tools/index.js.map +1 -1
- package/lib/cjs/tools/retrieve-knowledge.d.ts +3 -6
- package/lib/cjs/tools/retrieve-knowledge.js +7 -5
- package/lib/cjs/tools/retrieve-knowledge.js.map +1 -1
- package/lib/cjs/types.d.ts +2 -3
- package/lib/esm/agents/base-agent.d.ts +28 -0
- package/lib/esm/agents/base-agent.js +39 -0
- package/lib/esm/agents/base-agent.js.map +1 -0
- package/lib/esm/agents/index.d.ts +2 -0
- package/lib/esm/agents/index.js +8 -0
- package/lib/esm/agents/index.js.map +1 -0
- package/lib/esm/agents/router-agent.d.ts +25 -0
- package/lib/esm/agents/router-agent.js +33 -0
- package/lib/esm/agents/router-agent.js.map +1 -0
- package/lib/esm/agents/specialist-agent.d.ts +35 -0
- package/lib/esm/{agent-builder.js → agents/specialist-agent.js} +48 -49
- package/lib/esm/agents/specialist-agent.js.map +1 -0
- package/lib/esm/bot-config-tools.js +3 -4
- package/lib/esm/bot-config-tools.js.map +1 -1
- package/lib/esm/debug-logger.d.ts +1 -1
- package/lib/esm/debug-logger.js +4 -1
- package/lib/esm/debug-logger.js.map +1 -1
- package/lib/esm/guardrails/input.d.ts +1 -1
- package/lib/esm/guardrails/input.js +20 -9
- package/lib/esm/guardrails/input.js.map +1 -1
- package/lib/esm/index.d.ts +4 -1
- package/lib/esm/index.js +112 -51
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/llm-config.d.ts +2 -1
- package/lib/esm/llm-config.js +3 -0
- package/lib/esm/llm-config.js.map +1 -1
- package/lib/esm/runners/base-runner.d.ts +26 -0
- package/lib/esm/runners/base-runner.js +114 -0
- package/lib/esm/runners/base-runner.js.map +1 -0
- package/lib/esm/runners/index.d.ts +2 -0
- package/lib/esm/runners/index.js +8 -0
- package/lib/esm/runners/index.js.map +1 -0
- package/lib/esm/runners/router-runner.d.ts +6 -0
- package/lib/esm/runners/router-runner.js +29 -0
- package/lib/esm/runners/router-runner.js.map +1 -0
- package/lib/esm/runners/specialist-runner.d.ts +10 -0
- package/lib/esm/runners/specialist-runner.js +78 -0
- package/lib/esm/runners/specialist-runner.js.map +1 -0
- package/lib/esm/structured-output/bot-executor.d.ts +6 -42
- package/lib/esm/structured-output/bot-executor.js +9 -9
- package/lib/esm/structured-output/bot-executor.js.map +1 -1
- package/lib/esm/structured-output/carousel.d.ts +6 -78
- package/lib/esm/structured-output/carousel.js +2 -1
- package/lib/esm/structured-output/carousel.js.map +1 -1
- package/lib/esm/structured-output/exit.d.ts +4 -8
- package/lib/esm/structured-output/exit.js +4 -4
- package/lib/esm/structured-output/exit.js.map +1 -1
- package/lib/esm/structured-output/index.d.ts +48 -553
- package/lib/esm/structured-output/index.js +15 -0
- package/lib/esm/structured-output/index.js.map +1 -1
- package/lib/esm/structured-output/text-with-buttons.d.ts +10 -49
- package/lib/esm/structured-output/text-with-buttons.js +10 -10
- package/lib/esm/structured-output/text-with-buttons.js.map +1 -1
- package/lib/esm/structured-output/text.d.ts +4 -18
- package/lib/esm/structured-output/text.js +2 -1
- package/lib/esm/structured-output/text.js.map +1 -1
- package/lib/esm/tools/index.d.ts +1 -1
- package/lib/esm/tools/index.js +3 -2
- package/lib/esm/tools/index.js.map +1 -1
- package/lib/esm/tools/retrieve-knowledge.d.ts +3 -6
- package/lib/esm/tools/retrieve-knowledge.js +7 -5
- package/lib/esm/tools/retrieve-knowledge.js.map +1 -1
- package/lib/esm/types.d.ts +2 -3
- package/package.json +5 -6
- package/src/agents/base-agent.ts +75 -0
- package/src/agents/index.ts +2 -0
- package/src/agents/router-agent.ts +71 -0
- package/src/{agent-builder.ts → agents/specialist-agent.ts} +77 -77
- package/src/bot-config-tools.ts +3 -4
- package/src/debug-logger.ts +10 -4
- package/src/guardrails/input.ts +26 -9
- package/src/index.ts +216 -82
- package/src/llm-config.ts +5 -0
- package/src/runners/base-runner.ts +190 -0
- package/src/runners/index.ts +2 -0
- package/src/runners/router-runner.ts +41 -0
- package/src/runners/specialist-runner.ts +112 -0
- package/src/structured-output/bot-executor.ts +3 -3
- package/src/structured-output/carousel.ts +2 -2
- package/src/structured-output/exit.ts +3 -3
- package/src/structured-output/index.ts +15 -0
- package/src/structured-output/text-with-buttons.ts +3 -3
- package/src/structured-output/text.ts +2 -2
- package/src/tools/index.ts +4 -1
- package/src/tools/retrieve-knowledge.ts +32 -29
- package/src/types.ts +2 -3
- package/lib/cjs/agent-builder.d.ts +0 -37
- package/lib/cjs/agent-builder.js.map +0 -1
- package/lib/cjs/runner.d.ts +0 -18
- package/lib/cjs/runner.js +0 -180
- package/lib/cjs/runner.js.map +0 -1
- package/lib/esm/agent-builder.d.ts +0 -37
- package/lib/esm/agent-builder.js.map +0 -1
- package/lib/esm/runner.d.ts +0 -18
- package/lib/esm/runner.js +0 -180
- package/lib/esm/runner.js.map +0 -1
- package/src/runner.ts +0 -283
package/lib/esm/runner.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { ResolvedPlugins } from '@botonic/core';
|
|
2
|
-
import type { DebugLogger } from './debug-logger';
|
|
3
|
-
import { type LLMConfig } from './llm-config';
|
|
4
|
-
import type { AgenticInputMessage, AIAgent, Context, RunResult } from './types';
|
|
5
|
-
export declare class AIAgentRunner<TPlugins extends ResolvedPlugins = ResolvedPlugins, TExtraData = any> {
|
|
6
|
-
private agent;
|
|
7
|
-
private llmConfig;
|
|
8
|
-
private inferenceId;
|
|
9
|
-
private logger;
|
|
10
|
-
constructor(agent: AIAgent<TPlugins, TExtraData>, openAiClient: LLMConfig, inferenceId: string, logger: DebugLogger);
|
|
11
|
-
run(messages: AgenticInputMessage[], context: Context<TPlugins, TExtraData>): Promise<RunResult>;
|
|
12
|
-
private sendLlmRunTracking;
|
|
13
|
-
private getToolsExecuted;
|
|
14
|
-
private getToolResultsByCallId;
|
|
15
|
-
private extractToolResult;
|
|
16
|
-
private getToolExecutionInfo;
|
|
17
|
-
private getSafeToolArguments;
|
|
18
|
-
}
|
package/lib/esm/runner.js
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AIAgentRunner = void 0;
|
|
4
|
-
const agents_1 = require("@openai/agents");
|
|
5
|
-
const constants_1 = require("./constants");
|
|
6
|
-
const llm_config_1 = require("./llm-config");
|
|
7
|
-
const hubtype_api_client_1 = require("./services/hubtype-api-client");
|
|
8
|
-
const types_1 = require("./services/types");
|
|
9
|
-
const tools_1 = require("./tools");
|
|
10
|
-
class AIAgentRunner {
|
|
11
|
-
constructor(agent, openAiClient, inferenceId, logger) {
|
|
12
|
-
this.agent = agent;
|
|
13
|
-
this.llmConfig = openAiClient;
|
|
14
|
-
this.inferenceId = inferenceId;
|
|
15
|
-
this.logger = logger;
|
|
16
|
-
}
|
|
17
|
-
async run(messages, context) {
|
|
18
|
-
const startTime = Date.now();
|
|
19
|
-
this.logger.logRunnerStart(this.llmConfig.modelName, this.llmConfig.modelSettings);
|
|
20
|
-
try {
|
|
21
|
-
const modelProvider = this.llmConfig.modelProvider;
|
|
22
|
-
const modelSettings = this.llmConfig.modelSettings;
|
|
23
|
-
const hasRetrieveKnowledge = this.agent.tools.includes(tools_1.retrieveKnowledge);
|
|
24
|
-
if (hasRetrieveKnowledge && constants_1.OPENAI_PROVIDER === 'azure') {
|
|
25
|
-
modelSettings.toolChoice = tools_1.retrieveKnowledge.name;
|
|
26
|
-
}
|
|
27
|
-
const runner = new agents_1.Runner({
|
|
28
|
-
modelSettings,
|
|
29
|
-
modelProvider,
|
|
30
|
-
tracingDisabled: true,
|
|
31
|
-
});
|
|
32
|
-
// Type assertion to bypass strict type checking - the actual return type from runner.run()
|
|
33
|
-
// doesn't perfectly match our interface, but the properties we access are compatible
|
|
34
|
-
const result = (await runner.run(this.agent, messages, {
|
|
35
|
-
context,
|
|
36
|
-
}));
|
|
37
|
-
const endTime = Date.now();
|
|
38
|
-
await this.sendLlmRunTracking(result, context, startTime, endTime);
|
|
39
|
-
const outputMessages = result.finalOutput?.messages || [];
|
|
40
|
-
const hasExit = outputMessages.length === 0 ||
|
|
41
|
-
outputMessages.some(message => message.type === 'exit');
|
|
42
|
-
const toolsExecuted = this.getToolsExecuted(result, context);
|
|
43
|
-
const runResult = {
|
|
44
|
-
messages: hasExit
|
|
45
|
-
? []
|
|
46
|
-
: outputMessages.filter(message => message.type !== 'exit'),
|
|
47
|
-
toolsExecuted,
|
|
48
|
-
exit: hasExit,
|
|
49
|
-
memoryLength: messages.length,
|
|
50
|
-
error: false,
|
|
51
|
-
inputGuardrailsTriggered: [],
|
|
52
|
-
outputGuardrailsTriggered: [],
|
|
53
|
-
};
|
|
54
|
-
this.logger.logRunResult(runResult, startTime);
|
|
55
|
-
return runResult;
|
|
56
|
-
}
|
|
57
|
-
catch (error) {
|
|
58
|
-
if (error instanceof agents_1.InputGuardrailTripwireTriggered) {
|
|
59
|
-
const runResult = {
|
|
60
|
-
messages: [],
|
|
61
|
-
memoryLength: 0,
|
|
62
|
-
toolsExecuted: [],
|
|
63
|
-
exit: true,
|
|
64
|
-
error: false,
|
|
65
|
-
inputGuardrailsTriggered: error.result.output.outputInfo,
|
|
66
|
-
outputGuardrailsTriggered: [],
|
|
67
|
-
};
|
|
68
|
-
this.logger.logGuardrailTriggered();
|
|
69
|
-
this.logger.logRunResult(runResult, startTime);
|
|
70
|
-
return runResult;
|
|
71
|
-
}
|
|
72
|
-
this.logger.logRunnerError(startTime, error);
|
|
73
|
-
throw error;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
async sendLlmRunTracking(result, context, startTime, endTime) {
|
|
77
|
-
if (!constants_1.isProd) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
const rawResponses = result.rawResponses ?? [];
|
|
81
|
-
if (rawResponses.length === 0) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
const botId = context.request.session.bot.id;
|
|
85
|
-
const isTest = context.request.session.is_test_integration;
|
|
86
|
-
const totalDuration = endTime - startTime;
|
|
87
|
-
const durationPerCall = Math.round(totalDuration / rawResponses.length);
|
|
88
|
-
const temperature = this.llmConfig.modelSettings.temperature ?? 0;
|
|
89
|
-
const apiVersion = (0, llm_config_1.getApiVersion)();
|
|
90
|
-
const llmRuns = rawResponses.map(response => ({
|
|
91
|
-
inference_id: this.inferenceId,
|
|
92
|
-
is_test: isTest,
|
|
93
|
-
product_name: types_1.TrackProductName.AI_AGENT,
|
|
94
|
-
deployment_name: this.llmConfig.modelName,
|
|
95
|
-
model_name: response.providerData?.['model'] ??
|
|
96
|
-
this.llmConfig.modelName,
|
|
97
|
-
feature: types_1.TrackFeature.AI_AGENT_RUN,
|
|
98
|
-
api_version: apiVersion,
|
|
99
|
-
num_prompt_tokens: response.usage.inputTokens,
|
|
100
|
-
num_completion_tokens: response.usage.outputTokens,
|
|
101
|
-
duration_in_milliseconds: durationPerCall,
|
|
102
|
-
temperature,
|
|
103
|
-
error: null,
|
|
104
|
-
}));
|
|
105
|
-
const client = new hubtype_api_client_1.HubtypeApiClient(context.authToken);
|
|
106
|
-
await client.trackLlmRuns(botId, {
|
|
107
|
-
llm_runs: llmRuns,
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
getToolsExecuted(result, context) {
|
|
111
|
-
const toolResultsByCallId = this.getToolResultsByCallId(result.newItems);
|
|
112
|
-
return (result.newItems
|
|
113
|
-
?.filter(item => item instanceof agents_1.RunToolCallItem)
|
|
114
|
-
.map((item) => this.getToolExecutionInfo(item, context, toolResultsByCallId))
|
|
115
|
-
.filter((toolExecution) => toolExecution.toolName !== '') || []);
|
|
116
|
-
}
|
|
117
|
-
getToolResultsByCallId(newItems) {
|
|
118
|
-
const map = new Map();
|
|
119
|
-
for (const item of newItems || []) {
|
|
120
|
-
if (!(item instanceof agents_1.RunToolCallOutputItem)) {
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
const rawItem = item.rawItem;
|
|
124
|
-
const callId = rawItem?.callId;
|
|
125
|
-
const output = rawItem?.output;
|
|
126
|
-
if (callId == null || output == null) {
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
const text = this.extractToolResult(output);
|
|
130
|
-
if (text != null) {
|
|
131
|
-
map.set(callId, text);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return map;
|
|
135
|
-
}
|
|
136
|
-
extractToolResult(output) {
|
|
137
|
-
if (typeof output === 'string') {
|
|
138
|
-
return output;
|
|
139
|
-
}
|
|
140
|
-
return output?.type === 'text' && typeof output?.text === 'string'
|
|
141
|
-
? output.text
|
|
142
|
-
: undefined;
|
|
143
|
-
}
|
|
144
|
-
getToolExecutionInfo(item, context, toolResultsByCallId) {
|
|
145
|
-
if (item.rawItem.type !== 'function_call') {
|
|
146
|
-
return {
|
|
147
|
-
toolName: '',
|
|
148
|
-
toolArguments: {},
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
const toolName = item.rawItem.name;
|
|
152
|
-
const toolArguments = this.getSafeToolArguments(item.rawItem.arguments);
|
|
153
|
-
const toolResults = item.rawItem.callId
|
|
154
|
-
? toolResultsByCallId.get(item.rawItem.callId)
|
|
155
|
-
: undefined;
|
|
156
|
-
const toolExecution = {
|
|
157
|
-
toolName,
|
|
158
|
-
toolArguments,
|
|
159
|
-
toolResults,
|
|
160
|
-
};
|
|
161
|
-
if (toolName === tools_1.retrieveKnowledge.name) {
|
|
162
|
-
return {
|
|
163
|
-
...toolExecution,
|
|
164
|
-
knowledgebaseSourcesIds: context.knowledgeUsed.sourceIds,
|
|
165
|
-
knowledgebaseChunksIds: context.knowledgeUsed.chunksIds,
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
return toolExecution;
|
|
169
|
-
}
|
|
170
|
-
getSafeToolArguments(rawToolArguments) {
|
|
171
|
-
try {
|
|
172
|
-
return JSON.parse(rawToolArguments);
|
|
173
|
-
}
|
|
174
|
-
catch (_error) {
|
|
175
|
-
return {};
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
exports.AIAgentRunner = AIAgentRunner;
|
|
180
|
-
//# sourceMappingURL=runner.js.map
|
package/lib/esm/runner.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../../src/runner.ts"],"names":[],"mappings":";;;AAKA,2CAKuB;AACvB,2CAAqD;AAErD,6CAA4D;AAC5D,sEAAgE;AAChE,4CAAiE;AACjE,mCAA2C;AAsB3C,MAAa,aAAa;IASxB,YACE,KAAoC,EACpC,YAAuB,EACvB,WAAmB,EACnB,MAAmB;QAEnB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,SAAS,GAAG,YAAY,CAAA;QAC7B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;QAC9B,IAAI,CAAC,MAAM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IACtB,CAAC;IAEO,oBAAoB,CAAC,gBAAwB;QACnD,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAA;QACrC,CAAC;QAAC,OAAO,MAAM,EAAE,CAAC;YAChB,OAAO,EAAE,CAAA;QACX,CAAC;IACH,CAAC;CACF;AApPD,sCAoPC"}
|
package/src/runner.ts
DELETED
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
OutputMessage,
|
|
3
|
-
ResolvedPlugins,
|
|
4
|
-
ToolExecution,
|
|
5
|
-
} from '@botonic/core'
|
|
6
|
-
import {
|
|
7
|
-
InputGuardrailTripwireTriggered,
|
|
8
|
-
Runner,
|
|
9
|
-
RunToolCallItem,
|
|
10
|
-
RunToolCallOutputItem,
|
|
11
|
-
} from '@openai/agents'
|
|
12
|
-
import { isProd, OPENAI_PROVIDER } from './constants'
|
|
13
|
-
import type { DebugLogger } from './debug-logger'
|
|
14
|
-
import { getApiVersion, type LLMConfig } from './llm-config'
|
|
15
|
-
import { HubtypeApiClient } from './services/hubtype-api-client'
|
|
16
|
-
import { TrackFeature, TrackProductName } from './services/types'
|
|
17
|
-
import { retrieveKnowledge } from './tools'
|
|
18
|
-
import type {
|
|
19
|
-
AgenticInputMessage,
|
|
20
|
-
AgenticOutputMessage,
|
|
21
|
-
AIAgent,
|
|
22
|
-
Context,
|
|
23
|
-
ResultRawResponse,
|
|
24
|
-
RunResult,
|
|
25
|
-
} from './types'
|
|
26
|
-
|
|
27
|
-
// Minimal interface matching the properties we actually use from Runner.run() result
|
|
28
|
-
// This bypasses strict type checking while maintaining type safety for accessed properties
|
|
29
|
-
interface AIAgentRunnerResult {
|
|
30
|
-
finalOutput?: {
|
|
31
|
-
messages?: OutputMessage[]
|
|
32
|
-
}
|
|
33
|
-
newItems?: RunToolCallItem[]
|
|
34
|
-
rawResponses?: ResultRawResponse[]
|
|
35
|
-
// biome-ignore lint/suspicious/noExplicitAny: state is a complex internal type
|
|
36
|
-
state?: any
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export class AIAgentRunner<
|
|
40
|
-
TPlugins extends ResolvedPlugins = ResolvedPlugins,
|
|
41
|
-
TExtraData = any,
|
|
42
|
-
> {
|
|
43
|
-
private agent: AIAgent<TPlugins, TExtraData>
|
|
44
|
-
private llmConfig: LLMConfig
|
|
45
|
-
private inferenceId: string
|
|
46
|
-
private logger: DebugLogger
|
|
47
|
-
|
|
48
|
-
constructor(
|
|
49
|
-
agent: AIAgent<TPlugins, TExtraData>,
|
|
50
|
-
openAiClient: LLMConfig,
|
|
51
|
-
inferenceId: string,
|
|
52
|
-
logger: DebugLogger
|
|
53
|
-
) {
|
|
54
|
-
this.agent = agent
|
|
55
|
-
this.llmConfig = openAiClient
|
|
56
|
-
this.inferenceId = inferenceId
|
|
57
|
-
this.logger = logger
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async run(
|
|
61
|
-
messages: AgenticInputMessage[],
|
|
62
|
-
context: Context<TPlugins, TExtraData>
|
|
63
|
-
): Promise<RunResult> {
|
|
64
|
-
const startTime = Date.now()
|
|
65
|
-
|
|
66
|
-
this.logger.logRunnerStart(
|
|
67
|
-
this.llmConfig.modelName,
|
|
68
|
-
this.llmConfig.modelSettings
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
try {
|
|
72
|
-
const modelProvider = this.llmConfig.modelProvider
|
|
73
|
-
const modelSettings = this.llmConfig.modelSettings
|
|
74
|
-
|
|
75
|
-
const hasRetrieveKnowledge = this.agent.tools.includes(retrieveKnowledge)
|
|
76
|
-
if (hasRetrieveKnowledge && OPENAI_PROVIDER === 'azure') {
|
|
77
|
-
modelSettings.toolChoice = retrieveKnowledge.name
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const runner = new Runner({
|
|
81
|
-
modelSettings,
|
|
82
|
-
modelProvider,
|
|
83
|
-
tracingDisabled: true,
|
|
84
|
-
})
|
|
85
|
-
// Type assertion to bypass strict type checking - the actual return type from runner.run()
|
|
86
|
-
// doesn't perfectly match our interface, but the properties we access are compatible
|
|
87
|
-
const result = (await runner.run(this.agent, messages, {
|
|
88
|
-
context,
|
|
89
|
-
})) as AIAgentRunnerResult
|
|
90
|
-
|
|
91
|
-
const endTime = Date.now()
|
|
92
|
-
|
|
93
|
-
await this.sendLlmRunTracking(result, context, startTime, endTime)
|
|
94
|
-
|
|
95
|
-
const outputMessages = result.finalOutput?.messages || []
|
|
96
|
-
const hasExit =
|
|
97
|
-
outputMessages.length === 0 ||
|
|
98
|
-
outputMessages.some(message => message.type === 'exit')
|
|
99
|
-
const toolsExecuted = this.getToolsExecuted(result, context)
|
|
100
|
-
|
|
101
|
-
const runResult: RunResult = {
|
|
102
|
-
messages: hasExit
|
|
103
|
-
? []
|
|
104
|
-
: (outputMessages.filter(
|
|
105
|
-
message => message.type !== 'exit'
|
|
106
|
-
) as AgenticOutputMessage[]),
|
|
107
|
-
toolsExecuted,
|
|
108
|
-
exit: hasExit,
|
|
109
|
-
memoryLength: messages.length,
|
|
110
|
-
error: false,
|
|
111
|
-
inputGuardrailsTriggered: [],
|
|
112
|
-
outputGuardrailsTriggered: [],
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
this.logger.logRunResult(runResult, startTime)
|
|
116
|
-
|
|
117
|
-
return runResult
|
|
118
|
-
} catch (error) {
|
|
119
|
-
if (error instanceof InputGuardrailTripwireTriggered) {
|
|
120
|
-
const runResult: RunResult = {
|
|
121
|
-
messages: [],
|
|
122
|
-
memoryLength: 0,
|
|
123
|
-
toolsExecuted: [],
|
|
124
|
-
exit: true,
|
|
125
|
-
error: false,
|
|
126
|
-
inputGuardrailsTriggered: error.result.output.outputInfo,
|
|
127
|
-
outputGuardrailsTriggered: [],
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
this.logger.logGuardrailTriggered()
|
|
131
|
-
this.logger.logRunResult(runResult, startTime)
|
|
132
|
-
|
|
133
|
-
return runResult
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
this.logger.logRunnerError(startTime, error)
|
|
137
|
-
|
|
138
|
-
throw error
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
private async sendLlmRunTracking(
|
|
143
|
-
result: AIAgentRunnerResult,
|
|
144
|
-
context: Context<TPlugins, TExtraData>,
|
|
145
|
-
startTime: number,
|
|
146
|
-
endTime: number
|
|
147
|
-
): Promise<void> {
|
|
148
|
-
if (!isProd) {
|
|
149
|
-
return
|
|
150
|
-
}
|
|
151
|
-
const rawResponses = result.rawResponses ?? []
|
|
152
|
-
if (rawResponses.length === 0) {
|
|
153
|
-
return
|
|
154
|
-
}
|
|
155
|
-
const botId = context.request.session.bot.id
|
|
156
|
-
const isTest = context.request.session.is_test_integration
|
|
157
|
-
const totalDuration = endTime - startTime
|
|
158
|
-
const durationPerCall = Math.round(totalDuration / rawResponses.length)
|
|
159
|
-
const temperature =
|
|
160
|
-
(this.llmConfig.modelSettings.temperature as number | undefined) ?? 0
|
|
161
|
-
const apiVersion = getApiVersion()
|
|
162
|
-
|
|
163
|
-
const llmRuns = rawResponses.map(response => ({
|
|
164
|
-
inference_id: this.inferenceId,
|
|
165
|
-
is_test: isTest,
|
|
166
|
-
product_name: TrackProductName.AI_AGENT,
|
|
167
|
-
deployment_name: this.llmConfig.modelName,
|
|
168
|
-
model_name:
|
|
169
|
-
(response.providerData?.['model'] as string | undefined) ??
|
|
170
|
-
this.llmConfig.modelName,
|
|
171
|
-
feature: TrackFeature.AI_AGENT_RUN,
|
|
172
|
-
api_version: apiVersion,
|
|
173
|
-
num_prompt_tokens: response.usage.inputTokens,
|
|
174
|
-
num_completion_tokens: response.usage.outputTokens,
|
|
175
|
-
duration_in_milliseconds: durationPerCall,
|
|
176
|
-
temperature,
|
|
177
|
-
error: null,
|
|
178
|
-
}))
|
|
179
|
-
|
|
180
|
-
const client = new HubtypeApiClient(context.authToken)
|
|
181
|
-
await client.trackLlmRuns(botId, {
|
|
182
|
-
llm_runs: llmRuns,
|
|
183
|
-
})
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
private getToolsExecuted(
|
|
187
|
-
result,
|
|
188
|
-
context: Context<TPlugins, TExtraData>
|
|
189
|
-
): ToolExecution[] {
|
|
190
|
-
const toolResultsByCallId = this.getToolResultsByCallId(result.newItems)
|
|
191
|
-
|
|
192
|
-
return (
|
|
193
|
-
result.newItems
|
|
194
|
-
?.filter(item => item instanceof RunToolCallItem)
|
|
195
|
-
.map((item: RunToolCallItem) =>
|
|
196
|
-
this.getToolExecutionInfo(
|
|
197
|
-
item as RunToolCallItem,
|
|
198
|
-
context,
|
|
199
|
-
toolResultsByCallId
|
|
200
|
-
)
|
|
201
|
-
)
|
|
202
|
-
.filter(
|
|
203
|
-
(toolExecution: ToolExecution) => toolExecution.toolName !== ''
|
|
204
|
-
) || []
|
|
205
|
-
)
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
private getToolResultsByCallId(newItems: unknown[]): Map<string, string> {
|
|
209
|
-
const map = new Map<string, string>()
|
|
210
|
-
for (const item of newItems || []) {
|
|
211
|
-
if (!(item instanceof RunToolCallOutputItem)) {
|
|
212
|
-
continue
|
|
213
|
-
}
|
|
214
|
-
const rawItem = item.rawItem as {
|
|
215
|
-
callId?: string
|
|
216
|
-
output?: string | { type?: string; text?: string }
|
|
217
|
-
}
|
|
218
|
-
const callId = rawItem?.callId
|
|
219
|
-
const output = rawItem?.output
|
|
220
|
-
if (callId == null || output == null) {
|
|
221
|
-
continue
|
|
222
|
-
}
|
|
223
|
-
const text = this.extractToolResult(output)
|
|
224
|
-
if (text != null) {
|
|
225
|
-
map.set(callId, text)
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return map
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
private extractToolResult(
|
|
232
|
-
output: string | { type?: string; text?: string }
|
|
233
|
-
): string | undefined {
|
|
234
|
-
if (typeof output === 'string') {
|
|
235
|
-
return output
|
|
236
|
-
}
|
|
237
|
-
return output?.type === 'text' && typeof output?.text === 'string'
|
|
238
|
-
? output.text
|
|
239
|
-
: undefined
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
private getToolExecutionInfo(
|
|
243
|
-
item: RunToolCallItem,
|
|
244
|
-
context: Context<TPlugins, TExtraData>,
|
|
245
|
-
toolResultsByCallId: Map<string, string>
|
|
246
|
-
): ToolExecution {
|
|
247
|
-
if (item.rawItem.type !== 'function_call') {
|
|
248
|
-
return {
|
|
249
|
-
toolName: '',
|
|
250
|
-
toolArguments: {},
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
const toolName = item.rawItem.name
|
|
254
|
-
const toolArguments = this.getSafeToolArguments(item.rawItem.arguments)
|
|
255
|
-
const toolResults = item.rawItem.callId
|
|
256
|
-
? toolResultsByCallId.get(item.rawItem.callId)
|
|
257
|
-
: undefined
|
|
258
|
-
|
|
259
|
-
const toolExecution: ToolExecution = {
|
|
260
|
-
toolName,
|
|
261
|
-
toolArguments,
|
|
262
|
-
toolResults,
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (toolName === retrieveKnowledge.name) {
|
|
266
|
-
return {
|
|
267
|
-
...toolExecution,
|
|
268
|
-
knowledgebaseSourcesIds: context.knowledgeUsed.sourceIds,
|
|
269
|
-
knowledgebaseChunksIds: context.knowledgeUsed.chunksIds,
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
return toolExecution
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
private getSafeToolArguments(rawToolArguments: string): Record<string, any> {
|
|
277
|
-
try {
|
|
278
|
-
return JSON.parse(rawToolArguments)
|
|
279
|
-
} catch (_error) {
|
|
280
|
-
return {}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
}
|