Skip to content

Commit c35c211

Browse files
authored
feat(tui): expose model variant selection to plugins (anomalyco#51101)
1 parent efb8e23 commit c35c211

4 files changed

Lines changed: 107 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,16 @@ export interface UI {
499499
/** Closes an open tab, or the active tab when omitted, and returns false when no tab matched. */
500500
close(sessionID?: string): boolean
501501
}
502+
readonly model: {
503+
/** The prompt's selected model; variant is undefined for the model default. Reactive when read in a Solid computation. */
504+
current(): { readonly providerID: string; readonly modelID: string; readonly variant?: string } | undefined
505+
readonly variant: {
506+
/** Variant IDs of the selected model. Reactive when read in a Solid computation. */
507+
list(): readonly string[]
508+
/** Selects a variant of the selected model, or the model default when undefined. Returns false when no model is selected or the variant is unavailable. */
509+
set(variant: string | undefined): boolean
510+
}
511+
}
502512
/** Claims a place in the slot tree; see SlotClaim. */
503513
readonly slot: (claim: SlotClaim) => () => void
504514
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useAttention } from "../context/attention"
2121
import { useStorage } from "../context/storage"
2222
import { useSessionTabs } from "../context/session-tabs"
2323
import { useOptionalPanel } from "../context/panel"
24+
import { useLocal } from "../context/local"
2425
import { abbreviateHome } from "../util/path-format"
2526

2627
export type Dispose = () => Promise<void>
@@ -70,6 +71,7 @@ export function usePluginHost() {
7071
storage: useStorage(),
7172
sessionTabs: useSessionTabs(),
7273
panel: useOptionalPanel(),
74+
local: useLocal(),
7375
}
7476
}
7577

@@ -249,6 +251,22 @@ export function createPluginContext(input: {
249251
return true
250252
},
251253
},
254+
model: {
255+
current() {
256+
const selection = host.local.model.selection()
257+
if (!selection) return
258+
return { providerID: selection.providerID, modelID: selection.modelID, variant: selection.variant }
259+
},
260+
variant: {
261+
list: () => host.local.model.variant.list(),
262+
set(variant) {
263+
if (!host.local.model.selection()) return false
264+
if (variant !== undefined && !host.local.model.variant.list().includes(variant)) return false
265+
host.local.model.variant.set(variant)
266+
return true
267+
},
268+
},
269+
},
252270
slot(value: SlotClaim) {
253271
// Keys are counter-suffixed so one plugin may claim several places;
254272
// order within the plugin is registration order.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { expect, test } from "bun:test"
2+
import { createPluginContext, type Registry, type usePluginHost } from "../src/plugin/api"
3+
import { model, renderLocal } from "./fixture/local"
4+
5+
// Eagerly read host services need a shape; model access goes through the real LocalProvider.
6+
function pluginModel(local: ReturnType<typeof usePluginHost>["local"]) {
7+
const host = {
8+
app: {},
9+
client: {},
10+
keymap: {},
11+
shortcuts: {},
12+
keymapState: {},
13+
sessionTabs: {},
14+
local,
15+
} as unknown as ReturnType<typeof usePluginHost>
16+
const registry: Registry = { has: () => false, set() {}, remove() {}, active: () => true }
17+
return createPluginContext({ host, id: "test", options: undefined, owned: [], registry }).ui.model
18+
}
19+
20+
test("plugins read and select variants of the selected model", async () => {
21+
await using setup = await renderLocal({ models: [model("first", ["low", "high"])] })
22+
const selected = pluginModel(setup.local)
23+
24+
expect(selected.current()).toEqual({ providerID: "provider", modelID: "first", variant: undefined })
25+
expect(selected.variant.list()).toEqual(["low", "high"])
26+
27+
expect(selected.variant.set("high")).toBe(true)
28+
expect(selected.current()?.variant).toBe("high")
29+
expect(setup.local.model.variant.current()).toBe("high")
30+
31+
expect(selected.variant.set(undefined)).toBe(true)
32+
expect(selected.current()?.variant).toBeUndefined()
33+
})
34+
35+
test("plugins cannot select unavailable variants or variants without a model", async () => {
36+
await using setup = await renderLocal({ models: [model("first", ["low", "high"])] })
37+
const selected = pluginModel(setup.local)
38+
expect(selected.variant.set("max")).toBe(false)
39+
expect(selected.current()?.variant).toBeUndefined()
40+
41+
await using empty = await renderLocal({ models: [] })
42+
const none = pluginModel(empty.local)
43+
expect(none.current()).toBeUndefined()
44+
expect(none.variant.list()).toEqual([])
45+
expect(none.variant.set(undefined)).toBe(false)
46+
})

‎services/www/src/docs/content/build/plugins/cli.mdx‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,39 @@ if (context.ui.tabs.enabled()) {
389389
}
390390
```
391391

392+
## Model
393+
394+
Read the prompt's selected model and change its variant. `current()` and `variant.list()` are reactive when read in a
395+
Solid computation. A `variant` of `undefined` is the model default.
396+
397+
```ts
398+
const selected = context.ui.model.current()
399+
const variants = context.ui.model.variant.list()
400+
context.ui.model.variant.set("high")
401+
context.ui.model.variant.set(undefined)
402+
```
403+
404+
`variant.set` returns `false` when no model is selected or the model has no such variant. Bind it to a command to
405+
replace the built-in variant cycle, for example to skip the model default:
406+
407+
```ts
408+
context.keymap.layer(() => ({
409+
commands: [
410+
{
411+
id: "acme.variant.cycle",
412+
title: "Cycle variants",
413+
bind: "tab",
414+
run() {
415+
const variants = context.ui.model.variant.list()
416+
if (variants.length === 0) return
417+
const index = variants.indexOf(context.ui.model.current()?.variant ?? "")
418+
context.ui.model.variant.set(variants[(index + 1) % variants.length])
419+
},
420+
},
421+
],
422+
}))
423+
```
424+
392425
## Slots
393426

394427
Slots insert or replace JSX at `app`, `home.footer`, `home.footer.status`, `prompt.footer`, `prompt.footer.status`,

0 commit comments

Comments
 (0)