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.
Files changed (230) hide show
  1. ccai/agentkit/__init__.py +304 -0
  2. ccai/agentkit/addressresolver.py +177 -0
  3. ccai/agentkit/agent.py +2945 -0
  4. ccai/agentkit/agentaddress.py +187 -0
  5. ccai/agentkit/agentconfiguration.py +581 -0
  6. ccai/agentkit/agentidentity.py +152 -0
  7. ccai/agentkit/agentregistry.py +221 -0
  8. ccai/agentkit/agentstatemachine.py +134 -0
  9. ccai/agentkit/alternateproviderstrategy.py +36 -0
  10. ccai/agentkit/approvalgate.py +238 -0
  11. ccai/agentkit/approvalrequest.py +250 -0
  12. ccai/agentkit/asyncagent.py +187 -0
  13. ccai/agentkit/breakpoint.py +150 -0
  14. ccai/agentkit/builtinagentmetrics.py +114 -0
  15. ccai/agentkit/builtins/__init__.py +11 -0
  16. ccai/agentkit/builtins/filereadtool.py +90 -0
  17. ccai/agentkit/builtins/filewritetool.py +93 -0
  18. ccai/agentkit/builtins/httprequesttool.py +169 -0
  19. ccai/agentkit/builtins/shellcommandtool.py +190 -0
  20. ccai/agentkit/cachedresultstrategy.py +43 -0
  21. ccai/agentkit/checkpointmanager.py +219 -0
  22. ccai/agentkit/circuitbreaker.py +230 -0
  23. ccai/agentkit/circuitbreakerconfiguration.py +158 -0
  24. ccai/agentkit/circuitbreakerevent.py +158 -0
  25. ccai/agentkit/circuitbreakerhealthcheck.py +32 -0
  26. ccai/agentkit/circuitbreakerregistry.py +116 -0
  27. ccai/agentkit/circuitbreakerstate.py +44 -0
  28. ccai/agentkit/clearcacheaction.py +46 -0
  29. ccai/agentkit/concurrencybridge.py +143 -0
  30. ccai/agentkit/concurrencymodel.py +19 -0
  31. ccai/agentkit/conditionevaluator.py +594 -0
  32. ccai/agentkit/configurationchangeevent.py +179 -0
  33. ccai/agentkit/configurationerrors.py +152 -0
  34. ccai/agentkit/configurationlayer.py +160 -0
  35. ccai/agentkit/configurationlayersource.py +46 -0
  36. ccai/agentkit/configurationmanager.py +680 -0
  37. ccai/agentkit/configurationschema.py +224 -0
  38. ccai/agentkit/configurationschemafield.py +217 -0
  39. ccai/agentkit/configurationserializer.py +163 -0
  40. ccai/agentkit/configurationvalidationresult.py +118 -0
  41. ccai/agentkit/contextsummarizer.py +170 -0
  42. ccai/agentkit/contextwindow.py +264 -0
  43. ccai/agentkit/contextwindowstrategy.py +57 -0
  44. ccai/agentkit/correlationcontext.py +156 -0
  45. ccai/agentkit/costestimate.py +139 -0
  46. ccai/agentkit/costtracker.py +121 -0
  47. ccai/agentkit/deadletterentry.py +134 -0
  48. ccai/agentkit/deadletterquery.py +119 -0
  49. ccai/agentkit/deadletterqueue.py +123 -0
  50. ccai/agentkit/deadletterstore.py +86 -0
  51. ccai/agentkit/debuginspector.py +211 -0
  52. ccai/agentkit/debugsession.py +223 -0
  53. ccai/agentkit/defaultvaluestrategy.py +43 -0
  54. ccai/agentkit/degradationchangeevent.py +120 -0
  55. ccai/agentkit/degradationlevel.py +44 -0
  56. ccai/agentkit/degradationmanager.py +212 -0
  57. ccai/agentkit/degradationpolicy.py +130 -0
  58. ccai/agentkit/degradationstrategy.py +66 -0
  59. ccai/agentkit/embeddingprovider.py +106 -0
  60. ccai/agentkit/errors.py +95 -0
  61. ccai/agentkit/executionrecord.py +213 -0
  62. ccai/agentkit/executionrecorder.py +180 -0
  63. ccai/agentkit/executionrecording.py +230 -0
  64. ccai/agentkit/executionreplayer.py +187 -0
  65. ccai/agentkit/expirationpolicy.py +116 -0
  66. ccai/agentkit/fallbackchain.py +82 -0
  67. ccai/agentkit/featureflag.py +154 -0
  68. ccai/agentkit/featureflagevaluator.py +109 -0
  69. ccai/agentkit/featureflagregistry.py +258 -0
  70. ccai/agentkit/filedeadletterstore.py +112 -0
  71. ccai/agentkit/filememorystore.py +291 -0
  72. ccai/agentkit/healthcheck.py +46 -0
  73. ccai/agentkit/healthcheckregistry.py +51 -0
  74. ccai/agentkit/healthmonitor.py +258 -0
  75. ccai/agentkit/healthstatus.py +105 -0
  76. ccai/agentkit/hotreloadmonitor.py +214 -0
  77. ccai/agentkit/hotreloadpolicy.py +160 -0
  78. ccai/agentkit/inmemorydeadletterstore.py +61 -0
  79. ccai/agentkit/inmemorystore.py +173 -0
  80. ccai/agentkit/jsonmessageserializer.py +89 -0
  81. ccai/agentkit/jsonoutputparser.py +50 -0
  82. ccai/agentkit/lifecyclestate.py +22 -0
  83. ccai/agentkit/llmconversation.py +153 -0
  84. ccai/agentkit/llmerrors.py +193 -0
  85. ccai/agentkit/llmmessage.py +158 -0
  86. ccai/agentkit/llmprovider.py +158 -0
  87. ccai/agentkit/llmproviderregistry.py +200 -0
  88. ccai/agentkit/llmresponse.py +190 -0
  89. ccai/agentkit/llmstreamchunk.py +65 -0
  90. ccai/agentkit/llmstreamhandler.py +48 -0
  91. ccai/agentkit/llmtoolbridge.py +154 -0
  92. ccai/agentkit/logexporter.py +316 -0
  93. ccai/agentkit/logformatter.py +169 -0
  94. ccai/agentkit/memoryconfiguration.py +243 -0
  95. ccai/agentkit/memoryentry.py +254 -0
  96. ccai/agentkit/memoryerrors.py +135 -0
  97. ccai/agentkit/memoryindex.py +213 -0
  98. ccai/agentkit/memoryquery.py +248 -0
  99. ccai/agentkit/memoryqueryresult.py +86 -0
  100. ccai/agentkit/memoryscope.py +54 -0
  101. ccai/agentkit/memorystore.py +260 -0
  102. ccai/agentkit/memorystorehealthcheck.py +33 -0
  103. ccai/agentkit/memorystoreregistry.py +106 -0
  104. ccai/agentkit/message.py +310 -0
  105. ccai/agentkit/messageenvelope.py +144 -0
  106. ccai/agentkit/messagepattern.py +50 -0
  107. ccai/agentkit/messagerouter.py +290 -0
  108. ccai/agentkit/messageserializer.py +106 -0
  109. ccai/agentkit/messageserializerregistry.py +163 -0
  110. ccai/agentkit/messagingconfiguration.py +397 -0
  111. ccai/agentkit/messagingerrors.py +338 -0
  112. ccai/agentkit/metricdefinition.py +164 -0
  113. ccai/agentkit/metricmeasurement.py +178 -0
  114. ccai/agentkit/metricscollector.py +258 -0
  115. ccai/agentkit/metricsexporter.py +303 -0
  116. ccai/agentkit/metricsregistry.py +130 -0
  117. ccai/agentkit/metricssnapshot.py +155 -0
  118. ccai/agentkit/metrictype.py +51 -0
  119. ccai/agentkit/msgpackmessageserializer.py +97 -0
  120. ccai/agentkit/observabilityconfiguration.py +546 -0
  121. ccai/agentkit/observabilityerror.py +67 -0
  122. ccai/agentkit/observabilitypipeline.py +252 -0
  123. ccai/agentkit/outputparser.py +82 -0
  124. ccai/agentkit/outputschema.py +142 -0
  125. ccai/agentkit/parallelbranch.py +87 -0
  126. ccai/agentkit/platform/__init__.py +43 -0
  127. ccai/agentkit/platform/adapters/__init__.py +18 -0
  128. ccai/agentkit/platform/adapters/awsplatformadapter.py +1272 -0
  129. ccai/agentkit/platform/adapters/azureplatformadapter.py +1227 -0
  130. ccai/agentkit/platform/adapters/gcpplatformadapter.py +1257 -0
  131. ccai/agentkit/platform/adapters/localplatformadapter.py +956 -0
  132. ccai/agentkit/platform/adapters/platformadapter.py +139 -0
  133. ccai/agentkit/platform/deployment/__init__.py +15 -0
  134. ccai/agentkit/platform/deployment/deploymentdescriptor.py +234 -0
  135. ccai/agentkit/platform/deployment/deploymentdescriptorparser.py +178 -0
  136. ccai/agentkit/platform/deployment/deploymentdescriptorprinter.py +97 -0
  137. ccai/agentkit/platform/errors.py +224 -0
  138. ccai/agentkit/platform/platformconfiguration.py +412 -0
  139. ccai/agentkit/platform/platformdetector.py +125 -0
  140. ccai/agentkit/platform/platformruntime.py +331 -0
  141. ccai/agentkit/platform/platformserviceregistry.py +149 -0
  142. ccai/agentkit/platform/platformtype.py +60 -0
  143. ccai/agentkit/platform/services/__init__.py +19 -0
  144. ccai/agentkit/platform/services/computeservice.py +112 -0
  145. ccai/agentkit/platform/services/invocationresult.py +173 -0
  146. ccai/agentkit/platform/services/messagingservice.py +135 -0
  147. ccai/agentkit/platform/services/platformservice.py +111 -0
  148. ccai/agentkit/platform/services/queuemessage.py +193 -0
  149. ccai/agentkit/platform/services/storageobject.py +195 -0
  150. ccai/agentkit/platform/services/storageservice.py +159 -0
  151. ccai/agentkit/pluginconfiguration.py +241 -0
  152. ccai/agentkit/plugindependency.py +129 -0
  153. ccai/agentkit/plugindescriptor.py +225 -0
  154. ccai/agentkit/pluginlifecyclehook.py +41 -0
  155. ccai/agentkit/pluginmanager.py +360 -0
  156. ccai/agentkit/pluginstatus.py +47 -0
  157. ccai/agentkit/pooledagent.py +116 -0
  158. ccai/agentkit/prompttemplate.py +221 -0
  159. ccai/agentkit/prompttemplateregistry.py +145 -0
  160. ccai/agentkit/protobufmessageserializer.py +144 -0
  161. ccai/agentkit/pubsubbroker.py +311 -0
  162. ccai/agentkit/py.typed +0 -0
  163. ccai/agentkit/reconnecttransportaction.py +47 -0
  164. ccai/agentkit/regexoutputparser.py +68 -0
  165. ccai/agentkit/requestresponsehandler.py +274 -0
  166. ccai/agentkit/resetcircuitbreakeraction.py +48 -0
  167. ccai/agentkit/resilienceconfiguration.py +191 -0
  168. ccai/agentkit/resilienceerrors.py +103 -0
  169. ccai/agentkit/resiliencepipeline.py +201 -0
  170. ccai/agentkit/retryattempt.py +158 -0
  171. ccai/agentkit/retryexecutor.py +157 -0
  172. ccai/agentkit/retrypolicy.py +195 -0
  173. ccai/agentkit/retryresult.py +186 -0
  174. ccai/agentkit/selfhealingaction.py +42 -0
  175. ccai/agentkit/selfhealingpolicy.py +67 -0
  176. ccai/agentkit/selfhealingresult.py +130 -0
  177. ccai/agentkit/stepexecutionstatus.py +52 -0
  178. ccai/agentkit/stepresult.py +198 -0
  179. ccai/agentkit/steps/__init__.py +9 -0
  180. ccai/agentkit/steps/agentstep.py +247 -0
  181. ccai/agentkit/steps/approvalstep.py +276 -0
  182. ccai/agentkit/steps/llmstep.py +403 -0
  183. ccai/agentkit/steps/subworkflowstep.py +270 -0
  184. ccai/agentkit/steps/toolstep.py +265 -0
  185. ccai/agentkit/structuredlogentry.py +222 -0
  186. ccai/agentkit/structuredlogger.py +189 -0
  187. ccai/agentkit/subscription.py +141 -0
  188. ccai/agentkit/threadedagent.py +94 -0
  189. ccai/agentkit/tokenusage.py +136 -0
  190. ccai/agentkit/tool.py +203 -0
  191. ccai/agentkit/toolchain.py +268 -0
  192. ccai/agentkit/toolchainexecutor.py +134 -0
  193. ccai/agentkit/toolchainresult.py +166 -0
  194. ccai/agentkit/tooldescriptor.py +203 -0
  195. ccai/agentkit/toolerrors.py +160 -0
  196. ccai/agentkit/toolexecutor.py +168 -0
  197. ccai/agentkit/toolfactory.py +60 -0
  198. ccai/agentkit/toolregistry.py +184 -0
  199. ccai/agentkit/toolresult.py +142 -0
  200. ccai/agentkit/toolschema.py +197 -0
  201. ccai/agentkit/tracecontext.py +294 -0
  202. ccai/agentkit/traceevent.py +166 -0
  203. ccai/agentkit/traceexporter.py +305 -0
  204. ccai/agentkit/tracer.py +234 -0
  205. ccai/agentkit/tracespan.py +349 -0
  206. ccai/agentkit/transitionrecord.py +84 -0
  207. ccai/agentkit/transport.py +334 -0
  208. ccai/agentkit/transportconfiguration.py +436 -0
  209. ccai/agentkit/transporthealthcheck.py +36 -0
  210. ccai/agentkit/transportregistry.py +224 -0
  211. ccai/agentkit/transports/__init__.py +28 -0
  212. ccai/agentkit/transports/grpctransport.py +410 -0
  213. ccai/agentkit/transports/httptransport.py +423 -0
  214. ccai/agentkit/transports/messagequeuetransport.py +489 -0
  215. ccai/agentkit/transports/websockettransport.py +473 -0
  216. ccai/agentkit/workflowcheckpoint.py +208 -0
  217. ccai/agentkit/workflowconfiguration.py +295 -0
  218. ccai/agentkit/workflowcontext.py +249 -0
  219. ccai/agentkit/workflowdefinition.py +790 -0
  220. ccai/agentkit/workflowedge.py +128 -0
  221. ccai/agentkit/workflowerrors.py +295 -0
  222. ccai/agentkit/workflowexecutionstatus.py +48 -0
  223. ccai/agentkit/workflowexecutor.py +1121 -0
  224. ccai/agentkit/workflowregistry.py +185 -0
  225. ccai/agentkit/workflowresult.py +250 -0
  226. ccai/agentkit/workflowstep.py +195 -0
  227. codecraftai_agent_kit-1.3.0.dist-info/METADATA +99 -0
  228. codecraftai_agent_kit-1.3.0.dist-info/RECORD +230 -0
  229. codecraftai_agent_kit-1.3.0.dist-info/WHEEL +4 -0
  230. codecraftai_agent_kit-1.3.0.dist-info/licenses/LICENSE.md +204 -0
@@ -0,0 +1,224 @@
1
+ """
2
+ Defines the ConfigurationSchema class representing a declarative schema
3
+ definition for agent configuration in the codecraftai-agent-kit framework.
4
+
5
+ A ConfigurationSchema contains a schema name and a list of
6
+ ConfigurationSchemaField objects defining the expected configuration
7
+ fields. It provides validation of configuration dictionaries against
8
+ the schema definition and supports JSON serialization round-trips.
9
+ """
10
+
11
+ import json
12
+
13
+ from ccai.agentkit.configurationschemafield import ConfigurationSchemaField
14
+ from ccai.agentkit.configurationvalidationresult import ConfigurationValidationResult
15
+
16
+
17
+ class ConfigurationSchema:
18
+ """
19
+ Declarative schema definition describing the expected structure, types,
20
+ constraints, and default values of an Agent's configuration.
21
+
22
+ :param name: The name identifying this configuration schema.
23
+ :param fields: An optional list of ConfigurationSchemaField objects.
24
+ """
25
+
26
+ def __init__(self, name: str, fields: list = None) -> None:
27
+ self._name = name
28
+
29
+ if fields is not None:
30
+ self._fields = list(fields)
31
+ else:
32
+ self._fields = []
33
+
34
+ return
35
+
36
+ @property
37
+ def name(self) -> str:
38
+ """
39
+ The name identifying this configuration schema.
40
+
41
+ :returns: The schema name string.
42
+ """
43
+ return self._name
44
+
45
+ @property
46
+ def fields(self) -> list:
47
+ """
48
+ A defensive copy of the list of ConfigurationSchemaField objects.
49
+
50
+ :returns: A list of ConfigurationSchemaField objects.
51
+ """
52
+ return list(self._fields)
53
+
54
+ @property
55
+ def field_count(self) -> int:
56
+ """
57
+ The count of defined ConfigurationSchemaField objects.
58
+
59
+ :returns: The number of fields in this schema.
60
+ """
61
+ count = len(self._fields)
62
+ return count
63
+
64
+ def add_field(self, field: ConfigurationSchemaField) -> None:
65
+ """
66
+ Adds a ConfigurationSchemaField to the schema definition.
67
+
68
+ :param field: The ConfigurationSchemaField to add.
69
+ """
70
+ self._fields.append(field)
71
+ return
72
+
73
+ def get_field(self, field_path: str) -> ConfigurationSchemaField:
74
+ """
75
+ Returns the ConfigurationSchemaField matching the given field path,
76
+ or None when the field path is not defined.
77
+
78
+ :param field_path: The dot-separated field path string to look up.
79
+
80
+ :returns: The matching ConfigurationSchemaField, or None.
81
+ """
82
+ for field in self._fields:
83
+ if field.field_path == field_path:
84
+ return field
85
+
86
+ return None
87
+
88
+ def validate(self, configuration: dict) -> ConfigurationValidationResult:
89
+ """
90
+ Validates a configuration dictionary against this schema.
91
+
92
+ For each field in the schema, resolves the value from the
93
+ configuration using the dot-separated field path. Reports
94
+ errors for missing required fields and type mismatches.
95
+
96
+ :param configuration: The configuration dictionary to validate.
97
+
98
+ :returns: A ConfigurationValidationResult with the validation outcome.
99
+ """
100
+ errors = []
101
+ warnings = []
102
+
103
+ for field in self._fields:
104
+ value = self._resolve_field_value(configuration, field.field_path)
105
+
106
+ if value is None and field.required is True:
107
+ error_msg = "Required field '{}' is missing".format(field.field_path)
108
+ errors.append(error_msg)
109
+ continue
110
+
111
+ if value is None and field.required is False:
112
+ continue
113
+
114
+ field_errors = field.validate_value(value)
115
+ errors.extend(field_errors)
116
+
117
+ has_errors = len(errors) > 0
118
+ valid = has_errors is False
119
+
120
+ result = ConfigurationValidationResult(valid=valid, errors=errors, warnings=warnings)
121
+ return result
122
+
123
+ def _resolve_field_value(self, configuration: dict, field_path: str):
124
+ """
125
+ Resolves a value from a configuration dictionary using a
126
+ dot-separated field path.
127
+
128
+ :param configuration: The configuration dictionary to traverse.
129
+ :param field_path: The dot-separated path string.
130
+
131
+ :returns: The resolved value, or None if not found.
132
+ """
133
+ parts = field_path.split(".")
134
+ current = configuration
135
+
136
+ for part in parts:
137
+ if isinstance(current, dict) is False:
138
+ return None
139
+
140
+ if part not in current:
141
+ return None
142
+
143
+ current = current[part]
144
+
145
+ return current
146
+
147
+ def as_dict(self) -> dict:
148
+ """
149
+ Returns a dictionary representation of the schema definition.
150
+
151
+ :returns: A dictionary with name and fields.
152
+ """
153
+ result = {
154
+ "name": self._name,
155
+ "fields": [field.as_dict() for field in self._fields],
156
+ }
157
+ return result
158
+
159
+ def to_json(self) -> str:
160
+ """
161
+ Returns a JSON string representation of the schema definition.
162
+
163
+ :returns: A JSON string containing the schema definition.
164
+ """
165
+ data = self.as_dict()
166
+ json_str = json.dumps(data)
167
+ return json_str
168
+
169
+ @classmethod
170
+ def from_dict(cls, data: dict) -> "ConfigurationSchema":
171
+ """
172
+ Constructs a ConfigurationSchema from a dictionary.
173
+
174
+ :param data: A dictionary containing name and fields.
175
+
176
+ :returns: A ConfigurationSchema instance reconstructed from the dictionary.
177
+ """
178
+ name = data["name"]
179
+ fields_data = data.get("fields", [])
180
+
181
+ fields = []
182
+ for field_data in fields_data:
183
+ field = ConfigurationSchemaField.from_dict(field_data)
184
+ fields.append(field)
185
+
186
+ schema = cls(name=name, fields=fields)
187
+ return schema
188
+
189
+ @classmethod
190
+ def from_json(cls, json_str: str) -> "ConfigurationSchema":
191
+ """
192
+ Deserializes a JSON string into a ConfigurationSchema instance.
193
+
194
+ :param json_str: A JSON string containing schema fields.
195
+
196
+ :returns: A ConfigurationSchema instance reconstructed from the JSON data.
197
+ """
198
+ data = json.loads(json_str)
199
+ schema = cls.from_dict(data)
200
+ return schema
201
+
202
+ def __repr__(self) -> str:
203
+ """
204
+ Returns a detailed string representation including class name,
205
+ schema name, and field count.
206
+
207
+ :returns: A repr string for debugging.
208
+ """
209
+ repr_str = "ConfigurationSchema(name={!r}, field_count={})".format(
210
+ self._name, len(self._fields)
211
+ )
212
+ return repr_str
213
+
214
+ def __str__(self) -> str:
215
+ """
216
+ Returns a human-readable summary including the schema name
217
+ and field count.
218
+
219
+ :returns: A human-readable string.
220
+ """
221
+ str_val = "ConfigurationSchema '{}' ({} fields)".format(
222
+ self._name, len(self._fields)
223
+ )
224
+ return str_val
@@ -0,0 +1,217 @@
1
+ """
2
+ Defines the ConfigurationSchemaField class representing a single field
3
+ within a ConfigurationSchema in the codecraftai-agent-kit framework.
4
+
5
+ A ConfigurationSchemaField contains a field path, type constraint,
6
+ required flag, optional default value, description, and optional
7
+ validation callable. The type constraint is validated against a set
8
+ of valid types at construction time.
9
+ """
10
+
11
+ import json
12
+
13
+ from ccai.agentkit.errors import SemanticError
14
+
15
+
16
+ class ConfigurationSchemaField:
17
+ """
18
+ Immutable object representing a single field within a ConfigurationSchema.
19
+
20
+ Contains a field path, type constraint, required flag, optional default,
21
+ description, and optional validation callable.
22
+
23
+ :param field_path: Dot-separated path string identifying the field.
24
+ :param type_constraint: The expected type as a string (one of VALID_TYPES).
25
+ :param required: Whether this field is required in the configuration.
26
+ :param default_value: An optional default value for the field.
27
+ :param description: A human-readable description of the field.
28
+ :param validation_callable: An optional callable for custom validation.
29
+
30
+ :raises: SemanticError
31
+ """
32
+
33
+ VALID_TYPES = {"string", "integer", "float", "boolean", "list", "dictionary"}
34
+
35
+ TYPE_MAP = {
36
+ "string": str,
37
+ "integer": int,
38
+ "float": float,
39
+ "boolean": bool,
40
+ "list": list,
41
+ "dictionary": dict,
42
+ }
43
+
44
+ def __init__(self, field_path: str, type_constraint: str, required: bool = True,
45
+ default_value=None, description: str = "",
46
+ validation_callable=None) -> None:
47
+ self._field_path = field_path
48
+ self._type_constraint = type_constraint
49
+ self._required = required
50
+ self._default_value = default_value
51
+ self._description = description
52
+ self._validation_callable = validation_callable
53
+
54
+ if self._type_constraint not in self.VALID_TYPES:
55
+ errmsg = "Invalid type constraint '{}'. Valid types: {}".format(
56
+ self._type_constraint, ", ".join(sorted(self.VALID_TYPES)))
57
+ raise SemanticError(errmsg)
58
+
59
+ return
60
+
61
+ @property
62
+ def field_path(self) -> str:
63
+ """
64
+ The dot-separated path string identifying this field.
65
+
66
+ :returns: The field path string.
67
+ """
68
+ return self._field_path
69
+
70
+ @property
71
+ def type_constraint(self) -> str:
72
+ """
73
+ The expected type constraint as a string.
74
+
75
+ :returns: The type constraint string.
76
+ """
77
+ return self._type_constraint
78
+
79
+ @property
80
+ def required(self) -> bool:
81
+ """
82
+ Whether this field is required in the configuration.
83
+
84
+ :returns: True if the field is required.
85
+ """
86
+ return self._required
87
+
88
+ @property
89
+ def default_value(self):
90
+ """
91
+ The optional default value for this field.
92
+
93
+ :returns: The default value, or None if not set.
94
+ """
95
+ return self._default_value
96
+
97
+ @property
98
+ def description(self) -> str:
99
+ """
100
+ A human-readable description of this field.
101
+
102
+ :returns: The description string.
103
+ """
104
+ return self._description
105
+
106
+ @property
107
+ def validation_callable(self):
108
+ """
109
+ The optional callable for custom validation.
110
+
111
+ :returns: The validation callable, or None if not set.
112
+ """
113
+ return self._validation_callable
114
+
115
+ def validate_value(self, value) -> list:
116
+ """
117
+ Validates a value against this field's type constraint and custom validation.
118
+
119
+ :param value: The value to validate.
120
+
121
+ :returns: A list of validation error strings. Empty list if valid.
122
+ """
123
+ errors = []
124
+
125
+ expected_type = self.TYPE_MAP[self._type_constraint]
126
+
127
+ is_correct_type = isinstance(value, expected_type)
128
+ if is_correct_type is False:
129
+ actual_type_name = type(value).__name__
130
+ error_msg = "Field '{}': expected type '{}', got '{}'".format(
131
+ self._field_path, self._type_constraint, actual_type_name)
132
+ errors.append(error_msg)
133
+
134
+ if self._validation_callable is not None:
135
+ custom_result = self._validation_callable(value)
136
+ if custom_result is not None:
137
+ if len(custom_result) > 0:
138
+ errors.append(custom_result)
139
+
140
+ return errors
141
+
142
+ def as_dict(self) -> dict:
143
+ """
144
+ Returns a dictionary representation of the field definition,
145
+ excluding the validation callable.
146
+
147
+ :returns: A dictionary with field_path, type_constraint, required,
148
+ default_value, and description.
149
+ """
150
+ result = {
151
+ "field_path": self._field_path,
152
+ "type_constraint": self._type_constraint,
153
+ "required": self._required,
154
+ "default_value": self._default_value,
155
+ "description": self._description,
156
+ }
157
+ return result
158
+
159
+ def to_json(self) -> str:
160
+ """
161
+ Returns a JSON string representation of the field definition,
162
+ excluding the validation callable.
163
+
164
+ :returns: A JSON string containing the field definition.
165
+ """
166
+ data = self.as_dict()
167
+ json_str = json.dumps(data)
168
+ return json_str
169
+
170
+ @classmethod
171
+ def from_dict(cls, data: dict) -> "ConfigurationSchemaField":
172
+ """
173
+ Constructs a ConfigurationSchemaField from a dictionary.
174
+
175
+ :param data: A dictionary containing field_path, type_constraint,
176
+ and optionally required, default_value, and description.
177
+
178
+ :returns: A ConfigurationSchemaField instance reconstructed from the dictionary.
179
+ """
180
+ field_path = data["field_path"]
181
+ type_constraint = data["type_constraint"]
182
+ required = data.get("required", True)
183
+ default_value = data.get("default_value", None)
184
+ description = data.get("description", "")
185
+
186
+ field = cls(
187
+ field_path=field_path,
188
+ type_constraint=type_constraint,
189
+ required=required,
190
+ default_value=default_value,
191
+ description=description,
192
+ )
193
+ return field
194
+
195
+ def __repr__(self) -> str:
196
+ """
197
+ Returns a detailed string representation including class name,
198
+ field path, type constraint, and required flag.
199
+
200
+ :returns: A repr string for debugging.
201
+ """
202
+ repr_str = "ConfigurationSchemaField(field_path={!r}, type_constraint={!r}, required={})".format(
203
+ self._field_path, self._type_constraint, self._required
204
+ )
205
+ return repr_str
206
+
207
+ def __str__(self) -> str:
208
+ """
209
+ Returns a human-readable summary including the field path,
210
+ type, and description.
211
+
212
+ :returns: A human-readable string.
213
+ """
214
+ str_val = "ConfigurationSchemaField '{}' (type={}, description={})".format(
215
+ self._field_path, self._type_constraint, self._description
216
+ )
217
+ return str_val
@@ -0,0 +1,163 @@
1
+ """
2
+ Defines the ConfigurationSerializer class for serializing and deserializing
3
+ configuration dictionaries to and from JSON and YAML formats.
4
+
5
+ The ConfigurationSerializer provides round-trip fidelity for configuration
6
+ data and excludes resolved credential values from serialized output to
7
+ prevent accidental credential leakage.
8
+ """
9
+
10
+ import json
11
+
12
+ from ccai.agentkit.errors import ConfigurationError
13
+
14
+ try:
15
+ import yaml
16
+ YAML_AVAILABLE = True
17
+ except ImportError:
18
+ YAML_AVAILABLE = False
19
+
20
+
21
+ # Keys containing any of these substrings (case-insensitive) are treated as
22
+ # credential keys and their values are redacted during serialization.
23
+ _CREDENTIAL_KEY_PATTERNS = ("credential", "secret", "password", "token")
24
+
25
+ # Placeholder string used to replace redacted credential values.
26
+ _REDACTED_PLACEHOLDER = "[REDACTED]"
27
+
28
+
29
+ class ConfigurationSerializer:
30
+ """
31
+ Serializes and deserializes configuration dictionaries to and from
32
+ JSON and YAML formats with round-trip fidelity.
33
+ Excludes resolved credential values from serialized output.
34
+ """
35
+
36
+ def to_json(self, configuration: dict) -> str:
37
+ """
38
+ Serializes a configuration dictionary to a JSON string.
39
+ Credential values are filtered out and replaced with a
40
+ redacted placeholder before serialization.
41
+
42
+ :param configuration: The configuration dictionary.
43
+
44
+ :returns: A JSON string representation.
45
+ """
46
+ filtered = self._filter_credentials(configuration)
47
+ json_str = json.dumps(filtered)
48
+ return json_str
49
+
50
+ def from_json(self, json_str: str) -> dict:
51
+ """
52
+ Deserializes a JSON string to a configuration dictionary.
53
+
54
+ :param json_str: The JSON string.
55
+
56
+ :returns: A configuration dictionary.
57
+ :raises: ConfigurationError
58
+ """
59
+ try:
60
+ result = json.loads(json_str)
61
+ except (json.JSONDecodeError, TypeError) as err:
62
+ errmsg = "Failed to parse JSON configuration: {}".format(str(err))
63
+ raise ConfigurationError(errmsg, cause=err) from err
64
+
65
+ return result
66
+
67
+ def to_yaml(self, configuration: dict) -> str:
68
+ """
69
+ Serializes a configuration dictionary to a YAML string.
70
+ Credential values are filtered out and replaced with a
71
+ redacted placeholder before serialization.
72
+
73
+ :param configuration: The configuration dictionary.
74
+
75
+ :returns: A YAML string representation.
76
+ :raises: ConfigurationError
77
+ """
78
+ if YAML_AVAILABLE is not True:
79
+ errmsg = "PyYAML is not available. Install PyYAML to use YAML serialization."
80
+ raise ConfigurationError(errmsg)
81
+
82
+ filtered = self._filter_credentials(configuration)
83
+ yaml_str = yaml.dump(filtered, default_flow_style=False)
84
+ return yaml_str
85
+
86
+ def from_yaml(self, yaml_str: str) -> dict:
87
+ """
88
+ Deserializes a YAML string to a configuration dictionary.
89
+
90
+ :param yaml_str: The YAML string.
91
+
92
+ :returns: A configuration dictionary.
93
+ :raises: ConfigurationError
94
+ """
95
+ if YAML_AVAILABLE is not True:
96
+ errmsg = "PyYAML is not available. Install PyYAML to use YAML deserialization."
97
+ raise ConfigurationError(errmsg)
98
+
99
+ try:
100
+ result = yaml.safe_load(yaml_str)
101
+ except yaml.YAMLError as err:
102
+ errmsg = "Failed to parse YAML configuration: {}".format(str(err))
103
+ raise ConfigurationError(errmsg, cause=err) from err
104
+
105
+ if result is None:
106
+ result = {}
107
+
108
+ return result
109
+
110
+ def _filter_credentials(self, configuration: dict) -> dict:
111
+ """
112
+ Recursively filters credential values from a configuration dictionary.
113
+ Keys containing credential-related substrings are replaced with a
114
+ redacted placeholder. Values that are dicts containing a '_resolved'
115
+ key are also replaced with the redacted placeholder.
116
+
117
+ :param configuration: The configuration dictionary to filter.
118
+
119
+ :returns: A new dictionary with credential values redacted.
120
+ """
121
+ filtered = {}
122
+
123
+ for key, value in configuration.items():
124
+ key_lower = key.lower()
125
+ is_credential_key = False
126
+
127
+ for pattern in _CREDENTIAL_KEY_PATTERNS:
128
+ if pattern in key_lower:
129
+ is_credential_key = True
130
+ break
131
+
132
+ if is_credential_key is True:
133
+ filtered[key] = _REDACTED_PLACEHOLDER
134
+ continue
135
+
136
+ if isinstance(value, dict):
137
+ if "_resolved" in value:
138
+ filtered[key] = _REDACTED_PLACEHOLDER
139
+ else:
140
+ filtered[key] = self._filter_credentials(value)
141
+ continue
142
+
143
+ filtered[key] = value
144
+
145
+ return filtered
146
+
147
+ def __repr__(self) -> str:
148
+ """
149
+ Returns a detailed string representation including the class name.
150
+
151
+ :returns: A repr string for debugging.
152
+ """
153
+ repr_str = "ConfigurationSerializer()"
154
+ return repr_str
155
+
156
+ def __str__(self) -> str:
157
+ """
158
+ Returns a human-readable summary including the supported format names.
159
+
160
+ :returns: A human-readable string.
161
+ """
162
+ str_val = "ConfigurationSerializer (formats: json, yaml)"
163
+ return str_val