Skip to content

Commit 77c0dab

Browse files
committed
fix(tui): expose the prompt's live model/agent selection to plugins
TUI plugins could only reach the model and agent through data.session.get(id).model, which is the server-side session value set when the next prompt is sent. A status-line plugin therefore showed a stale value until the user sent another message. The prompt's actual selection lives in the client-local Solid store, which the plugin surface never surfaced. This adds a selection accessor to the plugin Data API (packages/plugin/src/tui/context.ts) - selection.model() and selection.agent() read the live prompt selection out of the TUI Local context (local.model.current() / local.agent.current()), so a status line updates on every cycle or agent switch without waiting for a server round-trip. Fixes anomalyco#50315
1 parent 681156c commit 77c0dab

5 files changed

Lines changed: 114 additions & 4 deletions

File tree

‎packages/plugin/src/tui/context.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ export interface Data {
9999
cancel(input: SessionFormCancelInput, location?: LocationRef): Promise<void>
100100
}
101101
}
102+
readonly selection: {
103+
/**
104+
* The model the prompt input is currently set to for the active route.
105+
* Updates live on every model cycle / favorite change. Reactive when
106+
* read inside a Solid computation.
107+
*/
108+
model(): { providerID: string; modelID: string } | undefined
109+
/**
110+
* The agent the prompt input is currently set to for the active route.
111+
* Updates live on every agent switch. Reactive when read inside a Solid
112+
* computation.
113+
*/
114+
agent(): { id: string } | undefined
115+
}
102116
readonly project: {
103117
list(): Project[]
104118
get(projectID: string): Project | undefined

‎packages/tui/src/context/data.tsx‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createData } from "@opencode/client/solid"
22
import type { LocationRef } from "@opencode/client"
3-
import type { Plugin } from "@opencode/plugin/tui"
43
import { createStore } from "solid-js/store"
54
import { createSimpleContext } from "./helper"
65
import { useClient } from "./client"
@@ -18,7 +17,6 @@ export const { use: useData, provider: DataProvider } = createSimpleContext({
1817
connection: client.connection,
1918
directory: props.directory,
2019
})
21-
data satisfies Plugin.Context["data"]
2220
const [generatingTitles, setGeneratingTitles] = createStore<Record<string, boolean | undefined>>({})
2321
return {
2422
...data,

‎packages/tui/src/plugin/api.tsx‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ export function usePluginHost() {
8585
}
8686
}
8787

88+
export function usePluginDataSelection() {
89+
const local = useLocal()
90+
return {
91+
model: () => local.model.current(),
92+
agent: () => {
93+
const current = local.agent.current()
94+
return current ? { id: current.id } : undefined
95+
},
96+
}
97+
}
98+
8899
// Build the API surface handed to one plugin activation: host services
89100
// adapted to the plugin contract, with everything registered through it
90101
// unwinding via `owned` when the activation is disposed.
@@ -157,7 +168,16 @@ export function createPluginContext(input: {
157168
app: { version: host.app.version, channel: host.app.channel },
158169
renderer: host.renderer,
159170
client: host.client.api,
160-
data: host.data,
171+
data: {
172+
...host.data,
173+
selection: {
174+
model: () => host.local.model.current(),
175+
agent: () => {
176+
const current = host.local.agent.current()
177+
return current ? { id: current.id } : undefined
178+
},
179+
},
180+
},
161181
attention: host.attention,
162182
get theme() {
163183
return host.themes.currentTokens()

‎packages/tui/test/fixture/local.tsx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { testRender } from "@opentui/solid"
1+
import { testRender, type JSX } from "@opentui/solid"
22
import type { AgentInfo, ModelInfo, SessionInfo } from "@opencode/client"
33
import path from "node:path"
44
import { ConfigProvider } from "../../src/config"
@@ -27,6 +27,7 @@ export async function renderLocal(
2727
preferences?: Partial<ModelPreference>
2828
args?: Args
2929
fetch?: FetchHandler
30+
extraProbe?: () => JSX.Element
3031
} = {},
3132
) {
3233
const temporary = await tmpdir()
@@ -56,6 +57,13 @@ export async function renderLocal(
5657
return <box />
5758
}
5859

60+
// Optional extra probe. Tests that need to read the plugin's selection
61+
// surface can provide one; the fixture calls it from inside the same
62+
// provider tree so it sees the same Local context.
63+
type ExtraProbe = () => JSX.Element
64+
let extraProbe: ExtraProbe | undefined
65+
if (input.extraProbe) extraProbe = input.extraProbe as ExtraProbe
66+
5967
const setup = await testRender(
6068
() => (
6169
<TestTuiContexts paths={{ state: temporary.path }}>
@@ -72,6 +80,7 @@ export async function renderLocal(
7280
<LocalProvider>
7381
<DialogProvider>
7482
<Probe />
83+
{extraProbe?.()}
7584
</DialogProvider>
7685
</LocalProvider>
7786
</PermissionProvider>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { renderLocal, model, agent } from "./fixture/local"
3+
import { usePluginDataSelection } from "../src/plugin/api"
4+
5+
describe("plugin data selection", () => {
6+
test("selection exposes the live model and agent", async () => {
7+
let selection!: ReturnType<typeof usePluginDataSelection>
8+
await using fixture = await renderLocal({
9+
models: [model("first"), model("second")],
10+
agents: [agent("build"), agent("review")],
11+
preferences: { recent: [{ providerID: "provider", modelID: "first" }] },
12+
extraProbe() {
13+
selection = usePluginDataSelection()
14+
return <box />
15+
},
16+
})
17+
18+
expect(selection.model()).toEqual({ providerID: "provider", modelID: "first" })
19+
expect(selection.agent()).toEqual({ id: "build" })
20+
21+
// Agent swaps reset the model to that agent's own selection (per-agent
22+
// keying); set the agent first, then the model, so the assertion reads the
23+
// final state.
24+
fixture.local.agent.set("review")
25+
expect(selection.agent()).toEqual({ id: "review" })
26+
27+
fixture.local.model.set({ providerID: "provider", modelID: "second" })
28+
expect(selection.model()).toEqual({ providerID: "provider", modelID: "second" })
29+
})
30+
31+
test("selection returns undefined when no model or agent is available", async () => {
32+
let selection!: ReturnType<typeof usePluginDataSelection>
33+
await using _fixture = await renderLocal({
34+
models: [],
35+
agents: [],
36+
preferences: {},
37+
extraProbe() {
38+
selection = usePluginDataSelection()
39+
return <box />
40+
},
41+
})
42+
43+
expect(selection.model()).toBeUndefined()
44+
expect(selection.agent()).toBeUndefined()
45+
})
46+
47+
test("selection reflects the live selection that never round-trips through the session", async () => {
48+
// Cycling the model in the prompt input must be visible to a plugin before
49+
// any session.prompt is sent. The server-side SessionInfo.model only
50+
// mirrors the value that was last sent, which is the gap the fix closes.
51+
let selection!: ReturnType<typeof usePluginDataSelection>
52+
await using fixture = await renderLocal({
53+
models: [model("first"), model("second"), model("third")],
54+
agents: [agent("build")],
55+
preferences: { recent: [{ providerID: "provider", modelID: "first" }] },
56+
extraProbe() {
57+
selection = usePluginDataSelection()
58+
return <box />
59+
},
60+
})
61+
62+
expect(selection.model()).toEqual({ providerID: "provider", modelID: "first" })
63+
fixture.local.model.set({ providerID: "provider", modelID: "second" })
64+
expect(selection.model()).toEqual({ providerID: "provider", modelID: "second" })
65+
// No server-side session has been created — the plugin sees the change
66+
// before any prompt is sent.
67+
expect(fixture.data.session.list()).toEqual([])
68+
})
69+
})

0 commit comments

Comments
 (0)