feat(companion): add typed agent stream subscription helpers
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 convenience helpers (`bootstrapNode`, optional `autoConnect`, `dispose()`) and event helpers (`subscribeEvents()`, `subscribeEvent()`, `waitForEvent()` with timeout/predicate/abort support, `clearEventSubscriptions()`).
|
- `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 convenience helpers (`bootstrapNode`, optional `autoConnect`, `dispose()`) and event helpers (`subscribeEvents()`, `subscribeEvent()`, `subscribeAgentStream()`, `subscribeAgentTyping()`, `waitForEvent()` with timeout/predicate/abort support, `clearEventSubscriptions()`).
|
||||||
- `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)
|
||||||
|
|||||||
@@ -473,6 +473,20 @@
|
|||||||
],
|
],
|
||||||
"test_status": "pnpm test:run src/companion/platformClients.test.ts src/companion/runtimeClient.test.ts src/companion/heartbeatLoop.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
|
"test_status": "pnpm test:run src/companion/platformClients.test.ts src/companion/runtimeClient.test.ts src/companion/heartbeatLoop.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
|
||||||
},
|
},
|
||||||
|
"companion-runtime-typed-stream-subscriptions": {
|
||||||
|
"status": "completed",
|
||||||
|
"date": "2026-02-17",
|
||||||
|
"updated": "2026-02-17",
|
||||||
|
"summary": "Added typed stream-event convenience subscriptions on `CompanionRuntimeClient` (`subscribeAgentStream`, `subscribeAgentTyping`) and exported canonical event-name constants.",
|
||||||
|
"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/heartbeatLoop.test.ts src/companion/platformClients.integration.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",
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
export {
|
export {
|
||||||
CompanionRuntimeClient,
|
CompanionRuntimeClient,
|
||||||
GatewayRpcError,
|
GatewayRpcError,
|
||||||
|
COMPANION_EVENT_NAMES,
|
||||||
} from './runtimeClient.js';
|
} from './runtimeClient.js';
|
||||||
export {
|
export {
|
||||||
MacOSCompanionClient,
|
MacOSCompanionClient,
|
||||||
|
|||||||
@@ -196,6 +196,34 @@ describe('CompanionRuntimeClient', () => {
|
|||||||
expect(streamHandler).toHaveBeenCalledTimes(1);
|
expect(streamHandler).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('supports subscribeAgentStream and subscribeAgentTyping helpers', () => {
|
||||||
|
const client = new CompanionRuntimeClient({
|
||||||
|
url: 'ws://127.0.0.1:1',
|
||||||
|
});
|
||||||
|
const streamHandler = vi.fn();
|
||||||
|
const typingHandler = vi.fn();
|
||||||
|
client.subscribeAgentStream(streamHandler);
|
||||||
|
client.subscribeAgentTyping(typingHandler);
|
||||||
|
|
||||||
|
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||||
|
JSON.stringify({
|
||||||
|
id: 52,
|
||||||
|
event: 'agent.stream',
|
||||||
|
data: { token: 'x' },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||||
|
JSON.stringify({
|
||||||
|
id: 53,
|
||||||
|
event: 'agent.typing',
|
||||||
|
data: { active: true },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(streamHandler).toHaveBeenCalledWith({ token: 'x' });
|
||||||
|
expect(typingHandler).toHaveBeenCalledWith({ active: true });
|
||||||
|
});
|
||||||
|
|
||||||
it('clears all event subscriptions', () => {
|
it('clears all event subscriptions', () => {
|
||||||
const client = new CompanionRuntimeClient({
|
const client = new CompanionRuntimeClient({
|
||||||
url: 'ws://127.0.0.1:1',
|
url: 'ws://127.0.0.1:1',
|
||||||
|
|||||||
@@ -45,6 +45,12 @@ export type CompanionEventHandler = (event: string, data: unknown) => void;
|
|||||||
export type CompanionTypedEventHandler<TData = unknown> = (data: TData) => void;
|
export type CompanionTypedEventHandler<TData = unknown> = (data: TData) => void;
|
||||||
export type CompanionEventPredicate<TData = unknown> = (data: TData) => boolean;
|
export type CompanionEventPredicate<TData = unknown> = (data: TData) => boolean;
|
||||||
|
|
||||||
|
export const COMPANION_EVENT_NAMES = {
|
||||||
|
agentStream: 'agent.stream',
|
||||||
|
agentTyping: 'agent.typing',
|
||||||
|
contextWarning: 'context_warning',
|
||||||
|
} as const;
|
||||||
|
|
||||||
export interface RegisterNodeInput {
|
export interface RegisterNodeInput {
|
||||||
nodeId: string;
|
nodeId: string;
|
||||||
role: string;
|
role: string;
|
||||||
@@ -385,6 +391,18 @@ export class CompanionRuntimeClient {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
subscribeAgentStream<TData = unknown>(
|
||||||
|
handler: CompanionTypedEventHandler<TData>,
|
||||||
|
): () => void {
|
||||||
|
return this.subscribeEvent<TData>(COMPANION_EVENT_NAMES.agentStream, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
subscribeAgentTyping<TData = unknown>(
|
||||||
|
handler: CompanionTypedEventHandler<TData>,
|
||||||
|
): () => void {
|
||||||
|
return this.subscribeEvent<TData>(COMPANION_EVENT_NAMES.agentTyping, handler);
|
||||||
|
}
|
||||||
|
|
||||||
waitForEvent<TData = unknown>(
|
waitForEvent<TData = unknown>(
|
||||||
eventName: string,
|
eventName: string,
|
||||||
options?: {
|
options?: {
|
||||||
|
|||||||
Reference in New Issue
Block a user