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,473 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the WebSocketTransport class, a concrete Transport
|
|
3
|
+
implementation that uses WebSocket protocol semantics for
|
|
4
|
+
persistent bidirectional communication between agents.
|
|
5
|
+
|
|
6
|
+
The WebSocketTransport maintains a persistent WebSocket connection
|
|
7
|
+
to the configured endpoint. It constructs ws:// or wss:// URLs
|
|
8
|
+
from the TransportConfiguration (host, port, path), serializes
|
|
9
|
+
messages with the MessageSerializer for the message's serialization
|
|
10
|
+
format (JSON by default), and sends them as WebSocket text frames
|
|
11
|
+
for textual formats such as JSON and as binary frames otherwise.
|
|
12
|
+
|
|
13
|
+
TLS is supported via wss:// URL scheme when the configuration
|
|
14
|
+
options include ``{"tls": True}``.
|
|
15
|
+
|
|
16
|
+
The transport exposes a connection_state property that tracks
|
|
17
|
+
the current state of the WebSocket connection as one of
|
|
18
|
+
"disconnected", "connecting", "connected", or "closing".
|
|
19
|
+
|
|
20
|
+
The transport raises TransportError for connection loss, send
|
|
21
|
+
failures, and receive failures.
|
|
22
|
+
|
|
23
|
+
Structured logging is performed under the
|
|
24
|
+
ccai.agentkit.messaging.transport.websocket logger hierarchy.
|
|
25
|
+
|
|
26
|
+
The transport is designed to work with any WebSocket client that
|
|
27
|
+
provides connect, send, recv, and close methods. The actual
|
|
28
|
+
WebSocket client library (such as the ``websockets`` package) is
|
|
29
|
+
not imported directly; instead, the transport wraps whatever
|
|
30
|
+
client is provided or attempts to import ``websockets.sync.client``
|
|
31
|
+
at connect time.
|
|
32
|
+
|
|
33
|
+
.. module:: ccai.agentkit.transports.websockettransport
|
|
34
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
35
|
+
:synopsis: WebSocket transport implementation for persistent
|
|
36
|
+
bidirectional agent messaging.
|
|
37
|
+
|
|
38
|
+
Requirements: 17.1-17.8
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
import logging
|
|
42
|
+
import ssl
|
|
43
|
+
|
|
44
|
+
from typing import Dict
|
|
45
|
+
from typing import List
|
|
46
|
+
from typing import Optional
|
|
47
|
+
|
|
48
|
+
from ccai.agentkit.message import Message
|
|
49
|
+
from ccai.agentkit.messageenvelope import MessageEnvelope
|
|
50
|
+
from ccai.agentkit.messagingerrors import MessageSerializationError
|
|
51
|
+
from ccai.agentkit.messagingerrors import TransportError
|
|
52
|
+
from ccai.agentkit.transport import Transport
|
|
53
|
+
from ccai.agentkit.transportconfiguration import TransportConfiguration
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class WebSocketTransport(Transport):
|
|
57
|
+
"""
|
|
58
|
+
Transport implementation using WebSocket protocol semantics
|
|
59
|
+
with persistent bidirectional connections.
|
|
60
|
+
|
|
61
|
+
Uses a WebSocket client that provides connect, send, recv,
|
|
62
|
+
and close methods. The transport constructs the target URL
|
|
63
|
+
from the TransportConfiguration host, port, and optional
|
|
64
|
+
path fields. TLS is supported via wss:// URL scheme when
|
|
65
|
+
the configuration options include ``{"tls": True}``.
|
|
66
|
+
|
|
67
|
+
The connect method establishes a persistent WebSocket
|
|
68
|
+
connection to the configured endpoint. The disconnect method
|
|
69
|
+
gracefully closes the connection. The send method serializes
|
|
70
|
+
the message with the MessageSerializer for its serialization
|
|
71
|
+
format (JSON by default) and sends it as a WebSocket text frame
|
|
72
|
+
when the serializer produces text (JSON or a text/* content
|
|
73
|
+
type) and as a binary frame otherwise. The receive method reads
|
|
74
|
+
a WebSocket frame and deserializes it to a Message with the
|
|
75
|
+
serializer for the transport's format, which is the
|
|
76
|
+
``serialization_format`` configuration option or JSON.
|
|
77
|
+
|
|
78
|
+
The connection_state property tracks the current state of
|
|
79
|
+
the WebSocket connection as one of "disconnected",
|
|
80
|
+
"connecting", "connected", or "closing".
|
|
81
|
+
|
|
82
|
+
TransportError is raised for connection loss, send failures,
|
|
83
|
+
and receive failures.
|
|
84
|
+
|
|
85
|
+
:param name: The transport name string.
|
|
86
|
+
:param configuration: The TransportConfiguration instance
|
|
87
|
+
defining WebSocket connection parameters.
|
|
88
|
+
:param credential_names: An optional list of credential
|
|
89
|
+
reference names required for authentication.
|
|
90
|
+
:param tags: An optional dictionary of tags for categorization
|
|
91
|
+
and discovery.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
VALID_CONNECTION_STATES = {"disconnected", "connecting", "connected", "closing"}
|
|
95
|
+
|
|
96
|
+
def __init__(self, name: str, configuration: TransportConfiguration,
|
|
97
|
+
credential_names: Optional[List[str]] = None,
|
|
98
|
+
tags: Optional[Dict[str, str]] = None) -> None:
|
|
99
|
+
|
|
100
|
+
super().__init__(
|
|
101
|
+
name=name,
|
|
102
|
+
protocol_type="websocket",
|
|
103
|
+
configuration=configuration,
|
|
104
|
+
credential_names=credential_names,
|
|
105
|
+
tags=tags
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
self._ws_logger = logging.getLogger(
|
|
109
|
+
"ccai.agentkit.messaging.transport.websocket"
|
|
110
|
+
)
|
|
111
|
+
self._connection_state = "disconnected"
|
|
112
|
+
self._ws_client = None
|
|
113
|
+
self._ws_url = None
|
|
114
|
+
self._ssl_context = None
|
|
115
|
+
|
|
116
|
+
return
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def connection_state(self) -> str:
|
|
120
|
+
"""
|
|
121
|
+
The current state of the WebSocket connection.
|
|
122
|
+
|
|
123
|
+
Valid states are "disconnected", "connecting", "connected",
|
|
124
|
+
and "closing".
|
|
125
|
+
|
|
126
|
+
:returns: The connection state string.
|
|
127
|
+
"""
|
|
128
|
+
return self._connection_state
|
|
129
|
+
|
|
130
|
+
def _build_ws_url(self) -> str:
|
|
131
|
+
"""
|
|
132
|
+
Constructs the WebSocket URL from the TransportConfiguration
|
|
133
|
+
host, port, and optional path fields.
|
|
134
|
+
|
|
135
|
+
The URL scheme is determined by the TLS setting in the
|
|
136
|
+
configuration options. When ``options["tls"]`` is True,
|
|
137
|
+
the scheme is "wss"; otherwise it is "ws".
|
|
138
|
+
|
|
139
|
+
:returns: The WebSocket URL string.
|
|
140
|
+
"""
|
|
141
|
+
config = self._configuration
|
|
142
|
+
options = config.options
|
|
143
|
+
|
|
144
|
+
use_tls = options.get("tls", False)
|
|
145
|
+
scheme = "wss" if use_tls else "ws"
|
|
146
|
+
|
|
147
|
+
host = config.host
|
|
148
|
+
port = config.port
|
|
149
|
+
path = config.path
|
|
150
|
+
|
|
151
|
+
ws_url = "{}://{}:{}".format(scheme, host, port)
|
|
152
|
+
|
|
153
|
+
if path is not None and len(path.strip()) > 0:
|
|
154
|
+
clean_path = path.strip()
|
|
155
|
+
if not clean_path.startswith("/"):
|
|
156
|
+
clean_path = "/" + clean_path
|
|
157
|
+
ws_url = ws_url + clean_path
|
|
158
|
+
|
|
159
|
+
return ws_url
|
|
160
|
+
|
|
161
|
+
def _build_ssl_context(self) -> Optional[ssl.SSLContext]:
|
|
162
|
+
"""
|
|
163
|
+
Builds an SSL context when TLS is enabled in the
|
|
164
|
+
configuration options.
|
|
165
|
+
|
|
166
|
+
:returns: An ssl.SSLContext instance or None if TLS is
|
|
167
|
+
not enabled.
|
|
168
|
+
"""
|
|
169
|
+
options = self._configuration.options
|
|
170
|
+
use_tls = options.get("tls", False)
|
|
171
|
+
|
|
172
|
+
if not use_tls:
|
|
173
|
+
return None
|
|
174
|
+
|
|
175
|
+
ssl_context = ssl.create_default_context()
|
|
176
|
+
|
|
177
|
+
return ssl_context
|
|
178
|
+
|
|
179
|
+
def connect(self) -> None:
|
|
180
|
+
"""
|
|
181
|
+
Establishes a persistent WebSocket connection to the
|
|
182
|
+
configured endpoint.
|
|
183
|
+
|
|
184
|
+
Constructs the ws:// or wss:// URL from the configuration,
|
|
185
|
+
builds an SSL context if TLS is enabled, and attempts to
|
|
186
|
+
connect using the ``websockets.sync.client.connect``
|
|
187
|
+
function.
|
|
188
|
+
|
|
189
|
+
Sets the connection_state to "connecting" during the
|
|
190
|
+
connection attempt, "connected" upon success, and
|
|
191
|
+
"disconnected" if the connection fails.
|
|
192
|
+
|
|
193
|
+
:raises TransportError: If the WebSocket connection cannot
|
|
194
|
+
be established.
|
|
195
|
+
"""
|
|
196
|
+
self._connection_state = "connecting"
|
|
197
|
+
|
|
198
|
+
try:
|
|
199
|
+
self._ws_url = self._build_ws_url()
|
|
200
|
+
self._ssl_context = self._build_ssl_context()
|
|
201
|
+
|
|
202
|
+
try:
|
|
203
|
+
import websockets.sync.client as ws_sync_client
|
|
204
|
+
except ImportError as import_err:
|
|
205
|
+
self._connection_state = "disconnected"
|
|
206
|
+
self._ws_logger.warning(
|
|
207
|
+
"WebSocket transport connect failed: name=%s, "
|
|
208
|
+
"error=websockets package not installed",
|
|
209
|
+
self._name
|
|
210
|
+
)
|
|
211
|
+
raise TransportError(
|
|
212
|
+
transport_name=self._name,
|
|
213
|
+
operation="connect",
|
|
214
|
+
cause=import_err
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
if self._ssl_context is not None:
|
|
218
|
+
self._ws_client = ws_sync_client.connect(
|
|
219
|
+
self._ws_url,
|
|
220
|
+
ssl=self._ssl_context
|
|
221
|
+
)
|
|
222
|
+
else:
|
|
223
|
+
self._ws_client = ws_sync_client.connect(
|
|
224
|
+
self._ws_url
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
self._connected = True
|
|
228
|
+
self._connection_state = "connected"
|
|
229
|
+
|
|
230
|
+
self._ws_logger.info(
|
|
231
|
+
"WebSocket transport connected: name=%s, endpoint=%s",
|
|
232
|
+
self._name, self._ws_url
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
except TransportError:
|
|
236
|
+
raise
|
|
237
|
+
|
|
238
|
+
except Exception as ex:
|
|
239
|
+
self._connection_state = "disconnected"
|
|
240
|
+
self._connected = False
|
|
241
|
+
self._ws_client = None
|
|
242
|
+
|
|
243
|
+
self._ws_logger.warning(
|
|
244
|
+
"WebSocket transport connect failed: name=%s, "
|
|
245
|
+
"endpoint=%s, error=%s",
|
|
246
|
+
self._name, self._ws_url, ex
|
|
247
|
+
)
|
|
248
|
+
raise TransportError(
|
|
249
|
+
transport_name=self._name,
|
|
250
|
+
operation="connect",
|
|
251
|
+
cause=ex
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
return
|
|
255
|
+
|
|
256
|
+
def disconnect(self) -> None:
|
|
257
|
+
"""
|
|
258
|
+
Gracefully closes the WebSocket connection.
|
|
259
|
+
|
|
260
|
+
Sets the connection_state to "closing" during the close
|
|
261
|
+
operation and "disconnected" upon completion.
|
|
262
|
+
|
|
263
|
+
:raises TransportError: If the disconnection fails.
|
|
264
|
+
"""
|
|
265
|
+
self._connection_state = "closing"
|
|
266
|
+
|
|
267
|
+
try:
|
|
268
|
+
if self._ws_client is not None:
|
|
269
|
+
self._ws_client.close()
|
|
270
|
+
self._ws_client = None
|
|
271
|
+
|
|
272
|
+
self._connected = False
|
|
273
|
+
self._connection_state = "disconnected"
|
|
274
|
+
self._ssl_context = None
|
|
275
|
+
|
|
276
|
+
self._ws_logger.info(
|
|
277
|
+
"WebSocket transport disconnected: name=%s",
|
|
278
|
+
self._name
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
except Exception as ex:
|
|
282
|
+
self._connection_state = "disconnected"
|
|
283
|
+
self._connected = False
|
|
284
|
+
self._ws_client = None
|
|
285
|
+
|
|
286
|
+
self._ws_logger.warning(
|
|
287
|
+
"WebSocket transport disconnect failed: name=%s, error=%s",
|
|
288
|
+
self._name, ex
|
|
289
|
+
)
|
|
290
|
+
raise TransportError(
|
|
291
|
+
transport_name=self._name,
|
|
292
|
+
operation="disconnect",
|
|
293
|
+
cause=ex
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
return
|
|
297
|
+
|
|
298
|
+
def send(self, message: Message) -> MessageEnvelope:
|
|
299
|
+
"""
|
|
300
|
+
Sends a Message as a WebSocket frame.
|
|
301
|
+
|
|
302
|
+
The message dictionary from its as_dict method is serialized
|
|
303
|
+
with the MessageSerializer that resolve_serializer returns
|
|
304
|
+
for the message's serialization_format (JSON by default).
|
|
305
|
+
Textual formats (JSON or a text/* content type) are sent as
|
|
306
|
+
a text frame and other formats as a binary frame over the
|
|
307
|
+
persistent WebSocket connection.
|
|
308
|
+
|
|
309
|
+
:param message: The Message to send.
|
|
310
|
+
|
|
311
|
+
:returns: A MessageEnvelope with delivery metadata.
|
|
312
|
+
|
|
313
|
+
:raises TransportError: If the transport is not connected,
|
|
314
|
+
if the message cannot be serialized in its serialization
|
|
315
|
+
format, if the WebSocket connection is lost, or if the
|
|
316
|
+
send operation fails.
|
|
317
|
+
"""
|
|
318
|
+
if not self._connected or self._ws_client is None:
|
|
319
|
+
errmsg = "Transport '{}' is not connected.".format(self._name)
|
|
320
|
+
self._ws_logger.warning(
|
|
321
|
+
"WebSocket transport send failed: name=%s, "
|
|
322
|
+
"reason=not connected",
|
|
323
|
+
self._name
|
|
324
|
+
)
|
|
325
|
+
raise TransportError(
|
|
326
|
+
transport_name=self._name,
|
|
327
|
+
operation="send",
|
|
328
|
+
cause=ConnectionError(errmsg)
|
|
329
|
+
)
|
|
330
|
+
|
|
331
|
+
# Encode the message before touching the connection so that a message
|
|
332
|
+
# that cannot be serialized is not reported as a connection loss.
|
|
333
|
+
try:
|
|
334
|
+
serializer = self.resolve_serializer(message.serialization_format)
|
|
335
|
+
|
|
336
|
+
message_data = message.as_dict()
|
|
337
|
+
serialized_data = serializer.serialize(message_data)
|
|
338
|
+
|
|
339
|
+
content_type = serializer.content_type
|
|
340
|
+
is_text_format = content_type == "application/json" or content_type.startswith("text/")
|
|
341
|
+
|
|
342
|
+
if is_text_format is True:
|
|
343
|
+
frame_data = serialized_data.decode("utf-8")
|
|
344
|
+
else:
|
|
345
|
+
frame_data = serialized_data
|
|
346
|
+
|
|
347
|
+
except Exception as ser_err:
|
|
348
|
+
self._ws_logger.warning(
|
|
349
|
+
"WebSocket transport send failed (serialization): "
|
|
350
|
+
"name=%s, message_id=%s, format=%s, error=%s",
|
|
351
|
+
self._name, message.identifier,
|
|
352
|
+
message.serialization_format, ser_err
|
|
353
|
+
)
|
|
354
|
+
raise TransportError(
|
|
355
|
+
transport_name=self._name,
|
|
356
|
+
operation="send",
|
|
357
|
+
cause=ser_err
|
|
358
|
+
)
|
|
359
|
+
|
|
360
|
+
try:
|
|
361
|
+
self._ws_client.send(frame_data)
|
|
362
|
+
|
|
363
|
+
self._ws_logger.info(
|
|
364
|
+
"WebSocket transport message sent: name=%s, "
|
|
365
|
+
"message_id=%s, sender=%s, recipient=%s, pattern=%s",
|
|
366
|
+
self._name, message.identifier,
|
|
367
|
+
message.sender.agent_identifier,
|
|
368
|
+
message.recipient.agent_identifier,
|
|
369
|
+
message.pattern.value
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
envelope = MessageEnvelope(
|
|
373
|
+
message=message,
|
|
374
|
+
transport_name=self._name,
|
|
375
|
+
attempt_count=1,
|
|
376
|
+
delivery_status="delivered"
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
return envelope
|
|
380
|
+
|
|
381
|
+
except Exception as ex:
|
|
382
|
+
self._connection_state = "disconnected"
|
|
383
|
+
self._connected = False
|
|
384
|
+
|
|
385
|
+
self._ws_logger.warning(
|
|
386
|
+
"WebSocket transport send failed (connection loss): "
|
|
387
|
+
"name=%s, message_id=%s, endpoint=%s, error=%s",
|
|
388
|
+
self._name, message.identifier, self._ws_url, ex
|
|
389
|
+
)
|
|
390
|
+
raise TransportError(
|
|
391
|
+
transport_name=self._name,
|
|
392
|
+
operation="send",
|
|
393
|
+
cause=ex
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
def receive(self) -> Message:
|
|
397
|
+
"""
|
|
398
|
+
Receives a Message from the WebSocket connection.
|
|
399
|
+
|
|
400
|
+
Reads a frame from the persistent WebSocket connection and
|
|
401
|
+
deserializes it into a Message instance with the
|
|
402
|
+
MessageSerializer for the transport's format, which is the
|
|
403
|
+
``serialization_format`` configuration option or JSON.
|
|
404
|
+
|
|
405
|
+
:returns: The received Message.
|
|
406
|
+
|
|
407
|
+
:raises TransportError: If the transport is not connected,
|
|
408
|
+
if the WebSocket connection is lost, or if the received
|
|
409
|
+
data cannot be deserialized.
|
|
410
|
+
"""
|
|
411
|
+
if not self._connected or self._ws_client is None:
|
|
412
|
+
errmsg = "Transport '{}' is not connected.".format(self._name)
|
|
413
|
+
self._ws_logger.warning(
|
|
414
|
+
"WebSocket transport receive failed: name=%s, "
|
|
415
|
+
"reason=not connected",
|
|
416
|
+
self._name
|
|
417
|
+
)
|
|
418
|
+
raise TransportError(
|
|
419
|
+
transport_name=self._name,
|
|
420
|
+
operation="receive",
|
|
421
|
+
cause=ConnectionError(errmsg)
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
try:
|
|
425
|
+
raw_data = self._ws_client.recv()
|
|
426
|
+
|
|
427
|
+
serializer = self.resolve_serializer(direction="deserialize")
|
|
428
|
+
|
|
429
|
+
# Text frames arrive as str and binary frames as bytes.
|
|
430
|
+
if isinstance(raw_data, str):
|
|
431
|
+
raw_bytes = raw_data.encode("utf-8")
|
|
432
|
+
else:
|
|
433
|
+
raw_bytes = raw_data
|
|
434
|
+
|
|
435
|
+
message_data = serializer.deserialize(raw_bytes)
|
|
436
|
+
message = Message.from_dict(message_data)
|
|
437
|
+
|
|
438
|
+
self._ws_logger.info(
|
|
439
|
+
"WebSocket transport message received: name=%s, "
|
|
440
|
+
"message_id=%s, sender=%s, pattern=%s",
|
|
441
|
+
self._name, message.identifier,
|
|
442
|
+
message.sender.agent_identifier,
|
|
443
|
+
message.pattern.value
|
|
444
|
+
)
|
|
445
|
+
|
|
446
|
+
return message
|
|
447
|
+
|
|
448
|
+
except (MessageSerializationError, KeyError, TypeError, ValueError) as parse_err:
|
|
449
|
+
self._ws_logger.warning(
|
|
450
|
+
"WebSocket transport receive failed (deserialization): "
|
|
451
|
+
"name=%s, error=%s",
|
|
452
|
+
self._name, parse_err
|
|
453
|
+
)
|
|
454
|
+
raise TransportError(
|
|
455
|
+
transport_name=self._name,
|
|
456
|
+
operation="receive",
|
|
457
|
+
cause=parse_err
|
|
458
|
+
)
|
|
459
|
+
|
|
460
|
+
except Exception as ex:
|
|
461
|
+
self._connection_state = "disconnected"
|
|
462
|
+
self._connected = False
|
|
463
|
+
|
|
464
|
+
self._ws_logger.warning(
|
|
465
|
+
"WebSocket transport receive failed (connection loss): "
|
|
466
|
+
"name=%s, endpoint=%s, error=%s",
|
|
467
|
+
self._name, self._ws_url, ex
|
|
468
|
+
)
|
|
469
|
+
raise TransportError(
|
|
470
|
+
transport_name=self._name,
|
|
471
|
+
operation="receive",
|
|
472
|
+
cause=ex
|
|
473
|
+
)
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the WorkflowCheckpoint class representing an immutable
|
|
3
|
+
snapshot of workflow execution state at a specific point in the
|
|
4
|
+
Workflow & Orchestration feature.
|
|
5
|
+
|
|
6
|
+
A WorkflowCheckpoint contains the workflow name, execution
|
|
7
|
+
identifier, the set of completed step identifiers, the serialized
|
|
8
|
+
WorkflowContext state, the current step identifier, and a
|
|
9
|
+
timestamp. The class provides JSON serialization and
|
|
10
|
+
deserialization for persistence and reconstruction.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import json
|
|
14
|
+
|
|
15
|
+
from datetime import datetime, timezone
|
|
16
|
+
from typing import Optional
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class WorkflowCheckpoint:
|
|
20
|
+
"""
|
|
21
|
+
Immutable snapshot of workflow execution state at a specific
|
|
22
|
+
point.
|
|
23
|
+
|
|
24
|
+
A WorkflowCheckpoint captures the workflow name, execution
|
|
25
|
+
identifier, the set of completed step identifiers, the
|
|
26
|
+
serialized WorkflowContext state as a dictionary, the current
|
|
27
|
+
step identifier, and a UTC timestamp. All fields are exposed
|
|
28
|
+
as read-only properties. The timestamp defaults to the
|
|
29
|
+
current UTC time when not provided.
|
|
30
|
+
|
|
31
|
+
:param workflow_name: The name of the workflow being
|
|
32
|
+
checkpointed.
|
|
33
|
+
:param execution_id: The unique identifier of the workflow
|
|
34
|
+
execution.
|
|
35
|
+
:param completed_step_ids: A set of step identifiers that
|
|
36
|
+
have completed execution.
|
|
37
|
+
:param context_state: A dictionary representing the
|
|
38
|
+
serialized WorkflowContext state.
|
|
39
|
+
:param current_step_id: The identifier of the step that
|
|
40
|
+
was executing or about to execute when the checkpoint
|
|
41
|
+
was taken.
|
|
42
|
+
:param timestamp: An optional UTC datetime for the
|
|
43
|
+
checkpoint. Defaults to the current UTC time.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def __init__(self, workflow_name: str, execution_id: str,
|
|
47
|
+
completed_step_ids: set, context_state: dict,
|
|
48
|
+
current_step_id: str,
|
|
49
|
+
timestamp: Optional[datetime] = None) -> None:
|
|
50
|
+
self._workflow_name = workflow_name
|
|
51
|
+
self._execution_id = execution_id
|
|
52
|
+
self._completed_step_ids = set(completed_step_ids)
|
|
53
|
+
self._context_state = dict(context_state)
|
|
54
|
+
self._current_step_id = current_step_id
|
|
55
|
+
self._timestamp = timestamp if timestamp is not None else datetime.now(timezone.utc)
|
|
56
|
+
return
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def workflow_name(self) -> str:
|
|
60
|
+
"""
|
|
61
|
+
The name of the workflow being checkpointed.
|
|
62
|
+
|
|
63
|
+
:returns: The workflow name string.
|
|
64
|
+
"""
|
|
65
|
+
return self._workflow_name
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def execution_id(self) -> str:
|
|
69
|
+
"""
|
|
70
|
+
The unique identifier of the workflow execution.
|
|
71
|
+
|
|
72
|
+
:returns: The execution identifier string.
|
|
73
|
+
"""
|
|
74
|
+
return self._execution_id
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def completed_step_ids(self) -> set:
|
|
78
|
+
"""
|
|
79
|
+
The set of step identifiers that have completed execution.
|
|
80
|
+
|
|
81
|
+
Returns a defensive copy to preserve immutability.
|
|
82
|
+
|
|
83
|
+
:returns: A set of step identifier strings.
|
|
84
|
+
"""
|
|
85
|
+
rtnval = set(self._completed_step_ids)
|
|
86
|
+
return rtnval
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def context_state(self) -> dict:
|
|
90
|
+
"""
|
|
91
|
+
The serialized WorkflowContext state as a dictionary.
|
|
92
|
+
|
|
93
|
+
Returns a defensive copy to preserve immutability.
|
|
94
|
+
|
|
95
|
+
:returns: A dictionary representing the context state.
|
|
96
|
+
"""
|
|
97
|
+
rtnval = dict(self._context_state)
|
|
98
|
+
return rtnval
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def current_step_id(self) -> str:
|
|
102
|
+
"""
|
|
103
|
+
The identifier of the step that was executing or about
|
|
104
|
+
to execute when the checkpoint was taken.
|
|
105
|
+
|
|
106
|
+
:returns: The current step identifier string.
|
|
107
|
+
"""
|
|
108
|
+
return self._current_step_id
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def timestamp(self) -> datetime:
|
|
112
|
+
"""
|
|
113
|
+
The UTC timestamp when the checkpoint was taken.
|
|
114
|
+
|
|
115
|
+
:returns: A datetime object in UTC.
|
|
116
|
+
"""
|
|
117
|
+
return self._timestamp
|
|
118
|
+
|
|
119
|
+
def as_dict(self) -> dict:
|
|
120
|
+
"""
|
|
121
|
+
Returns a dictionary representation of the checkpoint
|
|
122
|
+
fields.
|
|
123
|
+
|
|
124
|
+
The completed_step_ids set is serialized as a sorted
|
|
125
|
+
list for deterministic JSON output. The timestamp is
|
|
126
|
+
serialized as an ISO format string.
|
|
127
|
+
|
|
128
|
+
:returns: A dictionary containing all checkpoint fields.
|
|
129
|
+
"""
|
|
130
|
+
rtnval = {
|
|
131
|
+
"workflow_name": self._workflow_name,
|
|
132
|
+
"execution_id": self._execution_id,
|
|
133
|
+
"completed_step_ids": sorted(self._completed_step_ids),
|
|
134
|
+
"context_state": self._context_state,
|
|
135
|
+
"current_step_id": self._current_step_id,
|
|
136
|
+
"timestamp": self._timestamp.isoformat()
|
|
137
|
+
}
|
|
138
|
+
return rtnval
|
|
139
|
+
|
|
140
|
+
def to_json(self) -> str:
|
|
141
|
+
"""
|
|
142
|
+
Returns a JSON string representation of the checkpoint
|
|
143
|
+
fields.
|
|
144
|
+
|
|
145
|
+
:returns: A JSON string containing all checkpoint fields.
|
|
146
|
+
"""
|
|
147
|
+
data = self.as_dict()
|
|
148
|
+
rtnval = json.dumps(data)
|
|
149
|
+
return rtnval
|
|
150
|
+
|
|
151
|
+
@classmethod
|
|
152
|
+
def from_json(cls, json_str: str) -> "WorkflowCheckpoint":
|
|
153
|
+
"""
|
|
154
|
+
Deserializes a JSON string into a WorkflowCheckpoint
|
|
155
|
+
instance.
|
|
156
|
+
|
|
157
|
+
The completed_step_ids list is converted back to a set.
|
|
158
|
+
The timestamp is parsed from an ISO format string.
|
|
159
|
+
|
|
160
|
+
:param json_str: A JSON string containing checkpoint
|
|
161
|
+
fields.
|
|
162
|
+
|
|
163
|
+
:returns: A WorkflowCheckpoint instance reconstructed
|
|
164
|
+
from the JSON data.
|
|
165
|
+
"""
|
|
166
|
+
data = json.loads(json_str)
|
|
167
|
+
|
|
168
|
+
completed_step_ids = set(data["completed_step_ids"])
|
|
169
|
+
timestamp = datetime.fromisoformat(data["timestamp"])
|
|
170
|
+
|
|
171
|
+
rtnval = cls(
|
|
172
|
+
workflow_name=data["workflow_name"],
|
|
173
|
+
execution_id=data["execution_id"],
|
|
174
|
+
completed_step_ids=completed_step_ids,
|
|
175
|
+
context_state=data["context_state"],
|
|
176
|
+
current_step_id=data["current_step_id"],
|
|
177
|
+
timestamp=timestamp
|
|
178
|
+
)
|
|
179
|
+
return rtnval
|
|
180
|
+
|
|
181
|
+
def __repr__(self) -> str:
|
|
182
|
+
"""
|
|
183
|
+
Returns a detailed string representation including the
|
|
184
|
+
class name, workflow name, execution identifier, and the
|
|
185
|
+
count of completed steps.
|
|
186
|
+
|
|
187
|
+
:returns: A repr string for debugging.
|
|
188
|
+
"""
|
|
189
|
+
completed_count = len(self._completed_step_ids)
|
|
190
|
+
repr_str = "{}(workflow_name={!r}, execution_id={!r}, completed_steps={})".format(
|
|
191
|
+
self.__class__.__name__, self._workflow_name,
|
|
192
|
+
self._execution_id, completed_count
|
|
193
|
+
)
|
|
194
|
+
return repr_str
|
|
195
|
+
|
|
196
|
+
def __str__(self) -> str:
|
|
197
|
+
"""
|
|
198
|
+
Returns a human-readable summary including the workflow
|
|
199
|
+
name, execution identifier, and timestamp.
|
|
200
|
+
|
|
201
|
+
:returns: A human-readable string describing the
|
|
202
|
+
checkpoint.
|
|
203
|
+
"""
|
|
204
|
+
str_val = "WorkflowCheckpoint: {} [execution: {}, timestamp: {}]".format(
|
|
205
|
+
self._workflow_name, self._execution_id,
|
|
206
|
+
self._timestamp.isoformat()
|
|
207
|
+
)
|
|
208
|
+
return str_val
|