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:
1 parent
dc3730eba7
commit
2fb4a8ea79
16 files changed
+884
-26
No files matched your search
@@ -53,6 +53,9 @@ M0 完成,并继续做到了 M3。所有验证都连的是本机正在运行
|
|||||||
| 图片内联预览 | 发送的图片附件保留本地副本,消息气泡内直接显示缩略图(200×150 圆角),不受后续重载影响 |
|
| 图片内联预览 | 发送的图片附件保留本地副本,消息气泡内直接显示缩略图(200×150 圆角),不受后续重载影响 |
|
||||||
| 撤回 | 用户消息菜单「撤回此消息及之后」:二次确认后 stage+commit 服务端 revert,删除该消息及之后内容 |
|
| 撤回 | 用户消息菜单「撤回此消息及之后」:二次确认后 stage+commit 服务端 revert,删除该消息及之后内容 |
|
||||||
| 应用图标 | 使用 OpenChamber 官方图标(`assets/icon.png`,取自 `packages/mobile/assets/icon-only.png`) |
|
| 应用图标 | 使用 OpenChamber 官方图标(`assets/icon.png`,取自 `packages/mobile/assets/icon-only.png`) |
|
||||||
|
| 上下文占用 | 输入区右侧环形进度 + 百分比(绿/橙/红阈值 75%/90%,与官方一致);点击打开详情 Sheet:模型、已用 tokens、上下文上限、输出预留、占用百分比、本轮费用;阈值按 `上限 - min(输出, 32K)` 计算 |
|
||||||
|
| 工具图片预览 | 工具调用涉及图片时(read 的 `state.attachments` data URL、输入 `filePath/path`、输出文本中的绝对图片路径),在工具行下显示图片链接;点击后从 data URL 解码或经 `/api/fs/raw`(带 `allowOutsideWorkspace=true`,工作区外的 /tmp 截图也能看)取回,全屏 Sheet 预览并缓存 |
|
||||||
|
| 目录请求头 | `x-opencode-directory` 附带 `x-opencode-directory-encoding: uri`,修复 scoped 请求(如 `/api/fs/raw`)被当成未解码路径的问题 |
|
||||||
| Agent 选择 | 只列 `mode=primary` 的 Agent,子智能体(General/Explore)不再出现 |
|
| Agent 选择 | 只列 `mode=primary` 的 Agent,子智能体(General/Explore)不再出现 |
|
||||||
| 模型/Agent | 聊天页顶栏打开选择 Sheet(可搜索),切换后同步会话列表 |
|
| 模型/Agent | 聊天页顶栏打开选择 Sheet(可搜索),切换后同步会话列表 |
|
||||||
| 新建会话 | 会话页「新建」:标题 + 工作目录(服务端默认目录与已加载会话的目录),创建后直接进入聊天 |
|
| 新建会话 | 会话页「新建」:标题 + 工作目录(服务端默认目录与已加载会话的目录),创建后直接进入聊天 |
|
||||||
|
|||||||
@@ -3,13 +3,14 @@
|
|||||||
A lightweight OpenChamber client for phones, written in Go with
|
A lightweight OpenChamber client for phones, written in Go with
|
||||||
[Asuka](https://asuka.egoist.dev/)'s native widgets. Android first.
|
[Asuka](https://asuka.egoist.dev/)'s native widgets. Android first.
|
||||||
|
|
||||||
Status: 0.2.1 — multiple saved servers with a connect list (add, edit,
|
Status: 0.2.2 — multiple saved servers with a connect list (add, edit,
|
||||||
delete, switch; password prompt; HTTP or SOCKS5 proxy and self-signed
|
delete, switch; password prompt; HTTP or SOCKS5 proxy and self-signed
|
||||||
certificates per server), session search, live session list, chat with
|
certificates per server), session search, live session list, chat with
|
||||||
streaming Markdown, image and file attachments with inline previews,
|
streaming Markdown, image and file attachments with inline previews, a
|
||||||
permissions, model/agent switching, turn and prompt actions (copy, fork,
|
context usage ring with a detail sheet, previews of images named by tool
|
||||||
revert, read aloud), new sessions, archive/delete/rename, the OpenChamber
|
calls, permissions, model/agent switching, turn and prompt actions (copy,
|
||||||
icon, and a UI-independent API client.
|
fork, revert, read aloud), new sessions, archive/delete/rename, the
|
||||||
|
OpenChamber icon, and a UI-independent API client.
|
||||||
|
|
||||||
## What is here
|
## What is here
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "OpenChamber",
|
"name": "OpenChamber",
|
||||||
"id": "com.openchamber.native",
|
"id": "com.openchamber.native",
|
||||||
"version": "0.2.1",
|
"version": "0.2.2",
|
||||||
"build": 15,
|
"build": 16,
|
||||||
"orientations": [
|
"orientations": [
|
||||||
"portrait",
|
"portrait",
|
||||||
"landscape"
|
"landscape"
|
||||||
|
|||||||
@@ -94,6 +94,20 @@ func main() {
|
|||||||
printJSON(page)
|
printJSON(page)
|
||||||
fmt.Fprintf(os.Stderr, "%d messages, next=%q\n", len(page.Data), page.Cursor.Next)
|
fmt.Fprintf(os.Stderr, "%d messages, next=%q\n", len(page.Data), page.Cursor.Next)
|
||||||
|
|
||||||
|
case "raw":
|
||||||
|
if flag.NArg() < 2 {
|
||||||
|
fail(fmt.Errorf("raw needs an absolute file path, and optionally a directory"))
|
||||||
|
}
|
||||||
|
dir := ""
|
||||||
|
if flag.NArg() > 2 {
|
||||||
|
dir = flag.Arg(2)
|
||||||
|
}
|
||||||
|
data, mime, err := client.RawFile(ctx, flag.Arg(1), dir)
|
||||||
|
if err != nil {
|
||||||
|
fail(err)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stderr, "%d bytes, %s\n", len(data), mime)
|
||||||
|
|
||||||
case "prompt":
|
case "prompt":
|
||||||
if flag.NArg() < 3 {
|
if flag.NArg() < 3 {
|
||||||
fail(fmt.Errorf("prompt needs a session id and text"))
|
fail(fmt.Errorf("prompt needs a session id and text"))
|
||||||
@@ -193,6 +207,7 @@ commands:
|
|||||||
status probe /auth/session
|
status probe /auth/session
|
||||||
sessions list sessions (newest first)
|
sessions list sessions (newest first)
|
||||||
messages <session-id> latest messages of a session
|
messages <session-id> latest messages of a session
|
||||||
|
raw <path> [directory] fetch a file the server can read
|
||||||
prompt <session-id> <text> send a message
|
prompt <session-id> <text> send a message
|
||||||
interrupt <session-id> stop the running turn
|
interrupt <session-id> stop the running turn
|
||||||
events watch the event stream
|
events watch the event stream
|
||||||
|
|||||||
+32
-2
@@ -60,7 +60,9 @@ type chat struct {
|
|||||||
permList *signal.Signal[[]ocapi.PermissionRequest]
|
permList *signal.Signal[[]ocapi.PermissionRequest]
|
||||||
hasPerms *signal.Signal[bool]
|
hasPerms *signal.Signal[bool]
|
||||||
|
|
||||||
modelNames *signal.Signal[map[string]string]
|
modelNames *signal.Signal[map[string]string]
|
||||||
|
modelLimits *signal.Signal[map[string]modelLimit]
|
||||||
|
usageOpen *signal.Signal[bool]
|
||||||
|
|
||||||
footerOpen *signal.Signal[bool]
|
footerOpen *signal.Signal[bool]
|
||||||
footerID *signal.Signal[string]
|
footerID *signal.Signal[string]
|
||||||
@@ -73,6 +75,13 @@ type chat struct {
|
|||||||
lastScroll float32
|
lastScroll float32
|
||||||
imagePaths map[string][]string
|
imagePaths map[string][]string
|
||||||
|
|
||||||
|
previewOpen *signal.Signal[bool]
|
||||||
|
previewLoading *signal.Signal[bool]
|
||||||
|
previewPath *signal.Signal[string]
|
||||||
|
previewTitle *signal.Signal[string]
|
||||||
|
previewCache map[string]string
|
||||||
|
previewSeq int
|
||||||
|
|
||||||
pickerOpen *signal.Signal[bool]
|
pickerOpen *signal.Signal[bool]
|
||||||
pickerKind string
|
pickerKind string
|
||||||
pickerItems *signal.Signal[[]pickerItem]
|
pickerItems *signal.Signal[[]pickerItem]
|
||||||
@@ -113,6 +122,8 @@ func (c *controller) openChat(s *ocapi.Session, nav *ui.Navigator) *chat {
|
|||||||
permList: signal.NewFunc([]ocapi.PermissionRequest{}, nil),
|
permList: signal.NewFunc([]ocapi.PermissionRequest{}, nil),
|
||||||
hasPerms: signal.New(false),
|
hasPerms: signal.New(false),
|
||||||
modelNames: signal.NewFunc(map[string]string{}, nil),
|
modelNames: signal.NewFunc(map[string]string{}, nil),
|
||||||
|
modelLimits: signal.NewFunc(map[string]modelLimit{}, nil),
|
||||||
|
usageOpen: signal.New(false),
|
||||||
footerOpen: signal.New(false),
|
footerOpen: signal.New(false),
|
||||||
footerID: signal.New(""),
|
footerID: signal.New(""),
|
||||||
userOpen: signal.New(false),
|
userOpen: signal.New(false),
|
||||||
@@ -121,6 +132,11 @@ func (c *controller) openChat(s *ocapi.Session, nav *ui.Navigator) *chat {
|
|||||||
attaching: signal.New(false),
|
attaching: signal.New(false),
|
||||||
jumpToLatest: signal.New(false),
|
jumpToLatest: signal.New(false),
|
||||||
imagePaths: map[string][]string{},
|
imagePaths: map[string][]string{},
|
||||||
|
previewOpen: signal.New(false),
|
||||||
|
previewLoading: signal.New(false),
|
||||||
|
previewPath: signal.New(""),
|
||||||
|
previewTitle: signal.New(""),
|
||||||
|
previewCache: map[string]string{},
|
||||||
pickerOpen: signal.New(false),
|
pickerOpen: signal.New(false),
|
||||||
pickerItems: signal.NewFunc([]pickerItem{}, nil),
|
pickerItems: signal.NewFunc([]pickerItem{}, nil),
|
||||||
pickerFilter: signal.New(""),
|
pickerFilter: signal.New(""),
|
||||||
@@ -150,6 +166,7 @@ func (ch *chat) loadCatalog() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
names := make(map[string]string, len(models))
|
names := make(map[string]string, len(models))
|
||||||
|
limits := make(map[string]modelLimit, len(models))
|
||||||
for _, m := range models {
|
for _, m := range models {
|
||||||
name := m.Name
|
name := m.Name
|
||||||
if name == "" {
|
if name == "" {
|
||||||
@@ -157,8 +174,14 @@ func (ch *chat) loadCatalog() {
|
|||||||
}
|
}
|
||||||
names[m.ProviderID+"/"+m.ID] = name
|
names[m.ProviderID+"/"+m.ID] = name
|
||||||
names[m.ID] = name
|
names[m.ID] = name
|
||||||
|
lim := modelLimit{Context: m.Limit.Context, Output: m.Limit.Output}
|
||||||
|
limits[m.ProviderID+"/"+m.ID] = lim
|
||||||
|
limits[m.ID] = lim
|
||||||
}
|
}
|
||||||
ui.Post(func() { ch.modelNames.Set(names) })
|
ui.Post(func() {
|
||||||
|
ch.modelNames.Set(names)
|
||||||
|
ch.modelLimits.Set(limits)
|
||||||
|
})
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -824,6 +847,8 @@ func (ch *chat) screen() ui.Node {
|
|||||||
),
|
),
|
||||||
ch.footerSheet(),
|
ch.footerSheet(),
|
||||||
ch.userSheet(),
|
ch.userSheet(),
|
||||||
|
ch.usageSheet(),
|
||||||
|
ch.previewSheet(),
|
||||||
ui.Sheet(ui.SheetProps{
|
ui.Sheet(ui.SheetProps{
|
||||||
Open: ch.pickerOpen,
|
Open: ch.pickerOpen,
|
||||||
Detents: ui.DetentLarge,
|
Detents: ui.DetentLarge,
|
||||||
@@ -1115,6 +1140,7 @@ func (ch *chat) composer() ui.Node {
|
|||||||
Multiline: true,
|
Multiline: true,
|
||||||
Style: ui.Style{Grow: 1, Lines: 5},
|
Style: ui.Style{Grow: 1, Lines: 5},
|
||||||
}),
|
}),
|
||||||
|
ch.usageButton(),
|
||||||
ui.Show(ch.running,
|
ui.Show(ch.running,
|
||||||
func() ui.Node {
|
func() ui.Node {
|
||||||
return ui.Button(ui.ButtonProps{
|
return ui.Button(ui.ButtonProps{
|
||||||
@@ -1481,6 +1507,10 @@ func (ch *chat) toolRow(cur chatItem) ui.Node {
|
|||||||
}},
|
}},
|
||||||
ui.Text(headline, ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel, Lines: 2}),
|
ui.Text(headline, ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel, Lines: 2}),
|
||||||
),
|
),
|
||||||
|
ui.For(ch.toolImageAccessor(part), func(ref imageRef) string { return ref.Source },
|
||||||
|
func(ref signal.Accessor[imageRef], _ signal.Accessor[int]) ui.Node {
|
||||||
|
return ui.Dynamic(func() ui.Node { return ch.imageLinkRow(ref.Get()) })
|
||||||
|
}),
|
||||||
ui.Show(open, func() ui.Node {
|
ui.Show(open, func() ui.Node {
|
||||||
return ui.View(ui.ViewProps{Style: ui.Style{Padding: ui.Symmetric(8, 10), CornerRadius: 8,
|
return ui.View(ui.ViewProps{Style: ui.Style{Padding: ui.Symmetric(8, 10), CornerRadius: 8,
|
||||||
Background: colors.SecondaryBackground, Gap: 8}},
|
Background: colors.SecondaryBackground, Gap: 8}},
|
||||||
|
|||||||
@@ -65,11 +65,16 @@ func newChatServer(t *testing.T) *chatServer {
|
|||||||
fmt.Fprint(w, `{"data":[
|
fmt.Fprint(w, `{"data":[
|
||||||
{"id":"msg_1","type":"user","text":"What is this?","time":{"created":10}},
|
{"id":"msg_1","type":"user","text":"What is this?","time":{"created":10}},
|
||||||
{"id":"msg_2","type":"assistant","agent":"build","model":{"id":"gpt-4","providerID":"openai"},
|
{"id":"msg_2","type":"assistant","agent":"build","model":{"id":"gpt-4","providerID":"openai"},
|
||||||
"time":{"created":11,"completed":12000},
|
"time":{"created":11,"completed":12000},"cost":0.02,
|
||||||
|
"tokens":{"input":100500,"output":0,"reasoning":0,"cache":{"read":0,"write":0}},
|
||||||
"content":[{"type":"text","text":"**Hello** from the agent"},
|
"content":[{"type":"text","text":"**Hello** from the agent"},
|
||||||
{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed",
|
{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed",
|
||||||
"input":{"command":"echo hi"},"content":[],
|
"input":{"command":"echo hi"},"content":[],
|
||||||
"metadata":{"openchamberContentShell":{}}}}]}
|
"metadata":{"openchamberContentShell":{}}}},
|
||||||
|
{"type":"tool","id":"call_2","name":"read","state":{"status":"completed",
|
||||||
|
"input":{"filePath":"/home/me/project/shot.png"},"content":[],
|
||||||
|
"attachments":[{"type":"file","mime":"image/png","filename":"shot.png",
|
||||||
|
"url":"data:image/png;base64,iVBORw0KGgo="}]}}]}
|
||||||
],"cursor":{}}`)
|
],"cursor":{}}`)
|
||||||
})
|
})
|
||||||
mux.HandleFunc("/api/session/ses_new/message", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/api/session/ses_new/message", func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -91,8 +96,8 @@ func newChatServer(t *testing.T) *chatServer {
|
|||||||
})
|
})
|
||||||
mux.HandleFunc("/api/model", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/api/model", func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprint(w, `{"data":[
|
fmt.Fprint(w, `{"data":[
|
||||||
{"id":"gpt-4","providerID":"openai","name":"GPT-4"},
|
{"id":"gpt-4","providerID":"openai","name":"GPT-4","limit":{"context":200000,"output":64000}},
|
||||||
{"id":"claude","providerID":"anthropic","name":"Claude"}]}`)
|
{"id":"claude","providerID":"anthropic","name":"Claude","limit":{"context":200000,"output":32000}}]}`)
|
||||||
})
|
})
|
||||||
mux.HandleFunc("/api/agent", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/api/agent", func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprint(w, `{"data":[
|
fmt.Fprint(w, `{"data":[
|
||||||
@@ -127,10 +132,15 @@ func newChatServer(t *testing.T) *chatServer {
|
|||||||
})
|
})
|
||||||
mux.HandleFunc("/api/session/ses_1/message/msg_2", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/api/session/ses_1/message/msg_2", func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprint(w, `{"id":"msg_2","type":"assistant","agent":"build","model":{"id":"gpt-4","providerID":"openai"},
|
fmt.Fprint(w, `{"id":"msg_2","type":"assistant","agent":"build","model":{"id":"gpt-4","providerID":"openai"},
|
||||||
"time":{"created":11,"completed":12000},
|
"time":{"created":11,"completed":12000},"cost":0.02,
|
||||||
|
"tokens":{"input":100500,"output":0,"reasoning":0,"cache":{"read":0,"write":0}},
|
||||||
"content":[{"type":"text","text":"**Hello** from the agent"},
|
"content":[{"type":"text","text":"**Hello** from the agent"},
|
||||||
{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed",
|
{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed",
|
||||||
"input":{"command":"echo hi"},"content":[{"type":"text","text":"tool output here"}]}}]}`)
|
"input":{"command":"echo hi"},"content":[{"type":"text","text":"tool output here"}]}},
|
||||||
|
{"type":"tool","id":"call_2","name":"read","state":{"status":"completed",
|
||||||
|
"input":{"filePath":"/home/me/project/shot.png"},"content":[],
|
||||||
|
"attachments":[{"type":"file","mime":"image/png","filename":"shot.png",
|
||||||
|
"url":"data:image/png;base64,iVBORw0KGgo="}]}}]}`)
|
||||||
})
|
})
|
||||||
mux.HandleFunc("/api/session/ses_1/revert/stage", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/api/session/ses_1/revert/stage", func(w http.ResponseWriter, r *http.Request) {
|
||||||
var body map[string]any
|
var body map[string]any
|
||||||
@@ -416,3 +426,32 @@ func TestRevertFromUserMessage(t *testing.T) {
|
|||||||
t.Errorf("commit not sent: %v", commit)
|
t.Errorf("commit not sent: %v", commit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextUsageRingAndSheet(t *testing.T) {
|
||||||
|
srv := newChatServer(t)
|
||||||
|
a := uitest.Mount(t, func() ui.Node { return rootWithStore(&memStore{}) }, uitest.Platform(ui.PlatformAndroid))
|
||||||
|
connectViaForm(t, a, srv.URL)
|
||||||
|
a.WaitFor("First session")
|
||||||
|
a.Tap("First session")
|
||||||
|
a.WaitFor("Hello from the agent")
|
||||||
|
|
||||||
|
// 100500 tokens over 200000 - min(64000, 32000).
|
||||||
|
a.WaitFor("59.8%")
|
||||||
|
a.Tap("上下文占用")
|
||||||
|
a.WaitFor("上下文上限")
|
||||||
|
a.WaitFor("200.0K")
|
||||||
|
a.WaitFor("64.0K")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToolImagePreviewSheet(t *testing.T) {
|
||||||
|
srv := newChatServer(t)
|
||||||
|
a := uitest.Mount(t, func() ui.Node { return rootWithStore(&memStore{}) }, uitest.Platform(ui.PlatformAndroid))
|
||||||
|
connectViaForm(t, a, srv.URL)
|
||||||
|
a.WaitFor("First session")
|
||||||
|
a.Tap("First session")
|
||||||
|
a.WaitFor("Hello from the agent")
|
||||||
|
|
||||||
|
a.WaitFor("预览 shot.png")
|
||||||
|
a.Tap("预览 shot.png")
|
||||||
|
a.WaitFor("图片预览")
|
||||||
|
}
|
||||||
@@ -0,0 +1,198 @@
|
|||||||
|
package appui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/egoist/asuka/colors"
|
||||||
|
"github.com/egoist/asuka/fonts"
|
||||||
|
"github.com/egoist/asuka/signal"
|
||||||
|
"github.com/egoist/asuka/ui"
|
||||||
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// modelLimit is a model's context and output window.
|
||||||
|
type modelLimit struct {
|
||||||
|
Context int
|
||||||
|
Output int
|
||||||
|
}
|
||||||
|
|
||||||
|
// usageReading is the context window of the last assistant message that
|
||||||
|
// reported tokens, as the first-party client shows it. The output
|
||||||
|
// reservation is capped at 32k tokens, matching its calculation.
|
||||||
|
type usageReading struct {
|
||||||
|
Used int
|
||||||
|
Limit int
|
||||||
|
Output int
|
||||||
|
Pct float64
|
||||||
|
Known bool
|
||||||
|
Cost float64
|
||||||
|
Model string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ch *chat) usageReading(msgs []ocapi.Message) usageReading {
|
||||||
|
var last *ocapi.Message
|
||||||
|
for i := len(msgs) - 1; i >= 0; i-- {
|
||||||
|
if msgs[i].Type == "assistant" && msgs[i].Tokens != nil {
|
||||||
|
last = &msgs[i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if last == nil {
|
||||||
|
return usageReading{}
|
||||||
|
}
|
||||||
|
// A message the event stream added has no model yet; the session's model
|
||||||
|
// is the latest one any message reports.
|
||||||
|
model := last.Model
|
||||||
|
for i := len(msgs) - 1; i >= 0 && model == nil; i-- {
|
||||||
|
if msgs[i].Type == "assistant" && msgs[i].Model != nil {
|
||||||
|
model = msgs[i].Model
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if model == nil {
|
||||||
|
return usageReading{}
|
||||||
|
}
|
||||||
|
var r usageReading
|
||||||
|
t := last.Tokens
|
||||||
|
if t.Total > 0 {
|
||||||
|
r.Used = int(t.Total)
|
||||||
|
} else {
|
||||||
|
r.Used = int(t.Input + t.Output + t.Reasoning + t.Cache.Read + t.Cache.Write)
|
||||||
|
}
|
||||||
|
lim := ch.modelLimit(model)
|
||||||
|
r.Limit, r.Output = lim.Context, lim.Output
|
||||||
|
r.Model = ch.modelName(model)
|
||||||
|
// The turn's cost is every step after its prompt.
|
||||||
|
for i := len(msgs) - 1; i >= 0; i-- {
|
||||||
|
if msgs[i].Type == "user" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if msgs[i].Type == "assistant" {
|
||||||
|
r.Cost += msgs[i].Cost
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r.Limit <= 0 || r.Used <= 0 {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
reserve := r.Output
|
||||||
|
if reserve > 32000 {
|
||||||
|
reserve = 32000
|
||||||
|
}
|
||||||
|
threshold := r.Limit - reserve
|
||||||
|
if threshold < 1 {
|
||||||
|
threshold = 1
|
||||||
|
}
|
||||||
|
r.Pct = math.Min(100, float64(r.Used)/float64(threshold)*100)
|
||||||
|
r.Known = true
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ch *chat) contextUsage() usageReading {
|
||||||
|
return ch.usageReading(ch.msgs.Peek())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ch *chat) modelLimit(m *ocapi.ModelRef) modelLimit {
|
||||||
|
if m == nil {
|
||||||
|
return modelLimit{}
|
||||||
|
}
|
||||||
|
limits := ch.modelLimits.Get()
|
||||||
|
if l, ok := limits[m.ProviderID+"/"+m.ID]; ok {
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
return limits[m.ID]
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatTokens(n int) string {
|
||||||
|
switch {
|
||||||
|
case n >= 1_000_000:
|
||||||
|
return fmt.Sprintf("%.1fM", float64(n)/1_000_000)
|
||||||
|
case n >= 1_000:
|
||||||
|
return fmt.Sprintf("%.1fK", float64(n)/1_000)
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%d", n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// usageTone follows the first-party thresholds: 75% warns, 90% errors.
|
||||||
|
func usageTone(pct float64) ui.Color {
|
||||||
|
switch {
|
||||||
|
case pct >= 90:
|
||||||
|
return colors.Red
|
||||||
|
case pct >= 75:
|
||||||
|
return colors.Orange
|
||||||
|
default:
|
||||||
|
return colors.Green
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// usageButton is the composer's context ring and percentage.
|
||||||
|
func (ch *chat) usageButton() ui.Node {
|
||||||
|
return ui.Dynamic(func() ui.Node {
|
||||||
|
r := ch.usageReading(ch.msgs.Get())
|
||||||
|
if !r.Known {
|
||||||
|
return ui.View(ui.ViewProps{})
|
||||||
|
}
|
||||||
|
progress := signal.Func(func() float64 { return r.Pct / 100 })
|
||||||
|
return ui.View(ui.ViewProps{
|
||||||
|
OnPress: func() { ch.usageOpen.Set(true) },
|
||||||
|
Label: ui.Val("上下文占用"),
|
||||||
|
Hint: ui.Val("查看上下文详情"),
|
||||||
|
Style: ui.Style{Padding: ui.All(4), Align: ui.AlignCenter},
|
||||||
|
}, ui.Row(ui.Style{Gap: 4, Align: ui.AlignCenter},
|
||||||
|
ui.ProgressRing(progress, ui.Style{Width: ui.Pt(18), Height: ui.Pt(18), Color: usageTone(r.Pct)}),
|
||||||
|
ui.StaticText(fmt.Sprintf("%.1f%%", math.Min(r.Pct, 999)),
|
||||||
|
ui.Style{Font: fonts.Caption, Color: colors.SecondaryLabel}),
|
||||||
|
))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ch *chat) usageSheet() ui.Node {
|
||||||
|
return ui.Sheet(ui.SheetProps{
|
||||||
|
Open: ch.usageOpen,
|
||||||
|
Detents: ui.DetentMedium,
|
||||||
|
Grabber: true,
|
||||||
|
OnDismiss: func() { ch.usageOpen.Set(false) },
|
||||||
|
Content: func() ui.Node {
|
||||||
|
return ui.Dynamic(func() ui.Node {
|
||||||
|
r := ch.usageReading(ch.msgs.Get())
|
||||||
|
rows := []ui.Node{usageRow("模型", r.Model)}
|
||||||
|
if r.Known {
|
||||||
|
rows = append(rows,
|
||||||
|
usageRow("已用 tokens", formatTokens(r.Used)),
|
||||||
|
usageRow("上下文上限", formatTokens(r.Limit)),
|
||||||
|
usageRow("输出预留", formatTokens(r.Output)),
|
||||||
|
usageRow("占用", fmt.Sprintf("%.1f%%", math.Min(r.Pct, 999))),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
rows = append(rows,
|
||||||
|
usageRow("已用 tokens", formatTokens(r.Used)),
|
||||||
|
usageRow("上下文上限", "未知"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if r.Cost > 0 {
|
||||||
|
rows = append(rows, usageRow("本轮费用", formatCost(r.Cost)))
|
||||||
|
}
|
||||||
|
return ui.SafeArea(ui.Style{Padding: ui.All(20), Gap: 10},
|
||||||
|
ui.StaticText("上下文占用", ui.Style{Font: fonts.Title2.Weighted(fonts.Bold)}),
|
||||||
|
ui.Column(ui.Style{Gap: 10}, rows...),
|
||||||
|
ui.StaticText("输出预留按最多 32K tokens 计算,与官方客户端一致。",
|
||||||
|
ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatCost(v float64) string {
|
||||||
|
if v < 0.01 {
|
||||||
|
return fmt.Sprintf("$%.4f", v)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("$%.2f", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func usageRow(label, value string) ui.Node {
|
||||||
|
return ui.Row(ui.Style{Gap: 12, Align: ui.AlignCenter},
|
||||||
|
ui.StaticText(label, ui.Style{Font: fonts.Body, Color: colors.SecondaryLabel, Grow: 1}),
|
||||||
|
ui.StaticText(value, ui.Style{Font: fonts.Body}),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package appui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/egoist/asuka/signal"
|
||||||
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
func usageTestChat(limits map[string]modelLimit) *chat {
|
||||||
|
return &chat{
|
||||||
|
modelLimits: signal.NewFunc(limits, nil),
|
||||||
|
modelNames: signal.NewFunc(map[string]string{}, nil),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUsageReadingCapsOutputReserve(t *testing.T) {
|
||||||
|
ch := usageTestChat(map[string]modelLimit{
|
||||||
|
"p/m": {Context: 200_000, Output: 64_000},
|
||||||
|
})
|
||||||
|
msgs := []ocapi.Message{{
|
||||||
|
Type: "assistant",
|
||||||
|
Model: &ocapi.ModelRef{ID: "m", ProviderID: "p"},
|
||||||
|
Tokens: &ocapi.TokenUsage{Total: 50_000},
|
||||||
|
Cost: 0.02,
|
||||||
|
}}
|
||||||
|
r := ch.usageReading(msgs)
|
||||||
|
if !r.Known {
|
||||||
|
t.Fatal("usage should be known")
|
||||||
|
}
|
||||||
|
if r.Limit != 200_000 || r.Output != 64_000 || r.Used != 50_000 {
|
||||||
|
t.Errorf("reading = %+v", r)
|
||||||
|
}
|
||||||
|
// 200k - min(64k, 32k) = 168k; 50k / 168k = 29.76%
|
||||||
|
want := 50_000.0 / 168_000.0 * 100
|
||||||
|
if math.Abs(r.Pct-want) > 1e-9 {
|
||||||
|
t.Errorf("pct = %v, want %v", r.Pct, want)
|
||||||
|
}
|
||||||
|
if r.Cost != 0.02 {
|
||||||
|
t.Errorf("cost = %v", r.Cost)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUsageReadingFallsBackToFieldSum(t *testing.T) {
|
||||||
|
ch := usageTestChat(map[string]modelLimit{
|
||||||
|
"p/m": {Context: 100_000, Output: 8_000},
|
||||||
|
})
|
||||||
|
tokens := &ocapi.TokenUsage{Input: 10, Output: 20, Reasoning: 30}
|
||||||
|
tokens.Cache.Read = 40
|
||||||
|
tokens.Cache.Write = 50
|
||||||
|
r := ch.usageReading([]ocapi.Message{{
|
||||||
|
Type: "assistant",
|
||||||
|
Model: &ocapi.ModelRef{ID: "m", ProviderID: "p"},
|
||||||
|
Tokens: tokens,
|
||||||
|
}})
|
||||||
|
if r.Used != 150 {
|
||||||
|
t.Errorf("used = %d, want 150", r.Used)
|
||||||
|
}
|
||||||
|
if want := 150.0 / 92_000.0 * 100; math.Abs(r.Pct-want) > 1e-9 {
|
||||||
|
t.Errorf("pct = %v, want %v", r.Pct, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUsageReadingUnknownWithoutLimit(t *testing.T) {
|
||||||
|
ch := usageTestChat(nil)
|
||||||
|
r := ch.usageReading([]ocapi.Message{{
|
||||||
|
Type: "assistant",
|
||||||
|
Model: &ocapi.ModelRef{ID: "m", ProviderID: "p"},
|
||||||
|
Tokens: &ocapi.TokenUsage{Total: 500},
|
||||||
|
}})
|
||||||
|
if r.Known || r.Pct != 0 {
|
||||||
|
t.Errorf("reading = %+v, want unknown", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUsageReadingBorrowsModelFromEarlierMessage(t *testing.T) {
|
||||||
|
ch := usageTestChat(map[string]modelLimit{
|
||||||
|
"p/m": {Context: 100_000, Output: 8_000},
|
||||||
|
})
|
||||||
|
msgs := []ocapi.Message{
|
||||||
|
{Type: "assistant", Model: &ocapi.ModelRef{ID: "m", ProviderID: "p"},
|
||||||
|
Tokens: &ocapi.TokenUsage{Total: 1_000}},
|
||||||
|
// Added by the event stream: tokens, but no model yet.
|
||||||
|
{Type: "assistant", Tokens: &ocapi.TokenUsage{Total: 2_000}},
|
||||||
|
}
|
||||||
|
r := ch.usageReading(msgs)
|
||||||
|
if !r.Known || r.Used != 2_000 || r.Limit != 100_000 {
|
||||||
|
t.Errorf("reading = %+v", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,264 @@
|
|||||||
|
package appui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/egoist/asuka/colors"
|
||||||
|
"github.com/egoist/asuka/fonts"
|
||||||
|
"github.com/egoist/asuka/sdk/store"
|
||||||
|
"github.com/egoist/asuka/signal"
|
||||||
|
"github.com/egoist/asuka/ui"
|
||||||
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
var imagePathPattern = regexp.MustCompile(`(/[^\s"'` + "`" + `()\[\]{}<>]+\.(?:png|jpg|jpeg|gif|webp|bmp|avif))`)
|
||||||
|
|
||||||
|
var imageExtensions = map[string]bool{
|
||||||
|
".png": true, ".jpg": true, ".jpeg": true, ".gif": true,
|
||||||
|
".webp": true, ".bmp": true, ".avif": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// imageRef is one previewable image a tool call names: the Source is an
|
||||||
|
// absolute path or a data URL, the Label what its row shows.
|
||||||
|
type imageRef struct {
|
||||||
|
Source string
|
||||||
|
Label string
|
||||||
|
}
|
||||||
|
|
||||||
|
func isImagePath(path string) bool {
|
||||||
|
return imageExtensions[strings.ToLower(filepath.Ext(path))]
|
||||||
|
}
|
||||||
|
|
||||||
|
// toolImages lists the images a tool call names: its file input, file parts
|
||||||
|
// in its output (data URLs from a read, for example), and paths in its text
|
||||||
|
// output. Relative paths resolve against the session's directory.
|
||||||
|
func (ch *chat) toolImages(p ocapi.ContentPart) []imageRef {
|
||||||
|
seen := map[string]bool{}
|
||||||
|
out := make([]imageRef, 0, 2)
|
||||||
|
hasLabel := func(label string) bool {
|
||||||
|
for _, ref := range out {
|
||||||
|
if ref.Label == label {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
add := func(source, label string) {
|
||||||
|
source = strings.TrimSpace(source)
|
||||||
|
if strings.HasPrefix(source, "file://") {
|
||||||
|
source = strings.TrimPrefix(source, "file://")
|
||||||
|
}
|
||||||
|
if source == "" || seen[source] {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(source, "data:") {
|
||||||
|
if strings.Contains(source, "://") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(source, "/") {
|
||||||
|
if ch.dir == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
source = filepath.Join(ch.dir, source)
|
||||||
|
}
|
||||||
|
if !isImagePath(source) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if label == "" {
|
||||||
|
label = filepath.Base(source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if label == "" {
|
||||||
|
label = "图片"
|
||||||
|
}
|
||||||
|
seen[source] = true
|
||||||
|
out = append(out, imageRef{Source: source, Label: label})
|
||||||
|
}
|
||||||
|
addPath := func(path string) {
|
||||||
|
path = strings.TrimSpace(path)
|
||||||
|
if strings.HasPrefix(path, "file://") {
|
||||||
|
path = strings.TrimPrefix(path, "file://")
|
||||||
|
} else if strings.Contains(path, "://") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(path, "/") {
|
||||||
|
if ch.dir == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
path = filepath.Join(ch.dir, path)
|
||||||
|
}
|
||||||
|
if !isImagePath(path) || hasLabel(filepath.Base(path)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
add(path, "")
|
||||||
|
}
|
||||||
|
attachments := 0
|
||||||
|
if p.State != nil {
|
||||||
|
// Server-projected tool attachments carry the image itself, so they
|
||||||
|
// come first: a read of an image needs no file request.
|
||||||
|
for _, group := range [][]ocapi.ContentPart{p.State.Attachments, p.State.Content} {
|
||||||
|
for _, c := range group {
|
||||||
|
switch c.Type {
|
||||||
|
case "file":
|
||||||
|
source := c.URL
|
||||||
|
if source == "" {
|
||||||
|
source = c.URI
|
||||||
|
}
|
||||||
|
if source != "" && strings.HasPrefix(c.MIME, "image/") {
|
||||||
|
attachments++
|
||||||
|
add(source, c.Filename)
|
||||||
|
}
|
||||||
|
case "text":
|
||||||
|
for _, m := range imagePathPattern.FindAllString(c.Text, -1) {
|
||||||
|
addPath(m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// The attachment already is the image the call named.
|
||||||
|
if attachments == 0 {
|
||||||
|
for _, key := range []string{"filePath", "path", "file", "file_path", "sourcePath"} {
|
||||||
|
if v, ok := p.State.Input[key].(string); ok {
|
||||||
|
addPath(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ch *chat) toolImageAccessor(part signal.Accessor[ocapi.ContentPart]) signal.Accessor[[]imageRef] {
|
||||||
|
return signal.Func(func() []imageRef { return ch.toolImages(part.Get()) })
|
||||||
|
}
|
||||||
|
|
||||||
|
// previewKey names a cached preview: a hash keeps data URLs out of the map.
|
||||||
|
func previewKey(source string) string {
|
||||||
|
sum := sha1.Sum([]byte(source))
|
||||||
|
return hex.EncodeToString(sum[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ch *chat) previewImage(ref imageRef) {
|
||||||
|
ch.previewSeq++
|
||||||
|
seq := ch.previewSeq
|
||||||
|
ch.previewTitle.Set(ref.Label)
|
||||||
|
ch.previewPath.Set("")
|
||||||
|
ch.previewLoading.Set(true)
|
||||||
|
ch.previewOpen.Set(true)
|
||||||
|
key := previewKey(ref.Source)
|
||||||
|
if local, ok := ch.previewCache[key]; ok {
|
||||||
|
ch.previewPath.Set(local)
|
||||||
|
ch.previewLoading.Set(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
data, err := ch.loadPreview(ref.Source)
|
||||||
|
if err != nil {
|
||||||
|
ui.Post(func() {
|
||||||
|
if seq != ch.previewSeq {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ch.previewLoading.Set(false)
|
||||||
|
ch.previewOpen.Set(false)
|
||||||
|
ui.Snackbar(ui.SnackbarProps{Message: "无法预览:" + friendlyError(err)})
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dir := filepath.Join(store.Dir(), "previews")
|
||||||
|
_ = os.MkdirAll(dir, 0o700)
|
||||||
|
name := strings.Map(func(r rune) rune {
|
||||||
|
if r == '/' || r == '\\' || r == ':' || r < ' ' {
|
||||||
|
return '_'
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}, ref.Label)
|
||||||
|
local := filepath.Join(dir, fmt.Sprintf("%d-%s", time.Now().UnixNano(), name))
|
||||||
|
if err := os.WriteFile(local, data, 0o600); err != nil {
|
||||||
|
ui.Post(func() {
|
||||||
|
if seq != ch.previewSeq {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ch.previewLoading.Set(false)
|
||||||
|
ch.previewOpen.Set(false)
|
||||||
|
ui.Snackbar(ui.SnackbarProps{Message: "无法保存预览"})
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ui.Post(func() {
|
||||||
|
if seq != ch.previewSeq {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ch.previewCache[key] = local
|
||||||
|
ch.previewPath.Set(local)
|
||||||
|
ch.previewLoading.Set(false)
|
||||||
|
})
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadPreview reads a data URL itself or asks the server for the file.
|
||||||
|
func (ch *chat) loadPreview(source string) ([]byte, error) {
|
||||||
|
if strings.HasPrefix(source, "data:") {
|
||||||
|
comma := strings.IndexByte(source, ',')
|
||||||
|
if comma < 0 {
|
||||||
|
return nil, fmt.Errorf("malformed data URL")
|
||||||
|
}
|
||||||
|
meta := source[len("data:"):comma]
|
||||||
|
if !strings.Contains(meta, ";base64") {
|
||||||
|
return nil, fmt.Errorf("unsupported data URL")
|
||||||
|
}
|
||||||
|
return base64.StdEncoding.DecodeString(source[comma+1:])
|
||||||
|
}
|
||||||
|
client := ch.client()
|
||||||
|
if client == nil {
|
||||||
|
return nil, fmt.Errorf("not connected")
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
data, _, err := client.RawFile(ctx, source, ch.dir)
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ch *chat) previewSheet() ui.Node {
|
||||||
|
return ui.Sheet(ui.SheetProps{
|
||||||
|
Open: ch.previewOpen,
|
||||||
|
Detents: ui.DetentLarge,
|
||||||
|
Grabber: true,
|
||||||
|
OnDismiss: func() { ch.previewOpen.Set(false) },
|
||||||
|
Content: func() ui.Node {
|
||||||
|
return ui.View(ui.ViewProps{Label: ui.Val("图片预览"), Style: ui.Style{Grow: 1}},
|
||||||
|
ui.SafeArea(ui.Style{Padding: ui.All(16), Gap: 10},
|
||||||
|
ui.Text(ch.previewTitle, ui.Style{Font: fonts.Headline, Lines: 2}),
|
||||||
|
ui.Show(ch.previewLoading,
|
||||||
|
func() ui.Node { return ui.Activity() },
|
||||||
|
func() ui.Node {
|
||||||
|
return ui.Image(ui.ImageProps{
|
||||||
|
Source: ch.previewPath,
|
||||||
|
Mode: ui.ImageFit,
|
||||||
|
Style: ui.Style{Width: ui.Pct(100), Height: ui.Pt(420), CornerRadius: 10},
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// imageLinkRow is one previewable image under a tool call.
|
||||||
|
func (ch *chat) imageLinkRow(ref imageRef) ui.Node {
|
||||||
|
return ui.View(ui.ViewProps{
|
||||||
|
OnPress: func() { ch.previewImage(ref) },
|
||||||
|
Label: ui.Val("预览 " + ref.Label),
|
||||||
|
Style: ui.Style{Padding: ui.Symmetric(3, 0)},
|
||||||
|
}, ui.Row(ui.Style{Gap: 6, Align: ui.AlignCenter},
|
||||||
|
ui.Symbol("photo", ui.Style{Font: fonts.Caption, Color: colors.Accent}),
|
||||||
|
ui.StaticText(ref.Label, ui.Style{Font: fonts.Caption, Color: colors.Accent, Lines: 1}),
|
||||||
|
))
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package appui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToolImagesFromOutput(t *testing.T) {
|
||||||
|
ch := &chat{dir: "/work"}
|
||||||
|
part := ocapi.ContentPart{
|
||||||
|
Type: "tool",
|
||||||
|
State: &ocapi.ToolState{
|
||||||
|
Input: map[string]any{"path": "shots/a.png"},
|
||||||
|
Content: []ocapi.ContentPart{
|
||||||
|
{Type: "text", Text: "wrote /work/shots/a.png and /work/notes.txt"},
|
||||||
|
{Type: "file", MIME: "image/png", URL: "data:image/png;base64,AAAA", Filename: "b.png"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
refs := ch.toolImages(part)
|
||||||
|
if len(refs) != 2 {
|
||||||
|
t.Fatalf("refs = %+v", refs)
|
||||||
|
}
|
||||||
|
if refs[0].Source != "/work/shots/a.png" || refs[0].Label != "a.png" {
|
||||||
|
t.Errorf("first = %+v", refs[0])
|
||||||
|
}
|
||||||
|
if refs[1].Source != "data:image/png;base64,AAAA" || refs[1].Label != "b.png" {
|
||||||
|
t.Errorf("second = %+v", refs[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToolImagesPrefersReadAttachment(t *testing.T) {
|
||||||
|
ch := &chat{dir: "/work"}
|
||||||
|
part := ocapi.ContentPart{
|
||||||
|
Type: "tool",
|
||||||
|
State: &ocapi.ToolState{
|
||||||
|
Input: map[string]any{"filePath": "/tmp/upload.png"},
|
||||||
|
Attachments: []ocapi.ContentPart{{
|
||||||
|
Type: "file", MIME: "image/png", Filename: "upload.png",
|
||||||
|
URL: "data:image/png;base64,AAAA",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
refs := ch.toolImages(part)
|
||||||
|
if len(refs) != 1 || refs[0].Source != "data:image/png;base64,AAAA" {
|
||||||
|
t.Errorf("refs = %+v", refs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToolImagesUsesURIFileContent(t *testing.T) {
|
||||||
|
ch := &chat{dir: "/work"}
|
||||||
|
part := ocapi.ContentPart{
|
||||||
|
Type: "tool",
|
||||||
|
State: &ocapi.ToolState{
|
||||||
|
Content: []ocapi.ContentPart{{
|
||||||
|
Type: "file", MIME: "image/jpeg", Name: "shot.jpg",
|
||||||
|
URI: "file:///work/shot.jpg",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
refs := ch.toolImages(part)
|
||||||
|
if len(refs) != 1 || refs[0].Source != "/work/shot.jpg" || refs[0].Label != "shot.jpg" {
|
||||||
|
t.Errorf("refs = %+v", refs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToolImagesSkipsNonImages(t *testing.T) {
|
||||||
|
ch := &chat{dir: "/work"}
|
||||||
|
part := ocapi.ContentPart{
|
||||||
|
Type: "tool",
|
||||||
|
State: &ocapi.ToolState{
|
||||||
|
Input: map[string]any{"path": "/work/main.go"},
|
||||||
|
Content: []ocapi.ContentPart{
|
||||||
|
{Type: "text", Text: "read /work/main.go and /work/logo.webp"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
refs := ch.toolImages(part)
|
||||||
|
if len(refs) != 1 || refs[0].Source != "/work/logo.webp" {
|
||||||
|
t.Errorf("refs = %+v", refs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadPreviewDataURL(t *testing.T) {
|
||||||
|
ch := &chat{}
|
||||||
|
data, err := ch.loadPreview("data:image/png;base64,iVBORw0KGgo=")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.HasPrefix(data, []byte("\x89PNG\r\n\x1a\n")) {
|
||||||
|
t.Errorf("data = %x", data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadPreviewRejectsPlainDataURL(t *testing.T) {
|
||||||
|
ch := &chat{}
|
||||||
|
if _, err := ch.loadPreview("data:text/plain,hello"); err == nil {
|
||||||
|
t.Fatal("expected an error for a non-base64 data URL")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -281,6 +281,7 @@ func (c *Client) newRequest(ctx context.Context, method, path string, ro request
|
|||||||
}
|
}
|
||||||
if ro.directory != "" {
|
if ro.directory != "" {
|
||||||
req.Header.Set("x-opencode-directory", url.PathEscape(ro.directory))
|
req.Header.Set("x-opencode-directory", url.PathEscape(ro.directory))
|
||||||
|
req.Header.Set("x-opencode-directory-encoding", "uri")
|
||||||
}
|
}
|
||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,6 +112,9 @@ func TestListSessionsScoped(t *testing.T) {
|
|||||||
if got := r.Header.Get("x-opencode-directory"); got != "%2Fhome%2Fme%2Fproj" {
|
if got := r.Header.Get("x-opencode-directory"); got != "%2Fhome%2Fme%2Fproj" {
|
||||||
t.Errorf("directory header = %q", got)
|
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" {
|
if got := r.URL.Query().Get("limit"); got != "50" {
|
||||||
t.Errorf("limit = %q", got)
|
t.Errorf("limit = %q", got)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -177,6 +177,10 @@ type ModelInfo struct {
|
|||||||
ProviderID string `json:"providerID"`
|
ProviderID string `json:"providerID"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Variant string `json:"variant"`
|
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.
|
// Models lists the models the server offers for a directory.
|
||||||
|
|||||||
+17
-11
@@ -61,6 +61,7 @@ type ModelRef struct {
|
|||||||
|
|
||||||
// TokenUsage is a session's or message's token counters.
|
// TokenUsage is a session's or message's token counters.
|
||||||
type TokenUsage struct {
|
type TokenUsage struct {
|
||||||
|
Total int64 `json:"total,omitempty"`
|
||||||
Input int64 `json:"input"`
|
Input int64 `json:"input"`
|
||||||
Output int64 `json:"output"`
|
Output int64 `json:"output"`
|
||||||
Reasoning int64 `json:"reasoning"`
|
Reasoning int64 `json:"reasoning"`
|
||||||
@@ -146,12 +147,16 @@ type MessageTime struct {
|
|||||||
|
|
||||||
// ContentPart is a block of an assistant message.
|
// ContentPart is a block of an assistant message.
|
||||||
type ContentPart struct {
|
type ContentPart struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Text string `json:"text,omitempty"`
|
Text string `json:"text,omitempty"`
|
||||||
ID string `json:"id,omitempty"`
|
ID string `json:"id,omitempty"`
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
State *ToolState `json:"state,omitempty"`
|
State *ToolState `json:"state,omitempty"`
|
||||||
Time *PartTime `json:"time,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.
|
// PartTime is when a reasoning/text block was written.
|
||||||
@@ -162,11 +167,12 @@ type PartTime struct {
|
|||||||
|
|
||||||
// ToolState is a tool call's state machine.
|
// ToolState is a tool call's state machine.
|
||||||
type ToolState struct {
|
type ToolState struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Input map[string]any `json:"input,omitempty"`
|
Input map[string]any `json:"input,omitempty"`
|
||||||
Content []ContentPart `json:"content,omitempty"`
|
Content []ContentPart `json:"content,omitempty"`
|
||||||
Metadata map[string]any `json:"metadata,omitempty"`
|
Attachments []ContentPart `json:"attachments,omitempty"`
|
||||||
Error json.RawMessage `json:"error,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
|
// PromptFile is a file riding with a prompt: a data URL or a URI the
|
||||||
|
|||||||
Reference in new issue
Block a user