Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
114 lines
3.7 KiB
TypeScript
114 lines
3.7 KiB
TypeScript
export * as AgentV2 from "./agent"
|
|
|
|
import { makeLocationNode } from "./effect/app-node"
|
|
import { Array, Context, Effect, Layer, Types } from "effect"
|
|
import { Agent } from "@opencode-ai/schema/agent"
|
|
import { EventV2 } from "./event"
|
|
import { State } from "./state"
|
|
|
|
export const ID = Agent.ID
|
|
export type ID = typeof ID.Type
|
|
export const defaultID = ID.make("build")
|
|
|
|
export const Color = Agent.Color
|
|
|
|
export const Info = Agent.Info
|
|
export type Info = Agent.Info
|
|
|
|
export const Event = Agent.Event
|
|
|
|
export interface Selection {
|
|
readonly id: ID
|
|
readonly info: Info | undefined
|
|
}
|
|
|
|
type Data = {
|
|
agents: Map<ID, Types.DeepMutable<Info>>
|
|
default?: ID
|
|
}
|
|
|
|
export type Draft = {
|
|
list: () => readonly Info[]
|
|
get: (id: ID) => Info | undefined
|
|
default: (id: ID | undefined) => void
|
|
update: (id: ID, fn: (agent: Types.DeepMutable<Info>) => void) => void
|
|
remove: (id: ID) => void
|
|
}
|
|
|
|
export interface Interface extends State.Transformable<Draft> {
|
|
readonly get: (id: ID) => Effect.Effect<Info | undefined>
|
|
readonly default: () => Effect.Effect<Info | undefined>
|
|
readonly resolve: (id?: ID | string) => Effect.Effect<Info | undefined>
|
|
readonly select: (id?: ID | string) => Effect.Effect<Selection>
|
|
readonly list: () => Effect.Effect<Info[]>
|
|
}
|
|
|
|
export class Service extends Context.Service<Service, Interface>()("@opencode/v2/Agent") {}
|
|
|
|
const layer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const events = yield* EventV2.Service
|
|
const state = State.create<Data, Draft>({
|
|
initial: () => ({ agents: new Map() }),
|
|
draft: (draft) => ({
|
|
list: () => Array.fromIterable(draft.agents.values()) as Info[],
|
|
get: (id) => draft.agents.get(id),
|
|
default: (id) => {
|
|
draft.default = id
|
|
},
|
|
update: (id, fn) => {
|
|
const current = draft.agents.get(id) ?? (Info.empty(id) as Types.DeepMutable<Info>)
|
|
if (!draft.agents.has(id)) draft.agents.set(id, current)
|
|
fn(current)
|
|
current.id = id
|
|
},
|
|
remove: (id) => {
|
|
draft.agents.delete(id)
|
|
},
|
|
}),
|
|
finalize: () => events.publish(Event.Updated, {}).pipe(Effect.asVoid),
|
|
})
|
|
const selectable = (agent: Info | undefined) =>
|
|
agent && agent.mode !== "subagent" && !agent.hidden ? agent : undefined
|
|
const selectedDefault = () => {
|
|
const data = state.get()
|
|
const configured = data.default ? selectable(data.agents.get(data.default)) : undefined
|
|
if (configured) return configured
|
|
const build = selectable(data.agents.get(ID.make("build")))
|
|
if (build) return build
|
|
for (const agent of data.agents.values()) {
|
|
const fallback = selectable(agent)
|
|
if (fallback) return fallback
|
|
}
|
|
}
|
|
|
|
return Service.of({
|
|
transform: state.transform,
|
|
reload: state.reload,
|
|
get: Effect.fn("AgentV2.get")(function* (id) {
|
|
return state.get().agents.get(id)
|
|
}),
|
|
default: Effect.fn("AgentV2.default")(function* () {
|
|
return selectedDefault()
|
|
}),
|
|
resolve: Effect.fn("AgentV2.resolve")(function* (id) {
|
|
if (id !== undefined) return state.get().agents.get(ID.make(id))
|
|
return selectedDefault()
|
|
}),
|
|
select: Effect.fn("AgentV2.select")(function* (id) {
|
|
if (id !== undefined) {
|
|
const selected = ID.make(id)
|
|
return { id: selected, info: state.get().agents.get(selected) }
|
|
}
|
|
const info = selectedDefault()
|
|
return { id: info?.id ?? defaultID, info }
|
|
}),
|
|
list: Effect.fn("AgentV2.list")(function* () {
|
|
return Array.fromIterable(state.get().agents.values())
|
|
}),
|
|
})
|
|
}),
|
|
)
|
|
|
|
export const node = makeLocationNode({ service: Service, layer, deps: [EventV2.node] })
|