mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(session-ui): render user message attachments above text and admit inline files (#54650)
This commit is contained in:
committed by
GitHub
Unverified
parent
43bb7ba3a2
commit
4ea42c47df
@@ -141,6 +141,27 @@ function locationQuery(ref: LocationRef) {
|
||||
return { directory: ref.directory }
|
||||
}
|
||||
|
||||
function optimisticFiles(files: SessionPromptInput["files"]) {
|
||||
const items = files?.flatMap((file) => {
|
||||
if (!file.uri.startsWith("data:")) return []
|
||||
const comma = file.uri.indexOf(",")
|
||||
if (comma < 0) return []
|
||||
const header = file.uri.slice(5, comma)
|
||||
const parts = header.split(";")
|
||||
if (!parts.slice(1).some((part) => part.toLowerCase() === "base64")) return []
|
||||
const item: NonNullable<Extract<SessionInboxInfo, { type: "user" }>["payload"]["files"]>[number] = {
|
||||
data: file.uri.slice(comma + 1),
|
||||
mime: parts[0] || "application/octet-stream",
|
||||
source: { type: "inline" },
|
||||
}
|
||||
if (file.name !== undefined) item.name = file.name
|
||||
if (file.description !== undefined) item.description = file.description
|
||||
if (file.mention !== undefined) item.mention = { ...file.mention }
|
||||
return [item]
|
||||
})
|
||||
return items?.length ? items : undefined
|
||||
}
|
||||
|
||||
function formRequestOptions(sessionID: string, ref?: LocationRef) {
|
||||
if (sessionID !== "global" || !ref) return undefined
|
||||
return {
|
||||
@@ -1597,6 +1618,13 @@ export function createData(config: CreateDataInput) {
|
||||
!messageIndex.get(request.sessionID)?.has(id) &&
|
||||
!store.session.pending[request.sessionID]?.some((item) => item.id === id)
|
||||
if (fresh) {
|
||||
const files = optimisticFiles(request.files)
|
||||
const payload: Extract<SessionInboxInfo, { type: "user" }>["payload"] = {
|
||||
text: request.text,
|
||||
agents: request.agents?.map((agent) => ({ ...agent })),
|
||||
metadata: request.metadata,
|
||||
}
|
||||
if (files) payload.files = files
|
||||
outbox.add(id)
|
||||
admitLocal({
|
||||
id,
|
||||
@@ -1604,14 +1632,11 @@ export function createData(config: CreateDataInput) {
|
||||
time: { created: Date.now() },
|
||||
type: "user",
|
||||
delivery: request.delivery ?? "steer",
|
||||
// Files and skills stay off the optimistic row: their durable
|
||||
// File-URI attachments and skills stay off the optimistic row: their durable
|
||||
// forms are server-loaded (content, mime, resolution), so they
|
||||
// fill in when the echo upserts the row.
|
||||
payload: {
|
||||
text: request.text,
|
||||
agents: request.agents?.map((agent) => ({ ...agent })),
|
||||
metadata: request.metadata,
|
||||
},
|
||||
// fill in when the echo upserts the row. Inline data URLs already carry
|
||||
// their mime and base64 payload, so they render immediately.
|
||||
payload,
|
||||
})
|
||||
}
|
||||
return sendAdmission(
|
||||
|
||||
@@ -178,3 +178,52 @@ test("a queued prompt is queued regardless of execution state", () => {
|
||||
setup.dispose()
|
||||
}
|
||||
})
|
||||
|
||||
test("optimistic prompt admission preserves inline data attachments before the server echo", async () => {
|
||||
const gate = Promise.withResolvers<void>()
|
||||
const setup = fixture()
|
||||
try {
|
||||
const sending = setup.data.session.prompt({
|
||||
sessionID,
|
||||
id: "msg_with_image",
|
||||
text: "Inspect this screenshot",
|
||||
files: [
|
||||
{ uri: "data:image/png;base64,YQ==", name: "shot.png" },
|
||||
{ uri: "file:///project/README.md", name: "README.md" },
|
||||
],
|
||||
gate: gate.promise,
|
||||
})
|
||||
|
||||
const expected = [
|
||||
{
|
||||
data: "YQ==",
|
||||
mime: "image/png",
|
||||
source: { type: "inline" as const },
|
||||
name: "shot.png",
|
||||
},
|
||||
]
|
||||
|
||||
expect(setup.data.session.pending.list(sessionID)).toMatchObject([
|
||||
{
|
||||
id: "msg_with_image",
|
||||
type: "user",
|
||||
payload: {
|
||||
text: "Inspect this screenshot",
|
||||
files: expected,
|
||||
},
|
||||
},
|
||||
])
|
||||
expect(setup.data.session.message.get(sessionID, "msg_with_image")).toMatchObject({
|
||||
id: "msg_with_image",
|
||||
type: "user",
|
||||
text: "Inspect this screenshot",
|
||||
files: expected,
|
||||
})
|
||||
|
||||
gate.resolve()
|
||||
await sending
|
||||
} finally {
|
||||
gate.resolve()
|
||||
setup.dispose()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -79,4 +79,11 @@ story("renders user image, file attachment, file reference, and agent reference"
|
||||
await expect(timeline.getByText("tsconfig.json")).toBeVisible()
|
||||
await expect(timeline.getByText("@src/a.ts", { exact: true })).toBeVisible()
|
||||
await expect(timeline.getByText("@explore", { exact: true })).toBeVisible()
|
||||
|
||||
const attachments = timeline.locator('[data-slot="user-message-attachments"]')
|
||||
const body = timeline.locator('[data-slot="user-message-body"]')
|
||||
const [attachmentsBox, bodyBox] = await Promise.all([attachments.boundingBox(), body.boundingBox()])
|
||||
expect(attachmentsBox).not.toBeNull()
|
||||
expect(bodyBox).not.toBeNull()
|
||||
expect(bodyBox!.y - (attachmentsBox!.y + attachmentsBox!.height)).toBe(8)
|
||||
})
|
||||
|
||||
@@ -3,18 +3,22 @@ import type { PromptFileAttachment } from "@opencode/client/promise"
|
||||
import { attached, typeLabel } from "./message-file"
|
||||
|
||||
function file(source: PromptFileAttachment["source"], mention = false): PromptFileAttachment {
|
||||
return {
|
||||
const item: PromptFileAttachment = {
|
||||
data: "",
|
||||
mime: "text/plain",
|
||||
source,
|
||||
...(mention ? { mention: { text: "@README.md", start: 0, end: 10 } } : {}),
|
||||
}
|
||||
|
||||
if (mention) item.mention = { text: "@README.md", start: 0, end: 10 }
|
||||
|
||||
return item
|
||||
}
|
||||
|
||||
describe("message-file", () => {
|
||||
test("only treats data-backed files without mentions as attachments", () => {
|
||||
expect(attached(file({ type: "inline" }))).toBe(true)
|
||||
expect(attached(file({ type: "uri", uri: "data:text/plain;base64,SGVsbG8=" }))).toBe(true)
|
||||
expect(attached(file({ type: "uri", uri: "blob:http://localhost/image-1" }))).toBe(true)
|
||||
expect(attached(file({ type: "uri", uri: "file:///repo/README.md" }))).toBe(false)
|
||||
expect(attached(file({ type: "inline" }, true))).toBe(false)
|
||||
})
|
||||
|
||||
@@ -7,13 +7,13 @@ export function attached(file: PromptFileAttachment) {
|
||||
|
||||
if (file.source.type === "inline") return true
|
||||
|
||||
return file.source.uri.startsWith("data:")
|
||||
return file.source.uri.startsWith("data:") || file.source.uri.startsWith("blob:")
|
||||
}
|
||||
|
||||
// language metadata only; grammars stay behind shiki's lazy imports
|
||||
const LANGUAGE_NAMES = new Map<string, string>(
|
||||
bundledLanguagesInfo.flatMap((info) =>
|
||||
[info.id, ...(info.aliases ?? [])].map((alias) => [alias, info.name] as [string, string]),
|
||||
[info.id, ...(info.aliases ?? [])].map((alias): [string, string] => [alias, info.name]),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -1231,8 +1231,8 @@
|
||||
}
|
||||
}
|
||||
|
||||
[data-slot="user-message-body"] + [data-slot="user-message-attachments"],
|
||||
[data-slot="user-message-comments"] + [data-slot="user-message-attachments"] {
|
||||
[data-slot="user-message-attachments"] + [data-slot="user-message-body"],
|
||||
[data-slot="user-message-attachments"] + [data-slot="user-message-comments"] {
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
import { createEffect, createMemo, createSignal, For, onCleanup, Show, type ComponentProps, type JSX } from "solid-js"
|
||||
import {
|
||||
createEffect,
|
||||
createMemo,
|
||||
createSignal,
|
||||
For,
|
||||
Index,
|
||||
onCleanup,
|
||||
Show,
|
||||
type ComponentProps,
|
||||
type JSX,
|
||||
} from "solid-js"
|
||||
import { createStore } from "solid-js/store"
|
||||
import { createMediaQuery } from "@solid-primitives/media"
|
||||
import { useData } from "../context"
|
||||
@@ -323,11 +333,16 @@ export function CurrentUserMessageDisplay(props: {
|
||||
</AttachmentCard>
|
||||
)}
|
||||
</For>
|
||||
<For each={attachments()}>
|
||||
<Index each={attachments()}>
|
||||
{(file) => {
|
||||
const url = () => (file.source.type === "uri" ? file.source.uri : `data:${file.mime};base64,${file.data}`)
|
||||
const name = () => file.name ?? i18n.t("ui.message.attachment.alt")
|
||||
const image = () => file.mime.startsWith("image/")
|
||||
const url = () => {
|
||||
const source = file().source
|
||||
|
||||
return source.type === "uri" ? source.uri : `data:${file().mime};base64,${file().data}`
|
||||
}
|
||||
|
||||
const name = () => file().name ?? i18n.t("ui.message.attachment.alt")
|
||||
const image = () => file().mime.startsWith("image/")
|
||||
|
||||
return (
|
||||
<Show
|
||||
@@ -347,14 +362,14 @@ export function CurrentUserMessageDisplay(props: {
|
||||
title={getFilename(name())}
|
||||
hover={name()}
|
||||
clickable={!!props.actions?.openAttachment}
|
||||
onClick={() => props.actions?.openAttachment?.(file)}
|
||||
onClick={() => props.actions?.openAttachment?.(file())}
|
||||
>
|
||||
{typeLabel(name(), file.mime, i18n.t("ui.common.file"))}
|
||||
{typeLabel(name(), file().mime, i18n.t("ui.common.file"))}
|
||||
</AttachmentCard>
|
||||
</Show>
|
||||
)
|
||||
}}
|
||||
</For>
|
||||
</Index>
|
||||
</div>
|
||||
</Show>
|
||||
)
|
||||
@@ -365,6 +380,7 @@ export function CurrentUserMessageDisplay(props: {
|
||||
data-pending={waiting() ? "true" : undefined}
|
||||
data-timeline-part-id={props.text ? `${props.message.id}:text:0` : undefined}
|
||||
>
|
||||
{renderAttachments()}
|
||||
<Show
|
||||
when={props.text}
|
||||
fallback={
|
||||
@@ -382,7 +398,6 @@ export function CurrentUserMessageDisplay(props: {
|
||||
</div>
|
||||
</div>
|
||||
</Show>
|
||||
{renderAttachments()}
|
||||
{/* Like the TUI, a pending steer of only attachments still offers its actions. */}
|
||||
<Show when={props.text || comments().length > 0 || pending()}>
|
||||
<div data-slot="user-message-copy-wrapper">
|
||||
|
||||
@@ -38,6 +38,10 @@ export default defineMain({
|
||||
dedupe: ["solid-js", "solid-js/web", "@solidjs/meta"],
|
||||
alias: [
|
||||
{ find: "@solidjs/router", replacement: path.resolve(mocks, "solid-router.tsx") },
|
||||
{
|
||||
find: "@tanstack/solid-query",
|
||||
replacement: path.resolve(here, "../../app/node_modules/@tanstack/solid-query"),
|
||||
},
|
||||
{ find: /^@\/providers\/models\/selection$/, replacement: path.resolve(mocks, "app/context/local.ts") },
|
||||
{ find: /^@\/workspaces\/files\/model$/, replacement: path.resolve(mocks, "app/context/file.ts") },
|
||||
{ find: /^@\/shell\/state\/layout$/, replacement: path.resolve(mocks, "app/context/layout.ts") },
|
||||
|
||||
Reference in New Issue
Block a user