Skip to content
Closed
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
15 changes: 15 additions & 0 deletions packages/rag/src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import { splitIntoParagraphs, splitIntoSentences } from "../utils/text";
import { createTikTokenizer, sliceByTokenRange, tokenizeWithPositions } from "../utils/tokenizer";
import { detectFormat } from "../utils/format-detector";

describe("Tokenizer utils", () => {
it("tokenizes with positions and slices by range", () => {
Expand Down Expand Up @@ -32,3 +33,17 @@ describe("createTikTokenizer", () => {
expect(tokenizer.countTokens("hello")).toBeGreaterThan(0);
});
});

describe("detectFormat", () => {
it("detects JSON objects", () => {
expect(detectFormat('{"id":1}')).toBe("json");
});

it("detects top-level JSON arrays", () => {
expect(detectFormat('[{"id":1},{"id":2}]')).toBe("json");
});

it("falls back to text for incomplete JSON", () => {
expect(detectFormat('[{"id":1}')).not.toBe("json");
});
});
2 changes: 1 addition & 1 deletion packages/rag/src/utils/format-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function detectFormat(input: string): DetectedFormat {
if (!text) return "text";

// JSON: quick check + parse
if ((text.startsWith("{") || text.startsWith("[")) && text.endsWith("}")) {
if ((text.startsWith("{") && text.endsWith("}")) || (text.startsWith("[") && text.endsWith("]"))) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This new condition exceeds the repository’s 100-character formatter width and will be reformatted by the Biome CI check. Wrap the condition across lines to keep this file formatter-clean.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/rag/src/utils/format-detector.ts, line 22:

<comment>This new condition exceeds the repository’s 100-character formatter width and will be reformatted by the Biome CI check. Wrap the condition across lines to keep this file formatter-clean.</comment>

<file context>
@@ -19,7 +19,7 @@ export function detectFormat(input: string): DetectedFormat {
 
   // JSON: quick check + parse
-  if ((text.startsWith("{") || text.startsWith("[")) && text.endsWith("}")) {
+  if ((text.startsWith("{") && text.endsWith("}")) || (text.startsWith("[") && text.endsWith("]"))) {
     try {
       JSON.parse(text);
</file context>
Suggested change
if ((text.startsWith("{") && text.endsWith("}")) || (text.startsWith("[") && text.endsWith("]"))) {
if (
(text.startsWith("{") && text.endsWith("}")) ||
(text.startsWith("[") && text.endsWith("]"))
) {

try {
JSON.parse(text);
return "json";
Expand Down