chatUnread.ts 1.8 KB

1234567891011121314151617181920212223242526
  1. /** List badges count completed results; transport rows and progress never count. */
  2. export function isFinalChatResult(message: Record<string, any>): boolean {
  3. if (message.role !== 'assistant' || message.error || message.is_streaming || message.isStreaming) return false
  4. if (message.final_result === false || message.is_interim || message.display_kind || message.displayKind) return false
  5. const status = message.status ?? message.finish_reason ?? message.finishReason
  6. if (['streaming', 'pending', 'running', 'interim', 'failed', 'error', 'cancelled', 'interrupted', 'tool_calls', 'function_call'].includes(status)) return false
  7. // Live projections retain tool traces on the final answer. Historical tool
  8. // invocation rows have no explicit completion marker and must be excluded.
  9. if (message.final_result !== true) {
  10. let tools = message.tool_calls ?? message.toolCalls ?? message.tools
  11. if (typeof tools === 'string') {
  12. try { tools = JSON.parse(tools) } catch { return false }
  13. }
  14. if ((Array.isArray(tools) ? tools.length > 0 : !!tools) || message.function_call) return false
  15. if (Array.isArray(message.content) && message.content.some(block =>
  16. ['tool_use', 'tool_call', 'function_call'].includes(block?.type))) return false
  17. }
  18. const content = message.content ?? message.text
  19. const visible = (value: unknown) => typeof value === 'string'
  20. && value.replace(/<(think|thinking|reasoning)\b[^>]*>[\s\S]*?(?:<\/\1>|$)/gi, '').trim().length > 0
  21. return visible(content)
  22. || (Array.isArray(content) && content.some(block => block && (
  23. ['text', 'output_text'].includes(block.type) && visible(block.text)
  24. || ['image', 'image_url', 'file', 'audio', 'video'].includes(block.type))))
  25. || (Array.isArray(message.attachments) && message.attachments.length > 0)
  26. }