Skip to content

Commit 610ed01

Browse files
committed
Wrap metadata with type safetey
1 parent 5a5d20c commit 610ed01

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ npx trigger.dev@latest dev
3232
### Relevant Files
3333

3434
- `src/trigger/tasks.ts`: Where our `generateFunctionDocs` background task is defined.
35+
- `src/lib/metadataStore.ts`: Wraps the run metadata with type-safe access.
3536
- `src/app/page.tsx`: The main page that invokes the server action function that triggers the background task.
3637
- `src/app/actions.ts`: The server action function that triggers the background task and redirects to `/runs/[id]`.
3738
- `src/app/runs/[id]/page.tsx`: The page that displays the status of the background task and the result when it's done.

src/app/hooks/useGenerateFunctionDocs.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { ExampleMetadataSchema } from "@/lib/schemas";
3+
import { parseStatus } from "@/lib/metadataStore";
44
import { type generateFunctionDocs } from "@/trigger/tasks";
55
import { useRealtimeRun } from "@trigger.dev/react-hooks";
66

@@ -11,6 +11,15 @@ interface GenerateFunctioDocsStatus {
1111
output?: string;
1212
}
1313

14+
/**
15+
* Hook that subscribes to the generateFunctionDocs task and returns the status and output of the task.
16+
*
17+
* Uses the `useRealtimeRun` hook to subscribe to the task.
18+
*
19+
* See more about the `useRealtimeRun` hook in the [Trigger docs](https://trigger.dev/docs/frontend/react-hooks#userealtimerun).
20+
*
21+
* @param id the run id of the generateFunctionDocs task
22+
*/
1423
export function useGenerateFunctionDocs(id: string) {
1524
const { run, error } = useRealtimeRun<typeof generateFunctionDocs>(id);
1625

@@ -23,9 +32,9 @@ export function useGenerateFunctionDocs(id: string) {
2332

2433
// Parse metadata if available
2534
if (run?.metadata) {
26-
const metadata = ExampleMetadataSchema.parse(run.metadata);
27-
status.progress = metadata.status.progress;
28-
status.label = metadata.status.label;
35+
const { progress, label } = parseStatus(run.metadata);
36+
status.progress = progress;
37+
status.label = label;
2938
}
3039

3140
return {

src/lib/metadataStore.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { metadata } from "@trigger.dev/sdk/v3";
2+
import { z } from "zod";
3+
4+
const GenerateFunctionDocsStatus = z.object({
5+
progress: z.number(),
6+
label: z.string(),
7+
});
8+
9+
type GenerateFunctionDocsStatus = z.infer<typeof GenerateFunctionDocsStatus>;
10+
11+
const GenerateFunctionDocsMetadata = z.object({
12+
status: GenerateFunctionDocsStatus,
13+
});
14+
15+
type GenerateFunctionDocsMetadata = z.infer<
16+
typeof GenerateFunctionDocsMetadata
17+
>;
18+
19+
/**
20+
* Update the status of the generate function docs task. Wraps the `metadata.set` method.
21+
*/
22+
export function updateStatus(status: GenerateFunctionDocsStatus) {
23+
// `metadata.set` can be used to update the status of the task
24+
// as long as `updateStatus` is called within the task's `run` function.
25+
metadata.set("status", status);
26+
}
27+
28+
/**
29+
* Parse the status from the metadata.
30+
*
31+
* Used by the `useGenerateFunctionDocs` hook to parse the status
32+
*/
33+
export function parseStatus(data: unknown): GenerateFunctionDocsStatus {
34+
return GenerateFunctionDocsMetadata.parse(data).status;
35+
}

src/trigger/tasks.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { metadata, schemaTask } from "@trigger.dev/sdk/v3";
22
import { setTimeout } from "timers/promises";
33
import { OpenAI } from "openai";
44
import { z } from "zod";
5+
import { updateStatus } from "@/lib/metadataStore";
56

67
const openai = new OpenAI({
78
apiKey: process.env.OPENAI_API_KEY,
@@ -15,15 +16,21 @@ export const generateFunctionDocs = schemaTask({
1516
}),
1617
maxDuration: 300, // 5 minutes
1718
run: async (payload, { ctx }) => {
18-
metadata.set("status", { progress: 0, label: "Initializing..." });
19+
updateStatus({ progress: 0, label: "Initializing..." });
1920

2021
await setTimeout(1000);
2122

22-
metadata.set("status", { progress: 19, label: "Processing data..." });
23+
updateStatus({
24+
progress: 19,
25+
label: "Processing data...",
26+
});
2327

2428
await setTimeout(1000);
2529

26-
metadata.set("status", { progress: 45, label: "Analyzing results..." });
30+
updateStatus({
31+
progress: 45,
32+
label: "Analyzing results...",
33+
});
2734

2835
await setTimeout(1000);
2936

@@ -34,7 +41,7 @@ export const generateFunctionDocs = schemaTask({
3441
model: "chatgpt-4o-latest",
3542
});
3643

37-
metadata.set("status", { progress: 85, label: "Finalizing..." });
44+
updateStatus({ progress: 85, label: "Finalizing..." });
3845

3946
await setTimeout(1000);
4047

0 commit comments

Comments
 (0)