@zyro-sec/server 1.0.2 → 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.
- package/package.json +8 -8
- package/script/build.ts +28 -28
- package/script/pack.ts +5 -5
- package/script/workerd-probe.ts +59 -59
- package/src/api.ts +12 -12
- package/src/auth.ts +70 -70
- package/src/content-headers.ts +32 -32
- package/src/cors.ts +34 -34
- package/src/event-feed.ts +93 -93
- package/src/fetch.ts +67 -67
- package/src/handlers/agent.ts +23 -23
- package/src/handlers/artifact.ts +242 -242
- package/src/handlers/automation.ts +63 -63
- package/src/handlers/blob.ts +55 -55
- package/src/handlers/command.ts +8 -8
- package/src/handlers/config.ts +26 -26
- package/src/handlers/credential.ts +87 -87
- package/src/handlers/debug.ts +57 -57
- package/src/handlers/event.ts +38 -38
- package/src/handlers/form.ts +15 -15
- package/src/handlers/fs.ts +180 -180
- package/src/handlers/generate.ts +117 -117
- package/src/handlers/integration.ts +310 -310
- package/src/handlers/job.ts +29 -29
- package/src/handlers/location.ts +33 -33
- package/src/handlers/machine.ts +336 -336
- package/src/handlers/mcp.ts +160 -160
- package/src/handlers/media.ts +44 -44
- package/src/handlers/message.ts +82 -82
- package/src/handlers/migration.ts +13 -13
- package/src/handlers/model.ts +25 -25
- package/src/handlers/permission.ts +131 -131
- package/src/handlers/persistent-pty.ts +285 -285
- package/src/handlers/plugin.ts +97 -97
- package/src/handlers/project.ts +23 -23
- package/src/handlers/provider.ts +32 -32
- package/src/handlers/pty-socket.ts +47 -47
- package/src/handlers/pty.ts +181 -181
- package/src/handlers/push.ts +67 -67
- package/src/handlers/reference.ts +8 -8
- package/src/handlers/rpc.ts +31 -31
- package/src/handlers/server.ts +58 -58
- package/src/handlers/session-error.ts +47 -47
- package/src/handlers/session.ts +722 -722
- package/src/handlers/shell.ts +72 -72
- package/src/handlers/skill.ts +34 -34
- package/src/handlers/usage.ts +23 -23
- package/src/handlers/vcs.ts +104 -104
- package/src/handlers/voice.ts +50 -50
- package/src/handlers/websearch.ts +50 -50
- package/src/handlers/worktree.ts +60 -60
- package/src/handlers.ts +69 -69
- package/src/location.ts +86 -86
- package/src/media-uri.ts +13 -13
- package/src/middleware/authorization.ts +111 -111
- package/src/middleware/defect-response.ts +39 -39
- package/src/middleware/form-location.ts +44 -44
- package/src/middleware/schema-error.ts +49 -49
- package/src/middleware/session-location.ts +30 -30
- package/src/options.ts +61 -61
- package/src/pairing.ts +36 -36
- package/src/process.ts +274 -274
- package/src/range.ts +16 -16
- package/src/request-site.ts +36 -36
- package/src/request-tracing.ts +9 -9
- package/src/routes.ts +261 -261
- package/src/server-info.ts +41 -41
- package/src/service-status.ts +30 -30
- package/src/workerd.ts +184 -184
- package/test/fixture/mcp-starts.cjs +20 -20
- package/test/fixture/routes.ts +37 -37
- package/test/fixture/server.ts +20 -20
- package/test/fixture/worktree-delegate/index.ts +13 -13
- package/test/fixture/worktree-plugin/index.ts +47 -47
- package/tsconfig.build.json +15 -15
- package/tsconfig.json +11 -11
- package/test/artifact.test.ts +0 -224
- package/test/auth.test.ts +0 -24
- package/test/automation.test.ts +0 -315
- package/test/client-mcp.test.ts +0 -369
- package/test/codemode-api.test.ts +0 -88
- package/test/config.test.ts +0 -104
- package/test/cors.test.ts +0 -24
- package/test/defect-response.test.ts +0 -83
- package/test/event-feed.test.ts +0 -201
- package/test/fetch-option.test.ts +0 -104
- package/test/fetch.test.ts +0 -698
- package/test/flag-channel.test.ts +0 -126
- package/test/fs.test.ts +0 -439
- package/test/generate.test.ts +0 -132
- package/test/handler-policy.test.ts +0 -43
- package/test/heap-dump.test.ts +0 -137
- package/test/integration-policy.test.ts +0 -93
- package/test/job.test.ts +0 -186
- package/test/machine-iroh.test.ts +0 -259
- package/test/machine-link.test.ts +0 -1234
- package/test/machine-routes.test.ts +0 -198
- package/test/media.test.ts +0 -343
- package/test/message.test.ts +0 -105
- package/test/model.test.ts +0 -62
- package/test/options.test.ts +0 -42
- package/test/permission-saved.test.ts +0 -73
- package/test/persistent-pty.test.ts +0 -1007
- package/test/process.test.ts +0 -261
- package/test/provider.test.ts +0 -88
- package/test/pty-socket.property.test.ts +0 -240
- package/test/pty-socket.test.ts +0 -89
- package/test/pty-transport.test.ts +0 -200
- package/test/push-routes.test.ts +0 -157
- package/test/request-site.test.ts +0 -71
- package/test/request-tracing.test.ts +0 -87
- package/test/rpc-handler-errors.test.ts +0 -76
- package/test/schema-error.test.ts +0 -68
- package/test/service-status.test.ts +0 -34
- package/test/session-admit-location.test.ts +0 -92
- package/test/session-create.test.ts +0 -65
- package/test/session-diff.test.ts +0 -100
- package/test/session-form.test.ts +0 -50
- package/test/session-import.test.ts +0 -152
- package/test/session-instances.test.ts +0 -335
- package/test/session-unarchive.test.ts +0 -73
- package/test/session-update.test.ts +0 -66
- package/test/skill.test.ts +0 -57
- package/test/sse-failure.test.ts +0 -71
- package/test/usage.test.ts +0 -88
- package/test/vcs.test.ts +0 -356
- package/test/video-generate.test.ts +0 -131
- package/test/voice.test.ts +0 -170
- package/test/workerd.test.ts +0 -227
- package/test/worktree.test.ts +0 -333
package/src/handlers/fs.ts
CHANGED
|
@@ -1,180 +1,180 @@
|
|
|
1
|
-
import { Environment } from "@zyro-sec/core/environment/index"
|
|
2
|
-
import { FileSystem } from "@zyro-sec/core/filesystem"
|
|
3
|
-
import { FileWatch } from "@zyro-sec/core/filesystem/file-watch"
|
|
4
|
-
import { Location } from "@zyro-sec/core/location"
|
|
5
|
-
import { RelativePath } from "@zyro-sec/core/schema"
|
|
6
|
-
import { FileNotFoundError, ServiceUnavailableError } from "@zyro-sec/protocol/errors"
|
|
7
|
-
import { Effect, Option, Stream } from "effect"
|
|
8
|
-
import { type HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
9
|
-
import { HttpApiBuilder } from "effect/http-api"
|
|
10
|
-
import path from "path"
|
|
11
|
-
import { Api } from "../api"
|
|
12
|
-
import { ContentHeaders } from "../content-headers"
|
|
13
|
-
import { response } from "../location"
|
|
14
|
-
import { ByteRange } from "../range"
|
|
15
|
-
|
|
16
|
-
export const FileSystemHandler = HttpApiBuilder.group(Api, "server.fs", (handlers) =>
|
|
17
|
-
Effect.gen(function* () {
|
|
18
|
-
const fileWatch = yield* FileWatch.Service
|
|
19
|
-
return handlers
|
|
20
|
-
.handleRaw("fs.read", (ctx) =>
|
|
21
|
-
Effect.gen(function* () {
|
|
22
|
-
const path = yield* decodeRequestPath(ctx.request.url)
|
|
23
|
-
const fs = yield* FileSystem.Service
|
|
24
|
-
const file = yield* fs
|
|
25
|
-
.read({ path })
|
|
26
|
-
.pipe(
|
|
27
|
-
Effect.mapError(
|
|
28
|
-
(error) => new FileNotFoundError({ path: error.path, message: `File not found: ${error.path}` }),
|
|
29
|
-
),
|
|
30
|
-
)
|
|
31
|
-
return serveFile(ctx.request, file, ContentHeaders.download({ path, mime: file.mime }))
|
|
32
|
-
}),
|
|
33
|
-
)
|
|
34
|
-
.handle("fs.list", (ctx) =>
|
|
35
|
-
response(
|
|
36
|
-
Effect.gen(function* () {
|
|
37
|
-
const fs = yield* FileSystem.Service
|
|
38
|
-
return yield* fs
|
|
39
|
-
.list(ctx.query)
|
|
40
|
-
.pipe(
|
|
41
|
-
Effect.mapError(
|
|
42
|
-
(error) => new FileNotFoundError({ path: error.path, message: `Directory not found: ${error.path}` }),
|
|
43
|
-
),
|
|
44
|
-
)
|
|
45
|
-
}),
|
|
46
|
-
),
|
|
47
|
-
)
|
|
48
|
-
.handle("fs.find", (ctx) =>
|
|
49
|
-
response(
|
|
50
|
-
Effect.gen(function* () {
|
|
51
|
-
const fs = yield* FileSystem.Service
|
|
52
|
-
return yield* fs.find(ctx.query)
|
|
53
|
-
}),
|
|
54
|
-
),
|
|
55
|
-
)
|
|
56
|
-
.handle("fs.write", (ctx) =>
|
|
57
|
-
response(
|
|
58
|
-
Effect.gen(function* () {
|
|
59
|
-
const fs = yield* FileSystem.Service
|
|
60
|
-
return yield* fs.write({ path: ctx.query.path, data: ctx.payload })
|
|
61
|
-
}),
|
|
62
|
-
),
|
|
63
|
-
)
|
|
64
|
-
.handle("fs.watch", (ctx) =>
|
|
65
|
-
Effect.gen(function* () {
|
|
66
|
-
const environment = yield* Environment.Service
|
|
67
|
-
if (!environment.local)
|
|
68
|
-
return yield* new ServiceUnavailableError({
|
|
69
|
-
service: "filewatcher",
|
|
70
|
-
message: "File watching is not available for this location",
|
|
71
|
-
})
|
|
72
|
-
const fs = yield* FileSystem.Service
|
|
73
|
-
const location = yield* Location.Service
|
|
74
|
-
// Same confinement as read: the file must exist inside the location.
|
|
75
|
-
yield* fs
|
|
76
|
-
.read({ path: RelativePath.make(ctx.payload.path) })
|
|
77
|
-
.pipe(
|
|
78
|
-
Effect.mapError(
|
|
79
|
-
(error) => new FileNotFoundError({ path: error.path, message: `File not found: ${error.path}` }),
|
|
80
|
-
),
|
|
81
|
-
)
|
|
82
|
-
yield* fileWatch
|
|
83
|
-
.watch({
|
|
84
|
-
location: { directory: location.directory, workspaceID: location.workspaceID },
|
|
85
|
-
file: path.resolve(location.directory, ctx.payload.path),
|
|
86
|
-
})
|
|
87
|
-
.pipe(
|
|
88
|
-
Effect.mapError(
|
|
89
|
-
(error) => new ServiceUnavailableError({ message: error.message, service: "filewatcher" }),
|
|
90
|
-
),
|
|
91
|
-
)
|
|
92
|
-
}),
|
|
93
|
-
)
|
|
94
|
-
.handle("fs.unwatch", (ctx) =>
|
|
95
|
-
Effect.gen(function* () {
|
|
96
|
-
const environment = yield* Environment.Service
|
|
97
|
-
if (!environment.local) return
|
|
98
|
-
const location = yield* Location.Service
|
|
99
|
-
yield* fileWatch.unwatch({
|
|
100
|
-
location: { directory: location.directory, workspaceID: location.workspaceID },
|
|
101
|
-
file: path.resolve(location.directory, ctx.payload.path),
|
|
102
|
-
})
|
|
103
|
-
}),
|
|
104
|
-
)
|
|
105
|
-
}),
|
|
106
|
-
)
|
|
107
|
-
|
|
108
|
-
function decodeRequestPath(url: string) {
|
|
109
|
-
const raw = new URL(url, "http://localhost").pathname.slice(13)
|
|
110
|
-
return Effect.try({
|
|
111
|
-
try: () => RelativePath.make(decodeURIComponent(raw)),
|
|
112
|
-
catch: () => new FileNotFoundError({ path: raw, message: `File not found: ${raw}` }),
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function serveFile(
|
|
117
|
-
request: HttpServerRequest.HttpServerRequest,
|
|
118
|
-
file: FileSystem.File,
|
|
119
|
-
content: ReturnType<typeof ContentHeaders.download>,
|
|
120
|
-
) {
|
|
121
|
-
const etag = `W/"${file.size.toString(16)}-${Option.match(file.mtime, {
|
|
122
|
-
onNone: () => "0",
|
|
123
|
-
onSome: (mtime) => mtime.getTime().toString(16),
|
|
124
|
-
})}"`
|
|
125
|
-
const lastModified = Option.getOrUndefined(Option.map(file.mtime, (mtime) => mtime.toUTCString()))
|
|
126
|
-
// Clients must revalidate after agent edits; no-transform keeps compression from changing Content-Length.
|
|
127
|
-
const validators = {
|
|
128
|
-
"cache-control": "no-cache, no-transform",
|
|
129
|
-
etag,
|
|
130
|
-
...(lastModified === undefined ? {} : { "last-modified": lastModified }),
|
|
131
|
-
}
|
|
132
|
-
if (isNotModified(request, etag, lastModified))
|
|
133
|
-
return HttpServerResponse.empty({ status: 304, headers: { ...validators, ...content } })
|
|
134
|
-
|
|
135
|
-
const rangeHeader = request.method === "GET" ? request.headers["range"] : undefined
|
|
136
|
-
const range =
|
|
137
|
-
rangeHeader !== undefined && matchesIfRange(request.headers["if-range"], etag, lastModified)
|
|
138
|
-
? ByteRange.parse(rangeHeader, file.size)
|
|
139
|
-
: undefined
|
|
140
|
-
if (range === "unsatisfiable")
|
|
141
|
-
return HttpServerResponse.empty({
|
|
142
|
-
status: 416,
|
|
143
|
-
headers: { "accept-ranges": "bytes", "content-range": `bytes */${file.size}` },
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
const offset = range?.start ?? 0
|
|
147
|
-
const length = range === undefined ? file.size : range.end - range.start + 1
|
|
148
|
-
return HttpServerResponse.stream(length === 0 ? Stream.empty : file.stream({ offset, bytesToRead: length }), {
|
|
149
|
-
status: range === undefined ? 200 : 206,
|
|
150
|
-
contentLength: length,
|
|
151
|
-
headers: {
|
|
152
|
-
...validators,
|
|
153
|
-
...content,
|
|
154
|
-
"accept-ranges": "bytes",
|
|
155
|
-
...(range === undefined ? {} : { "content-range": `bytes ${range.start}-${range.end}/${file.size}` }),
|
|
156
|
-
},
|
|
157
|
-
})
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function isNotModified(request: HttpServerRequest.HttpServerRequest, etag: string, lastModified: string | undefined) {
|
|
161
|
-
const ifNoneMatch = request.headers["if-none-match"]
|
|
162
|
-
if (ifNoneMatch !== undefined)
|
|
163
|
-
return ifNoneMatch.split(",").some((tag) => tag.trim() === "*" || stripWeak(tag) === stripWeak(etag))
|
|
164
|
-
const ifModifiedSince = request.headers["if-modified-since"]
|
|
165
|
-
return (
|
|
166
|
-
ifModifiedSince !== undefined &&
|
|
167
|
-
lastModified !== undefined &&
|
|
168
|
-
Date.parse(lastModified) <= Date.parse(ifModifiedSince)
|
|
169
|
-
)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function matchesIfRange(ifRange: string | undefined, etag: string, lastModified: string | undefined) {
|
|
173
|
-
if (ifRange === undefined) return true
|
|
174
|
-
if (/^\s*(w\/)?"/i.test(ifRange)) return stripWeak(ifRange) === stripWeak(etag)
|
|
175
|
-
return lastModified !== undefined && Date.parse(ifRange) === Date.parse(lastModified)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function stripWeak(etag: string) {
|
|
179
|
-
return etag.trim().replace(/^w\//i, "")
|
|
180
|
-
}
|
|
1
|
+
import { Environment } from "@zyro-sec/core/environment/index"
|
|
2
|
+
import { FileSystem } from "@zyro-sec/core/filesystem"
|
|
3
|
+
import { FileWatch } from "@zyro-sec/core/filesystem/file-watch"
|
|
4
|
+
import { Location } from "@zyro-sec/core/location"
|
|
5
|
+
import { RelativePath } from "@zyro-sec/core/schema"
|
|
6
|
+
import { FileNotFoundError, ServiceUnavailableError } from "@zyro-sec/protocol/errors"
|
|
7
|
+
import { Effect, Option, Stream } from "effect"
|
|
8
|
+
import { type HttpServerRequest, HttpServerResponse } from "effect/http"
|
|
9
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
10
|
+
import path from "path"
|
|
11
|
+
import { Api } from "../api"
|
|
12
|
+
import { ContentHeaders } from "../content-headers"
|
|
13
|
+
import { response } from "../location"
|
|
14
|
+
import { ByteRange } from "../range"
|
|
15
|
+
|
|
16
|
+
export const FileSystemHandler = HttpApiBuilder.group(Api, "server.fs", (handlers) =>
|
|
17
|
+
Effect.gen(function* () {
|
|
18
|
+
const fileWatch = yield* FileWatch.Service
|
|
19
|
+
return handlers
|
|
20
|
+
.handleRaw("fs.read", (ctx) =>
|
|
21
|
+
Effect.gen(function* () {
|
|
22
|
+
const path = yield* decodeRequestPath(ctx.request.url)
|
|
23
|
+
const fs = yield* FileSystem.Service
|
|
24
|
+
const file = yield* fs
|
|
25
|
+
.read({ path })
|
|
26
|
+
.pipe(
|
|
27
|
+
Effect.mapError(
|
|
28
|
+
(error) => new FileNotFoundError({ path: error.path, message: `File not found: ${error.path}` }),
|
|
29
|
+
),
|
|
30
|
+
)
|
|
31
|
+
return serveFile(ctx.request, file, ContentHeaders.download({ path, mime: file.mime }))
|
|
32
|
+
}),
|
|
33
|
+
)
|
|
34
|
+
.handle("fs.list", (ctx) =>
|
|
35
|
+
response(
|
|
36
|
+
Effect.gen(function* () {
|
|
37
|
+
const fs = yield* FileSystem.Service
|
|
38
|
+
return yield* fs
|
|
39
|
+
.list(ctx.query)
|
|
40
|
+
.pipe(
|
|
41
|
+
Effect.mapError(
|
|
42
|
+
(error) => new FileNotFoundError({ path: error.path, message: `Directory not found: ${error.path}` }),
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
}),
|
|
46
|
+
),
|
|
47
|
+
)
|
|
48
|
+
.handle("fs.find", (ctx) =>
|
|
49
|
+
response(
|
|
50
|
+
Effect.gen(function* () {
|
|
51
|
+
const fs = yield* FileSystem.Service
|
|
52
|
+
return yield* fs.find(ctx.query)
|
|
53
|
+
}),
|
|
54
|
+
),
|
|
55
|
+
)
|
|
56
|
+
.handle("fs.write", (ctx) =>
|
|
57
|
+
response(
|
|
58
|
+
Effect.gen(function* () {
|
|
59
|
+
const fs = yield* FileSystem.Service
|
|
60
|
+
return yield* fs.write({ path: ctx.query.path, data: ctx.payload })
|
|
61
|
+
}),
|
|
62
|
+
),
|
|
63
|
+
)
|
|
64
|
+
.handle("fs.watch", (ctx) =>
|
|
65
|
+
Effect.gen(function* () {
|
|
66
|
+
const environment = yield* Environment.Service
|
|
67
|
+
if (!environment.local)
|
|
68
|
+
return yield* new ServiceUnavailableError({
|
|
69
|
+
service: "filewatcher",
|
|
70
|
+
message: "File watching is not available for this location",
|
|
71
|
+
})
|
|
72
|
+
const fs = yield* FileSystem.Service
|
|
73
|
+
const location = yield* Location.Service
|
|
74
|
+
// Same confinement as read: the file must exist inside the location.
|
|
75
|
+
yield* fs
|
|
76
|
+
.read({ path: RelativePath.make(ctx.payload.path) })
|
|
77
|
+
.pipe(
|
|
78
|
+
Effect.mapError(
|
|
79
|
+
(error) => new FileNotFoundError({ path: error.path, message: `File not found: ${error.path}` }),
|
|
80
|
+
),
|
|
81
|
+
)
|
|
82
|
+
yield* fileWatch
|
|
83
|
+
.watch({
|
|
84
|
+
location: { directory: location.directory, workspaceID: location.workspaceID },
|
|
85
|
+
file: path.resolve(location.directory, ctx.payload.path),
|
|
86
|
+
})
|
|
87
|
+
.pipe(
|
|
88
|
+
Effect.mapError(
|
|
89
|
+
(error) => new ServiceUnavailableError({ message: error.message, service: "filewatcher" }),
|
|
90
|
+
),
|
|
91
|
+
)
|
|
92
|
+
}),
|
|
93
|
+
)
|
|
94
|
+
.handle("fs.unwatch", (ctx) =>
|
|
95
|
+
Effect.gen(function* () {
|
|
96
|
+
const environment = yield* Environment.Service
|
|
97
|
+
if (!environment.local) return
|
|
98
|
+
const location = yield* Location.Service
|
|
99
|
+
yield* fileWatch.unwatch({
|
|
100
|
+
location: { directory: location.directory, workspaceID: location.workspaceID },
|
|
101
|
+
file: path.resolve(location.directory, ctx.payload.path),
|
|
102
|
+
})
|
|
103
|
+
}),
|
|
104
|
+
)
|
|
105
|
+
}),
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
function decodeRequestPath(url: string) {
|
|
109
|
+
const raw = new URL(url, "http://localhost").pathname.slice(13)
|
|
110
|
+
return Effect.try({
|
|
111
|
+
try: () => RelativePath.make(decodeURIComponent(raw)),
|
|
112
|
+
catch: () => new FileNotFoundError({ path: raw, message: `File not found: ${raw}` }),
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function serveFile(
|
|
117
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
118
|
+
file: FileSystem.File,
|
|
119
|
+
content: ReturnType<typeof ContentHeaders.download>,
|
|
120
|
+
) {
|
|
121
|
+
const etag = `W/"${file.size.toString(16)}-${Option.match(file.mtime, {
|
|
122
|
+
onNone: () => "0",
|
|
123
|
+
onSome: (mtime) => mtime.getTime().toString(16),
|
|
124
|
+
})}"`
|
|
125
|
+
const lastModified = Option.getOrUndefined(Option.map(file.mtime, (mtime) => mtime.toUTCString()))
|
|
126
|
+
// Clients must revalidate after agent edits; no-transform keeps compression from changing Content-Length.
|
|
127
|
+
const validators = {
|
|
128
|
+
"cache-control": "no-cache, no-transform",
|
|
129
|
+
etag,
|
|
130
|
+
...(lastModified === undefined ? {} : { "last-modified": lastModified }),
|
|
131
|
+
}
|
|
132
|
+
if (isNotModified(request, etag, lastModified))
|
|
133
|
+
return HttpServerResponse.empty({ status: 304, headers: { ...validators, ...content } })
|
|
134
|
+
|
|
135
|
+
const rangeHeader = request.method === "GET" ? request.headers["range"] : undefined
|
|
136
|
+
const range =
|
|
137
|
+
rangeHeader !== undefined && matchesIfRange(request.headers["if-range"], etag, lastModified)
|
|
138
|
+
? ByteRange.parse(rangeHeader, file.size)
|
|
139
|
+
: undefined
|
|
140
|
+
if (range === "unsatisfiable")
|
|
141
|
+
return HttpServerResponse.empty({
|
|
142
|
+
status: 416,
|
|
143
|
+
headers: { "accept-ranges": "bytes", "content-range": `bytes */${file.size}` },
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
const offset = range?.start ?? 0
|
|
147
|
+
const length = range === undefined ? file.size : range.end - range.start + 1
|
|
148
|
+
return HttpServerResponse.stream(length === 0 ? Stream.empty : file.stream({ offset, bytesToRead: length }), {
|
|
149
|
+
status: range === undefined ? 200 : 206,
|
|
150
|
+
contentLength: length,
|
|
151
|
+
headers: {
|
|
152
|
+
...validators,
|
|
153
|
+
...content,
|
|
154
|
+
"accept-ranges": "bytes",
|
|
155
|
+
...(range === undefined ? {} : { "content-range": `bytes ${range.start}-${range.end}/${file.size}` }),
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function isNotModified(request: HttpServerRequest.HttpServerRequest, etag: string, lastModified: string | undefined) {
|
|
161
|
+
const ifNoneMatch = request.headers["if-none-match"]
|
|
162
|
+
if (ifNoneMatch !== undefined)
|
|
163
|
+
return ifNoneMatch.split(",").some((tag) => tag.trim() === "*" || stripWeak(tag) === stripWeak(etag))
|
|
164
|
+
const ifModifiedSince = request.headers["if-modified-since"]
|
|
165
|
+
return (
|
|
166
|
+
ifModifiedSince !== undefined &&
|
|
167
|
+
lastModified !== undefined &&
|
|
168
|
+
Date.parse(lastModified) <= Date.parse(ifModifiedSince)
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function matchesIfRange(ifRange: string | undefined, etag: string, lastModified: string | undefined) {
|
|
173
|
+
if (ifRange === undefined) return true
|
|
174
|
+
if (/^\s*(w\/)?"/i.test(ifRange)) return stripWeak(ifRange) === stripWeak(etag)
|
|
175
|
+
return lastModified !== undefined && Date.parse(ifRange) === Date.parse(lastModified)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function stripWeak(etag: string) {
|
|
179
|
+
return etag.trim().replace(/^w\//i, "")
|
|
180
|
+
}
|
package/src/handlers/generate.ts
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
import { Generate } from "@zyro-sec/core/generate"
|
|
2
|
-
import { VideoGeneration } from "@zyro-sec/core/video-generation"
|
|
3
|
-
import { Location } from "@zyro-sec/core/location"
|
|
4
|
-
import { LocationServiceMap } from "@zyro-sec/core/location-services"
|
|
5
|
-
import { AbsolutePath } from "@zyro-sec/core/schema"
|
|
6
|
-
import {
|
|
7
|
-
VideoNotFoundError,
|
|
8
|
-
InvalidRequestError,
|
|
9
|
-
ModelNotFoundError,
|
|
10
|
-
ProviderRequestError,
|
|
11
|
-
ServiceUnavailableError,
|
|
12
|
-
UnsupportedModelError,
|
|
13
|
-
} from "@zyro-sec/protocol/errors"
|
|
14
|
-
import type { DecisionRequest, VideoID, ImageRequest, VideoRequest } from "@zyro-sec/schema/generate"
|
|
15
|
-
import { Global } from "@zyro-sec/util/global"
|
|
16
|
-
import { Model } from "@zyro-sec/schema/model"
|
|
17
|
-
import { Effect } from "effect"
|
|
18
|
-
import { HttpApiBuilder } from "effect/http-api"
|
|
19
|
-
import { Api } from "../api"
|
|
20
|
-
import { locationErrors } from "../location"
|
|
21
|
-
|
|
22
|
-
export const GenerateHandler = HttpApiBuilder.group(Api, "server.generate", (handlers) =>
|
|
23
|
-
Effect.gen(function* () {
|
|
24
|
-
const global = yield* Global.Service
|
|
25
|
-
const locations = yield* LocationServiceMap.Service
|
|
26
|
-
const videos = yield* VideoGeneration.Service
|
|
27
|
-
const services = locations.get(Location.Ref.make({ directory: AbsolutePath.make(global.config) }))
|
|
28
|
-
const text = Effect.fn("server.generate.text")(
|
|
29
|
-
function* (request: { readonly payload: { readonly prompt: string; readonly model?: Model.Ref } }) {
|
|
30
|
-
const generate = yield* Generate.Service
|
|
31
|
-
return { data: { text: yield* generate.text(request.payload).pipe(Effect.mapError(generateError)) } }
|
|
32
|
-
},
|
|
33
|
-
Effect.provide(services),
|
|
34
|
-
locationErrors,
|
|
35
|
-
)
|
|
36
|
-
const image = Effect.fn("server.generate.image")(
|
|
37
|
-
function* (request: { readonly payload: ImageRequest }) {
|
|
38
|
-
const generate = yield* Generate.Service
|
|
39
|
-
return { data: yield* generate.image(request.payload).pipe(Effect.mapError(generateError)) }
|
|
40
|
-
},
|
|
41
|
-
Effect.provide(services),
|
|
42
|
-
locationErrors,
|
|
43
|
-
)
|
|
44
|
-
const decisions = Effect.fn("server.generate.decisions")(
|
|
45
|
-
function* (request: { readonly payload: DecisionRequest }) {
|
|
46
|
-
const names = new Set(request.payload.questions.map((question) => question.name))
|
|
47
|
-
if (names.size !== request.payload.questions.length)
|
|
48
|
-
return yield* new InvalidRequestError({
|
|
49
|
-
message: "Decision question names must be unique",
|
|
50
|
-
field: "questions",
|
|
51
|
-
})
|
|
52
|
-
const generate = yield* Generate.Service
|
|
53
|
-
return { data: yield* generate.decisions(request.payload).pipe(Effect.mapError(generateError)) }
|
|
54
|
-
},
|
|
55
|
-
Effect.provide(services),
|
|
56
|
-
locationErrors,
|
|
57
|
-
)
|
|
58
|
-
const videoCreate = Effect.fn("server.generate.video.create")(
|
|
59
|
-
function* (request: { readonly payload: VideoRequest }) {
|
|
60
|
-
return {
|
|
61
|
-
data: yield* videos.start(Generate.videoInput(request.payload)).pipe(Effect.mapError(generateError)),
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
Effect.provide(services),
|
|
65
|
-
locationErrors,
|
|
66
|
-
)
|
|
67
|
-
const videoGet = Effect.fn("server.generate.video.get")(
|
|
68
|
-
function* (request: { readonly params: { readonly id: VideoID } }) {
|
|
69
|
-
const job = yield* videos
|
|
70
|
-
.get(request.params.id)
|
|
71
|
-
.pipe(Effect.mapError((error) => new VideoNotFoundError({ videoID: error.id, message: error.message })))
|
|
72
|
-
return { data: job }
|
|
73
|
-
},
|
|
74
|
-
Effect.provide(services),
|
|
75
|
-
locationErrors,
|
|
76
|
-
)
|
|
77
|
-
return handlers
|
|
78
|
-
.handle("generate.text", text)
|
|
79
|
-
.handle("generate.textLegacy", text)
|
|
80
|
-
.handle("generate.image", image)
|
|
81
|
-
.handle("generate.decisions", decisions)
|
|
82
|
-
.handle("generate.video.create", videoCreate)
|
|
83
|
-
.handle("generate.video.get", videoGet)
|
|
84
|
-
}),
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
function generateError(error: Generate.Error) {
|
|
88
|
-
switch (error._tag) {
|
|
89
|
-
case "Generate.ModelNotFoundError":
|
|
90
|
-
return new ModelNotFoundError({ providerID: error.providerID, modelID: error.modelID, message: error.message })
|
|
91
|
-
case "Generate.UnsupportedModelError":
|
|
92
|
-
return new UnsupportedModelError({
|
|
93
|
-
providerID: error.providerID,
|
|
94
|
-
modelID: error.modelID,
|
|
95
|
-
capability: error.capability,
|
|
96
|
-
message: error.message,
|
|
97
|
-
})
|
|
98
|
-
case "Generate.InvalidRequestError":
|
|
99
|
-
return new InvalidRequestError({
|
|
100
|
-
message: error.message,
|
|
101
|
-
...(error.field === undefined ? {} : { field: error.field }),
|
|
102
|
-
})
|
|
103
|
-
case "Generate.ProviderRequestError":
|
|
104
|
-
return new ProviderRequestError({
|
|
105
|
-
providerID: error.providerID,
|
|
106
|
-
reason: error.reason,
|
|
107
|
-
message: error.message,
|
|
108
|
-
...(error.status === undefined ? {} : { status: error.status }),
|
|
109
|
-
...(error.retryAfterMs === undefined ? {} : { retryAfterMs: error.retryAfterMs }),
|
|
110
|
-
})
|
|
111
|
-
case "Generate.UnavailableError":
|
|
112
|
-
return new ServiceUnavailableError({
|
|
113
|
-
message: error.message,
|
|
114
|
-
...(error.service === undefined ? {} : { service: error.service }),
|
|
115
|
-
})
|
|
116
|
-
}
|
|
117
|
-
}
|
|
1
|
+
import { Generate } from "@zyro-sec/core/generate"
|
|
2
|
+
import { VideoGeneration } from "@zyro-sec/core/video-generation"
|
|
3
|
+
import { Location } from "@zyro-sec/core/location"
|
|
4
|
+
import { LocationServiceMap } from "@zyro-sec/core/location-services"
|
|
5
|
+
import { AbsolutePath } from "@zyro-sec/core/schema"
|
|
6
|
+
import {
|
|
7
|
+
VideoNotFoundError,
|
|
8
|
+
InvalidRequestError,
|
|
9
|
+
ModelNotFoundError,
|
|
10
|
+
ProviderRequestError,
|
|
11
|
+
ServiceUnavailableError,
|
|
12
|
+
UnsupportedModelError,
|
|
13
|
+
} from "@zyro-sec/protocol/errors"
|
|
14
|
+
import type { DecisionRequest, VideoID, ImageRequest, VideoRequest } from "@zyro-sec/schema/generate"
|
|
15
|
+
import { Global } from "@zyro-sec/util/global"
|
|
16
|
+
import { Model } from "@zyro-sec/schema/model"
|
|
17
|
+
import { Effect } from "effect"
|
|
18
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
19
|
+
import { Api } from "../api"
|
|
20
|
+
import { locationErrors } from "../location"
|
|
21
|
+
|
|
22
|
+
export const GenerateHandler = HttpApiBuilder.group(Api, "server.generate", (handlers) =>
|
|
23
|
+
Effect.gen(function* () {
|
|
24
|
+
const global = yield* Global.Service
|
|
25
|
+
const locations = yield* LocationServiceMap.Service
|
|
26
|
+
const videos = yield* VideoGeneration.Service
|
|
27
|
+
const services = locations.get(Location.Ref.make({ directory: AbsolutePath.make(global.config) }))
|
|
28
|
+
const text = Effect.fn("server.generate.text")(
|
|
29
|
+
function* (request: { readonly payload: { readonly prompt: string; readonly model?: Model.Ref } }) {
|
|
30
|
+
const generate = yield* Generate.Service
|
|
31
|
+
return { data: { text: yield* generate.text(request.payload).pipe(Effect.mapError(generateError)) } }
|
|
32
|
+
},
|
|
33
|
+
Effect.provide(services),
|
|
34
|
+
locationErrors,
|
|
35
|
+
)
|
|
36
|
+
const image = Effect.fn("server.generate.image")(
|
|
37
|
+
function* (request: { readonly payload: ImageRequest }) {
|
|
38
|
+
const generate = yield* Generate.Service
|
|
39
|
+
return { data: yield* generate.image(request.payload).pipe(Effect.mapError(generateError)) }
|
|
40
|
+
},
|
|
41
|
+
Effect.provide(services),
|
|
42
|
+
locationErrors,
|
|
43
|
+
)
|
|
44
|
+
const decisions = Effect.fn("server.generate.decisions")(
|
|
45
|
+
function* (request: { readonly payload: DecisionRequest }) {
|
|
46
|
+
const names = new Set(request.payload.questions.map((question) => question.name))
|
|
47
|
+
if (names.size !== request.payload.questions.length)
|
|
48
|
+
return yield* new InvalidRequestError({
|
|
49
|
+
message: "Decision question names must be unique",
|
|
50
|
+
field: "questions",
|
|
51
|
+
})
|
|
52
|
+
const generate = yield* Generate.Service
|
|
53
|
+
return { data: yield* generate.decisions(request.payload).pipe(Effect.mapError(generateError)) }
|
|
54
|
+
},
|
|
55
|
+
Effect.provide(services),
|
|
56
|
+
locationErrors,
|
|
57
|
+
)
|
|
58
|
+
const videoCreate = Effect.fn("server.generate.video.create")(
|
|
59
|
+
function* (request: { readonly payload: VideoRequest }) {
|
|
60
|
+
return {
|
|
61
|
+
data: yield* videos.start(Generate.videoInput(request.payload)).pipe(Effect.mapError(generateError)),
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
Effect.provide(services),
|
|
65
|
+
locationErrors,
|
|
66
|
+
)
|
|
67
|
+
const videoGet = Effect.fn("server.generate.video.get")(
|
|
68
|
+
function* (request: { readonly params: { readonly id: VideoID } }) {
|
|
69
|
+
const job = yield* videos
|
|
70
|
+
.get(request.params.id)
|
|
71
|
+
.pipe(Effect.mapError((error) => new VideoNotFoundError({ videoID: error.id, message: error.message })))
|
|
72
|
+
return { data: job }
|
|
73
|
+
},
|
|
74
|
+
Effect.provide(services),
|
|
75
|
+
locationErrors,
|
|
76
|
+
)
|
|
77
|
+
return handlers
|
|
78
|
+
.handle("generate.text", text)
|
|
79
|
+
.handle("generate.textLegacy", text)
|
|
80
|
+
.handle("generate.image", image)
|
|
81
|
+
.handle("generate.decisions", decisions)
|
|
82
|
+
.handle("generate.video.create", videoCreate)
|
|
83
|
+
.handle("generate.video.get", videoGet)
|
|
84
|
+
}),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
function generateError(error: Generate.Error) {
|
|
88
|
+
switch (error._tag) {
|
|
89
|
+
case "Generate.ModelNotFoundError":
|
|
90
|
+
return new ModelNotFoundError({ providerID: error.providerID, modelID: error.modelID, message: error.message })
|
|
91
|
+
case "Generate.UnsupportedModelError":
|
|
92
|
+
return new UnsupportedModelError({
|
|
93
|
+
providerID: error.providerID,
|
|
94
|
+
modelID: error.modelID,
|
|
95
|
+
capability: error.capability,
|
|
96
|
+
message: error.message,
|
|
97
|
+
})
|
|
98
|
+
case "Generate.InvalidRequestError":
|
|
99
|
+
return new InvalidRequestError({
|
|
100
|
+
message: error.message,
|
|
101
|
+
...(error.field === undefined ? {} : { field: error.field }),
|
|
102
|
+
})
|
|
103
|
+
case "Generate.ProviderRequestError":
|
|
104
|
+
return new ProviderRequestError({
|
|
105
|
+
providerID: error.providerID,
|
|
106
|
+
reason: error.reason,
|
|
107
|
+
message: error.message,
|
|
108
|
+
...(error.status === undefined ? {} : { status: error.status }),
|
|
109
|
+
...(error.retryAfterMs === undefined ? {} : { retryAfterMs: error.retryAfterMs }),
|
|
110
|
+
})
|
|
111
|
+
case "Generate.UnavailableError":
|
|
112
|
+
return new ServiceUnavailableError({
|
|
113
|
+
message: error.message,
|
|
114
|
+
...(error.service === undefined ? {} : { service: error.service }),
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
}
|