Skip to content

Commit fbd9b7c

Browse files
authored
feat(app): restore to message and fork session (#17092)
1 parent 58f45ae commit fbd9b7c

9 files changed

Lines changed: 328 additions & 6 deletions

File tree

packages/app/src/i18n/en.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@ export const dict = {
530530
"session.todo.title": "Todos",
531531
"session.todo.collapse": "Collapse",
532532
"session.todo.expand": "Expand",
533+
"session.revertDock.summary.one": "{{count}} rolled back message",
534+
"session.revertDock.summary.other": "{{count}} rolled back messages",
535+
"session.revertDock.collapse": "Collapse rolled back messages",
536+
"session.revertDock.expand": "Expand rolled back messages",
537+
"session.revertDock.restore": "Restore message",
533538

534539
"session.new.title": "Build anything",
535540
"session.new.worktree.main": "Main branch",

packages/app/src/pages/session.tsx

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { SessionSidePanel } from "@/pages/session/session-side-panel"
4343
import { TerminalPanel } from "@/pages/session/terminal-panel"
4444
import { useSessionCommands } from "@/pages/session/use-session-commands"
4545
import { useSessionHashScroll } from "@/pages/session/use-session-hash-scroll"
46+
import { extractPromptFromParts } from "@/utils/prompt"
4647
import { same } from "@/utils/same"
4748
import { formatServerError } from "@/utils/server-errors"
4849

@@ -286,6 +287,7 @@ export default function Page() {
286287
const [ui, setUi] = createStore({
287288
git: false,
288289
pendingMessage: undefined as string | undefined,
290+
restoring: undefined as string | undefined,
289291
reviewSnap: false,
290292
scrollGesture: 0,
291293
scroll: {
@@ -1179,6 +1181,110 @@ export default function Page() {
11791181
scroller: () => scroller,
11801182
})
11811183

1184+
const draft = (id: string) =>
1185+
extractPromptFromParts(sync.data.part[id] ?? [], {
1186+
directory: sdk.directory,
1187+
attachmentName: language.t("common.attachment"),
1188+
})
1189+
1190+
const line = (id: string) => {
1191+
const text = draft(id)
1192+
.map((part) => (part.type === "image" ? `[image:${part.filename}]` : part.content))
1193+
.join("")
1194+
.replace(/\s+/g, " ")
1195+
.trim()
1196+
if (text) return text
1197+
return `[${language.t("common.attachment")}]`
1198+
}
1199+
1200+
const fail = (err: unknown) => {
1201+
showToast({
1202+
variant: "error",
1203+
title: language.t("common.requestFailed"),
1204+
description: formatServerError(err, language.t),
1205+
})
1206+
}
1207+
1208+
const busy = (sessionID: string) => {
1209+
if (sync.data.session_status[sessionID]?.type !== "idle") return true
1210+
return (sync.data.message[sessionID] ?? []).some(
1211+
(item) => item.role === "assistant" && typeof item.time.completed !== "number",
1212+
)
1213+
}
1214+
1215+
const halt = (sessionID: string) =>
1216+
busy(sessionID) ? sdk.client.session.abort({ sessionID }).catch(() => {}) : Promise.resolve()
1217+
1218+
const fork = (input: { sessionID: string; messageID: string }) => {
1219+
const value = draft(input.messageID)
1220+
return sdk.client.session
1221+
.fork(input)
1222+
.then((result) => {
1223+
const next = result.data
1224+
if (!next) {
1225+
showToast({
1226+
variant: "error",
1227+
title: language.t("common.requestFailed"),
1228+
})
1229+
return
1230+
}
1231+
navigate(`/${base64Encode(sdk.directory)}/session/${next.id}`)
1232+
requestAnimationFrame(() => {
1233+
prompt.set(value)
1234+
})
1235+
})
1236+
.catch(fail)
1237+
}
1238+
1239+
const revert = (input: { sessionID: string; messageID: string }) => {
1240+
const value = draft(input.messageID)
1241+
return halt(input.sessionID)
1242+
.then(() => sdk.client.session.revert(input))
1243+
.then(() => {
1244+
prompt.set(value)
1245+
})
1246+
.catch(fail)
1247+
}
1248+
1249+
const restore = (id: string) => {
1250+
const sessionID = params.id
1251+
if (!sessionID || ui.restoring) return
1252+
1253+
const next = userMessages().find((item) => item.id > id)
1254+
setUi("restoring", id)
1255+
1256+
const task = !next
1257+
? halt(sessionID)
1258+
.then(() => sdk.client.session.unrevert({ sessionID }))
1259+
.then(() => {
1260+
prompt.reset()
1261+
})
1262+
: halt(sessionID)
1263+
.then(() =>
1264+
sdk.client.session.revert({
1265+
sessionID,
1266+
messageID: next.id,
1267+
}),
1268+
)
1269+
.then(() => {
1270+
prompt.set(draft(next.id))
1271+
})
1272+
1273+
return task.catch(fail).finally(() => {
1274+
setUi("restoring", (value) => (value === id ? undefined : value))
1275+
})
1276+
}
1277+
1278+
const rolled = createMemo(() => {
1279+
const id = revertMessageID()
1280+
if (!id) return []
1281+
return userMessages()
1282+
.filter((item) => item.id >= id)
1283+
.map((item) => ({ id: item.id, text: line(item.id) }))
1284+
})
1285+
1286+
const actions = { fork, revert }
1287+
11821288
createResizeObserver(
11831289
() => promptDock,
11841290
({ height }) => {
@@ -1268,6 +1374,7 @@ export default function Page() {
12681374
loadingClass: "px-4 py-4 text-text-weak",
12691375
emptyClass: "h-full pb-64 -mt-4 flex flex-col items-center justify-center text-center gap-6",
12701376
})}
1377+
actions={actions}
12711378
scroll={ui.scroll}
12721379
onResumeScroll={resumeScroll}
12731380
setScrollRef={setScrollRef}
@@ -1333,6 +1440,15 @@ export default function Page() {
13331440
resumeScroll()
13341441
}}
13351442
onResponseSubmit={resumeScroll}
1443+
revert={
1444+
rolled().length > 0
1445+
? {
1446+
items: rolled(),
1447+
restoring: ui.restoring,
1448+
onRestore: restore,
1449+
}
1450+
: undefined
1451+
}
13361452
setPromptDockRef={(el) => {
13371453
promptDock = el
13381454
}}

packages/app/src/pages/session/composer/session-composer-region.tsx

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { usePrompt } from "@/context/prompt"
88
import { getSessionHandoff, setSessionHandoff } from "@/pages/session/handoff"
99
import { SessionPermissionDock } from "@/pages/session/composer/session-permission-dock"
1010
import { SessionQuestionDock } from "@/pages/session/composer/session-question-dock"
11+
import { SessionRevertDock } from "@/pages/session/composer/session-revert-dock"
1112
import type { SessionComposerState } from "@/pages/session/composer/session-composer-state"
1213
import { SessionTodoDock } from "@/pages/session/composer/session-todo-dock"
1314

@@ -20,6 +21,11 @@ export function SessionComposerRegion(props: {
2021
onNewSessionWorktreeReset: () => void
2122
onSubmit: () => void
2223
onResponseSubmit: () => void
24+
revert?: {
25+
items: { id: string; text: string }[]
26+
restoring?: string
27+
onRestore: (id: string) => void
28+
}
2329
setPromptDockRef: (el: HTMLDivElement) => void
2430
visualDuration?: number
2531
bounce?: number
@@ -116,6 +122,8 @@ export function SessionComposerRegion(props: {
116122
const value = createMemo(() => Math.max(0, Math.min(1, progress())))
117123
const [height, setHeight] = createSignal(320)
118124
const dock = createMemo(() => (gate.ready && props.state.dock()) || value() > 0.001)
125+
const rolled = createMemo(() => (props.revert?.items.length ? props.revert : undefined))
126+
const lift = createMemo(() => (rolled() ? 18 : 36 * value()))
119127
const full = createMemo(() => Math.max(78, height()))
120128
const [contentRef, setContentRef] = createSignal<HTMLDivElement>()
121129

@@ -170,9 +178,22 @@ export function SessionComposerRegion(props: {
170178
<Show
171179
when={prompt.ready()}
172180
fallback={
173-
<div class="w-full min-h-32 md:min-h-40 rounded-md border border-border-weak-base bg-background-base/50 px-4 py-3 text-text-weak whitespace-pre-wrap pointer-events-none">
174-
{handoffPrompt() || language.t("prompt.loading")}
175-
</div>
181+
<>
182+
<Show when={rolled()} keyed>
183+
{(revert) => (
184+
<div class="pb-2">
185+
<SessionRevertDock
186+
items={revert.items}
187+
restoring={revert.restoring}
188+
onRestore={revert.onRestore}
189+
/>
190+
</div>
191+
)}
192+
</Show>
193+
<div class="w-full min-h-32 md:min-h-40 rounded-md border border-border-weak-base bg-background-base/50 px-4 py-3 text-text-weak whitespace-pre-wrap pointer-events-none">
194+
{handoffPrompt() || language.t("prompt.loading")}
195+
</div>
196+
</>
176197
}
177198
>
178199
<Show when={dock()}>
@@ -209,12 +230,23 @@ export function SessionComposerRegion(props: {
209230
</div>
210231
</div>
211232
</Show>
233+
<Show when={rolled()} keyed>
234+
{(revert) => (
235+
<div
236+
style={{
237+
"margin-top": `${-36 * value()}px`,
238+
}}
239+
>
240+
<SessionRevertDock items={revert.items} restoring={revert.restoring} onRestore={revert.onRestore} />
241+
</div>
242+
)}
243+
</Show>
212244
<div
213245
classList={{
214246
"relative z-10": true,
215247
}}
216248
style={{
217-
"margin-top": `${-36 * value()}px`,
249+
"margin-top": `${-lift()}px`,
218250
}}
219251
>
220252
<PromptInput
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { For, Show, createMemo } from "solid-js"
2+
import { createStore } from "solid-js/store"
3+
import { Button } from "@opencode-ai/ui/button"
4+
import { DockTray } from "@opencode-ai/ui/dock-surface"
5+
import { IconButton } from "@opencode-ai/ui/icon-button"
6+
import { useLanguage } from "@/context/language"
7+
8+
export function SessionRevertDock(props: {
9+
items: { id: string; text: string }[]
10+
restoring?: string
11+
onRestore: (id: string) => void
12+
}) {
13+
const language = useLanguage()
14+
const [store, setStore] = createStore({
15+
collapsed: false,
16+
})
17+
18+
const toggle = () => setStore("collapsed", (value) => !value)
19+
const total = createMemo(() => props.items.length)
20+
const label = createMemo(() =>
21+
language.t(total() === 1 ? "session.revertDock.summary.one" : "session.revertDock.summary.other", {
22+
count: total(),
23+
}),
24+
)
25+
const preview = createMemo(() => props.items[0]?.text ?? "")
26+
27+
return (
28+
<DockTray data-component="session-revert-dock">
29+
<div
30+
class="pl-3 pr-2 py-2 flex items-center gap-2"
31+
role="button"
32+
tabIndex={0}
33+
onClick={toggle}
34+
onKeyDown={(event) => {
35+
if (event.key !== "Enter" && event.key !== " ") return
36+
event.preventDefault()
37+
toggle()
38+
}}
39+
>
40+
<span class="shrink-0 text-14-regular text-text-strong cursor-default">{label()}</span>
41+
<Show when={store.collapsed && preview()}>
42+
<span class="min-w-0 flex-1 truncate text-14-regular text-text-base cursor-default">{preview()}</span>
43+
</Show>
44+
<div class="ml-auto shrink-0">
45+
<IconButton
46+
data-collapsed={store.collapsed ? "true" : "false"}
47+
icon="chevron-down"
48+
size="normal"
49+
variant="ghost"
50+
style={{ transform: `rotate(${store.collapsed ? 180 : 0}deg)` }}
51+
onMouseDown={(event) => {
52+
event.preventDefault()
53+
event.stopPropagation()
54+
}}
55+
onClick={(event) => {
56+
event.stopPropagation()
57+
toggle()
58+
}}
59+
aria-label={
60+
store.collapsed ? language.t("session.revertDock.expand") : language.t("session.revertDock.collapse")
61+
}
62+
/>
63+
</div>
64+
</div>
65+
66+
<Show when={store.collapsed}>
67+
<div class="h-5" aria-hidden="true" />
68+
</Show>
69+
70+
<Show when={!store.collapsed}>
71+
<div class="px-3 pb-11 flex flex-col gap-1.5 max-h-42 overflow-y-auto no-scrollbar">
72+
<For each={props.items}>
73+
{(item) => (
74+
<div class="flex items-center gap-2 min-w-0 rounded-[10px] border border-border-weak-base bg-background-stronger px-2.5 py-2">
75+
<span class="min-w-0 flex-1 truncate text-13-regular text-text-strong">{item.text}</span>
76+
<Button
77+
size="small"
78+
variant="secondary"
79+
class="shrink-0"
80+
disabled={!!props.restoring}
81+
onClick={() => props.onRestore(item.id)}
82+
>
83+
{language.t("session.revertDock.restore")}
84+
</Button>
85+
</div>
86+
)}
87+
</For>
88+
</div>
89+
</Show>
90+
</DockTray>
91+
)
92+
}

packages/app/src/pages/session/message-timeline.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ type MessageComment = {
3636
const emptyMessages: MessageType[] = []
3737
const idle = { type: "idle" as const }
3838

39+
type UserActions = {
40+
fork?: (input: { sessionID: string; messageID: string }) => Promise<void> | void
41+
revert?: (input: { sessionID: string; messageID: string }) => Promise<void> | void
42+
}
43+
3944
const messageComments = (parts: Part[]): MessageComment[] =>
4045
parts.flatMap((part) => {
4146
if (part.type !== "text" || !(part as TextPart).synthetic) return []
@@ -186,6 +191,7 @@ function createTimelineStaging(input: TimelineStageInput) {
186191
export function MessageTimeline(props: {
187192
mobileChanges: boolean
188193
mobileFallback: JSX.Element
194+
actions?: UserActions
189195
scroll: { overflow: boolean; bottom: boolean }
190196
onResumeScroll: () => void
191197
setScrollRef: (el: HTMLDivElement | undefined) => void
@@ -805,6 +811,7 @@ export function MessageTimeline(props: {
805811
<SessionTurn
806812
sessionID={sessionID() ?? ""}
807813
messageID={messageID}
814+
actions={props.actions}
808815
active={active()}
809816
queued={queued()}
810817
status={active() ? sessionStatus() : undefined}

packages/ui/src/components/icon.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const icons = {
99
"bubble-5": `<path d="M18.3327 9.99935C18.3327 5.57227 15.0919 2.91602 9.99935 2.91602C4.90676 2.91602 1.66602 5.57227 1.66602 9.99935C1.66602 11.1487 2.45505 13.1006 2.57637 13.3939C2.58707 13.4197 2.59766 13.4434 2.60729 13.4697C2.69121 13.6987 3.04209 14.9354 1.66602 16.7674C3.51787 17.6528 5.48453 16.1973 5.48453 16.1973C6.84518 16.9193 8.46417 17.0827 9.99935 17.0827C15.0919 17.0827 18.3327 14.4264 18.3327 9.99935Z" stroke="currentColor" stroke-linecap="square"/>`,
1010
prompt: `<path d="M14.5841 12.0807H17.9193V2.91406H5.6276V6.2474M14.5859 6.2474H2.08594V15.4141H5.0026V17.4974L8.7526 15.4141H14.5859V6.2474Z" stroke="currentColor" stroke-linecap="square"/>`,
1111
brain: `<path d="M13.332 8.7487C11.4911 8.7487 9.9987 7.25631 9.9987 5.41536M6.66536 11.2487C8.50631 11.2487 9.9987 12.7411 9.9987 14.582M9.9987 2.78209L9.9987 17.0658M16.004 15.0475C17.1255 14.5876 17.9154 13.4849 17.9154 12.1978C17.9154 11.3363 17.5615 10.5575 16.9913 9.9987C17.5615 9.43991 17.9154 8.66108 17.9154 7.79962C17.9154 6.21199 16.7136 4.90504 15.1702 4.73878C14.7858 3.21216 13.4039 2.08203 11.758 2.08203C11.1171 2.08203 10.5162 2.25337 9.9987 2.55275C9.48117 2.25337 8.88032 2.08203 8.23944 2.08203C6.59353 2.08203 5.21157 3.21216 4.82722 4.73878C3.28377 4.90504 2.08203 6.21199 2.08203 7.79962C2.08203 8.66108 2.43585 9.43991 3.00609 9.9987C2.43585 10.5575 2.08203 11.3363 2.08203 12.1978C2.08203 13.4849 2.87191 14.5876 3.99339 15.0475C4.46688 16.7033 5.9917 17.9154 7.79962 17.9154C8.61335 17.9154 9.36972 17.6698 9.9987 17.2488C10.6277 17.6698 11.384 17.9154 12.1978 17.9154C14.0057 17.9154 15.5305 16.7033 16.004 15.0475Z" stroke="currentColor"/>`,
12+
fork: `<path d="M2.91602 7.91406L2.91602 2.91406H7.91602M12.0827 2.91406H17.0827L17.0827 7.91406M9.99935 9.9974L9.99935 17.0807M9.99935 9.9974L3.33268 3.33073M9.99935 9.9974L16.666 3.33073" stroke="currentColor" stroke-linecap="square"/>`,
1213
"bullet-list": `<path d="M9.58329 13.7497H17.0833M9.58329 6.24967H17.0833M6.24996 6.24967C6.24996 7.17015 5.50377 7.91634 4.58329 7.91634C3.66282 7.91634 2.91663 7.17015 2.91663 6.24967C2.91663 5.3292 3.66282 4.58301 4.58329 4.58301C5.50377 4.58301 6.24996 5.3292 6.24996 6.24967ZM6.24996 13.7497C6.24996 14.6701 5.50377 15.4163 4.58329 15.4163C3.66282 15.4163 2.91663 14.6701 2.91663 13.7497C2.91663 12.8292 3.66282 12.083 4.58329 12.083C5.50377 12.083 6.24996 12.8292 6.24996 13.7497Z" stroke="currentColor" stroke-linecap="square"/>`,
1314
"check-small": `<path d="M6.5 11.4412L8.97059 13.5L13.5 6.5" stroke="currentColor" stroke-linecap="square"/>`,
1415
"chevron-down": `<path d="M6.6665 8.33325L9.99984 11.6666L13.3332 8.33325" stroke="currentColor" stroke-linecap="square"/>`,
@@ -80,6 +81,7 @@ const icons = {
8081
selector: `<path d="M6.66626 12.5033L9.99959 15.8366L13.3329 12.5033M6.66626 7.50326L9.99959 4.16992L13.3329 7.50326" stroke="currentColor" stroke-linecap="square"/>`,
8182
"arrow-down-to-line": `<path d="M15.2083 11.6667L10 16.875L4.79167 11.6667M10 16.25V3.125" stroke="currentColor" stroke-width="1.25" stroke-linecap="square"/>`,
8283
warning: `<path d="M10 7.91667V11.6667M10 13.7417V13.75M10 2.5L1.875 16.25H18.125L10 2.5Z" stroke="currentColor" stroke-linecap="square"/>`,
84+
reset: `<path d="M5.83333 4.16406L2.5 7.4974L5.83333 10.8307M3.33333 7.4974H17.9167V15.4141H10" stroke="currentColor" stroke-linecap="square"/>`,
8385
link: `<path d="M2.08334 12.0833L1.72979 11.7298L1.37624 12.0833L1.72979 12.4369L2.08334 12.0833ZM7.91668 17.9167L7.56312 18.2702L7.91668 18.6238L8.27023 18.2702L7.91668 17.9167ZM17.9167 7.91666L18.2702 8.27022L18.6238 7.91666L18.2702 7.56311L17.9167 7.91666ZM12.0833 2.08333L12.4369 1.72977L12.0833 1.37622L11.7298 1.72977L12.0833 2.08333ZM8.39646 5.06311L8.0429 5.41666L8.75001 6.12377L9.10356 5.77021L8.75001 5.41666L8.39646 5.06311ZM5.77023 9.10355L6.12378 8.74999L5.41668 8.04289L5.06312 8.39644L5.41668 8.74999L5.77023 9.10355ZM14.2298 10.8964L13.8762 11.25L14.5833 11.9571L14.9369 11.6035L14.5833 11.25L14.2298 10.8964ZM11.6036 14.9369L11.9571 14.5833L11.25 13.8762L10.8965 14.2298L11.25 14.5833L11.6036 14.9369ZM7.14646 12.1464L6.7929 12.5L7.50001 13.2071L7.85356 12.8535L7.50001 12.5L7.14646 12.1464ZM12.8536 7.85355L13.2071 7.49999L12.5 6.79289L12.1465 7.14644L12.5 7.49999L12.8536 7.85355ZM2.08334 12.0833L1.72979 12.4369L7.56312 18.2702L7.91668 17.9167L8.27023 17.5631L2.4369 11.7298L2.08334 12.0833ZM17.9167 7.91666L18.2702 7.56311L12.4369 1.72977L12.0833 2.08333L11.7298 2.43688L17.5631 8.27022L17.9167 7.91666ZM12.0833 2.08333L11.7298 1.72977L8.39646 5.06311L8.75001 5.41666L9.10356 5.77021L12.4369 2.43688L12.0833 2.08333ZM5.41668 8.74999L5.06312 8.39644L1.72979 11.7298L2.08334 12.0833L2.4369 12.4369L5.77023 9.10355L5.41668 8.74999ZM14.5833 11.25L14.9369 11.6035L18.2702 8.27022L17.9167 7.91666L17.5631 7.56311L14.2298 10.8964L14.5833 11.25ZM7.91668 17.9167L8.27023 18.2702L11.6036 14.9369L11.25 14.5833L10.8965 14.2298L7.56312 17.5631L7.91668 17.9167ZM7.50001 12.5L7.85356 12.8535L12.8536 7.85355L12.5 7.49999L12.1465 7.14644L7.14646 12.1464L7.50001 12.5Z" fill="currentColor"/>`,
8486
providers: `<path d="M10.0001 4.37562V2.875M13 4.37793V2.87793M7.00014 4.37793V2.875M10 17.1279V15.6279M13 17.1279V15.6279M7 17.1279V15.6279M15.625 13.0029H17.125M15.625 7.00293H17.125M15.625 10.0029H17.125M2.875 10.0029H4.375M2.875 13.0029H4.375M2.875 7.00293H4.375M4.375 4.37793H15.625V15.6279H4.375V4.37793ZM12.6241 10.0022C12.6241 11.4519 11.4488 12.6272 9.99908 12.6272C8.54934 12.6272 7.37408 11.4519 7.37408 10.0022C7.37408 8.55245 8.54934 7.3772 9.99908 7.3772C11.4488 7.3772 12.6241 8.55245 12.6241 10.0022Z" stroke="currentColor" stroke-linecap="square"/>`,
8587
models: `<path fill-rule="evenodd" clip-rule="evenodd" d="M17.5 10C12.2917 10 10 12.2917 10 17.5C10 12.2917 7.70833 10 2.5 10C7.70833 10 10 7.70833 10 2.5C10 7.70833 12.2917 10 17.5 10Z" stroke="currentColor"/>`,

0 commit comments

Comments
 (0)