codecraftai-agent-kit 1.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ccai/agentkit/__init__.py +304 -0
- ccai/agentkit/addressresolver.py +177 -0
- ccai/agentkit/agent.py +2945 -0
- ccai/agentkit/agentaddress.py +187 -0
- ccai/agentkit/agentconfiguration.py +581 -0
- ccai/agentkit/agentidentity.py +152 -0
- ccai/agentkit/agentregistry.py +221 -0
- ccai/agentkit/agentstatemachine.py +134 -0
- ccai/agentkit/alternateproviderstrategy.py +36 -0
- ccai/agentkit/approvalgate.py +238 -0
- ccai/agentkit/approvalrequest.py +250 -0
- ccai/agentkit/asyncagent.py +187 -0
- ccai/agentkit/breakpoint.py +150 -0
- ccai/agentkit/builtinagentmetrics.py +114 -0
- ccai/agentkit/builtins/__init__.py +11 -0
- ccai/agentkit/builtins/filereadtool.py +90 -0
- ccai/agentkit/builtins/filewritetool.py +93 -0
- ccai/agentkit/builtins/httprequesttool.py +169 -0
- ccai/agentkit/builtins/shellcommandtool.py +190 -0
- ccai/agentkit/cachedresultstrategy.py +43 -0
- ccai/agentkit/checkpointmanager.py +219 -0
- ccai/agentkit/circuitbreaker.py +230 -0
- ccai/agentkit/circuitbreakerconfiguration.py +158 -0
- ccai/agentkit/circuitbreakerevent.py +158 -0
- ccai/agentkit/circuitbreakerhealthcheck.py +32 -0
- ccai/agentkit/circuitbreakerregistry.py +116 -0
- ccai/agentkit/circuitbreakerstate.py +44 -0
- ccai/agentkit/clearcacheaction.py +46 -0
- ccai/agentkit/concurrencybridge.py +143 -0
- ccai/agentkit/concurrencymodel.py +19 -0
- ccai/agentkit/conditionevaluator.py +594 -0
- ccai/agentkit/configurationchangeevent.py +179 -0
- ccai/agentkit/configurationerrors.py +152 -0
- ccai/agentkit/configurationlayer.py +160 -0
- ccai/agentkit/configurationlayersource.py +46 -0
- ccai/agentkit/configurationmanager.py +680 -0
- ccai/agentkit/configurationschema.py +224 -0
- ccai/agentkit/configurationschemafield.py +217 -0
- ccai/agentkit/configurationserializer.py +163 -0
- ccai/agentkit/configurationvalidationresult.py +118 -0
- ccai/agentkit/contextsummarizer.py +170 -0
- ccai/agentkit/contextwindow.py +264 -0
- ccai/agentkit/contextwindowstrategy.py +57 -0
- ccai/agentkit/correlationcontext.py +156 -0
- ccai/agentkit/costestimate.py +139 -0
- ccai/agentkit/costtracker.py +121 -0
- ccai/agentkit/deadletterentry.py +134 -0
- ccai/agentkit/deadletterquery.py +119 -0
- ccai/agentkit/deadletterqueue.py +123 -0
- ccai/agentkit/deadletterstore.py +86 -0
- ccai/agentkit/debuginspector.py +211 -0
- ccai/agentkit/debugsession.py +223 -0
- ccai/agentkit/defaultvaluestrategy.py +43 -0
- ccai/agentkit/degradationchangeevent.py +120 -0
- ccai/agentkit/degradationlevel.py +44 -0
- ccai/agentkit/degradationmanager.py +212 -0
- ccai/agentkit/degradationpolicy.py +130 -0
- ccai/agentkit/degradationstrategy.py +66 -0
- ccai/agentkit/embeddingprovider.py +106 -0
- ccai/agentkit/errors.py +95 -0
- ccai/agentkit/executionrecord.py +213 -0
- ccai/agentkit/executionrecorder.py +180 -0
- ccai/agentkit/executionrecording.py +230 -0
- ccai/agentkit/executionreplayer.py +187 -0
- ccai/agentkit/expirationpolicy.py +116 -0
- ccai/agentkit/fallbackchain.py +82 -0
- ccai/agentkit/featureflag.py +154 -0
- ccai/agentkit/featureflagevaluator.py +109 -0
- ccai/agentkit/featureflagregistry.py +258 -0
- ccai/agentkit/filedeadletterstore.py +112 -0
- ccai/agentkit/filememorystore.py +291 -0
- ccai/agentkit/healthcheck.py +46 -0
- ccai/agentkit/healthcheckregistry.py +51 -0
- ccai/agentkit/healthmonitor.py +258 -0
- ccai/agentkit/healthstatus.py +105 -0
- ccai/agentkit/hotreloadmonitor.py +214 -0
- ccai/agentkit/hotreloadpolicy.py +160 -0
- ccai/agentkit/inmemorydeadletterstore.py +61 -0
- ccai/agentkit/inmemorystore.py +173 -0
- ccai/agentkit/jsonmessageserializer.py +89 -0
- ccai/agentkit/jsonoutputparser.py +50 -0
- ccai/agentkit/lifecyclestate.py +22 -0
- ccai/agentkit/llmconversation.py +153 -0
- ccai/agentkit/llmerrors.py +193 -0
- ccai/agentkit/llmmessage.py +158 -0
- ccai/agentkit/llmprovider.py +158 -0
- ccai/agentkit/llmproviderregistry.py +200 -0
- ccai/agentkit/llmresponse.py +190 -0
- ccai/agentkit/llmstreamchunk.py +65 -0
- ccai/agentkit/llmstreamhandler.py +48 -0
- ccai/agentkit/llmtoolbridge.py +154 -0
- ccai/agentkit/logexporter.py +316 -0
- ccai/agentkit/logformatter.py +169 -0
- ccai/agentkit/memoryconfiguration.py +243 -0
- ccai/agentkit/memoryentry.py +254 -0
- ccai/agentkit/memoryerrors.py +135 -0
- ccai/agentkit/memoryindex.py +213 -0
- ccai/agentkit/memoryquery.py +248 -0
- ccai/agentkit/memoryqueryresult.py +86 -0
- ccai/agentkit/memoryscope.py +54 -0
- ccai/agentkit/memorystore.py +260 -0
- ccai/agentkit/memorystorehealthcheck.py +33 -0
- ccai/agentkit/memorystoreregistry.py +106 -0
- ccai/agentkit/message.py +310 -0
- ccai/agentkit/messageenvelope.py +144 -0
- ccai/agentkit/messagepattern.py +50 -0
- ccai/agentkit/messagerouter.py +290 -0
- ccai/agentkit/messageserializer.py +106 -0
- ccai/agentkit/messageserializerregistry.py +163 -0
- ccai/agentkit/messagingconfiguration.py +397 -0
- ccai/agentkit/messagingerrors.py +338 -0
- ccai/agentkit/metricdefinition.py +164 -0
- ccai/agentkit/metricmeasurement.py +178 -0
- ccai/agentkit/metricscollector.py +258 -0
- ccai/agentkit/metricsexporter.py +303 -0
- ccai/agentkit/metricsregistry.py +130 -0
- ccai/agentkit/metricssnapshot.py +155 -0
- ccai/agentkit/metrictype.py +51 -0
- ccai/agentkit/msgpackmessageserializer.py +97 -0
- ccai/agentkit/observabilityconfiguration.py +546 -0
- ccai/agentkit/observabilityerror.py +67 -0
- ccai/agentkit/observabilitypipeline.py +252 -0
- ccai/agentkit/outputparser.py +82 -0
- ccai/agentkit/outputschema.py +142 -0
- ccai/agentkit/parallelbranch.py +87 -0
- ccai/agentkit/platform/__init__.py +43 -0
- ccai/agentkit/platform/adapters/__init__.py +18 -0
- ccai/agentkit/platform/adapters/awsplatformadapter.py +1272 -0
- ccai/agentkit/platform/adapters/azureplatformadapter.py +1227 -0
- ccai/agentkit/platform/adapters/gcpplatformadapter.py +1257 -0
- ccai/agentkit/platform/adapters/localplatformadapter.py +956 -0
- ccai/agentkit/platform/adapters/platformadapter.py +139 -0
- ccai/agentkit/platform/deployment/__init__.py +15 -0
- ccai/agentkit/platform/deployment/deploymentdescriptor.py +234 -0
- ccai/agentkit/platform/deployment/deploymentdescriptorparser.py +178 -0
- ccai/agentkit/platform/deployment/deploymentdescriptorprinter.py +97 -0
- ccai/agentkit/platform/errors.py +224 -0
- ccai/agentkit/platform/platformconfiguration.py +412 -0
- ccai/agentkit/platform/platformdetector.py +125 -0
- ccai/agentkit/platform/platformruntime.py +331 -0
- ccai/agentkit/platform/platformserviceregistry.py +149 -0
- ccai/agentkit/platform/platformtype.py +60 -0
- ccai/agentkit/platform/services/__init__.py +19 -0
- ccai/agentkit/platform/services/computeservice.py +112 -0
- ccai/agentkit/platform/services/invocationresult.py +173 -0
- ccai/agentkit/platform/services/messagingservice.py +135 -0
- ccai/agentkit/platform/services/platformservice.py +111 -0
- ccai/agentkit/platform/services/queuemessage.py +193 -0
- ccai/agentkit/platform/services/storageobject.py +195 -0
- ccai/agentkit/platform/services/storageservice.py +159 -0
- ccai/agentkit/pluginconfiguration.py +241 -0
- ccai/agentkit/plugindependency.py +129 -0
- ccai/agentkit/plugindescriptor.py +225 -0
- ccai/agentkit/pluginlifecyclehook.py +41 -0
- ccai/agentkit/pluginmanager.py +360 -0
- ccai/agentkit/pluginstatus.py +47 -0
- ccai/agentkit/pooledagent.py +116 -0
- ccai/agentkit/prompttemplate.py +221 -0
- ccai/agentkit/prompttemplateregistry.py +145 -0
- ccai/agentkit/protobufmessageserializer.py +144 -0
- ccai/agentkit/pubsubbroker.py +311 -0
- ccai/agentkit/py.typed +0 -0
- ccai/agentkit/reconnecttransportaction.py +47 -0
- ccai/agentkit/regexoutputparser.py +68 -0
- ccai/agentkit/requestresponsehandler.py +274 -0
- ccai/agentkit/resetcircuitbreakeraction.py +48 -0
- ccai/agentkit/resilienceconfiguration.py +191 -0
- ccai/agentkit/resilienceerrors.py +103 -0
- ccai/agentkit/resiliencepipeline.py +201 -0
- ccai/agentkit/retryattempt.py +158 -0
- ccai/agentkit/retryexecutor.py +157 -0
- ccai/agentkit/retrypolicy.py +195 -0
- ccai/agentkit/retryresult.py +186 -0
- ccai/agentkit/selfhealingaction.py +42 -0
- ccai/agentkit/selfhealingpolicy.py +67 -0
- ccai/agentkit/selfhealingresult.py +130 -0
- ccai/agentkit/stepexecutionstatus.py +52 -0
- ccai/agentkit/stepresult.py +198 -0
- ccai/agentkit/steps/__init__.py +9 -0
- ccai/agentkit/steps/agentstep.py +247 -0
- ccai/agentkit/steps/approvalstep.py +276 -0
- ccai/agentkit/steps/llmstep.py +403 -0
- ccai/agentkit/steps/subworkflowstep.py +270 -0
- ccai/agentkit/steps/toolstep.py +265 -0
- ccai/agentkit/structuredlogentry.py +222 -0
- ccai/agentkit/structuredlogger.py +189 -0
- ccai/agentkit/subscription.py +141 -0
- ccai/agentkit/threadedagent.py +94 -0
- ccai/agentkit/tokenusage.py +136 -0
- ccai/agentkit/tool.py +203 -0
- ccai/agentkit/toolchain.py +268 -0
- ccai/agentkit/toolchainexecutor.py +134 -0
- ccai/agentkit/toolchainresult.py +166 -0
- ccai/agentkit/tooldescriptor.py +203 -0
- ccai/agentkit/toolerrors.py +160 -0
- ccai/agentkit/toolexecutor.py +168 -0
- ccai/agentkit/toolfactory.py +60 -0
- ccai/agentkit/toolregistry.py +184 -0
- ccai/agentkit/toolresult.py +142 -0
- ccai/agentkit/toolschema.py +197 -0
- ccai/agentkit/tracecontext.py +294 -0
- ccai/agentkit/traceevent.py +166 -0
- ccai/agentkit/traceexporter.py +305 -0
- ccai/agentkit/tracer.py +234 -0
- ccai/agentkit/tracespan.py +349 -0
- ccai/agentkit/transitionrecord.py +84 -0
- ccai/agentkit/transport.py +334 -0
- ccai/agentkit/transportconfiguration.py +436 -0
- ccai/agentkit/transporthealthcheck.py +36 -0
- ccai/agentkit/transportregistry.py +224 -0
- ccai/agentkit/transports/__init__.py +28 -0
- ccai/agentkit/transports/grpctransport.py +410 -0
- ccai/agentkit/transports/httptransport.py +423 -0
- ccai/agentkit/transports/messagequeuetransport.py +489 -0
- ccai/agentkit/transports/websockettransport.py +473 -0
- ccai/agentkit/workflowcheckpoint.py +208 -0
- ccai/agentkit/workflowconfiguration.py +295 -0
- ccai/agentkit/workflowcontext.py +249 -0
- ccai/agentkit/workflowdefinition.py +790 -0
- ccai/agentkit/workflowedge.py +128 -0
- ccai/agentkit/workflowerrors.py +295 -0
- ccai/agentkit/workflowexecutionstatus.py +48 -0
- ccai/agentkit/workflowexecutor.py +1121 -0
- ccai/agentkit/workflowregistry.py +185 -0
- ccai/agentkit/workflowresult.py +250 -0
- ccai/agentkit/workflowstep.py +195 -0
- codecraftai_agent_kit-1.3.0.dist-info/METADATA +99 -0
- codecraftai_agent_kit-1.3.0.dist-info/RECORD +230 -0
- codecraftai_agent_kit-1.3.0.dist-info/WHEEL +4 -0
- codecraftai_agent_kit-1.3.0.dist-info/licenses/LICENSE.md +204 -0
ccai/agentkit/message.py
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the Message class representing an immutable unit of communication
|
|
3
|
+
between agents in the Communication & Messaging feature.
|
|
4
|
+
|
|
5
|
+
A Message contains a unique identifier, sender and recipient AgentAddress
|
|
6
|
+
instances, a payload, a MessagePattern, optional headers, an optional
|
|
7
|
+
correlation identifier for request/response tracking, a UTC timestamp,
|
|
8
|
+
and an optional serialization format name that selects the
|
|
9
|
+
MessageSerializer a Transport uses to put the message on the wire.
|
|
10
|
+
It supports serialization to dictionary and JSON formats, and
|
|
11
|
+
deserialization from dictionary and JSON.
|
|
12
|
+
|
|
13
|
+
.. module:: ccai.agentkit.message
|
|
14
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
15
|
+
:synopsis: Immutable message object for agent-to-agent communication.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import json
|
|
19
|
+
import uuid
|
|
20
|
+
|
|
21
|
+
from datetime import datetime
|
|
22
|
+
from datetime import timezone
|
|
23
|
+
from typing import Dict
|
|
24
|
+
from typing import Optional
|
|
25
|
+
|
|
26
|
+
from ccai.agentkit.agentaddress import AgentAddress
|
|
27
|
+
from ccai.agentkit.messagepattern import MessagePattern
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
PAYLOAD_PREVIEW_MAX_LENGTH = 50
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Message:
|
|
34
|
+
"""
|
|
35
|
+
Immutable object representing a unit of communication between agents.
|
|
36
|
+
|
|
37
|
+
Contains a unique identifier, sender AgentAddress, recipient AgentAddress,
|
|
38
|
+
payload (dictionary, list, string, or bytes), headers dictionary, optional
|
|
39
|
+
correlation identifier for request/response tracking, a MessagePattern
|
|
40
|
+
value, a UTC timestamp, and an optional serialization format name.
|
|
41
|
+
|
|
42
|
+
When created without an explicit identifier, a UUID-based identifier is
|
|
43
|
+
generated automatically. When created without an explicit timestamp, the
|
|
44
|
+
current UTC time is used.
|
|
45
|
+
|
|
46
|
+
The serialization format name (e.g. 'json', 'msgpack') selects the
|
|
47
|
+
MessageSerializer that a Transport uses to encode the message. The Agent
|
|
48
|
+
sets it from the serialization_format passed to send_message or request,
|
|
49
|
+
or from the MessagingConfiguration default. When it is None, the
|
|
50
|
+
Transport uses its own default format.
|
|
51
|
+
|
|
52
|
+
:param sender: The AgentAddress of the sending agent.
|
|
53
|
+
:param recipient: The AgentAddress of the receiving agent.
|
|
54
|
+
:param payload: The message payload as a Python object (dictionary, list,
|
|
55
|
+
string, or bytes).
|
|
56
|
+
:param pattern: The MessagePattern value indicating the communication pattern.
|
|
57
|
+
:param headers: Optional dictionary of message headers.
|
|
58
|
+
:param correlation_id: Optional correlation identifier for request/response
|
|
59
|
+
tracking.
|
|
60
|
+
:param identifier: Optional unique identifier. If not provided, a UUID is
|
|
61
|
+
generated.
|
|
62
|
+
:param timestamp: Optional UTC timestamp. If not provided, the current UTC
|
|
63
|
+
time is used.
|
|
64
|
+
:param serialization_format: Optional serialization format name that
|
|
65
|
+
selects the MessageSerializer used by the Transport. If not provided,
|
|
66
|
+
the Transport default is used.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
def __init__(self, sender: AgentAddress, recipient: AgentAddress,
|
|
70
|
+
payload: object, pattern: MessagePattern,
|
|
71
|
+
headers: Optional[Dict] = None,
|
|
72
|
+
correlation_id: Optional[str] = None,
|
|
73
|
+
identifier: Optional[str] = None,
|
|
74
|
+
timestamp: Optional[datetime] = None,
|
|
75
|
+
serialization_format: Optional[str] = None) -> None:
|
|
76
|
+
|
|
77
|
+
if identifier is not None:
|
|
78
|
+
self._identifier = identifier
|
|
79
|
+
else:
|
|
80
|
+
self._identifier = str(uuid.uuid4())
|
|
81
|
+
|
|
82
|
+
self._sender = sender
|
|
83
|
+
self._recipient = recipient
|
|
84
|
+
self._payload = payload
|
|
85
|
+
self._pattern = pattern
|
|
86
|
+
|
|
87
|
+
if headers is not None:
|
|
88
|
+
self._headers = dict(headers)
|
|
89
|
+
else:
|
|
90
|
+
self._headers = {}
|
|
91
|
+
|
|
92
|
+
self._correlation_id = correlation_id
|
|
93
|
+
|
|
94
|
+
if timestamp is not None:
|
|
95
|
+
self._timestamp = timestamp
|
|
96
|
+
else:
|
|
97
|
+
self._timestamp = datetime.now(timezone.utc)
|
|
98
|
+
|
|
99
|
+
self._serialization_format = serialization_format
|
|
100
|
+
|
|
101
|
+
return
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def identifier(self) -> str:
|
|
105
|
+
"""
|
|
106
|
+
The unique identifier of the message.
|
|
107
|
+
|
|
108
|
+
:returns: The identifier string.
|
|
109
|
+
"""
|
|
110
|
+
return self._identifier
|
|
111
|
+
|
|
112
|
+
@property
|
|
113
|
+
def sender(self) -> AgentAddress:
|
|
114
|
+
"""
|
|
115
|
+
The AgentAddress of the sending agent.
|
|
116
|
+
|
|
117
|
+
:returns: The sender AgentAddress.
|
|
118
|
+
"""
|
|
119
|
+
return self._sender
|
|
120
|
+
|
|
121
|
+
@property
|
|
122
|
+
def recipient(self) -> AgentAddress:
|
|
123
|
+
"""
|
|
124
|
+
The AgentAddress of the receiving agent.
|
|
125
|
+
|
|
126
|
+
:returns: The recipient AgentAddress.
|
|
127
|
+
"""
|
|
128
|
+
return self._recipient
|
|
129
|
+
|
|
130
|
+
@property
|
|
131
|
+
def payload(self) -> object:
|
|
132
|
+
"""
|
|
133
|
+
The message payload.
|
|
134
|
+
|
|
135
|
+
:returns: The payload as a Python object.
|
|
136
|
+
"""
|
|
137
|
+
return self._payload
|
|
138
|
+
|
|
139
|
+
@property
|
|
140
|
+
def pattern(self) -> MessagePattern:
|
|
141
|
+
"""
|
|
142
|
+
The communication pattern of the message.
|
|
143
|
+
|
|
144
|
+
:returns: The MessagePattern value.
|
|
145
|
+
"""
|
|
146
|
+
return self._pattern
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def headers(self) -> Dict:
|
|
150
|
+
"""
|
|
151
|
+
A defensive copy of the message headers dictionary.
|
|
152
|
+
|
|
153
|
+
:returns: A dictionary copy of the headers.
|
|
154
|
+
"""
|
|
155
|
+
return dict(self._headers)
|
|
156
|
+
|
|
157
|
+
@property
|
|
158
|
+
def correlation_id(self) -> Optional[str]:
|
|
159
|
+
"""
|
|
160
|
+
The optional correlation identifier for request/response tracking.
|
|
161
|
+
|
|
162
|
+
:returns: The correlation identifier string or None.
|
|
163
|
+
"""
|
|
164
|
+
return self._correlation_id
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def timestamp(self) -> datetime:
|
|
168
|
+
"""
|
|
169
|
+
The UTC timestamp of the message.
|
|
170
|
+
|
|
171
|
+
:returns: The timestamp as a datetime object.
|
|
172
|
+
"""
|
|
173
|
+
return self._timestamp
|
|
174
|
+
|
|
175
|
+
@property
|
|
176
|
+
def serialization_format(self) -> Optional[str]:
|
|
177
|
+
"""
|
|
178
|
+
The optional serialization format name that selects the
|
|
179
|
+
MessageSerializer a Transport uses to encode the message.
|
|
180
|
+
|
|
181
|
+
:returns: The serialization format name string or None when the
|
|
182
|
+
Transport default should be used.
|
|
183
|
+
"""
|
|
184
|
+
return self._serialization_format
|
|
185
|
+
|
|
186
|
+
def as_dict(self) -> dict:
|
|
187
|
+
"""
|
|
188
|
+
Returns a dictionary representation of the message fields.
|
|
189
|
+
|
|
190
|
+
Converts sender and recipient to dictionaries via their as_dict
|
|
191
|
+
methods, converts the pattern to its string value, and converts
|
|
192
|
+
the timestamp to ISO format.
|
|
193
|
+
|
|
194
|
+
:returns: A dictionary with all message fields.
|
|
195
|
+
"""
|
|
196
|
+
result = {
|
|
197
|
+
"identifier": self._identifier,
|
|
198
|
+
"sender": self._sender.as_dict(),
|
|
199
|
+
"recipient": self._recipient.as_dict(),
|
|
200
|
+
"payload": self._payload,
|
|
201
|
+
"pattern": self._pattern.value,
|
|
202
|
+
"headers": dict(self._headers),
|
|
203
|
+
"correlation_id": self._correlation_id,
|
|
204
|
+
"timestamp": self._timestamp.isoformat(),
|
|
205
|
+
"serialization_format": self._serialization_format
|
|
206
|
+
}
|
|
207
|
+
return result
|
|
208
|
+
|
|
209
|
+
def to_json(self) -> str:
|
|
210
|
+
"""
|
|
211
|
+
Returns a JSON string representation of the message fields.
|
|
212
|
+
|
|
213
|
+
:returns: A JSON string containing all message fields.
|
|
214
|
+
"""
|
|
215
|
+
data = self.as_dict()
|
|
216
|
+
json_str = json.dumps(data)
|
|
217
|
+
return json_str
|
|
218
|
+
|
|
219
|
+
@classmethod
|
|
220
|
+
def from_dict(cls, data: dict) -> "Message":
|
|
221
|
+
"""
|
|
222
|
+
Constructs a Message from a dictionary.
|
|
223
|
+
|
|
224
|
+
Reconstructs AgentAddress instances from nested dictionaries,
|
|
225
|
+
the MessagePattern from its string value, and the timestamp
|
|
226
|
+
from an ISO format string. The serialization format is
|
|
227
|
+
optional so dictionaries written before the field existed
|
|
228
|
+
can still be read.
|
|
229
|
+
|
|
230
|
+
:param data: A dictionary containing message fields.
|
|
231
|
+
|
|
232
|
+
:returns: A Message instance reconstructed from the dictionary data.
|
|
233
|
+
"""
|
|
234
|
+
sender = AgentAddress.from_dict(data["sender"])
|
|
235
|
+
recipient = AgentAddress.from_dict(data["recipient"])
|
|
236
|
+
pattern = MessagePattern.from_string(data["pattern"])
|
|
237
|
+
payload = data["payload"]
|
|
238
|
+
headers = data.get("headers", {})
|
|
239
|
+
correlation_id = data.get("correlation_id", None)
|
|
240
|
+
identifier = data["identifier"]
|
|
241
|
+
timestamp_str = data["timestamp"]
|
|
242
|
+
timestamp = datetime.fromisoformat(timestamp_str)
|
|
243
|
+
serialization_format = data.get("serialization_format", None)
|
|
244
|
+
|
|
245
|
+
message = cls(
|
|
246
|
+
sender=sender,
|
|
247
|
+
recipient=recipient,
|
|
248
|
+
payload=payload,
|
|
249
|
+
pattern=pattern,
|
|
250
|
+
headers=headers,
|
|
251
|
+
correlation_id=correlation_id,
|
|
252
|
+
identifier=identifier,
|
|
253
|
+
timestamp=timestamp,
|
|
254
|
+
serialization_format=serialization_format
|
|
255
|
+
)
|
|
256
|
+
return message
|
|
257
|
+
|
|
258
|
+
@classmethod
|
|
259
|
+
def from_json(cls, json_str: str) -> "Message":
|
|
260
|
+
"""
|
|
261
|
+
Deserializes a JSON string into a Message instance.
|
|
262
|
+
|
|
263
|
+
:param json_str: A JSON string containing message fields.
|
|
264
|
+
|
|
265
|
+
:returns: A Message instance reconstructed from the JSON data.
|
|
266
|
+
"""
|
|
267
|
+
data = json.loads(json_str)
|
|
268
|
+
message = cls.from_dict(data)
|
|
269
|
+
return message
|
|
270
|
+
|
|
271
|
+
def __repr__(self) -> str:
|
|
272
|
+
"""
|
|
273
|
+
Returns a detailed string representation including class name,
|
|
274
|
+
identifier, pattern, and a truncated payload preview.
|
|
275
|
+
|
|
276
|
+
The payload preview is truncated to the first 50 characters.
|
|
277
|
+
|
|
278
|
+
:returns: A repr string for debugging.
|
|
279
|
+
"""
|
|
280
|
+
payload_str = repr(self._payload)
|
|
281
|
+
if len(payload_str) > PAYLOAD_PREVIEW_MAX_LENGTH:
|
|
282
|
+
payload_preview = payload_str[:PAYLOAD_PREVIEW_MAX_LENGTH] + "..."
|
|
283
|
+
else:
|
|
284
|
+
payload_preview = payload_str
|
|
285
|
+
|
|
286
|
+
repr_str = "Message(identifier={!r}, pattern={!r}, payload={})".format(
|
|
287
|
+
self._identifier, self._pattern.value, payload_preview
|
|
288
|
+
)
|
|
289
|
+
return repr_str
|
|
290
|
+
|
|
291
|
+
def __str__(self) -> str:
|
|
292
|
+
"""
|
|
293
|
+
Returns a human-readable summary including the sender, recipient,
|
|
294
|
+
pattern, and a truncated payload preview.
|
|
295
|
+
|
|
296
|
+
The payload preview is truncated to the first 50 characters.
|
|
297
|
+
|
|
298
|
+
:returns: A human-readable string.
|
|
299
|
+
"""
|
|
300
|
+
payload_str = repr(self._payload)
|
|
301
|
+
if len(payload_str) > PAYLOAD_PREVIEW_MAX_LENGTH:
|
|
302
|
+
payload_preview = payload_str[:PAYLOAD_PREVIEW_MAX_LENGTH] + "..."
|
|
303
|
+
else:
|
|
304
|
+
payload_preview = payload_str
|
|
305
|
+
|
|
306
|
+
str_val = "Message: sender={}, recipient={}, pattern={}, payload={}".format(
|
|
307
|
+
self._sender.agent_identifier, self._recipient.agent_identifier,
|
|
308
|
+
self._pattern.value, payload_preview
|
|
309
|
+
)
|
|
310
|
+
return str_val
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the MessageEnvelope class representing an immutable wrapper
|
|
3
|
+
around a Message that adds transport-level delivery metadata.
|
|
4
|
+
|
|
5
|
+
A MessageEnvelope contains the wrapped Message, the transport name,
|
|
6
|
+
the delivery attempt count, and the delivery status. The delivery
|
|
7
|
+
status is validated against a set of valid statuses: "pending",
|
|
8
|
+
"delivered", and "failed".
|
|
9
|
+
|
|
10
|
+
.. module:: ccai.agentkit.messageenvelope
|
|
11
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
12
|
+
:synopsis: Immutable transport-level message wrapper with delivery metadata.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from typing import Dict
|
|
16
|
+
|
|
17
|
+
from ccai.agentkit.errors import SemanticError
|
|
18
|
+
from ccai.agentkit.message import Message
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class MessageEnvelope:
|
|
22
|
+
"""
|
|
23
|
+
Immutable wrapper around a Message that adds transport-level delivery
|
|
24
|
+
metadata.
|
|
25
|
+
|
|
26
|
+
Contains the wrapped Message, the transport name identifying which
|
|
27
|
+
transport handled the delivery, the delivery attempt count, and the
|
|
28
|
+
delivery status. The delivery status must be one of the values in
|
|
29
|
+
VALID_STATUSES: "pending", "delivered", or "failed".
|
|
30
|
+
|
|
31
|
+
:param message: The wrapped Message instance.
|
|
32
|
+
:param transport_name: The name of the transport handling delivery.
|
|
33
|
+
:param attempt_count: The number of delivery attempts. Defaults to 1.
|
|
34
|
+
:param delivery_status: The delivery status string. Defaults to "pending".
|
|
35
|
+
|
|
36
|
+
:raises SemanticError: If delivery_status is not one of the valid statuses.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
VALID_STATUSES = {"pending", "delivered", "failed"}
|
|
40
|
+
|
|
41
|
+
def __init__(self, message: Message, transport_name: str,
|
|
42
|
+
attempt_count: int = 1,
|
|
43
|
+
delivery_status: str = "pending") -> None:
|
|
44
|
+
|
|
45
|
+
if delivery_status not in self.VALID_STATUSES:
|
|
46
|
+
errmsg = (
|
|
47
|
+
"Invalid delivery_status {!r}. "
|
|
48
|
+
"Valid statuses are: {}."
|
|
49
|
+
).format(delivery_status, sorted(self.VALID_STATUSES))
|
|
50
|
+
raise SemanticError(errmsg)
|
|
51
|
+
|
|
52
|
+
self._message = message
|
|
53
|
+
self._transport_name = transport_name
|
|
54
|
+
self._attempt_count = attempt_count
|
|
55
|
+
self._delivery_status = delivery_status
|
|
56
|
+
|
|
57
|
+
return
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def message(self) -> Message:
|
|
61
|
+
"""
|
|
62
|
+
The wrapped Message instance.
|
|
63
|
+
|
|
64
|
+
:returns: The Message object.
|
|
65
|
+
"""
|
|
66
|
+
return self._message
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def transport_name(self) -> str:
|
|
70
|
+
"""
|
|
71
|
+
The name of the transport handling delivery.
|
|
72
|
+
|
|
73
|
+
:returns: The transport name string.
|
|
74
|
+
"""
|
|
75
|
+
return self._transport_name
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def attempt_count(self) -> int:
|
|
79
|
+
"""
|
|
80
|
+
The number of delivery attempts.
|
|
81
|
+
|
|
82
|
+
:returns: The attempt count integer.
|
|
83
|
+
"""
|
|
84
|
+
return self._attempt_count
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def delivery_status(self) -> str:
|
|
88
|
+
"""
|
|
89
|
+
The delivery status string.
|
|
90
|
+
|
|
91
|
+
:returns: The delivery status ("pending", "delivered", or "failed").
|
|
92
|
+
"""
|
|
93
|
+
return self._delivery_status
|
|
94
|
+
|
|
95
|
+
def as_dict(self) -> Dict:
|
|
96
|
+
"""
|
|
97
|
+
Returns a dictionary representation of the envelope fields
|
|
98
|
+
including the wrapped message.
|
|
99
|
+
|
|
100
|
+
The wrapped message is converted to a dictionary via its
|
|
101
|
+
as_dict method.
|
|
102
|
+
|
|
103
|
+
:returns: A dictionary with message, transport_name,
|
|
104
|
+
attempt_count, and delivery_status.
|
|
105
|
+
"""
|
|
106
|
+
result = {
|
|
107
|
+
"message": self._message.as_dict(),
|
|
108
|
+
"transport_name": self._transport_name,
|
|
109
|
+
"attempt_count": self._attempt_count,
|
|
110
|
+
"delivery_status": self._delivery_status
|
|
111
|
+
}
|
|
112
|
+
return result
|
|
113
|
+
|
|
114
|
+
def __repr__(self) -> str:
|
|
115
|
+
"""
|
|
116
|
+
Returns a detailed string representation including class name,
|
|
117
|
+
message identifier, transport name, and delivery status.
|
|
118
|
+
|
|
119
|
+
:returns: A repr string for debugging.
|
|
120
|
+
"""
|
|
121
|
+
repr_str = (
|
|
122
|
+
"MessageEnvelope(message_id={!r}, transport_name={!r}, "
|
|
123
|
+
"delivery_status={!r})"
|
|
124
|
+
).format(
|
|
125
|
+
self._message.identifier, self._transport_name,
|
|
126
|
+
self._delivery_status
|
|
127
|
+
)
|
|
128
|
+
return repr_str
|
|
129
|
+
|
|
130
|
+
def __str__(self) -> str:
|
|
131
|
+
"""
|
|
132
|
+
Returns a human-readable summary including the message identifier,
|
|
133
|
+
transport name, attempt count, and delivery status.
|
|
134
|
+
|
|
135
|
+
:returns: A human-readable string.
|
|
136
|
+
"""
|
|
137
|
+
str_val = (
|
|
138
|
+
"MessageEnvelope: message_id={}, transport={}, "
|
|
139
|
+
"attempts={}, status={}"
|
|
140
|
+
).format(
|
|
141
|
+
self._message.identifier, self._transport_name,
|
|
142
|
+
self._attempt_count, self._delivery_status
|
|
143
|
+
)
|
|
144
|
+
return str_val
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the MessagePattern enum representing the communication patterns
|
|
3
|
+
for messages in the Communication & Messaging feature.
|
|
4
|
+
|
|
5
|
+
.. module:: ccai.agentkit.messagepattern
|
|
6
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
7
|
+
:synopsis: Enumeration of message communication patterns.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import enum
|
|
11
|
+
|
|
12
|
+
from ccai.agentkit.errors import SemanticError
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class MessagePattern(enum.Enum):
|
|
16
|
+
"""
|
|
17
|
+
Enumeration of message communication patterns.
|
|
18
|
+
|
|
19
|
+
REQUEST: Expects a correlated response from the recipient.
|
|
20
|
+
RESPONSE: Reply to a previously received request.
|
|
21
|
+
FIRE_AND_FORGET: No response expected from the recipient.
|
|
22
|
+
EVENT: Pub/sub broadcast to all topic subscribers.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
REQUEST = "REQUEST"
|
|
26
|
+
RESPONSE = "RESPONSE"
|
|
27
|
+
FIRE_AND_FORGET = "FIRE_AND_FORGET"
|
|
28
|
+
EVENT = "EVENT"
|
|
29
|
+
|
|
30
|
+
@classmethod
|
|
31
|
+
def from_string(cls, value: str) -> "MessagePattern":
|
|
32
|
+
"""
|
|
33
|
+
Returns the MessagePattern member matching the given string value.
|
|
34
|
+
|
|
35
|
+
:param value: The string value to match.
|
|
36
|
+
|
|
37
|
+
:returns: The matching MessagePattern member.
|
|
38
|
+
:raises SemanticError: If the value does not match any member.
|
|
39
|
+
"""
|
|
40
|
+
upper_value = value.upper()
|
|
41
|
+
|
|
42
|
+
for member in cls:
|
|
43
|
+
if member.value == upper_value:
|
|
44
|
+
return member
|
|
45
|
+
|
|
46
|
+
valid_values = [m.value for m in cls]
|
|
47
|
+
errmsg = "Invalid MessagePattern value '{}'. Valid values are: {}.".format(
|
|
48
|
+
value, ", ".join(valid_values)
|
|
49
|
+
)
|
|
50
|
+
raise SemanticError(errmsg)
|