(
messages: (
| NormalizedUserMessage
| NormalizedAssistantMessage
| AttachmentMessage
| SystemMessage
)[],
syntheticStreamingToolUseMessages: NormalizedAssistantMessage[],
)
| 853 | |
| 854 | // Re-order, to move result messages to be after their tool use messages |
| 855 | export function reorderMessagesInUI( |
| 856 | messages: ( |
| 857 | | NormalizedUserMessage |
| 858 | | NormalizedAssistantMessage |
| 859 | | AttachmentMessage |
| 860 | | SystemMessage |
| 861 | )[], |
| 862 | syntheticStreamingToolUseMessages: NormalizedAssistantMessage[], |
| 863 | ): ( |
| 864 | | NormalizedUserMessage |
| 865 | | NormalizedAssistantMessage |
| 866 | | AttachmentMessage |
| 867 | | SystemMessage |
| 868 | )[] { |
| 869 | // Maps tool use ID to its related messages |
| 870 | const toolUseGroups = new Map< |
| 871 | string, |
| 872 | { |
| 873 | toolUse: ToolUseRequestMessage | null |
| 874 | preHooks: AttachmentMessage[] |
| 875 | toolResult: NormalizedUserMessage | null |
| 876 | postHooks: AttachmentMessage[] |
| 877 | } |
| 878 | >() |
| 879 | |
| 880 | // First pass: group messages by tool use ID |
| 881 | for (const message of messages) { |
| 882 | // Handle tool use messages |
| 883 | if (isToolUseRequestMessage(message)) { |
| 884 | const toolUseID = message.message.content[0]?.id |
| 885 | if (toolUseID) { |
| 886 | if (!toolUseGroups.has(toolUseID)) { |
| 887 | toolUseGroups.set(toolUseID, { |
| 888 | toolUse: null, |
| 889 | preHooks: [], |
| 890 | toolResult: null, |
| 891 | postHooks: [], |
| 892 | }) |
| 893 | } |
| 894 | toolUseGroups.get(toolUseID)!.toolUse = message |
| 895 | } |
| 896 | continue |
| 897 | } |
| 898 | |
| 899 | // Handle pre-tool-use hooks |
| 900 | if ( |
| 901 | isHookAttachmentMessage(message) && |
| 902 | message.attachment.hookEvent === 'PreToolUse' |
| 903 | ) { |
| 904 | const toolUseID = message.attachment.toolUseID |
| 905 | if (!toolUseGroups.has(toolUseID)) { |
| 906 | toolUseGroups.set(toolUseID, { |
| 907 | toolUse: null, |
| 908 | preHooks: [], |
| 909 | toolResult: null, |
| 910 | postHooks: [], |
| 911 | }) |
| 912 | } |
no test coverage detected