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,134 @@
1
+ """
2
+ Defines the ToolChainExecutor class for sequential execution of tool chains.
3
+
4
+ ToolChainExecutor runs each step in a ToolChain in sequence through the
5
+ ToolExecutor, applying input-output mappings between steps, and returns
6
+ a ToolChainResult with the overall outcome.
7
+ """
8
+
9
+ import time
10
+
11
+ from ccai.agentkit.toolchain import ToolChain
12
+ from ccai.agentkit.toolchainresult import ToolChainResult
13
+ from ccai.agentkit.toolexecutor import ToolExecutor
14
+
15
+
16
+ class ToolChainExecutor:
17
+ """
18
+ Executes a ToolChain by running each step in sequence through the ToolExecutor.
19
+
20
+ For each step in the chain, the executor looks up the tool from the chain's
21
+ registry, applies input-output mappings from the previous step's output,
22
+ and invokes the tool via the ToolExecutor. If any step fails, execution
23
+ stops and a failure result is returned. If all steps succeed, a success
24
+ result is returned with the final step's output.
25
+
26
+ :param tool_executor: The ToolExecutor instance used to execute each step.
27
+ """
28
+
29
+ def __init__(self, tool_executor: ToolExecutor) -> None:
30
+ self._tool_executor = tool_executor
31
+ return
32
+
33
+ def execute(self, chain: ToolChain, initial_inputs: dict,
34
+ agent_identifier: str = None) -> ToolChainResult:
35
+ """
36
+ Executes the chain steps in order, applying mappings between steps.
37
+
38
+ For each step, the tool is looked up from the chain's registry.
39
+ If the step has mappings and is not the first step, the previous
40
+ step's output fields are mapped to the current step's input fields.
41
+ If no mappings are defined, the previous step's output is passed
42
+ directly as the current step's inputs.
43
+
44
+ :param chain: The ToolChain to execute.
45
+ :param initial_inputs: Input dictionary for the first step.
46
+ :param agent_identifier: Optional agent identifier for logging.
47
+
48
+ :returns: A ToolChainResult with the overall outcome.
49
+ """
50
+ start_time = time.time()
51
+
52
+ step_results = []
53
+ current_inputs = initial_inputs
54
+ current_output = None
55
+
56
+ steps = chain._steps
57
+
58
+ for step_index, step in enumerate(steps):
59
+ tool_name = step["tool_name"]
60
+ tool = chain._registry.lookup(tool_name=tool_name)
61
+
62
+ is_first_step = step_index == 0
63
+
64
+ if is_first_step is not True:
65
+ mappings = step.get("mappings", None)
66
+ current_inputs = self._apply_mappings(
67
+ mappings=mappings,
68
+ previous_output=current_output,
69
+ )
70
+
71
+ result = self._tool_executor.execute(
72
+ tool=tool,
73
+ inputs=current_inputs,
74
+ agent_identifier=agent_identifier,
75
+ )
76
+
77
+ step_results.append(result)
78
+
79
+ if result.is_success is not True:
80
+ end_time = time.time()
81
+ duration = end_time - start_time
82
+
83
+ rtnval = ToolChainResult(
84
+ chain_name=chain.name,
85
+ status=ToolChainResult.STATUS_FAILURE,
86
+ step_results=step_results,
87
+ duration=duration,
88
+ error=result.error,
89
+ )
90
+ return rtnval
91
+
92
+ current_output = result.output
93
+
94
+ end_time = time.time()
95
+ duration = end_time - start_time
96
+
97
+ rtnval = ToolChainResult(
98
+ chain_name=chain.name,
99
+ status=ToolChainResult.STATUS_SUCCESS,
100
+ output=current_output,
101
+ step_results=step_results,
102
+ duration=duration,
103
+ )
104
+ return rtnval
105
+
106
+ def _apply_mappings(self, mappings: dict, previous_output: dict) -> dict:
107
+ """
108
+ Applies input-output mappings to transform the previous step's
109
+ output into the current step's inputs.
110
+
111
+ If mappings is None or empty, the previous output is passed
112
+ directly as the current inputs. Otherwise, a new dictionary
113
+ is constructed where each target input field is populated from
114
+ the corresponding source output field.
115
+
116
+ :param mappings: A dictionary mapping output field names to input
117
+ field names, or None for pass-through.
118
+ :param previous_output: The output dictionary from the previous step.
119
+
120
+ :returns: The input dictionary for the current step.
121
+ """
122
+ has_mappings = mappings is not None and len(mappings) > 0
123
+
124
+ if has_mappings is not True:
125
+ rtnval = previous_output
126
+ return rtnval
127
+
128
+ mapped_inputs = {}
129
+
130
+ for output_field, input_field in mappings.items():
131
+ mapped_inputs[input_field] = previous_output[output_field]
132
+
133
+ rtnval = mapped_inputs
134
+ return rtnval
@@ -0,0 +1,166 @@
1
+ """
2
+ Defines the ToolChainResult class for immutable tool chain execution results.
3
+
4
+ ToolChainResult encapsulates the outcome of a tool chain execution,
5
+ including the chain name, overall execution status, final output value,
6
+ list of individual ToolResult objects for each step, total execution
7
+ duration, and any error information.
8
+ """
9
+
10
+
11
+ class ToolChainResult:
12
+ """
13
+ Immutable result of a tool chain execution.
14
+
15
+ Provides read-only access to the chain name, overall execution status,
16
+ final output dictionary, list of individual ToolResult objects for each
17
+ step, total execution duration in seconds, and any error that occurred
18
+ during execution.
19
+
20
+ :param chain_name: The name of the executed tool chain.
21
+ :param status: The overall execution status, either STATUS_SUCCESS or STATUS_FAILURE.
22
+ :param output: An optional dictionary containing the final step output on success.
23
+ :param step_results: An optional list of ToolResult objects for each step.
24
+ :param duration: The total execution duration in seconds as a float.
25
+ :param error: An optional exception from the failed step.
26
+ """
27
+
28
+ STATUS_SUCCESS = "success"
29
+ STATUS_FAILURE = "failure"
30
+
31
+ def __init__(self, chain_name: str, status: str, output: dict = None,
32
+ step_results: list = None, duration: float = 0.0,
33
+ error: Exception = None) -> None:
34
+ self._chain_name = chain_name
35
+ self._status = status
36
+ self._output = output
37
+ self._step_results = list(step_results) if step_results is not None else []
38
+ self._duration = duration
39
+ self._error = error
40
+ return
41
+
42
+ @property
43
+ def chain_name(self) -> str:
44
+ """
45
+ The name of the executed tool chain.
46
+
47
+ :returns: The chain name string.
48
+ """
49
+ rtnval = self._chain_name
50
+ return rtnval
51
+
52
+ @property
53
+ def status(self) -> str:
54
+ """
55
+ The overall execution status of the tool chain.
56
+
57
+ :returns: The status string, either STATUS_SUCCESS or STATUS_FAILURE.
58
+ """
59
+ rtnval = self._status
60
+ return rtnval
61
+
62
+ @property
63
+ def output(self) -> dict:
64
+ """
65
+ The final step output dictionary from the tool chain execution.
66
+
67
+ :returns: A dictionary containing the final output, or None if no output.
68
+ """
69
+ rtnval = self._output
70
+ return rtnval
71
+
72
+ @property
73
+ def step_results(self) -> list:
74
+ """
75
+ The list of individual ToolResult objects for each step in the chain.
76
+
77
+ :returns: A list of ToolResult objects.
78
+ """
79
+ rtnval = list(self._step_results)
80
+ return rtnval
81
+
82
+ @property
83
+ def duration(self) -> float:
84
+ """
85
+ The total execution duration in seconds.
86
+
87
+ :returns: The duration as a float.
88
+ """
89
+ rtnval = self._duration
90
+ return rtnval
91
+
92
+ @property
93
+ def error(self) -> Exception:
94
+ """
95
+ The exception from the failed step, if any.
96
+
97
+ :returns: An Exception instance, or None if execution succeeded.
98
+ """
99
+ rtnval = self._error
100
+ return rtnval
101
+
102
+ @property
103
+ def is_success(self) -> bool:
104
+ """
105
+ Whether the tool chain execution completed successfully.
106
+
107
+ :returns: True when the status equals STATUS_SUCCESS, False otherwise.
108
+ """
109
+ rtnval = self._status == self.STATUS_SUCCESS
110
+ return rtnval
111
+
112
+ def as_dict(self) -> dict:
113
+ """
114
+ Returns a dictionary representation of the chain result fields.
115
+
116
+ The step_results field is included as a list of dictionaries
117
+ produced by calling as_dict() on each ToolResult. The error
118
+ field is included as its string representation, or None if
119
+ no error occurred.
120
+
121
+ :returns: A dictionary containing all chain result fields.
122
+ """
123
+ step_results_dicts = []
124
+ for step_result in self._step_results:
125
+ step_dict = step_result.as_dict()
126
+ step_results_dicts.append(step_dict)
127
+
128
+ if self._error is not None:
129
+ error_value = str(self._error)
130
+ else:
131
+ error_value = None
132
+
133
+ rtnval = {
134
+ "chain_name": self._chain_name,
135
+ "status": self._status,
136
+ "output": self._output,
137
+ "step_results": step_results_dicts,
138
+ "duration": self._duration,
139
+ "error": error_value,
140
+ }
141
+ return rtnval
142
+
143
+ def __repr__(self) -> str:
144
+ """
145
+ Returns a detailed string representation including the class name,
146
+ chain name, and overall execution status.
147
+
148
+ :returns: A repr string for debugging.
149
+ """
150
+ rtnval = "ToolChainResult(chain_name='{}', status='{}')".format(
151
+ self._chain_name, self._status
152
+ )
153
+ return rtnval
154
+
155
+ def __str__(self) -> str:
156
+ """
157
+ Returns a human-readable summary including the chain name,
158
+ status, step count, and total duration.
159
+
160
+ :returns: A human-readable string.
161
+ """
162
+ step_count = len(self._step_results)
163
+ rtnval = "ToolChainResult: {} - {} ({} steps, duration: {:.3f}s)".format(
164
+ self._chain_name, self._status, step_count, self._duration
165
+ )
166
+ return rtnval
@@ -0,0 +1,203 @@
1
+ """
2
+ Defines the ToolDescriptor class for immutable tool metadata.
3
+
4
+ ToolDescriptor provides a read-only view of a Tool's name, description,
5
+ version, input schema, output schema, and tags. It supports serialization
6
+ to and from JSON for storage and transmission of tool definitions.
7
+ """
8
+
9
+ import json
10
+
11
+ from ccai.agentkit.toolschema import ToolSchema
12
+
13
+
14
+ class ToolDescriptor:
15
+ """
16
+ Immutable metadata object describing a Tool's capabilities
17
+ without exposing the Tool implementation.
18
+
19
+ :param name: The tool name string.
20
+ :param description: A human-readable description of the tool.
21
+ :param version: The version string for the tool.
22
+ :param input_schema: A ToolSchema defining the tool's input parameters.
23
+ :param output_schema: A ToolSchema defining the tool's output parameters.
24
+ :param tags: An optional dictionary of key-value tags for categorization.
25
+ """
26
+
27
+ def __init__(self, name: str, description: str, version: str,
28
+ input_schema: ToolSchema, output_schema: ToolSchema,
29
+ tags: dict = None) -> None:
30
+ self._name = name
31
+ self._description = description
32
+ self._version = version
33
+ self._input_schema = input_schema
34
+ self._output_schema = output_schema
35
+
36
+ if tags is not None:
37
+ self._tags = dict(tags)
38
+ else:
39
+ self._tags = {}
40
+
41
+ return
42
+
43
+ @property
44
+ def name(self) -> str:
45
+ """
46
+ The tool name.
47
+
48
+ :returns: The name string.
49
+ """
50
+ rtnval = self._name
51
+ return rtnval
52
+
53
+ @property
54
+ def description(self) -> str:
55
+ """
56
+ A human-readable description of the tool.
57
+
58
+ :returns: The description string.
59
+ """
60
+ rtnval = self._description
61
+ return rtnval
62
+
63
+ @property
64
+ def version(self) -> str:
65
+ """
66
+ The version string for the tool.
67
+
68
+ :returns: The version string.
69
+ """
70
+ rtnval = self._version
71
+ return rtnval
72
+
73
+ @property
74
+ def input_schema(self) -> ToolSchema:
75
+ """
76
+ The ToolSchema defining the tool's input parameters.
77
+
78
+ :returns: A ToolSchema instance, or None if no input schema is defined.
79
+ """
80
+ rtnval = self._input_schema
81
+ return rtnval
82
+
83
+ @property
84
+ def output_schema(self) -> ToolSchema:
85
+ """
86
+ The ToolSchema defining the tool's output parameters.
87
+
88
+ :returns: A ToolSchema instance, or None if no output schema is defined.
89
+ """
90
+ rtnval = self._output_schema
91
+ return rtnval
92
+
93
+ @property
94
+ def tags(self) -> dict:
95
+ """
96
+ A defensive copy of the tags dictionary.
97
+
98
+ :returns: A dictionary of key-value tags.
99
+ """
100
+ rtnval = dict(self._tags)
101
+ return rtnval
102
+
103
+ def as_dict(self) -> dict:
104
+ """
105
+ Returns a dictionary representation of the descriptor fields.
106
+
107
+ Input and output schemas are included as their as_dict()
108
+ representations, or None if the schema is None.
109
+
110
+ :returns: A dictionary containing all descriptor fields.
111
+ """
112
+ if self._input_schema is not None:
113
+ input_schema_data = self._input_schema.as_dict()
114
+ else:
115
+ input_schema_data = None
116
+
117
+ if self._output_schema is not None:
118
+ output_schema_data = self._output_schema.as_dict()
119
+ else:
120
+ output_schema_data = None
121
+
122
+ rtnval = {
123
+ "name": self._name,
124
+ "description": self._description,
125
+ "version": self._version,
126
+ "input_schema": input_schema_data,
127
+ "output_schema": output_schema_data,
128
+ "tags": dict(self._tags),
129
+ }
130
+ return rtnval
131
+
132
+ def to_json(self) -> str:
133
+ """
134
+ Returns a JSON string representation of the descriptor fields.
135
+
136
+ :returns: A JSON string containing the descriptor data.
137
+ """
138
+ data = self.as_dict()
139
+ rtnval = json.dumps(data)
140
+ return rtnval
141
+
142
+ @classmethod
143
+ def from_json(cls, json_str: str) -> "ToolDescriptor":
144
+ """
145
+ Deserializes a JSON string into a ToolDescriptor instance.
146
+
147
+ Reconstructs ToolSchema instances from the embedded schema
148
+ dictionaries in the JSON data.
149
+
150
+ :param json_str: A JSON string containing descriptor data.
151
+
152
+ :returns: A ToolDescriptor instance reconstructed from the JSON data.
153
+ """
154
+ data = json.loads(json_str)
155
+
156
+ name = data["name"]
157
+ description = data["description"]
158
+ version = data["version"]
159
+ tags = data.get("tags", {})
160
+
161
+ input_schema_data = data.get("input_schema", None)
162
+ if input_schema_data is not None:
163
+ input_schema = ToolSchema.from_dict(schema_data=input_schema_data)
164
+ else:
165
+ input_schema = None
166
+
167
+ output_schema_data = data.get("output_schema", None)
168
+ if output_schema_data is not None:
169
+ output_schema = ToolSchema.from_dict(schema_data=output_schema_data)
170
+ else:
171
+ output_schema = None
172
+
173
+ rtnval = cls(
174
+ name=name,
175
+ description=description,
176
+ version=version,
177
+ input_schema=input_schema,
178
+ output_schema=output_schema,
179
+ tags=tags,
180
+ )
181
+ return rtnval
182
+
183
+ def __repr__(self) -> str:
184
+ """
185
+ Returns a detailed string representation including the class name,
186
+ tool name, and version.
187
+
188
+ :returns: A repr string for debugging.
189
+ """
190
+ rtnval = "ToolDescriptor(name='{}', version='{}')".format(
191
+ self._name, self._version
192
+ )
193
+ return rtnval
194
+
195
+ def __str__(self) -> str:
196
+ """
197
+ Returns a human-readable summary including the tool name
198
+ and description.
199
+
200
+ :returns: A human-readable string.
201
+ """
202
+ rtnval = "ToolDescriptor: {} - {}".format(self._name, self._description)
203
+ return rtnval
@@ -0,0 +1,160 @@
1
+ """
2
+ Exception classes for the Tool & Capability System.
3
+
4
+ Provides ToolValidationError for input/output schema validation failures
5
+ and ToolExecutionError for tool runtime execution failures.
6
+ """
7
+
8
+ import os
9
+
10
+ from typing import List
11
+
12
+ from ccai.agentkit.errors import AgentKitError
13
+
14
+
15
+ class ToolValidationError(AgentKitError):
16
+ """
17
+ Raised when input or output validation against a ToolSchema fails.
18
+
19
+ :param tool_name: The name of the tool that failed validation.
20
+ :param direction: The validation direction, either "input" or "output".
21
+ :param errors: A list of validation error strings.
22
+ """
23
+
24
+ def __init__(self, tool_name: str, direction: str, errors: List[str]) -> None:
25
+ self._tool_name = tool_name
26
+ self._direction = direction
27
+ self._errors = list(errors)
28
+
29
+ errmsg = self._format_message()
30
+ super().__init__(errmsg)
31
+
32
+ return
33
+
34
+ @property
35
+ def tool_name(self) -> str:
36
+ """
37
+ The name of the tool that failed validation.
38
+
39
+ :returns: The tool name string.
40
+ """
41
+ return self._tool_name
42
+
43
+ @property
44
+ def direction(self) -> str:
45
+ """
46
+ The validation direction, either "input" or "output".
47
+
48
+ :returns: The direction string.
49
+ """
50
+ return self._direction
51
+
52
+ @property
53
+ def errors(self) -> List[str]:
54
+ """
55
+ The list of validation error strings.
56
+
57
+ :returns: A list of error strings.
58
+ """
59
+ return list(self._errors)
60
+
61
+ def _format_message(self) -> str:
62
+ """
63
+ Builds a descriptive error message from the tool name, direction,
64
+ and errors list.
65
+
66
+ :returns: A formatted error message string.
67
+ """
68
+ error_count = len(self._errors)
69
+
70
+ msg_lines = [
71
+ "Tool '{}' {} validation failed with {} error(s).".format(
72
+ self._tool_name, self._direction, error_count
73
+ )
74
+ ]
75
+
76
+ if len(self._errors) > 0:
77
+ msg_lines.append("ERRORS:")
78
+ for error_item in self._errors:
79
+ msg_lines.append(" - {}".format(error_item))
80
+
81
+ errmsg = os.linesep.join(msg_lines)
82
+
83
+ return errmsg
84
+
85
+ def __repr__(self) -> str:
86
+ """
87
+ Returns a detailed string representation including class name,
88
+ tool name, direction, and error count.
89
+
90
+ :returns: A repr string for debugging.
91
+ """
92
+ error_count = len(self._errors)
93
+ repr_str = "{}(tool_name={!r}, direction={!r}, error_count={})".format(
94
+ self.__class__.__name__, self._tool_name, self._direction, error_count
95
+ )
96
+ return repr_str
97
+
98
+
99
+ class ToolExecutionError(AgentKitError):
100
+ """
101
+ Raised when a Tool's execute method encounters a runtime error.
102
+
103
+ :param tool_name: The name of the tool that failed execution.
104
+ :param cause: The original exception that caused the failure.
105
+ """
106
+
107
+ def __init__(self, tool_name: str, cause: Exception) -> None:
108
+ self._tool_name = tool_name
109
+ self._cause = cause
110
+
111
+ errmsg = self._format_message()
112
+ super().__init__(errmsg, cause=cause)
113
+
114
+ return
115
+
116
+ @property
117
+ def tool_name(self) -> str:
118
+ """
119
+ The name of the tool that failed execution.
120
+
121
+ :returns: The tool name string.
122
+ """
123
+ return self._tool_name
124
+
125
+ @property
126
+ def cause(self) -> Exception:
127
+ """
128
+ The original exception that caused the failure.
129
+
130
+ :returns: The cause exception.
131
+ """
132
+ return self._cause
133
+
134
+ def _format_message(self) -> str:
135
+ """
136
+ Builds a descriptive error message from the tool name and the
137
+ cause exception.
138
+
139
+ :returns: A formatted error message string.
140
+ """
141
+ msg_lines = [
142
+ "Tool '{}' execution failed.".format(self._tool_name),
143
+ "CAUSE: {}".format(str(self._cause))
144
+ ]
145
+
146
+ errmsg = os.linesep.join(msg_lines)
147
+
148
+ return errmsg
149
+
150
+ def __repr__(self) -> str:
151
+ """
152
+ Returns a detailed string representation including class name,
153
+ tool name, and cause.
154
+
155
+ :returns: A repr string for debugging.
156
+ """
157
+ repr_str = "{}(tool_name={!r}, cause={!r})".format(
158
+ self.__class__.__name__, self._tool_name, self._cause
159
+ )
160
+ return repr_str