@botonic/plugin-ai-agents 0.45.0-alpha.1 → 0.46.0-alpha.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/lib/cjs/agent-builder.d.ts +3 -0
- package/lib/cjs/agent-builder.js +7 -15
- package/lib/cjs/agent-builder.js.map +1 -1
- package/lib/cjs/bot-config-tools.d.ts +7 -0
- package/lib/cjs/bot-config-tools.js +19 -0
- package/lib/cjs/bot-config-tools.js.map +1 -0
- package/lib/cjs/constants.d.ts +2 -1
- package/lib/cjs/constants.js +3 -2
- package/lib/cjs/constants.js.map +1 -1
- package/lib/cjs/debug-logger.d.ts +4 -2
- package/lib/cjs/debug-logger.js +12 -14
- package/lib/cjs/debug-logger.js.map +1 -1
- package/lib/cjs/guardrails/input.d.ts +2 -1
- package/lib/cjs/guardrails/input.js +7 -2
- package/lib/cjs/guardrails/input.js.map +1 -1
- package/lib/cjs/hubtype-api-client.d.ts +2 -2
- package/lib/cjs/hubtype-api-client.js +4 -4
- package/lib/cjs/hubtype-api-client.js.map +1 -1
- package/lib/cjs/index.d.ts +4 -1
- package/lib/cjs/index.js +25 -12
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/llm-config.d.ts +15 -0
- package/lib/cjs/llm-config.js +66 -0
- package/lib/cjs/llm-config.js.map +1 -0
- package/lib/cjs/runner.d.ts +5 -1
- package/lib/cjs/runner.js +53 -11
- package/lib/cjs/runner.js.map +1 -1
- package/lib/esm/agent-builder.d.ts +3 -0
- package/lib/esm/agent-builder.js +7 -15
- package/lib/esm/agent-builder.js.map +1 -1
- package/lib/esm/bot-config-tools.d.ts +7 -0
- package/lib/esm/bot-config-tools.js +19 -0
- package/lib/esm/bot-config-tools.js.map +1 -0
- package/lib/esm/constants.d.ts +2 -1
- package/lib/esm/constants.js +3 -2
- package/lib/esm/constants.js.map +1 -1
- package/lib/esm/debug-logger.d.ts +4 -2
- package/lib/esm/debug-logger.js +12 -14
- package/lib/esm/debug-logger.js.map +1 -1
- package/lib/esm/guardrails/input.d.ts +2 -1
- package/lib/esm/guardrails/input.js +7 -2
- package/lib/esm/guardrails/input.js.map +1 -1
- package/lib/esm/hubtype-api-client.d.ts +2 -2
- package/lib/esm/hubtype-api-client.js +4 -4
- package/lib/esm/hubtype-api-client.js.map +1 -1
- package/lib/esm/index.d.ts +4 -1
- package/lib/esm/index.js +25 -12
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/llm-config.d.ts +15 -0
- package/lib/esm/llm-config.js +66 -0
- package/lib/esm/llm-config.js.map +1 -0
- package/lib/esm/runner.d.ts +5 -1
- package/lib/esm/runner.js +53 -11
- package/lib/esm/runner.js.map +1 -1
- package/package.json +5 -4
- package/src/agent-builder.ts +19 -19
- package/src/bot-config-tools.ts +21 -0
- package/src/constants.ts +3 -2
- package/src/debug-logger.ts +24 -28
- package/src/guardrails/input.ts +13 -4
- package/src/hubtype-api-client.ts +4 -4
- package/src/index.ts +45 -14
- package/src/llm-config.ts +96 -0
- package/src/runner.ts +79 -12
- package/lib/cjs/openai.d.ts +0 -1
- package/lib/cjs/openai.js +0 -39
- package/lib/cjs/openai.js.map +0 -1
- package/lib/esm/openai.d.ts +0 -1
- package/lib/esm/openai.js +0 -39
- package/lib/esm/openai.js.map +0 -1
- package/src/openai.ts +0 -49
package/lib/esm/index.js
CHANGED
|
@@ -6,12 +6,11 @@ const agent_builder_1 = require("./agent-builder");
|
|
|
6
6
|
const constants_1 = require("./constants");
|
|
7
7
|
const debug_logger_1 = require("./debug-logger");
|
|
8
8
|
const hubtype_api_client_1 = require("./hubtype-api-client");
|
|
9
|
-
const
|
|
9
|
+
const llm_config_1 = require("./llm-config");
|
|
10
10
|
const runner_1 = require("./runner");
|
|
11
11
|
class BotonicPluginAiAgents {
|
|
12
12
|
constructor(options) {
|
|
13
13
|
this.toolDefinitions = [];
|
|
14
|
-
(0, openai_1.setUpOpenAI)(options?.maxRetries, options?.timeout);
|
|
15
14
|
if (options?.messageHistoryApiVersion === 'v1' && options?.memory) {
|
|
16
15
|
throw new Error('Cannot use memory when messageHistoryApiVersion is "v1". ' +
|
|
17
16
|
'Either set messageHistoryApiVersion to "v2" or remove memory.');
|
|
@@ -20,6 +19,8 @@ class BotonicPluginAiAgents {
|
|
|
20
19
|
this.toolDefinitions = options?.customTools || [];
|
|
21
20
|
this.messageHistoryApiVersion = options?.messageHistoryApiVersion ?? 'v2';
|
|
22
21
|
this.memory = options?.memory ?? {};
|
|
22
|
+
this.timeout = options?.timeout ?? constants_1.DEFAULT_TIMEOUT_16_SECONDS;
|
|
23
|
+
this.maxRetries = options?.maxRetries ?? constants_1.DEFAULT_MAX_RETRIES;
|
|
23
24
|
this.logger = (0, debug_logger_1.createDebugLogger)(options?.enableDebug ?? false);
|
|
24
25
|
this.logger.logInitialConfig({
|
|
25
26
|
messageHistoryApiVersion: this.messageHistoryApiVersion,
|
|
@@ -32,24 +33,33 @@ class BotonicPluginAiAgents {
|
|
|
32
33
|
pre() {
|
|
33
34
|
return;
|
|
34
35
|
}
|
|
35
|
-
async getInference(
|
|
36
|
+
async getInference(botContext, aiAgentArgs) {
|
|
36
37
|
try {
|
|
37
|
-
const authToken = constants_1.isProd
|
|
38
|
+
const authToken = constants_1.isProd
|
|
39
|
+
? botContext.session._access_token
|
|
40
|
+
: this.authToken;
|
|
38
41
|
if (!authToken) {
|
|
39
42
|
throw new Error('Auth token is required');
|
|
40
43
|
}
|
|
44
|
+
// Create client for OpenAI/Azure OpenAI
|
|
45
|
+
const llmConfig = new llm_config_1.LLMConfig(this.maxRetries, this.timeout, aiAgentArgs.model, aiAgentArgs.verbosity);
|
|
46
|
+
// Build tools
|
|
41
47
|
const tools = this.buildTools(aiAgentArgs.activeTools?.map(tool => tool.name) || []);
|
|
48
|
+
// Build agent
|
|
42
49
|
const agent = new agent_builder_1.AIAgentBuilder({
|
|
43
50
|
name: aiAgentArgs.name,
|
|
44
51
|
instructions: aiAgentArgs.instructions,
|
|
45
52
|
tools: tools,
|
|
46
|
-
contactInfo:
|
|
53
|
+
contactInfo: botContext.session.user.contact_info || [],
|
|
47
54
|
inputGuardrailRules: aiAgentArgs.inputGuardrailRules || [],
|
|
48
55
|
sourceIds: aiAgentArgs.sourceIds || [],
|
|
49
|
-
campaignsContext:
|
|
56
|
+
campaignsContext: botContext.input.context?.campaigns_v2,
|
|
50
57
|
logger: this.logger,
|
|
58
|
+
llmConfig,
|
|
51
59
|
}).build();
|
|
52
|
-
|
|
60
|
+
// Get messages
|
|
61
|
+
const messages = await this.getMessages(botContext, authToken, constants_1.MAX_MEMORY_LENGTH);
|
|
62
|
+
// Build context
|
|
53
63
|
const context = {
|
|
54
64
|
authToken,
|
|
55
65
|
sourceIds: aiAgentArgs.sourceIds || [],
|
|
@@ -59,10 +69,12 @@ class BotonicPluginAiAgents {
|
|
|
59
69
|
chunksIds: [],
|
|
60
70
|
chunkTexts: [],
|
|
61
71
|
},
|
|
62
|
-
request,
|
|
72
|
+
request: botContext,
|
|
63
73
|
};
|
|
74
|
+
// Log agent debug info
|
|
64
75
|
this.logger.logAgentDebugInfo(aiAgentArgs, tools.map(t => t.name), messages);
|
|
65
|
-
|
|
76
|
+
// Run agent
|
|
77
|
+
const runner = new runner_1.AIAgentRunner(agent, llmConfig, this.logger);
|
|
66
78
|
return await runner.run(messages, context);
|
|
67
79
|
}
|
|
68
80
|
catch (error) {
|
|
@@ -78,16 +90,16 @@ class BotonicPluginAiAgents {
|
|
|
78
90
|
};
|
|
79
91
|
}
|
|
80
92
|
}
|
|
81
|
-
async getMessages(
|
|
93
|
+
async getMessages(botContext, authToken, memoryLength) {
|
|
82
94
|
const hubtypeClient = new hubtype_api_client_1.HubtypeApiClient(authToken);
|
|
83
95
|
if (!constants_1.isProd) {
|
|
84
96
|
return await hubtypeClient.getLocalMessages(memoryLength);
|
|
85
97
|
}
|
|
86
98
|
if (this.messageHistoryApiVersion === 'v1') {
|
|
87
|
-
return await hubtypeClient.getMessages(
|
|
99
|
+
return await hubtypeClient.getMessages(botContext, memoryLength);
|
|
88
100
|
}
|
|
89
101
|
// Default to V2
|
|
90
|
-
const result = await hubtypeClient.getMessagesV2(
|
|
102
|
+
const result = await hubtypeClient.getMessagesV2(botContext, {
|
|
91
103
|
maxMessages: this.memory.maxMessages ?? memoryLength,
|
|
92
104
|
includeToolCalls: this.memory.includeToolCalls ?? true,
|
|
93
105
|
maxFullToolResults: this.memory.maxFullToolResults ?? 1,
|
|
@@ -108,5 +120,6 @@ class BotonicPluginAiAgents {
|
|
|
108
120
|
}
|
|
109
121
|
}
|
|
110
122
|
exports.default = BotonicPluginAiAgents;
|
|
123
|
+
tslib_1.__exportStar(require("./bot-config-tools"), exports);
|
|
111
124
|
tslib_1.__exportStar(require("./types"), exports);
|
|
112
125
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAMA,2CAAqC;AAErC,mDAAgD;AAChD,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAMA,2CAAqC;AAErC,mDAAgD;AAChD,2CAKoB;AACpB,iDAAoE;AACpE,6DAAuD;AACvD,6CAAwC;AACxC,qCAAwC;AAYxC,MAAqB,qBAAqB;IAaxC,YAAY,OAAoD;QAFzD,oBAAe,GAAuC,EAAE,CAAA;QAG7D,IAAI,OAAO,EAAE,wBAAwB,KAAK,IAAI,IAAI,OAAO,EAAE,MAAM,EAAE,CAAC;YAClE,MAAM,IAAI,KAAK,CACb,2DAA2D;gBACzD,+DAA+D,CAClE,CAAA;QACH,CAAC;QAED,IAAI,CAAC,SAAS,GAAG,OAAO,EAAE,SAAS,CAAA;QACnC,IAAI,CAAC,eAAe,GAAG,OAAO,EAAE,WAAW,IAAI,EAAE,CAAA;QACjD,IAAI,CAAC,wBAAwB,GAAG,OAAO,EAAE,wBAAwB,IA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AAM,CAAC,IAAI,CAAC,EAAE,CACxD,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CACpC,CAAA;QACD,OAAO,cAAc,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE;YACzC,OAAO,IAAA,aAAI,EAA0C;gBACnD,IAAI,EAAE,cAAc,CAAC,IAAI;gBACzB,WAAW,EAAE,cAAc,CAAC,WAAW;gBACvC,UAAU,EAAE,cAAc,CAAC,MAAM;gBACjC,OAAO,EAAE,cAAc,CAAC,IAAI;aAC7B,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC;CACF;AAtKD,wCAsKC;AAED,6DAAkC;AAClC,kDAAuB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { VerbosityLevel } from '@botonic/core';
|
|
2
|
+
import { type ModelProvider, type ModelSettings } from '@openai/agents';
|
|
3
|
+
export declare class LLMConfig {
|
|
4
|
+
private readonly maxRetries;
|
|
5
|
+
private readonly timeout;
|
|
6
|
+
readonly modelName: string;
|
|
7
|
+
readonly modelSettings: ModelSettings;
|
|
8
|
+
readonly modelProvider: ModelProvider;
|
|
9
|
+
constructor(maxRetries: number, timeout: number, modelName: string, verbosity: VerbosityLevel);
|
|
10
|
+
private getModelProvider;
|
|
11
|
+
private getClient;
|
|
12
|
+
private getOpenAIClient;
|
|
13
|
+
private getAzureClient;
|
|
14
|
+
private getModelSettings;
|
|
15
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LLMConfig = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const agents_1 = require("@openai/agents");
|
|
6
|
+
const openai_1 = tslib_1.__importStar(require("openai"));
|
|
7
|
+
const constants_1 = require("./constants");
|
|
8
|
+
class LLMConfig {
|
|
9
|
+
constructor(maxRetries, timeout, modelName, verbosity) {
|
|
10
|
+
this.maxRetries = maxRetries;
|
|
11
|
+
this.timeout = timeout;
|
|
12
|
+
this.modelName = constants_1.OPENAI_PROVIDER === 'openai' ? constants_1.OPENAI_MODEL : modelName;
|
|
13
|
+
this.modelProvider = this.getModelProvider();
|
|
14
|
+
this.modelSettings = this.getModelSettings(modelName, verbosity);
|
|
15
|
+
}
|
|
16
|
+
getModelProvider() {
|
|
17
|
+
const client = this.getClient();
|
|
18
|
+
return new agents_1.OpenAIProvider({
|
|
19
|
+
openAIClient: client,
|
|
20
|
+
useResponses: false,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
getClient() {
|
|
24
|
+
if (constants_1.OPENAI_PROVIDER === 'openai') {
|
|
25
|
+
return this.getOpenAIClient();
|
|
26
|
+
}
|
|
27
|
+
return this.getAzureClient();
|
|
28
|
+
}
|
|
29
|
+
getOpenAIClient() {
|
|
30
|
+
return new openai_1.default({
|
|
31
|
+
apiKey: constants_1.OPENAI_API_KEY,
|
|
32
|
+
timeout: this.timeout,
|
|
33
|
+
maxRetries: this.maxRetries,
|
|
34
|
+
dangerouslyAllowBrowser: !constants_1.isProd,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
getAzureClient() {
|
|
38
|
+
return new openai_1.AzureOpenAI({
|
|
39
|
+
apiKey: constants_1.AZURE_OPENAI_API_KEY,
|
|
40
|
+
apiVersion: constants_1.AZURE_OPENAI_API_VERSION,
|
|
41
|
+
deployment: this.modelName,
|
|
42
|
+
baseURL: constants_1.AZURE_OPENAI_API_BASE,
|
|
43
|
+
timeout: this.timeout,
|
|
44
|
+
maxRetries: this.maxRetries,
|
|
45
|
+
dangerouslyAllowBrowser: !constants_1.isProd,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
getModelSettings(model, verbosity) {
|
|
49
|
+
if (model.includes('gpt-5')) {
|
|
50
|
+
return {
|
|
51
|
+
reasoning: { effort: 'none' },
|
|
52
|
+
temperature: 1,
|
|
53
|
+
text: { verbosity },
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (model.includes('gpt-4')) {
|
|
57
|
+
return {
|
|
58
|
+
temperature: 0,
|
|
59
|
+
text: { verbosity },
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`Unsupported model: ${model}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.LLMConfig = LLMConfig;
|
|
66
|
+
//# sourceMappingURL=llm-config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-config.js","sourceRoot":"","sources":["../../src/llm-config.ts"],"names":[],"mappings":";;;;AACA,2CAIuB;AACvB,yDAA4C;AAC5C,2CAQoB;AAEpB,MAAa,SAAS;IAOpB,YACE,UAAkB,EAClB,OAAe,EACf,SAAiB,EACjB,SAAyB;QAEzB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,SAAS,GAAG,2BAAe,KAAK,QAAQ,CAAC,CAAC,CAAC,wBAAY,CAAC,CAAC,CAAC,SAAS,CAAA;QACxE,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,gBAAgB,EAAE,CAAA;QAC5C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,gBAAgB,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;IAClE,CAAC;IAEO,gBAAgB;QACtB,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAA;QAC/B,OAAO,IAAI,uBAAc,CAAC;YACxB,YAAY,EAAE,MAAM;YACpB,YAAY,EAAE,KAAK;SACpB,CAAC,CAAA;IACJ,CAAC;IAEO,SAAS;QACf,IAAI,2BAAe,KAAK,QAAQ,EAAE,CAAC;YACjC,OAAO,IAAI,CAAC,eAAe,EAAE,CAAA;QAC/B,CAAC;QAED,OAAO,IAAI,CAAC,cAAc,EAAE,CAAA;IAC9B,CAAC;IAEO,eAAe;QACrB,OAAO,IAAI,gBAAM,CAAC;YAChB,MAAM,EAAE,0BAAc;YACtB,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,uBAAuB,EAAE,CAAC,kBAAM;SACjC,CAAC,CAAA;IACJ,CAAC;IAEO,cAAc;QACpB,OAAO,IAAI,oBAAW,CAAC;YACrB,MAAM,EAAE,gCAAoB;YAC5B,UAAU,EAAE,oCAAwB;YACpC,UAAU,EAAE,IAAI,CAAC,SAAS;YAC1B,OAAO,EAAE,iCAAqB;YAC9B,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,uBAAuB,EAAE,CAAC,kBAAM;SACjC,CAAC,CAAA;IACJ,CAAC;IAEO,gBAAgB,CACtB,KAAa,EACb,SAAyB;QAEzB,IAAI,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YAC5B,OAAO;gBACL,SAAS,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE;gBAC7B,WAAW,EAAE,CAAC;gBACd,IAAI,EAAE,EAAE,SAAS,EAAE;aACpB,CAAA;QACH,CAAC;QAED,IAAI,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YAC5B,OAAO;gBACL,WAAW,EAAE,CAAC;gBACd,IAAI,EAAE,EAAE,SAAS,EAAE;aACpB,CAAA;QACH,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,sBAAsB,KAAK,EAAE,CAAC,CAAA;IAChD,CAAC;CACF;AA9ED,8BA8EC"}
|
package/lib/esm/runner.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import type { ResolvedPlugins } from '@botonic/core';
|
|
2
2
|
import type { DebugLogger } from './debug-logger';
|
|
3
|
+
import type { LLMConfig } from './llm-config';
|
|
3
4
|
import type { AgenticInputMessage, AIAgent, Context, RunResult } from './types';
|
|
4
5
|
export declare class AIAgentRunner<TPlugins extends ResolvedPlugins = ResolvedPlugins, TExtraData = any> {
|
|
5
6
|
private agent;
|
|
6
7
|
private logger;
|
|
7
|
-
|
|
8
|
+
private llmConfig;
|
|
9
|
+
constructor(agent: AIAgent<TPlugins, TExtraData>, openAiClient: LLMConfig, logger: DebugLogger);
|
|
8
10
|
run(messages: AgenticInputMessage[], context: Context<TPlugins, TExtraData>): Promise<RunResult>;
|
|
9
11
|
private getToolsExecuted;
|
|
12
|
+
private getToolResultsByCallId;
|
|
13
|
+
private extractToolResult;
|
|
10
14
|
private getToolExecutionInfo;
|
|
11
15
|
private getSafeToolArguments;
|
|
12
16
|
}
|
package/lib/esm/runner.js
CHANGED
|
@@ -2,18 +2,28 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AIAgentRunner = void 0;
|
|
4
4
|
const agents_1 = require("@openai/agents");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
5
6
|
const tools_1 = require("./tools");
|
|
6
7
|
class AIAgentRunner {
|
|
7
|
-
constructor(agent, logger) {
|
|
8
|
+
constructor(agent, openAiClient, logger) {
|
|
8
9
|
this.agent = agent;
|
|
10
|
+
this.llmConfig = openAiClient;
|
|
9
11
|
this.logger = logger;
|
|
10
12
|
}
|
|
11
13
|
async run(messages, context) {
|
|
12
14
|
const startTime = Date.now();
|
|
13
|
-
this.logger.logRunnerStart();
|
|
15
|
+
this.logger.logRunnerStart(this.llmConfig.modelName, this.llmConfig.modelSettings);
|
|
14
16
|
try {
|
|
17
|
+
const modelProvider = this.llmConfig.modelProvider;
|
|
18
|
+
const modelSettings = this.llmConfig.modelSettings;
|
|
19
|
+
const hasRetrieveKnowledge = this.agent.tools.includes(tools_1.retrieveKnowledge);
|
|
20
|
+
if (hasRetrieveKnowledge && constants_1.OPENAI_PROVIDER === 'azure') {
|
|
21
|
+
modelSettings.toolChoice = tools_1.retrieveKnowledge.name;
|
|
22
|
+
}
|
|
15
23
|
const runner = new agents_1.Runner({
|
|
16
|
-
modelSettings
|
|
24
|
+
modelSettings,
|
|
25
|
+
modelProvider,
|
|
26
|
+
tracingDisabled: true,
|
|
17
27
|
});
|
|
18
28
|
// Type assertion to bypass strict type checking - the actual return type from runner.run()
|
|
19
29
|
// doesn't perfectly match our interface, but the properties we access are compatible
|
|
@@ -58,12 +68,40 @@ class AIAgentRunner {
|
|
|
58
68
|
}
|
|
59
69
|
}
|
|
60
70
|
getToolsExecuted(result, context) {
|
|
71
|
+
const toolResultsByCallId = this.getToolResultsByCallId(result.newItems);
|
|
61
72
|
return (result.newItems
|
|
62
73
|
?.filter(item => item instanceof agents_1.RunToolCallItem)
|
|
63
|
-
.map((item) => this.getToolExecutionInfo(item, context))
|
|
74
|
+
.map((item) => this.getToolExecutionInfo(item, context, toolResultsByCallId))
|
|
64
75
|
.filter((toolExecution) => toolExecution.toolName !== '') || []);
|
|
65
76
|
}
|
|
66
|
-
|
|
77
|
+
getToolResultsByCallId(newItems) {
|
|
78
|
+
const map = new Map();
|
|
79
|
+
for (const item of newItems || []) {
|
|
80
|
+
if (!(item instanceof agents_1.RunToolCallOutputItem)) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const rawItem = item.rawItem;
|
|
84
|
+
const callId = rawItem?.callId;
|
|
85
|
+
const output = rawItem?.output;
|
|
86
|
+
if (callId == null || output == null) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const text = this.extractToolResult(output);
|
|
90
|
+
if (text != null) {
|
|
91
|
+
map.set(callId, text);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return map;
|
|
95
|
+
}
|
|
96
|
+
extractToolResult(output) {
|
|
97
|
+
if (typeof output === 'string') {
|
|
98
|
+
return output;
|
|
99
|
+
}
|
|
100
|
+
return output?.type === 'text' && typeof output?.text === 'string'
|
|
101
|
+
? output.text
|
|
102
|
+
: undefined;
|
|
103
|
+
}
|
|
104
|
+
getToolExecutionInfo(item, context, toolResultsByCallId) {
|
|
67
105
|
if (item.rawItem.type !== 'function_call') {
|
|
68
106
|
return {
|
|
69
107
|
toolName: '',
|
|
@@ -72,18 +110,22 @@ class AIAgentRunner {
|
|
|
72
110
|
}
|
|
73
111
|
const toolName = item.rawItem.name;
|
|
74
112
|
const toolArguments = this.getSafeToolArguments(item.rawItem.arguments);
|
|
113
|
+
const toolResults = item.rawItem.callId
|
|
114
|
+
? toolResultsByCallId.get(item.rawItem.callId)
|
|
115
|
+
: undefined;
|
|
116
|
+
const toolExecution = {
|
|
117
|
+
toolName,
|
|
118
|
+
toolArguments,
|
|
119
|
+
toolResults,
|
|
120
|
+
};
|
|
75
121
|
if (toolName === tools_1.retrieveKnowledge.name) {
|
|
76
122
|
return {
|
|
77
|
-
|
|
78
|
-
toolArguments,
|
|
123
|
+
...toolExecution,
|
|
79
124
|
knowledgebaseSourcesIds: context.knowledgeUsed.sourceIds,
|
|
80
125
|
knowledgebaseChunksIds: context.knowledgeUsed.chunksIds,
|
|
81
126
|
};
|
|
82
127
|
}
|
|
83
|
-
return
|
|
84
|
-
toolName,
|
|
85
|
-
toolArguments,
|
|
86
|
-
};
|
|
128
|
+
return toolExecution;
|
|
87
129
|
}
|
|
88
130
|
getSafeToolArguments(rawToolArguments) {
|
|
89
131
|
try {
|
package/lib/esm/runner.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../../src/runner.ts"],"names":[],"mappings":";;;AAKA,
|
|
1
|
+
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../../src/runner.ts"],"names":[],"mappings":";;;AAKA,2CAKuB;AACvB,2CAA6C;AAG7C,mCAA2C;AAkB3C,MAAa,aAAa;IAQxB,YACE,KAAoC,EACpC,YAAuB,EACvB,MAAmB;QAEnB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,SAAS,GAAG,YAAY,CAAA;QAC7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;IAED,KAAK,CAAC,GAAG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ACtC,mBAAwC;QAExC,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,eAAe,EAAE,CAAC;YAC1C,OAAO;gBACL,QAAQ,EAAE,EAAE;gBACZ,aAAa,EAAE,EAAE;aAClB,CAAA;QACH,CAAC;QACD,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAA;QAClC,MAAM,aAAa,GAAG,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;QACvE,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM;YACrC,CAAC,CAAC,mBAAmB,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC;YAC9C,CAAC,CAAC,SAAS,CAAA;QAEb,MAAM,aAAa,GAAkB;YACnC,QAAQ;YACR,aAAa;YACb,WAAW;SACZ,CAAA;QAED,IAAI,QAAQ,KAAK,yBAAiB,CAAC,IAAI,EAAE,CAAC;YACxC,OAAO;gBACL,GAAG,aAAa;gBAChB,uBAAuB,EAAE,OAAO,CAAC,aAAa,CAAC,SAAS;gBACxD,sBAAsB,EAAE,OAAO,CAAC,aAAa,CAAC,SAAS;aACxD,CAAA;QACH,CAAC;QAED,OAAO,aAAa,CAAA;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;AAjMD,sCAiMC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@botonic/plugin-ai-agents",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.46.0-alpha.0",
|
|
4
4
|
"main": "./lib/cjs/index.js",
|
|
5
5
|
"module": "./lib/esm/index.js",
|
|
6
6
|
"description": "Use AI Agents to generate your contents",
|
|
@@ -14,12 +14,13 @@
|
|
|
14
14
|
"format": "biome format --write src/ tests/"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@botonic/core": "^0.
|
|
17
|
+
"@botonic/core": "^0.46.0-alpha.0",
|
|
18
18
|
"@openai/agents": "^0.3.9",
|
|
19
|
-
"axios": "^1.13.
|
|
19
|
+
"axios": "^1.13.6",
|
|
20
20
|
"openai": "^6.0.0",
|
|
21
21
|
"uuid": "^10.0.0",
|
|
22
|
-
"zod": "3.25.76"
|
|
22
|
+
"zod": "3.25.76",
|
|
23
|
+
"zod-to-json-schema": "^3.25.1"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"@types/uuid": "^10.0.0"
|
package/src/agent-builder.ts
CHANGED
|
@@ -3,12 +3,12 @@ import {
|
|
|
3
3
|
Agent,
|
|
4
4
|
type AgentOutputType,
|
|
5
5
|
type InputGuardrail,
|
|
6
|
-
type ModelSettings,
|
|
7
6
|
} from '@openai/agents'
|
|
8
7
|
|
|
9
|
-
import {
|
|
8
|
+
import { OPENAI_PROVIDER } from './constants'
|
|
10
9
|
import type { DebugLogger } from './debug-logger'
|
|
11
10
|
import { createInputGuardrail } from './guardrails'
|
|
11
|
+
import type { LLMConfig } from './llm-config'
|
|
12
12
|
import { OutputSchema } from './structured-output'
|
|
13
13
|
import { mandatoryTools, retrieveKnowledge } from './tools'
|
|
14
14
|
import type { AIAgent, Context, GuardrailRule, Tool } from './types'
|
|
@@ -24,6 +24,7 @@ interface AIAgentBuilderOptions<
|
|
|
24
24
|
contactInfo: ContactInfo[]
|
|
25
25
|
inputGuardrailRules: GuardrailRule[]
|
|
26
26
|
sourceIds: string[]
|
|
27
|
+
llmConfig: LLMConfig
|
|
27
28
|
logger: DebugLogger
|
|
28
29
|
}
|
|
29
30
|
|
|
@@ -35,6 +36,7 @@ export class AIAgentBuilder<
|
|
|
35
36
|
private instructions: string
|
|
36
37
|
private tools: Tool<TPlugins, TExtraData>[]
|
|
37
38
|
private inputGuardrails: InputGuardrail[]
|
|
39
|
+
public llmConfig: LLMConfig
|
|
38
40
|
private logger: DebugLogger
|
|
39
41
|
|
|
40
42
|
constructor(options: AIAgentBuilderOptions<TPlugins, TExtraData>) {
|
|
@@ -46,35 +48,34 @@ export class AIAgentBuilder<
|
|
|
46
48
|
)
|
|
47
49
|
this.tools = this.addHubtypeTools(options.tools, options.sourceIds)
|
|
48
50
|
this.inputGuardrails = []
|
|
51
|
+
this.llmConfig = options.llmConfig
|
|
49
52
|
this.logger = options.logger
|
|
50
53
|
if (options.inputGuardrailRules.length > 0) {
|
|
51
|
-
const inputGuardrail = createInputGuardrail(
|
|
54
|
+
const inputGuardrail = createInputGuardrail(
|
|
55
|
+
options.inputGuardrailRules,
|
|
56
|
+
options.llmConfig
|
|
57
|
+
)
|
|
52
58
|
this.inputGuardrails.push(inputGuardrail)
|
|
53
59
|
}
|
|
54
60
|
}
|
|
55
61
|
|
|
56
62
|
build(): AIAgent<TPlugins, TExtraData> {
|
|
57
|
-
const modelSettings: ModelSettings = {} as ModelSettings
|
|
58
|
-
if (OPENAI_PROVIDER === 'openai') {
|
|
59
|
-
modelSettings.reasoning = { effort: 'none' }
|
|
60
|
-
modelSettings.text = { verbosity: 'medium' }
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const hasRetrieveKnowledge = this.tools.includes(retrieveKnowledge)
|
|
64
|
-
if (hasRetrieveKnowledge && OPENAI_PROVIDER === 'azure') {
|
|
65
|
-
modelSettings.toolChoice = retrieveKnowledge.name
|
|
66
|
-
}
|
|
67
|
-
|
|
68
63
|
// When using standard OpenAI API, we need to specify the model
|
|
69
64
|
// Azure OpenAI uses deployment name instead
|
|
70
|
-
|
|
65
|
+
|
|
66
|
+
const model = this.llmConfig.modelName
|
|
67
|
+
const hasRetrieveKnowledge = this.tools.includes(retrieveKnowledge)
|
|
71
68
|
|
|
72
69
|
this.logger.logModelSettings({
|
|
73
70
|
provider: OPENAI_PROVIDER,
|
|
74
71
|
model,
|
|
75
|
-
reasoning: modelSettings.reasoning as
|
|
76
|
-
|
|
77
|
-
|
|
72
|
+
reasoning: this.llmConfig.modelSettings.reasoning as
|
|
73
|
+
| { effort: string }
|
|
74
|
+
| undefined,
|
|
75
|
+
text: this.llmConfig.modelSettings.text as
|
|
76
|
+
| { verbosity: string }
|
|
77
|
+
| undefined,
|
|
78
|
+
toolChoice: this.llmConfig.modelSettings.toolChoice as string | undefined,
|
|
78
79
|
hasRetrieveKnowledge,
|
|
79
80
|
})
|
|
80
81
|
|
|
@@ -89,7 +90,6 @@ export class AIAgentBuilder<
|
|
|
89
90
|
outputType: OutputSchema,
|
|
90
91
|
inputGuardrails: this.inputGuardrails,
|
|
91
92
|
outputGuardrails: [],
|
|
92
|
-
modelSettings,
|
|
93
93
|
})
|
|
94
94
|
}
|
|
95
95
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ToolConfigJSON } from '@botonic/core'
|
|
2
|
+
import { zodToJsonSchema } from 'zod-to-json-schema'
|
|
3
|
+
|
|
4
|
+
import type { CustomTool } from './types'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Maps custom AI agent tools to the format expected by bot config (e.g. for flow-builder).
|
|
8
|
+
* Converts each tool's Zod schema to JSON Schema so it can be serialized in the config.
|
|
9
|
+
*/
|
|
10
|
+
export function getToolsForBotConfig(
|
|
11
|
+
customTools: CustomTool[]
|
|
12
|
+
): ToolConfigJSON[] {
|
|
13
|
+
return customTools.map(tool => ({
|
|
14
|
+
name: tool.name,
|
|
15
|
+
description: tool.description,
|
|
16
|
+
// Cast to avoid TS "Type instantiation is excessively deep" with zodToJsonSchema + ZodSchema
|
|
17
|
+
schema: zodToJsonSchema(tool.schema as never, {
|
|
18
|
+
$refStrategy: 'none',
|
|
19
|
+
}) as ToolConfigJSON['schema'],
|
|
20
|
+
}))
|
|
21
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -10,11 +10,12 @@ export const OPENAI_PROVIDER: 'openai' | 'azure' =
|
|
|
10
10
|
// Azure OpenAI Configuration
|
|
11
11
|
export const AZURE_OPENAI_API_KEY = process.env.AZURE_OPENAI_API_KEY // pragma: allowlist secret
|
|
12
12
|
export const AZURE_OPENAI_API_BASE = process.env.AZURE_OPENAI_API_BASE
|
|
13
|
-
export const AZURE_OPENAI_API_DEPLOYMENT_NAME =
|
|
14
|
-
process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME || 'gpt-41-mini_p1'
|
|
15
13
|
export const AZURE_OPENAI_API_VERSION =
|
|
16
14
|
process.env.AZURE_OPENAI_API_VERSION || '2025-01-01-preview'
|
|
17
15
|
|
|
18
16
|
export const isProd = process.env.NODE_ENV === 'production'
|
|
19
17
|
|
|
20
18
|
export const MAX_MEMORY_LENGTH = 25
|
|
19
|
+
|
|
20
|
+
export const DEFAULT_TIMEOUT_16_SECONDS = 16000
|
|
21
|
+
export const DEFAULT_MAX_RETRIES = 2
|
package/src/debug-logger.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import type { AiAgentArgs } from '@botonic/core'
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
AZURE_OPENAI_API_BASE,
|
|
5
|
-
AZURE_OPENAI_API_DEPLOYMENT_NAME,
|
|
6
|
-
MAX_MEMORY_LENGTH,
|
|
7
|
-
OPENAI_MODEL,
|
|
8
|
-
OPENAI_PROVIDER,
|
|
9
|
-
} from './constants'
|
|
1
|
+
import type { AiAgentArgs, ToolExecution } from '@botonic/core'
|
|
2
|
+
import type { ModelSettings } from '@openai/agents'
|
|
3
|
+
import { MAX_MEMORY_LENGTH, OPENAI_PROVIDER } from './constants'
|
|
10
4
|
import type { AgenticInputMessage, MemoryOptions, RunResult } from './types'
|
|
11
5
|
|
|
12
6
|
const PREFIX = '[BotonicPluginAiAgents]'
|
|
@@ -36,32 +30,17 @@ export interface DebugLogger {
|
|
|
36
30
|
messages: AgenticInputMessage[]
|
|
37
31
|
): void
|
|
38
32
|
logModelSettings(settings: ModelSettingsInfo): void
|
|
39
|
-
logRunnerStart(): void
|
|
33
|
+
logRunnerStart(model: string, modelSettings: ModelSettings): void
|
|
40
34
|
logRunResult(runResult: RunResult, startTime: number): void
|
|
41
35
|
logGuardrailTriggered(): void
|
|
42
36
|
logRunnerError(startTime: number, error: unknown): void
|
|
37
|
+
logToolExecution(toolExecution: ToolExecution): void
|
|
43
38
|
}
|
|
44
39
|
|
|
45
40
|
class EnabledDebugLogger implements DebugLogger {
|
|
46
|
-
private getModelInfo(): { model: string; apiBase: string } {
|
|
47
|
-
const model =
|
|
48
|
-
OPENAI_PROVIDER === 'azure'
|
|
49
|
-
? AZURE_OPENAI_API_DEPLOYMENT_NAME
|
|
50
|
-
: OPENAI_MODEL
|
|
51
|
-
const apiBase =
|
|
52
|
-
OPENAI_PROVIDER === 'azure' && AZURE_OPENAI_API_BASE
|
|
53
|
-
? AZURE_OPENAI_API_BASE
|
|
54
|
-
: 'OpenAI API'
|
|
55
|
-
return { model, apiBase }
|
|
56
|
-
}
|
|
57
|
-
|
|
58
41
|
logInitialConfig(config: DebugLoggerConfig): void {
|
|
59
|
-
const { model, apiBase } = this.getModelInfo()
|
|
60
|
-
|
|
61
42
|
console.log(`${PREFIX} === Plugin Initialization ===`)
|
|
62
43
|
console.log(`${PREFIX} Provider: ${OPENAI_PROVIDER}`)
|
|
63
|
-
console.log(`${PREFIX} Model/Deployment: ${model}`)
|
|
64
|
-
console.log(`${PREFIX} API Base: ${apiBase}`)
|
|
65
44
|
console.log(
|
|
66
45
|
`${PREFIX} Message History API Version: ${config.messageHistoryApiVersion}`
|
|
67
46
|
)
|
|
@@ -116,8 +95,10 @@ class EnabledDebugLogger implements DebugLogger {
|
|
|
116
95
|
console.log(`${PREFIX} === End Model Settings ===`)
|
|
117
96
|
}
|
|
118
97
|
|
|
119
|
-
logRunnerStart(): void {
|
|
98
|
+
logRunnerStart(model: string, modelSettings: ModelSettings): void {
|
|
120
99
|
console.log(`${PREFIX} === Runner Execution Start ===`)
|
|
100
|
+
console.log(`${PREFIX} Model: ${model}`)
|
|
101
|
+
console.log(`${PREFIX} Model Settings: ${JSON.stringify(modelSettings)}`)
|
|
121
102
|
}
|
|
122
103
|
|
|
123
104
|
logRunResult(runResult: RunResult, startTime: number): void {
|
|
@@ -153,16 +134,31 @@ class EnabledDebugLogger implements DebugLogger {
|
|
|
153
134
|
console.log(`${PREFIX} Runner execution failed after ${elapsedMs}ms`)
|
|
154
135
|
console.log(`${PREFIX} Error:`, error)
|
|
155
136
|
}
|
|
137
|
+
|
|
138
|
+
logToolExecution(toolExecution: ToolExecution): void {
|
|
139
|
+
console.log(`${PREFIX} Tool Execution: ${toolExecution.toolName}`)
|
|
140
|
+
console.log(
|
|
141
|
+
`${PREFIX} Tool Arguments: ${JSON.stringify(toolExecution.toolArguments)}`
|
|
142
|
+
)
|
|
143
|
+
console.log(`${PREFIX} Tool Results: ${toolExecution.toolResults}`)
|
|
144
|
+
console.log(
|
|
145
|
+
`${PREFIX} Knowledgebase Sources IDs: ${JSON.stringify(toolExecution.knowledgebaseSourcesIds)}`
|
|
146
|
+
)
|
|
147
|
+
console.log(
|
|
148
|
+
`${PREFIX} Knowledgebase Chunks IDs: ${JSON.stringify(toolExecution.knowledgebaseChunksIds)}`
|
|
149
|
+
)
|
|
150
|
+
}
|
|
156
151
|
}
|
|
157
152
|
|
|
158
153
|
class DisabledDebugLogger implements DebugLogger {
|
|
159
154
|
logInitialConfig(): void {}
|
|
160
155
|
logAgentDebugInfo(): void {}
|
|
161
156
|
logModelSettings(): void {}
|
|
162
|
-
logRunnerStart(): void {}
|
|
157
|
+
logRunnerStart(_model: string, _modelSettings: ModelSettings): void {}
|
|
163
158
|
logRunResult(): void {}
|
|
164
159
|
logGuardrailTriggered(): void {}
|
|
165
160
|
logRunnerError(): void {}
|
|
161
|
+
logToolExecution(): void {}
|
|
166
162
|
}
|
|
167
163
|
|
|
168
164
|
export function createDebugLogger(enableDebug: boolean): DebugLogger {
|
package/src/guardrails/input.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent,
|
|
3
3
|
type InputGuardrail,
|
|
4
|
-
|
|
4
|
+
Runner,
|
|
5
5
|
type UserMessageItem,
|
|
6
6
|
} from '@openai/agents'
|
|
7
7
|
import { z } from 'zod'
|
|
8
|
-
|
|
8
|
+
import type { LLMConfig } from '../llm-config'
|
|
9
9
|
import type { GuardrailRule } from '../types'
|
|
10
10
|
|
|
11
|
-
export function createInputGuardrail(
|
|
11
|
+
export function createInputGuardrail(
|
|
12
|
+
rules: GuardrailRule[],
|
|
13
|
+
llmConfig: LLMConfig
|
|
14
|
+
): InputGuardrail {
|
|
12
15
|
const outputType = z.object(
|
|
13
16
|
Object.fromEntries(
|
|
14
17
|
rules.map(rule => [rule.name, z.boolean().describe(rule.description)])
|
|
@@ -26,7 +29,13 @@ export function createInputGuardrail(rules: GuardrailRule[]): InputGuardrail {
|
|
|
26
29
|
name: 'InputGuardrail',
|
|
27
30
|
execute: async ({ input, context }) => {
|
|
28
31
|
const lastMessage = input[input.length - 1] as UserMessageItem
|
|
29
|
-
const
|
|
32
|
+
const runner = new Runner({
|
|
33
|
+
modelSettings: llmConfig.modelSettings,
|
|
34
|
+
modelProvider: llmConfig.modelProvider,
|
|
35
|
+
tracingDisabled: true,
|
|
36
|
+
})
|
|
37
|
+
const result = await runner.run(agent, [lastMessage], { context })
|
|
38
|
+
|
|
30
39
|
const finalOutput = result.finalOutput as Record<string, boolean>
|
|
31
40
|
if (finalOutput === undefined) {
|
|
32
41
|
throw new Error('Guardrail agent failed to produce output')
|