@zyro-sec/server 1.0.0 → 1.0.2
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 -18
- 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,722 @@
|
|
|
1
|
+
import { Instance } from "@zyro-sec/core/instance/service"
|
|
2
|
+
import { Session } from "@zyro-sec/core/session"
|
|
3
|
+
import { SessionStats } from "@zyro-sec/core/session/stats"
|
|
4
|
+
import { SessionTitle } from "@zyro-sec/core/session/title"
|
|
5
|
+
import { SessionTransfer } from "@zyro-sec/core/session/transfer"
|
|
6
|
+
import { InstructionEntry } from "@zyro-sec/core/session/instruction-entry"
|
|
7
|
+
import { Form } from "@zyro-sec/core/form"
|
|
8
|
+
import { DateTime, Effect, Stream } from "effect"
|
|
9
|
+
import { HttpServerResponse } from "effect/http"
|
|
10
|
+
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
|
|
11
|
+
import { SessionMediaMessageResponse } from "@zyro-sec/protocol/groups/message"
|
|
12
|
+
import { SessionMediaInboxResponse } from "@zyro-sec/protocol/groups/session"
|
|
13
|
+
import { Api } from "../api"
|
|
14
|
+
import { wantsMediaRefs } from "../media-uri"
|
|
15
|
+
import {
|
|
16
|
+
ConflictError,
|
|
17
|
+
CommandExecutionError,
|
|
18
|
+
CommandNotFoundError,
|
|
19
|
+
FormAlreadySettledError,
|
|
20
|
+
FormInvalidAnswerError,
|
|
21
|
+
FormNotFoundError,
|
|
22
|
+
InvalidRequestError,
|
|
23
|
+
InvalidCursorError,
|
|
24
|
+
MessageNotFoundError,
|
|
25
|
+
ServiceUnavailableError,
|
|
26
|
+
SessionBusyError,
|
|
27
|
+
SkillNotFoundError,
|
|
28
|
+
} from "@zyro-sec/protocol/errors"
|
|
29
|
+
import { AbsolutePath } from "@zyro-sec/core/schema"
|
|
30
|
+
import { locationErrors } from "../location"
|
|
31
|
+
import { failedMessageDecode, failedSnapshot, missingMessage, missingSession, skillLoadFailed } from "./session-error"
|
|
32
|
+
|
|
33
|
+
function missingForm(id: Form.ID) {
|
|
34
|
+
return new FormNotFoundError({ id, message: `Form not found: ${id}` })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const SessionHandler = HttpApiBuilder.group(Api, "server.session", (handlers) =>
|
|
38
|
+
Effect.gen(function* () {
|
|
39
|
+
const session = yield* Session.Service
|
|
40
|
+
const transfer = yield* SessionTransfer.Service
|
|
41
|
+
const instructions = yield* InstructionEntry.Service
|
|
42
|
+
const instances = yield* Instance.Service
|
|
43
|
+
const requireSession = (sessionID: Session.ID) =>
|
|
44
|
+
session.get(sessionID).pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
45
|
+
const requireOwnedForm = Effect.fnUntraced(function* (sessionID: Form.Info["sessionID"], formID: Form.ID) {
|
|
46
|
+
const form = yield* Form.Service
|
|
47
|
+
const info = yield* form.get(formID).pipe(Effect.catchTag("Form.NotFoundError", () => missingForm(formID)))
|
|
48
|
+
if (info.sessionID !== sessionID) return yield* missingForm(formID)
|
|
49
|
+
return { form, info }
|
|
50
|
+
})
|
|
51
|
+
// Only these routes take input a person typed, so only they bring an archived session back. Automations, plugins
|
|
52
|
+
// and subagents prompt in process, and synthetic input is the app's own, so a background session stays archived.
|
|
53
|
+
const unarchiveOnUserInput = (sessionID: Session.ID) =>
|
|
54
|
+
session.unarchive(sessionID).pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
55
|
+
const busySession = (error: Session.BusyError) =>
|
|
56
|
+
new SessionBusyError({
|
|
57
|
+
sessionID: error.sessionID,
|
|
58
|
+
message: `Session is busy: ${error.sessionID}`,
|
|
59
|
+
})
|
|
60
|
+
const pendingMutation = (effect: ReturnType<typeof session.cancelInbox>, conflict: string) =>
|
|
61
|
+
effect.pipe(
|
|
62
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
63
|
+
Effect.catchTag(
|
|
64
|
+
"Session.InboxConflictError",
|
|
65
|
+
(error) => new ConflictError({ resource: error.inboxID, message: `${conflict}: ${error.inboxID}` }),
|
|
66
|
+
),
|
|
67
|
+
Effect.as(HttpApiSchema.NoContent.make()),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
return handlers
|
|
71
|
+
.handle(
|
|
72
|
+
"session.list",
|
|
73
|
+
Effect.fn(function* (ctx) {
|
|
74
|
+
return yield* session
|
|
75
|
+
.list(ctx.query)
|
|
76
|
+
.pipe(
|
|
77
|
+
Effect.catchTag(
|
|
78
|
+
"Session.InvalidListCursorError",
|
|
79
|
+
(error) => new InvalidCursorError({ message: error.message }),
|
|
80
|
+
),
|
|
81
|
+
)
|
|
82
|
+
}),
|
|
83
|
+
)
|
|
84
|
+
.handle(
|
|
85
|
+
"session.stats",
|
|
86
|
+
Effect.fn(function* (ctx) {
|
|
87
|
+
const timezone = ctx.query.timezone ?? "UTC"
|
|
88
|
+
yield* Effect.try({
|
|
89
|
+
try: () => new Intl.DateTimeFormat("en-US", { timeZone: timezone }),
|
|
90
|
+
catch: () => new InvalidRequestError({ message: `Invalid time zone: ${timezone}` }),
|
|
91
|
+
})
|
|
92
|
+
return {
|
|
93
|
+
data: yield* SessionStats.get({
|
|
94
|
+
from: ctx.query.from,
|
|
95
|
+
to: ctx.query.to,
|
|
96
|
+
projectID: ctx.query.project,
|
|
97
|
+
timezone,
|
|
98
|
+
tools: ctx.query.tools,
|
|
99
|
+
}).pipe(
|
|
100
|
+
Effect.mapError(() => new InvalidRequestError({ message: "Stats range must end after it starts" })),
|
|
101
|
+
),
|
|
102
|
+
}
|
|
103
|
+
}),
|
|
104
|
+
)
|
|
105
|
+
.handle(
|
|
106
|
+
"session.create",
|
|
107
|
+
Effect.fn(function* (ctx) {
|
|
108
|
+
return {
|
|
109
|
+
data: yield* session
|
|
110
|
+
.create({
|
|
111
|
+
id: ctx.payload.id,
|
|
112
|
+
title: ctx.payload.title,
|
|
113
|
+
agent: ctx.payload.agent,
|
|
114
|
+
model: ctx.payload.model,
|
|
115
|
+
metadata: ctx.payload.metadata,
|
|
116
|
+
permissions: ctx.payload.permissions,
|
|
117
|
+
...(ctx.payload.parentID === undefined
|
|
118
|
+
? { location: ctx.payload.location ?? { directory: AbsolutePath.make(process.cwd()) } }
|
|
119
|
+
: { parentID: ctx.payload.parentID }),
|
|
120
|
+
})
|
|
121
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession)),
|
|
122
|
+
}
|
|
123
|
+
}),
|
|
124
|
+
)
|
|
125
|
+
.handle(
|
|
126
|
+
"session.import",
|
|
127
|
+
Effect.fn(function* (ctx) {
|
|
128
|
+
return {
|
|
129
|
+
data: yield* transfer
|
|
130
|
+
.import({
|
|
131
|
+
data: { info: ctx.payload.info, messages: ctx.payload.messages },
|
|
132
|
+
location: ctx.payload.location ?? { directory: AbsolutePath.make(process.cwd()) },
|
|
133
|
+
})
|
|
134
|
+
.pipe(
|
|
135
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
136
|
+
Effect.catchTag(
|
|
137
|
+
"SessionTransfer.ImportConflictError",
|
|
138
|
+
(error) =>
|
|
139
|
+
new ConflictError({
|
|
140
|
+
message: `Session already exists: ${error.sessionID}`,
|
|
141
|
+
resource: error.sessionID,
|
|
142
|
+
}),
|
|
143
|
+
),
|
|
144
|
+
),
|
|
145
|
+
}
|
|
146
|
+
}),
|
|
147
|
+
)
|
|
148
|
+
.handle(
|
|
149
|
+
"session.export",
|
|
150
|
+
Effect.fn(function* (ctx) {
|
|
151
|
+
return {
|
|
152
|
+
data: yield* transfer
|
|
153
|
+
.export({ sessionID: ctx.params.sessionID, sanitize: ctx.query.sanitize })
|
|
154
|
+
.pipe(
|
|
155
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
156
|
+
Effect.catchTag("Session.MessageDecodeError", failedMessageDecode),
|
|
157
|
+
),
|
|
158
|
+
}
|
|
159
|
+
}),
|
|
160
|
+
)
|
|
161
|
+
.handle(
|
|
162
|
+
"session.active",
|
|
163
|
+
Effect.fn(function* () {
|
|
164
|
+
const active = yield* session.active
|
|
165
|
+
return {
|
|
166
|
+
data: Object.fromEntries(Array.from(active, (sessionID) => [sessionID, { type: "running" as const }])),
|
|
167
|
+
}
|
|
168
|
+
}),
|
|
169
|
+
)
|
|
170
|
+
.handle(
|
|
171
|
+
"session.get",
|
|
172
|
+
Effect.fn(function* (ctx) {
|
|
173
|
+
return {
|
|
174
|
+
data: yield* session
|
|
175
|
+
.get(ctx.params.sessionID)
|
|
176
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession)),
|
|
177
|
+
}
|
|
178
|
+
}),
|
|
179
|
+
)
|
|
180
|
+
.handle(
|
|
181
|
+
"session.view",
|
|
182
|
+
Effect.fn(function* (ctx) {
|
|
183
|
+
yield* session
|
|
184
|
+
.view({ sessionID: ctx.params.sessionID, idle: DateTime.toEpochMillis(ctx.payload.idle) })
|
|
185
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
186
|
+
return HttpApiSchema.NoContent.make()
|
|
187
|
+
}),
|
|
188
|
+
)
|
|
189
|
+
.handle(
|
|
190
|
+
"session.remove",
|
|
191
|
+
Effect.fn(function* (ctx) {
|
|
192
|
+
yield* session.remove(ctx.params.sessionID).pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
193
|
+
return HttpApiSchema.NoContent.make()
|
|
194
|
+
}),
|
|
195
|
+
)
|
|
196
|
+
.handle(
|
|
197
|
+
"session.environment",
|
|
198
|
+
Effect.fn(function* (ctx) {
|
|
199
|
+
yield* session
|
|
200
|
+
.environment({ sessionID: ctx.params.sessionID, variables: ctx.payload.variables })
|
|
201
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
202
|
+
return HttpApiSchema.NoContent.make()
|
|
203
|
+
}),
|
|
204
|
+
)
|
|
205
|
+
.handle(
|
|
206
|
+
"session.fork",
|
|
207
|
+
Effect.fn(function* (ctx) {
|
|
208
|
+
return {
|
|
209
|
+
data: yield* session.fork({ sessionID: ctx.params.sessionID, before: ctx.payload.before }).pipe(
|
|
210
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
211
|
+
Effect.catchTag("Session.MessageNotFoundError", missingMessage),
|
|
212
|
+
Effect.catchTag(
|
|
213
|
+
"Session.ForkEmptyError",
|
|
214
|
+
(error) => new InvalidRequestError({ message: error.message, kind: "empty_session" }),
|
|
215
|
+
),
|
|
216
|
+
),
|
|
217
|
+
}
|
|
218
|
+
}),
|
|
219
|
+
)
|
|
220
|
+
.handle(
|
|
221
|
+
"session.switchAgent",
|
|
222
|
+
Effect.fn(function* (ctx) {
|
|
223
|
+
yield* session
|
|
224
|
+
.switchAgent({ sessionID: ctx.params.sessionID, agent: ctx.payload.agent })
|
|
225
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
226
|
+
return HttpApiSchema.NoContent.make()
|
|
227
|
+
}),
|
|
228
|
+
)
|
|
229
|
+
.handle(
|
|
230
|
+
"session.switchModel",
|
|
231
|
+
Effect.fn(function* (ctx) {
|
|
232
|
+
yield* session
|
|
233
|
+
.switchModel({ sessionID: ctx.params.sessionID, model: ctx.payload.model })
|
|
234
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
235
|
+
return HttpApiSchema.NoContent.make()
|
|
236
|
+
}),
|
|
237
|
+
)
|
|
238
|
+
.handle(
|
|
239
|
+
"session.update",
|
|
240
|
+
Effect.fn(function* (ctx) {
|
|
241
|
+
const info = yield* requireSession(ctx.params.sessionID)
|
|
242
|
+
if (ctx.payload.title !== undefined) {
|
|
243
|
+
if (ctx.payload.title) {
|
|
244
|
+
yield* session
|
|
245
|
+
.rename({ sessionID: ctx.params.sessionID, title: ctx.payload.title })
|
|
246
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
247
|
+
} else {
|
|
248
|
+
yield* SessionTitle.Service.use((title) => title.generate(ctx.params.sessionID)).pipe(
|
|
249
|
+
instances.provide(info),
|
|
250
|
+
locationErrors,
|
|
251
|
+
)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (ctx.payload.metadata !== undefined)
|
|
255
|
+
yield* session
|
|
256
|
+
.setMetadata({ sessionID: ctx.params.sessionID, metadata: ctx.payload.metadata })
|
|
257
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
258
|
+
if (ctx.payload.permissions !== undefined)
|
|
259
|
+
yield* session
|
|
260
|
+
.setPermissions({ sessionID: ctx.params.sessionID, permissions: ctx.payload.permissions })
|
|
261
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
262
|
+
if (ctx.payload.archived !== undefined)
|
|
263
|
+
yield* (ctx.payload.archived ? session.archive : session.unarchive)(ctx.params.sessionID).pipe(
|
|
264
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
265
|
+
)
|
|
266
|
+
return HttpApiSchema.NoContent.make()
|
|
267
|
+
}),
|
|
268
|
+
)
|
|
269
|
+
.handle(
|
|
270
|
+
"session.move",
|
|
271
|
+
Effect.fn(function* (ctx) {
|
|
272
|
+
yield* session
|
|
273
|
+
.move({
|
|
274
|
+
sessionID: ctx.params.sessionID,
|
|
275
|
+
directory: ctx.payload.directory,
|
|
276
|
+
delivery: ctx.payload.delivery,
|
|
277
|
+
})
|
|
278
|
+
.pipe(
|
|
279
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
280
|
+
Effect.catchTag("Session.DestinationNotFoundError", (error) =>
|
|
281
|
+
Effect.fail(new InvalidRequestError({ message: `Directory does not exist: ${error.directory}` })),
|
|
282
|
+
),
|
|
283
|
+
Effect.catchTag("Session.DestinationNotDirectoryError", (error) =>
|
|
284
|
+
Effect.fail(new InvalidRequestError({ message: `Not a directory: ${error.directory}` })),
|
|
285
|
+
),
|
|
286
|
+
Effect.catchTag("Session.DestinationUnavailableError", (error) =>
|
|
287
|
+
Effect.fail(new InvalidRequestError({ message: `Directory is unavailable: ${error.directory}` })),
|
|
288
|
+
),
|
|
289
|
+
)
|
|
290
|
+
return HttpApiSchema.NoContent.make()
|
|
291
|
+
}),
|
|
292
|
+
)
|
|
293
|
+
.handle(
|
|
294
|
+
"session.prompt",
|
|
295
|
+
Effect.fn(function* (ctx) {
|
|
296
|
+
const data = yield* session
|
|
297
|
+
.prompt({
|
|
298
|
+
sessionID: ctx.params.sessionID,
|
|
299
|
+
id: ctx.payload.id,
|
|
300
|
+
text: ctx.payload.text,
|
|
301
|
+
files: ctx.payload.files,
|
|
302
|
+
agents: ctx.payload.agents,
|
|
303
|
+
skills: ctx.payload.skills,
|
|
304
|
+
metadata: ctx.payload.metadata,
|
|
305
|
+
delivery: ctx.payload.delivery,
|
|
306
|
+
resume: ctx.payload.resume,
|
|
307
|
+
})
|
|
308
|
+
.pipe(
|
|
309
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
310
|
+
Effect.catchTag("Session.PromptConflictError", (error) =>
|
|
311
|
+
Effect.fail(
|
|
312
|
+
new ConflictError({
|
|
313
|
+
message: `Prompt message ID conflicts with an existing durable record: ${error.messageID}`,
|
|
314
|
+
resource: error.messageID,
|
|
315
|
+
}),
|
|
316
|
+
),
|
|
317
|
+
),
|
|
318
|
+
Effect.catchTag("Session.AttachmentError", (error) =>
|
|
319
|
+
Effect.fail(new InvalidRequestError({ message: error.message, field: "files" })),
|
|
320
|
+
),
|
|
321
|
+
Effect.catchTag("Session.SkillNotFoundError", (error) =>
|
|
322
|
+
Effect.fail(new InvalidRequestError({ message: `Skill not found: ${error.skill}`, field: "skills" })),
|
|
323
|
+
),
|
|
324
|
+
Effect.catchTag("Skill.LoadError", skillLoadFailed),
|
|
325
|
+
locationErrors,
|
|
326
|
+
)
|
|
327
|
+
yield* unarchiveOnUserInput(ctx.params.sessionID)
|
|
328
|
+
return { data }
|
|
329
|
+
}),
|
|
330
|
+
)
|
|
331
|
+
.handle(
|
|
332
|
+
"session.command",
|
|
333
|
+
Effect.fn(function* (ctx) {
|
|
334
|
+
yield* session
|
|
335
|
+
.command({
|
|
336
|
+
sessionID: ctx.params.sessionID,
|
|
337
|
+
command: ctx.payload.name,
|
|
338
|
+
text: ctx.payload.text,
|
|
339
|
+
files: ctx.payload.files,
|
|
340
|
+
agents: ctx.payload.agents,
|
|
341
|
+
skills: ctx.payload.skills,
|
|
342
|
+
delivery: ctx.payload.delivery,
|
|
343
|
+
})
|
|
344
|
+
.pipe(
|
|
345
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
346
|
+
Effect.catchTag("Command.NotFoundError", (error) =>
|
|
347
|
+
Effect.fail(
|
|
348
|
+
new CommandNotFoundError({
|
|
349
|
+
command: error.command,
|
|
350
|
+
message: error.message,
|
|
351
|
+
}),
|
|
352
|
+
),
|
|
353
|
+
),
|
|
354
|
+
Effect.catchTag("Command.ExecutionError", (error) =>
|
|
355
|
+
Effect.fail(
|
|
356
|
+
new CommandExecutionError({
|
|
357
|
+
command: error.command,
|
|
358
|
+
message: error.message,
|
|
359
|
+
}),
|
|
360
|
+
),
|
|
361
|
+
),
|
|
362
|
+
locationErrors,
|
|
363
|
+
)
|
|
364
|
+
yield* unarchiveOnUserInput(ctx.params.sessionID)
|
|
365
|
+
return HttpApiSchema.NoContent.make()
|
|
366
|
+
}),
|
|
367
|
+
)
|
|
368
|
+
.handle(
|
|
369
|
+
"session.skill",
|
|
370
|
+
Effect.fn(function* (ctx) {
|
|
371
|
+
yield* session
|
|
372
|
+
.skill({
|
|
373
|
+
sessionID: ctx.params.sessionID,
|
|
374
|
+
skill: ctx.payload.id,
|
|
375
|
+
resume: ctx.payload.resume,
|
|
376
|
+
})
|
|
377
|
+
.pipe(
|
|
378
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
379
|
+
Effect.catchTag("Session.SkillNotFoundError", (error) =>
|
|
380
|
+
Effect.fail(new SkillNotFoundError({ skill: error.skill, message: `Skill not found: ${error.skill}` })),
|
|
381
|
+
),
|
|
382
|
+
Effect.catchTag("Skill.LoadError", skillLoadFailed),
|
|
383
|
+
locationErrors,
|
|
384
|
+
)
|
|
385
|
+
yield* unarchiveOnUserInput(ctx.params.sessionID)
|
|
386
|
+
return HttpApiSchema.NoContent.make()
|
|
387
|
+
}),
|
|
388
|
+
)
|
|
389
|
+
.handle(
|
|
390
|
+
"session.synthetic",
|
|
391
|
+
Effect.fn(function* (ctx) {
|
|
392
|
+
const data = yield* session
|
|
393
|
+
.synthetic({
|
|
394
|
+
id: ctx.payload.id,
|
|
395
|
+
sessionID: ctx.params.sessionID,
|
|
396
|
+
text: ctx.payload.text,
|
|
397
|
+
description: ctx.payload.description,
|
|
398
|
+
metadata: ctx.payload.metadata,
|
|
399
|
+
delivery: ctx.payload.delivery,
|
|
400
|
+
resume: ctx.payload.resume,
|
|
401
|
+
})
|
|
402
|
+
.pipe(
|
|
403
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
404
|
+
Effect.catchTag("Session.SyntheticConflictError", (error) =>
|
|
405
|
+
Effect.fail(
|
|
406
|
+
new ConflictError({
|
|
407
|
+
message: `Synthetic input ID conflicts with an existing durable record: ${error.inputID}`,
|
|
408
|
+
resource: error.inputID,
|
|
409
|
+
}),
|
|
410
|
+
),
|
|
411
|
+
),
|
|
412
|
+
)
|
|
413
|
+
return { data }
|
|
414
|
+
}),
|
|
415
|
+
)
|
|
416
|
+
.handle(
|
|
417
|
+
"session.shell",
|
|
418
|
+
Effect.fn(function* (ctx) {
|
|
419
|
+
// The shell call returns when the command ends, so the session comes back as it starts.
|
|
420
|
+
yield* unarchiveOnUserInput(ctx.params.sessionID)
|
|
421
|
+
yield* session
|
|
422
|
+
.shell({ sessionID: ctx.params.sessionID, id: ctx.payload.id, command: ctx.payload.command })
|
|
423
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
424
|
+
return HttpApiSchema.NoContent.make()
|
|
425
|
+
}),
|
|
426
|
+
)
|
|
427
|
+
.handle(
|
|
428
|
+
"session.compact",
|
|
429
|
+
Effect.fn(function* (ctx) {
|
|
430
|
+
return {
|
|
431
|
+
data: yield* session
|
|
432
|
+
.compact({ sessionID: ctx.params.sessionID, id: ctx.payload.id, delivery: ctx.payload.delivery })
|
|
433
|
+
.pipe(
|
|
434
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
435
|
+
Effect.catchTag("Session.CompactionConflictError", (error) =>
|
|
436
|
+
Effect.fail(
|
|
437
|
+
new ConflictError({
|
|
438
|
+
message: `Compaction input ID conflicts with an existing durable record: ${error.inputID}`,
|
|
439
|
+
resource: error.inputID,
|
|
440
|
+
}),
|
|
441
|
+
),
|
|
442
|
+
),
|
|
443
|
+
),
|
|
444
|
+
}
|
|
445
|
+
}),
|
|
446
|
+
)
|
|
447
|
+
.handle(
|
|
448
|
+
"session.wait",
|
|
449
|
+
Effect.fn(function* (ctx) {
|
|
450
|
+
yield* session.wait(ctx.params.sessionID).pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
451
|
+
return HttpApiSchema.NoContent.make()
|
|
452
|
+
}),
|
|
453
|
+
)
|
|
454
|
+
.handle(
|
|
455
|
+
"session.revert.stage",
|
|
456
|
+
Effect.fn(function* (ctx) {
|
|
457
|
+
yield* Effect.log("session.revert.stage", {
|
|
458
|
+
sessionID: ctx.params.sessionID,
|
|
459
|
+
messageID: ctx.payload.messageID,
|
|
460
|
+
files: ctx.payload.files,
|
|
461
|
+
})
|
|
462
|
+
return {
|
|
463
|
+
data: yield* session.revert
|
|
464
|
+
.stage({ ...ctx.params, ...ctx.payload })
|
|
465
|
+
.pipe(
|
|
466
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
467
|
+
Effect.catchTag("Session.MessageNotFoundError", missingMessage),
|
|
468
|
+
Effect.catchTag("Session.BusyError", busySession),
|
|
469
|
+
Effect.catchTag("Snapshot.Error", failedSnapshot("stage session revert", ctx.params.sessionID)),
|
|
470
|
+
locationErrors,
|
|
471
|
+
),
|
|
472
|
+
}
|
|
473
|
+
}),
|
|
474
|
+
)
|
|
475
|
+
.handle(
|
|
476
|
+
"session.revert.clear",
|
|
477
|
+
Effect.fn(function* (ctx) {
|
|
478
|
+
yield* Effect.log("session.revert.clear", { sessionID: ctx.params.sessionID })
|
|
479
|
+
yield* session.revert
|
|
480
|
+
.clear(ctx.params.sessionID)
|
|
481
|
+
.pipe(
|
|
482
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
483
|
+
Effect.catchTag("Session.BusyError", busySession),
|
|
484
|
+
Effect.catchTag("Snapshot.Error", failedSnapshot("clear session revert", ctx.params.sessionID)),
|
|
485
|
+
locationErrors,
|
|
486
|
+
)
|
|
487
|
+
return HttpApiSchema.NoContent.make()
|
|
488
|
+
}),
|
|
489
|
+
)
|
|
490
|
+
.handle(
|
|
491
|
+
"session.revert.commit",
|
|
492
|
+
Effect.fn(function* (ctx) {
|
|
493
|
+
yield* Effect.log("session.revert.commit", { sessionID: ctx.params.sessionID })
|
|
494
|
+
yield* session.revert
|
|
495
|
+
.commit(ctx.params.sessionID)
|
|
496
|
+
.pipe(
|
|
497
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
498
|
+
Effect.catchTag("Session.BusyError", busySession),
|
|
499
|
+
)
|
|
500
|
+
return HttpApiSchema.NoContent.make()
|
|
501
|
+
}),
|
|
502
|
+
)
|
|
503
|
+
.handle(
|
|
504
|
+
"session.context",
|
|
505
|
+
Effect.fn(function* (ctx) {
|
|
506
|
+
return {
|
|
507
|
+
data: yield* session
|
|
508
|
+
.context(ctx.params.sessionID)
|
|
509
|
+
.pipe(
|
|
510
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
511
|
+
Effect.catchTag("Session.MessageDecodeError", failedMessageDecode),
|
|
512
|
+
),
|
|
513
|
+
}
|
|
514
|
+
}),
|
|
515
|
+
)
|
|
516
|
+
.handle(
|
|
517
|
+
"session.diff",
|
|
518
|
+
Effect.fn(function* (ctx) {
|
|
519
|
+
return {
|
|
520
|
+
data: yield* session.diff({ sessionID: ctx.params.sessionID, ...ctx.query }).pipe(
|
|
521
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
522
|
+
Effect.catchTag("Session.MessageNotFoundError", missingMessage),
|
|
523
|
+
Effect.catchTag(
|
|
524
|
+
"Session.TurnRangeError",
|
|
525
|
+
(error) => new InvalidRequestError({ message: error.message, field: error.field }),
|
|
526
|
+
),
|
|
527
|
+
Effect.catchTag("Snapshot.Error", failedSnapshot("diff session turn", ctx.params.sessionID)),
|
|
528
|
+
locationErrors,
|
|
529
|
+
),
|
|
530
|
+
}
|
|
531
|
+
}),
|
|
532
|
+
)
|
|
533
|
+
.handle(
|
|
534
|
+
"session.inbox.list",
|
|
535
|
+
Effect.fn(function* (ctx) {
|
|
536
|
+
if (wantsMediaRefs(ctx.request))
|
|
537
|
+
return yield* HttpServerResponse.schemaJson(SessionMediaInboxResponse)({
|
|
538
|
+
data: yield* session
|
|
539
|
+
.inboxWithRefs(ctx.params.sessionID)
|
|
540
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession)),
|
|
541
|
+
}).pipe(Effect.orDie)
|
|
542
|
+
return {
|
|
543
|
+
data: yield* session
|
|
544
|
+
.inbox(ctx.params.sessionID)
|
|
545
|
+
.pipe(Effect.catchTag("Session.NotFoundError", missingSession)),
|
|
546
|
+
}
|
|
547
|
+
}),
|
|
548
|
+
)
|
|
549
|
+
.handle(
|
|
550
|
+
"session.inbox.cancel",
|
|
551
|
+
Effect.fn(function* (ctx) {
|
|
552
|
+
yield* session.cancelInbox({ sessionID: ctx.params.sessionID, inboxID: ctx.params.inboxID }).pipe(
|
|
553
|
+
Effect.catchTag("Session.NotFoundError", missingSession),
|
|
554
|
+
Effect.catchTag("Session.InboxConflictError", () => Effect.void),
|
|
555
|
+
)
|
|
556
|
+
return HttpApiSchema.NoContent.make()
|
|
557
|
+
}),
|
|
558
|
+
)
|
|
559
|
+
.handle(
|
|
560
|
+
"session.inbox.update",
|
|
561
|
+
Effect.fn(function* (ctx) {
|
|
562
|
+
return yield* pendingMutation(
|
|
563
|
+
ctx.payload.delivery === "steer"
|
|
564
|
+
? session.steerInbox({ sessionID: ctx.params.sessionID, inboxID: ctx.params.inboxID })
|
|
565
|
+
: session.queueInbox({ sessionID: ctx.params.sessionID, inboxID: ctx.params.inboxID }),
|
|
566
|
+
`Pending input cannot change to ${ctx.payload.delivery}`,
|
|
567
|
+
)
|
|
568
|
+
}),
|
|
569
|
+
)
|
|
570
|
+
.handle(
|
|
571
|
+
"session.instructions.entry.list",
|
|
572
|
+
Effect.fn(function* (ctx) {
|
|
573
|
+
yield* requireSession(ctx.params.sessionID)
|
|
574
|
+
return { data: yield* instructions.list(ctx.params.sessionID) }
|
|
575
|
+
}),
|
|
576
|
+
)
|
|
577
|
+
.handle(
|
|
578
|
+
"session.instructions.entry.put",
|
|
579
|
+
Effect.fn(function* (ctx) {
|
|
580
|
+
yield* requireSession(ctx.params.sessionID)
|
|
581
|
+
yield* instructions.put({ sessionID: ctx.params.sessionID, key: ctx.params.key, value: ctx.payload.value })
|
|
582
|
+
return HttpApiSchema.NoContent.make()
|
|
583
|
+
}),
|
|
584
|
+
)
|
|
585
|
+
.handle(
|
|
586
|
+
"session.instructions.entry.remove",
|
|
587
|
+
Effect.fn(function* (ctx) {
|
|
588
|
+
yield* requireSession(ctx.params.sessionID)
|
|
589
|
+
yield* instructions.remove({ sessionID: ctx.params.sessionID, key: ctx.params.key })
|
|
590
|
+
return HttpApiSchema.NoContent.make()
|
|
591
|
+
}),
|
|
592
|
+
)
|
|
593
|
+
.handle(
|
|
594
|
+
"session.generate",
|
|
595
|
+
Effect.fn(function* (ctx) {
|
|
596
|
+
const result = yield* session
|
|
597
|
+
.generate({ sessionID: ctx.params.sessionID, prompt: ctx.payload.prompt })
|
|
598
|
+
.pipe(
|
|
599
|
+
Effect.mapError((error) =>
|
|
600
|
+
error._tag === "Session.NotFoundError"
|
|
601
|
+
? missingSession(error)
|
|
602
|
+
: new ServiceUnavailableError({ message: error.message, service: "session generation" }),
|
|
603
|
+
),
|
|
604
|
+
)
|
|
605
|
+
return { data: result }
|
|
606
|
+
}),
|
|
607
|
+
)
|
|
608
|
+
.handle(
|
|
609
|
+
"session.log",
|
|
610
|
+
Effect.fn(function* (ctx) {
|
|
611
|
+
yield* session.get(ctx.params.sessionID).pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
612
|
+
return session
|
|
613
|
+
.log({ sessionID: ctx.params.sessionID, after: ctx.query.after, follow: ctx.query.follow })
|
|
614
|
+
.pipe(Stream.orDie)
|
|
615
|
+
}),
|
|
616
|
+
)
|
|
617
|
+
.handle(
|
|
618
|
+
"session.interrupt",
|
|
619
|
+
Effect.fn(function* (ctx) {
|
|
620
|
+
yield* requireSession(ctx.params.sessionID)
|
|
621
|
+
return { interrupted: yield* session.interrupt(ctx.params.sessionID, { resume: ctx.query.resume }) }
|
|
622
|
+
}),
|
|
623
|
+
)
|
|
624
|
+
.handle(
|
|
625
|
+
"session.background",
|
|
626
|
+
Effect.fn(function* (ctx) {
|
|
627
|
+
yield* session.background(ctx.params.sessionID).pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
628
|
+
return HttpApiSchema.NoContent.make()
|
|
629
|
+
}),
|
|
630
|
+
)
|
|
631
|
+
.handle(
|
|
632
|
+
"session.message",
|
|
633
|
+
Effect.fn(function* (ctx) {
|
|
634
|
+
yield* session.get(ctx.params.sessionID).pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
635
|
+
if (wantsMediaRefs(ctx.request)) {
|
|
636
|
+
const message = yield* session.messageWithRefs(ctx.params)
|
|
637
|
+
if (message)
|
|
638
|
+
return yield* HttpServerResponse.schemaJson(SessionMediaMessageResponse)({ data: message }).pipe(
|
|
639
|
+
Effect.orDie,
|
|
640
|
+
)
|
|
641
|
+
} else {
|
|
642
|
+
const message = yield* session.message(ctx.params)
|
|
643
|
+
if (message) return { data: message }
|
|
644
|
+
}
|
|
645
|
+
return yield* new MessageNotFoundError({
|
|
646
|
+
sessionID: ctx.params.sessionID,
|
|
647
|
+
messageID: ctx.params.messageID,
|
|
648
|
+
message: `Message not found: ${ctx.params.messageID}`,
|
|
649
|
+
})
|
|
650
|
+
}),
|
|
651
|
+
)
|
|
652
|
+
.handle(
|
|
653
|
+
"session.form.list",
|
|
654
|
+
Effect.fn(function* (ctx) {
|
|
655
|
+
const form = yield* Form.Service
|
|
656
|
+
return { data: yield* form.list({ sessionID: ctx.params.sessionID }) }
|
|
657
|
+
}),
|
|
658
|
+
)
|
|
659
|
+
.handle(
|
|
660
|
+
"session.form.create",
|
|
661
|
+
Effect.fn(function* (ctx) {
|
|
662
|
+
const form = yield* Form.Service
|
|
663
|
+
const created = yield* form
|
|
664
|
+
.create({
|
|
665
|
+
id: ctx.payload.id,
|
|
666
|
+
sessionID: ctx.params.sessionID,
|
|
667
|
+
title: ctx.payload.title,
|
|
668
|
+
metadata: ctx.payload.metadata,
|
|
669
|
+
fields: ctx.payload.fields,
|
|
670
|
+
})
|
|
671
|
+
.pipe(
|
|
672
|
+
Effect.catchTags({
|
|
673
|
+
"Form.AlreadyExistsError": (error) => new ConflictError({ resource: error.id, message: error.message }),
|
|
674
|
+
"Form.InvalidFormError": (error) =>
|
|
675
|
+
new InvalidRequestError({ message: error.message, field: "fields" }),
|
|
676
|
+
}),
|
|
677
|
+
)
|
|
678
|
+
return { data: created }
|
|
679
|
+
}),
|
|
680
|
+
)
|
|
681
|
+
.handle(
|
|
682
|
+
"session.form.get",
|
|
683
|
+
Effect.fn(function* (ctx) {
|
|
684
|
+
const owned = yield* requireOwnedForm(ctx.params.sessionID, ctx.params.formID)
|
|
685
|
+
const state = yield* owned.form
|
|
686
|
+
.state(ctx.params.formID)
|
|
687
|
+
.pipe(Effect.catchTag("Form.NotFoundError", () => missingForm(ctx.params.formID)))
|
|
688
|
+
return { data: { ...owned.info, state } }
|
|
689
|
+
}),
|
|
690
|
+
)
|
|
691
|
+
.handle(
|
|
692
|
+
"session.form.reply",
|
|
693
|
+
Effect.fn(function* (ctx) {
|
|
694
|
+
const owned = yield* requireOwnedForm(ctx.params.sessionID, ctx.params.formID)
|
|
695
|
+
yield* owned.form.reply({ id: ctx.params.formID, answer: ctx.payload.answer }).pipe(
|
|
696
|
+
Effect.catchTags({
|
|
697
|
+
"Form.AlreadySettledError": (error) =>
|
|
698
|
+
new FormAlreadySettledError({ id: error.id, message: error.message }),
|
|
699
|
+
"Form.InvalidAnswerError": (error) =>
|
|
700
|
+
new FormInvalidAnswerError({ id: error.id, message: error.message }),
|
|
701
|
+
"Form.NotFoundError": () => missingForm(ctx.params.formID),
|
|
702
|
+
}),
|
|
703
|
+
)
|
|
704
|
+
return HttpApiSchema.NoContent.make()
|
|
705
|
+
}),
|
|
706
|
+
)
|
|
707
|
+
.handle(
|
|
708
|
+
"session.form.cancel",
|
|
709
|
+
Effect.fn(function* (ctx) {
|
|
710
|
+
const owned = yield* requireOwnedForm(ctx.params.sessionID, ctx.params.formID)
|
|
711
|
+
yield* owned.form.cancel(ctx.params.formID, { message: ctx.query.message }).pipe(
|
|
712
|
+
Effect.catchTags({
|
|
713
|
+
"Form.AlreadySettledError": (error) =>
|
|
714
|
+
new FormAlreadySettledError({ id: error.id, message: error.message }),
|
|
715
|
+
"Form.NotFoundError": () => missingForm(ctx.params.formID),
|
|
716
|
+
}),
|
|
717
|
+
)
|
|
718
|
+
return HttpApiSchema.NoContent.make()
|
|
719
|
+
}),
|
|
720
|
+
)
|
|
721
|
+
}),
|
|
722
|
+
)
|