-
Notifications
You must be signed in to change notification settings - Fork 1
Preserve routed context for agent.run in WebSocket callbacks
#1365
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
Open
parteeksingh24
wants to merge
4
commits into
main
Choose a base branch
from
fix/websocket-agent-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8af363b
fix(runtime): preserve websocket route context for agent.run
parteeksingh24 5aaa55b
test(runtime): reject websocket waiters on close
parteeksingh24 004336b
Merge branch 'main' into fix/websocket-agent-context
parteeksingh24 67e3698
test(runtime): avoid createApp bootstrap in websocket context test
parteeksingh24 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
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,277 @@ | ||
| import { describe, expect, test } from 'bun:test'; | ||
| import type { AuthInterface } from '@agentuity/auth/types'; | ||
| import { trace } from '@opentelemetry/api'; | ||
| import { Hono } from 'hono'; | ||
| import { websocket as bunWebsocket } from 'hono/bun'; | ||
| import { runInHTTPContext } from '../src/_context'; | ||
| import { createAgent, createAgentMiddleware } from '../src/agent'; | ||
| import { websocket } from '../src/handlers/websocket'; | ||
| import type { Logger } from '../src/logger'; | ||
| import type { Session, Thread } from '../src/session'; | ||
| import WaitUntilHandler from '../src/_waituntil'; | ||
| import { z } from 'zod'; | ||
|
|
||
| interface EchoReadyMessage { | ||
| type: 'ready'; | ||
| } | ||
|
|
||
| interface EchoSuccessMessage { | ||
| type: 'echo'; | ||
| routeSessionId: string; | ||
| routeThreadId: string; | ||
| data: { | ||
| echo: string; | ||
| sessionId: string; | ||
| threadId: string; | ||
| userId: string | null; | ||
| }; | ||
| } | ||
|
|
||
| interface EchoErrorMessage { | ||
| type: 'error'; | ||
| message: string; | ||
| } | ||
|
|
||
| type SocketMessage = EchoReadyMessage | EchoSuccessMessage | EchoErrorMessage; | ||
|
|
||
| function createMockAuth(userId: string): AuthInterface { | ||
| return { | ||
| user: { id: userId, email: `${userId}@example.com`, name: 'Test User' }, | ||
| session: { id: 'session-123', userId }, | ||
| authMethod: 'session', | ||
| raw: {}, | ||
| getUser: async () => ({ id: userId, email: `${userId}@example.com`, name: 'Test User' }), | ||
| getToken: async () => null, | ||
| getOrg: async () => null, | ||
| getOrgRole: async () => null, | ||
| hasOrgRole: async () => false, | ||
| apiKey: null, | ||
| hasPermission: () => false, | ||
| }; | ||
| } | ||
|
|
||
| function createMockLogger(): Logger { | ||
| const noop = () => {}; | ||
| return { | ||
| trace: noop, | ||
| debug: noop, | ||
| info: noop, | ||
| warn: noop, | ||
| error: noop, | ||
| fatal: noop as Logger['fatal'], | ||
| child: () => createMockLogger(), | ||
| }; | ||
| } | ||
|
|
||
| function createMockThread(id: string): Thread { | ||
| const thread: Thread = { | ||
| id, | ||
| state: new Map(), | ||
| addEventListener: () => {}, | ||
| removeEventListener: () => {}, | ||
| destroy: async () => {}, | ||
| empty: () => thread.state.size === 0, | ||
| }; | ||
| return thread; | ||
| } | ||
|
|
||
| function createMockSession(thread: Thread, id: string): Session { | ||
| return { | ||
| id, | ||
| thread, | ||
| state: new Map(), | ||
| addEventListener: () => {}, | ||
| removeEventListener: () => {}, | ||
| serializeUserData: () => undefined, | ||
| }; | ||
| } | ||
|
|
||
| function waitForOpen(socket: WebSocket): Promise<void> { | ||
| return new Promise((resolve, reject) => { | ||
| const cleanup = () => { | ||
| socket.removeEventListener('open', onOpen); | ||
| socket.removeEventListener('error', onError); | ||
| socket.removeEventListener('close', onClose); | ||
| }; | ||
|
|
||
| const onOpen = () => { | ||
| cleanup(); | ||
| resolve(); | ||
| }; | ||
| const onError = () => { | ||
| cleanup(); | ||
| reject(new Error('WebSocket failed to open')); | ||
| }; | ||
| const onClose = () => { | ||
| cleanup(); | ||
| reject(new Error('WebSocket closed before opening')); | ||
| }; | ||
|
|
||
| socket.addEventListener('open', onOpen, { once: true }); | ||
| socket.addEventListener('error', onError, { once: true }); | ||
| socket.addEventListener('close', onClose, { once: true }); | ||
| }); | ||
| } | ||
|
|
||
| function waitForJsonMessage(socket: WebSocket): Promise<SocketMessage> { | ||
| return new Promise((resolve, reject) => { | ||
| const cleanup = () => { | ||
| socket.removeEventListener('message', onMessage); | ||
| socket.removeEventListener('error', onError); | ||
| socket.removeEventListener('close', onClose); | ||
| }; | ||
|
|
||
| const onMessage = (event: MessageEvent) => { | ||
| cleanup(); | ||
| try { | ||
| resolve(JSON.parse(String(event.data)) as SocketMessage); | ||
| } catch (error) { | ||
| reject(error); | ||
| } | ||
| }; | ||
| const onError = () => { | ||
| cleanup(); | ||
| reject(new Error('WebSocket errored while waiting for a message')); | ||
| }; | ||
| const onClose = () => { | ||
| cleanup(); | ||
| reject(new Error('WebSocket closed before a message was received')); | ||
| }; | ||
|
|
||
| socket.addEventListener('message', onMessage, { once: true }); | ||
| socket.addEventListener('error', onError, { once: true }); | ||
| socket.addEventListener('close', onClose, { once: true }); | ||
| }); | ||
| } | ||
|
|
||
| async function closeSocket(socket: WebSocket): Promise<void> { | ||
| if (socket.readyState === WebSocket.CLOSED) { | ||
| return; | ||
| } | ||
|
|
||
| await new Promise<void>((resolve) => { | ||
| socket.addEventListener('close', () => resolve(), { once: true }); | ||
| socket.close(); | ||
| }); | ||
| } | ||
|
|
||
| describe('WebSocket agent context propagation', () => { | ||
| test('agent.run inside ws.onMessage preserves session, thread, and routed auth context', async () => { | ||
| const tracer = trace.getTracer('websocket-agent-context-test'); | ||
|
|
||
| const echoAgent = createAgent('websocket-agent-context-propagation-test', { | ||
| schema: { | ||
| input: z.string(), | ||
| output: z.object({ | ||
| echo: z.string(), | ||
| sessionId: z.string(), | ||
| threadId: z.string(), | ||
| userId: z.string().nullable(), | ||
| }), | ||
| }, | ||
| handler: async (ctx, input) => { | ||
| return { | ||
| echo: input, | ||
| sessionId: ctx.sessionId, | ||
| threadId: ctx.thread.id, | ||
| userId: ctx.auth?.user?.id ?? null, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const app = new Hono(); | ||
|
|
||
| app.use('*', async (c, next) => { | ||
| await runInHTTPContext(c, next); | ||
| }); | ||
|
|
||
| app.use('/api/*', async (c, next) => { | ||
| const logger = createMockLogger(); | ||
| const thread = createMockThread(`thrd_${crypto.randomUUID()}`); | ||
| const session = createMockSession(thread, `sess_${crypto.randomUUID()}`); | ||
| const waitUntilHandler = new WaitUntilHandler(tracer); | ||
|
|
||
| c.set('logger', logger); | ||
| c.set('tracer', tracer); | ||
| c.set('sessionId', session.id); | ||
| c.set('thread', thread); | ||
| c.set('session', session); | ||
| c.set('waitUntilHandler', waitUntilHandler); | ||
| c.set('agentIds', new Set<string>()); | ||
| c.set('trigger', 'websocket'); | ||
| c.set('app', {}); | ||
| await next(); | ||
| }); | ||
|
|
||
| app.use('/api/*', createAgentMiddleware('')); | ||
|
|
||
| // This middleware runs after createAgentMiddleware('') and should still | ||
| // be visible to the agent via the lazy ctx.auth getter. | ||
| app.use('/api/*', async (c, next) => { | ||
| c.set('auth', createMockAuth('late-bound-user')); | ||
| await next(); | ||
| }); | ||
|
|
||
| app.get( | ||
| '/api/echo', | ||
| websocket((c, ws) => { | ||
| ws.onOpen(() => { | ||
| ws.send(JSON.stringify({ type: 'ready' })); | ||
| }); | ||
|
|
||
| ws.onMessage(async (event) => { | ||
| try { | ||
| const result = await echoAgent.run(String(event.data)); | ||
| ws.send( | ||
| JSON.stringify({ | ||
| type: 'echo', | ||
| routeSessionId: c.var.sessionId, | ||
| routeThreadId: c.var.thread.id, | ||
| data: result, | ||
| }) | ||
| ); | ||
| } catch (error) { | ||
| ws.send( | ||
| JSON.stringify({ | ||
| type: 'error', | ||
| message: error instanceof Error ? error.message : String(error), | ||
| }) | ||
| ); | ||
| } | ||
| }); | ||
| }) | ||
| ); | ||
|
|
||
| const server = Bun.serve({ | ||
| port: 0, | ||
| fetch: (request, server) => app.fetch(request, server), | ||
| websocket: bunWebsocket, | ||
| }); | ||
|
|
||
| const socket = new WebSocket(`ws://127.0.0.1:${server.port}/api/echo`); | ||
|
|
||
| try { | ||
| await waitForOpen(socket); | ||
|
|
||
| const ready = await waitForJsonMessage(socket); | ||
| expect(ready).toEqual({ type: 'ready' }); | ||
|
|
||
| socket.send('hello from websocket test'); | ||
|
|
||
| const response = await waitForJsonMessage(socket); | ||
| if (response.type !== 'echo') { | ||
| throw new Error(`Expected echo response, received ${JSON.stringify(response)}`); | ||
| } | ||
|
|
||
| expect(response.data.echo).toBe('hello from websocket test'); | ||
| expect(response.data.sessionId).toBe(response.routeSessionId); | ||
| expect(response.data.threadId).toBe(response.routeThreadId); | ||
| expect(response.data.userId).toBe('late-bound-user'); | ||
| expect(response.data.sessionId.length).toBeGreaterThan(0); | ||
| expect(response.data.threadId.length).toBeGreaterThan(0); | ||
| } finally { | ||
| await closeSocket(socket); | ||
| server.stop(true); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.