@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
|
@@ -131,7 +131,7 @@ export class HubtypeApiClient {
|
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
async getMessages(
|
|
134
|
-
|
|
134
|
+
botContext: BotContext,
|
|
135
135
|
maxMemoryLength: number
|
|
136
136
|
): Promise<AgenticInputMessage[]> {
|
|
137
137
|
const url = `${HUBTYPE_API_URL}/external/v1/ai/agent/message_history/`
|
|
@@ -140,7 +140,7 @@ export class HubtypeApiClient {
|
|
|
140
140
|
Authorization: `Bearer ${this.authToken}`,
|
|
141
141
|
}
|
|
142
142
|
const params = {
|
|
143
|
-
last_message_id:
|
|
143
|
+
last_message_id: botContext.input.message_id,
|
|
144
144
|
num_messages: maxMemoryLength,
|
|
145
145
|
}
|
|
146
146
|
|
|
@@ -158,7 +158,7 @@ export class HubtypeApiClient {
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
async getMessagesV2(
|
|
161
|
-
|
|
161
|
+
botContext: BotContext,
|
|
162
162
|
options: GetMessagesV2Options = {}
|
|
163
163
|
): Promise<GetMessagesV2Result> {
|
|
164
164
|
const url = `${HUBTYPE_API_URL}/external/v2/ai/agent/message_history/`
|
|
@@ -167,7 +167,7 @@ export class HubtypeApiClient {
|
|
|
167
167
|
Authorization: `Bearer ${this.authToken}`,
|
|
168
168
|
}
|
|
169
169
|
const params: MessageHistoryV2Params = {
|
|
170
|
-
last_message_id:
|
|
170
|
+
last_message_id: botContext.input.message_id,
|
|
171
171
|
...(options.maxMessages !== undefined && {
|
|
172
172
|
max_messages: options.maxMessages,
|
|
173
173
|
}),
|
package/src/index.ts
CHANGED
|
@@ -7,10 +7,15 @@ import type {
|
|
|
7
7
|
import { tool } from '@openai/agents'
|
|
8
8
|
|
|
9
9
|
import { AIAgentBuilder } from './agent-builder'
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
DEFAULT_MAX_RETRIES,
|
|
12
|
+
DEFAULT_TIMEOUT_16_SECONDS,
|
|
13
|
+
isProd,
|
|
14
|
+
MAX_MEMORY_LENGTH,
|
|
15
|
+
} from './constants'
|
|
11
16
|
import { createDebugLogger, type DebugLogger } from './debug-logger'
|
|
12
17
|
import { HubtypeApiClient } from './hubtype-api-client'
|
|
13
|
-
import {
|
|
18
|
+
import { LLMConfig } from './llm-config'
|
|
14
19
|
import { AIAgentRunner } from './runner'
|
|
15
20
|
import type {
|
|
16
21
|
AgenticInputMessage,
|
|
@@ -32,11 +37,11 @@ export default class BotonicPluginAiAgents<
|
|
|
32
37
|
private readonly messageHistoryApiVersion: MessageHistoryApiVersion
|
|
33
38
|
private readonly memory: MemoryOptions
|
|
34
39
|
private readonly logger: DebugLogger
|
|
40
|
+
private readonly timeout: number
|
|
41
|
+
private readonly maxRetries: number
|
|
35
42
|
public toolDefinitions: CustomTool<TPlugins, TExtraData>[] = []
|
|
36
43
|
|
|
37
44
|
constructor(options?: PluginAiAgentOptions<TPlugins, TExtraData>) {
|
|
38
|
-
setUpOpenAI(options?.maxRetries, options?.timeout)
|
|
39
|
-
|
|
40
45
|
if (options?.messageHistoryApiVersion === 'v1' && options?.memory) {
|
|
41
46
|
throw new Error(
|
|
42
47
|
'Cannot use memory when messageHistoryApiVersion is "v1". ' +
|
|
@@ -48,6 +53,8 @@ export default class BotonicPluginAiAgents<
|
|
|
48
53
|
this.toolDefinitions = options?.customTools || []
|
|
49
54
|
this.messageHistoryApiVersion = options?.messageHistoryApiVersion ?? 'v2'
|
|
50
55
|
this.memory = options?.memory ?? {}
|
|
56
|
+
this.timeout = options?.timeout ?? DEFAULT_TIMEOUT_16_SECONDS
|
|
57
|
+
this.maxRetries = options?.maxRetries ?? DEFAULT_MAX_RETRIES
|
|
51
58
|
this.logger = createDebugLogger(options?.enableDebug ?? false)
|
|
52
59
|
|
|
53
60
|
this.logger.logInitialConfig({
|
|
@@ -64,34 +71,51 @@ export default class BotonicPluginAiAgents<
|
|
|
64
71
|
}
|
|
65
72
|
|
|
66
73
|
async getInference(
|
|
67
|
-
|
|
74
|
+
botContext: BotContext<TPlugins, TExtraData>,
|
|
68
75
|
aiAgentArgs: AiAgentArgs
|
|
69
76
|
): Promise<InferenceResponse> {
|
|
70
77
|
try {
|
|
71
|
-
const authToken = isProd
|
|
78
|
+
const authToken = isProd
|
|
79
|
+
? botContext.session._access_token
|
|
80
|
+
: this.authToken
|
|
72
81
|
if (!authToken) {
|
|
73
82
|
throw new Error('Auth token is required')
|
|
74
83
|
}
|
|
75
84
|
|
|
85
|
+
// Create client for OpenAI/Azure OpenAI
|
|
86
|
+
const llmConfig = new LLMConfig(
|
|
87
|
+
this.maxRetries,
|
|
88
|
+
this.timeout,
|
|
89
|
+
aiAgentArgs.model,
|
|
90
|
+
aiAgentArgs.verbosity
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
// Build tools
|
|
76
94
|
const tools = this.buildTools(
|
|
77
95
|
aiAgentArgs.activeTools?.map(tool => tool.name) || []
|
|
78
96
|
)
|
|
97
|
+
|
|
98
|
+
// Build agent
|
|
79
99
|
const agent = new AIAgentBuilder<TPlugins, TExtraData>({
|
|
80
100
|
name: aiAgentArgs.name,
|
|
81
101
|
instructions: aiAgentArgs.instructions,
|
|
82
102
|
tools: tools,
|
|
83
|
-
contactInfo:
|
|
103
|
+
contactInfo: botContext.session.user.contact_info || [],
|
|
84
104
|
inputGuardrailRules: aiAgentArgs.inputGuardrailRules || [],
|
|
85
105
|
sourceIds: aiAgentArgs.sourceIds || [],
|
|
86
|
-
campaignsContext:
|
|
106
|
+
campaignsContext: botContext.input.context?.campaigns_v2,
|
|
87
107
|
logger: this.logger,
|
|
108
|
+
llmConfig,
|
|
88
109
|
}).build()
|
|
89
110
|
|
|
111
|
+
// Get messages
|
|
90
112
|
const messages = await this.getMessages(
|
|
91
|
-
|
|
113
|
+
botContext,
|
|
92
114
|
authToken,
|
|
93
115
|
MAX_MEMORY_LENGTH
|
|
94
116
|
)
|
|
117
|
+
|
|
118
|
+
// Build context
|
|
95
119
|
const context: Context<TPlugins, TExtraData> = {
|
|
96
120
|
authToken,
|
|
97
121
|
sourceIds: aiAgentArgs.sourceIds || [],
|
|
@@ -101,16 +125,22 @@ export default class BotonicPluginAiAgents<
|
|
|
101
125
|
chunksIds: [],
|
|
102
126
|
chunkTexts: [],
|
|
103
127
|
},
|
|
104
|
-
request,
|
|
128
|
+
request: botContext,
|
|
105
129
|
}
|
|
106
130
|
|
|
131
|
+
// Log agent debug info
|
|
107
132
|
this.logger.logAgentDebugInfo(
|
|
108
133
|
aiAgentArgs,
|
|
109
134
|
tools.map(t => t.name),
|
|
110
135
|
messages
|
|
111
136
|
)
|
|
112
137
|
|
|
113
|
-
|
|
138
|
+
// Run agent
|
|
139
|
+
const runner = new AIAgentRunner<TPlugins, TExtraData>(
|
|
140
|
+
agent,
|
|
141
|
+
llmConfig,
|
|
142
|
+
this.logger
|
|
143
|
+
)
|
|
114
144
|
return await runner.run(messages, context)
|
|
115
145
|
} catch (error) {
|
|
116
146
|
console.error('error plugin returns undefined', error)
|
|
@@ -127,7 +157,7 @@ export default class BotonicPluginAiAgents<
|
|
|
127
157
|
}
|
|
128
158
|
|
|
129
159
|
private async getMessages(
|
|
130
|
-
|
|
160
|
+
botContext: BotContext,
|
|
131
161
|
authToken: string,
|
|
132
162
|
memoryLength: number
|
|
133
163
|
): Promise<AgenticInputMessage[]> {
|
|
@@ -138,11 +168,11 @@ export default class BotonicPluginAiAgents<
|
|
|
138
168
|
}
|
|
139
169
|
|
|
140
170
|
if (this.messageHistoryApiVersion === 'v1') {
|
|
141
|
-
return await hubtypeClient.getMessages(
|
|
171
|
+
return await hubtypeClient.getMessages(botContext, memoryLength)
|
|
142
172
|
}
|
|
143
173
|
|
|
144
174
|
// Default to V2
|
|
145
|
-
const result = await hubtypeClient.getMessagesV2(
|
|
175
|
+
const result = await hubtypeClient.getMessagesV2(botContext, {
|
|
146
176
|
maxMessages: this.memory.maxMessages ?? memoryLength,
|
|
147
177
|
includeToolCalls: this.memory.includeToolCalls ?? true,
|
|
148
178
|
maxFullToolResults: this.memory.maxFullToolResults ?? 1,
|
|
@@ -166,4 +196,5 @@ export default class BotonicPluginAiAgents<
|
|
|
166
196
|
}
|
|
167
197
|
}
|
|
168
198
|
|
|
199
|
+
export * from './bot-config-tools'
|
|
169
200
|
export * from './types'
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { VerbosityLevel } from '@botonic/core'
|
|
2
|
+
import {
|
|
3
|
+
type ModelProvider,
|
|
4
|
+
type ModelSettings,
|
|
5
|
+
OpenAIProvider,
|
|
6
|
+
} from '@openai/agents'
|
|
7
|
+
import OpenAI, { AzureOpenAI } from 'openai'
|
|
8
|
+
import {
|
|
9
|
+
AZURE_OPENAI_API_BASE,
|
|
10
|
+
AZURE_OPENAI_API_KEY,
|
|
11
|
+
AZURE_OPENAI_API_VERSION,
|
|
12
|
+
isProd,
|
|
13
|
+
OPENAI_API_KEY,
|
|
14
|
+
OPENAI_MODEL,
|
|
15
|
+
OPENAI_PROVIDER,
|
|
16
|
+
} from './constants'
|
|
17
|
+
|
|
18
|
+
export class LLMConfig {
|
|
19
|
+
private readonly maxRetries: number
|
|
20
|
+
private readonly timeout: number
|
|
21
|
+
public readonly modelName: string
|
|
22
|
+
public readonly modelSettings: ModelSettings
|
|
23
|
+
public readonly modelProvider: ModelProvider
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
maxRetries: number,
|
|
27
|
+
timeout: number,
|
|
28
|
+
modelName: string,
|
|
29
|
+
verbosity: VerbosityLevel
|
|
30
|
+
) {
|
|
31
|
+
this.maxRetries = maxRetries
|
|
32
|
+
this.timeout = timeout
|
|
33
|
+
this.modelName = OPENAI_PROVIDER === 'openai' ? OPENAI_MODEL : modelName
|
|
34
|
+
this.modelProvider = this.getModelProvider()
|
|
35
|
+
this.modelSettings = this.getModelSettings(modelName, verbosity)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private getModelProvider(): ModelProvider {
|
|
39
|
+
const client = this.getClient()
|
|
40
|
+
return new OpenAIProvider({
|
|
41
|
+
openAIClient: client,
|
|
42
|
+
useResponses: false,
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private getClient(): OpenAI | AzureOpenAI {
|
|
47
|
+
if (OPENAI_PROVIDER === 'openai') {
|
|
48
|
+
return this.getOpenAIClient()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return this.getAzureClient()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private getOpenAIClient(): OpenAI {
|
|
55
|
+
return new OpenAI({
|
|
56
|
+
apiKey: OPENAI_API_KEY,
|
|
57
|
+
timeout: this.timeout,
|
|
58
|
+
maxRetries: this.maxRetries,
|
|
59
|
+
dangerouslyAllowBrowser: !isProd,
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private getAzureClient(): AzureOpenAI {
|
|
64
|
+
return new AzureOpenAI({
|
|
65
|
+
apiKey: AZURE_OPENAI_API_KEY,
|
|
66
|
+
apiVersion: AZURE_OPENAI_API_VERSION,
|
|
67
|
+
deployment: this.modelName,
|
|
68
|
+
baseURL: AZURE_OPENAI_API_BASE,
|
|
69
|
+
timeout: this.timeout,
|
|
70
|
+
maxRetries: this.maxRetries,
|
|
71
|
+
dangerouslyAllowBrowser: !isProd,
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private getModelSettings(
|
|
76
|
+
model: string,
|
|
77
|
+
verbosity: VerbosityLevel
|
|
78
|
+
): ModelSettings {
|
|
79
|
+
if (model.includes('gpt-5')) {
|
|
80
|
+
return {
|
|
81
|
+
reasoning: { effort: 'none' },
|
|
82
|
+
temperature: 1,
|
|
83
|
+
text: { verbosity },
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (model.includes('gpt-4')) {
|
|
88
|
+
return {
|
|
89
|
+
temperature: 0,
|
|
90
|
+
text: { verbosity },
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
throw new Error(`Unsupported model: ${model}`)
|
|
95
|
+
}
|
|
96
|
+
}
|
package/src/runner.ts
CHANGED
|
@@ -7,9 +7,11 @@ import {
|
|
|
7
7
|
InputGuardrailTripwireTriggered,
|
|
8
8
|
Runner,
|
|
9
9
|
RunToolCallItem,
|
|
10
|
+
RunToolCallOutputItem,
|
|
10
11
|
} from '@openai/agents'
|
|
11
|
-
|
|
12
|
+
import { OPENAI_PROVIDER } from './constants'
|
|
12
13
|
import type { DebugLogger } from './debug-logger'
|
|
14
|
+
import type { LLMConfig } from './llm-config'
|
|
13
15
|
import { retrieveKnowledge } from './tools'
|
|
14
16
|
import type {
|
|
15
17
|
AgenticInputMessage,
|
|
@@ -34,9 +36,15 @@ export class AIAgentRunner<
|
|
|
34
36
|
> {
|
|
35
37
|
private agent: AIAgent<TPlugins, TExtraData>
|
|
36
38
|
private logger: DebugLogger
|
|
39
|
+
private llmConfig: LLMConfig
|
|
37
40
|
|
|
38
|
-
constructor(
|
|
41
|
+
constructor(
|
|
42
|
+
agent: AIAgent<TPlugins, TExtraData>,
|
|
43
|
+
openAiClient: LLMConfig,
|
|
44
|
+
logger: DebugLogger
|
|
45
|
+
) {
|
|
39
46
|
this.agent = agent
|
|
47
|
+
this.llmConfig = openAiClient
|
|
40
48
|
this.logger = logger
|
|
41
49
|
}
|
|
42
50
|
|
|
@@ -46,11 +54,24 @@ export class AIAgentRunner<
|
|
|
46
54
|
): Promise<RunResult> {
|
|
47
55
|
const startTime = Date.now()
|
|
48
56
|
|
|
49
|
-
this.logger.logRunnerStart(
|
|
57
|
+
this.logger.logRunnerStart(
|
|
58
|
+
this.llmConfig.modelName,
|
|
59
|
+
this.llmConfig.modelSettings
|
|
60
|
+
)
|
|
50
61
|
|
|
51
62
|
try {
|
|
63
|
+
const modelProvider = this.llmConfig.modelProvider
|
|
64
|
+
const modelSettings = this.llmConfig.modelSettings
|
|
65
|
+
|
|
66
|
+
const hasRetrieveKnowledge = this.agent.tools.includes(retrieveKnowledge)
|
|
67
|
+
if (hasRetrieveKnowledge && OPENAI_PROVIDER === 'azure') {
|
|
68
|
+
modelSettings.toolChoice = retrieveKnowledge.name
|
|
69
|
+
}
|
|
70
|
+
|
|
52
71
|
const runner = new Runner({
|
|
53
|
-
modelSettings
|
|
72
|
+
modelSettings,
|
|
73
|
+
modelProvider,
|
|
74
|
+
tracingDisabled: true,
|
|
54
75
|
})
|
|
55
76
|
// Type assertion to bypass strict type checking - the actual return type from runner.run()
|
|
56
77
|
// doesn't perfectly match our interface, but the properties we access are compatible
|
|
@@ -109,11 +130,17 @@ export class AIAgentRunner<
|
|
|
109
130
|
result,
|
|
110
131
|
context: Context<TPlugins, TExtraData>
|
|
111
132
|
): ToolExecution[] {
|
|
133
|
+
const toolResultsByCallId = this.getToolResultsByCallId(result.newItems)
|
|
134
|
+
|
|
112
135
|
return (
|
|
113
136
|
result.newItems
|
|
114
137
|
?.filter(item => item instanceof RunToolCallItem)
|
|
115
138
|
.map((item: RunToolCallItem) =>
|
|
116
|
-
this.getToolExecutionInfo(
|
|
139
|
+
this.getToolExecutionInfo(
|
|
140
|
+
item as RunToolCallItem,
|
|
141
|
+
context,
|
|
142
|
+
toolResultsByCallId
|
|
143
|
+
)
|
|
117
144
|
)
|
|
118
145
|
.filter(
|
|
119
146
|
(toolExecution: ToolExecution) => toolExecution.toolName !== ''
|
|
@@ -121,9 +148,44 @@ export class AIAgentRunner<
|
|
|
121
148
|
)
|
|
122
149
|
}
|
|
123
150
|
|
|
151
|
+
private getToolResultsByCallId(newItems: unknown[]): Map<string, string> {
|
|
152
|
+
const map = new Map<string, string>()
|
|
153
|
+
for (const item of newItems || []) {
|
|
154
|
+
if (!(item instanceof RunToolCallOutputItem)) {
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
const rawItem = item.rawItem as {
|
|
158
|
+
callId?: string
|
|
159
|
+
output?: string | { type?: string; text?: string }
|
|
160
|
+
}
|
|
161
|
+
const callId = rawItem?.callId
|
|
162
|
+
const output = rawItem?.output
|
|
163
|
+
if (callId == null || output == null) {
|
|
164
|
+
continue
|
|
165
|
+
}
|
|
166
|
+
const text = this.extractToolResult(output)
|
|
167
|
+
if (text != null) {
|
|
168
|
+
map.set(callId, text)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return map
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private extractToolResult(
|
|
175
|
+
output: string | { type?: string; text?: string }
|
|
176
|
+
): string | undefined {
|
|
177
|
+
if (typeof output === 'string') {
|
|
178
|
+
return output
|
|
179
|
+
}
|
|
180
|
+
return output?.type === 'text' && typeof output?.text === 'string'
|
|
181
|
+
? output.text
|
|
182
|
+
: undefined
|
|
183
|
+
}
|
|
184
|
+
|
|
124
185
|
private getToolExecutionInfo(
|
|
125
186
|
item: RunToolCallItem,
|
|
126
|
-
context: Context<TPlugins, TExtraData
|
|
187
|
+
context: Context<TPlugins, TExtraData>,
|
|
188
|
+
toolResultsByCallId: Map<string, string>
|
|
127
189
|
): ToolExecution {
|
|
128
190
|
if (item.rawItem.type !== 'function_call') {
|
|
129
191
|
return {
|
|
@@ -133,20 +195,25 @@ export class AIAgentRunner<
|
|
|
133
195
|
}
|
|
134
196
|
const toolName = item.rawItem.name
|
|
135
197
|
const toolArguments = this.getSafeToolArguments(item.rawItem.arguments)
|
|
198
|
+
const toolResults = item.rawItem.callId
|
|
199
|
+
? toolResultsByCallId.get(item.rawItem.callId)
|
|
200
|
+
: undefined
|
|
201
|
+
|
|
202
|
+
const toolExecution: ToolExecution = {
|
|
203
|
+
toolName,
|
|
204
|
+
toolArguments,
|
|
205
|
+
toolResults,
|
|
206
|
+
}
|
|
136
207
|
|
|
137
208
|
if (toolName === retrieveKnowledge.name) {
|
|
138
209
|
return {
|
|
139
|
-
|
|
140
|
-
toolArguments,
|
|
210
|
+
...toolExecution,
|
|
141
211
|
knowledgebaseSourcesIds: context.knowledgeUsed.sourceIds,
|
|
142
212
|
knowledgebaseChunksIds: context.knowledgeUsed.chunksIds,
|
|
143
213
|
}
|
|
144
214
|
}
|
|
145
215
|
|
|
146
|
-
return
|
|
147
|
-
toolName,
|
|
148
|
-
toolArguments,
|
|
149
|
-
}
|
|
216
|
+
return toolExecution
|
|
150
217
|
}
|
|
151
218
|
|
|
152
219
|
private getSafeToolArguments(rawToolArguments: string): Record<string, any> {
|
package/lib/cjs/openai.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function setUpOpenAI(maxRetries?: number, timeout?: number): void;
|
package/lib/cjs/openai.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setUpOpenAI = setUpOpenAI;
|
|
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
|
-
function setUpOpenAI(maxRetries, timeout) {
|
|
9
|
-
if (constants_1.OPENAI_PROVIDER === 'azure') {
|
|
10
|
-
setAzureOpenAIClient(maxRetries, timeout);
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
setOpenAIClient(maxRetries, timeout);
|
|
14
|
-
}
|
|
15
|
-
(0, agents_1.setOpenAIAPI)('chat_completions');
|
|
16
|
-
(0, agents_1.setTracingDisabled)(true);
|
|
17
|
-
}
|
|
18
|
-
function setOpenAIClient(maxRetries, timeout) {
|
|
19
|
-
const client = new openai_1.default({
|
|
20
|
-
apiKey: constants_1.OPENAI_API_KEY,
|
|
21
|
-
timeout: timeout || 16000, // 16 seconds
|
|
22
|
-
maxRetries: maxRetries || 2,
|
|
23
|
-
dangerouslyAllowBrowser: !constants_1.isProd,
|
|
24
|
-
});
|
|
25
|
-
(0, agents_1.setDefaultOpenAIClient)(client);
|
|
26
|
-
}
|
|
27
|
-
function setAzureOpenAIClient(maxRetries, timeout) {
|
|
28
|
-
const client = new openai_1.AzureOpenAI({
|
|
29
|
-
apiKey: constants_1.AZURE_OPENAI_API_KEY,
|
|
30
|
-
apiVersion: constants_1.AZURE_OPENAI_API_VERSION,
|
|
31
|
-
deployment: constants_1.AZURE_OPENAI_API_DEPLOYMENT_NAME,
|
|
32
|
-
baseURL: constants_1.AZURE_OPENAI_API_BASE,
|
|
33
|
-
timeout: timeout || 16000, // 16 seconds
|
|
34
|
-
maxRetries: maxRetries || 2,
|
|
35
|
-
dangerouslyAllowBrowser: !constants_1.isProd,
|
|
36
|
-
});
|
|
37
|
-
(0, agents_1.setDefaultOpenAIClient)(client);
|
|
38
|
-
}
|
|
39
|
-
//# sourceMappingURL=openai.js.map
|
package/lib/cjs/openai.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/openai.ts"],"names":[],"mappings":";;AAiBA,kCAQC;;AAzBD,2CAIuB;AACvB,yDAA4C;AAE5C,2CAQoB;AAEpB,SAAgB,WAAW,CAAC,UAAmB,EAAE,OAAgB;IAC/D,IAAI,2BAAe,KAAK,OAAO,EAAE,CAAC;QAChC,oBAAoB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;IAC3C,CAAC;SAAM,CAAC;QACN,eAAe,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;IACtC,CAAC;IACD,IAAA,qBAAY,EAAC,kBAAkB,CAAC,CAAA;IAChC,IAAA,2BAAkB,EAAC,IAAI,CAAC,CAAA;AAC1B,CAAC;AAED,SAAS,eAAe,CAAC,UAAmB,EAAE,OAAgB;IAC5D,MAAM,MAAM,GAAG,IAAI,gBAAM,CAAC;QACxB,MAAM,EAAE,0BAAc;QACtB,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,aAAa;QACxC,UAAU,EAAE,UAAU,IAAI,CAAC;QAC3B,uBAAuB,EAAE,CAAC,kBAAM;KACjC,CAAC,CAAA;IACF,IAAA,+BAAsB,EAAC,MAAM,CAAC,CAAA;AAChC,CAAC;AAED,SAAS,oBAAoB,CAAC,UAAmB,EAAE,OAAgB;IACjE,MAAM,MAAM,GAAG,IAAI,oBAAW,CAAC;QAC7B,MAAM,EAAE,gCAAoB;QAC5B,UAAU,EAAE,oCAAwB;QACpC,UAAU,EAAE,4CAAgC;QAC5C,OAAO,EAAE,iCAAqB;QAC9B,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,aAAa;QACxC,UAAU,EAAE,UAAU,IAAI,CAAC;QAC3B,uBAAuB,EAAE,CAAC,kBAAM;KACjC,CAAC,CAAA;IACF,IAAA,+BAAsB,EAAC,MAAM,CAAC,CAAA;AAChC,CAAC"}
|
package/lib/esm/openai.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function setUpOpenAI(maxRetries?: number, timeout?: number): void;
|
package/lib/esm/openai.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setUpOpenAI = setUpOpenAI;
|
|
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
|
-
function setUpOpenAI(maxRetries, timeout) {
|
|
9
|
-
if (constants_1.OPENAI_PROVIDER === 'azure') {
|
|
10
|
-
setAzureOpenAIClient(maxRetries, timeout);
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
setOpenAIClient(maxRetries, timeout);
|
|
14
|
-
}
|
|
15
|
-
(0, agents_1.setOpenAIAPI)('chat_completions');
|
|
16
|
-
(0, agents_1.setTracingDisabled)(true);
|
|
17
|
-
}
|
|
18
|
-
function setOpenAIClient(maxRetries, timeout) {
|
|
19
|
-
const client = new openai_1.default({
|
|
20
|
-
apiKey: constants_1.OPENAI_API_KEY,
|
|
21
|
-
timeout: timeout || 16000, // 16 seconds
|
|
22
|
-
maxRetries: maxRetries || 2,
|
|
23
|
-
dangerouslyAllowBrowser: !constants_1.isProd,
|
|
24
|
-
});
|
|
25
|
-
(0, agents_1.setDefaultOpenAIClient)(client);
|
|
26
|
-
}
|
|
27
|
-
function setAzureOpenAIClient(maxRetries, timeout) {
|
|
28
|
-
const client = new openai_1.AzureOpenAI({
|
|
29
|
-
apiKey: constants_1.AZURE_OPENAI_API_KEY,
|
|
30
|
-
apiVersion: constants_1.AZURE_OPENAI_API_VERSION,
|
|
31
|
-
deployment: constants_1.AZURE_OPENAI_API_DEPLOYMENT_NAME,
|
|
32
|
-
baseURL: constants_1.AZURE_OPENAI_API_BASE,
|
|
33
|
-
timeout: timeout || 16000, // 16 seconds
|
|
34
|
-
maxRetries: maxRetries || 2,
|
|
35
|
-
dangerouslyAllowBrowser: !constants_1.isProd,
|
|
36
|
-
});
|
|
37
|
-
(0, agents_1.setDefaultOpenAIClient)(client);
|
|
38
|
-
}
|
|
39
|
-
//# sourceMappingURL=openai.js.map
|
package/lib/esm/openai.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/openai.ts"],"names":[],"mappings":";;AAiBA,kCAQC;;AAzBD,2CAIuB;AACvB,yDAA4C;AAE5C,2CAQoB;AAEpB,SAAgB,WAAW,CAAC,UAAmB,EAAE,OAAgB;IAC/D,IAAI,2BAAe,KAAK,OAAO,EAAE,CAAC;QAChC,oBAAoB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;IAC3C,CAAC;SAAM,CAAC;QACN,eAAe,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;IACtC,CAAC;IACD,IAAA,qBAAY,EAAC,kBAAkB,CAAC,CAAA;IAChC,IAAA,2BAAkB,EAAC,IAAI,CAAC,CAAA;AAC1B,CAAC;AAED,SAAS,eAAe,CAAC,UAAmB,EAAE,OAAgB;IAC5D,MAAM,MAAM,GAAG,IAAI,gBAAM,CAAC;QACxB,MAAM,EAAE,0BAAc;QACtB,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,aAAa;QACxC,UAAU,EAAE,UAAU,IAAI,CAAC;QAC3B,uBAAuB,EAAE,CAAC,kBAAM;KACjC,CAAC,CAAA;IACF,IAAA,+BAAsB,EAAC,MAAM,CAAC,CAAA;AAChC,CAAC;AAED,SAAS,oBAAoB,CAAC,UAAmB,EAAE,OAAgB;IACjE,MAAM,MAAM,GAAG,IAAI,oBAAW,CAAC;QAC7B,MAAM,EAAE,gCAAoB;QAC5B,UAAU,EAAE,oCAAwB;QACpC,UAAU,EAAE,4CAAgC;QAC5C,OAAO,EAAE,iCAAqB;QAC9B,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,aAAa;QACxC,UAAU,EAAE,UAAU,IAAI,CAAC;QAC3B,uBAAuB,EAAE,CAAC,kBAAM;KACjC,CAAC,CAAA;IACF,IAAA,+BAAsB,EAAC,MAAM,CAAC,CAAA;AAChC,CAAC"}
|
package/src/openai.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
setDefaultOpenAIClient,
|
|
3
|
-
setOpenAIAPI,
|
|
4
|
-
setTracingDisabled,
|
|
5
|
-
} from '@openai/agents'
|
|
6
|
-
import OpenAI, { AzureOpenAI } from 'openai'
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
AZURE_OPENAI_API_BASE,
|
|
10
|
-
AZURE_OPENAI_API_DEPLOYMENT_NAME,
|
|
11
|
-
AZURE_OPENAI_API_KEY,
|
|
12
|
-
AZURE_OPENAI_API_VERSION,
|
|
13
|
-
isProd,
|
|
14
|
-
OPENAI_API_KEY,
|
|
15
|
-
OPENAI_PROVIDER,
|
|
16
|
-
} from './constants'
|
|
17
|
-
|
|
18
|
-
export function setUpOpenAI(maxRetries?: number, timeout?: number) {
|
|
19
|
-
if (OPENAI_PROVIDER === 'azure') {
|
|
20
|
-
setAzureOpenAIClient(maxRetries, timeout)
|
|
21
|
-
} else {
|
|
22
|
-
setOpenAIClient(maxRetries, timeout)
|
|
23
|
-
}
|
|
24
|
-
setOpenAIAPI('chat_completions')
|
|
25
|
-
setTracingDisabled(true)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function setOpenAIClient(maxRetries?: number, timeout?: number) {
|
|
29
|
-
const client = new OpenAI({
|
|
30
|
-
apiKey: OPENAI_API_KEY,
|
|
31
|
-
timeout: timeout || 16000, // 16 seconds
|
|
32
|
-
maxRetries: maxRetries || 2,
|
|
33
|
-
dangerouslyAllowBrowser: !isProd,
|
|
34
|
-
})
|
|
35
|
-
setDefaultOpenAIClient(client)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function setAzureOpenAIClient(maxRetries?: number, timeout?: number) {
|
|
39
|
-
const client = new AzureOpenAI({
|
|
40
|
-
apiKey: AZURE_OPENAI_API_KEY,
|
|
41
|
-
apiVersion: AZURE_OPENAI_API_VERSION,
|
|
42
|
-
deployment: AZURE_OPENAI_API_DEPLOYMENT_NAME,
|
|
43
|
-
baseURL: AZURE_OPENAI_API_BASE,
|
|
44
|
-
timeout: timeout || 16000, // 16 seconds
|
|
45
|
-
maxRetries: maxRetries || 2,
|
|
46
|
-
dangerouslyAllowBrowser: !isProd,
|
|
47
|
-
})
|
|
48
|
-
setDefaultOpenAIClient(client)
|
|
49
|
-
}
|