add timestamps to messages

This commit is contained in:
knielsen404
2025-05-22 17:30:43 +02:00
parent 5b68e80be5
commit 6f70927c0b
+11 -1
View File
@@ -9,7 +9,7 @@ import select
import socket
import subprocess
import sys
from datetime import datetime
from invariant_sdk.async_client import AsyncClient
from invariant_sdk.types.append_messages import AppendMessagesRequest
from invariant_sdk.types.push_traces import PushTracesRequest
@@ -74,6 +74,11 @@ def deduplicate_annotations(ctx: McpContext, new_annotations: list) -> list:
return deduped_annotations
def generate_message_timestamp() -> str:
"""Generate a timestamp for a message."""
return datetime.now().isoformat()
def check_if_new_errors(ctx: McpContext, guardrails_result: dict) -> bool:
"""Checks if there are new errors in the guardrails result."""
annotations = create_annotations_from_guardrails_errors(
@@ -168,6 +173,10 @@ async def get_guardrails_check_result(
or (ctx.guardrails.logging_guardrails and action == GuardrailAction.LOG)
):
return {}
# Add timestamp to message if it doesn't already have one
if "timestamp" not in message:
message["timestamp"] = generate_message_timestamp()
# Prepare context and select appropriate guardrails
context = RequestContext.create(
@@ -471,6 +480,7 @@ async def process_line(
},
}
],
"timestamp": generate_message_timestamp(),
}
)
mcp_process.stdin.write(write_as_utf8_bytes(parsed_json))