mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(file): a file an agent shows takes no input for a moment after it appears
A workspace file that is not a page opens in the file panel, selected over whatever the pane showed, with nothing between a press already on its way and the file's links and buttons.
This commit is contained in:
parent
0d802601d4
commit
02eca641d5
5 changed files with 163 additions and 12 deletions
|
|
@ -171,7 +171,8 @@ export function createModel(ctx: SetupContext<typeof definition>) {
|
|||
const view = sessions.current()
|
||||
const files = filesPanel()
|
||||
|
||||
if (view?.key === effect.sessionKey && files.status === "active") files.value.open(view, effect.path, effect.select ? "select" : "append")
|
||||
// Nobody pressed anything for this file: the file panel holds a tab it selects this way behind its own guard.
|
||||
if (view?.key === effect.sessionKey && files.status === "active") files.value.open(view, effect.path, effect.select ? "select" : "append", true)
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
|||
45
packages/gui-plugins/src/file/arrival-guard-machine.test.ts
Normal file
45
packages/gui-plugins/src/file/arrival-guard-machine.test.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import { expect, test } from "bun:test"
|
||||
import {
|
||||
createArrivalGuardState,
|
||||
selectArrivalGuard,
|
||||
transitionArrivalGuard,
|
||||
type ArrivalGuardEvent,
|
||||
} from "./arrival-guard-machine"
|
||||
|
||||
const replay = (events: readonly ArrivalGuardEvent[], state = createArrivalGuardState()) =>
|
||||
events.reduce((current, event) => transitionArrivalGuard(current, event).state, state)
|
||||
|
||||
test("a file an agent selects takes no input until the delay after the person can see it", () => {
|
||||
const arrived = replay([{ type: "arrived", key: "a" }])
|
||||
const token = selectArrivalGuard(arrived, "a") ?? -1
|
||||
expect(token).toBe(1)
|
||||
expect(selectArrivalGuard(arrived, "b")).toBeUndefined()
|
||||
|
||||
// Another tab on show starts nothing; this tab starts the delay once.
|
||||
expect(transitionArrivalGuard(arrived, { type: "shown", key: "b" })).toEqual({ state: arrived, effects: [] })
|
||||
const shown = transitionArrivalGuard(arrived, { type: "shown", key: "a" })
|
||||
expect(shown.effects).toEqual([{ type: "expire", token }])
|
||||
expect(selectArrivalGuard(shown.state, "a")).toBe(token)
|
||||
expect(transitionArrivalGuard(shown.state, { type: "shown", key: "a" })).toEqual({ state: shown.state, effects: [] })
|
||||
|
||||
expect(selectArrivalGuard(replay([{ type: "elapsed", token }], shown.state), "a")).toBeUndefined()
|
||||
})
|
||||
|
||||
test("an old timer or one that never started cannot end a newer guard", () => {
|
||||
const first = replay([{ type: "arrived", key: "a" }, { type: "shown", key: "a" }])
|
||||
const second = replay([{ type: "arrived", key: "b" }], first)
|
||||
expect(selectArrivalGuard(second, "a")).toBeUndefined()
|
||||
expect(selectArrivalGuard(second, "b")).toBe(2)
|
||||
// The first arrival's timer fires, and a timer for the waiting guard was never started.
|
||||
expect(transitionArrivalGuard(second, { type: "elapsed", token: 1 }).state).toBe(second)
|
||||
expect(transitionArrivalGuard(second, { type: "elapsed", token: 2 }).state).toBe(second)
|
||||
})
|
||||
|
||||
test("the person's own selection ends the guard, and nothing changes without one", () => {
|
||||
const arrived = replay([{ type: "arrived", key: "a" }])
|
||||
expect(selectArrivalGuard(replay([{ type: "left" }], arrived), "a")).toBeUndefined()
|
||||
const none = createArrivalGuardState()
|
||||
|
||||
for (const event of [{ type: "shown", key: "a" }, { type: "elapsed", token: 1 }, { type: "left" }] as const)
|
||||
expect(transitionArrivalGuard(none, event).state).toBe(none)
|
||||
})
|
||||
59
packages/gui-plugins/src/file/arrival-guard-machine.ts
Normal file
59
packages/gui-plugins/src/file/arrival-guard-machine.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
* How long a file tab that an agent selected takes no input once the person can see it. A click or key press that was
|
||||
* on its way to what the pane showed before lands on nothing; the browser pane holds its pages back for as long.
|
||||
*/
|
||||
export const ARRIVAL_GUARD_MS = 500
|
||||
|
||||
/**
|
||||
* The file tab another plugin selected for an agent, in front of whatever the pane showed. `waiting` until the pane
|
||||
* shows the tab to the person, then `timing` for `ARRIVAL_GUARD_MS`. `token` tells one arrival's timer from the next.
|
||||
*/
|
||||
export type ArrivalGuardState =
|
||||
| { readonly status: "none"; readonly issued: number }
|
||||
| { readonly status: "waiting" | "timing"; readonly issued: number; readonly key: string; readonly token: number }
|
||||
|
||||
export type ArrivalGuardEvent =
|
||||
/** An agent's request selected the tab `key`, which the pane did not show. */
|
||||
| { readonly type: "arrived"; readonly key: string }
|
||||
/** The pane shows the tab `key` to the person. */
|
||||
| { readonly type: "shown"; readonly key: string }
|
||||
| { readonly type: "elapsed"; readonly token: number }
|
||||
/** The person selected a tab themselves, or closed this one: nothing is held for their own action. */
|
||||
| { readonly type: "left" }
|
||||
|
||||
/** Starts the delay after which `elapsed` ends the guard. */
|
||||
export type ArrivalGuardEffect = { readonly type: "expire"; readonly token: number }
|
||||
|
||||
export type ArrivalGuardTransition = {
|
||||
readonly state: ArrivalGuardState
|
||||
readonly effects: readonly ArrivalGuardEffect[]
|
||||
}
|
||||
|
||||
export function createArrivalGuardState(): ArrivalGuardState {
|
||||
return { status: "none", issued: 0 }
|
||||
}
|
||||
|
||||
export function transitionArrivalGuard(state: ArrivalGuardState, event: ArrivalGuardEvent): ArrivalGuardTransition {
|
||||
if (event.type === "arrived") {
|
||||
const token = state.issued + 1
|
||||
|
||||
return { state: { status: "waiting", issued: token, key: event.key, token }, effects: [] }
|
||||
}
|
||||
|
||||
if (state.status === "none") return { state, effects: [] }
|
||||
|
||||
if (event.type === "shown") {
|
||||
if (state.status !== "waiting" || state.key !== event.key) return { state, effects: [] }
|
||||
|
||||
return { state: { ...state, status: "timing" }, effects: [{ type: "expire", token: state.token }] }
|
||||
}
|
||||
|
||||
if (event.type === "elapsed" && (state.status !== "timing" || state.token !== event.token)) return { state, effects: [] }
|
||||
|
||||
return { state: { status: "none", issued: state.issued }, effects: [] }
|
||||
}
|
||||
|
||||
/** The token of the guard that holds the tab `key`, which takes no input while there is one. */
|
||||
export function selectArrivalGuard(state: ArrivalGuardState, key: string) {
|
||||
return state.status !== "none" && state.key === key ? state.token : undefined
|
||||
}
|
||||
|
|
@ -1,6 +1,10 @@
|
|||
import { Contract, type MountedSession } from "@opencode/plugin/gui"
|
||||
|
||||
/** Opens a workspace document as selectable source, without following its normal preview route. */
|
||||
/**
|
||||
* Opens a workspace document as selectable source, without following its normal preview route. `unprompted`: nobody
|
||||
* pressed anything to get it, as when an agent shows a file. A tab selected that way takes no input for a moment once
|
||||
* the person can see it, so a press already on its way to what the pane showed lands on nothing.
|
||||
*/
|
||||
export const FileSource = Contract.define<{
|
||||
open(session: MountedSession, path: string, tab?: "select" | "append"): void
|
||||
open(session: MountedSession, path: string, tab?: "select" | "append", unprompted?: boolean): void
|
||||
}, "file.source">("file.source")
|
||||
|
|
|
|||
|
|
@ -35,6 +35,13 @@ import { uuid } from "@opencode/util/uuid"
|
|||
import { resolveArtifactPath } from "./artifact"
|
||||
import { FileContext, type FileShared } from "./context"
|
||||
import { FileSource } from "./contract"
|
||||
import {
|
||||
ARRIVAL_GUARD_MS,
|
||||
createArrivalGuardState,
|
||||
selectArrivalGuard,
|
||||
transitionArrivalGuard,
|
||||
type ArrivalGuardEvent,
|
||||
} from "./arrival-guard-machine"
|
||||
import {
|
||||
FileTree,
|
||||
OpenInApp,
|
||||
|
|
@ -261,7 +268,22 @@ const setup: Plugin.Setup<typeof File> = (ctx) => {
|
|||
void screen.file.sync(path)
|
||||
}
|
||||
|
||||
ctx.provide(FileSource, { open(session, address, tab) {
|
||||
// The file tab an agent's show request put in front of the person (`arrival-guard-machine.ts`).
|
||||
const [arrival, setArrival] = createSignal(createArrivalGuardState())
|
||||
let arrivalTimer: ReturnType<typeof setTimeout> | undefined
|
||||
onCleanup(() => clearTimeout(arrivalTimer))
|
||||
|
||||
const dispatchArrival = (event: ArrivalGuardEvent) => {
|
||||
const next = transitionArrivalGuard(untrack(arrival), event)
|
||||
|
||||
setArrival(next.state)
|
||||
next.effects.forEach((effect) => {
|
||||
clearTimeout(arrivalTimer)
|
||||
arrivalTimer = setTimeout(() => dispatchArrival({ type: "elapsed", token: effect.token }), ARRIVAL_GUARD_MS)
|
||||
})
|
||||
}
|
||||
|
||||
ctx.provide(FileSource, { open(session, address, tab, unprompted) {
|
||||
const files = ctx.screen.current()?.file
|
||||
const path = files?.resolve(address)
|
||||
|
||||
|
|
@ -269,6 +291,10 @@ const setup: Plugin.Setup<typeof File> = (ctx) => {
|
|||
return ctx.toast.show({ variant: "error", title: ctx.t("load.failed") })
|
||||
|
||||
if (tab === "append" && layout.state(key(files, address), session) !== "visible") setUnseen(`${session.key}\n${fileTabId(files, address)}`, true)
|
||||
|
||||
// The selection changes what is under the person's pointer without a press of theirs.
|
||||
if (unprompted && tab === "select" && layout.state(key(files, address), session) !== "visible")
|
||||
dispatchArrival({ type: "arrived", key: `${session.key}\n${fileTabId(files, address)}` })
|
||||
openSource(session, address, tab ? { tab, background: true } : undefined)
|
||||
} })
|
||||
|
||||
|
|
@ -504,6 +530,8 @@ const setup: Plugin.Setup<typeof File> = (ctx) => {
|
|||
},
|
||||
close(input) {
|
||||
setUnseen(`${input.session.key}\n${input.tab.id}`, undefined)
|
||||
|
||||
if (selectArrivalGuard(arrival(), `${input.session.key}\n${input.tab.id}`) !== undefined) dispatchArrival({ type: "left" })
|
||||
tabs.get(input.screen)?.delete(input.tab.id)
|
||||
|
||||
if (isLauncher(input.tab.id))
|
||||
|
|
@ -514,21 +542,35 @@ const setup: Plugin.Setup<typeof File> = (ctx) => {
|
|||
render: (props) => {
|
||||
const panel = usePanel()
|
||||
createKeyed(() => panel.visible() && `${props.session.key}\n${props.tab.id}`, id => setUnseen(id, undefined))
|
||||
const guarded = createMemo(() => selectArrivalGuard(arrival(), `${props.session.key}\n${props.tab.id}`) !== undefined)
|
||||
|
||||
// The guard's delay starts when the person can see the tab, not while its pane is still closed or behind another.
|
||||
createKeyed(
|
||||
() => guarded() && panel.visible() && `${props.session.key}\n${props.tab.id}`,
|
||||
(key) => dispatchArrival({ type: "shown", key }),
|
||||
)
|
||||
|
||||
return (
|
||||
<FileProvider>
|
||||
<Suspense>
|
||||
<Show
|
||||
when={panel.placement() === "mobile"}
|
||||
fallback={<FileBrowser tab={() => props.tab} session={props.session} screen={props.screen} />}
|
||||
>
|
||||
<MobileFiles session={props.session} screen={props.screen} />
|
||||
</Show>
|
||||
</Suspense>
|
||||
{/* No box of its own: the tab an agent just put here takes no click or key until its guard ends. */}
|
||||
<div class="contents" inert={guarded()}>
|
||||
<Suspense>
|
||||
<Show
|
||||
when={panel.placement() === "mobile"}
|
||||
fallback={<FileBrowser tab={() => props.tab} session={props.session} screen={props.screen} />}
|
||||
>
|
||||
<MobileFiles session={props.session} screen={props.screen} />
|
||||
</Show>
|
||||
</Suspense>
|
||||
</div>
|
||||
</FileProvider>
|
||||
)
|
||||
},
|
||||
focus(input) {
|
||||
// The person selected another of the file tabs themselves: nothing is held for that.
|
||||
if (arrival().status !== "none" && selectArrivalGuard(arrival(), `${input.session.key}\n${input.tab.id}`) === undefined)
|
||||
dispatchArrival({ type: "left" })
|
||||
|
||||
if (!isFileTab(input.tab.id)) return
|
||||
|
||||
// A file the user selects on a wide screen is what the narrow-screen view shows too, not the browser. Narrow
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue