feat: add experimental background subagents (#27084)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { InstanceState } from "@/effect/instance-state"
|
||||
import { Runner } from "@/effect/runner"
|
||||
import { BackgroundJob } from "@/background/job"
|
||||
import { Effect, Latch, Layer, Scope, Context } from "effect"
|
||||
import * as Session from "./session"
|
||||
import { MessageV2 } from "./message-v2"
|
||||
@@ -27,6 +28,7 @@ export class Service extends Context.Service<Service, Interface>()("@opencode/Se
|
||||
export const layer = Layer.effect(
|
||||
Service,
|
||||
Effect.gen(function* () {
|
||||
const background = yield* BackgroundJob.Service
|
||||
const status = yield* SessionStatus.Service
|
||||
|
||||
const state = yield* InstanceState.make(
|
||||
@@ -72,6 +74,7 @@ export const layer = Layer.effect(
|
||||
})
|
||||
|
||||
const cancel = Effect.fn("SessionRunState.cancel")(function* (sessionID: SessionID) {
|
||||
yield* cancelBackgroundJobs(background, sessionID)
|
||||
const data = yield* InstanceState.get(state)
|
||||
const existing = data.runners.get(sessionID)
|
||||
if (!existing || !existing.busy) {
|
||||
@@ -104,7 +107,41 @@ export const layer = Layer.effect(
|
||||
}),
|
||||
)
|
||||
|
||||
export const defaultLayer = layer.pipe(Layer.provide(SessionStatus.defaultLayer))
|
||||
export const defaultLayer = layer.pipe(Layer.provide(BackgroundJob.defaultLayer), Layer.provide(SessionStatus.defaultLayer))
|
||||
|
||||
const cancelBackgroundJobs = Effect.fn("SessionRunState.cancelBackgroundJobs")(function* (
|
||||
background: BackgroundJob.Interface,
|
||||
sessionID: SessionID,
|
||||
) {
|
||||
const jobs = yield* background.list()
|
||||
const pending = new Set<string>([sessionID])
|
||||
const cancelled = new Set<string>()
|
||||
const matches = (job: BackgroundJob.Info) => {
|
||||
if (job.status !== "running") return false
|
||||
if (cancelled.has(job.id)) return false
|
||||
if (pending.has(job.id)) return true
|
||||
if (typeof job.metadata?.sessionId === "string" && pending.has(job.metadata.sessionId)) return true
|
||||
return typeof job.metadata?.parentSessionId === "string" && pending.has(job.metadata.parentSessionId)
|
||||
}
|
||||
let batch = jobs.filter(matches)
|
||||
while (batch.length > 0) {
|
||||
yield* Effect.forEach(
|
||||
batch,
|
||||
(job) =>
|
||||
background.cancel(job.id).pipe(
|
||||
Effect.tap(() =>
|
||||
Effect.sync(() => {
|
||||
cancelled.add(job.id)
|
||||
pending.add(job.id)
|
||||
if (typeof job.metadata?.sessionId === "string") pending.add(job.metadata.sessionId)
|
||||
}),
|
||||
),
|
||||
),
|
||||
{ concurrency: "unbounded", discard: true },
|
||||
)
|
||||
batch = jobs.filter(matches)
|
||||
}
|
||||
})
|
||||
|
||||
function busyError(sessionID: SessionID) {
|
||||
return new Session.BusyError({ sessionID })
|
||||
|
||||
Reference in New Issue
Block a user