(a: UserMessage, b: UserMessage)
| 2409 | } |
| 2410 | |
| 2411 | export function mergeUserMessages(a: UserMessage, b: UserMessage): UserMessage { |
| 2412 | const lastContent = normalizeUserTextContent(a.message.content) |
| 2413 | const currentContent = normalizeUserTextContent(b.message.content) |
| 2414 | if (feature('HISTORY_SNIP')) { |
| 2415 | // A merged message is only meta if ALL merged messages are meta. If any |
| 2416 | // operand is real user content, the result must not be flagged isMeta |
| 2417 | // (so [id:] tags get injected and it's treated as user-visible content). |
| 2418 | // Gated behind the full runtime check because changing isMeta semantics |
| 2419 | // affects downstream callers (e.g., VCR fixture hashing in SDK harness |
| 2420 | // tests), so this must only fire when snip is actually enabled — not |
| 2421 | // for all ants. |
| 2422 | const { isSnipRuntimeEnabled } = |
| 2423 | // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 2424 | require('../services/compact/snipCompact.js') as typeof import('../services/compact/snipCompact.js') |
| 2425 | if (isSnipRuntimeEnabled()) { |
| 2426 | return { |
| 2427 | ...a, |
| 2428 | isMeta: a.isMeta && b.isMeta ? (true as const) : undefined, |
| 2429 | uuid: a.isMeta ? b.uuid : a.uuid, |
| 2430 | message: { |
| 2431 | ...a.message, |
| 2432 | content: hoistToolResults( |
| 2433 | joinTextAtSeam(lastContent, currentContent), |
| 2434 | ), |
| 2435 | }, |
| 2436 | } |
| 2437 | } |
| 2438 | } |
| 2439 | return { |
| 2440 | ...a, |
| 2441 | // Preserve the non-meta message's uuid so [id:] tags (derived from uuid) |
| 2442 | // stay stable across API calls (meta messages like system context get fresh uuids each call) |
| 2443 | uuid: a.isMeta ? b.uuid : a.uuid, |
| 2444 | message: { |
| 2445 | ...a.message, |
| 2446 | content: hoistToolResults(joinTextAtSeam(lastContent, currentContent)), |
| 2447 | }, |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | function mergeAdjacentUserMessages( |
| 2452 | msgs: (UserMessage | AssistantMessage)[], |
no test coverage detected