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,154 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the FeatureFlag class representing an immutable named boolean
|
|
3
|
+
toggle that controls whether a specific capability or behavior is
|
|
4
|
+
enabled for an Agent in the codecraftai-agent-kit framework.
|
|
5
|
+
|
|
6
|
+
A FeatureFlag contains a flag name, a default value, an optional
|
|
7
|
+
description, and an optional metadata dictionary.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class FeatureFlag:
|
|
14
|
+
"""
|
|
15
|
+
Immutable object representing a named boolean toggle that controls
|
|
16
|
+
whether a specific capability or behavior is enabled for an Agent.
|
|
17
|
+
|
|
18
|
+
:param name: The flag name string.
|
|
19
|
+
:param default_value: The default boolean value for the flag.
|
|
20
|
+
:param description: An optional description string.
|
|
21
|
+
:param metadata: An optional metadata dictionary.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, name: str, default_value: bool = False,
|
|
25
|
+
description: str = None, metadata: dict = None) -> None:
|
|
26
|
+
self._name = name
|
|
27
|
+
self._default_value = default_value
|
|
28
|
+
self._description = description
|
|
29
|
+
|
|
30
|
+
if metadata is not None:
|
|
31
|
+
self._metadata = dict(metadata)
|
|
32
|
+
else:
|
|
33
|
+
self._metadata = {}
|
|
34
|
+
|
|
35
|
+
return
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def name(self) -> str:
|
|
39
|
+
"""
|
|
40
|
+
The flag name string.
|
|
41
|
+
|
|
42
|
+
:returns: The flag name.
|
|
43
|
+
"""
|
|
44
|
+
return self._name
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def default_value(self) -> bool:
|
|
48
|
+
"""
|
|
49
|
+
The default boolean value for the flag.
|
|
50
|
+
|
|
51
|
+
:returns: The default value.
|
|
52
|
+
"""
|
|
53
|
+
return self._default_value
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def description(self) -> str:
|
|
57
|
+
"""
|
|
58
|
+
The optional description string.
|
|
59
|
+
|
|
60
|
+
:returns: The description string, or None.
|
|
61
|
+
"""
|
|
62
|
+
return self._description
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def metadata(self) -> dict:
|
|
66
|
+
"""
|
|
67
|
+
A defensive copy of the metadata dictionary.
|
|
68
|
+
|
|
69
|
+
:returns: A dictionary copy of the metadata.
|
|
70
|
+
"""
|
|
71
|
+
return dict(self._metadata)
|
|
72
|
+
|
|
73
|
+
def as_dict(self) -> dict:
|
|
74
|
+
"""
|
|
75
|
+
Returns a dictionary representation of the flag fields.
|
|
76
|
+
|
|
77
|
+
:returns: A dictionary with name, default_value, description, and metadata.
|
|
78
|
+
"""
|
|
79
|
+
result = {
|
|
80
|
+
"name": self._name,
|
|
81
|
+
"default_value": self._default_value,
|
|
82
|
+
"description": self._description,
|
|
83
|
+
"metadata": dict(self._metadata)
|
|
84
|
+
}
|
|
85
|
+
return result
|
|
86
|
+
|
|
87
|
+
def to_json(self) -> str:
|
|
88
|
+
"""
|
|
89
|
+
Returns a JSON string representation of the flag fields.
|
|
90
|
+
|
|
91
|
+
:returns: A JSON string containing all flag fields.
|
|
92
|
+
"""
|
|
93
|
+
data = self.as_dict()
|
|
94
|
+
json_str = json.dumps(data)
|
|
95
|
+
return json_str
|
|
96
|
+
|
|
97
|
+
@classmethod
|
|
98
|
+
def from_dict(cls, data: dict) -> "FeatureFlag":
|
|
99
|
+
"""
|
|
100
|
+
Constructs a FeatureFlag from a dictionary.
|
|
101
|
+
|
|
102
|
+
:param data: A dictionary containing name, default_value, description, and metadata.
|
|
103
|
+
|
|
104
|
+
:returns: A FeatureFlag instance reconstructed from the dictionary.
|
|
105
|
+
"""
|
|
106
|
+
name = data["name"]
|
|
107
|
+
default_value = data.get("default_value", False)
|
|
108
|
+
description = data.get("description", None)
|
|
109
|
+
metadata = data.get("metadata", None)
|
|
110
|
+
|
|
111
|
+
flag = cls(
|
|
112
|
+
name=name,
|
|
113
|
+
default_value=default_value,
|
|
114
|
+
description=description,
|
|
115
|
+
metadata=metadata
|
|
116
|
+
)
|
|
117
|
+
return flag
|
|
118
|
+
|
|
119
|
+
@classmethod
|
|
120
|
+
def from_json(cls, json_str: str) -> "FeatureFlag":
|
|
121
|
+
"""
|
|
122
|
+
Deserializes a JSON string into a FeatureFlag instance.
|
|
123
|
+
|
|
124
|
+
:param json_str: A JSON string containing flag fields.
|
|
125
|
+
|
|
126
|
+
:returns: A FeatureFlag instance reconstructed from the JSON data.
|
|
127
|
+
"""
|
|
128
|
+
data = json.loads(json_str)
|
|
129
|
+
flag = cls.from_dict(data)
|
|
130
|
+
return flag
|
|
131
|
+
|
|
132
|
+
def __repr__(self) -> str:
|
|
133
|
+
"""
|
|
134
|
+
Returns a detailed string representation including class name,
|
|
135
|
+
flag name, and default value.
|
|
136
|
+
|
|
137
|
+
:returns: A repr string for debugging.
|
|
138
|
+
"""
|
|
139
|
+
repr_str = "FeatureFlag(name={!r}, default_value={!r})".format(
|
|
140
|
+
self._name, self._default_value
|
|
141
|
+
)
|
|
142
|
+
return repr_str
|
|
143
|
+
|
|
144
|
+
def __str__(self) -> str:
|
|
145
|
+
"""
|
|
146
|
+
Returns a human-readable summary including the flag name,
|
|
147
|
+
default value, and description.
|
|
148
|
+
|
|
149
|
+
:returns: A human-readable string.
|
|
150
|
+
"""
|
|
151
|
+
str_val = "FeatureFlag '{}' (default={}, description={!r})".format(
|
|
152
|
+
self._name, self._default_value, self._description
|
|
153
|
+
)
|
|
154
|
+
return str_val
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the FeatureFlagEvaluator class that evaluates FeatureFlag values
|
|
3
|
+
for an Agent with three-tier precedence in the codecraftai-agent-kit framework.
|
|
4
|
+
|
|
5
|
+
The evaluator checks: 1) runtime override, 2) configuration value at
|
|
6
|
+
feature_flags.<flag_name>, 3) default value.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from ccai.agentkit.errors import SemanticError
|
|
10
|
+
from ccai.agentkit.featureflagregistry import FeatureFlagRegistry
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class FeatureFlagEvaluator:
|
|
14
|
+
"""
|
|
15
|
+
Evaluates FeatureFlag values for an Agent with three-tier precedence:
|
|
16
|
+
runtime override, configuration value, default value.
|
|
17
|
+
|
|
18
|
+
Supports conditional evaluation based on context dictionaries.
|
|
19
|
+
|
|
20
|
+
:param registry: The FeatureFlagRegistry to evaluate flags from.
|
|
21
|
+
:param configuration_manager: An optional ConfigurationManager for
|
|
22
|
+
configuration-driven flag values.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, registry: FeatureFlagRegistry,
|
|
26
|
+
configuration_manager=None) -> None:
|
|
27
|
+
self._registry = registry
|
|
28
|
+
self._configuration_manager = configuration_manager
|
|
29
|
+
return
|
|
30
|
+
|
|
31
|
+
def evaluate(self, flag_name: str) -> bool:
|
|
32
|
+
"""
|
|
33
|
+
Evaluates a flag: runtime override > config at
|
|
34
|
+
feature_flags.<flag_name> > default value.
|
|
35
|
+
|
|
36
|
+
:param flag_name: The flag name to evaluate.
|
|
37
|
+
|
|
38
|
+
:returns: True if the flag is enabled.
|
|
39
|
+
|
|
40
|
+
:raises SemanticError: If the flag name is not registered.
|
|
41
|
+
"""
|
|
42
|
+
flag = self._registry.lookup(flag_name)
|
|
43
|
+
|
|
44
|
+
if flag is None:
|
|
45
|
+
errmsg = "FeatureFlag '{}' is not registered".format(flag_name)
|
|
46
|
+
raise SemanticError(errmsg)
|
|
47
|
+
|
|
48
|
+
# 1. Check runtime override in registry
|
|
49
|
+
if flag_name in self._registry._overrides:
|
|
50
|
+
return self._registry._overrides[flag_name]
|
|
51
|
+
|
|
52
|
+
# 2. Check config at feature_flags.<flag_name> via configuration_manager
|
|
53
|
+
if self._configuration_manager is not None:
|
|
54
|
+
config_path = "feature_flags.{}".format(flag_name)
|
|
55
|
+
config_value = self._configuration_manager.get(config_path)
|
|
56
|
+
|
|
57
|
+
if config_value is not None:
|
|
58
|
+
if isinstance(config_value, bool) is True:
|
|
59
|
+
return config_value
|
|
60
|
+
|
|
61
|
+
if isinstance(config_value, str) is True:
|
|
62
|
+
lower_value = config_value.lower()
|
|
63
|
+
if lower_value == "true":
|
|
64
|
+
return True
|
|
65
|
+
elif lower_value == "false":
|
|
66
|
+
return False
|
|
67
|
+
|
|
68
|
+
# 3. Fall back to default value
|
|
69
|
+
return flag.default_value
|
|
70
|
+
|
|
71
|
+
def evaluate_with_context(self, flag_name: str, context: dict) -> bool:
|
|
72
|
+
"""
|
|
73
|
+
Evaluates a flag with conditional rules based on context.
|
|
74
|
+
For now, delegates to evaluate (context support is extensible).
|
|
75
|
+
|
|
76
|
+
:param flag_name: The flag name to evaluate.
|
|
77
|
+
:param context: Context dictionary for conditional evaluation.
|
|
78
|
+
|
|
79
|
+
:returns: True if the flag is enabled.
|
|
80
|
+
|
|
81
|
+
:raises SemanticError: If the flag name is not registered.
|
|
82
|
+
"""
|
|
83
|
+
result = self.evaluate(flag_name)
|
|
84
|
+
return result
|
|
85
|
+
|
|
86
|
+
def __repr__(self) -> str:
|
|
87
|
+
"""
|
|
88
|
+
Returns a detailed string representation including class name
|
|
89
|
+
and the count of registered flags.
|
|
90
|
+
|
|
91
|
+
:returns: A repr string for debugging.
|
|
92
|
+
"""
|
|
93
|
+
flag_count = self._registry.count
|
|
94
|
+
repr_str = "FeatureFlagEvaluator(flag_count={})".format(flag_count)
|
|
95
|
+
return repr_str
|
|
96
|
+
|
|
97
|
+
def __str__(self) -> str:
|
|
98
|
+
"""
|
|
99
|
+
Returns a human-readable summary including the count of registered
|
|
100
|
+
flags and whether a ConfigurationManager is configured.
|
|
101
|
+
|
|
102
|
+
:returns: A human-readable string.
|
|
103
|
+
"""
|
|
104
|
+
flag_count = self._registry.count
|
|
105
|
+
has_config = self._configuration_manager is not None
|
|
106
|
+
str_val = "FeatureFlagEvaluator({} flags, configuration_manager={})".format(
|
|
107
|
+
flag_count, has_config
|
|
108
|
+
)
|
|
109
|
+
return str_val
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the FeatureFlagRegistry class that tracks FeatureFlag instances
|
|
3
|
+
for an Agent in the codecraftai-agent-kit framework.
|
|
4
|
+
|
|
5
|
+
The registry provides lookup, enumeration, runtime override, and
|
|
6
|
+
file-based loading of feature flag definitions.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import logging
|
|
11
|
+
|
|
12
|
+
from ccai.agentkit.errors import SemanticError
|
|
13
|
+
from ccai.agentkit.featureflag import FeatureFlag
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
import yaml
|
|
17
|
+
YAML_AVAILABLE = True
|
|
18
|
+
except ImportError:
|
|
19
|
+
YAML_AVAILABLE = False
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class FeatureFlagRegistry:
|
|
23
|
+
"""
|
|
24
|
+
Registry tracking FeatureFlag instances for an Agent.
|
|
25
|
+
|
|
26
|
+
Provides lookup, enumeration, runtime override, and file-based loading.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(self) -> None:
|
|
30
|
+
self._flags = {}
|
|
31
|
+
self._overrides = {}
|
|
32
|
+
self._logger = logging.getLogger("ccai.agentkit.featureflags")
|
|
33
|
+
return
|
|
34
|
+
|
|
35
|
+
def register(self, flag: FeatureFlag) -> None:
|
|
36
|
+
"""
|
|
37
|
+
Registers a FeatureFlag using its name as the key.
|
|
38
|
+
|
|
39
|
+
:param flag: The FeatureFlag to register.
|
|
40
|
+
|
|
41
|
+
:raises SemanticError: If a flag with the same name is already registered.
|
|
42
|
+
"""
|
|
43
|
+
flag_name = flag.name
|
|
44
|
+
|
|
45
|
+
if flag_name in self._flags:
|
|
46
|
+
errmsg = "A FeatureFlag with name '{}' is already registered".format(flag_name)
|
|
47
|
+
raise SemanticError(errmsg)
|
|
48
|
+
|
|
49
|
+
self._flags[flag_name] = flag
|
|
50
|
+
return
|
|
51
|
+
|
|
52
|
+
def deregister(self, flag_name: str) -> None:
|
|
53
|
+
"""
|
|
54
|
+
Deregisters a FeatureFlag by name.
|
|
55
|
+
|
|
56
|
+
:param flag_name: The name of the flag to deregister.
|
|
57
|
+
"""
|
|
58
|
+
if flag_name in self._flags:
|
|
59
|
+
del self._flags[flag_name]
|
|
60
|
+
|
|
61
|
+
if flag_name in self._overrides:
|
|
62
|
+
del self._overrides[flag_name]
|
|
63
|
+
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
def lookup(self, flag_name: str) -> FeatureFlag:
|
|
67
|
+
"""
|
|
68
|
+
Looks up a registered FeatureFlag by name.
|
|
69
|
+
|
|
70
|
+
:param flag_name: The name of the flag to look up.
|
|
71
|
+
|
|
72
|
+
:returns: The FeatureFlag instance, or None if not found.
|
|
73
|
+
"""
|
|
74
|
+
result = self._flags.get(flag_name, None)
|
|
75
|
+
return result
|
|
76
|
+
|
|
77
|
+
def list_all(self) -> list:
|
|
78
|
+
"""
|
|
79
|
+
Returns a list of all registered FeatureFlag instances.
|
|
80
|
+
|
|
81
|
+
:returns: A list of FeatureFlag objects.
|
|
82
|
+
"""
|
|
83
|
+
result = list(self._flags.values())
|
|
84
|
+
return result
|
|
85
|
+
|
|
86
|
+
def has_flag(self, flag_name: str) -> bool:
|
|
87
|
+
"""
|
|
88
|
+
Checks whether a FeatureFlag with the given name is registered.
|
|
89
|
+
|
|
90
|
+
:param flag_name: The flag name to check.
|
|
91
|
+
|
|
92
|
+
:returns: True if the flag is registered, False otherwise.
|
|
93
|
+
"""
|
|
94
|
+
result = flag_name in self._flags
|
|
95
|
+
return result
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def count(self) -> int:
|
|
99
|
+
"""
|
|
100
|
+
The count of currently registered flags.
|
|
101
|
+
|
|
102
|
+
:returns: The number of registered flags.
|
|
103
|
+
"""
|
|
104
|
+
result = len(self._flags)
|
|
105
|
+
return result
|
|
106
|
+
|
|
107
|
+
def is_enabled(self, flag_name: str) -> bool:
|
|
108
|
+
"""
|
|
109
|
+
Returns whether the flag is enabled, considering runtime overrides
|
|
110
|
+
and the default value in that precedence order.
|
|
111
|
+
|
|
112
|
+
The registry's is_enabled checks: 1) runtime override, 2) default value.
|
|
113
|
+
The evaluator adds the configuration layer.
|
|
114
|
+
|
|
115
|
+
:param flag_name: The flag name to evaluate.
|
|
116
|
+
|
|
117
|
+
:returns: True if the flag is enabled.
|
|
118
|
+
"""
|
|
119
|
+
if flag_name in self._overrides:
|
|
120
|
+
return self._overrides[flag_name]
|
|
121
|
+
|
|
122
|
+
flag = self._flags.get(flag_name, None)
|
|
123
|
+
|
|
124
|
+
if flag is not None:
|
|
125
|
+
return flag.default_value
|
|
126
|
+
|
|
127
|
+
return False
|
|
128
|
+
|
|
129
|
+
def set_override(self, flag_name: str, value: bool) -> None:
|
|
130
|
+
"""
|
|
131
|
+
Sets a runtime override for a flag.
|
|
132
|
+
|
|
133
|
+
:param flag_name: The flag name to override.
|
|
134
|
+
:param value: The override boolean value.
|
|
135
|
+
"""
|
|
136
|
+
self._overrides[flag_name] = value
|
|
137
|
+
effective_value = self.is_enabled(flag_name)
|
|
138
|
+
self._logger.info(
|
|
139
|
+
"Feature flag override set: flag='%s', override=%s, effective=%s",
|
|
140
|
+
flag_name, value, effective_value
|
|
141
|
+
)
|
|
142
|
+
return
|
|
143
|
+
|
|
144
|
+
def clear_override(self, flag_name: str) -> None:
|
|
145
|
+
"""
|
|
146
|
+
Clears the runtime override for a flag.
|
|
147
|
+
|
|
148
|
+
:param flag_name: The flag name to clear the override for.
|
|
149
|
+
"""
|
|
150
|
+
if flag_name in self._overrides:
|
|
151
|
+
del self._overrides[flag_name]
|
|
152
|
+
|
|
153
|
+
effective_value = self.is_enabled(flag_name)
|
|
154
|
+
self._logger.info(
|
|
155
|
+
"Feature flag override cleared: flag='%s', effective=%s",
|
|
156
|
+
flag_name, effective_value
|
|
157
|
+
)
|
|
158
|
+
return
|
|
159
|
+
|
|
160
|
+
def clear_all_overrides(self) -> None:
|
|
161
|
+
"""
|
|
162
|
+
Removes all runtime overrides.
|
|
163
|
+
"""
|
|
164
|
+
self._overrides.clear()
|
|
165
|
+
self._logger.info("All feature flag overrides cleared")
|
|
166
|
+
return
|
|
167
|
+
|
|
168
|
+
def load_from_json_file(self, file_path: str) -> None:
|
|
169
|
+
"""
|
|
170
|
+
Loads FeatureFlag definitions from a JSON file.
|
|
171
|
+
|
|
172
|
+
:param file_path: Path to the JSON file.
|
|
173
|
+
"""
|
|
174
|
+
with open(file_path, "r", encoding="utf-8") as file_handle:
|
|
175
|
+
data = json.load(file_handle)
|
|
176
|
+
|
|
177
|
+
self._load_flags_from_data(data)
|
|
178
|
+
return
|
|
179
|
+
|
|
180
|
+
def load_from_yaml_file(self, file_path: str) -> None:
|
|
181
|
+
"""
|
|
182
|
+
Loads FeatureFlag definitions from a YAML file.
|
|
183
|
+
|
|
184
|
+
:param file_path: Path to the YAML file.
|
|
185
|
+
"""
|
|
186
|
+
if YAML_AVAILABLE is False:
|
|
187
|
+
errmsg = "YAML support is not available. Install PyYAML to load YAML files."
|
|
188
|
+
raise SemanticError(errmsg)
|
|
189
|
+
|
|
190
|
+
with open(file_path, "r", encoding="utf-8") as file_handle:
|
|
191
|
+
data = yaml.safe_load(file_handle)
|
|
192
|
+
|
|
193
|
+
if data is None:
|
|
194
|
+
data = {}
|
|
195
|
+
|
|
196
|
+
self._load_flags_from_data(data)
|
|
197
|
+
return
|
|
198
|
+
|
|
199
|
+
def _load_flags_from_data(self, data) -> None:
|
|
200
|
+
"""
|
|
201
|
+
Loads FeatureFlag definitions from a parsed data structure.
|
|
202
|
+
Expects either a dict of flag_name -> flag_dict or a list of flag dicts.
|
|
203
|
+
|
|
204
|
+
:param data: The parsed data structure.
|
|
205
|
+
"""
|
|
206
|
+
if isinstance(data, dict) is True:
|
|
207
|
+
for flag_name, flag_data in data.items():
|
|
208
|
+
if isinstance(flag_data, dict) is True:
|
|
209
|
+
flag_data["name"] = flag_name
|
|
210
|
+
flag = FeatureFlag.from_dict(flag_data)
|
|
211
|
+
elif isinstance(flag_data, bool) is True:
|
|
212
|
+
flag = FeatureFlag(name=flag_name, default_value=flag_data)
|
|
213
|
+
else:
|
|
214
|
+
flag = FeatureFlag(name=flag_name, default_value=False)
|
|
215
|
+
|
|
216
|
+
if flag_name not in self._flags:
|
|
217
|
+
self._flags[flag_name] = flag
|
|
218
|
+
elif isinstance(data, list) is True:
|
|
219
|
+
for flag_data in data:
|
|
220
|
+
if isinstance(flag_data, dict) is True:
|
|
221
|
+
flag = FeatureFlag.from_dict(flag_data)
|
|
222
|
+
flag_name = flag.name
|
|
223
|
+
|
|
224
|
+
if flag_name not in self._flags:
|
|
225
|
+
self._flags[flag_name] = flag
|
|
226
|
+
|
|
227
|
+
return
|
|
228
|
+
|
|
229
|
+
def __repr__(self) -> str:
|
|
230
|
+
"""
|
|
231
|
+
Returns a detailed string representation including class name
|
|
232
|
+
and the count of registered flags.
|
|
233
|
+
|
|
234
|
+
:returns: A repr string for debugging.
|
|
235
|
+
"""
|
|
236
|
+
flag_count = len(self._flags)
|
|
237
|
+
repr_str = "FeatureFlagRegistry(count={})".format(flag_count)
|
|
238
|
+
return repr_str
|
|
239
|
+
|
|
240
|
+
def __str__(self) -> str:
|
|
241
|
+
"""
|
|
242
|
+
Returns a human-readable summary listing the registered flag
|
|
243
|
+
names and their current enabled status.
|
|
244
|
+
|
|
245
|
+
:returns: A human-readable string.
|
|
246
|
+
"""
|
|
247
|
+
flag_entries = []
|
|
248
|
+
|
|
249
|
+
for flag_name in sorted(self._flags.keys()):
|
|
250
|
+
enabled = self.is_enabled(flag_name)
|
|
251
|
+
entry = "{}={}".format(flag_name, enabled)
|
|
252
|
+
flag_entries.append(entry)
|
|
253
|
+
|
|
254
|
+
flags_summary = ", ".join(flag_entries)
|
|
255
|
+
str_val = "FeatureFlagRegistry({} flags: {})".format(
|
|
256
|
+
len(self._flags), flags_summary
|
|
257
|
+
)
|
|
258
|
+
return str_val
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the FileDeadLetterStore for JSON file-based dead letter storage.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
from typing import Optional
|
|
8
|
+
|
|
9
|
+
from ccai.agentkit.deadletterentry import DeadLetterEntry
|
|
10
|
+
from ccai.agentkit.deadletterquery import DeadLetterQuery
|
|
11
|
+
from ccai.agentkit.deadletterquery import DeadLetterQueryResult
|
|
12
|
+
from ccai.agentkit.deadletterstore import DeadLetterStore
|
|
13
|
+
from ccai.agentkit.resilienceerrors import ResilienceError
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class FileDeadLetterStore(DeadLetterStore):
|
|
17
|
+
"""
|
|
18
|
+
JSON file-based dead letter store. One file per entry using the
|
|
19
|
+
entry identifier as the filename.
|
|
20
|
+
|
|
21
|
+
:param directory: The directory path for storing entry files.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, directory: str) -> None:
|
|
25
|
+
super().__init__("file")
|
|
26
|
+
self._directory = directory
|
|
27
|
+
|
|
28
|
+
if not os.path.exists(directory):
|
|
29
|
+
os.makedirs(directory, exist_ok=True)
|
|
30
|
+
return
|
|
31
|
+
|
|
32
|
+
def store(self, entry: DeadLetterEntry) -> None:
|
|
33
|
+
filepath = self._entry_path(entry.identifier)
|
|
34
|
+
try:
|
|
35
|
+
with open(filepath, "w") as f:
|
|
36
|
+
f.write(entry.to_json())
|
|
37
|
+
except IOError as exc:
|
|
38
|
+
raise ResilienceError("Failed to store dead letter entry: {}".format(str(exc)), cause=exc)
|
|
39
|
+
return
|
|
40
|
+
|
|
41
|
+
def retrieve(self, identifier: str) -> Optional[DeadLetterEntry]:
|
|
42
|
+
filepath = self._entry_path(identifier)
|
|
43
|
+
if not os.path.exists(filepath):
|
|
44
|
+
return None
|
|
45
|
+
try:
|
|
46
|
+
with open(filepath, "r") as f:
|
|
47
|
+
json_str = f.read()
|
|
48
|
+
entry = DeadLetterEntry.from_json(json_str)
|
|
49
|
+
return entry
|
|
50
|
+
except IOError as exc:
|
|
51
|
+
raise ResilienceError("Failed to retrieve dead letter entry: {}".format(str(exc)), cause=exc)
|
|
52
|
+
|
|
53
|
+
def search(self, query: DeadLetterQuery) -> DeadLetterQueryResult:
|
|
54
|
+
entries = []
|
|
55
|
+
try:
|
|
56
|
+
for filename in os.listdir(self._directory):
|
|
57
|
+
if filename.endswith(".json"):
|
|
58
|
+
filepath = os.path.join(self._directory, filename)
|
|
59
|
+
with open(filepath, "r") as f:
|
|
60
|
+
json_str = f.read()
|
|
61
|
+
entry = DeadLetterEntry.from_json(json_str)
|
|
62
|
+
entries.append(entry)
|
|
63
|
+
except IOError as exc:
|
|
64
|
+
raise ResilienceError("Failed to search dead letter store: {}".format(str(exc)), cause=exc)
|
|
65
|
+
|
|
66
|
+
matches = entries
|
|
67
|
+
if query.operation_type is not None:
|
|
68
|
+
matches = [e for e in matches if e.operation_type == query.operation_type]
|
|
69
|
+
if query.source_component is not None:
|
|
70
|
+
matches = [e for e in matches if e.source_component == query.source_component]
|
|
71
|
+
if query.agent_identifier is not None:
|
|
72
|
+
matches = [e for e in matches if e.agent_identifier == query.agent_identifier]
|
|
73
|
+
if query.start_time is not None:
|
|
74
|
+
matches = [e for e in matches if e.timestamp >= query.start_time]
|
|
75
|
+
if query.end_time is not None:
|
|
76
|
+
matches = [e for e in matches if e.timestamp <= query.end_time]
|
|
77
|
+
|
|
78
|
+
total_count = len(matches)
|
|
79
|
+
if query.max_results is not None:
|
|
80
|
+
matches = matches[:query.max_results]
|
|
81
|
+
|
|
82
|
+
result = DeadLetterQueryResult(entries=matches, total_count=total_count)
|
|
83
|
+
return result
|
|
84
|
+
|
|
85
|
+
def delete(self, identifier: str) -> None:
|
|
86
|
+
filepath = self._entry_path(identifier)
|
|
87
|
+
try:
|
|
88
|
+
if os.path.exists(filepath):
|
|
89
|
+
os.remove(filepath)
|
|
90
|
+
except IOError as exc:
|
|
91
|
+
raise ResilienceError("Failed to delete dead letter entry: {}".format(str(exc)), cause=exc)
|
|
92
|
+
return
|
|
93
|
+
|
|
94
|
+
def purge(self) -> None:
|
|
95
|
+
try:
|
|
96
|
+
for filename in os.listdir(self._directory):
|
|
97
|
+
if filename.endswith(".json"):
|
|
98
|
+
filepath = os.path.join(self._directory, filename)
|
|
99
|
+
os.remove(filepath)
|
|
100
|
+
except IOError as exc:
|
|
101
|
+
raise ResilienceError("Failed to purge dead letter store: {}".format(str(exc)), cause=exc)
|
|
102
|
+
return
|
|
103
|
+
|
|
104
|
+
def count(self) -> int:
|
|
105
|
+
try:
|
|
106
|
+
count = sum(1 for f in os.listdir(self._directory) if f.endswith(".json"))
|
|
107
|
+
return count
|
|
108
|
+
except IOError as exc:
|
|
109
|
+
raise ResilienceError("Failed to count dead letter entries: {}".format(str(exc)), cause=exc)
|
|
110
|
+
|
|
111
|
+
def _entry_path(self, identifier: str) -> str:
|
|
112
|
+
return os.path.join(self._directory, "{}.json".format(identifier))
|