Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
34 changes: 31 additions & 3 deletions plugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/driver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand All @@ -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) },
Expand Down
25 changes: 24 additions & 1 deletion test/run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
Loading