Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function DialogAgent() {
return (
<DialogSelect
title="Select agent"
current={local.agent.current().name}
current={local.agent.current()?.name ?? ""}
options={options()}
onSelect={(option) => {
local.agent.set(option.value)
Expand Down
12 changes: 6 additions & 6 deletions packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ export function Prompt(props: PromptProps) {
if (store.mode === "shell") {
sdk.client.session.shell({
sessionID,
agent: local.agent.current().name,
agent: local.agent.current()?.name ?? "",
model: {
providerID: selectedModel.providerID,
modelID: selectedModel.modelID,
Expand All @@ -551,7 +551,7 @@ export function Prompt(props: PromptProps) {
sessionID,
command: command.slice(1),
arguments: args.join(" "),
agent: local.agent.current().name,
agent: local.agent.current()?.name ?? "",
model: `${selectedModel.providerID}/${selectedModel.modelID}`,
messageID,
variant,
Expand All @@ -567,7 +567,7 @@ export function Prompt(props: PromptProps) {
sessionID,
...selectedModel,
messageID,
agent: local.agent.current().name,
agent: local.agent.current()?.name ?? "",
model: selectedModel,
variant,
parts: [
Expand Down Expand Up @@ -687,7 +687,7 @@ export function Prompt(props: PromptProps) {
const highlight = createMemo(() => {
if (keybind.leader) return theme.border
if (store.mode === "shell") return theme.primary
return local.agent.color(local.agent.current().name)
return local.agent.color(local.agent.current()?.name ?? "")
})

const showVariant = createMemo(() => {
Expand All @@ -698,7 +698,7 @@ export function Prompt(props: PromptProps) {
})

const spinnerDef = createMemo(() => {
const color = local.agent.color(local.agent.current().name)
const color = local.agent.color(local.agent.current()?.name ?? "")
return {
frames: createFrames({
color,
Expand Down Expand Up @@ -932,7 +932,7 @@ export function Prompt(props: PromptProps) {
/>
<box flexDirection="row" flexShrink={0} paddingTop={1} gap={1}>
<text fg={highlight()}>
{store.mode === "shell" ? "Shell" : Locale.titlecase(local.agent.current().name)}{" "}
{store.mode === "shell" ? "Shell" : Locale.titlecase(local.agent.current()?.name ?? "")}{" "}
</text>
<Show when={store.mode === "normal"}>
<box flexDirection="row" gap={1}>
Expand Down
44 changes: 34 additions & 10 deletions packages/opencode/src/cli/cmd/tui/context/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,20 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
const [agentStore, setAgentStore] = createStore<{
current: string
}>({
current: agents()[0].name,
// Use empty string as safe initial value - will be updated reactively when agents load
current: agents()[0]?.name ?? "",
})

// Reactively update current agent when agents list changes (e.g., after login/bootstrap)
createEffect(() => {
const list = agents()
if (list.length === 0) return
// If current is empty or no longer valid, set to first agent
if (!agentStore.current || !list.some((x) => x.name === agentStore.current)) {
setAgentStore("current", list[0].name)
}
})

const { theme } = useTheme()
const colors = createMemo(() => [
theme.secondary,
Expand All @@ -54,7 +66,9 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
return agents()
},
current() {
return agents().find((x) => x.name === agentStore.current)!
const found = agents().find((x) => x.name === agentStore.current)
// Return first agent as fallback if current not found (during loading/transition)
return found ?? agents()[0]
},
set(name: string) {
if (!agents().some((x) => x.name === name))
Expand All @@ -67,11 +81,13 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
},
move(direction: 1 | -1) {
batch(() => {
let next = agents().findIndex((x) => x.name === agentStore.current) + direction
if (next < 0) next = agents().length - 1
if (next >= agents().length) next = 0
const value = agents()[next]
setAgentStore("current", value.name)
const list = agents()
if (list.length === 0) return
let next = list.findIndex((x) => x.name === agentStore.current) + direction
if (next < 0) next = list.length - 1
if (next >= list.length) next = 0
const value = list[next]
if (value) setAgentStore("current", value.name)
})
},
color(name: string) {
Expand Down Expand Up @@ -179,6 +195,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({

const currentModel = createMemo(() => {
const a = agent.current()
if (!a) return fallbackModel() ?? undefined
return (
getFirstValidModel(
() => modelStore.model[a.name],
Expand Down Expand Up @@ -227,7 +244,9 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
if (next >= recent.length) next = 0
const val = recent[next]
if (!val) return
setModelStore("model", agent.current().name, { ...val })
const currentAgent = agent.current()
if (!currentAgent) return
setModelStore("model", currentAgent.name, { ...val })
},
cycleFavorite(direction: 1 | -1) {
const favorites = modelStore.favorite.filter((item) => isModelValid(item))
Expand All @@ -253,7 +272,9 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
}
const next = favorites[index]
if (!next) return
setModelStore("model", agent.current().name, { ...next })
const currentAgent = agent.current()
if (!currentAgent) return
setModelStore("model", currentAgent.name, { ...next })
const uniq = uniqueBy([next, ...modelStore.recent], (x) => `${x.providerID}/${x.modelID}`)
if (uniq.length > 10) uniq.pop()
setModelStore(
Expand All @@ -272,7 +293,9 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
})
return
}
setModelStore("model", agent.current().name, model)
const currentAgent = agent.current()
if (!currentAgent) return
setModelStore("model", currentAgent.name, model)
if (options?.recent) {
const uniq = uniqueBy([model, ...modelStore.recent], (x) => `${x.providerID}/${x.modelID}`)
if (uniq.length > 10) uniq.pop()
Expand Down Expand Up @@ -368,6 +391,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
// Automatically update model when agent changes
createEffect(() => {
const value = agent.current()
if (!value) return
if (value.model) {
if (isModelValid(value.model))
model.set({
Expand Down