feat: port the client to MyGo native UI (0.3.0)
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.
This commit is contained in:
1 parent
2fb4a8ea79
commit
6c0ee61750
2538 files changed
+675779
-4792
No files matched your search
+127
-203
@@ -2,239 +2,163 @@ package appui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/asuka/fonts"
|
||||
"github.com/egoist/asuka/signal"
|
||||
"github.com/egoist/asuka/ui"
|
||||
"github.com/egoist/mygo/ui"
|
||||
"github.com/egoist/mygo/ui/mobile"
|
||||
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||||
)
|
||||
|
||||
func (c *controller) sessionMenu(s ocapi.Session) {
|
||||
ui.Alert(ui.AlertProps{
|
||||
Title: "会话",
|
||||
ActionSheet: true,
|
||||
Actions: []ui.Action{
|
||||
{Title: ui.Val("重命名"), Icon: "pencil", OnPress: func() { c.renameSession(s) }},
|
||||
{Title: ui.Val("归档"), Icon: "archivebox", OnPress: func() { c.archiveSession(s) }},
|
||||
{Title: ui.Val("删除"), Icon: ui.IconTrash, Role: ui.ActionDestructive,
|
||||
OnPress: func() { c.confirmDelete(s) }},
|
||||
},
|
||||
})
|
||||
// new session -----------------------------------------------------------
|
||||
|
||||
func (a *App) newSessionForm() {
|
||||
a.newTitle = ""
|
||||
a.newOpen = true
|
||||
}
|
||||
|
||||
func (c *controller) renameSession(s ocapi.Session) {
|
||||
name := signal.New(s.Title)
|
||||
ui.Alert(ui.AlertProps{
|
||||
Title: "重命名会话",
|
||||
Message: "标题留空时,服务端不会自动生成新标题。",
|
||||
Fields: []ui.AlertField{{Text: name, Placeholder: "标题"}},
|
||||
Actions: []ui.Action{
|
||||
{Title: ui.Val("取消"), Role: ui.ActionCancel},
|
||||
{Title: ui.Val("保存"), Role: ui.ActionDefault,
|
||||
Disabled: signal.Func(func() bool { return strings.TrimSpace(name.Get()) == "" }),
|
||||
OnPress: func() { c.patchSessionOnServer(s, name.Get()) }},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (c *controller) patchSessionOnServer(s ocapi.Session, title string) {
|
||||
client := c.client
|
||||
if client == nil {
|
||||
func (a *App) newSessionSheet(c *ui.Context) {
|
||||
if !a.newOpen {
|
||||
return
|
||||
}
|
||||
dir := sessionDir(s)
|
||||
mobile.Sheet(c, &a.newOpen, mobile.SheetOptions{Detents: []float32{0.4}}, func() {
|
||||
ui.Column(c).FillWidth().Padding(8, 16, 24, 16).Gap(10).Children(func() {
|
||||
ui.Text(c, "新建会话").FontSize(20).Bold()
|
||||
ui.TextInput(c, &a.newTitle).Placeholder("标题(可选)").AutoFocus()
|
||||
if ui.PrimaryButton(c, "创建").FillWidth().Clicked() {
|
||||
a.newOpen = false
|
||||
a.createSession()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) createSession() {
|
||||
dir := a.dir()
|
||||
title := a.newTitle
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
t := strings.TrimSpace(title)
|
||||
err := client.UpdateSession(ctx, s.ID, dir, ocapi.SessionPatch{Title: &t})
|
||||
ui.Post(func() {
|
||||
sess, err := a.client.CreateSession(ctx, ocapi.CreateSessionOptions{Title: title, Directory: dir})
|
||||
a.post(func() {
|
||||
if err != nil {
|
||||
c.status.Set(friendlyError(err))
|
||||
a.errorText = friendlyError(err)
|
||||
return
|
||||
}
|
||||
c.sessions.Update(func(list []ocapi.Session) []ocapi.Session {
|
||||
next := make([]ocapi.Session, len(list))
|
||||
copy(next, list)
|
||||
for i := range next {
|
||||
if next[i].ID == s.ID {
|
||||
next[i].Title = t
|
||||
}
|
||||
}
|
||||
return next
|
||||
})
|
||||
a.sessions = append([]ocapi.Session{sess}, a.sessions...)
|
||||
a.openChat(sess)
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *controller) archiveSession(s ocapi.Session) {
|
||||
client := c.client
|
||||
if client == nil {
|
||||
// session actions -------------------------------------------------------
|
||||
|
||||
func (a *App) openSessionActions(s ocapi.Session) {
|
||||
a.sessionActionOpen = true
|
||||
a.sessionActionTarget = s
|
||||
}
|
||||
|
||||
func (a *App) sessionActionSheet(c *ui.Context) {
|
||||
if !a.sessionActionOpen {
|
||||
return
|
||||
}
|
||||
dir := sessionDir(s)
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
archived := true
|
||||
err := client.UpdateSession(ctx, s.ID, dir, ocapi.SessionPatch{Archived: &archived})
|
||||
ui.Post(func() {
|
||||
if err != nil {
|
||||
c.status.Set(friendlyError(err))
|
||||
return
|
||||
}
|
||||
c.removeSession(s.ID)
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *controller) confirmDelete(s ocapi.Session) {
|
||||
ui.Alert(ui.AlertProps{
|
||||
Title: "删除会话?",
|
||||
Message: "会话及其子会话会被永久删除,无法撤销。",
|
||||
Actions: []ui.Action{
|
||||
{Title: ui.Val("取消"), Role: ui.ActionCancel},
|
||||
{Title: ui.Val("删除"), Role: ui.ActionDestructive,
|
||||
OnPress: func() { c.deleteSession(s) }},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (c *controller) deleteSession(s ocapi.Session) {
|
||||
client := c.client
|
||||
if client == nil {
|
||||
return
|
||||
}
|
||||
dir := sessionDir(s)
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
err := client.DeleteSession(ctx, s.ID, dir)
|
||||
ui.Post(func() {
|
||||
if err != nil {
|
||||
c.status.Set(friendlyError(err))
|
||||
return
|
||||
}
|
||||
c.removeSession(s.ID)
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
func sessionDir(s ocapi.Session) string {
|
||||
if s.Location != nil {
|
||||
return s.Location.Directory
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// directories lists the working directories a new session can use: the
|
||||
// server's default, then every directory the loaded sessions live in.
|
||||
func (c *controller) directories() signal.Accessor[[]string] {
|
||||
return signal.Func(func() []string {
|
||||
seen := map[string]bool{}
|
||||
out := make([]string, 0, 4)
|
||||
add := func(d string) {
|
||||
if d == "" || seen[d] {
|
||||
return
|
||||
}
|
||||
seen[d] = true
|
||||
out = append(out, d)
|
||||
}
|
||||
add(c.defaultDir.Get())
|
||||
for _, s := range c.sessions.Get() {
|
||||
if s.Location != nil {
|
||||
add(s.Location.Directory)
|
||||
}
|
||||
}
|
||||
return out
|
||||
})
|
||||
}
|
||||
|
||||
func chatActions(ch *chat) []ui.Action {
|
||||
return []ui.Action{
|
||||
{Title: ui.Val("模型"), OnPress: ch.openModelPicker},
|
||||
{Title: ui.Val("Agent"), OnPress: ch.openAgentPicker},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *controller) openNewSession() {
|
||||
if c.client == nil {
|
||||
return
|
||||
}
|
||||
c.newTitle.Set("")
|
||||
c.newDir.Set(0)
|
||||
c.creating.Set(false)
|
||||
c.newOpen.Set(true)
|
||||
}
|
||||
|
||||
func (c *controller) newSessionSheetNode() ui.Node {
|
||||
return ui.Sheet(ui.SheetProps{
|
||||
Open: c.newOpen,
|
||||
Detents: ui.DetentLarge,
|
||||
Grabber: true,
|
||||
Content: c.newSessionSheet,
|
||||
OnDismiss: func() { c.creating.Set(false) },
|
||||
})
|
||||
}
|
||||
|
||||
func (c *controller) newSessionSheet() ui.Node {
|
||||
return ui.SafeArea(ui.Style{Padding: ui.All(20), Gap: 14, Grow: 1},
|
||||
ui.StaticText("新建会话", ui.Style{Font: fonts.Title2.Weighted(fonts.Bold)}),
|
||||
ui.TextField(ui.TextFieldProps{
|
||||
Value: c.newTitle,
|
||||
Placeholder: ui.Val("标题(可选,留空由服务端生成)"),
|
||||
ReturnKey: ui.ReturnGo,
|
||||
OnSubmit: c.createSession,
|
||||
}),
|
||||
ui.Picker(ui.PickerProps{
|
||||
Items: c.directories(),
|
||||
Selected: c.newDir,
|
||||
Label: ui.Val("工作目录"),
|
||||
}),
|
||||
ui.Button(ui.ButtonProps{
|
||||
Title: ui.Val("创建"),
|
||||
Variant: ui.ButtonFilled,
|
||||
Disabled: c.creating,
|
||||
OnPress: c.createSession,
|
||||
}),
|
||||
ui.Show(c.creating, func() ui.Node { return ui.Activity() }),
|
||||
s := a.sessionActionTarget
|
||||
mobile.ActionSheet(c, &a.sessionActionOpen, mobile.ActionSheetOptions{Title: s.Title, Cancel: "取消"},
|
||||
mobile.Action{Label: "重命名", OnTap: func() { a.renameSession(s) }},
|
||||
mobile.Action{Label: "归档", OnTap: func() { a.setArchived(s, true) }},
|
||||
mobile.Action{Label: "删除", Destructive: true, OnTap: func() { a.deleteSession(s) }},
|
||||
)
|
||||
}
|
||||
|
||||
func (c *controller) createSession() {
|
||||
client := c.client
|
||||
nav := c.nav
|
||||
if client == nil || c.creating.Peek() {
|
||||
func (a *App) renameSession(s ocapi.Session) {
|
||||
a.renameOpen = true
|
||||
a.renameTarget = s
|
||||
a.renameText = s.Title
|
||||
}
|
||||
|
||||
func (a *App) renameSheet(c *ui.Context) {
|
||||
if !a.renameOpen {
|
||||
return
|
||||
}
|
||||
dirs := signal.Untrack(func() []string { return c.directories().Get() })
|
||||
dir := ""
|
||||
if idx := c.newDir.Peek(); idx >= 0 && idx < len(dirs) {
|
||||
dir = dirs[idx]
|
||||
}
|
||||
title := strings.TrimSpace(c.newTitle.Peek())
|
||||
c.creating.Set(true)
|
||||
mobile.Sheet(c, &a.renameOpen, mobile.SheetOptions{Detents: []float32{0.3}}, func() {
|
||||
ui.Column(c).FillWidth().Padding(8, 16, 24, 16).Gap(10).Children(func() {
|
||||
ui.Text(c, "重命名").FontSize(20).Bold()
|
||||
ui.TextInput(c, &a.renameText).AutoFocus()
|
||||
if ui.PrimaryButton(c, "保存").FillWidth().Clicked() {
|
||||
a.renameOpen = false
|
||||
a.doRename()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) doRename() {
|
||||
title := a.renameText
|
||||
s := a.renameTarget
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
s, err := client.CreateSession(ctx, ocapi.CreateSessionOptions{Directory: dir, Title: title})
|
||||
ui.Post(func() {
|
||||
c.creating.Set(false)
|
||||
err := a.client.UpdateSession(ctx, s.ID, a.dir(), ocapi.SessionPatch{Title: &title})
|
||||
a.post(func() {
|
||||
if err != nil {
|
||||
c.status.Set(friendlyError(err))
|
||||
a.errorText = friendlyError(err)
|
||||
return
|
||||
}
|
||||
c.newOpen.Set(false)
|
||||
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 nav != nil {
|
||||
ch := c.openChat(&s, nav)
|
||||
nav.Push(ch.screen, ui.ScreenOptions{Title: ch.title, Actions: chatActions(ch)})
|
||||
a.patchSession(s.ID, func(x *ocapi.Session) { x.Title = title })
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
func (a *App) setArchived(s ocapi.Session, on bool) {
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
err := a.client.UpdateSession(ctx, s.ID, a.dir(), ocapi.SessionPatch{Archived: &on})
|
||||
a.post(func() {
|
||||
if err != nil {
|
||||
a.errorText = friendlyError(err)
|
||||
}
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
func (a *App) deleteSession(s ocapi.Session) {
|
||||
a.sessionDeleteOpen = true
|
||||
target := s
|
||||
a.sessionDeleteTarget = &target
|
||||
}
|
||||
|
||||
func (a *App) sessionDeleteConfirm(c *ui.Context) {
|
||||
if !a.sessionDeleteOpen {
|
||||
return
|
||||
}
|
||||
title := ""
|
||||
if a.sessionDeleteTarget != nil {
|
||||
title = a.sessionDeleteTarget.Title
|
||||
}
|
||||
buttons := ui.AlertDialog(c, &a.sessionDeleteOpen, "删除会话?",
|
||||
"“"+title+"”及其子会话将被删除,且无法恢复。", "取消", "删除")
|
||||
if buttons == 1 {
|
||||
a.doDeleteSession()
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) doDeleteSession() {
|
||||
if a.sessionDeleteTarget == nil {
|
||||
return
|
||||
}
|
||||
s := *a.sessionDeleteTarget
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
err := a.client.DeleteSession(ctx, s.ID, a.dir())
|
||||
a.post(func() {
|
||||
if err != nil {
|
||||
a.errorText = friendlyError(err)
|
||||
return
|
||||
}
|
||||
a.removeSession(s.ID)
|
||||
if a.chat != nil && a.chat.id == s.ID {
|
||||
a.closeChat()
|
||||
}
|
||||
})
|
||||
}()
|
||||
|
||||
Reference in new issue
Block a user