@botonic/plugin-ai-agents 0.36.0-alpha.0 → 0.36.0-alpha.10

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 (110) hide show
  1. package/README.md +14 -0
  2. package/lib/cjs/agent-builder.d.ts +10 -0
  3. package/lib/cjs/agent-builder.js +43 -0
  4. package/lib/cjs/agent-builder.js.map +1 -0
  5. package/lib/cjs/constants.d.ts +5 -0
  6. package/lib/cjs/constants.js +6 -1
  7. package/lib/cjs/constants.js.map +1 -1
  8. package/lib/cjs/hubtype-api-client.d.ts +9 -0
  9. package/lib/cjs/hubtype-api-client.js +76 -0
  10. package/lib/cjs/hubtype-api-client.js.map +1 -0
  11. package/lib/cjs/index.d.ts +8 -5
  12. package/lib/cjs/index.js +52 -4
  13. package/lib/cjs/index.js.map +1 -1
  14. package/lib/cjs/openai.d.ts +1 -0
  15. package/lib/cjs/openai.js +23 -0
  16. package/lib/cjs/openai.js.map +1 -0
  17. package/lib/cjs/runner.d.ts +7 -0
  18. package/lib/cjs/runner.js +35 -0
  19. package/lib/cjs/runner.js.map +1 -0
  20. package/lib/cjs/structured-output/carousel.d.ts +100 -0
  21. package/lib/cjs/structured-output/carousel.js +21 -0
  22. package/lib/cjs/structured-output/carousel.js.map +1 -0
  23. package/lib/cjs/structured-output/exit.d.ts +12 -0
  24. package/lib/cjs/structured-output/exit.js +11 -0
  25. package/lib/cjs/structured-output/exit.js.map +1 -0
  26. package/lib/cjs/structured-output/index.d.ts +198 -0
  27. package/lib/cjs/structured-output/index.js +14 -0
  28. package/lib/cjs/structured-output/index.js.map +1 -0
  29. package/lib/cjs/structured-output/shared.d.ts +3 -0
  30. package/lib/cjs/structured-output/shared.js +3 -0
  31. package/lib/cjs/structured-output/shared.js.map +1 -0
  32. package/lib/cjs/structured-output/text-with-buttons.d.ts +34 -0
  33. package/lib/cjs/structured-output/text-with-buttons.js +15 -0
  34. package/lib/cjs/structured-output/text-with-buttons.js.map +1 -0
  35. package/lib/cjs/structured-output/text.d.ts +28 -0
  36. package/lib/cjs/structured-output/text.js +13 -0
  37. package/lib/cjs/structured-output/text.js.map +1 -0
  38. package/lib/cjs/tools/index.d.ts +2 -0
  39. package/lib/cjs/tools/index.js +5 -0
  40. package/lib/cjs/tools/index.js.map +1 -0
  41. package/lib/cjs/types.d.ts +21 -25
  42. package/lib/esm/agent-builder.d.ts +10 -0
  43. package/lib/esm/agent-builder.js +39 -0
  44. package/lib/esm/agent-builder.js.map +1 -0
  45. package/lib/esm/constants.d.ts +5 -0
  46. package/lib/esm/constants.js +5 -0
  47. package/lib/esm/constants.js.map +1 -1
  48. package/lib/esm/hubtype-api-client.d.ts +9 -0
  49. package/lib/esm/hubtype-api-client.js +72 -0
  50. package/lib/esm/hubtype-api-client.js.map +1 -0
  51. package/lib/esm/index.d.ts +8 -5
  52. package/lib/esm/index.js +52 -4
  53. package/lib/esm/index.js.map +1 -1
  54. package/lib/esm/openai.d.ts +1 -0
  55. package/lib/esm/openai.js +19 -0
  56. package/lib/esm/openai.js.map +1 -0
  57. package/lib/esm/runner.d.ts +7 -0
  58. package/lib/esm/runner.js +31 -0
  59. package/lib/esm/runner.js.map +1 -0
  60. package/lib/esm/structured-output/carousel.d.ts +100 -0
  61. package/lib/esm/structured-output/carousel.js +18 -0
  62. package/lib/esm/structured-output/carousel.js.map +1 -0
  63. package/lib/esm/structured-output/exit.d.ts +12 -0
  64. package/lib/esm/structured-output/exit.js +7 -0
  65. package/lib/esm/structured-output/exit.js.map +1 -0
  66. package/lib/esm/structured-output/index.d.ts +198 -0
  67. package/lib/esm/structured-output/index.js +11 -0
  68. package/lib/esm/structured-output/index.js.map +1 -0
  69. package/lib/esm/structured-output/shared.d.ts +3 -0
  70. package/lib/esm/structured-output/shared.js +2 -0
  71. package/lib/esm/structured-output/shared.js.map +1 -0
  72. package/lib/esm/structured-output/text-with-buttons.d.ts +34 -0
  73. package/lib/esm/structured-output/text-with-buttons.js +11 -0
  74. package/lib/esm/structured-output/text-with-buttons.js.map +1 -0
  75. package/lib/esm/structured-output/text.d.ts +28 -0
  76. package/lib/esm/structured-output/text.js +10 -0
  77. package/lib/esm/structured-output/text.js.map +1 -0
  78. package/lib/esm/tools/index.d.ts +2 -0
  79. package/lib/esm/tools/index.js +2 -0
  80. package/lib/esm/tools/index.js.map +1 -0
  81. package/lib/esm/types.d.ts +21 -25
  82. package/package.json +7 -4
  83. package/src/agent-builder.ts +48 -0
  84. package/src/constants.ts +9 -0
  85. package/src/hubtype-api-client.ts +89 -0
  86. package/src/index.ts +77 -7
  87. package/src/openai.ts +30 -0
  88. package/src/runner.ts +43 -0
  89. package/src/structured-output/carousel.ts +36 -0
  90. package/src/structured-output/exit.ts +15 -0
  91. package/src/structured-output/index.ts +27 -0
  92. package/src/structured-output/shared.ts +3 -0
  93. package/src/structured-output/text-with-buttons.ts +23 -0
  94. package/src/structured-output/text.ts +19 -0
  95. package/src/tools/index.ts +3 -0
  96. package/src/types.ts +27 -23
  97. package/lib/cjs/ai-agent-client.d.ts +0 -12
  98. package/lib/cjs/ai-agent-client.js +0 -78
  99. package/lib/cjs/ai-agent-client.js.map +0 -1
  100. package/lib/cjs/errors.d.ts +0 -3
  101. package/lib/cjs/errors.js +0 -11
  102. package/lib/cjs/errors.js.map +0 -1
  103. package/lib/esm/ai-agent-client.d.ts +0 -12
  104. package/lib/esm/ai-agent-client.js +0 -74
  105. package/lib/esm/ai-agent-client.js.map +0 -1
  106. package/lib/esm/errors.d.ts +0 -3
  107. package/lib/esm/errors.js +0 -7
  108. package/lib/esm/errors.js.map +0 -1
  109. package/src/ai-agent-client.ts +0 -112
  110. package/src/errors.ts +0 -6
@@ -0,0 +1,48 @@
1
+ import { Agent } from '@openai/agents'
2
+
3
+ import { OutputSchema } from './structured-output'
4
+ import { mandatoryTools } from './tools'
5
+ import { AIAgent, Tool } from './types'
6
+
7
+ export class AIAgentBuilder {
8
+ private name: string
9
+ private instructions: string
10
+ private tools: Tool[]
11
+
12
+ constructor(name: string, instructions: string, tools: Tool[]) {
13
+ this.name = name
14
+ this.instructions = this.addExtraInstructions(instructions)
15
+ this.tools = this.addHubtypeTools(tools)
16
+ }
17
+
18
+ build(): AIAgent {
19
+ return new Agent({
20
+ name: this.name,
21
+ instructions: this.instructions,
22
+ tools: this.tools,
23
+ outputType: OutputSchema,
24
+ })
25
+ }
26
+
27
+ private addExtraInstructions(instructions: string): string {
28
+ const metadata = `Current Date: ${new Date().toISOString()}`
29
+ const example = {
30
+ messages: [
31
+ {
32
+ type: 'text',
33
+ content: {
34
+ text: 'Hello, how can I help you today?',
35
+ },
36
+ },
37
+ ],
38
+ numMessages: 1,
39
+ }
40
+ const output = `Return a JSON that follows the output schema provided. Never return multiple output schemas concatenated by a line break.\n<example>\n${JSON.stringify(example)}\n</example>`
41
+ return `<instructions>\n${instructions}\n</instructions>\n\n<metadata>\n${metadata}\n</metadata>\n\n<output>\n${output}\n</output>`
42
+ }
43
+
44
+ private addHubtypeTools(tools: Tool[]): Tool[] {
45
+ const hubtypeTools: Tool[] = [...mandatoryTools]
46
+ return [...hubtypeTools, ...tools]
47
+ }
48
+ }
package/src/constants.ts CHANGED
@@ -1,2 +1,11 @@
1
1
  export const HUBTYPE_API_URL =
2
2
  process.env.HUBTYPE_API_URL || 'https://api.hubtype.com'
3
+
4
+ export const AZURE_OPENAI_API_KEY = process.env.AZURE_OPENAI_API_KEY // pragma: allowlist secret
5
+ export const AZURE_OPENAI_API_BASE = process.env.AZURE_OPENAI_API_BASE
6
+ export const AZURE_OPENAI_API_DEPLOYMENT_NAME =
7
+ process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME || 'gpt-4o-mini_p1'
8
+ export const AZURE_OPENAI_API_VERSION =
9
+ process.env.AZURE_OPENAI_API_VERSION || '2024-12-01-preview'
10
+
11
+ export const isProd = process.env.NODE_ENV === 'production'
@@ -0,0 +1,89 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention */
2
+ import { BotContext } from '@botonic/core'
3
+ import axios from 'axios'
4
+
5
+ import { HUBTYPE_API_URL } from './constants'
6
+ import { AgenticInputMessage } from './types'
7
+
8
+ interface HubtypeAssistantMessage {
9
+ role: 'assistant'
10
+ content: string
11
+ }
12
+
13
+ interface HubtypeUserMessage {
14
+ role: 'user'
15
+ content: string
16
+ }
17
+
18
+ type HubtypeMessage = HubtypeAssistantMessage | HubtypeUserMessage
19
+
20
+ export class HubtypeApiClient {
21
+ private readonly authToken: string
22
+
23
+ constructor(authToken: string) {
24
+ this.authToken = authToken
25
+ }
26
+
27
+ async getLocalMessages(memoryLength: number): Promise<AgenticInputMessage[]> {
28
+ const localBotonicState = localStorage.getItem('botonicState')
29
+ const botonicState = JSON.parse(localBotonicState || '{}')
30
+ const messages = botonicState.messages
31
+ const filteredMessages = messages
32
+ .filter(message => message.data.text)
33
+ .map(message => ({
34
+ role: message.sentBy === 'user' ? 'user' : 'assistant',
35
+ content: message.data.text,
36
+ }))
37
+ .map(message => this.formatMessage(message))
38
+ return filteredMessages.slice(-memoryLength)
39
+ }
40
+
41
+ async getMessages(
42
+ request: BotContext,
43
+ memoryLength: number
44
+ ): Promise<AgenticInputMessage[]> {
45
+ const url = `${HUBTYPE_API_URL}/external/v1/ai/agent/message_history/`
46
+ const headers = {
47
+ 'Content-Type': 'application/json',
48
+ Authorization: `Bearer ${this.authToken}`,
49
+ }
50
+ const params = {
51
+ last_message_id: request.input.message_id,
52
+ num_messages: memoryLength,
53
+ }
54
+
55
+ try {
56
+ const response = await axios.get<{ messages: HubtypeMessage[] }>(url, {
57
+ headers,
58
+ params,
59
+ })
60
+ const messages = response.data.messages
61
+ return messages.map(message => this.formatMessage(message))
62
+ } catch (error) {
63
+ console.error(error)
64
+ throw new Error('Failed to get messages from Hubtype')
65
+ }
66
+ }
67
+
68
+ private formatMessage(message: HubtypeMessage): AgenticInputMessage {
69
+ if (message.role === 'user') {
70
+ return {
71
+ content: message.content,
72
+ role: 'user',
73
+ }
74
+ } else if (message.role === 'assistant') {
75
+ return {
76
+ role: 'assistant',
77
+ content: [
78
+ {
79
+ type: 'output_text',
80
+ text: message.content,
81
+ },
82
+ ],
83
+ status: 'completed',
84
+ }
85
+ } else {
86
+ throw new Error('Invalid message role')
87
+ }
88
+ }
89
+ }
package/src/index.ts CHANGED
@@ -1,13 +1,29 @@
1
1
  import { BotContext, Plugin } from '@botonic/core'
2
+ import { tool } from '@openai/agents'
2
3
 
3
- import { AiAgentClient } from './ai-agent-client'
4
- import { AiAgentArgs, AiAgentResponse } from './types'
4
+ import { AIAgentBuilder } from './agent-builder'
5
+ import { isProd } from './constants'
6
+ import { HubtypeApiClient } from './hubtype-api-client'
7
+ import { setUpOpenAI } from './openai'
8
+ import { AIAgentRunner } from './runner'
9
+ import {
10
+ AgenticInputMessage,
11
+ AgenticOutputMessage,
12
+ AiAgentArgs,
13
+ Context,
14
+ CustomTool,
15
+ PluginAiAgentOptions,
16
+ Tool,
17
+ } from './types'
5
18
 
6
19
  export default class BotonicPluginAiAgents implements Plugin {
7
- public aiAgentClient: AiAgentClient
20
+ private readonly authToken?: string
21
+ public toolDefinitions: CustomTool[] = []
8
22
 
9
- constructor() {
10
- this.aiAgentClient = new AiAgentClient()
23
+ constructor(options?: PluginAiAgentOptions) {
24
+ setUpOpenAI()
25
+ this.authToken = options?.authToken
26
+ this.toolDefinitions = options?.customTools || []
11
27
  }
12
28
 
13
29
  pre(): void {
@@ -17,7 +33,61 @@ export default class BotonicPluginAiAgents implements Plugin {
17
33
  async getInference(
18
34
  request: BotContext,
19
35
  aiAgentArgs: AiAgentArgs
20
- ): Promise<AiAgentResponse> {
21
- return this.aiAgentClient.getInference(request, aiAgentArgs)
36
+ ): Promise<AgenticOutputMessage[] | undefined> {
37
+ try {
38
+ const authToken = isProd ? request.session._access_token : this.authToken
39
+ if (!authToken) {
40
+ throw new Error('Auth token is required')
41
+ }
42
+
43
+ const tools = this.buildTools(
44
+ aiAgentArgs.activeTools?.map(tool => tool.name) || []
45
+ )
46
+ const agent = new AIAgentBuilder(
47
+ aiAgentArgs.name,
48
+ aiAgentArgs.instructions,
49
+ tools
50
+ ).build()
51
+
52
+ const messages = await this.getMessages(request, authToken, 25)
53
+ const context: Context = {
54
+ authToken,
55
+ }
56
+
57
+ const runner = new AIAgentRunner(agent)
58
+ return await runner.run(messages, context)
59
+ } catch (error) {
60
+ console.error('error plugin returns undefined', error)
61
+ return undefined
62
+ }
63
+ }
64
+
65
+ private async getMessages(
66
+ request: BotContext,
67
+ authToken: string,
68
+ memoryLength: number
69
+ ): Promise<AgenticInputMessage[]> {
70
+ if (isProd) {
71
+ const hubtypeClient = new HubtypeApiClient(authToken)
72
+ return await hubtypeClient.getMessages(request, memoryLength)
73
+ }
74
+ const hubtypeClient = new HubtypeApiClient(authToken)
75
+ return await hubtypeClient.getLocalMessages(memoryLength)
76
+ }
77
+
78
+ private buildTools(activeToolNames: string[]): Tool[] {
79
+ const availableTools = this.toolDefinitions.filter(tool =>
80
+ activeToolNames.includes(tool.name)
81
+ )
82
+ return availableTools.map(toolDefinition => {
83
+ return tool<any, Context, any>({
84
+ name: toolDefinition.name,
85
+ description: toolDefinition.description,
86
+ parameters: toolDefinition.schema,
87
+ execute: toolDefinition.func,
88
+ })
89
+ })
22
90
  }
23
91
  }
92
+
93
+ export * from './types'
package/src/openai.ts ADDED
@@ -0,0 +1,30 @@
1
+ import {
2
+ setDefaultOpenAIClient,
3
+ setOpenAIAPI,
4
+ setTracingDisabled,
5
+ } from '@openai/agents'
6
+ import { AzureOpenAI } from 'openai'
7
+ import {
8
+ AZURE_OPENAI_API_BASE,
9
+ AZURE_OPENAI_API_DEPLOYMENT_NAME,
10
+ AZURE_OPENAI_API_KEY,
11
+ AZURE_OPENAI_API_VERSION,
12
+ isProd,
13
+ } from './constants'
14
+
15
+ export function setUpOpenAI() {
16
+ setAzureOpenAIClient()
17
+ setOpenAIAPI('chat_completions')
18
+ setTracingDisabled(true)
19
+ }
20
+
21
+ function setAzureOpenAIClient() {
22
+ const client = new AzureOpenAI({
23
+ apiKey: AZURE_OPENAI_API_KEY,
24
+ apiVersion: AZURE_OPENAI_API_VERSION,
25
+ deployment: AZURE_OPENAI_API_DEPLOYMENT_NAME,
26
+ baseURL: AZURE_OPENAI_API_BASE,
27
+ dangerouslyAllowBrowser: !isProd,
28
+ })
29
+ setDefaultOpenAIClient(client)
30
+ }
package/src/runner.ts ADDED
@@ -0,0 +1,43 @@
1
+ import { Runner } from '@openai/agents'
2
+ import {
3
+ AIAgent,
4
+ AgenticInputMessage,
5
+ AgenticOutputMessage,
6
+ Context,
7
+ } from './types'
8
+
9
+ export class AIAgentRunner {
10
+ private agent: AIAgent
11
+
12
+ constructor(agent: AIAgent) {
13
+ this.agent = agent
14
+ }
15
+
16
+ async run(
17
+ messages: AgenticInputMessage[],
18
+ context: Context,
19
+ maxRetries: number = 1
20
+ ): Promise<AgenticOutputMessage[]> {
21
+ return this.runWithRetry(messages, context, maxRetries, 1)
22
+ }
23
+
24
+ private async runWithRetry(
25
+ messages: AgenticInputMessage[],
26
+ context: Context,
27
+ maxRetries: number,
28
+ attempt: number
29
+ ): Promise<AgenticOutputMessage[]> {
30
+ try {
31
+ const runner = new Runner({
32
+ modelSettings: { temperature: 0 },
33
+ })
34
+ const result = await runner.run(this.agent, messages, { context })
35
+ return result.finalOutput?.messages || []
36
+ } catch (error) {
37
+ if (attempt > maxRetries) {
38
+ throw error
39
+ }
40
+ return this.runWithRetry(messages, context, maxRetries, attempt + 1)
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,36 @@
1
+ import { z } from 'zod'
2
+
3
+ import { BaseMessage } from './shared'
4
+
5
+ interface CarouselElement {
6
+ title: string
7
+ subtitle: string
8
+ image: string
9
+ button: { text: string; url: string }
10
+ }
11
+
12
+ export interface CarouselMessage extends BaseMessage {
13
+ type: 'carousel'
14
+ content: {
15
+ elements: CarouselElement[]
16
+ }
17
+ }
18
+
19
+ export const CarouselSchema = z
20
+ .object({
21
+ type: z.enum(['carousel']),
22
+ content: z.object({
23
+ elements: z.array(
24
+ z.object({
25
+ title: z.string(),
26
+ subtitle: z.string(),
27
+ image: z.string(),
28
+ button: z.object({
29
+ text: z.string(),
30
+ url: z.string(),
31
+ }),
32
+ })
33
+ ),
34
+ }),
35
+ })
36
+ .describe('A carousel message containing a list of elements')
@@ -0,0 +1,15 @@
1
+ import z from 'zod'
2
+
3
+ import { BaseMessage } from './shared'
4
+
5
+ export interface ExitMessage extends BaseMessage {
6
+ type: 'exit'
7
+ }
8
+
9
+ export const ExitSchema = z
10
+ .object({
11
+ type: z.enum(['exit']),
12
+ })
13
+ .describe(
14
+ 'An exit message. This message should only be used to exit the agent due to out of context or the user asking to exit the conversation'
15
+ )
@@ -0,0 +1,27 @@
1
+ import { z } from 'zod'
2
+
3
+ import { CarouselMessage, CarouselSchema } from './carousel'
4
+ import { ExitMessage, ExitSchema } from './exit'
5
+ import { TextMessage, TextSchema } from './text'
6
+ import {
7
+ TextWithButtonsMessage,
8
+ TextWithButtonsSchema,
9
+ } from './text-with-buttons'
10
+
11
+ export type OutputMessage =
12
+ | TextMessage
13
+ | TextWithButtonsMessage
14
+ | CarouselMessage
15
+ | ExitMessage
16
+
17
+ export interface Output {
18
+ messages: OutputMessage[]
19
+ }
20
+
21
+ export const OutputSchema = z
22
+ .object({
23
+ messages: z.array(
24
+ z.union([TextSchema, TextWithButtonsSchema, CarouselSchema, ExitSchema])
25
+ ),
26
+ })
27
+ .describe('The messages to be sent to the user')
@@ -0,0 +1,3 @@
1
+ export interface BaseMessage {
2
+ type: 'text' | 'textWithButtons' | 'carousel' | 'exit'
3
+ }
@@ -0,0 +1,23 @@
1
+ import z from 'zod'
2
+
3
+ import { BaseMessage } from './shared'
4
+
5
+ export interface TextWithButtonsMessage extends BaseMessage {
6
+ type: 'textWithButtons'
7
+ content: {
8
+ text: string
9
+ buttons: string[]
10
+ }
11
+ }
12
+
13
+ export const TextWithButtonsSchema = z
14
+ .object({
15
+ type: z.enum(['textWithButtons']),
16
+ content: z.object({
17
+ text: z.string(),
18
+ buttons: z.array(z.string()),
19
+ }),
20
+ })
21
+ .describe(
22
+ 'A text message with buttons to allow the user to use quick replies'
23
+ )
@@ -0,0 +1,19 @@
1
+ import { z } from 'zod'
2
+
3
+ import { BaseMessage } from './shared'
4
+
5
+ export interface TextMessage extends BaseMessage {
6
+ type: 'text'
7
+ content: {
8
+ text: string
9
+ }
10
+ }
11
+
12
+ export const TextSchema = z
13
+ .object({
14
+ type: z.enum(['text']),
15
+ content: z.object({
16
+ text: z.string(),
17
+ }),
18
+ })
19
+ .describe('A text message')
@@ -0,0 +1,3 @@
1
+ import { Tool } from '../types'
2
+
3
+ export const mandatoryTools: Tool[] = []
package/src/types.ts CHANGED
@@ -1,33 +1,37 @@
1
- /* eslint-disable @typescript-eslint/naming-convention */
2
- export interface AiAgentArgs {
3
- name: string
4
- instructions: string
5
- }
6
- export interface Config {
7
- headers: {
8
- Authorization: string
9
- 'Content-Type': string
10
- }
1
+ import {
2
+ Agent,
3
+ AgentInputItem,
4
+ RunContext,
5
+ Tool as OpenAITool,
6
+ } from '@openai/agents'
7
+ import { ZodSchema } from 'zod'
8
+
9
+ import { OutputMessage, OutputSchema } from './structured-output'
10
+
11
+ export interface Context {
12
+ authToken: string
11
13
  }
12
14
 
13
- export interface AiAgentRequestDataTest {
14
- messages: {
15
- role: MessageRole
16
- content: string
17
- }[]
15
+ export interface CustomTool {
18
16
  name: string
19
- instructions: string
17
+ description: string
18
+ schema: ZodSchema
19
+ func: (input?: any, runContext?: RunContext<Context>) => Promise<any>
20
20
  }
21
21
 
22
- export type MessageRole = 'user' | 'assistant'
22
+ export type Tool = OpenAITool<Context>
23
+ export type AIAgent = Agent<Context, typeof OutputSchema>
24
+
25
+ export interface PluginAiAgentOptions {
26
+ authToken?: string
27
+ customTools?: CustomTool[]
28
+ }
23
29
 
24
- export interface AiAgentRequestData {
25
- message: string
26
- memory_length: number
30
+ export interface AiAgentArgs {
27
31
  name: string
28
32
  instructions: string
33
+ activeTools?: { name: string }[]
29
34
  }
30
35
 
31
- export interface AiAgentResponse {
32
- message: { role: string; content: string }
33
- }
36
+ export type AgenticInputMessage = AgentInputItem
37
+ export type AgenticOutputMessage = OutputMessage
@@ -1,12 +0,0 @@
1
- import { BotContext } from '@botonic/core';
2
- import { AiAgentArgs, AiAgentResponse } from './types';
3
- export declare class AiAgentClient {
4
- private url;
5
- constructor();
6
- getInference(request: BotContext, aiAgentArgs: AiAgentArgs): Promise<AiAgentResponse>;
7
- private agentTestInference;
8
- private agentInference;
9
- private getConfig;
10
- private getData;
11
- private getDataTest;
12
- }
@@ -1,78 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AiAgentClient = void 0;
4
- const tslib_1 = require("tslib");
5
- /* eslint-disable @typescript-eslint/naming-convention */
6
- const core_1 = require("@botonic/core");
7
- const axios_1 = tslib_1.__importDefault(require("axios"));
8
- const constants_1 = require("./constants");
9
- const errors_1 = require("./errors");
10
- class AiAgentClient {
11
- constructor() {
12
- this.url = `${constants_1.HUBTYPE_API_URL}/external/v1/ai/agent`;
13
- }
14
- getInference(request, aiAgentArgs) {
15
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
16
- if ((0, core_1.isDev)(request.session)) {
17
- return this.agentTestInference(request, aiAgentArgs);
18
- }
19
- return this.agentInference(request, aiAgentArgs);
20
- });
21
- }
22
- agentTestInference(request, aiAgentArgs) {
23
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
24
- const config = this.getConfig(request);
25
- const data = this.getDataTest(request, aiAgentArgs);
26
- const response = yield axios_1.default.post(`${this.url}/test/`, data, config);
27
- return response.data;
28
- });
29
- }
30
- agentInference(request, aiAgentArgs) {
31
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
32
- const config = this.getConfig(request);
33
- const data = this.getData(request, aiAgentArgs);
34
- const response = yield axios_1.default.post(`${this.url}/run/`, data, config);
35
- return response.data;
36
- });
37
- }
38
- getConfig(request) {
39
- return {
40
- headers: {
41
- Authorization: `Bearer ${request.session._access_token}`,
42
- 'Content-Type': 'application/json',
43
- },
44
- };
45
- }
46
- getData(request, aiAgentArgs) {
47
- if (!request.input.data) {
48
- throw new errors_1.AiAgentError('No input data provided');
49
- }
50
- return {
51
- message: request.input.message_id,
52
- memory_length: 10,
53
- name: aiAgentArgs.name,
54
- instructions: aiAgentArgs.instructions,
55
- };
56
- }
57
- getDataTest(request, aiAgentArgs) {
58
- if (!request.input.data) {
59
- throw new errors_1.AiAgentError('No input data provided');
60
- }
61
- // TODO: We can get messages from localStorage in Dev mode and transform them to the correct format {role: MessageRole, content: string}
62
- // if (isDev(request.session)) {
63
- // const messages = localStorage.getItem('botonicState').
64
- // }
65
- return {
66
- messages: [
67
- {
68
- role: 'user',
69
- content: request.input.data,
70
- },
71
- ],
72
- name: aiAgentArgs.name,
73
- instructions: aiAgentArgs.instructions,
74
- };
75
- }
76
- }
77
- exports.AiAgentClient = AiAgentClient;
78
- //# sourceMappingURL=ai-agent-client.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ai-agent-client.js","sourceRoot":"","sources":["../../src/ai-agent-client.ts"],"names":[],"mappings":";;;;AAAA,yDAAyD;AACzD,wCAAiD;AACjD,0DAAyB;AAEzB,2CAA6C;AAC7C,qCAAuC;AASvC,MAAa,aAAa;IAGxB;QACE,IAAI,CAAC,GAAG,GAAG,GAAG,2BAAe,uBAAuB,CAAA;IACtD,CAAC;IAEK,YAAY,CAChB,OAAmB,EACnB,WAAwB;;YAExB,IAAI,IAAA,YAAK,EAAC,OAAO,CAAC,OAAO,CAAC,EAAE;gBAC1B,OAAO,IAAI,CAAC,kBAAkB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;aACrD;YAED,OAAO,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;QAClD,CAAC;KAAA;IAEa,kBAAkB,CAC9B,OAAmB,EACnB,WAAwB;;YAExB,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;YACtC,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;YACnD,MAAM,QAAQ,GAAG,MAAM,eAAK,CAAC,IAAI,CAC/B,GAAG,IAAI,CAAC,GAAG,QAAQ,EACnB,IAAI,EACJ,MAAM,CACP,CAAA;YAED,OAAO,QAAQ,CAAC,IAAI,CAAA;QACtB,CAAC;KAAA;IAEa,cAAc,CAC1B,OAAmB,EACnB,WAAwB;;YAExB,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;YACtC,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,WAAW,CAAC,CAAA;YAC/C,MAAM,QAAQ,GAAG,MAAM,eAAK,CAAC,IAAI,CAC/B,GAAG,IAAI,CAAC,GAAG,OAAO,EAClB,IAAI,EACJ,MAAM,CACP,CAAA;YAED,OAAO,QAAQ,CAAC,IAAI,CAAA;QACtB,CAAC;KAAA;IAEO,SAAS,CAAC,OAAmB;QACnC,OAAO;YACL,OAAO,EAAE;gBACP,aAAa,EAAE,UAAU,OAAO,CAAC,OAAO,CAAC,aAAa,EAAE;gBACxD,cAAc,EAAE,kBAAkB;aACnC;SACF,CAAA;IACH,CAAC;IAEO,OAAO,CACb,OAAmB,EACnB,WAAwB;QAExB,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE;YACvB,MAAM,IAAI,qBAAY,CAAC,wBAAwB,CAAC,CAAA;SACjD;QAED,OAAO;YACL,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,UAAU;YACjC,aAAa,EAAE,EAAE;YACjB,IAAI,EAAE,WAAW,CAAC,IAAI;YACtB,YAAY,EAAE,WAAW,CAAC,YAAY;SACvC,CAAA;IACH,CAAC;IAEO,WAAW,CACjB,OAAmB,EACnB,WAAwB;QAExB,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE;YACvB,MAAM,IAAI,qBAAY,CAAC,wBAAwB,CAAC,CAAA;SACjD;QAED,wIAAwI;QACxI,gCAAgC;QAChC,2DAA2D;QAC3D,IAAI;QAEJ,OAAO;YACL,QAAQ,EAAE;gBACR;oBACE,IAAI,EAAE,MAAM;oBACZ,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI;iBAC5B;aACF;YACD,IAAI,EAAE,WAAW,CAAC,IAAI;YACtB,YAAY,EAAE,WAAW,CAAC,YAAY;SACvC,CAAA;IACH,CAAC;CACF;AAjGD,sCAiGC"}
@@ -1,3 +0,0 @@
1
- export declare class AiAgentError extends Error {
2
- constructor(message: string);
3
- }
package/lib/cjs/errors.js DELETED
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AiAgentError = void 0;
4
- class AiAgentError extends Error {
5
- constructor(message) {
6
- super(message);
7
- this.name = 'AiAgentError';
8
- }
9
- }
10
- exports.AiAgentError = AiAgentError;
11
- //# sourceMappingURL=errors.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,cAAc,CAAA;IAC5B,CAAC;CACF;AALD,oCAKC"}
@@ -1,12 +0,0 @@
1
- import { BotContext } from '@botonic/core';
2
- import { AiAgentArgs, AiAgentResponse } from './types';
3
- export declare class AiAgentClient {
4
- private url;
5
- constructor();
6
- getInference(request: BotContext, aiAgentArgs: AiAgentArgs): Promise<AiAgentResponse>;
7
- private agentTestInference;
8
- private agentInference;
9
- private getConfig;
10
- private getData;
11
- private getDataTest;
12
- }