refactor(tui): inline reverted prompt mapping
This commit is contained in:
committed by
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
parent
71be35f7a0
commit
1302f229fe
@@ -6,7 +6,6 @@ import { useToast } from "../../ui/toast"
|
|||||||
import { useSDK } from "../../context/sdk"
|
import { useSDK } from "../../context/sdk"
|
||||||
import { errorMessage } from "../../util/error"
|
import { errorMessage } from "../../util/error"
|
||||||
import { DialogFork } from "./dialog-fork"
|
import { DialogFork } from "./dialog-fork"
|
||||||
import { revertedPrompt } from "../../util/revert-prompt"
|
|
||||||
import type { PromptInfo } from "../../prompt/history"
|
import type { PromptInfo } from "../../prompt/history"
|
||||||
|
|
||||||
export function DialogMessage(props: {
|
export function DialogMessage(props: {
|
||||||
@@ -30,7 +29,22 @@ export function DialogMessage(props: {
|
|||||||
description: "undo messages and file changes",
|
description: "undo messages and file changes",
|
||||||
onSelect: (dialog) => {
|
onSelect: (dialog) => {
|
||||||
const value = message()
|
const value = message()
|
||||||
if (value?.type === "user") props.setPrompt?.(revertedPrompt(value))
|
if (value?.type === "user") {
|
||||||
|
props.setPrompt?.({
|
||||||
|
text: value.text,
|
||||||
|
files: value.files?.map((file) => ({
|
||||||
|
uri: file.source.type === "uri" ? file.source.uri : `data:${file.mime};base64,${file.data}`,
|
||||||
|
name: file.name,
|
||||||
|
description: file.description,
|
||||||
|
mention: file.mention ? { ...file.mention } : undefined,
|
||||||
|
})),
|
||||||
|
agents: value.agents?.map((agent) => ({
|
||||||
|
name: agent.name,
|
||||||
|
mention: agent.mention ? { ...agent.mention } : undefined,
|
||||||
|
})),
|
||||||
|
pasted: [],
|
||||||
|
})
|
||||||
|
}
|
||||||
void sdk.api.session.revert
|
void sdk.api.session.revert
|
||||||
.stage({ sessionID: props.sessionID, messageID: props.messageID })
|
.stage({ sessionID: props.sessionID, messageID: props.messageID })
|
||||||
.catch((error) => toast.show({ message: errorMessage(error), variant: "error", duration: 5000 }))
|
.catch((error) => toast.show({ message: errorMessage(error), variant: "error", duration: 5000 }))
|
||||||
|
|||||||
@@ -75,7 +75,6 @@ import { usePathFormatter } from "../../context/path-format"
|
|||||||
import { LocationProvider } from "../../context/location"
|
import { LocationProvider } from "../../context/location"
|
||||||
import { createSessionRows, resolvePart, type PartRef, type SessionRow } from "./rows"
|
import { createSessionRows, resolvePart, type PartRef, type SessionRow } from "./rows"
|
||||||
import { switchLabel } from "../../util/model"
|
import { switchLabel } from "../../util/model"
|
||||||
import { revertedPrompt } from "../../util/revert-prompt"
|
|
||||||
|
|
||||||
addDefaultParsers(parsers.parsers)
|
addDefaultParsers(parsers.parsers)
|
||||||
|
|
||||||
@@ -425,7 +424,20 @@ export function Session() {
|
|||||||
void sdk.api.session.revert
|
void sdk.api.session.revert
|
||||||
.stage({ sessionID: route.sessionID, messageID: message.id })
|
.stage({ sessionID: route.sessionID, messageID: message.id })
|
||||||
.catch((error) => toast.show({ message: errorMessage(error), variant: "error", duration: 5000 }))
|
.catch((error) => toast.show({ message: errorMessage(error), variant: "error", duration: 5000 }))
|
||||||
prompt?.set(revertedPrompt(message))
|
prompt?.set({
|
||||||
|
text: message.text,
|
||||||
|
files: message.files?.map((file) => ({
|
||||||
|
uri: file.source.type === "uri" ? file.source.uri : `data:${file.mime};base64,${file.data}`,
|
||||||
|
name: file.name,
|
||||||
|
description: file.description,
|
||||||
|
mention: file.mention ? { ...file.mention } : undefined,
|
||||||
|
})),
|
||||||
|
agents: message.agents?.map((agent) => ({
|
||||||
|
name: agent.name,
|
||||||
|
mention: agent.mention ? { ...agent.mention } : undefined,
|
||||||
|
})),
|
||||||
|
pasted: [],
|
||||||
|
})
|
||||||
dialog.clear()
|
dialog.clear()
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
import type { SessionMessageUser } from "@opencode-ai/client"
|
|
||||||
import type { PromptInfo } from "../prompt/history"
|
|
||||||
|
|
||||||
export function revertedPrompt(message: SessionMessageUser): PromptInfo {
|
|
||||||
return {
|
|
||||||
text: message.text,
|
|
||||||
files: message.files?.map((file) => ({
|
|
||||||
uri: file.source.type === "uri" ? file.source.uri : `data:${file.mime};base64,${file.data}`,
|
|
||||||
name: file.name,
|
|
||||||
description: file.description,
|
|
||||||
mention: file.mention ? { ...file.mention } : undefined,
|
|
||||||
})),
|
|
||||||
agents: message.agents?.map((agent) => ({
|
|
||||||
name: agent.name,
|
|
||||||
mention: agent.mention ? { ...agent.mention } : undefined,
|
|
||||||
})),
|
|
||||||
pasted: [],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
import { describe, expect, test } from "bun:test"
|
|
||||||
import type { SessionMessageUser } from "@opencode-ai/client"
|
|
||||||
import { revertedPrompt } from "../../src/util/revert-prompt"
|
|
||||||
|
|
||||||
describe("reverted prompt", () => {
|
|
||||||
test("restores message text and references", () => {
|
|
||||||
const message = {
|
|
||||||
id: "message-1",
|
|
||||||
type: "user",
|
|
||||||
text: "Fix @test.ts with @review",
|
|
||||||
time: { created: 1 },
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
mime: "text/typescript",
|
|
||||||
name: "test.ts",
|
|
||||||
description: "test file",
|
|
||||||
data: "",
|
|
||||||
source: { type: "uri", uri: "file:///repo/test.ts" },
|
|
||||||
mention: { start: 4, end: 12, text: "@test.ts" },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
agents: [{ name: "review", mention: { start: 18, end: 25, text: "@review" } }],
|
|
||||||
} satisfies SessionMessageUser
|
|
||||||
|
|
||||||
expect(revertedPrompt(message)).toEqual({
|
|
||||||
text: "Fix @test.ts with @review",
|
|
||||||
files: [
|
|
||||||
{
|
|
||||||
uri: "file:///repo/test.ts",
|
|
||||||
name: "test.ts",
|
|
||||||
description: "test file",
|
|
||||||
mention: { start: 4, end: 12, text: "@test.ts" },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
agents: [{ name: "review", mention: { start: 18, end: 25, text: "@review" } }],
|
|
||||||
pasted: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user