mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-21 11:26:39 +00:00
refactor: simplify instance store wiring
This commit is contained in:
@@ -11,7 +11,10 @@ export const Instance = {
|
||||
return InstanceStore.runtime.runPromise((store) => store.load(input))
|
||||
},
|
||||
async provide<R>(input: { directory: string; init?: () => Promise<any>; fn: () => R }): Promise<R> {
|
||||
return context.provide(await Instance.load(input), async () => input.fn())
|
||||
return context.provide(
|
||||
await Instance.load({ directory: input.directory, init: input.init }),
|
||||
async () => input.fn(),
|
||||
)
|
||||
},
|
||||
get current() {
|
||||
return context.use()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Config } from "@/config/config"
|
||||
import { GlobalBus, type GlobalEvent as GlobalBusEvent } from "@/bus/global"
|
||||
import { Installation } from "@/installation"
|
||||
import { Instance } from "@/project/instance"
|
||||
import { InstanceStore } from "@/project/instance-store"
|
||||
import { InstallationVersion } from "@opencode-ai/core/installation/version"
|
||||
import * as Log from "@opencode-ai/core/util/log"
|
||||
import { Effect, Queue, Schema } from "effect"
|
||||
@@ -68,6 +68,7 @@ export const globalHandlers = HttpApiBuilder.group(RootHttpApi, "global", (handl
|
||||
Effect.gen(function* () {
|
||||
const config = yield* Config.Service
|
||||
const installation = yield* Installation.Service
|
||||
const store = yield* InstanceStore.Service
|
||||
|
||||
const health = Effect.fn("GlobalHttpApi.health")(function* () {
|
||||
return { healthy: true as const, version: InstallationVersion }
|
||||
@@ -86,7 +87,7 @@ export const globalHandlers = HttpApiBuilder.group(RootHttpApi, "global", (handl
|
||||
})
|
||||
|
||||
const dispose = Effect.fn("GlobalHttpApi.dispose")(function* () {
|
||||
yield* Effect.promise(() => Instance.disposeAll())
|
||||
yield* store.disposeAll()
|
||||
GlobalBus.emit("event", {
|
||||
directory: "global",
|
||||
payload: { type: "global.disposed", properties: {} },
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import type { WorkspaceID } from "@/control-plane/schema"
|
||||
import { WorkspaceContext } from "@/control-plane/workspace-context"
|
||||
import { WorkspaceRef } from "@/effect/instance-ref"
|
||||
import { Instance, type InstanceContext } from "@/project/instance"
|
||||
import { EffectBridge } from "@/effect/bridge"
|
||||
import type { InstanceContext } from "@/project/instance"
|
||||
import { InstanceStore } from "@/project/instance-store"
|
||||
import { Effect } from "effect"
|
||||
import { HttpEffect, HttpMiddleware, HttpServerRequest } from "effect/unstable/http"
|
||||
@@ -9,7 +7,7 @@ import { HttpEffect, HttpMiddleware, HttpServerRequest } from "effect/unstable/h
|
||||
type MarkedInstance = {
|
||||
ctx: InstanceContext
|
||||
store: InstanceStore.Interface
|
||||
workspaceID?: WorkspaceID
|
||||
bridge: EffectBridge.Shape
|
||||
}
|
||||
|
||||
// Disposal is requested by an endpoint handler, but must run from the outer
|
||||
@@ -19,20 +17,9 @@ const disposeAfterResponse = new WeakMap<object, MarkedInstance>()
|
||||
|
||||
const mark = (ctx: InstanceContext) =>
|
||||
Effect.gen(function* () {
|
||||
return { ctx, store: yield* InstanceStore.Service, workspaceID: yield* WorkspaceRef }
|
||||
return { ctx, store: yield* InstanceStore.Service, bridge: yield* EffectBridge.make() }
|
||||
})
|
||||
|
||||
// InstanceStore lifecycle operations still publish events through legacy ALS helpers.
|
||||
// Effect request handlers carry these values in services, so bridge them back
|
||||
// into the legacy contexts only around the lifecycle operation.
|
||||
const restoreMarked = <A>(marked: MarkedInstance, effect: Effect.Effect<A>) =>
|
||||
Effect.promise(() =>
|
||||
WorkspaceContext.provide({
|
||||
workspaceID: marked.workspaceID,
|
||||
fn: () => Instance.restore(marked.ctx, () => Effect.runPromise(effect)),
|
||||
}),
|
||||
)
|
||||
|
||||
export const markInstanceForDisposal = (ctx: InstanceContext) =>
|
||||
Effect.gen(function* () {
|
||||
const marked = yield* mark(ctx)
|
||||
@@ -49,7 +36,7 @@ export const markInstanceForReload = (ctx: InstanceContext, next: InstanceStore.
|
||||
Effect.gen(function* () {
|
||||
const marked = yield* mark(ctx)
|
||||
return yield* HttpEffect.appendPreResponseHandler((_request, response) =>
|
||||
Effect.as(Effect.uninterruptible(restoreMarked(marked, marked.store.reload(next))), response),
|
||||
Effect.as(Effect.uninterruptible(marked.bridge.run(marked.store.reload(next))), response),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -60,6 +47,6 @@ export const disposeMiddleware: HttpMiddleware.HttpMiddleware = (effect) =>
|
||||
const marked = disposeAfterResponse.get(request.source)
|
||||
if (!marked) return response
|
||||
disposeAfterResponse.delete(request.source)
|
||||
yield* Effect.uninterruptible(restoreMarked(marked, marked.store.dispose(marked.ctx)))
|
||||
yield* Effect.uninterruptible(marked.bridge.run(marked.store.dispose(marked.ctx)))
|
||||
return response
|
||||
})
|
||||
|
||||
@@ -3,7 +3,6 @@ import { AppRuntime } from "@/effect/app-runtime"
|
||||
import { InstanceBootstrap } from "@/project/bootstrap"
|
||||
import type { InstanceContext } from "@/project/instance"
|
||||
import { InstanceStore } from "@/project/instance-store"
|
||||
import { Filesystem } from "@/util/filesystem"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { HttpRouter, HttpServerResponse } from "effect/unstable/http"
|
||||
import { HttpApiMiddleware } from "effect/unstable/httpapi"
|
||||
@@ -29,7 +28,7 @@ function makeInstanceContext(
|
||||
directory: string,
|
||||
): Effect.Effect<InstanceContext> {
|
||||
return store.load({
|
||||
directory: Filesystem.resolve(decode(directory)),
|
||||
directory: decode(directory),
|
||||
init: () => AppRuntime.runPromise(InstanceBootstrap),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user