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
+64
-100
@@ -4,12 +4,11 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/egoist/asuka/ui"
|
||||
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||||
)
|
||||
|
||||
// eventTypes are the event kinds the list and chat read. Sending the filter
|
||||
// keeps a phone's stream small; server.connected always arrives.
|
||||
// eventTypes are the event kinds the list and chat read. Sending the
|
||||
// filter keeps a phone's stream small; server.connected always arrives.
|
||||
var eventTypes = []string{
|
||||
"session.created",
|
||||
"session.renamed",
|
||||
@@ -77,51 +76,51 @@ type permissionData struct {
|
||||
Resources []string `json:"resources"`
|
||||
}
|
||||
|
||||
// startEvents subscribes to the server's event stream for the given client.
|
||||
// It must run on the main thread (it writes the cancel function).
|
||||
func (c *controller) startEvents(client *ocapi.Client) {
|
||||
if c.cancelSSE != nil {
|
||||
c.cancelSSE()
|
||||
// startStream subscribes to the server's event stream. Call it on the main
|
||||
// thread; the stream runs on a goroutine and posts events back.
|
||||
func (a *App) startStream() {
|
||||
if a.client == nil {
|
||||
return
|
||||
}
|
||||
a.stopStream()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
c.cancelSSE = cancel
|
||||
// A stream that reconnects lost events; a fresh connection (the one
|
||||
// made when connecting) is the list's first read, not a gap.
|
||||
a.streamCancel = cancel
|
||||
wasDown := false
|
||||
client := a.client
|
||||
sub := ocapi.Subscription{
|
||||
Types: eventTypes,
|
||||
OnEvent: func(ev ocapi.Event) {
|
||||
ui.Post(func() {
|
||||
if c.client != client {
|
||||
a.post(func() {
|
||||
if a.client != client {
|
||||
return
|
||||
}
|
||||
c.applyEvent(ev)
|
||||
a.applyEvent(ev)
|
||||
})
|
||||
},
|
||||
OnState: func(st ocapi.SubscriptionState, err error) {
|
||||
switch st {
|
||||
case ocapi.Disconnected:
|
||||
wasDown = true
|
||||
ui.Post(func() {
|
||||
if c.client != client {
|
||||
a.post(func() {
|
||||
if a.client != client {
|
||||
return
|
||||
}
|
||||
c.status.Set("连接中断,正在重连…")
|
||||
a.errorText = "连接中断,正在重连…"
|
||||
})
|
||||
case ocapi.Subscribed:
|
||||
if !wasDown {
|
||||
return
|
||||
}
|
||||
wasDown = false
|
||||
// Events missed while the stream was down are gone;
|
||||
// re-read the list and the open chat's tail.
|
||||
ui.Post(func() {
|
||||
if c.client != client {
|
||||
a.post(func() {
|
||||
if a.client != client {
|
||||
return
|
||||
}
|
||||
c.refresh()
|
||||
if c.chat != nil {
|
||||
c.chat.reloadTail()
|
||||
a.errorText = ""
|
||||
a.loadSessions()
|
||||
a.loadPerms()
|
||||
if a.chat != nil {
|
||||
a.chat.reloadTail()
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -130,14 +129,25 @@ func (c *controller) startEvents(client *ocapi.Client) {
|
||||
go client.Subscribe(ctx, sub)
|
||||
}
|
||||
|
||||
// applyEvent folds one server event into the list state. Main thread.
|
||||
func (c *controller) applyEvent(ev ocapi.Event) {
|
||||
if c.chat != nil {
|
||||
func (a *App) stopStream() {
|
||||
if a.streamCancel != nil {
|
||||
a.streamCancel()
|
||||
a.streamCancel = nil
|
||||
}
|
||||
}
|
||||
|
||||
// applyEvent folds one server event into the list and, when it belongs to
|
||||
// the open chat, into the chat. Main thread.
|
||||
func (a *App) applyEvent(ev ocapi.Event) {
|
||||
if a.chat != nil {
|
||||
var ref struct {
|
||||
SessionID string `json:"sessionID"`
|
||||
}
|
||||
if ev.UnmarshalData(&ref) == nil && ref.SessionID == c.chat.id {
|
||||
c.chat.handleEvent(ev)
|
||||
if ev.UnmarshalData(&ref) == nil && ref.SessionID == a.chat.id {
|
||||
a.chat.handleEvent(ev)
|
||||
}
|
||||
if a.chatWin != nil {
|
||||
a.chatWin.Update(func() {})
|
||||
}
|
||||
}
|
||||
switch ev.Type {
|
||||
@@ -146,14 +156,12 @@ func (c *controller) applyEvent(ev ocapi.Event) {
|
||||
if ev.UnmarshalData(&d) != nil || d.SessionID == "" {
|
||||
return
|
||||
}
|
||||
list := c.sessions.Peek()
|
||||
for _, s := range list {
|
||||
for _, s := range a.sessions {
|
||||
if s.ID == d.SessionID {
|
||||
return
|
||||
}
|
||||
}
|
||||
next := make([]ocapi.Session, 0, len(list)+1)
|
||||
next = append(next, ocapi.Session{
|
||||
sess := ocapi.Session{
|
||||
ID: d.SessionID,
|
||||
ProjectID: d.ProjectID,
|
||||
Title: d.Title,
|
||||
@@ -161,82 +169,68 @@ func (c *controller) applyEvent(ev ocapi.Event) {
|
||||
Model: d.Model,
|
||||
Location: d.Location,
|
||||
Time: ocapi.SessionTime{Created: ev.Created, Updated: ev.Created},
|
||||
})
|
||||
c.sessions.Set(append(next, list...))
|
||||
}
|
||||
a.sessions = append([]ocapi.Session{sess}, a.sessions...)
|
||||
|
||||
case "session.renamed":
|
||||
var d sessionRenamedData
|
||||
if ev.UnmarshalData(&d) != nil {
|
||||
return
|
||||
}
|
||||
c.patchSession(d.SessionID, func(s *ocapi.Session) { s.Title = d.Title })
|
||||
a.patchSession(d.SessionID, func(s *ocapi.Session) { s.Title = d.Title })
|
||||
|
||||
case "session.model.selected":
|
||||
var d sessionModelData
|
||||
if ev.UnmarshalData(&d) != nil {
|
||||
return
|
||||
}
|
||||
c.patchSession(d.SessionID, func(s *ocapi.Session) { s.Model = d.Model })
|
||||
a.patchSession(d.SessionID, func(s *ocapi.Session) { s.Model = d.Model })
|
||||
|
||||
case "session.agent.selected":
|
||||
var d sessionAgentData
|
||||
if ev.UnmarshalData(&d) != nil {
|
||||
return
|
||||
}
|
||||
c.patchSession(d.SessionID, func(s *ocapi.Session) { s.Agent = d.Agent })
|
||||
a.patchSession(d.SessionID, func(s *ocapi.Session) { s.Agent = d.Agent })
|
||||
|
||||
case "session.archived":
|
||||
case "session.archived", "session.deleted":
|
||||
var d sessionRefData
|
||||
if ev.UnmarshalData(&d) != nil {
|
||||
return
|
||||
}
|
||||
c.removeSession(d.SessionID)
|
||||
|
||||
case "session.deleted":
|
||||
var d sessionRefData
|
||||
if ev.UnmarshalData(&d) != nil {
|
||||
return
|
||||
}
|
||||
c.removeSession(d.SessionID)
|
||||
a.removeSession(d.SessionID)
|
||||
|
||||
case "session.unarchived", "session.moved":
|
||||
// The event carries no full record; a list read is the truth.
|
||||
c.refresh()
|
||||
a.loadSessions()
|
||||
|
||||
case "session.execution.started":
|
||||
var d sessionRefData
|
||||
if ev.UnmarshalData(&d) != nil {
|
||||
return
|
||||
}
|
||||
c.setActive(d.SessionID, true)
|
||||
a.active[d.SessionID] = true
|
||||
|
||||
case "session.execution.succeeded", "session.execution.failed", "session.execution.interrupted":
|
||||
var d sessionRefData
|
||||
if ev.UnmarshalData(&d) != nil {
|
||||
return
|
||||
}
|
||||
c.setActive(d.SessionID, false)
|
||||
a.active[d.SessionID] = false
|
||||
|
||||
case "permission.asked":
|
||||
var d permissionData
|
||||
if ev.UnmarshalData(&d) != nil || d.ID == "" {
|
||||
return
|
||||
}
|
||||
list := c.perms.Peek()
|
||||
for _, p := range list {
|
||||
for _, p := range a.perms {
|
||||
if p.ID == d.ID {
|
||||
return
|
||||
}
|
||||
}
|
||||
next := make([]ocapi.PermissionRequest, len(list), len(list)+1)
|
||||
copy(next, list)
|
||||
next = append(next, ocapi.PermissionRequest{
|
||||
a.perms = append(a.perms, ocapi.PermissionRequest{
|
||||
ID: d.ID, SessionID: d.SessionID, Action: d.Action, Resources: d.Resources,
|
||||
})
|
||||
c.perms.Set(next)
|
||||
if c.chat != nil {
|
||||
c.chat.syncPerms()
|
||||
}
|
||||
|
||||
case "permission.replied":
|
||||
var d struct {
|
||||
@@ -245,61 +239,31 @@ func (c *controller) applyEvent(ev ocapi.Event) {
|
||||
if ev.UnmarshalData(&d) != nil || d.RequestID == "" {
|
||||
return
|
||||
}
|
||||
list := c.perms.Peek()
|
||||
next := make([]ocapi.PermissionRequest, 0, len(list))
|
||||
for _, p := range list {
|
||||
next := a.perms[:0]
|
||||
for _, p := range a.perms {
|
||||
if p.ID != d.RequestID {
|
||||
next = append(next, p)
|
||||
}
|
||||
}
|
||||
if len(next) != len(list) {
|
||||
c.perms.Set(next)
|
||||
if c.chat != nil {
|
||||
c.chat.syncPerms()
|
||||
}
|
||||
}
|
||||
a.perms = next
|
||||
}
|
||||
}
|
||||
|
||||
func (c *controller) patchSession(id string, patch func(*ocapi.Session)) {
|
||||
list := c.sessions.Peek()
|
||||
next := make([]ocapi.Session, len(list))
|
||||
copy(next, list)
|
||||
for i := range next {
|
||||
if next[i].ID == id {
|
||||
patch(&next[i])
|
||||
c.sessions.Set(next)
|
||||
func (a *App) patchSession(id string, patch func(*ocapi.Session)) {
|
||||
for i := range a.sessions {
|
||||
if a.sessions[i].ID == id {
|
||||
patch(&a.sessions[i])
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *controller) removeSession(id string) {
|
||||
list := c.sessions.Peek()
|
||||
next := make([]ocapi.Session, 0, len(list))
|
||||
for _, s := range list {
|
||||
func (a *App) removeSession(id string) {
|
||||
next := a.sessions[:0]
|
||||
for _, s := range a.sessions {
|
||||
if s.ID != id {
|
||||
next = append(next, s)
|
||||
}
|
||||
}
|
||||
if len(next) != len(list) {
|
||||
c.sessions.Set(next)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *controller) setActive(id string, on bool) {
|
||||
cur := c.active.Peek()
|
||||
if cur[id] == on {
|
||||
return
|
||||
}
|
||||
next := make(map[string]bool, len(cur)+1)
|
||||
for k, v := range cur {
|
||||
next[k] = v
|
||||
}
|
||||
if on {
|
||||
next[id] = true
|
||||
} else {
|
||||
delete(next, id)
|
||||
}
|
||||
c.active.Set(next)
|
||||
a.sessions = next
|
||||
}
|
||||
Reference in new issue
Block a user