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
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the MessageRouter class for routing incoming messages to
|
|
3
|
+
registered MessageHandler callbacks based on topic, pattern, or
|
|
4
|
+
custom filter criteria.
|
|
5
|
+
|
|
6
|
+
MessageRouter supports three registration modes: by topic string,
|
|
7
|
+
by MessagePattern value, and by custom filter callable. Handlers
|
|
8
|
+
are invoked in registration order. If a handler raises an exception,
|
|
9
|
+
the exception is logged at WARNING and remaining handlers continue
|
|
10
|
+
to be invoked. If no handler matches an incoming message, a
|
|
11
|
+
MessageRoutingError is raised.
|
|
12
|
+
|
|
13
|
+
.. module:: ccai.agentkit.messagerouter
|
|
14
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
15
|
+
:synopsis: Message routing to registered handler callbacks.
|
|
16
|
+
|
|
17
|
+
Requirements: 11.1-11.11, 26.13, 26.14
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import logging
|
|
21
|
+
|
|
22
|
+
from typing import Callable
|
|
23
|
+
from typing import List
|
|
24
|
+
|
|
25
|
+
from ccai.agentkit.message import Message
|
|
26
|
+
from ccai.agentkit.messagepattern import MessagePattern
|
|
27
|
+
from ccai.agentkit.messagingerrors import MessageRoutingError
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
_MATCH_TYPE_TOPIC = "topic"
|
|
31
|
+
_MATCH_TYPE_PATTERN = "pattern"
|
|
32
|
+
_MATCH_TYPE_FILTER = "filter"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class MessageRouter:
|
|
36
|
+
"""
|
|
37
|
+
Routes incoming messages to registered MessageHandler callbacks
|
|
38
|
+
based on topic, pattern, or custom filter criteria.
|
|
39
|
+
|
|
40
|
+
Handlers are registered with one of three matching strategies:
|
|
41
|
+
topic string matching (against message headers), MessagePattern
|
|
42
|
+
matching (against message.pattern), or custom filter callable
|
|
43
|
+
matching. Each registration returns a unique registration
|
|
44
|
+
identifier string that can be used for deregistration.
|
|
45
|
+
|
|
46
|
+
When routing a message, all matching handlers are invoked in
|
|
47
|
+
the order they were registered. If a handler raises an exception,
|
|
48
|
+
the exception is logged at WARNING level and the remaining
|
|
49
|
+
handlers continue to be invoked. If no handler matches, a
|
|
50
|
+
MessageRoutingError is raised.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
def __init__(self) -> None:
|
|
54
|
+
self._handlers: List[dict] = []
|
|
55
|
+
self._next_registration_id: int = 0
|
|
56
|
+
self._logger = logging.getLogger("ccai.agentkit.messaging.router")
|
|
57
|
+
return
|
|
58
|
+
|
|
59
|
+
def _allocate_registration_id(self) -> str:
|
|
60
|
+
"""
|
|
61
|
+
Allocates and returns the next unique registration identifier.
|
|
62
|
+
|
|
63
|
+
:returns: A registration identifier string.
|
|
64
|
+
"""
|
|
65
|
+
registration_id = "handler-{}".format(self._next_registration_id)
|
|
66
|
+
self._next_registration_id = self._next_registration_id + 1
|
|
67
|
+
return registration_id
|
|
68
|
+
|
|
69
|
+
def register_by_topic(self, topic: str, handler: Callable) -> str:
|
|
70
|
+
"""
|
|
71
|
+
Registers a handler for a specific topic string.
|
|
72
|
+
|
|
73
|
+
The handler will be invoked when a routed message has a
|
|
74
|
+
headers entry with key "topic" matching the registered
|
|
75
|
+
topic string.
|
|
76
|
+
|
|
77
|
+
:param topic: The topic string to match.
|
|
78
|
+
:param handler: The MessageHandler callback.
|
|
79
|
+
|
|
80
|
+
:returns: A registration identifier string.
|
|
81
|
+
"""
|
|
82
|
+
registration_id = self._allocate_registration_id()
|
|
83
|
+
|
|
84
|
+
entry = {
|
|
85
|
+
"registration_id": registration_id,
|
|
86
|
+
"match_type": _MATCH_TYPE_TOPIC,
|
|
87
|
+
"match_value": topic,
|
|
88
|
+
"handler": handler,
|
|
89
|
+
}
|
|
90
|
+
self._handlers.append(entry)
|
|
91
|
+
|
|
92
|
+
return registration_id
|
|
93
|
+
|
|
94
|
+
def register_by_pattern(self, pattern: MessagePattern, handler: Callable) -> str:
|
|
95
|
+
"""
|
|
96
|
+
Registers a handler for a specific MessagePattern value.
|
|
97
|
+
|
|
98
|
+
The handler will be invoked when a routed message has a
|
|
99
|
+
pattern matching the registered MessagePattern.
|
|
100
|
+
|
|
101
|
+
:param pattern: The MessagePattern to match.
|
|
102
|
+
:param handler: The MessageHandler callback.
|
|
103
|
+
|
|
104
|
+
:returns: A registration identifier string.
|
|
105
|
+
"""
|
|
106
|
+
registration_id = self._allocate_registration_id()
|
|
107
|
+
|
|
108
|
+
entry = {
|
|
109
|
+
"registration_id": registration_id,
|
|
110
|
+
"match_type": _MATCH_TYPE_PATTERN,
|
|
111
|
+
"match_value": pattern,
|
|
112
|
+
"handler": handler,
|
|
113
|
+
}
|
|
114
|
+
self._handlers.append(entry)
|
|
115
|
+
|
|
116
|
+
return registration_id
|
|
117
|
+
|
|
118
|
+
def register_by_filter(self, filter_fn: Callable, handler: Callable) -> str:
|
|
119
|
+
"""
|
|
120
|
+
Registers a handler with a custom filter callable.
|
|
121
|
+
|
|
122
|
+
The handler will be invoked when the filter callable
|
|
123
|
+
returns True for a routed message.
|
|
124
|
+
|
|
125
|
+
:param filter_fn: A callable accepting a Message and returning bool.
|
|
126
|
+
:param handler: The MessageHandler callback.
|
|
127
|
+
|
|
128
|
+
:returns: A registration identifier string.
|
|
129
|
+
"""
|
|
130
|
+
registration_id = self._allocate_registration_id()
|
|
131
|
+
|
|
132
|
+
entry = {
|
|
133
|
+
"registration_id": registration_id,
|
|
134
|
+
"match_type": _MATCH_TYPE_FILTER,
|
|
135
|
+
"match_value": filter_fn,
|
|
136
|
+
"handler": handler,
|
|
137
|
+
}
|
|
138
|
+
self._handlers.append(entry)
|
|
139
|
+
|
|
140
|
+
return registration_id
|
|
141
|
+
|
|
142
|
+
def deregister(self, registration_id: str) -> None:
|
|
143
|
+
"""
|
|
144
|
+
Removes a handler by its registration identifier.
|
|
145
|
+
|
|
146
|
+
If the registration identifier is not found, the operation
|
|
147
|
+
is a no-op.
|
|
148
|
+
|
|
149
|
+
:param registration_id: The registration identifier returned
|
|
150
|
+
at registration time.
|
|
151
|
+
"""
|
|
152
|
+
updated_handlers = []
|
|
153
|
+
|
|
154
|
+
for entry in self._handlers:
|
|
155
|
+
entry_id = entry["registration_id"]
|
|
156
|
+
is_target = entry_id == registration_id
|
|
157
|
+
|
|
158
|
+
if is_target is not True:
|
|
159
|
+
updated_handlers.append(entry)
|
|
160
|
+
|
|
161
|
+
self._handlers = updated_handlers
|
|
162
|
+
|
|
163
|
+
return
|
|
164
|
+
|
|
165
|
+
def _matches(self, entry: dict, message: Message) -> bool:
|
|
166
|
+
"""
|
|
167
|
+
Determines whether a handler entry matches the given message.
|
|
168
|
+
|
|
169
|
+
:param entry: The handler registration entry dictionary.
|
|
170
|
+
:param message: The incoming Message.
|
|
171
|
+
|
|
172
|
+
:returns: True if the entry matches the message, False otherwise.
|
|
173
|
+
"""
|
|
174
|
+
match_type = entry["match_type"]
|
|
175
|
+
match_value = entry["match_value"]
|
|
176
|
+
|
|
177
|
+
if match_type == _MATCH_TYPE_TOPIC:
|
|
178
|
+
message_topic = message.headers.get("topic", None)
|
|
179
|
+
is_match = message_topic == match_value
|
|
180
|
+
return is_match
|
|
181
|
+
|
|
182
|
+
if match_type == _MATCH_TYPE_PATTERN:
|
|
183
|
+
is_match = message.pattern == match_value
|
|
184
|
+
return is_match
|
|
185
|
+
|
|
186
|
+
if match_type == _MATCH_TYPE_FILTER:
|
|
187
|
+
filter_fn = match_value
|
|
188
|
+
is_match = filter_fn(message)
|
|
189
|
+
return is_match
|
|
190
|
+
|
|
191
|
+
return False
|
|
192
|
+
|
|
193
|
+
def route(self, message: Message) -> None:
|
|
194
|
+
"""
|
|
195
|
+
Routes a message to all matching handlers.
|
|
196
|
+
|
|
197
|
+
Invokes all registered handlers whose topic, pattern, or
|
|
198
|
+
custom filter matches the incoming message, in registration
|
|
199
|
+
order. If a handler raises an exception, the exception is
|
|
200
|
+
logged at WARNING level and remaining handlers continue to
|
|
201
|
+
be invoked.
|
|
202
|
+
|
|
203
|
+
:param message: The incoming Message.
|
|
204
|
+
|
|
205
|
+
:raises MessageRoutingError: If no handler matches the message.
|
|
206
|
+
"""
|
|
207
|
+
matched_count = 0
|
|
208
|
+
|
|
209
|
+
for entry in self._handlers:
|
|
210
|
+
is_match = self._matches(entry, message)
|
|
211
|
+
|
|
212
|
+
if is_match is True:
|
|
213
|
+
matched_count = matched_count + 1
|
|
214
|
+
handler = entry["handler"]
|
|
215
|
+
registration_id = entry["registration_id"]
|
|
216
|
+
|
|
217
|
+
try:
|
|
218
|
+
handler(message)
|
|
219
|
+
except Exception as exc:
|
|
220
|
+
self._logger.warning(
|
|
221
|
+
"Handler '%s' raised exception while processing "
|
|
222
|
+
"message '%s': %s",
|
|
223
|
+
registration_id, message.identifier, exc
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
if matched_count == 0:
|
|
227
|
+
topic = message.headers.get("topic", None)
|
|
228
|
+
pattern_value = message.pattern.value if message.pattern is not None else None
|
|
229
|
+
|
|
230
|
+
raise MessageRoutingError(
|
|
231
|
+
message_id=message.identifier,
|
|
232
|
+
target=topic,
|
|
233
|
+
detail="No handler matched message with pattern '{}' and topic '{}'".format(
|
|
234
|
+
pattern_value, topic
|
|
235
|
+
)
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
return
|
|
239
|
+
|
|
240
|
+
@property
|
|
241
|
+
def handler_count(self) -> int:
|
|
242
|
+
"""
|
|
243
|
+
The number of currently registered handlers.
|
|
244
|
+
|
|
245
|
+
:returns: The count of registered handlers.
|
|
246
|
+
"""
|
|
247
|
+
rtnval = len(self._handlers)
|
|
248
|
+
return rtnval
|
|
249
|
+
|
|
250
|
+
def __repr__(self) -> str:
|
|
251
|
+
"""
|
|
252
|
+
Returns a detailed string representation including the
|
|
253
|
+
class name and the count of registered handlers.
|
|
254
|
+
|
|
255
|
+
:returns: A repr string for debugging.
|
|
256
|
+
"""
|
|
257
|
+
repr_str = "{}(handler_count={})".format(
|
|
258
|
+
self.__class__.__name__, self.handler_count
|
|
259
|
+
)
|
|
260
|
+
return repr_str
|
|
261
|
+
|
|
262
|
+
def __str__(self) -> str:
|
|
263
|
+
"""
|
|
264
|
+
Returns a human-readable summary including the count of
|
|
265
|
+
registered handlers and the list of registered topics.
|
|
266
|
+
|
|
267
|
+
:returns: A human-readable string describing the router state.
|
|
268
|
+
"""
|
|
269
|
+
topics = []
|
|
270
|
+
|
|
271
|
+
for entry in self._handlers:
|
|
272
|
+
match_type = entry["match_type"]
|
|
273
|
+
|
|
274
|
+
if match_type == _MATCH_TYPE_TOPIC:
|
|
275
|
+
topic_value = entry["match_value"]
|
|
276
|
+
is_duplicate = topic_value in topics
|
|
277
|
+
|
|
278
|
+
if is_duplicate is not True:
|
|
279
|
+
topics.append(topic_value)
|
|
280
|
+
|
|
281
|
+
if len(topics) == 0:
|
|
282
|
+
str_val = "MessageRouter: {} handler(s) registered, no topics".format(
|
|
283
|
+
self.handler_count
|
|
284
|
+
)
|
|
285
|
+
else:
|
|
286
|
+
str_val = "MessageRouter: {} handler(s) registered, topics=[{}]".format(
|
|
287
|
+
self.handler_count, ", ".join(topics)
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
return str_val
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the MessageSerializer abstract base class for serializing
|
|
3
|
+
and deserializing message payloads between Python objects and wire
|
|
4
|
+
format bytes.
|
|
5
|
+
|
|
6
|
+
Concrete implementations handle specific formats such as JSON,
|
|
7
|
+
MessagePack, and Protobuf. Each serializer exposes a format name
|
|
8
|
+
and content type, and provides serialize and deserialize methods.
|
|
9
|
+
|
|
10
|
+
.. module:: ccai.agentkit.messageserializer
|
|
11
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
12
|
+
:synopsis: Abstract base class for message payload serializers.
|
|
13
|
+
|
|
14
|
+
Requirements: 9.1-9.4, 9.14, 9.15
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import abc
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MessageSerializer(abc.ABC):
|
|
21
|
+
"""
|
|
22
|
+
Abstract base class for serializing and deserializing message
|
|
23
|
+
payloads between Python objects and wire format bytes.
|
|
24
|
+
|
|
25
|
+
Subclasses must implement the serialize and deserialize methods
|
|
26
|
+
for their specific wire format. Each serializer is identified
|
|
27
|
+
by a format name (e.g. 'json', 'msgpack', 'protobuf') and a
|
|
28
|
+
content type (e.g. 'application/json').
|
|
29
|
+
|
|
30
|
+
:param format_name: The serializer format name string.
|
|
31
|
+
:param content_type: The MIME content type string for the
|
|
32
|
+
serialized format.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self, format_name: str, content_type: str) -> None:
|
|
36
|
+
self._format_name = format_name
|
|
37
|
+
self._content_type = content_type
|
|
38
|
+
return
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def format_name(self) -> str:
|
|
42
|
+
"""
|
|
43
|
+
The serializer format name.
|
|
44
|
+
|
|
45
|
+
:returns: The format name string (e.g. 'json', 'msgpack',
|
|
46
|
+
'protobuf').
|
|
47
|
+
"""
|
|
48
|
+
return self._format_name
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def content_type(self) -> str:
|
|
52
|
+
"""
|
|
53
|
+
The MIME content type for the serialized format.
|
|
54
|
+
|
|
55
|
+
:returns: The content type string (e.g. 'application/json',
|
|
56
|
+
'application/x-msgpack', 'application/x-protobuf').
|
|
57
|
+
"""
|
|
58
|
+
return self._content_type
|
|
59
|
+
|
|
60
|
+
@abc.abstractmethod
|
|
61
|
+
def serialize(self, obj: object) -> bytes:
|
|
62
|
+
"""
|
|
63
|
+
Serializes a Python object to bytes.
|
|
64
|
+
|
|
65
|
+
:param obj: The Python object to serialize.
|
|
66
|
+
|
|
67
|
+
:returns: The serialized bytes.
|
|
68
|
+
:raises MessageSerializationError: If the object cannot be
|
|
69
|
+
serialized.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
@abc.abstractmethod
|
|
73
|
+
def deserialize(self, data: bytes) -> object:
|
|
74
|
+
"""
|
|
75
|
+
Deserializes bytes to a Python object.
|
|
76
|
+
|
|
77
|
+
:param data: The bytes to deserialize.
|
|
78
|
+
|
|
79
|
+
:returns: The deserialized Python object.
|
|
80
|
+
:raises MessageSerializationError: If the bytes cannot be
|
|
81
|
+
deserialized.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
def __repr__(self) -> str:
|
|
85
|
+
"""
|
|
86
|
+
Returns a detailed string representation including the
|
|
87
|
+
class name and format name.
|
|
88
|
+
|
|
89
|
+
:returns: A repr string for debugging.
|
|
90
|
+
"""
|
|
91
|
+
repr_str = "{}(format_name={!r})".format(
|
|
92
|
+
self.__class__.__name__, self._format_name
|
|
93
|
+
)
|
|
94
|
+
return repr_str
|
|
95
|
+
|
|
96
|
+
def __str__(self) -> str:
|
|
97
|
+
"""
|
|
98
|
+
Returns a human-readable summary including the format name
|
|
99
|
+
and content type.
|
|
100
|
+
|
|
101
|
+
:returns: A human-readable string describing the serializer.
|
|
102
|
+
"""
|
|
103
|
+
str_val = "MessageSerializer: format={}, content_type={}".format(
|
|
104
|
+
self._format_name, self._content_type
|
|
105
|
+
)
|
|
106
|
+
return str_val
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the MessageSerializerRegistry class for tracking available
|
|
3
|
+
MessageSerializer instances within an Agent in the Communication &
|
|
4
|
+
Messaging feature.
|
|
5
|
+
|
|
6
|
+
MessageSerializerRegistry provides registration, deregistration,
|
|
7
|
+
lookup by format name, enumeration, and existence checks for
|
|
8
|
+
MessageSerializer instances. Each Agent maintains its own
|
|
9
|
+
MessageSerializerRegistry instance.
|
|
10
|
+
|
|
11
|
+
.. module:: ccai.agentkit.messageserializerregistry
|
|
12
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
13
|
+
:synopsis: Serializer registration and lookup by format name.
|
|
14
|
+
|
|
15
|
+
Requirements: 10.1-10.9, 26.11, 26.12
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import os
|
|
19
|
+
|
|
20
|
+
from typing import Dict
|
|
21
|
+
from typing import List
|
|
22
|
+
from typing import Optional
|
|
23
|
+
|
|
24
|
+
from ccai.agentkit.errors import SemanticError
|
|
25
|
+
from ccai.agentkit.messageserializer import MessageSerializer
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class MessageSerializerRegistry:
|
|
29
|
+
"""
|
|
30
|
+
Registry tracking available MessageSerializer instances.
|
|
31
|
+
Provides lookup by format name and enumeration.
|
|
32
|
+
|
|
33
|
+
Serializers are registered using their format name as the key.
|
|
34
|
+
Duplicate format names are rejected with a SemanticError.
|
|
35
|
+
Lookup by format name returns None when the format name is not
|
|
36
|
+
found.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def __init__(self) -> None:
|
|
40
|
+
self._serializers: Dict[str, MessageSerializer] = {}
|
|
41
|
+
return
|
|
42
|
+
|
|
43
|
+
def register(self, serializer: MessageSerializer) -> None:
|
|
44
|
+
"""
|
|
45
|
+
Registers a MessageSerializer instance using its format
|
|
46
|
+
name as the key.
|
|
47
|
+
|
|
48
|
+
:param serializer: The MessageSerializer instance to
|
|
49
|
+
register.
|
|
50
|
+
|
|
51
|
+
:raises SemanticError: If a serializer with the same
|
|
52
|
+
format name is already registered.
|
|
53
|
+
"""
|
|
54
|
+
format_name = serializer.format_name
|
|
55
|
+
is_duplicate = format_name in self._serializers
|
|
56
|
+
|
|
57
|
+
if is_duplicate is True:
|
|
58
|
+
errmsg_lines = [
|
|
59
|
+
"Duplicate serializer registration attempted.",
|
|
60
|
+
" Format name: '{}'".format(format_name),
|
|
61
|
+
" A serializer with this format name is already registered.",
|
|
62
|
+
]
|
|
63
|
+
errmsg = os.linesep.join(errmsg_lines)
|
|
64
|
+
raise SemanticError(errmsg)
|
|
65
|
+
|
|
66
|
+
self._serializers[format_name] = serializer
|
|
67
|
+
|
|
68
|
+
return
|
|
69
|
+
|
|
70
|
+
def deregister(self, format_name: str) -> None:
|
|
71
|
+
"""
|
|
72
|
+
Removes a MessageSerializer from the registry by its
|
|
73
|
+
format name.
|
|
74
|
+
|
|
75
|
+
If the format name is not found, the operation is a no-op.
|
|
76
|
+
|
|
77
|
+
:param format_name: The format name of the serializer to
|
|
78
|
+
remove.
|
|
79
|
+
"""
|
|
80
|
+
is_present = format_name in self._serializers
|
|
81
|
+
|
|
82
|
+
if is_present is True:
|
|
83
|
+
del self._serializers[format_name]
|
|
84
|
+
|
|
85
|
+
return
|
|
86
|
+
|
|
87
|
+
def lookup(self, format_name: str) -> Optional[MessageSerializer]:
|
|
88
|
+
"""
|
|
89
|
+
Looks up a registered MessageSerializer by its format name.
|
|
90
|
+
|
|
91
|
+
:param format_name: The format name of the serializer to
|
|
92
|
+
look up.
|
|
93
|
+
|
|
94
|
+
:returns: The MessageSerializer instance if found, or None
|
|
95
|
+
if not found.
|
|
96
|
+
"""
|
|
97
|
+
rtnval = self._serializers.get(format_name, None)
|
|
98
|
+
return rtnval
|
|
99
|
+
|
|
100
|
+
def list_all(self) -> List[MessageSerializer]:
|
|
101
|
+
"""
|
|
102
|
+
Returns a list of all registered MessageSerializer
|
|
103
|
+
instances.
|
|
104
|
+
|
|
105
|
+
:returns: A list of all registered MessageSerializer
|
|
106
|
+
instances.
|
|
107
|
+
"""
|
|
108
|
+
rtnval = list(self._serializers.values())
|
|
109
|
+
return rtnval
|
|
110
|
+
|
|
111
|
+
def has_serializer(self, format_name: str) -> bool:
|
|
112
|
+
"""
|
|
113
|
+
Checks whether a MessageSerializer with the given format
|
|
114
|
+
name is registered.
|
|
115
|
+
|
|
116
|
+
:param format_name: The format name to check.
|
|
117
|
+
|
|
118
|
+
:returns: True if a serializer with the given format name
|
|
119
|
+
is registered, False otherwise.
|
|
120
|
+
"""
|
|
121
|
+
rtnval = format_name in self._serializers
|
|
122
|
+
return rtnval
|
|
123
|
+
|
|
124
|
+
@property
|
|
125
|
+
def count(self) -> int:
|
|
126
|
+
"""
|
|
127
|
+
The number of currently registered serializers.
|
|
128
|
+
|
|
129
|
+
:returns: The count of registered serializers.
|
|
130
|
+
"""
|
|
131
|
+
rtnval = len(self._serializers)
|
|
132
|
+
return rtnval
|
|
133
|
+
|
|
134
|
+
def __repr__(self) -> str:
|
|
135
|
+
"""
|
|
136
|
+
Returns a detailed string representation including the
|
|
137
|
+
class name and the count of registered serializers.
|
|
138
|
+
|
|
139
|
+
:returns: A repr string for debugging.
|
|
140
|
+
"""
|
|
141
|
+
repr_str = "{}(count={})".format(
|
|
142
|
+
self.__class__.__name__, self.count
|
|
143
|
+
)
|
|
144
|
+
return repr_str
|
|
145
|
+
|
|
146
|
+
def __str__(self) -> str:
|
|
147
|
+
"""
|
|
148
|
+
Returns a human-readable summary listing the registered
|
|
149
|
+
format names.
|
|
150
|
+
|
|
151
|
+
:returns: A human-readable string describing the registry
|
|
152
|
+
contents.
|
|
153
|
+
"""
|
|
154
|
+
if self.count == 0:
|
|
155
|
+
str_val = "MessageSerializerRegistry: 0 serializers registered"
|
|
156
|
+
return str_val
|
|
157
|
+
|
|
158
|
+
format_names = sorted(self._serializers.keys())
|
|
159
|
+
|
|
160
|
+
str_val = "MessageSerializerRegistry: {} serializer(s) registered [{}]".format(
|
|
161
|
+
self.count, ", ".join(format_names)
|
|
162
|
+
)
|
|
163
|
+
return str_val
|