Repository navigation
fix(tui): allow changing default agent effort from home (#291) #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9fc0918
4c4c897
5b27aa0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,8 @@ import { usePermission } from "./permission" | |||||||||||||||||||||||||||||||||||
| import { supportsVision } from "../util/model" | ||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||
| coupledThinking, | ||||||||||||||||||||||||||||||||||||
| defaultLevel, | ||||||||||||||||||||||||||||||||||||
| effortRecord, | ||||||||||||||||||||||||||||||||||||
| isRecord, | ||||||||||||||||||||||||||||||||||||
| mergeEffortMetadata, | ||||||||||||||||||||||||||||||||||||
| metadataLevel, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -633,6 +635,61 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({ | |||||||||||||||||||||||||||||||||||
| const writes = new Map<string, Promise<void>>() | ||||||||||||||||||||||||||||||||||||
| const [pendingVersion, setPendingVersion] = createSignal(0) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Home default for new sessions, persisted for the TUI in the state | ||||||||||||||||||||||||||||||||||||
| // directory. `stored` is the optimistic display value while `persisted` | ||||||||||||||||||||||||||||||||||||
| // tracks what the file actually holds, so a failed write can roll the | ||||||||||||||||||||||||||||||||||||
| // display back to a value that was really saved instead of reporting a | ||||||||||||||||||||||||||||||||||||
| // selection that never landed. | ||||||||||||||||||||||||||||||||||||
| const defaultFile = path.join(paths.state, "effort.json") | ||||||||||||||||||||||||||||||||||||
| const [stored, setStored] = createSignal<Level | undefined>() | ||||||||||||||||||||||||||||||||||||
| let persisted: Level | undefined | ||||||||||||||||||||||||||||||||||||
| let touched = false | ||||||||||||||||||||||||||||||||||||
| let defaultSequence = 0 | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let defaultWrites = readJson<unknown>(defaultFile) | ||||||||||||||||||||||||||||||||||||
| .then((value) => { | ||||||||||||||||||||||||||||||||||||
| const level = metadataLevel(value) | ||||||||||||||||||||||||||||||||||||
| if (!level) throw new Error("Invalid agent effort default") | ||||||||||||||||||||||||||||||||||||
| persisted = level | ||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: If the initial
Suggested change
Reply with |
||||||||||||||||||||||||||||||||||||
| // A selection made while the file was still loading wins over the | ||||||||||||||||||||||||||||||||||||
| // file: the user's choice is newer than what was on disk. | ||||||||||||||||||||||||||||||||||||
| if (!touched) setStored(level) | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| .catch((error) => { | ||||||||||||||||||||||||||||||||||||
| if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return | ||||||||||||||||||||||||||||||||||||
| toast.show({ | ||||||||||||||||||||||||||||||||||||
| variant: "warning", | ||||||||||||||||||||||||||||||||||||
| message: `Failed to load the agent effort default: ${errorMessage(error)}`, | ||||||||||||||||||||||||||||||||||||
| duration: 3000, | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| function persistDefault(level: Level) { | ||||||||||||||||||||||||||||||||||||
| touched = true | ||||||||||||||||||||||||||||||||||||
| setStored(level) | ||||||||||||||||||||||||||||||||||||
| const attempt = ++defaultSequence | ||||||||||||||||||||||||||||||||||||
| // Wait for the initial read before writing so it cannot replace a | ||||||||||||||||||||||||||||||||||||
| // newer rollback baseline. Then persist rapid selections in order. | ||||||||||||||||||||||||||||||||||||
| const write = defaultWrites.then(() => writeJsonAtomic(defaultFile, effortRecord(level))) | ||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING:
Suggested change
|
||||||||||||||||||||||||||||||||||||
| defaultWrites = write.catch(() => {}) | ||||||||||||||||||||||||||||||||||||
| write.then( | ||||||||||||||||||||||||||||||||||||
| () => { | ||||||||||||||||||||||||||||||||||||
| persisted = level | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| (error) => { | ||||||||||||||||||||||||||||||||||||
| // Only the newest selection owns the displayed default; an older | ||||||||||||||||||||||||||||||||||||
| // failed write must not roll back a newer choice. | ||||||||||||||||||||||||||||||||||||
| if (attempt !== defaultSequence) return | ||||||||||||||||||||||||||||||||||||
| setStored(persisted) | ||||||||||||||||||||||||||||||||||||
| toast.show({ | ||||||||||||||||||||||||||||||||||||
| variant: "warning", | ||||||||||||||||||||||||||||||||||||
| message: `Failed to save the agent effort default: ${errorMessage(error)}`, | ||||||||||||||||||||||||||||||||||||
| duration: 3000, | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| function activeSessionID() { | ||||||||||||||||||||||||||||||||||||
| return route.data.type === "session" ? route.data.sessionID : undefined | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
@@ -658,26 +715,45 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({ | |||||||||||||||||||||||||||||||||||
| // write must be visible immediately, before the sync event that | ||||||||||||||||||||||||||||||||||||
| // confirms it lands on the session row. | ||||||||||||||||||||||||||||||||||||
| pendingVersion() | ||||||||||||||||||||||||||||||||||||
| // Home has no session to override, so the persisted new-session | ||||||||||||||||||||||||||||||||||||
| // default (then configuration) is what a new session would use. | ||||||||||||||||||||||||||||||||||||
| if (!sessionID) return defaultLevel({ selected: stored(), config: sync.data.config }) | ||||||||||||||||||||||||||||||||||||
| return effortSelection({ | ||||||||||||||||||||||||||||||||||||
| override: sessionID ? pending.get(sessionID) : undefined, | ||||||||||||||||||||||||||||||||||||
| override: pending.get(sessionID), | ||||||||||||||||||||||||||||||||||||
| metadata: row?.metadata, | ||||||||||||||||||||||||||||||||||||
| config: sync.data.config, | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| // Whether the selection is session-scoped right now. On the home | ||||||||||||||||||||||||||||||||||||
| // route there is no session to persist into, so the configured | ||||||||||||||||||||||||||||||||||||
| // default stays authoritative for new work. | ||||||||||||||||||||||||||||||||||||
| // route the persisted default is authoritative for new work. | ||||||||||||||||||||||||||||||||||||
| scoped() { | ||||||||||||||||||||||||||||||||||||
| return activeSessionID() !== undefined | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| // Metadata a session created right now starts from, so the home | ||||||||||||||||||||||||||||||||||||
| // default is stored on the session (and enforced by the server) | ||||||||||||||||||||||||||||||||||||
| // rather than only being displayed. Undefined when the user never | ||||||||||||||||||||||||||||||||||||
| // picked one, which leaves the configured default in charge. | ||||||||||||||||||||||||||||||||||||
| newSessionMetadata() { | ||||||||||||||||||||||||||||||||||||
| const level = stored() | ||||||||||||||||||||||||||||||||||||
| return level ? effortRecord(level) : undefined | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| // Show a newly created session's inherited selection immediately: | ||||||||||||||||||||||||||||||||||||
| // its row syncs a moment later, and reading through the same overlay | ||||||||||||||||||||||||||||||||||||
| // as a session write keeps the label from flashing the old default. | ||||||||||||||||||||||||||||||||||||
| seed(sessionID: string, metadata: Record<string, unknown> | undefined) { | ||||||||||||||||||||||||||||||||||||
| if (!metadata) return | ||||||||||||||||||||||||||||||||||||
| if (metadataLevel(sync.session.get(sessionID)?.metadata) === metadataLevel(metadata)) return | ||||||||||||||||||||||||||||||||||||
| pending.set(sessionID, metadata) | ||||||||||||||||||||||||||||||||||||
| setPendingVersion((version) => version + 1) | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| set(level: Level) { | ||||||||||||||||||||||||||||||||||||
| const sessionID = activeSessionID() | ||||||||||||||||||||||||||||||||||||
| if (!sessionID) { | ||||||||||||||||||||||||||||||||||||
| toast.show({ | ||||||||||||||||||||||||||||||||||||
| variant: "info", | ||||||||||||||||||||||||||||||||||||
| message: "Agent effort applies per session; open a session to change it.", | ||||||||||||||||||||||||||||||||||||
| duration: 3000, | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| // Home selection: a failed default write rolls the value back and | ||||||||||||||||||||||||||||||||||||
| // reports the failure, so the label never claims a level that is | ||||||||||||||||||||||||||||||||||||
| // not actually persisted. | ||||||||||||||||||||||||||||||||||||
| persistDefault(level) | ||||||||||||||||||||||||||||||||||||
| model.variant.couple(level) | ||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| const row = sync.session.get(sessionID) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||
| import { AgentEffort } from "@opencode-ai/schema/agent-effort" | ||||||
| import { RGBA } from "@opentui/core" | ||||||
|
|
||||||
| export type Level = AgentEffort.Level | ||||||
|
|
||||||
|
|
@@ -74,14 +75,68 @@ export function resolveEffort(input: { metadata?: unknown; config?: unknown; fal | |||||
| return metadataLevel(input.metadata) ?? configLevel(input.config) ?? AgentEffort.resolve(undefined, input.fallback) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * The TUI-level default new sessions start from: the selection persisted on | ||||||
| * home, then the configured `agent.effort`, then the schema fallback. The | ||||||
| * caller validates whatever it read from disk, so `selected` is already a | ||||||
| * level or nothing. | ||||||
| */ | ||||||
| export function defaultLevel(input: { selected?: Level; config?: unknown }): Level { | ||||||
| return input.selected ?? configLevel(input.config) ?? AgentEffort.resolve(undefined) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: The spec states the fallback chain is: persisted
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * A metadata record carrying exactly one persisted selection, used both for | ||||||
| * the session metadata write and for the TUI's persisted home default. | ||||||
| */ | ||||||
| export function effortRecord(level: Level): Record<string, unknown> { | ||||||
| return { [METADATA_KEY]: level } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * `session.update` replaces the metadata record wholesale, so every effort | ||||||
| * write merges over the current record to preserve unrelated keys. A | ||||||
| * non-record base (missing/corrupted metadata) collapses to the single key. | ||||||
| */ | ||||||
| export function mergeEffortMetadata(current: unknown, level: Level): Record<string, unknown> { | ||||||
| const base = isRecord(current) ? current : {} | ||||||
| return { ...base, [METADATA_KEY]: level } | ||||||
| return { ...base, ...effortRecord(level) } | ||||||
| } | ||||||
|
|
||||||
| // Effort hues: fast is green, medium is orange, thorough is red. Each level | ||||||
| // keeps two variants so the hue stays legible on both dark and light surfaces | ||||||
| // (the level names are always rendered next to the color, so the levels never | ||||||
| // depend on color alone). | ||||||
| const TONES: Record<Level, { dark: string; light: string }> = { | ||||||
| fast: { dark: "#3fb950", light: "#116329" }, | ||||||
| medium: { dark: "#f0883e", light: "#9a3412" }, | ||||||
| thorough: { dark: "#f85149", light: "#b3121f" }, | ||||||
| } | ||||||
|
|
||||||
| function luminance(color: RGBA) { | ||||||
| return 0.299 * color.r + 0.587 * color.g + 0.114 * color.b | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * The effort hue for a level, picked for the surface it is drawn on. A | ||||||
| * transparent surface means the terminal supplies the background, so the | ||||||
| * theme's text color stands in for it: readable light text implies a dark | ||||||
| * surface and therefore the bright hue variant. | ||||||
| */ | ||||||
| export function effortColor(level: Level, theme: { background: RGBA; text: RGBA }): RGBA { | ||||||
| const lightSurface = | ||||||
| theme.background.a === 0 ? luminance(theme.text) < 0.5 : luminance(theme.background) > 0.5 | ||||||
| return RGBA.fromHex(TONES[level][lightSurface ? "light" : "dark"]) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * The level's hue as a filled indicator plus the ink that stays readable on | ||||||
| * it, for surfaces (like a highlighted selector row) where the surrounding | ||||||
| * colors are not ours to choose. | ||||||
| */ | ||||||
| export function effortBadge(level: Level, theme: { background: RGBA; text: RGBA }): { fg: RGBA; bg: RGBA } { | ||||||
| const bg = effortColor(level, theme) | ||||||
| return { bg, fg: luminance(bg) > 0.5 ? RGBA.fromInts(0, 0, 0) : RGBA.fromInts(255, 255, 255) } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SUGGESTION: Verify
titleViewspan style props match renderer APIThe
<span style={{ fg: badge.fg, bg: badge.bg, bold: true }}>usesfg/bg/boldkeys. Confirm this matches the @opentui/solid JSX style property expectations (vscolor/backgroundColor/fontWeight). The test asserts the rendered colors, so it likely works, but worth a quick sanity check against other components.