@trigger.dev/sdk 4.5.0-rc.5 → 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.
- package/dist/commonjs/v3/ai.d.ts +178 -5
- package/dist/commonjs/v3/ai.js +603 -119
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat-client.js +3 -0
- package/dist/commonjs/v3/chat-client.js.map +1 -1
- package/dist/commonjs/v3/chat-react.js +10 -7
- package/dist/commonjs/v3/chat-react.js.map +1 -1
- package/dist/commonjs/v3/chat-server.d.ts +8 -0
- package/dist/commonjs/v3/chat-server.js +32 -10
- package/dist/commonjs/v3/chat-server.js.map +1 -1
- package/dist/commonjs/v3/chat-server.test.js +51 -0
- package/dist/commonjs/v3/chat-server.test.js.map +1 -1
- package/dist/commonjs/v3/chat.js +34 -6
- package/dist/commonjs/v3/chat.js.map +1 -1
- package/dist/commonjs/v3/chat.test.js +53 -0
- package/dist/commonjs/v3/chat.test.js.map +1 -1
- package/dist/commonjs/v3/createStartSessionAction.test.js +30 -0
- package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -1
- package/dist/commonjs/v3/sessions.d.ts +11 -6
- package/dist/commonjs/v3/sessions.js +10 -5
- package/dist/commonjs/v3/sessions.js.map +1 -1
- package/dist/commonjs/v3/test/mock-chat-agent.d.ts +6 -0
- package/dist/commonjs/v3/test/mock-chat-agent.js +1 -0
- package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +178 -5
- package/dist/esm/v3/ai.js +603 -120
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat-client.js +3 -0
- package/dist/esm/v3/chat-client.js.map +1 -1
- package/dist/esm/v3/chat-react.js +10 -7
- package/dist/esm/v3/chat-react.js.map +1 -1
- package/dist/esm/v3/chat-server.d.ts +8 -0
- package/dist/esm/v3/chat-server.js +32 -10
- package/dist/esm/v3/chat-server.js.map +1 -1
- package/dist/esm/v3/chat-server.test.js +51 -0
- package/dist/esm/v3/chat-server.test.js.map +1 -1
- package/dist/esm/v3/chat.js +34 -6
- package/dist/esm/v3/chat.js.map +1 -1
- package/dist/esm/v3/chat.test.js +53 -0
- package/dist/esm/v3/chat.test.js.map +1 -1
- package/dist/esm/v3/createStartSessionAction.test.js +30 -0
- package/dist/esm/v3/createStartSessionAction.test.js.map +1 -1
- package/dist/esm/v3/sessions.d.ts +11 -6
- package/dist/esm/v3/sessions.js +10 -5
- package/dist/esm/v3/sessions.js.map +1 -1
- package/dist/esm/v3/test/mock-chat-agent.d.ts +6 -0
- package/dist/esm/v3/test/mock-chat-agent.js +1 -0
- package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/docs/ai/prompts.mdx +430 -0
- package/docs/ai-chat/actions.mdx +115 -0
- package/docs/ai-chat/anatomy.mdx +71 -0
- package/docs/ai-chat/backend.mdx +817 -0
- package/docs/ai-chat/background-injection.mdx +221 -0
- package/docs/ai-chat/changelog.mdx +850 -0
- package/docs/ai-chat/chat-local.mdx +174 -0
- package/docs/ai-chat/client-protocol.mdx +1081 -0
- package/docs/ai-chat/compaction.mdx +411 -0
- package/docs/ai-chat/custom-agents.mdx +364 -0
- package/docs/ai-chat/error-handling.mdx +415 -0
- package/docs/ai-chat/fast-starts.mdx +672 -0
- package/docs/ai-chat/frontend.mdx +580 -0
- package/docs/ai-chat/how-it-works.mdx +230 -0
- package/docs/ai-chat/lifecycle-hooks.mdx +530 -0
- package/docs/ai-chat/mcp.mdx +101 -0
- package/docs/ai-chat/overview.mdx +90 -0
- package/docs/ai-chat/patterns/branching-conversations.mdx +284 -0
- package/docs/ai-chat/patterns/code-sandbox.mdx +126 -0
- package/docs/ai-chat/patterns/database-persistence.mdx +414 -0
- package/docs/ai-chat/patterns/human-in-the-loop.mdx +275 -0
- package/docs/ai-chat/patterns/large-payloads.mdx +169 -0
- package/docs/ai-chat/patterns/oom-resilience.mdx +120 -0
- package/docs/ai-chat/patterns/persistence-and-replay.mdx +211 -0
- package/docs/ai-chat/patterns/recovery-boot.mdx +230 -0
- package/docs/ai-chat/patterns/skills.mdx +221 -0
- package/docs/ai-chat/patterns/sub-agents.mdx +383 -0
- package/docs/ai-chat/patterns/tool-result-auditing.mdx +148 -0
- package/docs/ai-chat/patterns/trusted-edge-signals.mdx +337 -0
- package/docs/ai-chat/patterns/version-upgrades.mdx +172 -0
- package/docs/ai-chat/pending-messages.mdx +343 -0
- package/docs/ai-chat/prompt-caching.mdx +206 -0
- package/docs/ai-chat/quick-start.mdx +161 -0
- package/docs/ai-chat/reference.mdx +909 -0
- package/docs/ai-chat/server-chat.mdx +263 -0
- package/docs/ai-chat/sessions.mdx +333 -0
- package/docs/ai-chat/testing.mdx +682 -0
- package/docs/ai-chat/tools.mdx +191 -0
- package/docs/ai-chat/types.mdx +242 -0
- package/docs/ai-chat/upgrade-guide.mdx +515 -0
- package/docs/apikeys.mdx +54 -0
- package/docs/building-with-ai.mdx +261 -0
- package/docs/bulk-actions.mdx +49 -0
- package/docs/changelog.mdx +6 -0
- package/docs/cli-deploy-commands.mdx +9 -0
- package/docs/cli-dev-commands.mdx +9 -0
- package/docs/cli-dev.mdx +8 -0
- package/docs/cli-init-commands.mdx +58 -0
- package/docs/cli-introduction.mdx +25 -0
- package/docs/cli-list-profiles-commands.mdx +42 -0
- package/docs/cli-login-commands.mdx +33 -0
- package/docs/cli-logout-commands.mdx +33 -0
- package/docs/cli-preview-archive.mdx +59 -0
- package/docs/cli-promote-commands.mdx +9 -0
- package/docs/cli-switch.mdx +43 -0
- package/docs/cli-update-commands.mdx +42 -0
- package/docs/cli-whoami-commands.mdx +33 -0
- package/docs/community.mdx +6 -0
- package/docs/config/config-file.mdx +602 -0
- package/docs/config/extensions/additionalFiles.mdx +38 -0
- package/docs/config/extensions/additionalPackages.mdx +40 -0
- package/docs/config/extensions/aptGet.mdx +34 -0
- package/docs/config/extensions/audioWaveform.mdx +20 -0
- package/docs/config/extensions/custom.mdx +380 -0
- package/docs/config/extensions/emitDecoratorMetadata.mdx +29 -0
- package/docs/config/extensions/esbuildPlugin.mdx +31 -0
- package/docs/config/extensions/ffmpeg.mdx +45 -0
- package/docs/config/extensions/lightpanda.mdx +56 -0
- package/docs/config/extensions/overview.mdx +67 -0
- package/docs/config/extensions/playwright.mdx +195 -0
- package/docs/config/extensions/prismaExtension.mdx +1014 -0
- package/docs/config/extensions/puppeteer.mdx +30 -0
- package/docs/config/extensions/pythonExtension.mdx +182 -0
- package/docs/config/extensions/syncEnvVars.mdx +291 -0
- package/docs/context.mdx +235 -0
- package/docs/database-connections.mdx +213 -0
- package/docs/deploy-environment-variables.mdx +435 -0
- package/docs/deployment/atomic-deployment.mdx +172 -0
- package/docs/deployment/overview.mdx +257 -0
- package/docs/deployment/preview-branches.mdx +224 -0
- package/docs/errors-retrying.mdx +379 -0
- package/docs/github-actions.mdx +222 -0
- package/docs/github-integration.mdx +136 -0
- package/docs/github-repo.mdx +8 -0
- package/docs/help-email.mdx +6 -0
- package/docs/help-slack.mdx +11 -0
- package/docs/hidden-tasks.mdx +56 -0
- package/docs/how-it-works.mdx +454 -0
- package/docs/how-to-reduce-your-spend.mdx +217 -0
- package/docs/idempotency.mdx +504 -0
- package/docs/introduction.mdx +223 -0
- package/docs/limits.mdx +241 -0
- package/docs/logging.mdx +195 -0
- package/docs/machines.mdx +952 -0
- package/docs/manual-setup.mdx +632 -0
- package/docs/mcp-agent-rules.mdx +41 -0
- package/docs/mcp-introduction.mdx +385 -0
- package/docs/mcp-tools.mdx +273 -0
- package/docs/migrating-from-v3.mdx +334 -0
- package/docs/observability/dashboards.mdx +102 -0
- package/docs/observability/query.mdx +585 -0
- package/docs/open-source-contributing.mdx +16 -0
- package/docs/open-source-self-hosting.mdx +541 -0
- package/docs/private-networking/aws-console-setup.mdx +304 -0
- package/docs/private-networking/overview.mdx +144 -0
- package/docs/private-networking/troubleshooting.mdx +78 -0
- package/docs/queue-concurrency.mdx +354 -0
- package/docs/quick-start.mdx +97 -0
- package/docs/realtime/auth.mdx +208 -0
- package/docs/realtime/backend/overview.mdx +45 -0
- package/docs/realtime/backend/streams.mdx +418 -0
- package/docs/realtime/backend/subscribe.mdx +225 -0
- package/docs/realtime/how-it-works.mdx +94 -0
- package/docs/realtime/overview.mdx +63 -0
- package/docs/realtime/react-hooks/overview.mdx +73 -0
- package/docs/realtime/react-hooks/streams.mdx +449 -0
- package/docs/realtime/react-hooks/subscribe.mdx +674 -0
- package/docs/realtime/react-hooks/swr.mdx +87 -0
- package/docs/realtime/react-hooks/triggering.mdx +194 -0
- package/docs/realtime/react-hooks/use-wait-token.mdx +34 -0
- package/docs/realtime/run-object.mdx +174 -0
- package/docs/replaying.mdx +72 -0
- package/docs/request-feature.mdx +6 -0
- package/docs/roadmap.mdx +6 -0
- package/docs/run-tests.mdx +20 -0
- package/docs/run-usage.mdx +113 -0
- package/docs/runs/heartbeats.mdx +38 -0
- package/docs/runs/max-duration.mdx +139 -0
- package/docs/runs/metadata.mdx +734 -0
- package/docs/runs/priority.mdx +31 -0
- package/docs/runs.mdx +396 -0
- package/docs/self-hosting/docker.mdx +458 -0
- package/docs/self-hosting/env/supervisor.mdx +74 -0
- package/docs/self-hosting/env/webapp.mdx +276 -0
- package/docs/self-hosting/kubernetes.mdx +601 -0
- package/docs/self-hosting/overview.mdx +108 -0
- package/docs/skills.mdx +85 -0
- package/docs/tags.mdx +120 -0
- package/docs/tasks/overview.mdx +697 -0
- package/docs/tasks/scheduled.mdx +382 -0
- package/docs/tasks/schemaTask.mdx +413 -0
- package/docs/tasks/streams.mdx +884 -0
- package/docs/triggering.mdx +1320 -0
- package/docs/troubleshooting-alerts.mdx +385 -0
- package/docs/troubleshooting-debugging-in-vscode.mdx +8 -0
- package/docs/troubleshooting-github-issues.mdx +6 -0
- package/docs/troubleshooting-uptime-status.mdx +6 -0
- package/docs/troubleshooting.mdx +398 -0
- package/docs/upgrading-packages.mdx +80 -0
- package/docs/vercel-integration.mdx +207 -0
- package/docs/versioning.mdx +56 -0
- package/docs/video-walkthrough.mdx +23 -0
- package/docs/wait-for-token.mdx +540 -0
- package/docs/wait-for.mdx +42 -0
- package/docs/wait-until.mdx +53 -0
- package/docs/wait.mdx +18 -0
- package/docs/writing-tasks-introduction.mdx +33 -0
- package/package.json +10 -6
- package/skills/trigger-authoring-chat-agent/SKILL.md +296 -0
- package/skills/trigger-authoring-tasks/SKILL.md +254 -0
- package/skills/trigger-chat-agent-advanced/SKILL.md +368 -0
- package/skills/trigger-cost-savings/SKILL.md +116 -0
- package/skills/trigger-realtime-and-frontend/SKILL.md +276 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Versioning"
|
|
3
|
+
description: "We use atomic versioning to ensure that started tasks are not affected by changes to the task code."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
A version is a bundle of tasks at a certain point in time.
|
|
7
|
+
|
|
8
|
+
## Version identifiers
|
|
9
|
+
|
|
10
|
+
Version identifiers look like this:
|
|
11
|
+
|
|
12
|
+
- `20240313.1` - March 13th, 2024, version 1
|
|
13
|
+
- `20240313.2` - March 13th, 2024, version 2
|
|
14
|
+
- `20240314.1` - March 14th, 2024, version 1
|
|
15
|
+
|
|
16
|
+
You can see there are two parts to the version identifier:
|
|
17
|
+
|
|
18
|
+
- The date (in reverse format)
|
|
19
|
+
- The version number
|
|
20
|
+
|
|
21
|
+
Versions numbers are incremented each time a new version is created for that date and environment. So it's possible to have `20240313.1` in both the `dev` and `prod` environments.
|
|
22
|
+
|
|
23
|
+
## Version locking
|
|
24
|
+
|
|
25
|
+
When a task run starts it is locked to the latest version of the code (for that environment). Once locked it won't change versions, even if you deploy new versions. This is to ensure that a task run is not affected by changes to the code.
|
|
26
|
+
|
|
27
|
+
Delayed runs are locked to the version that's active when they begin executing, not when they're enqueued.
|
|
28
|
+
|
|
29
|
+
### Child tasks and version locking
|
|
30
|
+
|
|
31
|
+
Trigger and wait functions version lock child task runs to the parent task run version. This ensures the results from child runs match what the parent task is expecting. If you don't wait then version locking doesn't apply.
|
|
32
|
+
|
|
33
|
+
| Trigger function | Parent task version | Child task version | isLocked |
|
|
34
|
+
| ----------------------- | ------------------- | ------------------ | -------- |
|
|
35
|
+
| `trigger()` | `20240313.2` | Latest | No |
|
|
36
|
+
| `batchTrigger()` | `20240313.2` | Latest | No |
|
|
37
|
+
| `triggerAndWait()` | `20240313.2` | `20240313.2` | Yes |
|
|
38
|
+
| `batchTriggerAndWait()` | `20240313.2` | `20240313.2` | Yes |
|
|
39
|
+
|
|
40
|
+
## Local development
|
|
41
|
+
|
|
42
|
+
When running the local server (using `npx trigger.dev dev`), every relevant code change automatically creates a new version of all tasks.
|
|
43
|
+
|
|
44
|
+
So a task run will continue running on the version it was locked to. We do this by spawning a new process for each task run. This ensures that the task run is not affected by changes to the code.
|
|
45
|
+
|
|
46
|
+
## Deployment
|
|
47
|
+
|
|
48
|
+
Every deployment creates a new version of all tasks for that environment.
|
|
49
|
+
|
|
50
|
+
## Retries and reattempts
|
|
51
|
+
|
|
52
|
+
When a task has an uncaught error it will [retry](/errors-retrying), assuming you have not set `maxAttempts` to 0. Retries are locked to the original version of the run.
|
|
53
|
+
|
|
54
|
+
## Replays
|
|
55
|
+
|
|
56
|
+
A "replay" is a new run of a task that uses the same inputs but will use the latest version of the code. This is useful when you fix a bug and want to re-run a task with the same inputs. See [replaying](/replaying) for more information.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Video walkthrough"
|
|
3
|
+
description: "Go from zero to a working task in your Next.js app in 10 minutes."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
{" "}
|
|
7
|
+
<iframe
|
|
8
|
+
width="100%"
|
|
9
|
+
height="315"
|
|
10
|
+
src="https://www.youtube.com/embed/YH_4c0K7fGM?si=5JzZmZseuqI5aciM"
|
|
11
|
+
title="Trigger.dev walkthrough"
|
|
12
|
+
allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
|
13
|
+
referrerPolicy="strict-origin-when-cross-origin"
|
|
14
|
+
allowFullScreen
|
|
15
|
+
/>
|
|
16
|
+
|
|
17
|
+
### In this video we cover the following topics:
|
|
18
|
+
|
|
19
|
+
- [0:00](https://youtu.be/YH_4c0K7fGM?si=J8svVzotZtyTXDap&t=0) – [Install Trigger.dev](/quick-start) in an existing Next.js project
|
|
20
|
+
- [1:44](https://youtu.be/YH_4c0K7fGM?si=J8svVzotZtyTXDap&t=104) – [Run and test](/run-tests) the "Hello, world!" example project
|
|
21
|
+
- [2:09](https://youtu.be/YH_4c0K7fGM?si=FMTP8ep_cDBCU0_x&t=128) – Create and run an AI image generation task that uses [Fal.ai](https://fal.ai) – ([View the code](/guides/examples/fal-ai-image-to-cartoon))
|
|
22
|
+
- [6:25](https://youtu.be/YH_4c0K7fGM?si=pPc8iLI2Y9FGD3yo&t=385) – Create and run a [Realtime](/realtime/overview) example using [React hooks](/realtime/react-hooks) – ([View the code](/guides/examples/fal-ai-realtime))
|
|
23
|
+
- [11:10](https://youtu.be/YH_4c0K7fGM?si=Mjd0EvvNsNlVouvY&t=670) – [Deploy your task](/cli-deploy) to the Trigger.dev Cloud
|
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Wait for token"
|
|
3
|
+
description: "Wait until a token is completed using waitpoint tokens."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Waitpoint tokens pause task runs until you complete the token. They're commonly used for approval workflows and other scenarios where you need to wait for external confirmation, such as human-in-the-loop processes.
|
|
7
|
+
|
|
8
|
+
You can complete a token using the SDK or by making a POST request to the token's URL.
|
|
9
|
+
|
|
10
|
+
<Note>
|
|
11
|
+
If you're waiting for data from an [input stream](/tasks/streams#input-streams), use
|
|
12
|
+
[`inputStream.wait()`](/tasks/streams#wait--suspend-until-data-arrives) instead — it uses
|
|
13
|
+
waitpoint tokens internally but provides a simpler API with full type safety from your stream
|
|
14
|
+
definition.
|
|
15
|
+
</Note>
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
To get started using wait tokens, you need to first create a token using the `wait.createToken` function:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { wait } from "@trigger.dev/sdk";
|
|
23
|
+
|
|
24
|
+
// This can be called anywhere in your codebase, either in a task or in your backend code
|
|
25
|
+
const token = await wait.createToken({
|
|
26
|
+
timeout: "10m", // you can optionally specify a timeout for the token
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Once you have a token, you can wait for it to be completed using the `wait.forToken` function:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { wait } from "@trigger.dev/sdk";
|
|
34
|
+
|
|
35
|
+
type ApprovalToken = {
|
|
36
|
+
status: "approved" | "rejected";
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// This must be called inside a task run function
|
|
40
|
+
const result = await wait.forToken<ApprovalToken>(tokenId);
|
|
41
|
+
|
|
42
|
+
if (result.ok) {
|
|
43
|
+
console.log("Token completed", result.output.status); // "approved" or "rejected"
|
|
44
|
+
} else {
|
|
45
|
+
console.log("Token timed out", result.error);
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
To complete a token, you can use the `wait.completeToken` function:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { wait } from "@trigger.dev/sdk";
|
|
53
|
+
// This can be called anywhere in your codebase, or from an external service,
|
|
54
|
+
// passing in the token ID and the output of the token
|
|
55
|
+
await wait.completeToken<ApprovalToken>(tokenId, {
|
|
56
|
+
status: "approved",
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Completing from the browser
|
|
61
|
+
|
|
62
|
+
The `publicAccessToken` returned by `wait.createToken()` is scoped to that specific waitpoint and intended for client-side completion. The completion endpoint has CORS enabled, so you can call it directly from client-side code without proxying through your backend.
|
|
63
|
+
|
|
64
|
+
<Steps>
|
|
65
|
+
<Step title="Create the token in your backend">
|
|
66
|
+
```typescript
|
|
67
|
+
import { wait } from "@trigger.dev/sdk";
|
|
68
|
+
|
|
69
|
+
const token = await wait.createToken({ timeout: "10m" });
|
|
70
|
+
// Pass token.id and token.publicAccessToken to your frontend
|
|
71
|
+
```
|
|
72
|
+
</Step>
|
|
73
|
+
<Step title="Complete the token from the browser">
|
|
74
|
+
```typescript
|
|
75
|
+
// tokenId and publicAccessToken passed from your backend
|
|
76
|
+
const tokenId = token.id;
|
|
77
|
+
const publicAccessToken = token.publicAccessToken;
|
|
78
|
+
|
|
79
|
+
const response = await fetch(
|
|
80
|
+
`https://api.trigger.dev/api/v1/waitpoints/tokens/${tokenId}/complete`,
|
|
81
|
+
{
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
"Authorization": `Bearer ${publicAccessToken}`,
|
|
85
|
+
"Content-Type": "application/json",
|
|
86
|
+
},
|
|
87
|
+
body: JSON.stringify({ data: { status: "approved" } }),
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
if (!response.ok) {
|
|
92
|
+
throw new Error(`Failed to complete token: ${response.statusText}`);
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
</Step>
|
|
96
|
+
</Steps>
|
|
97
|
+
|
|
98
|
+
## Completing via webhook callback
|
|
99
|
+
|
|
100
|
+
<Warning>
|
|
101
|
+
The `token.url` webhook callback URL is designed for server-to-server use and does **not** have
|
|
102
|
+
CORS headers. Don't call it from the browser, use the [Completing from the
|
|
103
|
+
browser](#completing-from-the-browser) pattern instead.
|
|
104
|
+
</Warning>
|
|
105
|
+
|
|
106
|
+
Or you can make an HTTP POST request to the `url` it returns. This is an HTTP callback:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import { wait } from "@trigger.dev/sdk";
|
|
110
|
+
|
|
111
|
+
const token = await wait.createToken({
|
|
112
|
+
timeout: "10m",
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const call = await replicate.predictions.create({
|
|
116
|
+
version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
|
|
117
|
+
input: {
|
|
118
|
+
prompt: "A painting of a cat by Andy Warhol",
|
|
119
|
+
},
|
|
120
|
+
// pass the provided URL to Replicate's webhook, so they can "callback"
|
|
121
|
+
webhook: token.url,
|
|
122
|
+
webhook_events_filter: ["completed"],
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const prediction = await wait.forToken<Prediction>(token).unwrap();
|
|
126
|
+
// unwrap() throws a timeout error or returns the result 👆
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## wait.createToken
|
|
130
|
+
|
|
131
|
+
Create a waitpoint token.
|
|
132
|
+
|
|
133
|
+
### options
|
|
134
|
+
|
|
135
|
+
The `createToken` function accepts an object with the following properties:
|
|
136
|
+
|
|
137
|
+
<ParamField query="timeout" type="string" optional>
|
|
138
|
+
The maximum amount of time to wait for the token to be completed. Defaults to "10m".
|
|
139
|
+
</ParamField>
|
|
140
|
+
|
|
141
|
+
<ParamField query="idempotencyKey" type="string" optional>
|
|
142
|
+
An idempotency key for the token. If provided, the token will be completed with the same payload
|
|
143
|
+
if the same idempotency key is used again.
|
|
144
|
+
</ParamField>
|
|
145
|
+
|
|
146
|
+
<ParamField query="idempotencyKeyTTL" type="string" optional>
|
|
147
|
+
The time to live for the idempotency key. Defaults to "1h".
|
|
148
|
+
</ParamField>
|
|
149
|
+
|
|
150
|
+
<ParamField query="tags" type="string[]" optional>
|
|
151
|
+
Tags to attach to the token. Tags can be used to filter waitpoints in the dashboard.
|
|
152
|
+
</ParamField>
|
|
153
|
+
|
|
154
|
+
### returns
|
|
155
|
+
|
|
156
|
+
The `createToken` function returns a token object with the following properties:
|
|
157
|
+
|
|
158
|
+
<ParamField query="id" type="string">
|
|
159
|
+
The ID of the token. Starts with `waitpoint_`.
|
|
160
|
+
</ParamField>
|
|
161
|
+
|
|
162
|
+
<ParamField query="url" type="string">
|
|
163
|
+
The URL of the token. This is the URL you can make a POST request to in order to complete the token.
|
|
164
|
+
|
|
165
|
+
The JSON body of the POST request will be used as the output of the token. If there's no body the output will be an empty object `{}`.
|
|
166
|
+
|
|
167
|
+
</ParamField>
|
|
168
|
+
|
|
169
|
+
<ParamField query="isCached" type="boolean">
|
|
170
|
+
Whether the token is cached. Will return true if the token was created with an idempotency key and
|
|
171
|
+
the same idempotency key was used again.
|
|
172
|
+
</ParamField>
|
|
173
|
+
|
|
174
|
+
<ParamField query="publicAccessToken" type="string">
|
|
175
|
+
A Public Access Token that can be used to complete the token from a client-side application (or
|
|
176
|
+
another backend). See our [Realtime docs](/realtime/auth) for more details.
|
|
177
|
+
</ParamField>
|
|
178
|
+
|
|
179
|
+
### Example
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
import { wait } from "@trigger.dev/sdk";
|
|
183
|
+
|
|
184
|
+
const token = await wait.createToken({
|
|
185
|
+
timeout: "10m",
|
|
186
|
+
idempotencyKey: "my-idempotency-key",
|
|
187
|
+
tags: ["my-tag"],
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## wait.completeToken
|
|
192
|
+
|
|
193
|
+
Complete a waitpoint token.
|
|
194
|
+
|
|
195
|
+
### parameters
|
|
196
|
+
|
|
197
|
+
<ParamField query="id" type="string">
|
|
198
|
+
The ID of the token to complete.
|
|
199
|
+
</ParamField>
|
|
200
|
+
|
|
201
|
+
<ParamField query="output" type="any">
|
|
202
|
+
The data to complete the token with.
|
|
203
|
+
</ParamField>
|
|
204
|
+
|
|
205
|
+
### returns
|
|
206
|
+
|
|
207
|
+
The `completeToken` function returns an object with the following properties:
|
|
208
|
+
|
|
209
|
+
<ParamField query="success" type="boolean">
|
|
210
|
+
Whether the token was completed successfully.
|
|
211
|
+
</ParamField>
|
|
212
|
+
|
|
213
|
+
### Example
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { wait } from "@trigger.dev/sdk";
|
|
217
|
+
|
|
218
|
+
await wait.completeToken<ApprovalToken>(tokenId, {
|
|
219
|
+
status: "approved",
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### From another language
|
|
224
|
+
|
|
225
|
+
You can complete a token using a raw HTTP request or from another language.
|
|
226
|
+
|
|
227
|
+
<CodeGroup>
|
|
228
|
+
|
|
229
|
+
```bash curl
|
|
230
|
+
curl -X POST "https://api.trigger.dev/api/v1/waitpoints/tokens/{tokenId}/complete" \
|
|
231
|
+
-H "Authorization: Bearer {token}" \
|
|
232
|
+
-H "Content-Type: application/json" \
|
|
233
|
+
-d '{"data": { "status": "approved"}}'
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
```python python
|
|
237
|
+
import requests
|
|
238
|
+
|
|
239
|
+
response = requests.post(
|
|
240
|
+
"https://api.trigger.dev/api/v1/waitpoints/tokens/{tokenId}/complete",
|
|
241
|
+
headers={"Authorization": f"Bearer {token}"},
|
|
242
|
+
json={"data": { "status": "approved"}}
|
|
243
|
+
)
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
```ruby ruby
|
|
247
|
+
require "net/http"
|
|
248
|
+
|
|
249
|
+
uri = URI("https://api.trigger.dev/api/v1/waitpoints/tokens/{tokenId}/complete")
|
|
250
|
+
|
|
251
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
252
|
+
request = Net::HTTP::Post.new(uri)
|
|
253
|
+
request["Authorization"] = "Bearer {token}"
|
|
254
|
+
request["Content-Type"] = "application/json"
|
|
255
|
+
request.body = JSON.generate({ data: { status: "approved" } })
|
|
256
|
+
|
|
257
|
+
response = http.request(request)
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
```go go
|
|
261
|
+
package main
|
|
262
|
+
|
|
263
|
+
import (
|
|
264
|
+
"bytes"
|
|
265
|
+
"encoding/json"
|
|
266
|
+
"fmt"
|
|
267
|
+
"net/http"
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
func main() {
|
|
271
|
+
url := "https://api.trigger.dev/api/v1/waitpoints/tokens/{tokenId}/complete"
|
|
272
|
+
|
|
273
|
+
payload := map[string]interface{}{
|
|
274
|
+
"data": map[string]interface{}{
|
|
275
|
+
"status": "approved",
|
|
276
|
+
},
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
jsonData, err := json.Marshal(payload)
|
|
280
|
+
if err != nil {
|
|
281
|
+
fmt.Println("Error marshalling payload:", err)
|
|
282
|
+
return
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
|
286
|
+
if err != nil {
|
|
287
|
+
fmt.Println("Error creating request:", err)
|
|
288
|
+
return
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
req.Header.Set("Authorization", "Bearer {token}")
|
|
292
|
+
req.Header.Set("Content-Type", "application/json")
|
|
293
|
+
|
|
294
|
+
client := &http.Client{}
|
|
295
|
+
resp, err := client.Do(req)
|
|
296
|
+
if err != nil {
|
|
297
|
+
fmt.Println("Error sending request:", err)
|
|
298
|
+
return
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
defer resp.Body.Close()
|
|
302
|
+
|
|
303
|
+
fmt.Println("Response status:", resp.Status)
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
</CodeGroup>
|
|
308
|
+
|
|
309
|
+
## wait.forToken
|
|
310
|
+
|
|
311
|
+
Wait for a token to be completed.
|
|
312
|
+
|
|
313
|
+
### parameters
|
|
314
|
+
|
|
315
|
+
<ParamField query="token" type="string | { id: string }">
|
|
316
|
+
The token to wait for.
|
|
317
|
+
</ParamField>
|
|
318
|
+
|
|
319
|
+
### returns
|
|
320
|
+
|
|
321
|
+
The `forToken` function returns a result object with the following properties:
|
|
322
|
+
|
|
323
|
+
<ParamField query="ok" type="boolean">
|
|
324
|
+
Whether the token was completed successfully.
|
|
325
|
+
</ParamField>
|
|
326
|
+
|
|
327
|
+
<ParamField query="output" type="any">
|
|
328
|
+
If `ok` is `true`, this will be the output of the token.
|
|
329
|
+
</ParamField>
|
|
330
|
+
|
|
331
|
+
<ParamField query="error" type="Error">
|
|
332
|
+
If `ok` is `false`, this will be the error that occurred. The only error that can occur is a
|
|
333
|
+
timeout error.
|
|
334
|
+
</ParamField>
|
|
335
|
+
|
|
336
|
+
### unwrap()
|
|
337
|
+
|
|
338
|
+
We provide a handy `.unwrap()` method that will throw an error if the result is not ok. This means your happy path is a lot cleaner.
|
|
339
|
+
|
|
340
|
+
```ts
|
|
341
|
+
const approval = await wait.forToken<ApprovalToken>(tokenId).unwrap();
|
|
342
|
+
// unwrap means an error will throw if the waitpoint times out 👆
|
|
343
|
+
|
|
344
|
+
// This is the actual data you sent to the token now, not a result object
|
|
345
|
+
console.log("Approval", approval);
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
### Example
|
|
349
|
+
|
|
350
|
+
```ts
|
|
351
|
+
import { wait } from "@trigger.dev/sdk";
|
|
352
|
+
|
|
353
|
+
const result = await wait.forToken<ApprovalToken>(tokenId);
|
|
354
|
+
|
|
355
|
+
if (result.ok) {
|
|
356
|
+
console.log("Token completed", result.output.status); // "approved" or "rejected"
|
|
357
|
+
} else {
|
|
358
|
+
console.log("Token timed out", result.error);
|
|
359
|
+
}
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
## wait.listTokens
|
|
363
|
+
|
|
364
|
+
List all tokens for an environment.
|
|
365
|
+
|
|
366
|
+
### parameters
|
|
367
|
+
|
|
368
|
+
The `listTokens` function accepts an object with the following properties:
|
|
369
|
+
|
|
370
|
+
<ParamField query="status" type="string | string[]" optional>
|
|
371
|
+
Statuses to filter by. Can be one or more of: `WAITING`, `COMPLETED`, `TIMED_OUT`.
|
|
372
|
+
</ParamField>
|
|
373
|
+
|
|
374
|
+
<ParamField query="idempotencyKey" type="string" optional>
|
|
375
|
+
The idempotency key to filter by.
|
|
376
|
+
</ParamField>
|
|
377
|
+
|
|
378
|
+
<ParamField query="tags" type="string | string[]" optional>
|
|
379
|
+
Tags to filter by.
|
|
380
|
+
</ParamField>
|
|
381
|
+
|
|
382
|
+
<ParamField query="period" type="string" optional>
|
|
383
|
+
The period to filter by. Can be one of: `1h`, `1d`, `7d`, `30d`.
|
|
384
|
+
</ParamField>
|
|
385
|
+
|
|
386
|
+
<ParamField query="from" type="Date | number" optional>
|
|
387
|
+
The start date to filter by.
|
|
388
|
+
</ParamField>
|
|
389
|
+
|
|
390
|
+
<ParamField query="to" type="Date | number" optional>
|
|
391
|
+
The end date to filter by.
|
|
392
|
+
</ParamField>
|
|
393
|
+
|
|
394
|
+
### returns
|
|
395
|
+
|
|
396
|
+
The `listTokens` function returns a list of tokens that can be iterated over using a for-await-of loop.
|
|
397
|
+
|
|
398
|
+
Each token is an object with the following properties:
|
|
399
|
+
|
|
400
|
+
<ParamField query="id" type="string">
|
|
401
|
+
The ID of the token.
|
|
402
|
+
</ParamField>
|
|
403
|
+
|
|
404
|
+
<ParamField query="url" type="string">
|
|
405
|
+
The URL of the token. This is the URL you can make a POST request to in order to complete the token.
|
|
406
|
+
|
|
407
|
+
The JSON body of the POST request will be used as the output of the token. If there's no body the output will be an empty object `{}`.
|
|
408
|
+
|
|
409
|
+
</ParamField>
|
|
410
|
+
|
|
411
|
+
<ParamField query="status" type="string">
|
|
412
|
+
The status of the token.
|
|
413
|
+
</ParamField>
|
|
414
|
+
|
|
415
|
+
<ParamField query="completedAt" type="Date">
|
|
416
|
+
The date and time the token was completed.
|
|
417
|
+
</ParamField>
|
|
418
|
+
|
|
419
|
+
<ParamField query="timeoutAt" type="Date">
|
|
420
|
+
The date and time the token will timeout.
|
|
421
|
+
</ParamField>
|
|
422
|
+
|
|
423
|
+
<ParamField query="idempotencyKey" type="string">
|
|
424
|
+
The idempotency key of the token.
|
|
425
|
+
</ParamField>
|
|
426
|
+
|
|
427
|
+
<ParamField query="idempotencyKeyExpiresAt" type="Date">
|
|
428
|
+
The date and time the idempotency key will expire.
|
|
429
|
+
</ParamField>
|
|
430
|
+
|
|
431
|
+
<ParamField query="tags" type="string[]">
|
|
432
|
+
The tags of the token.
|
|
433
|
+
</ParamField>
|
|
434
|
+
|
|
435
|
+
<ParamField query="createdAt" type="Date">
|
|
436
|
+
The date and time the token was created.
|
|
437
|
+
</ParamField>
|
|
438
|
+
|
|
439
|
+
<Note>
|
|
440
|
+
The output of the token is not included in the list. To get the output, you need to retrieve the
|
|
441
|
+
token using the `wait.retrieveToken` function.
|
|
442
|
+
</Note>
|
|
443
|
+
|
|
444
|
+
### Example
|
|
445
|
+
|
|
446
|
+
```ts
|
|
447
|
+
import { wait } from "@trigger.dev/sdk";
|
|
448
|
+
|
|
449
|
+
const tokens = await wait.listTokens({
|
|
450
|
+
status: "COMPLETED",
|
|
451
|
+
tags: ["user:123"],
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
for await (const token of tokens) {
|
|
455
|
+
console.log(token);
|
|
456
|
+
}
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
## wait.retrieveToken
|
|
460
|
+
|
|
461
|
+
Retrieve a token by ID.
|
|
462
|
+
|
|
463
|
+
### parameters
|
|
464
|
+
|
|
465
|
+
<ParamField query="id" type="string">
|
|
466
|
+
The ID of the token to retrieve.
|
|
467
|
+
</ParamField>
|
|
468
|
+
|
|
469
|
+
### returns
|
|
470
|
+
|
|
471
|
+
The `retrieveToken` function returns a token object with the following properties:
|
|
472
|
+
|
|
473
|
+
<ParamField query="id" type="string">
|
|
474
|
+
The ID of the token.
|
|
475
|
+
</ParamField>
|
|
476
|
+
|
|
477
|
+
<ParamField query="url" type="string">
|
|
478
|
+
The URL of the token. This is the URL you can make a POST request to in order to complete the token.
|
|
479
|
+
|
|
480
|
+
The JSON body of the POST request will be used as the output of the token. If there's no body the output will be an empty object `{}`.
|
|
481
|
+
|
|
482
|
+
</ParamField>
|
|
483
|
+
|
|
484
|
+
<ParamField query="status" type="string">
|
|
485
|
+
The status of the token.
|
|
486
|
+
</ParamField>
|
|
487
|
+
|
|
488
|
+
<ParamField query="completedAt" type="Date">
|
|
489
|
+
The date and time the token was completed.
|
|
490
|
+
</ParamField>
|
|
491
|
+
|
|
492
|
+
<ParamField query="timeoutAt" type="Date">
|
|
493
|
+
The date and time the token will timeout.
|
|
494
|
+
</ParamField>
|
|
495
|
+
|
|
496
|
+
<ParamField query="idempotencyKey" type="string">
|
|
497
|
+
The idempotency key of the token.
|
|
498
|
+
</ParamField>
|
|
499
|
+
|
|
500
|
+
<ParamField query="idempotencyKeyExpiresAt" type="Date">
|
|
501
|
+
The date and time the idempotency key will expire.
|
|
502
|
+
</ParamField>
|
|
503
|
+
|
|
504
|
+
<ParamField query="tags" type="string[]">
|
|
505
|
+
The tags of the token.
|
|
506
|
+
</ParamField>
|
|
507
|
+
|
|
508
|
+
<ParamField query="createdAt" type="Date">
|
|
509
|
+
The date and time the token was created.
|
|
510
|
+
</ParamField>
|
|
511
|
+
|
|
512
|
+
<ParamField query="output" type="any">
|
|
513
|
+
The output of the token.
|
|
514
|
+
</ParamField>
|
|
515
|
+
|
|
516
|
+
<ParamField query="error" type="Error">
|
|
517
|
+
The error that occurred.
|
|
518
|
+
</ParamField>
|
|
519
|
+
|
|
520
|
+
### Example
|
|
521
|
+
|
|
522
|
+
```ts
|
|
523
|
+
import { wait } from "@trigger.dev/sdk";
|
|
524
|
+
|
|
525
|
+
const token = await wait.retrieveToken(tokenId);
|
|
526
|
+
|
|
527
|
+
console.log(token);
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
## Wait idempotency
|
|
531
|
+
|
|
532
|
+
You can pass an idempotency key to any wait function, allowing you to skip waits if the same idempotency key is used again. This can be useful if you want to skip waits when retrying a task, for example:
|
|
533
|
+
|
|
534
|
+
```ts
|
|
535
|
+
// Specify the idempotency key and TTL when creating a wait token
|
|
536
|
+
const token = await wait.createToken({
|
|
537
|
+
idempotencyKey: "my-idempotency-key",
|
|
538
|
+
idempotencyKeyTTL: "1h",
|
|
539
|
+
});
|
|
540
|
+
```
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Wait for"
|
|
3
|
+
description: "Wait for a period of time, then continue execution."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PausedExecutionFree from "/snippets/paused-execution-free.mdx"
|
|
7
|
+
|
|
8
|
+
Inside your tasks you can wait for a period of time before you want execution to continue.
|
|
9
|
+
|
|
10
|
+
```ts /trigger/long-task.ts
|
|
11
|
+
export const veryLongTask = task({
|
|
12
|
+
id: "very-long-task",
|
|
13
|
+
run: async (payload) => {
|
|
14
|
+
await wait.for({ seconds: 5 });
|
|
15
|
+
|
|
16
|
+
await wait.for({ minutes: 10 });
|
|
17
|
+
|
|
18
|
+
await wait.for({ hours: 1 });
|
|
19
|
+
|
|
20
|
+
await wait.for({ days: 1 });
|
|
21
|
+
|
|
22
|
+
await wait.for({ weeks: 1 });
|
|
23
|
+
|
|
24
|
+
await wait.for({ months: 1 });
|
|
25
|
+
|
|
26
|
+
await wait.for({ years: 1 });
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
This allows you to write linear code without having to worry about the complexity of scheduling or managing cron jobs.
|
|
32
|
+
|
|
33
|
+
<PausedExecutionFree />
|
|
34
|
+
|
|
35
|
+
## Wait idempotency
|
|
36
|
+
|
|
37
|
+
You can pass an idempotency key to any wait function, allowing you to skip waits if the same idempotency key is used again. This can be useful if you want to skip waits when retrying a task, for example:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
// Specify the idempotency key and TTL when waiting for a duration:
|
|
41
|
+
await wait.for({ seconds: 10 }, { idempotencyKey: "my-idempotency-key", idempotencyKeyTTL: "1h" });
|
|
42
|
+
```
|