Skip to content

Commit ad1078a

Browse files
committed
Add /fork slash command for keyboard-friendly session forking
- Create DialogForkTimeline component that shows timeline and forks on selection - Add /fork command to autocomplete that opens fork timeline dialog - Register session.fork command with keybind support (defaults to none) - Add session_fork keybind config option - Show toast notification when fork is created Closes anomalyco#5599
1 parent 72ebaeb commit ad1078a

4 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ export function Autocomplete(props: {
266266
description: "jump to message",
267267
onSelect: () => command.trigger("session.timeline"),
268268
},
269+
{
270+
display: "/fork",
271+
description: "fork from message",
272+
onSelect: () => command.trigger("session.fork"),
273+
},
269274
{
270275
display: "/thinking",
271276
description: "toggle thinking visibility",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { createMemo, onMount } from "solid-js"
2+
import { useSync } from "@tui/context/sync"
3+
import { DialogSelect, type DialogSelectOption } from "@tui/ui/dialog-select"
4+
import type { TextPart } from "@opencode-ai/sdk/v2"
5+
import { Locale } from "@/util/locale"
6+
import { useDialog } from "../../ui/dialog"
7+
import { useSDK } from "@tui/context/sdk"
8+
import { useRoute } from "@tui/context/route"
9+
import { useToast } from "../../ui/toast"
10+
11+
export function DialogForkTimeline(props: {
12+
sessionID: string
13+
onMove: (messageID: string) => void
14+
}) {
15+
const sync = useSync()
16+
const dialog = useDialog()
17+
const sdk = useSDK()
18+
const route = useRoute()
19+
const toast = useToast()
20+
21+
onMount(() => {
22+
dialog.setSize("large")
23+
})
24+
25+
const options = createMemo((): DialogSelectOption<string>[] => {
26+
const messages = sync.data.message[props.sessionID] ?? []
27+
const result = [] as DialogSelectOption<string>[]
28+
for (const message of messages) {
29+
if (message.role !== "user") continue
30+
const part = (sync.data.part[message.id] ?? []).find((x) => x.type === "text" && !x.synthetic) as TextPart
31+
if (!part) continue
32+
result.push({
33+
title: part.text.replace(/\n/g, " "),
34+
value: message.id,
35+
footer: Locale.time(message.time.created),
36+
onSelect: async (dialog) => {
37+
const result = await sdk.client.session.fork({
38+
sessionID: props.sessionID,
39+
messageID: message.id,
40+
})
41+
route.navigate({
42+
sessionID: result.data!.id,
43+
type: "session",
44+
})
45+
toast.show({ message: "Forked session created", variant: "success" })
46+
dialog.clear()
47+
},
48+
})
49+
}
50+
result.reverse()
51+
return result
52+
})
53+
54+
return <DialogSelect onMove={(option) => props.onMove(option.value)} title="Fork from message" options={options()} />
55+
}

‎packages/opencode/src/cli/cmd/tui/routes/session/index.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { iife } from "@/util/iife"
5353
import { DialogConfirm } from "@tui/ui/dialog-confirm"
5454
import { DialogPrompt } from "@tui/ui/dialog-prompt"
5555
import { DialogTimeline } from "./dialog-timeline"
56+
import { DialogForkTimeline } from "./dialog-fork-timeline"
5657
import { DialogSessionRename } from "../../component/dialog-session-rename"
5758
import { Sidebar } from "./sidebar"
5859
import { LANGUAGE_EXTENSIONS } from "@/lsp/language"
@@ -295,6 +296,25 @@ export function Session() {
295296
))
296297
},
297298
},
299+
{
300+
title: "Fork from message",
301+
value: "session.fork",
302+
keybind: "session_fork",
303+
category: "Session",
304+
onSelect: (dialog) => {
305+
dialog.replace(() => (
306+
<DialogForkTimeline
307+
onMove={(messageID) => {
308+
const child = scroll.getChildren().find((child) => {
309+
return child.id === messageID
310+
})
311+
if (child) scroll.scrollBy(child.y - scroll.y - 1)
312+
}}
313+
sessionID={route.sessionID}
314+
/>
315+
))
316+
},
317+
},
298318
{
299319
title: "Compact session",
300320
value: "session.compact",

‎packages/opencode/src/config/config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ export namespace Config {
433433
session_new: z.string().optional().default("<leader>n").describe("Create a new session"),
434434
session_list: z.string().optional().default("<leader>l").describe("List all sessions"),
435435
session_timeline: z.string().optional().default("<leader>g").describe("Show session timeline"),
436+
session_fork: z.string().optional().default("none").describe("Fork session from message"),
436437
session_share: z.string().optional().default("none").describe("Share current session"),
437438
session_unshare: z.string().optional().default("none").describe("Unshare current session"),
438439
session_interrupt: z.string().optional().default("escape").describe("Interrupt current session"),

0 commit comments

Comments
 (0)