@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
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import { expect } from "bun:test"
|
|
2
|
-
import { isConflictError, isForbiddenError, isInvalidRequestError, OpenCode } from "@zyro-sec/client"
|
|
3
|
-
import { Identity } from "@zyro-sec/core/machine/identity"
|
|
4
|
-
import { Machine } from "@zyro-sec/core/machine/index"
|
|
5
|
-
import { Iroh } from "@zyro-sec/core/machine/iroh"
|
|
6
|
-
import loadBinding from "@zyro-sec/core/machine/iroh-binding"
|
|
7
|
-
import { Join } from "@zyro-sec/core/machine/join"
|
|
8
|
-
import { Link } from "@zyro-sec/core/machine/link"
|
|
9
|
-
import { MACHINE_LINK_PATH } from "@zyro-sec/protocol/groups/machine"
|
|
10
|
-
import { Cause, Effect, Option, Queue } from "effect"
|
|
11
|
-
import { HttpServer } from "effect/http"
|
|
12
|
-
import { randomBytes } from "node:crypto"
|
|
13
|
-
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
14
|
-
import { it } from "../../core/test/lib/effect"
|
|
15
|
-
import { ServerProcess } from "../src/process"
|
|
16
|
-
|
|
17
|
-
const authorization = `Basic ${btoa("opencode:secret")}`
|
|
18
|
-
|
|
19
|
-
const start = Effect.fnUntraced(function* (machines: boolean) {
|
|
20
|
-
const directory = (yield* tmpdirScoped("opencode-machine-routes-")).path
|
|
21
|
-
const started = yield* ServerProcess.start<never, never>({
|
|
22
|
-
password: "secret",
|
|
23
|
-
app: { version: "test-version" },
|
|
24
|
-
database: { path: ":memory:" },
|
|
25
|
-
models: { fetch: false },
|
|
26
|
-
config: { directory, project: false, content: JSON.stringify({ experimental: { machines } }) },
|
|
27
|
-
fs: { filewatcher: false },
|
|
28
|
-
hostname: "127.0.0.1",
|
|
29
|
-
port: 0,
|
|
30
|
-
}).pipe(Effect.provideService(Iroh.Testing, { offline: true, load: loadBinding, watch: () => {} }))
|
|
31
|
-
const base = HttpServer.formatAddress(started.address)
|
|
32
|
-
const link = new URL(MACHINE_LINK_PATH, base)
|
|
33
|
-
link.protocol = "ws:"
|
|
34
|
-
return {
|
|
35
|
-
base,
|
|
36
|
-
link,
|
|
37
|
-
api: OpenCode.make({ baseUrl: base, headers: { authorization } }),
|
|
38
|
-
}
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
const dial = Effect.fnUntraced(function* (url: URL) {
|
|
42
|
-
const messages = yield* Queue.unbounded<Uint8Array, Cause.Done>()
|
|
43
|
-
const opened = Promise.withResolvers<boolean>()
|
|
44
|
-
const socket = yield* Effect.acquireRelease(
|
|
45
|
-
Effect.sync(() => {
|
|
46
|
-
const socket = Object.assign(new WebSocket(url), { binaryType: "arraybuffer" })
|
|
47
|
-
socket.addEventListener("open", () => opened.resolve(true))
|
|
48
|
-
socket.addEventListener("message", (event) => {
|
|
49
|
-
if (event.data instanceof ArrayBuffer) Queue.offerUnsafe(messages, new Uint8Array(event.data))
|
|
50
|
-
})
|
|
51
|
-
socket.addEventListener("close", () => {
|
|
52
|
-
Queue.endUnsafe(messages)
|
|
53
|
-
opened.resolve(false)
|
|
54
|
-
})
|
|
55
|
-
return socket
|
|
56
|
-
}),
|
|
57
|
-
(socket) => Effect.sync(() => socket.close()),
|
|
58
|
-
)
|
|
59
|
-
expect(yield* Effect.promise(() => opened.promise)).toBe(true)
|
|
60
|
-
return {
|
|
61
|
-
receive: Queue.take(messages).pipe(Effect.mapError(() => new Link.Closed())),
|
|
62
|
-
send: (message: Uint8Array) => Effect.sync(() => socket.send(new Uint8Array(message))),
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
it.live("adds, invites, lists, protects paired machines, and removes machines", () =>
|
|
67
|
-
Effect.gen(function* () {
|
|
68
|
-
const server = yield* start(true)
|
|
69
|
-
const reach = [
|
|
70
|
-
{ type: "url" as const, url: "wss://example.com/api/experimental/machine/link" },
|
|
71
|
-
{ type: "url" as const, url: server.link.href },
|
|
72
|
-
]
|
|
73
|
-
|
|
74
|
-
// Every management route requires the server password.
|
|
75
|
-
const responses = yield* Effect.promise(() =>
|
|
76
|
-
Promise.all([
|
|
77
|
-
fetch(`${server.base}/api/experimental/machine`),
|
|
78
|
-
fetch(`${server.base}/api/experimental/machine`, {
|
|
79
|
-
method: "POST",
|
|
80
|
-
headers: { "content-type": "application/json" },
|
|
81
|
-
body: JSON.stringify({ name: "gpu", reach }),
|
|
82
|
-
}),
|
|
83
|
-
fetch(`${server.base}/api/experimental/machine/gpu`, { method: "DELETE" }),
|
|
84
|
-
]),
|
|
85
|
-
)
|
|
86
|
-
expect(responses.map((response) => response.status)).toEqual([401, 401, 401])
|
|
87
|
-
|
|
88
|
-
expect(yield* Effect.promise(() => server.api.experimental.machine.list())).toEqual([])
|
|
89
|
-
|
|
90
|
-
// Invalid names return a 400 InvalidRequestError.
|
|
91
|
-
const invalid = yield* Effect.promise(() =>
|
|
92
|
-
server.api.experimental.machine.add({ name: "local", reach }).then(
|
|
93
|
-
() => undefined,
|
|
94
|
-
(error) => error,
|
|
95
|
-
),
|
|
96
|
-
)
|
|
97
|
-
expect(isInvalidRequestError(invalid)).toBe(true)
|
|
98
|
-
expect(invalid).toMatchObject({ _tag: "InvalidRequestError", kind: "invalid_name", field: "name" })
|
|
99
|
-
|
|
100
|
-
// Adding creates the machine and returns a join string whose expiry and reach match Join.decode.
|
|
101
|
-
const first = yield* Effect.promise(() => server.api.experimental.machine.add({ name: "gpu", reach }))
|
|
102
|
-
const decoded = Option.getOrThrow(Join.decode(first.join))
|
|
103
|
-
expect(first.name).toBe("gpu")
|
|
104
|
-
expect(first.expires).toBe(decoded.expires)
|
|
105
|
-
// The home's Iroh endpoint is bound by the first join string, which puts it ahead of the URLs.
|
|
106
|
-
expect(decoded.reach).toEqual([{ type: "iroh" }, ...reach])
|
|
107
|
-
expect(first.reach).toEqual(decoded.reach)
|
|
108
|
-
expect(yield* Effect.promise(() => server.api.experimental.machine.list())).toEqual([
|
|
109
|
-
{ id: "gpu", online: false, paired: false },
|
|
110
|
-
])
|
|
111
|
-
|
|
112
|
-
// Adding an unpaired machine again issues a fresh join string.
|
|
113
|
-
const second = yield* Effect.promise(() => server.api.experimental.machine.add({ name: "gpu", reach }))
|
|
114
|
-
expect(second.join).not.toBe(first.join)
|
|
115
|
-
const latest = Option.getOrThrow(Join.decode(second.join))
|
|
116
|
-
|
|
117
|
-
// Pair the machine over the real WebSocket link.
|
|
118
|
-
yield* Effect.scoped(
|
|
119
|
-
Effect.gen(function* () {
|
|
120
|
-
const socket = yield* dial(server.link)
|
|
121
|
-
const linked = yield* Link.connect(socket, {
|
|
122
|
-
home: latest.home,
|
|
123
|
-
identity: Identity.fromSeed(randomBytes(32)),
|
|
124
|
-
secret: latest.secret,
|
|
125
|
-
})
|
|
126
|
-
linked.send({
|
|
127
|
-
type: "hello",
|
|
128
|
-
protocol: 2,
|
|
129
|
-
os: "linux",
|
|
130
|
-
arch: "x64",
|
|
131
|
-
home: "/home/worker",
|
|
132
|
-
instance: Machine.Instance.make(crypto.randomUUID()),
|
|
133
|
-
processes: [],
|
|
134
|
-
})
|
|
135
|
-
yield* Effect.gen(function* () {
|
|
136
|
-
while (true) {
|
|
137
|
-
const listed = yield* Effect.promise(() => server.api.experimental.machine.list())
|
|
138
|
-
if (listed[0].online) {
|
|
139
|
-
expect(listed[0].connection).toEqual({ kind: "websocket" })
|
|
140
|
-
return
|
|
141
|
-
}
|
|
142
|
-
yield* Effect.sleep("10 millis")
|
|
143
|
-
}
|
|
144
|
-
}).pipe(Effect.timeout("3 seconds"))
|
|
145
|
-
}),
|
|
146
|
-
)
|
|
147
|
-
|
|
148
|
-
// Once paired, add without replace returns 409 ConflictError; replace: true issues a new invite.
|
|
149
|
-
const conflict = yield* Effect.promise(() =>
|
|
150
|
-
server.api.experimental.machine.add({ name: "gpu", reach }).then(
|
|
151
|
-
() => undefined,
|
|
152
|
-
(error) => error,
|
|
153
|
-
),
|
|
154
|
-
)
|
|
155
|
-
expect(isConflictError(conflict)).toBe(true)
|
|
156
|
-
expect(conflict).toMatchObject({ _tag: "ConflictError", resource: "gpu" })
|
|
157
|
-
|
|
158
|
-
const replaced = yield* Effect.promise(() =>
|
|
159
|
-
server.api.experimental.machine.add({ name: "gpu", reach, replace: true }),
|
|
160
|
-
)
|
|
161
|
-
expect(replaced.join).not.toBe(second.join)
|
|
162
|
-
expect(Option.isSome(Join.decode(replaced.join))).toBe(true)
|
|
163
|
-
|
|
164
|
-
// Removing deletes the machine; removing it again, or any unknown name, is 404 MachineNotFoundError.
|
|
165
|
-
yield* Effect.promise(() => server.api.experimental.machine.remove({ name: "gpu" }))
|
|
166
|
-
expect(yield* Effect.promise(() => server.api.experimental.machine.list())).toEqual([])
|
|
167
|
-
for (const name of ["gpu", "unknown"]) {
|
|
168
|
-
const missing = yield* Effect.promise(() =>
|
|
169
|
-
server.api.experimental.machine.remove({ name }).then(
|
|
170
|
-
() => undefined,
|
|
171
|
-
(error) => error,
|
|
172
|
-
),
|
|
173
|
-
)
|
|
174
|
-
expect(missing).toMatchObject({ _tag: "MachineNotFoundError", machine: name })
|
|
175
|
-
}
|
|
176
|
-
}),
|
|
177
|
-
)
|
|
178
|
-
|
|
179
|
-
it.live("rejects every machine management route with 403 ForbiddenError while machines are off", () =>
|
|
180
|
-
Effect.gen(function* () {
|
|
181
|
-
const server = yield* start(false)
|
|
182
|
-
const results = yield* Effect.promise(() =>
|
|
183
|
-
Promise.all(
|
|
184
|
-
[
|
|
185
|
-
server.api.experimental.machine.list(),
|
|
186
|
-
server.api.experimental.machine.add({ name: "gpu", reach: [] }),
|
|
187
|
-
server.api.experimental.machine.remove({ name: "gpu" }),
|
|
188
|
-
].map((call) =>
|
|
189
|
-
call.then(
|
|
190
|
-
() => undefined,
|
|
191
|
-
(error) => error,
|
|
192
|
-
),
|
|
193
|
-
),
|
|
194
|
-
),
|
|
195
|
-
)
|
|
196
|
-
expect(results.map(isForbiddenError)).toEqual([true, true, true])
|
|
197
|
-
}),
|
|
198
|
-
)
|
package/test/media.test.ts
DELETED
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
import { Database as Sqlite } from "bun:sqlite"
|
|
2
|
-
import { createHash } from "node:crypto"
|
|
3
|
-
import fs from "node:fs/promises"
|
|
4
|
-
import path from "node:path"
|
|
5
|
-
import { expect } from "bun:test"
|
|
6
|
-
import { OpenCode, type SessionMessageInfo } from "@zyro-sec/client"
|
|
7
|
-
import { Session } from "@zyro-sec/schema/session"
|
|
8
|
-
import { Effect } from "effect"
|
|
9
|
-
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
10
|
-
import { it } from "../../core/test/lib/effect"
|
|
11
|
-
import { ContentHeaders } from "../src/content-headers"
|
|
12
|
-
import { ServerFetch } from "../src/fetch"
|
|
13
|
-
|
|
14
|
-
const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
|
|
15
|
-
const text = Buffer.from("workspace file contents").toString("base64")
|
|
16
|
-
const svg = Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" onload="alert(1)"/>').toString("base64")
|
|
17
|
-
const html = Buffer.from("<!doctype html><script>alert(1)</script>").toString("base64")
|
|
18
|
-
const hash = (base64: string) => createHash("sha256").update(Buffer.from(base64, "base64")).digest("hex")
|
|
19
|
-
const size = (base64: string) => Buffer.from(base64, "base64").byteLength
|
|
20
|
-
|
|
21
|
-
const user: SessionMessageInfo = {
|
|
22
|
-
id: "msg_media_user",
|
|
23
|
-
type: "user",
|
|
24
|
-
text: "See attached",
|
|
25
|
-
files: [
|
|
26
|
-
{ data: png, mime: "image/png", source: { type: "inline" }, name: "shot.png" },
|
|
27
|
-
{
|
|
28
|
-
data: text,
|
|
29
|
-
mime: "text/plain",
|
|
30
|
-
source: { type: "uri", uri: "file:///workspace/readme.txt" },
|
|
31
|
-
name: "readme.txt",
|
|
32
|
-
},
|
|
33
|
-
{ data: svg, mime: "image/svg+xml", source: { type: "inline" }, name: "xss.svg" },
|
|
34
|
-
{ data: html, mime: "text/html", source: { type: "inline" }, name: "xss.html" },
|
|
35
|
-
],
|
|
36
|
-
time: { created: 100 },
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const assistant: SessionMessageInfo = {
|
|
40
|
-
id: "msg_media_assistant",
|
|
41
|
-
type: "assistant",
|
|
42
|
-
agent: "build",
|
|
43
|
-
model: { providerID: "test", id: "test" },
|
|
44
|
-
content: [
|
|
45
|
-
{
|
|
46
|
-
type: "tool",
|
|
47
|
-
id: "call_read_img",
|
|
48
|
-
name: "read",
|
|
49
|
-
executed: true,
|
|
50
|
-
state: {
|
|
51
|
-
status: "completed",
|
|
52
|
-
input: { path: "shot.png" },
|
|
53
|
-
content: [{ type: "file", uri: `data:image/png;base64,${png}`, mime: "image/png", name: "shot.png" }],
|
|
54
|
-
},
|
|
55
|
-
time: { created: 110, ran: 111, completed: 112 },
|
|
56
|
-
},
|
|
57
|
-
],
|
|
58
|
-
finish: "stop",
|
|
59
|
-
time: { created: 110, completed: 120 },
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const serve = (database: string, mediaBlob: boolean) =>
|
|
63
|
-
ServerFetch.make({
|
|
64
|
-
app: { version: "test" },
|
|
65
|
-
database: { path: database },
|
|
66
|
-
// Its own global config directory, so the developer's global config can't add remote MCP servers.
|
|
67
|
-
config: {
|
|
68
|
-
directory: path.dirname(database),
|
|
69
|
-
project: false,
|
|
70
|
-
content: JSON.stringify(mediaBlob ? { experimental: { media_blob: true } } : {}),
|
|
71
|
-
},
|
|
72
|
-
models: { fetch: false },
|
|
73
|
-
fs: { filewatcher: false },
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
const client = (handler: (request: Request) => Promise<Response>, capabilities?: string) =>
|
|
77
|
-
OpenCode.make({
|
|
78
|
-
baseUrl: "http://opencode.local",
|
|
79
|
-
...(capabilities === undefined ? {} : { headers: { "X-OpenCode-Capabilities": capabilities } }),
|
|
80
|
-
fetch: Object.assign((input: string | URL | Request, init?: RequestInit) => handler(new Request(input, init)), {
|
|
81
|
-
preconnect: fetch.preconnect,
|
|
82
|
-
}),
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
const media = (base64: string) => `media:${hash(base64)}`
|
|
86
|
-
const route = (base64: string) => `http://opencode.local/api/experimental/media/${hash(base64)}`
|
|
87
|
-
|
|
88
|
-
it.live("serves stored media as refs only to clients that opt in while experimental.media_blob is on", () =>
|
|
89
|
-
Effect.gen(function* () {
|
|
90
|
-
// Acquired first so it is removed after the servers below are released; Windows can't delete an open db.
|
|
91
|
-
const dir = (yield* tmpdirScoped("opencode-media-wire-")).path
|
|
92
|
-
yield* Effect.gen(function* () {
|
|
93
|
-
const database = path.join(dir, "opencode.db")
|
|
94
|
-
const handler = yield* serve(database, true)
|
|
95
|
-
const plain = client(handler)
|
|
96
|
-
const opted = client(handler, "other, Experimental-Media-URI")
|
|
97
|
-
const sessionID = Session.ID.create()
|
|
98
|
-
|
|
99
|
-
yield* Effect.promise(async () => {
|
|
100
|
-
const template = await plain.session.create({ title: "Media wire" })
|
|
101
|
-
// With the flag on, import stores the media as blobs.
|
|
102
|
-
await plain.experimental.session.import({ info: { ...template, id: sessionID }, messages: [user, assistant] })
|
|
103
|
-
|
|
104
|
-
// Without the capability, reads stay byte-for-byte what was imported.
|
|
105
|
-
expect((await plain.message.list({ sessionID, order: "asc" })).data).toEqual([user, assistant])
|
|
106
|
-
|
|
107
|
-
const refs: unknown = (await opted.message.list({ sessionID, order: "asc" })).data
|
|
108
|
-
expect(refs).toEqual([
|
|
109
|
-
{
|
|
110
|
-
...user,
|
|
111
|
-
files: [
|
|
112
|
-
{
|
|
113
|
-
uri: media(png),
|
|
114
|
-
mime: "image/png",
|
|
115
|
-
source: { type: "inline" },
|
|
116
|
-
name: "shot.png",
|
|
117
|
-
bytes: size(png),
|
|
118
|
-
},
|
|
119
|
-
{
|
|
120
|
-
uri: media(text),
|
|
121
|
-
mime: "text/plain",
|
|
122
|
-
source: { type: "uri", uri: "file:///workspace/readme.txt" },
|
|
123
|
-
name: "readme.txt",
|
|
124
|
-
bytes: size(text),
|
|
125
|
-
},
|
|
126
|
-
{
|
|
127
|
-
uri: media(svg),
|
|
128
|
-
mime: "image/svg+xml",
|
|
129
|
-
source: { type: "inline" },
|
|
130
|
-
name: "xss.svg",
|
|
131
|
-
bytes: size(svg),
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
uri: media(html),
|
|
135
|
-
mime: "text/html",
|
|
136
|
-
source: { type: "inline" },
|
|
137
|
-
name: "xss.html",
|
|
138
|
-
bytes: size(html),
|
|
139
|
-
},
|
|
140
|
-
],
|
|
141
|
-
},
|
|
142
|
-
{
|
|
143
|
-
...assistant,
|
|
144
|
-
content: [
|
|
145
|
-
{
|
|
146
|
-
type: "tool",
|
|
147
|
-
id: "call_read_img",
|
|
148
|
-
name: "read",
|
|
149
|
-
executed: true,
|
|
150
|
-
state: {
|
|
151
|
-
status: "completed",
|
|
152
|
-
input: { path: "shot.png" },
|
|
153
|
-
content: [{ type: "file", uri: media(png), mime: "image/png", name: "shot.png" }],
|
|
154
|
-
},
|
|
155
|
-
time: { created: 110, ran: 111, completed: 112 },
|
|
156
|
-
},
|
|
157
|
-
],
|
|
158
|
-
},
|
|
159
|
-
])
|
|
160
|
-
const single: unknown = await opted.session.message.get({ sessionID, messageID: "msg_media_user" })
|
|
161
|
-
expect(single).toMatchObject({ files: [{ uri: media(png) }, { uri: media(text) }, {}, {}] })
|
|
162
|
-
expect(await plain.session.message.get({ sessionID, messageID: "msg_media_user" })).toEqual(user)
|
|
163
|
-
expect((await opted.experimental.session.export({ sessionID })).messages).toEqual([user, assistant])
|
|
164
|
-
|
|
165
|
-
// The route serves raw bytes as downloads, with immutable caching and a 304; image types stay usable by <img>.
|
|
166
|
-
const image = await handler(new Request(route(png)))
|
|
167
|
-
expect(image.status).toBe(200)
|
|
168
|
-
expect(image.headers.get("content-type")).toBe("image/png")
|
|
169
|
-
expect(image.headers.get("content-disposition")).toBe("attachment")
|
|
170
|
-
expect(image.headers.get("cache-control")).toBe("private, max-age=31536000, immutable")
|
|
171
|
-
expect(image.headers.get("etag")).toBe(`"${hash(png)}"`)
|
|
172
|
-
expect(image.headers.get("x-content-type-options")).toBe("nosniff")
|
|
173
|
-
expect(image.headers.get("content-security-policy")).toBe(ContentHeaders.CONTENT_SECURITY_POLICY)
|
|
174
|
-
expect(Buffer.from(await image.arrayBuffer()).toString("base64")).toBe(png)
|
|
175
|
-
for (const unsafe of [svg, html]) {
|
|
176
|
-
const response = await handler(new Request(route(unsafe)))
|
|
177
|
-
expect(response.headers.get("content-type")).toBe("application/octet-stream")
|
|
178
|
-
expect(response.headers.get("content-disposition")).toBe("attachment")
|
|
179
|
-
expect(response.headers.get("content-security-policy")).toBe(ContentHeaders.CONTENT_SECURITY_POLICY)
|
|
180
|
-
expect(Buffer.from(await response.arrayBuffer()).toString("base64")).toBe(unsafe)
|
|
181
|
-
}
|
|
182
|
-
const cached = await handler(new Request(route(png), { headers: { "If-None-Match": `W/"${hash(png)}"` } }))
|
|
183
|
-
expect(cached.status).toBe(304)
|
|
184
|
-
expect(cached.headers.get("etag")).toBe(`"${hash(png)}"`)
|
|
185
|
-
expect((await cached.arrayBuffer()).byteLength).toBe(0)
|
|
186
|
-
for (const invalid of ["deadbeef", "A".repeat(64), "g".repeat(64)])
|
|
187
|
-
expect((await handler(new Request(`http://opencode.local/api/experimental/media/${invalid}`))).status).toBe(
|
|
188
|
-
400,
|
|
189
|
-
)
|
|
190
|
-
expect(
|
|
191
|
-
(await handler(new Request(`http://opencode.local/api/experimental/media/${"0".repeat(64)}`))).status,
|
|
192
|
-
).toBe(404)
|
|
193
|
-
expect(Buffer.from(await opted.experimental.media.get({ hash: hash(png) })).toString("base64")).toBe(png)
|
|
194
|
-
|
|
195
|
-
// A `media:` ref a client received can be sent back as an attachment; pending input keeps it a ref.
|
|
196
|
-
const queued = await opted.session.prompt({
|
|
197
|
-
sessionID,
|
|
198
|
-
text: "Again",
|
|
199
|
-
files: [{ uri: media(png), name: "again.png" }],
|
|
200
|
-
delivery: "queue",
|
|
201
|
-
resume: false,
|
|
202
|
-
})
|
|
203
|
-
const pending: unknown = await opted.session.inbox.list({ sessionID })
|
|
204
|
-
expect(pending).toMatchObject([
|
|
205
|
-
{
|
|
206
|
-
id: queued.id,
|
|
207
|
-
payload: {
|
|
208
|
-
files: [{ uri: media(png), mime: "image/png", name: "again.png", bytes: size(png) }],
|
|
209
|
-
},
|
|
210
|
-
},
|
|
211
|
-
])
|
|
212
|
-
expect(await plain.session.inbox.list({ sessionID })).toMatchObject([
|
|
213
|
-
{ id: queued.id, payload: { files: [{ data: png, mime: "image/png", name: "again.png" }] } },
|
|
214
|
-
])
|
|
215
|
-
await opted.session.inbox.cancel({ sessionID, inboxID: queued.id })
|
|
216
|
-
|
|
217
|
-
// A ref whose blob is gone reaches opted-in clients as the same placeholder inline readers get.
|
|
218
|
-
const gone = "e".repeat(64)
|
|
219
|
-
const orphan = "d".repeat(64)
|
|
220
|
-
const db = new Sqlite(database)
|
|
221
|
-
db.run("INSERT INTO blob (hash, mime, bytes, data, time_created) VALUES (?, 'image/png', 3, ?, 1)", [
|
|
222
|
-
orphan,
|
|
223
|
-
Buffer.from("abc"),
|
|
224
|
-
])
|
|
225
|
-
db.run(
|
|
226
|
-
"INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES (?, ?, 'user', 1001, 1001, 1001, ?)",
|
|
227
|
-
[
|
|
228
|
-
"msg_orphan_media",
|
|
229
|
-
sessionID,
|
|
230
|
-
JSON.stringify({
|
|
231
|
-
text: "orphan",
|
|
232
|
-
files: [{ uri: `media:${orphan}`, mime: "image/png", name: "gone.png", source: { type: "inline" } }],
|
|
233
|
-
time: { created: 1001 },
|
|
234
|
-
}),
|
|
235
|
-
],
|
|
236
|
-
)
|
|
237
|
-
db.run(
|
|
238
|
-
"INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES (?, ?, 'user', 1000, 1000, 1000, ?)",
|
|
239
|
-
[
|
|
240
|
-
"msg_gone_media",
|
|
241
|
-
sessionID,
|
|
242
|
-
JSON.stringify({
|
|
243
|
-
text: "gone",
|
|
244
|
-
files: [{ uri: `media:${gone}`, mime: "image/png", name: "gone.png", source: { type: "inline" } }],
|
|
245
|
-
time: { created: 1000 },
|
|
246
|
-
}),
|
|
247
|
-
],
|
|
248
|
-
)
|
|
249
|
-
db.close()
|
|
250
|
-
const placeholder = {
|
|
251
|
-
data: Buffer.from("[media unavailable: gone.png (image/png)]").toString("base64"),
|
|
252
|
-
mime: "text/plain",
|
|
253
|
-
source: { type: "inline" },
|
|
254
|
-
name: "gone.png",
|
|
255
|
-
}
|
|
256
|
-
expect(await opted.session.message.get({ sessionID, messageID: "msg_gone_media" })).toMatchObject({
|
|
257
|
-
files: [placeholder],
|
|
258
|
-
})
|
|
259
|
-
expect(await plain.session.message.get({ sessionID, messageID: "msg_gone_media" })).toMatchObject({
|
|
260
|
-
files: [placeholder],
|
|
261
|
-
})
|
|
262
|
-
// A stored blob nothing references is not served by the route, so it is not offered as a ref either.
|
|
263
|
-
expect((await handler(new Request(route(orphan)))).status).toBe(404)
|
|
264
|
-
expect(await opted.session.message.get({ sessionID, messageID: "msg_orphan_media" })).toMatchObject({
|
|
265
|
-
files: [placeholder],
|
|
266
|
-
})
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
// Another server over the same database without the flag: opted-in clients get bytes, and the route still
|
|
270
|
-
// serves what stored messages reference.
|
|
271
|
-
const disabled = yield* serve(database, false)
|
|
272
|
-
yield* Effect.promise(async () => {
|
|
273
|
-
expect((await disabled(new Request(route(png)))).status).toBe(200)
|
|
274
|
-
expect(
|
|
275
|
-
(await client(disabled, "experimental-media-uri").message.list({ sessionID, order: "asc" })).data.slice(0, 2),
|
|
276
|
-
).toEqual([user, assistant])
|
|
277
|
-
expect((await handler(new Request(route(png)))).status).toBe(200)
|
|
278
|
-
})
|
|
279
|
-
})
|
|
280
|
-
}).pipe(Effect.scoped),
|
|
281
|
-
)
|
|
282
|
-
|
|
283
|
-
it.live("follows experimental.media_blob from the session's project config and serves only referenced blobs", () =>
|
|
284
|
-
Effect.gen(function* () {
|
|
285
|
-
const dir = (yield* tmpdirScoped("opencode-media-project-")).path
|
|
286
|
-
yield* Effect.gen(function* () {
|
|
287
|
-
const database = path.join(dir, "opencode.db")
|
|
288
|
-
const global = path.join(dir, "global")
|
|
289
|
-
const project = path.join(dir, "project")
|
|
290
|
-
yield* Effect.promise(async () => {
|
|
291
|
-
await fs.mkdir(global, { recursive: true })
|
|
292
|
-
await fs.mkdir(project, { recursive: true })
|
|
293
|
-
await fs.writeFile(path.join(project, "opencode.json"), JSON.stringify({ experimental: { media_blob: true } }))
|
|
294
|
-
})
|
|
295
|
-
// The flag lives only in the project's config; the server's cwd and global config leave it off.
|
|
296
|
-
const handler = yield* ServerFetch.make({
|
|
297
|
-
app: { version: "test" },
|
|
298
|
-
database: { path: database },
|
|
299
|
-
config: { directory: global, project: true, claude: false },
|
|
300
|
-
models: { fetch: false },
|
|
301
|
-
fs: { filewatcher: false },
|
|
302
|
-
})
|
|
303
|
-
const plain = client(handler)
|
|
304
|
-
const opted = client(handler, "experimental-media-uri")
|
|
305
|
-
|
|
306
|
-
yield* Effect.promise(async () => {
|
|
307
|
-
const session = await plain.session.create({ location: { directory: project } })
|
|
308
|
-
const queued = await opted.session.prompt({
|
|
309
|
-
sessionID: session.id,
|
|
310
|
-
text: "Look",
|
|
311
|
-
files: [{ uri: `data:image/png;base64,${png}`, name: "shot.png" }],
|
|
312
|
-
delivery: "queue",
|
|
313
|
-
resume: false,
|
|
314
|
-
})
|
|
315
|
-
expect(await opted.session.inbox.list({ sessionID: session.id })).toMatchObject([
|
|
316
|
-
{ id: queued.id, payload: { files: [{ uri: media(png), mime: "image/png", name: "shot.png" }] } },
|
|
317
|
-
])
|
|
318
|
-
expect(await plain.session.inbox.list({ sessionID: session.id })).toMatchObject([
|
|
319
|
-
{ id: queued.id, payload: { files: [{ data: png, mime: "image/png", name: "shot.png" }] } },
|
|
320
|
-
])
|
|
321
|
-
expect((await handler(new Request(route(png)))).status).toBe(200)
|
|
322
|
-
|
|
323
|
-
// A blob no message or inbox item references is unreadable, by route or as a prompt attachment.
|
|
324
|
-
const db = new Sqlite(database)
|
|
325
|
-
db.run("INSERT INTO blob (hash, mime, bytes, data, time_created) VALUES (?, 'text/plain', ?, ?, 1)", [
|
|
326
|
-
hash(text),
|
|
327
|
-
size(text),
|
|
328
|
-
Buffer.from(text, "base64"),
|
|
329
|
-
])
|
|
330
|
-
db.close()
|
|
331
|
-
expect((await handler(new Request(route(text)))).status).toBe(404)
|
|
332
|
-
const attached = await opted.session
|
|
333
|
-
.prompt({ sessionID: session.id, text: "Steal", files: [{ uri: media(text) }], resume: false })
|
|
334
|
-
.then(
|
|
335
|
-
() => undefined,
|
|
336
|
-
(error: unknown) => error,
|
|
337
|
-
)
|
|
338
|
-
expect(attached).toBeDefined()
|
|
339
|
-
expect(await plain.session.inbox.list({ sessionID: session.id })).toHaveLength(1)
|
|
340
|
-
})
|
|
341
|
-
})
|
|
342
|
-
}).pipe(Effect.scoped),
|
|
343
|
-
)
|
package/test/message.test.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { expect } from "bun:test"
|
|
2
|
-
import { OpenCode, type SessionMessageInfo } from "@zyro-sec/client"
|
|
3
|
-
import { Session } from "@zyro-sec/schema/session"
|
|
4
|
-
import { Effect } from "effect"
|
|
5
|
-
import { it } from "../../core/test/lib/effect"
|
|
6
|
-
import { ServerFetch } from "../src/fetch"
|
|
7
|
-
|
|
8
|
-
const messages: SessionMessageInfo[] = [
|
|
9
|
-
{ id: "msg_z", type: "user", text: "First request", time: { created: 300 } },
|
|
10
|
-
{
|
|
11
|
-
id: "msg_assistant",
|
|
12
|
-
type: "assistant",
|
|
13
|
-
agent: "build",
|
|
14
|
-
model: { providerID: "test", id: "test" },
|
|
15
|
-
content: [{ type: "text", text: "First answer" }],
|
|
16
|
-
finish: "stop",
|
|
17
|
-
time: { created: 400, completed: 500 },
|
|
18
|
-
},
|
|
19
|
-
{ id: "msg_b", type: "user", text: "Second request", time: { created: 100 } },
|
|
20
|
-
{ id: "msg_synthetic", type: "synthetic", text: "Background completion", time: { created: 200 } },
|
|
21
|
-
{
|
|
22
|
-
id: "msg_compaction",
|
|
23
|
-
type: "compaction",
|
|
24
|
-
status: "completed",
|
|
25
|
-
reason: "manual",
|
|
26
|
-
summary: "Conversation summary",
|
|
27
|
-
time: { created: 700 },
|
|
28
|
-
},
|
|
29
|
-
{ id: "msg_x", type: "user", text: "Third request", time: { created: 600 } },
|
|
30
|
-
{ id: "msg_system", type: "system", text: "Updated instructions", time: { created: 800 } },
|
|
31
|
-
{ id: "msg_a", type: "user", text: "Fourth request", time: { created: 500 } },
|
|
32
|
-
{ id: "msg_idle", type: "idle", outcome: "succeeded", time: { created: 900 } },
|
|
33
|
-
]
|
|
34
|
-
|
|
35
|
-
const setup = Effect.gen(function* () {
|
|
36
|
-
const handler = yield* ServerFetch.make({
|
|
37
|
-
app: { version: "test" },
|
|
38
|
-
database: { path: ":memory:" },
|
|
39
|
-
config: { project: false },
|
|
40
|
-
models: { fetch: false },
|
|
41
|
-
fs: { filewatcher: false },
|
|
42
|
-
})
|
|
43
|
-
const api = OpenCode.make({
|
|
44
|
-
baseUrl: "http://opencode.local",
|
|
45
|
-
fetch: Object.assign((input: string | URL | Request, init?: RequestInit) => handler(new Request(input, init)), {
|
|
46
|
-
preconnect: fetch.preconnect,
|
|
47
|
-
}),
|
|
48
|
-
})
|
|
49
|
-
const session = yield* Effect.promise(async () => {
|
|
50
|
-
const template = await api.session.create({ title: "Message filtering" })
|
|
51
|
-
return api.experimental.session.import({ info: { ...template, id: Session.ID.create() }, messages })
|
|
52
|
-
})
|
|
53
|
-
return { api, handler, sessionID: session.id }
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it.live("filters message types before paginating in either direction through the generated client", () =>
|
|
57
|
-
Effect.gen(function* () {
|
|
58
|
-
const fixture = yield* setup
|
|
59
|
-
yield* Effect.promise(async () => {
|
|
60
|
-
const input = { sessionID: fixture.sessionID, type: "user", limit: 2 } as const
|
|
61
|
-
// Omission retains the full transcript, in durable sequence rather than timestamp or ID order.
|
|
62
|
-
expect(
|
|
63
|
-
(await fixture.api.message.list({ sessionID: fixture.sessionID })).data.map((message) => message.id),
|
|
64
|
-
).toEqual(messages.toReversed().map((message) => message.id))
|
|
65
|
-
for (const order of ["asc", "desc"] as const) {
|
|
66
|
-
const ids = order === "asc" ? ["msg_z", "msg_b", "msg_x", "msg_a"] : ["msg_a", "msg_x", "msg_b", "msg_z"]
|
|
67
|
-
const first = await fixture.api.message.list({ ...input, order })
|
|
68
|
-
expect(first.data.map((message) => message.id)).toEqual(ids.slice(0, 2))
|
|
69
|
-
if (!first.cursor.next) throw new Error("Expected a next cursor")
|
|
70
|
-
const second = await fixture.api.message.list({ ...input, cursor: first.cursor.next })
|
|
71
|
-
expect(second.data.map((message) => message.id)).toEqual(ids.slice(2))
|
|
72
|
-
if (!second.cursor.previous || !second.cursor.next) throw new Error("Expected previous and next cursors")
|
|
73
|
-
const previous = await fixture.api.message.list({ ...input, cursor: second.cursor.previous })
|
|
74
|
-
expect(previous.data).toEqual(first.data)
|
|
75
|
-
const end = await fixture.api.message.list({ ...input, cursor: second.cursor.next })
|
|
76
|
-
expect(end).toEqual({ data: [], cursor: { previous: null, next: null } })
|
|
77
|
-
}
|
|
78
|
-
expect((await fixture.api.message.list({ sessionID: fixture.sessionID, type: "compaction" })).data).toEqual([
|
|
79
|
-
messages[4],
|
|
80
|
-
])
|
|
81
|
-
expect(
|
|
82
|
-
(await fixture.api.message.list({ sessionID: fixture.sessionID, type: "assistant", limit: 1 })).data,
|
|
83
|
-
).toEqual([messages[1]])
|
|
84
|
-
expect((await fixture.api.message.list({ sessionID: fixture.sessionID, type: "idle" })).data).toEqual([
|
|
85
|
-
messages[8],
|
|
86
|
-
])
|
|
87
|
-
expect((await fixture.api.message.list({ sessionID: fixture.sessionID, type: "shell" })).data).toEqual([])
|
|
88
|
-
})
|
|
89
|
-
}),
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
it.live("rejects unknown message type filters at the HTTP boundary", () =>
|
|
93
|
-
Effect.gen(function* () {
|
|
94
|
-
const fixture = yield* setup
|
|
95
|
-
yield* Effect.promise(async () => {
|
|
96
|
-
for (const type of ["unknown", "tool", "User", ""]) {
|
|
97
|
-
const response = await fixture.handler(
|
|
98
|
-
new Request(`http://opencode.local/api/session/${fixture.sessionID}/message?type=${type}`),
|
|
99
|
-
)
|
|
100
|
-
expect(response.status).toBe(400)
|
|
101
|
-
expect(await response.json()).toMatchObject({ _tag: "InvalidRequestError" })
|
|
102
|
-
}
|
|
103
|
-
})
|
|
104
|
-
}),
|
|
105
|
-
)
|