feat(gateway): inject stable human-readable message timestamps
Consolidates these related Amy fork patches: - 429830f39 feat(gateway): inject message timestamps into user messages for LLM context - 3c3d6fac0 fix: handle both ISO string and epoch float timestamps in history replay - 2874f7725 feat: human-friendly timestamp format with weekday and timezone name - 3735f4c8b fix: render gateway message timestamps once
This commit is contained in:
committed by
Teknium
parent
b7f0c9cd52
commit
bd7fc8fdcd
+28
-5
@@ -2379,6 +2379,7 @@ class SessionDB:
|
||||
codex_message_items: Any = None,
|
||||
platform_message_id: str = None,
|
||||
observed: bool = False,
|
||||
timestamp: Any = None,
|
||||
) -> int:
|
||||
"""
|
||||
Append a message to a session. Returns the message row ID.
|
||||
@@ -2410,6 +2411,16 @@ class SessionDB:
|
||||
# cannot bind list/dict parameters directly.
|
||||
stored_content = self._encode_content(content)
|
||||
|
||||
message_timestamp = time.time()
|
||||
if timestamp is not None:
|
||||
try:
|
||||
if hasattr(timestamp, "timestamp"):
|
||||
message_timestamp = float(timestamp.timestamp())
|
||||
else:
|
||||
message_timestamp = float(timestamp)
|
||||
except (TypeError, ValueError):
|
||||
logger.debug("Ignoring invalid explicit message timestamp: %r", timestamp)
|
||||
|
||||
# Pre-compute tool call count
|
||||
num_tool_calls = 0
|
||||
if tool_calls is not None:
|
||||
@@ -2429,7 +2440,7 @@ class SessionDB:
|
||||
tool_call_id,
|
||||
tool_calls_json,
|
||||
tool_name,
|
||||
time.time(),
|
||||
message_timestamp,
|
||||
token_count,
|
||||
finish_reason,
|
||||
reasoning,
|
||||
@@ -2482,6 +2493,16 @@ class SessionDB:
|
||||
for msg in messages:
|
||||
role = msg.get("role", "unknown")
|
||||
tool_calls = msg.get("tool_calls")
|
||||
message_timestamp = now_ts
|
||||
if msg.get("timestamp") is not None:
|
||||
try:
|
||||
ts_value = msg.get("timestamp")
|
||||
if hasattr(ts_value, "timestamp"):
|
||||
message_timestamp = float(ts_value.timestamp())
|
||||
else:
|
||||
message_timestamp = float(ts_value)
|
||||
except (TypeError, ValueError):
|
||||
logger.debug("Ignoring invalid explicit message timestamp: %r", msg.get("timestamp"))
|
||||
reasoning_details = msg.get("reasoning_details") if role == "assistant" else None
|
||||
codex_reasoning_items = (
|
||||
msg.get("codex_reasoning_items") if role == "assistant" else None
|
||||
@@ -2519,7 +2540,7 @@ class SessionDB:
|
||||
msg.get("tool_call_id"),
|
||||
tool_calls_json,
|
||||
msg.get("tool_name"),
|
||||
now_ts,
|
||||
message_timestamp,
|
||||
msg.get("token_count"),
|
||||
msg.get("finish_reason"),
|
||||
msg.get("reasoning") if role == "assistant" else None,
|
||||
@@ -2536,7 +2557,7 @@ class SessionDB:
|
||||
total_tool_calls += (
|
||||
len(tool_calls) if isinstance(tool_calls, list) else 1
|
||||
)
|
||||
now_ts += 1e-6
|
||||
now_ts = max(now_ts + 1e-6, message_timestamp + 1e-6)
|
||||
|
||||
conn.execute(
|
||||
"UPDATE sessions SET message_count = ?, tool_call_count = ? WHERE id = ?",
|
||||
@@ -2867,9 +2888,9 @@ class SessionDB:
|
||||
rows = self._conn.execute(
|
||||
"SELECT role, content, tool_call_id, tool_calls, tool_name, "
|
||||
"finish_reason, reasoning, reasoning_content, reasoning_details, "
|
||||
"codex_reasoning_items, codex_message_items, platform_message_id, observed "
|
||||
"codex_reasoning_items, codex_message_items, platform_message_id, observed, timestamp "
|
||||
f"FROM messages WHERE session_id IN ({placeholders})"
|
||||
f"{active_clause} ORDER BY id",
|
||||
f"{active_clause} ORDER BY timestamp, id",
|
||||
tuple(session_ids),
|
||||
).fetchall()
|
||||
|
||||
@@ -2879,6 +2900,8 @@ class SessionDB:
|
||||
if row["role"] in {"user", "assistant"} and isinstance(content, str):
|
||||
content = sanitize_context(content).strip()
|
||||
msg = {"role": row["role"], "content": content}
|
||||
if row["timestamp"]:
|
||||
msg["timestamp"] = row["timestamp"]
|
||||
if row["tool_call_id"]:
|
||||
msg["tool_call_id"] = row["tool_call_id"]
|
||||
if row["tool_name"]:
|
||||
|
||||
Reference in New Issue
Block a user