perf: open long sessions without re-reading every tool result
A chat page carried every finished tool result, and the app fetched it twice when opening a chat, with a 20 second deadline. On a slow link a long session (this one was ~530 KB per page) timed out. - ask for shelled pages (x-openchamber-part-shells: v1), which drops finished tool results: ~180 KB for the same page - fetch one message by id when its tool row is opened - keep tool and step updates local from events instead of re-reading the page on every step, and only resync after a real disconnect - 60 second deadline with a retry button Bump version to 0.1.11 (build 12).
This commit is contained in:
1 parent
0e34d82a06
commit
e67490e768
8 files changed
+256
-20
No files matched your search
@@ -45,6 +45,7 @@ M0 完成,并继续做到了 M3。所有验证都连的是本机正在运行
|
||||
| 首屏无导航栏 | 未连接时直接显示服务器列表(自带标题),不再套用带「新建/刷新」的应用栏;连接后才建立导航栈与动作栏 |
|
||||
| 轮次页脚 | 每个回合结束后显示「模型 · Agent · 耗时」,右侧「…」打开操作面板(时间戳、复制回答、从这里分叉);模型名由 `/api/model` 解析,耗时按 prompt→完成计算;模型图标改用内置 SVG(不依赖平台符号表) |
|
||||
| 用户消息操作 | 每条用户消息下有「时间 · 复制 · …」一排;「…」面板含时间戳、复制消息、从此消息前分叉 |
|
||||
| 长会话加载 | 首屏只取最新 30 条(倒序);请求 `x-openchamber-part-shells: v1` 让服务端省略已完成工具结果(本会话 530KB→180KB);展开工具时再按消息 id 取全文;流式期间用事件本地更新工具/步骤,不再反复拉整页;60s 超时 + 重试;连接建立不重复同步 |
|
||||
| Agent 选择 | 只列 `mode=primary` 的 Agent,子智能体(General/Explore)不再出现 |
|
||||
| 模型/Agent | 聊天页顶栏打开选择 Sheet(可搜索),切换后同步会话列表 |
|
||||
| 新建会话 | 会话页「新建」:标题 + 工作目录(服务端默认目录与已加载会话的目录),创建后直接进入聊天 |
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "OpenChamber",
|
||||
"id": "com.openchamber.native",
|
||||
"version": "0.1.10",
|
||||
"build": 11,
|
||||
"version": "0.1.11",
|
||||
"build": 12,
|
||||
"orientations": ["portrait", "landscape"]
|
||||
}
|
||||
+153
-14
@@ -132,7 +132,7 @@ func (ch *chat) loadCatalog() {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
models, err := client.Models(ctx, ch.dir)
|
||||
if err != nil {
|
||||
@@ -337,10 +337,10 @@ func (ch *chat) load() {
|
||||
}
|
||||
ch.loading.Set(true)
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
page, err := client.ListMessages(ctx, ch.id, ocapi.ListMessagesOptions{
|
||||
Directory: ch.dir, Limit: 30, Order: "desc",
|
||||
Directory: ch.dir, Limit: 30, Order: "desc", PartShells: true,
|
||||
})
|
||||
ui.Post(func() {
|
||||
ch.loading.Set(false)
|
||||
@@ -370,10 +370,10 @@ func (ch *chat) loadOlder() {
|
||||
ch.loading.Set(true)
|
||||
cursor := ch.olderCursor
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
page, err := client.ListMessages(ctx, ch.id, ocapi.ListMessagesOptions{
|
||||
Directory: ch.dir, Limit: 30, Cursor: cursor,
|
||||
Directory: ch.dir, Limit: 30, Cursor: cursor, PartShells: true,
|
||||
})
|
||||
ui.Post(func() {
|
||||
ch.loading.Set(false)
|
||||
@@ -397,10 +397,10 @@ func (ch *chat) reloadTail() {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
page, err := client.ListMessages(ctx, ch.id, ocapi.ListMessagesOptions{
|
||||
Directory: ch.dir, Limit: 30, Order: "desc",
|
||||
Directory: ch.dir, Limit: 30, Order: "desc", PartShells: true,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
@@ -508,14 +508,14 @@ func (ch *chat) handleEvent(ev ocapi.Event) {
|
||||
ch.ensureAssistant(d.AssistantMessageID, ev.Created)
|
||||
ch.running.Set(true)
|
||||
case "session.step.ended", "session.step.failed":
|
||||
ch.scheduleReload()
|
||||
ch.applyStepEnd(ev)
|
||||
case "session.text.started", "session.text.delta", "session.text.ended":
|
||||
ch.applyDelta(ev, "text", ch.textSlots)
|
||||
case "session.reasoning.started", "session.reasoning.delta", "session.reasoning.ended":
|
||||
ch.applyDelta(ev, "reasoning", ch.reasoningSlots)
|
||||
case "session.tool.input.started", "session.tool.input.ended", "session.tool.called",
|
||||
"session.tool.success", "session.tool.failed", "session.tool.progress":
|
||||
ch.scheduleReload()
|
||||
ch.applyToolEvent(ev)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -526,6 +526,95 @@ type deltaData struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type toolEventData struct {
|
||||
AssistantMessageID string `json:"assistantMessageID"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Input map[string]any `json:"input"`
|
||||
Executed bool `json:"executed"`
|
||||
Content []ocapi.ContentPart `json:"content"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
Error json.RawMessage `json:"error"`
|
||||
}
|
||||
|
||||
// applyToolEvent folds a tool call's events into its part, so a running turn
|
||||
// does not have to re-read whole message pages to show progress.
|
||||
func (ch *chat) applyToolEvent(ev ocapi.Event) {
|
||||
var d toolEventData
|
||||
if ev.UnmarshalData(&d) != nil || d.AssistantMessageID == "" || d.ID == "" {
|
||||
return
|
||||
}
|
||||
ch.ensureAssistant(d.AssistantMessageID, ev.Created)
|
||||
ch.updateMessage(d.AssistantMessageID, func(m *ocapi.Message) {
|
||||
idx := -1
|
||||
for i := range m.Content {
|
||||
if m.Content[i].Type == "tool" && m.Content[i].ID == d.ID {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if idx < 0 {
|
||||
m.Content = append(m.Content, ocapi.ContentPart{
|
||||
Type: "tool", ID: d.ID, Name: d.Name,
|
||||
State: &ocapi.ToolState{Status: "running"},
|
||||
})
|
||||
idx = len(m.Content) - 1
|
||||
}
|
||||
part := &m.Content[idx]
|
||||
if part.State == nil {
|
||||
part.State = &ocapi.ToolState{}
|
||||
}
|
||||
if d.Name != "" {
|
||||
part.Name = d.Name
|
||||
}
|
||||
switch ev.Type {
|
||||
case "session.tool.input.started":
|
||||
part.State.Status = "streaming"
|
||||
case "session.tool.called":
|
||||
part.State.Status = "running"
|
||||
part.State.Input = d.Input
|
||||
case "session.tool.progress":
|
||||
if d.Metadata != nil {
|
||||
part.State.Metadata = d.Metadata
|
||||
}
|
||||
case "session.tool.success":
|
||||
part.State.Status = "completed"
|
||||
part.State.Input = d.Input
|
||||
part.State.Content = d.Content
|
||||
part.State.Metadata = d.Metadata
|
||||
case "session.tool.failed":
|
||||
part.State.Status = "error"
|
||||
part.State.Input = d.Input
|
||||
part.State.Content = d.Content
|
||||
part.State.Metadata = d.Metadata
|
||||
part.State.Error = d.Error
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type stepEndData struct {
|
||||
AssistantMessageID string `json:"assistantMessageID"`
|
||||
Finish string `json:"finish"`
|
||||
Cost float64 `json:"cost"`
|
||||
Tokens *ocapi.TokenUsage `json:"tokens"`
|
||||
}
|
||||
|
||||
func (ch *chat) applyStepEnd(ev ocapi.Event) {
|
||||
var d stepEndData
|
||||
if ev.UnmarshalData(&d) != nil || d.AssistantMessageID == "" {
|
||||
return
|
||||
}
|
||||
ch.ensureAssistant(d.AssistantMessageID, ev.Created)
|
||||
ch.updateMessage(d.AssistantMessageID, func(m *ocapi.Message) {
|
||||
if d.Finish != "" {
|
||||
m.Finish = d.Finish
|
||||
}
|
||||
m.Cost = d.Cost
|
||||
m.Tokens = d.Tokens
|
||||
m.Time.Completed = ev.Created
|
||||
})
|
||||
}
|
||||
|
||||
func (ch *chat) applyDelta(ev ocapi.Event, kind string, slots map[string]map[int]int) {
|
||||
var d deltaData
|
||||
if ev.UnmarshalData(&d) != nil || d.AssistantMessageID == "" {
|
||||
@@ -653,8 +742,10 @@ func (ch *chat) screen() ui.Node {
|
||||
return ui.SafeArea(ui.Style{Grow: 1},
|
||||
ui.Column(ui.Style{Grow: 1},
|
||||
ui.Show(signal.Func(func() bool { return ch.errText.Get() != "" }), func() ui.Node {
|
||||
return ui.Text(ch.errText, ui.Style{Color: colors.Red, Font: fonts.Footnote,
|
||||
Padding: ui.Symmetric(4, 16)})
|
||||
return ui.Row(ui.Style{Padding: ui.Symmetric(2, 12), Gap: 8, Align: ui.AlignCenter},
|
||||
ui.Text(ch.errText, ui.Style{Color: colors.Red, Font: fonts.Footnote, Grow: 1}),
|
||||
ui.Button(ui.ButtonProps{Title: ui.Val("重试"), OnPress: ch.load}),
|
||||
)
|
||||
}),
|
||||
ui.Show(ch.loading, func() ui.Node {
|
||||
return ui.Progress(nil, ui.Style{Padding: ui.Symmetric(2, 16)})
|
||||
@@ -694,7 +785,7 @@ func (ch *chat) openPicker(kind string) {
|
||||
ch.pickerLoad.Set(true)
|
||||
ch.pickerOpen.Set(true)
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
var items []pickerItem
|
||||
var err error
|
||||
@@ -799,7 +890,7 @@ func (ch *chat) applyPick(it pickerItem) {
|
||||
}
|
||||
kind := ch.pickerKind
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
var err error
|
||||
if kind == "model" && it.Model != nil {
|
||||
@@ -870,6 +961,49 @@ func (ch *chat) permissionSection() ui.Node {
|
||||
})
|
||||
}
|
||||
|
||||
// ensureToolLoaded completes a tool result a shelled page left out, when
|
||||
// its row is opened.
|
||||
func (ch *chat) ensureToolLoaded(msgID string) {
|
||||
m, ok := ch.byID.Peek()[msgID]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
shelled := false
|
||||
for _, p := range m.Content {
|
||||
if p.Type == "tool" && p.State != nil && len(p.State.Content) == 0 {
|
||||
if _, marked := p.State.Metadata["openchamberContentShell"]; marked {
|
||||
shelled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !shelled {
|
||||
return
|
||||
}
|
||||
client := ch.client()
|
||||
if client == nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
full, err := client.GetMessage(ctx, ch.id, msgID, ch.dir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ui.Post(func() {
|
||||
list := ch.msgs.Peek()
|
||||
next := make([]ocapi.Message, len(list))
|
||||
copy(next, list)
|
||||
for i := range next {
|
||||
if next[i].ID == msgID {
|
||||
next[i] = full
|
||||
}
|
||||
}
|
||||
ch.setMessages(next)
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
func (ch *chat) reply(req ocapi.PermissionRequest, decision ocapi.PermissionDecision) {
|
||||
client := ch.client()
|
||||
if client == nil {
|
||||
@@ -1208,7 +1342,12 @@ func (ch *chat) toolRow(cur chatItem) ui.Node {
|
||||
return fmt.Sprintf("%s %s %s", icon, strings.TrimSpace(p.Name+" "+toolSummary(p)), mark)
|
||||
})
|
||||
return ui.Column(ui.Style{Gap: 4, Padding: ui.Symmetric(4, 14)},
|
||||
ui.View(ui.ViewProps{OnPress: func() { ch.toggle(key) }},
|
||||
ui.View(ui.ViewProps{OnPress: func() {
|
||||
ch.toggle(key)
|
||||
if ch.expanded.Peek()[key] {
|
||||
ch.ensureToolLoaded(cur.MsgID)
|
||||
}
|
||||
}},
|
||||
ui.Text(headline, ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel, Lines: 2}),
|
||||
),
|
||||
ui.Show(open, func() ui.Node {
|
||||
|
||||
@@ -64,7 +64,10 @@ func newChatServer(t *testing.T) *chatServer {
|
||||
{"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"},
|
||||
"time":{"created":11,"completed":12000},
|
||||
"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",
|
||||
"input":{"command":"echo hi"},"content":[],
|
||||
"metadata":{"openchamberContentShell":{}}}}]}
|
||||
],"cursor":{}}`)
|
||||
})
|
||||
mux.HandleFunc("/api/session/ses_new/message", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -120,6 +123,13 @@ func newChatServer(t *testing.T) *chatServer {
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_new","title":"New chat","time":{"created":3,"updated":3},
|
||||
"location":{"directory":"/home/me/project"}}}`)
|
||||
})
|
||||
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"},
|
||||
"time":{"created":11,"completed":12000},
|
||||
"content":[{"type":"text","text":"**Hello** from the agent"},
|
||||
{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed",
|
||||
"input":{"command":"echo hi"},"content":[{"type":"text","text":"tool output here"}]}}]}`)
|
||||
})
|
||||
mux.HandleFunc("/api/session/ses_1/fork", func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]any
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
@@ -348,3 +358,17 @@ func TestUserMessageActions(t *testing.T) {
|
||||
a.Expect("复制消息")
|
||||
a.Back()
|
||||
}
|
||||
|
||||
func TestToolResultLoadsOnExpand(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")
|
||||
|
||||
// The list page shells the tool result; opening the row fetches it.
|
||||
a.ExpectNo("tool output here")
|
||||
a.Tap("✓ shell echo hi ▸")
|
||||
a.WaitFor("tool output here")
|
||||
}
|
||||
@@ -85,6 +85,9 @@ func (c *controller) startEvents(client *ocapi.Client) {
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
c.cancelSSE = cancel
|
||||
// A stream that reconnects lost events; a fresh connection (the one
|
||||
// made when connecting) is the list's first read, not a gap.
|
||||
wasDown := false
|
||||
sub := ocapi.Subscription{
|
||||
Types: eventTypes,
|
||||
OnEvent: func(ev ocapi.Event) {
|
||||
@@ -98,6 +101,7 @@ func (c *controller) startEvents(client *ocapi.Client) {
|
||||
OnState: func(st ocapi.SubscriptionState, err error) {
|
||||
switch st {
|
||||
case ocapi.Disconnected:
|
||||
wasDown = true
|
||||
ui.Post(func() {
|
||||
if c.client != client {
|
||||
return
|
||||
@@ -105,6 +109,10 @@ func (c *controller) startEvents(client *ocapi.Client) {
|
||||
c.status.Set("连接中断,正在重连…")
|
||||
})
|
||||
case ocapi.Subscribed:
|
||||
if !wasDown {
|
||||
return
|
||||
}
|
||||
wasDown = false
|
||||
// Events missed while the stream was down are gone;
|
||||
// re-read the list and the open chat's tail.
|
||||
ui.Post(func() {
|
||||
|
||||
@@ -155,3 +155,40 @@ func TestCertificateFingerprint(t *testing.T) {
|
||||
t.Fatalf("insecure option did not work: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartShellsHeaderAndGetMessage(t *testing.T) {
|
||||
var gotHeader string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/api/session/ses_1/message":
|
||||
gotHeader = r.Header.Get("x-openchamber-part-shells")
|
||||
fmt.Fprint(w, `{"data":[{"id":"msg_1","type":"assistant","content":[
|
||||
{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed","input":{},"content":[],"metadata":{"openchamberContentShell":{}}}}]}],"cursor":{}}`)
|
||||
case "/api/session/ses_1/message/msg_1":
|
||||
fmt.Fprint(w, `{"id":"msg_1","type":"assistant","content":[
|
||||
{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed","input":{},"content":[{"type":"text","text":"full output"}]}}]}`)
|
||||
default:
|
||||
t.Errorf("unexpected path %q", r.URL.Path)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c, _ := New(srv.URL, Options{})
|
||||
page, err := c.ListMessages(context.Background(), "ses_1", ListMessagesOptions{Limit: 10, PartShells: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if gotHeader != "v1" {
|
||||
t.Errorf("header = %q", gotHeader)
|
||||
}
|
||||
if len(page.Data) != 1 || len(page.Data[0].Content[0].State.Content) != 0 {
|
||||
t.Fatalf("page = %+v", page)
|
||||
}
|
||||
msg, err := c.GetMessage(context.Background(), "ses_1", "msg_1", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := msg.Content[0].State.Content[0].Text; got != "full output" {
|
||||
t.Fatalf("message = %+v", msg)
|
||||
}
|
||||
}
|
||||
@@ -290,6 +290,11 @@ func (c *Client) do(ctx context.Context, method, path string, ro requestOptions,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.send(req, out)
|
||||
}
|
||||
|
||||
// send performs an already-built request and decodes it.
|
||||
func (c *Client) send(req *http.Request, out any) error {
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -128,6 +128,10 @@ type ListMessagesOptions struct {
|
||||
Limit int
|
||||
Cursor string
|
||||
Order string // "asc" or "desc"
|
||||
// PartShells asks OpenChamber to leave finished tool results out of the
|
||||
// page, which is most of a long session's bytes. Fetch the message with
|
||||
// GetMessage when a row expands.
|
||||
PartShells bool
|
||||
}
|
||||
|
||||
// ListMessages reads one page of a session's messages. Newest first by
|
||||
@@ -143,10 +147,28 @@ func (c *Client) ListMessages(ctx context.Context, id string, o ListMessagesOpti
|
||||
if o.Order != "" {
|
||||
q.Set("order", o.Order)
|
||||
}
|
||||
req, err := c.newRequest(ctx, http.MethodGet, "/api/session/"+url.PathEscape(id)+"/message",
|
||||
requestOptions{directory: o.Directory, query: q}, nil)
|
||||
if err != nil {
|
||||
return MessagePage{}, err
|
||||
}
|
||||
if o.PartShells {
|
||||
req.Header.Set("x-openchamber-part-shells", "v1")
|
||||
}
|
||||
var page MessagePage
|
||||
err := c.do(ctx, http.MethodGet, "/api/session/"+url.PathEscape(id)+"/message",
|
||||
requestOptions{directory: o.Directory, query: q}, nil, &page)
|
||||
return page, err
|
||||
if err := c.send(req, &page); err != nil {
|
||||
return MessagePage{}, err
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
// GetMessage reads one whole message, including tool results a shelled list
|
||||
// page left out.
|
||||
func (c *Client) GetMessage(ctx context.Context, sessionID, messageID, directory string) (Message, error) {
|
||||
var msg Message
|
||||
path := "/api/session/" + url.PathEscape(sessionID) + "/message/" + url.PathEscape(messageID)
|
||||
err := c.do(ctx, http.MethodGet, path, requestOptions{directory: directory}, nil, &msg)
|
||||
return msg, err
|
||||
}
|
||||
|
||||
// ModelInfo is one model the server offers.
|
||||
|
||||
Reference in new issue
Block a user