From a7fbb3a30d1ea41ca075a6a37d2cfb2091c32a37 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Fri, 23 Oct 2026 14:42:57 -0700 Subject: [PATCH] feat(server): name the computer in the pairing exchange --- packages/client/src/effect/api/api.ts | 2 +- .../client/src/promise/generated/types.ts | 2 +- packages/protocol/openapi.json | 17 +++++++++++++++ packages/protocol/src/groups/server.ts | 6 ++++++ packages/server/src/handlers/server.ts | 5 ++++- packages/server/src/server-info.ts | 21 +++++++++++++++++-- packages/server/test/process.test.ts | 5 ++++- 7 files changed, 52 insertions(+), 6 deletions(-) diff --git a/packages/client/src/effect/api/api.ts b/packages/client/src/effect/api/api.ts index f58e47ad8af..046049c1b99 100644 --- a/packages/client/src/effect/api/api.ts +++ b/packages/client/src/effect/api/api.ts @@ -57,7 +57,7 @@ export type ServerPairOutput = { readonly code: string; readonly expires_in: num export type ServerPairOperation = () => Effect.Effect export type ServerConnectInput = { readonly code: string } -export type ServerConnectOutput = { readonly token: string } +export type ServerConnectOutput = { readonly token: string; readonly device?: { readonly name: string } | undefined } export type ServerConnectOperation = (input: ServerConnectInput) => Effect.Effect export interface ServerApi { diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index 9850eb46c8c..a2e8df68af3 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -11,7 +11,7 @@ export type ServerInfo = { export type PairingCode = { code: string; expires_in: number } -export type PairingSession = { token: string } +export type PairingSession = { token: string; device?: { name: string } | undefined } export type AbsolutePath = string diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index b4cd7f1b3a8..f9fb3215d8e 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -18254,6 +18254,23 @@ "properties": { "token": { "type": "string" + }, + "device": { + "anyOf": [ + { + "type": "object", + "properties": { + "name": { + "type": "string" + } + }, + "required": ["name"], + "additionalProperties": false + }, + { + "type": "null" + } + ] } }, "required": ["token"], diff --git a/packages/protocol/src/groups/server.ts b/packages/protocol/src/groups/server.ts index d78813bfc8b..4b33bc68252 100644 --- a/packages/protocol/src/groups/server.ts +++ b/packages/protocol/src/groups/server.ts @@ -29,6 +29,12 @@ export type PairingCode = typeof PairingCode.Type export const PairingSession = Schema.Struct({ token: Schema.String, + // The computer's human name ("Alex's MacBook Pro") so clients can label the pairing. Absent when unknown. + device: Schema.optional( + Schema.Struct({ + name: Schema.String, + }), + ), }).annotate({ identifier: "PairingSession" }) export type PairingSession = typeof PairingSession.Type diff --git a/packages/server/src/handlers/server.ts b/packages/server/src/handlers/server.ts index 7487581e70d..5382ab6fa6f 100644 --- a/packages/server/src/handlers/server.ts +++ b/packages/server/src/handlers/server.ts @@ -43,7 +43,10 @@ export const ServerHandler = HttpApiBuilder.group(Api, "server.server", (handler { status: 401 }, ) } - if (!browser) return { token } + if (!browser) { + const name = yield* ServerInfo.deviceName + return name === undefined ? { token } : { token, device: { name } } + } return HttpServerResponse.redirect("/").pipe( HttpServerResponse.setCookieUnsafe(ServerAuth.sessionCookieName(request.headers.host), token, { path: "/", diff --git a/packages/server/src/server-info.ts b/packages/server/src/server-info.ts index 28eecfaaa5b..be45360b440 100644 --- a/packages/server/src/server-info.ts +++ b/packages/server/src/server-info.ts @@ -1,5 +1,5 @@ -import { Context, Layer } from "effect" -import { networkInterfaces } from "node:os" +import { Context, Effect, Layer } from "effect" +import { hostname, networkInterfaces } from "node:os" import type { ServerOptions } from "./options" export class Service extends Context.Service< @@ -38,4 +38,21 @@ export function connectionURLs(value: string, requestedHostname?: string) { ] } +// The computer's human name: the macOS Computer Name ("Alex's MacBook Pro"), else the hostname without `.local`. +// Never fails; undefined when the runtime can't tell (e.g. workerd). +export const deviceName = Effect.gen(function* () { + const name = (yield* computerName) || (yield* Effect.try(() => hostname().replace(/\.local$/, ""))) + return name.trim() || undefined +}).pipe(Effect.orElseSucceed(() => undefined)) + +const computerName = Effect.tryPromise(async () => { + if (process.platform !== "darwin") return "" + const { execFile } = await import("node:child_process") + return new Promise((resolve) => + execFile("scutil", ["--get", "ComputerName"], { timeout: 1000 }, (error, stdout) => + resolve(error ? "" : stdout.trim()), + ), + ) +}) + export * as ServerInfo from "./server-info" diff --git a/packages/server/test/process.test.ts b/packages/server/test/process.test.ts index 20ada8bae07..8df61e9e67b 100644 --- a/packages/server/test/process.test.ts +++ b/packages/server/test/process.test.ts @@ -2,6 +2,7 @@ import { expect } from "bun:test" import { Effect } from "effect" import { HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http" import { it } from "../../core/test/lib/effect" +import { ServerInfo } from "../src/server-info" import { ServerProcess } from "../src/process" it.live("authenticates API requests behind the frontend transform while allowing browser preflight", () => @@ -219,7 +220,9 @@ it.live("pairing links sign in browsers with a cookie and API clients with a tok const client = yield* pair const redeemed = yield* request(`/auth/connect/${client.code}`) expect(redeemed.status).toBe(200) - const session = (yield* Effect.promise(() => redeemed.json())) as { token: string } + const session = (yield* Effect.promise(() => redeemed.json())) as { token: string; device?: { name: string } } + expect(session.device?.name).toBe(yield* ServerInfo.deviceName) + expect(session.device?.name).toBeTruthy() expect( (yield* request("/api/info", { headers: { authorization: `Basic ${btoa(`opencode:${session.token}`)}` } })) .status,