Repository navigation
feat(tui): load stored images by reference, like desktop #60116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
081b005
feat(tui): load stored media by ref (t570)
kitlangton 96e9f4d
fix(tui): keep undone prompts when their tab closes, keep plugin read…
kitlangton e16d9b2
fix(tui): rewind past images the server no longer has, simplify resto…
kitlangton a5d31ca
fix(tui): restore late undos into the session's current composer (t570)
kitlangton e6a9efc
fix(tui): register seeded composers for late undos (t570)
kitlangton de598c9
refactor(tui): share prompt restore paths and simplify media plumbing…
kitlangton 9c89ce2
test(tui): clear the home draft another test file leaves before media…
kitlangton 6fe2334
test(tui): wait for each rewind's outcome, not its request, in media-…
kitlangton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import type { make, RequestOptions } from "./client.js" | ||
| import type { MessageListInput, SessionInboxListInput, SessionMessageGetInput } from "./generated/types.js" | ||
|
|
||
| const MEDIA_URI_CAPABILITY = "experimental-media-uri" | ||
|
|
||
| const CAPABILITIES_HEADER = "X-OpenCode-Capabilities" | ||
|
|
||
| const MEDIA_REF = /^media:([0-9a-f]{64})$/ | ||
|
|
||
| /** `mediaRefs: false` reads stored media as inline bytes, for callers such as export that need self-contained messages. */ | ||
| export type MediaRequestOptions = RequestOptions & { readonly mediaRefs?: false } | ||
|
|
||
| /** The blob hash in a `media:<hash>` ref, the only form the server sends stored media in. */ | ||
| export function mediaHashFromUri(uri: string | undefined): string | undefined { | ||
| if (!uri) return undefined | ||
|
|
||
| return MEDIA_REF.exec(uri)?.[1] | ||
| } | ||
|
|
||
| /** | ||
| * Opts a client into the experimental `experimental-media-uri` capability: while `experimental.media_blob` is on, | ||
| * stored media comes back as `media:<hash>` refs, whose bytes `experimental.media.get` reads. | ||
| * | ||
| * Only the message and inbox reads can return refs, so only they carry the capability. | ||
| * A custom header makes a cross-origin request preflighted, which every other request must keep avoiding. | ||
| */ | ||
| export function withMediaRefs(api: ReturnType<typeof make>) { | ||
| return { | ||
| ...api, | ||
| message: { | ||
| ...api.message, | ||
| list: (input: MessageListInput, options?: MediaRequestOptions) => | ||
| api.message.list(input, mediaRefOptions(options)), | ||
| }, | ||
| session: { | ||
| ...api.session, | ||
| message: { | ||
| ...api.session.message, | ||
| get: (input: SessionMessageGetInput, options?: MediaRequestOptions) => | ||
| api.session.message.get(input, mediaRefOptions(options)), | ||
| }, | ||
| inbox: { | ||
| ...api.session.inbox, | ||
| list: (input: SessionInboxListInput, options?: MediaRequestOptions) => | ||
| api.session.inbox.list(input, mediaRefOptions(options)), | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| function mediaRefOptions(options: MediaRequestOptions | undefined): RequestOptions | undefined { | ||
| if (options?.mediaRefs === false) return options | ||
| const headers = new Headers(options?.headers) | ||
| headers.set(CAPABILITIES_HEADER, MEDIA_URI_CAPABILITY) | ||
|
|
||
| return { ...options, headers } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { mediaHashFromUri, OpenCode, withMediaRefs } from "../src/promise/index" | ||
|
|
||
| test("withMediaRefs asks for media refs on message and inbox reads only", async () => { | ||
| const requests: Array<{ path: string; headers: Headers }> = [] | ||
| const api = withMediaRefs( | ||
| OpenCode.make({ | ||
| baseUrl: "http://localhost:3000", | ||
| headers: { Authorization: "Basic secret" }, | ||
| // SAFETY: the API client only calls fetch with a URL and init, which this stub accepts. | ||
| fetch: (async (input: string | URL | Request, init?: RequestInit) => { | ||
| requests.push({ | ||
| path: new URL(input instanceof Request ? input.url : input).pathname, | ||
| headers: new Headers(init?.headers), | ||
| }) | ||
| return Response.json({ data: [], cursor: {} }) | ||
| }) as typeof fetch, | ||
| }), | ||
| ) | ||
|
|
||
| await api.message.list({ sessionID: "ses_1" }) | ||
| await api.message.list({ sessionID: "ses_1" }, { mediaRefs: false }) | ||
| await api.session.message.get({ sessionID: "ses_1", messageID: "msg_1" }) | ||
| await api.session.inbox.list({ sessionID: "ses_1" }, { headers: { "X-Trace": "1" } }) | ||
| await api.session.list() | ||
|
|
||
| expect( | ||
| requests.map((request) => [ | ||
| request.path, | ||
| request.headers.get("x-opencode-capabilities"), | ||
| request.headers.get("authorization"), | ||
| ]), | ||
| ).toEqual([ | ||
| ["/api/session/ses_1/message", "experimental-media-uri", "Basic secret"], | ||
| ["/api/session/ses_1/message", null, "Basic secret"], | ||
| ["/api/session/ses_1/message/msg_1", "experimental-media-uri", "Basic secret"], | ||
| ["/api/session/ses_1/inbox", "experimental-media-uri", "Basic secret"], | ||
| ["/api/session", null, "Basic secret"], | ||
| ]) | ||
| expect(requests[3]?.headers.get("x-trace")).toBe("1") | ||
| }) | ||
|
|
||
| test.each([ | ||
| [`media:${"a".repeat(64)}`, "a".repeat(64)], | ||
| [`media:${"A".repeat(64)}`, undefined], | ||
| [`media:${"a".repeat(63)}`, undefined], | ||
| ["data:image/png;base64,AAA", undefined], | ||
| [undefined, undefined], | ||
| ])("mediaHashFromUri(%p)", (uri, hash) => { | ||
| expect(mediaHashFromUri(uri)).toBe(hash) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { mediaHashFromUri } from "@opencode/client" | ||
| import { createEffect, createSignal, on, onCleanup, useContext, type Accessor } from "solid-js" | ||
| import { ClientContext } from "../context/client" | ||
|
|
||
| /** | ||
| * An `<image>` source for a URI that may be a `media:<hash>` ref. A ref reads its bytes from the server once something | ||
| * shows it, so images nobody looks at fetch nothing; other URIs pass through. `failed` covers a failed read and, through | ||
| * `onError`, a failed decode, until the URI changes. | ||
| */ | ||
| export function createMediaSource(uri: Accessor<string>) { | ||
| // Optional: image dialogs also render outside a connected client, where only plain URIs can load. | ||
| const media = useContext(ClientContext)?.media | ||
| const [bytes, setBytes] = createSignal(media?.peek(uri())) | ||
| const [failed, setFailed] = createSignal(false) | ||
|
|
||
| createEffect( | ||
| on(uri, (current) => { | ||
| setFailed(false) | ||
| const cached = media?.peek(current) | ||
| setBytes(cached) | ||
| if (cached || !mediaHashFromUri(current)) return | ||
| if (!media) return setFailed(true) | ||
| let active = true | ||
| onCleanup(() => { | ||
| active = false | ||
| }) | ||
| media.read(current).then( | ||
| (result) => { | ||
| if (active) setBytes(result) | ||
| }, | ||
| () => { | ||
| if (active) setFailed(true) | ||
| }, | ||
| ) | ||
| }), | ||
| ) | ||
|
|
||
| return { | ||
| source: (): string | Uint8Array | undefined => (mediaHashFromUri(uri()) ? bytes() : uri()), | ||
| failed, | ||
| onError: () => setFailed(true), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds the restored prompt after the composer's current text for every caller. Before,
/undo, timeline revert and the message dialog's revert replaced the composer; only the queued-prompt undo added to it. So now pressing undo twice givesA\n\nB(or the same prompt twice), and an unsent draft is kept in front of the undone prompt. This happens even withexperimental.media_bloboff. I saw it with this PR's fixture: type a draft, press<leader>utwice, and the composer shows the draft plus the undone prompt twice. CouldrestoreToTabtake a replace/append mode, so undo and revert keep replacing (both the mounted composer and the stashed draft) and only the queued-prompt undo adds to the text?