feat(server): name the computer in the pairing exchange

This commit is contained in:
Kit Langton 2026-10-23 14:42:57 -07:00
parent e31647d68e
commit a7fbb3a30d
7 changed files with 52 additions and 6 deletions

View file

@ -57,7 +57,7 @@ export type ServerPairOutput = { readonly code: string; readonly expires_in: num
export type ServerPairOperation<E = never> = () => Effect.Effect<ServerPairOutput, E>
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<E = never> = (input: ServerConnectInput) => Effect.Effect<ServerConnectOutput, E>
export interface ServerApi<E = never> {

View file

@ -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

View file

@ -18254,6 +18254,23 @@
"properties": {
"token": {
"type": "string"
},
"device": {
"anyOf": [
{
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": ["name"],
"additionalProperties": false
},
{
"type": "null"
}
]
}
},
"required": ["token"],

View file

@ -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

View file

@ -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: "/",

View file

@ -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<string>((resolve) =>
execFile("scutil", ["--get", "ComputerName"], { timeout: 1000 }, (error, stdout) =>
resolve(error ? "" : stdout.trim()),
),
)
})
export * as ServerInfo from "./server-info"

View file

@ -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,