A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
feat(acp): add dynamic model selection for ACP agents (#7432)
Luis Chaves Rodriguezf8b4df043016
diff --git a/frontend/src/components/chat/acp/__tests__/state.test.ts b/frontend/src/components/chat/acp/__tests__/state.test.tsindex c94e4cd56d..bc975c970c 100644--- a/frontend/src/components/chat/acp/__tests__/state.test.ts+++ b/frontend/src/components/chat/acp/__tests__/state.test.ts@@ -87,6 +87,7 @@ describe("state utility functions", () => {"createdAt": 1735689600000,"externalAgentSessionId": null,"lastUsedAt": 1735689600000,+ "selectedModel": null,"title": "New claude session",},],@@ -103,6 +104,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,};const initialState: AgentSessionState = {sessions: [existingSession],@@ -127,6 +129,7 @@ describe("state utility functions", () => {"createdAt": 1735689600000,"externalAgentSessionId": null,"lastUsedAt": 1735689600000,+ "selectedModel": null,"title": "New claude session",},],@@ -142,6 +145,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,};const initialState: AgentSessionState = {sessions: [existingSession],@@ -163,6 +167,7 @@ describe("state utility functions", () => {"createdAt": 1735689600000,"externalAgentSessionId": null,"lastUsedAt": 1735689600000,+ "selectedModel": null,"tabId": "tab_existing","title": "Hello",},@@ -180,6 +185,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: "claude-session-123" as ExternalAgentSessionId,+ selectedModel: null,};const initialState: AgentSessionState = {sessions: [claudeSession],@@ -208,6 +214,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: "gemini-session-456" as ExternalAgentSessionId,+ selectedModel: null,};const initialState: AgentSessionState = {sessions: [geminiSession],@@ -253,6 +260,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,},{agentId: "gemini",@@ -261,6 +269,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,},{agentId: "claude",@@ -269,6 +278,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,},];state = {@@ -289,6 +299,7 @@ describe("state utility functions", () => {"createdAt": 1735689600000,"externalAgentSessionId": null,"lastUsedAt": 1735689600000,+ "selectedModel": null,"tabId": "tab_1","title": "Claude session 1",},@@ -297,6 +308,7 @@ describe("state utility functions", () => {"createdAt": 1735689600000,"externalAgentSessionId": null,"lastUsedAt": 1735689600000,+ "selectedModel": null,"tabId": "tab_3","title": "Claude session 2",},@@ -323,6 +335,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,};const singleSessionState: AgentSessionState = {sessions: [singleSession],@@ -356,6 +369,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,},{agentId: "gemini",@@ -364,6 +378,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,},];state = {@@ -420,6 +435,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,},{agentId: "gemini",@@ -428,6 +444,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,},];state = {@@ -483,6 +500,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,},{agentId: "gemini",@@ -491,6 +509,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000,lastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,},];state = {@@ -557,6 +576,7 @@ describe("state utility functions", () => {createdAt: 1_735_689_600_000, // 2025-01-01T00:00:00ZlastUsedAt: 1_735_689_600_000,externalAgentSessionId: null,+ selectedModel: null,},{agentId: "gemini",@@ -565,6 +585,7 @@ describe("state utility functions", () => {createdAt: 1_735_693_200_000, // 2025-01-01T01:00:00ZlastUsedAt: 1_735_693_200_000,externalAgentSessionId: null,+ selectedModel: null,},{agentId: "claude",@@ -573,6 +594,7 @@ describe("state utility functions", () => {createdAt: 1_735_696_800_000, // 2025-01-01T02:00:00ZlastUsedAt: 1_735_696_800_000,externalAgentSessionId: null,+ selectedModel: null,},{agentId: "claude",@@ -581,6 +603,7 @@ describe("state utility functions", () => {createdAt: 1_735_700_400_000, // 2025-01-01T03:00:00ZlastUsedAt: 1_735_700_400_000,externalAgentSessionId: null,+ selectedModel: null,},];});diff --git a/frontend/src/components/chat/acp/agent-panel.tsx b/frontend/src/components/chat/acp/agent-panel.tsxindex 110798d6a6..e212d725ae 100644--- a/frontend/src/components/chat/acp/agent-panel.tsx+++ b/frontend/src/components/chat/acp/agent-panel.tsx@@ -31,6 +31,7 @@ import { cn } from "@/utils/cn";import { Logger } from "@/utils/Logger";import { AgentDocs } from "./agent-docs";import { AgentSelector } from "./agent-selector";+import { ModelSelector } from "./model-selector";import ScrollToBottomButton from "./scroll-to-bottom-button";import { SessionTabs } from "./session-tabs";import {@@ -82,6 +83,7 @@ import type {ExternalAgentSessionId,NotificationEvent,SessionMode,+ SessionModelState,} from "./types";const logger = Logger.get("agents");@@ -298,6 +300,8 @@ interface PromptAreaProps {fileInputRef: React.RefObject<HTMLInputElement | null>;sessionMode?: SessionMode;onModeChange?: (mode: string) => void;+ sessionModels?: SessionModelState | null;+ onModelChange?: (modelId: string) => void;}const PromptArea = memo<PromptAreaProps>(@@ -313,6 +317,8 @@ const PromptArea = memo<PromptAreaProps>(fileInputRef,sessionMode,onModeChange,+ sessionModels,+ onModelChange,}) => {const inputRef = useRef<ReactCodeMirrorRef | null>(null);const promptCompletions: AdditionalCompletions | undefined = useMemo(() => {@@ -376,6 +382,13 @@ const PromptArea = memo<PromptAreaProps>(onModeChange={onModeChange}/>)}+ {sessionModels && onModelChange && activeSessionId && (+ <ModelSelector+ sessionModels={sessionModels}+ onModelChange={onModelChange}+ disabled={isLoading}+ />+ )}</div><div className="flex flex-row"><Tooltip content="Add context">@@ -648,6 +661,9 @@ const AgentPanel: React.FC = () => {const [isLoading, setIsLoading] = useState(false);const [promptValue, setPromptValue] = useState("");const [files, setFiles] = useState<File[]>();+ const [sessionModels, setSessionModels] = useState<SessionModelState | null>(+ null,+ );const fileInputRef = useRef<HTMLInputElement>(null);const [selectedTab] = useAtom(selectedTabAtom);@@ -745,16 +761,26 @@ const AgentPanel: React.FC = () => {});}- logger.debug("Creating new agent session", {});+ // Get the selected model from the current session state+ const currentModel = selectedTab?.selectedModel ?? null;+ logger.debug("Creating new agent session", { model: currentModel });isCreatingNewSession.current = true;const newSession = await agent.newSession({cwd: getCwd(),mcpServers: [],+ _meta: currentModel ? { model: currentModel } : undefined,}).finally(() => {isCreatingNewSession.current = false;});++ // Capture models from the response+ if (newSession.models) {+ logger.debug("Session models received", { models: newSession.models });+ setSessionModels(newSession.models);+ }+setSessionState((prev) =>updateSessionExternalAgentSessionId(prev,@@ -774,11 +800,20 @@ const AgentPanel: React.FC = () => {if (!agent.loadSession) {throw new Error("Agent does not support loading sessions");}- await agent.loadSession({+ const loadedSession = await agent.loadSession({sessionId: previousSessionId,cwd: getCwd(),mcpServers: [],});++ // Capture models from the response if available+ if (loadedSession?.models) {+ logger.debug("Session models received", {+ models: loadedSession.models,+ });+ setSessionModels(loadedSession.models);+ }+setSessionState((prev) =>updateSessionExternalAgentSessionId(prev, previousSessionId),);@@ -945,6 +980,33 @@ const AgentPanel: React.FC = () => {disconnect();});+ const handleModelChange = useEvent((modelId: string) => {+ logger.debug("Model change requested", {+ modelId,+ sessionId: activeSessionId,+ });++ if (!agent || !activeSessionId) {+ toast({+ title: "Cannot change model",+ description: "Please connect to an agent with an active session first",+ variant: "danger",+ });+ return;+ }++ // Call agent.setSessionModel to notify the agent+ void agent.setSessionModel?.({+ sessionId: activeSessionId,+ modelId,+ });++ // Update local state+ setSessionModels((prev) =>+ prev ? { ...prev, currentModelId: modelId } : null,+ );+ });+const handleModeChange = useEvent((mode: string) => {logger.debug("Mode change requested", {sessionId: activeSessionId,@@ -1067,6 +1129,8 @@ const AgentPanel: React.FC = () => {commands={availableCommands}sessionMode={sessionMode}onModeChange={handleModeChange}+ sessionModels={sessionModels}+ onModelChange={handleModelChange}/></>);diff --git a/frontend/src/components/chat/acp/model-selector.tsx b/frontend/src/components/chat/acp/model-selector.tsxnew file mode 100644index 0000000000..f90de63b6e--- /dev/null+++ b/frontend/src/components/chat/acp/model-selector.tsx@@ -0,0 +1,73 @@+/* Copyright 2024 Marimo. All rights reserved. */++import { memo } from "react";+import {+ Select,+ SelectContent,+ SelectGroup,+ SelectItem,+ SelectLabel,+ SelectTrigger,+} from "@/components/ui/select";+import type { SessionModelState } from "./types";++interface ModelSelectorProps {+ sessionModels: SessionModelState | null;+ onModelChange: (modelId: string) => void;+ disabled?: boolean;+}++export const ModelSelector = memo<ModelSelectorProps>(+ ({ sessionModels, onModelChange, disabled }) => {+ if (!sessionModels || sessionModels.availableModels.length === 0) {+ return null;+ }++ const { availableModels, currentModelId } = sessionModels;+ const currentModel = availableModels.find(+ (m) => m.modelId === currentModelId,+ );+ const displayName = currentModel?.name ?? currentModelId;++ return (+ <Select+ value={currentModelId}+ onValueChange={onModelChange}+ disabled={disabled}+ >+ <SelectTrigger className="h-6 text-xs border-border shadow-none! ring-0! bg-muted hover:bg-muted/30 py-0 px-2 gap-1">+ {displayName}+ </SelectTrigger>+ <SelectContent>+ <SelectGroup>+ <SelectLabel>Model</SelectLabel>+ {availableModels.map((model, index) => (+ <SelectItem+ key={model.modelId}+ value={model.modelId}+ className="text-xs"+ >+ <div className="flex flex-col">+ <span>+ {model.name}+ {index === 0 && (+ <span className="text-muted-foreground ml-1">+ (default)+ </span>+ )}+ </span>+ {model.description && (+ <span className="text-muted-foreground text-xs pt-1 block">+ {model.description}+ </span>+ )}+ </div>+ </SelectItem>+ ))}+ </SelectGroup>+ </SelectContent>+ </Select>+ );+ },+);+ModelSelector.displayName = "ModelSelector";diff --git a/frontend/src/components/chat/acp/state.ts b/frontend/src/components/chat/acp/state.tsindex 1a1869da04..7d2fef762f 100644--- a/frontend/src/components/chat/acp/state.ts+++ b/frontend/src/components/chat/acp/state.ts@@ -25,6 +25,8 @@ export interface AgentSession {lastUsedAt: number;// Store the actual agent session ID for resumptionexternalAgentSessionId: ExternalAgentSessionId | null;+ // Selected model for this session (null = agent default)+ selectedModel: string | null;}export interface AgentSessionState {@@ -82,6 +84,7 @@ export function addSession(session: {agentId: ExternalAgentId;firstMessage?: string;+ model?: string | null;},): AgentSessionState {const sessionSupport = getAgentSessionSupport(session.agentId);@@ -111,6 +114,7 @@ export function addSession(lastUsedAt: now,tabId: existingSession.tabId, // Keep the same ID to maintain tab referenceexternalAgentSessionId: null, // Clear the external session ID+ selectedModel: session.model ?? existingSession.selectedModel ?? null,};return {@@ -133,6 +137,7 @@ export function addSession(createdAt: now,lastUsedAt: now,externalAgentSessionId: null,+ selectedModel: session.model ?? null,},],activeTabId: tabId,@@ -277,3 +282,24 @@ export function getAgentConnectionCommand(agentId: ExternalAgentId): string {const wrappedCommand = isPlatformWindows() ? `cmd /c ${command}` : command;return `npx stdio-to-ws "${wrappedCommand}" --port ${port}`;}++/**+ * Update the selected model for the currently active session+ */+export function updateSessionModel(+ state: AgentSessionState,+ model: string | null,+): AgentSessionState {+ const selectedTab = state.activeTabId;+ if (!selectedTab) {+ return state;+ }+ return {+ ...state,+ sessions: state.sessions.map((session) =>+ session.tabId === selectedTab+ ? { ...session, selectedModel: model }+ : session,+ ),+ };+}diff --git a/frontend/src/components/chat/acp/types.ts b/frontend/src/components/chat/acp/types.tsindex fe8b023968..a3a9ea40d8 100644--- a/frontend/src/components/chat/acp/types.ts+++ b/frontend/src/components/chat/acp/types.ts@@ -2,6 +2,12 @@import type { ContentBlock } from "@zed-industries/agent-client-protocol";import type { groupNotifications, useAcpClient } from "use-acp";+// Re-export model types for use in the app+export type {+ ModelInfo,+ SessionModelState,+} from "@zed-industries/agent-client-protocol";+export type SessionMode = ReturnType<typeof useAcpClient>["sessionMode"];export type NotificationEvent = Awaited<