@botonic/plugin-ai-agents 0.36.0-alpha.5 → 0.36.0-alpha.7

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 (116) hide show
  1. package/lib/cjs/constants.d.ts +1 -0
  2. package/lib/cjs/constants.js +2 -1
  3. package/lib/cjs/constants.js.map +1 -1
  4. package/lib/cjs/hubtype-api-client.d.ts +2 -0
  5. package/lib/cjs/hubtype-api-client.js +41 -2
  6. package/lib/cjs/hubtype-api-client.js.map +1 -1
  7. package/lib/cjs/index.d.ts +2 -1
  8. package/lib/cjs/index.js +23 -14
  9. package/lib/cjs/index.js.map +1 -1
  10. package/lib/cjs/runner.d.ts +9 -0
  11. package/lib/cjs/runner.js +58 -0
  12. package/lib/cjs/runner.js.map +1 -0
  13. package/lib/cjs/structured-output/carousel.d.ts +100 -0
  14. package/lib/cjs/structured-output/carousel.js +21 -0
  15. package/lib/cjs/structured-output/carousel.js.map +1 -0
  16. package/lib/cjs/structured-output/exit.d.ts +12 -0
  17. package/lib/cjs/structured-output/exit.js +11 -0
  18. package/lib/cjs/structured-output/exit.js.map +1 -0
  19. package/lib/cjs/structured-output/index.d.ts +198 -0
  20. package/lib/cjs/structured-output/index.js +14 -0
  21. package/lib/cjs/structured-output/index.js.map +1 -0
  22. package/lib/cjs/structured-output/shared.d.ts +3 -0
  23. package/lib/cjs/structured-output/shared.js +3 -0
  24. package/lib/cjs/structured-output/shared.js.map +1 -0
  25. package/lib/cjs/structured-output/text-with-buttons.d.ts +34 -0
  26. package/lib/cjs/structured-output/text-with-buttons.js +15 -0
  27. package/lib/cjs/structured-output/text-with-buttons.js.map +1 -0
  28. package/lib/cjs/structured-output/text.d.ts +28 -0
  29. package/lib/cjs/structured-output/text.js +13 -0
  30. package/lib/cjs/structured-output/text.js.map +1 -0
  31. package/lib/cjs/types.d.ts +6 -25
  32. package/lib/esm/constants.d.ts +1 -0
  33. package/lib/esm/constants.js +1 -0
  34. package/lib/esm/constants.js.map +1 -1
  35. package/lib/esm/hubtype-api-client.d.ts +2 -0
  36. package/lib/esm/hubtype-api-client.js +41 -2
  37. package/lib/esm/hubtype-api-client.js.map +1 -1
  38. package/lib/esm/index.d.ts +2 -1
  39. package/lib/esm/index.js +21 -12
  40. package/lib/esm/index.js.map +1 -1
  41. package/lib/esm/runner.d.ts +9 -0
  42. package/lib/esm/runner.js +54 -0
  43. package/lib/esm/runner.js.map +1 -0
  44. package/lib/esm/structured-output/carousel.d.ts +100 -0
  45. package/lib/esm/structured-output/carousel.js +18 -0
  46. package/lib/esm/structured-output/carousel.js.map +1 -0
  47. package/lib/esm/structured-output/exit.d.ts +12 -0
  48. package/lib/esm/structured-output/exit.js +7 -0
  49. package/lib/esm/structured-output/exit.js.map +1 -0
  50. package/lib/esm/structured-output/index.d.ts +198 -0
  51. package/lib/esm/structured-output/index.js +11 -0
  52. package/lib/esm/structured-output/index.js.map +1 -0
  53. package/lib/esm/structured-output/shared.d.ts +3 -0
  54. package/lib/esm/structured-output/shared.js +2 -0
  55. package/lib/esm/structured-output/shared.js.map +1 -0
  56. package/lib/esm/structured-output/text-with-buttons.d.ts +34 -0
  57. package/lib/esm/structured-output/text-with-buttons.js +11 -0
  58. package/lib/esm/structured-output/text-with-buttons.js.map +1 -0
  59. package/lib/esm/structured-output/text.d.ts +28 -0
  60. package/lib/esm/structured-output/text.js +10 -0
  61. package/lib/esm/structured-output/text.js.map +1 -0
  62. package/lib/esm/types.d.ts +6 -25
  63. package/package.json +3 -4
  64. package/src/constants.ts +2 -0
  65. package/src/hubtype-api-client.ts +55 -9
  66. package/src/index.ts +26 -19
  67. package/src/runner.ts +74 -0
  68. package/src/structured-output/carousel.ts +36 -0
  69. package/src/structured-output/exit.ts +15 -0
  70. package/src/structured-output/index.ts +27 -0
  71. package/src/structured-output/shared.ts +3 -0
  72. package/src/structured-output/text-with-buttons.ts +23 -0
  73. package/src/structured-output/text.ts +19 -0
  74. package/src/types.ts +7 -34
  75. package/lib/cjs/ai-agent-client.d.ts +0 -9
  76. package/lib/cjs/ai-agent-client.js +0 -49
  77. package/lib/cjs/ai-agent-client.js.map +0 -1
  78. package/lib/cjs/errors.d.ts +0 -3
  79. package/lib/cjs/errors.js +0 -11
  80. package/lib/cjs/errors.js.map +0 -1
  81. package/lib/cjs/hubtype-client.d.ts +0 -7
  82. package/lib/cjs/hubtype-client.js +0 -37
  83. package/lib/cjs/hubtype-client.js.map +0 -1
  84. package/lib/cjs/tools/custom.d.ts +0 -3
  85. package/lib/cjs/tools/custom.js +0 -14
  86. package/lib/cjs/tools/custom.js.map +0 -1
  87. package/lib/cjs/tools/default.d.ts +0 -6
  88. package/lib/cjs/tools/default.js +0 -41
  89. package/lib/cjs/tools/default.js.map +0 -1
  90. package/lib/cjs/utils.d.ts +0 -5
  91. package/lib/cjs/utils.js +0 -26
  92. package/lib/cjs/utils.js.map +0 -1
  93. package/lib/esm/ai-agent-client.d.ts +0 -9
  94. package/lib/esm/ai-agent-client.js +0 -45
  95. package/lib/esm/ai-agent-client.js.map +0 -1
  96. package/lib/esm/errors.d.ts +0 -3
  97. package/lib/esm/errors.js +0 -7
  98. package/lib/esm/errors.js.map +0 -1
  99. package/lib/esm/hubtype-client.d.ts +0 -7
  100. package/lib/esm/hubtype-client.js +0 -33
  101. package/lib/esm/hubtype-client.js.map +0 -1
  102. package/lib/esm/tools/custom.d.ts +0 -3
  103. package/lib/esm/tools/custom.js +0 -10
  104. package/lib/esm/tools/custom.js.map +0 -1
  105. package/lib/esm/tools/default.d.ts +0 -6
  106. package/lib/esm/tools/default.js +0 -38
  107. package/lib/esm/tools/default.js.map +0 -1
  108. package/lib/esm/utils.d.ts +0 -5
  109. package/lib/esm/utils.js +0 -22
  110. package/lib/esm/utils.js.map +0 -1
  111. package/src/ai-agent-client.ts +0 -69
  112. package/src/errors.ts +0 -6
  113. package/src/hubtype-client.ts +0 -43
  114. package/src/tools/custom.ts +0 -14
  115. package/src/tools/default.ts +0 -52
  116. package/src/utils.ts +0 -28
@@ -0,0 +1,198 @@
1
+ import { z } from 'zod';
2
+ import { CarouselMessage } from './carousel';
3
+ import { ExitMessage } from './exit';
4
+ import { TextMessage } from './text';
5
+ import { TextWithButtonsMessage } from './text-with-buttons';
6
+ export type OutputMessage = TextMessage | TextWithButtonsMessage | CarouselMessage | ExitMessage;
7
+ export interface Output {
8
+ messages: OutputMessage[];
9
+ }
10
+ export declare const OutputSchema: z.ZodObject<{
11
+ messages: z.ZodArray<z.ZodUnion<[z.ZodObject<{
12
+ type: z.ZodEnum<["text"]>;
13
+ content: z.ZodObject<{
14
+ text: z.ZodString;
15
+ }, "strip", z.ZodTypeAny, {
16
+ text: string;
17
+ }, {
18
+ text: string;
19
+ }>;
20
+ }, "strip", z.ZodTypeAny, {
21
+ type: "text";
22
+ content: {
23
+ text: string;
24
+ };
25
+ }, {
26
+ type: "text";
27
+ content: {
28
+ text: string;
29
+ };
30
+ }>, z.ZodObject<{
31
+ type: z.ZodEnum<["textWithButtons"]>;
32
+ content: z.ZodObject<{
33
+ text: z.ZodString;
34
+ buttons: z.ZodArray<z.ZodString, "many">;
35
+ }, "strip", z.ZodTypeAny, {
36
+ text: string;
37
+ buttons: string[];
38
+ }, {
39
+ text: string;
40
+ buttons: string[];
41
+ }>;
42
+ }, "strip", z.ZodTypeAny, {
43
+ type: "textWithButtons";
44
+ content: {
45
+ text: string;
46
+ buttons: string[];
47
+ };
48
+ }, {
49
+ type: "textWithButtons";
50
+ content: {
51
+ text: string;
52
+ buttons: string[];
53
+ };
54
+ }>, z.ZodObject<{
55
+ type: z.ZodEnum<["carousel"]>;
56
+ content: z.ZodObject<{
57
+ elements: z.ZodArray<z.ZodObject<{
58
+ title: z.ZodString;
59
+ subtitle: z.ZodString;
60
+ image: z.ZodString;
61
+ button: z.ZodObject<{
62
+ text: z.ZodString;
63
+ url: z.ZodString;
64
+ }, "strip", z.ZodTypeAny, {
65
+ text: string;
66
+ url: string;
67
+ }, {
68
+ text: string;
69
+ url: string;
70
+ }>;
71
+ }, "strip", z.ZodTypeAny, {
72
+ title: string;
73
+ subtitle: string;
74
+ image: string;
75
+ button: {
76
+ text: string;
77
+ url: string;
78
+ };
79
+ }, {
80
+ title: string;
81
+ subtitle: string;
82
+ image: string;
83
+ button: {
84
+ text: string;
85
+ url: string;
86
+ };
87
+ }>, "many">;
88
+ }, "strip", z.ZodTypeAny, {
89
+ elements: {
90
+ title: string;
91
+ subtitle: string;
92
+ image: string;
93
+ button: {
94
+ text: string;
95
+ url: string;
96
+ };
97
+ }[];
98
+ }, {
99
+ elements: {
100
+ title: string;
101
+ subtitle: string;
102
+ image: string;
103
+ button: {
104
+ text: string;
105
+ url: string;
106
+ };
107
+ }[];
108
+ }>;
109
+ }, "strip", z.ZodTypeAny, {
110
+ type: "carousel";
111
+ content: {
112
+ elements: {
113
+ title: string;
114
+ subtitle: string;
115
+ image: string;
116
+ button: {
117
+ text: string;
118
+ url: string;
119
+ };
120
+ }[];
121
+ };
122
+ }, {
123
+ type: "carousel";
124
+ content: {
125
+ elements: {
126
+ title: string;
127
+ subtitle: string;
128
+ image: string;
129
+ button: {
130
+ text: string;
131
+ url: string;
132
+ };
133
+ }[];
134
+ };
135
+ }>, z.ZodObject<{
136
+ type: z.ZodEnum<["exit"]>;
137
+ }, "strip", z.ZodTypeAny, {
138
+ type: "exit";
139
+ }, {
140
+ type: "exit";
141
+ }>]>, "many">;
142
+ }, "strip", z.ZodTypeAny, {
143
+ messages: ({
144
+ type: "carousel";
145
+ content: {
146
+ elements: {
147
+ title: string;
148
+ subtitle: string;
149
+ image: string;
150
+ button: {
151
+ text: string;
152
+ url: string;
153
+ };
154
+ }[];
155
+ };
156
+ } | {
157
+ type: "exit";
158
+ } | {
159
+ type: "text";
160
+ content: {
161
+ text: string;
162
+ };
163
+ } | {
164
+ type: "textWithButtons";
165
+ content: {
166
+ text: string;
167
+ buttons: string[];
168
+ };
169
+ })[];
170
+ }, {
171
+ messages: ({
172
+ type: "carousel";
173
+ content: {
174
+ elements: {
175
+ title: string;
176
+ subtitle: string;
177
+ image: string;
178
+ button: {
179
+ text: string;
180
+ url: string;
181
+ };
182
+ }[];
183
+ };
184
+ } | {
185
+ type: "exit";
186
+ } | {
187
+ type: "text";
188
+ content: {
189
+ text: string;
190
+ };
191
+ } | {
192
+ type: "textWithButtons";
193
+ content: {
194
+ text: string;
195
+ buttons: string[];
196
+ };
197
+ })[];
198
+ }>;
@@ -0,0 +1,11 @@
1
+ import { z } from 'zod';
2
+ import { CarouselSchema } from './carousel';
3
+ import { ExitSchema } from './exit';
4
+ import { TextSchema } from './text';
5
+ import { TextWithButtonsSchema, } from './text-with-buttons';
6
+ export const OutputSchema = z
7
+ .object({
8
+ messages: z.array(z.union([TextSchema, TextWithButtonsSchema, CarouselSchema, ExitSchema])),
9
+ })
10
+ .describe('The messages to be sent to the user');
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/structured-output/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAmB,cAAc,EAAE,MAAM,YAAY,CAAA;AAC5D,OAAO,EAAe,UAAU,EAAE,MAAM,QAAQ,CAAA;AAChD,OAAO,EAAe,UAAU,EAAE,MAAM,QAAQ,CAAA;AAChD,OAAO,EAEL,qBAAqB,GACtB,MAAM,qBAAqB,CAAA;AAY5B,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC;KAC1B,MAAM,CAAC;IACN,QAAQ,EAAE,CAAC,CAAC,KAAK,CACf,CAAC,CAAC,KAAK,CAAC,CAAC,UAAU,EAAE,qBAAqB,EAAE,cAAc,EAAE,UAAU,CAAC,CAAC,CACzE;CACF,CAAC;KACD,QAAQ,CAAC,qCAAqC,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ export interface BaseMessage {
2
+ type: 'text' | 'textWithButtons' | 'carousel' | 'exit';
3
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/structured-output/shared.ts"],"names":[],"mappings":""}
@@ -0,0 +1,34 @@
1
+ import z from 'zod';
2
+ import { BaseMessage } from './shared';
3
+ export interface TextWithButtonsMessage extends BaseMessage {
4
+ type: 'textWithButtons';
5
+ content: {
6
+ text: string;
7
+ buttons: string[];
8
+ };
9
+ }
10
+ export declare const TextWithButtonsSchema: z.ZodObject<{
11
+ type: z.ZodEnum<["textWithButtons"]>;
12
+ content: z.ZodObject<{
13
+ text: z.ZodString;
14
+ buttons: z.ZodArray<z.ZodString, "many">;
15
+ }, "strip", z.ZodTypeAny, {
16
+ text: string;
17
+ buttons: string[];
18
+ }, {
19
+ text: string;
20
+ buttons: string[];
21
+ }>;
22
+ }, "strip", z.ZodTypeAny, {
23
+ type: "textWithButtons";
24
+ content: {
25
+ text: string;
26
+ buttons: string[];
27
+ };
28
+ }, {
29
+ type: "textWithButtons";
30
+ content: {
31
+ text: string;
32
+ buttons: string[];
33
+ };
34
+ }>;
@@ -0,0 +1,11 @@
1
+ import z from 'zod';
2
+ export const TextWithButtonsSchema = z
3
+ .object({
4
+ type: z.enum(['textWithButtons']),
5
+ content: z.object({
6
+ text: z.string(),
7
+ buttons: z.array(z.string()),
8
+ }),
9
+ })
10
+ .describe('A text message with buttons to allow the user to use quick replies');
11
+ //# sourceMappingURL=text-with-buttons.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text-with-buttons.js","sourceRoot":"","sources":["../../../src/structured-output/text-with-buttons.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAYnB,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC;KACnC,MAAM,CAAC;IACN,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,iBAAiB,CAAC,CAAC;IACjC,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC;QAChB,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;QAChB,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;KAC7B,CAAC;CACH,CAAC;KACD,QAAQ,CACP,oEAAoE,CACrE,CAAA"}
@@ -0,0 +1,28 @@
1
+ import { z } from 'zod';
2
+ import { BaseMessage } from './shared';
3
+ export interface TextMessage extends BaseMessage {
4
+ type: 'text';
5
+ content: {
6
+ text: string;
7
+ };
8
+ }
9
+ export declare const TextSchema: z.ZodObject<{
10
+ type: z.ZodEnum<["text"]>;
11
+ content: z.ZodObject<{
12
+ text: z.ZodString;
13
+ }, "strip", z.ZodTypeAny, {
14
+ text: string;
15
+ }, {
16
+ text: string;
17
+ }>;
18
+ }, "strip", z.ZodTypeAny, {
19
+ type: "text";
20
+ content: {
21
+ text: string;
22
+ };
23
+ }, {
24
+ type: "text";
25
+ content: {
26
+ text: string;
27
+ };
28
+ }>;
@@ -0,0 +1,10 @@
1
+ import { z } from 'zod';
2
+ export const TextSchema = z
3
+ .object({
4
+ type: z.enum(['text']),
5
+ content: z.object({
6
+ text: z.string(),
7
+ }),
8
+ })
9
+ .describe('A text message');
10
+ //# sourceMappingURL=text.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text.js","sourceRoot":"","sources":["../../../src/structured-output/text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAWvB,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC;KACxB,MAAM,CAAC;IACN,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC;QAChB,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;KACjB,CAAC;CACH,CAAC;KACD,QAAQ,CAAC,gBAAgB,CAAC,CAAA"}
@@ -1,4 +1,6 @@
1
- import { ZodSchema } from 'zod';
1
+ import { AgentInputItem } from '@openai/agents';
2
+ import { ZodObject } from 'zod';
3
+ import { OutputMessage } from './structured-output';
2
4
  export interface PluginAiAgentOptions {
3
5
  authToken?: string;
4
6
  customTools?: CustomTool[];
@@ -6,8 +8,7 @@ export interface PluginAiAgentOptions {
6
8
  export interface CustomTool {
7
9
  name: string;
8
10
  description: string;
9
- schema: ZodSchema;
10
- returnDirect?: boolean;
11
+ schema: ZodObject<any>;
11
12
  func: (input?: any) => Promise<any>;
12
13
  }
13
14
  export interface AiAgentArgs {
@@ -17,25 +18,5 @@ export interface AiAgentArgs {
17
18
  name: string;
18
19
  }[];
19
20
  }
20
- export interface AgenticBaseMessage {
21
- role: 'assistant' | 'user' | 'tool' | 'exit';
22
- }
23
- export interface AssistantMessage extends AgenticBaseMessage {
24
- role: 'assistant';
25
- content: string;
26
- }
27
- export interface UserMessage extends AgenticBaseMessage {
28
- role: 'user';
29
- content: string;
30
- }
31
- export interface ToolMessage extends AgenticBaseMessage {
32
- role: 'tool';
33
- toolName: string;
34
- toolOutput: string | null;
35
- }
36
- export interface ExitMessage extends AgenticBaseMessage {
37
- role: 'exit';
38
- }
39
- export type AgenticInputMessage = AssistantMessage | UserMessage;
40
- export type AgenticOutputMessage = AssistantMessage | ToolMessage | ExitMessage;
41
- export type AgenticMessage = AssistantMessage | UserMessage | ToolMessage | ExitMessage;
21
+ export type AgenticInputMessage = AgentInputItem;
22
+ export type AgenticOutputMessage = OutputMessage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botonic/plugin-ai-agents",
3
- "version": "0.36.0-alpha.5",
3
+ "version": "0.36.0-alpha.7",
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,10 +14,9 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@botonic/core": "^0.36.0",
17
- "@langchain/core": "^0.3.57",
18
- "@langchain/langgraph": "^0.3.0",
19
- "@langchain/openai": "^0.5.12",
17
+ "@openai/agents": "^0.0.10",
20
18
  "axios": "^1.10.0",
19
+ "openai": "^5.8.3",
21
20
  "uuid": "^10.0.0",
22
21
  "zod": "^3.25.57"
23
22
  },
package/src/constants.ts CHANGED
@@ -7,3 +7,5 @@ export const AZURE_OPENAI_API_DEPLOYMENT_NAME =
7
7
  process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME || 'gpt-4o-mini_p1'
8
8
  export const AZURE_OPENAI_API_VERSION =
9
9
  process.env.AZURE_OPENAI_API_VERSION || '2024-12-01-preview'
10
+
11
+ export const isProd = process.env.NODE_ENV === 'production'
@@ -1,9 +1,22 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention */
1
2
  import { BotContext } from '@botonic/core'
2
3
  import axios from 'axios'
3
4
 
4
5
  import { HUBTYPE_API_URL } from './constants'
5
6
  import { AgenticInputMessage } from './types'
6
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
+
7
20
  export class HubtypeApiClient {
8
21
  private readonly authToken: string
9
22
 
@@ -11,6 +24,20 @@ export class HubtypeApiClient {
11
24
  this.authToken = authToken
12
25
  }
13
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
+
14
41
  async getMessages(
15
42
  request: BotContext,
16
43
  memoryLength: number
@@ -26,18 +53,37 @@ export class HubtypeApiClient {
26
53
  }
27
54
 
28
55
  try {
29
- const messages = await axios.get<{ messages: AgenticInputMessage[] }>(
30
- url,
31
- {
32
- headers,
33
- params,
34
- }
35
- )
36
-
37
- return messages.data.messages
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))
38
62
  } catch (error) {
39
63
  console.error(error)
40
64
  throw new Error('Failed to get messages from Hubtype')
41
65
  }
42
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
+ }
43
89
  }
package/src/index.ts CHANGED
@@ -1,20 +1,16 @@
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 { isProd } from './constants'
4
5
  import { HubtypeApiClient } from './hubtype-api-client'
5
- import { createCustomTool } from './tools/custom'
6
- import { MANDATORY_TOOLS } from './tools/default'
6
+ import { AIAgentRunner } from './runner'
7
7
  import {
8
8
  AgenticInputMessage,
9
9
  AgenticOutputMessage,
10
10
  AiAgentArgs,
11
11
  CustomTool,
12
12
  PluginAiAgentOptions,
13
- UserMessage,
14
13
  } from './types'
15
- import { AiProvider, loadChatModel } from './utils'
16
-
17
- const isProd = process.env.NODE_ENV === 'production'
18
14
 
19
15
  export default class BotonicPluginAiAgents implements Plugin {
20
16
  private readonly authToken?: string
@@ -32,7 +28,7 @@ export default class BotonicPluginAiAgents implements Plugin {
32
28
  async getInference(
33
29
  request: BotContext,
34
30
  aiAgentArgs: AiAgentArgs
35
- ): Promise<AgenticOutputMessage | undefined> {
31
+ ): Promise<AgenticOutputMessage[] | undefined> {
36
32
  try {
37
33
  const authToken = isProd ? request.session._access_token : this.authToken
38
34
  if (!authToken) {
@@ -40,20 +36,20 @@ export default class BotonicPluginAiAgents implements Plugin {
40
36
  }
41
37
 
42
38
  const messages = await this.getMessages(request, authToken, 10)
43
-
44
- const customTools = this.customTools.map(customTool =>
45
- createCustomTool(customTool)
46
- )
47
-
48
- const customActiveTools = customTools.filter(tool =>
39
+ const availableTools = this.customTools.filter(tool =>
49
40
  aiAgentArgs.activeTools?.map(tool => tool.name).includes(tool.name)
50
41
  )
51
- const tools = [...customActiveTools, ...MANDATORY_TOOLS]
42
+ const tools = this.createTools(availableTools)
52
43
 
53
- const chatModel = loadChatModel(AiProvider.AzureOpenAI)
54
- const aiAgentClient = new AiAgentClient(aiAgentArgs, chatModel, tools)
44
+ const runner = new AIAgentRunner(
45
+ aiAgentArgs.name,
46
+ aiAgentArgs.instructions,
47
+ tools
48
+ )
49
+ const output = await runner.run(messages)
50
+ console.log('\n\nOutput:', output)
55
51
 
56
- return await aiAgentClient.runAgent(messages)
52
+ return output
57
53
  } catch (error) {
58
54
  console.error('error plugin returns undefined', error)
59
55
  return undefined
@@ -69,8 +65,19 @@ export default class BotonicPluginAiAgents implements Plugin {
69
65
  const hubtypeClient = new HubtypeApiClient(authToken)
70
66
  return await hubtypeClient.getMessages(request, memoryLength)
71
67
  }
68
+ const hubtypeClient = new HubtypeApiClient(authToken)
69
+ return await hubtypeClient.getLocalMessages(memoryLength)
70
+ }
72
71
 
73
- return [{ role: 'user', content: request.input.data } as UserMessage]
72
+ private createTools(customTools: CustomTool[]) {
73
+ return customTools.map(customTool => {
74
+ return tool({
75
+ name: customTool.name,
76
+ description: customTool.description,
77
+ parameters: customTool.schema,
78
+ execute: customTool.func,
79
+ })
80
+ })
74
81
  }
75
82
  }
76
83
 
package/src/runner.ts ADDED
@@ -0,0 +1,74 @@
1
+ import {
2
+ Agent,
3
+ Runner,
4
+ setDefaultOpenAIClient,
5
+ setOpenAIAPI,
6
+ setTracingDisabled,
7
+ Tool,
8
+ } from '@openai/agents'
9
+ import { AzureOpenAI } from 'openai'
10
+
11
+ import {
12
+ AZURE_OPENAI_API_BASE,
13
+ AZURE_OPENAI_API_DEPLOYMENT_NAME,
14
+ AZURE_OPENAI_API_KEY,
15
+ AZURE_OPENAI_API_VERSION,
16
+ isProd,
17
+ } from './constants'
18
+ import { OutputMessage, OutputSchema } from './structured-output'
19
+ import { AgenticInputMessage } from './types'
20
+
21
+ export class AIAgentRunner {
22
+ private agent: Agent<any, typeof OutputSchema>
23
+
24
+ constructor(name: string, instructions: string, tools: Tool[]) {
25
+ if (AZURE_OPENAI_API_KEY) {
26
+ const client = new AzureOpenAI({
27
+ apiKey: AZURE_OPENAI_API_KEY,
28
+ apiVersion: AZURE_OPENAI_API_VERSION,
29
+ deployment: AZURE_OPENAI_API_DEPLOYMENT_NAME,
30
+ baseURL: AZURE_OPENAI_API_BASE,
31
+ dangerouslyAllowBrowser: !isProd,
32
+ })
33
+ setDefaultOpenAIClient(client as any)
34
+ setOpenAIAPI('chat_completions')
35
+ setTracingDisabled(true)
36
+ }
37
+
38
+ this.agent = new Agent({
39
+ name: name,
40
+ instructions: this.addExtraInstructions(instructions),
41
+ tools: tools,
42
+ outputType: OutputSchema,
43
+ })
44
+ }
45
+
46
+ async run(messages: AgenticInputMessage[]): Promise<OutputMessage[]> {
47
+ const runner = new Runner({
48
+ modelSettings: { temperature: 0 },
49
+ })
50
+ const result = await runner.run(this.agent, messages)
51
+ const finalOutput = result.finalOutput
52
+
53
+ return finalOutput?.messages || []
54
+ }
55
+
56
+ private addExtraInstructions(instructions: string): string {
57
+ const metadata = `Current Date: ${new Date().toISOString()}`
58
+
59
+ const outputExample = JSON.stringify({
60
+ messages: [
61
+ {
62
+ type: 'text',
63
+ content: {
64
+ text: 'Hello, how can I help you today?',
65
+ },
66
+ },
67
+ ],
68
+ numMessages: 1,
69
+ })
70
+ const output = `Return a JSON that follows the output schema provided. Never return multiple output schemas concatenated by a line break.\n<example>\n${outputExample}\n</example>`
71
+
72
+ return `<instructions>\n${instructions}\n</instructions>\n\n<metadata>\n${metadata}\n</metadata>\n\n<output>\n${output}\n</output>`
73
+ }
74
+ }
@@ -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')