Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/support-document-object-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@voltagent/rag": patch
---

Allow document IDs such as `constructor` and `__proto__` when chunking documents and reading their link graphs, while retaining ordinary objects for returned graphs.
40 changes: 40 additions & 0 deletions packages/rag/src/__tests__/document-keys.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, it } from "vitest";
import { StructuredDocument } from "../document/document";

const documentIds = ["constructor", "__proto__", "toString", "hasOwnProperty", "document-1"];

describe("StructuredDocument link graph keys", () => {
it.each(documentIds)("chunks a document with ID %s", (docId) => {
const doc = new StructuredDocument([{ text: "Document content.", docId }]);

const { chunks } = doc.chunk({ strategy: "token", maxTokens: 50 });
const graph = doc.getLinkGraph();

expect(chunks.length).toBeGreaterThan(0);
expect(Object.keys(graph)).toEqual([docId]);
expect(Object.getPrototypeOf(graph)).toBe(Object.prototype);
expect(Object.hasOwn(graph, docId)).toBe(true);
expect(graph[docId]).toEqual(chunks.map((chunk) => chunk.id));
expect(chunks.every((chunk) => chunk.metadata?.docId === docId)).toBe(true);
});

it.each(documentIds)("returns manually added links for document ID %s", (docId) => {
const doc = new StructuredDocument([{ text: "Document content.", docId }]);

doc.addLink(docId, "first-related", "document");
doc.addLink(docId, "second-related", "section");

const graph = doc.getLinkGraph();
expect(Object.keys(graph)).toEqual([docId]);
expect(Object.getPrototypeOf(graph)).toBe(Object.prototype);
expect(Object.hasOwn(graph, docId)).toBe(true);
expect(graph[docId]).toEqual(["first-related", "second-related"]);
});

it("keeps an empty document graph empty", () => {
const doc = new StructuredDocument([]);

expect(doc.chunk({ strategy: "token" }).chunks).toEqual([]);
expect(Object.keys(doc.getLinkGraph())).toEqual([]);
});
});
6 changes: 3 additions & 3 deletions packages/rag/src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class StructuredDocument {

chunk(params: { strategy: ChunkStrategy; maxTokens?: number }): { chunks: Chunk[] } {
const chunks: Chunk[] = [];
const graph: Record<string, string[]> = {};
const graph: Record<string, string[]> = Object.create(null);
this.nodes.forEach((node) => {
const cks = chunkByStrategy(params.strategy, node.text, {
maxTokens: params.maxTokens,
Expand All @@ -80,12 +80,12 @@ export class StructuredDocument {
}

getLinkGraph(): Record<string, string[]> {
const graph: Record<string, string[]> = {};
const graph: Record<string, string[]> = Object.create(null);
this.nodes.forEach((node) => {
node.links?.forEach((link) => {
graph[node.id] = [...(graph[node.id] ?? []), link.nodeId];
});
});
return graph;
return { ...graph };
}
}