@zyro-sec/sdk 1.0.2 → 1.0.4
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/README.md +148 -148
- package/package.json +10 -10
- package/script/build.ts +28 -28
- package/script/pack.ts +5 -5
- package/script/verify-package.ts +252 -252
- package/src/contracts.ts +26 -26
- package/src/effect/index.ts +11 -11
- package/src/effect/opencode.ts +78 -78
- package/src/effect/tool.ts +3 -3
- package/src/effect/workerd.ts +48 -48
- package/src/index.ts +6 -6
- package/src/internal/fetch.ts +117 -117
- package/src/internal/host.ts +73 -73
- package/src/internal/instances.ts +107 -107
- package/src/internal/workerd.ts +21 -21
- package/src/logging.ts +76 -76
- package/src/opencode.ts +10 -10
- package/src/promise.ts +85 -85
- package/src/tool.ts +3 -3
- package/src/workerd.ts +51 -51
- package/tsconfig.build.json +15 -15
- package/tsconfig.json +11 -11
- package/test/contract-identity.test.ts +0 -114
- package/test/embedded.test.ts +0 -748
- package/test/import-boundaries.test.ts +0 -40
- package/test/instances-effect.test.ts +0 -302
- package/test/instances-lifecycle.test.ts +0 -66
- package/test/instances.test.ts +0 -389
- package/test/logging.test.ts +0 -20
- package/test/promise.test.ts +0 -66
- package/test/request-retention.test.ts +0 -133
- package/test/transport.test.ts +0 -70
- package/test/worktree.test.ts +0 -65
package/src/effect/opencode.ts
CHANGED
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
export * as OpenCode from "./opencode"
|
|
2
|
-
|
|
3
|
-
import { OpenCode, type OpenCodeClient } from "@zyro-sec/client/effect"
|
|
4
|
-
import type { Session } from "@zyro-sec/core/session"
|
|
5
|
-
import type { Workspace } from "@zyro-sec/core/workspace"
|
|
6
|
-
import { Context, Effect, Layer } from "effect"
|
|
7
|
-
import type { Config, Scope } from "effect"
|
|
8
|
-
import { FetchHttpClient, HttpClient } from "effect/http"
|
|
9
|
-
import { EmbeddedHost } from "../internal/host"
|
|
10
|
-
import type { SdkInstances } from "../internal/instances"
|
|
11
|
-
|
|
12
|
-
export type { LogEntry, LogLevel, LogOptions, LogWriter } from "../logging"
|
|
13
|
-
|
|
14
|
-
export type CreateOptions<R = never> = EmbeddedHost.CreateOptions<R>
|
|
15
|
-
export type EmbedOptions = EmbeddedHost.EmbedOptions
|
|
16
|
-
export type InstanceOptions<R = never> = SdkInstances.Options<R>
|
|
17
|
-
export type InstanceConfiguration = SdkInstances.Configuration
|
|
18
|
-
|
|
19
|
-
export type Interface = Omit<OpenCodeClient, "plugin" | "workspace"> & {
|
|
20
|
-
readonly sessions: Omit<OpenCodeClient["session"], "create" | "get" | "move"> & {
|
|
21
|
-
readonly create: Session.Interface["create"]
|
|
22
|
-
readonly get: (input: { readonly sessionID: Session.ID }) => ReturnType<Session.Interface["get"]>
|
|
23
|
-
readonly move: Session.Interface["move"]
|
|
24
|
-
}
|
|
25
|
-
readonly events: OpenCodeClient["event"]
|
|
26
|
-
readonly workspace: {
|
|
27
|
-
readonly create: Workspace.Interface["create"]
|
|
28
|
-
readonly attach: Workspace.Interface["attach"]
|
|
29
|
-
readonly provision: (options: {
|
|
30
|
-
readonly workspaceID: Workspace.ID
|
|
31
|
-
}) => ReturnType<Workspace.Interface["provision"]>
|
|
32
|
-
readonly destroy: (options: { readonly workspaceID: Workspace.ID }) => ReturnType<Workspace.Interface["destroy"]>
|
|
33
|
-
readonly suspendIdle: Workspace.Interface["suspendIdle"]
|
|
34
|
-
}
|
|
35
|
-
readonly plugin: EmbeddedHost.Interface["plugins"]["register"] & OpenCodeClient["plugin"]
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export const create: <R = never>(
|
|
39
|
-
options?: CreateOptions<R>,
|
|
40
|
-
embed?: EmbedOptions,
|
|
41
|
-
) => Effect.Effect<Interface, Config.ConfigError | Error, Scope.Scope | R> = Effect.fn("OpenCode.create")(function* <
|
|
42
|
-
R = never,
|
|
43
|
-
>(options: CreateOptions<R> = {}, embed: EmbedOptions = {}) {
|
|
44
|
-
const host = yield* Effect.acquireRelease(EmbeddedHost.create(options, embed), (host) => Effect.promise(host.close))
|
|
45
|
-
const httpClient = yield* HttpClient.HttpClient.pipe(Effect.provide(FetchHttpClient.layer))
|
|
46
|
-
const client = yield* OpenCode.make({ baseUrl: "http://opencode.local" }).pipe(
|
|
47
|
-
Effect.provideService(
|
|
48
|
-
HttpClient.HttpClient,
|
|
49
|
-
// FetchHttpClient reads Fetch at request time; callers must not replace this host's in-process transport.
|
|
50
|
-
HttpClient.transformResponse(httpClient, Effect.provideService(FetchHttpClient.Fetch, host.fetch)),
|
|
51
|
-
),
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
...client,
|
|
56
|
-
sessions: {
|
|
57
|
-
...client.session,
|
|
58
|
-
create: host.sessions.create,
|
|
59
|
-
get: (input: { readonly sessionID: Session.ID }) => host.sessions.get(input.sessionID),
|
|
60
|
-
move: host.sessions.move,
|
|
61
|
-
},
|
|
62
|
-
events: client.event,
|
|
63
|
-
workspace: {
|
|
64
|
-
create: host.workspace.create,
|
|
65
|
-
attach: host.workspace.attach,
|
|
66
|
-
provision: ({ workspaceID }: { readonly workspaceID: Workspace.ID }) => host.workspace.provision(workspaceID),
|
|
67
|
-
destroy: ({ workspaceID }: { readonly workspaceID: Workspace.ID }) => host.workspace.destroy(workspaceID),
|
|
68
|
-
suspendIdle: host.workspace.suspendIdle,
|
|
69
|
-
},
|
|
70
|
-
plugin: Object.assign(host.plugins.register, client.plugin),
|
|
71
|
-
}
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
export class Service extends Context.Service<Service, Interface>()("@zyro-sec/sdk/OpenCode") {}
|
|
75
|
-
|
|
76
|
-
export const layer = <R = never>(
|
|
77
|
-
options: CreateOptions<R> = {},
|
|
78
|
-
): Layer.Layer<Service, Config.ConfigError | Error, Exclude<R, Scope.Scope>> => Layer.effect(Service, create(options))
|
|
1
|
+
export * as OpenCode from "./opencode"
|
|
2
|
+
|
|
3
|
+
import { OpenCode, type OpenCodeClient } from "@zyro-sec/client/effect"
|
|
4
|
+
import type { Session } from "@zyro-sec/core/session"
|
|
5
|
+
import type { Workspace } from "@zyro-sec/core/workspace"
|
|
6
|
+
import { Context, Effect, Layer } from "effect"
|
|
7
|
+
import type { Config, Scope } from "effect"
|
|
8
|
+
import { FetchHttpClient, HttpClient } from "effect/http"
|
|
9
|
+
import { EmbeddedHost } from "../internal/host"
|
|
10
|
+
import type { SdkInstances } from "../internal/instances"
|
|
11
|
+
|
|
12
|
+
export type { LogEntry, LogLevel, LogOptions, LogWriter } from "../logging"
|
|
13
|
+
|
|
14
|
+
export type CreateOptions<R = never> = EmbeddedHost.CreateOptions<R>
|
|
15
|
+
export type EmbedOptions = EmbeddedHost.EmbedOptions
|
|
16
|
+
export type InstanceOptions<R = never> = SdkInstances.Options<R>
|
|
17
|
+
export type InstanceConfiguration = SdkInstances.Configuration
|
|
18
|
+
|
|
19
|
+
export type Interface = Omit<OpenCodeClient, "plugin" | "workspace"> & {
|
|
20
|
+
readonly sessions: Omit<OpenCodeClient["session"], "create" | "get" | "move"> & {
|
|
21
|
+
readonly create: Session.Interface["create"]
|
|
22
|
+
readonly get: (input: { readonly sessionID: Session.ID }) => ReturnType<Session.Interface["get"]>
|
|
23
|
+
readonly move: Session.Interface["move"]
|
|
24
|
+
}
|
|
25
|
+
readonly events: OpenCodeClient["event"]
|
|
26
|
+
readonly workspace: {
|
|
27
|
+
readonly create: Workspace.Interface["create"]
|
|
28
|
+
readonly attach: Workspace.Interface["attach"]
|
|
29
|
+
readonly provision: (options: {
|
|
30
|
+
readonly workspaceID: Workspace.ID
|
|
31
|
+
}) => ReturnType<Workspace.Interface["provision"]>
|
|
32
|
+
readonly destroy: (options: { readonly workspaceID: Workspace.ID }) => ReturnType<Workspace.Interface["destroy"]>
|
|
33
|
+
readonly suspendIdle: Workspace.Interface["suspendIdle"]
|
|
34
|
+
}
|
|
35
|
+
readonly plugin: EmbeddedHost.Interface["plugins"]["register"] & OpenCodeClient["plugin"]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const create: <R = never>(
|
|
39
|
+
options?: CreateOptions<R>,
|
|
40
|
+
embed?: EmbedOptions,
|
|
41
|
+
) => Effect.Effect<Interface, Config.ConfigError | Error, Scope.Scope | R> = Effect.fn("OpenCode.create")(function* <
|
|
42
|
+
R = never,
|
|
43
|
+
>(options: CreateOptions<R> = {}, embed: EmbedOptions = {}) {
|
|
44
|
+
const host = yield* Effect.acquireRelease(EmbeddedHost.create(options, embed), (host) => Effect.promise(host.close))
|
|
45
|
+
const httpClient = yield* HttpClient.HttpClient.pipe(Effect.provide(FetchHttpClient.layer))
|
|
46
|
+
const client = yield* OpenCode.make({ baseUrl: "http://opencode.local" }).pipe(
|
|
47
|
+
Effect.provideService(
|
|
48
|
+
HttpClient.HttpClient,
|
|
49
|
+
// FetchHttpClient reads Fetch at request time; callers must not replace this host's in-process transport.
|
|
50
|
+
HttpClient.transformResponse(httpClient, Effect.provideService(FetchHttpClient.Fetch, host.fetch)),
|
|
51
|
+
),
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
...client,
|
|
56
|
+
sessions: {
|
|
57
|
+
...client.session,
|
|
58
|
+
create: host.sessions.create,
|
|
59
|
+
get: (input: { readonly sessionID: Session.ID }) => host.sessions.get(input.sessionID),
|
|
60
|
+
move: host.sessions.move,
|
|
61
|
+
},
|
|
62
|
+
events: client.event,
|
|
63
|
+
workspace: {
|
|
64
|
+
create: host.workspace.create,
|
|
65
|
+
attach: host.workspace.attach,
|
|
66
|
+
provision: ({ workspaceID }: { readonly workspaceID: Workspace.ID }) => host.workspace.provision(workspaceID),
|
|
67
|
+
destroy: ({ workspaceID }: { readonly workspaceID: Workspace.ID }) => host.workspace.destroy(workspaceID),
|
|
68
|
+
suspendIdle: host.workspace.suspendIdle,
|
|
69
|
+
},
|
|
70
|
+
plugin: Object.assign(host.plugins.register, client.plugin),
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
export class Service extends Context.Service<Service, Interface>()("@zyro-sec/sdk/OpenCode") {}
|
|
75
|
+
|
|
76
|
+
export const layer = <R = never>(
|
|
77
|
+
options: CreateOptions<R> = {},
|
|
78
|
+
): Layer.Layer<Service, Config.ConfigError | Error, Exclude<R, Scope.Scope>> => Layer.effect(Service, create(options))
|
package/src/effect/tool.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { RegistrationError } from "@zyro-sec/core/tool"
|
|
2
|
-
export { Error } from "@zyro-sec/schema/tool"
|
|
3
|
-
export type { Context, Info } from "@zyro-sec/schema/tool"
|
|
1
|
+
export { RegistrationError } from "@zyro-sec/core/tool"
|
|
2
|
+
export { Error } from "@zyro-sec/schema/tool"
|
|
3
|
+
export type { Context, Info } from "@zyro-sec/schema/tool"
|
package/src/effect/workerd.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
export * as OpenCodeWorkerd from "./workerd"
|
|
2
|
-
|
|
3
|
-
import { Effect, Layer } from "effect"
|
|
4
|
-
import type { Config, Scope } from "effect"
|
|
5
|
-
import { WorkerdProfile } from "../internal/workerd"
|
|
6
|
-
import { OpenCode } from "./opencode"
|
|
7
|
-
|
|
8
|
-
export type Configuration = WorkerdProfile.Configuration
|
|
9
|
-
|
|
10
|
-
export interface CreateOptions<R = never> extends WorkerdProfile.Options {
|
|
11
|
-
readonly log?: OpenCode.CreateOptions["log"]
|
|
12
|
-
readonly plugins?: OpenCode.CreateOptions["plugins"]
|
|
13
|
-
readonly workspaceProviders?: OpenCode.CreateOptions["workspaceProviders"]
|
|
14
|
-
readonly instances?: OpenCode.CreateOptions<R>["instances"]
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type Interface = OpenCode.Interface & {
|
|
18
|
-
/** Suspends any workspaces past their idle threshold; run from the Durable Object's `alarm()` method. */
|
|
19
|
-
readonly alarm: Effect.Effect<void>
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const create = <R = never>(
|
|
23
|
-
options: CreateOptions<R>,
|
|
24
|
-
): Effect.Effect<Interface, Config.ConfigError | Error, Scope.Scope | R> => {
|
|
25
|
-
const profile = WorkerdProfile.make(options)
|
|
26
|
-
return OpenCode.create(
|
|
27
|
-
{
|
|
28
|
-
...profile.options,
|
|
29
|
-
log: options.log,
|
|
30
|
-
plugins: options.plugins,
|
|
31
|
-
workspaceProviders: options.workspaceProviders,
|
|
32
|
-
instances: options.instances,
|
|
33
|
-
},
|
|
34
|
-
{ overrides: profile.replacements },
|
|
35
|
-
).pipe(
|
|
36
|
-
Effect.map((opencode) => ({
|
|
37
|
-
...opencode,
|
|
38
|
-
alarm: Effect.asVoid(opencode.workspace.suspendIdle()),
|
|
39
|
-
})),
|
|
40
|
-
)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export const layer = <R = never>(
|
|
44
|
-
options: CreateOptions<R>,
|
|
45
|
-
): Layer.Layer<OpenCode.Service, Config.ConfigError | Error, Exclude<R, Scope.Scope>> =>
|
|
46
|
-
Layer.effect(OpenCode.Service, create(options))
|
|
47
|
-
|
|
48
|
-
export type Requirements = Scope.Scope
|
|
1
|
+
export * as OpenCodeWorkerd from "./workerd"
|
|
2
|
+
|
|
3
|
+
import { Effect, Layer } from "effect"
|
|
4
|
+
import type { Config, Scope } from "effect"
|
|
5
|
+
import { WorkerdProfile } from "../internal/workerd"
|
|
6
|
+
import { OpenCode } from "./opencode"
|
|
7
|
+
|
|
8
|
+
export type Configuration = WorkerdProfile.Configuration
|
|
9
|
+
|
|
10
|
+
export interface CreateOptions<R = never> extends WorkerdProfile.Options {
|
|
11
|
+
readonly log?: OpenCode.CreateOptions["log"]
|
|
12
|
+
readonly plugins?: OpenCode.CreateOptions["plugins"]
|
|
13
|
+
readonly workspaceProviders?: OpenCode.CreateOptions["workspaceProviders"]
|
|
14
|
+
readonly instances?: OpenCode.CreateOptions<R>["instances"]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type Interface = OpenCode.Interface & {
|
|
18
|
+
/** Suspends any workspaces past their idle threshold; run from the Durable Object's `alarm()` method. */
|
|
19
|
+
readonly alarm: Effect.Effect<void>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const create = <R = never>(
|
|
23
|
+
options: CreateOptions<R>,
|
|
24
|
+
): Effect.Effect<Interface, Config.ConfigError | Error, Scope.Scope | R> => {
|
|
25
|
+
const profile = WorkerdProfile.make(options)
|
|
26
|
+
return OpenCode.create(
|
|
27
|
+
{
|
|
28
|
+
...profile.options,
|
|
29
|
+
log: options.log,
|
|
30
|
+
plugins: options.plugins,
|
|
31
|
+
workspaceProviders: options.workspaceProviders,
|
|
32
|
+
instances: options.instances,
|
|
33
|
+
},
|
|
34
|
+
{ overrides: profile.replacements },
|
|
35
|
+
).pipe(
|
|
36
|
+
Effect.map((opencode) => ({
|
|
37
|
+
...opencode,
|
|
38
|
+
alarm: Effect.asVoid(opencode.workspace.suspendIdle()),
|
|
39
|
+
})),
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const layer = <R = never>(
|
|
44
|
+
options: CreateOptions<R>,
|
|
45
|
+
): Layer.Layer<OpenCode.Service, Config.ConfigError | Error, Exclude<R, Scope.Scope>> =>
|
|
46
|
+
Layer.effect(OpenCode.Service, create(options))
|
|
47
|
+
|
|
48
|
+
export type Requirements = Scope.Scope
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * as OpenCode from "./opencode"
|
|
2
|
-
export * as Tool from "./tool"
|
|
3
|
-
|
|
4
|
-
export { ClientError } from "@zyro-sec/client"
|
|
5
|
-
export type { OpenCodeEvent } from "@zyro-sec/client"
|
|
6
|
-
export * from "./contracts"
|
|
1
|
+
export * as OpenCode from "./opencode"
|
|
2
|
+
export * as Tool from "./tool"
|
|
3
|
+
|
|
4
|
+
export { ClientError } from "@zyro-sec/client"
|
|
5
|
+
export type { OpenCodeEvent } from "@zyro-sec/client"
|
|
6
|
+
export * from "./contracts"
|
package/src/internal/fetch.ts
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
export * as OwnedFetch from "./fetch"
|
|
2
|
-
|
|
3
|
-
export function make(handler: (request: Request) => Promise<Response>, dispose: () => Promise<void>) {
|
|
4
|
-
const requests = new Map<AbortController, Promise<void>>()
|
|
5
|
-
const closed = new Error("OpenCode host is closed")
|
|
6
|
-
let closePromise: Promise<void> | undefined
|
|
7
|
-
const fetch = Object.assign(
|
|
8
|
-
(input: RequestInfo | URL, init?: RequestInit) => {
|
|
9
|
-
if (closePromise) return Promise.reject(closed)
|
|
10
|
-
const source = new Request(input, init)
|
|
11
|
-
if (source.signal.aborted) return Promise.reject(source.signal.reason)
|
|
12
|
-
const controller = new AbortController()
|
|
13
|
-
const abort = () => controller.abort(source.signal.reason)
|
|
14
|
-
source.signal.addEventListener("abort", abort, { once: true })
|
|
15
|
-
const request = new Request(source, { signal: controller.signal })
|
|
16
|
-
const lifetime = Promise.withResolvers<void>()
|
|
17
|
-
const finish = () => {
|
|
18
|
-
source.signal.removeEventListener("abort", abort)
|
|
19
|
-
requests.delete(controller)
|
|
20
|
-
lifetime.resolve()
|
|
21
|
-
}
|
|
22
|
-
requests.set(controller, lifetime.promise)
|
|
23
|
-
|
|
24
|
-
const handled = handler(request)
|
|
25
|
-
return rejectOnAbort(handled, request.signal).then(
|
|
26
|
-
(response) => trackResponse(response, request.signal, finish),
|
|
27
|
-
(cause) => {
|
|
28
|
-
void handled.then(finish, finish)
|
|
29
|
-
throw cause
|
|
30
|
-
},
|
|
31
|
-
)
|
|
32
|
-
},
|
|
33
|
-
{ preconnect: () => undefined },
|
|
34
|
-
) satisfies typeof globalThis.fetch
|
|
35
|
-
const close = () => {
|
|
36
|
-
if (closePromise) return closePromise
|
|
37
|
-
closePromise = Promise.resolve().then(async () => {
|
|
38
|
-
requests.forEach((_, controller) => controller.abort(closed))
|
|
39
|
-
await Promise.allSettled(requests.values())
|
|
40
|
-
await dispose()
|
|
41
|
-
})
|
|
42
|
-
return closePromise
|
|
43
|
-
}
|
|
44
|
-
return { fetch, close }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function rejectOnAbort<A>(promise: Promise<A>, signal: AbortSignal): Promise<A> {
|
|
48
|
-
if (signal.aborted) return Promise.reject(signal.reason)
|
|
49
|
-
return new Promise((resolve, reject) => {
|
|
50
|
-
const abort = () => reject(signal.reason)
|
|
51
|
-
signal.addEventListener("abort", abort, { once: true })
|
|
52
|
-
promise.then(
|
|
53
|
-
(value) => {
|
|
54
|
-
signal.removeEventListener("abort", abort)
|
|
55
|
-
resolve(value)
|
|
56
|
-
},
|
|
57
|
-
(cause) => {
|
|
58
|
-
signal.removeEventListener("abort", abort)
|
|
59
|
-
reject(cause)
|
|
60
|
-
},
|
|
61
|
-
)
|
|
62
|
-
})
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function trackResponse(response: Response, signal: AbortSignal, finish: () => void): Response {
|
|
66
|
-
if (!response.body) {
|
|
67
|
-
finish()
|
|
68
|
-
return response
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const reader = response.body.getReader()
|
|
72
|
-
let done = false
|
|
73
|
-
let abort = () => {}
|
|
74
|
-
const complete = () => {
|
|
75
|
-
if (done) return false
|
|
76
|
-
done = true
|
|
77
|
-
signal.removeEventListener("abort", abort)
|
|
78
|
-
return true
|
|
79
|
-
}
|
|
80
|
-
const body = new ReadableStream<Uint8Array>({
|
|
81
|
-
start(controller) {
|
|
82
|
-
abort = () => {
|
|
83
|
-
if (!complete()) return
|
|
84
|
-
controller.error(signal.reason)
|
|
85
|
-
void reader.cancel(signal.reason).then(finish, finish)
|
|
86
|
-
}
|
|
87
|
-
if (signal.aborted) abort()
|
|
88
|
-
else signal.addEventListener("abort", abort, { once: true })
|
|
89
|
-
},
|
|
90
|
-
async pull(controller) {
|
|
91
|
-
try {
|
|
92
|
-
const next = await reader.read()
|
|
93
|
-
if (done) return
|
|
94
|
-
if (!next.done) {
|
|
95
|
-
controller.enqueue(next.value)
|
|
96
|
-
return
|
|
97
|
-
}
|
|
98
|
-
if (!complete()) return
|
|
99
|
-
controller.close()
|
|
100
|
-
finish()
|
|
101
|
-
} catch (cause) {
|
|
102
|
-
if (!complete()) return
|
|
103
|
-
controller.error(cause)
|
|
104
|
-
finish()
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
async cancel(reason) {
|
|
108
|
-
if (!complete()) return
|
|
109
|
-
try {
|
|
110
|
-
await reader.cancel(reason)
|
|
111
|
-
} finally {
|
|
112
|
-
finish()
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
})
|
|
116
|
-
return new Response(body, response)
|
|
117
|
-
}
|
|
1
|
+
export * as OwnedFetch from "./fetch"
|
|
2
|
+
|
|
3
|
+
export function make(handler: (request: Request) => Promise<Response>, dispose: () => Promise<void>) {
|
|
4
|
+
const requests = new Map<AbortController, Promise<void>>()
|
|
5
|
+
const closed = new Error("OpenCode host is closed")
|
|
6
|
+
let closePromise: Promise<void> | undefined
|
|
7
|
+
const fetch = Object.assign(
|
|
8
|
+
(input: RequestInfo | URL, init?: RequestInit) => {
|
|
9
|
+
if (closePromise) return Promise.reject(closed)
|
|
10
|
+
const source = new Request(input, init)
|
|
11
|
+
if (source.signal.aborted) return Promise.reject(source.signal.reason)
|
|
12
|
+
const controller = new AbortController()
|
|
13
|
+
const abort = () => controller.abort(source.signal.reason)
|
|
14
|
+
source.signal.addEventListener("abort", abort, { once: true })
|
|
15
|
+
const request = new Request(source, { signal: controller.signal })
|
|
16
|
+
const lifetime = Promise.withResolvers<void>()
|
|
17
|
+
const finish = () => {
|
|
18
|
+
source.signal.removeEventListener("abort", abort)
|
|
19
|
+
requests.delete(controller)
|
|
20
|
+
lifetime.resolve()
|
|
21
|
+
}
|
|
22
|
+
requests.set(controller, lifetime.promise)
|
|
23
|
+
|
|
24
|
+
const handled = handler(request)
|
|
25
|
+
return rejectOnAbort(handled, request.signal).then(
|
|
26
|
+
(response) => trackResponse(response, request.signal, finish),
|
|
27
|
+
(cause) => {
|
|
28
|
+
void handled.then(finish, finish)
|
|
29
|
+
throw cause
|
|
30
|
+
},
|
|
31
|
+
)
|
|
32
|
+
},
|
|
33
|
+
{ preconnect: () => undefined },
|
|
34
|
+
) satisfies typeof globalThis.fetch
|
|
35
|
+
const close = () => {
|
|
36
|
+
if (closePromise) return closePromise
|
|
37
|
+
closePromise = Promise.resolve().then(async () => {
|
|
38
|
+
requests.forEach((_, controller) => controller.abort(closed))
|
|
39
|
+
await Promise.allSettled(requests.values())
|
|
40
|
+
await dispose()
|
|
41
|
+
})
|
|
42
|
+
return closePromise
|
|
43
|
+
}
|
|
44
|
+
return { fetch, close }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function rejectOnAbort<A>(promise: Promise<A>, signal: AbortSignal): Promise<A> {
|
|
48
|
+
if (signal.aborted) return Promise.reject(signal.reason)
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
const abort = () => reject(signal.reason)
|
|
51
|
+
signal.addEventListener("abort", abort, { once: true })
|
|
52
|
+
promise.then(
|
|
53
|
+
(value) => {
|
|
54
|
+
signal.removeEventListener("abort", abort)
|
|
55
|
+
resolve(value)
|
|
56
|
+
},
|
|
57
|
+
(cause) => {
|
|
58
|
+
signal.removeEventListener("abort", abort)
|
|
59
|
+
reject(cause)
|
|
60
|
+
},
|
|
61
|
+
)
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function trackResponse(response: Response, signal: AbortSignal, finish: () => void): Response {
|
|
66
|
+
if (!response.body) {
|
|
67
|
+
finish()
|
|
68
|
+
return response
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const reader = response.body.getReader()
|
|
72
|
+
let done = false
|
|
73
|
+
let abort = () => {}
|
|
74
|
+
const complete = () => {
|
|
75
|
+
if (done) return false
|
|
76
|
+
done = true
|
|
77
|
+
signal.removeEventListener("abort", abort)
|
|
78
|
+
return true
|
|
79
|
+
}
|
|
80
|
+
const body = new ReadableStream<Uint8Array>({
|
|
81
|
+
start(controller) {
|
|
82
|
+
abort = () => {
|
|
83
|
+
if (!complete()) return
|
|
84
|
+
controller.error(signal.reason)
|
|
85
|
+
void reader.cancel(signal.reason).then(finish, finish)
|
|
86
|
+
}
|
|
87
|
+
if (signal.aborted) abort()
|
|
88
|
+
else signal.addEventListener("abort", abort, { once: true })
|
|
89
|
+
},
|
|
90
|
+
async pull(controller) {
|
|
91
|
+
try {
|
|
92
|
+
const next = await reader.read()
|
|
93
|
+
if (done) return
|
|
94
|
+
if (!next.done) {
|
|
95
|
+
controller.enqueue(next.value)
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
if (!complete()) return
|
|
99
|
+
controller.close()
|
|
100
|
+
finish()
|
|
101
|
+
} catch (cause) {
|
|
102
|
+
if (!complete()) return
|
|
103
|
+
controller.error(cause)
|
|
104
|
+
finish()
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
async cancel(reason) {
|
|
108
|
+
if (!complete()) return
|
|
109
|
+
try {
|
|
110
|
+
await reader.cancel(reason)
|
|
111
|
+
} finally {
|
|
112
|
+
finish()
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
})
|
|
116
|
+
return new Response(body, response)
|
|
117
|
+
}
|
package/src/internal/host.ts
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
export * as EmbeddedHost from "./host"
|
|
2
|
-
|
|
3
|
-
import { SdkPlugins } from "@zyro-sec/core/plugin/sdk"
|
|
4
|
-
import type { Plugin } from "@zyro-sec/plugin/effect/plugin"
|
|
5
|
-
import { SessionRestart } from "@zyro-sec/core/session/execution/restart"
|
|
6
|
-
import { Session } from "@zyro-sec/core/session"
|
|
7
|
-
import { Workspace } from "@zyro-sec/core/workspace"
|
|
8
|
-
import { WorkspaceDriver } from "@zyro-sec/core/workspace/driver"
|
|
9
|
-
import { createEmbeddedRoutes } from "@zyro-sec/server/routes"
|
|
10
|
-
import type { ServerOptions } from "@zyro-sec/server/options"
|
|
11
|
-
import type { LayerNode } from "@zyro-sec/util/effect/layer-node"
|
|
12
|
-
import { Context, Effect, Layer, ManagedRuntime, Scope } from "effect"
|
|
13
|
-
import { HttpEffect, HttpRouter, HttpServer, HttpServerRequest } from "effect/http"
|
|
14
|
-
import { context, layer, type LogOptions } from "../logging"
|
|
15
|
-
import { OwnedFetch } from "./fetch"
|
|
16
|
-
import { SdkInstances } from "./instances"
|
|
17
|
-
|
|
18
|
-
export interface CreateOptions<R = never> extends Omit<ServerOptions, "hostname" | "port" | "password"> {
|
|
19
|
-
readonly log?: LogOptions
|
|
20
|
-
readonly plugins?: ReadonlyArray<Plugin>
|
|
21
|
-
readonly workspaceProviders?: Readonly<Record<string, WorkspaceDriver.Interface>>
|
|
22
|
-
readonly instances?: SdkInstances.Options<R>
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** Host hooks for embedding opencode on a non-default runtime profile. */
|
|
26
|
-
export interface EmbedOptions {
|
|
27
|
-
readonly overrides?: LayerNode.Replacements
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export const create = Effect.fn("EmbeddedHost.create")(function* <R = never>(
|
|
31
|
-
options: CreateOptions<R> = {},
|
|
32
|
-
embed: EmbedOptions = {},
|
|
33
|
-
) {
|
|
34
|
-
const { log, plugins, workspaceProviders, instances, ...server } = options
|
|
35
|
-
const selector = instances ? SdkInstances.provide(instances, yield* Effect.context<R>()) : undefined
|
|
36
|
-
const runtime = ManagedRuntime.make(
|
|
37
|
-
createEmbeddedRoutes(
|
|
38
|
-
{
|
|
39
|
-
...server,
|
|
40
|
-
app: { ...server.app, name: server.app?.name ?? "sdk" },
|
|
41
|
-
database: { path: ":memory:", ...server.database },
|
|
42
|
-
},
|
|
43
|
-
workspaceProviders
|
|
44
|
-
? [...(embed.overrides ?? []), WorkspaceDriver.node.replace(WorkspaceDriver.registryNode(workspaceProviders))]
|
|
45
|
-
: embed.overrides,
|
|
46
|
-
selector ? (replacements) => SdkInstances.node(selector, replacements) : undefined,
|
|
47
|
-
).pipe(Layer.provide(HttpServer.layerServices), Layer.provideMerge(layer(log))),
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
return yield* Effect.gen(function* () {
|
|
51
|
-
const services = yield* runtime.contextEffect
|
|
52
|
-
const sdkPlugins = Context.get(services, SdkPlugins.Service)
|
|
53
|
-
for (const plugin of plugins ?? []) yield* sdkPlugins.register(plugin)
|
|
54
|
-
// The sweep is a no-op when nothing is suspended. ManagedRuntime owns the
|
|
55
|
-
// fiber so recovery never delays startup but still stops with the host.
|
|
56
|
-
runtime.runFork(Context.get(services, SessionRestart.Service).resumeSuspendedSessions)
|
|
57
|
-
const handler = HttpEffect.toWebHandlerWith<never, HttpServerRequest.HttpServerRequest | Scope.Scope>(
|
|
58
|
-
context(services),
|
|
59
|
-
)(Context.get(services, HttpRouter.HttpRouter).asHttpEffect())
|
|
60
|
-
const transport = OwnedFetch.make(handler, runtime.dispose)
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
runtime,
|
|
64
|
-
fetch: transport.fetch,
|
|
65
|
-
plugins: sdkPlugins,
|
|
66
|
-
sessions: Context.get(services, Session.Service),
|
|
67
|
-
workspace: Context.get(services, Workspace.Service),
|
|
68
|
-
close: transport.close,
|
|
69
|
-
}
|
|
70
|
-
}).pipe(Effect.onError(() => runtime.disposeEffect))
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
export type Interface = Effect.Success<ReturnType<typeof create>>
|
|
1
|
+
export * as EmbeddedHost from "./host"
|
|
2
|
+
|
|
3
|
+
import { SdkPlugins } from "@zyro-sec/core/plugin/sdk"
|
|
4
|
+
import type { Plugin } from "@zyro-sec/plugin/effect/plugin"
|
|
5
|
+
import { SessionRestart } from "@zyro-sec/core/session/execution/restart"
|
|
6
|
+
import { Session } from "@zyro-sec/core/session"
|
|
7
|
+
import { Workspace } from "@zyro-sec/core/workspace"
|
|
8
|
+
import { WorkspaceDriver } from "@zyro-sec/core/workspace/driver"
|
|
9
|
+
import { createEmbeddedRoutes } from "@zyro-sec/server/routes"
|
|
10
|
+
import type { ServerOptions } from "@zyro-sec/server/options"
|
|
11
|
+
import type { LayerNode } from "@zyro-sec/util/effect/layer-node"
|
|
12
|
+
import { Context, Effect, Layer, ManagedRuntime, Scope } from "effect"
|
|
13
|
+
import { HttpEffect, HttpRouter, HttpServer, HttpServerRequest } from "effect/http"
|
|
14
|
+
import { context, layer, type LogOptions } from "../logging"
|
|
15
|
+
import { OwnedFetch } from "./fetch"
|
|
16
|
+
import { SdkInstances } from "./instances"
|
|
17
|
+
|
|
18
|
+
export interface CreateOptions<R = never> extends Omit<ServerOptions, "hostname" | "port" | "password"> {
|
|
19
|
+
readonly log?: LogOptions
|
|
20
|
+
readonly plugins?: ReadonlyArray<Plugin>
|
|
21
|
+
readonly workspaceProviders?: Readonly<Record<string, WorkspaceDriver.Interface>>
|
|
22
|
+
readonly instances?: SdkInstances.Options<R>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Host hooks for embedding opencode on a non-default runtime profile. */
|
|
26
|
+
export interface EmbedOptions {
|
|
27
|
+
readonly overrides?: LayerNode.Replacements
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const create = Effect.fn("EmbeddedHost.create")(function* <R = never>(
|
|
31
|
+
options: CreateOptions<R> = {},
|
|
32
|
+
embed: EmbedOptions = {},
|
|
33
|
+
) {
|
|
34
|
+
const { log, plugins, workspaceProviders, instances, ...server } = options
|
|
35
|
+
const selector = instances ? SdkInstances.provide(instances, yield* Effect.context<R>()) : undefined
|
|
36
|
+
const runtime = ManagedRuntime.make(
|
|
37
|
+
createEmbeddedRoutes(
|
|
38
|
+
{
|
|
39
|
+
...server,
|
|
40
|
+
app: { ...server.app, name: server.app?.name ?? "sdk" },
|
|
41
|
+
database: { path: ":memory:", ...server.database },
|
|
42
|
+
},
|
|
43
|
+
workspaceProviders
|
|
44
|
+
? [...(embed.overrides ?? []), WorkspaceDriver.node.replace(WorkspaceDriver.registryNode(workspaceProviders))]
|
|
45
|
+
: embed.overrides,
|
|
46
|
+
selector ? (replacements) => SdkInstances.node(selector, replacements) : undefined,
|
|
47
|
+
).pipe(Layer.provide(HttpServer.layerServices), Layer.provideMerge(layer(log))),
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
return yield* Effect.gen(function* () {
|
|
51
|
+
const services = yield* runtime.contextEffect
|
|
52
|
+
const sdkPlugins = Context.get(services, SdkPlugins.Service)
|
|
53
|
+
for (const plugin of plugins ?? []) yield* sdkPlugins.register(plugin)
|
|
54
|
+
// The sweep is a no-op when nothing is suspended. ManagedRuntime owns the
|
|
55
|
+
// fiber so recovery never delays startup but still stops with the host.
|
|
56
|
+
runtime.runFork(Context.get(services, SessionRestart.Service).resumeSuspendedSessions)
|
|
57
|
+
const handler = HttpEffect.toWebHandlerWith<never, HttpServerRequest.HttpServerRequest | Scope.Scope>(
|
|
58
|
+
context(services),
|
|
59
|
+
)(Context.get(services, HttpRouter.HttpRouter).asHttpEffect())
|
|
60
|
+
const transport = OwnedFetch.make(handler, runtime.dispose)
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
runtime,
|
|
64
|
+
fetch: transport.fetch,
|
|
65
|
+
plugins: sdkPlugins,
|
|
66
|
+
sessions: Context.get(services, Session.Service),
|
|
67
|
+
workspace: Context.get(services, Workspace.Service),
|
|
68
|
+
close: transport.close,
|
|
69
|
+
}
|
|
70
|
+
}).pipe(Effect.onError(() => runtime.disposeEffect))
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
export type Interface = Effect.Success<ReturnType<typeof create>>
|