feat(companion): add filtered runtime event subscription helper
This commit is contained in:
@@ -1190,7 +1190,7 @@ Methods:
|
|||||||
- `system.capabilities` returns gateway protocol and node policy snapshot.
|
- `system.capabilities` returns gateway protocol and node policy snapshot.
|
||||||
|
|
||||||
Companion runtime helper:
|
Companion runtime helper:
|
||||||
- `src/companion/runtimeClient.ts` provides a typed Node/WebSocket client for companion runtimes (macOS/iOS/Android workers) with wrappers for `node.register`, `node.capabilities.get`, `node.location.set/get`, `node.status.set`, `node.push_token.set`, `system.capabilities`, `system.nodes`, and canvas artifact RPCs (`canvas.put/get/list/delete/clear`), plus optional `autoConnect` mode and `subscribeEvents()` for gateway stream events.
|
- `src/companion/runtimeClient.ts` provides a typed Node/WebSocket client for companion runtimes (macOS/iOS/Android workers) with wrappers for `node.register`, `node.capabilities.get`, `node.location.set/get`, `node.status.set`, `node.push_token.set`, `system.capabilities`, `system.nodes`, and canvas artifact RPCs (`canvas.put/get/list/delete/clear`), plus optional `autoConnect` mode and event subscriptions (`subscribeEvents()`, `subscribeEvent()`).
|
||||||
- `src/companion/platformClients.ts` provides platform-focused wrappers:
|
- `src/companion/platformClients.ts` provides platform-focused wrappers:
|
||||||
- `MacOSCompanionClient` (`platform: "macos"`, APNs push registration)
|
- `MacOSCompanionClient` (`platform: "macos"`, APNs push registration)
|
||||||
- `IOSCompanionClient` (`platform: "ios"`, APNs push registration)
|
- `IOSCompanionClient` (`platform: "ios"`, APNs push registration)
|
||||||
|
|||||||
@@ -313,6 +313,20 @@
|
|||||||
],
|
],
|
||||||
"test_status": "pnpm test:run src/companion/runtimeClient.test.ts src/companion/platformClients.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
|
"test_status": "pnpm test:run src/companion/runtimeClient.test.ts src/companion/platformClients.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
|
||||||
},
|
},
|
||||||
|
"companion-runtime-filtered-event-helper": {
|
||||||
|
"status": "completed",
|
||||||
|
"date": "2026-02-17",
|
||||||
|
"updated": "2026-02-17",
|
||||||
|
"summary": "Added typed `subscribeEvent(eventName, handler)` helper to `CompanionRuntimeClient` for filtered event consumption on top of generic `subscribeEvents`, including unsubscribe behavior tests.",
|
||||||
|
"files_modified": [
|
||||||
|
"src/companion/runtimeClient.ts",
|
||||||
|
"src/companion/runtimeClient.test.ts",
|
||||||
|
"src/companion/index.ts",
|
||||||
|
"README.md",
|
||||||
|
"docs/plans/state.json"
|
||||||
|
],
|
||||||
|
"test_status": "pnpm test:run src/companion/runtimeClient.test.ts src/companion/platformClients.test.ts src/companion/platformClients.integration.test.ts src/companion/heartbeatLoop.test.ts + pnpm typecheck passing"
|
||||||
|
},
|
||||||
"browser-tools-activation-clarity": {
|
"browser-tools-activation-clarity": {
|
||||||
"status": "completed",
|
"status": "completed",
|
||||||
"date": "2026-02-17",
|
"date": "2026-02-17",
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export { CompanionHeartbeatLoop } from './heartbeatLoop.js';
|
|||||||
export type {
|
export type {
|
||||||
CompanionRuntimeClientOptions,
|
CompanionRuntimeClientOptions,
|
||||||
CompanionEventHandler,
|
CompanionEventHandler,
|
||||||
|
CompanionTypedEventHandler,
|
||||||
RegisterNodeInput,
|
RegisterNodeInput,
|
||||||
ListNodesInput,
|
ListNodesInput,
|
||||||
SetNodeStatusInput,
|
SetNodeStatusInput,
|
||||||
|
|||||||
@@ -151,6 +151,42 @@ describe('CompanionRuntimeClient', () => {
|
|||||||
expect(goodHandler).toHaveBeenCalledWith('agent.stream', { token: 'safe' });
|
expect(goodHandler).toHaveBeenCalledWith('agent.stream', { token: 'safe' });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('supports filtered subscribeEvent helper', () => {
|
||||||
|
const client = new CompanionRuntimeClient({
|
||||||
|
url: 'ws://127.0.0.1:1',
|
||||||
|
});
|
||||||
|
const streamHandler = vi.fn();
|
||||||
|
const unsubscribe = client.subscribeEvent<{ token: string }>('agent.stream', streamHandler);
|
||||||
|
|
||||||
|
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||||
|
JSON.stringify({
|
||||||
|
id: 45,
|
||||||
|
event: 'agent.stream',
|
||||||
|
data: { token: 'first' },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||||
|
JSON.stringify({
|
||||||
|
id: 46,
|
||||||
|
event: 'agent.typing',
|
||||||
|
data: { active: true },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(streamHandler).toHaveBeenCalledTimes(1);
|
||||||
|
expect(streamHandler).toHaveBeenCalledWith({ token: 'first' });
|
||||||
|
|
||||||
|
unsubscribe();
|
||||||
|
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||||
|
JSON.stringify({
|
||||||
|
id: 47,
|
||||||
|
event: 'agent.stream',
|
||||||
|
data: { token: 'second' },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(streamHandler).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
it('connects and performs node registration + capability discovery', async () => {
|
it('connects and performs node registration + capability discovery', async () => {
|
||||||
if (!LISTEN_ALLOWED) {
|
if (!LISTEN_ALLOWED) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ export interface CompanionRuntimeClientOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type CompanionEventHandler = (event: string, data: unknown) => void;
|
export type CompanionEventHandler = (event: string, data: unknown) => void;
|
||||||
|
export type CompanionTypedEventHandler<TData = unknown> = (data: TData) => void;
|
||||||
|
|
||||||
export interface RegisterNodeInput {
|
export interface RegisterNodeInput {
|
||||||
nodeId: string;
|
nodeId: string;
|
||||||
@@ -352,6 +353,18 @@ export class CompanionRuntimeClient {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
subscribeEvent<TData = unknown>(
|
||||||
|
eventName: string,
|
||||||
|
handler: CompanionTypedEventHandler<TData>,
|
||||||
|
): () => void {
|
||||||
|
return this.subscribeEvents((event, data) => {
|
||||||
|
if (event !== eventName) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handler(data as TData);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async call<T>(method: string, params?: Record<string, unknown>): Promise<T> {
|
async call<T>(method: string, params?: Record<string, unknown>): Promise<T> {
|
||||||
if (!this.connected) {
|
if (!this.connected) {
|
||||||
if (!this.autoConnect) {
|
if (!this.autoConnect) {
|
||||||
|
|||||||
Reference in New Issue
Block a user