@zyro-sec/server 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -8
- package/script/build.ts +28 -28
- package/script/pack.ts +5 -5
- package/script/workerd-probe.ts +59 -59
- package/src/api.ts +12 -12
- package/src/auth.ts +70 -70
- package/src/content-headers.ts +32 -32
- package/src/cors.ts +34 -34
- package/src/event-feed.ts +93 -93
- package/src/fetch.ts +67 -67
- package/src/handlers/agent.ts +23 -23
- package/src/handlers/artifact.ts +242 -242
- package/src/handlers/automation.ts +63 -63
- package/src/handlers/blob.ts +55 -55
- package/src/handlers/command.ts +8 -8
- package/src/handlers/config.ts +26 -26
- package/src/handlers/credential.ts +87 -87
- package/src/handlers/debug.ts +57 -57
- package/src/handlers/event.ts +38 -38
- package/src/handlers/form.ts +15 -15
- package/src/handlers/fs.ts +180 -180
- package/src/handlers/generate.ts +117 -117
- package/src/handlers/integration.ts +310 -310
- package/src/handlers/job.ts +29 -29
- package/src/handlers/location.ts +33 -33
- package/src/handlers/machine.ts +336 -336
- package/src/handlers/mcp.ts +160 -160
- package/src/handlers/media.ts +44 -44
- package/src/handlers/message.ts +82 -82
- package/src/handlers/migration.ts +13 -13
- package/src/handlers/model.ts +25 -25
- package/src/handlers/permission.ts +131 -131
- package/src/handlers/persistent-pty.ts +285 -285
- package/src/handlers/plugin.ts +97 -97
- package/src/handlers/project.ts +23 -23
- package/src/handlers/provider.ts +32 -32
- package/src/handlers/pty-socket.ts +47 -47
- package/src/handlers/pty.ts +181 -181
- package/src/handlers/push.ts +67 -67
- package/src/handlers/reference.ts +8 -8
- package/src/handlers/rpc.ts +31 -31
- package/src/handlers/server.ts +58 -58
- package/src/handlers/session-error.ts +47 -47
- package/src/handlers/session.ts +722 -722
- package/src/handlers/shell.ts +72 -72
- package/src/handlers/skill.ts +34 -34
- package/src/handlers/usage.ts +23 -23
- package/src/handlers/vcs.ts +104 -104
- package/src/handlers/voice.ts +50 -50
- package/src/handlers/websearch.ts +50 -50
- package/src/handlers/worktree.ts +60 -60
- package/src/handlers.ts +69 -69
- package/src/location.ts +86 -86
- package/src/media-uri.ts +13 -13
- package/src/middleware/authorization.ts +111 -111
- package/src/middleware/defect-response.ts +39 -39
- package/src/middleware/form-location.ts +44 -44
- package/src/middleware/schema-error.ts +49 -49
- package/src/middleware/session-location.ts +30 -30
- package/src/options.ts +61 -61
- package/src/pairing.ts +36 -36
- package/src/process.ts +274 -274
- package/src/range.ts +16 -16
- package/src/request-site.ts +36 -36
- package/src/request-tracing.ts +9 -9
- package/src/routes.ts +261 -261
- package/src/server-info.ts +41 -41
- package/src/service-status.ts +30 -30
- package/src/workerd.ts +184 -184
- package/test/fixture/mcp-starts.cjs +20 -20
- package/test/fixture/routes.ts +37 -37
- package/test/fixture/server.ts +20 -20
- package/test/fixture/worktree-delegate/index.ts +13 -13
- package/test/fixture/worktree-plugin/index.ts +47 -47
- package/tsconfig.build.json +15 -15
- package/tsconfig.json +11 -11
- package/test/artifact.test.ts +0 -224
- package/test/auth.test.ts +0 -24
- package/test/automation.test.ts +0 -315
- package/test/client-mcp.test.ts +0 -369
- package/test/codemode-api.test.ts +0 -88
- package/test/config.test.ts +0 -104
- package/test/cors.test.ts +0 -24
- package/test/defect-response.test.ts +0 -83
- package/test/event-feed.test.ts +0 -201
- package/test/fetch-option.test.ts +0 -104
- package/test/fetch.test.ts +0 -698
- package/test/flag-channel.test.ts +0 -126
- package/test/fs.test.ts +0 -439
- package/test/generate.test.ts +0 -132
- package/test/handler-policy.test.ts +0 -43
- package/test/heap-dump.test.ts +0 -137
- package/test/integration-policy.test.ts +0 -93
- package/test/job.test.ts +0 -186
- package/test/machine-iroh.test.ts +0 -259
- package/test/machine-link.test.ts +0 -1234
- package/test/machine-routes.test.ts +0 -198
- package/test/media.test.ts +0 -343
- package/test/message.test.ts +0 -105
- package/test/model.test.ts +0 -62
- package/test/options.test.ts +0 -42
- package/test/permission-saved.test.ts +0 -73
- package/test/persistent-pty.test.ts +0 -1007
- package/test/process.test.ts +0 -261
- package/test/provider.test.ts +0 -88
- package/test/pty-socket.property.test.ts +0 -240
- package/test/pty-socket.test.ts +0 -89
- package/test/pty-transport.test.ts +0 -200
- package/test/push-routes.test.ts +0 -157
- package/test/request-site.test.ts +0 -71
- package/test/request-tracing.test.ts +0 -87
- package/test/rpc-handler-errors.test.ts +0 -76
- package/test/schema-error.test.ts +0 -68
- package/test/service-status.test.ts +0 -34
- package/test/session-admit-location.test.ts +0 -92
- package/test/session-create.test.ts +0 -65
- package/test/session-diff.test.ts +0 -100
- package/test/session-form.test.ts +0 -50
- package/test/session-import.test.ts +0 -152
- package/test/session-instances.test.ts +0 -335
- package/test/session-unarchive.test.ts +0 -73
- package/test/session-update.test.ts +0 -66
- package/test/skill.test.ts +0 -57
- package/test/sse-failure.test.ts +0 -71
- package/test/usage.test.ts +0 -88
- package/test/vcs.test.ts +0 -356
- package/test/video-generate.test.ts +0 -131
- package/test/voice.test.ts +0 -170
- package/test/workerd.test.ts +0 -227
- package/test/worktree.test.ts +0 -333
|
@@ -1,285 +1,285 @@
|
|
|
1
|
-
import { PersistentPty } from "@zyro-sec/core/persistent-pty"
|
|
2
|
-
import { PtyTicket } from "@zyro-sec/core/pty/ticket"
|
|
3
|
-
import {
|
|
4
|
-
ForbiddenError,
|
|
5
|
-
InvalidRequestError,
|
|
6
|
-
PtyNotFoundError,
|
|
7
|
-
ServiceUnavailableError,
|
|
8
|
-
} from "@zyro-sec/protocol/errors"
|
|
9
|
-
import {
|
|
10
|
-
PTY_CONNECT_TICKET_QUERY,
|
|
11
|
-
PTY_CONNECT_TOKEN_HEADER,
|
|
12
|
-
PTY_CONNECT_TOKEN_HEADER_VALUE,
|
|
13
|
-
} from "@zyro-sec/protocol/groups/persistent-pty"
|
|
14
|
-
import { Effect, Queue } from "effect"
|
|
15
|
-
import { HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
16
|
-
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
|
|
17
|
-
import { Socket } from "effect/socket"
|
|
18
|
-
import { Api } from "../api"
|
|
19
|
-
import { CorsConfig, isAllowedRequestOrigin } from "../cors"
|
|
20
|
-
import { makeArtifact } from "./artifact"
|
|
21
|
-
import { makeBlobGet } from "./blob"
|
|
22
|
-
import { makeAutomation } from "./automation"
|
|
23
|
-
import { makeJob } from "./job"
|
|
24
|
-
import { makeMachine, makeMachineLink } from "./machine"
|
|
25
|
-
import { makeMediaGet } from "./media"
|
|
26
|
-
import { makePush } from "./push"
|
|
27
|
-
import { type Outbound, runPtySocket } from "./pty-socket"
|
|
28
|
-
|
|
29
|
-
export const PersistentPtyHandler = HttpApiBuilder.group(Api, "server.experimental", (handlers) =>
|
|
30
|
-
Effect.gen(function* () {
|
|
31
|
-
const tickets = yield* PtyTicket.Service
|
|
32
|
-
const cors = yield* CorsConfig
|
|
33
|
-
const pty = yield* PersistentPty.Service
|
|
34
|
-
const artifact = yield* makeArtifact
|
|
35
|
-
const job = yield* makeJob
|
|
36
|
-
const mediaGet = yield* makeMediaGet
|
|
37
|
-
const blobGet = yield* makeBlobGet
|
|
38
|
-
const automation = yield* makeAutomation
|
|
39
|
-
const machine = yield* makeMachine
|
|
40
|
-
const machineLink = yield* makeMachineLink
|
|
41
|
-
const push = yield* makePush
|
|
42
|
-
|
|
43
|
-
return handlers
|
|
44
|
-
.handleRaw("media.get", (ctx) => mediaGet(ctx.params.hash, ctx.request))
|
|
45
|
-
.handleRaw("blob.get", (ctx) => blobGet(ctx.params.id, ctx.request))
|
|
46
|
-
.handle("job.list", (ctx) => job.list(ctx.query))
|
|
47
|
-
.handle("job.cancel", (ctx) => job.cancel(ctx.params.id))
|
|
48
|
-
.handleRaw("machine.link", (ctx) => machineLink(ctx.request))
|
|
49
|
-
.handle("machine.list", () => machine.list())
|
|
50
|
-
.handle("machine.add", (ctx) => machine.add(ctx.payload))
|
|
51
|
-
.handle("machine.remove", (ctx) => machine.remove(ctx.params.name))
|
|
52
|
-
.handle("push.get", () => push.get())
|
|
53
|
-
.handle("push.device.register", (ctx) => push.register(ctx.params.deviceID, ctx.payload))
|
|
54
|
-
.handle("push.device.remove", (ctx) => push.remove(ctx.params.deviceID))
|
|
55
|
-
.handle("push.device.update", (ctx) => push.update(ctx.params.deviceID, ctx.payload))
|
|
56
|
-
.handle("push.device.session.set", (ctx) => push.setDeviceBell(ctx.params.deviceID, ctx.params.sessionID, ctx.payload))
|
|
57
|
-
.handle("push.device.session.remove", (ctx) => push.removeDeviceBell(ctx.params.deviceID, ctx.params.sessionID))
|
|
58
|
-
.handle("push.session.set", (ctx) => push.setSessionBell(ctx.params.sessionID, ctx.payload))
|
|
59
|
-
.handle("push.session.remove", (ctx) => push.removeSessionBell(ctx.params.sessionID))
|
|
60
|
-
.handle("push.settings.set", (ctx) => push.setSettings(ctx.payload))
|
|
61
|
-
.handle("artifacts.providers", (ctx) => artifact.providers(ctx.query, ctx.request))
|
|
62
|
-
.handle("artifacts.list", (ctx) => artifact.list(ctx.query, ctx.request))
|
|
63
|
-
.handle("artifacts.create", (ctx) => artifact.create(ctx.query, ctx.payload, ctx.request))
|
|
64
|
-
.handle("artifacts.get", (ctx) => artifact.get(ctx.params.artifactID, ctx.query, ctx.request))
|
|
65
|
-
.handle("artifacts.update", (ctx) => artifact.update(ctx.params.artifactID, ctx.query, ctx.payload, ctx.request))
|
|
66
|
-
.handle("artifacts.delete", (ctx) => artifact.remove(ctx.params.artifactID, ctx.query, ctx.request))
|
|
67
|
-
.handleRaw("artifacts.view", (ctx) => artifact.view(ctx.params.artifactID, ctx.query, ctx.request))
|
|
68
|
-
.handle("automation.create", (ctx) => automation.create(ctx.payload))
|
|
69
|
-
.handle("automation.list", (ctx) => automation.list(ctx.query))
|
|
70
|
-
.handle("automation.get", (ctx) => automation.get(ctx.params.automationID))
|
|
71
|
-
.handle("automation.pause", (ctx) => automation.pause(ctx.params.automationID))
|
|
72
|
-
.handle("automation.resume", (ctx) => automation.resume(ctx.params.automationID))
|
|
73
|
-
.handle("automation.delete", (ctx) => automation.remove(ctx.params.automationID))
|
|
74
|
-
.handle(
|
|
75
|
-
"persistentPty.read",
|
|
76
|
-
Effect.fn(function* (ctx) {
|
|
77
|
-
return { data: yield* pty.read(ctx.params.sessionID, ctx.query.lines).pipe(mapUnavailable) }
|
|
78
|
-
}),
|
|
79
|
-
)
|
|
80
|
-
.handle(
|
|
81
|
-
"persistentPty.list",
|
|
82
|
-
Effect.fn(function* (ctx) {
|
|
83
|
-
return { data: yield* pty.list(ctx.params.sessionID).pipe(mapUnavailable) }
|
|
84
|
-
}),
|
|
85
|
-
)
|
|
86
|
-
.handle(
|
|
87
|
-
"persistentPty.create",
|
|
88
|
-
Effect.fn(function* (ctx) {
|
|
89
|
-
return {
|
|
90
|
-
data: yield* pty
|
|
91
|
-
.create(ctx.params.sessionID, {
|
|
92
|
-
command: ctx.payload.command,
|
|
93
|
-
args: ctx.payload.args,
|
|
94
|
-
cwd: ctx.payload.cwd,
|
|
95
|
-
title: ctx.payload.title,
|
|
96
|
-
env: ctx.payload.env,
|
|
97
|
-
cols: ctx.payload.size?.cols,
|
|
98
|
-
rows: ctx.payload.size?.rows,
|
|
99
|
-
})
|
|
100
|
-
.pipe(
|
|
101
|
-
Effect.catchTags({
|
|
102
|
-
"PersistentPty.InvalidCwdError": (error) =>
|
|
103
|
-
new InvalidRequestError({ message: error.message, kind: "invalid_cwd", field: "cwd" }),
|
|
104
|
-
"PersistentPty.UnavailableError": unavailable,
|
|
105
|
-
}),
|
|
106
|
-
),
|
|
107
|
-
}
|
|
108
|
-
}),
|
|
109
|
-
)
|
|
110
|
-
.handle(
|
|
111
|
-
"persistentPty.shutdown",
|
|
112
|
-
Effect.fn(function* () {
|
|
113
|
-
yield* pty.shutdown().pipe(mapUnavailable)
|
|
114
|
-
return HttpApiSchema.NoContent.make()
|
|
115
|
-
}),
|
|
116
|
-
)
|
|
117
|
-
.handle(
|
|
118
|
-
"persistentPty.handoff",
|
|
119
|
-
Effect.fn(function* () {
|
|
120
|
-
return { handoff: yield* pty.handoff().pipe(mapUnavailable) }
|
|
121
|
-
}),
|
|
122
|
-
)
|
|
123
|
-
.handle(
|
|
124
|
-
"persistentPty.get",
|
|
125
|
-
Effect.fn(function* (ctx) {
|
|
126
|
-
return { data: yield* pty.get(ctx.params.ptyID).pipe(mapTerminalError) }
|
|
127
|
-
}),
|
|
128
|
-
)
|
|
129
|
-
.handle(
|
|
130
|
-
"persistentPty.update",
|
|
131
|
-
Effect.fn(function* (ctx) {
|
|
132
|
-
if (ctx.payload.size)
|
|
133
|
-
yield* pty
|
|
134
|
-
.resize(ctx.params.ptyID, ctx.payload.size.cols, ctx.payload.size.rows, ctx.payload.attachmentID)
|
|
135
|
-
.pipe(mapTerminalError)
|
|
136
|
-
if (ctx.payload.title === undefined) return { data: yield* pty.get(ctx.params.ptyID).pipe(mapTerminalError) }
|
|
137
|
-
return { data: yield* pty.rename(ctx.params.ptyID, ctx.payload.title).pipe(mapTerminalError) }
|
|
138
|
-
}),
|
|
139
|
-
)
|
|
140
|
-
.handle(
|
|
141
|
-
"persistentPty.snapshot",
|
|
142
|
-
Effect.fn(function* (ctx) {
|
|
143
|
-
return { data: yield* pty.snapshot(ctx.params.ptyID).pipe(mapTerminalError) }
|
|
144
|
-
}),
|
|
145
|
-
)
|
|
146
|
-
.handle(
|
|
147
|
-
"persistentPty.remove",
|
|
148
|
-
Effect.fn(function* (ctx) {
|
|
149
|
-
yield* pty.remove(ctx.params.ptyID).pipe(mapTerminalError)
|
|
150
|
-
return HttpApiSchema.NoContent.make()
|
|
151
|
-
}),
|
|
152
|
-
)
|
|
153
|
-
.handle(
|
|
154
|
-
"persistentPty.connectToken",
|
|
155
|
-
Effect.fn(function* (ctx) {
|
|
156
|
-
const request = yield* HttpServerRequest.HttpServerRequest
|
|
157
|
-
if (
|
|
158
|
-
request.headers[PTY_CONNECT_TOKEN_HEADER] !== PTY_CONNECT_TOKEN_HEADER_VALUE ||
|
|
159
|
-
!isAllowedRequestOrigin(request.headers.origin, request.headers.host, cors)
|
|
160
|
-
)
|
|
161
|
-
return yield* new ForbiddenError({ message: "Invalid persistent PTY connect token request" })
|
|
162
|
-
yield* pty.get(ctx.params.ptyID).pipe(mapTerminalError)
|
|
163
|
-
return { data: yield* tickets.issue({ ptyID: ctx.params.ptyID }) }
|
|
164
|
-
}),
|
|
165
|
-
)
|
|
166
|
-
.handleRaw(
|
|
167
|
-
"persistentPty.connect",
|
|
168
|
-
Effect.fn("PersistentPtyHandler.connect")(function* (ctx) {
|
|
169
|
-
if (!isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors))
|
|
170
|
-
return HttpServerResponse.empty({ status: 403 })
|
|
171
|
-
|
|
172
|
-
const url = new URL(ctx.request.url, "http://localhost")
|
|
173
|
-
const ticket = url.searchParams.get(PTY_CONNECT_TICKET_QUERY)
|
|
174
|
-
if (ticket && !(yield* tickets.consume({ ticket, ptyID: ctx.params.ptyID })))
|
|
175
|
-
return HttpServerResponse.empty({ status: 403 })
|
|
176
|
-
|
|
177
|
-
const cursor = Number(url.searchParams.get("cursor") ?? "0")
|
|
178
|
-
const role = url.searchParams.get("role") === "observer" ? "observer" : "controller"
|
|
179
|
-
const framedInput = url.searchParams.get("input_protocol") === "1"
|
|
180
|
-
const attachmentID = url.searchParams.get("attachment_id") ?? crypto.randomUUID()
|
|
181
|
-
if (!Number.isSafeInteger(cursor) || cursor < 0) return HttpServerResponse.empty({ status: 400 })
|
|
182
|
-
|
|
183
|
-
const socket = yield* Effect.orDie(ctx.request.upgrade)
|
|
184
|
-
const outbox = yield* Queue.unbounded<Outbound>()
|
|
185
|
-
let attachment: PersistentPty.Attachment | undefined
|
|
186
|
-
// Bun's native ws upgrade must start before asynchronous daemon I/O.
|
|
187
|
-
const onOpen = Effect.gen(function* () {
|
|
188
|
-
attachment = yield* pty
|
|
189
|
-
.attach(ctx.params.ptyID, {
|
|
190
|
-
cursor,
|
|
191
|
-
attachmentID,
|
|
192
|
-
role,
|
|
193
|
-
takeover: url.searchParams.get("takeover") === "true",
|
|
194
|
-
onEvent: (event) => {
|
|
195
|
-
if (event.type === "output") Queue.offerUnsafe(outbox, event.data)
|
|
196
|
-
if (event.type === "resized")
|
|
197
|
-
Queue.offerUnsafe(
|
|
198
|
-
outbox,
|
|
199
|
-
JSON.stringify({ ...event, checkpoint: Buffer.from(event.checkpoint).toString("base64") }),
|
|
200
|
-
)
|
|
201
|
-
if (event.type !== "output" && event.type !== "resized")
|
|
202
|
-
Queue.offerUnsafe(outbox, JSON.stringify(event))
|
|
203
|
-
},
|
|
204
|
-
onEnd: () => Queue.offerUnsafe(outbox, new Socket.CloseEvent(1000)),
|
|
205
|
-
})
|
|
206
|
-
.pipe(
|
|
207
|
-
Effect.catchTags({
|
|
208
|
-
"PersistentPty.NotFoundError": () => Effect.succeed(undefined),
|
|
209
|
-
"PersistentPty.UnavailableError": () => Effect.succeed(undefined),
|
|
210
|
-
}),
|
|
211
|
-
)
|
|
212
|
-
if (!attachment) {
|
|
213
|
-
Queue.offerUnsafe(outbox, new Socket.CloseEvent(4404, "terminal unavailable"))
|
|
214
|
-
return
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
Queue.offerUnsafe(
|
|
218
|
-
outbox,
|
|
219
|
-
JSON.stringify({
|
|
220
|
-
type: "attached",
|
|
221
|
-
attachmentID,
|
|
222
|
-
inputProtocol: framedInput ? 1 : 0,
|
|
223
|
-
info: attachment.info,
|
|
224
|
-
role: attachment.role,
|
|
225
|
-
generation: attachment.generation,
|
|
226
|
-
replay: {
|
|
227
|
-
requestedOffset: attachment.replay.requestedOffset,
|
|
228
|
-
availableOffset: attachment.replay.availableOffset,
|
|
229
|
-
endOffset: attachment.replay.endOffset,
|
|
230
|
-
truncated: attachment.replay.truncated,
|
|
231
|
-
},
|
|
232
|
-
}),
|
|
233
|
-
)
|
|
234
|
-
if (attachment.replay.data.length > 0) Queue.offerUnsafe(outbox, attachment.replay.data)
|
|
235
|
-
Queue.offerUnsafe(
|
|
236
|
-
outbox,
|
|
237
|
-
JSON.stringify({ type: "replay_complete", endOffset: attachment.replay.endOffset }),
|
|
238
|
-
)
|
|
239
|
-
attachment.activate()
|
|
240
|
-
})
|
|
241
|
-
|
|
242
|
-
yield* runPtySocket({
|
|
243
|
-
socket,
|
|
244
|
-
outbox,
|
|
245
|
-
onOpen,
|
|
246
|
-
onMessage: (message) => {
|
|
247
|
-
if (!attachment) return Effect.void
|
|
248
|
-
const data = typeof message === "string" ? Buffer.from(message) : message
|
|
249
|
-
if (!framedInput)
|
|
250
|
-
return pty
|
|
251
|
-
.input(ctx.params.ptyID, attachmentID, attachment.info.size.cols, attachment.info.size.rows, data)
|
|
252
|
-
.pipe(Effect.ignore)
|
|
253
|
-
if (data.byteLength < 5) return Effect.void
|
|
254
|
-
const view = new DataView(data.buffer, data.byteOffset, data.byteLength)
|
|
255
|
-
const type = data[0]
|
|
256
|
-
const cols = view.getUint16(1)
|
|
257
|
-
const rows = view.getUint16(3)
|
|
258
|
-
if ((type !== 0 && type !== 1) || cols === 0 || rows === 0) return Effect.void
|
|
259
|
-
if (type === 0) return pty.control(ctx.params.ptyID, attachmentID, cols, rows).pipe(Effect.ignore)
|
|
260
|
-
return pty.input(ctx.params.ptyID, attachmentID, cols, rows, data.subarray(5)).pipe(Effect.ignore)
|
|
261
|
-
},
|
|
262
|
-
detach: () => attachment?.detach(),
|
|
263
|
-
})
|
|
264
|
-
return HttpServerResponse.empty()
|
|
265
|
-
}),
|
|
266
|
-
)
|
|
267
|
-
}),
|
|
268
|
-
)
|
|
269
|
-
|
|
270
|
-
const mapUnavailable = <A>(effect: Effect.Effect<A, PersistentPty.UnavailableError>) =>
|
|
271
|
-
effect.pipe(Effect.catchTag("PersistentPty.UnavailableError", unavailable))
|
|
272
|
-
|
|
273
|
-
const mapTerminalError = <A>(effect: Effect.Effect<A, PersistentPty.NotFoundError | PersistentPty.UnavailableError>) =>
|
|
274
|
-
effect.pipe(
|
|
275
|
-
Effect.catchTags({
|
|
276
|
-
"PersistentPty.NotFoundError": notFound,
|
|
277
|
-
"PersistentPty.UnavailableError": unavailable,
|
|
278
|
-
}),
|
|
279
|
-
)
|
|
280
|
-
|
|
281
|
-
const notFound = (error: PersistentPty.NotFoundError) =>
|
|
282
|
-
new PtyNotFoundError({ ptyID: error.ptyID, message: `PTY session not found: ${error.ptyID}` })
|
|
283
|
-
|
|
284
|
-
const unavailable = (error: PersistentPty.UnavailableError) =>
|
|
285
|
-
new ServiceUnavailableError({ message: error.message, service: "opencode-pty" })
|
|
1
|
+
import { PersistentPty } from "@zyro-sec/core/persistent-pty"
|
|
2
|
+
import { PtyTicket } from "@zyro-sec/core/pty/ticket"
|
|
3
|
+
import {
|
|
4
|
+
ForbiddenError,
|
|
5
|
+
InvalidRequestError,
|
|
6
|
+
PtyNotFoundError,
|
|
7
|
+
ServiceUnavailableError,
|
|
8
|
+
} from "@zyro-sec/protocol/errors"
|
|
9
|
+
import {
|
|
10
|
+
PTY_CONNECT_TICKET_QUERY,
|
|
11
|
+
PTY_CONNECT_TOKEN_HEADER,
|
|
12
|
+
PTY_CONNECT_TOKEN_HEADER_VALUE,
|
|
13
|
+
} from "@zyro-sec/protocol/groups/persistent-pty"
|
|
14
|
+
import { Effect, Queue } from "effect"
|
|
15
|
+
import { HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
16
|
+
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
|
|
17
|
+
import { Socket } from "effect/socket"
|
|
18
|
+
import { Api } from "../api"
|
|
19
|
+
import { CorsConfig, isAllowedRequestOrigin } from "../cors"
|
|
20
|
+
import { makeArtifact } from "./artifact"
|
|
21
|
+
import { makeBlobGet } from "./blob"
|
|
22
|
+
import { makeAutomation } from "./automation"
|
|
23
|
+
import { makeJob } from "./job"
|
|
24
|
+
import { makeMachine, makeMachineLink } from "./machine"
|
|
25
|
+
import { makeMediaGet } from "./media"
|
|
26
|
+
import { makePush } from "./push"
|
|
27
|
+
import { type Outbound, runPtySocket } from "./pty-socket"
|
|
28
|
+
|
|
29
|
+
export const PersistentPtyHandler = HttpApiBuilder.group(Api, "server.experimental", (handlers) =>
|
|
30
|
+
Effect.gen(function* () {
|
|
31
|
+
const tickets = yield* PtyTicket.Service
|
|
32
|
+
const cors = yield* CorsConfig
|
|
33
|
+
const pty = yield* PersistentPty.Service
|
|
34
|
+
const artifact = yield* makeArtifact
|
|
35
|
+
const job = yield* makeJob
|
|
36
|
+
const mediaGet = yield* makeMediaGet
|
|
37
|
+
const blobGet = yield* makeBlobGet
|
|
38
|
+
const automation = yield* makeAutomation
|
|
39
|
+
const machine = yield* makeMachine
|
|
40
|
+
const machineLink = yield* makeMachineLink
|
|
41
|
+
const push = yield* makePush
|
|
42
|
+
|
|
43
|
+
return handlers
|
|
44
|
+
.handleRaw("media.get", (ctx) => mediaGet(ctx.params.hash, ctx.request))
|
|
45
|
+
.handleRaw("blob.get", (ctx) => blobGet(ctx.params.id, ctx.request))
|
|
46
|
+
.handle("job.list", (ctx) => job.list(ctx.query))
|
|
47
|
+
.handle("job.cancel", (ctx) => job.cancel(ctx.params.id))
|
|
48
|
+
.handleRaw("machine.link", (ctx) => machineLink(ctx.request))
|
|
49
|
+
.handle("machine.list", () => machine.list())
|
|
50
|
+
.handle("machine.add", (ctx) => machine.add(ctx.payload))
|
|
51
|
+
.handle("machine.remove", (ctx) => machine.remove(ctx.params.name))
|
|
52
|
+
.handle("push.get", () => push.get())
|
|
53
|
+
.handle("push.device.register", (ctx) => push.register(ctx.params.deviceID, ctx.payload))
|
|
54
|
+
.handle("push.device.remove", (ctx) => push.remove(ctx.params.deviceID))
|
|
55
|
+
.handle("push.device.update", (ctx) => push.update(ctx.params.deviceID, ctx.payload))
|
|
56
|
+
.handle("push.device.session.set", (ctx) => push.setDeviceBell(ctx.params.deviceID, ctx.params.sessionID, ctx.payload))
|
|
57
|
+
.handle("push.device.session.remove", (ctx) => push.removeDeviceBell(ctx.params.deviceID, ctx.params.sessionID))
|
|
58
|
+
.handle("push.session.set", (ctx) => push.setSessionBell(ctx.params.sessionID, ctx.payload))
|
|
59
|
+
.handle("push.session.remove", (ctx) => push.removeSessionBell(ctx.params.sessionID))
|
|
60
|
+
.handle("push.settings.set", (ctx) => push.setSettings(ctx.payload))
|
|
61
|
+
.handle("artifacts.providers", (ctx) => artifact.providers(ctx.query, ctx.request))
|
|
62
|
+
.handle("artifacts.list", (ctx) => artifact.list(ctx.query, ctx.request))
|
|
63
|
+
.handle("artifacts.create", (ctx) => artifact.create(ctx.query, ctx.payload, ctx.request))
|
|
64
|
+
.handle("artifacts.get", (ctx) => artifact.get(ctx.params.artifactID, ctx.query, ctx.request))
|
|
65
|
+
.handle("artifacts.update", (ctx) => artifact.update(ctx.params.artifactID, ctx.query, ctx.payload, ctx.request))
|
|
66
|
+
.handle("artifacts.delete", (ctx) => artifact.remove(ctx.params.artifactID, ctx.query, ctx.request))
|
|
67
|
+
.handleRaw("artifacts.view", (ctx) => artifact.view(ctx.params.artifactID, ctx.query, ctx.request))
|
|
68
|
+
.handle("automation.create", (ctx) => automation.create(ctx.payload))
|
|
69
|
+
.handle("automation.list", (ctx) => automation.list(ctx.query))
|
|
70
|
+
.handle("automation.get", (ctx) => automation.get(ctx.params.automationID))
|
|
71
|
+
.handle("automation.pause", (ctx) => automation.pause(ctx.params.automationID))
|
|
72
|
+
.handle("automation.resume", (ctx) => automation.resume(ctx.params.automationID))
|
|
73
|
+
.handle("automation.delete", (ctx) => automation.remove(ctx.params.automationID))
|
|
74
|
+
.handle(
|
|
75
|
+
"persistentPty.read",
|
|
76
|
+
Effect.fn(function* (ctx) {
|
|
77
|
+
return { data: yield* pty.read(ctx.params.sessionID, ctx.query.lines).pipe(mapUnavailable) }
|
|
78
|
+
}),
|
|
79
|
+
)
|
|
80
|
+
.handle(
|
|
81
|
+
"persistentPty.list",
|
|
82
|
+
Effect.fn(function* (ctx) {
|
|
83
|
+
return { data: yield* pty.list(ctx.params.sessionID).pipe(mapUnavailable) }
|
|
84
|
+
}),
|
|
85
|
+
)
|
|
86
|
+
.handle(
|
|
87
|
+
"persistentPty.create",
|
|
88
|
+
Effect.fn(function* (ctx) {
|
|
89
|
+
return {
|
|
90
|
+
data: yield* pty
|
|
91
|
+
.create(ctx.params.sessionID, {
|
|
92
|
+
command: ctx.payload.command,
|
|
93
|
+
args: ctx.payload.args,
|
|
94
|
+
cwd: ctx.payload.cwd,
|
|
95
|
+
title: ctx.payload.title,
|
|
96
|
+
env: ctx.payload.env,
|
|
97
|
+
cols: ctx.payload.size?.cols,
|
|
98
|
+
rows: ctx.payload.size?.rows,
|
|
99
|
+
})
|
|
100
|
+
.pipe(
|
|
101
|
+
Effect.catchTags({
|
|
102
|
+
"PersistentPty.InvalidCwdError": (error) =>
|
|
103
|
+
new InvalidRequestError({ message: error.message, kind: "invalid_cwd", field: "cwd" }),
|
|
104
|
+
"PersistentPty.UnavailableError": unavailable,
|
|
105
|
+
}),
|
|
106
|
+
),
|
|
107
|
+
}
|
|
108
|
+
}),
|
|
109
|
+
)
|
|
110
|
+
.handle(
|
|
111
|
+
"persistentPty.shutdown",
|
|
112
|
+
Effect.fn(function* () {
|
|
113
|
+
yield* pty.shutdown().pipe(mapUnavailable)
|
|
114
|
+
return HttpApiSchema.NoContent.make()
|
|
115
|
+
}),
|
|
116
|
+
)
|
|
117
|
+
.handle(
|
|
118
|
+
"persistentPty.handoff",
|
|
119
|
+
Effect.fn(function* () {
|
|
120
|
+
return { handoff: yield* pty.handoff().pipe(mapUnavailable) }
|
|
121
|
+
}),
|
|
122
|
+
)
|
|
123
|
+
.handle(
|
|
124
|
+
"persistentPty.get",
|
|
125
|
+
Effect.fn(function* (ctx) {
|
|
126
|
+
return { data: yield* pty.get(ctx.params.ptyID).pipe(mapTerminalError) }
|
|
127
|
+
}),
|
|
128
|
+
)
|
|
129
|
+
.handle(
|
|
130
|
+
"persistentPty.update",
|
|
131
|
+
Effect.fn(function* (ctx) {
|
|
132
|
+
if (ctx.payload.size)
|
|
133
|
+
yield* pty
|
|
134
|
+
.resize(ctx.params.ptyID, ctx.payload.size.cols, ctx.payload.size.rows, ctx.payload.attachmentID)
|
|
135
|
+
.pipe(mapTerminalError)
|
|
136
|
+
if (ctx.payload.title === undefined) return { data: yield* pty.get(ctx.params.ptyID).pipe(mapTerminalError) }
|
|
137
|
+
return { data: yield* pty.rename(ctx.params.ptyID, ctx.payload.title).pipe(mapTerminalError) }
|
|
138
|
+
}),
|
|
139
|
+
)
|
|
140
|
+
.handle(
|
|
141
|
+
"persistentPty.snapshot",
|
|
142
|
+
Effect.fn(function* (ctx) {
|
|
143
|
+
return { data: yield* pty.snapshot(ctx.params.ptyID).pipe(mapTerminalError) }
|
|
144
|
+
}),
|
|
145
|
+
)
|
|
146
|
+
.handle(
|
|
147
|
+
"persistentPty.remove",
|
|
148
|
+
Effect.fn(function* (ctx) {
|
|
149
|
+
yield* pty.remove(ctx.params.ptyID).pipe(mapTerminalError)
|
|
150
|
+
return HttpApiSchema.NoContent.make()
|
|
151
|
+
}),
|
|
152
|
+
)
|
|
153
|
+
.handle(
|
|
154
|
+
"persistentPty.connectToken",
|
|
155
|
+
Effect.fn(function* (ctx) {
|
|
156
|
+
const request = yield* HttpServerRequest.HttpServerRequest
|
|
157
|
+
if (
|
|
158
|
+
request.headers[PTY_CONNECT_TOKEN_HEADER] !== PTY_CONNECT_TOKEN_HEADER_VALUE ||
|
|
159
|
+
!isAllowedRequestOrigin(request.headers.origin, request.headers.host, cors)
|
|
160
|
+
)
|
|
161
|
+
return yield* new ForbiddenError({ message: "Invalid persistent PTY connect token request" })
|
|
162
|
+
yield* pty.get(ctx.params.ptyID).pipe(mapTerminalError)
|
|
163
|
+
return { data: yield* tickets.issue({ ptyID: ctx.params.ptyID }) }
|
|
164
|
+
}),
|
|
165
|
+
)
|
|
166
|
+
.handleRaw(
|
|
167
|
+
"persistentPty.connect",
|
|
168
|
+
Effect.fn("PersistentPtyHandler.connect")(function* (ctx) {
|
|
169
|
+
if (!isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors))
|
|
170
|
+
return HttpServerResponse.empty({ status: 403 })
|
|
171
|
+
|
|
172
|
+
const url = new URL(ctx.request.url, "http://localhost")
|
|
173
|
+
const ticket = url.searchParams.get(PTY_CONNECT_TICKET_QUERY)
|
|
174
|
+
if (ticket && !(yield* tickets.consume({ ticket, ptyID: ctx.params.ptyID })))
|
|
175
|
+
return HttpServerResponse.empty({ status: 403 })
|
|
176
|
+
|
|
177
|
+
const cursor = Number(url.searchParams.get("cursor") ?? "0")
|
|
178
|
+
const role = url.searchParams.get("role") === "observer" ? "observer" : "controller"
|
|
179
|
+
const framedInput = url.searchParams.get("input_protocol") === "1"
|
|
180
|
+
const attachmentID = url.searchParams.get("attachment_id") ?? crypto.randomUUID()
|
|
181
|
+
if (!Number.isSafeInteger(cursor) || cursor < 0) return HttpServerResponse.empty({ status: 400 })
|
|
182
|
+
|
|
183
|
+
const socket = yield* Effect.orDie(ctx.request.upgrade)
|
|
184
|
+
const outbox = yield* Queue.unbounded<Outbound>()
|
|
185
|
+
let attachment: PersistentPty.Attachment | undefined
|
|
186
|
+
// Bun's native ws upgrade must start before asynchronous daemon I/O.
|
|
187
|
+
const onOpen = Effect.gen(function* () {
|
|
188
|
+
attachment = yield* pty
|
|
189
|
+
.attach(ctx.params.ptyID, {
|
|
190
|
+
cursor,
|
|
191
|
+
attachmentID,
|
|
192
|
+
role,
|
|
193
|
+
takeover: url.searchParams.get("takeover") === "true",
|
|
194
|
+
onEvent: (event) => {
|
|
195
|
+
if (event.type === "output") Queue.offerUnsafe(outbox, event.data)
|
|
196
|
+
if (event.type === "resized")
|
|
197
|
+
Queue.offerUnsafe(
|
|
198
|
+
outbox,
|
|
199
|
+
JSON.stringify({ ...event, checkpoint: Buffer.from(event.checkpoint).toString("base64") }),
|
|
200
|
+
)
|
|
201
|
+
if (event.type !== "output" && event.type !== "resized")
|
|
202
|
+
Queue.offerUnsafe(outbox, JSON.stringify(event))
|
|
203
|
+
},
|
|
204
|
+
onEnd: () => Queue.offerUnsafe(outbox, new Socket.CloseEvent(1000)),
|
|
205
|
+
})
|
|
206
|
+
.pipe(
|
|
207
|
+
Effect.catchTags({
|
|
208
|
+
"PersistentPty.NotFoundError": () => Effect.succeed(undefined),
|
|
209
|
+
"PersistentPty.UnavailableError": () => Effect.succeed(undefined),
|
|
210
|
+
}),
|
|
211
|
+
)
|
|
212
|
+
if (!attachment) {
|
|
213
|
+
Queue.offerUnsafe(outbox, new Socket.CloseEvent(4404, "terminal unavailable"))
|
|
214
|
+
return
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
Queue.offerUnsafe(
|
|
218
|
+
outbox,
|
|
219
|
+
JSON.stringify({
|
|
220
|
+
type: "attached",
|
|
221
|
+
attachmentID,
|
|
222
|
+
inputProtocol: framedInput ? 1 : 0,
|
|
223
|
+
info: attachment.info,
|
|
224
|
+
role: attachment.role,
|
|
225
|
+
generation: attachment.generation,
|
|
226
|
+
replay: {
|
|
227
|
+
requestedOffset: attachment.replay.requestedOffset,
|
|
228
|
+
availableOffset: attachment.replay.availableOffset,
|
|
229
|
+
endOffset: attachment.replay.endOffset,
|
|
230
|
+
truncated: attachment.replay.truncated,
|
|
231
|
+
},
|
|
232
|
+
}),
|
|
233
|
+
)
|
|
234
|
+
if (attachment.replay.data.length > 0) Queue.offerUnsafe(outbox, attachment.replay.data)
|
|
235
|
+
Queue.offerUnsafe(
|
|
236
|
+
outbox,
|
|
237
|
+
JSON.stringify({ type: "replay_complete", endOffset: attachment.replay.endOffset }),
|
|
238
|
+
)
|
|
239
|
+
attachment.activate()
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
yield* runPtySocket({
|
|
243
|
+
socket,
|
|
244
|
+
outbox,
|
|
245
|
+
onOpen,
|
|
246
|
+
onMessage: (message) => {
|
|
247
|
+
if (!attachment) return Effect.void
|
|
248
|
+
const data = typeof message === "string" ? Buffer.from(message) : message
|
|
249
|
+
if (!framedInput)
|
|
250
|
+
return pty
|
|
251
|
+
.input(ctx.params.ptyID, attachmentID, attachment.info.size.cols, attachment.info.size.rows, data)
|
|
252
|
+
.pipe(Effect.ignore)
|
|
253
|
+
if (data.byteLength < 5) return Effect.void
|
|
254
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength)
|
|
255
|
+
const type = data[0]
|
|
256
|
+
const cols = view.getUint16(1)
|
|
257
|
+
const rows = view.getUint16(3)
|
|
258
|
+
if ((type !== 0 && type !== 1) || cols === 0 || rows === 0) return Effect.void
|
|
259
|
+
if (type === 0) return pty.control(ctx.params.ptyID, attachmentID, cols, rows).pipe(Effect.ignore)
|
|
260
|
+
return pty.input(ctx.params.ptyID, attachmentID, cols, rows, data.subarray(5)).pipe(Effect.ignore)
|
|
261
|
+
},
|
|
262
|
+
detach: () => attachment?.detach(),
|
|
263
|
+
})
|
|
264
|
+
return HttpServerResponse.empty()
|
|
265
|
+
}),
|
|
266
|
+
)
|
|
267
|
+
}),
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
const mapUnavailable = <A>(effect: Effect.Effect<A, PersistentPty.UnavailableError>) =>
|
|
271
|
+
effect.pipe(Effect.catchTag("PersistentPty.UnavailableError", unavailable))
|
|
272
|
+
|
|
273
|
+
const mapTerminalError = <A>(effect: Effect.Effect<A, PersistentPty.NotFoundError | PersistentPty.UnavailableError>) =>
|
|
274
|
+
effect.pipe(
|
|
275
|
+
Effect.catchTags({
|
|
276
|
+
"PersistentPty.NotFoundError": notFound,
|
|
277
|
+
"PersistentPty.UnavailableError": unavailable,
|
|
278
|
+
}),
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
const notFound = (error: PersistentPty.NotFoundError) =>
|
|
282
|
+
new PtyNotFoundError({ ptyID: error.ptyID, message: `PTY session not found: ${error.ptyID}` })
|
|
283
|
+
|
|
284
|
+
const unavailable = (error: PersistentPty.UnavailableError) =>
|
|
285
|
+
new ServiceUnavailableError({ message: error.message, service: "opencode-pty" })
|