Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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/detect-header-only-tables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@voltagent/rag": patch
---

Recognize header-only Markdown tables at the beginning of a document with either LF or CRLF line endings.
40 changes: 40 additions & 0 deletions packages/rag/src/utils/format-detector.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, it } from "vitest";
import { detectFormat } from "./format-detector";

describe("Markdown table detection", () => {
it("rejects a delimiter with a different number of cells", () => {
expect(detectFormat("| Name | Value |\n| ---")).toBe("text");
});

it("rejects long whitespace followed by an invalid delimiter character", () => {
expect(detectFormat("| Name |\n|--" + " ".repeat(100000) + "x")).toBe("text");
});

it.each(["\n", "\r\n"])("detects a table with only a header and delimiter (%j)", (newline) => {
const table = ["| Name | Value |", "| --- | --- |"].join(newline);

expect(detectFormat(table)).toBe("table");
});

it("treats a pipe row without a delimiter as text", () => {
expect(detectFormat("| Name | Value |")).toBe("text");
});

it.each(["\n", "\r\n"])("accepts a delimiter without a trailing pipe (%j)", (newline) => {
const header = "| Name | Value |";
const delimiter = "| :--- | ---:";

expect(detectFormat([header, delimiter].join(newline))).toBe("table");
expect(detectFormat([header, delimiter, "| one | two |"].join(newline))).toBe("table");
});

it.each(["\n", "\r\n"])("accepts aligned delimiter cells (%j)", (newline) => {
const table = ["| Name | Value |", "| :--- | ---: |"].join(newline);

expect(detectFormat(table)).toBe("table");
});

it("does not match an unrelated delimiter in prose", () => {
expect(detectFormat("| note |\nprose containing | -- | marker")).toBe("text");
});
});
19 changes: 17 additions & 2 deletions packages/rag/src/utils/format-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,23 @@ export function detectFormat(input: string): DetectedFormat {
}

// Table pipe syntax
if (/\n\|.+\|\n/.test(text) && /\|\s*-{2,}\s*\|/.test(text)) {
return "table";
const lines = text.split(/\r?\n/);
for (let i = 1; i < lines.length; i++) {
const header = lines[i - 1].trim();
const delimiter = lines[i].trim();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
if (!header.startsWith("|") || !header.endsWith("|") || !delimiter.startsWith("|")) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
continue;
}
const headerCells = header.slice(1, -1).split("|");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
embrace897 marked this conversation as resolved.
Resolved
const delimiterCells = delimiter
.slice(1, delimiter.endsWith("|") ? -1 : undefined)
.split("|");
if (
headerCells.length === delimiterCells.length &&
delimiterCells.every((cell) => /^:?-{2,}:?$/.test(cell.trim()))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
) {
return "table";
}
}

// Markdown-ish signals
Expand Down