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
+73
-69
@@ -10,13 +10,12 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/asuka/colors"
|
||||
"github.com/egoist/asuka/fonts"
|
||||
"github.com/egoist/asuka/sdk/store"
|
||||
"github.com/egoist/asuka/signal"
|
||||
"github.com/egoist/asuka/ui"
|
||||
"github.com/egoist/mygo"
|
||||
"github.com/egoist/mygo/ui"
|
||||
"github.com/egoist/mygo/ui/mobile"
|
||||
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||||
)
|
||||
|
||||
@@ -27,8 +26,7 @@ var imageExtensions = map[string]bool{
|
||||
".webp": true, ".bmp": true, ".avif": true,
|
||||
}
|
||||
|
||||
// imageRef is one previewable image a tool call names: the Source is an
|
||||
// absolute path or a data URL, the Label what its row shows.
|
||||
// imageRef is one previewable image a tool call names.
|
||||
type imageRef struct {
|
||||
Source string
|
||||
Label string
|
||||
@@ -38,9 +36,8 @@ func isImagePath(path string) bool {
|
||||
return imageExtensions[strings.ToLower(filepath.Ext(path))]
|
||||
}
|
||||
|
||||
// toolImages lists the images a tool call names: its file input, file parts
|
||||
// in its output (data URLs from a read, for example), and paths in its text
|
||||
// output. Relative paths resolve against the session's directory.
|
||||
// toolImages lists the images a tool call names: its file input, file
|
||||
// parts in its output, and paths in its text output.
|
||||
func (ch *chat) toolImages(p ocapi.ContentPart) []imageRef {
|
||||
seen := map[string]bool{}
|
||||
out := make([]imageRef, 0, 2)
|
||||
@@ -103,8 +100,6 @@ func (ch *chat) toolImages(p ocapi.ContentPart) []imageRef {
|
||||
}
|
||||
attachments := 0
|
||||
if p.State != nil {
|
||||
// Server-projected tool attachments carry the image itself, so they
|
||||
// come first: a read of an image needs no file request.
|
||||
for _, group := range [][]ocapi.ContentPart{p.State.Attachments, p.State.Content} {
|
||||
for _, c := range group {
|
||||
switch c.Type {
|
||||
@@ -124,7 +119,6 @@ func (ch *chat) toolImages(p ocapi.ContentPart) []imageRef {
|
||||
}
|
||||
}
|
||||
}
|
||||
// The attachment already is the image the call named.
|
||||
if attachments == 0 {
|
||||
for _, key := range []string{"filePath", "path", "file", "file_path", "sourcePath"} {
|
||||
if v, ok := p.State.Input[key].(string); ok {
|
||||
@@ -136,43 +130,49 @@ func (ch *chat) toolImages(p ocapi.ContentPart) []imageRef {
|
||||
return out
|
||||
}
|
||||
|
||||
func (ch *chat) toolImageAccessor(part signal.Accessor[ocapi.ContentPart]) signal.Accessor[[]imageRef] {
|
||||
return signal.Func(func() []imageRef { return ch.toolImages(part.Get()) })
|
||||
}
|
||||
// ------------------------------------------------------------- preview
|
||||
|
||||
// previewKey names a cached preview: a hash keeps data URLs out of the map.
|
||||
func previewKey(source string) string {
|
||||
sum := sha1.Sum([]byte(source))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// previewDir is where fetched previews are kept.
|
||||
func previewDir() string {
|
||||
dir, err := mygo.App.Path(mygo.PathUserData)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return filepath.Join(dir, "previews")
|
||||
}
|
||||
|
||||
func (ch *chat) previewImage(ref imageRef) {
|
||||
ch.previewSeq++
|
||||
seq := ch.previewSeq
|
||||
ch.previewTitle.Set(ref.Label)
|
||||
ch.previewPath.Set("")
|
||||
ch.previewLoading.Set(true)
|
||||
ch.previewOpen.Set(true)
|
||||
ch.previewTitle = ref.Label
|
||||
ch.previewPath = ""
|
||||
ch.previewLoading = true
|
||||
ch.previewOpen = true
|
||||
key := previewKey(ref.Source)
|
||||
if local, ok := ch.previewCache[key]; ok {
|
||||
ch.previewPath.Set(local)
|
||||
ch.previewLoading.Set(false)
|
||||
ch.previewPath = local
|
||||
ch.previewLoading = false
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
data, err := ch.loadPreview(ref.Source)
|
||||
if err != nil {
|
||||
ui.Post(func() {
|
||||
ch.a.chatPost(func() {
|
||||
if seq != ch.previewSeq {
|
||||
return
|
||||
}
|
||||
ch.previewLoading.Set(false)
|
||||
ch.previewOpen.Set(false)
|
||||
ui.Snackbar(ui.SnackbarProps{Message: "无法预览:" + friendlyError(err)})
|
||||
ch.previewLoading = false
|
||||
ch.previewOpen = false
|
||||
ch.errText = "无法预览:" + friendlyError(err)
|
||||
})
|
||||
return
|
||||
}
|
||||
dir := filepath.Join(store.Dir(), "previews")
|
||||
dir := previewDir()
|
||||
_ = os.MkdirAll(dir, 0o700)
|
||||
name := strings.Map(func(r rune) rune {
|
||||
if r == '/' || r == '\\' || r == ':' || r < ' ' {
|
||||
@@ -182,23 +182,22 @@ func (ch *chat) previewImage(ref imageRef) {
|
||||
}, ref.Label)
|
||||
local := filepath.Join(dir, fmt.Sprintf("%d-%s", time.Now().UnixNano(), name))
|
||||
if err := os.WriteFile(local, data, 0o600); err != nil {
|
||||
ui.Post(func() {
|
||||
ch.a.chatPost(func() {
|
||||
if seq != ch.previewSeq {
|
||||
return
|
||||
}
|
||||
ch.previewLoading.Set(false)
|
||||
ch.previewOpen.Set(false)
|
||||
ui.Snackbar(ui.SnackbarProps{Message: "无法保存预览"})
|
||||
ch.previewLoading = false
|
||||
ch.previewOpen = false
|
||||
})
|
||||
return
|
||||
}
|
||||
ui.Post(func() {
|
||||
ch.a.chatPost(func() {
|
||||
if seq != ch.previewSeq {
|
||||
return
|
||||
}
|
||||
ch.previewCache[key] = local
|
||||
ch.previewPath.Set(local)
|
||||
ch.previewLoading.Set(false)
|
||||
ch.previewPath = local
|
||||
ch.previewLoading = false
|
||||
})
|
||||
}()
|
||||
}
|
||||
@@ -216,9 +215,9 @@ func (ch *chat) loadPreview(source string) ([]byte, error) {
|
||||
}
|
||||
return base64.StdEncoding.DecodeString(source[comma+1:])
|
||||
}
|
||||
client := ch.client()
|
||||
client := ch.a.client
|
||||
if client == nil {
|
||||
return nil, fmt.Errorf("not connected")
|
||||
return nil, fmt.Errorf("未连接")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
@@ -226,39 +225,44 @@ func (ch *chat) loadPreview(source string) ([]byte, error) {
|
||||
return data, err
|
||||
}
|
||||
|
||||
func (ch *chat) previewSheet() ui.Node {
|
||||
return ui.Sheet(ui.SheetProps{
|
||||
Open: ch.previewOpen,
|
||||
Detents: ui.DetentLarge,
|
||||
Grabber: true,
|
||||
OnDismiss: func() { ch.previewOpen.Set(false) },
|
||||
Content: func() ui.Node {
|
||||
return ui.View(ui.ViewProps{Label: ui.Val("图片预览"), Style: ui.Style{Grow: 1}},
|
||||
ui.SafeArea(ui.Style{Padding: ui.All(16), Gap: 10},
|
||||
ui.Text(ch.previewTitle, ui.Style{Font: fonts.Headline, Lines: 2}),
|
||||
ui.Show(ch.previewLoading,
|
||||
func() ui.Node { return ui.Activity() },
|
||||
func() ui.Node {
|
||||
return ui.Image(ui.ImageProps{
|
||||
Source: ch.previewPath,
|
||||
Mode: ui.ImageFit,
|
||||
Style: ui.Style{Width: ui.Pct(100), Height: ui.Pt(420), CornerRadius: 10},
|
||||
})
|
||||
}),
|
||||
),
|
||||
)
|
||||
},
|
||||
// previewSheet shows the fetched image.
|
||||
func (ch *chat) previewSheet(c *ui.Context) {
|
||||
if !ch.previewOpen {
|
||||
return
|
||||
}
|
||||
mobile.Sheet(c, &ch.previewOpen, mobile.SheetOptions{Detents: []float32{0.92}}, func() {
|
||||
ui.Column(c).FillWidth().Padding(8, 16, 24, 16).Gap(8).Children(func() {
|
||||
ui.Text(c, ch.previewTitle).FontSize(16).FontWeight(600).MaxLines(2)
|
||||
if ch.previewLoading {
|
||||
ui.Column(c).Fill().Center().Height(300).Children(func() { ui.Spinner(c) })
|
||||
return
|
||||
}
|
||||
bmp := loadImageFile(ch.previewPath)
|
||||
if bmp != nil {
|
||||
ui.Image(c, bmp).FillWidth().Height(420).Radius(10).Clip()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// imageLinkRow is one previewable image under a tool call.
|
||||
func (ch *chat) imageLinkRow(ref imageRef) ui.Node {
|
||||
return ui.View(ui.ViewProps{
|
||||
OnPress: func() { ch.previewImage(ref) },
|
||||
Label: ui.Val("预览 " + ref.Label),
|
||||
Style: ui.Style{Padding: ui.Symmetric(3, 0)},
|
||||
}, ui.Row(ui.Style{Gap: 6, Align: ui.AlignCenter},
|
||||
ui.Symbol("photo", ui.Style{Font: fonts.Caption, Color: colors.Accent}),
|
||||
ui.StaticText(ref.Label, ui.Style{Font: fonts.Caption, Color: colors.Accent, Lines: 1}),
|
||||
))
|
||||
// loadImageFile decodes a local image file into a bitmap, caching it.
|
||||
var imageCache sync.Map // path -> *ui.Bitmap
|
||||
|
||||
func loadImageFile(path string) *ui.Bitmap {
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
if v, ok := imageCache.Load(path); ok {
|
||||
return v.(*ui.Bitmap)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
bmp, err := ui.DecodeBitmap(data)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
imageCache.Store(path, bmp)
|
||||
return bmp
|
||||
}
|
||||
Reference in new issue
Block a user