mirror of
https://github.com/mudler/LocalAI.git
synced 2026-06-13 03:09:03 -04:00
fix(agents): make React agent chat timestamps format-agnostic
The agent SSE bridge emits the json_message timestamp in three different encodings depending on deploy mode: an RFC3339 string (standalone agent pool), Unix milliseconds (local dispatcher), and Unix nanoseconds (the older NATS path). The React AgentChat handler passed data.timestamp straight through, so the standalone string and any numeric value outside the millisecond range rendered as "Invalid Timestamp" or a constant epoch-ish time. Add a small pure helper, normalizeTimestampMs, that accepts an RFC3339 string or a numeric epoch in s/ms/us/ns and returns JS milliseconds, falling back to Date.now() on null/empty/unparseable input. Use it in the json_message handler so the rendered time is correct regardless of which backend path produced it. Fixes #9867 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Assisted-by: claude:claude-opus-4-8 [Claude Code] Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
@@ -8,7 +8,7 @@ import CanvasPanel from '../components/CanvasPanel'
|
||||
import ResourceCards from '../components/ResourceCards'
|
||||
import ConfirmDialog from '../components/ConfirmDialog'
|
||||
import { useAgentChat } from '../hooks/useAgentChat'
|
||||
import { relativeTime } from '../utils/format'
|
||||
import { relativeTime, normalizeTimestampMs } from '../utils/format'
|
||||
import { copyToClipboard } from '../utils/clipboard'
|
||||
|
||||
function getLastMessagePreview(conv) {
|
||||
@@ -139,8 +139,9 @@ export default function AgentChat() {
|
||||
id: nextId(),
|
||||
sender,
|
||||
content: data.content || data.message || '',
|
||||
// Backend sends Unix milliseconds (see core/services/agents events).
|
||||
timestamp: data.timestamp || Date.now(),
|
||||
// Backend timestamp encoding varies by deploy mode (RFC3339 string,
|
||||
// Unix ms, or Unix ns); normalize to JS milliseconds.
|
||||
timestamp: normalizeTimestampMs(data.timestamp),
|
||||
}
|
||||
if (data.metadata && Object.keys(data.metadata).length > 0) {
|
||||
msg.metadata = data.metadata
|
||||
|
||||
20
core/http/react-ui/src/utils/format.js
vendored
20
core/http/react-ui/src/utils/format.js
vendored
@@ -12,6 +12,26 @@ export function percentColor(pct) {
|
||||
return 'var(--color-success)'
|
||||
}
|
||||
|
||||
// normalizeTimestampMs converts a timestamp emitted by the backend into JS
|
||||
// milliseconds, regardless of its encoding. The agent SSE bridge emits the
|
||||
// json_message timestamp in three different shapes depending on deploy mode:
|
||||
// an RFC3339 string (standalone agent pool), Unix milliseconds (local
|
||||
// dispatcher), or Unix nanoseconds (older NATS path). A numeric value is
|
||||
// classified by magnitude (s / ms / us / ns) so any of them yields a sane
|
||||
// epoch. Falls back to Date.now() for null/empty/unparseable input.
|
||||
export function normalizeTimestampMs(ts) {
|
||||
if (ts === null || ts === undefined || ts === '') return Date.now()
|
||||
if (typeof ts === 'string') {
|
||||
const parsed = Date.parse(ts)
|
||||
return Number.isNaN(parsed) ? Date.now() : parsed
|
||||
}
|
||||
if (typeof ts !== 'number' || !Number.isFinite(ts)) return Date.now()
|
||||
if (ts > 1e17) return Math.floor(ts / 1e6) // nanoseconds
|
||||
if (ts > 1e14) return Math.floor(ts / 1e3) // microseconds
|
||||
if (ts > 1e11) return ts // milliseconds
|
||||
return ts * 1000 // seconds
|
||||
}
|
||||
|
||||
export function formatTimestamp(ts) {
|
||||
if (!ts) return '-'
|
||||
const d = new Date(ts)
|
||||
|
||||
Reference in New Issue
Block a user