|
| 1 | +import { InstanceState } from "@/effect/instance-state" |
| 2 | +import { Identifier } from "@/id/id" |
| 3 | +import { Cause, Deferred, Effect, Fiber, Layer, Scope, Context } from "effect" |
| 4 | + |
| 5 | +export type Status = "running" | "completed" | "error" | "cancelled" |
| 6 | + |
| 7 | +export type Info = { |
| 8 | + id: string |
| 9 | + type: string |
| 10 | + title?: string |
| 11 | + status: Status |
| 12 | + started_at: number |
| 13 | + completed_at?: number |
| 14 | + output?: string |
| 15 | + error?: string |
| 16 | + metadata?: Record<string, unknown> |
| 17 | +} |
| 18 | + |
| 19 | +type Active = { |
| 20 | + info: Info |
| 21 | + done: Deferred.Deferred<Info> |
| 22 | + fiber?: Fiber.Fiber<void, unknown> |
| 23 | +} |
| 24 | + |
| 25 | +type State = { |
| 26 | + jobs: Map<string, Active> |
| 27 | + scope: Scope.Scope |
| 28 | +} |
| 29 | + |
| 30 | +export type StartInput = { |
| 31 | + id?: string |
| 32 | + type: string |
| 33 | + title?: string |
| 34 | + metadata?: Record<string, unknown> |
| 35 | + run: Effect.Effect<string, unknown> |
| 36 | +} |
| 37 | + |
| 38 | +export type WaitInput = { |
| 39 | + id: string |
| 40 | + timeout?: number |
| 41 | +} |
| 42 | + |
| 43 | +export type WaitResult = { |
| 44 | + info?: Info |
| 45 | + timedOut: boolean |
| 46 | +} |
| 47 | + |
| 48 | +export interface Interface { |
| 49 | + readonly list: () => Effect.Effect<Info[]> |
| 50 | + readonly get: (id: string) => Effect.Effect<Info | undefined> |
| 51 | + readonly start: (input: StartInput) => Effect.Effect<Info> |
| 52 | + readonly wait: (input: WaitInput) => Effect.Effect<WaitResult> |
| 53 | + readonly cancel: (id: string) => Effect.Effect<Info | undefined> |
| 54 | +} |
| 55 | + |
| 56 | +export class Service extends Context.Service<Service, Interface>()("@opencode/BackgroundJob") {} |
| 57 | + |
| 58 | +function snapshot(job: Active): Info { |
| 59 | + return { |
| 60 | + ...job.info, |
| 61 | + ...(job.info.metadata ? { metadata: { ...job.info.metadata } } : {}), |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function errorText(error: unknown) { |
| 66 | + if (error instanceof Error) return error.message |
| 67 | + return String(error) |
| 68 | +} |
| 69 | + |
| 70 | +export const layer = Layer.effect( |
| 71 | + Service, |
| 72 | + Effect.gen(function* () { |
| 73 | + const state = yield* InstanceState.make<State>( |
| 74 | + Effect.fn("BackgroundJob.state")(function* () { |
| 75 | + return { |
| 76 | + jobs: new Map(), |
| 77 | + scope: yield* Scope.Scope, |
| 78 | + } |
| 79 | + }), |
| 80 | + ) |
| 81 | + |
| 82 | + const finish = Effect.fn("BackgroundJob.finish")(function* ( |
| 83 | + job: Active, |
| 84 | + status: Exclude<Status, "running">, |
| 85 | + data?: { output?: string; error?: string }, |
| 86 | + ) { |
| 87 | + if (job.info.status !== "running") return snapshot(job) |
| 88 | + job.info.status = status |
| 89 | + job.info.completed_at = Date.now() |
| 90 | + if (data?.output !== undefined) job.info.output = data.output |
| 91 | + if (data?.error !== undefined) job.info.error = data.error |
| 92 | + job.fiber = undefined |
| 93 | + const info = snapshot(job) |
| 94 | + yield* Deferred.succeed(job.done, info).pipe(Effect.ignore) |
| 95 | + return info |
| 96 | + }) |
| 97 | + |
| 98 | + const list: Interface["list"] = Effect.fn("BackgroundJob.list")(function* () { |
| 99 | + const s = yield* InstanceState.get(state) |
| 100 | + return Array.from(s.jobs.values()) |
| 101 | + .map(snapshot) |
| 102 | + .toSorted((a, b) => a.started_at - b.started_at) |
| 103 | + }) |
| 104 | + |
| 105 | + const get: Interface["get"] = Effect.fn("BackgroundJob.get")(function* (id) { |
| 106 | + const s = yield* InstanceState.get(state) |
| 107 | + const job = s.jobs.get(id) |
| 108 | + if (!job) return |
| 109 | + return snapshot(job) |
| 110 | + }) |
| 111 | + |
| 112 | + const start: Interface["start"] = Effect.fn("BackgroundJob.start")(function* (input) { |
| 113 | + const s = yield* InstanceState.get(state) |
| 114 | + const id = input.id ?? Identifier.ascending("job") |
| 115 | + const existing = s.jobs.get(id) |
| 116 | + if (existing?.info.status === "running") return snapshot(existing) |
| 117 | + |
| 118 | + const job: Active = { |
| 119 | + info: { |
| 120 | + id, |
| 121 | + type: input.type, |
| 122 | + title: input.title, |
| 123 | + status: "running", |
| 124 | + started_at: Date.now(), |
| 125 | + metadata: input.metadata, |
| 126 | + }, |
| 127 | + done: yield* Deferred.make<Info>(), |
| 128 | + } |
| 129 | + s.jobs.set(id, job) |
| 130 | + job.fiber = yield* input.run.pipe( |
| 131 | + Effect.matchCauseEffect({ |
| 132 | + onSuccess: (output) => finish(job, "completed", { output }), |
| 133 | + onFailure: (cause) => |
| 134 | + finish(job, Cause.hasInterruptsOnly(cause) ? "cancelled" : "error", { |
| 135 | + error: errorText(Cause.squash(cause)), |
| 136 | + }), |
| 137 | + }), |
| 138 | + Effect.asVoid, |
| 139 | + Effect.forkIn(s.scope), |
| 140 | + ) |
| 141 | + return snapshot(job) |
| 142 | + }) |
| 143 | + |
| 144 | + const wait: Interface["wait"] = Effect.fn("BackgroundJob.wait")(function* (input) { |
| 145 | + const s = yield* InstanceState.get(state) |
| 146 | + const job = s.jobs.get(input.id) |
| 147 | + if (!job) return { timedOut: false } |
| 148 | + if (job.info.status !== "running") return { info: snapshot(job), timedOut: false } |
| 149 | + if (!input.timeout) return { info: yield* Deferred.await(job.done), timedOut: false } |
| 150 | + return yield* Effect.raceAll([ |
| 151 | + Deferred.await(job.done).pipe(Effect.map((info) => ({ info, timedOut: false }))), |
| 152 | + Effect.sleep(input.timeout).pipe(Effect.as({ info: snapshot(job), timedOut: true })), |
| 153 | + ]) |
| 154 | + }) |
| 155 | + |
| 156 | + const cancel: Interface["cancel"] = Effect.fn("BackgroundJob.cancel")(function* (id) { |
| 157 | + const s = yield* InstanceState.get(state) |
| 158 | + const job = s.jobs.get(id) |
| 159 | + if (!job) return |
| 160 | + if (job.info.status !== "running") return snapshot(job) |
| 161 | + const fiber = job.fiber |
| 162 | + const info = yield* finish(job, "cancelled") |
| 163 | + if (fiber) yield* Fiber.interrupt(fiber).pipe(Effect.ignore) |
| 164 | + return info |
| 165 | + }) |
| 166 | + |
| 167 | + return Service.of({ list, get, start, wait, cancel }) |
| 168 | + }), |
| 169 | +) |
| 170 | + |
| 171 | +export const defaultLayer = layer |
| 172 | + |
| 173 | +export * as BackgroundJob from "./job" |
0 commit comments