-
Notifications
You must be signed in to change notification settings - Fork 422
Expand file tree
/
Copy pathartifact.py
More file actions
195 lines (158 loc) · 5.48 KB
/
artifact.py
File metadata and controls
195 lines (158 loc) · 5.48 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""Utility functions for creating A2A Artifact objects."""
import uuid
from typing import Any
from a2a.types import (
Artifact,
DataPart,
Part,
TaskArtifactUpdateEvent,
TextPart,
)
from a2a.utils.parts import get_text_parts
def new_artifact(
parts: list[Part],
name: str,
description: str | None = None,
) -> Artifact:
"""Creates a new Artifact object.
Args:
parts: The list of `Part` objects forming the artifact's content.
name: The human-readable name of the artifact.
description: An optional description of the artifact.
Returns:
A new `Artifact` object with a generated artifact_id.
"""
return Artifact(
artifact_id=str(uuid.uuid4()),
parts=parts,
name=name,
description=description,
)
def new_text_artifact(
name: str,
text: str,
description: str | None = None,
) -> Artifact:
"""Creates a new Artifact object containing only a single TextPart.
Args:
name: The human-readable name of the artifact.
text: The text content of the artifact.
description: An optional description of the artifact.
Returns:
A new `Artifact` object with a generated artifact_id.
"""
return new_artifact(
[Part(root=TextPart(text=text))],
name,
description,
)
def new_data_artifact(
name: str,
data: dict[str, Any],
description: str | None = None,
) -> Artifact:
"""Creates a new Artifact object containing only a single DataPart.
Args:
name: The human-readable name of the artifact.
data: The structured data content of the artifact.
description: An optional description of the artifact.
Returns:
A new `Artifact` object with a generated artifact_id.
"""
return new_artifact(
[Part(root=DataPart(data=data))],
name,
description,
)
def get_artifact_text(artifact: Artifact, delimiter: str = '\n') -> str:
"""Extracts and joins all text content from an Artifact's parts.
Args:
artifact: The `Artifact` object.
delimiter: The string to use when joining text from multiple TextParts.
Returns:
A single string containing all text content, or an empty string if no text parts are found.
"""
return delimiter.join(get_text_parts(artifact.parts))
class ArtifactStreamer:
"""A stateful helper for streaming artifact updates with a stable artifact ID.
Solves the problem where calling ``new_text_artifact`` in a loop generates
a fresh ``artifact_id`` each time, making ``append=True`` unusable.
Example::
streamer = ArtifactStreamer(context_id, task_id, name='response')
async for chunk in llm.stream(prompt):
await event_queue.enqueue_event(streamer.append(chunk))
await event_queue.enqueue_event(streamer.finalize())
Args:
context_id: The context ID associated with the task.
task_id: The ID of the task this artifact belongs to.
name: A human-readable name for the artifact.
description: An optional description of the artifact.
"""
def __init__(
self,
context_id: str,
task_id: str,
name: str,
description: str | None = None,
) -> None:
self._context_id = context_id
self._task_id = task_id
self._name = name
self._description = description
self._artifact_id = str(uuid.uuid4())
self._finalized = False
@property
def artifact_id(self) -> str:
"""The stable artifact ID used across all chunks."""
return self._artifact_id
def append(self, text: str) -> TaskArtifactUpdateEvent:
"""Create an append event for the next chunk of text.
Args:
text: The text content to append.
Returns:
A ``TaskArtifactUpdateEvent`` with ``append=True`` and
``last_chunk=False``.
Raises:
RuntimeError: If ``finalize()`` has already been called.
"""
if self._finalized:
raise RuntimeError(
'Cannot append after finalize() has been called.'
)
return TaskArtifactUpdateEvent(
context_id=self._context_id,
task_id=self._task_id,
append=True,
last_chunk=False,
artifact=Artifact(
artifact_id=self._artifact_id,
name=self._name,
description=self._description,
parts=[Part(root=TextPart(text=text))],
),
)
def finalize(self, text: str = '') -> TaskArtifactUpdateEvent:
"""Create the final chunk event, closing the stream.
Args:
text: Optional final text content. Defaults to empty string.
Returns:
A ``TaskArtifactUpdateEvent`` with ``append=True`` and
``last_chunk=True``.
Raises:
RuntimeError: If ``finalize()`` has already been called.
"""
if self._finalized:
raise RuntimeError('finalize() has already been called.')
self._finalized = True
return TaskArtifactUpdateEvent(
context_id=self._context_id,
task_id=self._task_id,
append=True,
last_chunk=True,
artifact=Artifact(
artifact_id=self._artifact_id,
name=self._name,
description=self._description,
parts=[Part(root=TextPart(text=text))],
),
)