// The phone for test/pic/remote-interop.sh: the relay page's own protocol module (pic-relay,
// src/protocol) pairing with and driving a real pic remote through the real Worker. Bundled by the
// script with esbuild; "relay-protocol" is $PIC_RELAY_DIR/src/protocol.
// Usage: node phone.mjs ORIGIN OFFER_URL NAME DIR
// It writes DIR/code when the pairing code is up and DIR/paired once paired, and prints one line per
// step ("opened", "listed", "followed", "prompted"); it forgets the computer when DIR/unpair appears.
import { writeFileSync, existsSync } from "node:fs";
import { Connection, MemoryStore, pair } from "relay-protocol/client.js";
const [origin, link, name, dir] = process.argv.slice(2) as [string, string, string, string];
const say = (s: string) => process.stdout.write(s + "\n");
const die = (s: string): never => {
process.stderr.write(`phone: ${s}\n`);
process.exit(1);
};
setTimeout(() => die("deadline"), 150_000).unref();
function until<T>(f: () => T | undefined | null | false, ms: number, what: string): Promise<T> {
return new Promise((resolve, reject) => {
const t0 = Date.now();
const tick = () => {
const v = f();
if (v) return resolve(v);
if (Date.now() - t0 > ms) return reject(new Error(`timed out waiting for ${what}`));
setTimeout(tick, 25);
};
tick();
});
}
const store = new MemoryStore();
const deps = { fetch: (u: string, i?: RequestInit) => fetch(u, i), origin, store };
try {
const pairing = await pair(deps, link, name, (s) => {
if (s.step === "code") writeFileSync(`${dir}/code`, s.code);
if (s.step === "failed") process.stderr.write(`pair failed: ${s.reason}\n`);
});
writeFileSync(`${dir}/paired`, pairing.picName);
say(`paired ${pairing.picName}`);
const errors: string[] = [];
const conn = new Connection(deps, pairing, (e) => {
if (e.kind === "error") errors.push(`${e.code} ${e.message ?? ""}`);
});
await conn.start();
await until(() => conn.phase === "open", 40_000, "an open session");
say("opened");
await until(() => conn.sessions.length >= 1, 20_000, "the conversation list");
const info = conn.sessions[0]!;
say(`listed ${conn.sessions.length} ${info.session}`);
conn.follow(info.session);
const replica = await until(() => (conn.replica?.synced ? conn.replica : null), 20_000, "the first mark");
const types = replica.entries.map((e) => e.event.type);
if (!types.includes("agent_end")) die(`sync: no agent_end in ${JSON.stringify(types)}`);
say(`followed ${replica.cursor} ${types.join(",")}`);
const before = replica.entries.length;
const cmd = conn.say(info.session, "second question");
await until(() => cmd.response !== undefined || cmd.error, 30_000, "the prompt's answer");
if (cmd.error) die(`prompt refused: ${JSON.stringify(cmd.error)}`);
await until(() => replica.entries.length > before && replica.entries.at(-1)?.event.type === "agent_end", 40_000, "the reply's events");
say(`prompted ${replica.entries.slice(before).map((e) => e.event.type).join(",")}`);
if (errors.length) die(`errors: ${errors.join("; ")}`);
await until(() => existsSync(`${dir}/unpair`), 60_000, "the go-ahead to unpair");
await conn.forget();
say("forgotten");
process.exit(0);
} catch (e) {
die(String((e as Error)?.message ?? e));
}