@zyro-sec/server 1.0.1 → 1.0.3

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.
Files changed (130) hide show
  1. package/package.json +11 -8
  2. package/script/build.ts +28 -28
  3. package/script/pack.ts +5 -5
  4. package/script/workerd-probe.ts +59 -59
  5. package/src/api.ts +12 -12
  6. package/src/auth.ts +70 -70
  7. package/src/content-headers.ts +32 -32
  8. package/src/cors.ts +34 -34
  9. package/src/event-feed.ts +93 -93
  10. package/src/fetch.ts +67 -67
  11. package/src/handlers/agent.ts +23 -23
  12. package/src/handlers/artifact.ts +242 -242
  13. package/src/handlers/automation.ts +63 -63
  14. package/src/handlers/blob.ts +55 -55
  15. package/src/handlers/command.ts +8 -8
  16. package/src/handlers/config.ts +26 -26
  17. package/src/handlers/credential.ts +87 -87
  18. package/src/handlers/debug.ts +57 -57
  19. package/src/handlers/event.ts +38 -38
  20. package/src/handlers/form.ts +15 -15
  21. package/src/handlers/fs.ts +180 -180
  22. package/src/handlers/generate.ts +117 -117
  23. package/src/handlers/integration.ts +310 -310
  24. package/src/handlers/job.ts +29 -29
  25. package/src/handlers/location.ts +33 -33
  26. package/src/handlers/machine.ts +336 -336
  27. package/src/handlers/mcp.ts +160 -160
  28. package/src/handlers/media.ts +44 -44
  29. package/src/handlers/message.ts +82 -82
  30. package/src/handlers/migration.ts +13 -13
  31. package/src/handlers/model.ts +25 -25
  32. package/src/handlers/permission.ts +131 -131
  33. package/src/handlers/persistent-pty.ts +285 -285
  34. package/src/handlers/plugin.ts +97 -97
  35. package/src/handlers/project.ts +23 -23
  36. package/src/handlers/provider.ts +32 -32
  37. package/src/handlers/pty-socket.ts +47 -47
  38. package/src/handlers/pty.ts +181 -181
  39. package/src/handlers/push.ts +67 -67
  40. package/src/handlers/reference.ts +8 -8
  41. package/src/handlers/rpc.ts +31 -31
  42. package/src/handlers/server.ts +58 -58
  43. package/src/handlers/session-error.ts +47 -47
  44. package/src/handlers/session.ts +722 -722
  45. package/src/handlers/shell.ts +72 -72
  46. package/src/handlers/skill.ts +34 -34
  47. package/src/handlers/usage.ts +23 -23
  48. package/src/handlers/vcs.ts +104 -104
  49. package/src/handlers/voice.ts +50 -50
  50. package/src/handlers/websearch.ts +50 -50
  51. package/src/handlers/worktree.ts +60 -60
  52. package/src/handlers.ts +69 -69
  53. package/src/location.ts +86 -86
  54. package/src/media-uri.ts +13 -13
  55. package/src/middleware/authorization.ts +111 -111
  56. package/src/middleware/defect-response.ts +39 -39
  57. package/src/middleware/form-location.ts +44 -44
  58. package/src/middleware/schema-error.ts +49 -49
  59. package/src/middleware/session-location.ts +30 -30
  60. package/src/options.ts +61 -61
  61. package/src/pairing.ts +36 -36
  62. package/src/process.ts +274 -274
  63. package/src/range.ts +16 -16
  64. package/src/request-site.ts +36 -36
  65. package/src/request-tracing.ts +9 -9
  66. package/src/routes.ts +261 -261
  67. package/src/server-info.ts +41 -41
  68. package/src/service-status.ts +30 -30
  69. package/src/workerd.ts +184 -184
  70. package/test/fixture/mcp-starts.cjs +20 -20
  71. package/test/fixture/routes.ts +37 -37
  72. package/test/fixture/server.ts +20 -20
  73. package/test/fixture/worktree-delegate/index.ts +13 -13
  74. package/test/fixture/worktree-plugin/index.ts +47 -47
  75. package/tsconfig.build.json +15 -15
  76. package/tsconfig.json +11 -11
  77. package/test/artifact.test.ts +0 -224
  78. package/test/auth.test.ts +0 -24
  79. package/test/automation.test.ts +0 -315
  80. package/test/client-mcp.test.ts +0 -369
  81. package/test/codemode-api.test.ts +0 -88
  82. package/test/config.test.ts +0 -104
  83. package/test/cors.test.ts +0 -24
  84. package/test/defect-response.test.ts +0 -83
  85. package/test/event-feed.test.ts +0 -201
  86. package/test/fetch-option.test.ts +0 -104
  87. package/test/fetch.test.ts +0 -698
  88. package/test/flag-channel.test.ts +0 -126
  89. package/test/fs.test.ts +0 -439
  90. package/test/generate.test.ts +0 -132
  91. package/test/handler-policy.test.ts +0 -43
  92. package/test/heap-dump.test.ts +0 -137
  93. package/test/integration-policy.test.ts +0 -93
  94. package/test/job.test.ts +0 -186
  95. package/test/machine-iroh.test.ts +0 -259
  96. package/test/machine-link.test.ts +0 -1234
  97. package/test/machine-routes.test.ts +0 -198
  98. package/test/media.test.ts +0 -343
  99. package/test/message.test.ts +0 -105
  100. package/test/model.test.ts +0 -62
  101. package/test/options.test.ts +0 -42
  102. package/test/permission-saved.test.ts +0 -73
  103. package/test/persistent-pty.test.ts +0 -1007
  104. package/test/process.test.ts +0 -261
  105. package/test/provider.test.ts +0 -88
  106. package/test/pty-socket.property.test.ts +0 -240
  107. package/test/pty-socket.test.ts +0 -89
  108. package/test/pty-transport.test.ts +0 -200
  109. package/test/push-routes.test.ts +0 -157
  110. package/test/request-site.test.ts +0 -71
  111. package/test/request-tracing.test.ts +0 -87
  112. package/test/rpc-handler-errors.test.ts +0 -76
  113. package/test/schema-error.test.ts +0 -68
  114. package/test/service-status.test.ts +0 -34
  115. package/test/session-admit-location.test.ts +0 -92
  116. package/test/session-create.test.ts +0 -65
  117. package/test/session-diff.test.ts +0 -100
  118. package/test/session-form.test.ts +0 -50
  119. package/test/session-import.test.ts +0 -152
  120. package/test/session-instances.test.ts +0 -335
  121. package/test/session-unarchive.test.ts +0 -73
  122. package/test/session-update.test.ts +0 -66
  123. package/test/skill.test.ts +0 -57
  124. package/test/sse-failure.test.ts +0 -71
  125. package/test/usage.test.ts +0 -88
  126. package/test/vcs.test.ts +0 -356
  127. package/test/video-generate.test.ts +0 -131
  128. package/test/voice.test.ts +0 -170
  129. package/test/workerd.test.ts +0 -227
  130. package/test/worktree.test.ts +0 -333
@@ -1,181 +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
- )
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
+ )
@@ -1,67 +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
- })
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
+ })
@@ -1,8 +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
- )
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
+ )
@@ -1,31 +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
- )
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
+ )