Moves effect logging, observability, runtime utilities, flags, installation version info, and process utilities from opencode to core package. This enables better code sharing across packages and establishes core as the single source of truth for foundational utilities. All internal imports updated to use @opencode-ai/core paths for consistency.
53 lines
2.3 KiB
TypeScript
53 lines
2.3 KiB
TypeScript
import { Effect, Layer, ManagedRuntime } from "effect"
|
|
import * as Context from "effect/Context"
|
|
import { Instance } from "@/project/instance"
|
|
import { LocalContext } from "@/util"
|
|
import { InstanceRef, WorkspaceRef } from "./instance-ref"
|
|
import * as Observability from "@opencode-ai/core/effect/observability"
|
|
import { WorkspaceContext } from "@/control-plane/workspace-context"
|
|
import type { InstanceContext } from "@/project/instance"
|
|
import { memoMap } from "@opencode-ai/core/effect/memo-map"
|
|
|
|
type Refs = {
|
|
instance?: InstanceContext
|
|
workspace?: string
|
|
}
|
|
|
|
export function attachWith<A, E, R>(effect: Effect.Effect<A, E, R>, refs: Refs): Effect.Effect<A, E, R> {
|
|
if (!refs.instance && !refs.workspace) return effect
|
|
if (!refs.instance) return effect.pipe(Effect.provideService(WorkspaceRef, refs.workspace))
|
|
if (!refs.workspace) return effect.pipe(Effect.provideService(InstanceRef, refs.instance))
|
|
return effect.pipe(
|
|
Effect.provideService(InstanceRef, refs.instance),
|
|
Effect.provideService(WorkspaceRef, refs.workspace),
|
|
)
|
|
}
|
|
|
|
export function attach<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> {
|
|
try {
|
|
return attachWith(effect, {
|
|
instance: Instance.current,
|
|
workspace: WorkspaceContext.workspaceID,
|
|
})
|
|
} catch (err) {
|
|
if (!(err instanceof LocalContext.NotFound)) throw err
|
|
}
|
|
return effect
|
|
}
|
|
|
|
export function makeRuntime<I, S, E>(service: Context.Service<I, S>, layer: Layer.Layer<I, E>) {
|
|
let rt: ManagedRuntime.ManagedRuntime<I, E> | undefined
|
|
const getRuntime = () => (rt ??= ManagedRuntime.make(Layer.provideMerge(layer, Observability.layer), { memoMap }))
|
|
|
|
return {
|
|
runSync: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runSync(attach(service.use(fn))),
|
|
runPromiseExit: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>, options?: Effect.RunOptions) =>
|
|
getRuntime().runPromiseExit(attach(service.use(fn)), options),
|
|
runPromise: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>, options?: Effect.RunOptions) =>
|
|
getRuntime().runPromise(attach(service.use(fn)), options),
|
|
runFork: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runFork(attach(service.use(fn))),
|
|
runCallback: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) =>
|
|
getRuntime().runCallback(attach(service.use(fn))),
|
|
}
|
|
}
|