| 26 | |
| 27 | @dataclass |
| 28 | class Message: |
| 29 | role: Literal['system', 'user', 'assistant', 'tool'] |
| 30 | |
| 31 | content: Union[str, List[Dict[str, str]]] = '' |
| 32 | |
| 33 | tool_calls: List[ToolCall] = field(default_factory=list) |
| 34 | |
| 35 | tool_call_id: Optional[str] = None |
| 36 | |
| 37 | # Also defined in OpenAI message |
| 38 | name: Optional[str] = None |
| 39 | |
| 40 | # needed for output |
| 41 | reasoning_content: str = '' |
| 42 | |
| 43 | # request id |
| 44 | id: str = '' |
| 45 | |
| 46 | # continue generation mode |
| 47 | partial: bool = False |
| 48 | prefix: bool = False |
| 49 | |
| 50 | # UI resources from mcp result |
| 51 | resources: List[Dict[str, str]] = field(default_factory=list) |
| 52 | |
| 53 | # usage |
| 54 | completion_tokens: int = 0 |
| 55 | prompt_tokens: int = 0 |
| 56 | |
| 57 | # tokens that hit existing cache (billed at reduced rate like 0.1x) |
| 58 | cached_tokens: int = 0 |
| 59 | # tokens used to create new cache (explicit cache only, billed at higher rate like 1.25x) |
| 60 | cache_creation_input_tokens: int = 0 |
| 61 | |
| 62 | api_calls: int = 1 |
| 63 | |
| 64 | # Knowledge search (sirchmunk) related fields |
| 65 | # searching_detail: Search process logs and metadata for frontend display |
| 66 | searching_detail: Dict[str, Any] = field(default_factory=dict) |
| 67 | # search_result: Raw search results to be used as context for next LLM turn |
| 68 | search_result: List[Dict[str, Any]] = field(default_factory=list) |
| 69 | |
| 70 | def to_dict(self): |
| 71 | return asdict(self) |
| 72 | |
| 73 | def to_dict_clean(self): |
| 74 | raw_dict = asdict(self) |
| 75 | if raw_dict.get('tool_calls'): |
| 76 | for idx, tool_call in enumerate(raw_dict['tool_calls']): |
| 77 | try: |
| 78 | if tool_call['arguments']: |
| 79 | json.loads(tool_call['arguments']) |
| 80 | except Exception: |
| 81 | tool_call['arguments'] = '{}' |
| 82 | raw_dict['tool_calls'][idx] = { |
| 83 | 'id': tool_call['id'], |
| 84 | 'type': tool_call['type'], |
| 85 | 'function': { |
no outgoing calls