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

+79
View File
@@ -0,0 +1,79 @@
package dev.mygo
import android.Manifest
import android.os.Build
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import org.json.JSONObject
class PushPlugin : MyGoPlugin {
private var listening = false
init {
PluginHost.onIntent = { if (listening) opened(it) }
}
private fun opened(i: android.content.Intent) {
val extras = i.extras ?: return
if (!extras.containsKey("google.message_id")) return
val o = JSONObject()
for (k in extras.keySet()) extras.getString(k)?.let { o.put(k, it) }
i.removeExtra("google.message_id") // once, as an Activity made again has it still
PluginHost.emit("push", "opened", o)
}
override fun call(method: String, args: JSONObject, reply: PluginReply) {
when (method) {
"configure" -> {
if (FirebaseApp.getApps(PluginHost.context).isEmpty()) {
FirebaseApp.initializeApp(PluginHost.context, FirebaseOptions.Builder()
.setProjectId(args.optString("projectId"))
.setApplicationId(args.optString("appId"))
.setApiKey(args.optString("apiKey"))
.setGcmSenderId(args.optString("senderId"))
.build())
}
reply.success(null)
}
"register" -> {
val perms = if (Build.VERSION.SDK_INT >= 33) arrayOf(Manifest.permission.POST_NOTIFICATIONS) else emptyArray()
PluginHost.requestPermissions(perms) { granted ->
if (!granted) {
reply.failure(PluginFailure("Denied", "notifications are not allowed"))
return@requestPermissions
}
if (FirebaseApp.getApps(PluginHost.context).isEmpty()) {
reply.failure(PluginFailure("NoFirebase", "call push.Configure first"))
return@requestPermissions
}
FirebaseMessaging.getInstance().token.addOnCompleteListener { t ->
if (t.isSuccessful) reply.success(JSONObject().put("token", t.result))
else reply.failure(PluginFailure("Failed", t.exception?.toString() ?: ""))
}
}
}
"listenOpened" -> {
// What Firebase opens the app with, as the user tapped a notification.
listening = true
PluginHost.activity?.intent?.let { opened(it) }
reply.success(null)
}
else -> reply.failure(PluginFailure("NoMethod", method))
}
}
}
/** What Firebase hears of the app's messages. */
class PushService : FirebaseMessagingService() {
override fun onNewToken(token: String) = PluginHost.emit("push", "token", token)
override fun onMessageReceived(m: RemoteMessage) {
val o = JSONObject()
for ((k, v) in m.data) o.put(k, v)
m.notification?.let { o.put("title", it.title).put("body", it.body) }
PluginHost.emit("push", "message", o)
}
}
+82
View File
@@ -0,0 +1,82 @@
import UIKit
import UserNotifications
final class PushPlugin: MyGoPlugin {
private var waiting: PluginReply?
private var lastMessage = ""
private var lastMessageAt = Date.distantPast
/// A tap that launched the app comes before Go listens.
private var opened: [[String: Any]] = []
private var listening = false
private func plain(_ info: [AnyHashable: Any]) -> [String: Any] {
var out: [String: Any] = [:]
for (k, v) in info where JSONSerialization.isValidJSONObject([v]) { out["\(k)"] = v }
return out
}
func call(_ method: String, _ args: [String: Any], _ reply: @escaping PluginReply) {
switch method {
case "configure":
reply(.success(nil))
case "register":
waiting = reply
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
DispatchQueue.main.async {
guard granted else {
self.waiting?(.failure(PluginFailure("Denied", error?.localizedDescription ?? "notifications are not allowed")))
self.waiting = nil
return
}
UIApplication.shared.registerForRemoteNotifications()
}
}
case "listenOpened":
listening = true
for o in opened { PluginHost.emit("push", "opened", o) }
opened = []
reply(.success(nil))
default:
reply(.failure(PluginFailure("NoMethod", method)))
}
}
func applicationEvent(_ event: ApplicationEvent) {
switch event {
case .pushToken(let d):
let token = d.map { String(format: "%02x", $0) }.joined()
if let r = waiting {
r(.success(["token": token]))
waiting = nil
}
PluginHost.emit("push", "token", token)
case .pushError(let e):
waiting?(.failure(PluginFailure("Failed", e.localizedDescription)))
waiting = nil
case .pushMessage(let info):
// UIKit tells some twice: as it arrives, and as it would show.
let key = String(describing: info)
let now = Date()
if key == lastMessage && now.timeIntervalSince(lastMessageAt) < 2 { return }
lastMessage = key
lastMessageAt = now
PluginHost.emit("push", "message", plain(info))
case .pushOpened(let info):
if listening {
PluginHost.emit("push", "opened", plain(info))
} else {
opened.append(plain(info))
}
}
}
}
/// Only apps with this plugin have it: UIKit warns of a delegate with it and no
/// remote-notification background mode.
extension AppDelegate {
func application(_ application: UIApplication, didReceiveRemoteNotification info: [AnyHashable: Any],
fetchCompletionHandler done: @escaping (UIBackgroundFetchResult) -> Void) {
PluginHost.applicationEvent(.pushMessage(info))
done(.newData)
}
}
+31
View File
@@ -0,0 +1,31 @@
{
"mobile": {
"id": "push",
"ios": {
"sources": [
"ios/*.swift"
],
"class": "PushPlugin",
"infoPlist": {
"UIBackgroundModes": [
"remote-notification"
]
},
"entitlements": {
"aps-environment": "development"
}
},
"android": {
"sources": [
"android/*.kt"
],
"class": "PushPlugin",
"dependencies": [
"com.google.firebase:firebase-messaging:24.1.2"
],
"application": [
" <service android:name=\"dev.mygo.PushService\" android:exported=\"false\"><intent-filter><action android:name=\"com.google.firebase.MESSAGING_EVENT\" /></intent-filter></service>"
]
}
}
}
+67
View File
@@ -0,0 +1,67 @@
// Package push receives push notifications on phones: APNs on iOS, and Firebase
// Cloud Messaging on Android. It tells the token that the app's server sends to.
//
// iOS needs the Push Notifications capability in the app's provisioning
// profile. Android needs a Firebase project: give Configure its values, as in
// its google-services.json. Elsewhere its functions report
// errors.ErrUnsupported.
package push
import (
"context"
"encoding/json"
"github.com/egoist/mygo"
)
const pluginID = "push"
// Firebase is what Android's messaging needs of the app's Firebase project.
type Firebase struct {
ProjectID string `json:"projectId"`
AppID string `json:"appId"`
APIKey string `json:"apiKey"`
SenderID string `json:"senderId"`
}
// Configure sets up Firebase, on Android. Call it before Register. iOS ignores
// it.
func Configure(ctx context.Context, f Firebase) error {
return mygo.Native(pluginID).Call(ctx, "configure", f, nil)
}
// Register asks the user to allow notifications, and returns the token that
// the app's server sends them to, which may change: see OnToken. Call it off
// the main goroutine, as it waits for the user.
func Register(ctx context.Context) (string, error) {
var r struct {
Token string `json:"token"`
}
err := mygo.Native(pluginID).Call(ctx, "register", nil, &r)
return r.Token, err
}
// OnToken calls fn on the main thread as the token changes.
func OnToken(fn func(token string)) (off func()) {
return mygo.Native(pluginID).On("token", func(data []byte) {
var s string
if json.Unmarshal(data, &s) == nil {
fn(s)
}
})
}
// OnMessage calls fn on the main thread as a push arrives while the app runs,
// with what it carries, as JSON.
func OnMessage(fn func(data []byte)) (off func()) { return mygo.Native(pluginID).On("message", fn) }
// OnOpened calls fn on the main thread as the user taps a push's notification, with
// what it carries, as JSON: also the tap that launched the app, once this listens,
// so call it as the app is ready.
func OnOpened(fn func(data []byte)) (off func()) {
off = mygo.Native(pluginID).On("opened", fn)
mygo.App.WhenReady(func() {
go mygo.Native(pluginID).Call(context.Background(), "listenOpened", nil, nil)
})
return off
}