-
Notifications
You must be signed in to change notification settings - Fork 12
allow channel-token ws subscriptions within their own namespace #241
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
ayush-that
wants to merge
2
commits into
main
Choose a base branch
from
fix/channel-scoped-ws-subscribe
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.
+137
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,124 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { createServer, type Server } from 'node:http'; | ||
| import { once } from 'node:events'; | ||
| import { test } from 'node:test'; | ||
|
|
||
| import { WebSocket } from 'ws'; | ||
|
|
||
| import { ChannelRegistry, createJwtConfig, type AuthResolverOptions } from '../src/auth.js'; | ||
| import { attachGatewayWs } from '../src/ws-handler.js'; | ||
| import type { AgentInstanceManager } from '../src/agent-instance.js'; | ||
|
|
||
| const CHANNEL_KEY = 'test-channel-key'; | ||
|
|
||
| const makeRuntimeStub = () => { | ||
| const subscribed: string[] = []; | ||
| return { | ||
| subscribed, | ||
| runtime: { | ||
| events: { | ||
| subscribeFrom: (sessionId: string) => { | ||
| subscribed.push(sessionId); | ||
| return () => {}; | ||
| }, | ||
| }, | ||
| // Channel connections carry no channelUserId, so the caller never | ||
| // resolves to a userId; mirror that here. | ||
| resolveCallerUserId: async () => undefined, | ||
| verifySessionAccess: async () => { | ||
| throw new Error('verifySessionAccess should not be reached in these tests'); | ||
| }, | ||
| } as never, | ||
| }; | ||
| }; | ||
|
|
||
| const makeInstancesStub = (runtime: never): AgentInstanceManager => | ||
| ({ | ||
| getOrHydrate: async () => runtime, | ||
| wsConnect: () => {}, | ||
| wsDisconnect: () => {}, | ||
| touch: () => {}, | ||
| }) as unknown as AgentInstanceManager; | ||
|
|
||
| const makeAuthOptions = (): AuthResolverOptions => { | ||
| const channels = new ChannelRegistry(); | ||
| channels.register({ | ||
| channelId: 'chan-1', | ||
| apiKey: CHANNEL_KEY, | ||
| namespace: 'amiko', | ||
| agentId: 'agent-a', | ||
| }); | ||
| return { | ||
| userProviders: [], | ||
| channels, | ||
| jwt: createJwtConfig('test-secret'), | ||
| }; | ||
| }; | ||
|
|
||
| const listen = async (server: Server): Promise<number> => { | ||
| server.listen(0, '127.0.0.1'); | ||
| await once(server, 'listening'); | ||
| const address = server.address(); | ||
| assert.ok(address && typeof address === 'object'); | ||
| return address.port; | ||
| }; | ||
|
|
||
| interface WsResponse { | ||
| kind: 'response'; | ||
| id: string; | ||
| result?: { subscribed?: boolean }; | ||
| error?: { code: string; message: string }; | ||
| } | ||
|
|
||
| const subscribeOnce = (port: number, sessionId: string): Promise<WsResponse> => | ||
| new Promise((resolve, reject) => { | ||
| const ws = new WebSocket(`ws://127.0.0.1:${port}/api/agents/agent-a/ws`, { | ||
| headers: { authorization: `Bearer ${CHANNEL_KEY}` }, | ||
| }); | ||
| ws.on('open', () => { | ||
| ws.send( | ||
| JSON.stringify({ | ||
| kind: 'request', | ||
| id: 'req-1', | ||
| method: 'session.subscribe', | ||
| params: { sessionId }, | ||
| }), | ||
| ); | ||
| }); | ||
| ws.on('message', (data) => { | ||
| ws.close(); | ||
| resolve(JSON.parse(String(data)) as WsResponse); | ||
| }); | ||
| ws.on('error', reject); | ||
| setTimeout(() => reject(new Error('ws subscribe timed out')), 5000).unref(); | ||
| }); | ||
|
|
||
| test('channel token subscribes to a session in its own namespace', async () => { | ||
| const { runtime, subscribed } = makeRuntimeStub(); | ||
| const server = createServer(); | ||
| attachGatewayWs(server, { instances: makeInstancesStub(runtime), auth: makeAuthOptions() }); | ||
| const port = await listen(server); | ||
| try { | ||
| const response = await subscribeOnce(port, 'amiko:conv-1'); | ||
| assert.equal(response.result?.subscribed, true); | ||
| assert.deepEqual(subscribed, ['amiko:conv-1']); | ||
| } finally { | ||
| server.close(); | ||
| await once(server, 'close'); | ||
| } | ||
| }); | ||
|
|
||
| test('channel token cannot subscribe outside its namespace', async () => { | ||
| const { runtime, subscribed } = makeRuntimeStub(); | ||
| const server = createServer(); | ||
| attachGatewayWs(server, { instances: makeInstancesStub(runtime), auth: makeAuthOptions() }); | ||
| const port = await listen(server); | ||
| try { | ||
| const response = await subscribeOnce(port, 'telegram:conv-9'); | ||
| assert.equal(response.error?.code, 'SESSION_NOT_FOUND'); | ||
| assert.deepEqual(subscribed, []); | ||
| } finally { | ||
| server.close(); | ||
| await once(server, 'close'); | ||
| } | ||
| }); | ||
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.