Files
openchamber-android-native/internal/ocapi/files.go
T
blumia 2fb4a8ea79 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).
2026-11-18 02:15:11 +08:00

40 lines
1.1 KiB
Go

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
}