✨ working codebase context provider
This commit is contained in:
parent
8b00d558cd
commit
13f800d407
8 changed files with 134 additions and 171 deletions
|
|
@ -5,6 +5,7 @@ import {
|
|||
ContextProviderExtras,
|
||||
} from "../..";
|
||||
import { ExtensionIde } from "../../ide";
|
||||
import { getBasename } from "../../util";
|
||||
|
||||
class CodebaseContextProvider extends BaseContextProvider {
|
||||
static description: ContextProviderDescription = {
|
||||
|
|
@ -24,8 +25,16 @@ class CodebaseContextProvider extends BaseContextProvider {
|
|||
}
|
||||
const [v] = await extras.embeddingsProvider.embed([extras.fullInput]);
|
||||
const results = await new ExtensionIde().retrieveChunks(v, 10, []);
|
||||
console.log(results, "RESULTS");
|
||||
return [];
|
||||
|
||||
return results.map((r) => {
|
||||
const name = `${getBasename(r.filepath)} (${r.startLine}-${r.endLine})`;
|
||||
const description = `${r.filepath} (${r.startLine}-${r.endLine})`;
|
||||
return {
|
||||
name,
|
||||
description,
|
||||
content: `\`\`\`${name}\n${r.content}\n\`\`\``,
|
||||
};
|
||||
});
|
||||
}
|
||||
async load(): Promise<void> {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -347,6 +347,16 @@ export function getSidebarContent(
|
|||
);
|
||||
break;
|
||||
}
|
||||
case "retrieveChunks": {
|
||||
respond(
|
||||
await ide.retrieveChunks(
|
||||
data.message.v,
|
||||
data.message.n,
|
||||
data.message.tags
|
||||
)
|
||||
);
|
||||
break;
|
||||
}
|
||||
// History
|
||||
case "history": {
|
||||
respond(historyManager.list());
|
||||
|
|
|
|||
|
|
@ -301,10 +301,9 @@ class VsCodeIde implements IDE {
|
|||
// TODO: OR clause with tags
|
||||
let branch = await ideProtocolClient.getBranch();
|
||||
let dirs = await this.getWorkspaceDirs();
|
||||
let tag = [`${dirs[0] || "NONE"}::${branch}`];
|
||||
let sync_results = sync.retrieve(n, tags, v);
|
||||
let results = sync_results.map((r: any) => [tag, r.name, r.hash]);
|
||||
return results;
|
||||
let branchTag = `${dirs[0] || "NONE"}::${branch}`;
|
||||
let sync_results = sync.retrieve(n, [...tags, branchTag], v);
|
||||
return sync_results;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ async function resolveEditorContent(
|
|||
}
|
||||
);
|
||||
for (const resolvedItem of resolvedItems) {
|
||||
contextItemsText += `\`\`\`title="${item.label}"\n${resolvedItem.content}\n\`\`\`\n`;
|
||||
contextItemsText += resolvedItem.content;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
136
sync/Cargo.lock
generated
136
sync/Cargo.lock
generated
|
|
@ -501,12 +501,6 @@ dependencies = [
|
|||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||
|
||||
[[package]]
|
||||
name = "hex-literal"
|
||||
version = "0.4.1"
|
||||
|
|
@ -585,12 +579,6 @@ dependencies = [
|
|||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "2.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8"
|
||||
|
||||
[[package]]
|
||||
name = "inout"
|
||||
version = "0.1.3"
|
||||
|
|
@ -667,16 +655,6 @@ version = "0.4.12"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.20"
|
||||
|
|
@ -708,15 +686,6 @@ dependencies = [
|
|||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.7.1"
|
||||
|
|
@ -789,7 +758,7 @@ dependencies = [
|
|||
"bitflags 1.3.2",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"memoffset 0.7.1",
|
||||
"memoffset",
|
||||
"pin-utils",
|
||||
]
|
||||
|
||||
|
|
@ -833,29 +802,6 @@ version = "0.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-targets 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "password-hash"
|
||||
version = "0.4.2"
|
||||
|
|
@ -924,67 +870,6 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3"
|
||||
version = "0.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a89dc7a5850d0e983be1ec2a463a171d20990487c3cfcd68b5363f1ee3d6fe0"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"indoc",
|
||||
"libc",
|
||||
"memoffset 0.9.0",
|
||||
"parking_lot",
|
||||
"pyo3-build-config",
|
||||
"pyo3-ffi",
|
||||
"pyo3-macros",
|
||||
"unindent",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-build-config"
|
||||
version = "0.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07426f0d8fe5a601f26293f300afd1a7b1ed5e78b2a705870c5f30893c5163be"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"target-lexicon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-ffi"
|
||||
version = "0.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbb7dec17e17766b46bca4f1a4215a85006b4c2ecde122076c562dd058da6cf1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"pyo3-build-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-macros"
|
||||
version = "0.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05f738b4e40d50b5711957f142878cfa0f28e054aa0ebdfc3fd137a843f74ed3"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"pyo3-macros-backend",
|
||||
"quote",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-macros-backend"
|
||||
version = "0.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fc910d4851847827daf9d6cdd4a823fbdaab5b8818325c5e97a86da79e8881f"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.35"
|
||||
|
|
@ -1145,12 +1030,6 @@ dependencies = [
|
|||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "sct"
|
||||
version = "0.7.1"
|
||||
|
|
@ -1312,7 +1191,6 @@ dependencies = [
|
|||
"ignore",
|
||||
"ndarray",
|
||||
"neon",
|
||||
"pyo3",
|
||||
"rand",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
|
|
@ -1333,12 +1211,6 @@ dependencies = [
|
|||
"xattr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "target-lexicon"
|
||||
version = "0.12.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69758bda2e78f098e4ccb393021a0963bb3442eac05f135c30f61b7370bbafae"
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.9.0"
|
||||
|
|
@ -1432,12 +1304,6 @@ dependencies = [
|
|||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unindent"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ hex-literal = "0.4.1"
|
|||
homedir = "0.2.1"
|
||||
ignore = "0.4.20"
|
||||
ndarray = "0.15.6"
|
||||
pyo3 = { version = "0.20.0", features = ["extension-module"] }
|
||||
rand = "0.8.5"
|
||||
rusqlite = "0.30.0"
|
||||
serde = { version = "1.0.192", features = ["derive"] }
|
||||
|
|
|
|||
|
|
@ -22,9 +22,13 @@ fn get_top_n(v: Vec<f32>, vectors: Vec<Vec<f32>>, d: usize, top_n: usize) -> Vec
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Chunk {
|
||||
id: i32,
|
||||
content: String,
|
||||
embedding: String,
|
||||
pub hash: String,
|
||||
pub content: String,
|
||||
pub embedding: Vec<f32>,
|
||||
pub start_line: usize,
|
||||
pub end_line: usize,
|
||||
pub file_path: String,
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
pub fn embedding_to_text(embedding: Vec<f32>) -> String {
|
||||
|
|
@ -35,11 +39,19 @@ pub fn embedding_to_text(embedding: Vec<f32>) -> String {
|
|||
.join(",");
|
||||
}
|
||||
|
||||
pub fn text_to_embedding(text: String) -> Vec<f32> {
|
||||
return text
|
||||
.split(',')
|
||||
.map(|s| s.parse::<f32>().unwrap())
|
||||
.collect::<Vec<f32>>();
|
||||
pub fn text_to_embedding(text: String) -> Result<Vec<f32>, &'static str> {
|
||||
let mut embedding = Vec::new();
|
||||
for s in text.split(',') {
|
||||
match s.parse::<f32>() {
|
||||
Ok(value) => embedding.push(value),
|
||||
Err(_) => {
|
||||
println!("Failed to parse embedding from text: {}", text);
|
||||
return Err("Failed to parse embedding from text");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(embedding)
|
||||
}
|
||||
|
||||
fn get_conn() -> Connection {
|
||||
|
|
@ -60,7 +72,11 @@ pub fn create_database() {
|
|||
id INTEGER PRIMARY KEY,
|
||||
hash TEXT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
embedding TEXT NOT NULL
|
||||
embedding TEXT NOT NULL,
|
||||
start_line INTEGER NOT NULL,
|
||||
end_line INTEGER NOT NULL,
|
||||
file_path TEXT NOT NULL,
|
||||
idx INTEGER NOT NULL
|
||||
)",
|
||||
(),
|
||||
)
|
||||
|
|
@ -77,19 +93,19 @@ pub fn create_database() {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn add_chunk(hash: String, content: String, tags: Vec<String>, embedding: Vec<f32>) {
|
||||
pub fn add_chunk(chunk: Chunk, tags: Vec<String>) {
|
||||
let conn = get_conn();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO chunks (hash, content, embedding) VALUES (?1, ?2, ?3)",
|
||||
(&hash, &content, &embedding_to_text(embedding)),
|
||||
"INSERT INTO chunks (hash, content, embedding, start_line, end_line, file_path, idx) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
|
||||
(&chunk.hash, &chunk.content, &embedding_to_text(chunk.embedding), chunk.start_line, chunk.end_line, &chunk.file_path, chunk.index),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
for tag in tags {
|
||||
conn.execute(
|
||||
"INSERT INTO tags (chunk_hash, tag) VALUES (?1, ?2)",
|
||||
(&hash, &tag),
|
||||
(&chunk.hash, &tag),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
|
@ -142,9 +158,13 @@ pub fn retrieve(n: usize, tags: Vec<String>, v: Vec<f32>) -> Vec<Chunk> {
|
|||
let chunk_rows = stmt
|
||||
.query_map((tags.join(", "),), |row| {
|
||||
Ok(Chunk {
|
||||
id: row.get(0)?,
|
||||
content: row.get(1)?,
|
||||
embedding: row.get(2)?,
|
||||
hash: row.get(1)?,
|
||||
content: row.get(2)?,
|
||||
embedding: text_to_embedding(row.get(3)?).unwrap(),
|
||||
start_line: row.get(4)?,
|
||||
end_line: row.get(5)?,
|
||||
file_path: row.get(6)?,
|
||||
index: row.get(7)?,
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
|
|
@ -154,7 +174,7 @@ pub fn retrieve(n: usize, tags: Vec<String>, v: Vec<f32>) -> Vec<Chunk> {
|
|||
for chunk in chunk_rows {
|
||||
let chunk = chunk.unwrap();
|
||||
chunks.push(chunk.clone());
|
||||
let vector = text_to_embedding(chunk.embedding);
|
||||
let vector = chunk.embedding;
|
||||
vectors.push(vector);
|
||||
}
|
||||
|
||||
|
|
@ -215,13 +235,17 @@ mod tests {
|
|||
|
||||
for _ in 0..10_000 {
|
||||
let chunk = Chunk {
|
||||
id: 0,
|
||||
hash: "test".to_string(),
|
||||
content: "Test content".to_string(),
|
||||
embedding: embedding_to_text(rand_embedding(384)),
|
||||
embedding: rand_embedding(384),
|
||||
start_line: 0,
|
||||
end_line: 0,
|
||||
file_path: "test".to_string(),
|
||||
index: 0,
|
||||
};
|
||||
conn.execute(
|
||||
"INSERT INTO chunks (content, embedding) VALUES (?1, ?2)",
|
||||
(&chunk.content, &chunk.embedding),
|
||||
(&chunk.content, &embedding_to_text(chunk.embedding)),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
|
@ -235,9 +259,13 @@ mod tests {
|
|||
let chunk_iter = stmt
|
||||
.query_map([], |row| {
|
||||
Ok(Chunk {
|
||||
id: row.get(0)?,
|
||||
content: row.get(1)?,
|
||||
embedding: row.get(2)?,
|
||||
hash: row.get(1)?,
|
||||
content: row.get(2)?,
|
||||
embedding: text_to_embedding(row.get(3)?).unwrap(),
|
||||
start_line: row.get(4)?,
|
||||
end_line: row.get(5)?,
|
||||
file_path: row.get(6)?,
|
||||
index: row.get(7)?,
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
|
|
@ -247,7 +275,7 @@ mod tests {
|
|||
let mut i = 0;
|
||||
for chunk in chunk_iter {
|
||||
i += 1;
|
||||
let _ = text_to_embedding(chunk.unwrap().embedding);
|
||||
let _ = chunk.unwrap().embedding;
|
||||
}
|
||||
|
||||
println!("Found {} chunks", i);
|
||||
|
|
|
|||
|
|
@ -48,6 +48,22 @@ fn db_add_chunk(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|||
.get::<JsString, _, _>(&mut cx, "content")?
|
||||
.value(&mut cx);
|
||||
|
||||
let start_line = chunk_obj
|
||||
.get::<JsNumber, _, _>(&mut cx, "startLine")?
|
||||
.value(&mut cx) as usize;
|
||||
|
||||
let end_line = chunk_obj
|
||||
.get::<JsNumber, _, _>(&mut cx, "endLine")?
|
||||
.value(&mut cx) as usize;
|
||||
|
||||
let file_path = chunk_obj
|
||||
.get::<JsString, _, _>(&mut cx, "filepath")?
|
||||
.value(&mut cx);
|
||||
|
||||
let index = chunk_obj
|
||||
.get::<JsNumber, _, _>(&mut cx, "index")?
|
||||
.value(&mut cx) as usize;
|
||||
|
||||
let tags_vec = cx.argument::<JsArray>(1)?.to_vec(&mut cx).unwrap();
|
||||
let mut tags: Vec<String> = Vec::new();
|
||||
for item in tags_vec {
|
||||
|
|
@ -67,12 +83,23 @@ fn db_add_chunk(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|||
.value(&mut cx) as f32;
|
||||
embedding.push(float);
|
||||
}
|
||||
db::add_chunk(hash, content, tags, embedding);
|
||||
|
||||
let chunk = db::Chunk {
|
||||
hash,
|
||||
content,
|
||||
start_line,
|
||||
end_line,
|
||||
file_path,
|
||||
index,
|
||||
embedding,
|
||||
};
|
||||
|
||||
db::add_chunk(chunk, tags);
|
||||
|
||||
return Ok(JsUndefined::new(&mut cx));
|
||||
}
|
||||
|
||||
fn db_retrieve(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
||||
fn db_retrieve(mut cx: FunctionContext) -> JsResult<JsArray> {
|
||||
let n = cx.argument::<JsNumber>(0)?.value(&mut cx) as usize;
|
||||
|
||||
let tags_vec = cx.argument::<JsArray>(1)?.to_vec(&mut cx).unwrap();
|
||||
|
|
@ -95,9 +122,34 @@ fn db_retrieve(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
|||
v.push(float);
|
||||
}
|
||||
|
||||
db::retrieve(n, tags, v);
|
||||
let results = db::retrieve(n, tags, v);
|
||||
|
||||
return Ok(JsUndefined::new(&mut cx));
|
||||
let js_array = JsArray::new(&mut cx, results.len() as u32);
|
||||
for (i, chunk) in results.iter().enumerate() {
|
||||
let js_object = JsObject::new(&mut cx);
|
||||
|
||||
let hash = JsString::new(&mut cx, &chunk.hash);
|
||||
let _ = js_object.set(&mut cx, "hash", hash);
|
||||
|
||||
let content = JsString::new(&mut cx, &chunk.content);
|
||||
let _ = js_object.set(&mut cx, "content", content);
|
||||
|
||||
let start_line = JsNumber::new(&mut cx, chunk.start_line as f64);
|
||||
let _ = js_object.set(&mut cx, "startLine", start_line);
|
||||
|
||||
let end_line = JsNumber::new(&mut cx, chunk.end_line as f64);
|
||||
let _ = js_object.set(&mut cx, "endLine", end_line);
|
||||
|
||||
let file_path = JsString::new(&mut cx, &chunk.file_path);
|
||||
let _ = js_object.set(&mut cx, "filepath", file_path);
|
||||
|
||||
let index = JsNumber::new(&mut cx, chunk.index as f64);
|
||||
let _ = js_object.set(&mut cx, "index", index);
|
||||
|
||||
let _ = js_array.set(&mut cx, i as u32, js_object);
|
||||
}
|
||||
|
||||
return Ok(js_array);
|
||||
}
|
||||
|
||||
#[neon::main]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue