Skip to content

Commit 7d61144

Browse files
Apply PR #22753: core: move plugin intialisation to config layer override
2 parents 316b52c + 444a13b commit 7d61144

5 files changed

Lines changed: 35 additions & 13 deletions

File tree

packages/opencode/src/cli/cmd/serve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Installation } from "../../installation"
1111
import { PushRelay } from "../../server/push-relay"
1212
import { Log } from "../../util"
1313
import * as QRCode from "qrcode"
14+
import { bootstrap } from "../bootstrap"
1415

1516
const log = Log.create({ service: "serve" })
1617

@@ -199,7 +200,7 @@ export const ServeCommand = cmd({
199200
}),
200201
describe: "starts a headless opencode server",
201202
handler: async (args) => {
202-
const opts = await resolveNetworkOptions(args)
203+
const opts = await bootstrap(process.cwd(), () => resolveNetworkOptions(args))
203204
const relayURL = (
204205
args["relay-url"] ??
205206
process.env.OPENCODE_EXPERIMENTAL_PUSH_RELAY_URL ??
@@ -254,7 +255,6 @@ export const ServeCommand = cmd({
254255
if (!Flag.OPENCODE_SERVER_PASSWORD) {
255256
console.log("Warning: OPENCODE_SERVER_PASSWORD is not set; server is unsecured.")
256257
}
257-
258258
const server = await Server.listen(opts)
259259
console.log(`opencode server listening on http://${server.hostname}:${server.port}`)
260260

packages/opencode/src/cli/cmd/tui/thread.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { UI } from "@/cli/ui"
88
import { Log } from "@/util"
99
import { errorMessage } from "@/util/error"
1010
import { withTimeout } from "@/util/timeout"
11+
import { Instance } from "@/project/instance"
1112
import { withNetworkOptions, resolveNetworkOptionsNoConfig } from "@/cli/network"
1213
import { Filesystem } from "@/util"
1314
import type { GlobalEvent } from "@opencode-ai/sdk/v2"
@@ -178,7 +179,11 @@ export const TuiThreadCommand = cmd({
178179
const prompt = await input(args.prompt)
179180
const config = await TuiConfig.get()
180181

181-
const network = resolveNetworkOptionsNoConfig(args)
182+
const network = await Instance.provide({
183+
directory: cwd,
184+
fn: () => resolveNetworkOptionsNoConfig(args),
185+
})
186+
182187
const external =
183188
process.argv.includes("--port") ||
184189
process.argv.includes("--hostname") ||

packages/opencode/src/cli/cmd/web.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { withNetworkOptions, resolveNetworkOptions } from "../network"
55
import { Flag } from "../../flag/flag"
66
import open from "open"
77
import { networkInterfaces } from "os"
8+
import { bootstrap } from "../bootstrap"
89

910
function getNetworkIPs() {
1011
const nets = networkInterfaces()
@@ -36,7 +37,7 @@ export const WebCommand = cmd({
3637
if (!Flag.OPENCODE_SERVER_PASSWORD) {
3738
UI.println(UI.Style.TEXT_WARNING_BOLD + "! OPENCODE_SERVER_PASSWORD is not set; server is unsecured.")
3839
}
39-
const opts = await resolveNetworkOptions(args)
40+
const opts = await bootstrap(process.cwd(), () => resolveNetworkOptions(args))
4041
const server = await Server.listen(opts)
4142
UI.empty()
4243
UI.println(UI.logo(" "))

packages/opencode/src/effect/app-runtime.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,32 @@ import { Installation } from "@/installation"
4747
import { ShareNext } from "@/share"
4848
import { SessionShare } from "@/share"
4949
import { Npm } from "@opencode-ai/shared/npm"
50+
import * as Effect from "effect/Effect"
51+
52+
// Adjusts the default Config layer to ensure that plugins are always initialised before
53+
// any other layers read the current config
54+
const ConfigWithPluginPriority = Layer.effect(
55+
Config.Service,
56+
Effect.gen(function* () {
57+
const config = yield* Config.Service
58+
const plugin = yield* Plugin.Service
59+
60+
return {
61+
...config,
62+
get: () => Effect.andThen(plugin.init(), config.get),
63+
getGlobal: () => Effect.andThen(plugin.init(), config.getGlobal),
64+
getConsoleState: () => Effect.andThen(plugin.init(), config.getConsoleState),
65+
}
66+
}),
67+
).pipe(Layer.provide(Layer.merge(Plugin.defaultLayer, Config.defaultLayer)))
5068

5169
export const AppLayer = Layer.mergeAll(
5270
Npm.defaultLayer,
5371
AppFileSystem.defaultLayer,
5472
Bus.defaultLayer,
5573
Auth.defaultLayer,
5674
Account.defaultLayer,
57-
Config.defaultLayer,
75+
ConfigWithPluginPriority,
5876
Git.defaultLayer,
5977
Ripgrep.defaultLayer,
6078
File.defaultLayer,

packages/opencode/src/project/bootstrap.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Plugin } from "../plugin"
21
import { Format } from "../format"
32
import { LSP } from "../lsp"
43
import { File } from "../file"
@@ -8,6 +7,7 @@ import * as Vcs from "./vcs"
87
import { Bus } from "../bus"
98
import { Command } from "../command"
109
import { Instance } from "./instance"
10+
import { Plugin } from "../plugin"
1111
import { Log } from "@/util"
1212
import { FileWatcher } from "@/file/watcher"
1313
import { ShareNext } from "@/share"
@@ -16,12 +16,10 @@ import { Config } from "@/config"
1616

1717
export const InstanceBootstrap = Effect.gen(function* () {
1818
Log.Default.info("bootstrapping", { directory: Instance.directory })
19-
// everything depends on config so eager load it for nice traces
20-
yield* Config.Service.use((svc) => svc.get())
21-
// Plugin can mutate config so it has to be initialized before anything else.
22-
yield* Plugin.Service.use((svc) => svc.init())
23-
yield* Effect.all(
24-
[
19+
yield* Effect.all([
20+
Config.Service.use((i) => i.get()),
21+
...[
22+
Plugin.Service,
2523
LSP.Service,
2624
ShareNext.Service,
2725
Format.Service,
@@ -30,7 +28,7 @@ export const InstanceBootstrap = Effect.gen(function* () {
3028
Vcs.Service,
3129
Snapshot.Service,
3230
].map((s) => Effect.forkDetach(s.use((i) => i.init()))),
33-
).pipe(Effect.withSpan("InstanceBootstrap.init"))
31+
]).pipe(Effect.withSpan("InstanceBootstrap.init"))
3432

3533
yield* Bus.Service.use((svc) =>
3634
svc.subscribeCallback(Command.Event.Executed, async (payload) => {

0 commit comments

Comments
 (0)