@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.
- package/lib/cjs/constants.d.ts +1 -0
- package/lib/cjs/constants.js +2 -1
- package/lib/cjs/constants.js.map +1 -1
- package/lib/cjs/hubtype-api-client.d.ts +2 -0
- package/lib/cjs/hubtype-api-client.js +41 -2
- package/lib/cjs/hubtype-api-client.js.map +1 -1
- package/lib/cjs/index.d.ts +2 -1
- package/lib/cjs/index.js +23 -14
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/runner.d.ts +9 -0
- package/lib/cjs/runner.js +58 -0
- package/lib/cjs/runner.js.map +1 -0
- package/lib/cjs/structured-output/carousel.d.ts +100 -0
- package/lib/cjs/structured-output/carousel.js +21 -0
- package/lib/cjs/structured-output/carousel.js.map +1 -0
- package/lib/cjs/structured-output/exit.d.ts +12 -0
- package/lib/cjs/structured-output/exit.js +11 -0
- package/lib/cjs/structured-output/exit.js.map +1 -0
- package/lib/cjs/structured-output/index.d.ts +198 -0
- package/lib/cjs/structured-output/index.js +14 -0
- package/lib/cjs/structured-output/index.js.map +1 -0
- package/lib/cjs/structured-output/shared.d.ts +3 -0
- package/lib/cjs/structured-output/shared.js +3 -0
- package/lib/cjs/structured-output/shared.js.map +1 -0
- package/lib/cjs/structured-output/text-with-buttons.d.ts +34 -0
- package/lib/cjs/structured-output/text-with-buttons.js +15 -0
- package/lib/cjs/structured-output/text-with-buttons.js.map +1 -0
- package/lib/cjs/structured-output/text.d.ts +28 -0
- package/lib/cjs/structured-output/text.js +13 -0
- package/lib/cjs/structured-output/text.js.map +1 -0
- package/lib/cjs/types.d.ts +6 -25
- package/lib/esm/constants.d.ts +1 -0
- package/lib/esm/constants.js +1 -0
- package/lib/esm/constants.js.map +1 -1
- package/lib/esm/hubtype-api-client.d.ts +2 -0
- package/lib/esm/hubtype-api-client.js +41 -2
- package/lib/esm/hubtype-api-client.js.map +1 -1
- package/lib/esm/index.d.ts +2 -1
- package/lib/esm/index.js +21 -12
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/runner.d.ts +9 -0
- package/lib/esm/runner.js +54 -0
- package/lib/esm/runner.js.map +1 -0
- package/lib/esm/structured-output/carousel.d.ts +100 -0
- package/lib/esm/structured-output/carousel.js +18 -0
- package/lib/esm/structured-output/carousel.js.map +1 -0
- package/lib/esm/structured-output/exit.d.ts +12 -0
- package/lib/esm/structured-output/exit.js +7 -0
- package/lib/esm/structured-output/exit.js.map +1 -0
- package/lib/esm/structured-output/index.d.ts +198 -0
- package/lib/esm/structured-output/index.js +11 -0
- package/lib/esm/structured-output/index.js.map +1 -0
- package/lib/esm/structured-output/shared.d.ts +3 -0
- package/lib/esm/structured-output/shared.js +2 -0
- package/lib/esm/structured-output/shared.js.map +1 -0
- package/lib/esm/structured-output/text-with-buttons.d.ts +34 -0
- package/lib/esm/structured-output/text-with-buttons.js +11 -0
- package/lib/esm/structured-output/text-with-buttons.js.map +1 -0
- package/lib/esm/structured-output/text.d.ts +28 -0
- package/lib/esm/structured-output/text.js +10 -0
- package/lib/esm/structured-output/text.js.map +1 -0
- package/lib/esm/types.d.ts +6 -25
- package/package.json +3 -4
- package/src/constants.ts +2 -0
- package/src/hubtype-api-client.ts +55 -9
- package/src/index.ts +26 -19
- package/src/runner.ts +74 -0
- package/src/structured-output/carousel.ts +36 -0
- package/src/structured-output/exit.ts +15 -0
- package/src/structured-output/index.ts +27 -0
- package/src/structured-output/shared.ts +3 -0
- package/src/structured-output/text-with-buttons.ts +23 -0
- package/src/structured-output/text.ts +19 -0
- package/src/types.ts +7 -34
- package/lib/cjs/ai-agent-client.d.ts +0 -9
- package/lib/cjs/ai-agent-client.js +0 -49
- package/lib/cjs/ai-agent-client.js.map +0 -1
- package/lib/cjs/errors.d.ts +0 -3
- package/lib/cjs/errors.js +0 -11
- package/lib/cjs/errors.js.map +0 -1
- package/lib/cjs/hubtype-client.d.ts +0 -7
- package/lib/cjs/hubtype-client.js +0 -37
- package/lib/cjs/hubtype-client.js.map +0 -1
- package/lib/cjs/tools/custom.d.ts +0 -3
- package/lib/cjs/tools/custom.js +0 -14
- package/lib/cjs/tools/custom.js.map +0 -1
- package/lib/cjs/tools/default.d.ts +0 -6
- package/lib/cjs/tools/default.js +0 -41
- package/lib/cjs/tools/default.js.map +0 -1
- package/lib/cjs/utils.d.ts +0 -5
- package/lib/cjs/utils.js +0 -26
- package/lib/cjs/utils.js.map +0 -1
- package/lib/esm/ai-agent-client.d.ts +0 -9
- package/lib/esm/ai-agent-client.js +0 -45
- package/lib/esm/ai-agent-client.js.map +0 -1
- package/lib/esm/errors.d.ts +0 -3
- package/lib/esm/errors.js +0 -7
- package/lib/esm/errors.js.map +0 -1
- package/lib/esm/hubtype-client.d.ts +0 -7
- package/lib/esm/hubtype-client.js +0 -33
- package/lib/esm/hubtype-client.js.map +0 -1
- package/lib/esm/tools/custom.d.ts +0 -3
- package/lib/esm/tools/custom.js +0 -10
- package/lib/esm/tools/custom.js.map +0 -1
- package/lib/esm/tools/default.d.ts +0 -6
- package/lib/esm/tools/default.js +0 -38
- package/lib/esm/tools/default.js.map +0 -1
- package/lib/esm/utils.d.ts +0 -5
- package/lib/esm/utils.js +0 -22
- package/lib/esm/utils.js.map +0 -1
- package/src/ai-agent-client.ts +0 -69
- package/src/errors.ts +0 -6
- package/src/hubtype-client.ts +0 -43
- package/src/tools/custom.ts +0 -14
- package/src/tools/default.ts +0 -52
- package/src/utils.ts +0 -28
package/src/ai-agent-client.ts
DELETED
|
@@ -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
package/src/hubtype-client.ts
DELETED
|
@@ -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
|
-
}
|
package/src/tools/custom.ts
DELETED
|
@@ -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
|
-
}
|
package/src/tools/default.ts
DELETED
|
@@ -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
|
-
}
|