search WIP

This commit is contained in:
nicwands
2026-03-10 12:10:52 -04:00
parent 77b8ad2dcd
commit efc9c73751
8 changed files with 185 additions and 24 deletions

View File

@@ -98,8 +98,7 @@ class NotesAPI {
this.encryptionKey = encryptionKey || process.env.NOTES_ENCRYPTION_KEY;
this._sodiumReady = false;
this.index = new Index({
tokenize: "tolerant",
resolution: 9
tokenize: "forward"
});
}
async _initSodium() {
@@ -179,12 +178,23 @@ class NotesAPI {
try {
const note = this._decrypt(encryptedNote.data || encryptedNote);
this.notesCache.set(note.id, note);
this.index.add(note.id, note.title + "\n" + note.content);
const searchText = note.plainText || this._extractPlainText(note.content);
this.index.add(note.id, note.title + "\n" + searchText);
} catch (error) {
console.error("Failed to decrypt note:", error);
}
}
}
_extractPlainText(content) {
if (!content) return "";
if (typeof content === "string") return content;
const extractText = (node) => {
if (typeof node === "string") return node;
if (!node || !node.content) return "";
return node.content.map(extractText).join(" ");
};
return extractText(content);
}
/* -----------------------
Public API
------------------------*/
@@ -203,7 +213,7 @@ class NotesAPI {
getNote(id) {
return this.notesCache.get(id) ?? null;
}
async createNote(metadata = {}, content = "") {
async createNote(metadata = {}, content = "", plainText = "") {
const id = crypto.randomUUID();
const now = (/* @__PURE__ */ new Date()).toISOString();
const note = {
@@ -212,14 +222,15 @@ class NotesAPI {
category: metadata.category || null,
createdAt: now,
updatedAt: now,
content
content,
plainText
};
const encryptedNote = {
id: note.id,
data: this._encrypt(note)
};
this.notesCache.set(id, note);
this.index.add(id, note.title + "\n" + content);
this.index.add(id, note.title + "\n" + plainText);
await this.adapter.create(encryptedNote);
return note;
}
@@ -228,16 +239,17 @@ class NotesAPI {
this.notesCache.delete(id);
this.index.remove(id);
}
async updateNote(id, content) {
async updateNote(id, content, plainText = "") {
const note = this.notesCache.get(id);
if (!note) throw new Error("Note not found");
note.content = content;
note.plainText = plainText;
note.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
const encryptedNote = {
id: note.id,
data: this._encrypt(note)
};
this.index.update(id, note.title + "\n" + content);
this.index.update(id, note.title + "\n" + plainText);
await this.adapter.update(encryptedNote);
return note;
}
@@ -261,12 +273,18 @@ class NotesAPI {
id: note.id,
data: this._encrypt(note)
};
this.index.update(id, note.title + "\n" + note.content);
this.index.update(
id,
note.title + "\n" + (note.plainText || this._extractPlainText(note.content))
);
await this.adapter.update(encryptedNote);
return note;
}
search(query) {
const ids = this.index.search(query);
const ids = this.index.search(query, {
limit: 50,
suggest: true
});
return ids.map((id) => this.notesCache.get(id));
}
}