Skip to content

Commit fa5ca85

Browse files
authored
refactor(core): reuse plugin source discovery (anomalyco#41794)
1 parent 37a8c6c commit fa5ca85

3 files changed

Lines changed: 43 additions & 47 deletions

File tree

‎packages/core/src/plugin/internal.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ConfigProviderPlugin } from "../config/plugin/provider"
1717
import { ConfigPolicyPlugin } from "../config/plugin/policy"
1818
import { ConfigReferencePlugin } from "../config/plugin/reference"
1919
import { ConfigSkillPlugin } from "../config/plugin/skill"
20+
import { ConfigPluginSource } from "../config/plugin/source"
2021
import { ConfigWebSearchPlugin } from "../config/plugin/websearch"
2122
import { Bus } from "../bus"
2223
import { Environment } from "../environment"
@@ -76,6 +77,7 @@ const services = Effect.fn("PluginInternal.services")(function* () {
7677
const command = yield* Command.Service
7778
const config = yield* Config.Service
7879
const credential = yield* Credential.Service
80+
const pluginSources = yield* ConfigPluginSource.Service
7981
const bus = yield* Bus.Service
8082
const environment = yield* Environment.Service
8183
const mutation = yield* FileMutation.Service
@@ -112,6 +114,7 @@ const services = Effect.fn("PluginInternal.services")(function* () {
112114
Context.make(Command.Service, command),
113115
Context.make(Config.Service, config),
114116
Context.make(Credential.Service, credential),
117+
Context.make(ConfigPluginSource.Service, pluginSources),
115118
Context.make(Bus.Service, bus),
116119
Context.make(Environment.Service, environment),
117120
Context.make(FileMutation.Service, mutation),
@@ -155,6 +158,7 @@ export const requirements = LayerNode.group([
155158
Command.node,
156159
Config.node,
157160
Credential.node,
161+
ConfigPluginSource.node,
158162
Bus.node,
159163
Environment.node,
160164
FileMutation.node,

‎packages/core/src/plugin/skill.ts‎

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ import { define, type Context } from "@opencode-ai/plugin/effect/plugin"
66
import { Effect } from "effect"
77
import { AbsolutePath } from "../schema"
88
import { Skill } from "../skill"
9-
import { Config } from "../config"
10-
import { Location } from "../location"
11-
import { FSUtil } from "@opencode-ai/util/fs-util"
9+
import { ConfigPluginSource } from "../config/plugin/source"
1210
import os from "os"
13-
import path from "path"
14-
import { fileURLToPath } from "url"
1511
import opencodeContent from "./skill/opencode.md" with { type: "text" }
1612
import reportContent from "./skill/report.md" with { type: "text" }
1713

@@ -72,32 +68,10 @@ const reportContentWithDiagnostics = Effect.fn("SkillPlugin.reportContentWithDia
7268
})
7369

7470
const configuredPlugins = Effect.fn("SkillPlugin.configuredPlugins")(function* () {
75-
const config = yield* Config.Service
76-
const fs = yield* FSUtil.Service
77-
const location = yield* Location.Service
78-
return yield* Effect.forEach(yield* config.entries(), (entry) => {
79-
if (entry.type === "document") {
80-
const directory = entry.path ? path.dirname(entry.path) : location.directory
81-
return Effect.succeed(
82-
(entry.info.plugins ?? []).map((item) => {
83-
const ref = typeof item === "string" ? { package: item } : item
84-
if (ref.package.startsWith("file://")) return fileURLToPath(ref.package)
85-
if (ref.package.startsWith("./") || ref.package.startsWith("../")) return path.resolve(directory, ref.package)
86-
return ref.package
87-
}),
88-
)
89-
}
90-
if (entry.type !== "directory") return Effect.succeed([])
91-
return fs
92-
.scan("{plugin,plugins}/*.{ts,js}", {
93-
cwd: entry.path,
94-
absolute: true,
95-
include: "file",
96-
dot: true,
97-
symlink: true,
98-
})
99-
.pipe(Effect.orElseSucceed(() => []))
100-
}).pipe(Effect.map((items) => items.flat().toSorted()))
71+
const sources = yield* ConfigPluginSource.Service
72+
return (yield* sources.operations())
73+
.map((operation) => (operation.type === "remove" ? `-${operation.target}` : operation.target))
74+
.toSorted()
10175
})
10276

10377
function terminal() {

‎packages/core/test/plugin/skill.test.ts‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { describe, expect } from "bun:test"
2-
import { NodeFileSystem } from "@effect/platform-node"
3-
import { Config } from "@opencode-ai/core/config"
42
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
5-
import { FSUtil } from "@opencode-ai/util/fs-util"
6-
import { Location } from "@opencode-ai/core/location"
7-
import { Effect, Stream } from "effect"
3+
import { ConfigPluginSource } from "@opencode-ai/core/config/plugin/source"
4+
import { Effect, Layer, Stream } from "effect"
85
import { SkillPlugin } from "@opencode-ai/core/plugin/skill"
9-
import { AbsolutePath } from "@opencode-ai/core/schema"
106
import { Skill } from "@opencode-ai/core/skill"
11-
import { location } from "../fixture/location"
127
import { testEffect } from "../lib/effect"
138
import { host } from "./host"
149

1510
const it = testEffect(AppNodeBuilder.build(Skill.node))
11+
const sources = (operations: readonly ConfigPluginSource.Operation[] = []) =>
12+
Layer.succeed(
13+
ConfigPluginSource.Service,
14+
ConfigPluginSource.Service.of({ operations: () => Effect.succeed(operations), changes: () => Stream.never }),
15+
)
1616

1717
describe("SkillPlugin.Plugin", () => {
1818
it.effect("registers built-in skills", () =>
@@ -27,15 +27,7 @@ describe("SkillPlugin.Plugin", () => {
2727
reload: skill.reload,
2828
},
2929
}),
30-
).pipe(
31-
Effect.provide(Config.testLayer()),
32-
Effect.provideService(
33-
Location.Service,
34-
Location.Service.of(location({ directory: AbsolutePath.make(import.meta.dir) })),
35-
),
36-
Effect.provide(AppNodeBuilder.build(FSUtil.node)),
37-
Effect.provide(NodeFileSystem.layer),
38-
)
30+
).pipe(Effect.provide(sources()))
3931
const skills = yield* skill.list()
4032
const report = skills.find((item) => item.id === "report")
4133

@@ -58,4 +50,30 @@ describe("SkillPlugin.Plugin", () => {
5850
expect(report?.content).toContain("- install/channel: beta")
5951
}),
6052
)
53+
54+
it.effect("reports canonical configured plugin sources with existing labels and ordering", () =>
55+
Effect.gen(function* () {
56+
const skill = yield* Skill.Service
57+
yield* SkillPlugin.Plugin.effect(
58+
host({
59+
skill: {
60+
list: () => Effect.die("unused skill.list"),
61+
transform: skill.transform,
62+
reload: skill.reload,
63+
},
64+
}),
65+
)
66+
const report = (yield* skill.list()).find((item) => item.id === "report")
67+
expect(report?.content).toContain("- Active plugins: -disabled, local.ts, package-plugin, package-plugin")
68+
}).pipe(
69+
Effect.provide(
70+
sources([
71+
{ type: "add", target: "package-plugin", options: {} },
72+
{ type: "remove", target: "disabled" },
73+
{ type: "add", target: "local.ts", options: {}, mtime: 1 },
74+
{ type: "add", target: "package-plugin", options: { enabled: true } },
75+
]),
76+
),
77+
),
78+
)
6179
})

0 commit comments

Comments
 (0)