| 12345678910111213141516171819202122 |
- """HTTP SSE 编码;事件字段的唯一来源为 runtime.events。"""
- from __future__ import annotations
- import json
- from runtime.events import * # noqa: F401,F403
- from runtime.events import EventBuilder, ExecutionEvent
- def serialize_event(event: ExecutionEvent) -> str:
- return f"data: {json.dumps(event, ensure_ascii=False, default=str)}\n\n"
- class EventWriter(EventBuilder[str]):
- """兼容既有调用方;把中立事件编码成 SSE 帧。"""
- def __init__(self, session_id: str, trace_id: str = ""):
- super().__init__(session_id, trace_id, encode=serialize_event)
- def frame(self, event_type: str, **fields) -> str:
- return self.event(event_type, **fields)
- def comment(self, text: str) -> str:
- return f": {text}\n\n"
|