Răsfoiți Sursa

Harden assistant text extraction

Lukas Goldschmidt 1 lună în urmă
părinte
comite
6b59349aba
1 a modificat fișierele cu 13 adăugiri și 3 ștergeri
  1. 13 3
      index.ts

+ 13 - 3
index.ts

@@ -7,12 +7,22 @@ function appendLog(entry: Record<string, unknown>) {
     fs.appendFileSync(LOG_FILE, JSON.stringify(entry) + "\n", "utf8");
   } catch {}
 }
-function extractText(content: any): string {
+function extractText(content: any, seen?: Set<any>): string {
   if (typeof content === "string") return content;
+  if (content === null || content === undefined) return "";
+  const seenSet = seen ?? new Set<any>();
+  if (seenSet.has(content)) return "";
+  seenSet.add(content);
   if (Array.isArray(content)) {
     return content
-      .filter((b: any) => b.type === "text")
-      .map((b: any) => b.text as string)
+      .map((item) => extractText(item, seenSet))
+      .filter((text) => !!text)
+      .join("");
+  }
+  if (typeof content === "object") {
+    return Object.values(content)
+      .map((value) => extractText(value, seenSet))
+      .filter((text) => !!text)
       .join("");
   }
   return "";