forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation.py
More file actions
143 lines (122 loc) · 4.86 KB
/
Copy pathconversation.py
File metadata and controls
143 lines (122 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
Conversation models for the A2A protocol.
"""
import uuid
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Any
from .base import BaseModel
from .message import Message, MessageRole
from .content import (
TextContent, FunctionCallContent, FunctionResponseContent,
ErrorContent, FunctionParameter
)
@dataclass
class Conversation(BaseModel):
"""Represents an A2A conversation"""
conversation_id: str = field(default_factory=lambda: str(uuid.uuid4()))
messages: List[Message] = field(default_factory=list)
metadata: Optional[Dict[str, Any]] = None
def add_message(self, message: Message) -> Message:
"""
Add a message to the conversation
Args:
message: The message to add
Returns:
The added message
"""
# Set the conversation ID if not already set
if not message.conversation_id:
message.conversation_id = self.conversation_id
self.messages.append(message)
return message
def create_text_message(self, text: str, role: MessageRole,
parent_message_id: Optional[str] = None) -> Message:
"""
Create and add a text message to the conversation
Args:
text: The text content of the message
role: The role of the sender
parent_message_id: Optional ID of the parent message
Returns:
The created message
"""
content = TextContent(text=text)
message = Message(
content=content,
role=role,
conversation_id=self.conversation_id,
parent_message_id=parent_message_id
)
return self.add_message(message)
def create_function_call(self, name: str, parameters: List[Dict[str, Any]],
role: MessageRole = MessageRole.AGENT,
parent_message_id: Optional[str] = None) -> Message:
"""
Create and add a function call message to the conversation
Args:
name: The name of the function to call
parameters: List of parameter dictionaries with 'name' and 'value' keys
role: The role of the sender
parent_message_id: Optional ID of the parent message
Returns:
The created message
"""
function_params = [FunctionParameter(name=p["name"], value=p["value"]) for p in parameters]
content = FunctionCallContent(name=name, parameters=function_params)
message = Message(
content=content,
role=role,
conversation_id=self.conversation_id,
parent_message_id=parent_message_id
)
return self.add_message(message)
def create_function_response(self, name: str, response: Any,
role: MessageRole = MessageRole.AGENT,
parent_message_id: Optional[str] = None) -> Message:
"""
Create and add a function response message to the conversation
Args:
name: The name of the function that was called
response: The response data
role: The role of the sender
parent_message_id: Optional ID of the parent message
Returns:
The created message
"""
content = FunctionResponseContent(name=name, response=response)
message = Message(
content=content,
role=role,
conversation_id=self.conversation_id,
parent_message_id=parent_message_id
)
return self.add_message(message)
def create_error_message(self, error_message: str,
role: MessageRole = MessageRole.SYSTEM,
parent_message_id: Optional[str] = None) -> Message:
"""
Create and add an error message to the conversation
Args:
error_message: The error message text
role: The role of the sender
parent_message_id: Optional ID of the parent message
Returns:
The created message
"""
content = ErrorContent(message=error_message)
message = Message(
content=content,
role=role,
conversation_id=self.conversation_id,
parent_message_id=parent_message_id
)
return self.add_message(message)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'Conversation':
"""Create a Conversation from a dictionary"""
messages = [Message.from_dict(m) for m in data.get("messages", [])]
return cls(
conversation_id=data.get("conversation_id", str(uuid.uuid4())),
messages=messages,
metadata=data.get("metadata")
)