diff --git a/README.md b/README.md index 4055010..2d95f2b 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,34 @@ tell you things you cannot see. You already know that you cancelled it. The green hold stays until the next turn starts. The device holds the last program, so the plugin needs no timer to keep the color. A failed turn stays red the same way. +## Brightness + +In direct mode, the plugin writes each LED program at full brightness. Direct mode is the +mode without the SidePulse service (see [Two modes](#two-modes)). To dim the LEDs, +set the `brightness` option to an integer from 0 (off) to 255 (full). + +On OpenCode 2, use the object form of the plugin entry in `opencode.json(c)`: + +```jsonc +{ + "$schema": "https://opencode.ai/config.json", + "plugins": [{ "package": "opencode-sidepulse", "options": { "brightness": 40 } }] +} +``` + +On OpenCode 1, use a tuple in `plugin`: + +```json +{ + "$schema": "https://opencode.ai/config.json", + "plugin": [["opencode-sidepulse", { "brightness": 40 }]] +} +``` + +The plugin ignores a value that is not an integer from 0 to 255, and uses full +brightness. In app mode, the plugin ignores this option. The SidePulse service applies +the brightness from its own settings. + ## Two modes The SidePulse background service also writes the LED control file. Two writers on one file diff --git a/package.json b/package.json index efcdc1e..aecf7ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opencode-sidepulse", - "version": "0.3.0", + "version": "0.4.0", "description": "Show opencode agent status on a SidePulse Pro or SidePulse Dot LED device.", "keywords": [ "opencode", diff --git a/plugin.mjs b/plugin.mjs index a795d5c..f04e6a3 100644 --- a/plugin.mjs +++ b/plugin.mjs @@ -53,6 +53,25 @@ function mount() { return undefined } +// Direct mode only. In app mode the service applies its own brightness setting. +let brightness + +// The controller scales RGB by `brightness N`, 0 to 255. Anything else keeps full brightness. +function parseBrightness(value) { + return Number.isInteger(value) && value >= 0 && value <= 255 ? value : undefined +} + +function configure(options) { + const next = parseBrightness(options?.brightness) + if (next === brightness) return + brightness = next + shown = undefined +} + +function program(state) { + return brightness === undefined ? PROGRAMS[state] : `brightness ${brightness}\n${PROGRAMS[state]}` +} + let shown function write(state) { @@ -63,7 +82,7 @@ function write(state) { return false } try { - writeFileSync(join(dir, "LEDS.LED"), PROGRAMS[state]) + writeFileSync(join(dir, "LEDS.LED"), program(state)) shown = state return true } catch { @@ -387,7 +406,8 @@ function onEvent(event, cwd) { const plugin = { id: "opencode-sidepulse", - async server({ directory } = {}) { + async server({ directory } = {}, options) { + configure(options) const cwd = directory ?? process.cwd() const stop = keepalive() return { @@ -400,6 +420,7 @@ const plugin = { }, async setup(ctx) { + configure(ctx.options) const cwd = ctx.location?.directory ?? process.cwd() const stop = keepalive() const controller = new AbortController() @@ -432,12 +453,19 @@ export default plugin const selfCheck = import.meta.main || process.argv[1]?.endsWith("plugin.mjs") if (selfCheck) { - for (const [name, program] of Object.entries(PROGRAMS)) { + for (const [name, plain] of Object.entries(PROGRAMS)) { + const program = `brightness 255\n${plain}` const bytes = Buffer.byteLength(program) const lines = program.trimEnd().split("\n").length if (bytes > 512 || lines > 20) throw new Error(`${name} exceeds the controller: ${bytes} bytes, ${lines} lines`) } + const levels = [[0, 0], [255, 255], [64, 64], [-1], [256], [1.5], ["64"], [null], [undefined]] + for (const [input, want] of levels) { + const got = parseBrightness(input) + if (got !== want) throw new Error(`brightness ${JSON.stringify(input)}: got ${got}, want ${want}`) + } + const signals = ["start", "tool-start", "ask", "tool-end", "reply", "idle", "error"] const want = ["busy", "busy", "waiting", "waiting", "busy", "done", "error"] const got = signals.map(stateFor) diff --git a/test/driver.mjs b/test/driver.mjs index 83edac6..de86a86 100644 --- a/test/driver.mjs +++ b/test/driver.mjs @@ -59,7 +59,7 @@ let unload // OpenCode 1 hands hooks back from server() and carries event payloads in `properties`. async function v1() { - const h = await plugin.server({ directory }) + const h = await plugin.server({ directory }, scenario.options) hooks = { prompt: (sessionID) => h["chat.message"]({ sessionID }), before: (sessionID, tool) => h["tool.execute.before"]({ tool, sessionID }), @@ -76,6 +76,7 @@ async function v2() { let wake let ended = false const ctx = { + options: scenario.options ?? {}, location: { directory }, session: { hook: async (name, fn) => void (registered[`session.${name}`] = fn) }, tool: { hook: async (name, fn) => void (registered[`tool.${name}`] = fn) }, diff --git a/test/run.mjs b/test/run.mjs index f031aaa..6013e15 100644 --- a/test/run.mjs +++ b/test/run.mjs @@ -58,6 +58,7 @@ const times = (n, steps) => Array.from({ length: n }, () => steps).flat() // cli where the CLI is found: "env" (default), "home", "path" // decoy put a wrong CLI at "home" or "path". It must never run. // hang the CLI never answers +// options plugin options from opencode.json // env extra environment for the plugin // check a function given { bodies, result } for anything else const tests = [] @@ -505,6 +506,28 @@ test("direct mode: an unchanged state is not written again", { led: "someone else\n", }) +for (const opencode of [1, 2]) + test(`direct mode: the brightness option dims every program (OpenCode ${opencode})`, { + opencode, + service: "none", + options: { brightness: 40 }, + steps: [["prompt", A], ["led"], asked(A), ["led"]], + leds: [`brightness 40\n${LED.busy}`, `brightness 40\n${LED.waiting}`], + led: `brightness 40\n${LED.waiting}`, + }) + +test("direct mode: an invalid brightness keeps full brightness", { + service: "none", + options: { brightness: "dim" }, + steps: [["prompt", A]], + led: "busy", +}) + +test("app mode: the brightness option leaves the device to the service", { + options: { brightness: 40 }, + steps: [["prompt", A]], +}) + test("direct mode: no device is not an error", { service: "none", volume: null, @@ -723,7 +746,7 @@ async function run(t) { if (t.decoy === "home") script(join(home, ".local", "bin"), "sidepulse", decoy) if (t.decoy === "path") script(bin, "sidepulse", decoy) env.PATH = `${bin}${delimiter}${process.env.PATH}` - env.SCENARIO = JSON.stringify({ opencode: t.opencode ?? 1, steps: t.steps }) + env.SCENARIO = JSON.stringify({ opencode: t.opencode ?? 1, options: t.options, steps: t.steps }) env.SCENARIO_DIR = dir env.SCENARIO_LED = led