Skip to content

Commit 390e53c

Browse files
Document custom payload mapping and run store options
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent 9974f22 commit 390e53c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

packages/ai/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,58 @@ import { UIMessage } from "ai";
5959
type Payload = TriggerChatTransportPayload<UIMessage>;
6060
```
6161

62+
## Custom payload mapping
63+
64+
If your task expects a custom payload shape, provide `payloadMapper`:
65+
66+
```ts
67+
import { TriggerChatTransport } from "@trigger.dev/ai";
68+
import type { UIMessage } from "ai";
69+
70+
const transport = new TriggerChatTransport<
71+
UIMessage,
72+
{ prompt: string; tenantId: string | undefined }
73+
>({
74+
task: "ai-chat-custom",
75+
accessToken: "pk_...",
76+
payloadMapper: function payloadMapper(request) {
77+
const firstPart = request.messages[0]?.parts[0];
78+
79+
return {
80+
prompt: firstPart && firstPart.type === "text" ? firstPart.text : "",
81+
tenantId:
82+
typeof request.request.body === "object" && request.request.body
83+
? (request.request.body as Record<string, string>).tenantId
84+
: undefined,
85+
};
86+
},
87+
});
88+
```
89+
90+
## Optional persistent run state
91+
92+
`TriggerChatTransport` supports custom run stores (including async implementations) to persist reconnect state:
93+
94+
```ts
95+
import type { TriggerChatRunState, TriggerChatRunStore } from "@trigger.dev/ai";
96+
97+
class MemoryStore implements TriggerChatRunStore {
98+
private runs = new Map<string, TriggerChatRunState>();
99+
100+
async get(chatId: string) {
101+
return this.runs.get(chatId);
102+
}
103+
104+
async set(state: TriggerChatRunState) {
105+
this.runs.set(state.chatId, state);
106+
}
107+
108+
async delete(chatId: string) {
109+
this.runs.delete(chatId);
110+
}
111+
}
112+
```
113+
62114
## `ai.tool(...)` example
63115

64116
```ts

0 commit comments

Comments
 (0)