* Appends a [id:...] message ID tag to the last text block of a user message. * Only mutates the API-bound copy, not the stored message. * This lets Claude reference message IDs when calling the snip tool.
(message: UserMessage)
| 1618 | * This lets Claude reference message IDs when calling the snip tool. |
| 1619 | */ |
| 1620 | function appendMessageTagToUserMessage(message: UserMessage): UserMessage { |
| 1621 | if (message.isMeta) { |
| 1622 | return message |
| 1623 | } |
| 1624 | |
| 1625 | const tag = `\n[id:${deriveShortMessageId(message.uuid)}]` |
| 1626 | |
| 1627 | const content = message.message.content |
| 1628 | |
| 1629 | // Handle string content (most common for simple text input) |
| 1630 | if (typeof content === 'string') { |
| 1631 | return { |
| 1632 | ...message, |
| 1633 | message: { |
| 1634 | ...message.message, |
| 1635 | content: content + tag, |
| 1636 | }, |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | if (!Array.isArray(content) || content.length === 0) { |
| 1641 | return message |
| 1642 | } |
| 1643 | |
| 1644 | // Find the last text block |
| 1645 | let lastTextIdx = -1 |
| 1646 | for (let i = content.length - 1; i >= 0; i--) { |
| 1647 | if (content[i]!.type === 'text') { |
| 1648 | lastTextIdx = i |
| 1649 | break |
| 1650 | } |
| 1651 | } |
| 1652 | if (lastTextIdx === -1) { |
| 1653 | return message |
| 1654 | } |
| 1655 | |
| 1656 | const newContent = [...content] |
| 1657 | const textBlock = newContent[lastTextIdx] as TextBlockParam |
| 1658 | newContent[lastTextIdx] = { |
| 1659 | ...textBlock, |
| 1660 | text: textBlock.text + tag, |
| 1661 | } |
| 1662 | |
| 1663 | return { |
| 1664 | ...message, |
| 1665 | message: { |
| 1666 | ...message.message, |
| 1667 | content: newContent as typeof content, |
| 1668 | }, |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | /** |
| 1673 | * Strips tool_reference blocks from tool_result content in a user message. |
no test coverage detected