GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — opencode/packages/opencode/src/effect/bridge.ts at dev · ildella/opencode Expand file tree/ Copy pathbridge.tsMore file actions 84 lines (76 loc) · 3.44 KB / Copy pathbridge.tsFile metadata and controls84 lines (76 loc) · 3.44 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 import { Context, Effect, Exit, Fiber } from "effect" import { WorkspaceContext } from "@/control-plane/workspace-context" import type { WorkspaceV2 } from "@opencode-ai/core/workspace" import { InstanceRef, WorkspaceRef } from "./instance-ref" import { attachWith } from "./run-service" export interface Shape { readonly promise: <A, E, R>(effect: Effect.Effect<A, E, R>) => Promise<A> readonly fork: <A, E, R>(effect: Effect.Effect<A, E, R>) => Fiber.Fiber<A, E> readonly run: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E> readonly bind: <Args extends readonly unknown[], Result>(fn: (...args: Args) => Result) => (...args: Args) => Result } function restoreWorkspace<R>(workspace: WorkspaceV2.ID | undefined, fn: () => R): R { if (workspace !== undefined) return WorkspaceContext.restore(workspace, fn) return fn() } function captureSync() { const fiber = Fiber.getCurrent() const instance = fiber ? Context.getReferenceUnsafe(fiber.context, InstanceRef) : undefined const workspace = (fiber ? Context.getReferenceUnsafe(fiber.context, WorkspaceRef) : undefined) ?? WorkspaceContext.workspaceID return { instance, workspace } } export const bind = <Args extends readonly unknown[], Result>(fn: (...args: Args) => Result) => { const captured = captureSync() return (...args: Args) => restoreWorkspace(captured.workspace, () => Effect.runSync( attachWith( Effect.sync(() => fn(...args)), captured, ), ), ) } /** * Bridge from Effect into a Promise-returning JS callback while preserving * `WorkspaceContext` AsyncLocalStorage for callback code that still reads it. * `InstanceRef` is captured for effects run through the returned bridge APIs; * plain JS callbacks that need it should receive the ref explicitly. * * Mirrors `Effect.promise` but restores workspace ALS first. */ export const fromPromise = <T>(fn: () => Promise<T> | T): Effect.Effect<T> => Effect.gen(function* () { const workspace = yield* WorkspaceRef return yield* Effect.promise(() => Promise.resolve(restoreWorkspace(workspace, () => fn()))) }) export function make(): Effect.Effect<Shape> { return Effect.gen(function* () { const ctx = yield* Effect.context() const captured = captureSync() const instance = (yield* InstanceRef) ?? captured.instance const workspace = (yield* WorkspaceRef) ?? captured.workspace const wrap = <A, E, R>(effect: Effect.Effect<A, E, R>) => attachWith(effect.pipe(Effect.provide(ctx)) as Effect.Effect<A, E, never>, { instance, workspace }) return { promise: <A, E, R>(effect: Effect.Effect<A, E, R>) => restoreWorkspace(workspace, () => Effect.runPromise(wrap(effect))), fork: <A, E, R>(effect: Effect.Effect<A, E, R>) => restoreWorkspace(workspace, () => Effect.runFork(wrap(effect))), run: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.callback<A, E>((resume) => { restoreWorkspace(workspace, () => Effect.runPromiseExit(wrap(effect)).then((exit) => resume(Exit.isSuccess(exit) ? Effect.succeed(exit.value) : Effect.failCause(exit.cause)), ), ) }), bind: <Args extends readonly unknown[], Result>(fn: (...args: Args) => Result) => (...args: Args) => restoreWorkspace(workspace, () => Effect.runSync(wrap(Effect.sync(() => fn(...args))))), } satisfies Shape }) } export * as EffectBridge from "./bridge" |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |