Roo-Code/src/core/task/mergeConsecutiveApiMessages.ts
Daniel 6cfa82f571
Revert to pre-AI-SDK state (January 29, 2026) (#11462)
Revert to pre-AI-SDK state (commit 67e568f6b)

This commit reverts the codebase to the state before AI SDK migration work began.

Target commit: 67e568f6b - refactor: replace fetch_instructions with skill tool and built-in skills (#10913)
Date: January 29, 2026

This removes approximately 152 commits of AI SDK migration work.
A follow-up PR will add back bug fixes and features that are unrelated to AI SDK.

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-13 16:45:18 -05:00

59 lines
1.7 KiB
TypeScript

import { Anthropic } from "@anthropic-ai/sdk"
import type { ApiMessage } from "../task-persistence"
type Role = ApiMessage["role"]
function normalizeContentToBlocks(content: ApiMessage["content"]): Anthropic.Messages.ContentBlockParam[] {
if (Array.isArray(content)) {
return content as Anthropic.Messages.ContentBlockParam[]
}
if (content === undefined || content === null) {
return []
}
return [{ type: "text", text: String(content) }]
}
/**
* Non-destructively merges consecutive messages with the same role.
*
* Used for *API request shaping only* (do not use for storage), so rewind/edit operations
* can still reference the original individual messages.
*/
export function mergeConsecutiveApiMessages(messages: ApiMessage[], options?: { roles?: Role[] }): ApiMessage[] {
if (messages.length <= 1) {
return messages
}
const mergeRoles = new Set<Role>(options?.roles ?? ["user"]) // default: user only
const out: ApiMessage[] = []
for (const msg of messages) {
const prev = out[out.length - 1]
const canMerge =
prev &&
prev.role === msg.role &&
mergeRoles.has(msg.role) &&
// Allow merging regular messages into a summary (API-only shaping),
// but never merge a summary into something else.
!msg.isSummary &&
!prev.isTruncationMarker &&
!msg.isTruncationMarker
if (!canMerge) {
out.push(msg)
continue
}
const mergedContent = [...normalizeContentToBlocks(prev.content), ...normalizeContentToBlocks(msg.content)]
// Preserve the newest ts to keep chronological ordering for downstream logic.
out[out.length - 1] = {
...prev,
content: mergedContent,
ts: Math.max(prev.ts ?? 0, msg.ts ?? 0) || prev.ts || msg.ts,
}
}
return out
}