|
| 1 | +import { EffectBridge } from "@/effect" |
| 2 | +import { Pty } from "@/pty" |
| 3 | +import { PtyID } from "@/pty/schema" |
| 4 | +import { Effect, Layer, Schema } from "effect" |
| 5 | +import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http" |
| 6 | +import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiError, HttpApiGroup, OpenApi } from "effect/unstable/httpapi" |
| 7 | +import * as Socket from "effect/unstable/socket/Socket" |
| 8 | +import { Authorization } from "./auth" |
| 9 | + |
| 10 | +const root = "/pty" |
| 11 | +const Params = Schema.Struct({ |
| 12 | + ptyID: PtyID, |
| 13 | +}) |
| 14 | +const CursorQuery = Schema.Struct({ |
| 15 | + cursor: Schema.optional(Schema.String), |
| 16 | +}) |
| 17 | + |
| 18 | +export const PtyPaths = { |
| 19 | + list: root, |
| 20 | + create: root, |
| 21 | + get: `${root}/:ptyID`, |
| 22 | + update: `${root}/:ptyID`, |
| 23 | + remove: `${root}/:ptyID`, |
| 24 | + connect: `${root}/:ptyID/connect`, |
| 25 | +} as const |
| 26 | + |
| 27 | +export const PtyApi = HttpApi.make("pty") |
| 28 | + .add( |
| 29 | + HttpApiGroup.make("pty") |
| 30 | + .add( |
| 31 | + HttpApiEndpoint.get("list", PtyPaths.list, { |
| 32 | + success: Schema.Array(Pty.Info), |
| 33 | + }).annotateMerge( |
| 34 | + OpenApi.annotations({ |
| 35 | + identifier: "pty.list", |
| 36 | + summary: "List PTY sessions", |
| 37 | + description: "Get a list of all active pseudo-terminal (PTY) sessions managed by OpenCode.", |
| 38 | + }), |
| 39 | + ), |
| 40 | + HttpApiEndpoint.post("create", PtyPaths.create, { |
| 41 | + payload: Pty.CreateInput, |
| 42 | + success: Pty.Info, |
| 43 | + }).annotateMerge( |
| 44 | + OpenApi.annotations({ |
| 45 | + identifier: "pty.create", |
| 46 | + summary: "Create PTY session", |
| 47 | + description: "Create a new pseudo-terminal (PTY) session for running shell commands and processes.", |
| 48 | + }), |
| 49 | + ), |
| 50 | + HttpApiEndpoint.get("get", PtyPaths.get, { |
| 51 | + params: { ptyID: PtyID }, |
| 52 | + success: Pty.Info, |
| 53 | + error: HttpApiError.NotFound, |
| 54 | + }).annotateMerge( |
| 55 | + OpenApi.annotations({ |
| 56 | + identifier: "pty.get", |
| 57 | + summary: "Get PTY session", |
| 58 | + description: "Retrieve detailed information about a specific pseudo-terminal (PTY) session.", |
| 59 | + }), |
| 60 | + ), |
| 61 | + HttpApiEndpoint.put("update", PtyPaths.update, { |
| 62 | + params: { ptyID: PtyID }, |
| 63 | + payload: Pty.UpdateInput, |
| 64 | + success: Pty.Info, |
| 65 | + error: HttpApiError.NotFound, |
| 66 | + }).annotateMerge( |
| 67 | + OpenApi.annotations({ |
| 68 | + identifier: "pty.update", |
| 69 | + summary: "Update PTY session", |
| 70 | + description: "Update properties of an existing pseudo-terminal (PTY) session.", |
| 71 | + }), |
| 72 | + ), |
| 73 | + HttpApiEndpoint.delete("remove", PtyPaths.remove, { |
| 74 | + params: { ptyID: PtyID }, |
| 75 | + success: Schema.Boolean, |
| 76 | + }).annotateMerge( |
| 77 | + OpenApi.annotations({ |
| 78 | + identifier: "pty.remove", |
| 79 | + summary: "Remove PTY session", |
| 80 | + description: "Remove and terminate a specific pseudo-terminal (PTY) session.", |
| 81 | + }), |
| 82 | + ), |
| 83 | + ) |
| 84 | + .annotateMerge( |
| 85 | + OpenApi.annotations({ |
| 86 | + title: "pty", |
| 87 | + description: "Experimental HttpApi PTY routes.", |
| 88 | + }), |
| 89 | + ) |
| 90 | + .middleware(Authorization), |
| 91 | + ) |
| 92 | + .annotateMerge( |
| 93 | + OpenApi.annotations({ |
| 94 | + title: "opencode experimental HttpApi", |
| 95 | + version: "0.0.1", |
| 96 | + description: "Experimental HttpApi surface for selected instance routes.", |
| 97 | + }), |
| 98 | + ) |
| 99 | + |
| 100 | +export const ptyHandlers = Layer.unwrap( |
| 101 | + Effect.gen(function* () { |
| 102 | + const pty = yield* Pty.Service |
| 103 | + |
| 104 | + const list = Effect.fn("PtyHttpApi.list")(function* () { |
| 105 | + return yield* pty.list() |
| 106 | + }) |
| 107 | + |
| 108 | + const create = Effect.fn("PtyHttpApi.create")(function* (ctx: { payload: typeof Pty.CreateInput.Type }) { |
| 109 | + const bridge = yield* EffectBridge.make() |
| 110 | + return yield* Effect.promise(() => |
| 111 | + bridge.promise( |
| 112 | + pty.create({ |
| 113 | + ...ctx.payload, |
| 114 | + args: ctx.payload.args ? [...ctx.payload.args] : undefined, |
| 115 | + env: ctx.payload.env ? { ...ctx.payload.env } : undefined, |
| 116 | + }), |
| 117 | + ), |
| 118 | + ) |
| 119 | + }) |
| 120 | + |
| 121 | + const get = Effect.fn("PtyHttpApi.get")(function* (ctx: { params: { ptyID: PtyID } }) { |
| 122 | + const info = yield* pty.get(ctx.params.ptyID) |
| 123 | + if (!info) return yield* new HttpApiError.NotFound({}) |
| 124 | + return info |
| 125 | + }) |
| 126 | + |
| 127 | + const update = Effect.fn("PtyHttpApi.update")(function* (ctx: { |
| 128 | + params: { ptyID: PtyID } |
| 129 | + payload: typeof Pty.UpdateInput.Type |
| 130 | + }) { |
| 131 | + const info = yield* pty.update(ctx.params.ptyID, { |
| 132 | + ...ctx.payload, |
| 133 | + size: ctx.payload.size ? { ...ctx.payload.size } : undefined, |
| 134 | + }) |
| 135 | + if (!info) return yield* new HttpApiError.NotFound({}) |
| 136 | + return info |
| 137 | + }) |
| 138 | + |
| 139 | + const remove = Effect.fn("PtyHttpApi.remove")(function* (ctx: { params: { ptyID: PtyID } }) { |
| 140 | + yield* pty.remove(ctx.params.ptyID) |
| 141 | + return true |
| 142 | + }) |
| 143 | + |
| 144 | + return HttpApiBuilder.group(PtyApi, "pty", (handlers) => |
| 145 | + handlers |
| 146 | + .handle("list", list) |
| 147 | + .handle("create", create) |
| 148 | + .handle("get", get) |
| 149 | + .handle("update", update) |
| 150 | + .handle("remove", remove), |
| 151 | + ) |
| 152 | + }), |
| 153 | +) |
| 154 | + |
| 155 | +export const ptyConnectRoute = HttpRouter.add( |
| 156 | + "GET", |
| 157 | + PtyPaths.connect, |
| 158 | + Effect.gen(function* () { |
| 159 | + const pty = yield* Pty.Service |
| 160 | + const params = yield* HttpRouter.schemaPathParams(Params) |
| 161 | + if (!(yield* pty.get(params.ptyID))) return HttpServerResponse.empty({ status: 404 }) |
| 162 | + |
| 163 | + const query = yield* HttpServerRequest.schemaSearchParams(CursorQuery) |
| 164 | + const parsedCursor = query.cursor === undefined ? undefined : Number(query.cursor) |
| 165 | + const cursor = |
| 166 | + parsedCursor !== undefined && Number.isSafeInteger(parsedCursor) && parsedCursor >= -1 ? parsedCursor : undefined |
| 167 | + const socket = yield* Effect.orDie((yield* HttpServerRequest.HttpServerRequest).upgrade) |
| 168 | + const write = yield* socket.writer |
| 169 | + let closed = false |
| 170 | + const adapter = { |
| 171 | + get readyState() { |
| 172 | + return closed ? 3 : 1 |
| 173 | + }, |
| 174 | + send: (data: string | Uint8Array | ArrayBuffer) => { |
| 175 | + if (closed) return |
| 176 | + Effect.runFork( |
| 177 | + write(data instanceof ArrayBuffer ? new Uint8Array(data) : data).pipe(Effect.catch(() => Effect.void)), |
| 178 | + ) |
| 179 | + }, |
| 180 | + close: (code?: number, reason?: string) => { |
| 181 | + if (closed) return |
| 182 | + closed = true |
| 183 | + Effect.runFork(write(new Socket.CloseEvent(code, reason)).pipe(Effect.catch(() => Effect.void))) |
| 184 | + }, |
| 185 | + } |
| 186 | + const handler = yield* pty.connect(params.ptyID, adapter, cursor) |
| 187 | + if (!handler) return HttpServerResponse.empty() |
| 188 | + |
| 189 | + yield* socket |
| 190 | + .runRaw((message) => { |
| 191 | + handler.onMessage(typeof message === "string" ? message : message.slice().buffer) |
| 192 | + }) |
| 193 | + .pipe( |
| 194 | + Effect.catchReason("SocketError", "SocketCloseError", () => Effect.void), |
| 195 | + Effect.ensuring( |
| 196 | + Effect.sync(() => { |
| 197 | + closed = true |
| 198 | + handler.onClose() |
| 199 | + }), |
| 200 | + ), |
| 201 | + Effect.orDie, |
| 202 | + ) |
| 203 | + return HttpServerResponse.empty() |
| 204 | + }).pipe(Effect.provide(Pty.defaultLayer)), |
| 205 | +) |
0 commit comments