Skip to content
Closed
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
46 changes: 34 additions & 12 deletions packages/opencode/src/cli/cmd/tui/context/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
const agents = createMemo(() => sync.data.agent.filter((x) => x.mode !== "subagent" && !x.hidden))
const visibleAgents = createMemo(() => sync.data.agent.filter((x) => !x.hidden))
const [agentStore, setAgentStore] = createStore<{
current: string
current?: string
}>({
current: agents()[0].name,
current: undefined,
})
const { theme } = useTheme()
const colors = createMemo(() => [
Expand All @@ -52,12 +52,28 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
theme.error,
theme.info,
])
const selected = createMemo(() => {
const name = agentStore.current
if (name) {
const match = agents().find((x) => x.name === name)
if (match) return match
}
return agents()[0]
})
createEffect(() => {
const name = selected()?.name
if (name === agentStore.current) return
setAgentStore("current", name)
})
return {
list() {
return agents()
},
maybe() {
return selected()
},
current() {
return agents().find((x) => x.name === agentStore.current)!
return selected()!
},
set(name: string) {
if (!agents().some((x) => x.name === name))
Expand All @@ -70,10 +86,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]
const list = agents()
if (!list.length) return
let next = list.findIndex((x) => x.name === selected()?.name) + direction
if (next < 0) next = list.length - 1
if (next >= list.length) next = 0
const value = list[next]
if (!value) return
setAgentStore("current", value.name)
})
},
Expand Down Expand Up @@ -191,11 +210,11 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
})

const currentModel = createMemo(() => {
const a = agent.current()
const a = agent.maybe()
return (
getFirstValidModel(
() => modelStore.model[a.name],
() => a.model,
() => a && modelStore.model[a.name],
() => a?.model,
fallbackModel,
) ?? undefined
)
Expand Down Expand Up @@ -386,8 +405,8 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({

// Automatically update model when agent changes
createEffect(() => {
const value = agent.current()
if (value.model) {
const value = agent.maybe()
if (value?.model) {
if (isModelValid(value.model))
model.set({
providerID: value.model.providerID,
Expand All @@ -403,6 +422,9 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
})

const result = {
get ready() {
return agent.list().length > 0
},
model,
agent,
mcp,
Expand Down
Loading