|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from datetime import datetime |
| 5 | +from enum import Enum |
| 6 | +from typing import Any, Dict, List, Optional, TYPE_CHECKING |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from ravendb.documents.operations.ai.agents.run_conversation_operation import AiUsage |
| 10 | + |
| 11 | + |
| 12 | +class AiMessageRole(Enum): |
| 13 | + """Role of a message in an AI conversation.""" |
| 14 | + |
| 15 | + SYSTEM = "System" |
| 16 | + USER = "User" |
| 17 | + ASSISTANT = "Assistant" |
| 18 | + SUMMARY = "Summary" |
| 19 | + INTERNAL = "Internal" |
| 20 | + |
| 21 | + |
| 22 | +def _parse_timestamp(ts_value: Any) -> Optional[datetime]: |
| 23 | + """Parse a RavenDB timestamp string into a datetime. |
| 24 | +
|
| 25 | + Handles Z suffix and 7-digit fractional seconds (truncated to 6 for Python 3.9). |
| 26 | + """ |
| 27 | + if not ts_value: |
| 28 | + return None |
| 29 | + if isinstance(ts_value, datetime): |
| 30 | + return ts_value |
| 31 | + if isinstance(ts_value, str): |
| 32 | + # Replace Z suffix with +00:00 for Python fromisoformat |
| 33 | + ts_str = ts_value.replace("Z", "+00:00") |
| 34 | + # Truncate fractional seconds to 6 digits (Python 3.9 limit) |
| 35 | + if "." in ts_str: |
| 36 | + dot_idx = ts_str.index(".") |
| 37 | + remaining = ts_str[dot_idx + 1 :] |
| 38 | + frac_end = len(remaining) |
| 39 | + for i, c in enumerate(remaining): |
| 40 | + if c in ("+", "-") and i > 0: |
| 41 | + frac_end = i |
| 42 | + break |
| 43 | + frac = remaining[:frac_end][:6] |
| 44 | + rest = remaining[frac_end:] |
| 45 | + ts_str = ts_str[: dot_idx + 1] + frac + rest |
| 46 | + try: |
| 47 | + return datetime.fromisoformat(ts_str) |
| 48 | + except (ValueError, AttributeError): |
| 49 | + return None |
| 50 | + return None |
| 51 | + |
| 52 | + |
| 53 | +@dataclass |
| 54 | +class AiToolCallResult: |
| 55 | + """ |
| 56 | + Represents a tool call that originated in an assistant message, with its |
| 57 | + eventual response (result) and optional sub-conversation ID merged in. |
| 58 | + """ |
| 59 | + |
| 60 | + id: Optional[str] = None |
| 61 | + """The tool call ID from the model.""" |
| 62 | + |
| 63 | + name: Optional[str] = None |
| 64 | + """Tool name.""" |
| 65 | + |
| 66 | + arguments: Optional[str] = None |
| 67 | + """Arguments the model passed, as JSON string.""" |
| 68 | + |
| 69 | + result: Optional[str] = None |
| 70 | + """The tool's response content. None if still pending (ActionRequired).""" |
| 71 | + |
| 72 | + sub_conversation_id: Optional[str] = None |
| 73 | + """If this tool call was a sub-agent invocation, the ID of the spawned sub-conversation.""" |
| 74 | + |
| 75 | + @classmethod |
| 76 | + def from_json(cls, json_dict: Dict[str, Any]) -> AiToolCallResult: |
| 77 | + return cls( |
| 78 | + id=json_dict.get("Id"), |
| 79 | + name=json_dict.get("Name"), |
| 80 | + arguments=json_dict.get("Arguments"), |
| 81 | + result=json_dict.get("Result"), |
| 82 | + sub_conversation_id=json_dict.get("SubConversationId"), |
| 83 | + ) |
| 84 | + |
| 85 | + def to_json(self) -> Dict[str, Any]: |
| 86 | + return { |
| 87 | + "Id": self.id, |
| 88 | + "Name": self.name, |
| 89 | + "Arguments": self.arguments, |
| 90 | + "Result": self.result, |
| 91 | + "SubConversationId": self.sub_conversation_id, |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | +@dataclass |
| 96 | +class AiConversationMessage: |
| 97 | + """ |
| 98 | + A single message in an AI agent conversation. |
| 99 | +
|
| 100 | + Attributes: |
| 101 | + role: The role of the message sender. |
| 102 | + content: Text content. When the stored message has multiple text parts, |
| 103 | + they are joined with line breaks. None for assistant messages that |
| 104 | + only initiated tool calls. |
| 105 | + attachments: Attachment file names associated with this message, if any. |
| 106 | + timestamp: When this message was recorded (UTC). Guaranteed unique and |
| 107 | + monotonic within a conversation — safe to use as a paging cursor. |
| 108 | + tool_calls: Tool calls initiated by this assistant message, with their |
| 109 | + responses inlined. |
| 110 | + usage: Token usage for this message (typically on assistant messages). |
| 111 | + sub_conversation_id: For Internal role messages: the ID of the |
| 112 | + sub-conversation this message relates to. |
| 113 | + """ |
| 114 | + |
| 115 | + role: Optional[AiMessageRole] = None |
| 116 | + content: Optional[str] = None |
| 117 | + attachments: Optional[List[str]] = None |
| 118 | + timestamp: Optional[datetime] = None |
| 119 | + tool_calls: Optional[List[AiToolCallResult]] = None |
| 120 | + usage: Optional[Any] = None # AiUsage when resolved |
| 121 | + sub_conversation_id: Optional[str] = None |
| 122 | + |
| 123 | + @classmethod |
| 124 | + def from_json(cls, json_dict: Dict[str, Any]) -> AiConversationMessage: |
| 125 | + from ravendb.documents.operations.ai.agents.run_conversation_operation import AiUsage |
| 126 | + |
| 127 | + role_str = json_dict.get("Role") |
| 128 | + if role_str is None: |
| 129 | + raise ValueError("Message is missing 'Role' field") |
| 130 | + try: |
| 131 | + role = AiMessageRole(role_str) |
| 132 | + except ValueError: |
| 133 | + raise ValueError(f"Unknown AiMessageRole: '{role_str}'") |
| 134 | + |
| 135 | + content = json_dict.get("Content") |
| 136 | + raw_attachments = json_dict.get("Attachments") |
| 137 | + attachments = raw_attachments if raw_attachments is not None else None |
| 138 | + timestamp = _parse_timestamp(json_dict.get("Timestamp")) |
| 139 | + |
| 140 | + tool_calls = None |
| 141 | + raw_tool_calls = json_dict.get("ToolCalls") |
| 142 | + if raw_tool_calls: |
| 143 | + tool_calls = [AiToolCallResult.from_json(tc) for tc in raw_tool_calls] |
| 144 | + |
| 145 | + usage = None |
| 146 | + raw_usage = json_dict.get("Usage") |
| 147 | + if raw_usage: |
| 148 | + usage = AiUsage.from_json(raw_usage) |
| 149 | + |
| 150 | + sub_conversation_id = json_dict.get("SubConversationId") |
| 151 | + |
| 152 | + return cls( |
| 153 | + role=role, |
| 154 | + content=content, |
| 155 | + attachments=attachments, |
| 156 | + timestamp=timestamp, |
| 157 | + tool_calls=tool_calls, |
| 158 | + usage=usage, |
| 159 | + sub_conversation_id=sub_conversation_id, |
| 160 | + ) |
| 161 | + |
| 162 | + def to_json(self) -> Dict[str, Any]: |
| 163 | + return { |
| 164 | + "Role": self.role.value if self.role else None, |
| 165 | + "Content": self.content, |
| 166 | + "Attachments": self.attachments if self.attachments is not None else None, |
| 167 | + "Timestamp": self.timestamp.isoformat() if self.timestamp else None, |
| 168 | + "ToolCalls": [tc.to_json() for tc in self.tool_calls] if self.tool_calls else None, |
| 169 | + "Usage": self.usage.to_json() if self.usage else None, |
| 170 | + "SubConversationId": self.sub_conversation_id, |
| 171 | + } |
| 172 | + |
| 173 | + def __repr__(self) -> str: |
| 174 | + return ( |
| 175 | + f"AiConversationMessage(role={self.role}, content={self.content!r}, " |
| 176 | + f"timestamp={self.timestamp}, tool_calls={len(self.tool_calls) if self.tool_calls else 0})" |
| 177 | + ) |
0 commit comments