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
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@four-bytes/four-opencode-brain",
"version": "1.4.1",
"version": "1.5.1",
"description": "Unified brain plugin — single SQLite DB for RAG search, memory, and knowledge base with content-hash dedup and per-session LRU cache",
"license": "Apache-2.0",
"type": "module",
Expand All @@ -18,7 +18,7 @@
"four-bytes"
],
"dependencies": {
"@four-bytes/opencode-plugin-lib": "github:four-bytes/opencode-plugin-lib",
"@four-bytes/opencode-plugin-lib": "github:four-bytes/opencode-plugin-lib#v0.2.0",
"@opencode-ai/plugin": "1.16.2",
"@opentui/core": "0.3.2",
"@opentui/solid": "0.3.2",
Expand Down
60 changes: 2 additions & 58 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,2 @@
// ---------------------------------------------------------------------------
// Per-session LRU cache using Map (TTL = process lifetime)
// ---------------------------------------------------------------------------

export class LRUCache<K, V> {
private map = new Map<K, { value: V; ts: number }>();

constructor(private maxEntries: number = 1000) {}

get(key: K): V | undefined {
const entry = this.map.get(key);
if (!entry) return undefined;
// Refresh LRU position
this.map.delete(key);
this.map.set(key, entry);
return entry.value;
}

set(key: K, value: V): void {
if (this.map.has(key)) this.map.delete(key);
else if (this.map.size >= this.maxEntries) {
// Evict oldest (first key in insertion order)
const firstKey = this.map.keys().next().value;
if (firstKey !== undefined) this.map.delete(firstKey);
}
this.map.set(key, { value, ts: Date.now() });
}

has(key: K): boolean {
return this.map.has(key);
}

clear(): void {
this.map.clear();
}

get size(): number {
return this.map.size;
}
}

// ---------------------------------------------------------------------------
// Singleton session cache — reset on plugin init / opencode restart
// ---------------------------------------------------------------------------

export const sessionCache = {
embeddings: new LRUCache<string, Float32Array>(500), // text hash → embedding vector
search: new LRUCache<string, unknown[]>(200), // query+filters serialized → results
chunks: new LRUCache<string, unknown>(500), // chunk ID → chunk data
hashes: new LRUCache<string, string>(2000), // content → sha256 hex

reset(): void {
this.embeddings.clear();
this.search.clear();
this.chunks.clear();
this.hashes.clear();
},
};
// Re-exported from @four-bytes/opencode-plugin-lib
export { LRUCache, sessionCache } from "@four-bytes/opencode-plugin-lib";
Comment thread
four-bytes-robby marked this conversation as resolved.
10 changes: 2 additions & 8 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ---------------------------------------------------------------------------
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
// Throttled, rate-limited logger — no ANSI colors, compact single-line output
// Throttled, rate-limited console logger — captured by opencode session log
// ---------------------------------------------------------------------------

type LogLevel = "debug" | "info" | "warn" | "error";
Expand All @@ -23,11 +23,6 @@ function shouldLog(key: string, intervalMs: number = 60000): boolean {
return false;
}

/**
* Suppress all console output. Use after the initial version banner.
* When silent, only memory-based log aggregation happens — no console writes.
* Toggle back with setSilent(false) for debug mode.
*/
export function setSilent(val: boolean): void {
silent = val;
}
Expand All @@ -41,11 +36,9 @@ export function log(
if (level === "debug" && process.env.BRAIN_DEBUG !== "true") return;

if (level === "warn" || level === "info") {
// Throttle repetitive messages to once per 60s
if (!shouldLog(key, 60000)) return;
}

// Silent mode: skip all console output (errors still go to stderr)
if (silent && level !== "error") return;

const timestamp = new Date().toISOString();
Expand All @@ -55,5 +48,6 @@ export function log(

if (level === "error") console.error(line);
else if (level === "warn") console.warn(line);
else if (level === "debug") console.error(line);
else console.log(line);
}
Loading