|
| 1 | +import type { SessionMessageUser } from "@opencode/client/promise" |
| 2 | +import { useDialog } from "@opencode/ui/context/dialog" |
| 3 | +import { createEffect, createMemo, createResource, 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 | + |
| 12 | +function prompt(message: SessionMessageUser, attachment: string): JumpPrompt { |
| 13 | + const presentation = readPromptPresentation(message.metadata) |
| 14 | + |
| 15 | + const text = presentation?.displayText ?? message.text |
| 16 | + |
| 17 | + const voice = parseVoiceDelegation(text) |
| 18 | + |
| 19 | + const labels = [ |
| 20 | + voice?.framing ?? voice?.instruction ?? text, |
| 21 | + ...(presentation?.attachments ?? []).map((file) => file.name), |
| 22 | + ...(message.files ?? []).filter((file) => !file.mention).map((file) => file.name || attachment), |
| 23 | + ...(message.agents ?? []).filter((agent) => !agent.mention).map((agent) => `@${agent.name}`), |
| 24 | + ...(message.skills ?? []).filter((skill) => !skill.mention).map((skill) => skill.name), |
| 25 | + ...(presentation?.comments ?? []).map((comment) => { |
| 26 | + if (comment.comment) return comment.comment |
| 27 | + |
| 28 | + if (comment.type === "quote") return comment.text |
| 29 | + |
| 30 | + if (comment.type === "note") return comment.subject |
| 31 | + |
| 32 | + return comment.path |
| 33 | + }), |
| 34 | + ] |
| 35 | + |
| 36 | + return { id: message.id, text: labels.filter(Boolean).join("\n"), created: message.time.created } |
| 37 | +} |
| 38 | + |
| 39 | +export function DialogJumpToMessage(props: { |
| 40 | + session: SessionCommandContext["session"] |
| 41 | + onJump: (id: string) => void |
| 42 | +}) { |
| 43 | + const dialog = useDialog() |
| 44 | + const language = useLanguage() |
| 45 | + const data = useData() |
| 46 | + const sdk = useServerSDK() |
| 47 | + const key = props.session.identity.sessionKey() |
| 48 | + const id = props.session.identity.params.id |
| 49 | + |
| 50 | + if (!id) return void dialog.close() |
| 51 | + const controller = new AbortController() |
| 52 | + onCleanup(() => controller.abort()) |
| 53 | + createEffect(() => { |
| 54 | + if (key !== props.session.identity.sessionKey()) dialog.close() |
| 55 | + |
| 56 | + if (dialog.closing) controller.abort() |
| 57 | + }) |
| 58 | + |
| 59 | + // Read older prompts into the picker only. Prepending pages to the timeline while browsing would move the reader. |
| 60 | + const [history] = createResource(async () => { |
| 61 | + const rows: JumpPrompt[] = [] |
| 62 | + |
| 63 | + // Join the timeline's initial sync before interpreting a missing cursor as the end of history. |
| 64 | + await data.session.message.sync(id) |
| 65 | + controller.signal.throwIfAborted() |
| 66 | + |
| 67 | + if (!data.session.message.more(id)) return rows |
| 68 | + let cursor: string | undefined |
| 69 | + |
| 70 | + do { |
| 71 | + const page = await sdk.api.message.list( |
| 72 | + { sessionID: id, type: "user", limit: 200, cursor }, |
| 73 | + { signal: AbortSignal.any([controller.signal, AbortSignal.timeout(30_000)]) }, |
| 74 | + ) |
| 75 | + |
| 76 | + controller.signal.throwIfAborted() |
| 77 | + |
| 78 | + for (const message of page.data) { |
| 79 | + if (message.type === "user") rows.push(prompt(message, language.t("common.attachment"))) |
| 80 | + } |
| 81 | + |
| 82 | + cursor = page.cursor.next ?? undefined |
| 83 | + } while (cursor) |
| 84 | + |
| 85 | + return rows |
| 86 | + }) |
| 87 | + |
| 88 | + const prompts = createMemo(() => { |
| 89 | + const all = new Map((history.state === "ready" ? history() : []).map((row) => [row.id, row])) |
| 90 | + |
| 91 | + const reverted = data.session.get(id)?.revert?.messageID |
| 92 | + |
| 93 | + for (const message of props.session.history.visibleUserMessages()) { |
| 94 | + all.set(message.id, prompt(message, language.t("common.attachment"))) |
| 95 | + } |
| 96 | + |
| 97 | + const pending = new Set(data.session.input.list(id)) |
| 98 | + |
| 99 | + return [...all.values()].filter( |
| 100 | + (row) => row.text.trim() && !pending.has(row.id) && (!reverted || row.id < reverted), |
| 101 | + ) |
| 102 | + }) |
| 103 | + |
| 104 | + const [selection, setSelection] = createSignal<string>() |
| 105 | + |
| 106 | + // The store publishes backfill in one batch. Only then ask the virtualizer to land on the selected bubble. |
| 107 | + const [loaded] = createResource(selection, async (messageID) => { |
| 108 | + const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(60_000)]) |
| 109 | + |
| 110 | + await data.session.message.loadMore(id, { all: true, signal }) |
| 111 | + signal.throwIfAborted() |
| 112 | + |
| 113 | + if ( |
| 114 | + !props.session.history.visibleUserMessages().some((message) => message.id === messageID) || |
| 115 | + data.session.input.has(id, messageID) |
| 116 | + ) |
| 117 | + throw new Error("Message unavailable") |
| 118 | + |
| 119 | + return messageID |
| 120 | + }) |
| 121 | + |
| 122 | + const jump = (messageID: string) => { |
| 123 | + if (dialog.closing || key !== props.session.identity.sessionKey()) return |
| 124 | + dialog.close() |
| 125 | + props.onJump(messageID) |
| 126 | + } |
| 127 | + |
| 128 | + createEffect(() => { |
| 129 | + const messageID = loaded.state === "ready" && loaded() |
| 130 | + |
| 131 | + if (messageID) jump(messageID) |
| 132 | + }) |
| 133 | + |
| 134 | + const status = () => { |
| 135 | + if (loaded.loading) return language.t("dialog.jump.opening") |
| 136 | + |
| 137 | + if (loaded.error || history.error) return language.t("dialog.jump.failed") |
| 138 | + |
| 139 | + if (history.loading) return language.t("dialog.jump.loading") |
| 140 | + } |
| 141 | + |
| 142 | + return ( |
| 143 | + <JumpToMessage |
| 144 | + prompts={prompts()} |
| 145 | + opening={loaded.loading} |
| 146 | + status={status()} |
| 147 | + onClose={() => dialog.close()} |
| 148 | + onJump={(row) => { |
| 149 | + if (loaded.loading || dialog.closing || key !== props.session.identity.sessionKey()) return |
| 150 | + |
| 151 | + if (data.session.message.get(id, row.id)) return jump(row.id) |
| 152 | + setSelection(row.id) |
| 153 | + }} |
| 154 | + /> |
| 155 | + ) |
| 156 | +} |
0 commit comments