@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
@@ -1,69 +0,0 @@
1
- import { BaseChatModel } from '@langchain/core/language_models/chat_models'
2
- import {
3
- AIMessage as LangchainAIMessage,
4
- ToolMessage as LangchainToolMessage,
5
- } from '@langchain/core/messages'
6
- import { StructuredTool } from '@langchain/core/tools'
7
- import { CompiledStateGraph } from '@langchain/langgraph'
8
- import { createReactAgent } from '@langchain/langgraph/prebuilt'
9
-
10
- import { EXIT_TOOLS } from './tools/default'
11
- import {
12
- AgenticInputMessage,
13
- AgenticOutputMessage,
14
- AiAgentArgs,
15
- AssistantMessage,
16
- ExitMessage,
17
- ToolMessage,
18
- } from './types'
19
-
20
- export class AiAgentClient {
21
- public agent: CompiledStateGraph<any, any> // TODO: apply RunInput, RunOutput, etc.
22
-
23
- constructor(
24
- aiAgentArgs: AiAgentArgs,
25
- chatModel: BaseChatModel,
26
- tools: StructuredTool[] = []
27
- ) {
28
- this.agent = createReactAgent({
29
- name: aiAgentArgs.name,
30
- llm: chatModel,
31
- tools: tools,
32
- prompt: aiAgentArgs.instructions,
33
- })
34
- }
35
-
36
- async runAgent(
37
- _messages: AgenticInputMessage[]
38
- ): Promise<AgenticOutputMessage> {
39
- const response = await this.agent.invoke({
40
- messages: _messages.map((message: AgenticInputMessage) => ({
41
- role: message.role,
42
- content: message.content,
43
- })),
44
- })
45
- const lastMessage = response.messages.at(-1)
46
-
47
- if (lastMessage instanceof LangchainToolMessage) {
48
- if (lastMessage.name && EXIT_TOOLS.includes(lastMessage.name)) {
49
- return {
50
- role: 'exit',
51
- } as ExitMessage
52
- }
53
- return {
54
- role: 'tool',
55
- toolName: lastMessage.name,
56
- toolOutput: lastMessage.content,
57
- } as ToolMessage
58
- }
59
-
60
- if (lastMessage instanceof LangchainAIMessage) {
61
- return {
62
- role: 'assistant',
63
- content: lastMessage.content,
64
- } as AssistantMessage
65
- }
66
-
67
- throw new Error('Last message is not a valid message')
68
- }
69
- }
package/src/errors.ts DELETED
@@ -1,6 +0,0 @@
1
- export class AiAgentError extends Error {
2
- constructor(message: string) {
3
- super(message)
4
- this.name = 'AiAgentError'
5
- }
6
- }
@@ -1,43 +0,0 @@
1
- import { BotContext } from '@botonic/core'
2
- import axios from 'axios'
3
-
4
- import { HUBTYPE_API_URL } from './constants'
5
- import { AgenticInputMessage } from './types'
6
-
7
- export class HubtypeApiClient {
8
- private readonly authToken: string
9
-
10
- constructor(authToken: string) {
11
- this.authToken = authToken
12
- }
13
-
14
- async getMessages(
15
- request: BotContext,
16
- memoryLength: number
17
- ): Promise<AgenticInputMessage[]> {
18
- const url = `${HUBTYPE_API_URL}/external/v1/ai/agent/message_history/`
19
- const headers = {
20
- 'Content-Type': 'application/json',
21
- Authorization: `Bearer ${this.authToken}`,
22
- }
23
- const params = {
24
- last_message_id: request.input.message_id,
25
- num_messages: memoryLength,
26
- }
27
-
28
- try {
29
- const messages = await axios.get<{ messages: AgenticInputMessage[] }>(
30
- url,
31
- {
32
- headers,
33
- params,
34
- }
35
- )
36
-
37
- return messages.data.messages
38
- } catch (error) {
39
- console.error(error)
40
- throw new Error('Failed to get messages from Hubtype')
41
- }
42
- }
43
- }
@@ -1,14 +0,0 @@
1
- import { DynamicStructuredTool, tool } from '@langchain/core/tools'
2
-
3
- import { CustomTool } from '../types'
4
-
5
- export function createCustomTool(
6
- customTool: CustomTool
7
- ): DynamicStructuredTool {
8
- return tool(customTool.func, {
9
- name: customTool.name,
10
- description: customTool.description,
11
- schema: customTool.schema,
12
- returnDirect: customTool.returnDirect || false,
13
- })
14
- }
@@ -1,52 +0,0 @@
1
- import { tool } from '@langchain/core/tools'
2
- import { z } from 'zod'
3
-
4
- export const handoffToHumanAgent = tool(
5
- async () => {
6
- return null
7
- },
8
- {
9
- name: 'handoffToHumanAgent',
10
- schema: z.object({}),
11
- description:
12
- 'Use this tool strictly when the user wants to talk with a human agent.',
13
- returnDirect: true,
14
- }
15
- )
16
-
17
- export const outOfContext = tool(
18
- async () => {
19
- return null
20
- },
21
- {
22
- name: 'outOfContext',
23
- schema: z.object({}),
24
- description:
25
- 'Use this when the user talks about something that is clearly out of your context. Example: "I want to buy a car" when you are a travel agent.',
26
- returnDirect: true,
27
- }
28
- )
29
-
30
- export const finishConversation = tool(
31
- async () => {
32
- return null
33
- },
34
- {
35
- name: 'finishConversation',
36
- schema: z.object({}),
37
- description: 'Use this when the user wants to end the conversation.',
38
- returnDirect: true,
39
- }
40
- )
41
-
42
- export const MANDATORY_TOOLS = [
43
- handoffToHumanAgent,
44
- outOfContext,
45
- finishConversation,
46
- ]
47
-
48
- export const EXIT_TOOLS = [
49
- handoffToHumanAgent.name,
50
- outOfContext.name,
51
- finishConversation.name,
52
- ]
package/src/utils.ts DELETED
@@ -1,28 +0,0 @@
1
- import { AzureChatOpenAI } from '@langchain/openai'
2
-
3
- import {
4
- AZURE_OPENAI_API_BASE,
5
- AZURE_OPENAI_API_DEPLOYMENT_NAME,
6
- AZURE_OPENAI_API_KEY,
7
- AZURE_OPENAI_API_VERSION,
8
- } from './constants'
9
-
10
- export enum AiProvider {
11
- AzureOpenAI = 'azureOpenAI',
12
- }
13
-
14
- export function loadChatModel(provider: AiProvider) {
15
- switch (provider) {
16
- case AiProvider.AzureOpenAI:
17
- return new AzureChatOpenAI({
18
- azureOpenAIApiVersion: AZURE_OPENAI_API_VERSION,
19
- azureOpenAIApiKey: AZURE_OPENAI_API_KEY,
20
- azureOpenAIApiDeploymentName: AZURE_OPENAI_API_DEPLOYMENT_NAME,
21
- azureOpenAIEndpoint: AZURE_OPENAI_API_BASE,
22
- temperature: 0,
23
- // other params...
24
- })
25
- default:
26
- throw new Error(`Provider ${provider} not supported`)
27
- }
28
- }