feat(companion): add typed wait helpers for stream events

This commit is contained in:
William Valentin
2026-02-16 19:11:10 -08:00
parent 1b69970065
commit 43968f830a
4 changed files with 64 additions and 1 deletions
+34
View File
@@ -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;
+16
View File
@@ -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) {