mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-03 03:06:44 +00:00
refactor(permission): make experimental server layer-first
Expose the experimental permission and question HttpApi server as Effect layers and test it through NodeHttpServer.layerTest and HttpClient instead of an imperative listen API.
This commit is contained in:
@@ -1,15 +1,16 @@
|
|||||||
import { NodeHttpServer } from "@effect/platform-node"
|
import { NodeHttpServer } from "@effect/platform-node"
|
||||||
import { Context, Effect, Exit, Layer, Redacted, Scope, Schema } from "effect"
|
import { Effect, Layer, Redacted, Schema } from "effect"
|
||||||
import { HttpApiBuilder, HttpApiMiddleware, HttpApiSecurity } from "effect/unstable/httpapi"
|
import { HttpApiBuilder, HttpApiMiddleware, HttpApiSecurity } from "effect/unstable/httpapi"
|
||||||
import { HttpRouter, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
|
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
|
||||||
import { createServer } from "node:http"
|
import { createServer } from "node:http"
|
||||||
import { AppRuntime } from "@/effect/app-runtime"
|
import { AppRuntime } from "@/effect/app-runtime"
|
||||||
import { InstanceRef, WorkspaceRef } from "@/effect/instance-ref"
|
import { InstanceRef, WorkspaceRef } from "@/effect/instance-ref"
|
||||||
import { memoMap } from "@/effect/run-service"
|
|
||||||
import { Flag } from "@/flag/flag"
|
import { Flag } from "@/flag/flag"
|
||||||
import { InstanceBootstrap } from "@/project/bootstrap"
|
import { InstanceBootstrap } from "@/project/bootstrap"
|
||||||
import { Instance } from "@/project/instance"
|
import { Instance } from "@/project/instance"
|
||||||
import { Filesystem } from "@/util/filesystem"
|
import { Filesystem } from "@/util/filesystem"
|
||||||
|
import { Permission } from "@/permission"
|
||||||
|
import { Question } from "@/question"
|
||||||
import { PermissionApi, PermissionLive } from "./permission"
|
import { PermissionApi, PermissionLive } from "./permission"
|
||||||
import { QuestionApi, QuestionLive } from "./question"
|
import { QuestionApi, QuestionLive } from "./question"
|
||||||
|
|
||||||
@@ -25,13 +26,6 @@ const Headers = Schema.Struct({
|
|||||||
})
|
})
|
||||||
|
|
||||||
export namespace ExperimentalHttpApiServer {
|
export namespace ExperimentalHttpApiServer {
|
||||||
export type Listener = {
|
|
||||||
hostname: string
|
|
||||||
port: number
|
|
||||||
url: URL
|
|
||||||
stop: () => Promise<void>
|
|
||||||
}
|
|
||||||
|
|
||||||
function text(input: string, status: number, headers?: Record<string, string>) {
|
function text(input: string, status: number, headers?: Record<string, string>) {
|
||||||
return HttpServerResponse.text(input, { status, headers })
|
return HttpServerResponse.text(input, { status, headers })
|
||||||
}
|
}
|
||||||
@@ -116,12 +110,10 @@ export namespace ExperimentalHttpApiServer {
|
|||||||
}),
|
}),
|
||||||
).layer
|
).layer
|
||||||
|
|
||||||
export async function listen(opts: { hostname: string; port: number }): Promise<Listener> {
|
|
||||||
const scope = await Effect.runPromise(Scope.make())
|
|
||||||
const serverLayer = NodeHttpServer.layer(createServer, { port: opts.port, host: opts.hostname })
|
|
||||||
const QuestionSecured = QuestionApi.middleware(Authorization)
|
const QuestionSecured = QuestionApi.middleware(Authorization)
|
||||||
const PermissionSecured = PermissionApi.middleware(Authorization)
|
const PermissionSecured = PermissionApi.middleware(Authorization)
|
||||||
const routes = Layer.mergeAll(
|
|
||||||
|
export const routes = Layer.mergeAll(
|
||||||
HttpApiBuilder.layer(QuestionSecured, { openapiPath: "/experimental/httpapi/question/doc" }).pipe(
|
HttpApiBuilder.layer(QuestionSecured, { openapiPath: "/experimental/httpapi/question/doc" }).pipe(
|
||||||
Layer.provide(QuestionLive),
|
Layer.provide(QuestionLive),
|
||||||
),
|
),
|
||||||
@@ -129,28 +121,15 @@ export namespace ExperimentalHttpApiServer {
|
|||||||
Layer.provide(PermissionLive),
|
Layer.provide(PermissionLive),
|
||||||
),
|
),
|
||||||
).pipe(Layer.provide(auth), Layer.provide(normalize), Layer.provide(instance))
|
).pipe(Layer.provide(auth), Layer.provide(normalize), Layer.provide(instance))
|
||||||
const live = Layer.mergeAll(
|
|
||||||
serverLayer,
|
export const layer = (opts: { hostname: string; port: number }) =>
|
||||||
HttpRouter.serve(routes, { disableListenLog: true, disableLogger: true }).pipe(Layer.provide(serverLayer)),
|
HttpRouter.serve(routes, { disableListenLog: true, disableLogger: true }).pipe(
|
||||||
|
Layer.provideMerge(NodeHttpServer.layer(createServer, { port: opts.port, host: opts.hostname })),
|
||||||
)
|
)
|
||||||
|
|
||||||
const ctx = await Effect.runPromise(Layer.buildWithMemoMap(live, memoMap, scope))
|
export const layerTest = HttpRouter.serve(routes, { disableListenLog: true, disableLogger: true }).pipe(
|
||||||
const server = Context.get(ctx, HttpServer.HttpServer)
|
Layer.provideMerge(NodeHttpServer.layerTest),
|
||||||
|
Layer.provideMerge(Question.defaultLayer),
|
||||||
if (server.address._tag !== "TcpAddress") {
|
Layer.provideMerge(Permission.defaultLayer),
|
||||||
await Effect.runPromise(Scope.close(scope, Exit.void))
|
)
|
||||||
throw new Error("Experimental HttpApi server requires a TCP address")
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = new URL("http://localhost")
|
|
||||||
url.hostname = server.address.hostname
|
|
||||||
url.port = String(server.address.port)
|
|
||||||
|
|
||||||
return {
|
|
||||||
hostname: server.address.hostname,
|
|
||||||
port: server.address.port,
|
|
||||||
url,
|
|
||||||
stop: () => Effect.runPromise(Scope.close(scope, Exit.void)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +1,44 @@
|
|||||||
import { afterEach, describe, expect, test } from "bun:test"
|
import { describe, expect } from "bun:test"
|
||||||
import { AppRuntime } from "../../src/effect/app-runtime"
|
import { AppRuntime } from "../../src/effect/app-runtime"
|
||||||
import { Instance } from "../../src/project/instance"
|
import { InstanceRef } from "../../src/effect/instance-ref"
|
||||||
import { Permission } from "../../src/permission"
|
import { Permission } from "../../src/permission"
|
||||||
|
import { Instance } from "../../src/project/instance"
|
||||||
|
import { InstanceBootstrap } from "../../src/project/bootstrap"
|
||||||
import { ExperimentalHttpApiServer } from "../../src/server/instance/httpapi/server"
|
import { ExperimentalHttpApiServer } from "../../src/server/instance/httpapi/server"
|
||||||
import { SessionID } from "../../src/session/schema"
|
import { SessionID } from "../../src/session/schema"
|
||||||
import { Log } from "../../src/util/log"
|
import { Log } from "../../src/util/log"
|
||||||
import { tmpdir } from "../fixture/fixture"
|
import { tmpdir } from "../fixture/fixture"
|
||||||
|
import { testEffect } from "../lib/effect"
|
||||||
|
import { Effect, Fiber } from "effect"
|
||||||
|
import { HttpClient, HttpClientRequest } from "effect/unstable/http"
|
||||||
|
|
||||||
Log.init({ print: false })
|
Log.init({ print: false })
|
||||||
|
|
||||||
const ask = (input: Permission.AskInput) => AppRuntime.runPromise(Permission.Service.use((svc) => svc.ask(input)))
|
const it = testEffect(ExperimentalHttpApiServer.layerTest)
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
await Instance.disposeAll()
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("experimental permission httpapi", () => {
|
describe("experimental permission httpapi", () => {
|
||||||
test("lists pending permissions, replies, and serves docs", async () => {
|
it.live("lists pending permissions, replies, and serves docs", () =>
|
||||||
await using tmp = await tmpdir({ git: true })
|
Effect.gen(function* () {
|
||||||
const server = await ExperimentalHttpApiServer.listen({ hostname: "127.0.0.1", port: 0 })
|
const tmp = yield* Effect.promise(() => tmpdir({ git: true }))
|
||||||
|
yield* Effect.addFinalizer(() => Effect.promise(() => tmp[Symbol.asyncDispose]()))
|
||||||
|
yield* Effect.addFinalizer(() => Effect.promise(() => Instance.disposeAll()))
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
"x-opencode-directory": tmp.path,
|
"x-opencode-directory": tmp.path,
|
||||||
}
|
}
|
||||||
let pending!: ReturnType<typeof ask>
|
|
||||||
|
|
||||||
await Instance.provide({
|
const ctx = yield* Effect.promise(() =>
|
||||||
|
Instance.provide({
|
||||||
directory: tmp.path,
|
directory: tmp.path,
|
||||||
fn: async () => {
|
init: () => AppRuntime.runPromise(InstanceBootstrap),
|
||||||
pending = ask({
|
fn: () => Instance.current,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
const svc = yield* Permission.Service
|
||||||
|
const pending = yield* svc
|
||||||
|
.ask({
|
||||||
sessionID: SessionID.make("ses_test"),
|
sessionID: SessionID.make("ses_test"),
|
||||||
permission: "bash",
|
permission: "bash",
|
||||||
patterns: ["ls"],
|
patterns: ["ls"],
|
||||||
@@ -36,14 +46,19 @@ describe("experimental permission httpapi", () => {
|
|||||||
always: ["ls"],
|
always: ["ls"],
|
||||||
ruleset: [],
|
ruleset: [],
|
||||||
})
|
})
|
||||||
},
|
.pipe(Effect.provideService(InstanceRef, ctx), Effect.forkScoped)
|
||||||
})
|
|
||||||
|
|
||||||
try {
|
|
||||||
const list = await fetch(`${server.url}/experimental/httpapi/permission`, { headers })
|
|
||||||
|
|
||||||
|
let items: Array<any> = []
|
||||||
|
for (let i = 0; i < 10; i++) {
|
||||||
|
const list = yield* HttpClient.execute(
|
||||||
|
HttpClientRequest.get("/experimental/httpapi/permission").pipe(HttpClientRequest.setHeaders(headers)),
|
||||||
|
)
|
||||||
expect(list.status).toBe(200)
|
expect(list.status).toBe(200)
|
||||||
const items = await list.json()
|
items = (yield* list.json) as Array<any>
|
||||||
|
if (items.length > 0) break
|
||||||
|
yield* Effect.sleep("50 millis")
|
||||||
|
}
|
||||||
|
|
||||||
expect(items).toHaveLength(1)
|
expect(items).toHaveLength(1)
|
||||||
expect(items[0]).toMatchObject({
|
expect(items[0]).toMatchObject({
|
||||||
permission: "bash",
|
permission: "bash",
|
||||||
@@ -52,26 +67,25 @@ describe("experimental permission httpapi", () => {
|
|||||||
always: ["ls"],
|
always: ["ls"],
|
||||||
})
|
})
|
||||||
|
|
||||||
const doc = await fetch(`${server.url}/experimental/httpapi/permission/doc`, { headers })
|
const doc = yield* HttpClient.execute(
|
||||||
|
HttpClientRequest.get("/experimental/httpapi/permission/doc").pipe(HttpClientRequest.setHeaders(headers)),
|
||||||
|
)
|
||||||
expect(doc.status).toBe(200)
|
expect(doc.status).toBe(200)
|
||||||
const spec = await doc.json()
|
const spec = (yield* doc.json) as any
|
||||||
expect(spec.paths["/experimental/httpapi/permission"]?.get?.operationId).toBe("permission.list")
|
expect(spec.paths["/experimental/httpapi/permission"]?.get?.operationId).toBe("permission.list")
|
||||||
expect(spec.paths["/experimental/httpapi/permission/{requestID}/reply"]?.post?.operationId).toBe(
|
expect(spec.paths["/experimental/httpapi/permission/{requestID}/reply"]?.post?.operationId).toBe(
|
||||||
"permission.reply",
|
"permission.reply",
|
||||||
)
|
)
|
||||||
|
|
||||||
const reply = await fetch(`${server.url}/experimental/httpapi/permission/${items[0].id}/reply`, {
|
const reply = yield* HttpClient.execute(
|
||||||
method: "POST",
|
yield* HttpClientRequest.post(`/experimental/httpapi/permission/${items[0].id}/reply`).pipe(
|
||||||
headers,
|
HttpClientRequest.setHeaders(headers),
|
||||||
body: JSON.stringify({ reply: "once" }),
|
HttpClientRequest.bodyJson({ reply: "once" }),
|
||||||
})
|
),
|
||||||
|
)
|
||||||
expect(reply.status).toBe(200)
|
expect(reply.status).toBe(200)
|
||||||
expect(await reply.json()).toBe(true)
|
expect(yield* reply.json).toBe(true)
|
||||||
expect(await pending).toBeUndefined()
|
yield* Fiber.join(pending)
|
||||||
} finally {
|
}),
|
||||||
await server.stop()
|
)
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user