@botonic/plugin-ai-agents 0.42.4 → 0.43.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.
Files changed (52) hide show
  1. package/lib/cjs/agent-builder.d.ts +14 -3
  2. package/lib/cjs/agent-builder.js +44 -13
  3. package/lib/cjs/agent-builder.js.map +1 -1
  4. package/lib/cjs/constants.d.ts +3 -0
  5. package/lib/cjs/constants.js +6 -1
  6. package/lib/cjs/constants.js.map +1 -1
  7. package/lib/cjs/guardrails/input.js.map +1 -1
  8. package/lib/cjs/hubtype-api-client.d.ts +13 -0
  9. package/lib/cjs/hubtype-api-client.js +97 -0
  10. package/lib/cjs/hubtype-api-client.js.map +1 -1
  11. package/lib/cjs/index.d.ts +2 -0
  12. package/lib/cjs/index.js +31 -6
  13. package/lib/cjs/index.js.map +1 -1
  14. package/lib/cjs/openai.js +18 -3
  15. package/lib/cjs/openai.js.map +1 -1
  16. package/lib/cjs/runner.js +5 -1
  17. package/lib/cjs/runner.js.map +1 -1
  18. package/lib/cjs/tools/retrieve-knowledge.d.ts +8 -1
  19. package/lib/cjs/tools/retrieve-knowledge.js +2 -1
  20. package/lib/cjs/tools/retrieve-knowledge.js.map +1 -1
  21. package/lib/cjs/types.d.ts +13 -3
  22. package/lib/esm/agent-builder.d.ts +14 -3
  23. package/lib/esm/agent-builder.js +45 -14
  24. package/lib/esm/agent-builder.js.map +1 -1
  25. package/lib/esm/constants.d.ts +3 -0
  26. package/lib/esm/constants.js +5 -0
  27. package/lib/esm/constants.js.map +1 -1
  28. package/lib/esm/guardrails/input.js.map +1 -1
  29. package/lib/esm/hubtype-api-client.d.ts +13 -0
  30. package/lib/esm/hubtype-api-client.js +97 -0
  31. package/lib/esm/hubtype-api-client.js.map +1 -1
  32. package/lib/esm/index.d.ts +2 -0
  33. package/lib/esm/index.js +31 -6
  34. package/lib/esm/index.js.map +1 -1
  35. package/lib/esm/openai.js +18 -4
  36. package/lib/esm/openai.js.map +1 -1
  37. package/lib/esm/runner.js +5 -1
  38. package/lib/esm/runner.js.map +1 -1
  39. package/lib/esm/tools/retrieve-knowledge.d.ts +8 -1
  40. package/lib/esm/tools/retrieve-knowledge.js +2 -1
  41. package/lib/esm/tools/retrieve-knowledge.js.map +1 -1
  42. package/lib/esm/types.d.ts +13 -3
  43. package/package.json +3 -3
  44. package/src/agent-builder.ts +80 -26
  45. package/src/constants.ts +7 -0
  46. package/src/guardrails/input.ts +1 -1
  47. package/src/hubtype-api-client.ts +176 -0
  48. package/src/index.ts +39 -12
  49. package/src/openai.ts +19 -3
  50. package/src/runner.ts +15 -2
  51. package/src/tools/retrieve-knowledge.ts +3 -2
  52. package/src/types.ts +17 -3
@@ -1,10 +1,29 @@
1
- import { ResolvedPlugins } from '@botonic/core'
2
- import { Agent, InputGuardrail, ModelSettings } from '@openai/agents'
1
+ import { CampaignV2, ContactInfo, ResolvedPlugins } from '@botonic/core'
2
+ import {
3
+ Agent,
4
+ AgentOutputType,
5
+ InputGuardrail,
6
+ ModelSettings,
7
+ } from '@openai/agents'
3
8
 
9
+ import { OPENAI_MODEL, OPENAI_PROVIDER } from './constants'
4
10
  import { createInputGuardrail } from './guardrails'
5
11
  import { OutputSchema } from './structured-output'
6
12
  import { mandatoryTools, retrieveKnowledge } from './tools'
7
- import { AIAgent, ContactInfo, GuardrailRule, Tool } from './types'
13
+ import { AIAgent, Context, GuardrailRule, Tool } from './types'
14
+
15
+ interface AIAgentBuilderOptions<
16
+ TPlugins extends ResolvedPlugins = ResolvedPlugins,
17
+ TExtraData = any,
18
+ > {
19
+ name: string
20
+ instructions: string
21
+ tools: Tool<TPlugins, TExtraData>[]
22
+ campaignContext?: CampaignV2
23
+ contactInfo: ContactInfo[]
24
+ inputGuardrailRules: GuardrailRule[]
25
+ sourceIds: string[]
26
+ }
8
27
 
9
28
  export class AIAgentBuilder<
10
29
  TPlugins extends ResolvedPlugins = ResolvedPlugins,
@@ -15,33 +34,47 @@ export class AIAgentBuilder<
15
34
  private tools: Tool<TPlugins, TExtraData>[]
16
35
  private inputGuardrails: InputGuardrail[]
17
36
 
18
- constructor(
19
- name: string,
20
- instructions: string,
21
- tools: Tool<TPlugins, TExtraData>[],
22
- contactInfo: ContactInfo,
23
- inputGuardrailRules: GuardrailRule[],
24
- sourceIds: string[]
25
- ) {
26
- this.name = name
27
- this.instructions = this.addExtraInstructions(instructions, contactInfo)
28
- this.tools = this.addHubtypeTools(tools, sourceIds)
37
+ constructor(options: AIAgentBuilderOptions<TPlugins, TExtraData>) {
38
+ this.name = options.name
39
+ this.instructions = this.addExtraInstructions(
40
+ options.instructions,
41
+ options.contactInfo,
42
+ options.campaignContext
43
+ )
44
+ this.tools = this.addHubtypeTools(options.tools, options.sourceIds)
29
45
  this.inputGuardrails = []
30
- if (inputGuardrailRules.length > 0) {
31
- const inputGuardrail = createInputGuardrail(inputGuardrailRules)
46
+ if (options.inputGuardrailRules.length > 0) {
47
+ const inputGuardrail = createInputGuardrail(options.inputGuardrailRules)
32
48
  this.inputGuardrails.push(inputGuardrail)
33
49
  }
34
50
  }
35
51
 
36
52
  build(): AIAgent<TPlugins, TExtraData> {
37
- const modelSettings: ModelSettings = {}
53
+ const modelSettings: ModelSettings = {} as ModelSettings
54
+ if (OPENAI_PROVIDER === 'openai') {
55
+ // @ts-expect-error - reasoning.effort is valid but we need to update openai and typescript dependencies
56
+ modelSettings.reasoning = { effort: 'none' }
57
+ modelSettings.text = { verbosity: 'medium' }
58
+ }
38
59
 
39
- if (this.tools.includes(retrieveKnowledge)) {
60
+ if (this.tools.includes(retrieveKnowledge) && OPENAI_PROVIDER === 'azure') {
40
61
  modelSettings.toolChoice = retrieveKnowledge.name
41
62
  }
42
63
 
43
- return new Agent({
64
+ // When using standard OpenAI API, we need to specify the model
65
+ // Azure OpenAI uses deployment name instead
66
+ const model = OPENAI_PROVIDER === 'openai' ? OPENAI_MODEL : undefined
67
+
68
+ // TODO: Improve type safety - replace AgentOutputType<any> with AgentOutputType<typeof OutputSchema>
69
+ // Currently using explicit type parameters to avoid type inference issues where Agent constructor
70
+ // infers ZodObject instead of AgentOutputType<typeof OutputSchema>. The explicit type parameters
71
+ // ensure compatibility with AIAgent type definition. Future improvements:
72
+ // 1. Update @openai/agents package to properly infer AgentOutputType from outputType parameter
73
+ // 2. Replace AgentOutputType<any> with AgentOutputType<typeof OutputSchema> once type system allows
74
+ // 3. Consider updating AIAgent type definition if @openai/agents types change significantly
75
+ return new Agent<Context<TPlugins, TExtraData>, AgentOutputType<any>>({
44
76
  name: this.name,
77
+ model,
45
78
  instructions: this.instructions,
46
79
  tools: this.tools,
47
80
  outputType: OutputSchema,
@@ -53,20 +86,34 @@ export class AIAgentBuilder<
53
86
 
54
87
  private addExtraInstructions(
55
88
  initialInstructions: string,
56
- contactInfo: ContactInfo
89
+ contactInfo: ContactInfo[],
90
+ campaignContext?: CampaignV2
57
91
  ): string {
58
- const instructions = `<instructions>\n${initialInstructions}\n</instructions>`
92
+ const instructions = `<instructions>\n${initialInstructions.trim()}\n</instructions>`
59
93
  const metadataInstructions = this.getMetadataInstructions()
60
94
  const contactInfoInstructions = this.getContactInfoInstructions(contactInfo)
95
+ const campaignInstructions = this.getCampaignInstructions(campaignContext)
61
96
  const outputInstructions = this.getOutputInstructions()
62
- return `${instructions}\n\n${metadataInstructions}\n\n${contactInfoInstructions}\n\n${outputInstructions}`
97
+ return `${instructions}\n\n${metadataInstructions}\n\n${contactInfoInstructions}\n\n${campaignInstructions}\n\n${outputInstructions}`
63
98
  }
64
99
 
65
- private getContactInfoInstructions(contactInfo: ContactInfo): string {
66
- const structuredContactInfo = Object.entries(contactInfo)
67
- .map(([key, value]) => `${key}: ${value}`)
100
+ private getContactInfoInstructions(contactInfo: ContactInfo[]): string {
101
+ const structuredContactInfo = contactInfo
102
+ .map(
103
+ info =>
104
+ ` <contact_info>
105
+ <name>${info.name}</name>
106
+ <value>${info.value}</value>
107
+ <type>${info.type}</type>
108
+ ${
109
+ info.description
110
+ ? `<description>${info.description}</description>`
111
+ : ''
112
+ }
113
+ </contact_info>`
114
+ )
68
115
  .join('\n')
69
- return `<contact_info>\n${structuredContactInfo}\n</contact_info>`
116
+ return `<contact_info_fields>\n${structuredContactInfo}</contact_info_fields>`
70
117
  }
71
118
 
72
119
  private getMetadataInstructions(): string {
@@ -74,6 +121,13 @@ export class AIAgentBuilder<
74
121
  return `<metadata>\n${metadata}\n</metadata>`
75
122
  }
76
123
 
124
+ private getCampaignInstructions(campaignContext?: CampaignV2): string {
125
+ if (!campaignContext || !campaignContext.agent_context) {
126
+ return ''
127
+ }
128
+ return `<campaign_context>\n${campaignContext.agent_context}\n</campaign_context>`
129
+ }
130
+
77
131
  private getOutputInstructions(): string {
78
132
  const example = {
79
133
  messages: [
package/src/constants.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  export const HUBTYPE_API_URL =
2
2
  process.env.HUBTYPE_API_URL || 'https://api.hubtype.com'
3
3
 
4
+ // OpenAI Provider Configuration
5
+ export const OPENAI_API_KEY = process.env.OPENAI_API_KEY // pragma: allowlist secret
6
+ export const OPENAI_MODEL = process.env.OPENAI_MODEL || 'gpt-4.1-mini'
7
+ export const OPENAI_PROVIDER: 'openai' | 'azure' =
8
+ (process.env.OPENAI_PROVIDER as 'openai' | 'azure') || 'azure'
9
+
10
+ // Azure OpenAI Configuration
4
11
  export const AZURE_OPENAI_API_KEY = process.env.AZURE_OPENAI_API_KEY // pragma: allowlist secret
5
12
  export const AZURE_OPENAI_API_BASE = process.env.AZURE_OPENAI_API_BASE
6
13
  export const AZURE_OPENAI_API_DEPLOYMENT_NAME =
@@ -22,7 +22,7 @@ export function createInputGuardrail(rules: GuardrailRule[]): InputGuardrail {
22
22
  execute: async ({ input, context }) => {
23
23
  const lastMessage = input[input.length - 1] as UserMessageItem
24
24
  const result = await run(agent, [lastMessage], { context })
25
- const finalOutput = result.finalOutput
25
+ const finalOutput = result.finalOutput as Record<string, boolean>
26
26
  if (finalOutput === undefined) {
27
27
  throw new Error('Guardrail agent failed to produce output')
28
28
  }
@@ -17,6 +17,71 @@ interface HubtypeUserMessage {
17
17
 
18
18
  type HubtypeMessage = HubtypeAssistantMessage | HubtypeUserMessage
19
19
 
20
+ // V2 API Types
21
+ interface HubtypeToolCall {
22
+ id: string
23
+ type: 'function'
24
+ function: {
25
+ name: string
26
+ arguments: string
27
+ }
28
+ }
29
+
30
+ interface HubtypeUserMessageV2 {
31
+ role: 'user'
32
+ content: string | null
33
+ }
34
+
35
+ interface HubtypeAssistantMessageV2 {
36
+ role: 'assistant'
37
+ content: string | null
38
+ tool_calls?: HubtypeToolCall[] | null
39
+ }
40
+
41
+ interface HubtypeToolMessageV2 {
42
+ role: 'tool'
43
+ content: string | null
44
+ tool_call_id: string
45
+ }
46
+
47
+ interface HubtypeSystemMessageV2 {
48
+ role: 'system'
49
+ content: string | null
50
+ }
51
+
52
+ type HubtypeMessageV2 =
53
+ | HubtypeUserMessageV2
54
+ | HubtypeAssistantMessageV2
55
+ | HubtypeToolMessageV2
56
+ | HubtypeSystemMessageV2
57
+
58
+ interface MessageHistoryResponseV2 {
59
+ messages: HubtypeMessageV2[]
60
+ conversation_id: string | null
61
+ truncated: boolean
62
+ }
63
+
64
+ export interface GetMessagesV2Options {
65
+ maxMessages?: number
66
+ includeToolCalls?: boolean
67
+ maxFullToolResults?: number
68
+ debugMode?: boolean
69
+ }
70
+
71
+ export interface GetMessagesV2Result {
72
+ messages: AgenticInputMessage[]
73
+ conversationId: string | null
74
+ truncated: boolean
75
+ }
76
+
77
+ interface MessageHistoryV2Params {
78
+ last_message_id: string
79
+ max_messages?: number
80
+ include_tool_calls?: boolean
81
+ max_full_tool_results?: number
82
+ debug_mode?: boolean
83
+ }
84
+
20
85
  export class HubtypeApiClient {
21
86
  private readonly authToken: string
22
87
 
@@ -92,6 +157,117 @@ export class HubtypeApiClient {
92
157
  }
93
158
  }
94
159
 
160
+ async getMessagesV2(
161
+ request: BotContext,
162
+ options: GetMessagesV2Options = {}
163
+ ): Promise<GetMessagesV2Result> {
164
+ const url = `${HUBTYPE_API_URL}/external/v2/ai/agent/message_history/`
165
+ const headers = {
166
+ 'Content-Type': 'application/json',
167
+ Authorization: `Bearer ${this.authToken}`,
168
+ }
169
+ const params: MessageHistoryV2Params = {
170
+ last_message_id: request.input.message_id,
171
+ ...(options.maxMessages !== undefined && {
172
+ max_messages: options.maxMessages,
173
+ }),
174
+ ...(options.includeToolCalls !== undefined && {
175
+ include_tool_calls: options.includeToolCalls,
176
+ }),
177
+ ...(options.maxFullToolResults !== undefined && {
178
+ max_full_tool_results: options.maxFullToolResults,
179
+ }),
180
+ ...(options.debugMode !== undefined && {
181
+ debug_mode: options.debugMode,
182
+ }),
183
+ }
184
+
185
+ try {
186
+ const response = await axios.get<MessageHistoryResponseV2>(url, {
187
+ headers,
188
+ params,
189
+ })
190
+ const { messages, conversation_id, truncated } = response.data
191
+ const formattedMessages = messages
192
+ .map(message => this.formatMessageV2(message))
193
+ .filter((message): message is AgenticInputMessage => message !== null)
194
+ return {
195
+ messages: formattedMessages,
196
+ conversationId: conversation_id,
197
+ truncated,
198
+ }
199
+ } catch (error) {
200
+ console.error(error)
201
+ throw new Error('Failed to get messages from Hubtype V2 API')
202
+ }
203
+ }
204
+
205
+ private formatMessageV2(
206
+ message: HubtypeMessageV2
207
+ ): AgenticInputMessage | null {
208
+ switch (message.role) {
209
+ case 'user':
210
+ return {
211
+ role: 'user',
212
+ content: message.content ?? '',
213
+ }
214
+ case 'assistant': {
215
+ const assistantMessage = message as HubtypeAssistantMessageV2
216
+ // If assistant message has tool_calls, include them for context
217
+ // Using double assertion as the OpenAI API supports this format,
218
+ // but the agents SDK types are more restrictive
219
+ if (
220
+ assistantMessage.tool_calls &&
221
+ assistantMessage.tool_calls.length > 0
222
+ ) {
223
+ return {
224
+ role: 'assistant',
225
+ content: assistantMessage.content ?? '',
226
+ tool_calls: assistantMessage.tool_calls.map(tc => ({
227
+ id: tc.id,
228
+ type: tc.type,
229
+ function: {
230
+ name: tc.function.name,
231
+ arguments: tc.function.arguments,
232
+ },
233
+ })),
234
+ } as unknown as AgenticInputMessage
235
+ }
236
+ // Regular assistant message without tool_calls
237
+ return {
238
+ role: 'assistant',
239
+ content: [
240
+ {
241
+ type: 'output_text',
242
+ text: assistantMessage.content ?? '',
243
+ },
244
+ ],
245
+ status: 'completed',
246
+ }
247
+ }
248
+ case 'tool': {
249
+ // Tool messages provide context about previous tool executions
250
+ // Using double assertion as the OpenAI API supports this format,
251
+ // but the agents SDK types are more restrictive
252
+ const toolMessage = message as HubtypeToolMessageV2
253
+ return {
254
+ role: 'tool',
255
+ tool_call_id: toolMessage.tool_call_id,
256
+ content: toolMessage.content ?? '',
257
+ } as unknown as AgenticInputMessage
258
+ }
259
+ case 'system':
260
+ return {
261
+ role: 'system',
262
+ content: message.content ?? '',
263
+ }
264
+ default:
265
+ throw new Error(
266
+ `Invalid message role: ${(message as HubtypeMessageV2).role}`
267
+ )
268
+ }
269
+ }
270
+
95
271
  private formatMessage(message: HubtypeMessage): AgenticInputMessage {
96
272
  if (message.role === 'user') {
97
273
  return {
package/src/index.ts CHANGED
@@ -11,6 +11,8 @@ import {
11
11
  Context,
12
12
  CustomTool,
13
13
  InferenceResponse,
14
+ MemoryOptions,
15
+ MessageHistoryApiVersion,
14
16
  PluginAiAgentOptions,
15
17
  Tool,
16
18
  } from './types'
@@ -21,12 +23,24 @@ export default class BotonicPluginAiAgents<
21
23
  > implements Plugin
22
24
  {
23
25
  private readonly authToken?: string
26
+ private readonly messageHistoryApiVersion: MessageHistoryApiVersion
27
+ private readonly memory: MemoryOptions
24
28
  public toolDefinitions: CustomTool<TPlugins, TExtraData>[] = []
25
29
 
26
30
  constructor(options?: PluginAiAgentOptions<TPlugins, TExtraData>) {
27
31
  setUpOpenAI(options?.maxRetries, options?.timeout)
32
+
33
+ if (options?.messageHistoryApiVersion === 'v1' && options?.memory) {
34
+ throw new Error(
35
+ 'Cannot use memory when messageHistoryApiVersion is "v1". ' +
36
+ 'Either set messageHistoryApiVersion to "v2" or remove memory.'
37
+ )
38
+ }
39
+
28
40
  this.authToken = options?.authToken
29
41
  this.toolDefinitions = options?.customTools || []
42
+ this.messageHistoryApiVersion = options?.messageHistoryApiVersion ?? 'v2'
43
+ this.memory = options?.memory ?? {}
30
44
  }
31
45
 
32
46
  pre(): void {
@@ -46,14 +60,15 @@ export default class BotonicPluginAiAgents<
46
60
  const tools = this.buildTools(
47
61
  aiAgentArgs.activeTools?.map(tool => tool.name) || []
48
62
  )
49
- const agent = new AIAgentBuilder<TPlugins, TExtraData>(
50
- aiAgentArgs.name,
51
- aiAgentArgs.instructions,
52
- tools,
53
- request.session.user.contact_info || {},
54
- aiAgentArgs.inputGuardrailRules || [],
55
- aiAgentArgs.sourceIds || []
56
- ).build()
63
+ const agent = new AIAgentBuilder<TPlugins, TExtraData>({
64
+ name: aiAgentArgs.name,
65
+ instructions: aiAgentArgs.instructions,
66
+ tools: tools,
67
+ contactInfo: request.session.user.contact_info || [],
68
+ inputGuardrailRules: aiAgentArgs.inputGuardrailRules || [],
69
+ sourceIds: aiAgentArgs.sourceIds || [],
70
+ campaignContext: request.input.context?.campaign_v2,
71
+ }).build()
57
72
 
58
73
  const messages = await this.getMessages(
59
74
  request,
@@ -93,12 +108,24 @@ export default class BotonicPluginAiAgents<
93
108
  authToken: string,
94
109
  memoryLength: number
95
110
  ): Promise<AgenticInputMessage[]> {
96
- if (isProd) {
97
- const hubtypeClient = new HubtypeApiClient(authToken)
111
+ const hubtypeClient = new HubtypeApiClient(authToken)
112
+
113
+ if (!isProd) {
114
+ return await hubtypeClient.getLocalMessages(memoryLength)
115
+ }
116
+
117
+ if (this.messageHistoryApiVersion === 'v1') {
98
118
  return await hubtypeClient.getMessages(request, memoryLength)
99
119
  }
100
- const hubtypeClient = new HubtypeApiClient(authToken)
101
- return await hubtypeClient.getLocalMessages(memoryLength)
120
+
121
+ // Default to V2
122
+ const result = await hubtypeClient.getMessagesV2(request, {
123
+ maxMessages: this.memory.maxMessages ?? memoryLength,
124
+ includeToolCalls: this.memory.includeToolCalls ?? true,
125
+ maxFullToolResults: this.memory.maxFullToolResults ?? 1,
126
+ debugMode: this.memory.debugMode ?? false,
127
+ })
128
+ return result.messages
102
129
  }
103
130
 
104
131
  private buildTools(activeToolNames: string[]): Tool<TPlugins, TExtraData>[] {
package/src/openai.ts CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  setOpenAIAPI,
4
4
  setTracingDisabled,
5
5
  } from '@openai/agents'
6
- import { AzureOpenAI } from 'openai'
6
+ import OpenAI, { AzureOpenAI } from 'openai'
7
7
 
8
8
  import {
9
9
  AZURE_OPENAI_API_BASE,
@@ -11,21 +11,37 @@ import {
11
11
  AZURE_OPENAI_API_KEY,
12
12
  AZURE_OPENAI_API_VERSION,
13
13
  isProd,
14
+ OPENAI_API_KEY,
15
+ OPENAI_PROVIDER,
14
16
  } from './constants'
15
17
 
16
18
  export function setUpOpenAI(maxRetries?: number, timeout?: number) {
17
- setAzureOpenAIClient(maxRetries, timeout)
19
+ if (OPENAI_PROVIDER === 'azure') {
20
+ setAzureOpenAIClient(maxRetries, timeout)
21
+ } else {
22
+ setOpenAIClient(maxRetries, timeout)
23
+ }
18
24
  setOpenAIAPI('chat_completions')
19
25
  setTracingDisabled(true)
20
26
  }
21
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
+
22
38
  function setAzureOpenAIClient(maxRetries?: number, timeout?: number) {
23
39
  const client = new AzureOpenAI({
24
40
  apiKey: AZURE_OPENAI_API_KEY,
25
41
  apiVersion: AZURE_OPENAI_API_VERSION,
26
42
  deployment: AZURE_OPENAI_API_DEPLOYMENT_NAME,
27
43
  baseURL: AZURE_OPENAI_API_BASE,
28
- timeout: timeout || 8000, // 8 seconds
44
+ timeout: timeout || 16000, // 16 seconds
29
45
  maxRetries: maxRetries || 2,
30
46
  dangerouslyAllowBrowser: !isProd,
31
47
  })
package/src/runner.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ResolvedPlugins, ToolExecution } from '@botonic/core'
1
+ import { OutputMessage, ResolvedPlugins, ToolExecution } from '@botonic/core'
2
2
  import {
3
3
  InputGuardrailTripwireTriggered,
4
4
  Runner,
@@ -14,6 +14,15 @@ import {
14
14
  RunResult,
15
15
  } from './types'
16
16
 
17
+ // Minimal interface matching the properties we actually use from Runner.run() result
18
+ // This bypasses strict type checking while maintaining type safety for accessed properties
19
+ interface AIAgentRunnerResult {
20
+ finalOutput?: {
21
+ messages?: OutputMessage[]
22
+ }
23
+ newItems?: RunToolCallItem[]
24
+ }
25
+
17
26
  export class AIAgentRunner<
18
27
  TPlugins extends ResolvedPlugins = ResolvedPlugins,
19
28
  TExtraData = any,
@@ -32,7 +41,11 @@ export class AIAgentRunner<
32
41
  const runner = new Runner({
33
42
  modelSettings: { temperature: 0 },
34
43
  })
35
- const result = await runner.run(this.agent, messages, { context })
44
+ // Type assertion to bypass strict type checking - the actual return type from runner.run()
45
+ // doesn't perfectly match our interface, but the properties we access are compatible
46
+ const result = (await runner.run(this.agent, messages, {
47
+ context,
48
+ })) as AIAgentRunnerResult
36
49
 
37
50
  const outputMessages = result.finalOutput?.messages || []
38
51
  const hasExit =
@@ -4,7 +4,7 @@ import { z } from 'zod'
4
4
  import { HubtypeApiClient } from '../hubtype-api-client'
5
5
  import { Context } from '../types'
6
6
 
7
- export const retrieveKnowledge = tool<any, Context, any>({
7
+ export const retrieveKnowledge = tool({
8
8
  name: 'retrieve_knowledge',
9
9
  description:
10
10
  'Consult the knowledge base for information before answering. Use this tool to make sure the information you provide is faithful.',
@@ -12,10 +12,11 @@ export const retrieveKnowledge = tool<any, Context, any>({
12
12
  query: z.string().describe('The query to search the knowledge base for'),
13
13
  }),
14
14
  execute: async (
15
- { query }: { query: string },
15
+ input: { query: string },
16
16
  runContext?: RunContext<Context>
17
17
  ): Promise<string[]> => {
18
18
  const context = runContext?.context
19
+ const query = input.query
19
20
  if (!context) {
20
21
  throw new Error('Context is required')
21
22
  }
package/src/types.ts CHANGED
@@ -10,6 +10,7 @@ import {
10
10
  import {
11
11
  Agent,
12
12
  AgentInputItem,
13
+ AgentOutputType,
13
14
  RunContext as OpenAIRunContext,
14
15
  Tool as OpenAITool,
15
16
  } from '@openai/agents'
@@ -55,16 +56,25 @@ export interface Chunk {
55
56
  text: string
56
57
  }
57
58
 
58
- export type ContactInfo = Record<string, string>
59
-
60
59
  export type Tool<
61
60
  TPlugins extends ResolvedPlugins = ResolvedPlugins,
62
61
  TExtraData = any,
63
62
  > = OpenAITool<Context<TPlugins, TExtraData>>
63
+
64
64
  export type AIAgent<
65
65
  TPlugins extends ResolvedPlugins = ResolvedPlugins,
66
66
  TExtraData = any,
67
- > = Agent<Context<TPlugins, TExtraData>, typeof OutputSchema>
67
+ > = Agent<Context<TPlugins, TExtraData>, AgentOutputType<typeof OutputSchema>>
68
+
69
+ export type MessageHistoryApiVersion = 'v1' | 'v2'
70
+
71
+ export interface MemoryOptions {
72
+ maxMessages?: number
73
+ includeToolCalls?: boolean
74
+ maxFullToolResults?: number
75
+ debugMode?: boolean
76
+ }
77
+
68
78
  export interface PluginAiAgentOptions<
69
79
  TPlugins extends ResolvedPlugins = ResolvedPlugins,
70
80
  TExtraData = any,
@@ -73,6 +83,10 @@ export interface PluginAiAgentOptions<
73
83
  customTools?: CustomTool<TPlugins, TExtraData>[]
74
84
  maxRetries?: number
75
85
  timeout?: number
86
+ /** API version for message history endpoint. Defaults to 'v2'. */
87
+ messageHistoryApiVersion?: MessageHistoryApiVersion
88
+ /** Options for V2 message history API. Only used when messageHistoryApiVersion is 'v2'. */
89
+ memory?: MemoryOptions
76
90
  }
77
91
 
78
92
  export type AgenticInputMessage = AgentInputItem