From f368a2fa62bd2aa35a28121f542f50a1ec987f84 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Fri, 27 Feb 2026 17:01:56 -0500 Subject: [PATCH] Add agent color styling and session persistence --- .../src/components/agent/AgentQuickSelect.tsx | 89 +++++++------- .../src/components/message/PromptInput.tsx | 14 ++- frontend/src/hooks/useSessionAgent.ts | 115 ++++++++++++------ frontend/src/lib/agent-colors.ts | 95 +++++++++++++++ frontend/src/stores/sessionAgentStore.ts | 25 ++++ 5 files changed, 253 insertions(+), 85 deletions(-) create mode 100644 frontend/src/lib/agent-colors.ts create mode 100644 frontend/src/stores/sessionAgentStore.ts diff --git a/frontend/src/components/agent/AgentQuickSelect.tsx b/frontend/src/components/agent/AgentQuickSelect.tsx index e32ab192..2943fe67 100644 --- a/frontend/src/components/agent/AgentQuickSelect.tsx +++ b/frontend/src/components/agent/AgentQuickSelect.tsx @@ -9,6 +9,7 @@ import { DropdownMenuTrigger, } from '@/components/ui/dropdown-menu' import { useAgents } from '@/hooks/useOpenCode' +import { getAgentStyleVars } from '@/lib/agent-colors' interface AgentQuickSelectProps { opcodeUrl: string | null | undefined @@ -19,33 +20,33 @@ interface AgentQuickSelectProps { disabled?: boolean } -const getAgentStyles = (agent: string) => { - const lowerAgent = agent.toLowerCase() - if (lowerAgent === 'plan') { - return { - color: 'text-yellow-600 dark:text-yellow-500', - bg: 'bg-yellow-500/20 border-yellow-400 hover:bg-yellow-500/30 hover:border-yellow-300', - shadow: 'shadow-yellow-500/20 hover:shadow-yellow-500/30', - } - } - if (lowerAgent === 'build') { - return { - color: 'text-green-600 dark:text-green-500', - bg: 'bg-green-500/20 border-green-400 hover:bg-green-500/30 hover:border-green-300', - shadow: 'shadow-green-500/20 hover:shadow-green-500/30', - } - } - return { - color: 'text-blue-600 dark:text-blue-500', - bg: 'bg-blue-500/20 border-blue-400 hover:bg-blue-500/30 hover:border-blue-300', - shadow: 'shadow-blue-500/20 hover:shadow-blue-500/30', - } +interface AgentInfo { + name: string + color?: string + description?: string + mode?: string + hidden?: boolean } -const bashStyles = { - color: 'text-purple-700 dark:text-purple-300', - bg: 'bg-purple-500/20 border-purple-400', - shadow: 'shadow-purple-500/20 hover:shadow-purple-500/30', +const findAgentColor = (agents: AgentInfo[], agentName: string): string | undefined => { + return agents.find(a => a.name.toLowerCase() === agentName.toLowerCase())?.color +} + +const bashStyleVars: Record = { + '--agent-color-light': '#a753ae', + '--agent-color-dark': '#edb2f1', + '--agent-bg-light': 'rgba(167, 83, 174, 0.2)', + '--agent-bg-dark': 'rgba(237, 178, 241, 0.2)', + '--agent-bg-hover-light': 'rgba(167, 83, 174, 0.3)', + '--agent-bg-hover-dark': 'rgba(237, 178, 241, 0.3)', + '--agent-border-light': 'rgba(167, 83, 174, 0.6)', + '--agent-border-dark': 'rgba(237, 178, 241, 0.6)', + '--agent-border-hover-light': 'rgba(167, 83, 174, 0.5)', + '--agent-border-hover-dark': 'rgba(237, 178, 241, 0.5)', + '--agent-shadow-light': 'rgba(167, 83, 174, 0.2)', + '--agent-shadow-dark': 'rgba(237, 178, 241, 0.2)', + '--agent-shadow-hover-light': 'rgba(167, 83, 174, 0.3)', + '--agent-shadow-hover-dark': 'rgba(237, 178, 241, 0.3)', } export function AgentQuickSelect({ @@ -66,60 +67,52 @@ export function AgentQuickSelect({ ) }, [agents]) - const handleToggle = () => { - if (primaryAgents.length === 0) return - - const currentIndex = primaryAgents.findIndex( - (a) => a.name.toLowerCase() === currentAgent.toLowerCase() - ) - const nextIndex = (currentIndex + 1) % primaryAgents.length - onAgentChange(primaryAgents[nextIndex].name) - } - const handleSelect = (agentName: string) => { onAgentChange(agentName) } - const styles = isBashMode ? bashStyles : getAgentStyles(currentAgent) + const styleVars = isBashMode + ? bashStyleVars + : getAgentStyleVars(currentAgent, findAgentColor(agents, currentAgent)) const displayName = isBashMode ? 'Bash' : capitalize(currentAgent) const buttonContent = ( ) - if (primaryAgents.length <= 2) { - return buttonContent - } - return ( {buttonContent} - + {primaryAgents.map((agent) => { - const agentStyles = getAgentStyles(agent.name) + const apiColor = agent.color + const itemStyleVars = getAgentStyleVars(agent.name, apiColor) const isSelected = agent.name.toLowerCase() === currentAgent.toLowerCase() return ( handleSelect(agent.name)} - className="flex items-center justify-between" + className="group flex items-center justify-between" > -
- +
+ {capitalize(agent.name)} {agent.description && ( - + {agent.description} )} diff --git a/frontend/src/components/message/PromptInput.tsx b/frontend/src/components/message/PromptInput.tsx index 3ec8f075..8b8db373 100644 --- a/frontend/src/components/message/PromptInput.tsx +++ b/frontend/src/components/message/PromptInput.tsx @@ -9,6 +9,7 @@ import { useSessionAgent } from '@/hooks/useSessionAgent' import { useSTT } from '@/hooks/useSTT' import { useUserBash } from '@/stores/userBashStore' +import { useSessionAgentStore } from '@/stores/sessionAgentStore' import { useMobile } from '@/hooks/useMobile' import { usePermissions } from '@/contexts/EventContext' @@ -193,13 +194,15 @@ export const PromptInput = memo(forwardRef( if (hasActiveStream) { const parts = parsePromptToParts(prompt, attachedFiles, imageAttachments) + const agentUsed = selectedAgent || currentMode sendPrompt.mutate({ sessionID, parts, model: currentModel, - agent: selectedAgent || currentMode, + agent: agentUsed, variant: currentVariant }) + setStoredAgent(sessionID, agentUsed) setPrompt('') setAttachedFiles(new Map()) revokeBlobUrls(imageAttachments) @@ -217,6 +220,7 @@ export const PromptInput = memo(forwardRef( command, agent: currentMode }) + setStoredAgent(sessionID, currentMode) setPrompt('') setIsBashMode(false) clearSTT() @@ -239,15 +243,17 @@ export const PromptInput = memo(forwardRef( } const parts = parsePromptToParts(prompt, attachedFiles, imageAttachments) + const agentUsed = selectedAgent || currentMode sendPrompt.mutate({ sessionID, parts, model: currentModel, - agent: selectedAgent || currentMode, + agent: agentUsed, variant: currentVariant }) + setStoredAgent(sessionID, agentUsed) setPrompt('') setAttachedFiles(new Map()) revokeBlobUrls(imageAttachments) @@ -356,6 +362,7 @@ export const PromptInput = memo(forwardRef( const handleAgentChange = (agent: string) => { setLocalMode(agent) + setStoredAgent(sessionID, agent) } const handleVoiceToggle = async () => { @@ -718,6 +725,7 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read) const sessionAgent = useSessionAgent(opcodeUrl, sessionID, directory) const currentMode = localMode ?? sessionAgent.agent + const setStoredAgent = useSessionAgentStore((s) => s.setAgent) const { model, modelString } = useModelSelection(opcodeUrl, directory) const currentModel = modelString || '' @@ -806,7 +814,7 @@ return ( )}
-
+
s.agents[sessionID ?? ''] ?? null) + const setAgent = useSessionAgentStore((s) => s.setAgent) const prevRef = useRef({ agent: DEFAULT_AGENT, model: undefined, variant: undefined }) - return useMemo(() => { - let agent = DEFAULT_AGENT - let model: { providerID: string; modelID: string } | undefined - let variant: string | undefined + const result = useMemo(() => { + if (storedAgent) { + let model: { providerID: string; modelID: string } | undefined + let variant: string | undefined - if (messages && messages.length > 0) { - for (let i = messages.length - 1; i >= 0; i--) { - const msgWithParts = messages[i] - if (msgWithParts.info.role === 'user') { - const userInfo = msgWithParts.info as UserMessage - agent = userInfo.agent || DEFAULT_AGENT - model = userInfo.model - variant = userInfo.variant - break + if (messages && messages.length > 0) { + for (let i = messages.length - 1; i >= 0; i--) { + const msgWithParts = messages[i] + if (msgWithParts.info.role === 'user') { + const userInfo = msgWithParts.info as UserMessage + model = userInfo.model + variant = userInfo.variant + break + } + } + } + + const prev = prevRef.current + if ( + prev.agent === storedAgent && + prev.variant === variant && + prev.model?.providerID === model?.providerID && + prev.model?.modelID === model?.modelID + ) { + return prev + } + + const next: SessionAgentResult = { agent: storedAgent, model, variant } + prevRef.current = next + return next + } + + if (messagesLoading) { + return { agent: DEFAULT_AGENT, model: undefined, variant: undefined } + } + + if (!messages || messages.length === 0) { + return { agent: DEFAULT_AGENT, model: undefined, variant: undefined } + } + + for (let i = messages.length - 1; i >= 0; i--) { + const msgWithParts = messages[i] + if (msgWithParts.info.role === 'user') { + const userInfo = msgWithParts.info as UserMessage + if (userInfo.agent) { + const prev = prevRef.current + if ( + prev.agent === userInfo.agent && + prev.variant === userInfo.variant && + prev.model?.providerID === userInfo.model?.providerID && + prev.model?.modelID === userInfo.model?.modelID + ) { + return prev + } + + const next: SessionAgentResult = { + agent: userInfo.agent, + model: userInfo.model, + variant: userInfo.variant, + } + prevRef.current = next + return next } } } - const prev = prevRef.current - if ( - prev.agent === agent && - prev.variant === variant && - prev.model?.providerID === model?.providerID && - prev.model?.modelID === model?.modelID - ) { - return prev - } + return { agent: DEFAULT_AGENT, model: undefined, variant: undefined } + }, [messages, messagesLoading, storedAgent]) - const next: SessionAgentResult = { agent, model, variant } - prevRef.current = next - return next - }, [messages]) + useEffect(() => { + if (result.agent && sessionID) { + setAgent(sessionID, result.agent) + } + }, [result.agent, sessionID, setAgent]) + + return result } export function getSessionAgentFromMessages( messages: Array<{ role: string; agent?: string }> | undefined -): string { +): string | undefined { if (!messages || messages.length === 0) { - return DEFAULT_AGENT + return undefined } for (let i = messages.length - 1; i >= 0; i--) { const msg = messages[i] - if (msg.role === 'user' && 'agent' in msg) { - return (msg.agent as string) || DEFAULT_AGENT + if (msg.role === 'user' && 'agent' in msg && msg.agent) { + return msg.agent } } - return DEFAULT_AGENT + return undefined } diff --git a/frontend/src/lib/agent-colors.ts b/frontend/src/lib/agent-colors.ts new file mode 100644 index 00000000..8bd15574 --- /dev/null +++ b/frontend/src/lib/agent-colors.ts @@ -0,0 +1,95 @@ +function hexToRgb(hex: string): { r: number; g: number; b: number } | null { + const cleanHex = hex.replace(/^#/, '') + if (cleanHex.length !== 6) { + return null + } + + const result = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(cleanHex) + if (!result) { + return null + } + + return { + r: parseInt(result[1], 16), + g: parseInt(result[2], 16), + b: parseInt(result[3], 16), + } +} + +const DEFAULT_AGENT_COLORS: Record = { + plan: { light: '#a753ae', dark: '#edb2f1' }, + build: { light: '#034cff', dark: '#89b5ff' }, + docs: { light: '#ffb224', dark: '#ffb224' }, + ask: { light: '#0091ff', dark: '#0091ff' }, +} + +const FALLBACK_COLOR = { light: '#6b7280', dark: '#9ca3af' } + +function isValidHex(color: string | undefined): color is string { + if (!color) return false + return /^#[0-9A-Fa-f]{6}$/.test(color) +} + +export function getAgentColor( + agentName: string, + apiColor?: string +): { light: string; dark: string } { + if (isValidHex(apiColor)) { + return { light: apiColor, dark: apiColor } + } + + const lookupName = agentName.toLowerCase() + const defaultColor = DEFAULT_AGENT_COLORS[lookupName] + + if (defaultColor) { + return defaultColor + } + + return FALLBACK_COLOR +} + +export function getAgentStyleVars( + agentName: string, + apiColor?: string +): Record { + const colors = getAgentColor(agentName, apiColor) + + const lightRgb = hexToRgb(colors.light) + const darkRgb = hexToRgb(colors.dark) + + const fallbackLight = FALLBACK_COLOR.light + const fallbackDark = FALLBACK_COLOR.dark + + const lightVars = lightRgb + ? { + r: lightRgb.r, + g: lightRgb.g, + b: lightRgb.b, + } + : hexToRgb(fallbackLight)! + + const darkVars = darkRgb + ? { + r: darkRgb.r, + g: darkRgb.g, + b: darkRgb.b, + } + : hexToRgb(fallbackDark)! + + return { + '--agent-color-light': colors.light, + '--agent-color-dark': colors.dark, + '--agent-bg-light': `rgba(${lightVars.r}, ${lightVars.g}, ${lightVars.b}, 0.2)`, + '--agent-bg-dark': `rgba(${darkVars.r}, ${darkVars.g}, ${darkVars.b}, 0.2)`, + '--agent-bg-hover-light': `rgba(${lightVars.r}, ${lightVars.g}, ${lightVars.b}, 0.3)`, + '--agent-bg-hover-dark': `rgba(${darkVars.r}, ${darkVars.g}, ${darkVars.b}, 0.3)`, + '--agent-border-light': `rgba(${lightVars.r}, ${lightVars.g}, ${lightVars.b}, 0.6)`, + '--agent-border-dark': `rgba(${darkVars.r}, ${darkVars.g}, ${darkVars.b}, 0.6)`, + '--agent-border-hover-light': `rgba(${lightVars.r}, ${lightVars.g}, ${lightVars.b}, 0.5)`, + '--agent-border-hover-dark': `rgba(${darkVars.r}, ${darkVars.g}, ${darkVars.b}, 0.5)`, + '--agent-shadow-light': `rgba(${lightVars.r}, ${lightVars.g}, ${lightVars.b}, 0.2)`, + '--agent-shadow-dark': `rgba(${darkVars.r}, ${darkVars.g}, ${darkVars.b}, 0.2)`, + '--agent-shadow-hover-light': `rgba(${lightVars.r}, ${lightVars.g}, ${lightVars.b}, 0.3)`, + '--agent-shadow-hover-dark': `rgba(${darkVars.r}, ${darkVars.g}, ${darkVars.b}, 0.3)`, + } +} diff --git a/frontend/src/stores/sessionAgentStore.ts b/frontend/src/stores/sessionAgentStore.ts new file mode 100644 index 00000000..0da850ea --- /dev/null +++ b/frontend/src/stores/sessionAgentStore.ts @@ -0,0 +1,25 @@ +import { create } from 'zustand' +import { persist } from 'zustand/middleware' + +interface SessionAgentStore { + agents: Record + setAgent: (sessionID: string, agent: string) => void + getAgent: (sessionID: string) => string | null +} + +export const useSessionAgentStore = create()( + persist( + (set, get) => ({ + agents: {}, + setAgent: (sessionID: string, agent: string) => + set((state) => ({ + agents: { ...state.agents, [sessionID]: agent }, + })), + getAgent: (sessionID: string) => get().agents[sessionID] ?? null, + }), + { + name: 'opencode-session-agents', + partialize: (state) => ({ agents: state.agents }), + } + ) +)