mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 22:31:25 +00:00
refactor(shared): reuse chat content extractor for assistant text
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
||||
import { extractTextFromChatContent } from "../shared/chat-content.js";
|
||||
import { stripReasoningTagsFromText } from "../shared/text/reasoning-tags.js";
|
||||
import { sanitizeUserFacingText } from "./pi-embedded-helpers.js";
|
||||
import { formatToolDetail, resolveToolDisplay } from "./tool-display.js";
|
||||
@@ -207,25 +208,15 @@ export function stripThinkingTagsFromText(text: string): string {
|
||||
}
|
||||
|
||||
export function extractAssistantText(msg: AssistantMessage): string {
|
||||
const isTextBlock = (block: unknown): block is { type: "text"; text: string } => {
|
||||
if (!block || typeof block !== "object") {
|
||||
return false;
|
||||
}
|
||||
const rec = block as Record<string, unknown>;
|
||||
return rec.type === "text" && typeof rec.text === "string";
|
||||
};
|
||||
|
||||
const blocks = Array.isArray(msg.content)
|
||||
? msg.content
|
||||
.filter(isTextBlock)
|
||||
.map((c) =>
|
||||
stripThinkingTagsFromText(
|
||||
stripDowngradedToolCallText(stripMinimaxToolCallXml(c.text)),
|
||||
).trim(),
|
||||
)
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
const extracted = blocks.join("\n").trim();
|
||||
const extracted =
|
||||
extractTextFromChatContent(msg.content, {
|
||||
sanitizeText: (text) =>
|
||||
stripThinkingTagsFromText(
|
||||
stripDowngradedToolCallText(stripMinimaxToolCallXml(text)),
|
||||
).trim(),
|
||||
joinWith: "\n",
|
||||
normalizeText: (text) => text.trim(),
|
||||
}) ?? "";
|
||||
// Only apply keyword-based error rewrites when the assistant message is actually an error.
|
||||
// Otherwise normal prose that *mentions* errors (e.g. "context overflow") can get clobbered.
|
||||
const errorContext = msg.stopReason === "error" || Boolean(msg.errorMessage?.trim());
|
||||
|
||||
@@ -24,6 +24,7 @@ export {
|
||||
resolveSessionReference,
|
||||
shouldResolveSessionIdInput,
|
||||
} from "./sessions-resolution.js";
|
||||
import { extractTextFromChatContent } from "../../shared/chat-content.js";
|
||||
import { sanitizeUserFacingText } from "../pi-embedded-helpers.js";
|
||||
import {
|
||||
stripDowngradedToolCallText,
|
||||
@@ -152,23 +153,12 @@ export function extractAssistantText(message: unknown): string | undefined {
|
||||
if (!Array.isArray(content)) {
|
||||
return undefined;
|
||||
}
|
||||
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") {
|
||||
const sanitized = sanitizeTextContent(text);
|
||||
if (sanitized.trim()) {
|
||||
chunks.push(sanitized);
|
||||
}
|
||||
}
|
||||
}
|
||||
const joined = chunks.join("").trim();
|
||||
const joined =
|
||||
extractTextFromChatContent(content, {
|
||||
sanitizeText: sanitizeTextContent,
|
||||
joinWith: "",
|
||||
normalizeText: (text) => text.trim(),
|
||||
}) ?? "";
|
||||
const stopReason = (message as { stopReason?: unknown }).stopReason;
|
||||
const errorMessage = (message as { errorMessage?: unknown }).errorMessage;
|
||||
const errorContext =
|
||||
|
||||
Reference in New Issue
Block a user