forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartifact.py
More file actions
88 lines (69 loc) · 2.28 KB
/
artifact.py
File metadata and controls
88 lines (69 loc) · 2.28 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
"""Utility functions for creating A2A Artifact objects."""
import uuid
from typing import Any
from a2a.types import Artifact, DataPart, Part, 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))