2026-11-16 09:09:09 +08:00
|
|
|
package appui
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2026-11-16 18:05:15 +08:00
|
|
|
"math"
|
2026-11-16 09:09:09 +08:00
|
|
|
"sort"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/egoist/asuka/colors"
|
|
|
|
|
"github.com/egoist/asuka/fonts"
|
|
|
|
|
"github.com/egoist/asuka/sdk/markdown"
|
2026-11-17 21:52:06 +08:00
|
|
|
"github.com/egoist/asuka/sdk/speech"
|
2026-11-16 19:55:05 +08:00
|
|
|
"github.com/egoist/asuka/sdk/svg"
|
2026-11-16 09:09:09 +08:00
|
|
|
"github.com/egoist/asuka/signal"
|
|
|
|
|
"github.com/egoist/asuka/ui"
|
|
|
|
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// chatItem is one row of the chat list: a whole message, or one part of an
|
|
|
|
|
// assistant message. Splitting parts into rows keeps every row's structure
|
|
|
|
|
// fixed, which the list's recycled cells require.
|
|
|
|
|
type chatItem struct {
|
|
|
|
|
Key string
|
|
|
|
|
Kind string // user | thinking | text | reasoning | tool | shell | note
|
|
|
|
|
MsgID string
|
|
|
|
|
Part int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// chat is one open session: its messages, its event projection and its
|
|
|
|
|
// composer.
|
|
|
|
|
type chat struct {
|
|
|
|
|
c *controller
|
|
|
|
|
id string
|
|
|
|
|
dir string
|
|
|
|
|
nav *ui.Navigator
|
|
|
|
|
open bool
|
|
|
|
|
|
|
|
|
|
title *signal.Signal[string]
|
|
|
|
|
msgs *signal.Signal[[]ocapi.Message]
|
|
|
|
|
byID *signal.Signal[map[string]ocapi.Message]
|
|
|
|
|
running *signal.Signal[bool]
|
|
|
|
|
loading *signal.Signal[bool]
|
|
|
|
|
draft *signal.Signal[string]
|
|
|
|
|
expanded *signal.Signal[map[string]bool]
|
|
|
|
|
errText *signal.Signal[string]
|
|
|
|
|
|
|
|
|
|
olderCursor string
|
|
|
|
|
hasMore bool
|
|
|
|
|
|
|
|
|
|
textSlots map[string]map[int]int
|
|
|
|
|
reasoningSlots map[string]map[int]int
|
|
|
|
|
reloadScheduled bool
|
|
|
|
|
listNode ui.Node
|
2026-11-16 10:31:20 +08:00
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
permList *signal.Signal[[]ocapi.PermissionRequest]
|
|
|
|
|
hasPerms *signal.Signal[bool]
|
|
|
|
|
|
2026-11-18 02:15:11 +08:00
|
|
|
modelNames *signal.Signal[map[string]string]
|
|
|
|
|
modelLimits *signal.Signal[map[string]modelLimit]
|
|
|
|
|
usageOpen *signal.Signal[bool]
|
2026-11-16 18:05:15 +08:00
|
|
|
|
2026-11-16 18:34:32 +08:00
|
|
|
footerOpen *signal.Signal[bool]
|
|
|
|
|
footerID *signal.Signal[string]
|
2026-11-16 19:55:05 +08:00
|
|
|
userOpen *signal.Signal[bool]
|
|
|
|
|
userID *signal.Signal[string]
|
2026-11-16 18:34:32 +08:00
|
|
|
|
2026-11-17 21:52:06 +08:00
|
|
|
attachments *signal.Signal[[]attachment]
|
|
|
|
|
attaching *signal.Signal[bool]
|
|
|
|
|
jumpToLatest *signal.Signal[bool]
|
|
|
|
|
lastScroll float32
|
2026-11-17 23:46:06 +08:00
|
|
|
imagePaths map[string][]string
|
2026-11-17 21:52:06 +08:00
|
|
|
|
2026-11-18 02:15:11 +08:00
|
|
|
previewOpen *signal.Signal[bool]
|
|
|
|
|
previewLoading *signal.Signal[bool]
|
|
|
|
|
previewPath *signal.Signal[string]
|
|
|
|
|
previewTitle *signal.Signal[string]
|
|
|
|
|
previewCache map[string]string
|
|
|
|
|
previewSeq int
|
|
|
|
|
|
2026-11-16 10:31:20 +08:00
|
|
|
pickerOpen *signal.Signal[bool]
|
|
|
|
|
pickerKind string
|
|
|
|
|
pickerItems *signal.Signal[[]pickerItem]
|
|
|
|
|
pickerFilter *signal.Signal[string]
|
|
|
|
|
pickerLoad *signal.Signal[bool]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pickerItem is one row of the model/agent sheet.
|
|
|
|
|
type pickerItem struct {
|
|
|
|
|
Key string
|
|
|
|
|
Title string
|
|
|
|
|
Subtitle string
|
|
|
|
|
Model *ocapi.ModelRef
|
|
|
|
|
Agent string
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *controller) openChat(s *ocapi.Session, nav *ui.Navigator) *chat {
|
|
|
|
|
dir := ""
|
|
|
|
|
if s.Location != nil {
|
|
|
|
|
dir = s.Location.Directory
|
|
|
|
|
}
|
|
|
|
|
ch := &chat{
|
|
|
|
|
c: c,
|
|
|
|
|
id: s.ID,
|
|
|
|
|
dir: dir,
|
|
|
|
|
nav: nav,
|
|
|
|
|
open: true,
|
|
|
|
|
title: signal.New(s.Title),
|
|
|
|
|
msgs: signal.NewFunc([]ocapi.Message{}, nil),
|
|
|
|
|
byID: signal.NewFunc(map[string]ocapi.Message{}, nil),
|
|
|
|
|
running: signal.New(c.active.Peek()[s.ID]),
|
|
|
|
|
loading: signal.New(false),
|
|
|
|
|
draft: signal.New(""),
|
|
|
|
|
expanded: signal.NewFunc(map[string]bool{}, nil),
|
|
|
|
|
errText: signal.New(""),
|
|
|
|
|
textSlots: map[string]map[int]int{},
|
|
|
|
|
reasoningSlots: map[string]map[int]int{},
|
2026-11-16 11:52:38 +08:00
|
|
|
permList: signal.NewFunc([]ocapi.PermissionRequest{}, nil),
|
|
|
|
|
hasPerms: signal.New(false),
|
2026-11-16 18:05:15 +08:00
|
|
|
modelNames: signal.NewFunc(map[string]string{}, nil),
|
2026-11-18 02:15:11 +08:00
|
|
|
modelLimits: signal.NewFunc(map[string]modelLimit{}, nil),
|
|
|
|
|
usageOpen: signal.New(false),
|
2026-11-16 18:34:32 +08:00
|
|
|
footerOpen: signal.New(false),
|
|
|
|
|
footerID: signal.New(""),
|
2026-11-16 19:55:05 +08:00
|
|
|
userOpen: signal.New(false),
|
|
|
|
|
userID: signal.New(""),
|
2026-11-17 21:52:06 +08:00
|
|
|
attachments: signal.NewFunc([]attachment{}, nil),
|
|
|
|
|
attaching: signal.New(false),
|
|
|
|
|
jumpToLatest: signal.New(false),
|
2026-11-17 23:46:06 +08:00
|
|
|
imagePaths: map[string][]string{},
|
2026-11-18 02:15:11 +08:00
|
|
|
previewOpen: signal.New(false),
|
|
|
|
|
previewLoading: signal.New(false),
|
|
|
|
|
previewPath: signal.New(""),
|
|
|
|
|
previewTitle: signal.New(""),
|
|
|
|
|
previewCache: map[string]string{},
|
2026-11-16 10:31:20 +08:00
|
|
|
pickerOpen: signal.New(false),
|
|
|
|
|
pickerItems: signal.NewFunc([]pickerItem{}, nil),
|
|
|
|
|
pickerFilter: signal.New(""),
|
|
|
|
|
pickerLoad: signal.New(false),
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
c.chat = ch
|
2026-11-16 11:52:38 +08:00
|
|
|
ch.syncPerms()
|
2026-11-16 18:05:15 +08:00
|
|
|
ch.loadCatalog()
|
2026-11-16 09:09:09 +08:00
|
|
|
ch.load()
|
|
|
|
|
return ch
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) client() *ocapi.Client { return ch.c.client }
|
|
|
|
|
|
2026-11-16 18:05:15 +08:00
|
|
|
// loadCatalog resolves model ids to display names for the footer. A failed
|
|
|
|
|
// read is not worth showing: the footer falls back to the id.
|
|
|
|
|
func (ch *chat) loadCatalog() {
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
2026-11-16 20:04:42 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
2026-11-16 18:05:15 +08:00
|
|
|
defer cancel()
|
|
|
|
|
models, err := client.Models(ctx, ch.dir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
names := make(map[string]string, len(models))
|
2026-11-18 02:15:11 +08:00
|
|
|
limits := make(map[string]modelLimit, len(models))
|
2026-11-16 18:05:15 +08:00
|
|
|
for _, m := range models {
|
|
|
|
|
name := m.Name
|
|
|
|
|
if name == "" {
|
|
|
|
|
name = m.ID
|
|
|
|
|
}
|
|
|
|
|
names[m.ProviderID+"/"+m.ID] = name
|
|
|
|
|
names[m.ID] = name
|
2026-11-18 02:15:11 +08:00
|
|
|
lim := modelLimit{Context: m.Limit.Context, Output: m.Limit.Output}
|
|
|
|
|
limits[m.ProviderID+"/"+m.ID] = lim
|
|
|
|
|
limits[m.ID] = lim
|
2026-11-16 18:05:15 +08:00
|
|
|
}
|
2026-11-18 02:15:11 +08:00
|
|
|
ui.Post(func() {
|
|
|
|
|
ch.modelNames.Set(names)
|
|
|
|
|
ch.modelLimits.Set(limits)
|
|
|
|
|
})
|
2026-11-16 18:05:15 +08:00
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) modelName(m *ocapi.ModelRef) string {
|
|
|
|
|
if m == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
names := ch.modelNames.Get()
|
|
|
|
|
if name := names[m.ProviderID+"/"+m.ID]; name != "" {
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
if name := names[m.ID]; name != "" {
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
return m.ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// turnDuration is the time from this turn's prompt to the message being
|
|
|
|
|
// complete, as the first-party footer shows it.
|
|
|
|
|
func (ch *chat) turnDuration(id string) string {
|
|
|
|
|
msgs := ch.msgs.Peek()
|
|
|
|
|
idx := -1
|
|
|
|
|
for i := range msgs {
|
|
|
|
|
if msgs[i].ID == id {
|
|
|
|
|
idx = i
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if idx < 0 || msgs[idx].Time.Completed <= 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
start := int64(0)
|
|
|
|
|
for i := idx - 1; i >= 0; i-- {
|
|
|
|
|
if msgs[i].Type == "user" {
|
|
|
|
|
start = msgs[i].Time.Created
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-16 18:19:27 +08:00
|
|
|
if start <= 0 {
|
|
|
|
|
// The prompt fell outside the loaded page (a long turn). Measure
|
|
|
|
|
// from the first record of this run instead, so historical turns
|
|
|
|
|
// still show a duration.
|
|
|
|
|
j := idx
|
|
|
|
|
for j > 0 {
|
|
|
|
|
prev := msgs[j-1].Type
|
|
|
|
|
if prev == "user" || prev == "synthetic" {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
j--
|
|
|
|
|
}
|
|
|
|
|
start = msgs[j].Time.Created
|
|
|
|
|
}
|
2026-11-16 18:05:15 +08:00
|
|
|
if start <= 0 || msgs[idx].Time.Completed <= start {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return formatTurnDuration(time.Duration(msgs[idx].Time.Completed-start) * time.Millisecond)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func formatTurnDuration(d time.Duration) string {
|
|
|
|
|
ms := d.Milliseconds()
|
|
|
|
|
if ms < 60_000 {
|
|
|
|
|
return fmt.Sprintf("%.1fs", float64(ms)/1000)
|
|
|
|
|
}
|
|
|
|
|
total := int(math.Round(float64(ms) / 1000))
|
|
|
|
|
return fmt.Sprintf("%dm %ds", total/60, total%60)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 21:52:06 +08:00
|
|
|
func (ch *chat) speakAnswer(id string) {
|
|
|
|
|
text := ch.answerText(id)
|
|
|
|
|
if strings.TrimSpace(text) == "" {
|
|
|
|
|
ui.Snackbar(ui.SnackbarProps{Message: "没有可朗读的文字"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
speech.Speak(text, speech.Options{}, func(err error) {
|
|
|
|
|
if err != nil && err != speech.ErrStopped {
|
|
|
|
|
ui.Snackbar(ui.SnackbarProps{Message: "朗读失败:" + err.Error()})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) answerText(id string) string {
|
2026-11-16 18:05:15 +08:00
|
|
|
m := ch.byID.Peek()[id]
|
|
|
|
|
var b strings.Builder
|
|
|
|
|
for _, p := range m.Content {
|
|
|
|
|
if p.Type == "text" && p.Text != "" {
|
|
|
|
|
if b.Len() > 0 {
|
|
|
|
|
b.WriteString("\n\n")
|
|
|
|
|
}
|
|
|
|
|
b.WriteString(p.Text)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-17 21:52:06 +08:00
|
|
|
return strings.TrimSpace(b.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) copyAnswer(id string) {
|
|
|
|
|
text := ch.answerText(id)
|
2026-11-16 18:05:15 +08:00
|
|
|
if text == "" {
|
|
|
|
|
ui.Snackbar(ui.SnackbarProps{Message: "没有可复制的文字"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ui.Copy(text)
|
|
|
|
|
ui.Snackbar(ui.SnackbarProps{Message: "已复制回答"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) footerMenu(id string) {
|
2026-11-16 18:34:32 +08:00
|
|
|
ch.footerID.Set(id)
|
|
|
|
|
ch.footerOpen.Set(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// footerSheet is the "more" panel: the completion time, then the actions,
|
|
|
|
|
// as the first-party client shows on touch screens.
|
|
|
|
|
func (ch *chat) footerSheet() ui.Node {
|
|
|
|
|
return ui.Sheet(ui.SheetProps{
|
|
|
|
|
Open: ch.footerOpen,
|
|
|
|
|
Detents: ui.DetentMedium,
|
|
|
|
|
Grabber: true,
|
|
|
|
|
OnDismiss: func() { ch.footerOpen.Set(false) },
|
|
|
|
|
Content: func() ui.Node {
|
|
|
|
|
id := ch.footerID.Peek()
|
|
|
|
|
m := ch.byID.Peek()[id]
|
|
|
|
|
stamp := ""
|
|
|
|
|
if m.Time.Completed > 0 {
|
|
|
|
|
stamp = time.UnixMilli(m.Time.Completed).Format("2006-01-02 15:04")
|
|
|
|
|
}
|
|
|
|
|
act := func(f func()) func() {
|
|
|
|
|
return func() {
|
|
|
|
|
ch.footerOpen.Set(false)
|
|
|
|
|
f()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ui.SafeArea(ui.Style{Padding: ui.All(20), Gap: 4},
|
|
|
|
|
ui.StaticText("更多操作", ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
|
|
|
|
ui.Show(signal.Func(func() bool { return stamp != "" }), func() ui.Node {
|
|
|
|
|
return ui.Row(ui.Style{Padding: ui.Symmetric(8, 0), Gap: 8, Align: ui.AlignCenter},
|
|
|
|
|
ui.Symbol(ui.Icon("clock"), ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
|
|
|
|
ui.StaticText(stamp, ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
|
|
|
|
)
|
|
|
|
|
}),
|
2026-11-17 21:52:06 +08:00
|
|
|
sheetAction("朗读回答", "speaker.wave.2", act(func() { ch.speakAnswer(id) })),
|
|
|
|
|
sheetAction("停止朗读", "speaker.slash", act(func() { speech.Stop() })),
|
2026-11-16 18:34:32 +08:00
|
|
|
sheetAction("复制回答", "doc.on.doc", act(func() { ch.copyAnswer(id) })),
|
|
|
|
|
sheetAction("从这里分叉", "", act(func() { ch.forkFrom(id) })),
|
|
|
|
|
)
|
2026-11-16 18:05:15 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 18:34:32 +08:00
|
|
|
func sheetAction(title string, icon ui.Icon, onPress func()) ui.Node {
|
|
|
|
|
parts := make([]ui.Node, 0, 2)
|
|
|
|
|
if icon != "" {
|
|
|
|
|
parts = append(parts, ui.Symbol(icon, ui.Style{Font: fonts.Body, Color: colors.SecondaryLabel}))
|
|
|
|
|
}
|
|
|
|
|
parts = append(parts, ui.StaticText(title, ui.Style{Font: fonts.Body}))
|
|
|
|
|
return ui.View(ui.ViewProps{
|
|
|
|
|
OnPress: onPress,
|
|
|
|
|
Style: ui.Style{Padding: ui.Symmetric(12, 4), Align: ui.AlignCenter},
|
|
|
|
|
}, ui.Row(ui.Style{Gap: 12, Align: ui.AlignCenter}, parts...))
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 18:05:15 +08:00
|
|
|
func (ch *chat) forkFrom(id string) {
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
msgs := ch.msgs.Peek()
|
|
|
|
|
idx := -1
|
|
|
|
|
for i := range msgs {
|
|
|
|
|
if msgs[i].ID == id {
|
|
|
|
|
idx = i
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
before := ""
|
|
|
|
|
for i := idx + 1; i >= 0 && i < len(msgs); i++ {
|
|
|
|
|
if msgs[i].Type == "user" {
|
|
|
|
|
before = msgs[i].ID
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
s, err := client.ForkSession(ctx, ch.id, ch.dir, before)
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
ch.errText.Set(friendlyError(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.c.sessions.Update(func(list []ocapi.Session) []ocapi.Session {
|
|
|
|
|
next := make([]ocapi.Session, 0, len(list)+1)
|
|
|
|
|
next = append(next, s)
|
|
|
|
|
return append(next, list...)
|
|
|
|
|
})
|
|
|
|
|
if ch.nav != nil {
|
|
|
|
|
nc := ch.c.openChat(&s, ch.nav)
|
|
|
|
|
ch.nav.Push(nc.screen, ui.ScreenOptions{Title: nc.title, Actions: chatActions(nc)})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 09:09:09 +08:00
|
|
|
func (ch *chat) load() {
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.loading.Set(true)
|
|
|
|
|
go func() {
|
2026-11-16 20:04:42 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
2026-11-16 09:09:09 +08:00
|
|
|
defer cancel()
|
|
|
|
|
page, err := client.ListMessages(ctx, ch.id, ocapi.ListMessagesOptions{
|
2026-11-16 20:04:42 +08:00
|
|
|
Directory: ch.dir, Limit: 30, Order: "desc", PartShells: true,
|
2026-11-16 09:09:09 +08:00
|
|
|
})
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
ch.loading.Set(false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if ocapi.IsUnauthorized(err) {
|
|
|
|
|
ch.c.logout("登录已失效,请输入密码重新连接")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.errText.Set(friendlyError(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.olderCursor = page.Cursor.Next
|
|
|
|
|
ch.hasMore = page.Cursor.Next != ""
|
|
|
|
|
ch.setMessages(ascending(page.Data))
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) loadOlder() {
|
|
|
|
|
if !ch.hasMore || ch.loading.Peek() || ch.olderCursor == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.loading.Set(true)
|
|
|
|
|
cursor := ch.olderCursor
|
|
|
|
|
go func() {
|
2026-11-16 20:04:42 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
2026-11-16 09:09:09 +08:00
|
|
|
defer cancel()
|
|
|
|
|
page, err := client.ListMessages(ctx, ch.id, ocapi.ListMessagesOptions{
|
2026-11-16 20:04:42 +08:00
|
|
|
Directory: ch.dir, Limit: 30, Cursor: cursor, PartShells: true,
|
2026-11-16 09:09:09 +08:00
|
|
|
})
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
ch.loading.Set(false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ch.errText.Set(friendlyError(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
older := ascending(page.Data)
|
|
|
|
|
ch.olderCursor = page.Cursor.Next
|
|
|
|
|
ch.hasMore = page.Cursor.Next != ""
|
|
|
|
|
ch.setMessages(append(older, ch.msgs.Peek()...))
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reloadTail refetches the newest messages and replaces the tail, which is
|
|
|
|
|
// how tool state and turn boundaries become authoritative.
|
|
|
|
|
func (ch *chat) reloadTail() {
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
2026-11-16 20:04:42 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
2026-11-16 09:09:09 +08:00
|
|
|
defer cancel()
|
|
|
|
|
page, err := client.ListMessages(ctx, ch.id, ocapi.ListMessagesOptions{
|
2026-11-16 20:04:42 +08:00
|
|
|
Directory: ch.dir, Limit: 30, Order: "desc", PartShells: true,
|
2026-11-16 09:09:09 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
tail := ascending(page.Data)
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
ch.reloadScheduled = false
|
|
|
|
|
if len(tail) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
cutoff := tail[0].Time.Created
|
|
|
|
|
keep := make([]ocapi.Message, 0, len(ch.msgs.Peek())+len(tail))
|
|
|
|
|
for _, m := range ch.msgs.Peek() {
|
|
|
|
|
if m.Time.Created < cutoff {
|
|
|
|
|
keep = append(keep, m)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ch.setMessages(append(keep, tail...))
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// scheduleReload coalesces the many events a tool call produces.
|
|
|
|
|
func (ch *chat) scheduleReload() {
|
|
|
|
|
if ch.reloadScheduled {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.reloadScheduled = true
|
|
|
|
|
time.AfterFunc(300*time.Millisecond, func() {
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
if ch.open {
|
|
|
|
|
ch.reloadTail()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) send() {
|
|
|
|
|
text := strings.TrimSpace(ch.draft.Peek())
|
2026-11-17 21:52:06 +08:00
|
|
|
files := ch.attachmentFiles()
|
|
|
|
|
if (text == "" && len(files) == 0) || ch.client() == nil {
|
2026-11-16 09:09:09 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
id := newMessageID()
|
2026-11-17 21:52:06 +08:00
|
|
|
sent := ch.optimisticFiles()
|
2026-11-17 23:46:06 +08:00
|
|
|
for _, a := range ch.attachments.Peek() {
|
|
|
|
|
if a.LocalPath != "" {
|
|
|
|
|
ch.imagePaths[id] = append(ch.imagePaths[id], a.LocalPath)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-16 09:09:09 +08:00
|
|
|
ch.draft.Set("")
|
2026-11-17 21:52:06 +08:00
|
|
|
ch.attachments.Set(nil)
|
2026-11-16 09:09:09 +08:00
|
|
|
ch.errText.Set("")
|
|
|
|
|
ch.running.Set(true)
|
|
|
|
|
ch.addMessage(ocapi.Message{
|
2026-11-17 21:52:06 +08:00
|
|
|
ID: id,
|
|
|
|
|
Type: "user",
|
|
|
|
|
Text: text,
|
|
|
|
|
Files: sent,
|
|
|
|
|
Time: ocapi.MessageTime{Created: time.Now().UnixMilli()},
|
2026-11-16 09:09:09 +08:00
|
|
|
})
|
|
|
|
|
client := ch.client()
|
|
|
|
|
go func() {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
|
|
|
|
|
defer cancel()
|
2026-11-17 21:52:06 +08:00
|
|
|
err := client.Prompt(ctx, ch.id, ocapi.PromptOptions{Directory: ch.dir, Text: text, MessageID: id, Files: files})
|
2026-11-16 09:09:09 +08:00
|
|
|
if err == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
ch.running.Set(false)
|
|
|
|
|
if ocapi.IsUnauthorized(err) {
|
|
|
|
|
ch.c.logout("登录已失效,请输入密码重新连接")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.errText.Set(friendlyError(err))
|
|
|
|
|
})
|
|
|
|
|
}()
|
2026-11-17 21:52:06 +08:00
|
|
|
ch.jumpToLatest.Set(false)
|
2026-11-16 09:09:09 +08:00
|
|
|
ui.ScrollToEnd(ch.listNode, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) stop() {
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
_, err := client.Interrupt(ctx, ch.id, ch.dir)
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
ch.errText.Set(friendlyError(err))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleEvent runs on the main thread, routed by the controller's stream.
|
|
|
|
|
func (ch *chat) handleEvent(ev ocapi.Event) {
|
|
|
|
|
switch ev.Type {
|
|
|
|
|
case "session.execution.started":
|
|
|
|
|
ch.running.Set(true)
|
|
|
|
|
case "session.execution.succeeded", "session.execution.failed", "session.execution.interrupted":
|
|
|
|
|
ch.running.Set(false)
|
|
|
|
|
ch.scheduleReload()
|
|
|
|
|
case "session.step.started":
|
|
|
|
|
var d struct {
|
|
|
|
|
AssistantMessageID string `json:"assistantMessageID"`
|
|
|
|
|
}
|
|
|
|
|
if ev.UnmarshalData(&d) != nil || d.AssistantMessageID == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.ensureAssistant(d.AssistantMessageID, ev.Created)
|
|
|
|
|
ch.running.Set(true)
|
|
|
|
|
case "session.step.ended", "session.step.failed":
|
2026-11-16 20:04:42 +08:00
|
|
|
ch.applyStepEnd(ev)
|
2026-11-16 09:09:09 +08:00
|
|
|
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":
|
2026-11-16 20:04:42 +08:00
|
|
|
ch.applyToolEvent(ev)
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type deltaData struct {
|
|
|
|
|
AssistantMessageID string `json:"assistantMessageID"`
|
|
|
|
|
Ordinal int `json:"ordinal"`
|
|
|
|
|
Delta string `json:"delta"`
|
|
|
|
|
Text string `json:"text"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 20:04:42 +08:00
|
|
|
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 {
|
2026-11-16 20:11:14 +08:00
|
|
|
// A terminal event can arrive without the starting one (a
|
|
|
|
|
// stream joined late). Without a name the row would be
|
|
|
|
|
// blank; the next page read adds the call properly.
|
|
|
|
|
if d.Name == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-16 20:04:42 +08:00
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 09:09:09 +08:00
|
|
|
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 == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.ensureAssistant(d.AssistantMessageID, ev.Created)
|
|
|
|
|
ch.updateMessage(d.AssistantMessageID, func(m *ocapi.Message) {
|
|
|
|
|
idx := slotFor(slots, m, d.Ordinal, kind)
|
|
|
|
|
if idx < 0 || idx >= len(m.Content) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
part := &m.Content[idx]
|
|
|
|
|
switch {
|
|
|
|
|
case strings.HasSuffix(ev.Type, ".started"):
|
|
|
|
|
part.Type = kind
|
|
|
|
|
case strings.HasSuffix(ev.Type, ".delta"):
|
|
|
|
|
part.Text += d.Delta
|
|
|
|
|
case strings.HasSuffix(ev.Type, ".ended"):
|
|
|
|
|
part.Text = d.Text
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func slotFor(slots map[string]map[int]int, m *ocapi.Message, ordinal int, kind string) int {
|
|
|
|
|
byOrdinal := slots[m.ID]
|
|
|
|
|
if byOrdinal == nil {
|
|
|
|
|
byOrdinal = map[int]int{}
|
|
|
|
|
slots[m.ID] = byOrdinal
|
|
|
|
|
}
|
|
|
|
|
if idx, ok := byOrdinal[ordinal]; ok && idx < len(m.Content) {
|
|
|
|
|
return idx
|
|
|
|
|
}
|
|
|
|
|
m.Content = append(m.Content, ocapi.ContentPart{Type: kind})
|
|
|
|
|
idx := len(m.Content) - 1
|
|
|
|
|
byOrdinal[ordinal] = idx
|
|
|
|
|
return idx
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) ensureAssistant(id string, created int64) {
|
|
|
|
|
if _, ok := ch.byID.Peek()[id]; ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.addMessage(ocapi.Message{ID: id, Type: "assistant", Time: ocapi.MessageTime{Created: created}})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) addMessage(m ocapi.Message) {
|
|
|
|
|
list := ch.msgs.Peek()
|
|
|
|
|
next := make([]ocapi.Message, len(list), len(list)+1)
|
|
|
|
|
copy(next, list)
|
|
|
|
|
next = append(next, m)
|
|
|
|
|
sort.SliceStable(next, func(i, j int) bool {
|
|
|
|
|
if next[i].Time.Created == next[j].Time.Created {
|
|
|
|
|
return next[i].ID < next[j].ID
|
|
|
|
|
}
|
|
|
|
|
return next[i].Time.Created < next[j].Time.Created
|
|
|
|
|
})
|
|
|
|
|
ch.setMessages(next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) updateMessage(id string, fn func(*ocapi.Message)) {
|
|
|
|
|
list := ch.msgs.Peek()
|
|
|
|
|
next := make([]ocapi.Message, len(list))
|
|
|
|
|
copy(next, list)
|
|
|
|
|
for i := range next {
|
|
|
|
|
if next[i].ID == id {
|
|
|
|
|
fn(&next[i])
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ch.setMessages(next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setMessages publishes a new transcript: the rows read byID, the list reads
|
|
|
|
|
// the ordered slice. Duplicates (a page overlapping the loaded tail) are
|
|
|
|
|
// dropped by id, the later copy winning.
|
|
|
|
|
func (ch *chat) setMessages(list []ocapi.Message) {
|
|
|
|
|
sort.SliceStable(list, func(i, j int) bool {
|
|
|
|
|
if list[i].Time.Created == list[j].Time.Created {
|
|
|
|
|
return list[i].ID < list[j].ID
|
|
|
|
|
}
|
|
|
|
|
return list[i].Time.Created < list[j].Time.Created
|
|
|
|
|
})
|
|
|
|
|
index := make(map[string]ocapi.Message, len(list))
|
|
|
|
|
out := list[:0]
|
|
|
|
|
for _, m := range list {
|
|
|
|
|
if _, ok := index[m.ID]; ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
index[m.ID] = m
|
|
|
|
|
out = append(out, m)
|
|
|
|
|
}
|
|
|
|
|
ch.msgs.Set(out)
|
|
|
|
|
ch.byID.Set(index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) toggle(key string) {
|
|
|
|
|
cur := ch.expanded.Peek()
|
|
|
|
|
next := make(map[string]bool, len(cur)+1)
|
|
|
|
|
for k, v := range cur {
|
|
|
|
|
next[k] = v
|
|
|
|
|
}
|
|
|
|
|
next[key] = !cur[key]
|
|
|
|
|
ch.expanded.Set(next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) close() {
|
|
|
|
|
ch.open = false
|
|
|
|
|
if ch.c.chat == ch {
|
|
|
|
|
ch.c.chat = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Rendering
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
func (ch *chat) screen() ui.Node {
|
|
|
|
|
signal.OnCleanup(ch.close)
|
|
|
|
|
ch.listNode = ui.List(ui.ListProps{
|
|
|
|
|
Appearance: ui.ListBare,
|
|
|
|
|
Anchor: ui.AnchorBottom,
|
|
|
|
|
OnStartReached: ch.loadOlder,
|
2026-11-17 21:52:06 +08:00
|
|
|
OnScroll: func(_, y float32) {
|
|
|
|
|
if y < ch.lastScroll-60 {
|
|
|
|
|
ch.jumpToLatest.Set(true)
|
|
|
|
|
}
|
|
|
|
|
ch.lastScroll = y
|
|
|
|
|
},
|
2026-11-16 09:09:09 +08:00
|
|
|
}, ch.items(), func(it chatItem) string { return it.Key }, ch.itemRow)
|
|
|
|
|
|
|
|
|
|
return ui.SafeArea(ui.Style{Grow: 1},
|
2026-11-17 21:52:06 +08:00
|
|
|
ui.View(ui.ViewProps{Style: 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.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)})
|
|
|
|
|
}),
|
|
|
|
|
ch.listNode,
|
|
|
|
|
ch.permissionSection(),
|
|
|
|
|
ch.composer(),
|
|
|
|
|
),
|
|
|
|
|
ui.Show(ch.jumpToLatest, func() ui.Node {
|
|
|
|
|
return ui.View(ui.ViewProps{
|
|
|
|
|
OnPress: func() {
|
|
|
|
|
ch.jumpToLatest.Set(false)
|
|
|
|
|
ui.ScrollToEnd(ch.listNode, true)
|
|
|
|
|
},
|
|
|
|
|
Label: ui.Val("回到最新"),
|
|
|
|
|
Style: ui.Style{Absolute: true, Right: ui.Pt(16), Bottom: ui.Pt(96),
|
|
|
|
|
Padding: ui.Symmetric(8, 12), CornerRadius: 999, Background: colors.SecondaryBackground},
|
|
|
|
|
}, ui.Row(ui.Style{Gap: 4, Align: ui.AlignCenter},
|
|
|
|
|
ui.Symbol("arrow.down", ui.Style{Font: fonts.Caption, Color: colors.SecondaryLabel}),
|
|
|
|
|
ui.StaticText("最新", ui.Style{Font: fonts.Caption, Color: colors.SecondaryLabel}),
|
|
|
|
|
))
|
2026-11-16 09:09:09 +08:00
|
|
|
}),
|
|
|
|
|
),
|
2026-11-16 18:34:32 +08:00
|
|
|
ch.footerSheet(),
|
2026-11-16 19:55:05 +08:00
|
|
|
ch.userSheet(),
|
2026-11-18 02:15:11 +08:00
|
|
|
ch.usageSheet(),
|
|
|
|
|
ch.previewSheet(),
|
2026-11-16 10:31:20 +08:00
|
|
|
ui.Sheet(ui.SheetProps{
|
|
|
|
|
Open: ch.pickerOpen,
|
|
|
|
|
Detents: ui.DetentLarge,
|
|
|
|
|
Grabber: true,
|
|
|
|
|
Content: ch.pickerSheet,
|
|
|
|
|
OnDismiss: func() { ch.pickerOpen.Set(false) },
|
|
|
|
|
}),
|
2026-11-16 09:09:09 +08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 10:31:20 +08:00
|
|
|
func (ch *chat) openModelPicker() {
|
|
|
|
|
ch.openPicker("model")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) openAgentPicker() {
|
|
|
|
|
ch.openPicker("agent")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) openPicker(kind string) {
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.pickerKind = kind
|
|
|
|
|
ch.pickerFilter.Set("")
|
|
|
|
|
ch.pickerItems.Set(nil)
|
|
|
|
|
ch.pickerLoad.Set(true)
|
|
|
|
|
ch.pickerOpen.Set(true)
|
|
|
|
|
go func() {
|
2026-11-16 20:04:42 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
2026-11-16 10:31:20 +08:00
|
|
|
defer cancel()
|
|
|
|
|
var items []pickerItem
|
|
|
|
|
var err error
|
|
|
|
|
switch kind {
|
|
|
|
|
case "model":
|
|
|
|
|
var models []ocapi.ModelInfo
|
|
|
|
|
models, err = client.Models(ctx, ch.dir)
|
|
|
|
|
for _, m := range models {
|
|
|
|
|
name := m.Name
|
|
|
|
|
if name == "" {
|
|
|
|
|
name = m.ID
|
|
|
|
|
}
|
|
|
|
|
items = append(items, pickerItem{
|
|
|
|
|
Key: m.ProviderID + "/" + m.ID,
|
|
|
|
|
Title: name,
|
|
|
|
|
Subtitle: m.ProviderID + " · " + m.ID,
|
|
|
|
|
Model: &ocapi.ModelRef{ID: m.ID, ProviderID: m.ProviderID, Variant: m.Variant},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
case "agent":
|
|
|
|
|
var agents []ocapi.AgentInfo
|
|
|
|
|
agents, err = client.Agents(ctx, ch.dir)
|
|
|
|
|
for _, a := range agents {
|
2026-11-16 13:21:20 +08:00
|
|
|
// A session runs a primary agent; subagents are what the
|
|
|
|
|
// primary one delegates to.
|
|
|
|
|
if a.Hidden || a.Mode == "subagent" {
|
2026-11-16 10:31:20 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
items = append(items, pickerItem{
|
|
|
|
|
Key: a.ID,
|
|
|
|
|
Title: a.Name,
|
|
|
|
|
Subtitle: a.Description,
|
|
|
|
|
Agent: a.ID,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
ch.pickerLoad.Set(false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ch.errText.Set(friendlyError(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.pickerItems.Set(items)
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) filteredPicks() signal.Accessor[[]pickerItem] {
|
|
|
|
|
return signal.Func(func() []pickerItem {
|
|
|
|
|
q := strings.ToLower(strings.TrimSpace(ch.pickerFilter.Get()))
|
|
|
|
|
items := ch.pickerItems.Get()
|
|
|
|
|
if q == "" {
|
|
|
|
|
return items
|
|
|
|
|
}
|
|
|
|
|
out := make([]pickerItem, 0, len(items))
|
|
|
|
|
for _, it := range items {
|
|
|
|
|
if strings.Contains(strings.ToLower(it.Title), q) ||
|
|
|
|
|
strings.Contains(strings.ToLower(it.Subtitle), q) {
|
|
|
|
|
out = append(out, it)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) pickerSheet() ui.Node {
|
|
|
|
|
title := "选择模型"
|
|
|
|
|
if ch.pickerKind == "agent" {
|
|
|
|
|
title = "选择 Agent"
|
|
|
|
|
}
|
|
|
|
|
return ui.SafeArea(ui.Style{Padding: ui.All(20), Gap: 12, Grow: 1},
|
|
|
|
|
ui.StaticText(title, ui.Style{Font: fonts.Title2.Weighted(fonts.Bold)}),
|
|
|
|
|
ui.TextField(ui.TextFieldProps{
|
|
|
|
|
Value: ch.pickerFilter,
|
|
|
|
|
Placeholder: ui.Val("搜索"),
|
|
|
|
|
Style: ui.Style{Grow: 0},
|
|
|
|
|
}),
|
|
|
|
|
ui.Show(ch.pickerLoad, func() ui.Node {
|
|
|
|
|
return ui.Activity()
|
|
|
|
|
}),
|
|
|
|
|
ui.List(ui.ListProps{Appearance: ui.ListPlain, Style: ui.Style{Grow: 1}},
|
|
|
|
|
ch.filteredPicks(), func(it pickerItem) string { return it.Key },
|
|
|
|
|
func(it signal.Accessor[pickerItem], _ signal.Accessor[int]) ui.Node {
|
|
|
|
|
return ui.View(ui.ViewProps{OnPress: func() { ch.applyPick(it.Get()) },
|
|
|
|
|
Style: ui.Style{Padding: ui.Symmetric(10, 4), Gap: 2}},
|
|
|
|
|
ui.Text(signal.Func(func() string { return it.Get().Title }), ui.Style{Font: fonts.Headline, Lines: 1}),
|
|
|
|
|
ui.Show(signal.Func(func() bool { return it.Get().Subtitle != "" }),
|
|
|
|
|
func() ui.Node {
|
|
|
|
|
return ui.Text(signal.Func(func() string { return it.Get().Subtitle }),
|
|
|
|
|
ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel, Lines: 2})
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) applyPick(it pickerItem) {
|
|
|
|
|
ch.pickerOpen.Set(false)
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
kind := ch.pickerKind
|
|
|
|
|
go func() {
|
2026-11-16 20:04:42 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
2026-11-16 10:31:20 +08:00
|
|
|
defer cancel()
|
|
|
|
|
var err error
|
|
|
|
|
if kind == "model" && it.Model != nil {
|
|
|
|
|
err = client.SwitchModel(ctx, ch.id, ch.dir, *it.Model)
|
|
|
|
|
} else if it.Agent != "" {
|
|
|
|
|
err = client.SwitchAgent(ctx, ch.id, ch.dir, it.Agent)
|
|
|
|
|
}
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
ch.errText.Set(friendlyError(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if kind == "model" && it.Model != nil {
|
|
|
|
|
ch.c.patchSession(ch.id, func(s *ocapi.Session) { s.Model = it.Model })
|
|
|
|
|
} else if it.Agent != "" {
|
|
|
|
|
ch.c.patchSession(ch.id, func(s *ocapi.Session) { s.Agent = it.Agent })
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 09:09:09 +08:00
|
|
|
// perms are the pending permission requests for this session.
|
2026-11-16 11:52:38 +08:00
|
|
|
func (ch *chat) perms() []ocapi.PermissionRequest {
|
|
|
|
|
var out []ocapi.PermissionRequest
|
|
|
|
|
for _, p := range ch.c.perms.Get() {
|
|
|
|
|
if p.SessionID == ch.id {
|
|
|
|
|
out = append(out, p)
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// syncPerms publishes this session's pending permission requests. The list
|
|
|
|
|
// and the flag are plain signals so the section updates without a computed
|
|
|
|
|
// chain (which is timing-sensitive in the test host).
|
|
|
|
|
func (ch *chat) syncPerms() {
|
|
|
|
|
list := ch.perms()
|
|
|
|
|
ch.permList.Set(list)
|
|
|
|
|
ch.hasPerms.Set(len(list) > 0)
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) permissionSection() ui.Node {
|
2026-11-16 11:52:38 +08:00
|
|
|
return ui.Show(ch.hasPerms, func() ui.Node {
|
2026-11-16 09:09:09 +08:00
|
|
|
return ui.Column(ui.Style{Gap: 6, Padding: ui.Symmetric(6, 12)},
|
2026-11-16 11:52:38 +08:00
|
|
|
ui.For(ch.permList, func(p ocapi.PermissionRequest) string { return p.ID },
|
2026-11-16 09:09:09 +08:00
|
|
|
func(p signal.Accessor[ocapi.PermissionRequest], _ signal.Accessor[int]) ui.Node {
|
|
|
|
|
return ui.View(ui.ViewProps{Style: ui.Style{Gap: 8, Padding: ui.Symmetric(10, 12),
|
|
|
|
|
CornerRadius: 12, Background: colors.SecondaryBackground}},
|
|
|
|
|
ui.Text(signal.Func(func() string { return "需要权限:" + p.Get().Action }),
|
|
|
|
|
ui.Style{Font: fonts.Footnote.Weighted(fonts.Semibold)}),
|
|
|
|
|
ui.Show(signal.Func(func() bool { return strings.Join(p.Get().Resources, ", ") != "" }),
|
|
|
|
|
func() ui.Node {
|
|
|
|
|
return ui.Text(signal.Func(func() string {
|
|
|
|
|
return strings.Join(p.Get().Resources, ", ")
|
|
|
|
|
}), ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel})
|
|
|
|
|
}),
|
|
|
|
|
ui.Row(ui.Style{Gap: 8, Wrap: true},
|
|
|
|
|
ui.Button(ui.ButtonProps{Title: ui.Val("允许一次"), Variant: ui.ButtonTinted,
|
|
|
|
|
OnPress: func() { ch.reply(p.Get(), ocapi.PermissionOnce) }}),
|
|
|
|
|
ui.Button(ui.ButtonProps{Title: ui.Val("总是允许"), Variant: ui.ButtonTinted,
|
|
|
|
|
OnPress: func() { ch.reply(p.Get(), ocapi.PermissionAlways) }}),
|
|
|
|
|
ui.Button(ui.ButtonProps{Title: ui.Val("拒绝"), Variant: ui.ButtonGray,
|
|
|
|
|
OnPress: func() { ch.reply(p.Get(), ocapi.PermissionReject) }}),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 20:04:42 +08:00
|
|
|
// 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)
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 09:09:09 +08:00
|
|
|
func (ch *chat) reply(req ocapi.PermissionRequest, decision ocapi.PermissionDecision) {
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
err := client.ReplyPermission(ctx, req.SessionID, req.ID, decision, "")
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
ch.errText.Set(friendlyError(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
list := ch.c.perms.Peek()
|
|
|
|
|
next := make([]ocapi.PermissionRequest, 0, len(list))
|
|
|
|
|
for _, p := range list {
|
|
|
|
|
if p.ID != req.ID {
|
|
|
|
|
next = append(next, p)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ch.c.perms.Set(next)
|
2026-11-16 11:52:38 +08:00
|
|
|
ch.syncPerms()
|
2026-11-16 09:09:09 +08:00
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) composer() ui.Node {
|
2026-11-17 21:52:06 +08:00
|
|
|
return ui.Column(ui.Style{Gap: 6, Padding: ui.Symmetric(8, 0)},
|
|
|
|
|
ch.attachmentRow(),
|
|
|
|
|
ui.Row(ui.Style{Gap: 8, Align: ui.AlignEnd, Padding: ui.Symmetric(0, 12)},
|
|
|
|
|
ui.Show(ch.attaching,
|
|
|
|
|
func() ui.Node { return ui.Activity() },
|
|
|
|
|
func() ui.Node {
|
|
|
|
|
return ui.Button(ui.ButtonProps{Icon: "paperclip", Label: ui.Val("添加附件"),
|
|
|
|
|
OnPress: ch.attachMenu})
|
|
|
|
|
}),
|
|
|
|
|
ui.TextField(ui.TextFieldProps{
|
|
|
|
|
Value: ch.draft,
|
|
|
|
|
Placeholder: ui.Val("发消息…"),
|
|
|
|
|
Multiline: true,
|
|
|
|
|
Style: ui.Style{Grow: 1, Lines: 5},
|
2026-11-16 09:09:09 +08:00
|
|
|
}),
|
2026-11-18 02:15:11 +08:00
|
|
|
ch.usageButton(),
|
2026-11-17 21:52:06 +08:00
|
|
|
ui.Show(ch.running,
|
|
|
|
|
func() ui.Node {
|
|
|
|
|
return ui.Button(ui.ButtonProps{
|
|
|
|
|
Title: ui.Val("停止"),
|
|
|
|
|
Variant: ui.ButtonGray,
|
|
|
|
|
OnPress: ch.stop,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
func() ui.Node {
|
|
|
|
|
return ui.Button(ui.ButtonProps{
|
|
|
|
|
Icon: ui.IconSend,
|
|
|
|
|
Label: ui.Val("发送"),
|
|
|
|
|
Variant: ui.ButtonFilled,
|
|
|
|
|
Disabled: signal.Func(func() bool {
|
|
|
|
|
return strings.TrimSpace(ch.draft.Get()) == "" && len(ch.attachments.Get()) == 0
|
|
|
|
|
}),
|
|
|
|
|
OnPress: ch.send,
|
|
|
|
|
})
|
|
|
|
|
}),
|
|
|
|
|
),
|
2026-11-16 09:09:09 +08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// items derives the list's rows: one per message, or per assistant part.
|
|
|
|
|
func (ch *chat) items() signal.Accessor[[]chatItem] {
|
|
|
|
|
return signal.Func(func() []chatItem {
|
|
|
|
|
msgs := ch.msgs.Get()
|
|
|
|
|
out := make([]chatItem, 0, len(msgs))
|
2026-11-16 18:05:15 +08:00
|
|
|
for msgIdx, m := range msgs {
|
2026-11-16 09:09:09 +08:00
|
|
|
switch m.Type {
|
|
|
|
|
case "user":
|
|
|
|
|
out = append(out, chatItem{Key: "u/" + m.ID, Kind: "user", MsgID: m.ID})
|
2026-11-16 19:55:05 +08:00
|
|
|
out = append(out, chatItem{Key: "ua/" + m.ID, Kind: "useractions", MsgID: m.ID})
|
2026-11-16 09:09:09 +08:00
|
|
|
case "assistant":
|
2026-11-16 18:05:15 +08:00
|
|
|
if len(m.Content) == 0 && m.Time.Completed == 0 {
|
|
|
|
|
out = append(out, chatItem{Key: "a/" + m.ID, Kind: "thinking", MsgID: m.ID})
|
2026-11-16 09:09:09 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for i, p := range m.Content {
|
|
|
|
|
kind := p.Type
|
|
|
|
|
switch kind {
|
|
|
|
|
case "text", "reasoning":
|
|
|
|
|
case "tool":
|
|
|
|
|
default:
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
out = append(out, chatItem{
|
|
|
|
|
Key: fmt.Sprintf("a/%s/%s/%d", m.ID, kind, i),
|
|
|
|
|
Kind: kind,
|
|
|
|
|
MsgID: m.ID,
|
|
|
|
|
Part: i,
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-11-16 18:05:15 +08:00
|
|
|
if m.Time.Completed > 0 && turnEndsAt(msgs, msgIdx) {
|
|
|
|
|
out = append(out, chatItem{Key: "f/" + m.ID, Kind: "footer", MsgID: m.ID})
|
|
|
|
|
}
|
2026-11-16 09:09:09 +08:00
|
|
|
case "shell":
|
|
|
|
|
out = append(out, chatItem{Key: "s/" + m.ID, Kind: "shell", MsgID: m.ID})
|
|
|
|
|
case "system", "synthetic", "skill", "compaction":
|
|
|
|
|
if strings.TrimSpace(m.Text) != "" {
|
|
|
|
|
out = append(out, chatItem{Key: "n/" + m.ID, Kind: "note", MsgID: m.ID})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) itemRow(it signal.Accessor[chatItem], _ signal.Accessor[int]) ui.Node {
|
|
|
|
|
// The row's structure follows the item's kind, which changes only when
|
|
|
|
|
// a recycled cell is bound to another item.
|
|
|
|
|
return ui.Dynamic(func() ui.Node {
|
|
|
|
|
cur := it.Get()
|
|
|
|
|
switch cur.Kind {
|
|
|
|
|
case "user":
|
|
|
|
|
return ui.Row(ui.Style{Justify: ui.JustifyEnd, Padding: ui.Symmetric(4, 12)},
|
2026-11-17 21:52:06 +08:00
|
|
|
ui.View(ui.ViewProps{Style: ui.Style{Padding: ui.Symmetric(8, 12), CornerRadius: 16, Gap: 4,
|
2026-11-16 09:09:09 +08:00
|
|
|
MaxWidth: ui.Pct(80), Background: colors.Accent}},
|
|
|
|
|
ui.Text(ch.textOf(cur.MsgID), ui.Style{Color: colors.OnAccent}),
|
2026-11-17 23:46:06 +08:00
|
|
|
ui.For(ch.imagesFor(cur.MsgID), func(p string) string { return p },
|
|
|
|
|
func(p signal.Accessor[string], _ signal.Accessor[int]) ui.Node {
|
|
|
|
|
return ui.Image(ui.ImageProps{
|
|
|
|
|
Source: p,
|
|
|
|
|
Mode: ui.ImageFill,
|
|
|
|
|
Style: ui.Style{Width: ui.Pt(200), Height: ui.Pt(150),
|
|
|
|
|
CornerRadius: 10, Clip: true, MaxWidth: ui.Pct(100)},
|
|
|
|
|
})
|
|
|
|
|
}),
|
2026-11-17 21:52:06 +08:00
|
|
|
ui.Show(signal.Func(func() bool { return len(ch.byID.Get()[cur.MsgID].Files) > 0 }),
|
|
|
|
|
func() ui.Node {
|
|
|
|
|
return ui.Text(ch.filesText(cur.MsgID),
|
|
|
|
|
ui.Style{Color: colors.OnAccent, Font: fonts.Caption})
|
|
|
|
|
}),
|
2026-11-16 09:09:09 +08:00
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
case "thinking":
|
|
|
|
|
return ui.View(ui.ViewProps{Style: ui.Style{Padding: ui.Symmetric(6, 14)}},
|
|
|
|
|
ui.StaticText("正在思考…", ui.Style{Color: colors.SecondaryLabel, Font: fonts.Footnote}),
|
|
|
|
|
)
|
|
|
|
|
case "text":
|
|
|
|
|
return ui.View(ui.ViewProps{Style: ui.Style{Padding: ui.Symmetric(6, 14)}},
|
|
|
|
|
markdown.View(markdown.Props{
|
|
|
|
|
Text: ch.partText(cur.MsgID, cur.Part),
|
|
|
|
|
Streaming: ch.streaming(cur.MsgID),
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
case "reasoning":
|
|
|
|
|
return ch.reasoningRow(cur)
|
|
|
|
|
case "tool":
|
|
|
|
|
return ch.toolRow(cur)
|
2026-11-16 18:05:15 +08:00
|
|
|
case "footer":
|
|
|
|
|
return ch.footerRow(cur)
|
2026-11-16 19:55:05 +08:00
|
|
|
case "useractions":
|
|
|
|
|
return ch.userActionsRow(cur)
|
2026-11-16 09:09:09 +08:00
|
|
|
case "shell":
|
|
|
|
|
return ui.View(ui.ViewProps{Style: ui.Style{Padding: ui.Symmetric(4, 14)}},
|
|
|
|
|
ui.Text(ch.shellText(cur.MsgID), ui.Style{Font: codeFont(), Color: colors.SecondaryLabel}),
|
|
|
|
|
)
|
|
|
|
|
case "note":
|
|
|
|
|
return ui.View(ui.ViewProps{Style: ui.Style{Padding: ui.Symmetric(4, 14)}},
|
|
|
|
|
ui.Text(ch.textOf(cur.MsgID), ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return ui.View(ui.ViewProps{})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 18:05:15 +08:00
|
|
|
// turnEndsAt reports whether the assistant message at i ends its turn: no
|
|
|
|
|
// later assistant message before the next prompt.
|
|
|
|
|
func turnEndsAt(msgs []ocapi.Message, i int) bool {
|
|
|
|
|
for j := i + 1; j < len(msgs); j++ {
|
|
|
|
|
switch msgs[j].Type {
|
|
|
|
|
case "assistant":
|
|
|
|
|
return false
|
|
|
|
|
case "user", "synthetic":
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// footerRow is the line under a finished turn: the model, the agent, how
|
|
|
|
|
// long the turn took, and a menu of actions, as the first-party client
|
|
|
|
|
// shows on touch screens.
|
|
|
|
|
func (ch *chat) footerRow(cur chatItem) ui.Node {
|
|
|
|
|
facts := signal.Func(func() string {
|
|
|
|
|
m := ch.byID.Get()[cur.MsgID]
|
|
|
|
|
parts := make([]string, 0, 3)
|
|
|
|
|
if name := ch.modelName(m.Model); name != "" {
|
|
|
|
|
parts = append(parts, name)
|
|
|
|
|
}
|
|
|
|
|
if m.Agent != "" {
|
|
|
|
|
parts = append(parts, m.Agent)
|
|
|
|
|
}
|
|
|
|
|
if d := ch.turnDuration(cur.MsgID); d != "" {
|
|
|
|
|
parts = append(parts, d)
|
|
|
|
|
}
|
|
|
|
|
return strings.Join(parts, " · ")
|
|
|
|
|
})
|
|
|
|
|
return ui.Row(ui.Style{Padding: ui.Symmetric(6, 14), Gap: 6, Align: ui.AlignCenter},
|
2026-11-16 19:55:05 +08:00
|
|
|
svg.View(svg.Props{Icon: sparklesIcon, Color: ui.Val(colors.SecondaryLabel),
|
|
|
|
|
Style: ui.Style{Width: ui.Pt(14), Height: ui.Pt(14)}}),
|
2026-11-16 18:05:15 +08:00
|
|
|
ui.Text(facts, ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel, Lines: 1, Grow: 1}),
|
|
|
|
|
ui.Button(ui.ButtonProps{Icon: ui.IconMore, Label: ui.Val("更多操作"),
|
|
|
|
|
OnPress: func() { ch.footerMenu(cur.MsgID) }}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 19:55:05 +08:00
|
|
|
// userActionsRow is the line under a prompt: its time, copy, and a menu,
|
|
|
|
|
// as the first-party client shows.
|
|
|
|
|
func (ch *chat) userActionsRow(cur chatItem) ui.Node {
|
|
|
|
|
stamp := signal.Func(func() string {
|
|
|
|
|
m := ch.byID.Get()[cur.MsgID]
|
|
|
|
|
if m.Time.Created <= 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return time.UnixMilli(m.Time.Created).Format("15:04")
|
|
|
|
|
})
|
|
|
|
|
return ui.Row(ui.Style{Padding: ui.Symmetric(2, 12), Gap: 10, Justify: ui.JustifyEnd, Align: ui.AlignCenter},
|
|
|
|
|
ui.Row(ui.Style{Gap: 4, Align: ui.AlignCenter},
|
|
|
|
|
ui.Symbol(ui.Icon("clock"), ui.Style{Font: fonts.Caption, Color: colors.SecondaryLabel}),
|
|
|
|
|
ui.Text(stamp, ui.Style{Font: fonts.Caption, Color: colors.SecondaryLabel}),
|
|
|
|
|
),
|
|
|
|
|
ui.Button(ui.ButtonProps{Icon: "doc.on.doc", Label: ui.Val("复制消息"),
|
|
|
|
|
OnPress: func() { ch.copyUserMessage(cur.MsgID) }}),
|
|
|
|
|
ui.Button(ui.ButtonProps{Icon: ui.IconMore, Label: ui.Val("消息操作"),
|
|
|
|
|
OnPress: func() { ch.userActionsMenu(cur.MsgID) }}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) copyUserMessage(id string) {
|
|
|
|
|
m := ch.byID.Peek()[id]
|
|
|
|
|
text := strings.TrimSpace(m.Text)
|
|
|
|
|
if text == "" {
|
|
|
|
|
ui.Snackbar(ui.SnackbarProps{Message: "没有可复制的文字"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ui.Copy(text)
|
|
|
|
|
ui.Snackbar(ui.SnackbarProps{Message: "已复制消息"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) userActionsMenu(id string) {
|
|
|
|
|
ch.userID.Set(id)
|
|
|
|
|
ch.userOpen.Set(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) userSheet() ui.Node {
|
|
|
|
|
return ui.Sheet(ui.SheetProps{
|
|
|
|
|
Open: ch.userOpen,
|
|
|
|
|
Detents: ui.DetentMedium,
|
|
|
|
|
Grabber: true,
|
|
|
|
|
OnDismiss: func() { ch.userOpen.Set(false) },
|
|
|
|
|
Content: func() ui.Node {
|
|
|
|
|
id := ch.userID.Peek()
|
|
|
|
|
m := ch.byID.Peek()[id]
|
|
|
|
|
stamp := ""
|
|
|
|
|
if m.Time.Created > 0 {
|
|
|
|
|
stamp = time.UnixMilli(m.Time.Created).Format("2006-01-02 15:04")
|
|
|
|
|
}
|
|
|
|
|
act := func(f func()) func() {
|
|
|
|
|
return func() {
|
|
|
|
|
ch.userOpen.Set(false)
|
|
|
|
|
f()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ui.SafeArea(ui.Style{Padding: ui.All(20), Gap: 4},
|
|
|
|
|
ui.StaticText("更多操作", ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
|
|
|
|
ui.Show(signal.Func(func() bool { return stamp != "" }), func() ui.Node {
|
|
|
|
|
return ui.Row(ui.Style{Padding: ui.Symmetric(8, 0), Gap: 8, Align: ui.AlignCenter},
|
|
|
|
|
ui.Symbol(ui.Icon("clock"), ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
|
|
|
|
ui.StaticText(stamp, ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
|
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
sheetAction("复制消息", "doc.on.doc", act(func() { ch.copyUserMessage(id) })),
|
2026-11-17 23:46:06 +08:00
|
|
|
sheetAction("撤回此消息及之后", "", act(func() { ch.revertFrom(id) })),
|
2026-11-16 19:55:05 +08:00
|
|
|
sheetAction("从这里分叉", "", act(func() { ch.forkBefore(id) })),
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 23:46:06 +08:00
|
|
|
// revertFrom stages and commits a revert at a prompt, dropping that
|
|
|
|
|
// message and everything after it.
|
|
|
|
|
func (ch *chat) revertFrom(id string) {
|
|
|
|
|
ui.Alert(ui.AlertProps{
|
|
|
|
|
Title: "撤回消息?",
|
|
|
|
|
Message: "这条消息及其之后的内容会从会话中删除,且无法恢复。",
|
|
|
|
|
Actions: []ui.Action{
|
|
|
|
|
{Title: ui.Val("取消"), Role: ui.ActionCancel},
|
|
|
|
|
{Title: ui.Val("撤回"), Role: ui.ActionDestructive,
|
|
|
|
|
OnPress: func() { ch.doRevert(id) }},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) doRevert(id string) {
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.errText.Set("")
|
|
|
|
|
go func() {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
if err := client.StageRevert(ctx, ch.id, ch.dir, id); err != nil {
|
|
|
|
|
ui.Post(func() { ch.errText.Set(friendlyError(err)) })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := client.CommitRevert(ctx, ch.id, ch.dir); err != nil {
|
|
|
|
|
ui.Post(func() { ch.errText.Set(friendlyError(err)) })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
ch.reloadTail()
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 19:55:05 +08:00
|
|
|
// forkBefore copies the transcript before a message (a prompt) into a new
|
|
|
|
|
// session.
|
|
|
|
|
func (ch *chat) forkBefore(id string) {
|
|
|
|
|
client := ch.client()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
s, err := client.ForkSession(ctx, ch.id, ch.dir, id)
|
|
|
|
|
ui.Post(func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
ch.errText.Set(friendlyError(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ch.c.sessions.Update(func(list []ocapi.Session) []ocapi.Session {
|
|
|
|
|
next := make([]ocapi.Session, 0, len(list)+1)
|
|
|
|
|
next = append(next, s)
|
|
|
|
|
return append(next, list...)
|
|
|
|
|
})
|
|
|
|
|
if ch.nav != nil {
|
|
|
|
|
nc := ch.c.openChat(&s, ch.nav)
|
|
|
|
|
ch.nav.Push(nc.screen, ui.ScreenOptions{Title: nc.title, Actions: chatActions(nc)})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 09:09:09 +08:00
|
|
|
func (ch *chat) reasoningRow(cur chatItem) ui.Node {
|
|
|
|
|
key := fmt.Sprintf("reason/%s/%d", cur.MsgID, cur.Part)
|
|
|
|
|
open := signal.Func(func() bool { return ch.expanded.Get()[key] })
|
|
|
|
|
label := signal.Func(func() string {
|
|
|
|
|
n := len([]rune(ch.partTextValue(cur.MsgID, cur.Part)))
|
|
|
|
|
head := fmt.Sprintf("思考过程(%d 字)", n)
|
|
|
|
|
if open.Get() {
|
|
|
|
|
return "▾ " + head
|
|
|
|
|
}
|
|
|
|
|
return "▸ " + head
|
|
|
|
|
})
|
|
|
|
|
return ui.Column(ui.Style{Gap: 4, Padding: ui.Symmetric(4, 14)},
|
|
|
|
|
ui.View(ui.ViewProps{OnPress: func() { ch.toggle(key) }},
|
|
|
|
|
ui.Text(label, ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
|
|
|
|
),
|
|
|
|
|
ui.Show(open, func() ui.Node {
|
|
|
|
|
return ui.Text(ch.partText(cur.MsgID, cur.Part),
|
|
|
|
|
ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel})
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) toolRow(cur chatItem) ui.Node {
|
|
|
|
|
key := fmt.Sprintf("tool/%s/%d", cur.MsgID, cur.Part)
|
|
|
|
|
open := signal.Func(func() bool { return ch.expanded.Get()[key] })
|
|
|
|
|
part := ch.partAccessor(cur.MsgID, cur.Part)
|
|
|
|
|
status := signal.Func(func() string {
|
|
|
|
|
p := part.Get()
|
|
|
|
|
if p.State == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return p.State.Status
|
|
|
|
|
})
|
|
|
|
|
headline := signal.Func(func() string {
|
|
|
|
|
p := part.Get()
|
|
|
|
|
icon := "●"
|
|
|
|
|
switch status.Get() {
|
|
|
|
|
case "running", "streaming":
|
|
|
|
|
icon = "◌"
|
|
|
|
|
case "completed":
|
|
|
|
|
icon = "✓"
|
|
|
|
|
case "error":
|
|
|
|
|
icon = "✕"
|
|
|
|
|
}
|
|
|
|
|
mark := "▸"
|
|
|
|
|
if open.Get() {
|
|
|
|
|
mark = "▾"
|
|
|
|
|
}
|
|
|
|
|
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)},
|
2026-11-16 20:04:42 +08:00
|
|
|
ui.View(ui.ViewProps{OnPress: func() {
|
|
|
|
|
ch.toggle(key)
|
|
|
|
|
if ch.expanded.Peek()[key] {
|
|
|
|
|
ch.ensureToolLoaded(cur.MsgID)
|
|
|
|
|
}
|
|
|
|
|
}},
|
2026-11-16 09:09:09 +08:00
|
|
|
ui.Text(headline, ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel, Lines: 2}),
|
|
|
|
|
),
|
2026-11-18 02:15:11 +08:00
|
|
|
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()) })
|
|
|
|
|
}),
|
2026-11-16 09:09:09 +08:00
|
|
|
ui.Show(open, func() ui.Node {
|
|
|
|
|
return ui.View(ui.ViewProps{Style: ui.Style{Padding: ui.Symmetric(8, 10), CornerRadius: 8,
|
|
|
|
|
Background: colors.SecondaryBackground, Gap: 8}},
|
|
|
|
|
ui.Text(signal.Func(func() string {
|
|
|
|
|
p := part.Get()
|
|
|
|
|
if p.State == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
buf, _ := json.MarshalIndent(p.State.Input, "", " ")
|
|
|
|
|
return string(buf)
|
|
|
|
|
}), ui.Style{Font: codeFont()}),
|
|
|
|
|
ui.Text(signal.Func(func() string { return toolOutput(part.Get()) }), ui.Style{Font: codeFont()}),
|
|
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Value helpers
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
func (ch *chat) msg(id string) ocapi.Message { return ch.byID.Get()[id] }
|
|
|
|
|
|
2026-11-17 23:46:06 +08:00
|
|
|
func (ch *chat) imagesFor(id string) signal.Accessor[[]string] {
|
|
|
|
|
return signal.Func(func() []string { return ch.imagePaths[id] })
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 21:52:06 +08:00
|
|
|
func (ch *chat) filesText(id string) signal.Accessor[string] {
|
|
|
|
|
return signal.Func(func() string {
|
|
|
|
|
m := ch.byID.Get()[id]
|
|
|
|
|
names := make([]string, 0, len(m.Files))
|
|
|
|
|
for _, f := range m.Files {
|
|
|
|
|
if f.Name != "" {
|
|
|
|
|
names = append(names, "📎 "+f.Name)
|
|
|
|
|
} else {
|
|
|
|
|
names = append(names, "📎 附件")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return strings.Join(names, "\n")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 09:09:09 +08:00
|
|
|
func (ch *chat) textOf(id string) signal.Accessor[string] {
|
|
|
|
|
return signal.Func(func() string { return ch.byID.Get()[id].Text })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) partAccessor(id string, index int) signal.Accessor[ocapi.ContentPart] {
|
|
|
|
|
return signal.Func(func() ocapi.ContentPart {
|
|
|
|
|
m := ch.byID.Get()[id]
|
|
|
|
|
if index < 0 || index >= len(m.Content) {
|
|
|
|
|
return ocapi.ContentPart{}
|
|
|
|
|
}
|
|
|
|
|
return m.Content[index]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) partText(id string, index int) signal.Accessor[string] {
|
|
|
|
|
return signal.Func(func() string { return ch.partTextValue(id, index) })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) partTextValue(id string, index int) string {
|
|
|
|
|
m := ch.byID.Get()[id]
|
|
|
|
|
if index < 0 || index >= len(m.Content) {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return m.Content[index].Text
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) streaming(id string) signal.Accessor[bool] {
|
|
|
|
|
return signal.Func(func() bool {
|
|
|
|
|
m := ch.byID.Get()[id]
|
|
|
|
|
return m.Time.Completed == 0
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ch *chat) shellText(id string) signal.Accessor[string] {
|
|
|
|
|
return signal.Func(func() string {
|
|
|
|
|
m := ch.byID.Get()[id]
|
|
|
|
|
out := "$ " + m.Command
|
|
|
|
|
if strings.TrimSpace(m.Output) != "" {
|
|
|
|
|
out += "\n" + m.Output
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toolSummary(p ocapi.ContentPart) string {
|
|
|
|
|
if p.State == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
for _, k := range []string{"command", "filePath", "pattern", "path", "description", "query"} {
|
|
|
|
|
if v, ok := p.State.Input[k].(string); ok && strings.TrimSpace(v) != "" {
|
|
|
|
|
return firstLine(v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toolOutput(p ocapi.ContentPart) string {
|
|
|
|
|
if p.State == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
var b strings.Builder
|
|
|
|
|
for _, c := range p.State.Content {
|
|
|
|
|
if c.Type == "text" && c.Text != "" {
|
|
|
|
|
if b.Len() > 0 {
|
|
|
|
|
b.WriteByte('\n')
|
|
|
|
|
}
|
|
|
|
|
b.WriteString(c.Text)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out := b.String()
|
|
|
|
|
if len(out) > 4000 {
|
|
|
|
|
out = out[:4000] + "\n…"
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func firstLine(s string) string {
|
|
|
|
|
if i := strings.IndexByte(s, '\n'); i >= 0 {
|
|
|
|
|
return s[:i]
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func codeFont() fonts.Font { return fonts.Custom("monospace", fonts.Footnote) }
|
|
|
|
|
|
|
|
|
|
func ascending(list []ocapi.Message) []ocapi.Message {
|
|
|
|
|
out := make([]ocapi.Message, len(list))
|
|
|
|
|
for i, m := range list {
|
|
|
|
|
out[len(list)-1-i] = m
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newMessageID() string {
|
|
|
|
|
var b [8]byte
|
|
|
|
|
_, _ = rand.Read(b[:])
|
|
|
|
|
return "msg_" + fmt.Sprint(time.Now().UnixMilli()) + hex.EncodeToString(b[:])
|
|
|
|
|
}
|