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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,15 @@ Config sources — first defined value wins per field:
5. plugin options (directory-package registrations only)
6. env `TYPESAFE_API_KEY` / `JEV_API_URL` / `JEV_MIN_CONFIDENCE`

Config-file fields: `apiKey`, `apiUrl`, `minConfidence`, `model`, `timeoutMs`
(milliseconds; non-positive or bogus values count as unset) — see
[jev.yaml.example](./jev.yaml.example). `JEV_MODEL` / `JEV_TIMEOUT_MS` are env
fallbacks for `model` / `timeoutMs`.

| Env var | Default | Meaning |
| --- | --- | --- |
| `JEV_MODEL` | `jev-latest` | Jev model id |
| `JEV_TIMEOUT_MS` | `2000` | Jev round-trip timeout |
| `JEV_MODEL` | `jev-latest` | Jev model id (fallback for `model`) |
| `JEV_TIMEOUT_MS` | `2000` | Jev round-trip timeout (fallback for `timeoutMs`) |
| `JEV_DEBUG_FILE` | `/tmp/opencode-jev.log` | decision log path |
| `JEV_DEBUG` | — | `1` echoes the log to stdout |
| `JEV_DEBUG_MAX_BYTES` | `262144` | log rotation cap (one `.1` backup) |
Expand Down
13 changes: 8 additions & 5 deletions docs/DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ inspection` → `Jev client` → `interception core` → `transport` → `entryp
`./.opencode/jev.{yaml,json}` → `~/.config/jev/config.{yaml,json}` →
plugin `options` (directory-package registrations only) → env (`TYPESAFE_API_KEY` / `JEV_API_URL` /
`JEV_MIN_CONFIDENCE`).
`JEV_MODEL` / `JEV_TIMEOUT_MS` are env-only extras. Constants like
`JEV_MODEL` / `JEV_TIMEOUT_MS` are env fallbacks for the `model` / `timeoutMs`
fields (non-positive or bogus `timeoutMs` counts as unset). Constants like
`JEV_DEBUG_FILE` are captured at **module load**, not at setup.

## 3. Dev setup
Expand Down Expand Up @@ -204,10 +205,12 @@ export TYPESAFE_API_KEY="***" # also: JEV_API_URL, JEV_MIN_CONFIDENCE

Env vars must be visible to the **OpenCode server process** — export them in
the shell you launch `opencode` from (a macOS GUI-launched app won't see your
zsh exports; use route A or B there). The debug/behavior knobs
`JEV_MODEL`, `JEV_TIMEOUT_MS`, `JEV_DEBUG`, `JEV_DEBUG_FILE`,
`JEV_DEBUG_MAX_BYTES` are **env-only**, read once at module load — put them
in the shell you start OpenCode from.
zsh exports; use route A or B there). The debug knobs `JEV_DEBUG`,
`JEV_DEBUG_FILE`, `JEV_DEBUG_MAX_BYTES` are **env-only**, read once at module
load — put them in the shell you start OpenCode from. `JEV_MODEL` /
`JEV_TIMEOUT_MS` are env fallbacks for the `model` / `timeoutMs` config
fields — a config-file value wins over them; they are read at setup, not
module load.

### Watch it work

Expand Down
5 changes: 3 additions & 2 deletions docs/HOW_IT_WORKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ with N tool iterations and T tool schemas saves roughly `(N_applied) ×
- **Smaller request = faster prefill.** Fewer input tokens means less
prefill work at the provider, every iteration.
- **The Jev round-trip is the only added latency** — and it's bounded:
`JEV_TIMEOUT_MS` caps it (default 2000 ms worst case), and a Jev miss
`timeoutMs` caps it (env `JEV_TIMEOUT_MS` fallback; default 2000 ms worst
case), and a Jev miss
(timeout/error/low confidence) costs at most that one round-trip, never a
retry storm.
- **After a bad API key (401/403) the plugin latches off** — zero added
Expand All @@ -155,7 +156,7 @@ session*. The request is sent untouched when:
- Jev's pick isn't in this request's tool list
- The request already pins a tool (`tool_choice` is an object or `"none"`) or
carries no usable tools
- Jev times out (≤ `JEV_TIMEOUT_MS`, default 2 s), errors, returns malformed
- Jev times out (≤ `timeoutMs`, default 2 s), errors, returns malformed
JSON, or the key is invalid
- The traffic isn't the agent loop (`event.kind !== "primary"` — title
generation, compaction)
Expand Down
5 changes: 5 additions & 0 deletions jev.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ apiKey: "apikey_REPLACE_ME"

# Optional. Jev model. Default "jev-latest".
# model: "jev-latest"

# Optional. Jev round-trip timeout, milliseconds. Default 2000.
# Non-positive or non-numeric values count as unset (lower layer, then env
# JEV_TIMEOUT_MS, then default).
# timeoutMs: 2000
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import { Plugin } from "@opencode/plugin";
* 6. plugin options (ctx.options; only delivered when registered as a
* directory package under "plugins", not for auto-discovered files)
* 7. env TYPESAFE_API_KEY / JEV_API_URL / JEV_MIN_CONFIDENCE
* Extras: JEV_MODEL (default "jev-latest"), JEV_TIMEOUT_MS (default 2000).
* Env fallbacks: JEV_MODEL (for `model`), JEV_TIMEOUT_MS (for `timeoutMs`,
* default 2000). Non-positive/bogus `timeoutMs` in a layer counts as unset.
*
* Logging: OpenCode's background service discards plugin console output, so
* every decision is appended to /tmp/opencode-jev.log (override with
Expand Down Expand Up @@ -199,11 +200,20 @@ function pickString(layers: RawConfig[], key: string): string | undefined {
return undefined;
}

function pickNumber(layers: RawConfig[], key: string): number | undefined {
function pickNumber(
layers: RawConfig[],
key: string,
validate: (value: number) => boolean = () => true,
): number | undefined {
for (const layer of layers) {
const raw = layer[key];
const value = typeof raw === "string" ? Number(raw) : raw;
if (typeof value === "number" && Number.isFinite(value)) return value;
if (
typeof value === "number" &&
Number.isFinite(value) &&
validate(value)
)
return value;
}
return undefined;
}
Expand All @@ -226,7 +236,9 @@ async function loadConfig(
apiKey,
apiUrl: pickString(layers, "apiUrl") ?? DEFAULT_API_URL,
minConfidence: Math.min(Math.max(confidence, 0), 1),
timeoutMs: positiveIntFromEnv("JEV_TIMEOUT_MS", DEFAULT_TIMEOUT_MS),
timeoutMs:
pickNumber(layers, "timeoutMs", (v) => v > 0) ??
positiveIntFromEnv("JEV_TIMEOUT_MS", DEFAULT_TIMEOUT_MS),
model:
pickString(layers, "model") ?? process.env.JEV_MODEL ?? DEFAULT_MODEL,
};
Expand Down
48 changes: 48 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,51 @@ test("20. single function tool: passthrough, no fetch", async () => {
assert.deepEqual(await bodyJson(event), payload);
assert.equal(calls.length, 0); // nothing for jev to decide, no round-trip
});

test("21. config file timeoutMs beats env JEV_TIMEOUT_MS", async () => {
const cfgPath = path.join(tempDir(), "jev.yaml");
writeFileSync(cfgPath, "timeoutMs: 50\n");
const { calls, holder } = await setupPlugin(
{
TYPESAFE_API_KEY: "test-key",
JEV_CONFIG_PATH: cfgPath,
JEV_TIMEOUT_MS: "5000",
},
(_url, init) =>
new Promise((_resolve, reject) => {
init.signal.addEventListener("abort", () => {
reject(init.signal.reason ?? new Error("aborted"));
});
}),
);
assert.ok(holder.captured);
const payload = anthropicPayload();
const event = jsonEvent(ANTHROPIC_URL, payload);
const t0 = Date.now();
await holder.captured!(event); // must abort at ~50ms, not hang to env's 5000
const elapsed = Date.now() - t0;
assert.ok(elapsed < 1000, `expected ~50ms file timeout, took ${elapsed}ms`);
assert.deepEqual(await bodyJson(event), payload); // passthrough
assert.equal(calls.length, 1);
});

test("22. bogus config timeoutMs (0 / non-numeric): default used, routes normally", async () => {
for (const bogus of ["0", '"abc"']) {
const cfgPath = path.join(tempDir(), "jev.yaml");
writeFileSync(cfgPath, `timeoutMs: ${bogus}\n`);
const { calls, holder } = await setupPlugin(
{ TYPESAFE_API_KEY: "test-key", JEV_CONFIG_PATH: cfgPath },
() => jevResponse(choice("bash", 0.95)),
);
assert.ok(holder.captured);
const payload = anthropicPayload();
const event = jsonEvent(ANTHROPIC_URL, payload);
await holder.captured!(event); // no hang, no throw
// Non-positive/NaN counts as unset -> default 2000ms applies, so the
// instant stub answer arrives and the trim happens (a 0ms timeout would
// abort before the response and passthrough instead).
const body = await bodyJson(event);
assert.deepEqual(body.tools, [{ name: "bash", input_schema: {} }]);
assert.equal(calls.length, 1);
}
});
Loading