@zyro-sec/server 1.0.0 → 1.0.1
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 +15 -21
- package/script/build.ts +28 -0
- package/script/pack.ts +5 -0
- package/script/workerd-probe.ts +59 -0
- package/src/api.ts +12 -0
- package/src/auth.ts +70 -0
- package/src/content-headers.ts +32 -0
- package/src/cors.ts +34 -0
- package/src/event-feed.ts +93 -0
- package/src/fetch.ts +67 -0
- package/src/handlers/agent.ts +23 -0
- package/src/handlers/artifact.ts +242 -0
- package/src/handlers/automation.ts +63 -0
- package/src/handlers/blob.ts +55 -0
- package/src/handlers/command.ts +8 -0
- package/src/handlers/config.ts +26 -0
- package/src/handlers/credential.ts +87 -0
- package/src/handlers/debug.ts +57 -0
- package/src/handlers/event.ts +38 -0
- package/src/handlers/form.ts +15 -0
- package/src/handlers/fs.ts +180 -0
- package/src/handlers/generate.ts +117 -0
- package/src/handlers/integration.ts +310 -0
- package/src/handlers/job.ts +29 -0
- package/src/handlers/location.ts +33 -0
- package/src/handlers/machine.ts +336 -0
- package/src/handlers/mcp.ts +160 -0
- package/src/handlers/media.ts +44 -0
- package/src/handlers/message.ts +82 -0
- package/src/handlers/migration.ts +13 -0
- package/src/handlers/model.ts +25 -0
- package/src/handlers/permission.ts +131 -0
- package/src/handlers/persistent-pty.ts +285 -0
- package/src/handlers/plugin.ts +97 -0
- package/src/handlers/project.ts +23 -0
- package/src/handlers/provider.ts +32 -0
- package/src/handlers/pty-socket.ts +47 -0
- package/src/handlers/pty.ts +181 -0
- package/src/handlers/push.ts +67 -0
- package/src/handlers/reference.ts +8 -0
- package/src/handlers/rpc.ts +31 -0
- package/src/handlers/server.ts +58 -0
- package/src/handlers/session-error.ts +47 -0
- package/src/handlers/session.ts +722 -0
- package/src/handlers/shell.ts +72 -0
- package/src/handlers/skill.ts +34 -0
- package/src/handlers/usage.ts +23 -0
- package/src/handlers/vcs.ts +104 -0
- package/src/handlers/voice.ts +50 -0
- package/src/handlers/websearch.ts +50 -0
- package/src/handlers/worktree.ts +60 -0
- package/src/handlers.ts +69 -0
- package/src/location.ts +86 -0
- package/src/media-uri.ts +13 -0
- package/src/middleware/authorization.ts +111 -0
- package/src/middleware/defect-response.ts +39 -0
- package/src/middleware/form-location.ts +44 -0
- package/src/middleware/schema-error.ts +49 -0
- package/src/middleware/session-location.ts +30 -0
- package/src/options.ts +61 -0
- package/src/pairing.ts +36 -0
- package/src/process.ts +274 -0
- package/src/range.ts +16 -0
- package/src/request-site.ts +36 -0
- package/src/request-tracing.ts +9 -0
- package/src/routes.ts +261 -0
- package/src/server-info.ts +41 -0
- package/src/service-status.ts +30 -0
- package/src/workerd.ts +184 -0
- package/test/artifact.test.ts +224 -0
- package/test/auth.test.ts +24 -0
- package/test/automation.test.ts +315 -0
- package/test/client-mcp.test.ts +369 -0
- package/test/codemode-api.test.ts +88 -0
- package/test/config.test.ts +104 -0
- package/test/cors.test.ts +24 -0
- package/test/defect-response.test.ts +83 -0
- package/test/event-feed.test.ts +201 -0
- package/test/fetch-option.test.ts +104 -0
- package/test/fetch.test.ts +698 -0
- package/test/fixture/mcp-starts.cjs +20 -0
- package/test/fixture/routes.ts +37 -0
- package/test/fixture/server.ts +20 -0
- package/test/fixture/worktree-delegate/index.ts +13 -0
- package/test/fixture/worktree-plugin/index.ts +47 -0
- package/test/flag-channel.test.ts +126 -0
- package/test/fs.test.ts +439 -0
- package/test/generate.test.ts +132 -0
- package/test/handler-policy.test.ts +43 -0
- package/test/heap-dump.test.ts +137 -0
- package/test/integration-policy.test.ts +93 -0
- package/test/job.test.ts +186 -0
- package/test/machine-iroh.test.ts +259 -0
- package/test/machine-link.test.ts +1234 -0
- package/test/machine-routes.test.ts +198 -0
- package/test/media.test.ts +343 -0
- package/test/message.test.ts +105 -0
- package/test/model.test.ts +62 -0
- package/test/options.test.ts +42 -0
- package/test/permission-saved.test.ts +73 -0
- package/test/persistent-pty.test.ts +1007 -0
- package/test/process.test.ts +261 -0
- package/test/provider.test.ts +88 -0
- package/test/pty-socket.property.test.ts +240 -0
- package/test/pty-socket.test.ts +89 -0
- package/test/pty-transport.test.ts +200 -0
- package/test/push-routes.test.ts +157 -0
- package/test/request-site.test.ts +71 -0
- package/test/request-tracing.test.ts +87 -0
- package/test/rpc-handler-errors.test.ts +76 -0
- package/test/schema-error.test.ts +68 -0
- package/test/service-status.test.ts +34 -0
- package/test/session-admit-location.test.ts +92 -0
- package/test/session-create.test.ts +65 -0
- package/test/session-diff.test.ts +100 -0
- package/test/session-form.test.ts +50 -0
- package/test/session-import.test.ts +152 -0
- package/test/session-instances.test.ts +335 -0
- package/test/session-unarchive.test.ts +73 -0
- package/test/session-update.test.ts +66 -0
- package/test/skill.test.ts +57 -0
- package/test/sse-failure.test.ts +71 -0
- package/test/usage.test.ts +88 -0
- package/test/vcs.test.ts +356 -0
- package/test/video-generate.test.ts +131 -0
- package/test/voice.test.ts +170 -0
- package/test/workerd.test.ts +227 -0
- package/test/worktree.test.ts +333 -0
- package/tsconfig.build.json +15 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { Instance } from "@zyro-sec/core/instance/service"
|
|
2
|
+
import { Location } from "@zyro-sec/core/location"
|
|
3
|
+
import { Permission } from "@zyro-sec/core/permission"
|
|
4
|
+
import { PermissionSaved } from "@zyro-sec/core/permission/saved"
|
|
5
|
+
import { Session } from "@zyro-sec/core/session"
|
|
6
|
+
import { Effect } from "effect"
|
|
7
|
+
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
|
|
8
|
+
import { Api } from "../api"
|
|
9
|
+
import { ConflictError, PermissionNotFoundError, PermissionSavedNotFoundError } from "@zyro-sec/protocol/errors"
|
|
10
|
+
import { locationErrors, response, sessionInfo } from "../location"
|
|
11
|
+
import { missingSession } from "./session-error"
|
|
12
|
+
|
|
13
|
+
function missingRequest(id: Permission.ID) {
|
|
14
|
+
return new PermissionNotFoundError({ requestID: id, message: `Permission request not found: ${id}` })
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const PermissionHandler = HttpApiBuilder.group(Api, "server.permission", (handlers) =>
|
|
18
|
+
Effect.gen(function* () {
|
|
19
|
+
const instances = yield* Instance.Service
|
|
20
|
+
const sessions = yield* Session.Service
|
|
21
|
+
const requireOwnedRequest = Effect.fnUntraced(function* (
|
|
22
|
+
sessionID: Permission.Request["sessionID"],
|
|
23
|
+
requestID: Permission.ID,
|
|
24
|
+
) {
|
|
25
|
+
const permission = yield* Permission.Service
|
|
26
|
+
const request = yield* permission.get(requestID)
|
|
27
|
+
if (!request || request.sessionID !== sessionID) return yield* missingRequest(requestID)
|
|
28
|
+
return { permission, request }
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
return handlers
|
|
32
|
+
.handle(
|
|
33
|
+
"permission.request.list",
|
|
34
|
+
Effect.fn(function* () {
|
|
35
|
+
const permission = yield* Permission.Service
|
|
36
|
+
return yield* response(permission.list())
|
|
37
|
+
}),
|
|
38
|
+
)
|
|
39
|
+
.handle(
|
|
40
|
+
"session.permission.create",
|
|
41
|
+
Effect.fn(function* (ctx) {
|
|
42
|
+
const permission = yield* Permission.Service
|
|
43
|
+
return {
|
|
44
|
+
data: yield* permission
|
|
45
|
+
.ask({
|
|
46
|
+
id: ctx.payload.id,
|
|
47
|
+
sessionID: ctx.params.sessionID,
|
|
48
|
+
action: ctx.payload.action,
|
|
49
|
+
resources: ctx.payload.resources,
|
|
50
|
+
save: ctx.payload.save,
|
|
51
|
+
metadata: ctx.payload.metadata,
|
|
52
|
+
source: ctx.payload.source,
|
|
53
|
+
agent: ctx.payload.agent,
|
|
54
|
+
})
|
|
55
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession)),
|
|
56
|
+
}
|
|
57
|
+
}),
|
|
58
|
+
)
|
|
59
|
+
.handle(
|
|
60
|
+
"session.permission.list",
|
|
61
|
+
Effect.fn(function* (ctx) {
|
|
62
|
+
const session = yield* sessionInfo(sessions, ctx.params.sessionID)
|
|
63
|
+
const requests = yield* Permission.Service.use((permission) =>
|
|
64
|
+
permission.forSession(ctx.params.sessionID),
|
|
65
|
+
).pipe(instances.provide(session), locationErrors)
|
|
66
|
+
return { data: requests }
|
|
67
|
+
}),
|
|
68
|
+
)
|
|
69
|
+
.handle(
|
|
70
|
+
"session.permission.get",
|
|
71
|
+
Effect.fn(function* (ctx) {
|
|
72
|
+
const owned = yield* requireOwnedRequest(ctx.params.sessionID, ctx.params.requestID)
|
|
73
|
+
return { data: owned.request }
|
|
74
|
+
}),
|
|
75
|
+
)
|
|
76
|
+
.handle(
|
|
77
|
+
"session.permission.reply",
|
|
78
|
+
Effect.fn(function* (ctx) {
|
|
79
|
+
const owned = yield* requireOwnedRequest(ctx.params.sessionID, ctx.params.requestID)
|
|
80
|
+
yield* owned.permission
|
|
81
|
+
.reply({ requestID: ctx.params.requestID, reply: ctx.payload.decision, message: ctx.payload.message })
|
|
82
|
+
.pipe(Effect.catchTag("Permission.NotFoundError", () => missingRequest(ctx.params.requestID)))
|
|
83
|
+
return HttpApiSchema.NoContent.make()
|
|
84
|
+
}),
|
|
85
|
+
)
|
|
86
|
+
.handle(
|
|
87
|
+
"permission.saved.list",
|
|
88
|
+
Effect.fn(function* (ctx) {
|
|
89
|
+
const location = yield* Location.Service
|
|
90
|
+
const saved = yield* PermissionSaved.Service
|
|
91
|
+
return {
|
|
92
|
+
data: yield* saved.list({
|
|
93
|
+
projectID: ctx.query.projectID ?? location.project.id,
|
|
94
|
+
}),
|
|
95
|
+
}
|
|
96
|
+
}),
|
|
97
|
+
)
|
|
98
|
+
.handle(
|
|
99
|
+
"permission.saved.update",
|
|
100
|
+
Effect.fn(function* (ctx) {
|
|
101
|
+
const saved = yield* PermissionSaved.Service
|
|
102
|
+
return {
|
|
103
|
+
data: yield* saved
|
|
104
|
+
.update({ id: ctx.params.id, action: ctx.payload.action, resource: ctx.payload.resource })
|
|
105
|
+
.pipe(
|
|
106
|
+
Effect.catchTags({
|
|
107
|
+
"PermissionSaved.NotFoundError": (error) =>
|
|
108
|
+
new PermissionSavedNotFoundError({
|
|
109
|
+
id: error.id,
|
|
110
|
+
message: `Saved permission not found: ${error.id}`,
|
|
111
|
+
}),
|
|
112
|
+
"PermissionSaved.ConflictError": (error) =>
|
|
113
|
+
new ConflictError({
|
|
114
|
+
resource: error.id,
|
|
115
|
+
message: `A saved permission already allows ${error.action} for ${error.resource}`,
|
|
116
|
+
}),
|
|
117
|
+
}),
|
|
118
|
+
),
|
|
119
|
+
}
|
|
120
|
+
}),
|
|
121
|
+
)
|
|
122
|
+
.handle(
|
|
123
|
+
"permission.saved.remove",
|
|
124
|
+
Effect.fn(function* (ctx) {
|
|
125
|
+
const saved = yield* PermissionSaved.Service
|
|
126
|
+
yield* saved.remove(ctx.params.id)
|
|
127
|
+
return HttpApiSchema.NoContent.make()
|
|
128
|
+
}),
|
|
129
|
+
)
|
|
130
|
+
}),
|
|
131
|
+
)
|
|
@@ -0,0 +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" })
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Plugin } from "@zyro-sec/core/plugin"
|
|
2
|
+
import { PluginUpdate } from "@zyro-sec/core/plugin/update"
|
|
3
|
+
import { InvalidRequestError, PluginNotFoundError, ServiceUnavailableError } from "@zyro-sec/protocol/errors"
|
|
4
|
+
import { Cause, Effect, Exit } from "effect"
|
|
5
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
6
|
+
import { Api } from "../api"
|
|
7
|
+
import { response } from "../location"
|
|
8
|
+
|
|
9
|
+
export const PluginHandler = HttpApiBuilder.group(Api, "server.plugin", (handlers) =>
|
|
10
|
+
handlers
|
|
11
|
+
.handle("plugin.list", () =>
|
|
12
|
+
Effect.gen(function* () {
|
|
13
|
+
return yield* response(Plugin.Service.use((plugin) => plugin.list()))
|
|
14
|
+
}),
|
|
15
|
+
)
|
|
16
|
+
.handle("plugin.gui", (ctx) =>
|
|
17
|
+
Effect.gen(function* () {
|
|
18
|
+
const plugins = yield* Plugin.Service
|
|
19
|
+
const bundle = yield* plugins.gui(ctx.query.id)
|
|
20
|
+
if (!bundle)
|
|
21
|
+
return yield* new PluginNotFoundError({
|
|
22
|
+
pluginID: ctx.query.id,
|
|
23
|
+
message: `No active plugin with a ./gui-experimental bundle: ${ctx.query.id}`,
|
|
24
|
+
})
|
|
25
|
+
return yield* response(Effect.succeed(bundle))
|
|
26
|
+
}),
|
|
27
|
+
)
|
|
28
|
+
.handle("plugin.check", (ctx) =>
|
|
29
|
+
Effect.gen(function* () {
|
|
30
|
+
const plugins = yield* Plugin.Service
|
|
31
|
+
const inventory = yield* plugins.list()
|
|
32
|
+
const targets = [
|
|
33
|
+
...new Set(inventory.flatMap((plugin) => (plugin.source.type === "package" ? [plugin.source.target] : []))),
|
|
34
|
+
].filter((target) => ctx.payload.target === undefined || target === ctx.payload.target)
|
|
35
|
+
if (ctx.payload.target !== undefined && !targets.length)
|
|
36
|
+
return yield* new InvalidRequestError({
|
|
37
|
+
message: `Plugin package is not in the current server inventory: ${ctx.payload.target}`,
|
|
38
|
+
field: "target",
|
|
39
|
+
})
|
|
40
|
+
const updates = yield* PluginUpdate.Service
|
|
41
|
+
const outdated = new Map(
|
|
42
|
+
yield* Effect.forEach(
|
|
43
|
+
targets,
|
|
44
|
+
(target) => updates.check(target, { refresh: true }).pipe(Effect.map((value) => [target, value] as const)),
|
|
45
|
+
{ concurrency: "unbounded" },
|
|
46
|
+
),
|
|
47
|
+
)
|
|
48
|
+
return yield* response(
|
|
49
|
+
Effect.succeed(
|
|
50
|
+
inventory.map((plugin) => {
|
|
51
|
+
if (plugin.source.type !== "package" || !outdated.has(plugin.source.target)) return plugin
|
|
52
|
+
return {
|
|
53
|
+
...plugin,
|
|
54
|
+
source: {
|
|
55
|
+
type: "package" as const,
|
|
56
|
+
target: plugin.source.target,
|
|
57
|
+
...(plugin.source.version ? { version: plugin.source.version } : {}),
|
|
58
|
+
...(outdated.get(plugin.source.target) ? { outdated: true as const } : {}),
|
|
59
|
+
...(plugin.source.updating ? { updating: true as const } : {}),
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
}),
|
|
63
|
+
),
|
|
64
|
+
)
|
|
65
|
+
}),
|
|
66
|
+
)
|
|
67
|
+
.handle("plugin.update", (ctx) =>
|
|
68
|
+
Effect.gen(function* () {
|
|
69
|
+
const plugins = yield* Plugin.Service
|
|
70
|
+
const inventory = new Set(
|
|
71
|
+
(yield* plugins.list()).flatMap((plugin) => (plugin.source.type === "package" ? [plugin.source.target] : [])),
|
|
72
|
+
)
|
|
73
|
+
const unknown = ctx.payload.targets.filter((target) => !inventory.has(target))
|
|
74
|
+
if (unknown.length)
|
|
75
|
+
return yield* new InvalidRequestError({
|
|
76
|
+
message: `Plugin packages are not in the current server inventory: ${unknown.join(", ")}`,
|
|
77
|
+
field: "targets",
|
|
78
|
+
})
|
|
79
|
+
const updates = yield* PluginUpdate.Service
|
|
80
|
+
// Let every update run to completion instead of interrupting the rest on the first failure.
|
|
81
|
+
const failures = yield* Effect.forEach(
|
|
82
|
+
ctx.payload.targets,
|
|
83
|
+
(target) =>
|
|
84
|
+
updates.update(target).pipe(
|
|
85
|
+
Effect.exit,
|
|
86
|
+
Effect.map((exit) => (Exit.isFailure(exit) ? [`${target}: ${Cause.pretty(exit.cause)}`] : [])),
|
|
87
|
+
),
|
|
88
|
+
{ concurrency: "unbounded" },
|
|
89
|
+
).pipe(Effect.map((results) => results.flat()))
|
|
90
|
+
if (failures.length)
|
|
91
|
+
return yield* new ServiceUnavailableError({
|
|
92
|
+
message: `Failed to update plugin packages: ${failures.join("; ")}`,
|
|
93
|
+
service: "plugin",
|
|
94
|
+
})
|
|
95
|
+
}),
|
|
96
|
+
),
|
|
97
|
+
)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Project } from "@zyro-sec/core/project"
|
|
2
|
+
import { Effect } from "effect"
|
|
3
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
4
|
+
import { Api } from "../api"
|
|
5
|
+
import { ProjectNotFoundError } from "@zyro-sec/protocol/errors"
|
|
6
|
+
|
|
7
|
+
export const ProjectHandler = HttpApiBuilder.group(Api, "server.project", (handlers) =>
|
|
8
|
+
handlers
|
|
9
|
+
.handle("project.list", (ctx) => Project.Service.use((project) => project.list(ctx.query)))
|
|
10
|
+
.handle("project.update", (ctx) =>
|
|
11
|
+
Project.Service.use((project) =>
|
|
12
|
+
project.update({ ...ctx.payload, projectID: ctx.params.projectID }).pipe(
|
|
13
|
+
Effect.mapError(
|
|
14
|
+
() =>
|
|
15
|
+
new ProjectNotFoundError({
|
|
16
|
+
projectID: ctx.params.projectID,
|
|
17
|
+
message: `Project not found: ${ctx.params.projectID}`,
|
|
18
|
+
}),
|
|
19
|
+
),
|
|
20
|
+
),
|
|
21
|
+
),
|
|
22
|
+
),
|
|
23
|
+
)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Provider } from "@zyro-sec/core/provider"
|
|
2
|
+
import { ProviderNotFoundError } from "@zyro-sec/protocol/errors"
|
|
3
|
+
import { Effect } from "effect"
|
|
4
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
5
|
+
import { Api } from "../api"
|
|
6
|
+
import { response } from "../location"
|
|
7
|
+
|
|
8
|
+
export const ProviderHandler = HttpApiBuilder.group(Api, "server.provider", (handlers) =>
|
|
9
|
+
Effect.gen(function* () {
|
|
10
|
+
return handlers
|
|
11
|
+
.handle(
|
|
12
|
+
"provider.list",
|
|
13
|
+
Effect.fn(function* () {
|
|
14
|
+
const providers = yield* Provider.Service
|
|
15
|
+
return yield* response(providers.available())
|
|
16
|
+
}),
|
|
17
|
+
)
|
|
18
|
+
.handle(
|
|
19
|
+
"provider.get",
|
|
20
|
+
Effect.fn(function* (ctx) {
|
|
21
|
+
const providers = yield* Provider.Service
|
|
22
|
+
const provider = yield* providers.get(ctx.params.providerID)
|
|
23
|
+
if (!provider)
|
|
24
|
+
return yield* new ProviderNotFoundError({
|
|
25
|
+
providerID: ctx.params.providerID,
|
|
26
|
+
message: `Provider not found: ${ctx.params.providerID}`,
|
|
27
|
+
})
|
|
28
|
+
return yield* response(Effect.succeed(provider))
|
|
29
|
+
}),
|
|
30
|
+
)
|
|
31
|
+
}),
|
|
32
|
+
)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { LocationLifecycle } from "@zyro-sec/core/location-lifecycle"
|
|
2
|
+
import { Effect, Queue } from "effect"
|
|
3
|
+
import { Socket } from "effect/socket"
|
|
4
|
+
|
|
5
|
+
export type Outbound = string | Uint8Array | Socket.CloseEvent
|
|
6
|
+
|
|
7
|
+
// Outbound frames flow through one queue drained by a single writer so replay, live output, and the close
|
|
8
|
+
// frame keep their order. Either side closing ends the connection normally, and every exit detaches.
|
|
9
|
+
export function runPtySocket<R>(input: {
|
|
10
|
+
readonly socket: Socket.Socket
|
|
11
|
+
readonly outbox: Queue.Dequeue<Outbound>
|
|
12
|
+
readonly onOpen?: Effect.Effect<void, never, R>
|
|
13
|
+
readonly onMessage: (message: string | Uint8Array) => Effect.Effect<void, never, R>
|
|
14
|
+
readonly detach: () => void
|
|
15
|
+
}) {
|
|
16
|
+
const drain = Effect.gen(function* () {
|
|
17
|
+
const writer = yield* input.socket.writer
|
|
18
|
+
while (true) {
|
|
19
|
+
const item = yield* Queue.take(input.outbox)
|
|
20
|
+
yield* writer.write(item)
|
|
21
|
+
if (item instanceof Socket.CloseEvent) return
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
const read = Effect.gen(function* () {
|
|
25
|
+
const reader = yield* input.socket.reader
|
|
26
|
+
if (input.onOpen) yield* input.onOpen
|
|
27
|
+
while (true) yield* Effect.forEach(yield* reader.pull, input.onMessage, { discard: true })
|
|
28
|
+
})
|
|
29
|
+
return Effect.raceFirst(drain, read).pipe(
|
|
30
|
+
Effect.ensuring(Effect.sync(input.detach)),
|
|
31
|
+
Effect.catchReason("SocketError", "SocketCloseError", () => Effect.void),
|
|
32
|
+
Effect.orDie,
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// A socket borrows its Location for as long as it stays open. Its client reconnects right away when the socket
|
|
37
|
+
// closes, so it keeps the idle sweep from evicting the Location (eviction would only boot a fresh one). Any other
|
|
38
|
+
// shutdown (reload, VCS init, debug eviction) closes the socket with 1012, which releases the borrow so the
|
|
39
|
+
// Location's scope can close, and the client reconnects to the replacement.
|
|
40
|
+
export const holdLocation = Effect.fnUntraced(function* (outbox: Queue.Enqueue<Outbound>) {
|
|
41
|
+
const lifecycle = yield* LocationLifecycle.Service
|
|
42
|
+
yield* lifecycle.retain
|
|
43
|
+
yield* lifecycle.awaitShutdown.pipe(
|
|
44
|
+
Effect.andThen(Queue.offer(outbox, new Socket.CloseEvent(1012, "location shut down"))),
|
|
45
|
+
Effect.forkChild,
|
|
46
|
+
)
|
|
47
|
+
})
|