Skip to content

Commit 2416e9c

Browse files
committed
use model_dump()
2 parents 1e2fe73 + 811b6b5 commit 2416e9c

File tree

1 file changed

+16
-73
lines changed
  • livekit-agents/livekit/agents/telemetry

1 file changed

+16
-73
lines changed

livekit-agents/livekit/agents/telemetry/traces.py

Lines changed: 16 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
3434
from opentelemetry.exporter.otlp.proto.http import Compression
3535

36-
from google.protobuf.json_format import MessageToJson
36+
from google.protobuf.json_format import MessageToJson, MessageToDict
3737

3838
from ..utils import misc
3939
from ..log import logger
@@ -228,57 +228,18 @@ def _to_proto_chat_item(item: ChatItem) -> agent_pb.agent_session.ChatContext.Ch
228228
return item_pb
229229

230230

231-
def _to_log_chat_item(item: ChatItem) -> map[str, any] | None:
232-
if item.type == "message":
233-
return {
234-
"type": item.type,
235-
"id": item.id,
236-
"role": item.role,
237-
"content": [content for content in item.content if isinstance(content, str)],
238-
"interrupted": item.interrupted,
239-
"transcript_confidence": item.transcript_confidence,
240-
"extra": item.extra,
241-
"metrics": item.metrics,
242-
}
243-
244-
elif item.type == "function_call":
245-
try:
246-
arguments = json.loads(item.arguments)
247-
except:
248-
arguments = item.arguments
249-
250-
return {
251-
"type": item.type,
252-
"id": item.id,
253-
"call_id": item.call_id,
254-
"arguments": arguments,
255-
"name": item.name,
256-
}
231+
def _to_log_chat_item(item: ChatItem) -> dict[str, Any]:
232+
item_dict = item.model_dump()
257233

258-
elif item.type == "function_call_output":
259-
try:
260-
output = json.loads(item.output)
261-
except:
262-
output = item.output
263-
264-
return {
265-
"type": item.type,
266-
"id": item.id,
267-
"name": item.name,
268-
"call_id": item.call_id,
269-
"output": output,
270-
"is_error": item.is_error,
271-
}
272-
273-
elif item.type == "agent_handoff":
274-
return {
275-
"type": item.type,
276-
"id": item.id,
277-
"old_agent_id": item.old_agent_id,
278-
"new_agent_id": item.new_agent_id,
279-
}
234+
try:
235+
if item.type == "function_call":
236+
item["arguments"] = json.loads(item["arguments"])
237+
elif item.type == "function_call_output":
238+
item["output"] = json.loads(item["output"])
239+
except Exception:
240+
pass
280241

281-
return None
242+
return item_dict
282243

283244

284245
def _to_rfc3339(value: int | float | datetime) -> str:
@@ -301,9 +262,8 @@ async def _upload_session_report(
301262
report: SessionReport,
302263
http_session: aiohttp.ClientSession,
303264
) -> None:
304-
# emit logs
305265
chat_logger = get_logger_provider().get_logger(
306-
name="chat_history",
266+
name="chat history",
307267
attributes={
308268
"room_id": report.room_id,
309269
"job_id": report.job_id,
@@ -313,12 +273,8 @@ async def _upload_session_report(
313273
)
314274
chat_logger.emit(
315275
LogRecord(
316-
timestamp=int(report.audio_recording_started_at * 1e9),
317-
body="chat started",
318-
attributes={
319-
"chat.options": dict(report.options),
320-
"chat.report_timestamp": report.timestamp,
321-
},
276+
body="session report",
277+
attributes={"report.timestamp": report.timestamp},
322278
)
323279
)
324280
for item in report.chat_history.items:
@@ -328,9 +284,7 @@ async def _upload_session_report(
328284
LogRecord(
329285
timestamp=int(item.created_at * 1e9),
330286
body="chat item",
331-
attributes={
332-
"chat.item": item_log,
333-
},
287+
attributes={"chat.item": item_log},
334288
)
335289
)
336290

@@ -343,8 +297,7 @@ async def _upload_session_report(
343297
jwt = access_token.to_jwt()
344298

345299
header_msg = proto_metrics.MetricsRecordingHeader(
346-
room_id=room_id,
347-
enable_user_data_training=report.enable_user_data_training
300+
room_id=room_id, enable_user_data_training=report.enable_user_data_training
348301
)
349302
header_bytes = header_msg.SerializeToString()
350303

@@ -355,16 +308,6 @@ async def _upload_session_report(
355308
part.headers["Content-Type"] = "application/protobuf"
356309
part.headers["Content-Length"] = str(len(header_bytes))
357310

358-
# if chat_history_bytes:
359-
# part = mp.append(chat_history_bytes)
360-
# part.set_content_disposition(
361-
# "form-data", name="chat_history", filename="chat_history.binpb"
362-
# )
363-
# part.headers["Content-Type"] = "application/protobuf"
364-
# part.headers["Content-Length"] = str(len(chat_history_bytes))
365-
366-
# chat_history_pb = _to_proto_chat_ctx(report.chat_history)
367-
368311
if report.audio_recording_path and report.audio_recording_started_at:
369312
try:
370313
async with aiofiles.open(report.audio_recording_path, "rb") as f:

0 commit comments

Comments
 (0)