@botpress/adk 1.19.0-beta.9 → 2.0.0-alpha.1
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/dist/agent-init/agent-project-generator.d.ts +5 -1
- package/dist/agent-init/agent-project-generator.d.ts.map +1 -1
- package/dist/agent-project/agent-project.d.ts +3 -3
- package/dist/agent-project/agent-project.d.ts.map +1 -1
- package/dist/agent-project/agent-resolver.d.ts.map +1 -1
- package/dist/agent-project/config-writer.d.ts +1 -1
- package/dist/agent-project/dependency-state-to-dependencies.d.ts +11 -0
- package/dist/agent-project/dependency-state-to-dependencies.d.ts.map +1 -0
- package/dist/agent-project/types.d.ts +9 -4
- package/dist/agent-project/types.d.ts.map +1 -1
- package/dist/agent0/capabilities/commands/adk-build.md +6 -5
- package/dist/agent0/capabilities/commands/adk-debug.md +1 -1
- package/dist/agent0/capabilities/commands/adk-dev-console.md +1 -1
- package/dist/agent0/capabilities/commands/adk-document.md +10 -2
- package/dist/agent0/capabilities/commands/adk-eval.md +1 -1
- package/dist/agent0/capabilities/commands/adk-explain.md +2 -2
- package/dist/agent0/capabilities/commands/adk-init.md +2 -2
- package/dist/agent0/capabilities/commands/adk-ship.md +1 -1
- package/dist/agent0/capabilities/commands/adk-test.md +3 -3
- package/dist/agent0/capabilities/commands/adk-validate.md +3 -3
- package/dist/agent0/capabilities/skills/adk/SKILL.md +21 -21
- package/dist/agent0/capabilities/skills/adk/references/actions.md +245 -239
- package/dist/agent0/capabilities/skills/adk/references/advanced-patterns.md +185 -218
- package/dist/agent0/capabilities/skills/adk/references/agent-config.md +74 -71
- package/dist/agent0/capabilities/skills/adk/references/assets.md +45 -48
- package/dist/agent0/capabilities/skills/adk/references/autonomous-execution.md +40 -46
- package/dist/agent0/capabilities/skills/adk/references/cli.md +17 -7
- package/dist/agent0/capabilities/skills/adk/references/context-api.md +98 -94
- package/dist/agent0/capabilities/skills/adk/references/conversation-lifecycle.md +65 -62
- package/dist/agent0/capabilities/skills/adk/references/conversations.md +395 -385
- package/dist/agent0/capabilities/skills/adk/references/custom-components.md +1 -1
- package/dist/agent0/capabilities/skills/adk/references/desk.md +236 -234
- package/dist/agent0/capabilities/skills/adk/references/explain-config.md +54 -37
- package/dist/agent0/capabilities/skills/adk/references/files.md +301 -306
- package/dist/agent0/capabilities/skills/adk/references/integration-actions.md +211 -214
- package/dist/agent0/capabilities/skills/adk/references/integrations.md +33 -31
- package/dist/agent0/capabilities/skills/adk/references/interfaces.md +13 -12
- package/dist/agent0/capabilities/skills/adk/references/knowledge-bases.md +98 -111
- package/dist/agent0/capabilities/skills/adk/references/mcp-server.md +6 -0
- package/dist/agent0/capabilities/skills/adk/references/messages.md +280 -256
- package/dist/agent0/capabilities/skills/adk/references/model-configuration.md +52 -49
- package/dist/agent0/capabilities/skills/adk/references/patterns-mistakes.md +102 -101
- package/dist/agent0/capabilities/skills/adk/references/plugins.md +15 -15
- package/dist/agent0/capabilities/skills/adk/references/tables.md +204 -210
- package/dist/agent0/capabilities/skills/adk/references/tags.md +141 -143
- package/dist/agent0/capabilities/skills/adk/references/tools.md +191 -199
- package/dist/agent0/capabilities/skills/adk/references/triggers.md +331 -336
- package/dist/agent0/capabilities/skills/adk/references/workflow-steps.md +132 -96
- package/dist/agent0/capabilities/skills/adk/references/workflows.md +479 -464
- package/dist/agent0/capabilities/skills/adk/references/zai-agent-reference.md +127 -129
- package/dist/agent0/capabilities/skills/adk/references/zai-complete-guide.md +255 -276
- package/dist/agent0/capabilities/skills/adk-debugger/SKILL.md +21 -17
- package/dist/agent0/capabilities/skills/adk-debugger/references/common-failures.md +25 -25
- package/dist/agent0/capabilities/skills/adk-debugger/references/conversation-analysis.md +49 -41
- package/dist/agent0/capabilities/skills/adk-debugger/references/debug-workflow.md +20 -14
- package/dist/agent0/capabilities/skills/adk-debugger/references/llm-debugging.md +13 -13
- package/dist/agent0/capabilities/skills/adk-debugger/references/trace-summarization.md +18 -18
- package/dist/agent0/capabilities/skills/adk-debugger/references/traces-and-logs.md +67 -67
- package/dist/agent0/capabilities/skills/adk-dev-console/SKILL.md +36 -36
- package/dist/agent0/capabilities/skills/adk-dev-console/references/agent-steps.md +14 -6
- package/dist/agent0/capabilities/skills/adk-dev-console/references/multi-agent-dashboard.md +13 -13
- package/dist/agent0/capabilities/skills/adk-dev-console/references/pages.md +18 -0
- package/dist/agent0/capabilities/skills/adk-docs/SKILL.md +13 -13
- package/dist/agent0/capabilities/skills/adk-docs/references/doc-standards.md +15 -0
- package/dist/agent0/capabilities/skills/adk-evals/SKILL.md +29 -23
- package/dist/agent0/capabilities/skills/adk-evals/references/eval-format.md +31 -23
- package/dist/agent0/capabilities/skills/adk-evals/references/test-patterns.md +25 -36
- package/dist/agent0/capabilities/skills/adk-evals/references/testing-workflow.md +6 -4
- package/dist/agent0/index.js +41 -19
- package/dist/agent0/index.js.map +7 -7
- package/dist/agent0/runtime/client.d.ts +2 -0
- package/dist/agent0/runtime/client.d.ts.map +1 -1
- package/dist/agent0/runtime/engine.d.ts +2 -1
- package/dist/agent0/runtime/engine.d.ts.map +1 -1
- package/dist/agent0/runtime/process.d.ts +1 -0
- package/dist/agent0/runtime/process.d.ts.map +1 -1
- package/dist/assets/manager.d.ts +3 -0
- package/dist/assets/manager.d.ts.map +1 -1
- package/dist/auth/credentials.d.ts +1 -1
- package/dist/bot-generator/generator.d.ts +22 -0
- package/dist/bot-generator/generator.d.ts.map +1 -1
- package/dist/bot-generator/plugin-sync.d.ts.map +1 -1
- package/dist/commands/base-command.d.ts +1 -0
- package/dist/commands/base-command.d.ts.map +1 -1
- package/dist/commands/bp-dev-command.d.ts +1 -1
- package/dist/commands/bp-dev-command.d.ts.map +1 -1
- package/dist/commands/bp-dev-stderr.d.ts +9 -0
- package/dist/commands/bp-dev-stderr.d.ts.map +1 -1
- package/dist/commands/index.d.ts +1 -0
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/dependencies/catalog/catalog-service.d.ts +53 -0
- package/dist/dependencies/catalog/catalog-service.d.ts.map +1 -0
- package/dist/dependencies/catalog/client-factory.d.ts +33 -0
- package/dist/dependencies/catalog/client-factory.d.ts.map +1 -0
- package/dist/dependencies/catalog/integration-source.d.ts +34 -0
- package/dist/dependencies/catalog/integration-source.d.ts.map +1 -0
- package/dist/dependencies/catalog/interface-source.d.ts +23 -0
- package/dist/dependencies/catalog/interface-source.d.ts.map +1 -0
- package/dist/dependencies/catalog/plugin-source.d.ts +49 -0
- package/dist/dependencies/catalog/plugin-source.d.ts.map +1 -0
- package/dist/dependencies/catalog/resolution-cache.d.ts +76 -0
- package/dist/dependencies/catalog/resolution-cache.d.ts.map +1 -0
- package/dist/dependencies/dependency-manager.d.ts +31 -16
- package/dist/dependencies/dependency-manager.d.ts.map +1 -1
- package/dist/dependencies/errors.d.ts +2 -2
- package/dist/dependencies/errors.d.ts.map +1 -1
- package/dist/dependencies/index.d.ts +5 -4
- package/dist/dependencies/index.d.ts.map +1 -1
- package/dist/dependencies/index.js +2089 -1670
- package/dist/dependencies/index.js.map +37 -30
- package/dist/dependencies/legacy-lock-file.d.ts +15 -0
- package/dist/dependencies/legacy-lock-file.d.ts.map +1 -0
- package/dist/dependencies/migration.d.ts +19 -0
- package/dist/dependencies/migration.d.ts.map +1 -1
- package/dist/dependencies/resolvers/integration-resolver.d.ts +4 -3
- package/dist/dependencies/resolvers/integration-resolver.d.ts.map +1 -1
- package/dist/dependencies/resolvers/plugin-resolver.d.ts +4 -4
- package/dist/dependencies/resolvers/plugin-resolver.d.ts.map +1 -1
- package/dist/dependencies/snapshot-store.d.ts +59 -0
- package/dist/dependencies/snapshot-store.d.ts.map +1 -0
- package/dist/dependencies/status-resolver.d.ts +36 -0
- package/dist/dependencies/status-resolver.d.ts.map +1 -0
- package/dist/dependencies/status.d.ts +214 -0
- package/dist/dependencies/status.d.ts.map +1 -0
- package/dist/dependencies/types.d.ts +380 -29
- package/dist/dependencies/types.d.ts.map +1 -1
- package/dist/file-watcher/watcher.d.ts +21 -5
- package/dist/file-watcher/watcher.d.ts.map +1 -1
- package/dist/generators/action-types.d.ts.map +1 -1
- package/dist/generators/client-wrapper.d.ts.map +1 -1
- package/dist/index.d.ts +4 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3134 -2226
- package/dist/index.js.map +55 -48
- package/dist/integrations/config-utils.d.ts +11 -7
- package/dist/integrations/config-utils.d.ts.map +1 -1
- package/dist/integrations/hub-cache.d.ts +0 -58
- package/dist/integrations/hub-cache.d.ts.map +1 -1
- package/dist/integrations/index.d.ts +1 -3
- package/dist/integrations/index.d.ts.map +1 -1
- package/dist/integrations/manager.d.ts +19 -53
- package/dist/integrations/manager.d.ts.map +1 -1
- package/dist/interfaces/enhanced-cache.d.ts +7 -7
- package/dist/interfaces/enhanced-cache.d.ts.map +1 -1
- package/dist/interfaces/manager.d.ts +11 -17
- package/dist/interfaces/manager.d.ts.map +1 -1
- package/dist/knowledge/manager.d.ts +3 -0
- package/dist/knowledge/manager.d.ts.map +1 -1
- package/dist/plugins/index.d.ts +0 -1
- package/dist/plugins/index.d.ts.map +1 -1
- package/dist/plugins/manager.d.ts +16 -48
- package/dist/plugins/manager.d.ts.map +1 -1
- package/dist/preflight/checker.d.ts +6 -1
- package/dist/preflight/checker.d.ts.map +1 -1
- package/dist/preflight/dependency-gate.d.ts +37 -0
- package/dist/preflight/dependency-gate.d.ts.map +1 -0
- package/dist/preflight/index.d.ts +2 -1
- package/dist/preflight/index.d.ts.map +1 -1
- package/dist/preflight/types.d.ts +18 -0
- package/dist/preflight/types.d.ts.map +1 -1
- package/dist/runner/script-runner.d.ts.map +1 -1
- package/dist/tables/table-manager.d.ts +3 -0
- package/dist/tables/table-manager.d.ts.map +1 -1
- package/dist/templates/README.md +23 -13
- package/dist/templates/blank/agent.config.ts +3 -3
- package/dist/templates/blank/src/triggers/index.ts +1 -1
- package/dist/templates/botpress-desk/evals/index.eval.ts +1 -1
- package/dist/templates/botpress-desk/src/triggers/index.ts +1 -1
- package/dist/templates/crm-enrichment/README.md +3 -3
- package/dist/templates/crm-enrichment/agent.config.ts +2 -2
- package/dist/templates/crm-enrichment/src/conversations/index.ts +1 -1
- package/dist/templates/hello-world/agent.config.ts +3 -3
- package/dist/templates/hello-world/src/triggers/index.ts +1 -1
- package/dist/templates/knowledge-assistant/src/triggers/index.ts +1 -1
- package/dist/templates/template.config.json +3 -3
- package/dist/utils/ids.d.ts +10 -0
- package/dist/utils/ids.d.ts.map +1 -1
- package/dist/workspace/workspace-cache.d.ts +2 -1
- package/dist/workspace/workspace-cache.d.ts.map +1 -1
- package/package.json +3 -3
- package/dist/agent-project/lock-to-dependencies.d.ts +0 -11
- package/dist/agent-project/lock-to-dependencies.d.ts.map +0 -1
- package/dist/dependencies/lock-file.d.ts +0 -15
- package/dist/dependencies/lock-file.d.ts.map +0 -1
- package/dist/integrations/enhanced-cache.d.ts +0 -53
- package/dist/integrations/enhanced-cache.d.ts.map +0 -1
- package/dist/plugins/enhanced-cache.d.ts +0 -53
- package/dist/plugins/enhanced-cache.d.ts.map +0 -1
- package/dist/templates/blank/package.json +0 -17
- package/dist/templates/blank/tsconfig.json +0 -25
- package/dist/templates/botpress-desk/package.json +0 -17
- package/dist/templates/crm-enrichment/package.json +0 -18
- package/dist/templates/crm-enrichment/tsconfig.json +0 -25
- package/dist/templates/hello-world/package.json +0 -18
- package/dist/templates/hello-world/tsconfig.json +0 -25
- package/dist/templates/knowledge-assistant/package.json +0 -18
- package/dist/templates/knowledge-assistant/tsconfig.json +0 -25
- package/dist/templates/slack-triage/package.json +0 -18
- package/dist/templates/slack-triage/tsconfig.json +0 -25
|
@@ -5,12 +5,14 @@ Conversations handle incoming messages and route them based on channel. They're
|
|
|
5
5
|
## Basic Concepts
|
|
6
6
|
|
|
7
7
|
### What are Conversations?
|
|
8
|
+
|
|
8
9
|
- **Channel-specific**: Each conversation handles a specific channel (chat, Slack, etc.)
|
|
9
10
|
- **Stateful**: Maintain state per conversation
|
|
10
11
|
- **AI-powered**: Built-in support for AI execution with tools and knowledge
|
|
11
12
|
- **Interactive**: Can trigger workflows and handle workflow requests
|
|
12
13
|
|
|
13
14
|
### File Location
|
|
15
|
+
|
|
14
16
|
- **Location**: `src/conversations/*.ts`
|
|
15
17
|
- **Auto-registration**: Files automatically register for their specified channel
|
|
16
18
|
|
|
@@ -19,35 +21,35 @@ Conversations handle incoming messages and route them based on channel. They're
|
|
|
19
21
|
### Basic Structure
|
|
20
22
|
|
|
21
23
|
```typescript
|
|
22
|
-
import { Conversation, z } from
|
|
24
|
+
import { Conversation, z } from '@botpress/runtime'
|
|
23
25
|
|
|
24
26
|
export const Chat = new Conversation({
|
|
25
|
-
channel:
|
|
27
|
+
channel: 'chat.channel', // Required: channel ID
|
|
26
28
|
|
|
27
29
|
// Optional: Per-conversation state
|
|
28
30
|
state: z.object({
|
|
29
31
|
count: z.number().default(0),
|
|
30
|
-
activeWorkflowId: z.string().optional()
|
|
32
|
+
activeWorkflowId: z.string().optional(),
|
|
31
33
|
}),
|
|
32
34
|
|
|
33
35
|
async handler({ message, state, conversation, execute, type, event, request, completion }) {
|
|
34
36
|
// Access conversation state via the state parameter
|
|
35
|
-
state.count += 1
|
|
37
|
+
state.count += 1
|
|
36
38
|
|
|
37
39
|
// Send messages via conversation instance
|
|
38
40
|
await conversation.send({
|
|
39
|
-
type:
|
|
40
|
-
payload: { text:
|
|
41
|
-
})
|
|
41
|
+
type: 'text',
|
|
42
|
+
payload: { text: 'Hello!' },
|
|
43
|
+
})
|
|
42
44
|
|
|
43
45
|
// AI-powered response
|
|
44
46
|
await execute({
|
|
45
|
-
instructions:
|
|
47
|
+
instructions: 'You are a helpful assistant',
|
|
46
48
|
tools: [searchTool],
|
|
47
|
-
knowledge: [MyKnowledgeBase]
|
|
48
|
-
})
|
|
49
|
-
}
|
|
50
|
-
})
|
|
49
|
+
knowledge: [MyKnowledgeBase],
|
|
50
|
+
})
|
|
51
|
+
},
|
|
52
|
+
})
|
|
51
53
|
```
|
|
52
54
|
|
|
53
55
|
### Handler Parameters
|
|
@@ -74,25 +76,25 @@ Different platforms have different channel IDs:
|
|
|
74
76
|
|
|
75
77
|
```typescript
|
|
76
78
|
// Botpress Chat
|
|
77
|
-
channel:
|
|
79
|
+
channel: 'chat.channel'
|
|
78
80
|
|
|
79
81
|
// Slack
|
|
80
|
-
channel:
|
|
81
|
-
channel:
|
|
82
|
+
channel: 'slack.dm' // Direct messages
|
|
83
|
+
channel: 'slack.channel' // Channel messages
|
|
82
84
|
|
|
83
85
|
// Discord
|
|
84
|
-
channel:
|
|
85
|
-
channel:
|
|
86
|
+
channel: 'discord.dm'
|
|
87
|
+
channel: 'discord.channel'
|
|
86
88
|
|
|
87
89
|
// WhatsApp
|
|
88
|
-
channel:
|
|
90
|
+
channel: 'whatsapp.channel'
|
|
89
91
|
|
|
90
92
|
// Webchat
|
|
91
|
-
channel:
|
|
93
|
+
channel: 'webchat.channel'
|
|
92
94
|
|
|
93
95
|
// Teams
|
|
94
|
-
channel:
|
|
95
|
-
channel:
|
|
96
|
+
channel: 'teams.dm'
|
|
97
|
+
channel: 'teams.channel'
|
|
96
98
|
```
|
|
97
99
|
|
|
98
100
|
## Message Handling
|
|
@@ -101,37 +103,37 @@ channel: "teams.channel"
|
|
|
101
103
|
|
|
102
104
|
```typescript
|
|
103
105
|
export const Chat = new Conversation({
|
|
104
|
-
channel:
|
|
106
|
+
channel: 'chat.channel',
|
|
105
107
|
|
|
106
108
|
async handler({ message, conversation, execute }) {
|
|
107
109
|
// Check message type
|
|
108
|
-
if (message?.type ===
|
|
109
|
-
const text = message.payload.text
|
|
110
|
+
if (message?.type === 'text') {
|
|
111
|
+
const text = message.payload.text
|
|
110
112
|
|
|
111
113
|
// Command handling
|
|
112
|
-
if (text.startsWith(
|
|
114
|
+
if (text.startsWith('/help')) {
|
|
113
115
|
await conversation.send({
|
|
114
|
-
type:
|
|
115
|
-
payload: { text:
|
|
116
|
-
})
|
|
117
|
-
return
|
|
116
|
+
type: 'text',
|
|
117
|
+
payload: { text: 'Available commands:\n/help - Show help\n/status - Check status' },
|
|
118
|
+
})
|
|
119
|
+
return
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
// Regular message - use AI
|
|
121
123
|
await execute({
|
|
122
|
-
instructions:
|
|
123
|
-
})
|
|
124
|
+
instructions: 'Help the user with their request',
|
|
125
|
+
})
|
|
124
126
|
}
|
|
125
127
|
|
|
126
128
|
// Handle other message types
|
|
127
|
-
if (message?.type ===
|
|
129
|
+
if (message?.type === 'image') {
|
|
128
130
|
await conversation.send({
|
|
129
|
-
type:
|
|
130
|
-
payload: { text:
|
|
131
|
-
})
|
|
131
|
+
type: 'text',
|
|
132
|
+
payload: { text: 'I received your image!' },
|
|
133
|
+
})
|
|
132
134
|
}
|
|
133
|
-
}
|
|
134
|
-
})
|
|
135
|
+
},
|
|
136
|
+
})
|
|
135
137
|
```
|
|
136
138
|
|
|
137
139
|
See **[Messages](./messages.md)** for complete guide on all message types, metadata, and sending patterns.
|
|
@@ -147,21 +149,21 @@ Send messages to the conversation:
|
|
|
147
149
|
```typescript
|
|
148
150
|
// Send text message
|
|
149
151
|
await conversation.send({
|
|
150
|
-
type:
|
|
151
|
-
payload: { text:
|
|
152
|
-
})
|
|
152
|
+
type: 'text',
|
|
153
|
+
payload: { text: 'Hello!' },
|
|
154
|
+
})
|
|
153
155
|
|
|
154
156
|
// Send choice message
|
|
155
157
|
await conversation.send({
|
|
156
|
-
type:
|
|
158
|
+
type: 'choice',
|
|
157
159
|
payload: {
|
|
158
|
-
text:
|
|
160
|
+
text: 'Choose an option:',
|
|
159
161
|
options: [
|
|
160
|
-
{ label:
|
|
161
|
-
{ label:
|
|
162
|
-
]
|
|
163
|
-
}
|
|
164
|
-
})
|
|
162
|
+
{ label: 'Option 1', value: 'opt1' },
|
|
163
|
+
{ label: 'Option 2', value: 'opt2' },
|
|
164
|
+
],
|
|
165
|
+
},
|
|
166
|
+
})
|
|
165
167
|
```
|
|
166
168
|
|
|
167
169
|
See **[Messages](./messages.md)** for all message types and payloads.
|
|
@@ -172,22 +174,22 @@ Show typing indicator to the user:
|
|
|
172
174
|
|
|
173
175
|
```typescript
|
|
174
176
|
export const Chat = new Conversation({
|
|
175
|
-
channel:
|
|
177
|
+
channel: 'chat.channel',
|
|
176
178
|
|
|
177
179
|
async handler({ conversation }) {
|
|
178
180
|
// Start typing indicator
|
|
179
|
-
await conversation.startTyping()
|
|
181
|
+
await conversation.startTyping()
|
|
180
182
|
|
|
181
183
|
// Do some work (API call, processing, etc.)
|
|
182
|
-
await fetchData()
|
|
184
|
+
await fetchData()
|
|
183
185
|
|
|
184
186
|
// Send message (stops typing automatically)
|
|
185
187
|
await conversation.send({
|
|
186
|
-
type:
|
|
187
|
-
payload: { text: "Here's the data!" }
|
|
188
|
-
})
|
|
189
|
-
}
|
|
190
|
-
})
|
|
188
|
+
type: 'text',
|
|
189
|
+
payload: { text: "Here's the data!" },
|
|
190
|
+
})
|
|
191
|
+
},
|
|
192
|
+
})
|
|
191
193
|
```
|
|
192
194
|
|
|
193
195
|
### conversation.stopTyping()
|
|
@@ -196,24 +198,24 @@ Manually stop typing indicator:
|
|
|
196
198
|
|
|
197
199
|
```typescript
|
|
198
200
|
export const Chat = new Conversation({
|
|
199
|
-
channel:
|
|
201
|
+
channel: 'chat.channel',
|
|
200
202
|
|
|
201
203
|
async handler({ conversation }) {
|
|
202
|
-
await conversation.startTyping()
|
|
204
|
+
await conversation.startTyping()
|
|
203
205
|
|
|
204
206
|
try {
|
|
205
|
-
const result = await riskyOperation()
|
|
207
|
+
const result = await riskyOperation()
|
|
206
208
|
await conversation.send({
|
|
207
|
-
type:
|
|
208
|
-
payload: { text: result }
|
|
209
|
-
})
|
|
209
|
+
type: 'text',
|
|
210
|
+
payload: { text: result },
|
|
211
|
+
})
|
|
210
212
|
} catch (error) {
|
|
211
213
|
// Stop typing if sending fails
|
|
212
|
-
await conversation.stopTyping()
|
|
213
|
-
throw error
|
|
214
|
+
await conversation.stopTyping()
|
|
215
|
+
throw error
|
|
214
216
|
}
|
|
215
|
-
}
|
|
216
|
-
})
|
|
217
|
+
},
|
|
218
|
+
})
|
|
217
219
|
```
|
|
218
220
|
|
|
219
221
|
### conversation.tags
|
|
@@ -222,12 +224,12 @@ Access and modify conversation tags:
|
|
|
222
224
|
|
|
223
225
|
```typescript
|
|
224
226
|
// Read tags
|
|
225
|
-
const priority = conversation.tags.priority
|
|
226
|
-
const category = conversation.tags.category
|
|
227
|
+
const priority = conversation.tags.priority
|
|
228
|
+
const category = conversation.tags.category
|
|
227
229
|
|
|
228
230
|
// Set tags
|
|
229
|
-
conversation.tags.priority =
|
|
230
|
-
conversation.tags.category =
|
|
231
|
+
conversation.tags.priority = 'high'
|
|
232
|
+
conversation.tags.category = 'support'
|
|
231
233
|
```
|
|
232
234
|
|
|
233
235
|
See **[Tags](./tags.md)** for complete tag documentation.
|
|
@@ -236,12 +238,12 @@ See **[Tags](./tags.md)** for complete tag documentation.
|
|
|
236
238
|
|
|
237
239
|
```typescript
|
|
238
240
|
// Conversation ID
|
|
239
|
-
const id = conversation.id
|
|
241
|
+
const id = conversation.id
|
|
240
242
|
|
|
241
243
|
// Channel information
|
|
242
|
-
const channel = conversation.channel
|
|
243
|
-
const integration = conversation.integration
|
|
244
|
-
const alias = conversation.alias
|
|
244
|
+
const channel = conversation.channel // e.g., "webchat.channel"
|
|
245
|
+
const integration = conversation.integration // e.g., "webchat"
|
|
246
|
+
const alias = conversation.alias // Integration alias
|
|
245
247
|
```
|
|
246
248
|
|
|
247
249
|
## AI Execution
|
|
@@ -250,11 +252,11 @@ const alias = conversation.alias; // Integration alias
|
|
|
250
252
|
|
|
251
253
|
```typescript
|
|
252
254
|
export const Chat = new Conversation({
|
|
253
|
-
channel:
|
|
255
|
+
channel: 'chat.channel',
|
|
254
256
|
|
|
255
257
|
async handler({ message, execute }) {
|
|
256
258
|
await execute({
|
|
257
|
-
instructions:
|
|
259
|
+
instructions: 'You are a helpful customer support assistant',
|
|
258
260
|
|
|
259
261
|
// Optional: Add tools
|
|
260
262
|
tools: [searchTool, createTicketTool],
|
|
@@ -263,7 +265,7 @@ export const Chat = new Conversation({
|
|
|
263
265
|
knowledge: [DocsKnowledgeBase, FAQKnowledgeBase],
|
|
264
266
|
|
|
265
267
|
// Optional: Model configuration
|
|
266
|
-
model:
|
|
268
|
+
model: 'openai:gpt-4o',
|
|
267
269
|
temperature: 0.7,
|
|
268
270
|
|
|
269
271
|
// Optional: Execution hooks
|
|
@@ -271,13 +273,13 @@ export const Chat = new Conversation({
|
|
|
271
273
|
onTrace: ({ trace }) => console.log(trace),
|
|
272
274
|
onIterationEnd: async (iteration) => {
|
|
273
275
|
if (iteration.isFailed()) {
|
|
274
|
-
console.log(
|
|
276
|
+
console.log('Iteration failed:', iteration.error)
|
|
275
277
|
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
})
|
|
279
|
-
}
|
|
280
|
-
})
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
})
|
|
281
|
+
},
|
|
282
|
+
})
|
|
281
283
|
```
|
|
282
284
|
|
|
283
285
|
See **[Actions](./actions.md#converting-actions-to-tools)** for converting actions to tools with `.asTool()`.
|
|
@@ -290,38 +292,40 @@ Each conversation maintains its own state:
|
|
|
290
292
|
|
|
291
293
|
```typescript
|
|
292
294
|
export const Chat = new Conversation({
|
|
293
|
-
channel:
|
|
295
|
+
channel: 'chat.channel',
|
|
294
296
|
|
|
295
297
|
state: z.object({
|
|
296
298
|
messageCount: z.number().default(0),
|
|
297
299
|
userName: z.string().optional(),
|
|
298
|
-
preferences: z
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
300
|
+
preferences: z
|
|
301
|
+
.object({
|
|
302
|
+
language: z.string().default('en'),
|
|
303
|
+
theme: z.string().default('light'),
|
|
304
|
+
})
|
|
305
|
+
.default({}),
|
|
302
306
|
}),
|
|
303
307
|
|
|
304
308
|
async handler({ message, state, conversation }) {
|
|
305
309
|
// Access and modify state via the state parameter
|
|
306
|
-
state.messageCount += 1
|
|
310
|
+
state.messageCount += 1
|
|
307
311
|
|
|
308
|
-
if (!state.userName && message?.type ===
|
|
309
|
-
state.userName = message.payload.text
|
|
312
|
+
if (!state.userName && message?.type === 'text') {
|
|
313
|
+
state.userName = message.payload.text
|
|
310
314
|
await conversation.send({
|
|
311
|
-
type:
|
|
312
|
-
payload: { text: `Nice to meet you, ${state.userName}!` }
|
|
313
|
-
})
|
|
315
|
+
type: 'text',
|
|
316
|
+
payload: { text: `Nice to meet you, ${state.userName}!` },
|
|
317
|
+
})
|
|
314
318
|
}
|
|
315
319
|
|
|
316
320
|
// State persists across messages
|
|
317
321
|
if (state.messageCount > 10) {
|
|
318
322
|
await conversation.send({
|
|
319
|
-
type:
|
|
320
|
-
payload: { text:
|
|
321
|
-
})
|
|
323
|
+
type: 'text',
|
|
324
|
+
payload: { text: 'Thanks for being an active user!' },
|
|
325
|
+
})
|
|
322
326
|
}
|
|
323
|
-
}
|
|
324
|
-
})
|
|
327
|
+
},
|
|
328
|
+
})
|
|
325
329
|
```
|
|
326
330
|
|
|
327
331
|
## Workflow Integration
|
|
@@ -330,49 +334,51 @@ export const Chat = new Conversation({
|
|
|
330
334
|
|
|
331
335
|
```typescript
|
|
332
336
|
export const Chat = new Conversation({
|
|
333
|
-
channel:
|
|
337
|
+
channel: 'chat.channel',
|
|
334
338
|
state: z.object({
|
|
335
|
-
activeWorkflowId: z.string().optional()
|
|
339
|
+
activeWorkflowId: z.string().optional(),
|
|
336
340
|
}),
|
|
337
341
|
|
|
338
342
|
async handler({ message, state, conversation }) {
|
|
339
|
-
const text = message?.payload.text ||
|
|
343
|
+
const text = message?.payload.text || ''
|
|
340
344
|
|
|
341
|
-
if (text.startsWith(
|
|
345
|
+
if (text.startsWith('/process')) {
|
|
342
346
|
// Start workflow with conversation context
|
|
343
347
|
const instance = await ProcessWorkflow.start({
|
|
344
348
|
conversationId: conversation.id, // Critical for communication!
|
|
345
349
|
userId: user.id,
|
|
346
|
-
data: text.substring(8)
|
|
347
|
-
})
|
|
350
|
+
data: text.substring(8),
|
|
351
|
+
})
|
|
348
352
|
|
|
349
353
|
// Store workflow ID
|
|
350
|
-
state.activeWorkflowId = instance.id
|
|
354
|
+
state.activeWorkflowId = instance.id
|
|
351
355
|
|
|
352
356
|
await conversation.send({
|
|
353
|
-
type:
|
|
354
|
-
payload: { text: `Started processing: ${instance.id}` }
|
|
355
|
-
})
|
|
356
|
-
return
|
|
357
|
+
type: 'text',
|
|
358
|
+
payload: { text: `Started processing: ${instance.id}` },
|
|
359
|
+
})
|
|
360
|
+
return
|
|
357
361
|
}
|
|
358
362
|
|
|
359
|
-
if (text ===
|
|
363
|
+
if (text === '/status' && state.activeWorkflowId) {
|
|
360
364
|
// Check workflow status
|
|
361
365
|
const instance = await ProcessWorkflow.getOrCreate({
|
|
362
366
|
key: state.activeWorkflowId,
|
|
363
|
-
input: {
|
|
364
|
-
|
|
367
|
+
input: {
|
|
368
|
+
/* required input */
|
|
369
|
+
},
|
|
370
|
+
})
|
|
365
371
|
|
|
366
372
|
await conversation.send({
|
|
367
|
-
type:
|
|
373
|
+
type: 'text',
|
|
368
374
|
payload: {
|
|
369
|
-
text: `Status: ${instance?.status ||
|
|
370
|
-
}
|
|
371
|
-
})
|
|
372
|
-
return
|
|
375
|
+
text: `Status: ${instance?.status || 'Not found'}`,
|
|
376
|
+
},
|
|
377
|
+
})
|
|
378
|
+
return
|
|
373
379
|
}
|
|
374
|
-
}
|
|
375
|
-
})
|
|
380
|
+
},
|
|
381
|
+
})
|
|
376
382
|
```
|
|
377
383
|
|
|
378
384
|
### Handling Workflow Requests
|
|
@@ -380,11 +386,12 @@ export const Chat = new Conversation({
|
|
|
380
386
|
Workflows can request data from conversations using `step.request()`. The conversation receives a `workflow_request` type with the request object.
|
|
381
387
|
|
|
382
388
|
**Request Object Structure:**
|
|
389
|
+
|
|
383
390
|
```typescript
|
|
384
391
|
type WorkflowRequest = {
|
|
385
|
-
type: `${WorkflowName}:${RequestName}
|
|
386
|
-
workflow: BaseWorkflowInstance
|
|
387
|
-
step: string
|
|
392
|
+
type: `${WorkflowName}:${RequestName}` // e.g., "processOrder:email"
|
|
393
|
+
workflow: BaseWorkflowInstance // Workflow instance
|
|
394
|
+
step: string // Step name
|
|
388
395
|
}
|
|
389
396
|
```
|
|
390
397
|
|
|
@@ -394,28 +401,28 @@ When `type === "workflow_request"`, both `request` and `event` are properly type
|
|
|
394
401
|
|
|
395
402
|
```typescript
|
|
396
403
|
export const Chat = new Conversation({
|
|
397
|
-
channel:
|
|
404
|
+
channel: 'chat.channel',
|
|
398
405
|
|
|
399
406
|
async handler({ type, request, event, conversation, message }) {
|
|
400
|
-
if (type ===
|
|
407
|
+
if (type === 'workflow_request') {
|
|
401
408
|
// request is typed as WorkflowRequest
|
|
402
409
|
// event is typed as WorkflowDataRequestEventType
|
|
403
410
|
// event.payload.message contains the prompt from step.request()
|
|
404
411
|
|
|
405
412
|
await conversation.send({
|
|
406
|
-
type:
|
|
407
|
-
payload: { text: event.payload.message }
|
|
408
|
-
})
|
|
413
|
+
type: 'text',
|
|
414
|
+
payload: { text: event.payload.message },
|
|
415
|
+
})
|
|
409
416
|
|
|
410
417
|
// Check specific request type
|
|
411
|
-
if (request.type ===
|
|
412
|
-
const email = message?.payload.text ||
|
|
413
|
-
await request.workflow.provide(
|
|
418
|
+
if (request.type === 'processOrder:email') {
|
|
419
|
+
const email = message?.payload.text || 'user@example.com'
|
|
420
|
+
await request.workflow.provide('email', { email })
|
|
414
421
|
}
|
|
415
|
-
return
|
|
422
|
+
return
|
|
416
423
|
}
|
|
417
|
-
}
|
|
418
|
-
})
|
|
424
|
+
},
|
|
425
|
+
})
|
|
419
426
|
```
|
|
420
427
|
|
|
421
428
|
**Legacy: Using `isWorkflowDataRequest` Type Guard (Deprecated)**
|
|
@@ -423,25 +430,25 @@ export const Chat = new Conversation({
|
|
|
423
430
|
> **Deprecated:** Use the `type === "workflow_request"` discriminant instead. It provides the same typed event access plus the `request` object for additional context.
|
|
424
431
|
|
|
425
432
|
```typescript
|
|
426
|
-
import { isWorkflowDataRequest } from
|
|
433
|
+
import { isWorkflowDataRequest } from '@botpress/runtime'
|
|
427
434
|
|
|
428
435
|
export const Chat = new Conversation({
|
|
429
|
-
channel:
|
|
436
|
+
channel: 'chat.channel',
|
|
430
437
|
|
|
431
438
|
async handler({ event, conversation }) {
|
|
432
439
|
// ⚠️ Deprecated — prefer type === "workflow_request"
|
|
433
440
|
if (isWorkflowDataRequest(event)) {
|
|
434
441
|
await conversation.send({
|
|
435
|
-
type:
|
|
436
|
-
payload: { text: event.payload.message }
|
|
437
|
-
})
|
|
442
|
+
type: 'text',
|
|
443
|
+
payload: { text: event.payload.message },
|
|
444
|
+
})
|
|
438
445
|
|
|
439
|
-
const userInput = getUserInput()
|
|
440
|
-
await MyWorkflow.provide(event, { data: userInput })
|
|
441
|
-
return
|
|
446
|
+
const userInput = getUserInput()
|
|
447
|
+
await MyWorkflow.provide(event, { data: userInput })
|
|
448
|
+
return
|
|
442
449
|
}
|
|
443
|
-
}
|
|
444
|
-
})
|
|
450
|
+
},
|
|
451
|
+
})
|
|
445
452
|
```
|
|
446
453
|
|
|
447
454
|
### Handling Workflow Callbacks (Completion Events)
|
|
@@ -449,13 +456,14 @@ export const Chat = new Conversation({
|
|
|
449
456
|
When a workflow completes, fails, is canceled, or times out, the conversation receives a `workflow_callback` event. Use the `completion` object to inspect the result.
|
|
450
457
|
|
|
451
458
|
**Completion Object Structure:**
|
|
459
|
+
|
|
452
460
|
```typescript
|
|
453
461
|
type WorkflowCallback = {
|
|
454
|
-
type: string
|
|
455
|
-
workflow: BaseWorkflowInstance
|
|
456
|
-
status:
|
|
457
|
-
output?: z.infer<WorkflowOutput
|
|
458
|
-
error?: string
|
|
462
|
+
type: string // Workflow name
|
|
463
|
+
workflow: BaseWorkflowInstance // Workflow instance
|
|
464
|
+
status: 'completed' | 'failed' | 'canceled' | 'timed_out'
|
|
465
|
+
output?: z.infer<WorkflowOutput> // Workflow output (if completed)
|
|
466
|
+
error?: string // Error message (if failed)
|
|
459
467
|
}
|
|
460
468
|
```
|
|
461
469
|
|
|
@@ -463,54 +471,54 @@ type WorkflowCallback = {
|
|
|
463
471
|
|
|
464
472
|
```typescript
|
|
465
473
|
export const Chat = new Conversation({
|
|
466
|
-
channel:
|
|
474
|
+
channel: 'chat.channel',
|
|
467
475
|
|
|
468
476
|
async handler({ type, completion, event, conversation }) {
|
|
469
|
-
if (type ===
|
|
477
|
+
if (type === 'workflow_callback') {
|
|
470
478
|
// completion is typed as WorkflowCallback
|
|
471
479
|
// event is typed as WorkflowCallbackEventType
|
|
472
480
|
|
|
473
481
|
switch (completion.status) {
|
|
474
|
-
case
|
|
482
|
+
case 'completed':
|
|
475
483
|
await conversation.send({
|
|
476
|
-
type:
|
|
484
|
+
type: 'text',
|
|
477
485
|
payload: {
|
|
478
|
-
text: `Workflow "${completion.type}" completed successfully
|
|
479
|
-
}
|
|
480
|
-
})
|
|
486
|
+
text: `Workflow "${completion.type}" completed successfully!`,
|
|
487
|
+
},
|
|
488
|
+
})
|
|
481
489
|
// Access workflow output
|
|
482
490
|
if (completion.output) {
|
|
483
|
-
console.log(
|
|
491
|
+
console.log('Output:', completion.output)
|
|
484
492
|
}
|
|
485
|
-
break
|
|
493
|
+
break
|
|
486
494
|
|
|
487
|
-
case
|
|
495
|
+
case 'failed':
|
|
488
496
|
await conversation.send({
|
|
489
|
-
type:
|
|
497
|
+
type: 'text',
|
|
490
498
|
payload: {
|
|
491
|
-
text: `Workflow "${completion.type}" failed: ${completion.error}
|
|
492
|
-
}
|
|
493
|
-
})
|
|
494
|
-
break
|
|
499
|
+
text: `Workflow "${completion.type}" failed: ${completion.error}`,
|
|
500
|
+
},
|
|
501
|
+
})
|
|
502
|
+
break
|
|
495
503
|
|
|
496
|
-
case
|
|
504
|
+
case 'canceled':
|
|
497
505
|
await conversation.send({
|
|
498
|
-
type:
|
|
499
|
-
payload: { text: `Workflow "${completion.type}" was canceled.` }
|
|
500
|
-
})
|
|
501
|
-
break
|
|
506
|
+
type: 'text',
|
|
507
|
+
payload: { text: `Workflow "${completion.type}" was canceled.` },
|
|
508
|
+
})
|
|
509
|
+
break
|
|
502
510
|
|
|
503
|
-
case
|
|
511
|
+
case 'timed_out':
|
|
504
512
|
await conversation.send({
|
|
505
|
-
type:
|
|
506
|
-
payload: { text: `Workflow "${completion.type}" timed out.` }
|
|
507
|
-
})
|
|
508
|
-
break
|
|
513
|
+
type: 'text',
|
|
514
|
+
payload: { text: `Workflow "${completion.type}" timed out.` },
|
|
515
|
+
})
|
|
516
|
+
break
|
|
509
517
|
}
|
|
510
|
-
return
|
|
518
|
+
return
|
|
511
519
|
}
|
|
512
|
-
}
|
|
513
|
-
})
|
|
520
|
+
},
|
|
521
|
+
})
|
|
514
522
|
```
|
|
515
523
|
|
|
516
524
|
**Legacy: Using `isWorkflowCallback` Type Guard (Deprecated)**
|
|
@@ -518,18 +526,18 @@ export const Chat = new Conversation({
|
|
|
518
526
|
> **Deprecated:** Use the `type === "workflow_callback"` discriminant instead. It provides a typed `completion` object with workflow instance, status, output, and error information.
|
|
519
527
|
|
|
520
528
|
```typescript
|
|
521
|
-
import { isWorkflowCallback } from
|
|
529
|
+
import { isWorkflowCallback } from '@botpress/runtime'
|
|
522
530
|
|
|
523
531
|
export const Chat = new Conversation({
|
|
524
|
-
channel:
|
|
532
|
+
channel: 'chat.channel',
|
|
525
533
|
|
|
526
534
|
async handler({ event, conversation }) {
|
|
527
535
|
// ⚠️ Deprecated — prefer type === "workflow_callback"
|
|
528
536
|
if (isWorkflowCallback(event)) {
|
|
529
|
-
console.log(
|
|
537
|
+
console.log('Workflow completed:', event.payload)
|
|
530
538
|
}
|
|
531
|
-
}
|
|
532
|
-
})
|
|
539
|
+
},
|
|
540
|
+
})
|
|
533
541
|
```
|
|
534
542
|
|
|
535
543
|
### Combined Workflow Event Handling
|
|
@@ -538,46 +546,46 @@ A single conversation handler can handle all workflow event types:
|
|
|
538
546
|
|
|
539
547
|
```typescript
|
|
540
548
|
export const Chat = new Conversation({
|
|
541
|
-
channel:
|
|
549
|
+
channel: 'chat.channel',
|
|
542
550
|
state: z.object({
|
|
543
|
-
activeWorkflowId: z.string().optional()
|
|
551
|
+
activeWorkflowId: z.string().optional(),
|
|
544
552
|
}),
|
|
545
553
|
|
|
546
554
|
async handler({ type, message, request, completion, event, conversation, execute }) {
|
|
547
555
|
// Handle workflow data requests
|
|
548
|
-
if (type ===
|
|
556
|
+
if (type === 'workflow_request') {
|
|
549
557
|
await conversation.send({
|
|
550
|
-
type:
|
|
551
|
-
payload: { text: event.payload.message }
|
|
552
|
-
})
|
|
558
|
+
type: 'text',
|
|
559
|
+
payload: { text: event.payload.message },
|
|
560
|
+
})
|
|
553
561
|
// Wait for user input, then provide it back
|
|
554
|
-
return
|
|
562
|
+
return
|
|
555
563
|
}
|
|
556
564
|
|
|
557
565
|
// Handle workflow completion/failure
|
|
558
|
-
if (type ===
|
|
559
|
-
if (completion.status ===
|
|
566
|
+
if (type === 'workflow_callback') {
|
|
567
|
+
if (completion.status === 'completed') {
|
|
560
568
|
await conversation.send({
|
|
561
|
-
type:
|
|
562
|
-
payload: { text: `Done! Result: ${JSON.stringify(completion.output)}` }
|
|
563
|
-
})
|
|
569
|
+
type: 'text',
|
|
570
|
+
payload: { text: `Done! Result: ${JSON.stringify(completion.output)}` },
|
|
571
|
+
})
|
|
564
572
|
} else {
|
|
565
573
|
await conversation.send({
|
|
566
|
-
type:
|
|
567
|
-
payload: { text: `Workflow ended with status: ${completion.status}` }
|
|
568
|
-
})
|
|
574
|
+
type: 'text',
|
|
575
|
+
payload: { text: `Workflow ended with status: ${completion.status}` },
|
|
576
|
+
})
|
|
569
577
|
}
|
|
570
|
-
return
|
|
578
|
+
return
|
|
571
579
|
}
|
|
572
580
|
|
|
573
581
|
// Handle regular messages
|
|
574
|
-
if (type ===
|
|
582
|
+
if (type === 'message') {
|
|
575
583
|
await execute({
|
|
576
|
-
instructions:
|
|
577
|
-
})
|
|
584
|
+
instructions: 'You are a helpful assistant',
|
|
585
|
+
})
|
|
578
586
|
}
|
|
579
|
-
}
|
|
580
|
-
})
|
|
587
|
+
},
|
|
588
|
+
})
|
|
581
589
|
```
|
|
582
590
|
|
|
583
591
|
See **[Workflows](./workflows.md#conversation-communication)** for complete workflow request patterns including state management and multiple request types.
|
|
@@ -588,69 +596,67 @@ See **[Workflows](./workflows.md#conversation-communication)** for complete work
|
|
|
588
596
|
|
|
589
597
|
```typescript
|
|
590
598
|
export const Chat = new Conversation({
|
|
591
|
-
channel:
|
|
599
|
+
channel: 'chat.channel',
|
|
592
600
|
|
|
593
601
|
async handler({ message, conversation, execute }) {
|
|
594
|
-
if (message?.type !==
|
|
602
|
+
if (message?.type !== 'text') return
|
|
595
603
|
|
|
596
|
-
const text = message.payload.text
|
|
597
|
-
const command = text.split(
|
|
598
|
-
const args = text.substring(command.length).trim()
|
|
604
|
+
const text = message.payload.text
|
|
605
|
+
const command = text.split(' ')[0].toLowerCase()
|
|
606
|
+
const args = text.substring(command.length).trim()
|
|
599
607
|
|
|
600
608
|
switch (command) {
|
|
601
|
-
case
|
|
609
|
+
case '/help':
|
|
602
610
|
await conversation.send({
|
|
603
|
-
type:
|
|
611
|
+
type: 'text',
|
|
604
612
|
payload: {
|
|
605
613
|
text: `Available commands:
|
|
606
614
|
/help - Show this help
|
|
607
615
|
/search <query> - Search knowledge base
|
|
608
616
|
/refresh - Refresh knowledge base
|
|
609
|
-
/workflow <data> - Start workflow
|
|
610
|
-
}
|
|
611
|
-
})
|
|
612
|
-
break
|
|
617
|
+
/workflow <data> - Start workflow`,
|
|
618
|
+
},
|
|
619
|
+
})
|
|
620
|
+
break
|
|
613
621
|
|
|
614
|
-
case
|
|
615
|
-
const results = await MyKnowledgeBase.search({ query: args, limit: 5 })
|
|
622
|
+
case '/search':
|
|
623
|
+
const results = await MyKnowledgeBase.search({ query: args, limit: 5 })
|
|
616
624
|
await conversation.send({
|
|
617
|
-
type:
|
|
625
|
+
type: 'text',
|
|
618
626
|
payload: {
|
|
619
|
-
text: results.length > 0
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
});
|
|
624
|
-
break;
|
|
627
|
+
text: results.length > 0 ? `Found ${results.length} results:\n${results.join('\n')}` : 'No results found',
|
|
628
|
+
},
|
|
629
|
+
})
|
|
630
|
+
break
|
|
625
631
|
|
|
626
|
-
case
|
|
627
|
-
await MyKnowledgeBase.refresh()
|
|
632
|
+
case '/refresh':
|
|
633
|
+
await MyKnowledgeBase.refresh()
|
|
628
634
|
await conversation.send({
|
|
629
|
-
type:
|
|
630
|
-
payload: { text:
|
|
631
|
-
})
|
|
632
|
-
break
|
|
635
|
+
type: 'text',
|
|
636
|
+
payload: { text: 'Knowledge base refreshed!' },
|
|
637
|
+
})
|
|
638
|
+
break
|
|
633
639
|
|
|
634
|
-
case
|
|
640
|
+
case '/workflow':
|
|
635
641
|
const instance = await MyWorkflow.getOrCreate({
|
|
636
642
|
key: user.id,
|
|
637
|
-
input: { userId: user.id, data: args }
|
|
638
|
-
})
|
|
643
|
+
input: { userId: user.id, data: args },
|
|
644
|
+
})
|
|
639
645
|
await conversation.send({
|
|
640
|
-
type:
|
|
641
|
-
payload: { text: `Workflow ${instance.id} started` }
|
|
642
|
-
})
|
|
643
|
-
break
|
|
646
|
+
type: 'text',
|
|
647
|
+
payload: { text: `Workflow ${instance.id} started` },
|
|
648
|
+
})
|
|
649
|
+
break
|
|
644
650
|
|
|
645
651
|
default:
|
|
646
652
|
// Not a command, use AI
|
|
647
653
|
await execute({
|
|
648
|
-
instructions:
|
|
649
|
-
knowledge: [MyKnowledgeBase]
|
|
650
|
-
})
|
|
654
|
+
instructions: 'You are a helpful assistant',
|
|
655
|
+
knowledge: [MyKnowledgeBase],
|
|
656
|
+
})
|
|
651
657
|
}
|
|
652
|
-
}
|
|
653
|
-
})
|
|
658
|
+
},
|
|
659
|
+
})
|
|
654
660
|
```
|
|
655
661
|
|
|
656
662
|
## Advanced Patterns
|
|
@@ -662,7 +668,7 @@ Create separate conversations for different channels:
|
|
|
662
668
|
```typescript
|
|
663
669
|
// src/conversations/slack-dm.ts
|
|
664
670
|
export const SlackDM = new Conversation({
|
|
665
|
-
channel:
|
|
671
|
+
channel: 'slack.dm',
|
|
666
672
|
|
|
667
673
|
async handler({ message, execute }) {
|
|
668
674
|
// Slack-specific handling
|
|
@@ -671,26 +677,26 @@ export const SlackDM = new Conversation({
|
|
|
671
677
|
}
|
|
672
678
|
|
|
673
679
|
await execute({
|
|
674
|
-
instructions:
|
|
675
|
-
})
|
|
676
|
-
}
|
|
677
|
-
})
|
|
680
|
+
instructions: 'You are a Slack assistant. Use Slack markdown formatting.',
|
|
681
|
+
})
|
|
682
|
+
},
|
|
683
|
+
})
|
|
678
684
|
|
|
679
685
|
// src/conversations/slack-channel.ts
|
|
680
686
|
export const SlackChannel = new Conversation({
|
|
681
|
-
channel:
|
|
687
|
+
channel: 'slack.channel',
|
|
682
688
|
|
|
683
689
|
async handler({ message, execute }) {
|
|
684
690
|
// Only respond when mentioned
|
|
685
|
-
if (!message?.payload.text?.includes(
|
|
686
|
-
return
|
|
691
|
+
if (!message?.payload.text?.includes('@bot')) {
|
|
692
|
+
return // Don't respond
|
|
687
693
|
}
|
|
688
694
|
|
|
689
695
|
await execute({
|
|
690
|
-
instructions:
|
|
691
|
-
})
|
|
692
|
-
}
|
|
693
|
-
})
|
|
696
|
+
instructions: 'You are in a public Slack channel. Be concise.',
|
|
697
|
+
})
|
|
698
|
+
},
|
|
699
|
+
})
|
|
694
700
|
```
|
|
695
701
|
|
|
696
702
|
### Conversation Context
|
|
@@ -699,36 +705,40 @@ Maintain context across messages:
|
|
|
699
705
|
|
|
700
706
|
```typescript
|
|
701
707
|
export const Chat = new Conversation({
|
|
702
|
-
channel:
|
|
708
|
+
channel: 'chat.channel',
|
|
703
709
|
state: z.object({
|
|
704
|
-
context: z
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
710
|
+
context: z
|
|
711
|
+
.array(
|
|
712
|
+
z.object({
|
|
713
|
+
role: z.string(),
|
|
714
|
+
content: z.string(),
|
|
715
|
+
})
|
|
716
|
+
)
|
|
717
|
+
.default([]),
|
|
708
718
|
}),
|
|
709
719
|
|
|
710
720
|
async handler({ message, state, execute }) {
|
|
711
721
|
// Add user message to context
|
|
712
|
-
if (message?.type ===
|
|
722
|
+
if (message?.type === 'text') {
|
|
713
723
|
state.context.push({
|
|
714
|
-
role:
|
|
715
|
-
content: message.payload.text
|
|
716
|
-
})
|
|
724
|
+
role: 'user',
|
|
725
|
+
content: message.payload.text,
|
|
726
|
+
})
|
|
717
727
|
}
|
|
718
728
|
|
|
719
729
|
// Keep last 10 messages
|
|
720
730
|
if (state.context.length > 10) {
|
|
721
|
-
state.context = state.context.slice(-10)
|
|
731
|
+
state.context = state.context.slice(-10)
|
|
722
732
|
}
|
|
723
733
|
|
|
724
734
|
// Use context in AI execution
|
|
725
735
|
await execute({
|
|
726
736
|
instructions: `You are a helpful assistant.
|
|
727
737
|
Previous context:
|
|
728
|
-
${state.context.map(m => `${m.role}: ${m.content}`).join(
|
|
729
|
-
})
|
|
730
|
-
}
|
|
731
|
-
})
|
|
738
|
+
${state.context.map((m) => `${m.role}: ${m.content}`).join('\n')}`,
|
|
739
|
+
})
|
|
740
|
+
},
|
|
741
|
+
})
|
|
732
742
|
```
|
|
733
743
|
|
|
734
744
|
### Context API & Chat Instance
|
|
@@ -738,27 +748,25 @@ Access the chat instance to read conversation history, register custom component
|
|
|
738
748
|
#### Accessing the Chat Instance
|
|
739
749
|
|
|
740
750
|
```typescript
|
|
741
|
-
import { context, Conversation } from
|
|
751
|
+
import { context, Conversation } from '@botpress/runtime'
|
|
742
752
|
|
|
743
753
|
export const Chat = new Conversation({
|
|
744
|
-
channel:
|
|
754
|
+
channel: 'webchat.channel',
|
|
745
755
|
|
|
746
756
|
async handler({ message, execute, conversation }) {
|
|
747
757
|
// Get the chat instance
|
|
748
|
-
const chat = context.get(
|
|
758
|
+
const chat = context.get('chat')
|
|
749
759
|
|
|
750
760
|
// Fetch conversation transcript
|
|
751
|
-
const transcript = await chat.fetchTranscript()
|
|
761
|
+
const transcript = await chat.fetchTranscript()
|
|
752
762
|
|
|
753
763
|
// Analyze conversation history
|
|
754
|
-
const messageCount = transcript.length
|
|
755
|
-
const hasImages = transcript.some(
|
|
756
|
-
(msg) => msg.role === "user" && msg.attachments && msg.attachments.length > 0
|
|
757
|
-
);
|
|
764
|
+
const messageCount = transcript.length
|
|
765
|
+
const hasImages = transcript.some((msg) => msg.role === 'user' && msg.attachments && msg.attachments.length > 0)
|
|
758
766
|
|
|
759
|
-
console.log(`Conversation has ${messageCount} messages, includes images: ${hasImages}`)
|
|
760
|
-
}
|
|
761
|
-
})
|
|
767
|
+
console.log(`Conversation has ${messageCount} messages, includes images: ${hasImages}`)
|
|
768
|
+
},
|
|
769
|
+
})
|
|
762
770
|
```
|
|
763
771
|
|
|
764
772
|
#### Adaptive Model Selection
|
|
@@ -766,35 +774,34 @@ export const Chat = new Conversation({
|
|
|
766
774
|
Choose models dynamically based on conversation content (e.g., handle images with vision models):
|
|
767
775
|
|
|
768
776
|
```typescript
|
|
769
|
-
import { context, Conversation } from
|
|
777
|
+
import { context, Conversation } from '@botpress/runtime'
|
|
770
778
|
|
|
771
779
|
export const Chat = new Conversation({
|
|
772
|
-
channel:
|
|
780
|
+
channel: 'webchat.channel',
|
|
773
781
|
|
|
774
782
|
async handler({ message, execute, conversation }) {
|
|
775
|
-
const chat = context.get(
|
|
776
|
-
const transcript = await chat.fetchTranscript()
|
|
783
|
+
const chat = context.get('chat')
|
|
784
|
+
const transcript = await chat.fetchTranscript()
|
|
777
785
|
|
|
778
786
|
// Check if any user messages contain images
|
|
779
|
-
const hasImages = transcript.some(
|
|
780
|
-
(msg) => msg.role === "user" && msg.attachments?.length > 0
|
|
781
|
-
);
|
|
787
|
+
const hasImages = transcript.some((msg) => msg.role === 'user' && msg.attachments?.length > 0)
|
|
782
788
|
|
|
783
789
|
// Select appropriate model
|
|
784
790
|
const model = hasImages
|
|
785
|
-
?
|
|
786
|
-
:
|
|
791
|
+
? 'openai:gpt-4.1' // Vision-capable model for images
|
|
792
|
+
: 'cerebras:gpt-oss-120b' // Fast, cost-effective for text-only
|
|
787
793
|
|
|
788
794
|
await execute({
|
|
789
|
-
instructions:
|
|
795
|
+
instructions: 'You are a helpful assistant...',
|
|
790
796
|
model,
|
|
791
|
-
tools: [myTool]
|
|
792
|
-
})
|
|
793
|
-
}
|
|
794
|
-
})
|
|
797
|
+
tools: [myTool],
|
|
798
|
+
})
|
|
799
|
+
},
|
|
800
|
+
})
|
|
795
801
|
```
|
|
796
802
|
|
|
797
803
|
**Use Cases:**
|
|
804
|
+
|
|
798
805
|
- **Cost Optimization**: Use cheaper/faster models for text-only conversations
|
|
799
806
|
- **Capability Matching**: Switch to vision models only when images are present
|
|
800
807
|
- **Context-Aware Responses**: Adapt behavior based on conversation history
|
|
@@ -805,41 +812,42 @@ export const Chat = new Conversation({
|
|
|
805
812
|
Custom components are React `.bp.tsx` files that render rich UI in webchat. Register them in `src/components/index.ts` with `CustomComponent`, then list them in a conversation's `components` array:
|
|
806
813
|
|
|
807
814
|
```typescript
|
|
808
|
-
import { Conversation } from
|
|
809
|
-
import { TicketCardComponent } from
|
|
815
|
+
import { Conversation } from '@botpress/runtime'
|
|
816
|
+
import { TicketCardComponent } from '../components'
|
|
810
817
|
|
|
811
818
|
export const Chat = new Conversation({
|
|
812
|
-
channel:
|
|
819
|
+
channel: 'webchat.channel',
|
|
813
820
|
|
|
814
821
|
components: [TicketCardComponent],
|
|
815
822
|
|
|
816
823
|
async handler({ execute, conversation }) {
|
|
817
824
|
// The LLM can yield TicketCard autonomously
|
|
818
825
|
await execute({
|
|
819
|
-
instructions:
|
|
820
|
-
tools: [lookupTicket]
|
|
821
|
-
})
|
|
822
|
-
}
|
|
823
|
-
})
|
|
826
|
+
instructions: 'Always use the TicketCard component to display ticket details.',
|
|
827
|
+
tools: [lookupTicket],
|
|
828
|
+
})
|
|
829
|
+
},
|
|
830
|
+
})
|
|
824
831
|
```
|
|
825
832
|
|
|
826
833
|
You can also send a component explicitly:
|
|
827
834
|
|
|
828
835
|
```typescript
|
|
829
|
-
import { WelcomeBannerComponent } from
|
|
836
|
+
import { WelcomeBannerComponent } from '../components'
|
|
830
837
|
|
|
831
838
|
await conversation.send({
|
|
832
|
-
type:
|
|
839
|
+
type: 'customComponent',
|
|
833
840
|
payload: {
|
|
834
841
|
component: WelcomeBannerComponent,
|
|
835
842
|
props: {},
|
|
836
|
-
}
|
|
837
|
-
})
|
|
843
|
+
},
|
|
844
|
+
})
|
|
838
845
|
```
|
|
839
846
|
|
|
840
847
|
See the component registry reference for the full component creation flow (`.bp.tsx` files, metadata, and registration).
|
|
841
848
|
|
|
842
849
|
**Component Use Cases:**
|
|
850
|
+
|
|
843
851
|
- **Rich Answers**: Formatted responses with citations and styling
|
|
844
852
|
- **Interactive Elements**: Buttons, forms, and action cards
|
|
845
853
|
- **Data Visualization**: Charts, tables, and structured data display
|
|
@@ -852,137 +860,134 @@ The chat instance provides methods for managing conversation transcripts and com
|
|
|
852
860
|
**Transcript Management:**
|
|
853
861
|
|
|
854
862
|
```typescript
|
|
855
|
-
import { context } from
|
|
863
|
+
import { context } from '@botpress/runtime'
|
|
856
864
|
|
|
857
|
-
const chat = context.get(
|
|
865
|
+
const chat = context.get('chat')
|
|
858
866
|
|
|
859
867
|
// Fetch transcript (loads from API)
|
|
860
|
-
const transcript = await chat.fetchTranscript()
|
|
868
|
+
const transcript = await chat.fetchTranscript()
|
|
861
869
|
|
|
862
870
|
// Get transcript (returns cached copy)
|
|
863
|
-
const cached = await chat.getTranscript()
|
|
871
|
+
const cached = await chat.getTranscript()
|
|
864
872
|
|
|
865
873
|
// Set/replace entire transcript
|
|
866
874
|
await chat.setTranscript([
|
|
867
|
-
{ id:
|
|
868
|
-
{ id:
|
|
869
|
-
])
|
|
875
|
+
{ id: '1', role: 'user', content: 'Hello' },
|
|
876
|
+
{ id: '2', role: 'assistant', content: 'Hi there!' },
|
|
877
|
+
])
|
|
870
878
|
|
|
871
879
|
// Clear all messages
|
|
872
|
-
await chat.clearTranscript()
|
|
880
|
+
await chat.clearTranscript()
|
|
873
881
|
|
|
874
882
|
// Prepend messages to beginning
|
|
875
|
-
await chat.prependToTranscript([
|
|
876
|
-
{ id: "0", role: "summary", content: "Previous conversation summary..." }
|
|
877
|
-
]);
|
|
883
|
+
await chat.prependToTranscript([{ id: '0', role: 'summary', content: 'Previous conversation summary...' }])
|
|
878
884
|
|
|
879
885
|
// Remove specific message by ID
|
|
880
|
-
const removed = await chat.removeMessage(
|
|
886
|
+
const removed = await chat.removeMessage('msg-123')
|
|
881
887
|
|
|
882
888
|
// Remove messages by predicate
|
|
883
|
-
const count = await chat.removeMessages(
|
|
884
|
-
(item) => item.role === "assistant" && item.content.includes("error")
|
|
885
|
-
);
|
|
889
|
+
const count = await chat.removeMessages((item) => item.role === 'assistant' && item.content.includes('error'))
|
|
886
890
|
|
|
887
891
|
// Compact transcript (summarize/compress)
|
|
888
|
-
await chat.compactTranscript()
|
|
892
|
+
await chat.compactTranscript()
|
|
889
893
|
|
|
890
894
|
// Save transcript changes
|
|
891
|
-
await chat.saveTranscript()
|
|
895
|
+
await chat.saveTranscript()
|
|
892
896
|
```
|
|
893
897
|
|
|
894
898
|
**Component Management:**
|
|
895
899
|
|
|
896
900
|
```typescript
|
|
897
|
-
const chat = context.get(
|
|
901
|
+
const chat = context.get('chat')
|
|
898
902
|
|
|
899
903
|
// Register component with handler
|
|
900
904
|
chat.registerComponent({
|
|
901
905
|
component: myComponent,
|
|
902
906
|
handler: async (props) => {
|
|
903
907
|
// Handle component rendering
|
|
904
|
-
}
|
|
905
|
-
})
|
|
908
|
+
},
|
|
909
|
+
})
|
|
906
910
|
|
|
907
911
|
// Remove component by name
|
|
908
|
-
chat.removeComponent(
|
|
912
|
+
chat.removeComponent('Answer')
|
|
909
913
|
|
|
910
914
|
// Get all registered components
|
|
911
|
-
const components = await chat.getComponents()
|
|
915
|
+
const components = await chat.getComponents()
|
|
912
916
|
```
|
|
913
917
|
|
|
914
918
|
**Transcript Item Types:**
|
|
915
919
|
|
|
916
920
|
```typescript
|
|
917
921
|
type TranscriptUserMessage = {
|
|
918
|
-
id: string
|
|
919
|
-
role:
|
|
920
|
-
content: string
|
|
921
|
-
name?: string
|
|
922
|
-
createdAt?: string
|
|
923
|
-
attachments?: Array<{ type:
|
|
924
|
-
}
|
|
922
|
+
id: string
|
|
923
|
+
role: 'user'
|
|
924
|
+
content: string
|
|
925
|
+
name?: string
|
|
926
|
+
createdAt?: string
|
|
927
|
+
attachments?: Array<{ type: 'image'; url: string }>
|
|
928
|
+
}
|
|
925
929
|
|
|
926
930
|
type TranscriptAssistantMessage = {
|
|
927
|
-
id: string
|
|
928
|
-
role:
|
|
929
|
-
content: string
|
|
930
|
-
name?: string
|
|
931
|
-
createdAt?: string
|
|
932
|
-
}
|
|
931
|
+
id: string
|
|
932
|
+
role: 'assistant'
|
|
933
|
+
content: string
|
|
934
|
+
name?: string
|
|
935
|
+
createdAt?: string
|
|
936
|
+
}
|
|
933
937
|
|
|
934
938
|
type TranscriptEventMessage = {
|
|
935
|
-
id: string
|
|
936
|
-
role:
|
|
937
|
-
name: string
|
|
938
|
-
payload: unknown
|
|
939
|
-
createdAt?: string
|
|
940
|
-
}
|
|
939
|
+
id: string
|
|
940
|
+
role: 'event'
|
|
941
|
+
name: string
|
|
942
|
+
payload: unknown
|
|
943
|
+
createdAt?: string
|
|
944
|
+
}
|
|
941
945
|
|
|
942
946
|
type TranscriptSummaryMessage = {
|
|
943
|
-
id: string
|
|
944
|
-
role:
|
|
945
|
-
content: string
|
|
946
|
-
}
|
|
947
|
+
id: string
|
|
948
|
+
role: 'summary'
|
|
949
|
+
content: string
|
|
950
|
+
}
|
|
947
951
|
```
|
|
948
952
|
|
|
949
953
|
#### Transcript Analysis Patterns
|
|
950
954
|
|
|
951
955
|
```typescript
|
|
952
|
-
import { context, Conversation } from
|
|
956
|
+
import { context, Conversation } from '@botpress/runtime'
|
|
953
957
|
|
|
954
958
|
export const Chat = new Conversation({
|
|
955
|
-
channel:
|
|
959
|
+
channel: 'chat.channel',
|
|
956
960
|
|
|
957
961
|
async handler({ message, execute }) {
|
|
958
|
-
const chat = context.get(
|
|
959
|
-
const transcript = await chat.fetchTranscript()
|
|
962
|
+
const chat = context.get('chat')
|
|
963
|
+
const transcript = await chat.fetchTranscript()
|
|
960
964
|
|
|
961
965
|
// Detect conversation patterns
|
|
962
|
-
const userMessages = transcript.filter(msg => msg.role ===
|
|
963
|
-
const hasRepeatedQuestion = userMessages.some(
|
|
964
|
-
idx > 0 && msg.content === userMessages[idx - 1].content
|
|
965
|
-
)
|
|
966
|
+
const userMessages = transcript.filter((msg) => msg.role === 'user')
|
|
967
|
+
const hasRepeatedQuestion = userMessages.some(
|
|
968
|
+
(msg, idx) => idx > 0 && msg.content === userMessages[idx - 1].content
|
|
969
|
+
)
|
|
966
970
|
|
|
967
971
|
if (hasRepeatedQuestion) {
|
|
968
972
|
// User asked the same question twice - may need clarification
|
|
969
973
|
await execute({
|
|
970
974
|
instructions: `The user seems frustrated or confused.
|
|
971
975
|
Provide extra clarification and ask if they need more help.`,
|
|
972
|
-
tools: [searchTool]
|
|
973
|
-
})
|
|
976
|
+
tools: [searchTool],
|
|
977
|
+
})
|
|
974
978
|
} else {
|
|
975
979
|
// Normal conversation flow
|
|
976
980
|
await execute({
|
|
977
|
-
instructions:
|
|
978
|
-
tools: [searchTool]
|
|
979
|
-
})
|
|
981
|
+
instructions: 'You are a helpful assistant',
|
|
982
|
+
tools: [searchTool],
|
|
983
|
+
})
|
|
980
984
|
}
|
|
981
|
-
}
|
|
982
|
-
})
|
|
985
|
+
},
|
|
986
|
+
})
|
|
983
987
|
```
|
|
984
988
|
|
|
985
989
|
**Analysis Patterns:**
|
|
990
|
+
|
|
986
991
|
- **Detect frustration**: Repeated questions, negative sentiment
|
|
987
992
|
- **Identify complexity**: Long conversations may need escalation
|
|
988
993
|
- **Track engagement**: Message frequency and length analysis
|
|
@@ -992,38 +997,39 @@ export const Chat = new Conversation({
|
|
|
992
997
|
|
|
993
998
|
```typescript
|
|
994
999
|
export const Chat = new Conversation({
|
|
995
|
-
channel:
|
|
1000
|
+
channel: 'chat.channel',
|
|
996
1001
|
|
|
997
1002
|
async handler({ message, conversation, execute }) {
|
|
998
1003
|
try {
|
|
999
1004
|
await execute({
|
|
1000
|
-
instructions:
|
|
1001
|
-
tools: [riskyTool]
|
|
1002
|
-
})
|
|
1005
|
+
instructions: 'You are a helpful assistant',
|
|
1006
|
+
tools: [riskyTool],
|
|
1007
|
+
})
|
|
1003
1008
|
} catch (error) {
|
|
1004
|
-
console.error(
|
|
1009
|
+
console.error('Execution failed:', error)
|
|
1005
1010
|
|
|
1006
1011
|
// Send error message to user
|
|
1007
1012
|
await conversation.send({
|
|
1008
|
-
type:
|
|
1013
|
+
type: 'text',
|
|
1009
1014
|
payload: {
|
|
1010
|
-
text:
|
|
1011
|
-
}
|
|
1012
|
-
})
|
|
1015
|
+
text: 'I apologize, but I encountered an error. Please try again later.',
|
|
1016
|
+
},
|
|
1017
|
+
})
|
|
1013
1018
|
|
|
1014
1019
|
// Optionally notify admins
|
|
1015
1020
|
await notifyAdmins({
|
|
1016
1021
|
error: error.message,
|
|
1017
|
-
conversationId: conversation.id
|
|
1018
|
-
})
|
|
1022
|
+
conversationId: conversation.id,
|
|
1023
|
+
})
|
|
1019
1024
|
}
|
|
1020
|
-
}
|
|
1021
|
-
})
|
|
1025
|
+
},
|
|
1026
|
+
})
|
|
1022
1027
|
```
|
|
1023
1028
|
|
|
1024
1029
|
## Best Practices
|
|
1025
1030
|
|
|
1026
1031
|
### 1. Handle All Message Types
|
|
1032
|
+
|
|
1027
1033
|
```typescript
|
|
1028
1034
|
async handler({ message, conversation }) {
|
|
1029
1035
|
switch (message?.type) {
|
|
@@ -1043,20 +1049,22 @@ async handler({ message, conversation }) {
|
|
|
1043
1049
|
```
|
|
1044
1050
|
|
|
1045
1051
|
### 2. Validate Input
|
|
1052
|
+
|
|
1046
1053
|
```typescript
|
|
1047
|
-
if (message?.type ===
|
|
1048
|
-
const text = message.payload.text?.trim()
|
|
1054
|
+
if (message?.type === 'text') {
|
|
1055
|
+
const text = message.payload.text?.trim()
|
|
1049
1056
|
if (!text || text.length > 1000) {
|
|
1050
1057
|
await conversation.send({
|
|
1051
|
-
type:
|
|
1052
|
-
payload: { text:
|
|
1053
|
-
})
|
|
1054
|
-
return
|
|
1058
|
+
type: 'text',
|
|
1059
|
+
payload: { text: 'Please provide a valid message (1-1000 characters)' },
|
|
1060
|
+
})
|
|
1061
|
+
return
|
|
1055
1062
|
}
|
|
1056
1063
|
}
|
|
1057
1064
|
```
|
|
1058
1065
|
|
|
1059
1066
|
### 3. Use Early Returns
|
|
1067
|
+
|
|
1060
1068
|
```typescript
|
|
1061
1069
|
async handler({ message, state, conversation, execute }) {
|
|
1062
1070
|
// Handle commands first
|
|
@@ -1077,6 +1085,7 @@ async handler({ message, state, conversation, execute }) {
|
|
|
1077
1085
|
```
|
|
1078
1086
|
|
|
1079
1087
|
### 4. Provide Clear Instructions
|
|
1088
|
+
|
|
1080
1089
|
```typescript
|
|
1081
1090
|
await execute({
|
|
1082
1091
|
instructions: `You are a customer support assistant for ACME Corp.
|
|
@@ -1088,11 +1097,12 @@ Guidelines:
|
|
|
1088
1097
|
- Escalate to human agent if customer seems frustrated
|
|
1089
1098
|
|
|
1090
1099
|
Current user: ${user.name}
|
|
1091
|
-
Account type: ${user.accountType}
|
|
1092
|
-
})
|
|
1100
|
+
Account type: ${user.accountType}`,
|
|
1101
|
+
})
|
|
1093
1102
|
```
|
|
1094
1103
|
|
|
1095
1104
|
### 5. Clean Up State
|
|
1105
|
+
|
|
1096
1106
|
```typescript
|
|
1097
1107
|
// Reset state when conversation ends
|
|
1098
1108
|
async handler({ message, state, conversation }) {
|
|
@@ -1108,4 +1118,4 @@ async handler({ message, state, conversation }) {
|
|
|
1108
1118
|
});
|
|
1109
1119
|
}
|
|
1110
1120
|
}
|
|
1111
|
-
```
|
|
1121
|
+
```
|