Skip to content

Commit 84e6b54

Browse files
committed
feat(agent): sync model when switching agents
When switching agents via the Telegram bot, the model now automatically syncs to the model configured for that agent in OpenCode config. If the agent has no model configured, the current model is preserved. Manual model switching remains independent and unaffected.
1 parent ae492e9 commit 84e6b54

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/agent/manager.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ export async function getAvailableAgents(): Promise<AgentInfo[]> {
2626
}
2727

2828
// Filter out hidden agents and subagents (only show primary and all)
29-
const filtered = agents.filter(
30-
(agent) => !agent.hidden && (agent.mode === "primary" || agent.mode === "all"),
31-
);
29+
const filtered = agents
30+
.filter((agent) => !agent.hidden && (agent.mode === "primary" || agent.mode === "all"))
31+
.map((agent) => ({
32+
name: agent.name,
33+
description: agent.description,
34+
color: agent.color,
35+
mode: agent.mode,
36+
hidden: agent.hidden,
37+
steps: agent.steps,
38+
model: agent.model ? { modelID: agent.model.modelID, providerID: agent.model.providerID } : undefined,
39+
}));
3240

3341
logger.debug(`[AgentManager] Fetched ${filtered.length} available agents`);
3442
return filtered;
@@ -138,6 +146,12 @@ export function selectAgent(agentName: string): void {
138146
setCurrentAgent(agentName);
139147
}
140148

149+
export async function getModelForAgent(agentName: string): Promise<{ modelID: string; providerID: string } | null> {
150+
const agents = await getAvailableAgents();
151+
const agent = agents.find((a) => a.name === agentName);
152+
return agent?.model ?? null;
153+
}
154+
141155
/**
142156
* Get stored agent from settings (synchronous)
143157
* @returns Current agent name or default "build"

src/agent/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export interface AgentInfo {
88
mode: "subagent" | "primary" | "all";
99
hidden?: boolean;
1010
steps?: number;
11+
model?: {
12+
modelID: string;
13+
providerID: string;
14+
};
1115
}
1216

1317
/**

src/bot/handlers/agent.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Context, InlineKeyboard } from "grammy";
2-
import { selectAgent, getAvailableAgents, fetchCurrentAgent } from "../../agent/manager.js";
2+
import { selectAgent, getAvailableAgents, fetchCurrentAgent, getModelForAgent } from "../../agent/manager.js";
33
import { getAgentDisplayName } from "../../agent/types.js";
4-
import { getStoredModel } from "../../model/manager.js";
4+
import { getStoredModel, selectModel } from "../../model/manager.js";
55
import { formatVariantForButton } from "../../variant/manager.js";
66
import { logger } from "../../utils/logger.js";
77
import { createMainKeyboard } from "../utils/keyboard.js";
@@ -47,7 +47,16 @@ export async function handleAgentSelect(ctx: Context): Promise<boolean> {
4747
// Select agent and persist
4848
selectAgent(agentName);
4949

50-
// Update keyboard manager state
50+
const agentModel = await getModelForAgent(agentName);
51+
if (agentModel) {
52+
selectModel({
53+
providerID: agentModel.providerID,
54+
modelID: agentModel.modelID,
55+
variant: "default",
56+
});
57+
await pinnedMessageManager.refreshContextLimit();
58+
}
59+
5160
keyboardManager.updateAgent(agentName);
5261

5362
// Update Reply Keyboard with new agent, current model, and context

0 commit comments

Comments
 (0)