feat(companion): add context warning stream helpers
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user