@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,72 @@
|
|
|
1
|
+
import { Shell } from "@zyro-sec/core/shell"
|
|
2
|
+
import { Location } from "@zyro-sec/core/location"
|
|
3
|
+
import { Effect } from "effect"
|
|
4
|
+
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
|
|
5
|
+
import { ShellNotFoundError } from "@zyro-sec/protocol/errors"
|
|
6
|
+
import { Api } from "../api"
|
|
7
|
+
import { response } from "../location"
|
|
8
|
+
|
|
9
|
+
export const ShellHandler = HttpApiBuilder.group(Api, "server.shell", (handlers) =>
|
|
10
|
+
Effect.gen(function* () {
|
|
11
|
+
return handlers
|
|
12
|
+
.handle(
|
|
13
|
+
"shell.list",
|
|
14
|
+
Effect.fn(function* () {
|
|
15
|
+
const shell = yield* Shell.Service
|
|
16
|
+
return yield* response(shell.list())
|
|
17
|
+
}),
|
|
18
|
+
)
|
|
19
|
+
.handle(
|
|
20
|
+
"shell.create",
|
|
21
|
+
Effect.fn(function* (ctx) {
|
|
22
|
+
const shell = yield* Shell.Service
|
|
23
|
+
const location = yield* Location.Service
|
|
24
|
+
return yield* response(
|
|
25
|
+
shell.create({ ...ctx.payload, cwd: ctx.payload.cwd || location.directory }).pipe(Effect.orDie),
|
|
26
|
+
)
|
|
27
|
+
}),
|
|
28
|
+
)
|
|
29
|
+
.handle(
|
|
30
|
+
"shell.get",
|
|
31
|
+
Effect.fn(function* (ctx) {
|
|
32
|
+
const shell = yield* Shell.Service
|
|
33
|
+
return yield* response(
|
|
34
|
+
shell
|
|
35
|
+
.get(ctx.params.id)
|
|
36
|
+
.pipe(
|
|
37
|
+
Effect.catchTag(
|
|
38
|
+
"Shell.NotFoundError",
|
|
39
|
+
() =>
|
|
40
|
+
new ShellNotFoundError({ id: ctx.params.id, message: `Shell command not found: ${ctx.params.id}` }),
|
|
41
|
+
),
|
|
42
|
+
),
|
|
43
|
+
)
|
|
44
|
+
}),
|
|
45
|
+
)
|
|
46
|
+
.handle(
|
|
47
|
+
"shell.output",
|
|
48
|
+
Effect.fn(function* (ctx) {
|
|
49
|
+
const shell = yield* Shell.Service
|
|
50
|
+
return yield* response(
|
|
51
|
+
shell
|
|
52
|
+
.output(ctx.params.id, { cursor: ctx.query.cursor, limit: ctx.query.limit })
|
|
53
|
+
.pipe(
|
|
54
|
+
Effect.catchTag(
|
|
55
|
+
"Shell.NotFoundError",
|
|
56
|
+
() =>
|
|
57
|
+
new ShellNotFoundError({ id: ctx.params.id, message: `Shell command not found: ${ctx.params.id}` }),
|
|
58
|
+
),
|
|
59
|
+
),
|
|
60
|
+
)
|
|
61
|
+
}),
|
|
62
|
+
)
|
|
63
|
+
.handle(
|
|
64
|
+
"shell.remove",
|
|
65
|
+
Effect.fn(function* (ctx) {
|
|
66
|
+
const shell = yield* Shell.Service
|
|
67
|
+
yield* shell.remove(ctx.params.id).pipe(Effect.catchTag("Shell.NotFoundError", () => Effect.void))
|
|
68
|
+
return HttpApiSchema.NoContent.make()
|
|
69
|
+
}),
|
|
70
|
+
)
|
|
71
|
+
}),
|
|
72
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Skill } from "@zyro-sec/core/skill"
|
|
2
|
+
import { FileNotFoundError, SkillLoadError, SkillNotFoundError } from "@zyro-sec/protocol/errors"
|
|
3
|
+
import { Effect } from "effect"
|
|
4
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
5
|
+
import { Api } from "../api"
|
|
6
|
+
import { response } from "../location"
|
|
7
|
+
|
|
8
|
+
export const SkillHandler = HttpApiBuilder.group(Api, "server.skill", (handlers) =>
|
|
9
|
+
handlers
|
|
10
|
+
.handle("skill.list", () => response(Skill.Service.use((skill) => skill.list())))
|
|
11
|
+
.handle("skill.file", (ctx) =>
|
|
12
|
+
Effect.gen(function* () {
|
|
13
|
+
const skills = yield* Skill.Service
|
|
14
|
+
// The path after /api/experimental/skill/<id>/file/.
|
|
15
|
+
const raw = new URL(ctx.request.url, "http://localhost").pathname.split("/").slice(6).join("/")
|
|
16
|
+
const file = yield* Effect.try({
|
|
17
|
+
try: () => decodeURIComponent(raw),
|
|
18
|
+
catch: () => new FileNotFoundError({ path: raw, message: `File not found: ${raw}` }),
|
|
19
|
+
})
|
|
20
|
+
return yield* skills.read(ctx.params.skillID, file).pipe(
|
|
21
|
+
Effect.catchTags({
|
|
22
|
+
"Skill.NotFoundError": (error) =>
|
|
23
|
+
Effect.fail(
|
|
24
|
+
new SkillNotFoundError({ skill: ctx.params.skillID, message: `Skill not found: ${error.skill}` }),
|
|
25
|
+
),
|
|
26
|
+
"Skill.FileNotFoundError": (error) =>
|
|
27
|
+
Effect.fail(new FileNotFoundError({ path: error.file, message: `File not found: ${error.file}` })),
|
|
28
|
+
"Skill.LoadError": (error) =>
|
|
29
|
+
Effect.fail(new SkillLoadError({ skill: ctx.params.skillID, message: error.message })),
|
|
30
|
+
}),
|
|
31
|
+
)
|
|
32
|
+
}),
|
|
33
|
+
),
|
|
34
|
+
)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { UsageLimits } from "@zyro-sec/core/usage-limits"
|
|
2
|
+
import { ServiceUnavailableError } from "@zyro-sec/protocol/errors"
|
|
3
|
+
import { Effect } from "effect"
|
|
4
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
5
|
+
import { Api } from "../api"
|
|
6
|
+
import { response } from "../location"
|
|
7
|
+
|
|
8
|
+
export const UsageHandler = HttpApiBuilder.group(Api, "server.usage", (handlers) =>
|
|
9
|
+
Effect.gen(function* () {
|
|
10
|
+
return handlers.handle(
|
|
11
|
+
"usage.limits",
|
|
12
|
+
Effect.fn("server.usage.limits")(function* () {
|
|
13
|
+
const limits = yield* UsageLimits.Service
|
|
14
|
+
return yield* response(
|
|
15
|
+
limits.get().pipe(
|
|
16
|
+
Effect.map((info) => info ?? null),
|
|
17
|
+
Effect.mapError((error) => new ServiceUnavailableError({ message: error.message, service: "opencode" })),
|
|
18
|
+
),
|
|
19
|
+
)
|
|
20
|
+
}),
|
|
21
|
+
)
|
|
22
|
+
}),
|
|
23
|
+
)
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Vcs } from "@zyro-sec/core/vcs"
|
|
2
|
+
import { Project } from "@zyro-sec/core/project"
|
|
3
|
+
import { Worktree } from "@zyro-sec/core/worktree"
|
|
4
|
+
import { Bus } from "@zyro-sec/core/bus"
|
|
5
|
+
import { Location } from "@zyro-sec/core/location"
|
|
6
|
+
import { LocationServiceMap } from "@zyro-sec/core/location-services"
|
|
7
|
+
import { ConflictError, InvalidRequestError, ServiceUnavailableError } from "@zyro-sec/protocol/errors"
|
|
8
|
+
import { VcsInitNotSupportedError } from "@zyro-sec/protocol/groups/vcs"
|
|
9
|
+
import { Effect } from "effect"
|
|
10
|
+
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
|
|
11
|
+
import { Api } from "../api"
|
|
12
|
+
import { response } from "../location"
|
|
13
|
+
|
|
14
|
+
export const VcsHandler = HttpApiBuilder.group(Api, "server.vcs", (handlers) =>
|
|
15
|
+
Effect.gen(function* () {
|
|
16
|
+
const project = yield* Project.Service
|
|
17
|
+
const bus = yield* Bus.Service
|
|
18
|
+
const locations = yield* LocationServiceMap.Service
|
|
19
|
+
return handlers
|
|
20
|
+
.handle("vcs.init", (ctx) =>
|
|
21
|
+
Effect.gen(function* () {
|
|
22
|
+
const location = yield* Location.Service
|
|
23
|
+
const directory = location.project.directory
|
|
24
|
+
const providerID = ctx.query.provider ?? "git"
|
|
25
|
+
const vcs = yield* Vcs.Service
|
|
26
|
+
yield* vcs.initialize(providerID).pipe(
|
|
27
|
+
Effect.mapError((error) => {
|
|
28
|
+
if (error.kind === "missing")
|
|
29
|
+
return new InvalidRequestError({ message: "Project directory does not exist", field: "location" })
|
|
30
|
+
if (error.kind === "conflict")
|
|
31
|
+
return new ConflictError({ message: "Project already has version control", resource: directory })
|
|
32
|
+
if (error.kind === "unknown")
|
|
33
|
+
return new InvalidRequestError({
|
|
34
|
+
message: "Unknown VCS provider",
|
|
35
|
+
field: "provider",
|
|
36
|
+
})
|
|
37
|
+
if (error.kind === "unsupported")
|
|
38
|
+
return new VcsInitNotSupportedError({
|
|
39
|
+
providerID,
|
|
40
|
+
message: "VCS provider does not support initialization",
|
|
41
|
+
})
|
|
42
|
+
return new ServiceUnavailableError({ service: providerID, message: "VCS initialization failed" })
|
|
43
|
+
}),
|
|
44
|
+
)
|
|
45
|
+
const resolved = yield* project.resolve(directory)
|
|
46
|
+
if (!resolved.vcs)
|
|
47
|
+
return yield* new ServiceUnavailableError({ service: providerID, message: "VCS initialization failed" })
|
|
48
|
+
yield* locations.invalidate(
|
|
49
|
+
Location.Ref.make({ directory: location.directory, workspaceID: location.workspaceID }),
|
|
50
|
+
)
|
|
51
|
+
yield* bus.publish(Worktree.Event.Updated, { projectID: resolved.id })
|
|
52
|
+
return HttpApiSchema.NoContent.make()
|
|
53
|
+
}),
|
|
54
|
+
)
|
|
55
|
+
.handle("vcs.get", () =>
|
|
56
|
+
response(
|
|
57
|
+
Effect.gen(function* () {
|
|
58
|
+
const vcs = yield* Vcs.Service
|
|
59
|
+
return yield* vcs.info()
|
|
60
|
+
}),
|
|
61
|
+
),
|
|
62
|
+
)
|
|
63
|
+
.handle("vcs.base", () =>
|
|
64
|
+
response(
|
|
65
|
+
Effect.gen(function* () {
|
|
66
|
+
const vcs = yield* Vcs.Service
|
|
67
|
+
return yield* vcs
|
|
68
|
+
.base()
|
|
69
|
+
.pipe(Effect.mapError((error) => new ServiceUnavailableError({ service: "vcs", message: error.message })))
|
|
70
|
+
}),
|
|
71
|
+
),
|
|
72
|
+
)
|
|
73
|
+
.handle("vcs.status", () =>
|
|
74
|
+
response(
|
|
75
|
+
Effect.gen(function* () {
|
|
76
|
+
const vcs = yield* Vcs.Service
|
|
77
|
+
return yield* vcs.status()
|
|
78
|
+
}),
|
|
79
|
+
),
|
|
80
|
+
)
|
|
81
|
+
.handle("vcs.branch.list", (ctx) =>
|
|
82
|
+
response(
|
|
83
|
+
Effect.gen(function* () {
|
|
84
|
+
const vcs = yield* Vcs.Service
|
|
85
|
+
return yield* vcs.branches({ search: ctx.query.search, limit: Math.min(ctx.query.limit ?? 50, 100) })
|
|
86
|
+
}),
|
|
87
|
+
),
|
|
88
|
+
)
|
|
89
|
+
.handle("vcs.diff", (ctx) =>
|
|
90
|
+
response(
|
|
91
|
+
Effect.gen(function* () {
|
|
92
|
+
const vcs = yield* Vcs.Service
|
|
93
|
+
return yield* vcs
|
|
94
|
+
.diff(ctx.query.mode, {
|
|
95
|
+
context: ctx.query.context,
|
|
96
|
+
base: ctx.query.base,
|
|
97
|
+
untracked: ctx.query.untracked,
|
|
98
|
+
})
|
|
99
|
+
.pipe(Effect.mapError((error) => new ServiceUnavailableError({ service: "vcs", message: error.message })))
|
|
100
|
+
}),
|
|
101
|
+
),
|
|
102
|
+
)
|
|
103
|
+
}),
|
|
104
|
+
)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Location } from "@zyro-sec/core/location"
|
|
2
|
+
import { LocationServiceMap } from "@zyro-sec/core/location-services"
|
|
3
|
+
import { AbsolutePath } from "@zyro-sec/core/schema"
|
|
4
|
+
import { Voice } from "@zyro-sec/core/voice"
|
|
5
|
+
import { InvalidRequestError, ServiceUnavailableError } from "@zyro-sec/protocol/errors"
|
|
6
|
+
import { Global } from "@zyro-sec/util/global"
|
|
7
|
+
import { Effect } from "effect"
|
|
8
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
9
|
+
import { Api } from "../api"
|
|
10
|
+
import { locationErrors } from "../location"
|
|
11
|
+
|
|
12
|
+
export const VoiceHandler = HttpApiBuilder.group(Api, "server.voice", (handlers) =>
|
|
13
|
+
Effect.gen(function* () {
|
|
14
|
+
const global = yield* Global.Service
|
|
15
|
+
const locations = yield* LocationServiceMap.Service
|
|
16
|
+
const services = locations.get(Location.Ref.make({ directory: AbsolutePath.make(global.config) }))
|
|
17
|
+
return handlers
|
|
18
|
+
.handle(
|
|
19
|
+
"voice.models",
|
|
20
|
+
Effect.fn("server.voice.models")(
|
|
21
|
+
function* () {
|
|
22
|
+
const voice = yield* Voice.Service
|
|
23
|
+
return { data: yield* voice.models() }
|
|
24
|
+
},
|
|
25
|
+
Effect.provide(services),
|
|
26
|
+
locationErrors,
|
|
27
|
+
),
|
|
28
|
+
)
|
|
29
|
+
.handle(
|
|
30
|
+
"voice.session",
|
|
31
|
+
Effect.fn("server.voice.session")(
|
|
32
|
+
function* (request) {
|
|
33
|
+
const voice = yield* Voice.Service
|
|
34
|
+
const transport = yield* voice
|
|
35
|
+
.session(request.payload)
|
|
36
|
+
.pipe(
|
|
37
|
+
Effect.mapError((error) =>
|
|
38
|
+
error._tag === "Voice.InvalidRequestError"
|
|
39
|
+
? new InvalidRequestError({ message: error.message })
|
|
40
|
+
: new ServiceUnavailableError({ message: error.message, service: error.service }),
|
|
41
|
+
),
|
|
42
|
+
)
|
|
43
|
+
return { data: { transport } }
|
|
44
|
+
},
|
|
45
|
+
Effect.provide(services),
|
|
46
|
+
locationErrors,
|
|
47
|
+
),
|
|
48
|
+
)
|
|
49
|
+
}),
|
|
50
|
+
)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { WebSearch } from "@zyro-sec/core/websearch"
|
|
2
|
+
import { InvalidRequestError, ServiceUnavailableError } from "@zyro-sec/protocol/errors"
|
|
3
|
+
import { Effect } from "effect"
|
|
4
|
+
import { HttpApiBuilder } from "effect/http-api"
|
|
5
|
+
import { Api } from "../api"
|
|
6
|
+
import { response } from "../location"
|
|
7
|
+
|
|
8
|
+
export const WebSearchHandler = HttpApiBuilder.group(Api, "server.websearch", (handlers) =>
|
|
9
|
+
Effect.gen(function* () {
|
|
10
|
+
return handlers
|
|
11
|
+
.handle(
|
|
12
|
+
"websearch.providers",
|
|
13
|
+
Effect.fn("server.websearch.providers")(function* () {
|
|
14
|
+
const websearch = yield* WebSearch.Service
|
|
15
|
+
return yield* response(websearch.providers())
|
|
16
|
+
}),
|
|
17
|
+
)
|
|
18
|
+
.handle(
|
|
19
|
+
"websearch.query",
|
|
20
|
+
Effect.fn("server.websearch.query")(function* (request) {
|
|
21
|
+
const websearch = yield* WebSearch.Service
|
|
22
|
+
return yield* response(
|
|
23
|
+
websearch.query(request.payload).pipe(
|
|
24
|
+
Effect.catchTags({
|
|
25
|
+
"WebSearch.ProviderRequired": () =>
|
|
26
|
+
new InvalidRequestError({
|
|
27
|
+
message: "Web search provider is required",
|
|
28
|
+
kind: "websearch_provider_required",
|
|
29
|
+
field: "providerID",
|
|
30
|
+
}),
|
|
31
|
+
"WebSearch.ProviderNotFound": (error) =>
|
|
32
|
+
new InvalidRequestError({
|
|
33
|
+
message: `Web search provider not found: ${error.providerID}`,
|
|
34
|
+
kind: "websearch_provider_not_found",
|
|
35
|
+
field: "providerID",
|
|
36
|
+
}),
|
|
37
|
+
"WebSearch.Disabled": () =>
|
|
38
|
+
new InvalidRequestError({ message: "Web search is disabled", kind: "websearch_disabled" }),
|
|
39
|
+
"WebSearch.Request": (error) =>
|
|
40
|
+
new ServiceUnavailableError({
|
|
41
|
+
message: `Web search request failed: ${error.providerID}`,
|
|
42
|
+
service: error.providerID,
|
|
43
|
+
}),
|
|
44
|
+
}),
|
|
45
|
+
),
|
|
46
|
+
)
|
|
47
|
+
}),
|
|
48
|
+
)
|
|
49
|
+
}),
|
|
50
|
+
)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Git } from "@zyro-sec/core/git"
|
|
2
|
+
import { Worktree } from "@zyro-sec/core/worktree"
|
|
3
|
+
import { Project } from "@zyro-sec/core/project"
|
|
4
|
+
import { ProjectNotFoundError } from "@zyro-sec/protocol/errors"
|
|
5
|
+
import { WorktreeError } from "@zyro-sec/protocol/groups/worktree"
|
|
6
|
+
import { Effect } from "effect"
|
|
7
|
+
import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
|
|
8
|
+
import { Api } from "../api"
|
|
9
|
+
|
|
10
|
+
export const WorktreeHandler = HttpApiBuilder.group(Api, "server.worktree", (handlers) =>
|
|
11
|
+
Effect.gen(function* () {
|
|
12
|
+
const worktrees = yield* Worktree.Service
|
|
13
|
+
return handlers
|
|
14
|
+
.handle("worktree.list", (ctx) =>
|
|
15
|
+
worktrees.list(ctx.query).pipe(Effect.catchTag("Project.NotFoundError", missingProject)),
|
|
16
|
+
)
|
|
17
|
+
.handle("worktree.create", (ctx) => worktrees.create(ctx.payload).pipe(badRequest))
|
|
18
|
+
.handle("worktree.remove", (ctx) =>
|
|
19
|
+
worktrees.remove(ctx.payload).pipe(badRequest, Effect.as(HttpApiSchema.NoContent.make())),
|
|
20
|
+
)
|
|
21
|
+
.handle("worktree.refresh", (ctx) =>
|
|
22
|
+
worktrees.refresh(ctx.payload).pipe(badRequest, Effect.as(HttpApiSchema.NoContent.make())),
|
|
23
|
+
)
|
|
24
|
+
}),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
function badRequest<A, R>(effect: Effect.Effect<A, Worktree.Error, R>) {
|
|
28
|
+
return effect.pipe(
|
|
29
|
+
Effect.mapError((error) =>
|
|
30
|
+
error._tag === "Project.NotFoundError"
|
|
31
|
+
? missingProject(error)
|
|
32
|
+
: new WorktreeError({
|
|
33
|
+
name: "WorktreeError",
|
|
34
|
+
data: {
|
|
35
|
+
message: message(error),
|
|
36
|
+
forceRequired:
|
|
37
|
+
error instanceof Git.WorktreeError || error instanceof Worktree.OperationError
|
|
38
|
+
? error.forceRequired
|
|
39
|
+
: undefined,
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
),
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function missingProject(error: Project.NotFoundError) {
|
|
47
|
+
return new ProjectNotFoundError({ projectID: error.projectID, message: `Project not found: ${error.projectID}` })
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function message(error: Exclude<Worktree.Error, Project.NotFoundError>) {
|
|
51
|
+
if (error instanceof Worktree.SourceDirectoryNotFoundError)
|
|
52
|
+
return error.directory
|
|
53
|
+
? `Cannot create a worktree from ${error.directory}: it is not a checkout of this project that supports worktrees, such as a git repository`
|
|
54
|
+
: `Worktree source not found for project: ${error.projectID}`
|
|
55
|
+
if (error instanceof Worktree.DestinationExistsError) return `Worktree destination already exists: ${error.directory}`
|
|
56
|
+
if (error instanceof Worktree.DirectoryUnavailableError) return `Worktree directory unavailable: ${error.directory}`
|
|
57
|
+
if (error instanceof Worktree.InvalidDirectoryError) return `Invalid worktree directory: ${error.directory}`
|
|
58
|
+
if (error instanceof Worktree.StrategyUnavailableError) return `Worktree strategy unavailable: ${error.strategy}`
|
|
59
|
+
return error.message
|
|
60
|
+
}
|
package/src/handlers.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Layer } from "effect"
|
|
2
|
+
import { GenerateHandler } from "./handlers/generate"
|
|
3
|
+
import { VoiceHandler } from "./handlers/voice"
|
|
4
|
+
import { MessageHandler } from "./handlers/message"
|
|
5
|
+
import { ModelHandler } from "./handlers/model"
|
|
6
|
+
import { ProviderHandler } from "./handlers/provider"
|
|
7
|
+
import { SessionHandler } from "./handlers/session"
|
|
8
|
+
import { PermissionHandler } from "./handlers/permission"
|
|
9
|
+
import { FileSystemHandler } from "./handlers/fs"
|
|
10
|
+
import { FormHandler } from "./handlers/form"
|
|
11
|
+
import { CommandHandler } from "./handlers/command"
|
|
12
|
+
import { SkillHandler } from "./handlers/skill"
|
|
13
|
+
import { RpcHandler } from "./handlers/rpc"
|
|
14
|
+
import { EventHandler } from "./handlers/event"
|
|
15
|
+
import { AgentHandler } from "./handlers/agent"
|
|
16
|
+
import { PluginHandler } from "./handlers/plugin"
|
|
17
|
+
import { ServerHandler } from "./handlers/server"
|
|
18
|
+
import { DebugHandler } from "./handlers/debug"
|
|
19
|
+
import { PtyHandler } from "./handlers/pty"
|
|
20
|
+
import { PersistentPtyHandler } from "./handlers/persistent-pty"
|
|
21
|
+
import { ShellHandler } from "./handlers/shell"
|
|
22
|
+
import { ReferenceHandler } from "./handlers/reference"
|
|
23
|
+
import { LocationHandler } from "./handlers/location"
|
|
24
|
+
import { IntegrationHandler } from "./handlers/integration"
|
|
25
|
+
import { WebSearchHandler } from "./handlers/websearch"
|
|
26
|
+
import { UsageHandler } from "./handlers/usage"
|
|
27
|
+
import { McpHandler } from "./handlers/mcp"
|
|
28
|
+
import { CredentialHandler } from "./handlers/credential"
|
|
29
|
+
import { ProjectHandler } from "./handlers/project"
|
|
30
|
+
import { WorktreeHandler } from "./handlers/worktree"
|
|
31
|
+
import { VcsHandler } from "./handlers/vcs"
|
|
32
|
+
import { EventFeed } from "./event-feed"
|
|
33
|
+
import { MigrationHandler } from "./handlers/migration"
|
|
34
|
+
import { ConfigHandler } from "./handlers/config"
|
|
35
|
+
|
|
36
|
+
export const handlers = Layer.mergeAll(
|
|
37
|
+
ServerHandler,
|
|
38
|
+
DebugHandler,
|
|
39
|
+
MigrationHandler,
|
|
40
|
+
LocationHandler,
|
|
41
|
+
AgentHandler,
|
|
42
|
+
PluginHandler,
|
|
43
|
+
SessionHandler,
|
|
44
|
+
MessageHandler,
|
|
45
|
+
ModelHandler,
|
|
46
|
+
GenerateHandler,
|
|
47
|
+
VoiceHandler,
|
|
48
|
+
ProviderHandler,
|
|
49
|
+
IntegrationHandler,
|
|
50
|
+
WebSearchHandler,
|
|
51
|
+
UsageHandler,
|
|
52
|
+
McpHandler,
|
|
53
|
+
CredentialHandler,
|
|
54
|
+
ProjectHandler,
|
|
55
|
+
FormHandler,
|
|
56
|
+
PermissionHandler,
|
|
57
|
+
FileSystemHandler,
|
|
58
|
+
CommandHandler,
|
|
59
|
+
SkillHandler,
|
|
60
|
+
RpcHandler,
|
|
61
|
+
EventHandler.pipe(Layer.provide(EventFeed.layer)),
|
|
62
|
+
PtyHandler,
|
|
63
|
+
PersistentPtyHandler,
|
|
64
|
+
ShellHandler,
|
|
65
|
+
ReferenceHandler,
|
|
66
|
+
WorktreeHandler,
|
|
67
|
+
VcsHandler,
|
|
68
|
+
ConfigHandler,
|
|
69
|
+
)
|
package/src/location.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { FileSystem } from "@zyro-sec/core/filesystem"
|
|
2
|
+
import { Location } from "@zyro-sec/core/location"
|
|
3
|
+
import { LocationServiceMap } from "@zyro-sec/core/location-services"
|
|
4
|
+
import { AbsolutePath } from "@zyro-sec/core/schema"
|
|
5
|
+
import { Session } from "@zyro-sec/core/session"
|
|
6
|
+
import { LocationNotFoundError, InvalidRequestError } from "@zyro-sec/protocol/errors"
|
|
7
|
+
import { Effect, Layer, Schema } from "effect"
|
|
8
|
+
import { HttpServerRequest } from "effect/http"
|
|
9
|
+
import { HttpApiMiddleware } from "effect/http-api"
|
|
10
|
+
import { missingSession } from "./handlers/session-error"
|
|
11
|
+
|
|
12
|
+
export type LocationServices = Layer.Success<ReturnType<(typeof LocationServiceMap.Service)["get"]>>
|
|
13
|
+
|
|
14
|
+
export class LocationMiddleware extends HttpApiMiddleware.Service<LocationMiddleware, { provides: LocationServices }>()(
|
|
15
|
+
"@zyro-sec/HttpApiLocation",
|
|
16
|
+
{ error: [LocationNotFoundError] },
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
export function locationErrors<A, E, R>(effect: Effect.Effect<A, E, R>) {
|
|
20
|
+
return effect.pipe(
|
|
21
|
+
Effect.catchIf(
|
|
22
|
+
(error): error is Extract<E, FileSystem.DirectoryNotFoundError> =>
|
|
23
|
+
error instanceof FileSystem.DirectoryNotFoundError,
|
|
24
|
+
(error) =>
|
|
25
|
+
Effect.fail(
|
|
26
|
+
new LocationNotFoundError({
|
|
27
|
+
location: { directory: error.directory },
|
|
28
|
+
message: `Location not found: ${error.directory}`,
|
|
29
|
+
}),
|
|
30
|
+
),
|
|
31
|
+
),
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function response<A, E, R>(data: Effect.Effect<A, E, R>) {
|
|
36
|
+
return Effect.gen(function* () {
|
|
37
|
+
const location = yield* Location.Service
|
|
38
|
+
return {
|
|
39
|
+
location: new Location.Info({
|
|
40
|
+
directory: location.directory,
|
|
41
|
+
project: location.project,
|
|
42
|
+
}),
|
|
43
|
+
data: yield* data,
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const decodeSessionID = Schema.decodeUnknownEffect(Session.ID)
|
|
49
|
+
|
|
50
|
+
export const sessionInfo = Effect.fnUntraced(function* (sessions: Session.Interface, sessionID: unknown) {
|
|
51
|
+
const id = yield* decodeSessionID(sessionID).pipe(
|
|
52
|
+
Effect.mapError(() => new InvalidRequestError({ message: "Invalid session ID", field: "sessionID" })),
|
|
53
|
+
)
|
|
54
|
+
return yield* sessions.get(id).pipe(Effect.catchTag("Session.NotFoundError", missingSession))
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
export function requestRef(request: HttpServerRequest.HttpServerRequest): Location.Ref {
|
|
58
|
+
const query = new URL(request.url, "http://localhost").searchParams
|
|
59
|
+
const directory =
|
|
60
|
+
query.get("location[directory]") ||
|
|
61
|
+
(request.headers["x-opencode-directory"] ? decode(request.headers["x-opencode-directory"]) : process.cwd())
|
|
62
|
+
return Location.Ref.make({
|
|
63
|
+
directory: AbsolutePath.make(directory),
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function decode(input: string) {
|
|
68
|
+
try {
|
|
69
|
+
return decodeURIComponent(input)
|
|
70
|
+
} catch {
|
|
71
|
+
return input
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const layer = Layer.effect(
|
|
76
|
+
LocationMiddleware,
|
|
77
|
+
Effect.gen(function* () {
|
|
78
|
+
const locations = yield* LocationServiceMap.Service
|
|
79
|
+
return LocationMiddleware.of((effect) =>
|
|
80
|
+
Effect.gen(function* () {
|
|
81
|
+
const request = yield* HttpServerRequest.HttpServerRequest
|
|
82
|
+
return yield* effect.pipe(Effect.provide(locations.get(requestRef(request))), locationErrors)
|
|
83
|
+
}),
|
|
84
|
+
)
|
|
85
|
+
}),
|
|
86
|
+
)
|
package/src/media-uri.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { HttpServerRequest } from "effect/http"
|
|
2
|
+
|
|
3
|
+
const CAPABILITIES_HEADER = "x-opencode-capabilities"
|
|
4
|
+
const MEDIA_URI_CAPABILITY = "experimental-media-uri"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Whether the client opted into `media:` refs. The session's own location decides whether its stored media is
|
|
8
|
+
* actually left as refs (`experimental.media_blob`), so this only picks which read to call.
|
|
9
|
+
*/
|
|
10
|
+
export const wantsMediaRefs = (request: HttpServerRequest.HttpServerRequest) =>
|
|
11
|
+
request.headers[CAPABILITIES_HEADER]?.split(",").some(
|
|
12
|
+
(token) => token.trim().toLowerCase() === MEDIA_URI_CAPABILITY,
|
|
13
|
+
) ?? false
|