fix(pairing): shrink mobile QR payload
This commit is contained in:
parent
e8b4eb8972
commit
a4d4a3f545
4 changed files with 24 additions and 22 deletions
|
|
@ -4,3 +4,4 @@
|
||||||
- Need to figure out a good way to start new sessions.
|
- Need to figure out a good way to start new sessions.
|
||||||
- When an agent returns a generation, we should be able to expand it into a reader mode view.
|
- When an agent returns a generation, we should be able to expand it into a reader mode view.
|
||||||
- Work on the live activity widget.
|
- Work on the live activity widget.
|
||||||
|
- In the OpenCode Control app, if a link is generated in Markdown, it should be tappable and open in the device's default browser.
|
||||||
|
|
|
||||||
|
|
@ -370,7 +370,6 @@ function formatWorkingDirectory(directory?: string): string {
|
||||||
type DropdownMode = "none" | "server" | "session"
|
type DropdownMode = "none" | "server" | "session"
|
||||||
|
|
||||||
type Pair = {
|
type Pair = {
|
||||||
v: 1
|
|
||||||
serverID?: string
|
serverID?: string
|
||||||
relayURL: string
|
relayURL: string
|
||||||
relaySecret: string
|
relaySecret: string
|
||||||
|
|
@ -454,18 +453,19 @@ type Cam = {
|
||||||
|
|
||||||
function parsePairShape(data: unknown): Pair | undefined {
|
function parsePairShape(data: unknown): Pair | undefined {
|
||||||
if (!data || typeof data !== "object") return
|
if (!data || typeof data !== "object") return
|
||||||
if ((data as { v?: unknown }).v !== 1) return
|
const version = (data as { v?: unknown }).v
|
||||||
if (typeof (data as { relayURL?: unknown }).relayURL !== "string") return
|
if (version !== undefined && version !== 1) return
|
||||||
if (typeof (data as { relaySecret?: unknown }).relaySecret !== "string") return
|
if (typeof (data as { relaySecret?: unknown }).relaySecret !== "string") return
|
||||||
if (!Array.isArray((data as { hosts?: unknown }).hosts)) return
|
if (!Array.isArray((data as { hosts?: unknown }).hosts)) return
|
||||||
const hosts = (data as { hosts: unknown[] }).hosts.filter((item): item is string => typeof item === "string")
|
const hosts = (data as { hosts: unknown[] }).hosts.filter((item): item is string => typeof item === "string")
|
||||||
if (!hosts.length) return
|
if (!hosts.length) return
|
||||||
|
const relayURLRaw = (data as { relayURL?: unknown }).relayURL
|
||||||
|
const relayURL = typeof relayURLRaw === "string" && relayURLRaw.length > 0 ? relayURLRaw : DEFAULT_RELAY_URL
|
||||||
const serverIDRaw = (data as { serverID?: unknown }).serverID
|
const serverIDRaw = (data as { serverID?: unknown }).serverID
|
||||||
const serverID = typeof serverIDRaw === "string" && serverIDRaw.length > 0 ? serverIDRaw : undefined
|
const serverID = typeof serverIDRaw === "string" && serverIDRaw.length > 0 ? serverIDRaw : undefined
|
||||||
return {
|
return {
|
||||||
v: 1,
|
|
||||||
serverID,
|
serverID,
|
||||||
relayURL: (data as { relayURL: string }).relayURL,
|
relayURL,
|
||||||
relaySecret: (data as { relaySecret: string }).relaySecret,
|
relaySecret: (data as { relaySecret: string }).relaySecret,
|
||||||
hosts,
|
hosts,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,13 +15,17 @@ import * as QRCode from "qrcode"
|
||||||
const log = Log.create({ service: "serve" })
|
const log = Log.create({ service: "serve" })
|
||||||
|
|
||||||
type PairPayload = {
|
type PairPayload = {
|
||||||
v: 1
|
|
||||||
serverID?: string
|
serverID?: string
|
||||||
relayURL: string
|
relayURL: string
|
||||||
relaySecret: string
|
relaySecret: string
|
||||||
hosts: string[]
|
hosts: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PairQRCodePayload = {
|
||||||
|
relaySecret: string
|
||||||
|
hosts: string[]
|
||||||
|
}
|
||||||
|
|
||||||
type TailscaleStatus = {
|
type TailscaleStatus = {
|
||||||
Self?: {
|
Self?: {
|
||||||
DNSName?: unknown
|
DNSName?: unknown
|
||||||
|
|
@ -102,12 +106,12 @@ function hosts(hostname: string, port: number, advertised: string[] = [], includ
|
||||||
return [...preferred, ...entries.map((item) => item.url)]
|
return [...preferred, ...entries.map((item) => item.url)]
|
||||||
}
|
}
|
||||||
|
|
||||||
function pairLink(pair: unknown) {
|
function pairLink(pair: PairQRCodePayload) {
|
||||||
return `mobilevoice:///?pair=${encodeURIComponent(JSON.stringify(pair))}`
|
const payload: PairQRCodePayload = {
|
||||||
}
|
relaySecret: pair.relaySecret,
|
||||||
|
hosts: pair.hosts,
|
||||||
function pairServerID(input: { relayURL: string; relaySecret: string }) {
|
}
|
||||||
return createHash("sha256").update(`${input.relayURL}|${input.relaySecret}`).digest("hex").slice(0, 16)
|
return `mobilevoice:///?pair=${encodeURIComponent(JSON.stringify(payload))}`
|
||||||
}
|
}
|
||||||
|
|
||||||
function secretHash(input: string) {
|
function secretHash(input: string) {
|
||||||
|
|
@ -240,8 +244,6 @@ export const ServeCommand = cmd({
|
||||||
|
|
||||||
console.log("printing connect qr without starting the server")
|
console.log("printing connect qr without starting the server")
|
||||||
await printPairQR({
|
await printPairQR({
|
||||||
v: 1,
|
|
||||||
serverID: pairServerID({ relayURL, relaySecret }),
|
|
||||||
relayURL,
|
relayURL,
|
||||||
relaySecret,
|
relaySecret,
|
||||||
hosts: pairHosts,
|
hosts: pairHosts,
|
||||||
|
|
@ -274,8 +276,6 @@ export const ServeCommand = cmd({
|
||||||
})
|
})
|
||||||
const pair = started ??
|
const pair = started ??
|
||||||
PushRelay.pair() ?? {
|
PushRelay.pair() ?? {
|
||||||
v: 1 as const,
|
|
||||||
serverID: pairServerID({ relayURL, relaySecret }),
|
|
||||||
relayURL,
|
relayURL,
|
||||||
relaySecret,
|
relaySecret,
|
||||||
hosts: hosts(host, port, advertiseHosts),
|
hosts: hosts(host, port, advertiseHosts),
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,6 @@ import { Agent } from "@/agent/agent"
|
||||||
|
|
||||||
const PushPairPayload = z
|
const PushPairPayload = z
|
||||||
.object({
|
.object({
|
||||||
v: z.literal(1),
|
|
||||||
serverID: z.string().optional(),
|
|
||||||
relayURL: z.string(),
|
|
||||||
relaySecret: z.string(),
|
relaySecret: z.string(),
|
||||||
hosts: z.array(z.string()),
|
hosts: z.array(z.string()),
|
||||||
})
|
})
|
||||||
|
|
@ -49,12 +46,16 @@ const pushPairQROptions = {
|
||||||
width: 256,
|
width: 256,
|
||||||
}
|
}
|
||||||
|
|
||||||
function pushPairLink(payload: z.infer<typeof PushPairPayload>) {
|
function pushPairLink(input: { relaySecret: string; hosts: string[] }) {
|
||||||
|
const payload: z.infer<typeof PushPairPayload> = {
|
||||||
|
relaySecret: input.relaySecret,
|
||||||
|
hosts: input.hosts,
|
||||||
|
}
|
||||||
return `mobilevoice:///?pair=${encodeURIComponent(JSON.stringify(payload))}`
|
return `mobilevoice:///?pair=${encodeURIComponent(JSON.stringify(payload))}`
|
||||||
}
|
}
|
||||||
|
|
||||||
async function pushPairQRCode(payload: z.infer<typeof PushPairPayload>) {
|
async function pushPairQRCode(input: { relaySecret: string; hosts: string[] }) {
|
||||||
return QRCode.toDataURL(pushPairLink(payload), pushPairQROptions)
|
return QRCode.toDataURL(pushPairLink(input), pushPairQROptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
const ConsoleOrgOption = z.object({
|
const ConsoleOrgOption = z.object({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue