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:
blumia committed 2026-11-16 20:04:42 +08:00
1 parent 0e34d82a06
commit e67490e768
8 files changed
+256 -20

No files matched your search

+153 -14
View File
@@ -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 {