@botonic/plugin-ai-agents 0.43.0-beta.0 → 0.44.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.
Files changed (65) hide show
  1. package/lib/cjs/agent-builder.d.ts +14 -3
  2. package/lib/cjs/agent-builder.js +43 -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 +4 -6
  8. package/lib/cjs/guardrails/input.js.map +1 -1
  9. package/lib/cjs/hubtype-api-client.js +97 -106
  10. package/lib/cjs/hubtype-api-client.js.map +1 -1
  11. package/lib/cjs/index.js +63 -62
  12. package/lib/cjs/index.js.map +1 -1
  13. package/lib/cjs/openai.js +19 -5
  14. package/lib/cjs/openai.js.map +1 -1
  15. package/lib/cjs/runner.js +39 -38
  16. package/lib/cjs/runner.js.map +1 -1
  17. package/lib/cjs/tools/retrieve-knowledge.d.ts +8 -1
  18. package/lib/cjs/tools/retrieve-knowledge.js +5 -5
  19. package/lib/cjs/tools/retrieve-knowledge.js.map +1 -1
  20. package/lib/cjs/types.d.ts +2 -3
  21. package/lib/esm/agent-builder.d.ts +14 -3
  22. package/lib/esm/agent-builder.js +57 -23
  23. package/lib/esm/agent-builder.js.map +1 -1
  24. package/lib/esm/constants.d.ts +3 -0
  25. package/lib/esm/constants.js +15 -7
  26. package/lib/esm/constants.js.map +1 -1
  27. package/lib/esm/guardrails/index.js +4 -1
  28. package/lib/esm/guardrails/index.js.map +1 -1
  29. package/lib/esm/guardrails/input.js +11 -9
  30. package/lib/esm/guardrails/input.js.map +1 -1
  31. package/lib/esm/hubtype-api-client.js +105 -110
  32. package/lib/esm/hubtype-api-client.js.map +1 -1
  33. package/lib/esm/index.js +76 -72
  34. package/lib/esm/index.js.map +1 -1
  35. package/lib/esm/openai.js +33 -15
  36. package/lib/esm/openai.js.map +1 -1
  37. package/lib/esm/runner.js +47 -42
  38. package/lib/esm/runner.js.map +1 -1
  39. package/lib/esm/structured-output/carousel.js +15 -12
  40. package/lib/esm/structured-output/carousel.js.map +1 -1
  41. package/lib/esm/structured-output/exit.js +7 -3
  42. package/lib/esm/structured-output/exit.js.map +1 -1
  43. package/lib/esm/structured-output/index.js +10 -7
  44. package/lib/esm/structured-output/index.js.map +1 -1
  45. package/lib/esm/structured-output/text-with-buttons.js +12 -8
  46. package/lib/esm/structured-output/text-with-buttons.js.map +1 -1
  47. package/lib/esm/structured-output/text.js +8 -5
  48. package/lib/esm/structured-output/text.js.map +1 -1
  49. package/lib/esm/tools/index.js +6 -2
  50. package/lib/esm/tools/index.js.map +1 -1
  51. package/lib/esm/tools/retrieve-knowledge.d.ts +8 -1
  52. package/lib/esm/tools/retrieve-knowledge.js +15 -12
  53. package/lib/esm/tools/retrieve-knowledge.js.map +1 -1
  54. package/lib/esm/types.d.ts +2 -3
  55. package/lib/esm/types.js +2 -1
  56. package/package.json +5 -5
  57. package/src/agent-builder.ts +79 -26
  58. package/src/constants.ts +7 -0
  59. package/src/guardrails/input.ts +1 -1
  60. package/src/hubtype-api-client.ts +21 -14
  61. package/src/index.ts +10 -10
  62. package/src/openai.ts +19 -3
  63. package/src/runner.ts +15 -6
  64. package/src/tools/retrieve-knowledge.ts +3 -2
  65. package/src/types.ts +4 -3
@@ -74,6 +74,14 @@ export interface GetMessagesV2Result {
74
74
  truncated: boolean
75
75
  }
76
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
+
77
85
  export class HubtypeApiClient {
78
86
  private readonly authToken: string
79
87
 
@@ -158,21 +166,20 @@ export class HubtypeApiClient {
158
166
  'Content-Type': 'application/json',
159
167
  Authorization: `Bearer ${this.authToken}`,
160
168
  }
161
- const params: Record<string, string | number | boolean> = {
169
+ const params: MessageHistoryV2Params = {
162
170
  last_message_id: request.input.message_id,
163
- }
164
-
165
- if (options.maxMessages !== undefined) {
166
- params.max_messages = options.maxMessages
167
- }
168
- if (options.includeToolCalls !== undefined) {
169
- params.include_tool_calls = options.includeToolCalls
170
- }
171
- if (options.maxFullToolResults !== undefined) {
172
- params.max_full_tool_results = options.maxFullToolResults
173
- }
174
- if (options.debugMode !== undefined) {
175
- params.debug_mode = options.debugMode
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
+ }),
176
183
  }
177
184
 
178
185
  try {
package/src/index.ts CHANGED
@@ -20,8 +20,7 @@ import {
20
20
  export default class BotonicPluginAiAgents<
21
21
  TPlugins extends ResolvedPlugins = ResolvedPlugins,
22
22
  TExtraData = any,
23
- > implements Plugin
24
- {
23
+ > implements Plugin {
25
24
  private readonly authToken?: string
26
25
  private readonly messageHistoryApiVersion: MessageHistoryApiVersion
27
26
  private readonly memory: MemoryOptions
@@ -60,14 +59,15 @@ export default class BotonicPluginAiAgents<
60
59
  const tools = this.buildTools(
61
60
  aiAgentArgs.activeTools?.map(tool => tool.name) || []
62
61
  )
63
- const agent = new AIAgentBuilder<TPlugins, TExtraData>(
64
- aiAgentArgs.name,
65
- aiAgentArgs.instructions,
66
- tools,
67
- request.session.user.contact_info || {},
68
- aiAgentArgs.inputGuardrailRules || [],
69
- aiAgentArgs.sourceIds || []
70
- ).build()
62
+ const agent = new AIAgentBuilder<TPlugins, TExtraData>({
63
+ name: aiAgentArgs.name,
64
+ instructions: aiAgentArgs.instructions,
65
+ tools: tools,
66
+ contactInfo: request.session.user.contact_info || [],
67
+ inputGuardrailRules: aiAgentArgs.inputGuardrailRules || [],
68
+ sourceIds: aiAgentArgs.sourceIds || [],
69
+ campaignContext: request.input.context?.campaign_v2,
70
+ }).build()
71
71
 
72
72
  const messages = await this.getMessages(
73
73
  request,
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,
@@ -28,15 +37,15 @@ export class AIAgentRunner<
28
37
  messages: AgenticInputMessage[],
29
38
  context: Context<TPlugins, TExtraData>
30
39
  ): Promise<RunResult> {
31
- console.log(
32
- 'Running agent with messages',
33
- JSON.stringify(messages, null, 2)
34
- )
35
40
  try {
36
41
  const runner = new Runner({
37
42
  modelSettings: { temperature: 0 },
38
43
  })
39
- 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
40
49
 
41
50
  const outputMessages = result.finalOutput?.messages || []
42
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,16 @@ 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
+
68
69
  export type MessageHistoryApiVersion = 'v1' | 'v2'
69
70
 
70
71
  export interface MemoryOptions {