opencode-rag-plugin 2.3.1 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ReadMe.md +71 -8
- package/dist/chunker/skeleton.js +1 -1
- package/dist/cli/commands/decide.d.ts +25 -0
- package/dist/cli/commands/decide.js +160 -0
- package/dist/cli/commands/eval.d.ts +23 -0
- package/dist/cli/commands/eval.js +331 -1
- package/dist/cli/commands/index-command.js +28 -10
- package/dist/cli/commands/index.d.ts +2 -1
- package/dist/cli/commands/index.js +2 -1
- package/dist/cli/commands/init-helpers.d.ts +16 -1
- package/dist/cli/commands/init-helpers.js +84 -2
- package/dist/cli/commands/init.js +29 -4
- package/dist/cli/commands/status.js +14 -8
- package/dist/cli/format.d.ts +50 -0
- package/dist/cli/format.js +122 -0
- package/dist/cli/index.js +4 -1
- package/dist/cli/types.d.ts +2 -0
- package/dist/core/bootstrap.js +1 -0
- package/dist/core/config.d.ts +47 -0
- package/dist/core/config.js +34 -1
- package/dist/core/interfaces.d.ts +69 -0
- package/dist/decision/factory.d.ts +17 -0
- package/dist/decision/factory.js +17 -0
- package/dist/decision/format.d.ts +14 -0
- package/dist/decision/format.js +53 -0
- package/dist/decision/systemone.d.ts +76 -0
- package/dist/decision/systemone.js +234 -0
- package/dist/decision/validate.d.ts +26 -0
- package/dist/decision/validate.js +74 -0
- package/dist/embedder/factory.d.ts +1 -1
- package/dist/embedder/factory.js +80 -3
- package/dist/eval/decide-calibration.d.ts +112 -0
- package/dist/eval/decide-calibration.js +185 -0
- package/dist/eval/golden-gate.d.ts +65 -0
- package/dist/eval/golden-gate.js +61 -0
- package/dist/eval/golden-set.d.ts +81 -0
- package/dist/eval/golden-set.js +155 -0
- package/dist/eval/rerank-eval.js +10 -125
- package/dist/index.d.ts +6 -2
- package/dist/index.js +4 -0
- package/dist/indexer/pipeline.d.ts +0 -10
- package/dist/indexer/pipeline.js +1 -0
- package/dist/mcp/handlers.d.ts +17 -1
- package/dist/mcp/handlers.js +30 -0
- package/dist/mcp/server.d.ts +9 -1
- package/dist/mcp/server.js +47 -3
- package/dist/opencode/system-guidance.d.ts +11 -0
- package/dist/opencode/system-guidance.js +53 -0
- package/dist/opencode/tools.d.ts +24 -1
- package/dist/opencode/tools.js +101 -0
- package/dist/plugin.js +21 -3
- package/dist/quirks/quirk-store.d.ts +2 -0
- package/dist/quirks/quirk-store.js +33 -4
- package/dist/tui.js +21 -2
- package/dist/v2/v2-adapter.d.ts +20 -0
- package/dist/v2/v2-adapter.js +36 -8
- package/dist/vectorstore/lancedb.js +54 -5
- package/dist/watcher.d.ts +26 -1
- package/dist/watcher.js +71 -9
- package/dist/web/ui/assets/index-CWcp0jNf.css +2 -0
- package/dist/web/ui/index.html +2 -2
- package/package.json +5 -2
- package/dist/web/ui/assets/index-Dp009QCQ.css +0 -1
- /package/dist/web/ui/assets/{index-C0QXKA-b.js → index-DbXFrn53.js} +0 -0
package/ReadMe.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# OpenCodeRAG
|
|
2
2
|
|
|
3
|
-
OpenCodeRAG is a **local-first RAG plugin** for semantic code and image search. It converts your codebase into vector indices and retrieves relevant code chunks on natural language queries. The primary aim is to save tokens by replacing full-file reads with targeted chunk retrieval and to speed-up tool calls for large codebases. Integrates seamlessly with [OpenCode](https://opencode.ai) and works as a standalone MCP server or CLI tool for other AI harnesses.
|
|
3
|
+
OpenCodeRAG is a **local-first RAG plugin** for semantic code and image search, with a built-in **local decision model** for fast classification. It converts your codebase into vector indices and retrieves relevant code chunks on natural language queries. The primary aim is to save tokens by replacing full-file reads with targeted chunk retrieval and to speed-up tool calls for large codebases. Integrates seamlessly with [OpenCode](https://opencode.ai) and works as a standalone MCP server or CLI tool for other AI harnesses.
|
|
4
4
|
|
|
5
5
|
You don't need a dedicated GPU to run smaller embedding LLMs, as these models can still run performant on modern CPUs.
|
|
6
6
|
|
|
@@ -48,10 +48,11 @@ workspace, and `opencode-rag update` upgrades to a newer release.
|
|
|
48
48
|
| **Document support** | Markdown, LaTeX, PDF, DOCX, DOC, Excel |
|
|
49
49
|
| **Image indexing** | Describe images via vision LLM and store descriptions as searchable vector chunks |
|
|
50
50
|
| **Hybrid search** | Vector similarity + TF×IDF keyword fusion |
|
|
51
|
+
| **Decision model** | Local [tev1](https://ollama.com/library/tev1) classification — `make_decision` labels, routes, and scores short text with probabilities (40–150 ms warm); optional pre-ask routing so the agent asks fewer questions |
|
|
51
52
|
| **OpenCode plugin** | Auto-inject context, read-tool override, TUI settings, Ctrl+Enter to add RAG context, MCP registration on `init` |
|
|
52
53
|
| **Incremental indexing** | File-hash manifest, background watcher, auto-rebuild on corruption |
|
|
53
54
|
| **Privacy-first** | All processing stays local (when using Ollama) |
|
|
54
|
-
| **CLI Tools** | `init`, `index`, `query`, `status`, `list`, `show`, `dump`, `clear`, `describe-image`, `ui`, `mcp`, `setup`, `quirk`, `eval:sessions`, `eval:analyze`, `eval:compare` |
|
|
55
|
+
| **CLI Tools** | `init`, `index`, `query`, `status`, `list`, `show`, `dump`, `clear`, `describe-image`, `decide`, `ui`, `mcp`, `setup`, `quirk`, `eval:sessions`, `eval:analyze`, `eval:compare`, `eval:gate`, `eval:decide` |
|
|
55
56
|
| **Proxy-aware** | Corporate proxy support with raw-socket localhost bypass |
|
|
56
57
|
| **OpenAI / Anthropic / Cohere** | Use alternate embedding providers with API key auto-resolution |
|
|
57
58
|
| **Evaluation** | Session-level token tracking, RAG-on vs RAG-off comparison, tiktoken BPE counting |
|
|
@@ -75,7 +76,7 @@ Launch with `opencode-rag ui`. See [Web UI documentation](doc/webui.md) for deta
|
|
|
75
76
|
|---|---|
|
|
76
77
|
| [Architecture](doc/architecture.md) | Module design, data flow, tech stack |
|
|
77
78
|
| [Installation](doc/installation.md) | Full install guide, global setup, uninstall |
|
|
78
|
-
| [Configuration](doc/configuration.md) | All options: embedding, indexing, retrieval, description, image description, plugin |
|
|
79
|
+
| [Configuration](doc/configuration.md) | All options: embedding, indexing, retrieval, description, image description, decision, plugin |
|
|
79
80
|
| [Chunking](doc/chunking.md) | Language matrix, adding new chunkers, custom chunkers |
|
|
80
81
|
| [Embedding](doc/embedding.md) | Providers, model recommendations, proxy, dimension probing |
|
|
81
82
|
| [Retrieval](doc/retrieval.md) | Pipeline, hybrid search, score fusion, caching |
|
|
@@ -87,6 +88,63 @@ Launch with `opencode-rag ui`. See [Web UI documentation](doc/webui.md) for deta
|
|
|
87
88
|
| [Troubleshooting](doc/troubleshooting.md) | Common issues, logging, debugging |
|
|
88
89
|
| [Roadmap](doc/roadmap.md) | Completed items, short/mid/long-term plans |
|
|
89
90
|
|
|
91
|
+
## Decision Model (`make_decision`)
|
|
92
|
+
|
|
93
|
+
OpenCodeRAG answers fast **classification, routing, policy, and scoring questions** with a local decision model — Together AI's [tev1](https://ollama.com/library/tev1) via Ollama's `/v1/systemone` endpoint. Unlike chat models, tev1 scores a short `state` against 1–64 named questions and returns the chosen option with probabilities — typically in **40–150 ms warm**, without spending agent tokens.
|
|
94
|
+
|
|
95
|
+
| Question type | Use it for | Example |
|
|
96
|
+
|---|---|---|
|
|
97
|
+
| `choice` | Pick one of 2–24 options | "Which support intent is this?" → duplicate_charge / cancel / none |
|
|
98
|
+
| `noul` | True/false probability | "Does the customer explicitly ask for a refund?" |
|
|
99
|
+
| `score` | Place the input on a rubric | "How severe is this incident?" → cosmetic … critical |
|
|
100
|
+
|
|
101
|
+
One call, two questions:
|
|
102
|
+
|
|
103
|
+
```json
|
|
104
|
+
{
|
|
105
|
+
"state": "Customer: I was charged twice for the October subscription ($19.99 each). Please refund the extra charge.",
|
|
106
|
+
"questions": [
|
|
107
|
+
{ "id": "intent", "type": "choice", "instructions": "Which support intent matches?",
|
|
108
|
+
"criteria": { "duplicate_charge": "Charged more than once", "cancel": "Wants to cancel", "none": "None of the above" } },
|
|
109
|
+
{ "id": "refund", "type": "noul", "instructions": "Does the customer explicitly ask for a refund?" }
|
|
110
|
+
]
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
**Decision** — ollama/tev1:0.8b
|
|
116
|
+
|
|
117
|
+
- **intent** = `duplicate_charge` (confidence 0.97)
|
|
118
|
+
- **refund** = true (p=0.96)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**Prerequisites:** Ollama ≥ 0.35 and a pulled model:
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
ollama pull tev1:0.8b # 812 MB, fast — good for quick labels and CI
|
|
125
|
+
ollama pull tev1:4b # 4.4 GB, more accurate — recommended when scoring
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**Disabled by default** — opt in via `decision.enabled`:
|
|
129
|
+
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"decision": {
|
|
133
|
+
"enabled": true,
|
|
134
|
+
"provider": "ollama",
|
|
135
|
+
"model": "tev1:0.8b",
|
|
136
|
+
"keepAlive": "30m",
|
|
137
|
+
"routeBeforeAsking": false
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
**Using it well:** always include a `none` / "cannot be determined" option; keep `state` to the relevant excerpt (the effective context is ~2k tokens); treat `confidence < ~0.3` as undecided; don't use it for factual recall or as the only check for high-stakes decisions.
|
|
143
|
+
|
|
144
|
+
**Pre-ask routing (optional):** with `decision.routeBeforeAsking: true`, the injected agent guidance tells the agent to resolve option choices through `make_decision` before asking you — proceeding on a confident answer and asking only when the model is undecided or the choice is preference-based. Off by default.
|
|
145
|
+
|
|
146
|
+
`opencode-rag decide` exposes the same engine from the CLI, and `init` creates a dedicated `make-decision` agent skill. See [Plugin documentation](doc/plugin.md#make_decision) and [Configuration](doc/configuration.md#decision).
|
|
147
|
+
|
|
90
148
|
## Image Indexing
|
|
91
149
|
|
|
92
150
|
OpenCodeRAG can index image files (PNG, JPEG, WebP, etc.) by sending them to a vision-capable LLM and storing the generated text descriptions as searchable vector chunks. This makes visual assets discoverable via natural language queries (e.g., "login screen screenshot", "architecture diagram").
|
|
@@ -190,7 +248,7 @@ OpenCodeRAG ships a CLI-based [MCP (Model Context Protocol)](https://spec.modelc
|
|
|
190
248
|
opencode-rag mcp
|
|
191
249
|
```
|
|
192
250
|
|
|
193
|
-
> **Note:** The MCP server is **optional**. When running as an OpenCode plugin, the
|
|
251
|
+
> **Note:** The MCP server is **optional**. When running as an OpenCode plugin, the tools are registered **in-process** and work without the MCP server. The `chat.message` hook for hotkey injection also runs in-process. The MCP server is only needed when an **external** MCP client connects to OpenCodeRAG. The plugin auto-starts the server only if `mcp.enabled` is `true` in your config (default: `false`).
|
|
194
252
|
|
|
195
253
|
### MCP Tools
|
|
196
254
|
|
|
@@ -200,19 +258,21 @@ opencode-rag mcp
|
|
|
200
258
|
| `get_file_skeleton` | AST-based file outline (functions, classes, methods) |
|
|
201
259
|
| `find_usages` | Find all references to a symbol by name |
|
|
202
260
|
| `describe_image` | Describe an image file with the configured vision model (`imageDescription.onDemand` overrides apply) |
|
|
261
|
+
| `make_decision` | Classify/route/score short text with the local tev1 decision model (only when `decision.enabled` is `true`) |
|
|
203
262
|
|
|
204
263
|
Clients can configure the MCP server manually, or `opencode-rag init` auto-registers it.
|
|
205
264
|
|
|
206
265
|
## Agent Discovery
|
|
207
266
|
|
|
208
|
-
OpenCodeRAG registers tools that agents can invoke directly. Agents discover these tools via the OpenCode **skill system** -
|
|
267
|
+
OpenCodeRAG registers tools that agents can invoke directly. Agents discover these tools via the OpenCode **skill system** - `opencode-rag init` creates `.opencode/skills/opencode-rag/SKILL.md` (workflow guidance) and `.opencode/skills/make-decision/SKILL.md` (decision-model guidance):
|
|
209
268
|
|
|
210
269
|
1. **Skeleton first** - `get_file_skeleton(filePath)` to orient in a file
|
|
211
270
|
2. **Find usages** - `find_usages(symbolName)` before editing any symbol
|
|
212
271
|
3. **Search** - `search_semantic(query)` to find relevant code
|
|
213
272
|
4. **Describe images** - `describe_image(filePath)` when context involves an image
|
|
214
|
-
5. **
|
|
215
|
-
6. **
|
|
273
|
+
5. **Decide** - `make_decision(state, questions)` when a task reduces to a choice, true/false check, or rubric score over short text (when `decision.enabled`; with `decision.routeBeforeAsking` the agent resolves such choices before asking you)
|
|
274
|
+
6. **Read** - use `read` on specific line ranges
|
|
275
|
+
7. **Edit** - make changes with full context
|
|
216
276
|
|
|
217
277
|
### Available Tools
|
|
218
278
|
|
|
@@ -222,6 +282,7 @@ OpenCodeRAG registers tools that agents can invoke directly. Agents discover the
|
|
|
222
282
|
| `get_file_skeleton` | Quick file overview via AST | Before reading a large file to decide which sections matter |
|
|
223
283
|
| `find_usages` | Symbol reference search | **Before editing** any function, variable, or class |
|
|
224
284
|
| `describe_image` | Live image description via the configured vision model | When a user asks about a screenshot, diagram, or visual asset |
|
|
285
|
+
| `make_decision` | Live classification/routing/scoring via the local tev1 decision model (when enabled) | When a task reduces to a choice, true/false check, or rubric score over short text |
|
|
225
286
|
| `read` (optional) | RAG-enhanced file read | Full file contents with supplementary context chunks |
|
|
226
287
|
|
|
227
288
|
## OpenCode Integration
|
|
@@ -229,7 +290,7 @@ OpenCodeRAG registers tools that agents can invoke directly. Agents discover the
|
|
|
229
290
|
When using OpenCode, the plugin enhances your agent with three discovery mechanisms:
|
|
230
291
|
|
|
231
292
|
### 1. Skill-Based Discovery
|
|
232
|
-
`opencode-rag init` creates `.opencode/skills/opencode-rag/SKILL.md`
|
|
293
|
+
`opencode-rag init` creates `.opencode/skills/opencode-rag/SKILL.md` (tool workflow) and `.opencode/skills/make-decision/SKILL.md` (decision-model usage). Agents load them on demand via the `skill` tool, keeping token overhead minimal.
|
|
233
294
|
|
|
234
295
|
### 2. System Prompt Guidance
|
|
235
296
|
When chunks are indexed, a brief tool list is prepended to the system prompt so agents know the tools exist. This is skipped when no chunks are indexed to save tokens.
|
|
@@ -251,6 +312,8 @@ opencode-rag eval:compare <A> <B> # side-by-side comparison
|
|
|
251
312
|
|
|
252
313
|
Token counting uses tiktoken BPE (cl100k_base) for accurate code tokenization. See [Evaluation documentation](doc/evaluation.md) for details.
|
|
253
314
|
|
|
315
|
+
Retrieval quality has its own harness: `opencode-rag eval:gate` runs the labelled golden set and fails when Hit@5 / MRR / nDCG@10 drop below the documented floors, and `opencode-rag eval:decide` measures tev1's accuracy, ECE, and Brier score on a labelled calibration set.
|
|
316
|
+
|
|
254
317
|
---
|
|
255
318
|
|
|
256
319
|
## Privacy & Security
|
package/dist/chunker/skeleton.js
CHANGED
|
@@ -30,7 +30,7 @@ export const SKELETON_CONFIGS = {
|
|
|
30
30
|
".cxx": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
|
|
31
31
|
".h": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
|
|
32
32
|
".hpp": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
|
|
33
|
-
".cs": { grammarName: "
|
|
33
|
+
".cs": { grammarName: "c_sharp", nodeTypes: ["class_declaration", "method_declaration", "interface_declaration", "enum_declaration", "struct_declaration"] },
|
|
34
34
|
".rb": { grammarName: "ruby", nodeTypes: ["method", "class", "module"] },
|
|
35
35
|
".swift": { grammarName: "swift", nodeTypes: ["function_declaration", "class_declaration", "struct_declaration", "enum_declaration", "protocol_declaration"] },
|
|
36
36
|
".kt": { grammarName: "kotlin", nodeTypes: ["function_declaration", "class_declaration", "interface_declaration", "object_declaration"] },
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Decide command for classification/decision questions via the
|
|
3
|
+
* configured decision model (tev1 via Ollama's `/v1/systemone`).
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* `decide` command — answer classification/decision questions with a decision
|
|
7
|
+
* model.
|
|
8
|
+
*
|
|
9
|
+
* Single-question mode:
|
|
10
|
+
* opencode-rag decide "Which intent is this?" --state "..." --options a,b,c
|
|
11
|
+
* Full multi-question passthrough:
|
|
12
|
+
* opencode-rag decide --state "..." --questions '[{"id":"intent", ...}]'
|
|
13
|
+
*/
|
|
14
|
+
import type { Command } from "commander";
|
|
15
|
+
/**
|
|
16
|
+
* Register the `decide` command on the given Commander program.
|
|
17
|
+
*
|
|
18
|
+
* Loads the resolved config, validates that `decision.enabled` is set, and
|
|
19
|
+
* sends the request to the configured decision provider. State can be passed
|
|
20
|
+
* inline, from a file, or via stdin; questions can use the single-question
|
|
21
|
+
* convenience flags or the full JSON passthrough.
|
|
22
|
+
*
|
|
23
|
+
* @param program - The Commander `Command` instance to register on.
|
|
24
|
+
*/
|
|
25
|
+
export declare function registerDecideCommand(program: Command): void;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Decide command for classification/decision questions via the
|
|
3
|
+
* configured decision model (tev1 via Ollama's `/v1/systemone`).
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* `decide` command — answer classification/decision questions with a decision
|
|
7
|
+
* model.
|
|
8
|
+
*
|
|
9
|
+
* Single-question mode:
|
|
10
|
+
* opencode-rag decide "Which intent is this?" --state "..." --options a,b,c
|
|
11
|
+
* Full multi-question passthrough:
|
|
12
|
+
* opencode-rag decide --state "..." --questions '[{"id":"intent", ...}]'
|
|
13
|
+
*/
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
16
|
+
import { c, resolveCliContext, cleanupContext, logCliError, logCliInfo } from "../format.js";
|
|
17
|
+
import { validateDecisionRequest } from "../../decision/validate.js";
|
|
18
|
+
import { formatDecisionAnswers } from "../../decision/format.js";
|
|
19
|
+
/** Read all of stdin as UTF-8 text. */
|
|
20
|
+
async function readStdin() {
|
|
21
|
+
const chunks = [];
|
|
22
|
+
for await (const chunk of process.stdin) {
|
|
23
|
+
chunks.push(Buffer.from(chunk));
|
|
24
|
+
}
|
|
25
|
+
return Buffer.concat(chunks).toString("utf-8");
|
|
26
|
+
}
|
|
27
|
+
/** Resolve the state text from --state, --state-file, or piped stdin. */
|
|
28
|
+
async function resolveState(options, cwd) {
|
|
29
|
+
if (options.state !== undefined)
|
|
30
|
+
return options.state;
|
|
31
|
+
if (options.stateFile !== undefined) {
|
|
32
|
+
if (options.stateFile === "-")
|
|
33
|
+
return readStdin();
|
|
34
|
+
const resolved = path.isAbsolute(options.stateFile) ? options.stateFile : path.resolve(cwd, options.stateFile);
|
|
35
|
+
if (!existsSync(resolved)) {
|
|
36
|
+
throw new Error(`State file not found: ${options.stateFile}`);
|
|
37
|
+
}
|
|
38
|
+
return readFileSync(resolved, "utf-8");
|
|
39
|
+
}
|
|
40
|
+
if (!process.stdin.isTTY) {
|
|
41
|
+
return readStdin();
|
|
42
|
+
}
|
|
43
|
+
throw new Error("No state provided. Use --state <text>, --state-file <path> (or \"-\" for stdin), or pipe text via stdin.");
|
|
44
|
+
}
|
|
45
|
+
/** Build the questions array from --questions/--questions-file or the single-question flags. */
|
|
46
|
+
function buildQuestions(options, questionArg, cwd) {
|
|
47
|
+
if (options.questions !== undefined || options.questionsFile !== undefined) {
|
|
48
|
+
const raw = options.questionsFile !== undefined
|
|
49
|
+
? readFileSync(path.isAbsolute(options.questionsFile) ? options.questionsFile : path.resolve(cwd, options.questionsFile), "utf-8")
|
|
50
|
+
: options.questions;
|
|
51
|
+
let parsed;
|
|
52
|
+
try {
|
|
53
|
+
parsed = JSON.parse(raw);
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
throw new Error(`Questions JSON is invalid: ${err instanceof Error ? err.message : String(err)}`);
|
|
57
|
+
}
|
|
58
|
+
if (!Array.isArray(parsed) || parsed.length === 0) {
|
|
59
|
+
throw new Error("Questions JSON must be a non-empty array of question objects.");
|
|
60
|
+
}
|
|
61
|
+
return parsed;
|
|
62
|
+
}
|
|
63
|
+
if (questionArg === undefined || questionArg.trim().length === 0) {
|
|
64
|
+
throw new Error("Provide a question argument (single-question mode) or --questions <json>.");
|
|
65
|
+
}
|
|
66
|
+
const type = (options.type ?? "choice").toLowerCase();
|
|
67
|
+
const parts = options.options
|
|
68
|
+
? options.options.split(",").map((entry) => entry.trim()).filter((entry) => entry.length > 0)
|
|
69
|
+
: undefined;
|
|
70
|
+
if (type === "choice") {
|
|
71
|
+
if (!parts || parts.length < 2) {
|
|
72
|
+
throw new Error("--options must list 2-24 comma-separated options for a choice question.");
|
|
73
|
+
}
|
|
74
|
+
return [{
|
|
75
|
+
id: "decision",
|
|
76
|
+
type,
|
|
77
|
+
instructions: questionArg,
|
|
78
|
+
criteria: Object.fromEntries(parts.map((option) => [option, option])),
|
|
79
|
+
}];
|
|
80
|
+
}
|
|
81
|
+
if (type === "score") {
|
|
82
|
+
if (!parts || parts.length < 2) {
|
|
83
|
+
throw new Error("--options must list 2-24 comma-separated level descriptions (lowest first) for a score question.");
|
|
84
|
+
}
|
|
85
|
+
return [{ id: "decision", type, instructions: questionArg, criteria: parts }];
|
|
86
|
+
}
|
|
87
|
+
if (type === "noul") {
|
|
88
|
+
return [{ id: "decision", type, instructions: questionArg }];
|
|
89
|
+
}
|
|
90
|
+
throw new Error(`Unknown question type "${options.type}" — expected choice, noul, or score.`);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Register the `decide` command on the given Commander program.
|
|
94
|
+
*
|
|
95
|
+
* Loads the resolved config, validates that `decision.enabled` is set, and
|
|
96
|
+
* sends the request to the configured decision provider. State can be passed
|
|
97
|
+
* inline, from a file, or via stdin; questions can use the single-question
|
|
98
|
+
* convenience flags or the full JSON passthrough.
|
|
99
|
+
*
|
|
100
|
+
* @param program - The Commander `Command` instance to register on.
|
|
101
|
+
*/
|
|
102
|
+
export function registerDecideCommand(program) {
|
|
103
|
+
program
|
|
104
|
+
.command("decide")
|
|
105
|
+
.description("Answer classification/decision questions with the configured decision model (tev1 via Ollama)")
|
|
106
|
+
.argument("[question]", "question/instruction to decide (single-question mode)")
|
|
107
|
+
.option("--state <text>", "text to judge")
|
|
108
|
+
.option("--state-file <path>", "read the text to judge from a file (\"-\" for stdin)")
|
|
109
|
+
.option("-t, --type <type>", "question type: choice, noul, or score", "choice")
|
|
110
|
+
.option("--options <list>", "comma-separated options (choice) or level descriptions (score)")
|
|
111
|
+
.option("--questions <json>", "full questions array as JSON (advanced, multi-question)")
|
|
112
|
+
.option("--questions-file <path>", "read the questions array from a JSON file")
|
|
113
|
+
.option("--json", "print the raw result as JSON")
|
|
114
|
+
.option("-c, --config <path>", "path to config file")
|
|
115
|
+
.action(async (questionArg, options) => {
|
|
116
|
+
let logFilePath = path.resolve(process.cwd(), ".opencode", "opencode-rag.log");
|
|
117
|
+
let ctx;
|
|
118
|
+
try {
|
|
119
|
+
const cwd = process.cwd();
|
|
120
|
+
ctx = await resolveCliContext(options, logFilePath);
|
|
121
|
+
logFilePath = ctx.logFilePath;
|
|
122
|
+
const decisionConfig = ctx.config.decision;
|
|
123
|
+
if (!decisionConfig?.enabled) {
|
|
124
|
+
logCliError(logFilePath, "decide", '\nDecision model is not enabled in config. Set `decision.enabled` to true.', undefined);
|
|
125
|
+
process.exit(1);
|
|
126
|
+
}
|
|
127
|
+
const state = await resolveState(options, cwd);
|
|
128
|
+
const questions = buildQuestions(options, questionArg, cwd);
|
|
129
|
+
const validationError = validateDecisionRequest({
|
|
130
|
+
state,
|
|
131
|
+
questions,
|
|
132
|
+
maxStateChars: decisionConfig.maxStateChars,
|
|
133
|
+
});
|
|
134
|
+
if (validationError) {
|
|
135
|
+
logCliError(logFilePath, "decide", `\nInvalid decision request: ${validationError}`, undefined);
|
|
136
|
+
process.exit(1);
|
|
137
|
+
}
|
|
138
|
+
logCliInfo(logFilePath, "decide", `\n${c.heading("Deciding with:")} ${c.value(`${decisionConfig.provider}/${decisionConfig.model}`)} ` +
|
|
139
|
+
`(${questions.length} question${questions.length === 1 ? "" : "s"}, ${state.length} state chars)`);
|
|
140
|
+
const { createDecisionProvider } = await import("../../decision/factory.js");
|
|
141
|
+
const provider = createDecisionProvider(decisionConfig);
|
|
142
|
+
const result = await provider.decide({ state, questions });
|
|
143
|
+
if (options.json) {
|
|
144
|
+
logCliInfo(logFilePath, "decide", JSON.stringify({ provider: provider.name, model: decisionConfig.model, answers: result.answers }, null, 2));
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
logCliInfo(logFilePath, "decide", `\n${formatDecisionAnswers(result.answers, { provider: provider.name, model: decisionConfig.model })}\n`);
|
|
148
|
+
}
|
|
149
|
+
await cleanupContext(ctx);
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
const message = err.message || String(err);
|
|
153
|
+
logCliError(logFilePath, "decide", `\nDecision failed: ${message}`, err);
|
|
154
|
+
if (ctx)
|
|
155
|
+
await cleanupContext(ctx).catch(() => { });
|
|
156
|
+
process.exit(1);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=decide.js.map
|
|
@@ -33,3 +33,26 @@ export declare function registerEvalAnalyzeCommand(program: Command): void;
|
|
|
33
33
|
* @param program - The Commander `Command` instance to register on.
|
|
34
34
|
*/
|
|
35
35
|
export declare function registerEvalCompareCommand(program: Command): void;
|
|
36
|
+
/**
|
|
37
|
+
* Register the `eval:gate` command on the given Commander program.
|
|
38
|
+
*
|
|
39
|
+
* Runs the labelled golden set through the real retrieval pipeline (fusion only —
|
|
40
|
+
* the rerank stage is intentionally not active) and exits non-zero when Hit@5,
|
|
41
|
+
* MRR, or nDCG@10 drop below the documented floors. Exit contract:
|
|
42
|
+
* `0` = passed, `1` = gate failed (quality regression), `2` = could not run
|
|
43
|
+
* (missing labels/index, provider down, invalid options).
|
|
44
|
+
*
|
|
45
|
+
* @param program - The Commander `Command` instance to register on.
|
|
46
|
+
*/
|
|
47
|
+
export declare function registerEvalGateCommand(program: Command): void;
|
|
48
|
+
/**
|
|
49
|
+
* Register the `eval:decide` command on the given Commander program.
|
|
50
|
+
*
|
|
51
|
+
* Measures accuracy, ECE (10 bins) and Brier score of the tev1 decision model against
|
|
52
|
+
* the labelled calibration set. Unlike `eval:gate` this is a measurement, not a threshold
|
|
53
|
+
* check — it exits `0` when the run completed, `2` when it could not run (no decision
|
|
54
|
+
* config, Ollama unreachable, model missing).
|
|
55
|
+
*
|
|
56
|
+
* @param program - The Commander `Command` instance to register on.
|
|
57
|
+
*/
|
|
58
|
+
export declare function registerEvalDecideCommand(program: Command): void;
|