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.
65 lines
2.8 KiB
TypeScript
65 lines
2.8 KiB
TypeScript
// A Go side for the plugins' tests: the real page runtime of the bridge,
|
|
// installed as window.mygo, whose calls go to handlers written in
|
|
// TypeScript that stream through channels as Go methods do.
|
|
import { createRuntime } from "../packages/bridge/src/runtime";
|
|
import type { Outgoing } from "../packages/bridge/src/types";
|
|
|
|
/** A call being handled. */
|
|
export interface GoCall {
|
|
args: unknown[];
|
|
/** Sends a value on the channel passed as the i-th argument. */
|
|
send(i: number, value: unknown): void;
|
|
/** Aborted when the page closes a channel of the call. */
|
|
signal: AbortSignal;
|
|
}
|
|
|
|
export type Handler = (call: GoCall) => unknown;
|
|
|
|
/** Methods whose argument 1 is a channel, as those of the plugins. */
|
|
const streaming = new Set(["plugin:fetch.Fetch", "plugin:websocket.Connect", "plugin:watch.Watch"]);
|
|
|
|
/** Installs window.mygo with handlers for methods; returns what was called. */
|
|
export function fakeGo(handlers: Record<string, Handler>): { calls: [string, unknown[]][]; uninstall(): void } {
|
|
const calls: [string, unknown[]][] = [];
|
|
const aborts = new Map<number, AbortController>();
|
|
const { runtime, internal } = createRuntime(
|
|
{ platform: "darwin", windowId: 1, version: "0.0.0-test", secret: "s", locale: "en", dir: "ltr" },
|
|
(raw) => {
|
|
const m = JSON.parse(raw) as Outgoing;
|
|
if (m.t === "chan-close") aborts.get(m.c)?.abort();
|
|
if (m.t !== "call") return;
|
|
calls.push([m.m, m.a]);
|
|
const abort = new AbortController();
|
|
const chans = m.a.map((a, i) => (streaming.has(m.m) && i === 1 && handlers[m.m] ? (a as number) : 0));
|
|
chans.forEach((c) => c && aborts.set(c, abort));
|
|
// Like Go: a goroutine per call, the channels end before the reply.
|
|
setTimeout(async () => {
|
|
const handler = handlers[m.m];
|
|
let reply: { ok: boolean; v?: unknown; e?: string };
|
|
try {
|
|
if (!handler) throw new Error(`method ${m.m} is not bound`);
|
|
const v = await handler({
|
|
args: m.a,
|
|
send(i, value) {
|
|
if (abort.signal.aborted) throw new Error("mygo: channel closed");
|
|
internal.receive({ t: "chan", c: chans[i]!, k: m.k, p: value });
|
|
},
|
|
signal: abort.signal,
|
|
});
|
|
reply = { ok: true, v };
|
|
} catch (err) {
|
|
reply = { ok: false, e: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
for (const c of chans) if (c) internal.receive({ t: "chan", c, k: m.k, end: true });
|
|
internal.receive({ t: "reply", id: m.id, k: m.k, ...reply });
|
|
});
|
|
},
|
|
);
|
|
(globalThis as { mygo?: unknown }).mygo = runtime;
|
|
return { calls, uninstall: () => delete (globalThis as { mygo?: unknown }).mygo };
|
|
}
|
|
|
|
export const tick = () => new Promise((r) => setTimeout(r, 1));
|
|
|
|
export const b64 = (s: string) => Buffer.from(s).toString("base64");
|