Skip to content

Commit 57f1339

Browse files
committed
fix: update context handling in ChatClient and API route
1 parent 10480d7 commit 57f1339

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

examples/ts-react-chat/src/routes/api.tanchat.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const Route = createFileRoute('/api/tanchat')({
5959
const abortController = new AbortController()
6060

6161
const body = await request.json()
62-
const { messages, data, context } = body
62+
const { messages, data } = body
6363

6464
// Extract provider, model, and conversationId from data
6565
const provider: Provider = data?.provider || 'openai'
@@ -97,6 +97,13 @@ export const Route = createFileRoute('/api/tanchat')({
9797
`[API Route] Using provider: ${provider}, model: ${selectedModel}`,
9898
)
9999

100+
// Server-side context (e.g., database connections, user session)
101+
// This is separate from client context and only used for server tools
102+
const serverContext = {
103+
// Add server-side context here if needed
104+
// e.g., db, userId from session, etc.
105+
}
106+
100107
const stream = chat({
101108
adapter: adapter as any,
102109
model: selectedModel as any,
@@ -112,7 +119,7 @@ export const Route = createFileRoute('/api/tanchat')({
112119
messages,
113120
abortController,
114121
conversationId,
115-
...(context !== undefined && { context }),
122+
context: serverContext,
116123
})
117124
return toStreamResponse(stream, { abortController })
118125
} catch (error: any) {

packages/typescript/ai-client/src/chat-client.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,10 @@ export class ChatClient<
310310
// Call onResponse callback
311311
await this.callbacksRef.current.onResponse()
312312

313-
// Include conversationId and context in the body for server-side event correlation
313+
// Include conversationId in the body for server-side event correlation
314314
const bodyWithConversationId = {
315315
...this.body,
316316
conversationId: this.uniqueId,
317-
...(this.options.context !== undefined && {
318-
context: this.options.context,
319-
}),
320317
}
321318

322319
// Connect and stream

packages/typescript/ai-client/src/types.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,19 @@ export interface ChatClientOptions<
211211
}
212212

213213
/**
214-
* Context object that is automatically passed to all tool execute functions.
214+
* Context object that is automatically passed to client-side tool execute functions.
215215
*
216-
* This allows tools to access shared context (like user ID, local storage,
216+
* This allows client tools to access shared context (like user ID, local storage,
217217
* browser APIs, etc.) without needing to capture them via closures.
218-
* Works for both client and server tools.
219218
*
220-
* The context is also sent to the server in the request body, so server tools
221-
* can access the same context.
219+
* Note: This context is only used for client-side tools. Server tools should receive
220+
* their own context from the server-side chat() function.
222221
*
223222
* @example
224223
* const client = new ChatClient({
225224
* connection: fetchServerSentEvents('/api/chat'),
226225
* context: { userId: '123', localStorage },
227-
* tools: [getUserData],
226+
* tools: [clientTool],
228227
* });
229228
*/
230229
context?: TContext

packages/typescript/ai-client/tests/chat-client.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ describe('ChatClient', () => {
667667
expect(mockStorage.setItem).toHaveBeenCalledWith('pref_123_theme', 'dark')
668668
})
669669

670-
it('should send context to server in request body', async () => {
670+
it('should not send context to server (context is only for client tools)', async () => {
671671
const testContext = {
672672
userId: '123',
673673
sessionId: 'session-456',
@@ -688,9 +688,9 @@ describe('ChatClient', () => {
688688

689689
await client.sendMessage('Hello')
690690

691-
// Context should be in the request body
691+
// Context should NOT be in the request body (only used for client tools)
692692
expect(capturedBody).toBeDefined()
693-
expect(capturedBody.context).toEqual(testContext)
693+
expect(capturedBody.context).toBeUndefined()
694694
})
695695

696696
it('should work without context (context is optional)', async () => {

0 commit comments

Comments
 (0)