diff --git a/scripts/ui-fixtures.mjs b/scripts/ui-fixtures.mjs index abbe8ca..e2aba40 100644 --- a/scripts/ui-fixtures.mjs +++ b/scripts/ui-fixtures.mjs @@ -26,6 +26,40 @@ export const MOCK_DAEMON_ID = "d-mock-0f3a9c"; const MINUTE = 60_000; +/** + * 21×21 module grid for the mock pairing code (`rows` are `"0"`/`"1"` strings, + * no quiet zone). This is **not** a real QR encoding — the daemon's encoder + * lives in `src/core/qr.ts`; the harness only needs a plausible grid with the + * three finder patterns so the status page renders a real matrix. + */ +function mockQrRows(text, size = 21) { + const grid = Array.from({ length: size }, () => Array.from({ length: size }, () => "0")); + const finder = (ox, oy) => { + for (let y = 0; y < 7; y += 1) { + for (let x = 0; x < 7; x += 1) { + const ring = x === 0 || y === 0 || x === 6 || y === 6; + const core = x >= 2 && x <= 4 && y >= 2 && y <= 4; + grid[oy + y][ox + x] = ring || core ? "1" : "0"; + } + } + }; + finder(0, 0); + finder(size - 7, 0); + finder(0, size - 7); + + let state = 2166136261; + for (const char of text) state = (state * 31 + char.charCodeAt(0)) >>> 0; + for (let y = 0; y < size; y += 1) { + for (let x = 0; x < size; x += 1) { + const inFinder = (x < 8 && y < 8) || (x > size - 9 && y < 8) || (x < 8 && y > size - 9); + if (inFinder) continue; + state = (state * 1103515245 + 12345) >>> 0; + grid[y][x] = state % 100 < 48 ? "1" : "0"; + } + } + return grid.map((row) => row.join("")); +} + /** Fixed "now" for the running instance; callers may override. */ export function buildFixtures(now = Date.now()) { const iso = (ts) => new Date(ts).toISOString(); @@ -785,7 +819,6 @@ export function buildFixtures(now = Date.now()) { deviceId: "dev-local", name: "local browser", kind: "laptop", - scopes: ["read", "steer", "apply"], lastSeenAt: now - 15_000, lastActiveAt: now - 15_000, createdAt: now - 26 * 3_600_000, @@ -796,7 +829,6 @@ export function buildFixtures(now = Date.now()) { deviceId: "dev-phone", name: "Pixel 8 · pi2", kind: "phone", - scopes: ["read", "steer"], lastSeenAt: now - 47 * MINUTE, lastActiveAt: now - 47 * MINUTE, createdAt: now - 9 * 86_400_000, @@ -807,7 +839,6 @@ export function buildFixtures(now = Date.now()) { deviceId: "dev-tablet", name: "iPad Air", kind: "tablet", - scopes: ["read"], lastSeenAt: now - 6 * 86_400_000, lastActiveAt: now - 6 * 86_400_000, createdAt: now - 40 * 86_400_000, @@ -885,6 +916,21 @@ export function buildFixtures(now = Date.now()) { const sessionById = new Map(sessions.map((session) => [session.sessionId, session])); + // `POST /api/v1/pair` mint payload: the daemon picks the URL and encodes it + // server-side, so the client never re-derives either (docs/protocol.md §3.2). + const pairCode = { + accepted: true, + code: "QK7M-2XPD", + url: "http://192.168.2.10:4321/pair/QK7M2XPD", + expiresAt: now + 10 * MINUTE, + nonce: "n-9f2c1a", + qr: { version: 1, ecc: "M", mask: 3, size: 21, rows: mockQrRows("http://192.168.2.10:4321/pair/QK7M2XPD") }, + bases: [ + { kind: "loopback", name: "lo", address: "127.0.0.1:4321", url: "http://127.0.0.1:4321" }, + { kind: "lan", name: "eth0", address: "192.168.2.10:4321", url: "http://192.168.2.10:4321" }, + ], + }; + return { info: { ...info, port: undefined }, health, @@ -896,14 +942,8 @@ export function buildFixtures(now = Date.now()) { modelProviders, resources, diagnostics, - /** `GET /api/v1/pair` mint response (no code in the body). */ - pairCode: { - accepted: true, - code: "QK7M-2XPD", - url: "http://127.0.0.1:4321/pair/QK7M-2XPD", - expiresAt: now + 10 * MINUTE, - nonce: "n-9f2c1a", - }, + /** `POST /api/v1/pair` mint response (code + QR matrix + reachable bases). */ + pairCode, /** `attention` flattened per workspace for `GET /api/v1/attention`. */ attentionByWorkspace: (workspaceId) => sessions diff --git a/scripts/ui-mock-server.mjs b/scripts/ui-mock-server.mjs index e731976..1b43e38 100644 --- a/scripts/ui-mock-server.mjs +++ b/scripts/ui-mock-server.mjs @@ -196,7 +196,6 @@ const DEVICE_PUBLIC = { id: "dev-local", name: "local browser", kind: "laptop", - scopes: ["read", "steer", "apply"], }; /* ------------------------------------------------------------------ */ @@ -314,18 +313,21 @@ async function handleApi(req, res, url) { url: `http://127.0.0.1:${SERVER_PORT}/#t=${encodeURIComponent(MOCK_DEVICE_TOKEN)}`, }); } + // Mint: authenticated, returns the code + QR matrix + reachable bases. if (pathname === "/api/v1/pair" && method === "POST") { - const body = await readBody(req).catch(() => ({})); - if (typeof body.code === "string") { - // Public redeem: the fixture code pairs the browser as a laptop. - if (body.code.replace(/[\s-]/g, "").toUpperCase() !== FIXTURES.pairCode.code.replace(/-/g, "")) { - return unauthorized(res, "pairing code is invalid or expired"); - } - return sendJson(res, 200, { token: MOCK_DEVICE_TOKEN, device: DEVICE_PUBLIC }); - } + await readBody(req).catch(() => ({})); if (OPTIONS.pairOnly) return unauthorized(res); return sendJson(res, 200, { accepted: true, ...FIXTURES.pairCode }); } + // Public redeem: the code *is* the capability, no bearer required. It answers + // `{token,device}` and stays available under `--pair-only`. + if (pathname === "/api/v1/pair/redeem" && method === "POST") { + const body = await readBody(req).catch(() => ({})); + if (String(body.code ?? "").replace(/[\s-]/g, "").toUpperCase() !== FIXTURES.pairCode.code.replace(/-/g, "")) { + return unauthorized(res, "pairing code is invalid or expired"); + } + return sendJson(res, 200, { token: MOCK_DEVICE_TOKEN, device: DEVICE_PUBLIC }); + } /* Everything below needs a device token (and is refused in --pair-only). */ if (OPTIONS.pairOnly) return unauthorized(res); diff --git a/scripts/ui-shots.mjs b/scripts/ui-shots.mjs index af13070..c649594 100644 --- a/scripts/ui-shots.mjs +++ b/scripts/ui-shots.mjs @@ -274,7 +274,6 @@ function storageState(origin, { paired, theme }) { { name: "pi2-connection", value: "paired" }, { name: "pi2-endpoint", value: origin }, { name: "pi2-device-token", value: "mock-device-token-0000000000000000000000" }, - { name: "pi2-device-scopes", value: JSON.stringify(["read", "steer", "apply"]) }, { name: "pi2-device", value: "visual harness" }, { name: "pi2-connected-at", value: String(Date.now()) }, { name: "pi2-theme", value: theme }, diff --git a/web/src/features/connect/manual-form.tsx b/web/src/features/connect/manual-form.tsx index 09de493..e364e93 100644 --- a/web/src/features/connect/manual-form.tsx +++ b/web/src/features/connect/manual-form.tsx @@ -7,7 +7,7 @@ import { Field, Input } from "@/components/ui/input"; import { toast } from "@/components/ui/toast"; import { ApiError, errorI18nKey } from "@/lib/api"; -import { PAIR_CODE_LENGTH, isPairCode, normalizePairCode, parseEndpoint } from "./pairing"; +import { PAIR_CODE_LENGTH, isPairCode, normalizePairCode, parseEndpoint, parsePairLink } from "./pairing"; export interface ManualConnectInput { endpoint: string; @@ -61,6 +61,8 @@ export function ManualConnectForm({ }: ManualConnectFormProps) { const { t } = useTranslation(); const id = useId(); + const [link, setLink] = useState(""); + const [linkError, setLinkError] = useState(null); const [host, setHost] = useState(() => initialHost ?? ""); const [port, setPort] = useState(() => initialPort ?? defaultPort); const [code, setCode] = useState(() => normalizePairCode(initialCode ?? "")); @@ -93,7 +95,24 @@ export function ManualConnectForm({ setCodeError(null); setFormError(null); - const parsed = parseEndpoint(host, port); + // 整段粘贴的配对链接(`pi2://pair?…` / `…/pair/CODE`)先拆成地址 + 端口 + 配对码, + // 拆不出内容时留在链接输入框上报错。 + const pastedLink = parsePairLink(link) ?? parsePairLink(host); + if (link.trim() && !parsePairLink(link)) { + setLinkError(t("connect.invalidLink")); + return; + } + setLinkError(null); + const hostValue = pastedLink ? pastedLink.hostname : host; + const portValue = pastedLink ? String(pastedLink.port) : port; + const codeValue = pastedLink ? pastedLink.code : code; + if (pastedLink) { + setHost(pastedLink.hostname); + setPort(String(pastedLink.port)); + setCode(pastedLink.code); + } + + const parsed = parseEndpoint(hostValue, portValue); if ("error" in parsed) { if (parsed.error === "host") { setHostError(t("connect.hostRequired")); @@ -110,7 +129,7 @@ export function ManualConnectForm({ return; } - const normalized = normalizePairCode(code); + const normalized = normalizePairCode(codeValue); if (!normalized) { setCodeError(t("connect.pairCodeRequired")); return; @@ -141,6 +160,40 @@ export function ManualConnectForm({ return (