mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
refactor(core): tidy plugin host and config reload wiring (#61176)
This commit is contained in:
committed by
GitHub
Unverified
parent
11ce936b63
commit
d4a5fb4d2a
@@ -4,10 +4,11 @@ import { define } from "@opencode/plugin/effect/plugin"
|
||||
import { Document, Info, type Entry } from "@opencode/schema/config"
|
||||
import { ConfigAgent } from "@opencode/schema/config/agent"
|
||||
import path from "path"
|
||||
import { Effect, Option, PubSub, Schema, Stream } from "effect"
|
||||
import { Effect, Option, Schema } from "effect"
|
||||
import { Agent } from "../../agent.js"
|
||||
import { Config } from "../../config.js"
|
||||
import { ConfigMarkdown } from "../markdown.js"
|
||||
import { ConfigEntryObserver } from "./entry-observer.js"
|
||||
import { FSUtil } from "@opencode/util/fs-util"
|
||||
import { ConfigAgentV1 } from "../../v1/config/agent.js"
|
||||
import { ConfigMigrateV1 } from "../../v1/config/migrate.js"
|
||||
@@ -52,33 +53,13 @@ export const Plugin = define({
|
||||
const load = Effect.fn("ConfigAgentPlugin.load")(function* () {
|
||||
return yield* Effect.forEach(yield* config.entries(), loadEntry).pipe(Effect.map((documents) => documents.flat()))
|
||||
})
|
||||
const loaded = { documents: [] as Document[] }
|
||||
const reload = load().pipe(
|
||||
Effect.tap((documents) => Effect.sync(() => (loaded.documents = documents))),
|
||||
Effect.andThen(ctx.agent.reload()),
|
||||
)
|
||||
// One trigger feed serializes reloads and shares one debounce window;
|
||||
// subscribing before the initial scan means updates racing the scan still
|
||||
// trigger a rebuild. Each source is subscribed eagerly on its own fiber
|
||||
// (Stream.merge and Stream.debounce both open upstream a fiber hop later)
|
||||
// so no update slips through while the debounce starts its pull.
|
||||
const changes = yield* PubSub.sliding<void>(1)
|
||||
const notify = () => PubSub.publish(changes, undefined)
|
||||
yield* config.changes().pipe(
|
||||
Stream.filterEffect((update) => Effect.map(config.entries(), (entries) => isAgentSource(entries, update.path))),
|
||||
Stream.runForEach(notify),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
yield* ctx.event
|
||||
.subscribe({ types: ["config.updated"] })
|
||||
.pipe(Stream.runForEach(notify), Effect.forkScoped({ startImmediately: true }))
|
||||
const updates = yield* PubSub.subscribe(changes)
|
||||
yield* Stream.fromSubscription(updates).pipe(
|
||||
Stream.debounce("100 millis"),
|
||||
Stream.runForEach(() => reload),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
loaded.documents = yield* load()
|
||||
const loaded = yield* ConfigEntryObserver.observeSources({
|
||||
config,
|
||||
event: ctx.event,
|
||||
directories: sourceDirectories,
|
||||
load,
|
||||
reload: ctx.agent.reload(),
|
||||
})
|
||||
yield* ctx.agent.transform((editor) => {
|
||||
const permissions = expandPermissions(
|
||||
loaded.documents.flatMap((document) => document.info.permissions ?? []),
|
||||
@@ -127,16 +108,6 @@ export const Plugin = define({
|
||||
}),
|
||||
})
|
||||
|
||||
// Matches anything at or under <root>/{agent,agents,mode,modes}. No file-suffix
|
||||
// check: directory-level events such as renames carry no per-file paths.
|
||||
function isAgentSource(entries: Entry[], file: string) {
|
||||
return entries.some(
|
||||
(entry) =>
|
||||
entry.type === "directory" &&
|
||||
sourceDirectories.some((name) => FSUtil.contains(path.join(entry.path, name), file)),
|
||||
)
|
||||
}
|
||||
|
||||
function expandPermissions(rules: Permission.Ruleset, home: string): Permission.Ruleset {
|
||||
// Expand only resources tools resolve as filesystem paths. Bash resources are raw shell text:
|
||||
// rewriting `$HOME/private/**` would miss `$HOME/private/key`, and safe expansion needs shell-aware parsing.
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
export * as ConfigCommandPlugin from "./command.js"
|
||||
|
||||
import { define } from "@opencode/plugin/effect/plugin"
|
||||
import { Info, type Entry } from "@opencode/schema/config"
|
||||
import type { Entry } from "@opencode/schema/config"
|
||||
import { ConfigCommand } from "@opencode/schema/config/command"
|
||||
import { AppProcess } from "@opencode/util/process"
|
||||
import path from "path"
|
||||
import { Effect, Option, PubSub, Schema, Stream } from "effect"
|
||||
import { Effect, Option, Schema } from "effect"
|
||||
import { ChildProcess } from "effect/process"
|
||||
import { Agent } from "../../agent.js"
|
||||
import { Config } from "../../config.js"
|
||||
@@ -15,6 +15,7 @@ import { SubagentJob } from "../../session/subagent-job.js"
|
||||
import { ShellSelect } from "../../shell/select.js"
|
||||
import { FSUtil } from "@opencode/util/fs-util"
|
||||
import { ConfigMarkdown } from "../markdown.js"
|
||||
import { ConfigEntryObserver } from "./entry-observer.js"
|
||||
|
||||
const decodeCommand = Schema.decodeUnknownOption(ConfigCommand.Info)
|
||||
|
||||
@@ -38,33 +39,13 @@ export const Plugin = define({
|
||||
const load = Effect.fn("ConfigCommandPlugin.load")(function* () {
|
||||
return yield* Effect.forEach(yield* config.entries(), loadEntry).pipe(Effect.map((documents) => documents.flat()))
|
||||
})
|
||||
const loaded = { documents: [] as { commands: Info["commands"] }[] }
|
||||
const reload = load().pipe(
|
||||
Effect.tap((documents) => Effect.sync(() => (loaded.documents = documents))),
|
||||
Effect.andThen(ctx.command.reload()),
|
||||
)
|
||||
// One trigger feed serializes reloads and shares one debounce window;
|
||||
// subscribing before the initial scan means updates racing the scan still
|
||||
// trigger a rebuild. Each source is subscribed eagerly on its own fiber
|
||||
// (Stream.merge and Stream.debounce both open upstream a fiber hop later)
|
||||
// so no update slips through while the debounce starts its pull.
|
||||
const changes = yield* PubSub.sliding<void>(1)
|
||||
const notify = () => PubSub.publish(changes, undefined)
|
||||
yield* config.changes().pipe(
|
||||
Stream.filterEffect((update) => Effect.map(config.entries(), (entries) => isCommandSource(entries, update.path))),
|
||||
Stream.runForEach(notify),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
yield* ctx.event
|
||||
.subscribe({ types: ["config.updated"] })
|
||||
.pipe(Stream.runForEach(notify), Effect.forkScoped({ startImmediately: true }))
|
||||
const updates = yield* PubSub.subscribe(changes)
|
||||
yield* Stream.fromSubscription(updates).pipe(
|
||||
Stream.debounce("100 millis"),
|
||||
Stream.runForEach(() => reload),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
loaded.documents = yield* load()
|
||||
const loaded = yield* ConfigEntryObserver.observeSources({
|
||||
config,
|
||||
event: ctx.event,
|
||||
directories: sourceDirectories,
|
||||
load,
|
||||
reload: ctx.command.reload(),
|
||||
})
|
||||
yield* ctx.command.transform((editor) => {
|
||||
for (const document of loaded.documents) {
|
||||
for (const [name, command] of Object.entries(document.commands ?? {})) {
|
||||
@@ -137,16 +118,6 @@ export const Plugin = define({
|
||||
// Keep in sync with the loadDirectory scan pattern and the name-strip regex in decode.
|
||||
const sourceDirectories = ["command", "commands"] as const
|
||||
|
||||
// Matches anything at or under <root>/{command,commands}. No file-suffix check:
|
||||
// directory-level events such as renames carry no per-file paths.
|
||||
function isCommandSource(entries: Entry[], file: string) {
|
||||
return entries.some(
|
||||
(entry) =>
|
||||
entry.type === "directory" &&
|
||||
sourceDirectories.some((name) => FSUtil.contains(path.join(entry.path, name), file)),
|
||||
)
|
||||
}
|
||||
|
||||
function loadDirectory(fs: FSUtil.Interface, directory: string) {
|
||||
return Effect.gen(function* () {
|
||||
const files = yield* fs
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
export * as ConfigEntryObserver from "./entry-observer.js"
|
||||
|
||||
import type { EventDomain } from "@opencode/plugin/effect/event"
|
||||
import { Effect, Stream } from "effect"
|
||||
import type { Entry } from "@opencode/schema/config"
|
||||
import { FSUtil } from "@opencode/util/fs-util"
|
||||
import { Effect, PubSub, Stream } from "effect"
|
||||
import path from "path"
|
||||
import { Config } from "../../config.js"
|
||||
|
||||
export const observe = Effect.fnUntraced(function* (
|
||||
@@ -22,3 +25,55 @@ export const observe = Effect.fnUntraced(function* (
|
||||
loaded.entries = yield* config.entries()
|
||||
return loaded
|
||||
})
|
||||
|
||||
/**
|
||||
* Loads documents from config entries and reloads them when config is updated or a file changes
|
||||
* under one of `directories` in a config directory.
|
||||
*/
|
||||
export const observeSources = Effect.fnUntraced(function* <A>(input: {
|
||||
readonly config: Config.Interface
|
||||
readonly event: EventDomain
|
||||
readonly directories: readonly string[]
|
||||
readonly load: () => Effect.Effect<A[]>
|
||||
readonly reload: Effect.Effect<void>
|
||||
}) {
|
||||
const config = input.config
|
||||
const loaded = { documents: [] as A[] }
|
||||
const reload = input.load().pipe(
|
||||
Effect.tap((documents) => Effect.sync(() => (loaded.documents = documents))),
|
||||
Effect.andThen(input.reload),
|
||||
)
|
||||
// One trigger feed serializes reloads and shares one debounce window;
|
||||
// subscribing before the initial scan means updates racing the scan still
|
||||
// trigger a rebuild. Each source is subscribed eagerly on its own fiber
|
||||
// (Stream.merge and Stream.debounce both open upstream a fiber hop later)
|
||||
// so no update slips through while the debounce starts its pull.
|
||||
const changes = yield* PubSub.sliding<void>(1)
|
||||
const notify = () => PubSub.publish(changes, undefined)
|
||||
yield* config.changes().pipe(
|
||||
Stream.filterEffect((update) =>
|
||||
Effect.map(config.entries(), (entries) => isDirectorySource(entries, update.path, input.directories)),
|
||||
),
|
||||
Stream.runForEach(notify),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
yield* input.event
|
||||
.subscribe({ types: ["config.updated"] })
|
||||
.pipe(Stream.runForEach(notify), Effect.forkScoped({ startImmediately: true }))
|
||||
const updates = yield* PubSub.subscribe(changes)
|
||||
yield* Stream.fromSubscription(updates).pipe(
|
||||
Stream.debounce("100 millis"),
|
||||
Stream.runForEach(() => reload),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
loaded.documents = yield* input.load()
|
||||
return loaded
|
||||
})
|
||||
|
||||
// Matches anything at or under <root>/<name> for a config directory. No file-suffix
|
||||
// check: directory-level events such as renames carry no per-file paths.
|
||||
export function isDirectorySource(entries: readonly Entry[], file: string, names: readonly string[]) {
|
||||
return entries.some(
|
||||
(entry) => entry.type === "directory" && names.some((name) => FSUtil.contains(path.join(entry.path, name), file)),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import { Config } from "../../config.js"
|
||||
import { Watcher } from "../../filesystem/watcher.js"
|
||||
import { Location } from "../../location.js"
|
||||
import { PluginSourceDirectory } from "../../plugin/source-directory.js"
|
||||
import { ConfigEntryObserver } from "./entry-observer.js"
|
||||
|
||||
export type Operation =
|
||||
| {
|
||||
@@ -55,7 +56,7 @@ export const layer = Layer.effect(
|
||||
if (operation.type !== "add" || !path.isAbsolute(operation.target)) continue
|
||||
if (watched.has(operation.target)) continue
|
||||
// The config change feed already covers {plugin,plugins} directories.
|
||||
if (isPluginSource(entries, operation.target)) continue
|
||||
if (ConfigEntryObserver.isDirectorySource(entries, operation.target, PluginSourceDirectory.names)) continue
|
||||
watched.add(operation.target)
|
||||
const updates = yield* watcher.subscribe({
|
||||
path: operation.target,
|
||||
@@ -82,7 +83,9 @@ export const layer = Layer.effect(
|
||||
Stream.merge(
|
||||
config.changes().pipe(
|
||||
Stream.filterEffect((update) =>
|
||||
Effect.map(config.entries(), (entries) => isPluginSource(entries, update.path)),
|
||||
Effect.map(config.entries(), (entries) =>
|
||||
ConfigEntryObserver.isDirectorySource(entries, update.path, PluginSourceDirectory.names),
|
||||
),
|
||||
),
|
||||
Stream.map(() => undefined),
|
||||
),
|
||||
@@ -189,11 +192,3 @@ const scan = Effect.fn("ConfigPluginSource.scan")(function* (
|
||||
}),
|
||||
).pipe(Effect.map((operations) => operations.flat()))
|
||||
})
|
||||
|
||||
function isPluginSource(entries: readonly Entry[], file: string) {
|
||||
return entries.some(
|
||||
(entry) =>
|
||||
entry.type === "directory" &&
|
||||
PluginSourceDirectory.names.some((directory) => FSUtil.contains(path.join(entry.path, directory), file)),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import { PluginHost } from "./plugin/host.js"
|
||||
import { type Failure, type Generation, Service } from "./plugin/service.js"
|
||||
import { State } from "./state.js"
|
||||
|
||||
export { awaitActivation, type Generation, type Interface, Service } from "./plugin/service.js"
|
||||
export { awaitActivation, type Failure, type Generation, type Interface, Service } from "./plugin/service.js"
|
||||
|
||||
const layer = Layer.effect(
|
||||
Service,
|
||||
@@ -40,6 +40,7 @@ const layer = Layer.effect(
|
||||
const pendingFailures = yield* Queue.unbounded<PendingFailure>()
|
||||
let discovered: readonly Failure[] = []
|
||||
let inventory: Plugin.Info[] = []
|
||||
const snapshot = () => [...Array.from(active.values()).map(slotInfo), ...discovered]
|
||||
// A changed `./gui-experimental` bundle alone restarts nothing; its revision is part of what changes, so the event
|
||||
// tells the app to reload it.
|
||||
const publish = State.changes(
|
||||
@@ -128,7 +129,7 @@ const layer = Layer.effect(
|
||||
if (entry) active.set(definition.id, { ...entry, plugin: definition })
|
||||
}
|
||||
if (prefix === definitions.length && active.size === definitions.length) {
|
||||
inventory = [...Array.from(active.values()).map(slotInfo), ...failures]
|
||||
inventory = snapshot()
|
||||
yield* publish(inventory)
|
||||
return
|
||||
}
|
||||
@@ -180,7 +181,7 @@ const layer = Layer.effect(
|
||||
})
|
||||
}
|
||||
|
||||
inventory = [...Array.from(active.values()).map(slotInfo), ...failures]
|
||||
inventory = snapshot()
|
||||
}),
|
||||
)
|
||||
yield* publish(inventory)
|
||||
@@ -204,7 +205,7 @@ const layer = Layer.effect(
|
||||
if (closed) return
|
||||
// Failure is already recorded on its exact activation, so an old queued item
|
||||
// cannot disable a replacement and teardown need not wait for this worker.
|
||||
inventory = [...Array.from(active.values()).map(slotInfo), ...discovered]
|
||||
inventory = snapshot()
|
||||
const refreshed = yield* State.batch(item.refresh).pipe(Effect.exit)
|
||||
yield* publish(inventory)
|
||||
if (Exit.isFailure(refreshed))
|
||||
|
||||
@@ -14,6 +14,7 @@ import { Command } from "../command.js"
|
||||
import { Credential } from "../credential.js"
|
||||
import { Bus } from "../bus.js"
|
||||
import { Integration } from "../integration.js"
|
||||
import { IntegrationConnection } from "../integration/connection.js"
|
||||
import { KV } from "../kv.js"
|
||||
import { Location } from "../location.js"
|
||||
import { LocationServiceMap } from "../location-service-map.js"
|
||||
@@ -79,11 +80,11 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interfa
|
||||
const worktrees = yield* Worktree.Service
|
||||
const worktreeStrategies = yield* WorktreeStrategies.Service
|
||||
const currentWorktreeStrategies = location.workspaceID ? undefined : worktreeStrategies
|
||||
const locationInfo = () =>
|
||||
const locationInfo = (current: Location.Interface = location) =>
|
||||
new Location.Info({
|
||||
directory: location.directory,
|
||||
workspaceID: location.workspaceID,
|
||||
project: location.project,
|
||||
directory: current.directory,
|
||||
workspaceID: current.workspaceID,
|
||||
project: current.project,
|
||||
})
|
||||
const locationRef = (input?: { readonly location?: { readonly directory?: string; readonly workspace?: string } }) =>
|
||||
input?.location === undefined
|
||||
@@ -97,23 +98,30 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interfa
|
||||
ref.directory === location.directory && ref.workspaceID === location.workspaceID
|
||||
const response = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
||||
effect.pipe(Effect.map((data) => ({ location: locationInfo(), data })))
|
||||
const atLocation = <A, E, R>(ref: Location.Ref, effect: Effect.Effect<A, E, R>) =>
|
||||
Effect.gen(function* () {
|
||||
return { location: locationInfo(yield* Location.Service), data: yield* effect }
|
||||
}).pipe(Effect.provide(locations.get(ref)))
|
||||
|
||||
const sessionPermission = (input: Parameters<Plugin.Context["permission"]["get"]>[0]) =>
|
||||
permission
|
||||
.get(input.requestID)
|
||||
.pipe(
|
||||
Effect.flatMap((request) =>
|
||||
request?.sessionID === input.sessionID
|
||||
? Effect.succeed(request)
|
||||
: Effect.fail(new Error(`Permission request not found: ${input.requestID}`)),
|
||||
),
|
||||
)
|
||||
|
||||
const decodeWorktree = Schema.decodeUnknownEffect(Worktree.Info)
|
||||
const decodeWorktrees = Schema.decodeUnknownEffect(Schema.Array(Worktree.ListEntry))
|
||||
|
||||
const listAgents = Effect.fn("PluginHost.listAgents")((ref: Location.Ref) =>
|
||||
Effect.gen(function* () {
|
||||
const location = yield* Location.Service
|
||||
const agents = yield* Agent.Service
|
||||
return {
|
||||
location: new Location.Info({
|
||||
directory: location.directory,
|
||||
workspaceID: location.workspaceID,
|
||||
project: location.project,
|
||||
}),
|
||||
data: yield* agents.list(),
|
||||
}
|
||||
}).pipe(Effect.provide(locations.get(ref)), Effect.orDie),
|
||||
atLocation(
|
||||
ref,
|
||||
Effect.flatMap(Agent.Service, (agents) => agents.list()),
|
||||
).pipe(Effect.orDie),
|
||||
)
|
||||
|
||||
// Keep the instance graph's inferred types independent of Session handles.
|
||||
@@ -212,10 +220,7 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interfa
|
||||
add: (definition) =>
|
||||
editor.add({
|
||||
...definition,
|
||||
sourceConnection:
|
||||
definition.sourceConnection?.type === "credential"
|
||||
? { ...definition.sourceConnection, id: Credential.ID.make(definition.sourceConnection.id) }
|
||||
: definition.sourceConnection,
|
||||
sourceConnection: definition.sourceConnection && connectionInfo(definition.sourceConnection),
|
||||
}),
|
||||
update: (id, update) => editor.update(Provider.ID.make(id), update),
|
||||
remove: (id) => editor.remove(Provider.ID.make(id)),
|
||||
@@ -393,17 +398,11 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interfa
|
||||
reload: integration.reload,
|
||||
connection: {
|
||||
active: (id) => integration.connection.active(Integration.ID.make(id)),
|
||||
resolve: (connection) =>
|
||||
integration.connection.resolve(
|
||||
connection.type === "credential" ? { ...connection, id: Credential.ID.make(connection.id) } : connection,
|
||||
),
|
||||
resolve: (connection) => integration.connection.resolve(connectionInfo(connection)),
|
||||
status: (input) =>
|
||||
integration.connection.status({
|
||||
integrationID: Integration.ID.make(input.integrationID),
|
||||
connection:
|
||||
input.connection.type === "credential"
|
||||
? { ...input.connection, id: Credential.ID.make(input.connection.id) }
|
||||
: input.connection,
|
||||
connection: connectionInfo(input.connection),
|
||||
status: input.status,
|
||||
}),
|
||||
},
|
||||
@@ -427,18 +426,10 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interfa
|
||||
list: (input) => {
|
||||
const ref = locationRef(input)
|
||||
if (ref && !isCurrentLocation(ref))
|
||||
return Effect.gen(function* () {
|
||||
const location = yield* Location.Service
|
||||
const mcp = yield* Mcp.Service
|
||||
return {
|
||||
location: new Location.Info({
|
||||
directory: location.directory,
|
||||
workspaceID: location.workspaceID,
|
||||
project: location.project,
|
||||
}),
|
||||
data: yield* mcp.servers(),
|
||||
}
|
||||
}).pipe(Effect.provide(locations.get(ref)))
|
||||
return atLocation(
|
||||
ref,
|
||||
Effect.flatMap(Mcp.Service, (mcp) => mcp.servers()),
|
||||
)
|
||||
return response(mcp.servers())
|
||||
},
|
||||
reload: mcp.reload,
|
||||
@@ -456,26 +447,13 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interfa
|
||||
permission: {
|
||||
hook: (name, callback) => hooks.register("permission", name, callback),
|
||||
list: (input) => permission.forSession(input.sessionID),
|
||||
get: (input) =>
|
||||
permission
|
||||
.get(input.requestID)
|
||||
.pipe(
|
||||
Effect.flatMap((request) =>
|
||||
request?.sessionID === input.sessionID
|
||||
? Effect.succeed(request)
|
||||
: Effect.fail(new Error(`Permission request not found: ${input.requestID}`)),
|
||||
),
|
||||
),
|
||||
get: sessionPermission,
|
||||
reply: (input) =>
|
||||
permission
|
||||
.get(input.requestID)
|
||||
.pipe(
|
||||
Effect.flatMap((request) =>
|
||||
request?.sessionID === input.sessionID
|
||||
? permission.reply({ requestID: input.requestID, reply: input.decision, message: input.message })
|
||||
: Effect.fail(new Error(`Permission request not found: ${input.requestID}`)),
|
||||
),
|
||||
sessionPermission(input).pipe(
|
||||
Effect.andThen(() =>
|
||||
permission.reply({ requestID: input.requestID, reply: input.decision, message: input.message }),
|
||||
),
|
||||
),
|
||||
// Plain rejections stay defects, as for built-in tools, so they end the step.
|
||||
assert: (input) =>
|
||||
permission.assert(input).pipe(
|
||||
@@ -760,6 +738,14 @@ function methodImplementation(input: IntegrationMethodRegistration): Integration
|
||||
}
|
||||
}
|
||||
|
||||
function connectionInfo(
|
||||
connection:
|
||||
| (Omit<IntegrationConnection.CredentialInfo, "id"> & { readonly id: string })
|
||||
| IntegrationConnection.EnvInfo,
|
||||
): IntegrationConnection.Info {
|
||||
return connection.type === "credential" ? { ...connection, id: Credential.ID.make(connection.id) } : connection
|
||||
}
|
||||
|
||||
function credential(value: Credential.OAuth) {
|
||||
return Credential.OAuth.make({ ...value, methodID: Integration.MethodID.make(value.methodID) })
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ export * as PluginInternal from "./internal.js"
|
||||
import { LLMClient } from "@opencode/ai/effect"
|
||||
import type { Plugin } from "@opencode/plugin/effect/plugin"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
import type { Generation } from "./service.js"
|
||||
import { httpClient } from "@opencode/util/effect/app-node-platform"
|
||||
import { AppProcess } from "@opencode/util/process"
|
||||
import { Context, Effect, Scope } from "effect"
|
||||
@@ -303,10 +304,13 @@ export const list = Effect.fn("PluginInternal.list")(function* () {
|
||||
const context = Context.pick(...services)(yield* Effect.context<Requirements>())
|
||||
const resolve = (plugins: readonly InternalPlugin[]) =>
|
||||
plugins.map(
|
||||
(plugin): Plugin => ({
|
||||
id: plugin.id,
|
||||
effect: (host) => plugin.effect(host).pipe(Effect.provide(context)),
|
||||
}),
|
||||
(plugin) =>
|
||||
({
|
||||
id: plugin.id,
|
||||
effect: (host) => plugin.effect(host).pipe(Effect.provide(context)),
|
||||
revision: "internal",
|
||||
source: { type: "builtin" },
|
||||
}) satisfies Generation,
|
||||
)
|
||||
return {
|
||||
pre: resolve(pre),
|
||||
|
||||
@@ -2,7 +2,7 @@ export * as PluginModule from "./module.js"
|
||||
|
||||
import type { Plugin } from "@opencode/plugin/effect/plugin"
|
||||
import { Host } from "@opencode/plugin/host"
|
||||
import { GuiBundle } from "@opencode/schema/plugin"
|
||||
import { GuiBundle, type Source } from "@opencode/schema/plugin"
|
||||
import { createPluginSources } from "@opencode/plugin/source"
|
||||
import { Hash } from "@opencode/util/hash"
|
||||
import { Npm } from "@opencode/util/npm"
|
||||
@@ -58,6 +58,12 @@ export const make = Effect.fn("PluginModule.make")(function* () {
|
||||
}
|
||||
})
|
||||
|
||||
// A local plugin's directory (or legacy standalone file), where clients resolve its other parts.
|
||||
export function source(target: string, version?: string): Source {
|
||||
if (path.isAbsolute(target)) return { type: "local", path: target }
|
||||
return { type: "package", target, ...(version ? { version } : {}) }
|
||||
}
|
||||
|
||||
// A missing dependency may have missing parents too. Watch the nearest existing
|
||||
// ancestor recursively so creating the rest of the path can trigger recovery.
|
||||
function watchTarget(file: string): Promise<Watcher.WatchInput> {
|
||||
@@ -154,14 +160,7 @@ const load = Effect.fn("PluginModule.load")(function* (
|
||||
? { file: entrypoints.gui, revision: Hash.sha256(JSON.stringify([revision, bundle])), options: guiOptions }
|
||||
: undefined,
|
||||
revision,
|
||||
// A local plugin's directory (or legacy standalone file), where clients resolve its other parts.
|
||||
source: local
|
||||
? { type: "local" as const, path: operation.target }
|
||||
: {
|
||||
type: "package" as const,
|
||||
target: operation.target,
|
||||
...(installed?.version ? { version: installed.version } : {}),
|
||||
},
|
||||
source: source(operation.target, installed?.version),
|
||||
effect: (host) => plugin.effect({ ...host, options: operation.options }),
|
||||
} satisfies Generation
|
||||
})
|
||||
|
||||
@@ -2,7 +2,6 @@ export * as PluginSupervisor from "./supervisor.js"
|
||||
|
||||
import { Event } from "@opencode/schema/config"
|
||||
import { Cause, Effect, Layer, Queue, Stream } from "effect"
|
||||
import path from "path"
|
||||
import { ConfigPluginSource } from "../config/plugin/source.js"
|
||||
import { makeLocationNode } from "@opencode/util/effect/app-node"
|
||||
import { Bus } from "../bus.js"
|
||||
@@ -29,10 +28,7 @@ const resolve = Effect.fn("PluginSupervisor.resolve")(function* (
|
||||
const enabled = new Set(definitions.map((plugin) => plugin.id))
|
||||
const packages = new Map<string, Plugin.Generation>()
|
||||
const pending = new Set<string>()
|
||||
const failures = new Map<
|
||||
string,
|
||||
Plugin.Info & { readonly state: Extract<Plugin.State, { readonly status: "failed" }> }
|
||||
>()
|
||||
const failures = new Map<string, Plugin.Failure>()
|
||||
const plugins = () => [...definitions, ...packages.values()]
|
||||
|
||||
for (const operation of operations) {
|
||||
@@ -77,7 +73,7 @@ const resolve = Effect.fn("PluginSupervisor.resolve")(function* (
|
||||
}
|
||||
if ("error" in plugin) {
|
||||
failures.set(operation.target, {
|
||||
source: pluginSource(operation.target),
|
||||
source: PluginModule.source(operation.target),
|
||||
state: { status: "failed", error: plugin.error, ref: plugin.ref },
|
||||
features: { server: true },
|
||||
})
|
||||
@@ -147,16 +143,8 @@ export const layer = Layer.effectDiscard(
|
||||
// Combine internal plugins with host-contributed plugins in boot order.
|
||||
// Instance-bound plugins come last: later activation can override earlier
|
||||
// container writes, so the instance's explicit choices win over globals.
|
||||
const pre = [
|
||||
...internal.pre.map((plugin) => ({ ...plugin, revision: "internal", source: { type: "builtin" as const } })),
|
||||
...sdk.all(),
|
||||
...instance.all(),
|
||||
]
|
||||
const post = internal.post.map((plugin) => ({
|
||||
...plugin,
|
||||
revision: "internal",
|
||||
source: { type: "builtin" as const },
|
||||
}))
|
||||
const pre = [...internal.pre, ...sdk.all(), ...instance.all()]
|
||||
const post = internal.post
|
||||
const operations = yield* sources.operations()
|
||||
// Activate everything available locally before waiting on missing package installs.
|
||||
const immediate = yield* resolve(modules, pre, post, operations, false, running)
|
||||
@@ -257,9 +245,4 @@ const nodeDeps = [
|
||||
PluginInternal.requirements,
|
||||
] as const
|
||||
|
||||
function pluginSource(target: string): Plugin.Source {
|
||||
if (path.isAbsolute(target)) return { type: "local", path: target }
|
||||
return { type: "package", target }
|
||||
}
|
||||
|
||||
export const node = makeLocationNode({ name: "plugin-supervisor", layer, deps: nodeDeps })
|
||||
|
||||
Reference in New Issue
Block a user