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

+213
View File
@@ -0,0 +1,213 @@
// Package schemestream holds the parts of streaming custom scheme
// responses to WebView2 that do not need Windows, so that they are tested
// everywhere: Pipe, which carries a response's body from the goroutine of
// its handler to the reads of the webview with bounded memory, and
// ClampRange, which narrows the open ranges media elements ask for.
package schemestream
import (
"errors"
"io"
"strconv"
"strings"
"sync"
"time"
)
// ErrReleased is what writes return once the reader no longer wants the
// body: the webview released the stream, or the response was answered
// another way.
var ErrReleased = errors.New("mygo: the webview no longer reads the response")
// ErrTimeout is what Read returns when it waited the time it was given
// and found nothing to read.
var ErrTimeout = errors.New("mygo: no data to read in time")
// A Pipe carries a body from one writer, the handler's goroutine, to one
// reader, the webview. Write waits while the pipe holds limit bytes or
// more that were not read, so a handler faster than the reader does not
// pile up memory, and Read waits for as many bytes as it asks for, or the
// end of the body, so that a short read means the end as with a stream in
// memory.
type Pipe struct {
mu sync.Mutex
changed chan struct{} // closed and replaced on every change
buf []byte // written, not yet read
limit int
// want is how many bytes a waiting Read asks for: writes go on until
// the pipe holds that many, even past limit, so that Read gets them.
want int
closed bool // the writer is done: the body ends after buf
err error // the body broke off: reads fail
released bool // the reader is gone: writes fail
onFull func()
full bool // onFull was called
}
// NewPipe returns a pipe holding up to about limit bytes. onFull, if not
// nil, is called once, on the writer's goroutine, the first time a Write
// waits: then no reader has taken the body yet, or it reads slower than
// the writer writes.
func NewPipe(limit int, onFull func()) *Pipe {
return &Pipe{changed: make(chan struct{}), limit: limit, onFull: onFull}
}
func (p *Pipe) broadcast() {
close(p.changed)
p.changed = make(chan struct{})
}
// wait releases the lock until the pipe changes or timeout fires, which
// it reports as false.
func (p *Pipe) wait(timeout <-chan time.Time) bool {
ch := p.changed
p.mu.Unlock()
defer p.mu.Lock()
select {
case <-ch:
return true
case <-timeout:
return false
}
}
// Write appends b, after waiting while the pipe is full. It fails once
// the reader is gone or the body broke off.
func (p *Pipe) Write(b []byte) error {
p.mu.Lock()
defer p.mu.Unlock()
for {
switch {
case p.released:
return ErrReleased
case p.err != nil:
return p.err
case p.closed:
return io.ErrClosedPipe
}
if len(p.buf) < max(p.limit, p.want) {
break
}
if !p.full && p.onFull != nil {
p.full = true
p.mu.Unlock()
p.onFull()
p.mu.Lock()
continue
}
p.wait(nil)
}
p.buf = append(p.buf, b...)
p.broadcast()
return nil
}
// Close ends the body after what was written.
func (p *Pipe) Close() {
p.mu.Lock()
defer p.mu.Unlock()
if !p.closed {
p.closed = true
p.broadcast()
}
}
// Fail breaks the body off: reads return err once they took what was
// written, and writes fail.
func (p *Pipe) Fail(err error) {
p.mu.Lock()
defer p.mu.Unlock()
if p.err == nil {
p.err = err
p.broadcast()
}
}
// Release tells the pipe that nothing reads it any more: writes fail from
// now on, and what they wrote is dropped.
func (p *Pipe) Release() {
p.mu.Lock()
defer p.mu.Unlock()
if !p.released {
p.released = true
p.buf = nil
p.broadcast()
}
}
// Take returns what was written and not read, and empties the pipe, for
// a body that ended before anything read it.
func (p *Pipe) Take() []byte {
p.mu.Lock()
defer p.mu.Unlock()
b := p.buf
p.buf = nil
p.broadcast()
return b
}
// Read fills b, waiting until the pipe holds len(b) bytes or the body
// ended. It returns fewer bytes than len(b) with io.EOF at the end of the
// body, and the error of Fail, or ErrReleased, once the body broke off and
// was read up to there. With timeout > 0 it waits at most that long: then
// it returns what it has, and ErrTimeout if that is nothing.
func (p *Pipe) Read(b []byte, timeout time.Duration) (int, error) {
var deadline <-chan time.Time
if timeout > 0 {
t := time.NewTimer(timeout)
defer t.Stop()
deadline = t.C
}
p.mu.Lock()
defer p.mu.Unlock()
for len(p.buf) < len(b) && !p.closed && p.err == nil && !p.released {
p.want = len(b)
p.broadcast() // a writer waiting on a full pipe may write more now
if !p.wait(deadline) {
break
}
}
p.want = 0
n := copy(b, p.buf)
p.buf = p.buf[n:]
if len(p.buf) == 0 {
p.buf = nil // let the chunks go
}
if n > 0 {
p.broadcast()
}
switch {
case n == len(b):
return n, nil
case p.released:
return n, ErrReleased
case p.err != nil:
return n, p.err
case p.closed:
return n, io.EOF
case n == 0:
return 0, ErrTimeout
}
return n, nil
}
// ClampRange narrows a Range header asking for the rest of a resource
// from a byte on, "bytes=X-", to at most n bytes, "bytes=X-(X+n-1)", and
// returns other headers as they are. A media element asking an open range
// takes a shorter answer (206 with its Content-Range) and asks for what
// follows when it needs it, as it would over HTTP.
func ClampRange(h string, n int64) string {
spec, ok := strings.CutPrefix(h, "bytes=")
if !ok || n <= 0 {
return h
}
first, last, ok := strings.Cut(spec, "-")
if !ok || last != "" || first == "" || first[0] < '0' || first[0] > '9' {
return h
}
start, err := strconv.ParseInt(first, 10, 64)
if err != nil || start < 0 || start > (1<<63-1)-n {
return h
}
return "bytes=" + first + "-" + strconv.FormatInt(start+n-1, 10)
}
@@ -0,0 +1,236 @@
package schemestream
import (
"bytes"
"errors"
"io"
"sync/atomic"
"testing"
"time"
)
// readAll reads p as WebView2 reads a response stream: in blocks, until a
// read comes back short.
func readAll(t *testing.T, p *Pipe, block int) ([]byte, error) {
t.Helper()
var out []byte
buf := make([]byte, block)
for {
n, err := p.Read(buf, 0)
out = append(out, buf[:n]...)
if err != nil {
return out, err
}
}
}
func body(n int) []byte {
b := make([]byte, n)
for i := range b {
b[i] = byte(i * 7)
}
return b
}
func TestPipeStreams(t *testing.T) {
const limit, chunk = 64 << 10, 16 << 10
want := body(1<<20 + 123)
var fulls atomic.Int32
p := NewPipe(limit, func() { fulls.Add(1) })
held := make(chan int, 1024)
go func() {
for b := want; len(b) > 0; {
k := min(chunk, len(b))
if err := p.Write(b[:k]); err != nil {
t.Errorf("Write: %v", err)
return
}
b = b[k:]
p.mu.Lock()
held <- len(p.buf)
p.mu.Unlock()
}
p.Close()
close(held)
}()
got, err := readAll(t, p, 10_000)
if err != io.EOF {
t.Fatalf("the last Read: %v, want io.EOF", err)
}
if !bytes.Equal(got, want) {
t.Fatalf("read %d bytes, not the %d written", len(got), len(want))
}
for n := range held {
if n > limit+chunk {
t.Errorf("the pipe held %d bytes, more than limit + a chunk (%d)", n, limit+chunk)
}
}
if n := fulls.Load(); n != 1 {
t.Errorf("onFull called %d times, want 1", n)
}
}
func TestPipeWriteWaitsForReader(t *testing.T) {
full := make(chan struct{})
p := NewPipe(8, func() { close(full) })
if err := p.Write(body(8)); err != nil {
t.Fatal(err)
}
wrote := make(chan error, 1)
go func() { wrote <- p.Write(body(4)) }()
<-full
select {
case err := <-wrote:
t.Fatalf("Write returned (%v) while the pipe was full", err)
case <-time.After(50 * time.Millisecond):
}
buf := make([]byte, 6)
if n, err := p.Read(buf, 0); n != 6 || err != nil {
t.Fatalf("Read = %d, %v", n, err)
}
if err := <-wrote; err != nil {
t.Fatalf("Write after a read: %v", err)
}
}
func TestPipeReadWaitsForFullBlock(t *testing.T) {
p := NewPipe(1<<20, nil)
done := make(chan int, 1)
go func() {
n, _ := p.Read(make([]byte, 10), 0)
done <- n
}()
p.Write(body(4))
select {
case n := <-done:
t.Fatalf("Read returned %d bytes before the block was there", n)
case <-time.After(30 * time.Millisecond):
}
p.Write(body(6))
if n := <-done; n != 10 {
t.Fatalf("Read = %d, want 10", n)
}
}
// A Read asking more than the limit is not left waiting on a writer
// waiting on it.
func TestPipeReadLargerThanLimit(t *testing.T) {
p := NewPipe(4, nil)
go func() {
for range 10 {
if err := p.Write(body(3)); err != nil {
return
}
}
p.Close()
}()
got, err := readAll(t, p, 16)
if err != io.EOF || len(got) != 30 {
t.Fatalf("read %d bytes, %v; want 30, io.EOF", len(got), err)
}
}
func TestPipeShortReadAtEnd(t *testing.T) {
p := NewPipe(100, nil)
p.Write([]byte("hello"))
p.Close()
buf := make([]byte, 3)
if n, err := p.Read(buf, 0); n != 3 || err != nil {
t.Fatalf("Read = %d, %v; want 3, nil", n, err)
}
if n, err := p.Read(buf, 0); n != 2 || err != io.EOF {
t.Fatalf("Read = %d, %v; want 2, io.EOF", n, err)
}
if n, err := p.Read(buf, 0); n != 0 || err != io.EOF {
t.Fatalf("Read at the end = %d, %v; want 0, io.EOF", n, err)
}
if err := p.Write([]byte("x")); err == nil {
t.Error("Write after Close succeeded")
}
}
func TestPipeReleaseFailsWrites(t *testing.T) {
p := NewPipe(4, nil)
p.Write(body(4))
wrote := make(chan error, 1)
go func() { wrote <- p.Write(body(4)) }() // waits: full
time.Sleep(20 * time.Millisecond)
p.Release()
if err := <-wrote; !errors.Is(err, ErrReleased) {
t.Fatalf("a waiting Write after Release: %v, want ErrReleased", err)
}
if err := p.Write(body(1)); !errors.Is(err, ErrReleased) {
t.Fatalf("Write after Release: %v, want ErrReleased", err)
}
}
func TestPipeFail(t *testing.T) {
p := NewPipe(100, nil)
p.Write([]byte("abc"))
boom := errors.New("boom")
done := make(chan error, 1)
go func() {
_, err := p.Read(make([]byte, 10), 0)
done <- err
}()
time.Sleep(10 * time.Millisecond)
p.Fail(boom)
if err := <-done; err != boom {
t.Fatalf("Read after Fail: %v, want boom", err)
}
if err := p.Write([]byte("x")); err != boom {
t.Fatalf("Write after Fail: %v, want boom", err)
}
}
func TestPipeReadTimeout(t *testing.T) {
p := NewPipe(100, nil)
start := time.Now()
n, err := p.Read(make([]byte, 10), 20*time.Millisecond)
if n != 0 || err != ErrTimeout {
t.Fatalf("Read of an empty pipe = %d, %v; want 0, ErrTimeout", n, err)
}
if d := time.Since(start); d < 20*time.Millisecond {
t.Fatalf("Read returned after %v, before the timeout", d)
}
p.Write([]byte("ab"))
if n, err := p.Read(make([]byte, 10), 20*time.Millisecond); n != 2 || err != nil {
t.Fatalf("Read with some data = %d, %v; want 2, nil", n, err)
}
}
func TestPipeTake(t *testing.T) {
p := NewPipe(100, nil)
p.Write([]byte("ab"))
p.Write([]byte("cd"))
if got := string(p.Take()); got != "abcd" {
t.Fatalf("Take = %q", got)
}
if got := p.Take(); len(got) != 0 {
t.Fatalf("Take again = %q", got)
}
}
func TestClampRange(t *testing.T) {
const mb = 1 << 20
for _, c := range []struct{ in, want string }{
{"bytes=0-", "bytes=0-1048575"},
{"bytes=318963712-", "bytes=318963712-320012287"},
{"bytes=0-99", "bytes=0-99"}, // closed: as asked
{"bytes=0-99999999", "bytes=0-99999999"}, // closed, however long
{"bytes=-500", "bytes=-500"}, // the last bytes
{"bytes=0-,10-", "bytes=0-,10-"}, // several ranges
{"bytes=+5-", "bytes=+5-"}, // not a number
{"bytes=x-", "bytes=x-"}, // not a number
{"items=0-", "items=0-"}, // another unit
{"", ""}, // no range
{"bytes=9223372036854775807-", "bytes=9223372036854775807-"}, // would overflow
} {
if got := ClampRange(c.in, mb); got != c.want {
t.Errorf("ClampRange(%q) = %q, want %q", c.in, got, c.want)
}
}
if got := ClampRange("bytes=0-", 0); got != "bytes=0-" {
t.Errorf("ClampRange with n = 0: %q", got)
}
}