Skip to content

Commit 080e92d

Browse files
authored
refactor(server): remove dead PtyEnvironment service and simplify route handlers (anomalyco#57992)
1 parent f8d3f34 commit 080e92d

6 files changed

Lines changed: 27 additions & 100 deletions

File tree

‎packages/server/src/handlers/mcp.ts‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@ export const McpHandler = HttpApiBuilder.group(Api, "server.mcp", (handlers) =>
1515
"mcp.list",
1616
Effect.fn(function* () {
1717
const service = yield* Mcp.Service
18-
return yield* response(
19-
service
20-
.servers()
21-
.pipe(
22-
Effect.map((servers) =>
23-
servers.map((info) => ({ name: info.name, status: info.status, integrationID: info.integrationID })),
24-
),
25-
),
26-
)
18+
return yield* response(service.servers())
2719
}),
2820
)
2921
.handle(

‎packages/server/src/handlers/message.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SessionMessage } from "@opencode/core/session/message"
22
import { Session } from "@opencode/core/session"
3-
import { Effect, Schema } from "effect"
3+
import { Effect, Option, Schema } from "effect"
44
import { HttpApiBuilder } from "effect/http-api"
55
import { Api } from "../api"
66
import { InvalidCursorError } from "@opencode/protocol/errors"
@@ -14,14 +14,14 @@ const Cursor = Schema.Struct({
1414
direction: Schema.Union([Schema.Literal("previous"), Schema.Literal("next")]),
1515
})
1616

17-
const decodeCursor = Schema.decodeUnknownSync(Cursor)
17+
const decodeCursor = Schema.decodeUnknownOption(Schema.fromJsonString(Cursor))
1818

1919
const cursor = {
2020
encode(message: SessionMessage.Info, order: "asc" | "desc", direction: "previous" | "next") {
2121
return Buffer.from(JSON.stringify({ id: message.id, order, direction })).toString("base64url")
2222
},
2323
decode(input: string) {
24-
return decodeCursor(JSON.parse(Buffer.from(input, "base64url").toString("utf8")))
24+
return decodeCursor(Buffer.from(input, "base64url").toString("utf8"))
2525
},
2626
}
2727

@@ -34,10 +34,10 @@ export const MessageHandler = HttpApiBuilder.group(Api, "server.message", (handl
3434
Effect.fn(function* (ctx) {
3535
if (ctx.query.cursor && ctx.query.order !== undefined)
3636
return yield* new InvalidCursorError({ message: "Cursor cannot be combined with order" })
37-
const decoded = yield* Effect.try({
38-
try: () => (ctx.query.cursor ? cursor.decode(ctx.query.cursor) : undefined),
39-
catch: () => new InvalidCursorError({ message: "Invalid cursor" }),
40-
})
37+
const decoded = ctx.query.cursor
38+
? Option.getOrUndefined(cursor.decode(ctx.query.cursor)) ??
39+
(yield* new InvalidCursorError({ message: "Invalid cursor" }))
40+
: undefined
4141
const order = decoded?.order ?? ctx.query.order ?? "desc"
4242
const messages = yield* session
4343
.messages({

‎packages/server/src/handlers/pty.ts‎

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@ import {
1616
PTY_CONNECT_TOKEN_HEADER_VALUE,
1717
} from "@opencode/protocol/groups/pty"
1818
import { locationErrors, requestRef, response } from "../location"
19-
import { PtyEnvironment } from "../pty-environment"
2019
import { type Outbound, runPtySocket } from "./pty-socket"
2120

22-
const ticketScope = Effect.gen(function* () {
23-
const location = yield* Location.Service
24-
return { directory: location.directory as string, workspaceID: location.workspaceID }
25-
})
21+
const notFound = (ptyID: Pty.ID) => new PtyNotFoundError({ ptyID, message: `PTY session not found: ${ptyID}` })
2622

2723
export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
2824
Effect.gen(function* () {
2925
const locations = yield* LocationServiceMap.Service
3026
const tickets = yield* PtyTicket.Service
3127
const cors = yield* CorsConfig
32-
const environment = yield* PtyEnvironment.Service
3328

3429
return handlers
3530
.handle(
@@ -44,16 +39,10 @@ export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
4439
Effect.fn(function* (ctx) {
4540
const pty = yield* Pty.Service
4641
const location = yield* Location.Service
47-
const cwd = ctx.payload.cwd || location.directory
4842
return yield* response(
4943
pty.create({
5044
...ctx.payload,
51-
args: ctx.payload.args ? [...ctx.payload.args] : undefined,
52-
cwd,
53-
env: {
54-
...ctx.payload.env,
55-
...(yield* environment.get({ directory: location.directory, cwd })),
56-
},
45+
cwd: ctx.payload.cwd || location.directory,
5746
}),
5847
)
5948
}),
@@ -63,16 +52,7 @@ export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
6352
Effect.fn(function* (ctx) {
6453
const pty = yield* Pty.Service
6554
return yield* response(
66-
pty.get(ctx.params.ptyID).pipe(
67-
Effect.catchTag(
68-
"Pty.NotFoundError",
69-
() =>
70-
new PtyNotFoundError({
71-
ptyID: ctx.params.ptyID,
72-
message: `PTY session not found: ${ctx.params.ptyID}`,
73-
}),
74-
),
75-
),
55+
pty.get(ctx.params.ptyID).pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID))),
7656
)
7757
}),
7858
)
@@ -82,37 +62,18 @@ export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
8262
const pty = yield* Pty.Service
8363
return yield* response(
8464
pty
85-
.update(ctx.params.ptyID, {
86-
...ctx.payload,
87-
size: ctx.payload.size ? { ...ctx.payload.size } : undefined,
88-
})
89-
.pipe(
90-
Effect.catchTag(
91-
"Pty.NotFoundError",
92-
() =>
93-
new PtyNotFoundError({
94-
ptyID: ctx.params.ptyID,
95-
message: `PTY session not found: ${ctx.params.ptyID}`,
96-
}),
97-
),
98-
),
65+
.update(ctx.params.ptyID, ctx.payload)
66+
.pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID))),
9967
)
10068
}),
10169
)
10270
.handle(
10371
"pty.remove",
10472
Effect.fn(function* (ctx) {
10573
const pty = yield* Pty.Service
106-
yield* pty.remove(ctx.params.ptyID).pipe(
107-
Effect.catchTag(
108-
"Pty.NotFoundError",
109-
() =>
110-
new PtyNotFoundError({
111-
ptyID: ctx.params.ptyID,
112-
message: `PTY session not found: ${ctx.params.ptyID}`,
113-
}),
114-
),
115-
)
74+
yield* pty
75+
.remove(ctx.params.ptyID)
76+
.pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID)))
11677
return HttpApiSchema.NoContent.make()
11778
}),
11879
)
@@ -128,17 +89,15 @@ export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
12889
)
12990
return yield* new ForbiddenError({ message: "Invalid PTY connect token request" })
13091
const pty = yield* Pty.Service
131-
yield* pty.get(ctx.params.ptyID).pipe(
132-
Effect.catchTag(
133-
"Pty.NotFoundError",
134-
() =>
135-
new PtyNotFoundError({
136-
ptyID: ctx.params.ptyID,
137-
message: `PTY session not found: ${ctx.params.ptyID}`,
138-
}),
139-
),
92+
const location = yield* Location.Service
93+
yield* pty.get(ctx.params.ptyID).pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID)))
94+
return yield* response(
95+
tickets.issue({
96+
ptyID: ctx.params.ptyID,
97+
directory: location.directory,
98+
workspaceID: location.workspaceID,
99+
}),
140100
)
141-
return yield* response(tickets.issue({ ptyID: ctx.params.ptyID, ...(yield* ticketScope) }))
142101
}),
143102
)
144103
.handleRaw(

‎packages/server/src/handlers/worktree.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ export const WorktreeHandler = HttpApiBuilder.group(Api, "server.worktree", (han
2626

2727
function badRequest<A, R>(effect: Effect.Effect<A, Worktree.Error, R>) {
2828
return effect.pipe(
29-
Effect.catchTag("Project.NotFoundError", missingProject),
3029
Effect.mapError((error) =>
31-
error instanceof ProjectNotFoundError
32-
? error
30+
error._tag === "Project.NotFoundError"
31+
? missingProject(error)
3332
: new WorktreeError({
3433
name: "WorktreeError",
3534
data: {
@@ -45,9 +44,7 @@ function badRequest<A, R>(effect: Effect.Effect<A, Worktree.Error, R>) {
4544
}
4645

4746
function missingProject(error: Project.NotFoundError) {
48-
return Effect.fail(
49-
new ProjectNotFoundError({ projectID: error.projectID, message: `Project not found: ${error.projectID}` }),
50-
)
47+
return new ProjectNotFoundError({ projectID: error.projectID, message: `Project not found: ${error.projectID}` })
5148
}
5249

5350
function message(error: Exclude<Worktree.Error, Project.NotFoundError>) {

‎packages/server/src/pty-environment.ts‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎packages/server/src/routes.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import { CorsConfig } from "./cors"
4141
import { handlers } from "./handlers"
4242
import { authorizationLayer } from "./middleware/authorization"
4343
import { schemaErrorLayer } from "./middleware/schema-error"
44-
import { PtyEnvironment } from "./pty-environment"
4544
import { ServerPairing } from "./pairing"
4645
import { layer } from "./location"
4746
import { formLocationLayer } from "./middleware/form-location"
@@ -69,7 +68,6 @@ const applicationServiceNodes = [
6968
PersistentPty.node,
7069
Credential.node,
7170
WellKnown.node,
72-
PtyEnvironment.node,
7371
ServerPairing.node,
7472
LocationServiceMap.node,
7573
LocationActivity.node,

0 commit comments

Comments
 (0)