mirror of
https://github.com/openclaw/openclaw.git
synced 2026-10-03 09:39:25 +00:00
perf(ai): prepare assistant replay blocks in one pass (#141419)
Reuse the classified text, thinking, and tool-call collections and avoid joining text when replay needs content parts. Preserve Unicode sanitation, reasoning signatures, tool order, and the existing wire representation.
This commit is contained in:
parent
69faebcd4b
commit
8efdc03f8c
2 changed files with 71 additions and 54 deletions
|
|
@ -80,7 +80,7 @@ describe("convertMessages assistant text replay", () => {
|
|||
});
|
||||
|
||||
it.each([false, true])(
|
||||
"preserves sanitized block positions with thinking-as-text %s",
|
||||
"preserves interleaved text, thinking, and tool replay with thinking-as-text %s",
|
||||
(requiresThinkingAsText) => {
|
||||
const assistant: AssistantMessage = {
|
||||
role: "assistant",
|
||||
|
|
@ -88,20 +88,44 @@ describe("convertMessages assistant text replay", () => {
|
|||
provider: model.provider,
|
||||
model: model.id,
|
||||
content: [
|
||||
{ type: "thinking", thinking: "reason\ud800" },
|
||||
{ type: "thinking", thinking: " \t", thinkingSignature: "reasoning_text" },
|
||||
{
|
||||
type: "thinking",
|
||||
thinking: "reason\ud800",
|
||||
thinkingSignature: "reasoning_content",
|
||||
},
|
||||
{ type: "text", text: " \t" },
|
||||
{ type: "text", text: "first\ud800" },
|
||||
{ type: "text", text: "\udc00" },
|
||||
{ type: "thinking", thinking: "next😀" },
|
||||
{
|
||||
type: "toolCall",
|
||||
id: "call_lookup",
|
||||
name: "lookup",
|
||||
arguments: { query: "cats" },
|
||||
thoughtSignature: '{"type":"reasoning.encrypted","data":"synthetic"}',
|
||||
},
|
||||
{ type: "thinking", thinking: "next😀", thinkingSignature: "reasoning_text" },
|
||||
{ type: "text", text: "last😀" },
|
||||
],
|
||||
usage: emptyUsage,
|
||||
stopReason: "stop",
|
||||
stopReason: "toolUse",
|
||||
timestamp: 2,
|
||||
};
|
||||
const converted = convertMessages(
|
||||
model,
|
||||
{ messages: [assistant] },
|
||||
{
|
||||
messages: [
|
||||
assistant,
|
||||
{
|
||||
role: "toolResult",
|
||||
toolCallId: "call_lookup",
|
||||
toolName: "lookup",
|
||||
content: [{ type: "text", text: "found" }],
|
||||
isError: false,
|
||||
timestamp: 3,
|
||||
},
|
||||
],
|
||||
},
|
||||
{ ...resolveOpenAICompletionsCompat(model), requiresThinkingAsText },
|
||||
);
|
||||
|
||||
|
|
@ -116,7 +140,17 @@ describe("convertMessages assistant text replay", () => {
|
|||
{ type: "text", text: "last😀" },
|
||||
]
|
||||
: "first\n\nlast😀",
|
||||
...(!requiresThinkingAsText && { reasoning_content: "reason\ud800\nnext😀" }),
|
||||
tool_calls: [
|
||||
{
|
||||
id: "call_lookup",
|
||||
type: "function",
|
||||
function: { name: "lookup", arguments: '{"query":"cats"}' },
|
||||
},
|
||||
],
|
||||
reasoning_details: [{ type: "reasoning.encrypted", data: "synthetic" }],
|
||||
},
|
||||
{ role: "tool", content: "found", tool_call_id: "call_lookup" },
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import {
|
|||
isImageWithMediaPayload,
|
||||
} from "./providers/tool-result-text.js";
|
||||
import type { ResolvedOpenAICompletionsCompat } from "./transports/openai-completions-compat.js";
|
||||
import type { Context, Model, TextContent, ThinkingContent, ToolCall } from "./types.js";
|
||||
import type { Context, Model, ThinkingContent, ToolCall } from "./types.js";
|
||||
import { sanitizeSurrogates } from "./utils/sanitize-unicode.js";
|
||||
import { stripSystemPromptCacheBoundary } from "./utils/system-prompt-cache-boundary.js";
|
||||
|
||||
|
|
@ -25,18 +25,6 @@ type ChatCompletionContentPartVideo = {
|
|||
video_url: { url: string };
|
||||
};
|
||||
|
||||
function isTextContentBlock(block: { type: string }): block is TextContent {
|
||||
return block.type === "text";
|
||||
}
|
||||
|
||||
function isThinkingContentBlock(block: { type: string }): block is ThinkingContent {
|
||||
return block.type === "thinking";
|
||||
}
|
||||
|
||||
function isToolCallBlock(block: { type: string }): block is ToolCall {
|
||||
return block.type === "toolCall";
|
||||
}
|
||||
|
||||
function sanitizeToolResultText(text: string, fallback: string): string {
|
||||
const sanitized = sanitizeSurrogates(text);
|
||||
return sanitized.trim().length > 0 ? sanitized : fallback;
|
||||
|
|
@ -153,35 +141,36 @@ export function convertMessages(
|
|||
content: compat.requiresAssistantAfterToolResult ? "" : null,
|
||||
};
|
||||
|
||||
const assistantTexts = msg.content
|
||||
.filter(isTextContentBlock)
|
||||
.filter((block) => block.text.trim().length > 0)
|
||||
.map((block) => sanitizeSurrogates(block.text));
|
||||
// Separate content blocks are distinct utterances, so replay them the way
|
||||
// the string-content flattener does rather than running them together.
|
||||
const assistantText = assistantTexts.join("\n");
|
||||
|
||||
const nonEmptyThinkingBlocks = msg.content
|
||||
.filter(isThinkingContentBlock)
|
||||
.filter((block) => block.thinking.trim().length > 0);
|
||||
if (nonEmptyThinkingBlocks.length > 0) {
|
||||
if (compat.requiresThinkingAsText) {
|
||||
const thinkingText = nonEmptyThinkingBlocks
|
||||
.map((block) => sanitizeSurrogates(block.thinking))
|
||||
.join("\n\n");
|
||||
assistantMsg.content = [
|
||||
{ type: "text", text: thinkingText },
|
||||
...assistantTexts.map((text): ChatCompletionContentPartText => ({
|
||||
type: "text",
|
||||
text,
|
||||
})),
|
||||
];
|
||||
} else {
|
||||
// String content is the interoperable Chat Completions replay shape;
|
||||
// content-part arrays make some compatible servers mirror JSON.
|
||||
if (assistantText.length > 0) {
|
||||
assistantMsg.content = assistantText;
|
||||
}
|
||||
const assistantTexts: string[] = [];
|
||||
const nonEmptyThinkingBlocks: ThinkingContent[] = [];
|
||||
const toolCalls: ToolCall[] = [];
|
||||
msg.content.forEach((block) => {
|
||||
if (block.type === "text" && block.text.trim().length > 0) {
|
||||
assistantTexts.push(sanitizeSurrogates(block.text));
|
||||
} else if (block.type === "thinking" && block.thinking.trim().length > 0) {
|
||||
nonEmptyThinkingBlocks.push(block);
|
||||
} else if (block.type === "toolCall") {
|
||||
toolCalls.push(block);
|
||||
}
|
||||
});
|
||||
if (nonEmptyThinkingBlocks.length > 0 && compat.requiresThinkingAsText) {
|
||||
const thinkingText = nonEmptyThinkingBlocks
|
||||
.map((block) => sanitizeSurrogates(block.thinking))
|
||||
.join("\n\n");
|
||||
assistantMsg.content = [
|
||||
{ type: "text", text: thinkingText },
|
||||
...assistantTexts.map((text): ChatCompletionContentPartText => ({
|
||||
type: "text",
|
||||
text,
|
||||
})),
|
||||
];
|
||||
} else {
|
||||
// Keep separate utterances apart in the interoperable string replay shape.
|
||||
const assistantText = assistantTexts.join("\n");
|
||||
if (assistantText.length > 0) {
|
||||
assistantMsg.content = assistantText;
|
||||
}
|
||||
if (nonEmptyThinkingBlocks.length > 0) {
|
||||
let signature = nonEmptyThinkingBlocks.at(0)?.thinkingSignature;
|
||||
if (model.provider === "opencode-go" && signature === "reasoning") {
|
||||
signature = "reasoning_content";
|
||||
|
|
@ -191,11 +180,8 @@ export function convertMessages(
|
|||
nonEmptyThinkingBlocks.map((block) => block.thinking).join("\n");
|
||||
}
|
||||
}
|
||||
} else if (assistantText.length > 0) {
|
||||
assistantMsg.content = assistantText;
|
||||
}
|
||||
|
||||
const toolCalls = msg.content.filter(isToolCallBlock);
|
||||
if (toolCalls.length > 0) {
|
||||
assistantMsg.tool_calls = toolCalls.map((toolCall) => ({
|
||||
id: toolCall.id,
|
||||
|
|
@ -231,10 +217,7 @@ export function convertMessages(
|
|||
(assistantMsg as { reasoning_content?: string }).reasoning_content = "";
|
||||
}
|
||||
const content = assistantMsg.content;
|
||||
const hasContent =
|
||||
content !== null &&
|
||||
content !== undefined &&
|
||||
(typeof content === "string" ? content.length > 0 : content.length > 0);
|
||||
const hasContent = content !== null && content !== undefined && content.length > 0;
|
||||
if (!hasContent && !assistantMsg.tool_calls) {
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue