feat: context usage ring and tool image previews

- the composer shows a context ring and percentage from the last
  token-bearing assistant message, toned at 75% and 90%; tapping opens a
  sheet with the model, used tokens, limits, output reserve and turn cost
- images a tool call names (read attachments as data URLs, input paths,
  and image paths in output text) get a link under the tool row; tapping
  previews it from the data URL or /api/fs/raw, allowing paths outside
  the workspace
- directory-scoped requests now send x-opencode-directory-encoding: uri,
  which scoped routes such as /api/fs/raw need to decode the header
- ocprobe gains a raw file command for checking previews

Bump version to 0.2.2 (build 16).
This commit is contained in:
blumia committed 2026-11-18 02:15:11 +08:00
1 parent dc3730eba7
commit 2fb4a8ea79
16 files changed
+884 -26

No files matched your search

+1
View File
@@ -281,6 +281,7 @@ func (c *Client) newRequest(ctx context.Context, method, path string, ro request
}
if ro.directory != "" {
req.Header.Set("x-opencode-directory", url.PathEscape(ro.directory))
req.Header.Set("x-opencode-directory-encoding", "uri")
}
return req, nil
}
+3
View File
@@ -112,6 +112,9 @@ func TestListSessionsScoped(t *testing.T) {
if got := r.Header.Get("x-opencode-directory"); got != "%2Fhome%2Fme%2Fproj" {
t.Errorf("directory header = %q", got)
}
if got := r.Header.Get("x-opencode-directory-encoding"); got != "uri" {
t.Errorf("directory encoding = %q", got)
}
if got := r.URL.Query().Get("limit"); got != "50" {
t.Errorf("limit = %q", got)
}
+39
View File
@@ -0,0 +1,39 @@
package ocapi
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
)
// RawFile reads a file the server can see, for previews. content is the
// server's Content-Type; the body is bounded to 32 MiB. Paths outside the
// session's workspace are allowed: the tool call already read the file, and
// the preview shows the same one.
func (c *Client) RawFile(ctx context.Context, path, directory string) ([]byte, string, error) {
q := url.Values{}
q.Set("path", path)
q.Set("allowOutsideWorkspace", "true")
req, err := c.newRequest(ctx, http.MethodGet, "/api/fs/raw", requestOptions{directory: directory, query: q}, nil)
if err != nil {
return nil, "", err
}
hc := *c.http
hc.Timeout = 0
res, err := hc.Do(req)
if err != nil {
return nil, "", err
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
raw, _ := io.ReadAll(io.LimitReader(res.Body, 1<<14))
return nil, "", &APIError{Status: res.StatusCode, Raw: string(raw), Message: fmt.Sprintf("HTTP %d", res.StatusCode)}
}
data, err := io.ReadAll(io.LimitReader(res.Body, 32<<20))
if err != nil {
return nil, "", err
}
return data, res.Header.Get("Content-Type"), nil
}
+62
View File
@@ -0,0 +1,62 @@
package ocapi
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestRawFile(t *testing.T) {
var gotPath, gotDir string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/fs/raw" {
t.Errorf("path = %q", r.URL.Path)
}
if got := r.Header.Get("x-opencode-directory-encoding"); got != "uri" {
t.Errorf("directory encoding = %q", got)
}
gotPath = r.URL.Query().Get("path")
if got := r.URL.Query().Get("allowOutsideWorkspace"); got != "true" {
t.Errorf("allowOutsideWorkspace = %q", got)
}
gotDir, _ = url.PathUnescape(r.Header.Get("x-opencode-directory"))
w.Header().Set("Content-Type", "image/png")
fmt.Fprint(w, "PNGDATA")
}))
defer srv.Close()
c, err := New(srv.URL, Options{})
if err != nil {
t.Fatal(err)
}
data, mime, err := c.RawFile(context.Background(), "/work/a.png", "/work")
if err != nil {
t.Fatal(err)
}
if string(data) != "PNGDATA" {
t.Errorf("data = %q", data)
}
if mime != "image/png" {
t.Errorf("mime = %q", mime)
}
if gotPath != "/work/a.png" {
t.Errorf("query path = %q", gotPath)
}
if gotDir != "/work" {
t.Errorf("directory = %q", gotDir)
}
}
func TestRawFileError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "nope", http.StatusNotFound)
}))
defer srv.Close()
c, _ := New(srv.URL, Options{})
if _, _, err := c.RawFile(context.Background(), "/work/missing.png", ""); err == nil {
t.Fatal("expected an error")
}
}
+4
View File
@@ -177,6 +177,10 @@ type ModelInfo struct {
ProviderID string `json:"providerID"`
Name string `json:"name"`
Variant string `json:"variant"`
Limit struct {
Context int `json:"context"`
Output int `json:"output"`
} `json:"limit"`
}
// Models lists the models the server offers for a directory.
+17 -11
View File
@@ -61,6 +61,7 @@ type ModelRef struct {
// TokenUsage is a session's or message's token counters.
type TokenUsage struct {
Total int64 `json:"total,omitempty"`
Input int64 `json:"input"`
Output int64 `json:"output"`
Reasoning int64 `json:"reasoning"`
@@ -146,12 +147,16 @@ type MessageTime struct {
// ContentPart is a block of an assistant message.
type ContentPart struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
State *ToolState `json:"state,omitempty"`
Time *PartTime `json:"time,omitempty"`
Type string `json:"type"`
Text string `json:"text,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
State *ToolState `json:"state,omitempty"`
Time *PartTime `json:"time,omitempty"`
MIME string `json:"mime,omitempty"`
URL string `json:"url,omitempty"`
URI string `json:"uri,omitempty"`
Filename string `json:"filename,omitempty"`
}
// PartTime is when a reasoning/text block was written.
@@ -162,11 +167,12 @@ type PartTime struct {
// ToolState is a tool call's state machine.
type ToolState struct {
Status string `json:"status"`
Input map[string]any `json:"input,omitempty"`
Content []ContentPart `json:"content,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Error json.RawMessage `json:"error,omitempty"`
Status string `json:"status"`
Input map[string]any `json:"input,omitempty"`
Content []ContentPart `json:"content,omitempty"`
Attachments []ContentPart `json:"attachments,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Error json.RawMessage `json:"error,omitempty"`
}
// PromptFile is a file riding with a prompt: a data URL or a URI the