Skip to content

Commit a4d4a3f

Browse files
author
Ryan Vogel
committed
fix(pairing): shrink mobile QR payload
1 parent e8b4eb8 commit a4d4a3f

4 files changed

Lines changed: 24 additions & 22 deletions

File tree

packages/mobile-voice/notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
- Need to figure out a good way to start new sessions.
55
- When an agent returns a generation, we should be able to expand it into a reader mode view.
66
- Work on the live activity widget.
7+
- In the OpenCode Control app, if a link is generated in Markdown, it should be tappable and open in the device's default browser.

packages/mobile-voice/src/app/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ function formatWorkingDirectory(directory?: string): string {
370370
type DropdownMode = "none" | "server" | "session"
371371

372372
type Pair = {
373-
v: 1
374373
serverID?: string
375374
relayURL: string
376375
relaySecret: string
@@ -454,18 +453,19 @@ type Cam = {
454453

455454
function parsePairShape(data: unknown): Pair | undefined {
456455
if (!data || typeof data !== "object") return
457-
if ((data as { v?: unknown }).v !== 1) return
458-
if (typeof (data as { relayURL?: unknown }).relayURL !== "string") return
456+
const version = (data as { v?: unknown }).v
457+
if (version !== undefined && version !== 1) return
459458
if (typeof (data as { relaySecret?: unknown }).relaySecret !== "string") return
460459
if (!Array.isArray((data as { hosts?: unknown }).hosts)) return
461460
const hosts = (data as { hosts: unknown[] }).hosts.filter((item): item is string => typeof item === "string")
462461
if (!hosts.length) return
462+
const relayURLRaw = (data as { relayURL?: unknown }).relayURL
463+
const relayURL = typeof relayURLRaw === "string" && relayURLRaw.length > 0 ? relayURLRaw : DEFAULT_RELAY_URL
463464
const serverIDRaw = (data as { serverID?: unknown }).serverID
464465
const serverID = typeof serverIDRaw === "string" && serverIDRaw.length > 0 ? serverIDRaw : undefined
465466
return {
466-
v: 1,
467467
serverID,
468-
relayURL: (data as { relayURL: string }).relayURL,
468+
relayURL,
469469
relaySecret: (data as { relaySecret: string }).relaySecret,
470470
hosts,
471471
}

packages/opencode/src/cli/cmd/serve.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ import * as QRCode from "qrcode"
1515
const log = Log.create({ service: "serve" })
1616

1717
type PairPayload = {
18-
v: 1
1918
serverID?: string
2019
relayURL: string
2120
relaySecret: string
2221
hosts: string[]
2322
}
2423

24+
type PairQRCodePayload = {
25+
relaySecret: string
26+
hosts: string[]
27+
}
28+
2529
type TailscaleStatus = {
2630
Self?: {
2731
DNSName?: unknown
@@ -102,12 +106,12 @@ function hosts(hostname: string, port: number, advertised: string[] = [], includ
102106
return [...preferred, ...entries.map((item) => item.url)]
103107
}
104108

105-
function pairLink(pair: unknown) {
106-
return `mobilevoice:///?pair=${encodeURIComponent(JSON.stringify(pair))}`
107-
}
108-
109-
function pairServerID(input: { relayURL: string; relaySecret: string }) {
110-
return createHash("sha256").update(`${input.relayURL}|${input.relaySecret}`).digest("hex").slice(0, 16)
109+
function pairLink(pair: PairQRCodePayload) {
110+
const payload: PairQRCodePayload = {
111+
relaySecret: pair.relaySecret,
112+
hosts: pair.hosts,
113+
}
114+
return `mobilevoice:///?pair=${encodeURIComponent(JSON.stringify(payload))}`
111115
}
112116

113117
function secretHash(input: string) {
@@ -240,8 +244,6 @@ export const ServeCommand = cmd({
240244

241245
console.log("printing connect qr without starting the server")
242246
await printPairQR({
243-
v: 1,
244-
serverID: pairServerID({ relayURL, relaySecret }),
245247
relayURL,
246248
relaySecret,
247249
hosts: pairHosts,
@@ -274,8 +276,6 @@ export const ServeCommand = cmd({
274276
})
275277
const pair = started ??
276278
PushRelay.pair() ?? {
277-
v: 1 as const,
278-
serverID: pairServerID({ relayURL, relaySecret }),
279279
relayURL,
280280
relaySecret,
281281
hosts: hosts(host, port, advertiseHosts),

packages/opencode/src/server/routes/experimental.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ import { Agent } from "@/agent/agent"
2121

2222
const PushPairPayload = z
2323
.object({
24-
v: z.literal(1),
25-
serverID: z.string().optional(),
26-
relayURL: z.string(),
2724
relaySecret: z.string(),
2825
hosts: z.array(z.string()),
2926
})
@@ -49,12 +46,16 @@ const pushPairQROptions = {
4946
width: 256,
5047
}
5148

52-
function pushPairLink(payload: z.infer<typeof PushPairPayload>) {
49+
function pushPairLink(input: { relaySecret: string; hosts: string[] }) {
50+
const payload: z.infer<typeof PushPairPayload> = {
51+
relaySecret: input.relaySecret,
52+
hosts: input.hosts,
53+
}
5354
return `mobilevoice:///?pair=${encodeURIComponent(JSON.stringify(payload))}`
5455
}
5556

56-
async function pushPairQRCode(payload: z.infer<typeof PushPairPayload>) {
57-
return QRCode.toDataURL(pushPairLink(payload), pushPairQROptions)
57+
async function pushPairQRCode(input: { relaySecret: string; hosts: string[] }) {
58+
return QRCode.toDataURL(pushPairLink(input), pushPairQROptions)
5859
}
5960

6061
const ConsoleOrgOption = z.object({

0 commit comments

Comments
 (0)