Commit 9ff410b
authored
feat(sdk): type chat.createStartSessionAction against your chat agent (#3684)
## Summary
Type `chat.createStartSessionAction` against the chat agent so
`clientData` is typed end-to-end on the first turn. Closes the gap where
`useTriggerChatTransport`'s `startSession` callback already hands you a
typed `clientData` (via the transport generic) but the server-side
action couldn't accept it without untyped routing through the `metadata`
field.
## Design
`ChatStartSessionParams` gains a typed `clientData` field via the new
generic:
```ts
export type ChatStartSessionParams<TChat extends AnyTask = AnyTask> = {
chatId: string;
clientData?: InferChatClientData<TChat>;
triggerConfig?: Partial<SessionTriggerConfig>;
metadata?: Record<string, unknown>;
};
function createChatStartSessionAction<TChat extends AnyTask = AnyTask>(
taskId: string,
options?: CreateChatStartSessionActionOptions
): (params: ChatStartSessionParams<TChat>) => Promise<ChatStartSessionResult>
```
When provided, `clientData` is folded into the first run's
`triggerConfig.basePayload.metadata`, so `onPreload` / `onChatStart` see
the same shape per-turn `metadata` carries via the transport. The opaque
session-level `metadata` field stays exactly as before — it lands on the
Session row, not the run payload.
## Usage
```ts
// actions.ts
import { chat } from "@trigger.dev/sdk/ai";
import type { myChat } from "@/trigger/chat";
export const startChatSession = chat.createStartSessionAction<typeof myChat>("my-chat");
```
```tsx
// Chat.tsx
const transport = useTriggerChatTransport<typeof myChat>({
task: "my-chat",
accessToken: ({ chatId }) => mintChatAccessToken(chatId),
startSession: ({ chatId, clientData }) =>
startChatSession({ chatId, clientData }),
});
```
## Test plan
- [x] `pnpm run build --filter @trigger.dev/sdk` passes
- [ ] Verify a `chat.agent` with `clientDataSchema` reads the typed
clientData from `onPreload` payload metadata on the first turn1 parent d343727 commit 9ff410b
3 files changed
Lines changed: 195 additions & 17 deletions
File tree
- .changeset
- packages/trigger-sdk/src/v3
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9040 | 9040 | | |
9041 | 9041 | | |
9042 | 9042 | | |
9043 | | - | |
| 9043 | + | |
9044 | 9044 | | |
9045 | 9045 | | |
| 9046 | + | |
| 9047 | + | |
| 9048 | + | |
| 9049 | + | |
| 9050 | + | |
| 9051 | + | |
| 9052 | + | |
| 9053 | + | |
9046 | 9054 | | |
9047 | 9055 | | |
9048 | 9056 | | |
9049 | 9057 | | |
9050 | 9058 | | |
9051 | 9059 | | |
9052 | 9060 | | |
9053 | | - | |
| 9061 | + | |
| 9062 | + | |
| 9063 | + | |
| 9064 | + | |
| 9065 | + | |
9054 | 9066 | | |
9055 | 9067 | | |
9056 | 9068 | | |
| |||
9078 | 9090 | | |
9079 | 9091 | | |
9080 | 9092 | | |
| 9093 | + | |
| 9094 | + | |
| 9095 | + | |
9081 | 9096 | | |
9082 | 9097 | | |
9083 | 9098 | | |
9084 | 9099 | | |
9085 | 9100 | | |
| 9101 | + | |
9086 | 9102 | | |
9087 | | - | |
9088 | | - | |
9089 | | - | |
| 9103 | + | |
| 9104 | + | |
| 9105 | + | |
| 9106 | + | |
9090 | 9107 | | |
9091 | 9108 | | |
9092 | | - | |
| 9109 | + | |
9093 | 9110 | | |
9094 | | - | |
| 9111 | + | |
9095 | 9112 | | |
9096 | | - | |
9097 | | - | |
9098 | | - | |
9099 | | - | |
| 9113 | + | |
| 9114 | + | |
| 9115 | + | |
9100 | 9116 | | |
9101 | 9117 | | |
9102 | 9118 | | |
9103 | | - | |
| 9119 | + | |
9104 | 9120 | | |
9105 | 9121 | | |
9106 | | - | |
9107 | | - | |
| 9122 | + | |
| 9123 | + | |
9108 | 9124 | | |
9109 | 9125 | | |
9110 | 9126 | | |
| |||
9117 | 9133 | | |
9118 | 9134 | | |
9119 | 9135 | | |
9120 | | - | |
9121 | | - | |
9122 | | - | |
| 9136 | + | |
| 9137 | + | |
| 9138 | + | |
9123 | 9139 | | |
9124 | 9140 | | |
9125 | 9141 | | |
9126 | 9142 | | |
9127 | 9143 | | |
9128 | 9144 | | |
| 9145 | + | |
| 9146 | + | |
| 9147 | + | |
9129 | 9148 | | |
9130 | 9149 | | |
9131 | 9150 | | |
9132 | 9151 | | |
9133 | 9152 | | |
9134 | 9153 | | |
| 9154 | + | |
9135 | 9155 | | |
9136 | 9156 | | |
9137 | 9157 | | |
| |||
Lines changed: 136 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
0 commit comments