auth-yes/archive/.forum/poc-g1/code_intelligence_poc.ts

110 lines
3.1 KiB
TypeScript

import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
/**
* Proof of Concept: Local Code Intelligence (AST Parsing)
*
* In the full implementation, this would use Tree-sitter WASM or SCIP.
* For this Deno PoC, we will simulate the extraction of structured data
* from raw code by building a lightweight regex-based scanner that
* extracts exported function signatures. This proves the concept of
* transforming "raw text" into "structured JSON context" for an agent.
*/
export interface ExportSymbol {
name: string;
type: "function" | "class" | "const";
signature: string;
}
export function extractExports(sourceCode: string): ExportSymbol[] {
const exports: ExportSymbol[] = [];
// A naive regex for PoC purposes to find exported functions
// Matches: export function foo(bar: string): void {
const functionRegex =
/export\s+(?:async\s+)?function\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)(?:\s*:\s*([^ {]+))?/g;
let match;
while ((match = functionRegex.exec(sourceCode)) !== null) {
const name = match[1];
const args = match[2].trim();
const returnType = match[3] ? match[3].trim() : "any";
exports.push({
name,
type: "function",
signature: `(${args}) => ${returnType}`,
});
}
// Matches: export const foo = ...
const constRegex = /export\s+const\s+([a-zA-Z0-9_]+)\s*=/g;
while ((match = constRegex.exec(sourceCode)) !== null) {
exports.push({
name: match[1],
type: "const",
signature: "const",
});
}
return exports;
}
// In a real scenario, tests would be separated. For this PoC, we will run the tests here.
if (import.meta.main) {
console.log("Running Local Code Intelligence PoC tests...");
const mockSourceCode = `
import { stuff } from "somewhere";
/**
* Calculates a complex value.
*/
export async function calculateValue(input: number, mode: string): Promise<number> {
return input * 2;
}
// An internal helper
function internalHelper() {
return true;
}
export const MAX_RETRIES = 5;
export function doSomethingElse(): void {
console.log(MAX_RETRIES);
}
`;
try {
const extracted = extractExports(mockSourceCode);
assertEquals(extracted.length, 3);
const calcFunc = extracted.find((e) => e.name === "calculateValue");
assertEquals(calcFunc?.type, "function");
assertEquals(
calcFunc?.signature,
"(input: number, mode: string) => Promise<number>",
);
const maxRetries = extracted.find((e) => e.name === "MAX_RETRIES");
assertEquals(maxRetries?.type, "const");
const doSomething = extracted.find((e) => e.name === "doSomethingElse");
assertEquals(doSomething?.type, "function");
assertEquals(doSomething?.signature, "() => void");
console.log(
"✅ Local Code Intelligence PoC successful: Extracted structured context from raw source.",
);
// Simulate what the agent would actually see:
console.log("\n--- Agent Context Payload ---");
console.log(JSON.stringify(extracted, null, 2));
console.log("-----------------------------\n");
} catch (err) {
console.error("❌ Local Code Intelligence PoC failed:", err);
}
}