fix(models): make tier-change listener registration non-destructive

This commit is contained in:
William Valentin
2026-02-15 21:58:55 -08:00
parent 8709c3c9b4
commit c314e0f067
4 changed files with 32 additions and 1 deletions
+18
View File
@@ -463,4 +463,22 @@ describe('setClient and labels', () => {
expect(fallback.chat).not.toHaveBeenCalled();
expect(router.isTierStrict('default')).toBe(true);
});
it('setOnTierChange does not replace existing listeners', () => {
const router = new ModelRouter({
default: { chat: vi.fn() } as unknown as ModelClient,
fast: { chat: vi.fn() } as unknown as ModelClient,
fallbackChain: [],
});
const first = vi.fn();
const second = vi.fn();
router.addOnTierChange(first);
router.setOnTierChange(second);
router.setTier('fast');
expect(first).toHaveBeenCalledWith('fast');
expect(second).toHaveBeenCalledWith('fast');
});
});
+1 -1
View File
@@ -67,7 +67,7 @@ export class ModelRouter implements ModelClient {
}
setOnTierChange(callback: (tier: ModelTier) => void): void {
this.tierChangeListeners = [callback];
this.addOnTierChange(callback);
}
addOnTierChange(callback: (tier: ModelTier) => void): void {