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