auth-yes/forum/poc-g2/vector_db_poc.ts
Tyler Gillispie e5855248e6
feat: Port all agent-forum-v4 Gen 1 PoCs to Gen 2 using real tooling (#68)
This commit ports the remaining Gen 1 proof-of-concept experiments from `forum/poc-g1` into `forum/poc-g2` while substituting naive mocks with real, production-ready mechanisms.

Key advancements include:
- `code_intelligence_poc.ts` and `cfg_poc.ts`: Swapped regex matching for actual Javascript AST traversal using `acorn`.
- `static_analysis_poc.ts`: Replaced mock payloads with real `deno lint --json` output executed via `Deno.Command`.
- `vector_db_poc.ts` and `multi_vec_poc.ts`: Replaced basic JS arrays with actual `jsr:@db/sqlite` instances utilizing User-Defined Functions (UDFs) to perform native vector cosine similarity queries in memory or on disk.
- `protobuf_poc.ts`: Implemented robust protobuf serialization/deserialization via `protobufjs`.
- Semantic/Governance PoCs (`constitution_poc.ts`, `ontology_poc.ts`, `state_machine_poc.ts`, `orphan_branch_poc.ts`, etc): Replaced string-mock I/O with absolute filesystem reads, real YAML parsing using `jsr:@std/yaml`, and isolated `Deno.Command` Git sandboxes.
- Updated `forum/poc-g2/lab.ts` to orchestrate and execute all 19 experiments, proving 100% test pass rate with Gen 2 tooling.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: mrteye <1945243+mrteye@users.noreply.github.com>
2026-08-28 22:11:53 -07:00

94 lines
2.8 KiB
TypeScript

import { Database } from "jsr:@db/sqlite";
import {
assert,
assertEquals,
} from "https://deno.land/std@0.224.0/testing/asserts.ts";
/**
* Proof of Concept: Embedded Vector Database (Gen 2)
*
* Demonstrates the concept of fuzzy semantic retrieval using an actual
* SQLite database. While we are not loading a C extension like `sqlite-vec`
* directly here to keep the PoC universally executable without native build
* dependencies, we simulate it via SQL and User Defined Functions (UDF)
* provided by Deno's `jsr:@db/sqlite`.
*/
// A simple mock for cosine similarity of 1D arrays
function cosineSimilarity(vecA: number[], vecB: number[]): number {
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < vecA.length; i++) {
dotProduct += vecA[i] * vecB[i];
normA += vecA[i] ** 2;
normB += vecB[i] ** 2;
}
if (normA === 0 || normB === 0) return 0;
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
if (import.meta.main) {
console.log("Running Embedded Vector Database PoC (Gen 2) tests...");
try {
const db = new Database(":memory:");
// Create a user-defined function in SQLite to perform vector similarity!
db.function("vec_distance", (aStr: string, bStr: string) => {
const vecA = JSON.parse(aStr) as number[];
const vecB = JSON.parse(bStr) as number[];
return cosineSimilarity(vecA, vecB);
});
db.exec(`
CREATE TABLE documents (
id TEXT PRIMARY KEY,
text TEXT,
vector TEXT
);
`);
const insert = db.prepare(
"INSERT INTO documents (id, text, vector) VALUES (?, ?, ?)"
);
insert.run("docs-1", "How to run the server", JSON.stringify([0.8, 0.1, 0.1, 0.0]));
insert.run("docs-2", "Database connection logic", JSON.stringify([0.1, 0.9, 0.2, 0.1]));
insert.run("telemetry-1", "Server latency spikes", JSON.stringify([0.2, 0.1, 0.9, 0.3]));
insert.finalize();
// Query representing "I have a slow server issue"
const queryVectorStr = JSON.stringify([0.3, 0.0, 0.9, 0.2]);
console.log("Querying Vector DB...");
const results = db.prepare(`
SELECT id, text, vec_distance(vector, ?) as score
FROM documents
ORDER BY score DESC
`).all(queryVectorStr) as { id: string; text: string; score: number }[];
console.log(
"Top result:",
results[0].text,
`(Score: ${results[0].score.toFixed(2)})`,
);
assert(
results[0].score > 0.8,
"The telemetry doc should be the highest match",
);
assertEquals(results[0].id, "telemetry-1");
console.log(
"✅ Embedded Vector DB PoC (Gen 2) successful: Real SQLite fuzzy semantic match via UDF.",
);
db.close();
} catch (err) {
console.error("❌ Embedded Vector DB PoC (Gen 2) failed:", err);
Deno.exit(1);
}
}