summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/components
diff options
context:
space:
mode:
authorAdam <[email protected]>2026-02-06 09:37:49 -0600
committerGitHub <[email protected]>2026-02-06 09:37:49 -0600
commita4bc883595df9ea0f752079519081bc602408553 (patch)
tree583f21642f431899abe1dfb1f6bd9b2c01dc0206 /packages/app/src/components
parentc07077f96c0019b2e18e0e8e1e0383deda08b3e6 (diff)
downloadopencode-a4bc883595df9ea0f752079519081bc602408553.tar.gz
opencode-a4bc883595df9ea0f752079519081bc602408553.zip
chore: refactoring and tests (#12468)
Diffstat (limited to 'packages/app/src/components')
-rw-r--r--packages/app/src/components/file-tree.test.ts77
-rw-r--r--packages/app/src/components/file-tree.tsx60
-rw-r--r--packages/app/src/components/prompt-input.tsx910
-rw-r--r--packages/app/src/components/prompt-input/attachments.ts132
-rw-r--r--packages/app/src/components/prompt-input/editor-dom.test.ts51
-rw-r--r--packages/app/src/components/prompt-input/editor-dom.ts135
-rw-r--r--packages/app/src/components/prompt-input/history.test.ts69
-rw-r--r--packages/app/src/components/prompt-input/history.ts160
-rw-r--r--packages/app/src/components/prompt-input/submit.ts587
-rw-r--r--packages/app/src/components/session-context-usage.tsx31
-rw-r--r--packages/app/src/components/session/session-context-metrics.test.ts93
-rw-r--r--packages/app/src/components/session/session-context-metrics.ts94
-rw-r--r--packages/app/src/components/session/session-context-tab.tsx47
13 files changed, 1515 insertions, 931 deletions
diff --git a/packages/app/src/components/file-tree.test.ts b/packages/app/src/components/file-tree.test.ts
new file mode 100644
index 000000000..eb048e29e
--- /dev/null
+++ b/packages/app/src/components/file-tree.test.ts
@@ -0,0 +1,77 @@
+import { beforeAll, describe, expect, mock, test } from "bun:test"
+
+let shouldListRoot: typeof import("./file-tree").shouldListRoot
+let shouldListExpanded: typeof import("./file-tree").shouldListExpanded
+let dirsToExpand: typeof import("./file-tree").dirsToExpand
+
+beforeAll(async () => {
+ mock.module("@solidjs/router", () => ({
+ useParams: () => ({}),
+ }))
+ mock.module("@/context/file", () => ({
+ useFile: () => ({
+ tree: {
+ state: () => undefined,
+ list: () => Promise.resolve(),
+ children: () => [],
+ expand: () => {},
+ collapse: () => {},
+ },
+ }),
+ }))
+ mock.module("@opencode-ai/ui/collapsible", () => ({
+ Collapsible: {
+ Trigger: (props: { children?: unknown }) => props.children,
+ Content: (props: { children?: unknown }) => props.children,
+ },
+ }))
+ mock.module("@opencode-ai/ui/file-icon", () => ({ FileIcon: () => null }))
+ mock.module("@opencode-ai/ui/icon", () => ({ Icon: () => null }))
+ mock.module("@opencode-ai/ui/tooltip", () => ({ Tooltip: (props: { children?: unknown }) => props.children }))
+ const mod = await import("./file-tree")
+ shouldListRoot = mod.shouldListRoot
+ shouldListExpanded = mod.shouldListExpanded
+ dirsToExpand = mod.dirsToExpand
+})
+
+describe("file tree fetch discipline", () => {
+ test("root lists on mount unless already loaded or loading", () => {
+ expect(shouldListRoot({ level: 0 })).toBe(true)
+ expect(shouldListRoot({ level: 0, dir: { loaded: true } })).toBe(false)
+ expect(shouldListRoot({ level: 0, dir: { loading: true } })).toBe(false)
+ expect(shouldListRoot({ level: 1 })).toBe(false)
+ })
+
+ test("nested dirs list only when expanded and stale", () => {
+ expect(shouldListExpanded({ level: 1 })).toBe(false)
+ expect(shouldListExpanded({ level: 1, dir: { expanded: false } })).toBe(false)
+ expect(shouldListExpanded({ level: 1, dir: { expanded: true } })).toBe(true)
+ expect(shouldListExpanded({ level: 1, dir: { expanded: true, loaded: true } })).toBe(false)
+ expect(shouldListExpanded({ level: 1, dir: { expanded: true, loading: true } })).toBe(false)
+ expect(shouldListExpanded({ level: 0, dir: { expanded: true } })).toBe(false)
+ })
+
+ test("allowed auto-expand picks only collapsed dirs", () => {
+ const expanded = new Set<string>()
+ const filter = { dirs: new Set(["src", "src/components"]) }
+
+ const first = dirsToExpand({
+ level: 0,
+ filter,
+ expanded: (dir) => expanded.has(dir),
+ })
+
+ expect(first).toEqual(["src", "src/components"])
+
+ for (const dir of first) expanded.add(dir)
+
+ const second = dirsToExpand({
+ level: 0,
+ filter,
+ expanded: (dir) => expanded.has(dir),
+ })
+
+ expect(second).toEqual([])
+ expect(dirsToExpand({ level: 1, filter, expanded: () => false })).toEqual([])
+ })
+})
diff --git a/packages/app/src/components/file-tree.tsx b/packages/app/src/components/file-tree.tsx
index 491a16de7..183c1555b 100644
--- a/packages/app/src/components/file-tree.tsx
+++ b/packages/app/src/components/file-tree.tsx
@@ -8,6 +8,7 @@ import {
createMemo,
For,
Match,
+ on,
Show,
splitProps,
Switch,
@@ -25,6 +26,34 @@ type Filter = {
dirs: Set<string>
}
+export function shouldListRoot(input: { level: number; dir?: { loaded?: boolean; loading?: boolean } }) {
+ if (input.level !== 0) return false
+ if (input.dir?.loaded) return false
+ if (input.dir?.loading) return false
+ return true
+}
+
+export function shouldListExpanded(input: {
+ level: number
+ dir?: { expanded?: boolean; loaded?: boolean; loading?: boolean }
+}) {
+ if (input.level === 0) return false
+ if (!input.dir?.expanded) return false
+ if (input.dir.loaded) return false
+ if (input.dir.loading) return false
+ return true
+}
+
+export function dirsToExpand(input: {
+ level: number
+ filter?: { dirs: Set<string> }
+ expanded: (dir: string) => boolean
+}) {
+ if (input.level !== 0) return []
+ if (!input.filter) return []
+ return [...input.filter.dirs].filter((dir) => !input.expanded(dir))
+}
+
export default function FileTree(props: {
path: string
class?: string
@@ -111,19 +140,30 @@ export default function FileTree(props: {
createEffect(() => {
const current = filter()
- if (!current) return
- if (level !== 0) return
-
- for (const dir of current.dirs) {
- const expanded = untrack(() => file.tree.state(dir)?.expanded) ?? false
- if (expanded) continue
- file.tree.expand(dir)
- }
+ const dirs = dirsToExpand({
+ level,
+ filter: current,
+ expanded: (dir) => untrack(() => file.tree.state(dir)?.expanded) ?? false,
+ })
+ for (const dir of dirs) file.tree.expand(dir)
})
+ createEffect(
+ on(
+ () => props.path,
+ (path) => {
+ const dir = untrack(() => file.tree.state(path))
+ if (!shouldListRoot({ level, dir })) return
+ void file.tree.list(path)
+ },
+ { defer: false },
+ ),
+ )
+
createEffect(() => {
- const path = props.path
- untrack(() => void file.tree.list(path))
+ const dir = file.tree.state(props.path)
+ if (!shouldListExpanded({ level, dir })) return
+ void file.tree.list(props.path)
})
const nodes = createMemo(() => {
diff --git a/packages/app/src/components/prompt-input.tsx b/packages/app/src/components/prompt-input.tsx
index 9186dcfa3..3f0ba314e 100644
--- a/packages/app/src/components/prompt-input.tsx
+++ b/packages/app/src/components/prompt-input.tsx
@@ -1,21 +1,9 @@
import { useFilteredList } from "@opencode-ai/ui/hooks"
-import {
- createEffect,
- on,
- Component,
- Show,
- For,
- onMount,
- onCleanup,
- Switch,
- Match,
- createMemo,
- createSignal,
-} from "solid-js"
-import { createStore, produce } from "solid-js/store"
+import { createEffect, on, Component, Show, For, onCleanup, Switch, Match, createMemo, createSignal } from "solid-js"
+import { createStore } from "solid-js/store"
import { createFocusSignal } from "@solid-primitives/active-element"
import { useLocal } from "@/context/local"
-import { useFile, type FileSelection } from "@/context/file"
+import { useFile } from "@/context/file"
import {
ContentPart,
DEFAULT_PROMPT,
@@ -28,7 +16,7 @@ import {
} from "@/context/prompt"
import { useLayout } from "@/context/layout"
import { useSDK } from "@/context/sdk"
-import { useNavigate, useParams } from "@solidjs/router"
+import { useParams } from "@solidjs/router"
import { useSync } from "@/context/sync"
import { useComments } from "@/context/comments"
import { FileIcon } from "@opencode-ai/ui/file-icon"
@@ -47,27 +35,13 @@ import { DialogSelectModelUnpaid } from "@/components/dialog-select-model-unpaid
import { useProviders } from "@/hooks/use-providers"
import { useCommand } from "@/context/command"
import { Persist, persisted } from "@/utils/persist"
-import { Identifier } from "@/utils/id"
-import { Worktree as WorktreeState } from "@/utils/worktree"
import { SessionContextUsage } from "@/components/session-context-usage"
import { usePermission } from "@/context/permission"
import { useLanguage } from "@/context/language"
-import { useGlobalSync } from "@/context/global-sync"
-import { usePlatform } from "@/context/platform"
-import { createOpencodeClient, type Message, type Part } from "@opencode-ai/sdk/v2/client"
-import { Binary } from "@opencode-ai/util/binary"
-import { showToast } from "@opencode-ai/ui/toast"
-import { base64Encode } from "@opencode-ai/util/encode"
-
-const ACCEPTED_IMAGE_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp"]
-const ACCEPTED_FILE_TYPES = [...ACCEPTED_IMAGE_TYPES, "application/pdf"]
-
-type PendingPrompt = {
- abort: AbortController
- cleanup: VoidFunction
-}
-
-const pending = new Map<string, PendingPrompt>()
+import { createTextFragment, getCursorPosition, setCursorPosition, setRangeEdge } from "./prompt-input/editor-dom"
+import { createPromptAttachments, ACCEPTED_FILE_TYPES } from "./prompt-input/attachments"
+import { navigatePromptHistory, prependHistoryEntry, promptLength } from "./prompt-input/history"
+import { createPromptSubmit } from "./prompt-input/submit"
interface PromptInputProps {
class?: string
@@ -116,11 +90,8 @@ interface SlashCommand {
}
export const PromptInput: Component<PromptInputProps> = (props) => {
- const navigate = useNavigate()
const sdk = useSDK()
const sync = useSync()
- const globalSync = useGlobalSync()
- const platform = usePlatform()
const local = useLocal()
const files = useFile()
const prompt = usePrompt()
@@ -272,20 +243,6 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
}),
)
- const clonePromptParts = (prompt: Prompt): Prompt =>
- prompt.map((part) => {
- if (part.type === "text") return { ...part }
- if (part.type === "image") return { ...part }
- if (part.type === "agent") return { ...part }
- return {
- ...part,
- selection: part.selection ? { ...part.selection } : undefined,
- }
- })
-
- const promptLength = (prompt: Prompt) =>
- prompt.reduce((len, part) => len + ("content" in part ? part.content.length : 0), 0)
-
const applyHistoryPrompt = (p: Prompt, position: "start" | "end") => {
const length = position === "start" ? 0 : promptLength(p)
setStore("applyingHistory", true)
@@ -329,110 +286,6 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
const [composing, setComposing] = createSignal(false)
const isImeComposing = (event: KeyboardEvent) => event.isComposing || composing() || event.keyCode === 229
- const addImageAttachment = async (file: File) => {
- if (!ACCEPTED_FILE_TYPES.includes(file.type)) return
-
- const reader = new FileReader()
- reader.onload = () => {
- const dataUrl = reader.result as string
- const attachment: ImageAttachmentPart = {
- type: "image",
- id: crypto.randomUUID(),
- filename: file.name,
- mime: file.type,
- dataUrl,
- }
- const cursorPosition = prompt.cursor() ?? getCursorPosition(editorRef)
- prompt.set([...prompt.current(), attachment], cursorPosition)
- }
- reader.readAsDataURL(file)
- }
-
- const removeImageAttachment = (id: string) => {
- const current = prompt.current()
- const next = current.filter((part) => part.type !== "image" || part.id !== id)
- prompt.set(next, prompt.cursor())
- }
-
- const handlePaste = async (event: ClipboardEvent) => {
- if (!isFocused()) return
- const clipboardData = event.clipboardData
- if (!clipboardData) return
-
- event.preventDefault()
- event.stopPropagation()
-
- const items = Array.from(clipboardData.items)
- const fileItems = items.filter((item) => item.kind === "file")
- const imageItems = fileItems.filter((item) => ACCEPTED_FILE_TYPES.includes(item.type))
-
- if (imageItems.length > 0) {
- for (const item of imageItems) {
- const file = item.getAsFile()
- if (file) await addImageAttachment(file)
- }
- return
- }
-
- if (fileItems.length > 0) {
- showToast({
- title: language.t("prompt.toast.pasteUnsupported.title"),
- description: language.t("prompt.toast.pasteUnsupported.description"),
- })
- return
- }
-
- const plainText = clipboardData.getData("text/plain") ?? ""
- if (!plainText) return
- addPart({ type: "text", content: plainText, start: 0, end: 0 })
- }
-
- const handleGlobalDragOver = (event: DragEvent) => {
- if (dialog.active) return
-
- event.preventDefault()
- const hasFiles = event.dataTransfer?.types.includes("Files")
- if (hasFiles) {
- setStore("dragging", true)
- }
- }
-
- const handleGlobalDragLeave = (event: DragEvent) => {
- if (dialog.active) return
-
- // relatedTarget is null when leaving the document window
- if (!event.relatedTarget) {
- setStore("dragging", false)
- }
- }
-
- const handleGlobalDrop = async (event: DragEvent) => {
- if (dialog.active) return
-
- event.preventDefault()
- setStore("dragging", false)
-
- const dropped = event.dataTransfer?.files
- if (!dropped) return
-
- for (const file of Array.from(dropped)) {
- if (ACCEPTED_FILE_TYPES.includes(file.type)) {
- await addImageAttachment(file)
- }
- }
- }
-
- onMount(() => {
- document.addEventListener("dragover", handleGlobalDragOver)
- document.addEventListener("dragleave", handleGlobalDragLeave)
- document.addEventListener("drop", handleGlobalDrop)
- })
- onCleanup(() => {
- document.removeEventListener("dragover", handleGlobalDragOver)
- document.removeEventListener("dragleave", handleGlobalDragLeave)
- document.removeEventListener("drop", handleGlobalDrop)
- })
-
createEffect(() => {
if (!isFocused()) setStore("popover", null)
})
@@ -826,36 +679,6 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
queueScroll()
}
- const setRangeEdge = (range: Range, edge: "start" | "end", offset: number) => {
- let remaining = offset
- const nodes = Array.from(editorRef.childNodes)
-
- for (const node of nodes) {
- const length = getNodeLength(node)
- const isText = node.nodeType === Node.TEXT_NODE
- const isPill =
- node.nodeType === Node.ELEMENT_NODE &&
- ((node as HTMLElement).dataset.type === "file" || (node as HTMLElement).dataset.type === "agent")
- const isBreak = node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName === "BR"
-
- if (isText && remaining <= length) {
- if (edge === "start") range.setStart(node, remaining)
- if (edge === "end") range.setEnd(node, remaining)
- return
- }
-
- if ((isPill || isBreak) && remaining <= length) {
- if (edge === "start" && remaining === 0) range.setStartBefore(node)
- if (edge === "start" && remaining > 0) range.setStartAfter(node)
- if (edge === "end" && remaining === 0) range.setEndBefore(node)
- if (edge === "end" && remaining > 0) range.setEndAfter(node)
- return
- }
-
- remaining -= length
- }
- }
-
const addPart = (part: ContentPart) => {
const selection = window.getSelection()
if (!selection || selection.rangeCount === 0) return
@@ -873,8 +696,8 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
if (atMatch) {
const start = atMatch.index ?? cursorPosition - atMatch[0].length
- setRangeEdge(range, "start", start)
- setRangeEdge(range, "end", cursorPosition)
+ setRangeEdge(editorRef, range, "start", start)
+ setRangeEdge(editorRef, range, "end", cursorPosition)
}
range.deleteContents()
@@ -913,81 +736,57 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
setStore("popover", null)
}
- const abort = async () => {
- const sessionID = params.id
- if (!sessionID) return Promise.resolve()
- const queued = pending.get(sessionID)
- if (queued) {
- queued.abort.abort()
- queued.cleanup()
- pending.delete(sessionID)
- return Promise.resolve()
- }
- return sdk.client.session
- .abort({
- sessionID,
- })
- .catch(() => {})
- }
-
const addToHistory = (prompt: Prompt, mode: "normal" | "shell") => {
- const text = prompt
- .map((p) => ("content" in p ? p.content : ""))
- .join("")
- .trim()
- const hasImages = prompt.some((part) => part.type === "image")
- if (!text && !hasImages) return
-
- const entry = clonePromptParts(prompt)
const currentHistory = mode === "shell" ? shellHistory : history
const setCurrentHistory = mode === "shell" ? setShellHistory : setHistory
- const lastEntry = currentHistory.entries[0]
- if (lastEntry && isPromptEqual(lastEntry, entry)) return
-
- setCurrentHistory("entries", (entries) => [entry, ...entries].slice(0, MAX_HISTORY))
+ const next = prependHistoryEntry(currentHistory.entries, prompt)
+ if (next === currentHistory.entries) return
+ setCurrentHistory("entries", next)
}
const navigateHistory = (direction: "up" | "down") => {
- const entries = store.mode === "shell" ? shellHistory.entries : history.entries
- const current = store.historyIndex
-
- if (direction === "up") {
- if (entries.length === 0) return false
- if (current === -1) {
- setStore("savedPrompt", clonePromptParts(prompt.current()))
- setStore("historyIndex", 0)
- applyHistoryPrompt(entries[0], "start")
- return true
- }
- if (current < entries.length - 1) {
- const next = current + 1
- setStore("historyIndex", next)
- applyHistoryPrompt(entries[next], "start")
- return true
- }
- return false
- }
+ const result = navigatePromptHistory({
+ direction,
+ entries: store.mode === "shell" ? shellHistory.entries : history.entries,
+ historyIndex: store.historyIndex,
+ currentPrompt: prompt.current(),
+ savedPrompt: store.savedPrompt,
+ })
+ if (!result.handled) return false
+ setStore("historyIndex", result.historyIndex)
+ setStore("savedPrompt", result.savedPrompt)
+ applyHistoryPrompt(result.prompt, result.cursor)
+ return true
+ }
- if (current > 0) {
- const next = current - 1
- setStore("historyIndex", next)
- applyHistoryPrompt(entries[next], "end")
- return true
- }
- if (current === 0) {
- setStore("historyIndex", -1)
- const saved = store.savedPrompt
- if (saved) {
- applyHistoryPrompt(saved, "end")
- setStore("savedPrompt", null)
- return true
- }
- applyHistoryPrompt(DEFAULT_PROMPT, "end")
- return true
- }
+ const { addImageAttachment, removeImageAttachment, handlePaste } = createPromptAttachments({
+ editor: () => editorRef,
+ isFocused,
+ isDialogActive: () => !!dialog.active,
+ setDragging: (value) => setStore("dragging", value),
+ addPart,
+ })
- return false
- }
+ const { abort, handleSubmit } = createPromptSubmit({
+ info,
+ imageAttachments,
+ commentCount,
+ mode: () => store.mode,
+ working,
+ editor: () => editorRef,
+ queueScroll,
+ promptLength,
+ addToHistory,
+ resetHistoryNavigation: () => {
+ setStore("historyIndex", -1)
+ setStore("savedPrompt", null)
+ },
+ setMode: (mode) => setStore("mode", mode),
+ setPopover: (popover) => setStore("popover", popover),
+ newSessionWorktree: props.newSessionWorktree,
+ onNewSessionWorktreeReset: props.onNewSessionWorktreeReset,
+ onSubmit: props.onSubmit,
+ })
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Backspace") {
@@ -1127,503 +926,6 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
}
}
- const handleSubmit = async (event: Event) => {
- event.preventDefault()
-
- const currentPrompt = prompt.current()
- const text = currentPrompt.map((part) => ("content" in part ? part.content : "")).join("")
- const images = imageAttachments().slice()
- const mode = store.mode
-
- if (text.trim().length === 0 && images.length === 0 && commentCount() === 0) {
- if (working()) abort()
- return
- }
-
- const currentModel = local.model.current()
- const currentAgent = local.agent.current()
- if (!currentModel || !currentAgent) {
- showToast({
- title: language.t("prompt.toast.modelAgentRequired.title"),
- description: language.t("prompt.toast.modelAgentRequired.description"),
- })
- return
- }
-
- const errorMessage = (err: unknown) => {
- if (err && typeof err === "object" && "data" in err) {
- const data = (err as { data?: { message?: string } }).data
- if (data?.message) return data.message
- }
- if (err instanceof Error) return err.message
- return language.t("common.requestFailed")
- }
-
- addToHistory(currentPrompt, mode)
- setStore("historyIndex", -1)
- setStore("savedPrompt", null)
-
- const projectDirectory = sdk.directory
- const isNewSession = !params.id
- const worktreeSelection = props.newSessionWorktree ?? "main"
-
- let sessionDirectory = projectDirectory
- let client = sdk.client
-
- if (isNewSession) {
- if (worktreeSelection === "create") {
- const createdWorktree = await client.worktree
- .create({ directory: projectDirectory })
- .then((x) => x.data)
- .catch((err) => {
- showToast({
- title: language.t("prompt.toast.worktreeCreateFailed.title"),
- description: errorMessage(err),
- })
- return undefined
- })
-
- if (!createdWorktree?.directory) {
- showToast({
- title: language.t("prompt.toast.worktreeCreateFailed.title"),
- description: language.t("common.requestFailed"),
- })
- return
- }
- WorktreeState.pending(createdWorktree.directory)
- sessionDirectory = createdWorktree.directory
- }
-
- if (worktreeSelection !== "main" && worktreeSelection !== "create") {
- sessionDirectory = worktreeSelection
- }
-
- if (sessionDirectory !== projectDirectory) {
- client = createOpencodeClient({
- baseUrl: sdk.url,
- fetch: platform.fetch,
- directory: sessionDirectory,
- throwOnError: true,
- })
- globalSync.child(sessionDirectory)
- }
-
- props.onNewSessionWorktreeReset?.()
- }
-
- let session = info()
- if (!session && isNewSession) {
- session = await client.session
- .create()
- .then((x) => x.data ?? undefined)
- .catch((err) => {
- showToast({
- title: language.t("prompt.toast.sessionCreateFailed.title"),
- description: errorMessage(err),
- })
- return undefined
- })
- if (session) {
- layout.handoff.setTabs(base64Encode(sessionDirectory), session.id)
- navigate(`/${base64Encode(sessionDirectory)}/session/${session.id}`)
- }
- }
- if (!session) return
-
- props.onSubmit?.()
-
- const model = {
- modelID: currentModel.id,
- providerID: currentModel.provider.id,
- }
- const agent = currentAgent.name
- const variant = local.model.variant.current()
-
- const clearInput = () => {
- prompt.reset()
- setStore("mode", "normal")
- setStore("popover", null)
- }
-
- const restoreInput = () => {
- prompt.set(currentPrompt, promptLength(currentPrompt))
- setStore("mode", mode)
- setStore("popover", null)
- requestAnimationFrame(() => {
- editorRef.focus()
- setCursorPosition(editorRef, promptLength(currentPrompt))
- queueScroll()
- })
- }
-
- if (mode === "shell") {
- clearInput()
- client.session
- .shell({
- sessionID: session.id,
- agent,
- model,
- command: text,
- })
- .catch((err) => {
- showToast({
- title: language.t("prompt.toast.shellSendFailed.title"),
- description: errorMessage(err),
- })
- restoreInput()
- })
- return
- }
-
- if (text.startsWith("/")) {
- const [cmdName, ...args] = text.split(" ")
- const commandName = cmdName.slice(1)
- const customCommand = sync.data.command.find((c) => c.name === commandName)
- if (customCommand) {
- clearInput()
- client.session
- .command({
- sessionID: session.id,
- command: commandName,
- arguments: args.join(" "),
- agent,
- model: `${model.providerID}/${model.modelID}`,
- variant,
- parts: images.map((attachment) => ({
- id: Identifier.ascending("part"),
- type: "file" as const,
- mime: attachment.mime,
- url: attachment.dataUrl,
- filename: attachment.filename,
- })),
- })
- .catch((err) => {
- showToast({
- title: language.t("prompt.toast.commandSendFailed.title"),
- description: errorMessage(err),
- })
- restoreInput()
- })
- return
- }
- }
-
- const toAbsolutePath = (path: string) =>
- path.startsWith("/") ? path : (sessionDirectory + "/" + path).replace("//", "/")
-
- const fileAttachments = currentPrompt.filter((part) => part.type === "file") as FileAttachmentPart[]
- const agentAttachments = currentPrompt.filter((part) => part.type === "agent") as AgentPart[]
-
- const fileAttachmentParts = fileAttachments.map((attachment) => {
- const absolute = toAbsolutePath(attachment.path)
- const query = attachment.selection
- ? `?start=${attachment.selection.startLine}&end=${attachment.selection.endLine}`
- : ""
- return {
- id: Identifier.ascending("part"),
- type: "file" as const,
- mime: "text/plain",
- url: `file://${absolute}${query}`,
- filename: getFilename(attachment.path),
- source: {
- type: "file" as const,
- text: {
- value: attachment.content,
- start: attachment.start,
- end: attachment.end,
- },
- path: absolute,
- },
- }
- })
-
- const agentAttachmentParts = agentAttachments.map((attachment) => ({
- id: Identifier.ascending("part"),
- type: "agent" as const,
- name: attachment.name,
- source: {
- value: attachment.content,
- start: attachment.start,
- end: attachment.end,
- },
- }))
-
- const usedUrls = new Set(fileAttachmentParts.map((part) => part.url))
-
- const context = prompt.context.items().slice()
-
- const commentItems = context.filter((item) => item.type === "file" && !!item.comment?.trim())
-
- const contextParts: Array<
- | {
- id: string
- type: "text"
- text: string
- synthetic?: boolean
- }
- | {
- id: string
- type: "file"
- mime: string
- url: string
- filename?: string
- }
- > = []
-
- const commentNote = (path: string, selection: FileSelection | undefined, comment: string) => {
- const start = selection ? Math.min(selection.startLine, selection.endLine) : undefined
- const end = selection ? Math.max(selection.startLine, selection.endLine) : undefined
- const range =
- start === undefined || end === undefined
- ? "this file"
- : start === end
- ? `line ${start}`
- : `lines ${start} through ${end}`
-
- return `The user made the following comment regarding ${range} of ${path}: ${comment}`
- }
-
- const addContextFile = (input: { path: string; selection?: FileSelection; comment?: string }) => {
- const absolute = toAbsolutePath(input.path)
- const query = input.selection ? `?start=${input.selection.startLine}&end=${input.selection.endLine}` : ""
- const url = `file://${absolute}${query}`
-
- const comment = input.comment?.trim()
- if (!comment && usedUrls.has(url)) return
- usedUrls.add(url)
-
- if (comment) {
- contextParts.push({
- id: Identifier.ascending("part"),
- type: "text",
- text: commentNote(input.path, input.selection, comment),
- synthetic: true,
- })
- }
-
- contextParts.push({
- id: Identifier.ascending("part"),
- type: "file",
- mime: "text/plain",
- url,
- filename: getFilename(input.path),
- })
- }
-
- for (const item of context) {
- if (item.type !== "file") continue
- addContextFile({ path: item.path, selection: item.selection, comment: item.comment })
- }
-
- const imageAttachmentParts = images.map((attachment) => ({
- id: Identifier.ascending("part"),
- type: "file" as const,
- mime: attachment.mime,
- url: attachment.dataUrl,
- filename: attachment.filename,
- }))
-
- const messageID = Identifier.ascending("message")
- const textPart = {
- id: Identifier.ascending("part"),
- type: "text" as const,
- text,
- }
- const requestParts = [
- textPart,
- ...fileAttachmentParts,
- ...contextParts,
- ...agentAttachmentParts,
- ...imageAttachmentParts,
- ]
-
- const optimisticParts = requestParts.map((part) => ({
- ...part,
- sessionID: session.id,
- messageID,
- })) as unknown as Part[]
-
- const optimisticMessage: Message = {
- id: messageID,
- sessionID: session.id,
- role: "user",
- time: { created: Date.now() },
- agent,
- model,
- }
-
- const addOptimisticMessage = () => {
- if (sessionDirectory === projectDirectory) {
- sync.set(
- produce((draft) => {
- const messages = draft.message[session.id]
- if (!messages) {
- draft.message[session.id] = [optimisticMessage]
- } else {
- const result = Binary.search(messages, messageID, (m) => m.id)
- messages.splice(result.index, 0, optimisticMessage)
- }
- draft.part[messageID] = optimisticParts
- .filter((p) => !!p?.id)
- .slice()
- .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
- }),
- )
- return
- }
-
- globalSync.child(sessionDirectory)[1](
- produce((draft) => {
- const messages = draft.message[session.id]
- if (!messages) {
- draft.message[session.id] = [optimisticMessage]
- } else {
- const result = Binary.search(messages, messageID, (m) => m.id)
- messages.splice(result.index, 0, optimisticMessage)
- }
- draft.part[messageID] = optimisticParts
- .filter((p) => !!p?.id)
- .slice()
- .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
- }),
- )
- }
-
- const removeOptimisticMessage = () => {
- if (sessionDirectory === projectDirectory) {
- sync.set(
- produce((draft) => {
- const messages = draft.message[session.id]
- if (messages) {
- const result = Binary.search(messages, messageID, (m) => m.id)
- if (result.found) messages.splice(result.index, 1)
- }
- delete draft.part[messageID]
- }),
- )
- return
- }
-
- globalSync.child(sessionDirectory)[1](
- produce((draft) => {
- const messages = draft.message[session.id]
- if (messages) {
- const result = Binary.search(messages, messageID, (m) => m.id)
- if (result.found) messages.splice(result.index, 1)
- }
- delete draft.part[messageID]
- }),
- )
- }
-
- for (const item of commentItems) {
- prompt.context.remove(item.key)
- }
-
- clearInput()
- addOptimisticMessage()
-
- const waitForWorktree = async () => {
- const worktree = WorktreeState.get(sessionDirectory)
- if (!worktree || worktree.status !== "pending") return true
-
- if (sessionDirectory === projectDirectory) {
- sync.set("session_status", session.id, { type: "busy" })
- }
-
- const controller = new AbortController()
-
- const cleanup = () => {
- if (sessionDirectory === projectDirectory) {
- sync.set("session_status", session.id, { type: "idle" })
- }
- removeOptimisticMessage()
- for (const item of commentItems) {
- prompt.context.add({
- type: "file",
- path: item.path,
- selection: item.selection,
- comment: item.comment,
- commentID: item.commentID,
- commentOrigin: item.commentOrigin,
- preview: item.preview,
- })
- }
- restoreInput()
- }
-
- pending.set(session.id, { abort: controller, cleanup })
-
- const abort = new Promise<Awaited<ReturnType<typeof WorktreeState.wait>>>((resolve) => {
- if (controller.signal.aborted) {
- resolve({ status: "failed", message: "aborted" })
- return
- }
- controller.signal.addEventListener(
- "abort",
- () => {
- resolve({ status: "failed", message: "aborted" })
- },
- { once: true },
- )
- })
-
- const timeoutMs = 5 * 60 * 1000
- const timer = { id: undefined as number | undefined }
- const timeout = new Promise<Awaited<ReturnType<typeof WorktreeState.wait>>>((resolve) => {
- timer.id = window.setTimeout(() => {
- resolve({ status: "failed", message: language.t("workspace.error.stillPreparing") })
- }, timeoutMs)
- })
-
- const result = await Promise.race([WorktreeState.wait(sessionDirectory), abort, timeout]).finally(() => {
- if (timer.id === undefined) return
- clearTimeout(timer.id)
- })
- pending.delete(session.id)
- if (controller.signal.aborted) return false
- if (result.status === "failed") throw new Error(result.message)
- return true
- }
-
- const send = async () => {
- const ok = await waitForWorktree()
- if (!ok) return
- await client.session.prompt({
- sessionID: session.id,
- agent,
- model,
- messageID,
- parts: requestParts,
- variant,
- })
- }
-
- void send().catch((err) => {
- pending.delete(session.id)
- if (sessionDirectory === projectDirectory) {
- sync.set("session_status", session.id, { type: "idle" })
- }
- showToast({
- title: language.t("prompt.toast.promptSendFailed.title"),
- description: errorMessage(err),
- })
- removeOptimisticMessage()
- for (const item of commentItems) {
- prompt.context.add({
- type: "file",
- path: item.path,
- selection: item.selection,
- comment: item.comment,
- commentID: item.commentID,
- commentOrigin: item.commentOrigin,
- preview: item.preview,
- })
- }
- restoreInput()
- })
- }
-
return (
<div class="relative size-full _max-h-[320px] flex flex-col gap-3">
<Show when={store.popover}>
@@ -2087,109 +1389,3 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
</div>
)
}
-
-function createTextFragment(content: string): DocumentFragment {
- const fragment = document.createDocumentFragment()
- const segments = content.split("\n")
- segments.forEach((segment, index) => {
- if (segment) {
- fragment.appendChild(document.createTextNode(segment))
- } else if (segments.length > 1) {
- fragment.appendChild(document.createTextNode("\u200B"))
- }
- if (index < segments.length - 1) {
- fragment.appendChild(document.createElement("br"))
- }
- })
- return fragment
-}
-
-function getNodeLength(node: Node): number {
- if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName === "BR") return 1
- return (node.textContent ?? "").replace(/\u200B/g, "").length
-}
-
-function getTextLength(node: Node): number {
- if (node.nodeType === Node.TEXT_NODE) return (node.textContent ?? "").replace(/\u200B/g, "").length
- if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName === "BR") return 1
- let length = 0
- for (const child of Array.from(node.childNodes)) {
- length += getTextLength(child)
- }
- return length
-}
-
-function getCursorPosition(parent: HTMLElement): number {
- const selection = window.getSelection()
- if (!selection || selection.rangeCount === 0) return 0
- const range = selection.getRangeAt(0)
- if (!parent.contains(range.startContainer)) return 0
- const preCaretRange = range.cloneRange()
- preCaretRange.selectNodeContents(parent)
- preCaretRange.setEnd(range.startContainer, range.startOffset)
- return getTextLength(preCaretRange.cloneContents())
-}
-
-function setCursorPosition(parent: HTMLElement, position: number) {
- let remaining = position
- let node = parent.firstChild
- while (node) {
- const length = getNodeLength(node)
- const isText = node.nodeType === Node.TEXT_NODE
- const isPill =
- node.nodeType === Node.ELEMENT_NODE &&
- ((node as HTMLElement).dataset.type === "file" || (node as HTMLElement).dataset.type === "agent")
- const isBreak = node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName === "BR"
-
- if (isText && remaining <= length) {
- const range = document.createRange()
- const selection = window.getSelection()
- range.setStart(node, remaining)
- range.collapse(true)
- selection?.removeAllRanges()
- selection?.addRange(range)
- return
- }
-
- if ((isPill || isBreak) && remaining <= length) {
- const range = document.createRange()
- const selection = window.getSelection()
- if (remaining === 0) {
- range.setStartBefore(node)
- }
- if (remaining > 0 && isPill) {
- range.setStartAfter(node)
- }
- if (remaining > 0 && isBreak) {
- const next = node.nextSibling
- if (next && next.nodeType === Node.TEXT_NODE) {
- range.setStart(next, 0)
- }
- if (!next || next.nodeType !== Node.TEXT_NODE) {
- range.setStartAfter(node)
- }
- }
- range.collapse(true)
- selection?.removeAllRanges()
- selection?.addRange(range)
- return
- }
-
- remaining -= length
- node = node.nextSibling
- }
-
- const fallbackRange = document.createRange()
- const fallbackSelection = window.getSelection()
- const last = parent.lastChild
- if (last && last.nodeType === Node.TEXT_NODE) {
- const len = last.textContent ? last.textContent.length : 0
- fallbackRange.setStart(last, len)
- }
- if (!last || last.nodeType !== Node.TEXT_NODE) {
- fallbackRange.selectNodeContents(parent)
- }
- fallbackRange.collapse(false)
- fallbackSelection?.removeAllRanges()
- fallbackSelection?.addRange(fallbackRange)
-}
diff --git a/packages/app/src/components/prompt-input/attachments.ts b/packages/app/src/components/prompt-input/attachments.ts
new file mode 100644
index 000000000..4ea2cfb90
--- /dev/null
+++ b/packages/app/src/components/prompt-input/attachments.ts
@@ -0,0 +1,132 @@
+import { onCleanup, onMount } from "solid-js"
+import { showToast } from "@opencode-ai/ui/toast"
+import { usePrompt, type ContentPart, type ImageAttachmentPart } from "@/context/prompt"
+import { useLanguage } from "@/context/language"
+import { getCursorPosition } from "./editor-dom"
+
+export const ACCEPTED_IMAGE_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp"]
+export const ACCEPTED_FILE_TYPES = [...ACCEPTED_IMAGE_TYPES, "application/pdf"]
+
+type PromptAttachmentsInput = {
+ editor: () => HTMLDivElement | undefined
+ isFocused: () => boolean
+ isDialogActive: () => boolean
+ setDragging: (value: boolean) => void
+ addPart: (part: ContentPart) => void
+}
+
+export function createPromptAttachments(input: PromptAttachmentsInput) {
+ const prompt = usePrompt()
+ const language = useLanguage()
+
+ const addImageAttachment = async (file: File) => {
+ if (!ACCEPTED_FILE_TYPES.includes(file.type)) return
+
+ const reader = new FileReader()
+ reader.onload = () => {
+ const editor = input.editor()
+ if (!editor) return
+ const dataUrl = reader.result as string
+ const attachment: ImageAttachmentPart = {
+ type: "image",
+ id: crypto.randomUUID(),
+ filename: file.name,
+ mime: file.type,
+ dataUrl,
+ }
+ const cursorPosition = prompt.cursor() ?? getCursorPosition(editor)
+ prompt.set([...prompt.current(), attachment], cursorPosition)
+ }
+ reader.readAsDataURL(file)
+ }
+
+ const removeImageAttachment = (id: string) => {
+ const current = prompt.current()
+ const next = current.filter((part) => part.type !== "image" || part.id !== id)
+ prompt.set(next, prompt.cursor())
+ }
+
+ const handlePaste = async (event: ClipboardEvent) => {
+ if (!input.isFocused()) return
+ const clipboardData = event.clipboardData
+ if (!clipboardData) return
+
+ event.preventDefault()
+ event.stopPropagation()
+
+ const items = Array.from(clipboardData.items)
+ const fileItems = items.filter((item) => item.kind === "file")
+ const imageItems = fileItems.filter((item) => ACCEPTED_FILE_TYPES.includes(item.type))
+
+ if (imageItems.length > 0) {
+ for (const item of imageItems) {
+ const file = item.getAsFile()
+ if (file) await addImageAttachment(file)
+ }
+ return
+ }
+
+ if (fileItems.length > 0) {
+ showToast({
+ title: language.t("prompt.toast.pasteUnsupported.title"),
+ description: language.t("prompt.toast.pasteUnsupported.description"),
+ })
+ return
+ }
+
+ const plainText = clipboardData.getData("text/plain") ?? ""
+ if (!plainText) return
+ input.addPart({ type: "text", content: plainText, start: 0, end: 0 })
+ }
+
+ const handleGlobalDragOver = (event: DragEvent) => {
+ if (input.isDialogActive()) return
+
+ event.preventDefault()
+ const hasFiles = event.dataTransfer?.types.includes("Files")
+ if (hasFiles) {
+ input.setDragging(true)
+ }
+ }
+
+ const handleGlobalDragLeave = (event: DragEvent) => {
+ if (input.isDialogActive()) return
+ if (!event.relatedTarget) {
+ input.setDragging(false)
+ }
+ }
+
+ const handleGlobalDrop = async (event: DragEvent) => {
+ if (input.isDialogActive()) return
+
+ event.preventDefault()
+ input.setDragging(false)
+
+ const dropped = event.dataTransfer?.files
+ if (!dropped) return
+
+ for (const file of Array.from(dropped)) {
+ if (ACCEPTED_FILE_TYPES.includes(file.type)) {
+ await addImageAttachment(file)
+ }
+ }
+ }
+
+ onMount(() => {
+ document.addEventListener("dragover", handleGlobalDragOver)
+ document.addEventListener("dragleave", handleGlobalDragLeave)
+ document.addEventListener("drop", handleGlobalDrop)
+ })
+
+ onCleanup(() => {
+ document.removeEventListener("dragover", handleGlobalDragOver)
+ document.removeEventListener("dragleave", handleGlobalDragLeave)
+ document.removeEventListener("drop", handleGlobalDrop)
+ })
+
+ return {
+ addImageAttachment,
+ removeImageAttachment,
+ handlePaste,
+ }
+}
diff --git a/packages/app/src/components/prompt-input/editor-dom.test.ts b/packages/app/src/components/prompt-input/editor-dom.test.ts
new file mode 100644
index 000000000..fce8b4b95
--- /dev/null
+++ b/packages/app/src/components/prompt-input/editor-dom.test.ts
@@ -0,0 +1,51 @@
+import { describe, expect, test } from "bun:test"
+import { createTextFragment, getCursorPosition, getNodeLength, getTextLength, setCursorPosition } from "./editor-dom"
+
+describe("prompt-input editor dom", () => {
+ test("createTextFragment preserves newlines with br and zero-width placeholders", () => {
+ const fragment = createTextFragment("foo\n\nbar")
+ const container = document.createElement("div")
+ container.appendChild(fragment)
+
+ expect(container.childNodes.length).toBe(5)
+ expect(container.childNodes[0]?.textContent).toBe("foo")
+ expect((container.childNodes[1] as HTMLElement).tagName).toBe("BR")
+ expect(container.childNodes[2]?.textContent).toBe("\u200B")
+ expect((container.childNodes[3] as HTMLElement).tagName).toBe("BR")
+ expect(container.childNodes[4]?.textContent).toBe("bar")
+ })
+
+ test("length helpers treat breaks as one char and ignore zero-width chars", () => {
+ const container = document.createElement("div")
+ container.appendChild(document.createTextNode("ab\u200B"))
+ container.appendChild(document.createElement("br"))
+ container.appendChild(document.createTextNode("cd"))
+
+ expect(getNodeLength(container.childNodes[0]!)).toBe(2)
+ expect(getNodeLength(container.childNodes[1]!)).toBe(1)
+ expect(getTextLength(container)).toBe(5)
+ })
+
+ test("setCursorPosition and getCursorPosition round-trip with pills and breaks", () => {
+ const container = document.createElement("div")
+ const pill = document.createElement("span")
+ pill.dataset.type = "file"
+ pill.textContent = "@file"
+ container.appendChild(document.createTextNode("ab"))
+ container.appendChild(pill)
+ container.appendChild(document.createElement("br"))
+ container.appendChild(document.createTextNode("cd"))
+ document.body.appendChild(container)
+
+ setCursorPosition(container, 2)
+ expect(getCursorPosition(container)).toBe(2)
+
+ setCursorPosition(container, 7)
+ expect(getCursorPosition(container)).toBe(7)
+
+ setCursorPosition(container, 8)
+ expect(getCursorPosition(container)).toBe(8)
+
+ container.remove()
+ })
+})
diff --git a/packages/app/src/components/prompt-input/editor-dom.ts b/packages/app/src/components/prompt-input/editor-dom.ts
new file mode 100644
index 000000000..3116ceb12
--- /dev/null
+++ b/packages/app/src/components/prompt-input/editor-dom.ts
@@ -0,0 +1,135 @@
+export function createTextFragment(content: string): DocumentFragment {
+ const fragment = document.createDocumentFragment()
+ const segments = content.split("\n")
+ segments.forEach((segment, index) => {
+ if (segment) {
+ fragment.appendChild(document.createTextNode(segment))
+ } else if (segments.length > 1) {
+ fragment.appendChild(document.createTextNode("\u200B"))
+ }
+ if (index < segments.length - 1) {
+ fragment.appendChild(document.createElement("br"))
+ }
+ })
+ return fragment
+}
+
+export function getNodeLength(node: Node): number {
+ if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName === "BR") return 1
+ return (node.textContent ?? "").replace(/\u200B/g, "").length
+}
+
+export function getTextLength(node: Node): number {
+ if (node.nodeType === Node.TEXT_NODE) return (node.textContent ?? "").replace(/\u200B/g, "").length
+ if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName === "BR") return 1
+ let length = 0
+ for (const child of Array.from(node.childNodes)) {
+ length += getTextLength(child)
+ }
+ return length
+}
+
+export function getCursorPosition(parent: HTMLElement): number {
+ const selection = window.getSelection()
+ if (!selection || selection.rangeCount === 0) return 0
+ const range = selection.getRangeAt(0)
+ if (!parent.contains(range.startContainer)) return 0
+ const preCaretRange = range.cloneRange()
+ preCaretRange.selectNodeContents(parent)
+ preCaretRange.setEnd(range.startContainer, range.startOffset)
+ return getTextLength(preCaretRange.cloneContents())
+}
+
+export function setCursorPosition(parent: HTMLElement, position: number) {
+ let remaining = position
+ let node = parent.firstChild
+ while (node) {
+ const length = getNodeLength(node)
+ const isText = node.nodeType === Node.TEXT_NODE
+ const isPill =
+ node.nodeType === Node.ELEMENT_NODE &&
+ ((node as HTMLElement).dataset.type === "file" || (node as HTMLElement).dataset.type === "agent")
+ const isBreak = node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName === "BR"
+
+ if (isText && remaining <= length) {
+ const range = document.createRange()
+ const selection = window.getSelection()
+ range.setStart(node, remaining)
+ range.collapse(true)
+ selection?.removeAllRanges()
+ selection?.addRange(range)
+ return
+ }
+
+ if ((isPill || isBreak) && remaining <= length) {
+ const range = document.createRange()
+ const selection = window.getSelection()
+ if (remaining === 0) {
+ range.setStartBefore(node)
+ }
+ if (remaining > 0 && isPill) {
+ range.setStartAfter(node)
+ }
+ if (remaining > 0 && isBreak) {
+ const next = node.nextSibling
+ if (next && next.nodeType === Node.TEXT_NODE) {
+ range.setStart(next, 0)
+ }
+ if (!next || next.nodeType !== Node.TEXT_NODE) {
+ range.setStartAfter(node)
+ }
+ }
+ range.collapse(true)
+ selection?.removeAllRanges()
+ selection?.addRange(range)
+ return
+ }
+
+ remaining -= length
+ node = node.nextSibling
+ }
+
+ const fallbackRange = document.createRange()
+ const fallbackSelection = window.getSelection()
+ const last = parent.lastChild
+ if (last && last.nodeType === Node.TEXT_NODE) {
+ const len = last.textContent ? last.textContent.length : 0
+ fallbackRange.setStart(last, len)
+ }
+ if (!last || last.nodeType !== Node.TEXT_NODE) {
+ fallbackRange.selectNodeContents(parent)
+ }
+ fallbackRange.collapse(false)
+ fallbackSelection?.removeAllRanges()
+ fallbackSelection?.addRange(fallbackRange)
+}
+
+export function setRangeEdge(parent: HTMLElement, range: Range, edge: "start" | "end", offset: number) {
+ let remaining = offset
+ const nodes = Array.from(parent.childNodes)
+
+ for (const node of nodes) {
+ const length = getNodeLength(node)
+ const isText = node.nodeType === Node.TEXT_NODE
+ const isPill =
+ node.nodeType === Node.ELEMENT_NODE &&
+ ((node as HTMLElement).dataset.type === "file" || (node as HTMLElement).dataset.type === "agent")
+ const isBreak = node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName === "BR"
+
+ if (isText && remaining <= length) {
+ if (edge === "start") range.setStart(node, remaining)
+ if (edge === "end") range.setEnd(node, remaining)
+ return
+ }
+
+ if ((isPill || isBreak) && remaining <= length) {
+ if (edge === "start" && remaining === 0) range.setStartBefore(node)
+ if (edge === "start" && remaining > 0) range.setStartAfter(node)
+ if (edge === "end" && remaining === 0) range.setEndBefore(node)
+ if (edge === "end" && remaining > 0) range.setEndAfter(node)
+ return
+ }
+
+ remaining -= length
+ }
+}
diff --git a/packages/app/src/components/prompt-input/history.test.ts b/packages/app/src/components/prompt-input/history.test.ts
new file mode 100644
index 000000000..54be9cb75
--- /dev/null
+++ b/packages/app/src/components/prompt-input/history.test.ts
@@ -0,0 +1,69 @@
+import { describe, expect, test } from "bun:test"
+import type { Prompt } from "@/context/prompt"
+import { clonePromptParts, navigatePromptHistory, prependHistoryEntry, promptLength } from "./history"
+
+const DEFAULT_PROMPT: Prompt = [{ type: "text", content: "", start: 0, end: 0 }]
+
+const text = (value: string): Prompt => [{ type: "text", content: value, start: 0, end: value.length }]
+
+describe("prompt-input history", () => {
+ test("prependHistoryEntry skips empty prompt and deduplicates consecutive entries", () => {
+ const first = prependHistoryEntry([], DEFAULT_PROMPT)
+ expect(first).toEqual([])
+
+ const withOne = prependHistoryEntry([], text("hello"))
+ expect(withOne).toHaveLength(1)
+
+ const deduped = prependHistoryEntry(withOne, text("hello"))
+ expect(deduped).toBe(withOne)
+ })
+
+ test("navigatePromptHistory restores saved prompt when moving down from newest", () => {
+ const entries = [text("third"), text("second"), text("first")]
+ const up = navigatePromptHistory({
+ direction: "up",
+ entries,
+ historyIndex: -1,
+ currentPrompt: text("draft"),
+ savedPrompt: null,
+ })
+ expect(up.handled).toBe(true)
+ if (!up.handled) throw new Error("expected handled")
+ expect(up.historyIndex).toBe(0)
+ expect(up.cursor).toBe("start")
+
+ const down = navigatePromptHistory({
+ direction: "down",
+ entries,
+ historyIndex: up.historyIndex,
+ currentPrompt: text("ignored"),
+ savedPrompt: up.savedPrompt,
+ })
+ expect(down.handled).toBe(true)
+ if (!down.handled) throw new Error("expected handled")
+ expect(down.historyIndex).toBe(-1)
+ expect(down.prompt[0]?.type === "text" ? down.prompt[0].content : "").toBe("draft")
+ })
+
+ test("helpers clone prompt and count text content length", () => {
+ const original: Prompt = [
+ { type: "text", content: "one", start: 0, end: 3 },
+ {
+ type: "file",
+ path: "src/a.ts",
+ content: "@src/a.ts",
+ start: 3,
+ end: 12,
+ selection: { startLine: 1, startChar: 1, endLine: 2, endChar: 1 },
+ },
+ { type: "image", id: "1", filename: "img.png", mime: "image/png", dataUrl: "data:image/png;base64,abc" },
+ ]
+ const copy = clonePromptParts(original)
+ expect(copy).not.toBe(original)
+ expect(promptLength(copy)).toBe(12)
+ if (copy[1]?.type !== "file") throw new Error("expected file")
+ copy[1].selection!.startLine = 9
+ if (original[1]?.type !== "file") throw new Error("expected file")
+ expect(original[1].selection?.startLine).toBe(1)
+ })
+})
diff --git a/packages/app/src/components/prompt-input/history.ts b/packages/app/src/components/prompt-input/history.ts
new file mode 100644
index 000000000..63164f0ba
--- /dev/null
+++ b/packages/app/src/components/prompt-input/history.ts
@@ -0,0 +1,160 @@
+import type { Prompt } from "@/context/prompt"
+
+const DEFAULT_PROMPT: Prompt = [{ type: "text", content: "", start: 0, end: 0 }]
+
+export const MAX_HISTORY = 100
+
+export function clonePromptParts(prompt: Prompt): Prompt {
+ return prompt.map((part) => {
+ if (part.type === "text") return { ...part }
+ if (part.type === "image") return { ...part }
+ if (part.type === "agent") return { ...part }
+ return {
+ ...part,
+ selection: part.selection ? { ...part.selection } : undefined,
+ }
+ })
+}
+
+export function promptLength(prompt: Prompt) {
+ return prompt.reduce((len, part) => len + ("content" in part ? part.content.length : 0), 0)
+}
+
+export function prependHistoryEntry(entries: Prompt[], prompt: Prompt, max = MAX_HISTORY) {
+ const text = prompt
+ .map((part) => ("content" in part ? part.content : ""))
+ .join("")
+ .trim()
+ const hasImages = prompt.some((part) => part.type === "image")
+ if (!text && !hasImages) return entries
+
+ const entry = clonePromptParts(prompt)
+ const last = entries[0]
+ if (last && isPromptEqual(last, entry)) return entries
+ return [entry, ...entries].slice(0, max)
+}
+
+function isPromptEqual(promptA: Prompt, promptB: Prompt) {
+ if (promptA.length !== promptB.length) return false
+ for (let i = 0; i < promptA.length; i++) {
+ const partA = promptA[i]
+ const partB = promptB[i]
+ if (partA.type !== partB.type) return false
+ if (partA.type === "text" && partA.content !== (partB.type === "text" ? partB.content : "")) return false
+ if (partA.type === "file") {
+ if (partA.path !== (partB.type === "file" ? partB.path : "")) return false
+ const a = partA.selection
+ const b = partB.type === "file" ? partB.selection : undefined
+ const sameSelection =
+ (!a && !b) ||
+ (!!a &&
+ !!b &&
+ a.startLine === b.startLine &&
+ a.startChar === b.startChar &&
+ a.endLine === b.endLine &&
+ a.endChar === b.endChar)
+ if (!sameSelection) return false
+ }
+ if (partA.type === "agent" && partA.name !== (partB.type === "agent" ? partB.name : "")) return false
+ if (partA.type === "image" && partA.id !== (partB.type === "image" ? partB.id : "")) return false
+ }
+ return true
+}
+
+type HistoryNavInput = {
+ direction: "up" | "down"
+ entries: Prompt[]
+ historyIndex: number
+ currentPrompt: Prompt
+ savedPrompt: Prompt | null
+}
+
+type HistoryNavResult =
+ | {
+ handled: false
+ historyIndex: number
+ savedPrompt: Prompt | null
+ }
+ | {
+ handled: true
+ historyIndex: number
+ savedPrompt: Prompt | null
+ prompt: Prompt
+ cursor: "start" | "end"
+ }
+
+export function navigatePromptHistory(input: HistoryNavInput): HistoryNavResult {
+ if (input.direction === "up") {
+ if (input.entries.length === 0) {
+ return {
+ handled: false,
+ historyIndex: input.historyIndex,
+ savedPrompt: input.savedPrompt,
+ }
+ }
+
+ if (input.historyIndex === -1) {
+ return {
+ handled: true,
+ historyIndex: 0,
+ savedPrompt: clonePromptParts(input.currentPrompt),
+ prompt: input.entries[0],
+ cursor: "start",
+ }
+ }
+
+ if (input.historyIndex < input.entries.length - 1) {
+ const next = input.historyIndex + 1
+ return {
+ handled: true,
+ historyIndex: next,
+ savedPrompt: input.savedPrompt,
+ prompt: input.entries[next],
+ cursor: "start",
+ }
+ }
+
+ return {
+ handled: false,
+ historyIndex: input.historyIndex,
+ savedPrompt: input.savedPrompt,
+ }
+ }
+
+ if (input.historyIndex > 0) {
+ const next = input.historyIndex - 1
+ return {
+ handled: true,
+ historyIndex: next,
+ savedPrompt: input.savedPrompt,
+ prompt: input.entries[next],
+ cursor: "end",
+ }
+ }
+
+ if (input.historyIndex === 0) {
+ if (input.savedPrompt) {
+ return {
+ handled: true,
+ historyIndex: -1,
+ savedPrompt: null,
+ prompt: input.savedPrompt,
+ cursor: "end",
+ }
+ }
+
+ return {
+ handled: true,
+ historyIndex: -1,
+ savedPrompt: null,
+ prompt: DEFAULT_PROMPT,
+ cursor: "end",
+ }
+ }
+
+ return {
+ handled: false,
+ historyIndex: input.historyIndex,
+ savedPrompt: input.savedPrompt,
+ }
+}
diff --git a/packages/app/src/components/prompt-input/submit.ts b/packages/app/src/components/prompt-input/submit.ts
new file mode 100644
index 000000000..1e5ebe4cb
--- /dev/null
+++ b/packages/app/src/components/prompt-input/submit.ts
@@ -0,0 +1,587 @@
+import { Accessor } from "solid-js"
+import { produce } from "solid-js/store"
+import { useNavigate, useParams } from "@solidjs/router"
+import { getFilename } from "@opencode-ai/util/path"
+import { createOpencodeClient, type Message, type Part } from "@opencode-ai/sdk/v2/client"
+import { Binary } from "@opencode-ai/util/binary"
+import { showToast } from "@opencode-ai/ui/toast"
+import { base64Encode } from "@opencode-ai/util/encode"
+import { useLocal } from "@/context/local"
+import {
+ usePrompt,
+ type AgentPart,
+ type FileAttachmentPart,
+ type ImageAttachmentPart,
+ type Prompt,
+} from "@/context/prompt"
+import { useLayout } from "@/context/layout"
+import { useSDK } from "@/context/sdk"
+import { useSync } from "@/context/sync"
+import { useGlobalSync } from "@/context/global-sync"
+import { usePlatform } from "@/context/platform"
+import { useLanguage } from "@/context/language"
+import { Identifier } from "@/utils/id"
+import { Worktree as WorktreeState } from "@/utils/worktree"
+import type { FileSelection } from "@/context/file"
+import { setCursorPosition } from "./editor-dom"
+
+type PendingPrompt = {
+ abort: AbortController
+ cleanup: VoidFunction
+}
+
+const pending = new Map<string, PendingPrompt>()
+
+type PromptSubmitInput = {
+ info: Accessor<{ id: string } | undefined>
+ imageAttachments: Accessor<ImageAttachmentPart[]>
+ commentCount: Accessor<number>
+ mode: Accessor<"normal" | "shell">
+ working: Accessor<boolean>
+ editor: () => HTMLDivElement | undefined
+ queueScroll: () => void
+ promptLength: (prompt: Prompt) => number
+ addToHistory: (prompt: Prompt, mode: "normal" | "shell") => void
+ resetHistoryNavigation: () => void
+ setMode: (mode: "normal" | "shell") => void
+ setPopover: (popover: "at" | "slash" | null) => void
+ newSessionWorktree?: string
+ onNewSessionWorktreeReset?: () => void
+ onSubmit?: () => void
+}
+
+type CommentItem = {
+ path: string
+ selection?: FileSelection
+ comment?: string
+ commentID?: string
+ commentOrigin?: "review" | "file"
+ preview?: string
+}
+
+export function createPromptSubmit(input: PromptSubmitInput) {
+ const navigate = useNavigate()
+ const sdk = useSDK()
+ const sync = useSync()
+ const globalSync = useGlobalSync()
+ const platform = usePlatform()
+ const local = useLocal()
+ const prompt = usePrompt()
+ const layout = useLayout()
+ const language = useLanguage()
+ const params = useParams()
+
+ const errorMessage = (err: unknown) => {
+ if (err && typeof err === "object" && "data" in err) {
+ const data = (err as { data?: { message?: string } }).data
+ if (data?.message) return data.message
+ }
+ if (err instanceof Error) return err.message
+ return language.t("common.requestFailed")
+ }
+
+ const abort = async () => {
+ const sessionID = params.id
+ if (!sessionID) return Promise.resolve()
+ const queued = pending.get(sessionID)
+ if (queued) {
+ queued.abort.abort()
+ queued.cleanup()
+ pending.delete(sessionID)
+ return Promise.resolve()
+ }
+ return sdk.client.session
+ .abort({
+ sessionID,
+ })
+ .catch(() => {})
+ }
+
+ const restoreCommentItems = (items: CommentItem[]) => {
+ for (const item of items) {
+ prompt.context.add({
+ type: "file",
+ path: item.path,
+ selection: item.selection,
+ comment: item.comment,
+ commentID: item.commentID,
+ commentOrigin: item.commentOrigin,
+ preview: item.preview,
+ })
+ }
+ }
+
+ const removeCommentItems = (items: { key: string }[]) => {
+ for (const item of items) {
+ prompt.context.remove(item.key)
+ }
+ }
+
+ const handleSubmit = async (event: Event) => {
+ event.preventDefault()
+
+ const currentPrompt = prompt.current()
+ const text = currentPrompt.map((part) => ("content" in part ? part.content : "")).join("")
+ const images = input.imageAttachments().slice()
+ const mode = input.mode()
+
+ if (text.trim().length === 0 && images.length === 0 && input.commentCount() === 0) {
+ if (input.working()) abort()
+ return
+ }
+
+ const currentModel = local.model.current()
+ const currentAgent = local.agent.current()
+ if (!currentModel || !currentAgent) {
+ showToast({
+ title: language.t("prompt.toast.modelAgentRequired.title"),
+ description: language.t("prompt.toast.modelAgentRequired.description"),
+ })
+ return
+ }
+
+ input.addToHistory(currentPrompt, mode)
+ input.resetHistoryNavigation()
+
+ const projectDirectory = sdk.directory
+ const isNewSession = !params.id
+ const worktreeSelection = input.newSessionWorktree ?? "main"
+
+ let sessionDirectory = projectDirectory
+ let client = sdk.client
+
+ if (isNewSession) {
+ if (worktreeSelection === "create") {
+ const createdWorktree = await client.worktree
+ .create({ directory: projectDirectory })
+ .then((x) => x.data)
+ .catch((err) => {
+ showToast({
+ title: language.t("prompt.toast.worktreeCreateFailed.title"),
+ description: errorMessage(err),
+ })
+ return undefined
+ })
+
+ if (!createdWorktree?.directory) {
+ showToast({
+ title: language.t("prompt.toast.worktreeCreateFailed.title"),
+ description: language.t("common.requestFailed"),
+ })
+ return
+ }
+ WorktreeState.pending(createdWorktree.directory)
+ sessionDirectory = createdWorktree.directory
+ }
+
+ if (worktreeSelection !== "main" && worktreeSelection !== "create") {
+ sessionDirectory = worktreeSelection
+ }
+
+ if (sessionDirectory !== projectDirectory) {
+ client = createOpencodeClient({
+ baseUrl: sdk.url,
+ fetch: platform.fetch,
+ directory: sessionDirectory,
+ throwOnError: true,
+ })
+ globalSync.child(sessionDirectory)
+ }
+
+ input.onNewSessionWorktreeReset?.()
+ }
+
+ let session = input.info()
+ if (!session && isNewSession) {
+ session = await client.session
+ .create()
+ .then((x) => x.data ?? undefined)
+ .catch((err) => {
+ showToast({
+ title: language.t("prompt.toast.sessionCreateFailed.title"),
+ description: errorMessage(err),
+ })
+ return undefined
+ })
+ if (session) {
+ layout.handoff.setTabs(base64Encode(sessionDirectory), session.id)
+ navigate(`/${base64Encode(sessionDirectory)}/session/${session.id}`)
+ }
+ }
+ if (!session) return
+
+ input.onSubmit?.()
+
+ const model = {
+ modelID: currentModel.id,
+ providerID: currentModel.provider.id,
+ }
+ const agent = currentAgent.name
+ const variant = local.model.variant.current()
+
+ const clearInput = () => {
+ prompt.reset()
+ input.setMode("normal")
+ input.setPopover(null)
+ }
+
+ const restoreInput = () => {
+ prompt.set(currentPrompt, input.promptLength(currentPrompt))
+ input.setMode(mode)
+ input.setPopover(null)
+ requestAnimationFrame(() => {
+ const editor = input.editor()
+ if (!editor) return
+ editor.focus()
+ setCursorPosition(editor, input.promptLength(currentPrompt))
+ input.queueScroll()
+ })
+ }
+
+ if (mode === "shell") {
+ clearInput()
+ client.session
+ .shell({
+ sessionID: session.id,
+ agent,
+ model,
+ command: text,
+ })
+ .catch((err) => {
+ showToast({
+ title: language.t("prompt.toast.shellSendFailed.title"),
+ description: errorMessage(err),
+ })
+ restoreInput()
+ })
+ return
+ }
+
+ if (text.startsWith("/")) {
+ const [cmdName, ...args] = text.split(" ")
+ const commandName = cmdName.slice(1)
+ const customCommand = sync.data.command.find((c) => c.name === commandName)
+ if (customCommand) {
+ clearInput()
+ client.session
+ .command({
+ sessionID: session.id,
+ command: commandName,
+ arguments: args.join(" "),
+ agent,
+ model: `${model.providerID}/${model.modelID}`,
+ variant,
+ parts: images.map((attachment) => ({
+ id: Identifier.ascending("part"),
+ type: "file" as const,
+ mime: attachment.mime,
+ url: attachment.dataUrl,
+ filename: attachment.filename,
+ })),
+ })
+ .catch((err) => {
+ showToast({
+ title: language.t("prompt.toast.commandSendFailed.title"),
+ description: errorMessage(err),
+ })
+ restoreInput()
+ })
+ return
+ }
+ }
+
+ const toAbsolutePath = (path: string) =>
+ path.startsWith("/") ? path : (sessionDirectory + "/" + path).replace("//", "/")
+
+ const fileAttachments = currentPrompt.filter((part) => part.type === "file") as FileAttachmentPart[]
+ const agentAttachments = currentPrompt.filter((part) => part.type === "agent") as AgentPart[]
+
+ const fileAttachmentParts = fileAttachments.map((attachment) => {
+ const absolute = toAbsolutePath(attachment.path)
+ const query = attachment.selection
+ ? `?start=${attachment.selection.startLine}&end=${attachment.selection.endLine}`
+ : ""
+ return {
+ id: Identifier.ascending("part"),
+ type: "file" as const,
+ mime: "text/plain",
+ url: `file://${absolute}${query}`,
+ filename: getFilename(attachment.path),
+ source: {
+ type: "file" as const,
+ text: {
+ value: attachment.content,
+ start: attachment.start,
+ end: attachment.end,
+ },
+ path: absolute,
+ },
+ }
+ })
+
+ const agentAttachmentParts = agentAttachments.map((attachment) => ({
+ id: Identifier.ascending("part"),
+ type: "agent" as const,
+ name: attachment.name,
+ source: {
+ value: attachment.content,
+ start: attachment.start,
+ end: attachment.end,
+ },
+ }))
+
+ const usedUrls = new Set(fileAttachmentParts.map((part) => part.url))
+
+ const context = prompt.context.items().slice()
+ const commentItems = context.filter((item) => item.type === "file" && !!item.comment?.trim())
+
+ const contextParts: Array<
+ | {
+ id: string
+ type: "text"
+ text: string
+ synthetic?: boolean
+ }
+ | {
+ id: string
+ type: "file"
+ mime: string
+ url: string
+ filename?: string
+ }
+ > = []
+
+ const commentNote = (path: string, selection: FileSelection | undefined, comment: string) => {
+ const start = selection ? Math.min(selection.startLine, selection.endLine) : undefined
+ const end = selection ? Math.max(selection.startLine, selection.endLine) : undefined
+ const range =
+ start === undefined || end === undefined
+ ? "this file"
+ : start === end
+ ? `line ${start}`
+ : `lines ${start} through ${end}`
+
+ return `The user made the following comment regarding ${range} of ${path}: ${comment}`
+ }
+
+ const addContextFile = (item: { path: string; selection?: FileSelection; comment?: string }) => {
+ const absolute = toAbsolutePath(item.path)
+ const query = item.selection ? `?start=${item.selection.startLine}&end=${item.selection.endLine}` : ""
+ const url = `file://${absolute}${query}`
+
+ const comment = item.comment?.trim()
+ if (!comment && usedUrls.has(url)) return
+ usedUrls.add(url)
+
+ if (comment) {
+ contextParts.push({
+ id: Identifier.ascending("part"),
+ type: "text",
+ text: commentNote(item.path, item.selection, comment),
+ synthetic: true,
+ })
+ }
+
+ contextParts.push({
+ id: Identifier.ascending("part"),
+ type: "file",
+ mime: "text/plain",
+ url,
+ filename: getFilename(item.path),
+ })
+ }
+
+ for (const item of context) {
+ if (item.type !== "file") continue
+ addContextFile({ path: item.path, selection: item.selection, comment: item.comment })
+ }
+
+ const imageAttachmentParts = images.map((attachment) => ({
+ id: Identifier.ascending("part"),
+ type: "file" as const,
+ mime: attachment.mime,
+ url: attachment.dataUrl,
+ filename: attachment.filename,
+ }))
+
+ const messageID = Identifier.ascending("message")
+ const requestParts = [
+ {
+ id: Identifier.ascending("part"),
+ type: "text" as const,
+ text,
+ },
+ ...fileAttachmentParts,
+ ...contextParts,
+ ...agentAttachmentParts,
+ ...imageAttachmentParts,
+ ]
+
+ const optimisticParts = requestParts.map((part) => ({
+ ...part,
+ sessionID: session.id,
+ messageID,
+ })) as unknown as Part[]
+
+ const optimisticMessage: Message = {
+ id: messageID,
+ sessionID: session.id,
+ role: "user",
+ time: { created: Date.now() },
+ agent,
+ model,
+ }
+
+ const addOptimisticMessage = () => {
+ if (sessionDirectory === projectDirectory) {
+ sync.set(
+ produce((draft) => {
+ const messages = draft.message[session.id]
+ if (!messages) {
+ draft.message[session.id] = [optimisticMessage]
+ } else {
+ const result = Binary.search(messages, messageID, (m) => m.id)
+ messages.splice(result.index, 0, optimisticMessage)
+ }
+ draft.part[messageID] = optimisticParts
+ .filter((part) => !!part?.id)
+ .slice()
+ .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
+ }),
+ )
+ return
+ }
+
+ globalSync.child(sessionDirectory)[1](
+ produce((draft) => {
+ const messages = draft.message[session.id]
+ if (!messages) {
+ draft.message[session.id] = [optimisticMessage]
+ } else {
+ const result = Binary.search(messages, messageID, (m) => m.id)
+ messages.splice(result.index, 0, optimisticMessage)
+ }
+ draft.part[messageID] = optimisticParts
+ .filter((part) => !!part?.id)
+ .slice()
+ .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
+ }),
+ )
+ }
+
+ const removeOptimisticMessage = () => {
+ if (sessionDirectory === projectDirectory) {
+ sync.set(
+ produce((draft) => {
+ const messages = draft.message[session.id]
+ if (messages) {
+ const result = Binary.search(messages, messageID, (m) => m.id)
+ if (result.found) messages.splice(result.index, 1)
+ }
+ delete draft.part[messageID]
+ }),
+ )
+ return
+ }
+
+ globalSync.child(sessionDirectory)[1](
+ produce((draft) => {
+ const messages = draft.message[session.id]
+ if (messages) {
+ const result = Binary.search(messages, messageID, (m) => m.id)
+ if (result.found) messages.splice(result.index, 1)
+ }
+ delete draft.part[messageID]
+ }),
+ )
+ }
+
+ removeCommentItems(commentItems)
+ clearInput()
+ addOptimisticMessage()
+
+ const waitForWorktree = async () => {
+ const worktree = WorktreeState.get(sessionDirectory)
+ if (!worktree || worktree.status !== "pending") return true
+
+ if (sessionDirectory === projectDirectory) {
+ sync.set("session_status", session.id, { type: "busy" })
+ }
+
+ const controller = new AbortController()
+ const cleanup = () => {
+ if (sessionDirectory === projectDirectory) {
+ sync.set("session_status", session.id, { type: "idle" })
+ }
+ removeOptimisticMessage()
+ restoreCommentItems(commentItems)
+ restoreInput()
+ }
+
+ pending.set(session.id, { abort: controller, cleanup })
+
+ const abortWait = new Promise<Awaited<ReturnType<typeof WorktreeState.wait>>>((resolve) => {
+ if (controller.signal.aborted) {
+ resolve({ status: "failed", message: "aborted" })
+ return
+ }
+ controller.signal.addEventListener(
+ "abort",
+ () => {
+ resolve({ status: "failed", message: "aborted" })
+ },
+ { once: true },
+ )
+ })
+
+ const timeoutMs = 5 * 60 * 1000
+ const timer = { id: undefined as number | undefined }
+ const timeout = new Promise<Awaited<ReturnType<typeof WorktreeState.wait>>>((resolve) => {
+ timer.id = window.setTimeout(() => {
+ resolve({ status: "failed", message: language.t("workspace.error.stillPreparing") })
+ }, timeoutMs)
+ })
+
+ const result = await Promise.race([WorktreeState.wait(sessionDirectory), abortWait, timeout]).finally(() => {
+ if (timer.id === undefined) return
+ clearTimeout(timer.id)
+ })
+ pending.delete(session.id)
+ if (controller.signal.aborted) return false
+ if (result.status === "failed") throw new Error(result.message)
+ return true
+ }
+
+ const send = async () => {
+ const ok = await waitForWorktree()
+ if (!ok) return
+ await client.session.prompt({
+ sessionID: session.id,
+ agent,
+ model,
+ messageID,
+ parts: requestParts,
+ variant,
+ })
+ }
+
+ void send().catch((err) => {
+ pending.delete(session.id)
+ if (sessionDirectory === projectDirectory) {
+ sync.set("session_status", session.id, { type: "idle" })
+ }
+ showToast({
+ title: language.t("prompt.toast.promptSendFailed.title"),
+ description: errorMessage(err),
+ })
+ removeOptimisticMessage()
+ restoreCommentItems(commentItems)
+ restoreInput()
+ })
+ }
+
+ return {
+ abort,
+ handleSubmit,
+ }
+}
diff --git a/packages/app/src/components/session-context-usage.tsx b/packages/app/src/components/session-context-usage.tsx
index c6256395f..4e5dae139 100644
--- a/packages/app/src/components/session-context-usage.tsx
+++ b/packages/app/src/components/session-context-usage.tsx
@@ -3,12 +3,11 @@ import { Tooltip } from "@opencode-ai/ui/tooltip"
import { ProgressCircle } from "@opencode-ai/ui/progress-circle"
import { Button } from "@opencode-ai/ui/button"
import { useParams } from "@solidjs/router"
-import { AssistantMessage } from "@opencode-ai/sdk/v2/client"
-import { findLast } from "@opencode-ai/util/array"
import { useLayout } from "@/context/layout"
import { useSync } from "@/context/sync"
import { useLanguage } from "@/context/language"
+import { getSessionContextMetrics } from "@/components/session/session-context-metrics"
interface SessionContextUsageProps {
variant?: "button" | "indicator"
@@ -34,26 +33,10 @@ export function SessionContextUsage(props: SessionContextUsageProps) {
}),
)
+ const metrics = createMemo(() => getSessionContextMetrics(messages(), sync.data.provider.all))
+ const context = createMemo(() => metrics().context)
const cost = createMemo(() => {
- const total = messages().reduce((sum, x) => sum + (x.role === "assistant" ? x.cost : 0), 0)
- return usd().format(total)
- })
-
- const context = createMemo(() => {
- const locale = language.locale()
- const last = findLast(messages(), (x) => {
- if (x.role !== "assistant") return false
- const total = x.tokens.input + x.tokens.output + x.tokens.reasoning + x.tokens.cache.read + x.tokens.cache.write
- return total > 0
- }) as AssistantMessage
- if (!last) return
- const total =
- last.tokens.input + last.tokens.output + last.tokens.reasoning + last.tokens.cache.read + last.tokens.cache.write
- const model = sync.data.provider.all.find((x) => x.id === last.providerID)?.models[last.modelID]
- return {
- tokens: total.toLocaleString(locale),
- percentage: model?.limit.context ? Math.round((total / model.limit.context) * 100) : null,
- }
+ return usd().format(metrics().totalCost)
})
const openContext = () => {
@@ -67,7 +50,7 @@ export function SessionContextUsage(props: SessionContextUsageProps) {
const circle = () => (
<div class="flex items-center justify-center">
- <ProgressCircle size={16} strokeWidth={2} percentage={context()?.percentage ?? 0} />
+ <ProgressCircle size={16} strokeWidth={2} percentage={context()?.usage ?? 0} />
</div>
)
@@ -77,11 +60,11 @@ export function SessionContextUsage(props: SessionContextUsageProps) {
{(ctx) => (
<>
<div class="flex items-center gap-2">
- <span class="text-text-invert-strong">{ctx().tokens}</span>
+ <span class="text-text-invert-strong">{ctx().total.toLocaleString(language.locale())}</span>
<span class="text-text-invert-base">{language.t("context.usage.tokens")}</span>
</div>
<div class="flex items-center gap-2">
- <span class="text-text-invert-strong">{ctx().percentage ?? 0}%</span>
+ <span class="text-text-invert-strong">{ctx().usage ?? 0}%</span>
<span class="text-text-invert-base">{language.t("context.usage.usage")}</span>
</div>
</>
diff --git a/packages/app/src/components/session/session-context-metrics.test.ts b/packages/app/src/components/session/session-context-metrics.test.ts
new file mode 100644
index 000000000..68903a455
--- /dev/null
+++ b/packages/app/src/components/session/session-context-metrics.test.ts
@@ -0,0 +1,93 @@
+import { describe, expect, test } from "bun:test"
+import type { Message } from "@opencode-ai/sdk/v2/client"
+import { getSessionContextMetrics } from "./session-context-metrics"
+
+const assistant = (
+ id: string,
+ tokens: { input: number; output: number; reasoning: number; read: number; write: number },
+ cost: number,
+ providerID = "openai",
+ modelID = "gpt-4.1",
+) => {
+ return {
+ id,
+ role: "assistant",
+ providerID,
+ modelID,
+ cost,
+ tokens: {
+ input: tokens.input,
+ output: tokens.output,
+ reasoning: tokens.reasoning,
+ cache: {
+ read: tokens.read,
+ write: tokens.write,
+ },
+ },
+ time: { created: 1 },
+ } as unknown as Message
+}
+
+const user = (id: string) => {
+ return {
+ id,
+ role: "user",
+ cost: 0,
+ time: { created: 1 },
+ } as unknown as Message
+}
+
+describe("getSessionContextMetrics", () => {
+ test("computes totals and usage from latest assistant with tokens", () => {
+ const messages = [
+ user("u1"),
+ assistant("a1", { input: 0, output: 0, reasoning: 0, read: 0, write: 0 }, 0.5),
+ assistant("a2", { input: 300, output: 100, reasoning: 50, read: 25, write: 25 }, 1.25),
+ ]
+ const providers = [
+ {
+ id: "openai",
+ name: "OpenAI",
+ models: {
+ "gpt-4.1": {
+ name: "GPT-4.1",
+ limit: { context: 1000 },
+ },
+ },
+ },
+ ]
+
+ const metrics = getSessionContextMetrics(messages, providers)
+
+ expect(metrics.totalCost).toBe(1.75)
+ expect(metrics.context?.message.id).toBe("a2")
+ expect(metrics.context?.total).toBe(500)
+ expect(metrics.context?.usage).toBe(50)
+ expect(metrics.context?.providerLabel).toBe("OpenAI")
+ expect(metrics.context?.modelLabel).toBe("GPT-4.1")
+ })
+
+ test("preserves fallback labels and null usage when model metadata is missing", () => {
+ const messages = [assistant("a1", { input: 40, output: 10, reasoning: 0, read: 0, write: 0 }, 0.1, "p-1", "m-1")]
+ const providers = [{ id: "p-1", models: {} }]
+
+ const metrics = getSessionContextMetrics(messages, providers)
+
+ expect(metrics.context?.providerLabel).toBe("p-1")
+ expect(metrics.context?.modelLabel).toBe("m-1")
+ expect(metrics.context?.limit).toBeUndefined()
+ expect(metrics.context?.usage).toBeNull()
+ })
+
+ test("memoizes by message and provider array identity", () => {
+ const messages = [assistant("a1", { input: 10, output: 10, reasoning: 10, read: 10, write: 10 }, 0.25)]
+ const providers = [{ id: "openai", models: {} }]
+
+ const one = getSessionContextMetrics(messages, providers)
+ const two = getSessionContextMetrics(messages, providers)
+ const three = getSessionContextMetrics([...messages], providers)
+
+ expect(two).toBe(one)
+ expect(three).not.toBe(one)
+ })
+})
diff --git a/packages/app/src/components/session/session-context-metrics.ts b/packages/app/src/components/session/session-context-metrics.ts
new file mode 100644
index 000000000..2b6edbd95
--- /dev/null
+++ b/packages/app/src/components/session/session-context-metrics.ts
@@ -0,0 +1,94 @@
+import type { AssistantMessage, Message } from "@opencode-ai/sdk/v2/client"
+
+type Provider = {
+ id: string
+ name?: string
+ models: Record<string, Model | undefined>
+}
+
+type Model = {
+ name?: string
+ limit: {
+ context: number
+ }
+}
+
+type Context = {
+ message: AssistantMessage
+ provider?: Provider
+ model?: Model
+ providerLabel: string
+ modelLabel: string
+ limit: number | undefined
+ input: number
+ output: number
+ reasoning: number
+ cacheRead: number
+ cacheWrite: number
+ total: number
+ usage: number | null
+}
+
+type Metrics = {
+ totalCost: number
+ context: Context | undefined
+}
+
+const cache = new WeakMap<Message[], WeakMap<Provider[], Metrics>>()
+
+const tokenTotal = (msg: AssistantMessage) => {
+ return msg.tokens.input + msg.tokens.output + msg.tokens.reasoning + msg.tokens.cache.read + msg.tokens.cache.write
+}
+
+const lastAssistantWithTokens = (messages: Message[]) => {
+ for (let i = messages.length - 1; i >= 0; i--) {
+ const msg = messages[i]
+ if (msg.role !== "assistant") continue
+ if (tokenTotal(msg) <= 0) continue
+ return msg
+ }
+}
+
+const build = (messages: Message[], providers: Provider[]): Metrics => {
+ const totalCost = messages.reduce((sum, msg) => sum + (msg.role === "assistant" ? msg.cost : 0), 0)
+ const message = lastAssistantWithTokens(messages)
+ if (!message) return { totalCost, context: undefined }
+
+ const provider = providers.find((item) => item.id === message.providerID)
+ const model = provider?.models[message.modelID]
+ const limit = model?.limit.context
+ const total = tokenTotal(message)
+
+ return {
+ totalCost,
+ context: {
+ message,
+ provider,
+ model,
+ providerLabel: provider?.name ?? message.providerID,
+ modelLabel: model?.name ?? message.modelID,
+ limit,
+ input: message.tokens.input,
+ output: message.tokens.output,
+ reasoning: message.tokens.reasoning,
+ cacheRead: message.tokens.cache.read,
+ cacheWrite: message.tokens.cache.write,
+ total,
+ usage: limit ? Math.round((total / limit) * 100) : null,
+ },
+ }
+}
+
+export function getSessionContextMetrics(messages: Message[], providers: Provider[]) {
+ const byProvider = cache.get(messages)
+ if (byProvider) {
+ const hit = byProvider.get(providers)
+ if (hit) return hit
+ }
+
+ const value = build(messages, providers)
+ const next = byProvider ?? new WeakMap<Provider[], Metrics>()
+ next.set(providers, value)
+ if (!byProvider) cache.set(messages, next)
+ return value
+}
diff --git a/packages/app/src/components/session/session-context-tab.tsx b/packages/app/src/components/session/session-context-tab.tsx
index 37733caff..8aae44863 100644
--- a/packages/app/src/components/session/session-context-tab.tsx
+++ b/packages/app/src/components/session/session-context-tab.tsx
@@ -11,8 +11,9 @@ import { Accordion } from "@opencode-ai/ui/accordion"
import { StickyAccordionHeader } from "@opencode-ai/ui/sticky-accordion-header"
import { Code } from "@opencode-ai/ui/code"
import { Markdown } from "@opencode-ai/ui/markdown"
-import type { AssistantMessage, Message, Part, UserMessage } from "@opencode-ai/sdk/v2/client"
+import type { Message, Part, UserMessage } from "@opencode-ai/sdk/v2/client"
import { useLanguage } from "@/context/language"
+import { getSessionContextMetrics } from "./session-context-metrics"
interface SessionContextTabProps {
messages: () => Message[]
@@ -34,44 +35,11 @@ export function SessionContextTab(props: SessionContextTabProps) {
}),
)
- const ctx = createMemo(() => {
- const last = findLast(props.messages(), (x) => {
- if (x.role !== "assistant") return false
- const total = x.tokens.input + x.tokens.output + x.tokens.reasoning + x.tokens.cache.read + x.tokens.cache.write
- return total > 0
- }) as AssistantMessage
- if (!last) return
-
- const provider = sync.data.provider.all.find((x) => x.id === last.providerID)
- const model = provider?.models[last.modelID]
- const limit = model?.limit.context
-
- const input = last.tokens.input
- const output = last.tokens.output
- const reasoning = last.tokens.reasoning
- const cacheRead = last.tokens.cache.read
- const cacheWrite = last.tokens.cache.write
- const total = input + output + reasoning + cacheRead + cacheWrite
- const usage = limit ? Math.round((total / limit) * 100) : null
-
- return {
- message: last,
- provider,
- model,
- limit,
- input,
- output,
- reasoning,
- cacheRead,
- cacheWrite,
- total,
- usage,
- }
- })
+ const metrics = createMemo(() => getSessionContextMetrics(props.messages(), sync.data.provider.all))
+ const ctx = createMemo(() => metrics().context)
const cost = createMemo(() => {
- const total = props.messages().reduce((sum, x) => sum + (x.role === "assistant" ? x.cost : 0), 0)
- return usd().format(total)
+ return usd().format(metrics().totalCost)
})
const counts = createMemo(() => {
@@ -114,14 +82,13 @@ export function SessionContextTab(props: SessionContextTabProps) {
const providerLabel = createMemo(() => {
const c = ctx()
if (!c) return "—"
- return c.provider?.name ?? c.message.providerID
+ return c.providerLabel
})
const modelLabel = createMemo(() => {
const c = ctx()
if (!c) return "—"
- if (c.model?.name) return c.model.name
- return c.message.modelID
+ return c.modelLabel
})
const breakdown = createMemo(