gatekpr_opencode/lib.rs
1//! OpenCode CLI integration for RAG-powered validation enrichment
2//!
3//! This crate provides a client that invokes the OpenCode CLI to enrich
4//! validation findings with context from the RAG system. It implements
5//! a two-stage validation flow:
6//!
7//! - Stage 1: Local pipeline produces RawFinding
8//! - Stage 2: OpenCode CLI enriches to EnrichedFinding with RAG context
9//!
10//! ## Architecture
11//!
12//! The client executes `opencode run` with structured prompts, instructing
13//! the LLM to use MCP tools (`search_docs_for_rule`, `get_fix_hint`) to
14//! fetch relevant Shopify documentation and provide detailed analysis.
15//!
16//! Each finding is processed in a fresh OpenCode session to avoid context
17//! window accumulation. This prioritizes accuracy over speed.
18//!
19//! ## Usage
20//!
21//! ```rust,ignore
22//! use gatekpr_opencode::{OpenCodeClient, RawFinding, Severity};
23//!
24//! // Create client (auto-detects CLI path)
25//! let client = OpenCodeClient::auto()?;
26//!
27//! // Enrich findings from local pipeline
28//! let raw_findings = vec![
29//! RawFinding::new("WH001", Severity::Critical, "webhooks", "src/app.ts", "Missing GDPR webhook")
30//! .with_line(42)
31//! ];
32//!
33//! let enriched = client.enrich_findings(raw_findings, Path::new("./my-app")).await?;
34//!
35//! // Enriched findings include:
36//! // - Detailed issue description and impact
37//! // - Fix recommendations with code snippets
38//! // - Documentation references from RAG
39//! ```
40//!
41//! ## Configuration
42//!
43//! The client can be configured via environment variables:
44//!
45//! - `OPENCODE_CLI_PATH`: Path to opencode binary (default: auto-detect)
46//! - `OPENCODE_MODEL`: Model to use (default: zai-coding-plan/glm-4.7)
47//! - `OPENCODE_TIMEOUT`: Timeout in seconds (default: 120)
48//! - `MCP_SERVER_PATH`: Path to gatekpr-mcp-server binary
49
50pub mod client;
51pub mod config;
52pub mod error;
53pub mod models;
54
55pub use client::OpenCodeClient;
56pub use config::OpenCodeConfig;
57pub use error::{OpenCodeError, Result};
58pub use models::*;