mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
refactor(shared): reuse chat content extractor for assistant text
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
export function extractTextFromChatContent(
|
||||
content: unknown,
|
||||
opts?: { sanitizeText?: (text: string) => string },
|
||||
opts?: {
|
||||
sanitizeText?: (text: string) => string;
|
||||
joinWith?: string;
|
||||
normalizeText?: (text: string) => string;
|
||||
},
|
||||
): string | null {
|
||||
const normalize = (text: string) => text.replace(/\s+/g, " ").trim();
|
||||
const normalize = opts?.normalizeText ?? ((text: string) => text.replace(/\s+/g, " ").trim());
|
||||
const joinWith = opts?.joinWith ?? " ";
|
||||
|
||||
if (typeof content === "string") {
|
||||
const value = opts?.sanitizeText ? opts.sanitizeText(content) : content;
|
||||
@@ -32,6 +37,6 @@ export function extractTextFromChatContent(
|
||||
}
|
||||
}
|
||||
|
||||
const joined = normalize(chunks.join(" "));
|
||||
const joined = normalize(chunks.join(joinWith));
|
||||
return joined ? joined : null;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,22 @@ describe("extractTextFromChatContent", () => {
|
||||
}),
|
||||
).toBe("Here ok");
|
||||
});
|
||||
|
||||
it("supports custom join and normalization", () => {
|
||||
expect(
|
||||
extractTextFromChatContent(
|
||||
[
|
||||
{ type: "text", text: " hello " },
|
||||
{ type: "text", text: "world " },
|
||||
],
|
||||
{
|
||||
sanitizeText: (text) => text.trim(),
|
||||
joinWith: "\n",
|
||||
normalizeText: (text) => text.trim(),
|
||||
},
|
||||
),
|
||||
).toBe("hello\nworld");
|
||||
});
|
||||
});
|
||||
|
||||
describe("shared/frontmatter", () => {
|
||||
|
||||
Reference in New Issue
Block a user