

























@@ -6,6 +6,10 @@
66import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
77import { getEnvApiKey } from "../llm/env-api-keys.js";
88import { calculateCost, clampThinkingLevel } from "../llm/model-utils.js";
9+import {
10+ANTHROPIC_OMITTED_REASONING_TEXT,
11+findActiveAnthropicToolTurnAssistantIndex,
12+} from "../llm/providers/anthropic-thinking-replay.js";
913import type { AnthropicOptions } from "../llm/providers/anthropic.js";
1014import type {
1115AssistantMessageDiagnostic,
@@ -372,11 +376,18 @@ function convertAnthropicMessages(
372376messages: Context["messages"],
373377model: AnthropicTransportModel,
374378isOAuthToken: boolean,
375-options?: { allowReasoningContentReplay?: boolean },
379+options?: {
380+allowReasoningContentReplay?: boolean;
381+replayThinkingEnabled?: boolean;
382+},
376383) {
377384const params: Array<Record<string, unknown>> = [];
378385const allowReasoningContentReplay = options?.allowReasoningContentReplay === true;
386+const replayThinkingEnabled = options?.replayThinkingEnabled !== false;
379387const transformedMessages = transformTransportMessages(messages, model, normalizeToolCallId);
388+const activeToolTurnAssistantIndex = replayThinkingEnabled
389+ ? -1
390+ : findActiveAnthropicToolTurnAssistantIndex(transformedMessages);
380391for (let i = 0; i < transformedMessages.length; i += 1) {
381392const msg = transformedMessages[i];
382393if (msg.role === "user") {
@@ -428,6 +439,7 @@ function convertAnthropicMessages(
428439if (msg.role === "assistant") {
429440const blocks: Array<Record<string, unknown>> = [];
430441const reasoningContent: string[] = [];
442+let omittedThinking = false;
431443for (const block of msg.content) {
432444if (block.type === "text") {
433445if (block.text.trim().length > 0) {
@@ -439,16 +451,20 @@ function convertAnthropicMessages(
439451continue;
440452}
441453if (block.type === "thinking") {
454+const thinkingSignature = block.thinkingSignature?.trim();
455+const isReasoningContent = thinkingSignature === "reasoning_content";
456+if (!replayThinkingEnabled && i !== activeToolTurnAssistantIndex && !isReasoningContent) {
457+omittedThinking = true;
458+continue;
459+}
442460if (block.redacted) {
443461blocks.push({
444462type: "redacted_thinking",
445463data: block.thinkingSignature,
446464});
447465continue;
448466}
449-const thinkingSignature = block.thinkingSignature?.trim();
450-const hasNativeThinkingSignature =
451-Boolean(thinkingSignature) && thinkingSignature !== "reasoning_content";
467+const hasNativeThinkingSignature = Boolean(thinkingSignature) && !isReasoningContent;
452468if (block.thinking.trim().length === 0 && !hasNativeThinkingSignature) {
453469continue;
454470}
@@ -490,6 +506,9 @@ function convertAnthropicMessages(
490506});
491507}
492508}
509+if (blocks.length === 0 && omittedThinking) {
510+blocks.push({ type: "text", text: ANTHROPIC_OMITTED_REASONING_TEXT });
511+}
493512if (blocks.length > 0) {
494513const assistantMsg: Record<string, unknown> = { role: "assistant", content: blocks };
495514if (reasoningContent.length > 0) {
@@ -899,6 +918,8 @@ function buildAnthropicParams(
899918isOAuthToken: boolean,
900919options: AnthropicTransportOptions | undefined,
901920) {
921+const fable5 = usesClaudeFable5MessagesContract(model);
922+const replayThinkingEnabled = fable5 || options?.thinkingEnabled === true;
902923const maxTokens = resolveAnthropicMessagesMaxTokens({
903924modelMaxTokens: model.maxTokens,
904925requestedMaxTokens: options?.maxTokens,
@@ -920,6 +941,7 @@ function buildAnthropicParams(
920941messages: ensureNonEmptyAnthropicMessages(
921942convertAnthropicMessages(context.messages, model, isOAuthToken, {
922943allowReasoningContentReplay: supportsReasoningContentReplay(model),
944+ replayThinkingEnabled,
923945}),
924946),
925947max_tokens: maxTokens,
@@ -961,7 +983,6 @@ function buildAnthropicParams(
961983if (context.tools) {
962984params.tools = convertAnthropicTools(context.tools, isOAuthToken);
963985}
964-const fable5 = usesClaudeFable5MessagesContract(model);
965986if (fable5 || model.reasoning || supportsAdaptiveThinking(model)) {
966987if (fable5 || options?.thinkingEnabled) {
967988if (supportsAdaptiveThinking(model)) {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。