Skip to content

Commit 8dd993d

Browse files
committed
fix(tui): stabilize reload connection state
1 parent 24ab17e commit 8dd993d

16 files changed

Lines changed: 224 additions & 19 deletions

File tree

‎.opencode/plugins/sample-agent.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default {
2+
id: "sample-agent-plugin",
3+
setup: async (ctx) => {
4+
await ctx.agent.transform((agents) => {
5+
agents.update("sample-plugin-agent", (agent) => {
6+
agent.description = "Example subagent registered by .opencode/plugins/sample-agent.ts"
7+
agent.mode = "subagent"
8+
agent.prompt = [
9+
"You are the sample plugin agent for this repository.",
10+
"Use this agent to verify that local plugin auto-discovery can add agents.",
11+
"Keep responses concise and explain which plugin registered you when asked.",
12+
].join("\n")
13+
})
14+
})
15+
},
16+
}

‎packages/cli/src/commands/handlers/serve.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default Runtime.handler(
3838
createOpencodeClient({
3939
baseUrl: HttpServer.formatAddress(address),
4040
headers: ServerAuth.headers({ password }),
41-
}).v2.location.get(undefined, { throwOnError: true }),
41+
}).v2.health.get({}),
4242
)
4343
if (input.service) yield* daemon.register(address)
4444
const url = HttpServer.formatAddress(address)

‎packages/core/src/config/plugin/external.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ const PluginModule = Schema.Struct({
2929
]),
3030
})
3131

32+
const PluginPackage = Schema.Struct({
33+
exports: Schema.optional(Schema.Unknown),
34+
main: Schema.optional(Schema.String),
35+
module: Schema.optional(Schema.String),
36+
})
37+
3238
export const Plugin = define({
3339
id: "config-plugin",
3440
effect: Effect.fn(function* (ctx) {
@@ -65,8 +71,30 @@ export const Plugin = define({
6571
symlink: true,
6672
})
6773
.pipe(Effect.orElseSucceed(() => []))
74+
const directories = yield* fs
75+
.glob("{plugin,plugins}/*", {
76+
cwd: entry.path,
77+
absolute: true,
78+
include: "all",
79+
dot: true,
80+
symlink: true,
81+
})
82+
.pipe(
83+
Effect.flatMap((items) =>
84+
Effect.filter(items, (item) => fs.isDir(item), {
85+
concurrency: "unbounded",
86+
}),
87+
),
88+
Effect.orElseSucceed(() => []),
89+
)
90+
const packages = yield* Effect.forEach(
91+
directories.sort(),
92+
(directory) => resolvePackageEntrypoint(fs, directory),
93+
{ concurrency: "unbounded" },
94+
).pipe(Effect.map((items) => items.filter((item): item is string => item !== undefined)))
6895
files.sort()
6996
for (const file of files) configured.push({ package: file })
97+
for (const file of packages) configured.push({ package: file })
7098
}
7199
}
72100

@@ -89,3 +117,18 @@ export const Plugin = define({
89117
})
90118
}),
91119
})
120+
121+
const resolvePackageEntrypoint = Effect.fnUntraced(function* (fs: FSUtil.Interface, directory: string) {
122+
const pkg = yield* fs.readJson(path.join(directory, "package.json")).pipe(
123+
Effect.flatMap(Schema.decodeUnknownEffect(PluginPackage)),
124+
Effect.catch(() => Effect.succeed(undefined)),
125+
)
126+
const exported = typeof pkg?.exports === "string" ? pkg.exports : undefined
127+
const entries = [exported, pkg?.module, pkg?.main, "index.ts", "index.js"]
128+
129+
return yield* Effect.forEach(entries, (entry) => {
130+
if (!entry) return Effect.succeed(undefined)
131+
const file = path.resolve(directory, entry)
132+
return fs.isFile(file).pipe(Effect.map((exists) => (exists ? file : undefined)))
133+
}).pipe(Effect.map((items) => items.find((item): item is string => item !== undefined)))
134+
})

‎packages/core/src/observability/logging.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Formatter, Logger, type LogLevel } from "effect"
22
import path from "path"
33
import { Global } from "../global"
4+
import { InstallationChannel, InstallationLocal } from "../installation/version"
45
import { runID } from "./shared"
56

67
function formatter(id: string = runID) {
@@ -46,9 +47,14 @@ function format(input: unknown) {
4647
return /^[^\s="\\]+$/.test(value) ? value : JSON.stringify(value)
4748
}
4849

49-
export function fileLogger(file = path.join(Global.Path.log, "opencode.log"), id: string = runID) {
50+
export function file(local = InstallationLocal, channel = InstallationChannel) {
51+
if (!local) return path.join(Global.Path.log, "opencode.log")
52+
return path.join(Global.Path.log, `opencode-${channel.replace(/[^a-zA-Z0-9._-]/g, "-")}.log`)
53+
}
54+
55+
export function fileLogger(target = file(), id: string = runID) {
5056
// Do not set batchWindow to 0; it causes high idle CPU usage.
51-
return Logger.toFile(formatter(id), file, { flag: "a" })
57+
return Logger.toFile(formatter(id), target, { flag: "a" })
5258
}
5359

5460
const stderrLogger = Logger.make((options) => process.stderr.write(formatter().log(options) + "\n"))

‎packages/core/src/plugin/models-dev.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { define } from "./internal"
2+
import type { ModelV2Info } from "@opencode-ai/sdk/v2/types"
23
import { Effect, Stream } from "effect"
34
import { EventV2 } from "../event"
45
import { ModelV2 } from "../model"
@@ -45,6 +46,15 @@ function variants(model: ModelsDev.Model) {
4546
}))
4647
}
4748

49+
function mergeVariants(model: ModelV2Info, next: ModelV2Info["variants"]) {
50+
const existing = new Map(model.variants.map((variant) => [variant.id, variant]))
51+
const nextIDs = new Set(next.map((variant) => variant.id))
52+
model.variants = [
53+
...next.map((variant) => existing.get(variant.id) ?? variant),
54+
...model.variants.filter((variant) => !nextIDs.has(variant.id)),
55+
]
56+
}
57+
4858
export const ModelsDevPlugin = define({
4959
id: "models-dev",
5060
effect: Effect.fn(function* (ctx) {
@@ -111,7 +121,7 @@ export const ModelsDevPlugin = define({
111121
input: [...(model.modalities?.input ?? [])],
112122
output: [...(model.modalities?.output ?? [])],
113123
}
114-
draft.variants = variants(model)
124+
mergeVariants(draft, variants(model))
115125
draft.time.released = released(model.release_date)
116126
draft.cost = cost(model.cost)
117127
draft.status = model.status ?? "active"

‎packages/core/src/plugin/provider/opencode.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,16 @@ export const OpencodePlugin = define<HttpClient.HttpClient | EventV2.Service | S
142142
Object.assign(model.request.headers, config.headers)
143143
Object.assign(model.request.body, lowerer.request(withoutCredentials(config.options)))
144144
if (config.variants !== undefined) {
145-
model.variants = Object.entries(config.variants).map(([id, options]) => ({
146-
id: ModelV2.VariantID.make(id),
147-
headers: { ...(options.headers ?? {}) },
148-
body: lowerer.request(withoutCredentials(options)),
149-
}))
145+
for (const [id, options] of Object.entries(config.variants)) {
146+
const variantID = ModelV2.VariantID.make(id)
147+
let existing = model.variants.find((item) => item.id === variantID)
148+
if (!existing) {
149+
existing = { id: variantID, headers: {}, body: {} }
150+
model.variants.push(existing)
151+
}
152+
Object.assign(existing.headers, options.headers)
153+
Object.assign(existing.body, lowerer.request(withoutCredentials(options)))
154+
}
150155
}
151156
if (config.release_date !== undefined) {
152157
const released = Date.parse(config.release_date)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { define } from "@opencode-ai/plugin/v2/promise"
2+
3+
export default define({
4+
id: "folder-plugin",
5+
setup: async (ctx) => {
6+
await ctx.agent.transform((agents) => {
7+
agents.update("folder", (agent) => {
8+
agent.description = "Loaded from plugin folder"
9+
agent.mode = "subagent"
10+
})
11+
})
12+
},
13+
})

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ describe("ConfigExternalPlugin", () => {
234234
description: "Loaded from plugin directory",
235235
mode: "subagent",
236236
})
237+
expect(yield* waitForAgent(agents, "folder")).toMatchObject({
238+
description: "Loaded from plugin folder",
239+
mode: "subagent",
240+
})
237241
}),
238242
)
239243
})

‎packages/core/test/logging.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { describe, expect, test } from "bun:test"
2+
import path from "path"
3+
import { Global } from "@opencode-ai/core/global"
4+
import { Logging } from "@opencode-ai/core/observability/logging"
5+
6+
describe("Logging", () => {
7+
test("uses a local-specific log file for local installs", () => {
8+
expect(Logging.file(true, "local")).toBe(path.join(Global.Path.log, "opencode-local.log"))
9+
})
10+
11+
test("keeps non-local installs on the default log file", () => {
12+
expect(Logging.file(false, "next")).toBe(path.join(Global.Path.log, "opencode.log"))
13+
})
14+
})

‎packages/core/test/plugin/fixtures/models-dev.json‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@
99
"id": "local",
1010
"name": "Local",
1111
"env": [],
12-
"models": {}
12+
"models": {
13+
"model": {
14+
"id": "model",
15+
"name": "Local Model",
16+
"release_date": "2026-01-01",
17+
"attachment": false,
18+
"reasoning": true,
19+
"temperature": true,
20+
"tool_call": true,
21+
"limit": { "context": 1000, "output": 100 },
22+
"experimental": {
23+
"modes": {
24+
"high": {
25+
"provider": { "body": { "reasoning_effort": "high" } }
26+
},
27+
"max": {
28+
"provider": { "headers": { "x-variant": "max" }, "body": { "reasoning_effort": "max" } }
29+
}
30+
}
31+
}
32+
}
33+
}
1334
}
1435
}

0 commit comments

Comments
 (0)