Skip to content

Commit d3a0059

Browse files
committed
feat: retain message content when forking session
Add initialPrompt support to SessionRoute to populate input box with selected message content when forking a session. - Extend SessionRoute type to support optional initialPrompt - Add effect in session component to handle initial prompt from fork - Modify fork action to extract message content and pass it to new session Fixes anomalyco#5495
1 parent f254cf7 commit d3a0059

3 files changed

Lines changed: 58 additions & 34 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/context/route.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type HomeRoute = {
1010
export type SessionRoute = {
1111
type: "session"
1212
sessionID: string
13+
initialPrompt?: PromptInfo
1314
}
1415

1516
export type Route = HomeRoute | SessionRoute

‎packages/opencode/src/cli/cmd/tui/routes/session/dialog-message.tsx‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,38 @@ export function DialogMessage(props: {
7171
dialog.clear()
7272
},
7373
},
74-
{
75-
title: "Fork",
76-
value: "session.fork",
77-
description: "create a new session",
78-
onSelect: async (dialog) => {
79-
const result = await sdk.client.session.fork({
80-
sessionID: props.sessionID,
81-
messageID: props.messageID,
82-
})
83-
route.navigate({
84-
sessionID: result.data!.id,
85-
type: "session",
86-
})
87-
dialog.clear()
88-
},
89-
},
74+
{
75+
title: "Fork",
76+
value: "session.fork",
77+
description: "create a new session",
78+
onSelect: async (dialog) => {
79+
const result = await sdk.client.session.fork({
80+
sessionID: props.sessionID,
81+
messageID: props.messageID,
82+
})
83+
const msg = message()
84+
let initialPrompt: PromptInfo | undefined
85+
if (msg) {
86+
const parts = sync.data.part[msg.id]
87+
initialPrompt = parts.reduce(
88+
(agg, part) => {
89+
if (part.type === "text") {
90+
if (!part.synthetic) agg.input += part.text
91+
}
92+
if (part.type === "file") agg.parts.push(part)
93+
return agg
94+
},
95+
{ input: "", parts: [] as PromptInfo["parts"] },
96+
)
97+
}
98+
route.navigate({
99+
sessionID: result.data!.id,
100+
type: "session",
101+
initialPrompt,
102+
})
103+
dialog.clear()
104+
},
105+
},
90106
]}
91107
/>
92108
)

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,31 @@ export function Session() {
144144
return new CustomSpeedScroll(3)
145145
})
146146

147-
createEffect(async () => {
148-
await sync.session
149-
.sync(route.sessionID)
150-
.then(() => {
151-
if (scroll) scroll.scrollBy(100_000)
152-
})
153-
.catch((e) => {
154-
console.error(e)
155-
toast.show({
156-
message: `Session not found: ${route.sessionID}`,
157-
variant: "error",
158-
})
159-
return navigate({ type: "home" })
160-
})
161-
})
162-
163-
const toast = useToast()
164-
const sdk = useSDK()
147+
createEffect(async () => {
148+
await sync.session
149+
.sync(route.sessionID)
150+
.then(() => {
151+
if (scroll) scroll.scrollBy(100_000)
152+
})
153+
.catch((e) => {
154+
console.error(e)
155+
toast.show({
156+
message: `Session not found: ${route.sessionID}`,
157+
variant: "error",
158+
})
159+
return navigate({ type: "home" })
160+
})
161+
})
162+
163+
const toast = useToast()
164+
const sdk = useSDK()
165+
166+
// Handle initial prompt from fork
167+
createEffect(() => {
168+
if (route.initialPrompt && prompt) {
169+
prompt.set(route.initialPrompt)
170+
}
171+
})
165172

166173
// Auto-navigate to whichever session currently needs permission input
167174
createEffect(() => {

0 commit comments

Comments
 (0)