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,158 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the CircuitBreakerConfiguration class for configuring circuit
|
|
3
|
+
breaker thresholds and timing in the Error Handling & Resilience feature.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
|
|
8
|
+
from typing import List
|
|
9
|
+
from typing import Optional
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CircuitBreakerConfiguration:
|
|
13
|
+
"""
|
|
14
|
+
Immutable configuration object defining the thresholds and timing
|
|
15
|
+
for a CircuitBreaker.
|
|
16
|
+
|
|
17
|
+
:param failure_threshold: Number of failures before opening the circuit.
|
|
18
|
+
:param success_threshold: Number of successes in half-open before closing.
|
|
19
|
+
:param open_state_timeout: Seconds to wait in open state before half-open.
|
|
20
|
+
:param half_open_max_attempts: Max attempts allowed in half-open state.
|
|
21
|
+
:param monitored_exception_types: Optional list of exception types to monitor.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, failure_threshold: int, success_threshold: int,
|
|
25
|
+
open_state_timeout: float, half_open_max_attempts: int,
|
|
26
|
+
monitored_exception_types: Optional[List[type]] = None) -> None:
|
|
27
|
+
self._failure_threshold = failure_threshold
|
|
28
|
+
self._success_threshold = success_threshold
|
|
29
|
+
self._open_state_timeout = open_state_timeout
|
|
30
|
+
self._half_open_max_attempts = half_open_max_attempts
|
|
31
|
+
self._monitored_exception_types = list(monitored_exception_types) if monitored_exception_types is not None else []
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def failure_threshold(self) -> int:
|
|
36
|
+
"""
|
|
37
|
+
Number of failures before opening the circuit.
|
|
38
|
+
|
|
39
|
+
:returns: The failure threshold as an integer.
|
|
40
|
+
"""
|
|
41
|
+
return self._failure_threshold
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def success_threshold(self) -> int:
|
|
45
|
+
"""
|
|
46
|
+
Number of successes in half-open before closing.
|
|
47
|
+
|
|
48
|
+
:returns: The success threshold as an integer.
|
|
49
|
+
"""
|
|
50
|
+
return self._success_threshold
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def open_state_timeout(self) -> float:
|
|
54
|
+
"""
|
|
55
|
+
Seconds to wait in open state before transitioning to half-open.
|
|
56
|
+
|
|
57
|
+
:returns: The timeout as a float.
|
|
58
|
+
"""
|
|
59
|
+
return self._open_state_timeout
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def half_open_max_attempts(self) -> int:
|
|
63
|
+
"""
|
|
64
|
+
Maximum attempts allowed in half-open state.
|
|
65
|
+
|
|
66
|
+
:returns: The max attempts as an integer.
|
|
67
|
+
"""
|
|
68
|
+
return self._half_open_max_attempts
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def monitored_exception_types(self) -> list:
|
|
72
|
+
"""
|
|
73
|
+
List of exception types to monitor.
|
|
74
|
+
|
|
75
|
+
:returns: A list of exception types.
|
|
76
|
+
"""
|
|
77
|
+
return list(self._monitored_exception_types)
|
|
78
|
+
|
|
79
|
+
def as_dict(self) -> dict:
|
|
80
|
+
"""
|
|
81
|
+
Returns a dictionary representation of the configuration fields.
|
|
82
|
+
|
|
83
|
+
:returns: A dictionary of configuration fields.
|
|
84
|
+
"""
|
|
85
|
+
result = {
|
|
86
|
+
"failure_threshold": self._failure_threshold,
|
|
87
|
+
"success_threshold": self._success_threshold,
|
|
88
|
+
"open_state_timeout": self._open_state_timeout,
|
|
89
|
+
"half_open_max_attempts": self._half_open_max_attempts
|
|
90
|
+
}
|
|
91
|
+
return result
|
|
92
|
+
|
|
93
|
+
def to_json(self) -> str:
|
|
94
|
+
"""
|
|
95
|
+
Returns a JSON string representation of the configuration fields.
|
|
96
|
+
|
|
97
|
+
:returns: A JSON string.
|
|
98
|
+
"""
|
|
99
|
+
data = self.as_dict()
|
|
100
|
+
json_str = json.dumps(data)
|
|
101
|
+
return json_str
|
|
102
|
+
|
|
103
|
+
@classmethod
|
|
104
|
+
def from_dict(cls, data: dict) -> "CircuitBreakerConfiguration":
|
|
105
|
+
"""
|
|
106
|
+
Constructs a CircuitBreakerConfiguration from a dictionary.
|
|
107
|
+
|
|
108
|
+
:param data: Dictionary containing configuration fields.
|
|
109
|
+
|
|
110
|
+
:returns: A CircuitBreakerConfiguration instance.
|
|
111
|
+
"""
|
|
112
|
+
config = cls(
|
|
113
|
+
failure_threshold=data["failure_threshold"],
|
|
114
|
+
success_threshold=data["success_threshold"],
|
|
115
|
+
open_state_timeout=data["open_state_timeout"],
|
|
116
|
+
half_open_max_attempts=data["half_open_max_attempts"]
|
|
117
|
+
)
|
|
118
|
+
return config
|
|
119
|
+
|
|
120
|
+
@classmethod
|
|
121
|
+
def from_json(cls, json_str: str) -> "CircuitBreakerConfiguration":
|
|
122
|
+
"""
|
|
123
|
+
Deserializes a JSON string into a CircuitBreakerConfiguration.
|
|
124
|
+
|
|
125
|
+
:param json_str: JSON string to deserialize.
|
|
126
|
+
|
|
127
|
+
:returns: A CircuitBreakerConfiguration instance.
|
|
128
|
+
"""
|
|
129
|
+
data = json.loads(json_str)
|
|
130
|
+
config = cls.from_dict(data)
|
|
131
|
+
return config
|
|
132
|
+
|
|
133
|
+
def __repr__(self) -> str:
|
|
134
|
+
"""
|
|
135
|
+
Returns a detailed string representation including class name,
|
|
136
|
+
failure threshold, and open state timeout.
|
|
137
|
+
|
|
138
|
+
:returns: A repr string for debugging.
|
|
139
|
+
"""
|
|
140
|
+
repr_str = "CircuitBreakerConfiguration(failure_threshold={}, open_state_timeout={})".format(
|
|
141
|
+
self._failure_threshold, self._open_state_timeout
|
|
142
|
+
)
|
|
143
|
+
return repr_str
|
|
144
|
+
|
|
145
|
+
def __str__(self) -> str:
|
|
146
|
+
"""
|
|
147
|
+
Returns a human-readable summary of the circuit breaker configuration.
|
|
148
|
+
|
|
149
|
+
:returns: A human-readable string.
|
|
150
|
+
"""
|
|
151
|
+
str_val = (
|
|
152
|
+
"CircuitBreakerConfiguration: failure_threshold={}, success_threshold={}, "
|
|
153
|
+
"open_state_timeout={}s, half_open_max_attempts={}"
|
|
154
|
+
).format(
|
|
155
|
+
self._failure_threshold, self._success_threshold,
|
|
156
|
+
self._open_state_timeout, self._half_open_max_attempts
|
|
157
|
+
)
|
|
158
|
+
return str_val
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the CircuitBreakerEvent class representing a state transition
|
|
3
|
+
in a CircuitBreaker in the Error Handling & Resilience feature.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
|
|
10
|
+
from ccai.agentkit.circuitbreakerstate import CircuitBreakerState
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class CircuitBreakerEvent:
|
|
14
|
+
"""
|
|
15
|
+
Immutable object representing a state transition or significant event
|
|
16
|
+
in a CircuitBreaker.
|
|
17
|
+
|
|
18
|
+
:param name: The circuit breaker name.
|
|
19
|
+
:param previous_state: The state before the transition.
|
|
20
|
+
:param new_state: The state after the transition.
|
|
21
|
+
:param reason: The reason for the transition.
|
|
22
|
+
:param timestamp: The datetime of the event. Defaults to UTC now.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, name: str, previous_state: CircuitBreakerState,
|
|
26
|
+
new_state: CircuitBreakerState, reason: str,
|
|
27
|
+
timestamp: datetime = None) -> None:
|
|
28
|
+
self._name = name
|
|
29
|
+
self._previous_state = previous_state
|
|
30
|
+
self._new_state = new_state
|
|
31
|
+
self._reason = reason
|
|
32
|
+
self._timestamp = timestamp if timestamp is not None else datetime.utcnow()
|
|
33
|
+
return
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def name(self) -> str:
|
|
37
|
+
"""
|
|
38
|
+
The circuit breaker name.
|
|
39
|
+
|
|
40
|
+
:returns: The name string.
|
|
41
|
+
"""
|
|
42
|
+
return self._name
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def previous_state(self) -> CircuitBreakerState:
|
|
46
|
+
"""
|
|
47
|
+
The state before the transition.
|
|
48
|
+
|
|
49
|
+
:returns: The previous CircuitBreakerState.
|
|
50
|
+
"""
|
|
51
|
+
return self._previous_state
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def new_state(self) -> CircuitBreakerState:
|
|
55
|
+
"""
|
|
56
|
+
The state after the transition.
|
|
57
|
+
|
|
58
|
+
:returns: The new CircuitBreakerState.
|
|
59
|
+
"""
|
|
60
|
+
return self._new_state
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def reason(self) -> str:
|
|
64
|
+
"""
|
|
65
|
+
The reason for the transition.
|
|
66
|
+
|
|
67
|
+
:returns: The reason string.
|
|
68
|
+
"""
|
|
69
|
+
return self._reason
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def timestamp(self) -> datetime:
|
|
73
|
+
"""
|
|
74
|
+
The datetime of the event.
|
|
75
|
+
|
|
76
|
+
:returns: The timestamp as a datetime object.
|
|
77
|
+
"""
|
|
78
|
+
return self._timestamp
|
|
79
|
+
|
|
80
|
+
def as_dict(self) -> dict:
|
|
81
|
+
"""
|
|
82
|
+
Returns a dictionary representation of the event fields.
|
|
83
|
+
|
|
84
|
+
:returns: A dictionary of event fields.
|
|
85
|
+
"""
|
|
86
|
+
result = {
|
|
87
|
+
"name": self._name,
|
|
88
|
+
"previous_state": self._previous_state.value,
|
|
89
|
+
"new_state": self._new_state.value,
|
|
90
|
+
"reason": self._reason,
|
|
91
|
+
"timestamp": self._timestamp.isoformat()
|
|
92
|
+
}
|
|
93
|
+
return result
|
|
94
|
+
|
|
95
|
+
def to_json(self) -> str:
|
|
96
|
+
"""
|
|
97
|
+
Returns a JSON string representation of the event fields.
|
|
98
|
+
|
|
99
|
+
:returns: A JSON string.
|
|
100
|
+
"""
|
|
101
|
+
data = self.as_dict()
|
|
102
|
+
json_str = json.dumps(data)
|
|
103
|
+
return json_str
|
|
104
|
+
|
|
105
|
+
@classmethod
|
|
106
|
+
def from_dict(cls, data: dict) -> "CircuitBreakerEvent":
|
|
107
|
+
"""
|
|
108
|
+
Constructs a CircuitBreakerEvent from a dictionary.
|
|
109
|
+
|
|
110
|
+
:param data: Dictionary containing event fields.
|
|
111
|
+
|
|
112
|
+
:returns: A CircuitBreakerEvent instance.
|
|
113
|
+
"""
|
|
114
|
+
event = cls(
|
|
115
|
+
name=data["name"],
|
|
116
|
+
previous_state=CircuitBreakerState.from_string(data["previous_state"]),
|
|
117
|
+
new_state=CircuitBreakerState.from_string(data["new_state"]),
|
|
118
|
+
reason=data["reason"],
|
|
119
|
+
timestamp=datetime.fromisoformat(data["timestamp"])
|
|
120
|
+
)
|
|
121
|
+
return event
|
|
122
|
+
|
|
123
|
+
@classmethod
|
|
124
|
+
def from_json(cls, json_str: str) -> "CircuitBreakerEvent":
|
|
125
|
+
"""
|
|
126
|
+
Deserializes a JSON string into a CircuitBreakerEvent instance.
|
|
127
|
+
|
|
128
|
+
:param json_str: JSON string to deserialize.
|
|
129
|
+
|
|
130
|
+
:returns: A CircuitBreakerEvent instance.
|
|
131
|
+
"""
|
|
132
|
+
data = json.loads(json_str)
|
|
133
|
+
event = cls.from_dict(data)
|
|
134
|
+
return event
|
|
135
|
+
|
|
136
|
+
def __repr__(self) -> str:
|
|
137
|
+
"""
|
|
138
|
+
Returns a detailed string representation including class name,
|
|
139
|
+
circuit breaker name, previous state, and new state.
|
|
140
|
+
|
|
141
|
+
:returns: A repr string for debugging.
|
|
142
|
+
"""
|
|
143
|
+
repr_str = "CircuitBreakerEvent(name={!r}, {} -> {})".format(
|
|
144
|
+
self._name, self._previous_state.value, self._new_state.value
|
|
145
|
+
)
|
|
146
|
+
return repr_str
|
|
147
|
+
|
|
148
|
+
def __str__(self) -> str:
|
|
149
|
+
"""
|
|
150
|
+
Returns a human-readable summary including the circuit breaker name,
|
|
151
|
+
state transition, and reason.
|
|
152
|
+
|
|
153
|
+
:returns: A human-readable string.
|
|
154
|
+
"""
|
|
155
|
+
str_val = "CircuitBreakerEvent name='{}', {} -> {}, reason='{}'".format(
|
|
156
|
+
self._name, self._previous_state.value, self._new_state.value, self._reason
|
|
157
|
+
)
|
|
158
|
+
return str_val
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the CircuitBreakerHealthCheck for checking CircuitBreaker state.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from ccai.agentkit.circuitbreakerstate import CircuitBreakerState
|
|
6
|
+
from ccai.agentkit.healthcheck import HealthCheck
|
|
7
|
+
from ccai.agentkit.healthstatus import HealthStatus
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class CircuitBreakerHealthCheck(HealthCheck):
|
|
11
|
+
"""
|
|
12
|
+
Checks CircuitBreaker state. Returns "degraded" when OPEN,
|
|
13
|
+
"healthy" when CLOSED.
|
|
14
|
+
|
|
15
|
+
:param name: The health check name.
|
|
16
|
+
:param circuit_breaker: The CircuitBreaker instance to check.
|
|
17
|
+
:param check_interval: The interval between checks in seconds.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self, name: str, circuit_breaker, check_interval: float = 30.0) -> None:
|
|
21
|
+
super().__init__(name, check_interval)
|
|
22
|
+
self._circuit_breaker = circuit_breaker
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
def check(self) -> HealthStatus:
|
|
26
|
+
state = self._circuit_breaker.state
|
|
27
|
+
if state == CircuitBreakerState.OPEN:
|
|
28
|
+
return HealthStatus(self._name, "degraded", detail="circuit breaker is OPEN")
|
|
29
|
+
elif state == CircuitBreakerState.HALF_OPEN:
|
|
30
|
+
return HealthStatus(self._name, "degraded", detail="circuit breaker is HALF_OPEN")
|
|
31
|
+
else:
|
|
32
|
+
return HealthStatus(self._name, "healthy")
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the CircuitBreakerRegistry class for tracking all CircuitBreaker
|
|
3
|
+
instances for an Agent in the Error Handling & Resilience feature.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import List
|
|
7
|
+
from typing import Optional
|
|
8
|
+
|
|
9
|
+
from ccai.agentkit.circuitbreaker import CircuitBreaker
|
|
10
|
+
from ccai.agentkit.errors import SemanticError
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class CircuitBreakerRegistry:
|
|
14
|
+
"""
|
|
15
|
+
Registry tracking all CircuitBreaker instances for an Agent.
|
|
16
|
+
Provides lookup by name and enumeration.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(self) -> None:
|
|
20
|
+
self._circuit_breakers = {}
|
|
21
|
+
return
|
|
22
|
+
|
|
23
|
+
def register(self, circuit_breaker: CircuitBreaker) -> None:
|
|
24
|
+
"""
|
|
25
|
+
Registers a CircuitBreaker using its name as the key.
|
|
26
|
+
|
|
27
|
+
:param circuit_breaker: The CircuitBreaker to register.
|
|
28
|
+
|
|
29
|
+
:raises SemanticError: If a circuit breaker with the same name exists.
|
|
30
|
+
"""
|
|
31
|
+
name = circuit_breaker.name
|
|
32
|
+
|
|
33
|
+
if name in self._circuit_breakers:
|
|
34
|
+
errmsg = "CircuitBreaker with name '{}' is already registered.".format(name)
|
|
35
|
+
raise SemanticError(errmsg)
|
|
36
|
+
|
|
37
|
+
self._circuit_breakers[name] = circuit_breaker
|
|
38
|
+
return
|
|
39
|
+
|
|
40
|
+
def deregister(self, name: str) -> None:
|
|
41
|
+
"""
|
|
42
|
+
Removes a CircuitBreaker by name.
|
|
43
|
+
|
|
44
|
+
:param name: The name of the circuit breaker to remove.
|
|
45
|
+
"""
|
|
46
|
+
self._circuit_breakers.pop(name, None)
|
|
47
|
+
return
|
|
48
|
+
|
|
49
|
+
def lookup(self, name: str) -> Optional[CircuitBreaker]:
|
|
50
|
+
"""
|
|
51
|
+
Looks up a registered CircuitBreaker by name.
|
|
52
|
+
|
|
53
|
+
:param name: The name to look up.
|
|
54
|
+
|
|
55
|
+
:returns: The CircuitBreaker instance or None.
|
|
56
|
+
"""
|
|
57
|
+
return self._circuit_breakers.get(name)
|
|
58
|
+
|
|
59
|
+
def list_all(self) -> List[CircuitBreaker]:
|
|
60
|
+
"""
|
|
61
|
+
Returns all registered CircuitBreaker instances.
|
|
62
|
+
|
|
63
|
+
:returns: A list of CircuitBreaker instances.
|
|
64
|
+
"""
|
|
65
|
+
return list(self._circuit_breakers.values())
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def count(self) -> int:
|
|
69
|
+
"""
|
|
70
|
+
The count of currently registered circuit breakers.
|
|
71
|
+
|
|
72
|
+
:returns: The count as an integer.
|
|
73
|
+
"""
|
|
74
|
+
return len(self._circuit_breakers)
|
|
75
|
+
|
|
76
|
+
def has_circuit_breaker(self, name: str) -> bool:
|
|
77
|
+
"""
|
|
78
|
+
Checks whether a CircuitBreaker with the given name is registered.
|
|
79
|
+
|
|
80
|
+
:param name: The name to check.
|
|
81
|
+
|
|
82
|
+
:returns: True if registered, False otherwise.
|
|
83
|
+
"""
|
|
84
|
+
return name in self._circuit_breakers
|
|
85
|
+
|
|
86
|
+
def reset_all(self) -> None:
|
|
87
|
+
"""
|
|
88
|
+
Calls the reset method on all registered CircuitBreaker instances.
|
|
89
|
+
"""
|
|
90
|
+
for cb in self._circuit_breakers.values():
|
|
91
|
+
cb.reset()
|
|
92
|
+
return
|
|
93
|
+
|
|
94
|
+
def __repr__(self) -> str:
|
|
95
|
+
"""
|
|
96
|
+
Returns a detailed string representation including class name
|
|
97
|
+
and count of registered circuit breakers.
|
|
98
|
+
|
|
99
|
+
:returns: A repr string for debugging.
|
|
100
|
+
"""
|
|
101
|
+
repr_str = "CircuitBreakerRegistry(count={})".format(self.count)
|
|
102
|
+
return repr_str
|
|
103
|
+
|
|
104
|
+
def __str__(self) -> str:
|
|
105
|
+
"""
|
|
106
|
+
Returns a human-readable summary listing the registered circuit
|
|
107
|
+
breaker names and their current states.
|
|
108
|
+
|
|
109
|
+
:returns: A human-readable string.
|
|
110
|
+
"""
|
|
111
|
+
entries = []
|
|
112
|
+
for name, cb in self._circuit_breakers.items():
|
|
113
|
+
entries.append("{}={}".format(name, cb.state.value))
|
|
114
|
+
|
|
115
|
+
str_val = "CircuitBreakerRegistry: [{}]".format(", ".join(entries))
|
|
116
|
+
return str_val
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the CircuitBreakerState enum representing the possible states
|
|
3
|
+
of a circuit breaker in the Error Handling & Resilience feature.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import enum
|
|
7
|
+
|
|
8
|
+
from ccai.agentkit.errors import SemanticError
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class CircuitBreakerState(enum.Enum):
|
|
12
|
+
"""
|
|
13
|
+
Enumeration defining the state of a CircuitBreaker.
|
|
14
|
+
|
|
15
|
+
CLOSED: Normal operation, requests pass through.
|
|
16
|
+
OPEN: Service is degraded, requests are rejected immediately.
|
|
17
|
+
HALF_OPEN: Recovery testing, limited requests allowed through.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
CLOSED = "CLOSED"
|
|
21
|
+
OPEN = "OPEN"
|
|
22
|
+
HALF_OPEN = "HALF_OPEN"
|
|
23
|
+
|
|
24
|
+
@classmethod
|
|
25
|
+
def from_string(cls, value: str) -> "CircuitBreakerState":
|
|
26
|
+
"""
|
|
27
|
+
Returns the CircuitBreakerState member matching the given string.
|
|
28
|
+
|
|
29
|
+
:param value: The string value to match.
|
|
30
|
+
|
|
31
|
+
:returns: The matching CircuitBreakerState member.
|
|
32
|
+
:raises SemanticError: If the value does not match any member.
|
|
33
|
+
"""
|
|
34
|
+
upper_value = value.upper()
|
|
35
|
+
|
|
36
|
+
for member in cls:
|
|
37
|
+
if member.value == upper_value:
|
|
38
|
+
return member
|
|
39
|
+
|
|
40
|
+
valid_values = [m.value for m in cls]
|
|
41
|
+
errmsg = "Invalid CircuitBreakerState value '{}'. Valid values are: {}.".format(
|
|
42
|
+
value, ", ".join(valid_values)
|
|
43
|
+
)
|
|
44
|
+
raise SemanticError(errmsg)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the ClearCacheAction for clearing cached data in a component.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from ccai.agentkit.healthstatus import HealthStatus
|
|
6
|
+
from ccai.agentkit.selfhealingaction import SelfHealingAction
|
|
7
|
+
from ccai.agentkit.selfhealingresult import SelfHealingResult
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ClearCacheAction(SelfHealingAction):
|
|
11
|
+
"""
|
|
12
|
+
Clears cached data in a specified component by invoking a callable.
|
|
13
|
+
|
|
14
|
+
:param cache_clear_callable: A callable that clears the cache.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, cache_clear_callable) -> None:
|
|
18
|
+
super().__init__(action_name="clear_cache")
|
|
19
|
+
self._cache_clear_callable = cache_clear_callable
|
|
20
|
+
return
|
|
21
|
+
|
|
22
|
+
def heal(self, check_name: str, status: HealthStatus) -> SelfHealingResult:
|
|
23
|
+
"""
|
|
24
|
+
Invokes the cache clear callable.
|
|
25
|
+
|
|
26
|
+
:param check_name: Name of the health check that triggered healing.
|
|
27
|
+
:param status: The HealthStatus that triggered the action.
|
|
28
|
+
|
|
29
|
+
:returns: A SelfHealingResult indicating success or failure.
|
|
30
|
+
"""
|
|
31
|
+
try:
|
|
32
|
+
self._cache_clear_callable()
|
|
33
|
+
result = SelfHealingResult(
|
|
34
|
+
check_name=check_name,
|
|
35
|
+
action_name=self._action_name,
|
|
36
|
+
success=True
|
|
37
|
+
)
|
|
38
|
+
return result
|
|
39
|
+
except Exception as exc:
|
|
40
|
+
result = SelfHealingResult(
|
|
41
|
+
check_name=check_name,
|
|
42
|
+
action_name=self._action_name,
|
|
43
|
+
success=False,
|
|
44
|
+
error_message=str(exc)
|
|
45
|
+
)
|
|
46
|
+
return result
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the ConcurrencyBridge utility for cross-model communication
|
|
3
|
+
between agents using different concurrency models in the
|
|
4
|
+
codecraftai-agent-kit framework.
|
|
5
|
+
|
|
6
|
+
The bridge detects the concurrency model of a target agent and
|
|
7
|
+
adapts lifecycle invocations and hook calls accordingly, allowing
|
|
8
|
+
parent-child agents with different concurrency models to communicate.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import asyncio
|
|
12
|
+
|
|
13
|
+
from typing import Callable
|
|
14
|
+
|
|
15
|
+
from ccai.agentkit.concurrencymodel import ConcurrencyModel
|
|
16
|
+
from ccai.agentkit.transitionrecord import TransitionRecord
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _get_agent_concurrency_model(agent: object) -> ConcurrencyModel:
|
|
20
|
+
"""
|
|
21
|
+
Detects the concurrency model of an agent.
|
|
22
|
+
|
|
23
|
+
Checks for a concurrency_model property on the agent. If not
|
|
24
|
+
present, returns None to indicate a standard synchronous agent.
|
|
25
|
+
|
|
26
|
+
:param agent: The agent instance to inspect.
|
|
27
|
+
|
|
28
|
+
:returns: The ConcurrencyModel of the agent, or None for standard agents.
|
|
29
|
+
"""
|
|
30
|
+
has_model = hasattr(agent, "concurrency_model")
|
|
31
|
+
|
|
32
|
+
if has_model is True:
|
|
33
|
+
model = agent.concurrency_model
|
|
34
|
+
return model
|
|
35
|
+
|
|
36
|
+
return None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ConcurrencyBridge:
|
|
40
|
+
"""
|
|
41
|
+
Utility class for cross-model communication between agents
|
|
42
|
+
using different concurrency models.
|
|
43
|
+
|
|
44
|
+
Provides static methods to invoke lifecycle operations and
|
|
45
|
+
hook callbacks across concurrency model boundaries. When a
|
|
46
|
+
parent agent and child agent use different concurrency models,
|
|
47
|
+
the bridge adapts the invocation accordingly.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
@staticmethod
|
|
51
|
+
def invoke_lifecycle(agent: object, method_name: str) -> None:
|
|
52
|
+
"""
|
|
53
|
+
Invokes a lifecycle method on an agent, adapting to its
|
|
54
|
+
concurrency model.
|
|
55
|
+
|
|
56
|
+
For AsyncAgent instances, the async version of the method
|
|
57
|
+
is run on the event loop. For ThreadedAgent and PooledAgent
|
|
58
|
+
instances, the method is called directly (it handles its
|
|
59
|
+
own threading). For standard agents, the method is called
|
|
60
|
+
synchronously.
|
|
61
|
+
|
|
62
|
+
:param agent: The agent instance to invoke the method on.
|
|
63
|
+
:param method_name: The name of the lifecycle method
|
|
64
|
+
(e.g., 'initialize', 'start', 'stop').
|
|
65
|
+
"""
|
|
66
|
+
model = _get_agent_concurrency_model(agent)
|
|
67
|
+
|
|
68
|
+
if model is ConcurrencyModel.ASYNCIO:
|
|
69
|
+
async_method_name = "async_{}".format(method_name)
|
|
70
|
+
has_async_method = hasattr(agent, async_method_name)
|
|
71
|
+
|
|
72
|
+
if has_async_method is True:
|
|
73
|
+
async_method = getattr(agent, async_method_name)
|
|
74
|
+
_run_coroutine(async_method())
|
|
75
|
+
else:
|
|
76
|
+
method = getattr(agent, method_name)
|
|
77
|
+
method()
|
|
78
|
+
else:
|
|
79
|
+
method = getattr(agent, method_name)
|
|
80
|
+
method()
|
|
81
|
+
|
|
82
|
+
return
|
|
83
|
+
|
|
84
|
+
@staticmethod
|
|
85
|
+
def invoke_hook(agent: object, callback: Callable,
|
|
86
|
+
record: TransitionRecord) -> None:
|
|
87
|
+
"""
|
|
88
|
+
Invokes a hook callback on an agent, adapting to the
|
|
89
|
+
concurrency model of the agent that owns the hook.
|
|
90
|
+
|
|
91
|
+
For async callbacks, the coroutine is run on the event loop.
|
|
92
|
+
For synchronous callbacks, they are called directly.
|
|
93
|
+
|
|
94
|
+
:param agent: The agent instance to pass to the callback.
|
|
95
|
+
:param callback: The hook callback to invoke.
|
|
96
|
+
:param record: The TransitionRecord to pass to the callback.
|
|
97
|
+
"""
|
|
98
|
+
is_coroutine_function = asyncio.iscoroutinefunction(callback)
|
|
99
|
+
|
|
100
|
+
if is_coroutine_function is True:
|
|
101
|
+
coroutine = callback(agent, record)
|
|
102
|
+
_run_coroutine(coroutine)
|
|
103
|
+
else:
|
|
104
|
+
callback(agent, record)
|
|
105
|
+
|
|
106
|
+
return
|
|
107
|
+
|
|
108
|
+
@staticmethod
|
|
109
|
+
def adapt_parent_child(parent: object, child: object,
|
|
110
|
+
method_name: str) -> None:
|
|
111
|
+
"""
|
|
112
|
+
Adapts a lifecycle call from a parent agent to a child agent
|
|
113
|
+
that may use a different concurrency model.
|
|
114
|
+
|
|
115
|
+
Detects the child's concurrency model and invokes the
|
|
116
|
+
lifecycle method appropriately.
|
|
117
|
+
|
|
118
|
+
:param parent: The parent agent instance.
|
|
119
|
+
:param child: The child agent instance.
|
|
120
|
+
:param method_name: The lifecycle method name to invoke on the child.
|
|
121
|
+
"""
|
|
122
|
+
ConcurrencyBridge.invoke_lifecycle(child, method_name)
|
|
123
|
+
return
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _run_coroutine(coroutine: object) -> None:
|
|
127
|
+
"""
|
|
128
|
+
Runs a coroutine, handling both cases where an event loop
|
|
129
|
+
is already running and where one needs to be created.
|
|
130
|
+
|
|
131
|
+
:param coroutine: The coroutine to run.
|
|
132
|
+
"""
|
|
133
|
+
try:
|
|
134
|
+
loop = asyncio.get_running_loop()
|
|
135
|
+
except RuntimeError:
|
|
136
|
+
loop = None
|
|
137
|
+
|
|
138
|
+
if loop is not None and loop.is_running():
|
|
139
|
+
asyncio.ensure_future(coroutine)
|
|
140
|
+
else:
|
|
141
|
+
asyncio.run(coroutine)
|
|
142
|
+
|
|
143
|
+
return
|