@zyro-sec/sdk 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/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
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
export * as SdkInstances from "./instances"
|
|
2
|
-
|
|
3
|
-
import { Instance } from "@zyro-sec/core/instance"
|
|
4
|
-
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
5
|
-
import { Plugin } from "@zyro-sec/core/plugin"
|
|
6
|
-
import type { InstancePlugins } from "@zyro-sec/core/plugin/instance"
|
|
7
|
-
import type { Session } from "@zyro-sec/schema/session"
|
|
8
|
-
import { makeGlobalNode } from "@zyro-sec/util/effect/app-node"
|
|
9
|
-
import type { LayerNode } from "@zyro-sec/util/effect/layer-node"
|
|
10
|
-
import { Context, Duration, Effect, Layer, LayerMap, RcMap, Scope } from "effect"
|
|
11
|
-
|
|
12
|
-
export interface Configuration {
|
|
13
|
-
readonly plugins: InstancePlugins.List
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface Options<R = never> {
|
|
17
|
-
/** Select a sharing key within the Session's current Location. Must not initialize plugins. */
|
|
18
|
-
readonly key: (session: Session.Info) => string
|
|
19
|
-
/**
|
|
20
|
-
* Reconstruct configuration on a cache miss. Resources belong to the instance Scope; other requirements
|
|
21
|
-
* are the services the SDK entrypoint was built with.
|
|
22
|
-
*/
|
|
23
|
-
readonly configure: (key: string) => Effect.Effect<Configuration, unknown, R | Scope.Scope>
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Closes `configure` over services captured where the SDK entrypoint was built; the host graph has none of its own. */
|
|
27
|
-
export function provide<R>(options: Options<R>, context: Context.Context<R>): Options {
|
|
28
|
-
return {
|
|
29
|
-
key: options.key,
|
|
30
|
-
// The ambient side wins the merge, so the instance Scope owns configured resources rather than the
|
|
31
|
-
// build Scope that was captured alongside the services.
|
|
32
|
-
configure: (key) =>
|
|
33
|
-
options
|
|
34
|
-
.configure(key)
|
|
35
|
-
.pipe(Effect.updateContext((ambient: Context.Context<Scope.Scope>) => Context.merge(context, ambient))),
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Replaces the host's `Instance.node`; `replacements` resolves lazily so instances inherit the final host graph. */
|
|
40
|
-
export function node(options: Options, replacements: () => LayerNode.Replacements) {
|
|
41
|
-
return makeGlobalNode({
|
|
42
|
-
service: Instance.Service,
|
|
43
|
-
layer: layer(options, replacements),
|
|
44
|
-
deps: [LocationServiceMap.node],
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function layer(options: Options, replacements: () => LayerNode.Replacements) {
|
|
49
|
-
return Layer.effect(
|
|
50
|
-
Instance.Service,
|
|
51
|
-
Effect.gen(function* () {
|
|
52
|
-
const scope = yield* Effect.scope
|
|
53
|
-
const locations = yield* LocationServiceMap.Service
|
|
54
|
-
const key = (session: Session.Info) => ({
|
|
55
|
-
key: options.key(session),
|
|
56
|
-
...LocationServiceMap.canonical(session.location),
|
|
57
|
-
})
|
|
58
|
-
const provide = (session: Session.Info) => Effect.provide(instances.get(key(session)))
|
|
59
|
-
const instances: LayerMap.LayerMap<
|
|
60
|
-
ReturnType<typeof key>,
|
|
61
|
-
Instance.Services,
|
|
62
|
-
Instance.Error
|
|
63
|
-
> = yield* LayerMap.make(
|
|
64
|
-
(input: ReturnType<typeof key>) =>
|
|
65
|
-
Layer.unwrap(
|
|
66
|
-
Effect.gen(function* () {
|
|
67
|
-
const configuration = yield* options.configure(input.key).pipe(Effect.orDie)
|
|
68
|
-
return Instance.layer(input, {
|
|
69
|
-
plugins: configuration.plugins,
|
|
70
|
-
replacements: [
|
|
71
|
-
...replacements(),
|
|
72
|
-
// Instances borrow this selector and the host's Location map instead of retaining
|
|
73
|
-
// their Layer scopes; retaining the selector would block its shutdown on its own entries.
|
|
74
|
-
Instance.node.replace(Layer.succeed(Instance.Service, { provide })),
|
|
75
|
-
LocationServiceMap.node.replace(Layer.succeed(LocationServiceMap.Service, locations)),
|
|
76
|
-
],
|
|
77
|
-
}).pipe(
|
|
78
|
-
Layer.tap((context) =>
|
|
79
|
-
Effect.gen(function* () {
|
|
80
|
-
const plugins = yield* Plugin.Service
|
|
81
|
-
yield* plugins.awaitActivation
|
|
82
|
-
// Covers setup failures and IDs colliding with host plugins; Core reports both in the inventory.
|
|
83
|
-
const failed = (yield* plugins.list()).filter(
|
|
84
|
-
(plugin) =>
|
|
85
|
-
plugin.state.status === "failed" &&
|
|
86
|
-
configuration.plugins.some((configured) => configured.id === plugin.id),
|
|
87
|
-
)
|
|
88
|
-
if (failed.length > 0)
|
|
89
|
-
yield* Effect.die(
|
|
90
|
-
new Error(`Instance plugin setup failed: ${failed.map((plugin) => plugin.id).join(", ")}`),
|
|
91
|
-
)
|
|
92
|
-
}).pipe(Effect.provide(context)),
|
|
93
|
-
),
|
|
94
|
-
)
|
|
95
|
-
}),
|
|
96
|
-
).pipe(
|
|
97
|
-
// Eviction can close the lookup's scope; do not make that fiber wait on itself.
|
|
98
|
-
Layer.tapCause(() =>
|
|
99
|
-
instances.invalidate(input).pipe(Effect.forkIn(scope, { startImmediately: true }), Effect.asVoid),
|
|
100
|
-
),
|
|
101
|
-
),
|
|
102
|
-
{ idleTimeToLive: Duration.infinity },
|
|
103
|
-
)
|
|
104
|
-
return Instance.Service.of({ provide, live: (session) => RcMap.has(instances.rcMap, key(session)) })
|
|
105
|
-
}),
|
|
106
|
-
)
|
|
107
|
-
}
|
|
1
|
+
export * as SdkInstances from "./instances"
|
|
2
|
+
|
|
3
|
+
import { Instance } from "@zyro-sec/core/instance"
|
|
4
|
+
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
5
|
+
import { Plugin } from "@zyro-sec/core/plugin"
|
|
6
|
+
import type { InstancePlugins } from "@zyro-sec/core/plugin/instance"
|
|
7
|
+
import type { Session } from "@zyro-sec/schema/session"
|
|
8
|
+
import { makeGlobalNode } from "@zyro-sec/util/effect/app-node"
|
|
9
|
+
import type { LayerNode } from "@zyro-sec/util/effect/layer-node"
|
|
10
|
+
import { Context, Duration, Effect, Layer, LayerMap, RcMap, Scope } from "effect"
|
|
11
|
+
|
|
12
|
+
export interface Configuration {
|
|
13
|
+
readonly plugins: InstancePlugins.List
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface Options<R = never> {
|
|
17
|
+
/** Select a sharing key within the Session's current Location. Must not initialize plugins. */
|
|
18
|
+
readonly key: (session: Session.Info) => string
|
|
19
|
+
/**
|
|
20
|
+
* Reconstruct configuration on a cache miss. Resources belong to the instance Scope; other requirements
|
|
21
|
+
* are the services the SDK entrypoint was built with.
|
|
22
|
+
*/
|
|
23
|
+
readonly configure: (key: string) => Effect.Effect<Configuration, unknown, R | Scope.Scope>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Closes `configure` over services captured where the SDK entrypoint was built; the host graph has none of its own. */
|
|
27
|
+
export function provide<R>(options: Options<R>, context: Context.Context<R>): Options {
|
|
28
|
+
return {
|
|
29
|
+
key: options.key,
|
|
30
|
+
// The ambient side wins the merge, so the instance Scope owns configured resources rather than the
|
|
31
|
+
// build Scope that was captured alongside the services.
|
|
32
|
+
configure: (key) =>
|
|
33
|
+
options
|
|
34
|
+
.configure(key)
|
|
35
|
+
.pipe(Effect.updateContext((ambient: Context.Context<Scope.Scope>) => Context.merge(context, ambient))),
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Replaces the host's `Instance.node`; `replacements` resolves lazily so instances inherit the final host graph. */
|
|
40
|
+
export function node(options: Options, replacements: () => LayerNode.Replacements) {
|
|
41
|
+
return makeGlobalNode({
|
|
42
|
+
service: Instance.Service,
|
|
43
|
+
layer: layer(options, replacements),
|
|
44
|
+
deps: [LocationServiceMap.node],
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function layer(options: Options, replacements: () => LayerNode.Replacements) {
|
|
49
|
+
return Layer.effect(
|
|
50
|
+
Instance.Service,
|
|
51
|
+
Effect.gen(function* () {
|
|
52
|
+
const scope = yield* Effect.scope
|
|
53
|
+
const locations = yield* LocationServiceMap.Service
|
|
54
|
+
const key = (session: Session.Info) => ({
|
|
55
|
+
key: options.key(session),
|
|
56
|
+
...LocationServiceMap.canonical(session.location),
|
|
57
|
+
})
|
|
58
|
+
const provide = (session: Session.Info) => Effect.provide(instances.get(key(session)))
|
|
59
|
+
const instances: LayerMap.LayerMap<
|
|
60
|
+
ReturnType<typeof key>,
|
|
61
|
+
Instance.Services,
|
|
62
|
+
Instance.Error
|
|
63
|
+
> = yield* LayerMap.make(
|
|
64
|
+
(input: ReturnType<typeof key>) =>
|
|
65
|
+
Layer.unwrap(
|
|
66
|
+
Effect.gen(function* () {
|
|
67
|
+
const configuration = yield* options.configure(input.key).pipe(Effect.orDie)
|
|
68
|
+
return Instance.layer(input, {
|
|
69
|
+
plugins: configuration.plugins,
|
|
70
|
+
replacements: [
|
|
71
|
+
...replacements(),
|
|
72
|
+
// Instances borrow this selector and the host's Location map instead of retaining
|
|
73
|
+
// their Layer scopes; retaining the selector would block its shutdown on its own entries.
|
|
74
|
+
Instance.node.replace(Layer.succeed(Instance.Service, { provide })),
|
|
75
|
+
LocationServiceMap.node.replace(Layer.succeed(LocationServiceMap.Service, locations)),
|
|
76
|
+
],
|
|
77
|
+
}).pipe(
|
|
78
|
+
Layer.tap((context) =>
|
|
79
|
+
Effect.gen(function* () {
|
|
80
|
+
const plugins = yield* Plugin.Service
|
|
81
|
+
yield* plugins.awaitActivation
|
|
82
|
+
// Covers setup failures and IDs colliding with host plugins; Core reports both in the inventory.
|
|
83
|
+
const failed = (yield* plugins.list()).filter(
|
|
84
|
+
(plugin) =>
|
|
85
|
+
plugin.state.status === "failed" &&
|
|
86
|
+
configuration.plugins.some((configured) => configured.id === plugin.id),
|
|
87
|
+
)
|
|
88
|
+
if (failed.length > 0)
|
|
89
|
+
yield* Effect.die(
|
|
90
|
+
new Error(`Instance plugin setup failed: ${failed.map((plugin) => plugin.id).join(", ")}`),
|
|
91
|
+
)
|
|
92
|
+
}).pipe(Effect.provide(context)),
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
}),
|
|
96
|
+
).pipe(
|
|
97
|
+
// Eviction can close the lookup's scope; do not make that fiber wait on itself.
|
|
98
|
+
Layer.tapCause(() =>
|
|
99
|
+
instances.invalidate(input).pipe(Effect.forkIn(scope, { startImmediately: true }), Effect.asVoid),
|
|
100
|
+
),
|
|
101
|
+
),
|
|
102
|
+
{ idleTimeToLive: Duration.infinity },
|
|
103
|
+
)
|
|
104
|
+
return Instance.Service.of({ provide, live: (session) => RcMap.has(instances.rcMap, key(session)) })
|
|
105
|
+
}),
|
|
106
|
+
)
|
|
107
|
+
}
|
package/src/internal/workerd.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
export * as WorkerdProfile from "./workerd"
|
|
2
|
-
|
|
3
|
-
import type { Config } from "@zyro-sec/schema/config"
|
|
4
|
-
import { ServerWorkerd } from "@zyro-sec/server/workerd"
|
|
5
|
-
|
|
6
|
-
export type Configuration = Omit<typeof Config.Info.Encoded, "plugins">
|
|
7
|
-
|
|
8
|
-
export interface Options extends Omit<ServerWorkerd.Options, "password" | "config"> {
|
|
9
|
-
readonly config?: Configuration
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function make({ config, ...options }: Options) {
|
|
13
|
-
const server = {
|
|
14
|
-
...options,
|
|
15
|
-
config: config === undefined ? undefined : { content: JSON.stringify(config) },
|
|
16
|
-
}
|
|
17
|
-
return {
|
|
18
|
-
options: ServerWorkerd.serverOptions(server),
|
|
19
|
-
replacements: ServerWorkerd.replacements(server),
|
|
20
|
-
}
|
|
21
|
-
}
|
|
1
|
+
export * as WorkerdProfile from "./workerd"
|
|
2
|
+
|
|
3
|
+
import type { Config } from "@zyro-sec/schema/config"
|
|
4
|
+
import { ServerWorkerd } from "@zyro-sec/server/workerd"
|
|
5
|
+
|
|
6
|
+
export type Configuration = Omit<typeof Config.Info.Encoded, "plugins">
|
|
7
|
+
|
|
8
|
+
export interface Options extends Omit<ServerWorkerd.Options, "password" | "config"> {
|
|
9
|
+
readonly config?: Configuration
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function make({ config, ...options }: Options) {
|
|
13
|
+
const server = {
|
|
14
|
+
...options,
|
|
15
|
+
config: config === undefined ? undefined : { content: JSON.stringify(config) },
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
options: ServerWorkerd.serverOptions(server),
|
|
19
|
+
replacements: ServerWorkerd.replacements(server),
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/logging.ts
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
import { Context, Formatter, Layer, Logger, References } from "effect"
|
|
2
|
-
|
|
3
|
-
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"
|
|
4
|
-
|
|
5
|
-
export type LogEntry = {
|
|
6
|
-
readonly level: LogLevel
|
|
7
|
-
readonly message: string
|
|
8
|
-
readonly attributes?: Readonly<Record<string, unknown>>
|
|
9
|
-
readonly cause?: unknown
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type LogWriter = (entry: LogEntry) => void
|
|
13
|
-
|
|
14
|
-
export type LogOptions = {
|
|
15
|
-
readonly level?: LogLevel
|
|
16
|
-
readonly emit: LogWriter
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const levels: Record<LogLevel, Logger.Options<unknown>["logLevel"]> = {
|
|
20
|
-
trace: "Trace",
|
|
21
|
-
debug: "Debug",
|
|
22
|
-
info: "Info",
|
|
23
|
-
warn: "Warn",
|
|
24
|
-
error: "Error",
|
|
25
|
-
fatal: "Fatal",
|
|
26
|
-
}
|
|
27
|
-
const levelNames = new Map<Logger.Options<unknown>["logLevel"], LogLevel>([
|
|
28
|
-
[levels.trace, "trace"],
|
|
29
|
-
[levels.debug, "debug"],
|
|
30
|
-
[levels.info, "info"],
|
|
31
|
-
[levels.warn, "warn"],
|
|
32
|
-
[levels.error, "error"],
|
|
33
|
-
[levels.fatal, "fatal"],
|
|
34
|
-
])
|
|
35
|
-
|
|
36
|
-
export function layer(log?: LogOptions) {
|
|
37
|
-
const logger = Logger.make((options) => {
|
|
38
|
-
if (!log) return
|
|
39
|
-
const level = levelNames.get(options.logLevel)
|
|
40
|
-
if (!level) return
|
|
41
|
-
const entry = Logger.formatStructured.log(options)
|
|
42
|
-
const values = Array.isArray(entry.message) ? entry.message : [entry.message]
|
|
43
|
-
const [message, ...data] = values
|
|
44
|
-
const details =
|
|
45
|
-
data.length === 1 && typeof data[0] === "object" && data[0] !== null && !Array.isArray(data[0])
|
|
46
|
-
? (data[0] as Readonly<Record<string, unknown>>)
|
|
47
|
-
: undefined
|
|
48
|
-
const { cause: detailCause, ...detailAttributes } = details ?? {}
|
|
49
|
-
const attributes = {
|
|
50
|
-
...entry.annotations,
|
|
51
|
-
...detailAttributes,
|
|
52
|
-
...(Object.keys(entry.spans).length > 0 ? { spans: entry.spans } : {}),
|
|
53
|
-
...(!details && data.length > 0 ? { data: data.length === 1 ? data[0] : data } : {}),
|
|
54
|
-
}
|
|
55
|
-
try {
|
|
56
|
-
log.emit({
|
|
57
|
-
level,
|
|
58
|
-
message: typeof message === "string" ? message : Formatter.format(message),
|
|
59
|
-
...(Object.keys(attributes).length > 0 ? { attributes } : {}),
|
|
60
|
-
...(entry.cause === undefined && detailCause === undefined ? {} : { cause: entry.cause ?? detailCause }),
|
|
61
|
-
})
|
|
62
|
-
} catch {
|
|
63
|
-
// A host logger must not break OpenCode operations.
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
return Layer.merge(
|
|
67
|
-
Logger.layer([logger], { mergeWithExisting: false }),
|
|
68
|
-
Layer.succeed(References.MinimumLogLevel, levels[log?.level ?? "info"]),
|
|
69
|
-
)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function context(source: Context.Context<never>) {
|
|
73
|
-
return Context.make(Logger.CurrentLoggers, Context.get(source, Logger.CurrentLoggers)).pipe(
|
|
74
|
-
Context.add(References.MinimumLogLevel, Context.get(source, References.MinimumLogLevel)),
|
|
75
|
-
)
|
|
76
|
-
}
|
|
1
|
+
import { Context, Formatter, Layer, Logger, References } from "effect"
|
|
2
|
+
|
|
3
|
+
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"
|
|
4
|
+
|
|
5
|
+
export type LogEntry = {
|
|
6
|
+
readonly level: LogLevel
|
|
7
|
+
readonly message: string
|
|
8
|
+
readonly attributes?: Readonly<Record<string, unknown>>
|
|
9
|
+
readonly cause?: unknown
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type LogWriter = (entry: LogEntry) => void
|
|
13
|
+
|
|
14
|
+
export type LogOptions = {
|
|
15
|
+
readonly level?: LogLevel
|
|
16
|
+
readonly emit: LogWriter
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const levels: Record<LogLevel, Logger.Options<unknown>["logLevel"]> = {
|
|
20
|
+
trace: "Trace",
|
|
21
|
+
debug: "Debug",
|
|
22
|
+
info: "Info",
|
|
23
|
+
warn: "Warn",
|
|
24
|
+
error: "Error",
|
|
25
|
+
fatal: "Fatal",
|
|
26
|
+
}
|
|
27
|
+
const levelNames = new Map<Logger.Options<unknown>["logLevel"], LogLevel>([
|
|
28
|
+
[levels.trace, "trace"],
|
|
29
|
+
[levels.debug, "debug"],
|
|
30
|
+
[levels.info, "info"],
|
|
31
|
+
[levels.warn, "warn"],
|
|
32
|
+
[levels.error, "error"],
|
|
33
|
+
[levels.fatal, "fatal"],
|
|
34
|
+
])
|
|
35
|
+
|
|
36
|
+
export function layer(log?: LogOptions) {
|
|
37
|
+
const logger = Logger.make((options) => {
|
|
38
|
+
if (!log) return
|
|
39
|
+
const level = levelNames.get(options.logLevel)
|
|
40
|
+
if (!level) return
|
|
41
|
+
const entry = Logger.formatStructured.log(options)
|
|
42
|
+
const values = Array.isArray(entry.message) ? entry.message : [entry.message]
|
|
43
|
+
const [message, ...data] = values
|
|
44
|
+
const details =
|
|
45
|
+
data.length === 1 && typeof data[0] === "object" && data[0] !== null && !Array.isArray(data[0])
|
|
46
|
+
? (data[0] as Readonly<Record<string, unknown>>)
|
|
47
|
+
: undefined
|
|
48
|
+
const { cause: detailCause, ...detailAttributes } = details ?? {}
|
|
49
|
+
const attributes = {
|
|
50
|
+
...entry.annotations,
|
|
51
|
+
...detailAttributes,
|
|
52
|
+
...(Object.keys(entry.spans).length > 0 ? { spans: entry.spans } : {}),
|
|
53
|
+
...(!details && data.length > 0 ? { data: data.length === 1 ? data[0] : data } : {}),
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
log.emit({
|
|
57
|
+
level,
|
|
58
|
+
message: typeof message === "string" ? message : Formatter.format(message),
|
|
59
|
+
...(Object.keys(attributes).length > 0 ? { attributes } : {}),
|
|
60
|
+
...(entry.cause === undefined && detailCause === undefined ? {} : { cause: entry.cause ?? detailCause }),
|
|
61
|
+
})
|
|
62
|
+
} catch {
|
|
63
|
+
// A host logger must not break OpenCode operations.
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
return Layer.merge(
|
|
67
|
+
Logger.layer([logger], { mergeWithExisting: false }),
|
|
68
|
+
Layer.succeed(References.MinimumLogLevel, levels[log?.level ?? "info"]),
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function context(source: Context.Context<never>) {
|
|
73
|
+
return Context.make(Logger.CurrentLoggers, Context.get(source, Logger.CurrentLoggers)).pipe(
|
|
74
|
+
Context.add(References.MinimumLogLevel, Context.get(source, References.MinimumLogLevel)),
|
|
75
|
+
)
|
|
76
|
+
}
|
package/src/opencode.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { PromiseSdk } from "./promise"
|
|
2
|
-
|
|
3
|
-
export type { LogEntry, LogLevel, LogOptions, LogWriter } from "./logging"
|
|
4
|
-
|
|
5
|
-
export type CreateOptions = PromiseSdk.CreateOptions
|
|
6
|
-
export type InstanceOptions = PromiseSdk.InstanceOptions
|
|
7
|
-
export type InstanceConfiguration = PromiseSdk.InstanceConfiguration
|
|
8
|
-
export type Interface = PromiseSdk.Interface
|
|
9
|
-
|
|
10
|
-
export const create = (options: CreateOptions = {}) => PromiseSdk.create(options)
|
|
1
|
+
import { PromiseSdk } from "./promise"
|
|
2
|
+
|
|
3
|
+
export type { LogEntry, LogLevel, LogOptions, LogWriter } from "./logging"
|
|
4
|
+
|
|
5
|
+
export type CreateOptions = PromiseSdk.CreateOptions
|
|
6
|
+
export type InstanceOptions = PromiseSdk.InstanceOptions
|
|
7
|
+
export type InstanceConfiguration = PromiseSdk.InstanceConfiguration
|
|
8
|
+
export type Interface = PromiseSdk.Interface
|
|
9
|
+
|
|
10
|
+
export const create = (options: CreateOptions = {}) => PromiseSdk.create(options)
|
package/src/promise.ts
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
export * as PromiseSdk from "./promise"
|
|
2
|
-
|
|
3
|
-
import { OpenCode, type OpenCodeClient } from "@zyro-sec/client"
|
|
4
|
-
import type { Plugin } from "@zyro-sec/plugin"
|
|
5
|
-
import { Session } from "@zyro-sec/schema/session"
|
|
6
|
-
import { Effect, Schema } from "effect"
|
|
7
|
-
import { EmbeddedHost } from "./internal/host"
|
|
8
|
-
|
|
9
|
-
export interface InstanceConfiguration {
|
|
10
|
-
readonly plugins: ReadonlyArray<Plugin.Plugin>
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface InstanceOptions {
|
|
14
|
-
/** Select a sharing key within the Session's current Location. Must not initialize plugins. */
|
|
15
|
-
readonly key: (session: typeof Session.Info.Encoded) => string
|
|
16
|
-
/** Reconstruct configuration on a cache miss, including after a host restart. */
|
|
17
|
-
readonly configure: (key: string) => InstanceConfiguration | Promise<InstanceConfiguration>
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface CreateOptions
|
|
21
|
-
extends Omit<EmbeddedHost.CreateOptions, "plugins" | "workspaceProviders" | "instances"> {
|
|
22
|
-
readonly plugins?: ReadonlyArray<Plugin.Plugin>
|
|
23
|
-
readonly instances?: InstanceOptions
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type Interface = Omit<OpenCodeClient, "plugin"> & {
|
|
27
|
-
readonly sessions: OpenCodeClient["session"]
|
|
28
|
-
readonly events: OpenCodeClient["event"]
|
|
29
|
-
readonly plugin: ((plugin: Plugin.Plugin) => Promise<void>) & OpenCodeClient["plugin"]
|
|
30
|
-
readonly close: () => Promise<void>
|
|
31
|
-
readonly [Symbol.asyncDispose]: () => Promise<void>
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export async function createWithHost(
|
|
35
|
-
options: CreateOptions = {},
|
|
36
|
-
embed: EmbeddedHost.EmbedOptions = {},
|
|
37
|
-
): Promise<{ readonly sdk: Interface; readonly host: EmbeddedHost.Interface }> {
|
|
38
|
-
const { plugins, instances, ...hostOptions } = options
|
|
39
|
-
const host = await Effect.runPromise(
|
|
40
|
-
EmbeddedHost.create(
|
|
41
|
-
{
|
|
42
|
-
...hostOptions,
|
|
43
|
-
plugins: await adaptPlugins(plugins),
|
|
44
|
-
instances: instances
|
|
45
|
-
? {
|
|
46
|
-
key: (session) => instances.key(Schema.encodeSync(Session.Info)(session)),
|
|
47
|
-
configure: (key) =>
|
|
48
|
-
Effect.gen(function* () {
|
|
49
|
-
const { PluginPromise } = yield* Effect.promise(() => import("@zyro-sec/core/plugin/promise"))
|
|
50
|
-
const configuration = yield* Effect.tryPromise(async () => instances.configure(key))
|
|
51
|
-
return { plugins: configuration.plugins.map(PluginPromise.fromPromise) }
|
|
52
|
-
}),
|
|
53
|
-
}
|
|
54
|
-
: undefined,
|
|
55
|
-
},
|
|
56
|
-
embed,
|
|
57
|
-
),
|
|
58
|
-
)
|
|
59
|
-
const client = OpenCode.make({ baseUrl: "http://opencode.local", fetch: host.fetch })
|
|
60
|
-
const register = async (plugin: Plugin.Plugin) => {
|
|
61
|
-
const { PluginPromise } = await import("@zyro-sec/core/plugin/promise")
|
|
62
|
-
return host.runtime.runPromise(host.plugins.register(PluginPromise.fromPromise(plugin)))
|
|
63
|
-
}
|
|
64
|
-
return {
|
|
65
|
-
host,
|
|
66
|
-
sdk: {
|
|
67
|
-
...client,
|
|
68
|
-
sessions: client.session,
|
|
69
|
-
events: client.event,
|
|
70
|
-
plugin: Object.assign(register, client.plugin),
|
|
71
|
-
close: host.close,
|
|
72
|
-
[Symbol.asyncDispose]: host.close,
|
|
73
|
-
},
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export async function create(options: CreateOptions = {}): Promise<Interface> {
|
|
78
|
-
return (await createWithHost(options)).sdk
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
async function adaptPlugins(plugins: ReadonlyArray<Plugin.Plugin> | undefined) {
|
|
82
|
-
if (!plugins?.length) return undefined
|
|
83
|
-
const { PluginPromise } = await import("@zyro-sec/core/plugin/promise")
|
|
84
|
-
return plugins.map(PluginPromise.fromPromise)
|
|
85
|
-
}
|
|
1
|
+
export * as PromiseSdk from "./promise"
|
|
2
|
+
|
|
3
|
+
import { OpenCode, type OpenCodeClient } from "@zyro-sec/client"
|
|
4
|
+
import type { Plugin } from "@zyro-sec/plugin"
|
|
5
|
+
import { Session } from "@zyro-sec/schema/session"
|
|
6
|
+
import { Effect, Schema } from "effect"
|
|
7
|
+
import { EmbeddedHost } from "./internal/host"
|
|
8
|
+
|
|
9
|
+
export interface InstanceConfiguration {
|
|
10
|
+
readonly plugins: ReadonlyArray<Plugin.Plugin>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface InstanceOptions {
|
|
14
|
+
/** Select a sharing key within the Session's current Location. Must not initialize plugins. */
|
|
15
|
+
readonly key: (session: typeof Session.Info.Encoded) => string
|
|
16
|
+
/** Reconstruct configuration on a cache miss, including after a host restart. */
|
|
17
|
+
readonly configure: (key: string) => InstanceConfiguration | Promise<InstanceConfiguration>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface CreateOptions
|
|
21
|
+
extends Omit<EmbeddedHost.CreateOptions, "plugins" | "workspaceProviders" | "instances"> {
|
|
22
|
+
readonly plugins?: ReadonlyArray<Plugin.Plugin>
|
|
23
|
+
readonly instances?: InstanceOptions
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type Interface = Omit<OpenCodeClient, "plugin"> & {
|
|
27
|
+
readonly sessions: OpenCodeClient["session"]
|
|
28
|
+
readonly events: OpenCodeClient["event"]
|
|
29
|
+
readonly plugin: ((plugin: Plugin.Plugin) => Promise<void>) & OpenCodeClient["plugin"]
|
|
30
|
+
readonly close: () => Promise<void>
|
|
31
|
+
readonly [Symbol.asyncDispose]: () => Promise<void>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function createWithHost(
|
|
35
|
+
options: CreateOptions = {},
|
|
36
|
+
embed: EmbeddedHost.EmbedOptions = {},
|
|
37
|
+
): Promise<{ readonly sdk: Interface; readonly host: EmbeddedHost.Interface }> {
|
|
38
|
+
const { plugins, instances, ...hostOptions } = options
|
|
39
|
+
const host = await Effect.runPromise(
|
|
40
|
+
EmbeddedHost.create(
|
|
41
|
+
{
|
|
42
|
+
...hostOptions,
|
|
43
|
+
plugins: await adaptPlugins(plugins),
|
|
44
|
+
instances: instances
|
|
45
|
+
? {
|
|
46
|
+
key: (session) => instances.key(Schema.encodeSync(Session.Info)(session)),
|
|
47
|
+
configure: (key) =>
|
|
48
|
+
Effect.gen(function* () {
|
|
49
|
+
const { PluginPromise } = yield* Effect.promise(() => import("@zyro-sec/core/plugin/promise"))
|
|
50
|
+
const configuration = yield* Effect.tryPromise(async () => instances.configure(key))
|
|
51
|
+
return { plugins: configuration.plugins.map(PluginPromise.fromPromise) }
|
|
52
|
+
}),
|
|
53
|
+
}
|
|
54
|
+
: undefined,
|
|
55
|
+
},
|
|
56
|
+
embed,
|
|
57
|
+
),
|
|
58
|
+
)
|
|
59
|
+
const client = OpenCode.make({ baseUrl: "http://opencode.local", fetch: host.fetch })
|
|
60
|
+
const register = async (plugin: Plugin.Plugin) => {
|
|
61
|
+
const { PluginPromise } = await import("@zyro-sec/core/plugin/promise")
|
|
62
|
+
return host.runtime.runPromise(host.plugins.register(PluginPromise.fromPromise(plugin)))
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
host,
|
|
66
|
+
sdk: {
|
|
67
|
+
...client,
|
|
68
|
+
sessions: client.session,
|
|
69
|
+
events: client.event,
|
|
70
|
+
plugin: Object.assign(register, client.plugin),
|
|
71
|
+
close: host.close,
|
|
72
|
+
[Symbol.asyncDispose]: host.close,
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function create(options: CreateOptions = {}): Promise<Interface> {
|
|
78
|
+
return (await createWithHost(options)).sdk
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function adaptPlugins(plugins: ReadonlyArray<Plugin.Plugin> | undefined) {
|
|
82
|
+
if (!plugins?.length) return undefined
|
|
83
|
+
const { PluginPromise } = await import("@zyro-sec/core/plugin/promise")
|
|
84
|
+
return plugins.map(PluginPromise.fromPromise)
|
|
85
|
+
}
|
package/src/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 { ToolContext as Context, Info } from "@zyro-sec/plugin/promise/tool"
|
|
1
|
+
export { RegistrationError } from "@zyro-sec/core/tool"
|
|
2
|
+
export { Error } from "@zyro-sec/schema/tool"
|
|
3
|
+
export type { ToolContext as Context, Info } from "@zyro-sec/plugin/promise/tool"
|