|
| 1 | +import type { SessionMessageUser } from "@opencode/client/promise" |
| 2 | +import { useDialog } from "@opencode/ui/context/dialog" |
| 3 | +import { createEffect, createMemo, createSignal, onCleanup } from "solid-js" |
| 4 | +import { readPromptPresentation } from "@/composer/comment-note" |
| 5 | +import { useLanguage } from "@/runtime/i18n/language" |
| 6 | +import { useServerSDK } from "@/runtime/server/client" |
| 7 | +import { useData } from "@/runtime/server/current" |
| 8 | +import type { SessionCommandContext } from "./use-session-commands" |
| 9 | +import { parseVoiceDelegation } from "../timeline/voice-delegation" |
| 10 | +import { JumpToMessage, type JumpPrompt } from "./jump-to-message" |
| 11 | +import { |
| 12 | + selectJumpHistory, |
| 13 | + selectJumpOpening, |
| 14 | + selectJumpStatus, |
| 15 | + startJumpToMessage, |
| 16 | + transitionJumpToMessage, |
| 17 | + type JumpToMessageEffect, |
| 18 | + type JumpToMessageEvent, |
| 19 | +} from "./jump-to-message-machine" |
| 20 | + |
| 21 | +function prompt(message: SessionMessageUser, attachment: string): JumpPrompt { |
| 22 | + const presentation = readPromptPresentation(message.metadata) |
| 23 | + |
| 24 | + const text = presentation?.displayText ?? message.text |
| 25 | + |
| 26 | + const voice = parseVoiceDelegation(text) |
| 27 | + |
| 28 | + const labels = [ |
| 29 | + voice?.framing ?? voice?.instruction ?? text, |
| 30 | + ...(presentation?.attachments ?? []).map((file) => file.name), |
| 31 | + ...(message.files ?? []).filter((file) => !file.mention).map((file) => file.name || attachment), |
| 32 | + ...(message.agents ?? []).filter((agent) => !agent.mention).map((agent) => `@${agent.name}`), |
| 33 | + ...(message.skills ?? []).filter((skill) => !skill.mention).map((skill) => skill.name), |
| 34 | + ...(presentation?.comments ?? []).map((comment) => { |
| 35 | + if (comment.comment) return comment.comment |
| 36 | + |
| 37 | + if (comment.type === "quote") return comment.text |
| 38 | + |
| 39 | + if (comment.type === "note") return comment.subject |
| 40 | + |
| 41 | + return comment.path |
| 42 | + }), |
| 43 | + ] |
| 44 | + |
| 45 | + return { |
| 46 | + id: message.id, |
| 47 | + text: labels.filter(Boolean).join("\n"), |
| 48 | + created: message.time.created, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export function DialogJumpToMessage(props: { |
| 53 | + session: SessionCommandContext["session"] |
| 54 | + onJump: (id: string) => void |
| 55 | +}) { |
| 56 | + const dialog = useDialog() |
| 57 | + const language = useLanguage() |
| 58 | + const data = useData() |
| 59 | + const sdk = useServerSDK() |
| 60 | + const key = props.session.identity.sessionKey() |
| 61 | + const id = props.session.identity.params.id |
| 62 | + |
| 63 | + if (!id) return void dialog.close() |
| 64 | + let controller: AbortController | undefined |
| 65 | + const start = startJumpToMessage() |
| 66 | + |
| 67 | + const [state, setState] = createSignal(start.state) |
| 68 | + |
| 69 | + const dispatch = (event: JumpToMessageEvent) => { |
| 70 | + const next = transitionJumpToMessage(state(), event) |
| 71 | + |
| 72 | + if (next.state !== state()) setState(next.state) |
| 73 | + next.effects.forEach(run) |
| 74 | + } |
| 75 | + |
| 76 | + const run = (effect: JumpToMessageEffect) => { |
| 77 | + if (effect.type === "cancel") return controller?.abort() |
| 78 | + |
| 79 | + if (effect.type === "jump") { |
| 80 | + dialog.close() |
| 81 | + |
| 82 | + return props.onJump(effect.id) |
| 83 | + } |
| 84 | + |
| 85 | + controller?.abort() |
| 86 | + const current = new AbortController() |
| 87 | + |
| 88 | + controller = current |
| 89 | + |
| 90 | + if (effect.type === "history.load") |
| 91 | + return void loadHistory(current.signal).then( |
| 92 | + (history) => |
| 93 | + dispatch({ |
| 94 | + type: "history.loaded", |
| 95 | + attempt: effect.attempt, |
| 96 | + history, |
| 97 | + }), |
| 98 | + () => dispatch({ type: "history.failed", attempt: effect.attempt }), |
| 99 | + ) |
| 100 | + |
| 101 | + void reveal(effect.target, AbortSignal.any([current.signal, AbortSignal.timeout(60_000)])).then( |
| 102 | + () => dispatch({ type: "opened", attempt: effect.attempt }), |
| 103 | + () => dispatch({ type: "open.failed", attempt: effect.attempt }), |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + // Read older prompts into the picker only. Prepending pages to the timeline while browsing would move the reader. |
| 108 | + const loadHistory = async (signal: AbortSignal) => { |
| 109 | + const rows: JumpPrompt[] = [] |
| 110 | + |
| 111 | + // Join the timeline's initial sync before interpreting a missing cursor as the end of history. |
| 112 | + await data.session.message.sync(id) |
| 113 | + signal.throwIfAborted() |
| 114 | + |
| 115 | + if (!data.session.message.more(id)) return rows |
| 116 | + let cursor: string | undefined |
| 117 | + |
| 118 | + do { |
| 119 | + const page = await sdk.api.message.list( |
| 120 | + { sessionID: id, type: "user", limit: 200, cursor }, |
| 121 | + { signal: AbortSignal.any([signal, AbortSignal.timeout(30_000)]) }, |
| 122 | + ) |
| 123 | + |
| 124 | + signal.throwIfAborted() |
| 125 | + |
| 126 | + for (const message of page.data) { |
| 127 | + if (message.type === "user") rows.push(prompt(message, language.t("common.attachment"))) |
| 128 | + } |
| 129 | + |
| 130 | + cursor = page.cursor.next ?? undefined |
| 131 | + } while (cursor) |
| 132 | + |
| 133 | + return rows |
| 134 | + } |
| 135 | + |
| 136 | + // Page back only as far as the prompt, so the rest of a long session stays unloaded. |
| 137 | + const reveal = async (messageID: string, signal: AbortSignal) => { |
| 138 | + while (!data.session.message.get(id, messageID)) { |
| 139 | + if (!data.session.message.more(id)) throw new Error("Message unavailable") |
| 140 | + await data.session.message.loadMore(id, { signal }) |
| 141 | + signal.throwIfAborted() |
| 142 | + } |
| 143 | + |
| 144 | + if ( |
| 145 | + !props.session.history.visibleUserMessages().some((message) => message.id === messageID) || |
| 146 | + data.session.input.has(id, messageID) |
| 147 | + ) |
| 148 | + throw new Error("Message unavailable") |
| 149 | + } |
| 150 | + |
| 151 | + start.effects.forEach(run) |
| 152 | + onCleanup(() => dispatch({ type: "disposed" })) |
| 153 | + createEffect(() => { |
| 154 | + if (key !== props.session.identity.sessionKey() || dialog.closing) dispatch({ type: "disposed" }) |
| 155 | + |
| 156 | + if (key !== props.session.identity.sessionKey()) dialog.close() |
| 157 | + }) |
| 158 | + |
| 159 | + const prompts = createMemo(() => { |
| 160 | + const all = new Map(selectJumpHistory(state()).map((row) => [row.id, row])) |
| 161 | + |
| 162 | + const reverted = data.session.get(id)?.revert?.messageID |
| 163 | + |
| 164 | + for (const message of props.session.history.visibleUserMessages()) { |
| 165 | + all.set(message.id, prompt(message, language.t("common.attachment"))) |
| 166 | + } |
| 167 | + |
| 168 | + const pending = new Set(data.session.input.list(id)) |
| 169 | + |
| 170 | + return [...all.values()].filter( |
| 171 | + (row) => row.text.trim() && !pending.has(row.id) && (!reverted || row.id < reverted), |
| 172 | + ) |
| 173 | + }) |
| 174 | + |
| 175 | + const status = () => { |
| 176 | + const note = selectJumpStatus(state()) |
| 177 | + |
| 178 | + return note && language.t(`dialog.jump.${note}`) |
| 179 | + } |
| 180 | + |
| 181 | + return ( |
| 182 | + <JumpToMessage |
| 183 | + prompts={prompts()} |
| 184 | + opening={selectJumpOpening(state())} |
| 185 | + status={status()} |
| 186 | + onRetry={state().status === "failed" ? () => dispatch({ type: "retry" }) : undefined} |
| 187 | + onClose={() => dialog.close()} |
| 188 | + onJump={(row) => |
| 189 | + dispatch({ |
| 190 | + type: "selected", |
| 191 | + id: row.id, |
| 192 | + loaded: !!data.session.message.get(id, row.id), |
| 193 | + }) |
| 194 | + } |
| 195 | + /> |
| 196 | + ) |
| 197 | +} |
0 commit comments