mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
103 lines
3.7 KiB
TypeScript
103 lines
3.7 KiB
TypeScript
import { FileSystem } from "@opencode/core/filesystem"
|
|
import { Location } from "@opencode/core/location"
|
|
import { Plugin } from "@opencode/core/plugin"
|
|
import { LocationServiceMap } from "@opencode/core/location-services"
|
|
import { AbsolutePath } from "@opencode/core/schema"
|
|
import { Session } from "@opencode/core/session"
|
|
import { LocationNotFoundError, InvalidRequestError, ServiceUnavailableError } from "@opencode/protocol/errors"
|
|
import { Effect, Layer, Schema } from "effect"
|
|
import { HttpServerRequest } from "effect/http"
|
|
import { HttpApiMiddleware } from "effect/http-api"
|
|
import { missingSession } from "./handlers/session-error"
|
|
|
|
export type LocationServices = Layer.Success<ReturnType<(typeof LocationServiceMap.Service)["get"]>>
|
|
|
|
export class LocationMiddleware extends HttpApiMiddleware.Service<LocationMiddleware, { provides: LocationServices }>()(
|
|
"@opencode/HttpApiLocation",
|
|
{ error: [LocationNotFoundError] },
|
|
) {}
|
|
|
|
export function locationErrors<A, E, R>(effect: Effect.Effect<A, E, R>) {
|
|
return effect.pipe(
|
|
Effect.catchIf(
|
|
(error): error is Extract<E, FileSystem.DirectoryNotFoundError> =>
|
|
error instanceof FileSystem.DirectoryNotFoundError,
|
|
(error) =>
|
|
Effect.fail(
|
|
new LocationNotFoundError({
|
|
location: { directory: error.directory },
|
|
message: `Location not found: ${error.directory}`,
|
|
}),
|
|
),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Temporary: managed policy is applied by the last internal plugin, and State shows each registration as soon as it is
|
|
// made, so until activation finishes a provider the policy denies is still listed and usable. Only the endpoints that
|
|
// list or use providers wait. Remove once a plugin batch becomes visible only when it ends.
|
|
export const awaitPlugins = Plugin.awaitActivation.pipe(
|
|
Effect.timeoutOrElse({
|
|
duration: "5 seconds",
|
|
orElse: () =>
|
|
Effect.fail(
|
|
new ServiceUnavailableError({
|
|
message: "Location plugins did not finish activating",
|
|
service: "plugin",
|
|
}),
|
|
),
|
|
}),
|
|
)
|
|
|
|
export function response<A, E, R>(data: Effect.Effect<A, E, R>) {
|
|
return Effect.gen(function* () {
|
|
const location = yield* Location.Service
|
|
return {
|
|
location: new Location.Info({
|
|
directory: location.directory,
|
|
project: location.project,
|
|
}),
|
|
data: yield* data,
|
|
}
|
|
})
|
|
}
|
|
|
|
const decodeSessionID = Schema.decodeUnknownEffect(Session.ID)
|
|
|
|
export const sessionInfo = Effect.fnUntraced(function* (sessions: Session.Interface, sessionID: unknown) {
|
|
const id = yield* decodeSessionID(sessionID).pipe(
|
|
Effect.mapError(() => new InvalidRequestError({ message: "Invalid session ID", field: "sessionID" })),
|
|
)
|
|
return yield* sessions.get(id).pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
})
|
|
|
|
export function requestRef(request: HttpServerRequest.HttpServerRequest): Location.Ref {
|
|
const query = new URL(request.url, "http://localhost").searchParams
|
|
const directory =
|
|
query.get("location[directory]") ||
|
|
(request.headers["x-opencode-directory"] ? decode(request.headers["x-opencode-directory"]) : process.cwd())
|
|
return Location.Ref.make({
|
|
directory: AbsolutePath.make(directory),
|
|
})
|
|
}
|
|
|
|
function decode(input: string) {
|
|
try {
|
|
return decodeURIComponent(input)
|
|
} catch {
|
|
return input
|
|
}
|
|
}
|
|
|
|
export const layer = Layer.effect(
|
|
LocationMiddleware,
|
|
Effect.gen(function* () {
|
|
const locations = yield* LocationServiceMap.Service
|
|
return LocationMiddleware.of((effect) =>
|
|
Effect.gen(function* () {
|
|
const request = yield* HttpServerRequest.HttpServerRequest
|
|
return yield* effect.pipe(Effect.provide(locations.get(requestRef(request))), locationErrors)
|
|
}),
|
|
)
|
|
}),
|
|
)
|