@zyro-sec/server 0.0.0-stage → 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 +40 -4
- 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
- package/README.md +0 -3
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { Pty } from "@zyro-sec/core/pty"
|
|
2
|
+
import { PtyProtocol } from "@zyro-sec/core/pty/protocol"
|
|
3
|
+
import { PtyTicket } from "@zyro-sec/core/pty/ticket"
|
|
4
|
+
import { Location } from "@zyro-sec/core/location"
|
|
5
|
+
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
6
|
+
import { Effect, Queue } from "effect"
|
|
7
|
+
import { HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
8
|
+
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
|
|
9
|
+
import { Socket } from "effect/socket"
|
|
10
|
+
import { Api } from "../api"
|
|
11
|
+
import { CorsConfig, isAllowedRequestOrigin } from "../cors"
|
|
12
|
+
import { ForbiddenError, PtyNotFoundError } from "@zyro-sec/protocol/errors"
|
|
13
|
+
import {
|
|
14
|
+
PTY_CONNECT_TICKET_QUERY,
|
|
15
|
+
PTY_CONNECT_TOKEN_HEADER,
|
|
16
|
+
PTY_CONNECT_TOKEN_HEADER_VALUE,
|
|
17
|
+
} from "@zyro-sec/protocol/groups/pty"
|
|
18
|
+
import { locationErrors, requestRef, response } from "../location"
|
|
19
|
+
import { holdLocation, type Outbound, runPtySocket } from "./pty-socket"
|
|
20
|
+
|
|
21
|
+
const notFound = (ptyID: Pty.ID) => new PtyNotFoundError({ ptyID, message: `PTY session not found: ${ptyID}` })
|
|
22
|
+
|
|
23
|
+
export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
|
|
24
|
+
Effect.gen(function* () {
|
|
25
|
+
const locations = yield* LocationServiceMap.Service
|
|
26
|
+
const tickets = yield* PtyTicket.Service
|
|
27
|
+
const cors = yield* CorsConfig
|
|
28
|
+
|
|
29
|
+
return handlers
|
|
30
|
+
.handle(
|
|
31
|
+
"pty.list",
|
|
32
|
+
Effect.fn(function* () {
|
|
33
|
+
const pty = yield* Pty.Service
|
|
34
|
+
return yield* response(pty.list())
|
|
35
|
+
}),
|
|
36
|
+
)
|
|
37
|
+
.handle(
|
|
38
|
+
"pty.create",
|
|
39
|
+
Effect.fn(function* (ctx) {
|
|
40
|
+
const pty = yield* Pty.Service
|
|
41
|
+
const location = yield* Location.Service
|
|
42
|
+
return yield* response(
|
|
43
|
+
pty.create({
|
|
44
|
+
...ctx.payload,
|
|
45
|
+
cwd: ctx.payload.cwd || location.directory,
|
|
46
|
+
}),
|
|
47
|
+
)
|
|
48
|
+
}),
|
|
49
|
+
)
|
|
50
|
+
.handle(
|
|
51
|
+
"pty.get",
|
|
52
|
+
Effect.fn(function* (ctx) {
|
|
53
|
+
const pty = yield* Pty.Service
|
|
54
|
+
return yield* response(
|
|
55
|
+
pty.get(ctx.params.ptyID).pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID))),
|
|
56
|
+
)
|
|
57
|
+
}),
|
|
58
|
+
)
|
|
59
|
+
.handle(
|
|
60
|
+
"pty.update",
|
|
61
|
+
Effect.fn(function* (ctx) {
|
|
62
|
+
const pty = yield* Pty.Service
|
|
63
|
+
return yield* response(
|
|
64
|
+
pty
|
|
65
|
+
.update(ctx.params.ptyID, ctx.payload)
|
|
66
|
+
.pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID))),
|
|
67
|
+
)
|
|
68
|
+
}),
|
|
69
|
+
)
|
|
70
|
+
.handle(
|
|
71
|
+
"pty.remove",
|
|
72
|
+
Effect.fn(function* (ctx) {
|
|
73
|
+
const pty = yield* Pty.Service
|
|
74
|
+
yield* pty
|
|
75
|
+
.remove(ctx.params.ptyID)
|
|
76
|
+
.pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID)))
|
|
77
|
+
return HttpApiSchema.NoContent.make()
|
|
78
|
+
}),
|
|
79
|
+
)
|
|
80
|
+
.handle(
|
|
81
|
+
"pty.connectToken",
|
|
82
|
+
Effect.fn(function* (ctx) {
|
|
83
|
+
const request = yield* HttpServerRequest.HttpServerRequest
|
|
84
|
+
// The custom header forces a CORS preflight, so cross-origin browser pages cannot
|
|
85
|
+
// mint tickets without passing the server's origin policy.
|
|
86
|
+
if (
|
|
87
|
+
request.headers[PTY_CONNECT_TOKEN_HEADER] !== PTY_CONNECT_TOKEN_HEADER_VALUE ||
|
|
88
|
+
!isAllowedRequestOrigin(request.headers.origin, request.headers.host, cors)
|
|
89
|
+
)
|
|
90
|
+
return yield* new ForbiddenError({ message: "Invalid PTY connect token request" })
|
|
91
|
+
const pty = yield* Pty.Service
|
|
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
|
+
}),
|
|
100
|
+
)
|
|
101
|
+
}),
|
|
102
|
+
)
|
|
103
|
+
.handleRaw(
|
|
104
|
+
"pty.connect",
|
|
105
|
+
Effect.fn("PtyHandler.connect")(function* (ctx) {
|
|
106
|
+
if (!isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors))
|
|
107
|
+
return HttpServerResponse.empty({ status: 403 })
|
|
108
|
+
|
|
109
|
+
const ref = LocationServiceMap.canonical(requestRef(ctx.request))
|
|
110
|
+
const url = new URL(ctx.request.url, "http://localhost")
|
|
111
|
+
const ticket = url.searchParams.get(PTY_CONNECT_TICKET_QUERY)
|
|
112
|
+
if (
|
|
113
|
+
ticket &&
|
|
114
|
+
!(yield* tickets.consume({
|
|
115
|
+
ticket,
|
|
116
|
+
ptyID: ctx.params.ptyID,
|
|
117
|
+
directory: ref.directory,
|
|
118
|
+
workspaceID: ref.workspaceID,
|
|
119
|
+
}))
|
|
120
|
+
)
|
|
121
|
+
return HttpServerResponse.empty({ status: 403 })
|
|
122
|
+
|
|
123
|
+
return yield* Effect.gen(function* () {
|
|
124
|
+
const pty = yield* Pty.Service
|
|
125
|
+
const exists = yield* pty.get(ctx.params.ptyID).pipe(
|
|
126
|
+
Effect.as(true),
|
|
127
|
+
Effect.catchTag("Pty.NotFoundError", () => Effect.succeed(false)),
|
|
128
|
+
)
|
|
129
|
+
if (!exists) return HttpServerResponse.empty({ status: 404 })
|
|
130
|
+
const parsedCursor = url.searchParams.get("cursor")
|
|
131
|
+
const cursorNumber = parsedCursor === null ? undefined : Number(parsedCursor)
|
|
132
|
+
const cursor =
|
|
133
|
+
cursorNumber !== undefined && Number.isSafeInteger(cursorNumber) && cursorNumber >= -1
|
|
134
|
+
? cursorNumber
|
|
135
|
+
: undefined
|
|
136
|
+
|
|
137
|
+
const socket = yield* Effect.orDie(ctx.request.upgrade)
|
|
138
|
+
// TODO: Integrate graceful-shutdown socket tracking before clients migrate to this route.
|
|
139
|
+
const outbox = yield* Queue.unbounded<Outbound>()
|
|
140
|
+
yield* holdLocation(outbox)
|
|
141
|
+
const attachment = yield* pty
|
|
142
|
+
.attach(ctx.params.ptyID, {
|
|
143
|
+
cursor,
|
|
144
|
+
onData: (chunk) => Queue.offerUnsafe(outbox, chunk),
|
|
145
|
+
onEnd: () => Queue.offerUnsafe(outbox, new Socket.CloseEvent(1000)),
|
|
146
|
+
})
|
|
147
|
+
.pipe(
|
|
148
|
+
Effect.catchTags({
|
|
149
|
+
"Pty.NotFoundError": () =>
|
|
150
|
+
Effect.sync(() => {
|
|
151
|
+
Queue.offerUnsafe(outbox, new Socket.CloseEvent(4404, "session not found"))
|
|
152
|
+
}),
|
|
153
|
+
"Pty.ExitedError": () =>
|
|
154
|
+
Effect.sync(() => {
|
|
155
|
+
Queue.offerUnsafe(outbox, new Socket.CloseEvent(4404, "session exited"))
|
|
156
|
+
}),
|
|
157
|
+
}),
|
|
158
|
+
)
|
|
159
|
+
if (attachment) {
|
|
160
|
+
for (const chunk of PtyProtocol.chunks(attachment.replay)) Queue.offerUnsafe(outbox, chunk)
|
|
161
|
+
Queue.offerUnsafe(outbox, PtyProtocol.metaFrame(attachment.cursor))
|
|
162
|
+
attachment.activate()
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
yield* runPtySocket({
|
|
166
|
+
socket,
|
|
167
|
+
outbox,
|
|
168
|
+
onMessage: (message) =>
|
|
169
|
+
Effect.sync(() => {
|
|
170
|
+
if (!attachment) return
|
|
171
|
+
const decoded = PtyProtocol.decodeInput(message)
|
|
172
|
+
if (decoded !== undefined) attachment.write(decoded)
|
|
173
|
+
}),
|
|
174
|
+
detach: () => attachment?.detach(),
|
|
175
|
+
})
|
|
176
|
+
return HttpServerResponse.empty()
|
|
177
|
+
}).pipe(Effect.scoped, Effect.provide(locations.get(ref)), locationErrors)
|
|
178
|
+
}),
|
|
179
|
+
)
|
|
180
|
+
}),
|
|
181
|
+
)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Effect, Option } from "effect"
|
|
2
|
+
import { HttpApiSchema } from "effect/http-api"
|
|
3
|
+
import { PushList } from "@zyro-sec/core/push/list"
|
|
4
|
+
import { InvalidRequestError, PushDeviceNotFoundError } from "@zyro-sec/protocol/errors"
|
|
5
|
+
import { Session } from "@zyro-sec/core/session"
|
|
6
|
+
import type { Push } from "@zyro-sec/schema/push"
|
|
7
|
+
|
|
8
|
+
export const makePush = Effect.gen(function* () {
|
|
9
|
+
const list = yield* PushList.Service
|
|
10
|
+
const sessions = yield* Session.Service
|
|
11
|
+
|
|
12
|
+
const invalid = (error: PushList.InvalidError) => new InvalidRequestError({ message: error.reason })
|
|
13
|
+
const notFound = (error: PushList.NotFoundError) =>
|
|
14
|
+
new PushDeviceNotFoundError({ deviceID: error.id, message: error.message })
|
|
15
|
+
// A subagent's notifications follow its top-level session's bell, so a bell on the subagent would do nothing.
|
|
16
|
+
const rejectSubagent = Effect.fnUntraced(function* (sessionID: Session.ID) {
|
|
17
|
+
const session = Option.getOrUndefined(yield* sessions.get(sessionID).pipe(Effect.option))
|
|
18
|
+
if (session?.parentID)
|
|
19
|
+
return yield* new InvalidRequestError({ message: "Set the bell on the top-level session, not on a subagent" })
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
get: Effect.fn("PushHandler.get")(function* () {
|
|
24
|
+
return { data: yield* list.info() }
|
|
25
|
+
}),
|
|
26
|
+
register: Effect.fn("PushHandler.register")(function* (id: Push.DeviceID, payload: Push.Register) {
|
|
27
|
+
return { data: yield* list.register(id, payload).pipe(Effect.mapError(invalid)) }
|
|
28
|
+
}),
|
|
29
|
+
remove: Effect.fn("PushHandler.remove")(function* (id: Push.DeviceID) {
|
|
30
|
+
yield* list.remove(id)
|
|
31
|
+
return HttpApiSchema.NoContent.make()
|
|
32
|
+
}),
|
|
33
|
+
update: Effect.fn("PushHandler.update")(function* (id: Push.DeviceID, payload: Push.DevicePatch) {
|
|
34
|
+
return { data: yield* list.update(id, payload).pipe(Effect.mapError(notFound)) }
|
|
35
|
+
}),
|
|
36
|
+
setDeviceBell: Effect.fn("PushHandler.setDeviceBell")(function* (
|
|
37
|
+
id: Push.DeviceID,
|
|
38
|
+
sessionID: Session.ID,
|
|
39
|
+
bell: Push.Bell,
|
|
40
|
+
) {
|
|
41
|
+
yield* rejectSubagent(sessionID)
|
|
42
|
+
return {
|
|
43
|
+
data: yield* list.setDeviceBell(id, sessionID, bell).pipe(
|
|
44
|
+
Effect.catchTags({
|
|
45
|
+
"PushList.InvalidError": (error) => Effect.fail(invalid(error)),
|
|
46
|
+
"PushList.NotFoundError": (error) => Effect.fail(notFound(error)),
|
|
47
|
+
}),
|
|
48
|
+
),
|
|
49
|
+
}
|
|
50
|
+
}),
|
|
51
|
+
removeDeviceBell: Effect.fn("PushHandler.removeDeviceBell")(function* (id: Push.DeviceID, sessionID: Session.ID) {
|
|
52
|
+
yield* list.removeDeviceBell(id, sessionID).pipe(Effect.mapError(notFound))
|
|
53
|
+
return HttpApiSchema.NoContent.make()
|
|
54
|
+
}),
|
|
55
|
+
setSessionBell: Effect.fn("PushHandler.setSessionBell")(function* (sessionID: Session.ID, bell: Push.Bell) {
|
|
56
|
+
yield* rejectSubagent(sessionID)
|
|
57
|
+
return { data: yield* list.setSessionBell(sessionID, bell).pipe(Effect.mapError(invalid)) }
|
|
58
|
+
}),
|
|
59
|
+
removeSessionBell: Effect.fn("PushHandler.removeSessionBell")(function* (sessionID: Session.ID) {
|
|
60
|
+
yield* list.removeSessionBell(sessionID)
|
|
61
|
+
return HttpApiSchema.NoContent.make()
|
|
62
|
+
}),
|
|
63
|
+
setSettings: Effect.fn("PushHandler.setSettings")(function* (settings: Push.Settings) {
|
|
64
|
+
return { data: yield* list.setSettings(settings).pipe(Effect.mapError(invalid)) }
|
|
65
|
+
}),
|
|
66
|
+
}
|
|
67
|
+
})
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Reference } from "@zyro-sec/core/reference"
|
|
2
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
3
|
+
import { Api } from "../api"
|
|
4
|
+
import { response } from "../location"
|
|
5
|
+
|
|
6
|
+
export const ReferenceHandler = HttpApiBuilder.group(Api, "server.reference", (handlers) =>
|
|
7
|
+
handlers.handle("reference.list", () => response(Reference.Service.use((reference) => reference.list()))),
|
|
8
|
+
)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Rpc } from "@zyro-sec/core/rpc"
|
|
2
|
+
import { RpcError, RpcInternalError } from "@zyro-sec/protocol/errors"
|
|
3
|
+
import { Effect } from "effect"
|
|
4
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
5
|
+
import { Api } from "../api"
|
|
6
|
+
|
|
7
|
+
export const RpcHandler = HttpApiBuilder.group(Api, "server.rpc", (handlers) =>
|
|
8
|
+
handlers.handle("rpc.call", ({ params, payload }) =>
|
|
9
|
+
Effect.gen(function* () {
|
|
10
|
+
const rpc = yield* Rpc.Service
|
|
11
|
+
const output = yield* rpc.call(params.rpcID, params.method, payload.input)
|
|
12
|
+
return output === undefined ? {} : { output }
|
|
13
|
+
}).pipe(
|
|
14
|
+
Effect.mapError((error) =>
|
|
15
|
+
error.type === "rpc.invalid_output" || error.type === "rpc.internal"
|
|
16
|
+
? new RpcInternalError({ type: error.type, message: error.message })
|
|
17
|
+
: new RpcError({
|
|
18
|
+
type: error.type,
|
|
19
|
+
message: error.message,
|
|
20
|
+
...(error.data === undefined ? {} : { data: error.data }),
|
|
21
|
+
}),
|
|
22
|
+
),
|
|
23
|
+
// Defects outside handler execution are still logged, never echoed to the client.
|
|
24
|
+
Effect.catchDefect((defect) =>
|
|
25
|
+
Effect.logError("rpc call failed", { rpc: params.rpcID, method: params.method, defect }).pipe(
|
|
26
|
+
Effect.andThen(Effect.fail(new RpcInternalError({ type: "rpc.internal", message: "RPC call failed" }))),
|
|
27
|
+
),
|
|
28
|
+
),
|
|
29
|
+
),
|
|
30
|
+
),
|
|
31
|
+
)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { hostname } from "node:os"
|
|
2
|
+
import { Duration, Effect } from "effect"
|
|
3
|
+
import { HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
4
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
5
|
+
import { PersistentPty } from "@zyro-sec/core/persistent-pty"
|
|
6
|
+
import { UnauthorizedError } from "@zyro-sec/protocol/errors"
|
|
7
|
+
import { Api } from "../api"
|
|
8
|
+
import { ServerAuth } from "../auth"
|
|
9
|
+
import { ServerInfo } from "../server-info"
|
|
10
|
+
import { ServerPairing } from "../pairing"
|
|
11
|
+
|
|
12
|
+
export const ServerHandler = HttpApiBuilder.group(Api, "server.server", (handlers) =>
|
|
13
|
+
Effect.gen(function* () {
|
|
14
|
+
const pairing = yield* ServerPairing.Service
|
|
15
|
+
const auth = yield* ServerAuth.Config
|
|
16
|
+
|
|
17
|
+
return handlers
|
|
18
|
+
.handle("server.info", () =>
|
|
19
|
+
Effect.gen(function* () {
|
|
20
|
+
const info = yield* ServerInfo.Service
|
|
21
|
+
return {
|
|
22
|
+
version: info.app.version ?? "unknown",
|
|
23
|
+
pid: process.pid ?? 0,
|
|
24
|
+
hostname: hostname() || undefined,
|
|
25
|
+
urls: info.urls(),
|
|
26
|
+
paths: info.paths,
|
|
27
|
+
capabilities: { persistentPty: PersistentPty.available },
|
|
28
|
+
}
|
|
29
|
+
}),
|
|
30
|
+
)
|
|
31
|
+
.handle("server.pair", () => pairing.issue())
|
|
32
|
+
.handle(
|
|
33
|
+
"server.connect",
|
|
34
|
+
Effect.fn(function* (ctx) {
|
|
35
|
+
const request = yield* HttpServerRequest.HttpServerRequest
|
|
36
|
+
// Browser navigations ask for HTML; everything else is an API client that wants the token.
|
|
37
|
+
const browser = request.headers.accept?.includes("text/html") === true
|
|
38
|
+
const token = (yield* pairing.consume(ctx.params.code)) ? ServerAuth.issueSession(auth) : undefined
|
|
39
|
+
if (token === undefined) {
|
|
40
|
+
if (!browser) return yield* new UnauthorizedError({ message: "Pairing link expired or already used" })
|
|
41
|
+
return HttpServerResponse.text(
|
|
42
|
+
"This pairing link expired or was already used. Run `opencode pair` to get a new one.",
|
|
43
|
+
{ status: 401 },
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
if (!browser) return { token }
|
|
47
|
+
return HttpServerResponse.redirect("/").pipe(
|
|
48
|
+
HttpServerResponse.setCookieUnsafe(ServerAuth.sessionCookieName(request.headers.host), token, {
|
|
49
|
+
path: "/",
|
|
50
|
+
httpOnly: true,
|
|
51
|
+
sameSite: "lax",
|
|
52
|
+
maxAge: Duration.seconds(ServerAuth.SESSION_TTL_SECONDS),
|
|
53
|
+
}),
|
|
54
|
+
)
|
|
55
|
+
}),
|
|
56
|
+
)
|
|
57
|
+
}),
|
|
58
|
+
)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Session } from "@zyro-sec/core/session"
|
|
2
|
+
import type { Snapshot } from "@zyro-sec/core/snapshot"
|
|
3
|
+
import { Skill } from "@zyro-sec/core/skill"
|
|
4
|
+
import { MessageNotFoundError, SessionNotFoundError, SkillLoadError, UnknownError } from "@zyro-sec/protocol/errors"
|
|
5
|
+
import { Effect } from "effect"
|
|
6
|
+
|
|
7
|
+
export function missingSession(error: Session.NotFoundError) {
|
|
8
|
+
return new SessionNotFoundError({
|
|
9
|
+
sessionID: error.sessionID,
|
|
10
|
+
message: `Session not found: ${error.sessionID}`,
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function skillLoadFailed(error: Skill.LoadError) {
|
|
15
|
+
return new SkillLoadError({ skill: Skill.ID.make(error.skill), message: error.message })
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function missingMessage(error: Session.MessageNotFoundError) {
|
|
19
|
+
return new MessageNotFoundError({
|
|
20
|
+
sessionID: error.sessionID,
|
|
21
|
+
messageID: error.messageID,
|
|
22
|
+
message: `Message not found: ${error.messageID}`,
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function failedMessageDecode(error: Session.MessageDecodeError) {
|
|
27
|
+
const ref = `err_${crypto.randomUUID().slice(0, 8)}`
|
|
28
|
+
return Effect.logError("failed to decode session message").pipe(
|
|
29
|
+
Effect.annotateLogs({ ref, sessionID: error.sessionID, messageID: error.messageID }),
|
|
30
|
+
Effect.andThen(
|
|
31
|
+
Effect.fail(new UnknownError({ message: "Unexpected server error. Check server logs for details.", ref })),
|
|
32
|
+
),
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Snapshot repositories are host state clients cannot repair, so surface only a log reference. */
|
|
37
|
+
export function failedSnapshot(operation: string, sessionID: Session.ID) {
|
|
38
|
+
return (error: Snapshot.Error) => {
|
|
39
|
+
const ref = `err_${crypto.randomUUID().slice(0, 8)}`
|
|
40
|
+
return Effect.logError(`failed to ${operation}`, { cause: error }).pipe(
|
|
41
|
+
Effect.annotateLogs({ ref, sessionID }),
|
|
42
|
+
Effect.andThen(
|
|
43
|
+
Effect.fail(new UnknownError({ message: "Unexpected server error. Check server logs for details.", ref })),
|
|
44
|
+
),
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
}
|