Skip to content

Commit 2d55630

Browse files
committed
fix comments
1 parent 3f1c842 commit 2d55630

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

src/a2a/helpers/proto_helpers.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ def new_artifact(
6464
parts: list[Part],
6565
name: str,
6666
description: str | None = None,
67+
artifact_id: str | None = None,
6768
) -> Artifact:
6869
"""Creates a new Artifact object."""
6970
return Artifact(
70-
artifact_id=str(uuid.uuid4()),
71+
artifact_id=artifact_id or str(uuid.uuid4()),
7172
parts=parts,
7273
name=name,
7374
description=description,
@@ -78,12 +79,14 @@ def new_text_artifact(
7879
name: str,
7980
text: str,
8081
description: str | None = None,
82+
artifact_id: str | None = None,
8183
) -> Artifact:
8284
"""Creates a new Artifact object containing only a single text Part."""
8385
return new_artifact(
8486
[Part(text=text)],
8587
name,
8688
description,
89+
artifact_id=artifact_id,
8790
)
8891

8992

@@ -97,6 +100,8 @@ def get_artifact_text(artifact: Artifact, delimiter: str = '\n') -> str:
97100

98101
def new_task_from_user_message(user_message: Message) -> Task:
99102
"""Creates a new Task object from an initial user message."""
103+
if user_message.role != Role.ROLE_USER:
104+
raise ValueError('Message must be from a user')
100105
if not user_message.parts:
101106
raise ValueError('Message parts cannot be empty')
102107
for part in user_message.parts:
@@ -173,13 +178,14 @@ def new_text_artifact_update_event( # noqa: PLR0913
173178
text: str,
174179
append: bool = False,
175180
last_chunk: bool = False,
181+
artifact_id: str | None = None,
176182
) -> TaskArtifactUpdateEvent:
177183
"""Creates a TaskArtifactUpdateEvent with a single text artifact."""
178184
return TaskArtifactUpdateEvent(
179185
task_id=task_id,
180186
context_id=context_id,
181-
artifact=Artifact(
182-
artifact_id=str(uuid.uuid4()), name=name, parts=[Part(text=text)]
187+
artifact=new_text_artifact(
188+
name=name, text=text, artifact_id=artifact_id
183189
),
184190
append=append,
185191
last_chunk=last_chunk,

tests/helpers/test_proto_helpers.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ def test_new_text_artifact() -> None:
7979
assert art.artifact_id != ''
8080

8181

82+
def test_new_text_artifact_with_id() -> None:
83+
art = new_text_artifact(name='test', text='content', description='desc', artifact_id='art1')
84+
assert art.name == 'test'
85+
assert art.description == 'desc'
86+
assert len(art.parts) == 1
87+
assert art.parts[0].text == 'content'
88+
assert art.artifact_id == 'art1'
89+
90+
8291
def test_get_artifact_text() -> None:
8392
art = Artifact(parts=[Part(text='hello'), Part(text='world')])
8493
assert get_artifact_text(art) == 'hello\nworld'
@@ -171,6 +180,21 @@ def test_new_text_artifact_update_event() -> None:
171180
assert event.last_chunk is True
172181

173182

183+
def test_new_text_artifact_update_event_with_id() -> None:
184+
event = new_text_artifact_update_event(
185+
task_id='task1',
186+
context_id='ctx1',
187+
name='test',
188+
text='content',
189+
artifact_id='art1',
190+
)
191+
assert event.task_id == 'task1'
192+
assert event.context_id == 'ctx1'
193+
assert event.artifact.name == 'test'
194+
assert event.artifact.parts[0].text == 'content'
195+
assert event.artifact.artifact_id == 'art1'
196+
197+
174198
def test_get_stream_response_text_message() -> None:
175199
resp = StreamResponse(message=Message(parts=[Part(text='hello')]))
176200
assert get_stream_response_text(resp) == 'hello'

0 commit comments

Comments
 (0)