@trigger.dev/sdk 4.7.3 → 4.7.5
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/commonjs/v3/ai.d.ts +26 -0
- package/dist/commonjs/v3/ai.js +7 -5
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/concurrencyLimits.d.ts +37 -7
- package/dist/commonjs/v3/concurrencyLimits.js +38 -70
- package/dist/commonjs/v3/concurrencyLimits.js.map +1 -1
- package/dist/commonjs/v3/index.d.ts +1 -1
- package/dist/commonjs/v3/index.js.map +1 -1
- package/dist/commonjs/v3/query.d.ts +434 -1
- package/dist/commonjs/v3/query.js +115 -0
- package/dist/commonjs/v3/query.js.map +1 -1
- package/dist/commonjs/v3/runs.d.ts +22 -1
- package/dist/commonjs/v3/runs.js +6 -3
- package/dist/commonjs/v3/runs.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +26 -0
- package/dist/esm/v3/ai.js +7 -5
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/concurrencyLimits.d.ts +37 -7
- package/dist/esm/v3/concurrencyLimits.js +38 -70
- package/dist/esm/v3/concurrencyLimits.js.map +1 -1
- package/dist/esm/v3/index.d.ts +1 -1
- package/dist/esm/v3/index.js.map +1 -1
- package/dist/esm/v3/query.d.ts +434 -1
- package/dist/esm/v3/query.js +115 -0
- package/dist/esm/v3/query.js.map +1 -1
- package/dist/esm/v3/runs.d.ts +22 -1
- package/dist/esm/v3/runs.js +6 -3
- package/dist/esm/v3/runs.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/docs/ai-chat/actions.mdx +6 -1
- package/docs/ai-chat/backend.mdx +47 -0
- package/docs/ai-chat/patterns/redacting-telemetry.mdx +117 -0
- package/docs/ai-chat/patterns/sub-agents.mdx +33 -0
- package/docs/ai-chat/pending-messages.mdx +2 -0
- package/docs/ai-chat/reference.mdx +53 -2
- package/docs/ai-chat/transcript-storage.mdx +1 -1
- package/docs/apikeys.mdx +9 -7
- package/docs/billing-limits.mdx +1 -1
- package/docs/cli-env-commands.mdx +5 -4
- package/docs/cli-init-commands.mdx +27 -1
- package/docs/cli-login-commands.mdx +20 -0
- package/docs/cli-projects-commands.mdx +13 -1
- package/docs/cli-runs-commands.mdx +3 -2
- package/docs/cli-tasks-commands.mdx +95 -0
- package/docs/cli-update-commands.mdx +9 -3
- package/docs/cli-whoami-commands.mdx +12 -0
- package/docs/concurrency.mdx +32 -0
- package/docs/config/config-file.mdx +58 -0
- package/docs/config/extensions/audioWaveform.mdx +4 -1
- package/docs/config/extensions/overview.mdx +2 -0
- package/docs/config/extensions/prismaExtension.mdx +6 -7
- package/docs/config/extensions/puppeteer.mdx +3 -5
- package/docs/config/extensions/pythonExtension.mdx +9 -1
- package/docs/config/extensions/syncEnvVars.mdx +6 -0
- package/docs/deploy-environment-variables.mdx +5 -1
- package/docs/deployment/atomic-deployment.mdx +5 -3
- package/docs/deployment/dev-branches.mdx +5 -5
- package/docs/deployment/overview.mdx +9 -8
- package/docs/deployment/preview-branches.mdx +7 -4
- package/docs/errors-retrying.mdx +4 -4
- package/docs/github-actions.mdx +3 -3
- package/docs/how-it-works.mdx +5 -5
- package/docs/introduction.mdx +6 -0
- package/docs/limits.mdx +32 -8
- package/docs/logging.mdx +1 -1
- package/docs/machines.mdx +1 -1
- package/docs/manual-setup.mdx +1 -1
- package/docs/mcp-introduction.mdx +8 -7
- package/docs/mcp-tools.mdx +113 -3
- package/docs/migrating-from-v3.mdx +1 -1
- package/docs/observability/dashboards.mdx +64 -0
- package/docs/observability/query.mdx +6 -2
- package/docs/private-networking/aws-console-setup.mdx +1 -1
- package/docs/private-networking/overview.mdx +1 -1
- package/docs/quick-start.mdx +3 -40
- package/docs/realtime/auth.mdx +1 -1
- package/docs/realtime/backend/input-streams.mdx +159 -0
- package/docs/realtime/backend/streams.mdx +25 -10
- package/docs/realtime/how-it-works.mdx +1 -1
- package/docs/realtime/react-hooks/overview.mdx +1 -1
- package/docs/realtime/react-hooks/streams.mdx +2 -2
- package/docs/realtime/react-hooks/subscribe.mdx +19 -3
- package/docs/realtime/react-hooks/triggering.mdx +2 -2
- package/docs/realtime/react-hooks/use-wait-token.mdx +1 -1
- package/docs/realtime/run-object.mdx +47 -24
- package/docs/replaying.mdx +26 -1
- package/docs/reports.mdx +4 -2
- package/docs/run-tests.mdx +2 -2
- package/docs/run-usage.mdx +3 -3
- package/docs/runs/heartbeats.mdx +2 -2
- package/docs/runs/list-best-practices.mdx +234 -0
- package/docs/runs/max-duration.mdx +4 -0
- package/docs/runs/metadata.mdx +7 -10
- package/docs/runs.mdx +18 -8
- package/docs/self-hosting/env/supervisor.mdx +3 -0
- package/docs/self-hosting/env/webapp.mdx +5 -0
- package/docs/self-hosting/overview.mdx +26 -0
- package/docs/tags.mdx +6 -0
- package/docs/tasks/scheduled.mdx +11 -5
- package/docs/triggering.mdx +8 -0
- package/docs/troubleshooting-alerts.mdx +102 -3
- package/docs/troubleshooting.mdx +1 -1
- package/docs/upgrading-packages.mdx +3 -3
- package/docs/vercel-integration.mdx +13 -3
- package/docs/versioning.mdx +1 -1
- package/docs/wait-for-token.mdx +3 -3
- package/docs/webhooks/channels.mdx +158 -0
- package/docs/webhooks/connect.mdx +84 -0
- package/docs/webhooks/deliveries.mdx +103 -0
- package/docs/webhooks/filters.mdx +144 -0
- package/docs/webhooks/human-in-the-loop.mdx +162 -0
- package/docs/webhooks/linear.mdx +190 -0
- package/docs/webhooks/overview.mdx +119 -0
- package/docs/webhooks/session-routing.mdx +145 -0
- package/docs/webhooks/slack.mdx +397 -0
- package/docs/webhooks/sources.mdx +221 -0
- package/docs/webhooks/waiters.mdx +194 -0
- package/package.json +3 -2
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Routing to a session"
|
|
3
|
+
description: "Route verified webhook deliveries to a durable per-key session instead of a fresh run."
|
|
4
|
+
sidebarTitle: "Session routing"
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
import WebhooksBetaBanner from "/snippets/webhooks-beta-banner.mdx";
|
|
8
|
+
|
|
9
|
+
<WebhooksBetaBanner />
|
|
10
|
+
|
|
11
|
+
A plain [`webhook()`](/webhooks/overview) runs a fresh, stateless run for every delivery. Sometimes you want the opposite: deliveries that share a key (a customer, an installation, an issue) should land on **one durable [session](/ai-chat/sessions)** and be handled in order, with state carried across them. That is what session routing does.
|
|
12
|
+
|
|
13
|
+
Instead of a handler, you declare a `chat.event` on an [endpoint](/webhooks/overview) and list it on an agent. The verified delivery is routed to a find-or-created session and arrives as an [action](/ai-chat/actions).
|
|
14
|
+
|
|
15
|
+
## Declaring a chat event
|
|
16
|
+
|
|
17
|
+
`chat.event` describes the routing only: the endpoint it subscribes to, a `key` that identifies the session, and a `type` label for the delivered action. It has no handler.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { webhooks } from "@trigger.dev/sdk";
|
|
21
|
+
import { chat } from "@trigger.dev/sdk/ai";
|
|
22
|
+
|
|
23
|
+
export const stripe = webhooks.endpoint.define({ id: "stripe", source: webhooks.stripe() });
|
|
24
|
+
|
|
25
|
+
export const orderEvents = chat.event({
|
|
26
|
+
id: "order-events",
|
|
27
|
+
endpoint: stripe,
|
|
28
|
+
// one session per customer
|
|
29
|
+
key: "{event.data.object.customer}",
|
|
30
|
+
// invoices always carry a customer; events without one would have no session to go to
|
|
31
|
+
filter: "event.type startsWith 'invoice.'",
|
|
32
|
+
type: "order.event",
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The endpoint's other subscribers, such as a `webhook()` task, still get the same deliveries.
|
|
37
|
+
|
|
38
|
+
### The key
|
|
39
|
+
|
|
40
|
+
The `key` is what makes routing durable: two deliveries that resolve to the same key reach the same session; a different key gets its own. It is a template string, evaluated server-side at ingest, whose `{...}` placeholders read three namespaces:
|
|
41
|
+
|
|
42
|
+
- `{event.*}`: the parsed event body.
|
|
43
|
+
- `{webhook.*}`: endpoint metadata (`externalRef`, `tenantId`, `id`, `source`, `deliveryId`).
|
|
44
|
+
- `{header.name}`: an inbound header.
|
|
45
|
+
|
|
46
|
+
These are the same namespaces [filter](/webhooks/filters) expressions use. Every placeholder names one: a bare `{customer}` is an error.
|
|
47
|
+
|
|
48
|
+
`{a || b}` falls back to `b` when `a` is missing or empty. Compose several placeholders into one key: `"{webhook.externalRef}-{event.issue.id}"`. Each placeholder is checked against the event type at build time, so a bad field is a red squiggle on the `key` line, not a runtime surprise.
|
|
49
|
+
|
|
50
|
+
### The `type` label
|
|
51
|
+
|
|
52
|
+
`type` is a name you choose for the delivered action. It flows straight through to `action.type` on the envelope, so your handler can tell webhook actions apart from each other and from browser actions. It is optional and defaults to the descriptor `id`.
|
|
53
|
+
|
|
54
|
+
## Handling deliveries on an agent
|
|
55
|
+
|
|
56
|
+
List the descriptor on a [`chat.agent`](/ai-chat/overview) (or a session agent). The delivery arrives at `onAction`, not as a chat message:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { chat } from "@trigger.dev/sdk/ai";
|
|
60
|
+
import { orderEvents } from "./order-events";
|
|
61
|
+
|
|
62
|
+
export const orderAgent = chat.agent({
|
|
63
|
+
id: "order-agent",
|
|
64
|
+
events: [orderEvents],
|
|
65
|
+
onAction: async ({ action }) => {
|
|
66
|
+
switch (action.type) {
|
|
67
|
+
case "order.event":
|
|
68
|
+
// action.event is the verified Stripe event, fully typed
|
|
69
|
+
console.log("order for", action.event.data.object.customer);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
run: async ({ messages, signal }) => {
|
|
74
|
+
/* ... */
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`action.type` is a closed union of the `type` labels of the events you listed, and each arm narrows `action.event` to that event's payload type. The full envelope is `{ type, event, source, headers, deliveryId, externalDeliveryId, endpoint }`. `deliveryId` is the delivery's `whd_` id, as in the dashboard and the [deliveries API](/webhooks/deliveries): a [replay](/webhooks/deliveries#replay) gets a new one. `externalDeliveryId` is the provider's id for the delivery (Stripe's `evt_` id, for example), the same across replays. `endpoint` is the endpoint the delivery arrived on.
|
|
80
|
+
|
|
81
|
+
Because it is an [action](/ai-chat/actions), the handler is not a turn by default: it can read the event, mutate session state with `chat.history`, and return nothing. To have the agent answer, return `chat.turn()`: the edit is saved, then a full turn runs on the session's history exactly as a message turn does. Webhook actions bypass your `actionSchema` (their shape is fixed and already verified by the source), so `action.type` and `action.event` are typed from the descriptor, not the schema.
|
|
82
|
+
|
|
83
|
+
## Answering into a channel
|
|
84
|
+
|
|
85
|
+
When the same session is also a [channel](/webhooks/channels) conversation (a Slack thread keyed to the same customer, say), a turn requested from a webhook action posts its answer into that thread. The first channel message **binds** the session to its connector; every later turn on the session, whatever started it, egresses through the binding.
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
export const orderAgent = chat.agent({
|
|
89
|
+
id: "order-agent",
|
|
90
|
+
channels: [
|
|
91
|
+
slack({
|
|
92
|
+
id: "support-slack",
|
|
93
|
+
endpoint: slackEvents,
|
|
94
|
+
token: process.env.SLACK_BOT_TOKEN!,
|
|
95
|
+
startOn: appMentions(),
|
|
96
|
+
}),
|
|
97
|
+
],
|
|
98
|
+
events: [orderEvents],
|
|
99
|
+
onAction: async ({ action }) => {
|
|
100
|
+
if (action.type === "order.event") {
|
|
101
|
+
// A user-role note, so the model lane ends on a user message. A system-role
|
|
102
|
+
// injection becomes instructions and leaves the lane ending on the assistant's
|
|
103
|
+
// last reply, which most models treat as a prefill and answer with nothing.
|
|
104
|
+
chat.inject([
|
|
105
|
+
{
|
|
106
|
+
role: "user",
|
|
107
|
+
content: `[payments] Order ${action.event.data.object.id} was paid. Tell the thread in one sentence.`,
|
|
108
|
+
},
|
|
109
|
+
]);
|
|
110
|
+
return chat.turn(); // the agent tells the thread about the payment
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
run: async ({ messages, streamText }) =>
|
|
114
|
+
streamText({ model: anthropic("claude-sonnet-4-5"), messages }),
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
A bound turn posts the final answer as a fresh message in the thread (no placeholder, since there is no triggering message to react to). If the session has never had a channel message, there is nothing to post into and the turn's answer stays on the session's stream only.
|
|
119
|
+
|
|
120
|
+
<Note>
|
|
121
|
+
The descriptor never names the agent, and the agent references the descriptor, so there is no
|
|
122
|
+
circular dependency. Whichever agent lists it becomes the routing target.
|
|
123
|
+
</Note>
|
|
124
|
+
|
|
125
|
+
## Several agents on one endpoint
|
|
126
|
+
|
|
127
|
+
Listing a descriptor on an agent subscribes that agent to the descriptor's endpoint, with the subscriber id `agentId:eventId`. If two agents list the same descriptor, the endpoint has two subscribers and every delivery goes to both, on the one URL the provider already knows.
|
|
128
|
+
|
|
129
|
+
A session belongs to the agent that created it. When two agents compute the same key, the second agent's delivery to that session fails with an error naming the owner. Both agents see the same endpoint, so a `{webhook.*}` placeholder resolves the same for each and can't separate them. Give each agent its own descriptor with a constant prefix in the key instead, for example `"billing-{event.data.object.customer}"` and `"support-{event.data.object.customer}"`.
|
|
130
|
+
|
|
131
|
+
An exported `chat.event` that no agent lists routes nothing. `trigger dev` warns about it (it is not an error, since declaring the descriptor before wiring the agent is a normal step), so wire it onto an agent once you are ready.
|
|
132
|
+
|
|
133
|
+
## Filtering
|
|
134
|
+
|
|
135
|
+
A [`filter`](/webhooks/filters) works here exactly as on a `webhook()` task: a delivery that doesn't match never reaches the session, and the delivery records the reason.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
export const orderEvents = chat.event({
|
|
139
|
+
id: "order-events",
|
|
140
|
+
endpoint: stripe,
|
|
141
|
+
key: "{event.data.object.customer}",
|
|
142
|
+
type: "order.event",
|
|
143
|
+
filter: "event.type == 'payment_intent.succeeded'",
|
|
144
|
+
});
|
|
145
|
+
```
|
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Slack agents"
|
|
3
|
+
description: "Turn a chat.agent into a Slack agent: users message it in Slack, it shows its status while it works, streams its reply with its tool calls as a plan, asks for approvals, and stops on Slack's stop button."
|
|
4
|
+
sidebarTitle: "Slack agents"
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
import WebhooksBetaBanner from "/snippets/webhooks-beta-banner.mdx";
|
|
8
|
+
|
|
9
|
+
<WebhooksBetaBanner />
|
|
10
|
+
|
|
11
|
+
**`slack()` from `@trigger.dev/slack` turns a `chat.agent` into a native Slack agent.** Each Slack agent session (a conversation with the agent, or a thread in a channel) becomes a durable Trigger.dev [session](/ai-chat/sessions). A message runs as a turn, the session shows the agent working, the reply streams into the thread as the model writes it, and Slack's stop button stops the turn.
|
|
12
|
+
|
|
13
|
+
```ts /trigger/support.ts
|
|
14
|
+
import { webhooks } from "@trigger.dev/sdk";
|
|
15
|
+
import { chat } from "@trigger.dev/sdk/ai";
|
|
16
|
+
import { appMentions, slack, webhookSource } from "@trigger.dev/slack";
|
|
17
|
+
import { anthropic } from "@ai-sdk/anthropic";
|
|
18
|
+
|
|
19
|
+
const slackEvents = webhooks.endpoint.define({ id: "slack", source: webhookSource() });
|
|
20
|
+
|
|
21
|
+
export const support = chat.agent({
|
|
22
|
+
id: "support",
|
|
23
|
+
channels: [
|
|
24
|
+
slack({
|
|
25
|
+
id: "slack",
|
|
26
|
+
endpoint: slackEvents,
|
|
27
|
+
token: process.env.SLACK_BOT_TOKEN!,
|
|
28
|
+
startOn: appMentions(),
|
|
29
|
+
}),
|
|
30
|
+
],
|
|
31
|
+
tools: { searchDocs, refundOrder },
|
|
32
|
+
run: async ({ messages, tools, signal, streamText }) =>
|
|
33
|
+
streamText({ model: anthropic("claude-sonnet-4-5"), messages, tools, abortSignal: signal }),
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`run()` is the same function that answers a browser chat or a [Linear session](/webhooks/linear). Signature verification, Slack's `url_verification` handshake, retry dedup, per-thread state, status updates and streaming are handled by the connector.
|
|
38
|
+
|
|
39
|
+
## Setup
|
|
40
|
+
|
|
41
|
+
<Steps>
|
|
42
|
+
<Step title="Deploy the agent">
|
|
43
|
+
Declare the endpoint with `webhookSource()` from `@trigger.dev/slack` and pass it to `slack({ endpoint })`. Deploying registers the hosted [endpoint](/webhooks/connect) and gives you its URL.
|
|
44
|
+
</Step>
|
|
45
|
+
<Step title="Create the Slack app from a manifest">
|
|
46
|
+
At [api.slack.com/apps](https://api.slack.com/apps), create an app **From a manifest** and paste this, with the endpoint's URL in both request URLs:
|
|
47
|
+
|
|
48
|
+
```yaml manifest.yaml
|
|
49
|
+
display_information:
|
|
50
|
+
name: Support Agent
|
|
51
|
+
features:
|
|
52
|
+
bot_user:
|
|
53
|
+
display_name: Support Agent
|
|
54
|
+
always_online: true
|
|
55
|
+
app_home:
|
|
56
|
+
messages_tab_enabled: true
|
|
57
|
+
messages_tab_read_only_enabled: false
|
|
58
|
+
agent_view:
|
|
59
|
+
agent_description: Answers support questions and handles refunds.
|
|
60
|
+
oauth_config:
|
|
61
|
+
scopes:
|
|
62
|
+
bot:
|
|
63
|
+
- assistant:write
|
|
64
|
+
- chat:write
|
|
65
|
+
- files:read
|
|
66
|
+
- reactions:write
|
|
67
|
+
- im:history
|
|
68
|
+
- app_mentions:read
|
|
69
|
+
- channels:history
|
|
70
|
+
- groups:history
|
|
71
|
+
settings:
|
|
72
|
+
event_subscriptions:
|
|
73
|
+
request_url: https://your-endpoint-url
|
|
74
|
+
bot_events:
|
|
75
|
+
- message.im
|
|
76
|
+
- app_mention
|
|
77
|
+
- message.channels
|
|
78
|
+
- message.groups
|
|
79
|
+
- agent_session_stopped
|
|
80
|
+
interactivity:
|
|
81
|
+
is_enabled: true
|
|
82
|
+
request_url: https://your-endpoint-url
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`agent_view` makes the app an agent, `agent_session_stopped` turns on the stop button, interactivity delivers clicks on the agent's buttons, `files:read` lets the agent read the images and PDFs users share with it, `reactions:write` lets it mark a message it picked up mid-reply, and `app_mention` with `app_mentions:read` lets an @mention start a session in a channel (see [Channel threads](#channel-threads)). Drop `app_mention`, `message.channels`, `message.groups` and their scopes if the agent should only answer in its own conversations.
|
|
86
|
+
</Step>
|
|
87
|
+
<Step title="Connect the endpoint">
|
|
88
|
+
Paste the app's **Signing Secret** (Basic Information, App Credentials) as the endpoint's signing secret. Until it is set the endpoint rejects Slack's signed requests, so Slack shows the request URL as not verified: once the secret is in, click **Verify** on the app's Event Subscriptions page (or **Click here to verify** on the App Manifest page). Install the app to your workspace and pass the bot token (`xoxb-...`) as `token`. The endpoint's setup prompt walks an AI coding agent through these settings.
|
|
89
|
+
</Step>
|
|
90
|
+
<Step title="Try it">
|
|
91
|
+
Open the agent in Slack and send it a message. The session shows it working within a second, and the reply streams in. In a channel, invite the bot (`/invite @Support Agent`) and message it in a thread.
|
|
92
|
+
</Step>
|
|
93
|
+
</Steps>
|
|
94
|
+
|
|
95
|
+
## What Slack shows
|
|
96
|
+
|
|
97
|
+
| Moment | In Slack |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| A turn starts | The session's status is **processing**. On the first turn the session is titled with the prompt. |
|
|
100
|
+
| The model writes | The reply streams into the thread with `chat.startStream`, `chat.appendStream` and `chat.stopStream`, as markdown. |
|
|
101
|
+
| A tool is called | A task in the reply's plan, titled by the tool's `title` (or its name made readable), marked complete when the tool returns and failed if it throws. The plan reads "Working on it", then "Done", "Stopped" or "Waiting for approval" when the turn ends. |
|
|
102
|
+
| A tool needs a decision | Approve and Deny buttons, and the status is **suspended** (see [Approvals and buttons](#approvals-and-buttons)). |
|
|
103
|
+
| The turn fails | The reply ends with "Something went wrong answering this. Try again." (`errorMessage` changes it). The error is logged on the run. |
|
|
104
|
+
| The turn ends | The status goes back to **active**, whether it answered, stopped, failed or had nothing to say. |
|
|
105
|
+
| `chat.close()` | The status is **closed**. |
|
|
106
|
+
|
|
107
|
+
Set `title` to name the session yourself; return null to keep Slack's own. A title the user sets in Slack is never overwritten. Slack drops a processing status after an hour, so a longer turn re-sends it every 50 minutes. Give a tool a readable task title with the AI SDK's `tool({ title: "Search the archive", ... })`; without one the task shows the tool's name made readable (`searchArchive` shows "Search archive"). `progress: "tasks"` shows one task card per call between the reply's text instead of a plan, `progress: "off"` streams the reply without them, and `delivery: "final"` posts the whole reply once the turn ends instead of streaming it.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
slack({
|
|
111
|
+
id: "slack",
|
|
112
|
+
endpoint: slackEvents,
|
|
113
|
+
token,
|
|
114
|
+
startOn: appMentions(),
|
|
115
|
+
title: (prompt) => `Support: ${prompt.slice(0, 80)}`,
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
If a streaming request fails in a way that leaves its outcome unknown (a timeout or a dropped connection), the connector does not retry it, since Slack may already have shown that text. It closes the stream and posts the rest of the reply as its own message, so nothing is lost, at the cost of possibly repeating one batch. If that post fails too, the whole reply is posted again as a normal message. Rate limits wait out `Retry-After` and resend the same batch. If the worker running a turn dies mid-stream, the next worker closes that stream with a short "Interrupted" note before it picks the conversation back up.
|
|
120
|
+
|
|
121
|
+
## Approvals and buttons
|
|
122
|
+
|
|
123
|
+
A tool with no `execute` pauses the turn until the user answers it in Slack. By default `slack()` posts Approve and Deny buttons, and a click resolves the tool to `{ approved: boolean }`, collapses the buttons to the decision and resumes the turn. See [human-in-the-loop](/webhooks/human-in-the-loop) for the tool definition.
|
|
124
|
+
|
|
125
|
+
`approval` replaces those buttons for every such tool. Build buttons with `button()`, whose `output` is what the click resolves the tool to:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
slack({
|
|
129
|
+
id: "slack",
|
|
130
|
+
endpoint: slackEvents,
|
|
131
|
+
token,
|
|
132
|
+
startOn: appMentions(),
|
|
133
|
+
approval: (call, { button }) => ({
|
|
134
|
+
text: `Run ${call.toolName}?`,
|
|
135
|
+
blocks: [
|
|
136
|
+
{ type: "section", text: { type: "mrkdwn", text: `Run \`${call.toolName}\`?` } },
|
|
137
|
+
{
|
|
138
|
+
type: "actions",
|
|
139
|
+
elements: [
|
|
140
|
+
button({ text: "Ship it", style: "primary", output: { approved: true } }),
|
|
141
|
+
button({ text: "Not now", output: { approved: false } }),
|
|
142
|
+
],
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
}),
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
`interactions` gives a single tool its own UI, keyed by tool name, so a tool can ask the user to pick, rate or choose and continue with the answer:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
export const moji = chat.agent({
|
|
153
|
+
id: "moji",
|
|
154
|
+
channels: [
|
|
155
|
+
slack({
|
|
156
|
+
id: "slack",
|
|
157
|
+
endpoint: slackEvents,
|
|
158
|
+
token,
|
|
159
|
+
startOn: appMentions(),
|
|
160
|
+
interactions: {
|
|
161
|
+
pickEmoji: (call, { button }) => {
|
|
162
|
+
const { options } = call.input as { options: { name: string }[] };
|
|
163
|
+
return {
|
|
164
|
+
text: "Pick one",
|
|
165
|
+
blocks: [
|
|
166
|
+
{ type: "section", text: { type: "mrkdwn", text: "Which one should I add?" } },
|
|
167
|
+
{
|
|
168
|
+
type: "actions",
|
|
169
|
+
elements: options.map((o) => button({ text: `:${o.name}:`, output: { picked: o.name } })),
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
};
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
}),
|
|
176
|
+
],
|
|
177
|
+
tools: {
|
|
178
|
+
pickEmoji: tool({
|
|
179
|
+
description: "Ask the user to pick one of the generated emoji",
|
|
180
|
+
inputSchema: z.object({ options: z.array(z.object({ name: z.string() })) }),
|
|
181
|
+
outputSchema: z.object({ picked: z.string() }),
|
|
182
|
+
}),
|
|
183
|
+
addEmoji,
|
|
184
|
+
},
|
|
185
|
+
run: async ({ messages, tools, signal, streamText }) =>
|
|
186
|
+
streamText({ model, messages, tools, abortSignal: signal, stopWhen: stepCountIs(10) }),
|
|
187
|
+
});
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
The click resolves `pickEmoji` to `{ picked: "wizard" }`, the buttons collapse to the choice, and the turn continues.
|
|
191
|
+
|
|
192
|
+
### Buttons that send the agent an action
|
|
193
|
+
|
|
194
|
+
Buttons that aren't answering a tool, such as follow-up questions, are built with `slackButton()` and posted with the surface's `post()`. A click reaches `onAction`, validated against `actionSchema` like a browser action, and returning `chat.turn(message)` answers it in the thread:
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
import { slackButton, type SlackRunSurface } from "@trigger.dev/slack";
|
|
198
|
+
|
|
199
|
+
export const support = chat.agent({
|
|
200
|
+
id: "support",
|
|
201
|
+
channels: [slack({ id: "slack", endpoint: slackEvents, token, startOn: appMentions() })],
|
|
202
|
+
actionSchema: z.object({ type: z.literal("ask"), question: z.string() }),
|
|
203
|
+
tools: {
|
|
204
|
+
offerFollowUps: tool({
|
|
205
|
+
description: "Offer the user up to three follow-up questions as buttons",
|
|
206
|
+
inputSchema: z.object({ questions: z.array(z.string()).max(3) }),
|
|
207
|
+
execute: async ({ questions }) => {
|
|
208
|
+
await chat.surface<SlackRunSurface>()?.post({
|
|
209
|
+
text: "Follow-up questions",
|
|
210
|
+
blocks: [
|
|
211
|
+
{
|
|
212
|
+
type: "actions",
|
|
213
|
+
elements: questions.map((question) =>
|
|
214
|
+
slackButton({ text: question, action: { type: "ask", question } })
|
|
215
|
+
),
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
});
|
|
219
|
+
return "offered";
|
|
220
|
+
},
|
|
221
|
+
}),
|
|
222
|
+
},
|
|
223
|
+
onAction: async ({ action }) => chat.turn(action.question),
|
|
224
|
+
run: async ({ messages, tools, signal, streamText }) =>
|
|
225
|
+
streamText({ model, messages, tools, abortSignal: signal, stopWhen: stepCountIs(10) }),
|
|
226
|
+
});
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
`onAction` also gets the click itself as `channel.event`, typed with `SlackBlockActions`: who clicked, and the message the button was on. An action doesn't have to answer; return nothing to only do work. Here `actionSchema` accepts a `pick` action alongside `ask`:
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
import type { SlackBlockActions } from "@trigger.dev/slack";
|
|
233
|
+
|
|
234
|
+
actionSchema: z.discriminatedUnion("type", [
|
|
235
|
+
z.object({ type: z.literal("ask"), question: z.string() }),
|
|
236
|
+
z.object({ type: z.literal("pick"), name: z.string() }),
|
|
237
|
+
]),
|
|
238
|
+
onAction: async ({ action, channel }) => {
|
|
239
|
+
const click = channel?.event as SlackBlockActions | undefined;
|
|
240
|
+
if (action.type === "pick") {
|
|
241
|
+
await addEmoji.trigger({ name: action.name, pickedBy: click?.user?.id });
|
|
242
|
+
return; // no turn
|
|
243
|
+
}
|
|
244
|
+
return chat.turn(action.question);
|
|
245
|
+
},
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Only buttons built with `button()` or `slackButton()` reach the agent. Buttons your own code builds by hand go to a [`webhook()`](/webhooks/overview) subscribed to the same endpoint, and never start a turn.
|
|
249
|
+
|
|
250
|
+
## Stopping
|
|
251
|
+
|
|
252
|
+
Slack shows a stop button while the agent is working. It aborts the running turn through the run's `signal`, exactly like a browser chat's stop: the model call and any tool that honours the signal stop, the partial reply stays in the thread, and the status goes back to active. The next message continues the conversation.
|
|
253
|
+
|
|
254
|
+
- A stop pressed while nothing is running does nothing, and a repeated or delayed stop never stops a later turn.
|
|
255
|
+
- A stop that reaches a session waiting on an approval cancels it: the buttons are replaced with "Stopped", the pending tool call is recorded as stopped so the model sees that on the next turn, and a later click resumes nothing.
|
|
256
|
+
- Stopping cannot undo what already happened. A tool that finished before the stop has had its effect.
|
|
257
|
+
|
|
258
|
+
## Channel threads
|
|
259
|
+
|
|
260
|
+
`startOn` is required: it decides which channel messages start a session. Pass `appMentions()` to have the agent answer only when it is @mentioned. A mention in a channel starts a session and the agent answers in a thread under it, then follows the rest of that thread without being mentioned again. `startOn` only applies to channels: a message in the agent's own conversation always starts a session.
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
import { slack, appMentions } from "@trigger.dev/slack";
|
|
264
|
+
|
|
265
|
+
slack({ id: "slack", endpoint: slackEvents, token, startOn: appMentions() });
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
`appMentions()` uses Slack's `app_mention` event, so the app needs the `app_mention` bot event and the `app_mentions:read` scope (both in the manifest above), and no bot user id. Slack also delivers each mention as an ordinary channel message; the connector ignores that copy, so one mention runs one turn.
|
|
269
|
+
|
|
270
|
+
What each value does:
|
|
271
|
+
|
|
272
|
+
| `startOn` | The agent starts a session in a channel |
|
|
273
|
+
| --- | --- |
|
|
274
|
+
| `appMentions()` | When it is @mentioned (recommended). |
|
|
275
|
+
| `mentions("U012BOT")` | When a message mentions that bot user id, from message events, without the `app_mention` subscription. |
|
|
276
|
+
| `"event.event.type == 'message'"` | On every message it can see. |
|
|
277
|
+
| `"event.event.channel_type == 'im'"` | Never: it answers only in its own conversation. |
|
|
278
|
+
|
|
279
|
+
In a channel shared with another organization through Slack Connect, pass `ownWorkspaceOnly: true` to answer only people from your own workspace. Messages, mentions and button clicks from the other organization's users are dropped before they reach the agent. In Enterprise Grid this also drops users whose home workspace is another workspace in the org.
|
|
280
|
+
|
|
281
|
+
When someone mentions the agent partway through a thread, `seedFromThread: true` loads the thread's earlier replies into the new session first, so the agent answers with the context a reader of the thread has. It loads the history once, on the session's first turn, paging through `conversations.replies` and keeping the newest 50 replies (`{ limit }` changes that). Slack limits new commercially distributed apps outside the Marketplace to 15 replies per call and one call a minute, so such an app may only get the thread's first page; internal apps keep the normal limits. A rate limit keeps what was fetched rather than failing the turn.
|
|
282
|
+
|
|
283
|
+
## Messages sent while the agent is working
|
|
284
|
+
|
|
285
|
+
A message sent while the agent is still answering gets 👀 as soon as it arrives, so the user knows it was seen (`receivedReaction` changes the emoji, null turns it off). It then waits and runs as the next turn, in the order messages arrived. With [`pendingMessages`](/ai-chat/pending-messages) on the agent, it can be folded into the reply the agent is already writing instead: `shouldInject` decides at each step, and the message reaches the model before its next step. A message `shouldInject` declines still runs as its own turn.
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
export const support = chat.agent({
|
|
289
|
+
id: "support",
|
|
290
|
+
channels: [slack({ id: "slack", endpoint: slackEvents, token, startOn: appMentions() })],
|
|
291
|
+
pendingMessages: { shouldInject: () => true },
|
|
292
|
+
run: async ({ messages, signal, streamText }) =>
|
|
293
|
+
streamText({ model, messages, abortSignal: signal, stopWhen: stepCountIs(10) }),
|
|
294
|
+
});
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
A message is only folded in at a step boundary, so a reply that is one long model step with no tool calls finishes first and the message runs next. The stop button and button clicks are never folded in.
|
|
298
|
+
|
|
299
|
+
## Files
|
|
300
|
+
|
|
301
|
+
Images (PNG, JPEG, GIF, WebP) and PDFs that a user shares in the conversation reach the model with their message, including a file sent with no text. The transcript keeps each file's Slack URL, and the `streamText` given to `run()` downloads it with the bot token when the model reads it, once per run. An image over 5 MB is sent as its largest Slack thumbnail.
|
|
302
|
+
|
|
303
|
+
Other file types, files over 20 MB, and files the app can't read (deleted, or shared from another workspace) reach the model as a one-line note naming the file, so it can say it can't open them. A file that is deleted after it was shared becomes the same note on later turns, and the conversation carries on.
|
|
304
|
+
|
|
305
|
+
A custom `inbound` keeps file support by adding `slackFileParts()` to its message:
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
import { slackFileParts } from "@trigger.dev/slack";
|
|
309
|
+
|
|
310
|
+
inbound: (event) => ({
|
|
311
|
+
message: {
|
|
312
|
+
id: event.event?.ts ?? "",
|
|
313
|
+
role: "user",
|
|
314
|
+
parts: [{ type: "text", text: event.event?.text ?? "" }, ...slackFileParts(event.event?.files)],
|
|
315
|
+
},
|
|
316
|
+
}),
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
The download happens in the `streamText` passed to `run()`. A `streamText` imported from `ai` sends the Slack URL to the model as is, which the model can't open.
|
|
320
|
+
|
|
321
|
+
## Answering into the session from elsewhere
|
|
322
|
+
|
|
323
|
+
The first Slack message binds the session to its thread and to the user it is answering. A [`chat.event`](/webhooks/session-routing#answering-into-a-channel) action that returns `chat.turn()`, or a browser client on the same session, answers into that Slack thread with the same status, streaming and approval buttons.
|
|
324
|
+
|
|
325
|
+
## Slash commands
|
|
326
|
+
|
|
327
|
+
Slack posts a slash command as a form, which reaches subscribers as an object of its fields, and the endpoint acknowledges it with an empty `200` so Slack shows the user nothing until you reply. Give slash commands their own endpoint, typed with `SlackSlashCommand`, and set its URL as the command's Request URL under Slash Commands in the Slack app's settings. Use the same Signing Secret as the agent's endpoint. Reply through the command's `response_url`:
|
|
328
|
+
|
|
329
|
+
```ts /trigger/commands.ts
|
|
330
|
+
import { webhook, webhooks } from "@trigger.dev/sdk";
|
|
331
|
+
import { webhookSource, type SlackSlashCommand } from "@trigger.dev/slack";
|
|
332
|
+
|
|
333
|
+
const slackCommands = webhooks.endpoint.define({
|
|
334
|
+
id: "slack-commands",
|
|
335
|
+
source: webhookSource<SlackSlashCommand>(),
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
export const deployCommand = webhook({
|
|
339
|
+
id: "deploy-command",
|
|
340
|
+
endpoint: slackCommands,
|
|
341
|
+
filter: "event.command == '/deploy'",
|
|
342
|
+
onEvent: async ({ event }) => {
|
|
343
|
+
await fetch(event.response_url, {
|
|
344
|
+
method: "POST",
|
|
345
|
+
headers: { "content-type": "application/json" },
|
|
346
|
+
body: JSON.stringify({ response_type: "ephemeral", text: `Deploying ${event.text}` }),
|
|
347
|
+
});
|
|
348
|
+
},
|
|
349
|
+
});
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
Any subscriber works here: a [`chat.event`](/webhooks/session-routing) keyed on `{event.channel_id}:{event.user_id}` routes a user's commands to one session, and a [waiter](/webhooks/waiters) can match on `event.command`.
|
|
353
|
+
|
|
354
|
+
## Block Kit replies
|
|
355
|
+
|
|
356
|
+
Replies are sent as markdown. For a reply that is posted rather than streamed (`delivery: "final"`), return `blocks` from `outbound` to post Block Kit. `toSlackBlocks()` splits markdown into `section` blocks under Slack's 3000-character limit, never inside a code fence:
|
|
357
|
+
|
|
358
|
+
```ts
|
|
359
|
+
import { slack, toSlackBlocks } from "@trigger.dev/slack";
|
|
360
|
+
|
|
361
|
+
slack({
|
|
362
|
+
id: "slack",
|
|
363
|
+
endpoint: slackEvents,
|
|
364
|
+
token,
|
|
365
|
+
startOn: appMentions(),
|
|
366
|
+
delivery: "final",
|
|
367
|
+
outbound: (reply) =>
|
|
368
|
+
reply.text
|
|
369
|
+
? {
|
|
370
|
+
text: reply.text.slice(0, 3000), // notification fallback
|
|
371
|
+
blocks: [{ type: "header", text: { type: "plain_text", text: "Support" } }, ...toSlackBlocks(reply.text)],
|
|
372
|
+
}
|
|
373
|
+
: null,
|
|
374
|
+
});
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
## Options
|
|
378
|
+
|
|
379
|
+
| Option | Type | Description |
|
|
380
|
+
| --- | --- | --- |
|
|
381
|
+
| `id` | `string` | Connector id, unique per agent. |
|
|
382
|
+
| `endpoint` | endpoint | The Slack endpoint, declared with `webhooks.endpoint.define({ id, source: webhookSource() })`. `webhook()` tasks can subscribe to the same endpoint. |
|
|
383
|
+
| `token` | `string` or resolver | Bot token (`xoxb-...`), or a function of the event (its `team_id`) for multi-workspace apps. |
|
|
384
|
+
| `delivery` | `"stream"` or `"final"` | `"stream"` (default) streams the reply natively. `"final"` posts it once when the turn ends. |
|
|
385
|
+
| `title` | `(prompt, event) => string \| null` | The session title, set on its first turn. Defaults to the first line of the prompt. |
|
|
386
|
+
| `progress` | `"plan"`, `"tasks"` or `"off"` | How tool calls show in the streamed reply: one plan block (default), a task card per call, or none. Task titles come from each tool's `title`. |
|
|
387
|
+
| `startOn` | filter string | Required. Which channel messages start a session, usually `appMentions()` (see [Channel threads](#channel-threads)). Existing sessions always resume, and the agent's own conversation always starts one. Checked against the endpoint's event type. |
|
|
388
|
+
| `filter` | filter string | Extra [filter](/webhooks/filters) for message events, composed with the built-in guard that drops the bot's own messages. Checked against the endpoint's event type, like `webhook()`'s `filter`. |
|
|
389
|
+
| `key` | `string` | Session [key](/webhooks/session-routing) template. Defaults to one session per thread. |
|
|
390
|
+
| `seedFromThread` | `boolean` or `{ limit }` | Load a thread's earlier replies into a new session. Off by default. |
|
|
391
|
+
| `approval` | `(call, { button }) => message` | The UI for a tool with no `execute`. Defaults to Approve and Deny resolving to `{ approved }`. |
|
|
392
|
+
| `interactions` | `{ [toolName]: (call, { button }) => message }` | A tool's own UI; a click resolves the tool with the button's `output`. |
|
|
393
|
+
| `receivedReaction` | `string \| null` | Emoji added, on arrival, to a message sent while the agent is answering. Default `"eyes"`, null for none. |
|
|
394
|
+
| `errorMessage` | `string \| (ctx) => string` | What the reply shows when a turn fails, fixed or built from the failure ([details](/webhooks/channels#when-a-turn-fails)). Default "Something went wrong answering this. Try again." |
|
|
395
|
+
| `ownWorkspaceOnly` | `boolean` | Drop messages, mentions and button clicks from people outside the app's workspace, such as Slack Connect guests. Off by default. |
|
|
396
|
+
| `inbound` / `outbound` | functions | Map a Slack event to the turn, and a posted reply to a Slack message. |
|
|
397
|
+
| `apiBaseUrl` | `string` | Override the Slack Web API base, for testing against a mock. |
|