From 6072fc5c1b025cdaae813e03f56746257d0d7067 Mon Sep 17 00:00:00 2001 From: "opencode-agent[bot]" Date: Tue, 11 Aug 2026 22:44:47 +0000 Subject: [PATCH] chore: generate --- packages/cli/src/app-assets.ts | 28 +++++++++----------- packages/cli/src/services/web-ui.ts | 41 +++++++++++++---------------- packages/cli/test/web-ui.test.ts | 4 ++- packages/server/src/process.ts | 8 +----- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/packages/cli/src/app-assets.ts b/packages/cli/src/app-assets.ts index e80e15137da..22c48492730 100644 --- a/packages/cli/src/app-assets.ts +++ b/packages/cli/src/app-assets.ts @@ -4,9 +4,7 @@ import { brotliDecompressSync } from "node:zlib" import { OPENCODE_LOCAL } from "./version" export type AssetMap = Readonly> -type EncodedAssetMap = Readonly< - Record -> +type EncodedAssetMap = Readonly> export const load = Effect.fn("cli.app-assets.load")(function* () { const embedded = yield* Effect.tryPromise(() => import("virtual:opencode-app-assets")).pipe(Effect.option) @@ -25,19 +23,17 @@ const sourceAssets = Effect.fnUntraced(function* () { const root = path.resolve(import.meta.dirname, "../../app/dist") const files = yield* fs.readDirectory(root, { recursive: true }) return Object.fromEntries( - ( - yield* Effect.forEach( - files.filter((file) => !file.endsWith(".map")), - Effect.fnUntraced(function* (file) { - const target = path.join(root, file) - if ((yield* fs.stat(target)).type === "Directory") return - const body = Buffer.from(yield* fs.readFile(target)) - const encoding = isText(file) ? "utf8" : "base64" - return [file, { encoding, content: body.toString(encoding) }] as const - }), - { concurrency: "unbounded" }, - ) - ).filter((asset) => asset !== undefined), + (yield* Effect.forEach( + files.filter((file) => !file.endsWith(".map")), + Effect.fnUntraced(function* (file) { + const target = path.join(root, file) + if ((yield* fs.stat(target)).type === "Directory") return + const body = Buffer.from(yield* fs.readFile(target)) + const encoding = isText(file) ? "utf8" : "base64" + return [file, { encoding, content: body.toString(encoding) }] as const + }), + { concurrency: "unbounded" }, + )).filter((asset) => asset !== undefined), ) }) diff --git a/packages/cli/src/services/web-ui.ts b/packages/cli/src/services/web-ui.ts index 57d3706c6c4..041e4897fb0 100644 --- a/packages/cli/src/services/web-ui.ts +++ b/packages/cli/src/services/web-ui.ts @@ -4,45 +4,40 @@ import { HttpServerError, HttpServerRequest, HttpServerResponse } from "effect/u import { createHash } from "node:crypto" import { load, type AssetMap } from "../app-assets" -export const handler = Effect.fn("cli.web-ui.handler")(function* (options?: { - readonly assets?: AssetMap -}) { +export const handler = Effect.fn("cli.web-ui.handler")(function* (options?: { readonly assets?: AssetMap }) { const fileSystem = yield* FileSystem.FileSystem const assets = options?.assets ? Effect.succeed(options.assets) : yield* Effect.cached(load().pipe(Effect.provideService(FileSystem.FileSystem, fileSystem))) return (api: Effect.Effect) => api.pipe( - Effect.catchIf( - isRouteNotFound, - () => - HttpServerRequest.HttpServerRequest.pipe( - Effect.flatMap((request) => { - const url = new URL(request.url, "http://localhost") - if (url.pathname === "/api" || url.pathname.startsWith("/api/")) - return Effect.succeed(HttpServerResponse.empty({ status: 404 })) - return assets.pipe(Effect.flatMap((files) => serveUI(request, url, files))) - }), - ), + Effect.catchIf(isRouteNotFound, () => + HttpServerRequest.HttpServerRequest.pipe( + Effect.flatMap((request) => { + const url = new URL(request.url, "http://localhost") + if (url.pathname === "/api" || url.pathname.startsWith("/api/")) + return Effect.succeed(HttpServerResponse.empty({ status: 404 })) + return assets.pipe(Effect.flatMap((files) => serveUI(request, url, files))) + }), + ), ), ) }) -function serveUI( - request: HttpServerRequest.HttpServerRequest, - url: URL, - assets: AssetMap, -) { +function serveUI(request: HttpServerRequest.HttpServerRequest, url: URL, assets: AssetMap) { const key = url.pathname.replace(/^\//, "") const name = assets[key] !== undefined ? key : "index.html" const file = assets[name] if (!file) return Effect.succeed(HttpServerResponse.empty({ status: 404 })) - if (request.method !== "GET" && request.method !== "HEAD") return Effect.succeed(HttpServerResponse.empty({ status: 405 })) + if (request.method !== "GET" && request.method !== "HEAD") + return Effect.succeed(HttpServerResponse.empty({ status: 405 })) const html = name === "index.html" const headers = { "content-type": FSUtil.mimeType(name), "cache-control": html ? "no-cache" : "public, max-age=31536000, immutable", - "content-security-policy": html ? cspForHtml(typeof file === "string" ? file : Buffer.from(file).toString()) : csp(), + "content-security-policy": html + ? cspForHtml(typeof file === "string" ? file : Buffer.from(file).toString()) + : csp(), "x-content-type-options": "nosniff", } return Effect.succeed( @@ -59,7 +54,9 @@ function csp(hash = "") { } function cspForHtml(body: string) { - const match = body.match(/]*\bsrc\s*=)[^>]*\bid=(["'])oc-theme-preload-script\1[^>]*>([\s\S]*?)<\/script>/i) + const match = body.match( + /]*\bsrc\s*=)[^>]*\bid=(["'])oc-theme-preload-script\1[^>]*>([\s\S]*?)<\/script>/i, + ) return csp(match ? createHash("sha256").update(match[2]).digest("base64") : "") } diff --git a/packages/cli/test/web-ui.test.ts b/packages/cli/test/web-ui.test.ts index 5dce8c5b7de..19230253394 100644 --- a/packages/cli/test/web-ui.test.ts +++ b/packages/cli/test/web-ui.test.ts @@ -55,7 +55,9 @@ describe("web UI", () => { expect(yield* Effect.promise(() => script.text())).toBe("console.log('embedded')") const font = yield* Effect.promise(() => fetch(`${origin}/font.woff2`)) - expect(new Uint8Array(yield* Effect.promise(() => font.arrayBuffer()))).toEqual(new Uint8Array([0, 1, 2, 255])) + expect(new Uint8Array(yield* Effect.promise(() => font.arrayBuffer()))).toEqual( + new Uint8Array([0, 1, 2, 255]), + ) const fallback = yield* Effect.promise(() => fetch(`${origin}/workspace/example`)) expect(yield* Effect.promise(() => fallback.text())).toContain("embedded") diff --git a/packages/server/src/process.ts b/packages/server/src/process.ts index 2b5e8783db1..02ee3a8b413 100644 --- a/packages/server/src/process.ts +++ b/packages/server/src/process.ts @@ -5,13 +5,7 @@ import { SessionRestart } from "@opencode-ai/core/session/execution/restart" import { ServiceStatus } from "@opencode-ai/protocol/groups/health" import { hasPtyConnectTicketURL } from "@opencode-ai/protocol/groups/pty" import { Cause, Context, Deferred, Effect, Exit, Layer, Option, Ref, Schema, Scope } from "effect" -import { - HttpMiddleware, - HttpRouter, - HttpServer, - HttpServerRequest, - HttpServerResponse, -} from "effect/unstable/http" +import { HttpMiddleware, HttpRouter, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/unstable/http" import { randomUUID } from "node:crypto" import { createServer } from "node:http" import { ServerAuth } from "./auth"