refactor(shared): dedupe chat content text extraction
This commit is contained in:
26
src/shared/chat-content.test.ts
Normal file
26
src/shared/chat-content.test.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { extractTextFromChatContent } from "./chat-content.js";
|
||||
|
||||
describe("extractTextFromChatContent", () => {
|
||||
it("normalizes string content", () => {
|
||||
expect(extractTextFromChatContent(" hello\nworld ")).toBe("hello world");
|
||||
});
|
||||
|
||||
it("extracts text blocks from array content", () => {
|
||||
expect(
|
||||
extractTextFromChatContent([
|
||||
{ type: "text", text: " hello " },
|
||||
{ type: "image_url", image_url: "https://example.com" },
|
||||
{ type: "text", text: "world" },
|
||||
]),
|
||||
).toBe("hello world");
|
||||
});
|
||||
|
||||
it("applies sanitizer when provided", () => {
|
||||
expect(
|
||||
extractTextFromChatContent("Here [Tool Call: foo (ID: 1)] ok", {
|
||||
sanitizeText: (text) => text.replace(/\[Tool Call:[^\]]+\]\s*/g, ""),
|
||||
}),
|
||||
).toBe("Here ok");
|
||||
});
|
||||
});
|
||||
37
src/shared/chat-content.ts
Normal file
37
src/shared/chat-content.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
export function extractTextFromChatContent(
|
||||
content: unknown,
|
||||
opts?: { sanitizeText?: (text: string) => string },
|
||||
): string | null {
|
||||
const normalize = (text: string) => text.replace(/\s+/g, " ").trim();
|
||||
|
||||
if (typeof content === "string") {
|
||||
const value = opts?.sanitizeText ? opts.sanitizeText(content) : content;
|
||||
const normalized = normalize(value);
|
||||
return normalized ? normalized : null;
|
||||
}
|
||||
|
||||
if (!Array.isArray(content)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const chunks: string[] = [];
|
||||
for (const block of content) {
|
||||
if (!block || typeof block !== "object") {
|
||||
continue;
|
||||
}
|
||||
if ((block as { type?: unknown }).type !== "text") {
|
||||
continue;
|
||||
}
|
||||
const text = (block as { text?: unknown }).text;
|
||||
if (typeof text !== "string") {
|
||||
continue;
|
||||
}
|
||||
const value = opts?.sanitizeText ? opts.sanitizeText(text) : text;
|
||||
if (value.trim()) {
|
||||
chunks.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
const joined = normalize(chunks.join(" "));
|
||||
return joined ? joined : null;
|
||||
}
|
||||
Reference in New Issue
Block a user