Repository navigation
Expand file tree
/
Copy pathmodule.ts
More file actions
186 lines (177 loc) · 8.19 KB
/
Copy pathmodule.ts
File metadata and controls
186 lines (177 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
export * as PluginModule from "./module.js"
import type { Plugin } from "@opencode/plugin/effect/plugin"
import { Host } from "@opencode/plugin/host"
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"
import { Deferred, Effect, FiberSet, PubSub, Schema, Stream } from "effect"
import path from "path"
import { readFile, stat } from "node:fs/promises"
import { fileURLToPath, pathToFileURL } from "url"
import type { ConfigPluginSource } from "../config/plugin/source.js"
import type { Generation } from "../plugin.js"
import { PluginPromise } from "./promise.js"
import { Config } from "../config.js"
import { ManagedPolicy } from "../managed-policy.js"
import { Watcher } from "../filesystem/watcher.js"
export const make = Effect.fn("PluginModule.make")(function* () {
const config = yield* Config.Service
const watcher = yield* Watcher.Service
const scope = yield* Effect.scope
const runPromise = yield* FiberSet.makeRuntimePromise()
const changes = yield* PubSub.unbounded<void>()
const watched = new Set<string>()
const watch = Effect.fn("PluginModule.watch")(function* (file: string) {
if (watched.has(file)) return
watched.add(file)
const ready = yield* Deferred.make<void>()
const target = yield* Effect.promise(() => watchTarget(file))
const updates = yield* watcher.subscribe(target, Deferred.succeed(ready, undefined))
yield* updates.pipe(
Stream.runForEach(() => PubSub.publish(changes, undefined)),
Effect.ensuring(Deferred.succeed(ready, undefined)),
Effect.forkIn(scope, { startImmediately: true }),
)
yield* Deferred.await(ready)
})
const sources = yield* Effect.acquireRelease(
Effect.sync(() => createPluginSources((file) => runPromise(watch(file)))),
(sources) => Effect.sync(() => sources.dispose()),
)
return {
load: (
operation: Extract<ConfigPluginSource.Operation, { type: "add" }>,
options?: { readonly install?: boolean },
) =>
Effect.gen(function* () {
const version = operation.target.lastIndexOf("@")
const target =
path.isAbsolute(operation.target) || version <= 0 ? operation.target : operation.target.slice(0, version)
if (ManagedPolicy.decision(ManagedPolicy.statements(yield* config.entries()), "plugin.use", target) === "deny")
return { blocked: true as const }
return yield* load(operation, sources, watch, options)
}),
changes: () => Stream.fromPubSub(changes),
}
})
// 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> {
return stat(file).then(
(info) => ({ path: file, type: info.isDirectory() ? "directory" : "file" }),
(cause) => {
if (path.dirname(file) === file) throw cause
return watchTarget(path.dirname(file))
},
)
}
const GuiOptions = GuiBundle.fields.options
const Module = Schema.Struct({
default: Schema.Union([
Schema.Struct({
id: Schema.String,
effect: Schema.declare<Plugin["effect"]>((input): input is Plugin["effect"] => typeof input === "function"),
}),
Schema.Struct({
id: Schema.String,
setup: Schema.declare<Parameters<typeof PluginPromise.fromPromise>[0]["setup"]>(
(input): input is Parameters<typeof PluginPromise.fromPromise>[0]["setup"] => typeof input === "function",
),
}),
]),
})
export class LoadError extends Schema.TaggedError<LoadError>()("PluginModule.LoadError", {
message: Schema.String,
cause: Schema.optional(Schema.Defect()),
}) {}
const load = Effect.fn("PluginModule.load")(function* (
operation: Extract<ConfigPluginSource.Operation, { type: "add" }>,
sources: ReturnType<typeof createPluginSources>,
watch: (file: string) => Effect.Effect<void>,
options?: { readonly install?: boolean },
) {
const local = path.isAbsolute(operation.target)
const npm = yield* Npm.Service
const installed = local
? undefined
: options?.install === false
? yield* npm.resolve(operation.target)
: yield* npm.add(operation.target)
// Legacy auto-discovery still admits standalone server sources. Configured
// local plugins always arrive here as directories.
const entrypoints: Host.Entrypoints =
local && (yield* Effect.promise(() => stat(operation.target))).isFile()
? { server: pathToFileURL(operation.target).href }
: yield* Effect.sync(() => Host.resolve(installed ?? { directory: operation.target }))
const entrypoint = entrypoints.server
// A TUI-only package is not the server's to list; the TUI loads it from its own config.
const found = entrypoint || entrypoints.gui
if (!local && options?.install === false && !found) return { pending: true as const }
if (!found) return yield* new LoadError({ message: `Plugin entrypoint not found: ${operation.target}` })
yield* Effect.log({ msg: "loading plugin", id: operation.target, entrypoint: found })
// A package with only a GUI part (and maybe a TUI part) has nothing to run here: its id is its package name, and its
// other parts load from this listing.
const server = entrypoint
? yield* loadServer(entrypoint, local ? sources : undefined, installed?.revision)
: undefined
const plugin = server?.plugin ?? {
id: Host.packageID(installed ?? { directory: operation.target }),
effect: () => Effect.void,
}
const revision = JSON.stringify([operation, server ? server.version : installed?.revision])
// A local plugin's `./gui-experimental` bundle is outside the server entry's import graph: watch it, and let its
// content change the GUI's revision, so an edit reloads the GUI like a server or TUI edit reloads theirs. A package's
// bundle changes only with its installed version. The GUI's revision is a hash: windows receive it, and the operation
// holds the options.
const gui = local && entrypoints.gui ? fileURLToPath(entrypoints.gui) : undefined
if (gui) yield* watch(gui)
const bundle = gui ? Hash.sha256(yield* Effect.promise(() => readFile(gui))) : undefined
// The GUI receives the same options as the server entry; they cross to the app, so they must be JSON.
const guiOptions = entrypoints.gui
? yield* Schema.decodeUnknownEffect(GuiOptions)(operation.options).pipe(
Effect.mapError(
(cause) => new LoadError({ message: "Plugin options must be JSON to reach its ./gui-experimental", cause }),
),
)
: {}
return {
id: plugin.id,
features: {
...(entrypoint ? { server: true as const } : {}),
...(entrypoints.tui ? { tui: true as const } : {}),
...(entrypoint && entrypoints.rpc ? { rpc: true as const } : {}),
...(entrypoints.gui ? { gui: true as const } : {}),
},
gui: entrypoints.gui
? { file: entrypoints.gui, revision: Hash.sha256(JSON.stringify([revision, bundle])), options: guiOptions }
: undefined,
revision,
source: source(operation.target, installed?.version),
effect: (host) => plugin.effect({ ...host, options: operation.options }),
} satisfies Generation
})
const loadServer = Effect.fn("PluginModule.loadServer")(function* (
entrypoint: string,
sources: ReturnType<typeof createPluginSources> | undefined,
revision: string | undefined,
) {
const loaded = yield* Effect.promise(() =>
sources ? sources.read(entrypoint) : Host.load(entrypoint).then((module) => ({ module, version: revision })),
)
const value = (yield* Schema.decodeUnknownEffect(Module)(loaded.module).pipe(
Effect.mapError(
(cause) =>
new LoadError({
message: "Plugin must export a default definition with an id and an effect or setup function.",
cause,
}),
),
)).default
return { plugin: "effect" in value ? value : PluginPromise.fromPromise(value), version: loaded.version }
})