@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,113 @@
1
+ ---
2
+ title: "Usage"
3
+ description: "Get compute duration and cost from inside a run, or for a specific block of code."
4
+ ---
5
+
6
+ ## Getting the run cost and duration
7
+
8
+ You can get the cost and duration of the current including retries of the same run.
9
+
10
+ ```ts
11
+ import { task, usage, wait } from "@trigger.dev/sdk";
12
+
13
+ export const heavyTask = task({
14
+ id: "heavy-task",
15
+ machine: {
16
+ preset: "medium-2x",
17
+ },
18
+ run: async (payload, { ctx }) => {
19
+ // Do some compute
20
+ const result = await convertVideo(payload.videoUrl);
21
+
22
+ // Get the current cost and duration up until this line of code
23
+ // This includes the compute time of the previous lines
24
+ let currentUsage = usage.getCurrent();
25
+ /* currentUsage = {
26
+ compute: {
27
+ attempt: {
28
+ costInCents: 0.01700,
29
+ durationMs: 1000,
30
+ },
31
+ total: {
32
+ costInCents: 0.0255,
33
+ durationMs: 1500,
34
+ },
35
+ },
36
+ baseCostInCents: 0.0025,
37
+ totalCostInCents: 0.028,
38
+ }
39
+ */
40
+
41
+ // In the cloud product we do not count waits towards the compute cost or duration.
42
+ // We also don't include time between attempts or before the run starts executing your code.
43
+ // So this line does not affect the cost or duration.
44
+ await wait.for({ seconds: 5 });
45
+
46
+ // This will give the same result as before the wait.
47
+ currentUsage = usage.getCurrent();
48
+
49
+ // Do more compute
50
+ const result = await convertVideo(payload.videoUrl);
51
+
52
+ // This would give a different value
53
+ currentUsage = usage.getCurrent();
54
+ },
55
+ });
56
+ ```
57
+
58
+ <Note>
59
+ In Trigger.dev cloud we do not include time between attempts, before your code executes, or waits
60
+ towards the compute cost or duration.
61
+ </Note>
62
+
63
+ ## Getting the run cost and duration from your backend
64
+
65
+ You can use [runs.retrieve()](/management/runs/retrieve) to get a single run or [runs.list()](/management/runs/list) to get a list of runs. The response will include `costInCents` `baseCostInCents` and `durationMs` fields.
66
+
67
+ ```ts single run
68
+ import { runs } from "@trigger.dev/sdk";
69
+
70
+ const run = await runs.retrieve("run-id");
71
+ console.log(run.costInCents, run.baseCostInCents, run.durationMs);
72
+ const totalCost = run.costInCents + run.baseCostInCents;
73
+ ```
74
+
75
+ ```ts multiple runs
76
+ import { runs } from "@trigger.dev/sdk";
77
+
78
+ let totalCost = 0;
79
+ for await (const run of runs.list({ tag: "user_123456" })) {
80
+ totalCost += run.costInCents + run.baseCostInCents;
81
+ console.log(run.costInCents, run.baseCostInCents, run.durationMs);
82
+ }
83
+
84
+ console.log("Total cost", totalCost);
85
+ ```
86
+
87
+ ## Getting the cost and duration of a block of code
88
+
89
+ You can also wrap code with `usage.measure` to get the cost and duration of that block of code:
90
+
91
+ ```ts
92
+ import { usage, logger } from "@trigger.dev/sdk";
93
+
94
+ // Inside a task run function, or inside a function that's called from there.
95
+ const { result, compute } = await usage.measure(async () => {
96
+ //...Do something for 1 second
97
+ return {
98
+ foo: "bar",
99
+ };
100
+ });
101
+
102
+ logger.info("Result", { result, compute });
103
+ /* result = {
104
+ foo: "bar"
105
+ }
106
+ compute = {
107
+ costInCents: 0.01700,
108
+ durationMs: 1000,
109
+ }
110
+ */
111
+ ```
112
+
113
+ This will work from inside the `run` function, our lifecycle hooks (like `onStart`, `onFailure`, `onSuccess`, etc.), or any function you're calling from the `run` function. It won't work for code that's not executed using Trigger.dev.
@@ -0,0 +1,38 @@
1
+ ---
2
+ title: "Heartbeats"
3
+ sidebarTitle: "Heartbeats"
4
+ description: "Keep long-running or CPU-heavy tasks from being marked as stalled."
5
+ ---
6
+
7
+ We send a heartbeat from your task to the platform every 30 seconds. If we don't receive a heartbeat within 5 minutes, we mark the run as stalled and stop it with a `TASK_RUN_STALLED_EXECUTING` error.
8
+
9
+ Code that blocks the event loop for too long (for example, a tight loop doing synchronous work on a large dataset) can prevent heartbeats from being sent. In that case, use `heartbeats.yield()` inside the loop so the runtime can yield to the event loop and send a heartbeat. You can call it every iteration; the implementation only yields when needed.
10
+
11
+ ```ts
12
+ import { task, heartbeats } from "@trigger.dev/sdk";
13
+
14
+ export const processLargeDataset = task({
15
+ id: "process-large-dataset",
16
+ run: async (payload: { items: string[] }) => {
17
+ for (const row of payload.items) {
18
+ await heartbeats.yield();
19
+ processRow(row);
20
+ }
21
+ return { processed: payload.items.length };
22
+ },
23
+ });
24
+
25
+ function processRow(row: string) {
26
+ // synchronous CPU-heavy work
27
+ }
28
+ ```
29
+
30
+ If you see `TASK_RUN_STALLED_EXECUTING`, see [Task run stalled executing](/troubleshooting#task-run-stalled-executing) in the troubleshooting guide.
31
+
32
+ ## Sending progress to Trigger.dev
33
+
34
+ To stream progress or status updates to the dashboard and your app, use [run metadata](/runs/metadata). Call `metadata.set()` (or `metadata.append()`) as the task runs. The dashboard and [Realtime](/realtime) (including `runs.subscribeToRun` and the React hooks) receive those updates as they happen. See [Progress monitoring](/realtime/backend/subscribe#progress-monitoring) for a full example.
35
+
36
+ ## Sending updates to your own system
37
+
38
+ Trigger.dev doesn’t push run updates to external services. To send progress or heartbeats to your own backend (for example Supabase Realtime), call your API or client from inside the task when you want to emit an update—e.g. in the same loop where you call `heartbeats.yield()` or `metadata.set()`. Use whatever your stack supports: HTTP, the Supabase client, or another SDK.
@@ -0,0 +1,139 @@
1
+ ---
2
+ title: "Max duration"
3
+ sidebarTitle: "Max duration"
4
+ description: "Set a maximum duration for a task to run."
5
+ ---
6
+
7
+ The `maxDuration` parameter sets a maximum compute time limit for tasks. When a task exceeds this duration, it will be automatically stopped. This helps prevent runaway tasks and manage compute resources effectively.
8
+
9
+ You must set a default maxDuration in your `trigger.config.ts` file, which will apply to all tasks unless overridden:
10
+
11
+ ```ts /config/trigger.config.ts
12
+ import { defineConfig } from "@trigger.dev/sdk";
13
+
14
+ export default defineConfig({
15
+ project: "proj_gtcwttqhhtlasxgfuhxs",
16
+ maxDuration: 60, // 60 seconds or 1 minute
17
+ });
18
+ ```
19
+
20
+ <Note>
21
+ The minimum maxDuration is 5 seconds. If you want to avoid timeouts, set this value to a very large number of seconds.
22
+ </Note>
23
+
24
+ You can set the `maxDuration` for a run in the following ways:
25
+
26
+ - Across all your tasks in the [config](/config/config-file#max-duration)
27
+ - On a specific task
28
+ - On a specific run when you [trigger a task](/triggering#maxduration)
29
+
30
+ ## How it works
31
+
32
+ The `maxDuration` is set in seconds, and is compared to the CPU time elapsed since the start of a single execution (which we call [attempts](/runs#attempts)) of the task. The CPU time is the time that the task has been actively running on the CPU, and does not include time spent waiting during the following:
33
+
34
+ - `wait.for` calls
35
+ - `triggerAndWait` calls
36
+ - `batchTriggerAndWait` calls
37
+
38
+ You can inspect the CPU time of a task inside the run function with our `usage` utility:
39
+
40
+ ```ts /trigger/max-duration.ts
41
+ import { task, usage } from "@trigger.dev/sdk";
42
+
43
+ export const maxDurationTask = task({
44
+ id: "max-duration-task",
45
+ maxDuration: 300, // 300 seconds or 5 minutes
46
+ run: async (payload: any, { ctx }) => {
47
+ let currentUsage = usage.getCurrent();
48
+
49
+ currentUsage.attempt.durationMs; // The CPU time in milliseconds since the start of the run
50
+ },
51
+ });
52
+ ```
53
+
54
+ The above value will be compared to the `maxDuration` you set. If the task exceeds the `maxDuration`, it will be stopped with the following error:
55
+
56
+ ![Max duration error](/runs/max-duration-error.png)
57
+
58
+ ## Configuring for a task
59
+
60
+ You can set a `maxDuration` on a specific task:
61
+
62
+ ```ts /trigger/max-duration-task.ts
63
+ import { task } from "@trigger.dev/sdk";
64
+
65
+ export const maxDurationTask = task({
66
+ id: "max-duration-task",
67
+ maxDuration: 300, // 300 seconds or 5 minutes
68
+ run: async (payload: any, { ctx }) => {
69
+ //...
70
+ },
71
+ });
72
+ ```
73
+
74
+ This will override the default `maxDuration` set in the config file. If you have a config file with a default `maxDuration` of 60 seconds, and you set a `maxDuration` of 300 seconds on a task, the task will run for 300 seconds.
75
+
76
+ You can "turn off" the Max duration set in your config file for a specific task like so:
77
+
78
+ ```ts /trigger/max-duration-task.ts
79
+ import { task, timeout } from "@trigger.dev/sdk";
80
+
81
+ export const maxDurationTask = task({
82
+ id: "max-duration-task",
83
+ maxDuration: timeout.None, // No max duration
84
+ run: async (payload: any, { ctx }) => {
85
+ //...
86
+ },
87
+ });
88
+ ```
89
+
90
+ ## Configuring for a run
91
+
92
+ You can set a `maxDuration` on a specific run when you trigger a task:
93
+
94
+ ```ts /trigger/max-duration.ts
95
+ import { maxDurationTask } from "./trigger/max-duration-task";
96
+
97
+ // Trigger the task with a maxDuration of 300 seconds
98
+ const run = await maxDurationTask.trigger(
99
+ { foo: "bar" },
100
+ {
101
+ maxDuration: 300, // 300 seconds or 5 minutes
102
+ }
103
+ );
104
+ ```
105
+
106
+ You can also set the `maxDuration` to `timeout.None` to turn off the max duration for a specific run:
107
+
108
+ ```ts /trigger/max-duration.ts
109
+ import { maxDurationTask } from "./trigger/max-duration-task";
110
+ import { timeout } from "@trigger.dev/sdk";
111
+
112
+ // Trigger the task with no maxDuration
113
+ const run = await maxDurationTask.trigger(
114
+ { foo: "bar" },
115
+ {
116
+ maxDuration: timeout.None, // No max duration
117
+ }
118
+ );
119
+ ```
120
+
121
+ ## maxDuration in run context
122
+
123
+ You can access the `maxDuration` set for a run in the run context:
124
+
125
+ ```ts /trigger/max-duration-task.ts
126
+ import { task } from "@trigger.dev/sdk";
127
+
128
+ export const maxDurationTask = task({
129
+ id: "max-duration-task",
130
+ maxDuration: 300, // 300 seconds or 5 minutes
131
+ run: async (payload: any, { ctx }) => {
132
+ console.log(ctx.run.maxDuration); // 300
133
+ },
134
+ });
135
+ ```
136
+
137
+ ## maxDuration and lifecycle functions
138
+
139
+ When a task run exceeds the `maxDuration`, the lifecycle functions `cleanup`, `onSuccess`, and `onFailure` will not be called.