fix: sync model tier between TUI and WebChat when switching models

ModelRouter now supports multiple tier-change listeners via addOnTierChange(),
SessionBridge subscribes to tier changes and propagates them to all WebChat
agents (both existing and newly created), and the fullscreen TUI now also
updates the agent's tier when switching models (matching minimal TUI behavior).
This commit is contained in:
William Valentin
2026-02-10 20:22:40 -08:00
parent bf9ca690f3
commit 7a69794418
3 changed files with 38 additions and 5 deletions
+21 -1
View File
@@ -2,7 +2,7 @@ import { randomUUID } from 'crypto';
import type { SessionManager } from '../session/manager.js';
import type { Session } from '../session/manager.js';
import type { ModelClient } from '../models/types.js';
import type { ModelRouter } from '../models/router.js';
import type { ModelRouter, ModelTier } from '../models/router.js';
import type { ToolRegistry } from '../tools/registry.js';
import type { ToolExecutor } from '../tools/executor.js';
import { NativeAgent } from '../backends/native/agent.js';
@@ -27,9 +27,27 @@ export class SessionBridge {
private clients: Map<string, ClientEntry> = new Map();
private agents: Map<string, NativeAgent> = new Map();
private config: SessionBridgeConfig;
/** Tracks the current model tier so new agents inherit it and existing agents stay in sync. */
private currentTier: ModelTier = 'default';
constructor(config: SessionBridgeConfig) {
this.config = config;
// If the model client is a ModelRouter, subscribe to tier changes
// so all WebChat agents stay in sync with TUI model switches.
if ('getClient' in config.modelClient) {
const router = config.modelClient as ModelRouter;
this.currentTier = router.getTier();
router.addOnTierChange((tier: ModelTier) => this.onTierChanged(tier));
}
}
/** Called when the ModelRouter's active tier changes. Updates all existing agents. */
private onTierChanged(tier: ModelTier): void {
this.currentTier = tier;
for (const agent of this.agents.values()) {
agent.setModelTier(tier);
}
}
/** Register a new WS connection. Returns the assigned connection ID. */
@@ -172,6 +190,8 @@ export class SessionBridge {
toolRegistry: this.config.toolRegistry,
toolExecutor: this.config.toolExecutor,
});
// Inherit the current model tier so the agent uses the same model as the TUI
agent.setModelTier(this.currentTier);
this.agents.set(sessionId, agent);
}
return agent;