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:
OpenChamber committed 2026-11-18 13:22:59 +08:00
1 parent 2fb4a8ea79
commit 6c0ee61750
2538 files changed
+675779 -4792

No files matched your search

+83 -92
View File
@@ -1,13 +1,12 @@
package appui
import (
"context"
"fmt"
"math"
"github.com/egoist/asuka/colors"
"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"
)
@@ -18,8 +17,7 @@ type modelLimit struct {
}
// usageReading is the context window of the last assistant message that
// reported tokens, as the first-party client shows it. The output
// reservation is capped at 32k tokens, matching its calculation.
// reported tokens.
type usageReading struct {
Used int
Limit int
@@ -41,8 +39,6 @@ func (ch *chat) usageReading(msgs []ocapi.Message) usageReading {
if last == nil {
return usageReading{}
}
// A message the event stream added has no model yet; the session's model
// is the latest one any message reports.
model := last.Model
for i := len(msgs) - 1; i >= 0 && model == nil; i-- {
if msgs[i].Type == "assistant" && msgs[i].Model != nil {
@@ -61,8 +57,7 @@ func (ch *chat) usageReading(msgs []ocapi.Message) usageReading {
}
lim := ch.modelLimit(model)
r.Limit, r.Output = lim.Context, lim.Output
r.Model = ch.modelName(model)
// The turn's cost is every step after its prompt.
r.Model = ch.a.modelName(model)
for i := len(msgs) - 1; i >= 0; i-- {
if msgs[i].Type == "user" {
break
@@ -87,19 +82,34 @@ func (ch *chat) usageReading(msgs []ocapi.Message) usageReading {
return r
}
func (ch *chat) contextUsage() usageReading {
return ch.usageReading(ch.msgs.Peek())
}
func (ch *chat) modelLimit(m *ocapi.ModelRef) modelLimit {
if m == nil {
return modelLimit{}
}
limits := ch.modelLimits.Get()
if l, ok := limits[m.ProviderID+"/"+m.ID]; ok {
if l, ok := ch.modelLimits[m.ProviderID+"/"+m.ID]; ok {
return l
}
return limits[m.ID]
return ch.modelLimits[m.ID]
}
func (a *App) loadModelLimits() {
go func() {
models, err := a.client.Models(context.Background(), a.dir())
a.post(func() {
if err != nil {
return
}
limits := map[string]modelLimit{}
for _, m := range models {
lim := modelLimit{Context: m.Limit.Context, Output: m.Limit.Output}
limits[m.ProviderID+"/"+m.ID] = lim
limits[m.ID] = lim
}
if a.chat != nil {
a.chat.modelLimits = limits
}
})
}()
}
func formatTokens(n int) string {
@@ -113,76 +123,6 @@ func formatTokens(n int) string {
}
}
// usageTone follows the first-party thresholds: 75% warns, 90% errors.
func usageTone(pct float64) ui.Color {
switch {
case pct >= 90:
return colors.Red
case pct >= 75:
return colors.Orange
default:
return colors.Green
}
}
// usageButton is the composer's context ring and percentage.
func (ch *chat) usageButton() ui.Node {
return ui.Dynamic(func() ui.Node {
r := ch.usageReading(ch.msgs.Get())
if !r.Known {
return ui.View(ui.ViewProps{})
}
progress := signal.Func(func() float64 { return r.Pct / 100 })
return ui.View(ui.ViewProps{
OnPress: func() { ch.usageOpen.Set(true) },
Label: ui.Val("上下文占用"),
Hint: ui.Val("查看上下文详情"),
Style: ui.Style{Padding: ui.All(4), Align: ui.AlignCenter},
}, ui.Row(ui.Style{Gap: 4, Align: ui.AlignCenter},
ui.ProgressRing(progress, ui.Style{Width: ui.Pt(18), Height: ui.Pt(18), Color: usageTone(r.Pct)}),
ui.StaticText(fmt.Sprintf("%.1f%%", math.Min(r.Pct, 999)),
ui.Style{Font: fonts.Caption, Color: colors.SecondaryLabel}),
))
})
}
func (ch *chat) usageSheet() ui.Node {
return ui.Sheet(ui.SheetProps{
Open: ch.usageOpen,
Detents: ui.DetentMedium,
Grabber: true,
OnDismiss: func() { ch.usageOpen.Set(false) },
Content: func() ui.Node {
return ui.Dynamic(func() ui.Node {
r := ch.usageReading(ch.msgs.Get())
rows := []ui.Node{usageRow("模型", r.Model)}
if r.Known {
rows = append(rows,
usageRow("已用 tokens", formatTokens(r.Used)),
usageRow("上下文上限", formatTokens(r.Limit)),
usageRow("输出预留", formatTokens(r.Output)),
usageRow("占用", fmt.Sprintf("%.1f%%", math.Min(r.Pct, 999))),
)
} else {
rows = append(rows,
usageRow("已用 tokens", formatTokens(r.Used)),
usageRow("上下文上限", "未知"),
)
}
if r.Cost > 0 {
rows = append(rows, usageRow("本轮费用", formatCost(r.Cost)))
}
return ui.SafeArea(ui.Style{Padding: ui.All(20), Gap: 10},
ui.StaticText("上下文占用", ui.Style{Font: fonts.Title2.Weighted(fonts.Bold)}),
ui.Column(ui.Style{Gap: 10}, rows...),
ui.StaticText("输出预留按最多 32K tokens 计算,与官方客户端一致。",
ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
)
})
},
})
}
func formatCost(v float64) string {
if v < 0.01 {
return fmt.Sprintf("$%.4f", v)
@@ -190,9 +130,60 @@ func formatCost(v float64) string {
return fmt.Sprintf("$%.2f", v)
}
func usageRow(label, value string) ui.Node {
return ui.Row(ui.Style{Gap: 12, Align: ui.AlignCenter},
ui.StaticText(label, ui.Style{Font: fonts.Body, Color: colors.SecondaryLabel, Grow: 1}),
ui.StaticText(value, ui.Style{Font: fonts.Body}),
)
// usageButton is the composer's context ring and percentage.
func (ch *chat) usageButton(c *ui.Context) {
t := c.Theme()
r := ch.usageReading(ch.msgs)
if !r.Known {
return
}
btn := ui.Row(c).Gap(4).AlignItems(ui.Center).Padding(4, 4).Role(ui.RoleButton).Label("上下文占用")
btn.Children(func() {
ui.Text(c, fmt.Sprintf("%.0f%%", r.Pct)).FontSize(13).TextColor(t.TextMuted)
})
if btn.Clicked() {
ch.usageOpen = true
}
}
// usageSheet shows the context detail.
func (ch *chat) usageSheet(c *ui.Context) {
if !ch.usageOpen {
return
}
t := c.Theme()
mobileSheet(c, &ch.usageOpen, 0.5, func() {
r := ch.usageReading(ch.msgs)
ui.Column(c).FillWidth().Padding(8, 16, 24, 16).Gap(8).Children(func() {
ui.Text(c, "上下文占用").FontSize(20).Bold()
usageRow(c, t, "模型", r.Model)
if r.Known {
usageRow(c, t, "已用 tokens", formatTokens(r.Used))
usageRow(c, t, "上下文上限", formatTokens(r.Limit))
usageRow(c, t, "输出预留", formatTokens(r.Output))
usageRow(c, t, "占用", fmt.Sprintf("%.1f%%", math.Min(r.Pct, 999)))
} else {
usageRow(c, t, "已用 tokens", formatTokens(r.Used))
usageRow(c, t, "上下文上限", "未知")
}
if r.Cost > 0 {
usageRow(c, t, "本轮费用", formatCost(r.Cost))
}
ui.Text(c, "输出预留按最多 32K tokens 计算,与官方客户端一致。").FontSize(12).TextColor(t.TextMuted)
})
})
}
func usageRow(c *ui.Context, t *ui.Theme, label, value string) ui.Element {
row := ui.Row(c).FillWidth().Gap(12).AlignItems(ui.Center)
row.Children(func() {
ui.Text(c, label).FontSize(14).TextColor(t.TextMuted).Grow(1)
ui.Text(c, value).FontSize(14)
})
return row
}
// mobileSheet shows a sheet of a detent.
func mobileSheet(c *ui.Context, open *bool, detent float32, content func()) {
mobile.Sheet(c, open, mobile.SheetOptions{Detents: []float32{detent}}, content)
}