feat(companion): add typed wait helpers for stream events
This commit is contained in:
@@ -1190,7 +1190,7 @@ Methods:
|
||||
- `system.capabilities` returns gateway protocol and node policy snapshot.
|
||||
|
||||
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()`, `subscribeAgentStream()`, `subscribeAgentTyping()`, `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, `waitForAgentStream()`, `waitForAgentTyping()`, `clearEventSubscriptions()`).
|
||||
- `src/companion/platformClients.ts` provides platform-focused wrappers:
|
||||
- `MacOSCompanionClient` (`platform: "macos"`, APNs push registration)
|
||||
- `IOSCompanionClient` (`platform: "ios"`, APNs push registration)
|
||||
|
||||
@@ -514,6 +514,19 @@
|
||||
],
|
||||
"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-wait-stream-helpers": {
|
||||
"status": "completed",
|
||||
"date": "2026-02-17",
|
||||
"updated": "2026-02-17",
|
||||
"summary": "Added typed stream wait helpers on `CompanionRuntimeClient` (`waitForAgentStream`, `waitForAgentTyping`) built on `waitForEvent` for common companion synchronization flows.",
|
||||
"files_modified": [
|
||||
"src/companion/runtimeClient.ts",
|
||||
"src/companion/runtimeClient.test.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": {
|
||||
"status": "completed",
|
||||
"date": "2026-02-17",
|
||||
|
||||
@@ -321,6 +321,40 @@ describe('CompanionRuntimeClient', () => {
|
||||
await awaited;
|
||||
});
|
||||
|
||||
it('waitForAgentStream resolves on agent.stream events', async () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
|
||||
const awaited = client.waitForAgentStream<{ token: string }>({ timeoutMs: 2000 });
|
||||
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||
JSON.stringify({
|
||||
id: 54,
|
||||
event: 'agent.stream',
|
||||
data: { token: 'typed-stream' },
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(awaited).resolves.toEqual({ token: 'typed-stream' });
|
||||
});
|
||||
|
||||
it('waitForAgentTyping resolves on agent.typing events', async () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
|
||||
const awaited = client.waitForAgentTyping<{ active: boolean }>({ timeoutMs: 2000 });
|
||||
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
|
||||
JSON.stringify({
|
||||
id: 55,
|
||||
event: 'agent.typing',
|
||||
data: { active: true },
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(awaited).resolves.toEqual({ active: true });
|
||||
});
|
||||
|
||||
it('connects and performs node registration + capability discovery', async () => {
|
||||
if (!LISTEN_ALLOWED) {
|
||||
return;
|
||||
|
||||
@@ -459,6 +459,22 @@ export class CompanionRuntimeClient {
|
||||
});
|
||||
}
|
||||
|
||||
waitForAgentStream<TData = unknown>(options?: {
|
||||
timeoutMs?: number;
|
||||
predicate?: CompanionEventPredicate<TData>;
|
||||
signal?: AbortSignal;
|
||||
}): Promise<TData> {
|
||||
return this.waitForEvent<TData>(COMPANION_EVENT_NAMES.agentStream, options);
|
||||
}
|
||||
|
||||
waitForAgentTyping<TData = unknown>(options?: {
|
||||
timeoutMs?: number;
|
||||
predicate?: CompanionEventPredicate<TData>;
|
||||
signal?: AbortSignal;
|
||||
}): Promise<TData> {
|
||||
return this.waitForEvent<TData>(COMPANION_EVENT_NAMES.agentTyping, options);
|
||||
}
|
||||
|
||||
async call<T>(method: string, params?: Record<string, unknown>): Promise<T> {
|
||||
if (!this.connected) {
|
||||
if (!this.autoConnect) {
|
||||
|
||||
Reference in New Issue
Block a user