-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(sdk): type chat.createStartSessionAction against your chat agent #3684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ericallam
merged 3 commits into
main
from
feat/chat-start-session-action-typed-client-data
May 21, 2026
+195
−17
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
3050c89
feat(sdk): type chat.createStartSessionAction against your chat agent
ericallam 2b0c24d
test(sdk): cover createStartSessionAction generic + clientData fold
ericallam c087368
test(sdk): fix expectTypeOf assertion shape for createStartSessionAction
ericallam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --- | ||
| "@trigger.dev/sdk": patch | ||
| --- | ||
|
|
||
| Type `chat.createStartSessionAction` against your chat agent so `clientData` is typed end-to-end on the first turn: | ||
|
|
||
| ```ts | ||
| import { chat } from "@trigger.dev/sdk/ai"; | ||
| import type { myChat } from "@/trigger/chat"; | ||
|
|
||
| export const startChatSession = chat.createStartSessionAction<typeof myChat>("my-chat"); | ||
|
|
||
| // In the browser, threaded from the transport's typed startSession callback: | ||
| const transport = useTriggerChatTransport<typeof myChat>({ | ||
| task: "my-chat", | ||
| startSession: ({ chatId, clientData }) => | ||
| startChatSession({ chatId, clientData }), | ||
| // ... | ||
| }); | ||
| ``` | ||
|
|
||
| `ChatStartSessionParams` gains a typed `clientData` field — folded into the first run's `payload.metadata` so `onPreload` / `onChatStart` see the same shape per-turn `metadata` carries via the transport. The opaque session-level `metadata` field is unchanged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
packages/trigger-sdk/src/v3/createStartSessionAction.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { afterEach, describe, expect, expectTypeOf, it } from "vitest"; | ||
| import { z } from "zod"; | ||
| import type { CreateSessionRequestBody, CreatedSessionResponseBody } from "@trigger.dev/core/v3"; | ||
|
|
||
| import { chat } from "./ai.js"; | ||
| import { | ||
| __setSessionStartImplForTests, | ||
| __setSessionOpenImplForTests, | ||
| SessionHandle, | ||
| } from "./sessions.js"; | ||
| import { apiClientManager } from "@trigger.dev/core/v3"; | ||
|
|
||
| // `auth.createPublicToken` is called by the action when no start token is | ||
| // supplied. Provide a minimal API client config so the mint path doesn't | ||
| // throw before we get to assert the captured request body. | ||
| apiClientManager.setGlobalAPIClientConfiguration({ | ||
| baseURL: "https://example.invalid", | ||
| accessToken: "tr_test_secret", | ||
| }); | ||
|
|
||
| // Capture the request body the action would send to `sessions.start()`. | ||
| let lastStartBody: CreateSessionRequestBody | undefined; | ||
|
|
||
| function installStartFixture() { | ||
| __setSessionStartImplForTests(async (body): Promise<CreatedSessionResponseBody> => { | ||
| lastStartBody = body; | ||
| return { | ||
| id: "session_fixture", | ||
| externalId: body.externalId ?? null, | ||
| type: body.type, | ||
| taskIdentifier: body.taskIdentifier, | ||
| triggerConfig: body.triggerConfig, | ||
| currentRunId: "run_fixture", | ||
| tags: body.triggerConfig.tags ?? [], | ||
| metadata: body.metadata ?? null, | ||
| closedAt: null, | ||
| closedReason: null, | ||
| expiresAt: null, | ||
| createdAt: new Date(), | ||
| updatedAt: new Date(), | ||
| runId: "run_fixture", | ||
| publicAccessToken: "tr_pat_fixture", | ||
| isCached: false, | ||
| }; | ||
| }); | ||
| __setSessionOpenImplForTests(() => new SessionHandle("session_fixture")); | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| __setSessionStartImplForTests(undefined); | ||
| __setSessionOpenImplForTests(undefined); | ||
| lastStartBody = undefined; | ||
| }); | ||
|
|
||
| // Build a fake chat agent task shape that the generic can narrow against. | ||
| // We only need the static type — the runtime never invokes this task because | ||
| // `__setSessionStartImplForTests` intercepts the network call. | ||
| const fakeChat = chat | ||
| .withClientData({ | ||
| schema: z.object({ | ||
| userId: z.string(), | ||
| plan: z.enum(["free", "pro"]), | ||
| }), | ||
| }) | ||
| .agent({ | ||
| id: "fake-chat", | ||
| run: async () => undefined as any, | ||
| }); | ||
|
|
||
| describe("chat.createStartSessionAction — runtime", () => { | ||
| it("folds typed clientData into basePayload.metadata so onChatStart sees it on the first turn", async () => { | ||
| installStartFixture(); | ||
|
|
||
| const start = chat.createStartSessionAction<typeof fakeChat>("fake-chat"); | ||
|
|
||
| const result = await start({ | ||
| chatId: "chat-1", | ||
| clientData: { userId: "u-1", plan: "pro" }, | ||
| }); | ||
|
|
||
| expect(result.publicAccessToken).toBe("tr_pat_fixture"); | ||
| expect(lastStartBody?.triggerConfig.basePayload).toMatchObject({ | ||
| messages: [], | ||
| trigger: "preload", | ||
| metadata: { userId: "u-1", plan: "pro" }, | ||
| chatId: "chat-1", | ||
| }); | ||
| }); | ||
|
|
||
| it("leaves basePayload.metadata unset when clientData is not provided", async () => { | ||
| installStartFixture(); | ||
|
|
||
| const start = chat.createStartSessionAction("fake-chat"); | ||
| await start({ chatId: "chat-2" }); | ||
|
|
||
| expect(lastStartBody?.triggerConfig.basePayload).not.toHaveProperty("metadata"); | ||
| }); | ||
|
|
||
| it("keeps session-level metadata distinct from per-turn clientData", async () => { | ||
| installStartFixture(); | ||
|
|
||
| const start = chat.createStartSessionAction<typeof fakeChat>("fake-chat"); | ||
| await start({ | ||
| chatId: "chat-3", | ||
| clientData: { userId: "u-3", plan: "free" }, | ||
| metadata: { source: "marketing-site" }, | ||
| }); | ||
|
|
||
| // Per-turn shape (visible to onPreload / onChatStart): | ||
| expect(lastStartBody?.triggerConfig.basePayload).toMatchObject({ | ||
| metadata: { userId: "u-3", plan: "free" }, | ||
| }); | ||
| // Session-row metadata (opaque, never typed via clientDataSchema): | ||
| expect(lastStartBody?.metadata).toEqual({ source: "marketing-site" }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("chat.createStartSessionAction — types", () => { | ||
| it("narrows clientData against the chat agent's clientDataSchema", () => { | ||
| const start = chat.createStartSessionAction<typeof fakeChat>("fake-chat"); | ||
|
|
||
| // The clientData field is typed off the agent's schema. | ||
| expectTypeOf<Parameters<typeof start>[0]["clientData"]>().toEqualTypeOf< | ||
| { userId: string; plan: "free" | "pro" } | undefined | ||
| >(); | ||
| // The agent's typed clientData is strictly narrower than `unknown`. | ||
| expectTypeOf<Parameters<typeof start>[0]["clientData"]>().not.toEqualTypeOf<unknown>(); | ||
| }); | ||
|
|
||
| it("defaults clientData to unknown when called without a generic", () => { | ||
| const start = chat.createStartSessionAction("fake-chat"); | ||
| expectTypeOf(start).parameter(0).toHaveProperty("clientData"); | ||
| // Untyped variant — clientData is `unknown`. | ||
| expectTypeOf<Parameters<typeof start>[0]["clientData"]>().toEqualTypeOf<unknown>(); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.