This commit is contained in:
2026-05-13 12:05:55 +03:00
parent c5b61ee3d8
commit 674c3cbd44
43 changed files with 382 additions and 639 deletions
+19 -4
View File
@@ -3,6 +3,9 @@ import path from "node:path";
import {readdir, readFile, unlink, writeFile} from "node:fs/promises";
import {notesDir, notesRootFile} from "../../index";
import {asNonEmptyString} from "./utils";
import {toolsLogger} from "./tool-logger";
const logger = toolsLogger.child("notes");
export type NoteListItem = {
fileName: string;
@@ -58,7 +61,8 @@ export const getNoteContentTool = {
} satisfies AiTool;
export async function listNotes(): Promise<ListNotesResult> {
console.log("LIST_NOTES");
const startedAt = Date.now();
logger.debug("list.start");
try {
const entries = await readdir(notesDir, {withFileTypes: true});
@@ -91,8 +95,10 @@ export async function listNotes(): Promise<ListNotesResult> {
notes.sort((a, b) => a.title.localeCompare(b.title));
logger.debug("list.done", {notes: notes.length, duration: logger.duration(startedAt)});
return {success: true, notes};
} catch (error) {
logger.error("list.failed", {duration: logger.duration(startedAt), error});
const errorMessage = error instanceof Error ? error.message : String(error);
return {success: false, error: `Failed to list notes: ${errorMessage}`};
}
@@ -101,7 +107,8 @@ export async function listNotes(): Promise<ListNotesResult> {
export async function getNoteContent(
args?: Record<string, unknown>,
): Promise<GetNoteContentResult> {
console.log("GET_NOTE_CONTENT; ARGS: ", args);
const startedAt = Date.now();
logger.debug("get_content.start", {args});
const fileName = asNonEmptyString(args?.fileName) ?? "";
if (!fileName.trim().length) {
@@ -118,6 +125,7 @@ export async function getNoteContent(
const normalizedFileName = path.basename(noteFilePath);
const relativePath = path.relative(path.dirname(notesRootFile), noteFilePath);
logger.debug("get_content.done", {fileName: normalizedFileName, relativePath, chars: content.length, duration: logger.duration(startedAt)});
return {
success: true,
fileName: normalizedFileName,
@@ -127,6 +135,7 @@ export async function getNoteContent(
content,
};
} catch (error) {
logger.error("list.failed", {duration: logger.duration(startedAt), error});
const errorMessage = error instanceof Error ? error.message : String(error);
return {success: false, error: `Failed to read note: ${errorMessage}`};
}
@@ -219,7 +228,8 @@ export const deleteNoteTool = {
export async function updateNoteContent(
args?: Record<string, unknown>,
): Promise<UpdateNoteContentResult> {
console.log("UPDATE_NOTE_CONTENT; ARGS: ", args);
const startedAt = Date.now();
logger.debug("update_content.start", {args});
const fileName = asNonEmptyString(args?.fileName) ?? "";
if (!fileName.trim().length) {
@@ -239,9 +249,11 @@ export async function updateNoteContent(
try {
await readFile(noteFilePath, "utf-8");
await writeFile(noteFilePath, content, "utf-8");
logger.debug("update_content.done", {fileName, filePath: noteFilePath, chars: content.length, duration: logger.duration(startedAt)});
return {success: true, filePath: noteFilePath};
} catch (error) {
logger.error("list.failed", {duration: logger.duration(startedAt), error});
const errorMessage = error instanceof Error ? error.message : String(error);
return {success: false, error: `Failed to update note: ${errorMessage}`};
}
@@ -250,7 +262,8 @@ export async function updateNoteContent(
export async function deleteNote(
args?: Record<string, unknown>,
): Promise<DeleteNoteResult> {
console.log("DELETE_NOTE; ARGS: ", args);
const startedAt = Date.now();
logger.debug("delete.start", {args});
const fileName = asNonEmptyString(args?.fileName) ?? "";
if (!fileName.trim().length) {
@@ -265,9 +278,11 @@ export async function deleteNote(
try {
await unlink(noteFilePath);
await removeNoteLinkFromRoot(noteFilePath);
logger.debug("delete.done", {fileName, filePath: noteFilePath, duration: logger.duration(startedAt)});
return {success: true, filePath: noteFilePath};
} catch (error) {
logger.error("list.failed", {duration: logger.duration(startedAt), error});
const errorMessage = error instanceof Error ? error.message : String(error);
return {success: false, error: `Failed to delete note: ${errorMessage}`};
}