feat(companion): add context warning stream helpers

This commit is contained in:
William Valentin
2026-02-16 19:12:02 -08:00
parent 43968f830a
commit 636f24016f
4 changed files with 65 additions and 1 deletions
+37
View File
@@ -224,6 +224,24 @@ describe('CompanionRuntimeClient', () => {
expect(typingHandler).toHaveBeenCalledWith({ active: true });
});
it('supports subscribeContextWarning helper', () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
const warningHandler = vi.fn();
client.subscribeContextWarning(warningHandler);
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
id: 56,
event: 'context_warning',
data: { thresholdPct: 80, estimatedPct: 92 },
}),
);
expect(warningHandler).toHaveBeenCalledWith({ thresholdPct: 80, estimatedPct: 92 });
});
it('clears all event subscriptions', () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
@@ -355,6 +373,25 @@ describe('CompanionRuntimeClient', () => {
await expect(awaited).resolves.toEqual({ active: true });
});
it('waitForContextWarning resolves on context_warning events', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
const awaited = client.waitForContextWarning<{ thresholdPct: number; estimatedPct: number }>({
timeoutMs: 2000,
});
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
id: 57,
event: 'context_warning',
data: { thresholdPct: 75, estimatedPct: 88 },
}),
);
await expect(awaited).resolves.toEqual({ thresholdPct: 75, estimatedPct: 88 });
});
it('connects and performs node registration + capability discovery', async () => {
if (!LISTEN_ALLOWED) {
return;
+14
View File
@@ -403,6 +403,12 @@ export class CompanionRuntimeClient {
return this.subscribeEvent<TData>(COMPANION_EVENT_NAMES.agentTyping, handler);
}
subscribeContextWarning<TData = unknown>(
handler: CompanionTypedEventHandler<TData>,
): () => void {
return this.subscribeEvent<TData>(COMPANION_EVENT_NAMES.contextWarning, handler);
}
waitForEvent<TData = unknown>(
eventName: string,
options?: {
@@ -475,6 +481,14 @@ export class CompanionRuntimeClient {
return this.waitForEvent<TData>(COMPANION_EVENT_NAMES.agentTyping, options);
}
waitForContextWarning<TData = unknown>(options?: {
timeoutMs?: number;
predicate?: CompanionEventPredicate<TData>;
signal?: AbortSignal;
}): Promise<TData> {
return this.waitForEvent<TData>(COMPANION_EVENT_NAMES.contextWarning, options);
}
async call<T>(method: string, params?: Record<string, unknown>): Promise<T> {
if (!this.connected) {
if (!this.autoConnect) {