feat: add frozen Agent/Message/Thread types (id-keyed)
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
from __future__ import annotations
|
||||
import uuid
|
||||
import datetime
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Agent:
|
||||
id: uuid.UUID
|
||||
name: str
|
||||
def __hash__(self): return hash(self.id)
|
||||
def __eq__(self, other): return isinstance(other, Agent) and self.id == other.id
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Message:
|
||||
id: uuid.UUID
|
||||
sender: Agent
|
||||
content: str
|
||||
seq: int
|
||||
ts: datetime.datetime
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Thread:
|
||||
id: uuid.UUID
|
||||
participants: frozenset[Agent]
|
||||
created_at: datetime.datetime
|
||||
@@ -0,0 +1,24 @@
|
||||
import uuid
|
||||
from agentmsgs.core.types import Agent, Thread, Message
|
||||
import datetime
|
||||
|
||||
def test_agent_identity_on_id_not_name():
|
||||
a1 = Agent(id=uuid.uuid4(), name="Alice")
|
||||
a2 = Agent(id=uuid.uuid4(), name="Alice")
|
||||
assert a1 != a2 # different id => not equal even though name same
|
||||
assert hash(a1) != hash(a2)
|
||||
|
||||
def test_agent_hash_on_id():
|
||||
uid = uuid.uuid4()
|
||||
a1 = Agent(id=uid, name="Alice")
|
||||
a2 = Agent(id=uid, name="Bob") # same id, different name => equal per design (id is identity)
|
||||
assert a1 == a2
|
||||
assert hash(a1) == hash(a2)
|
||||
|
||||
def test_thread_multiple_with_same_participants_allowed():
|
||||
a = Agent(id=uuid.uuid4(), name="A")
|
||||
b = Agent(id=uuid.uuid4(), name="B")
|
||||
t1 = Thread(id=uuid.uuid4(), participants=frozenset({a,b}), created_at=datetime.datetime.now(datetime.timezone.utc))
|
||||
t2 = Thread(id=uuid.uuid4(), participants=frozenset({a,b}), created_at=datetime.datetime.now(datetime.timezone.utc))
|
||||
assert t1 != t2
|
||||
assert t1.participants == t2.participants
|
||||
Reference in New Issue
Block a user