forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent-v2-bridge.ts
More file actions
71 lines (64 loc) · 2.76 KB
/
Copy pathevent-v2-bridge.ts
File metadata and controls
71 lines (64 loc) · 2.76 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
// Opencode publish boundary for core events. Attach routed instance location
// so direct EventV2 consumers can isolate directory/workspace streams.
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { InstanceRef, WorkspaceRef } from "@/effect/instance-ref"
import { GlobalBus } from "@/bus/global"
import { EventV2 } from "@opencode-ai/core/event"
import { Location } from "@opencode-ai/core/location"
import { Project } from "@opencode-ai/core/project"
import { AbsolutePath } from "@opencode-ai/core/schema"
import { Context, Effect, Layer } from "effect"
export class Service extends Context.Service<Service, EventV2.Interface>()("@opencode/EventV2Bridge") {}
const layer = Layer.effect(
Service,
Effect.gen(function* () {
const events = yield* EventV2.Service
const publish: EventV2.Interface["publish"] = (definition, data, options) =>
Effect.gen(function* () {
if (options?.location) return yield* events.publish(definition, data, options)
const ctx = yield* InstanceRef
if (!ctx) return yield* events.publish(definition, data, options)
const workspaceID = yield* WorkspaceRef
return yield* events.publish(definition, data, {
...options,
location: new Location.Info({
directory: AbsolutePath.make(ctx.directory),
...(workspaceID ? { workspaceID } : {}),
project: { id: Project.ID.make(ctx.project.id), directory: AbsolutePath.make(ctx.worktree) },
}),
})
})
const unsubscribe = yield* events.listen((event) =>
Effect.gen(function* () {
const ctx = yield* InstanceRef
const workspaceID = (yield* WorkspaceRef) ?? event.location?.workspaceID
GlobalBus.emit("event", {
directory: event.location?.directory ?? ctx?.directory,
project: ctx?.project.id,
workspace: workspaceID,
payload: { id: event.id, type: event.type, properties: event.data },
})
if (event.durable === undefined) return
GlobalBus.emit("event", {
directory: event.location?.directory ?? ctx?.directory,
project: ctx?.project.id,
workspace: workspaceID,
payload: {
type: "sync",
syncEvent: {
id: event.id,
type: EventV2.versionedType(event.type, event.durable.version),
seq: event.durable.seq,
aggregateID: event.durable.aggregateID,
data: event.data,
},
},
})
}),
)
yield* Effect.addFinalizer(() => unsubscribe)
return Service.of({ ...events, publish })
}),
)
export const node = LayerNode.make({ service: Service, layer: layer, deps: [EventV2.node] })
export * as EventV2Bridge from "./event-v2-bridge"