mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
feat(server): name the computer in the pairing exchange
This commit is contained in:
parent
e31647d68e
commit
a7fbb3a30d
7 changed files with 52 additions and 6 deletions
2
packages/client/src/effect/api/api.ts
generated
2
packages/client/src/effect/api/api.ts
generated
|
|
@ -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> {
|
||||
|
|
|
|||
2
packages/client/src/promise/generated/types.ts
generated
2
packages/client/src/promise/generated/types.ts
generated
|
|
@ -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
|
||||
|
||||
|
|
|
|||
17
packages/protocol/openapi.json
generated
17
packages/protocol/openapi.json
generated
|
|
@ -18254,6 +18254,23 @@
|
|||
"properties": {
|
||||
"token": {
|
||||
"type": "string"
|
||||
},
|
||||
"device": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["name"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": ["token"],
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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: "/",
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue