messageDisplay.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { visibleMessageText } from '@shared/messageFiles'
  2. const ATTACHED_CONTEXT_MARKER_RE = /(?:^|\n)--- Attached Context ---\s*\n/
  3. const CONTEXT_WARNINGS_MARKER_RE = /(?:^|\n)--- Context Warnings ---[\s\S]*$/
  4. const CONTEXT_REF_RE = /@(file|folder|url|image|tool|terminal):(?:"[^"\n]+"|'[^'\n]+'|`[^`\n]+`|\S+)/g
  5. /**
  6. * Project persisted message content into the text shown to a person.
  7. *
  8. * Hermes deliberately keeps expanded reference payloads in the stored user
  9. * message so the model can consume them. Those payloads are transport data,
  10. * not part of the user's visible transcript. Keep the raw content in state and
  11. * apply this projection only at display/copy boundaries.
  12. */
  13. export function displayContentForMessage(role: string, content: string): string {
  14. if (role === 'assistant') return visibleMessageText(content)
  15. if (role !== 'user') return content
  16. const marker = content.match(ATTACHED_CONTEXT_MARKER_RE)
  17. if (!marker || marker.index === undefined) {
  18. return content.replace(CONTEXT_WARNINGS_MARKER_RE, '').trim()
  19. }
  20. const visibleText = content
  21. .slice(0, marker.index)
  22. .replace(CONTEXT_WARNINGS_MARKER_RE, '')
  23. .trim()
  24. const attachedContext = content.slice(marker.index + marker[0].length)
  25. const references = [...new Set(
  26. Array.from(attachedContext.matchAll(CONTEXT_REF_RE), match => match[0]),
  27. )]
  28. const missingReferences = references.filter(reference => !visibleText.includes(reference))
  29. return [missingReferences.join('\n'), visibleText].filter(Boolean).join('\n\n') || visibleText
  30. }