Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
345 lines
8.7 KiB
Go
345 lines
8.7 KiB
Go
package appui
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/egoist/mygo"
|
|
"github.com/egoist/mygo/ui"
|
|
"github.com/egoist/mygo/ui/mobile"
|
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
|
"github.com/openchamber/openchamber-native/internal/tts"
|
|
)
|
|
|
|
// sheets draws the chat's overlays: menus, pickers and previews.
|
|
func (ch *chat) sheets(c *ui.Context) {
|
|
ch.footerSheet(c)
|
|
ch.userSheet(c)
|
|
ch.pickerSheet(c)
|
|
ch.usageSheet(c)
|
|
ch.previewSheet(c)
|
|
ch.attachSheet(c)
|
|
ch.revertConfirm(c)
|
|
ch.a.sessionActionSheet(c)
|
|
ch.a.newSessionSheet(c)
|
|
ch.a.serverActionSheet(c)
|
|
ch.a.deleteConfirmSheet(c)
|
|
ch.a.renameSheet(c)
|
|
ch.a.sessionDeleteConfirm(c)
|
|
}
|
|
|
|
// openFooter opens the actions of the turn that finished last.
|
|
func (ch *chat) openFooter() {
|
|
for i := len(ch.msgs) - 1; i >= 0; i-- {
|
|
if ch.msgs[i].Type == "assistant" && ch.msgs[i].Time.Completed > 0 {
|
|
ch.footerOpen = true
|
|
ch.footerID = ch.msgs[i].ID
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (ch *chat) footerSheet(c *ui.Context) {
|
|
if !ch.footerOpen {
|
|
return
|
|
}
|
|
mobile.ActionSheet(c, &ch.footerOpen, mobile.ActionSheetOptions{Title: "更多操作", Cancel: "取消"},
|
|
mobile.Action{Label: "朗读回答", OnTap: func() { ch.speakAnswer(ch.footerID) }},
|
|
mobile.Action{Label: "停止朗读", OnTap: func() { _ = tts.Stop() }},
|
|
mobile.Action{Label: "复制回答", OnTap: func() { ch.copyAnswer(ch.footerID) }},
|
|
mobile.Action{Label: "从这里分叉", OnTap: func() { ch.forkFrom(ch.footerID) }},
|
|
)
|
|
}
|
|
|
|
func (ch *chat) userSheet(c *ui.Context) {
|
|
if !ch.userOpen {
|
|
return
|
|
}
|
|
mobile.ActionSheet(c, &ch.userOpen, mobile.ActionSheetOptions{Title: "消息操作", Cancel: "取消"},
|
|
mobile.Action{Label: "复制消息", OnTap: func() { ch.copyUserMessage(ch.userID) }},
|
|
mobile.Action{Label: "撤回此消息及之后", OnTap: func() { ch.askRevert(ch.userID) }},
|
|
mobile.Action{Label: "从这里分叉", OnTap: func() { ch.forkBefore(ch.userID) }},
|
|
)
|
|
}
|
|
|
|
func (ch *chat) copyAnswer(id string) {
|
|
m := ch.byID[id]
|
|
parts := make([]string, 0, len(m.Content))
|
|
for _, p := range m.Content {
|
|
if p.Type == "text" && p.Text != "" {
|
|
parts = append(parts, p.Text)
|
|
}
|
|
}
|
|
text := strings.Join(parts, "\n")
|
|
if text == "" {
|
|
text = m.Text
|
|
}
|
|
if text == "" {
|
|
ch.a.chatPost(func() { ch.errText = "没有可复制的文字" })
|
|
return
|
|
}
|
|
mygo.Clipboard.WriteText(text)
|
|
}
|
|
|
|
func (ch *chat) copyUserMessage(id string) {
|
|
m := ch.byID[id]
|
|
text := strings.TrimSpace(m.Text)
|
|
if text == "" {
|
|
ch.a.chatPost(func() { ch.errText = "没有可复制的文字" })
|
|
return
|
|
}
|
|
mygo.Clipboard.WriteText(text)
|
|
}
|
|
|
|
// ------------------------------------------------------------ read aloud
|
|
|
|
func (ch *chat) speakAnswer(id string) {
|
|
text := ""
|
|
m := ch.byID[id]
|
|
parts := make([]string, 0, len(m.Content))
|
|
for _, p := range m.Content {
|
|
if p.Type == "text" && p.Text != "" {
|
|
parts = append(parts, p.Text)
|
|
}
|
|
}
|
|
text = strings.Join(parts, " ")
|
|
if text == "" {
|
|
text = m.Text
|
|
}
|
|
if strings.TrimSpace(text) == "" {
|
|
return
|
|
}
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
defer cancel()
|
|
if err := tts.Speak(ctx, text); err != nil {
|
|
ch.a.chatPost(func() { ch.errText = "朗读不可用:" + friendlyError(err) })
|
|
}
|
|
}()
|
|
}
|
|
|
|
// ------------------------------------------------------------ fork / revert
|
|
|
|
func (ch *chat) forkFrom(id string) {
|
|
ch.fork(false, id)
|
|
}
|
|
|
|
func (ch *chat) forkBefore(id string) {
|
|
ch.fork(true, id)
|
|
}
|
|
|
|
func (ch *chat) fork(beforeMsg bool, id string) {
|
|
before := ""
|
|
if beforeMsg {
|
|
before = id
|
|
}
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
sess, err := ch.a.client.ForkSession(ctx, ch.id, ch.dir, before)
|
|
ch.a.chatPost(func() {
|
|
if err != nil {
|
|
ch.errText = friendlyError(err)
|
|
return
|
|
}
|
|
ch.a.sessions = append([]ocapi.Session{sess}, ch.a.sessions...)
|
|
ch.a.openChat(sess)
|
|
})
|
|
}()
|
|
}
|
|
|
|
func (ch *chat) askRevert(id string) {
|
|
ch.revertID = id
|
|
ch.confirmRevert = true
|
|
}
|
|
|
|
func (ch *chat) revertConfirm(c *ui.Context) {
|
|
if !ch.confirmRevert {
|
|
return
|
|
}
|
|
buttons := ui.AlertDialog(c, &ch.confirmRevert, "撤回消息?",
|
|
"这条消息及其之后的内容会从会话中删除,且无法恢复。", "取消", "撤回")
|
|
if buttons == 1 {
|
|
ch.doRevert(ch.revertID)
|
|
}
|
|
}
|
|
|
|
func (ch *chat) doRevert(id string) {
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
|
|
defer cancel()
|
|
client := ch.a.client
|
|
if client.StageRevert(ctx, ch.id, ch.dir, id) != nil {
|
|
ch.a.chatPost(func() { ch.errText = "无法撤回" })
|
|
return
|
|
}
|
|
if err := client.CommitRevert(ctx, ch.id, ch.dir); err != nil {
|
|
ch.a.chatPost(func() { ch.errText = friendlyError(err) })
|
|
return
|
|
}
|
|
ch.a.chatPost(func() { ch.reloadTail() })
|
|
}()
|
|
}
|
|
|
|
// ------------------------------------------------------------ model / agent
|
|
|
|
func (ch *chat) openPicker(kind string) {
|
|
ch.pickerKind = kind
|
|
ch.pickerFilter = ""
|
|
ch.pickerItems = nil
|
|
ch.pickerOpen = true
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
var items []pickerItem
|
|
var err error
|
|
switch kind {
|
|
case "model":
|
|
var models []ocapi.ModelInfo
|
|
models, err = ch.a.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,
|
|
Model: &ocapi.ModelRef{ID: m.ID, ProviderID: m.ProviderID},
|
|
})
|
|
}
|
|
case "agent":
|
|
var agents []ocapi.AgentInfo
|
|
agents, err = ch.a.client.Agents(ctx, ch.dir)
|
|
for _, ag := range agents {
|
|
if ag.Mode != "primary" {
|
|
continue
|
|
}
|
|
items = append(items, pickerItem{Key: "agent/" + ag.ID, Title: ag.Name, Agent: ag.ID})
|
|
}
|
|
}
|
|
ch.a.chatPost(func() {
|
|
if err != nil {
|
|
ch.errText = friendlyError(err)
|
|
return
|
|
}
|
|
ch.pickerItems = items
|
|
})
|
|
}()
|
|
}
|
|
|
|
func (ch *chat) pickerSheet(c *ui.Context) {
|
|
if !ch.pickerOpen {
|
|
return
|
|
}
|
|
mobile.Sheet(c, &ch.pickerOpen, mobile.SheetOptions{Detents: []float32{0.85}}, func() {
|
|
ui.Column(c).FillWidth().Padding(8, 16, 24, 16).Gap(8).Children(func() {
|
|
title := "选择模型"
|
|
if ch.pickerKind == "agent" {
|
|
title = "选择智能体"
|
|
}
|
|
ui.Text(c, title).FontSize(20).Bold()
|
|
ui.TextInput(c, &ch.pickerFilter).Placeholder("搜索").PaddingY(4)
|
|
ui.Scroll(c).Grow(1).Children(func() {
|
|
ui.Column(c).FillWidth().Gap(2).Children(func() {
|
|
filter := strings.ToLower(ch.pickerFilter)
|
|
for _, it := range ch.pickerItems {
|
|
if filter != "" && !strings.Contains(strings.ToLower(it.Title), filter) {
|
|
continue
|
|
}
|
|
row := mobile.ListRow(c, mobile.RowOptions{Title: it.Title})
|
|
if row.Clicked() {
|
|
ch.pickerOpen = false
|
|
ch.selectPickerItem(it)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
func (ch *chat) selectPickerItem(it pickerItem) {
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
var err error
|
|
switch ch.pickerKind {
|
|
case "model":
|
|
err = ch.a.client.SwitchModel(ctx, ch.id, ch.dir, *it.Model)
|
|
case "agent":
|
|
err = ch.a.client.SwitchAgent(ctx, ch.id, ch.dir, it.Agent)
|
|
}
|
|
ch.a.chatPost(func() {
|
|
if err != nil {
|
|
ch.errText = friendlyError(err)
|
|
}
|
|
})
|
|
}()
|
|
}
|
|
|
|
// ------------------------------------------------------------ attachments
|
|
|
|
func (ch *chat) attachSheet(c *ui.Context) {
|
|
if !ch.attachOpen {
|
|
return
|
|
}
|
|
mobile.ActionSheet(c, &ch.attachOpen, mobile.ActionSheetOptions{Title: "添加附件", Cancel: "取消"},
|
|
mobile.Action{Label: "照片", OnTap: func() { ch.pickFiles(true) }},
|
|
mobile.Action{Label: "文件", OnTap: func() { ch.pickFiles(false) }},
|
|
)
|
|
}
|
|
|
|
func (ch *chat) pickFiles(imagesOnly bool) {
|
|
ch.a.post(func() { ch.attachBusy = true })
|
|
go func() {
|
|
opts := mygo.OpenDialogOptions{Multiple: true}
|
|
if imagesOnly {
|
|
opts.Filters = []mygo.FileFilter{{Name: "图片", MIMETypes: []string{"image/*"}}}
|
|
}
|
|
paths, err := mygo.Dialog.Open(opts)
|
|
ch.a.chatPost(func() {
|
|
ch.attachBusy = false
|
|
if err != nil {
|
|
ch.errText = friendlyError(err)
|
|
return
|
|
}
|
|
for _, p := range paths {
|
|
ch.addAttachment(p)
|
|
}
|
|
})
|
|
}()
|
|
}
|
|
|
|
func (ch *chat) addAttachment(path string) {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if info.Size() > 10<<20 {
|
|
ch.a.chatPost(func() { ch.errText = "附件不能超过 10MB" })
|
|
return
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
mime := mimeByExt(path)
|
|
url := "data:" + mime + ";base64," + base64URL(data)
|
|
kept := ""
|
|
if strings.HasPrefix(mime, "image/") {
|
|
kept = keepImage(path, info.Name())
|
|
}
|
|
ch.a.chatPost(func() {
|
|
if len(ch.attachments) >= 5 {
|
|
ch.errText = "最多 5 个附件"
|
|
return
|
|
}
|
|
ch.attachments = append(ch.attachments, attachment{
|
|
Name: info.Name(), MIME: mime, DataURL: url, Size: info.Size(), LocalPath: kept,
|
|
})
|
|
})
|
|
}
|