Просмотр исходного кода

Merge pull request #208 from tobi/fix/json-output-and-index-paths

Tobias Lütke 3 месяцев назад
Родитель
Сommit
1a67e1a093
2 измененных файлов с 35 добавлено и 5 удалено
  1. 10 1
      src/collections.ts
  2. 25 4
      src/qmd.ts

+ 10 - 1
src/collections.ts

@@ -58,7 +58,16 @@ let currentIndexName: string = "index";
  * Config file will be ~/.config/qmd/{indexName}.yml
  */
 export function setConfigIndexName(name: string): void {
-  currentIndexName = name;
+  // Resolve relative paths to absolute paths and sanitize for use as filename
+  if (name.includes('/')) {
+    const { resolve } = require('path');
+    const { cwd } = require('process');
+    const absolutePath = resolve(cwd(), name);
+    // Replace path separators with underscores to create a valid filename
+    currentIndexName = absolutePath.replace(/\//g, '_').replace(/^_/, '');
+  } else {
+    currentIndexName = name;
+  }
 }
 
 function getConfigDir(): string {

+ 25 - 4
src/qmd.ts

@@ -120,7 +120,16 @@ function getDbPath(): string {
 }
 
 function setIndexName(name: string | null): void {
-  storeDbPathOverride = name ? getDefaultDbPath(name) : undefined;
+  let normalizedName = name;
+  // Normalize relative paths to prevent malformed database paths
+  if (name && name.includes('/')) {
+    const { resolve } = require('path');
+    const { cwd } = require('process');
+    const absolutePath = resolve(cwd(), name);
+    // Replace path separators with underscores to create a valid filename
+    normalizedName = absolutePath.replace(/\//g, '_').replace(/^_/, '');
+  }
+  storeDbPathOverride = normalizedName ? getDefaultDbPath(normalizedName) : undefined;
   // Reset open handle so next use opens the new index
   closeDb();
 }
@@ -1959,7 +1968,11 @@ function search(query: string, opts: OutputOptions): void {
   closeDb();
 
   if (resultsWithContext.length === 0) {
-    console.log("No results found.");
+    if (opts.format === "json") {
+      console.log("[]");
+    } else {
+      console.log("No results found.");
+    }
     return;
   }
   outputResults(resultsWithContext, query, opts);
@@ -2013,7 +2026,11 @@ async function vectorSearch(query: string, opts: OutputOptions, _model: string =
     closeDb();
 
     if (results.length === 0) {
-      console.log("No results found.");
+      if (opts.format === "json") {
+        console.log("[]");
+      } else {
+        console.log("No results found.");
+      }
       return;
     }
 
@@ -2072,7 +2089,11 @@ async function querySearch(query: string, opts: OutputOptions, _embedModel: stri
     closeDb();
 
     if (results.length === 0) {
-      console.log("No results found.");
+      if (opts.format === "json") {
+        console.log("[]");
+      } else {
+        console.log("No results found.");
+      }
       return;
     }