@trigger.dev/sdk 4.5.0-rc.6 → 4.5.0-rc.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/dist/commonjs/v3/ai.d.ts +171 -5
  2. package/dist/commonjs/v3/ai.js +309 -22
  3. package/dist/commonjs/v3/ai.js.map +1 -1
  4. package/dist/commonjs/v3/chat-server.d.ts +8 -0
  5. package/dist/commonjs/v3/chat-server.js +32 -10
  6. package/dist/commonjs/v3/chat-server.js.map +1 -1
  7. package/dist/commonjs/v3/chat-server.test.js +51 -0
  8. package/dist/commonjs/v3/chat-server.test.js.map +1 -1
  9. package/dist/commonjs/v3/createStartSessionAction.test.js +30 -0
  10. package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -1
  11. package/dist/commonjs/v3/sessions.d.ts +3 -2
  12. package/dist/commonjs/v3/sessions.js +3 -2
  13. package/dist/commonjs/v3/sessions.js.map +1 -1
  14. package/dist/commonjs/version.js +1 -1
  15. package/dist/esm/v3/ai.d.ts +171 -5
  16. package/dist/esm/v3/ai.js +309 -22
  17. package/dist/esm/v3/ai.js.map +1 -1
  18. package/dist/esm/v3/chat-server.d.ts +8 -0
  19. package/dist/esm/v3/chat-server.js +32 -10
  20. package/dist/esm/v3/chat-server.js.map +1 -1
  21. package/dist/esm/v3/chat-server.test.js +51 -0
  22. package/dist/esm/v3/chat-server.test.js.map +1 -1
  23. package/dist/esm/v3/createStartSessionAction.test.js +30 -0
  24. package/dist/esm/v3/createStartSessionAction.test.js.map +1 -1
  25. package/dist/esm/v3/sessions.d.ts +3 -2
  26. package/dist/esm/v3/sessions.js +3 -2
  27. package/dist/esm/v3/sessions.js.map +1 -1
  28. package/dist/esm/version.js +1 -1
  29. package/docs/ai/prompts.mdx +430 -0
  30. package/docs/ai-chat/actions.mdx +115 -0
  31. package/docs/ai-chat/anatomy.mdx +71 -0
  32. package/docs/ai-chat/backend.mdx +817 -0
  33. package/docs/ai-chat/background-injection.mdx +221 -0
  34. package/docs/ai-chat/changelog.mdx +850 -0
  35. package/docs/ai-chat/chat-local.mdx +174 -0
  36. package/docs/ai-chat/client-protocol.mdx +1081 -0
  37. package/docs/ai-chat/compaction.mdx +411 -0
  38. package/docs/ai-chat/custom-agents.mdx +364 -0
  39. package/docs/ai-chat/error-handling.mdx +415 -0
  40. package/docs/ai-chat/fast-starts.mdx +672 -0
  41. package/docs/ai-chat/frontend.mdx +580 -0
  42. package/docs/ai-chat/how-it-works.mdx +230 -0
  43. package/docs/ai-chat/lifecycle-hooks.mdx +530 -0
  44. package/docs/ai-chat/mcp.mdx +101 -0
  45. package/docs/ai-chat/overview.mdx +90 -0
  46. package/docs/ai-chat/patterns/branching-conversations.mdx +284 -0
  47. package/docs/ai-chat/patterns/code-sandbox.mdx +126 -0
  48. package/docs/ai-chat/patterns/database-persistence.mdx +414 -0
  49. package/docs/ai-chat/patterns/human-in-the-loop.mdx +275 -0
  50. package/docs/ai-chat/patterns/large-payloads.mdx +169 -0
  51. package/docs/ai-chat/patterns/oom-resilience.mdx +120 -0
  52. package/docs/ai-chat/patterns/persistence-and-replay.mdx +211 -0
  53. package/docs/ai-chat/patterns/recovery-boot.mdx +230 -0
  54. package/docs/ai-chat/patterns/skills.mdx +221 -0
  55. package/docs/ai-chat/patterns/sub-agents.mdx +383 -0
  56. package/docs/ai-chat/patterns/tool-result-auditing.mdx +148 -0
  57. package/docs/ai-chat/patterns/trusted-edge-signals.mdx +337 -0
  58. package/docs/ai-chat/patterns/version-upgrades.mdx +172 -0
  59. package/docs/ai-chat/pending-messages.mdx +343 -0
  60. package/docs/ai-chat/prompt-caching.mdx +206 -0
  61. package/docs/ai-chat/quick-start.mdx +161 -0
  62. package/docs/ai-chat/reference.mdx +909 -0
  63. package/docs/ai-chat/server-chat.mdx +263 -0
  64. package/docs/ai-chat/sessions.mdx +333 -0
  65. package/docs/ai-chat/testing.mdx +682 -0
  66. package/docs/ai-chat/tools.mdx +191 -0
  67. package/docs/ai-chat/types.mdx +242 -0
  68. package/docs/ai-chat/upgrade-guide.mdx +515 -0
  69. package/docs/apikeys.mdx +54 -0
  70. package/docs/building-with-ai.mdx +261 -0
  71. package/docs/bulk-actions.mdx +49 -0
  72. package/docs/changelog.mdx +6 -0
  73. package/docs/cli-deploy-commands.mdx +9 -0
  74. package/docs/cli-dev-commands.mdx +9 -0
  75. package/docs/cli-dev.mdx +8 -0
  76. package/docs/cli-init-commands.mdx +58 -0
  77. package/docs/cli-introduction.mdx +25 -0
  78. package/docs/cli-list-profiles-commands.mdx +42 -0
  79. package/docs/cli-login-commands.mdx +33 -0
  80. package/docs/cli-logout-commands.mdx +33 -0
  81. package/docs/cli-preview-archive.mdx +59 -0
  82. package/docs/cli-promote-commands.mdx +9 -0
  83. package/docs/cli-switch.mdx +43 -0
  84. package/docs/cli-update-commands.mdx +42 -0
  85. package/docs/cli-whoami-commands.mdx +33 -0
  86. package/docs/community.mdx +6 -0
  87. package/docs/config/config-file.mdx +602 -0
  88. package/docs/config/extensions/additionalFiles.mdx +38 -0
  89. package/docs/config/extensions/additionalPackages.mdx +40 -0
  90. package/docs/config/extensions/aptGet.mdx +34 -0
  91. package/docs/config/extensions/audioWaveform.mdx +20 -0
  92. package/docs/config/extensions/custom.mdx +380 -0
  93. package/docs/config/extensions/emitDecoratorMetadata.mdx +29 -0
  94. package/docs/config/extensions/esbuildPlugin.mdx +31 -0
  95. package/docs/config/extensions/ffmpeg.mdx +45 -0
  96. package/docs/config/extensions/lightpanda.mdx +56 -0
  97. package/docs/config/extensions/overview.mdx +67 -0
  98. package/docs/config/extensions/playwright.mdx +195 -0
  99. package/docs/config/extensions/prismaExtension.mdx +1014 -0
  100. package/docs/config/extensions/puppeteer.mdx +30 -0
  101. package/docs/config/extensions/pythonExtension.mdx +182 -0
  102. package/docs/config/extensions/syncEnvVars.mdx +291 -0
  103. package/docs/context.mdx +235 -0
  104. package/docs/database-connections.mdx +213 -0
  105. package/docs/deploy-environment-variables.mdx +435 -0
  106. package/docs/deployment/atomic-deployment.mdx +172 -0
  107. package/docs/deployment/overview.mdx +257 -0
  108. package/docs/deployment/preview-branches.mdx +224 -0
  109. package/docs/errors-retrying.mdx +379 -0
  110. package/docs/github-actions.mdx +222 -0
  111. package/docs/github-integration.mdx +136 -0
  112. package/docs/github-repo.mdx +8 -0
  113. package/docs/help-email.mdx +6 -0
  114. package/docs/help-slack.mdx +11 -0
  115. package/docs/hidden-tasks.mdx +56 -0
  116. package/docs/how-it-works.mdx +454 -0
  117. package/docs/how-to-reduce-your-spend.mdx +217 -0
  118. package/docs/idempotency.mdx +504 -0
  119. package/docs/introduction.mdx +223 -0
  120. package/docs/limits.mdx +241 -0
  121. package/docs/logging.mdx +195 -0
  122. package/docs/machines.mdx +952 -0
  123. package/docs/manual-setup.mdx +632 -0
  124. package/docs/mcp-agent-rules.mdx +41 -0
  125. package/docs/mcp-introduction.mdx +385 -0
  126. package/docs/mcp-tools.mdx +273 -0
  127. package/docs/migrating-from-v3.mdx +334 -0
  128. package/docs/observability/dashboards.mdx +102 -0
  129. package/docs/observability/query.mdx +585 -0
  130. package/docs/open-source-contributing.mdx +16 -0
  131. package/docs/open-source-self-hosting.mdx +541 -0
  132. package/docs/private-networking/aws-console-setup.mdx +304 -0
  133. package/docs/private-networking/overview.mdx +144 -0
  134. package/docs/private-networking/troubleshooting.mdx +78 -0
  135. package/docs/queue-concurrency.mdx +354 -0
  136. package/docs/quick-start.mdx +97 -0
  137. package/docs/realtime/auth.mdx +208 -0
  138. package/docs/realtime/backend/overview.mdx +45 -0
  139. package/docs/realtime/backend/streams.mdx +418 -0
  140. package/docs/realtime/backend/subscribe.mdx +225 -0
  141. package/docs/realtime/how-it-works.mdx +94 -0
  142. package/docs/realtime/overview.mdx +63 -0
  143. package/docs/realtime/react-hooks/overview.mdx +73 -0
  144. package/docs/realtime/react-hooks/streams.mdx +449 -0
  145. package/docs/realtime/react-hooks/subscribe.mdx +674 -0
  146. package/docs/realtime/react-hooks/swr.mdx +87 -0
  147. package/docs/realtime/react-hooks/triggering.mdx +194 -0
  148. package/docs/realtime/react-hooks/use-wait-token.mdx +34 -0
  149. package/docs/realtime/run-object.mdx +174 -0
  150. package/docs/replaying.mdx +72 -0
  151. package/docs/request-feature.mdx +6 -0
  152. package/docs/roadmap.mdx +6 -0
  153. package/docs/run-tests.mdx +20 -0
  154. package/docs/run-usage.mdx +113 -0
  155. package/docs/runs/heartbeats.mdx +38 -0
  156. package/docs/runs/max-duration.mdx +139 -0
  157. package/docs/runs/metadata.mdx +734 -0
  158. package/docs/runs/priority.mdx +31 -0
  159. package/docs/runs.mdx +396 -0
  160. package/docs/self-hosting/docker.mdx +458 -0
  161. package/docs/self-hosting/env/supervisor.mdx +74 -0
  162. package/docs/self-hosting/env/webapp.mdx +276 -0
  163. package/docs/self-hosting/kubernetes.mdx +601 -0
  164. package/docs/self-hosting/overview.mdx +108 -0
  165. package/docs/skills.mdx +85 -0
  166. package/docs/tags.mdx +120 -0
  167. package/docs/tasks/overview.mdx +697 -0
  168. package/docs/tasks/scheduled.mdx +382 -0
  169. package/docs/tasks/schemaTask.mdx +413 -0
  170. package/docs/tasks/streams.mdx +884 -0
  171. package/docs/triggering.mdx +1320 -0
  172. package/docs/troubleshooting-alerts.mdx +385 -0
  173. package/docs/troubleshooting-debugging-in-vscode.mdx +8 -0
  174. package/docs/troubleshooting-github-issues.mdx +6 -0
  175. package/docs/troubleshooting-uptime-status.mdx +6 -0
  176. package/docs/troubleshooting.mdx +398 -0
  177. package/docs/upgrading-packages.mdx +80 -0
  178. package/docs/vercel-integration.mdx +207 -0
  179. package/docs/versioning.mdx +56 -0
  180. package/docs/video-walkthrough.mdx +23 -0
  181. package/docs/wait-for-token.mdx +540 -0
  182. package/docs/wait-for.mdx +42 -0
  183. package/docs/wait-until.mdx +53 -0
  184. package/docs/wait.mdx +18 -0
  185. package/docs/writing-tasks-introduction.mdx +33 -0
  186. package/package.json +8 -5
  187. package/skills/trigger-authoring-chat-agent/SKILL.md +296 -0
  188. package/skills/trigger-authoring-tasks/SKILL.md +254 -0
  189. package/skills/trigger-chat-agent-advanced/SKILL.md +368 -0
  190. package/skills/trigger-cost-savings/SKILL.md +116 -0
  191. package/skills/trigger-realtime-and-frontend/SKILL.md +276 -0
@@ -0,0 +1,116 @@
1
+ ---
2
+ name: trigger-cost-savings
3
+ description: >
4
+ Analyze Trigger.dev tasks, schedules, and runs for cost optimization opportunities. Use when
5
+ asked to reduce spend, optimize costs, audit usage, right-size machines, or review task
6
+ efficiency. Combines static source analysis with live run analysis via the Trigger.dev MCP
7
+ tools (list_runs, get_run_details, get_current_worker).
8
+ type: core
9
+ library: trigger.dev
10
+ sources:
11
+ - docs/how-to-reduce-your-spend.mdx
12
+ - docs/machines.mdx
13
+ - docs/runs/max-duration.mdx
14
+ - docs/queue-concurrency.mdx
15
+ - docs/idempotency.mdx
16
+ - docs/triggering.mdx
17
+ - docs/errors-retrying.mdx
18
+ - docs/limits.mdx
19
+ ---
20
+
21
+ # Trigger.dev Cost Savings Analysis
22
+
23
+ Analyze task runs and configurations to find cost reduction opportunities. This skill pairs static source analysis with live run analysis via the Trigger.dev MCP server.
24
+
25
+ ## Before you start: read the canonical guidance
26
+
27
+ The authoritative, version-pinned cost guidance ships beside this skill. Read it first so your recommendations match the installed SDK version:
28
+
29
+ - `@trigger.dev/sdk/docs/how-to-reduce-your-spend.mdx` — the canonical "reduce your spend" guide (machine sizing, idempotency de-dup, parallelism, retries, `maxDuration`, checkpointed waits, debounce).
30
+ - Supporting references: `@trigger.dev/sdk/docs/machines.mdx`, `runs/max-duration.mdx`, `queue-concurrency.mdx`, `idempotency.mdx`, `triggering.mdx` (debounce + batch), `errors-retrying.mdx` (`AbortTaskRunError`).
31
+
32
+ ## Prerequisites: MCP tools
33
+
34
+ Live run analysis needs the **Trigger.dev MCP server**. Verify these tools are available:
35
+
36
+ - `list_runs` — list runs with filters (status, task, time period, machine size)
37
+ - `get_run_details` — get run logs, duration, and status
38
+ - `get_current_worker` — get registered tasks and their configurations
39
+
40
+ If they're not available, tell the user to install the MCP server:
41
+
42
+ ```bash
43
+ npx trigger.dev@latest install-mcp
44
+ ```
45
+
46
+ Without the MCP tools you can still do the static source analysis below; do not fabricate run data.
47
+
48
+ ## Analysis workflow
49
+
50
+ ### Step 1: Static analysis (source code)
51
+
52
+ Scan task files for:
53
+
54
+ 1. **Oversized machines** — tasks on `large-1x`/`large-2x` without clear need.
55
+ 2. **Missing `maxDuration`** — no execution-time limit (runaway-cost risk).
56
+ 3. **Excessive retries** — `maxAttempts` > 5 without `AbortTaskRunError` for known-permanent failures.
57
+ 4. **Missing debounce** — high-frequency triggers without debounce.
58
+ 5. **Missing idempotency** — payment/critical tasks without idempotency keys.
59
+ 6. **Polling instead of waits** — `setTimeout`/`setInterval`/sleep loops instead of `wait.for()`.
60
+ 7. **Short waits** — `wait.for()` under 5 seconds (not checkpointed, wastes compute).
61
+ 8. **Sequential instead of batch** — multiple `triggerAndWait()` calls that could be `batchTriggerAndWait()`.
62
+ 9. **Over-scheduled crons** — schedules firing more often than needed.
63
+
64
+ ### Step 2: Run analysis (requires MCP tools)
65
+
66
+ - **2a. Expensive tasks** — `list_runs` over `period: "30d"`/`"7d"`; find high total compute (duration × count), high failure rates, and large machines with short durations (over-provisioned).
67
+ - **2b. Failure patterns** — `list_runs` with `status: "FAILED"`/`"CRASHED"`; separate transient (retryable) from permanent; suggest `AbortTaskRunError` for the latter; estimate wasted retry compute.
68
+ - **2c. Machine utilization** — `get_run_details` on sample runs; if a `large-2x` task consistently runs in under a second, or is I/O-bound (API/DB), it's over-provisioned.
69
+ - **2d. Schedule frequency** — `get_current_worker` to list cron patterns; flag schedules that are too frequent for their purpose.
70
+
71
+ ### Step 3: Generate recommendations
72
+
73
+ Present a prioritized report with estimated impact:
74
+
75
+ ```markdown
76
+ ## Cost Optimization Report
77
+
78
+ ### High impact
79
+ 1. **Right-size `process-images`** — currently `large-2x`, average run 2s. `small-2x` could cut this task's cost by ~16x.
80
+ `machine: { preset: "small-2x" }` // was "large-2x"
81
+
82
+ ### Medium impact
83
+ 2. **Debounce `sync-user-data`** — 847 runs/day, often bursty.
84
+ `debounce: { key: \`user-${userId}\`, delay: "5s" }`
85
+
86
+ ### Low impact / best practice
87
+ 3. **Add `maxDuration` to `generate-report`** — no timeout configured.
88
+ `maxDuration: 300` // 5 minutes
89
+ ```
90
+
91
+ ## Machine preset costs (relative)
92
+
93
+ Larger machines cost proportionally more per second of compute:
94
+
95
+ | Preset | vCPU | RAM | Relative cost |
96
+ |--------|------|-----|---------------|
97
+ | micro | 0.25 | 0.25 GB | 0.25x |
98
+ | small-1x | 0.5 | 0.5 GB | 1x (baseline) |
99
+ | small-2x | 1 | 1 GB | 2x |
100
+ | medium-1x | 1 | 2 GB | 2x |
101
+ | medium-2x | 2 | 4 GB | 4x |
102
+ | large-1x | 4 | 8 GB | 8x |
103
+ | large-2x | 8 | 16 GB | 16x |
104
+
105
+ ## Key principles
106
+
107
+ - **Waits > 5 seconds are free** — checkpointed, no compute charge.
108
+ - **Start small, scale up** — the default `small-1x` is right for most tasks.
109
+ - **I/O-bound tasks don't need big machines** — API calls and DB queries wait on the network.
110
+ - **Debounce saves the most on high-frequency tasks** — it consolidates bursts into single runs.
111
+ - **Idempotency prevents duplicate billed work** — especially for expensive operations.
112
+ - **`AbortTaskRunError` stops wasteful retries** — don't pay to retry permanent failures.
113
+
114
+ ## Version
115
+
116
+ This skill is bundled inside `@trigger.dev/sdk` and read directly from `node_modules`, so it always matches your installed SDK version (see the adjacent `package.json`). The full cost documentation ships alongside it under `@trigger.dev/sdk/docs/`.
@@ -0,0 +1,276 @@
1
+ ---
2
+ name: trigger-realtime-and-frontend
3
+ description: >
4
+ Trigger.dev client/frontend surface: subscribe to runs in realtime
5
+ (runs.subscribeToRun and the @trigger.dev/react-hooks hook useRealtimeRun),
6
+ consume metadata and AI/text streams in React (useRealtimeStream), trigger
7
+ tasks from the browser (useTaskTrigger, useRealtimeTaskTrigger), and mint
8
+ scoped frontend credentials with auth.createPublicToken /
9
+ auth.createTriggerPublicToken.
10
+ Load when wiring a frontend (React/Next.js/Remix) or backend-for-frontend to
11
+ show live run progress, status badges, token streams, trigger buttons, or
12
+ wait-token approval UIs. NOT for writing the backend task itself (streams.define
13
+ / metadata.set is trigger-authoring-tasks territory); this is the consumer side.
14
+ type: core
15
+ library: trigger.dev
16
+ sources:
17
+ - docs/realtime/overview.mdx
18
+ - docs/realtime/how-it-works.mdx
19
+ - docs/realtime/auth.mdx
20
+ - docs/realtime/run-object.mdx
21
+ - docs/realtime/react-hooks/overview.mdx
22
+ - docs/realtime/react-hooks/subscribe.mdx
23
+ - docs/realtime/react-hooks/triggering.mdx
24
+ - docs/realtime/react-hooks/streams.mdx
25
+ - docs/realtime/react-hooks/swr.mdx
26
+ - docs/realtime/react-hooks/use-wait-token.mdx
27
+ - docs/realtime/backend/subscribe.mdx
28
+ ---
29
+
30
+ # Realtime and Frontend
31
+
32
+ The consumer side of Trigger.dev's run state and streams: read live run
33
+ updates, render AI/text streams, and trigger tasks from a browser. Hooks come
34
+ from `@trigger.dev/react-hooks`; token minting and backend subscription come
35
+ from `@trigger.dev/sdk`.
36
+
37
+ ## Setup
38
+
39
+ ```bash
40
+ npm add @trigger.dev/react-hooks # frontend hooks (React/Next.js/Remix)
41
+ # @trigger.dev/sdk is already installed for the backend
42
+ ```
43
+
44
+ The flow is always: mint a scoped token in the backend, pass it to the
45
+ frontend, subscribe with a hook.
46
+
47
+ ```ts
48
+ // backend (API route / server action)
49
+ import { auth } from "@trigger.dev/sdk";
50
+
51
+ const publicAccessToken = await auth.createPublicToken({
52
+ scopes: { read: { runs: ["run_1234"] } }, // a token with no scopes is useless
53
+ });
54
+ ```
55
+
56
+ ```tsx
57
+ // frontend
58
+ "use client";
59
+ import { useRealtimeRun } from "@trigger.dev/react-hooks";
60
+
61
+ export function RunStatus({ runId, publicAccessToken }: { runId: string; publicAccessToken: string }) {
62
+ const { run, error } = useRealtimeRun(runId, { accessToken: publicAccessToken });
63
+ if (error) return <div>Error: {error.message}</div>;
64
+ if (!run) return <div>Loading...</div>;
65
+ return <div>Run: {run.status}</div>;
66
+ }
67
+ ```
68
+
69
+ There are two token kinds: Public Access Tokens (read/subscribe, from
70
+ `auth.createPublicToken`) and Trigger Tokens (trigger-from-browser, single-use,
71
+ from `auth.createTriggerPublicToken`). Both default to a 15 minute expiry.
72
+
73
+ ## Core patterns
74
+
75
+ ### 1. Subscribe to a run and render metadata progress
76
+
77
+ `metadata` is `Record<string, DeserializedJson>`, so nested values need a cast.
78
+
79
+ ```tsx
80
+ "use client";
81
+ import { useRealtimeRun } from "@trigger.dev/react-hooks";
82
+ import type { myTask } from "@/trigger/myTask";
83
+
84
+ export function Progress({ runId, publicAccessToken }: { runId: string; publicAccessToken: string }) {
85
+ const { run, error } = useRealtimeRun<typeof myTask>(runId, { accessToken: publicAccessToken });
86
+ if (error) return <div>Error: {error.message}</div>;
87
+ if (!run) return <div>Loading...</div>;
88
+ const progress = run.metadata?.progress as { percentage?: number } | undefined;
89
+ return <div>{run.status}: {progress?.percentage ?? 0}%</div>;
90
+ }
91
+ ```
92
+
93
+ Pass `onComplete: (run, error) => {}` to react when the run finishes.
94
+
95
+ ### 2. Status-only subscription with `skipColumns`
96
+
97
+ For a badge or progress bar you do not need `payload`/`output`. Skipping them
98
+ reduces wire size and avoids "Large HTTP Payload" warnings.
99
+
100
+ ```tsx
101
+ const { run } = useRealtimeRun(runId, {
102
+ accessToken: publicAccessToken,
103
+ skipColumns: ["payload", "output"],
104
+ });
105
+ ```
106
+
107
+ You can skip any of: `payload`, `output`, `metadata`, `startedAt`, `delayUntil`,
108
+ `queuedAt`, `expiredAt`, `completedAt`, `number`, `isTest`, `usageDurationMs`,
109
+ `costInCents`, `baseCostInCents`, `ttl`, `payloadType`, `outputType`, `runTags`,
110
+ `error`.
111
+
112
+ ### 3. Trigger from the browser with a Trigger Token
113
+
114
+ `accessToken` here is a Trigger Token (`auth.createTriggerPublicToken`), not a
115
+ Public Access Token.
116
+
117
+ ```tsx
118
+ "use client";
119
+ import { useTaskTrigger } from "@trigger.dev/react-hooks";
120
+ import type { myTask } from "@/trigger/myTask";
121
+
122
+ export function TriggerButton({ triggerToken }: { triggerToken: string }) {
123
+ const { submit, handle, isLoading } = useTaskTrigger<typeof myTask>("my-task", {
124
+ accessToken: triggerToken,
125
+ });
126
+ if (handle) return <div>Run ID: {handle.id}</div>;
127
+ return (
128
+ <button onClick={() => submit({ foo: "bar" }, { tags: ["user:123"] })} disabled={isLoading}>
129
+ {isLoading ? "Triggering..." : "Run"}
130
+ </button>
131
+ );
132
+ }
133
+ ```
134
+
135
+ `submit(payload, options?)` takes the same options as a backend `trigger` call.
136
+
137
+ ### 4. Trigger and subscribe in one hook
138
+
139
+ ```tsx
140
+ "use client";
141
+ import { useRealtimeTaskTrigger } from "@trigger.dev/react-hooks";
142
+ import type { myTask } from "@/trigger/myTask";
143
+
144
+ export function Runner({ publicAccessToken }: { publicAccessToken: string }) {
145
+ const { submit, run, isLoading } = useRealtimeTaskTrigger<typeof myTask>("my-task", {
146
+ accessToken: publicAccessToken,
147
+ });
148
+ if (run) return <div>{run.status}</div>;
149
+ return <button onClick={() => submit({ foo: "bar" })} disabled={isLoading}>Run</button>;
150
+ }
151
+ ```
152
+
153
+ Use `useRealtimeTaskTriggerWithStreams<typeof myTask, STREAMS>` when you also
154
+ want the task's streams (it returns `{ submit, run, streams, error, isLoading }`).
155
+
156
+ ### 5. Consume an AI/text stream (SDK 4.1.0+, recommended)
157
+
158
+ `useRealtimeStream` takes a defined stream for full type safety, or a `runId`
159
+ plus optional stream key. Returns `{ parts, error }`.
160
+
161
+ ```tsx
162
+ "use client";
163
+ import { useRealtimeStream } from "@trigger.dev/react-hooks";
164
+ import { aiStream } from "@/trigger/streams"; // a defined stream -> typed parts
165
+
166
+ export function StreamView({ runId, publicAccessToken }: { runId: string; publicAccessToken: string }) {
167
+ const { parts, error } = useRealtimeStream(aiStream, runId, {
168
+ accessToken: publicAccessToken,
169
+ timeoutInSeconds: 300, // default 60
170
+ onData: (chunk) => console.log(chunk),
171
+ });
172
+ if (error) return <div>Error: {error.message}</div>;
173
+ if (!parts) return <div>Loading...</div>;
174
+ return <div>{parts.join("")}</div>;
175
+ }
176
+ ```
177
+
178
+ Without a defined stream: `useRealtimeStream<string>(runId, "ai-output", { accessToken })`,
179
+ or omit the key to use the default stream. Other options: `baseURL`, `startIndex`,
180
+ `throttleInMs` (default 16). The legacy `useRealtimeRunWithStreams(runId, options)`
181
+ hook is still supported when you need both the run and all its streams at once.
182
+
183
+ ### 6. Send input back into a running task
184
+
185
+ ```tsx
186
+ "use client";
187
+ import { useInputStreamSend } from "@trigger.dev/react-hooks";
188
+ import { approval } from "@/trigger/streams";
189
+
190
+ export function ApprovalForm({ runId, accessToken }: { runId: string; accessToken: string }) {
191
+ const { send, isLoading, isReady } = useInputStreamSend(approval.id, runId, { accessToken });
192
+ return (
193
+ <button disabled={!isReady || isLoading} onClick={() => send({ approved: true })}>
194
+ Approve
195
+ </button>
196
+ );
197
+ }
198
+ ```
199
+
200
+ ### 7. Complete a wait token from React
201
+
202
+ ```ts
203
+ // backend: create the token, return id + publicAccessToken to the frontend
204
+ import { wait } from "@trigger.dev/sdk";
205
+ const token = await wait.createToken({ timeout: "10m" });
206
+ return { tokenId: token.id, publicToken: token.publicAccessToken };
207
+ ```
208
+
209
+ ```tsx
210
+ "use client";
211
+ import { useWaitToken } from "@trigger.dev/react-hooks";
212
+
213
+ export function Approve({ tokenId, publicToken }: { tokenId: string; publicToken: string }) {
214
+ const { complete } = useWaitToken(tokenId, { accessToken: publicToken });
215
+ return <button onClick={() => complete({ approved: true })}>Approve</button>;
216
+ }
217
+ ```
218
+
219
+ ### 8. Subscribe from the backend (async iterators)
220
+
221
+ ```ts
222
+ import { runs, tasks } from "@trigger.dev/sdk";
223
+ import type { myTask } from "./trigger/my-task";
224
+
225
+ const handle = await tasks.trigger("my-task", { some: "data" });
226
+ for await (const run of runs.subscribeToRun<typeof myTask>(handle.id)) {
227
+ console.log(run.payload.some, run.output?.some); // typed
228
+ }
229
+ ```
230
+
231
+ `runs.subscribeToRun` completes when the run finishes, so the loop exits on its own.
232
+
233
+ ## Common mistakes
234
+
235
+ 1. **CRITICAL: Triggering from the browser with a Public Access Token.** The
236
+ read token from `createPublicToken` cannot trigger tasks.
237
+ - Wrong: `useTaskTrigger("my-task", { accessToken: publicAccessTokenFromCreatePublicToken })`
238
+ - Correct: mint a single-use Trigger Token with `auth.createTriggerPublicToken("my-task")` and pass that.
239
+
240
+ 2. **Token with no scopes.** A scopeless token authorizes nothing, so every subscribe 403s.
241
+ - Wrong: `await auth.createPublicToken()`
242
+ - Correct: `await auth.createPublicToken({ scopes: { read: { runs: ["run_1234"] } } })`
243
+
244
+ 3. **Polling with `useRun`/SWR for live updates.** `useRun` is the SWR-based
245
+ management-API hook (not recommended for live state); set `refreshInterval: 0`
246
+ to stop polling if you do use it.
247
+ - Wrong: `useRun(runId, { refreshInterval: 1000 })` to track progress
248
+ - Correct: `useRealtimeRun(runId, { accessToken })` (no polling, no WebSocket setup)
249
+
250
+ 4. **Forgetting `"use client"`.** Realtime/trigger hooks cannot run in a server component.
251
+ - Wrong: a Next.js App Router server component using `useRealtimeRun`
252
+ - Correct: put `"use client";` at the top of any component using these hooks.
253
+
254
+ 5. **Shipping `payload`/`output` you do not render.**
255
+ - Wrong: `useRealtimeRun(runId, { accessToken })` for a status badge (large payloads over the wire)
256
+ - Correct: `useRealtimeRun(runId, { accessToken, skipColumns: ["payload", "output"] })`
257
+
258
+ 6. **Subscribing before the handle exists.**
259
+ - Wrong: `useRealtimeRun(handle, { accessToken: handle?.publicAccessToken })` with no guard
260
+ - Correct: add `enabled: !!handle` so it subscribes only once the trigger returns a handle.
261
+
262
+ ## References
263
+
264
+ Sibling skills:
265
+ - `trigger-authoring-tasks` for the task side: `streams.define()`, `metadata.set()`, and `wait.createToken`.
266
+ - `trigger-authoring-chat-agent` and `trigger-chat-agent-advanced` for chat agents, which build on these realtime streams.
267
+
268
+ Reference docs ship beside this skill in the same package, read them locally (no network), pinned to your installed version. The `sources:` frontmatter above lists every doc this skill draws from, all under `@trigger.dev/sdk/docs/`. Start with:
269
+ - `@trigger.dev/sdk/docs/realtime/react-hooks/subscribe.mdx`
270
+ - `@trigger.dev/sdk/docs/realtime/react-hooks/streams.mdx`
271
+ - `@trigger.dev/sdk/docs/realtime/auth.mdx`
272
+ - `@trigger.dev/sdk/docs/realtime/run-object.mdx` (the realtime run object differs from the management-API object returned by `useRun`)
273
+
274
+ ## Version
275
+
276
+ This skill is bundled inside `@trigger.dev/sdk` and read directly from `node_modules`, so it always matches your installed SDK version (see the adjacent `package.json`). The full documentation for these APIs ships alongside it under `@trigger.dev/sdk/docs/`.