utils.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import type {
  2. AssistantMessage,
  3. AttachmentMessage,
  4. SystemMessage,
  5. UserMessage,
  6. } from 'src/types/message.js'
  7. /**
  8. * Tags user messages with a sourceToolUseID so they stay transient until the tool resolves.
  9. * This prevents the "is running" message from being duplicated in the UI.
  10. */
  11. export function tagMessagesWithToolUseID(
  12. messages: (UserMessage | AttachmentMessage | SystemMessage)[],
  13. toolUseID: string | undefined,
  14. ): (UserMessage | AttachmentMessage | SystemMessage)[] {
  15. if (!toolUseID) {
  16. return messages
  17. }
  18. return messages.map(m => {
  19. if (m.type === 'user') {
  20. return { ...m, sourceToolUseID: toolUseID }
  21. }
  22. return m
  23. })
  24. }
  25. /**
  26. * Extracts the tool use ID from a parent message for a given tool name.
  27. */
  28. export function getToolUseIDFromParentMessage(
  29. parentMessage: AssistantMessage,
  30. toolName: string,
  31. ): string | undefined {
  32. const toolUseBlock = parentMessage.message.content.find(
  33. block => block.type === 'tool_use' && block.name === toolName,
  34. )
  35. return toolUseBlock && toolUseBlock.type === 'tool_use'
  36. ? toolUseBlock.id
  37. : undefined
  38. }