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,136 @@
1
+ """
2
+ Defines the TokenUsage class for immutable token count records.
3
+
4
+ TokenUsage encapsulates the prompt token count, completion token count,
5
+ and total token count for a single LLM request. The total must equal
6
+ the sum of prompt and completion tokens.
7
+ """
8
+
9
+ from ccai.agentkit.errors import SemanticError
10
+
11
+
12
+ class TokenUsage:
13
+ """
14
+ Immutable record of token counts for a single LLM request.
15
+
16
+ Provides read-only access to prompt tokens, completion tokens,
17
+ and total tokens. Validates that the total equals the sum of
18
+ prompt and completion tokens at construction time.
19
+
20
+ :param prompt_tokens: Number of prompt tokens consumed.
21
+ :param completion_tokens: Number of completion tokens consumed.
22
+ :param total_tokens: Total token count.
23
+ """
24
+
25
+ def __init__(self, prompt_tokens: int, completion_tokens: int, total_tokens: int) -> None:
26
+ expected_total = prompt_tokens + completion_tokens
27
+
28
+ if total_tokens != expected_total:
29
+ errmsg = "TokenUsage total_tokens ({}) does not equal prompt_tokens ({}) + completion_tokens ({}) = {}.".format(
30
+ total_tokens, prompt_tokens, completion_tokens, expected_total
31
+ )
32
+ raise SemanticError(errmsg)
33
+
34
+ self._prompt_tokens = prompt_tokens
35
+ self._completion_tokens = completion_tokens
36
+ self._total_tokens = total_tokens
37
+ return
38
+
39
+ @property
40
+ def prompt_tokens(self) -> int:
41
+ """
42
+ The number of prompt tokens consumed.
43
+
44
+ :returns: The prompt token count as an integer.
45
+ """
46
+ rtnval = self._prompt_tokens
47
+ return rtnval
48
+
49
+ @property
50
+ def completion_tokens(self) -> int:
51
+ """
52
+ The number of completion tokens consumed.
53
+
54
+ :returns: The completion token count as an integer.
55
+ """
56
+ rtnval = self._completion_tokens
57
+ return rtnval
58
+
59
+ @property
60
+ def total_tokens(self) -> int:
61
+ """
62
+ The total token count.
63
+
64
+ :returns: The total token count as an integer.
65
+ """
66
+ rtnval = self._total_tokens
67
+ return rtnval
68
+
69
+ def as_dict(self) -> dict:
70
+ """
71
+ Returns a dictionary representation of the token counts.
72
+
73
+ :returns: A dictionary with prompt_tokens, completion_tokens,
74
+ and total_tokens keys.
75
+ """
76
+ rtnval = {
77
+ "prompt_tokens": self._prompt_tokens,
78
+ "completion_tokens": self._completion_tokens,
79
+ "total_tokens": self._total_tokens
80
+ }
81
+ return rtnval
82
+
83
+ def __add__(self, other: "TokenUsage") -> "TokenUsage":
84
+ """
85
+ Returns a new TokenUsage whose counts are the sum of the two operands.
86
+
87
+ :param other: The other TokenUsage to add.
88
+
89
+ :returns: A new TokenUsage with summed counts.
90
+ """
91
+ rtnval = TokenUsage(
92
+ prompt_tokens=self._prompt_tokens + other._prompt_tokens,
93
+ completion_tokens=self._completion_tokens + other._completion_tokens,
94
+ total_tokens=self._total_tokens + other._total_tokens
95
+ )
96
+ return rtnval
97
+
98
+ def __eq__(self, other) -> bool:
99
+ """
100
+ Checks equality based on all three token count fields.
101
+
102
+ :param other: The object to compare against.
103
+
104
+ :returns: True if all token counts are equal, False otherwise.
105
+ """
106
+ if not isinstance(other, TokenUsage):
107
+ return False
108
+
109
+ rtnval = (self._prompt_tokens == other._prompt_tokens and
110
+ self._completion_tokens == other._completion_tokens and
111
+ self._total_tokens == other._total_tokens)
112
+ return rtnval
113
+
114
+ def __repr__(self) -> str:
115
+ """
116
+ Returns a detailed string representation including the class name
117
+ and all token counts.
118
+
119
+ :returns: A repr string for debugging.
120
+ """
121
+ rtnval = "{}(prompt={}, completion={}, total={})".format(
122
+ self.__class__.__name__, self._prompt_tokens,
123
+ self._completion_tokens, self._total_tokens
124
+ )
125
+ return rtnval
126
+
127
+ def __str__(self) -> str:
128
+ """
129
+ Returns a human-readable summary of the token counts.
130
+
131
+ :returns: A human-readable string.
132
+ """
133
+ rtnval = "Tokens: {} prompt, {} completion, {} total".format(
134
+ self._prompt_tokens, self._completion_tokens, self._total_tokens
135
+ )
136
+ return rtnval
ccai/agentkit/tool.py ADDED
@@ -0,0 +1,203 @@
1
+ """
2
+ Defines the Tool abstract base class for all tools in the codecraftai-agent-kit
3
+ framework.
4
+
5
+ Tool provides a declarative interface for defining tool behavior through
6
+ class-level constants (TOOL_NAME, TOOL_DESCRIPTION, TOOL_VERSION,
7
+ INPUT_SCHEMA, OUTPUT_SCHEMA) and an abstract execute method that subclasses
8
+ must implement.
9
+
10
+ The Tool base class validates class-level schema declarations at construction
11
+ time, supports optional tags and credential names, and exposes a descriptor
12
+ property for metadata discovery.
13
+ """
14
+
15
+ import abc
16
+ import os
17
+
18
+ from ccai.agentkit.errors import SemanticError
19
+ from ccai.agentkit.toolschema import ToolSchema
20
+ from ccai.agentkit.tooldescriptor import ToolDescriptor
21
+
22
+
23
+ class Tool(abc.ABC):
24
+ """
25
+ Abstract base class for all tools. Subclasses define tool behavior
26
+ by implementing the execute method and declaring schemas via
27
+ class-level constants.
28
+
29
+ :param tags: An optional dictionary of key-value tags for categorization.
30
+ :param credential_names: An optional list of credential reference names
31
+ that this tool requires for execution.
32
+ """
33
+
34
+ TOOL_NAME: str = None
35
+ TOOL_DESCRIPTION: str = ""
36
+ TOOL_VERSION: str = "1.0.0"
37
+ INPUT_SCHEMA: dict = None
38
+ OUTPUT_SCHEMA: dict = None
39
+
40
+ def __init__(self, tags: dict = None, credential_names: list = None) -> None:
41
+ self._name = self.TOOL_NAME
42
+ self._description = self.TOOL_DESCRIPTION
43
+ self._version = self.TOOL_VERSION
44
+ self._input_schema = None
45
+ self._output_schema = None
46
+
47
+ if tags is not None:
48
+ self._tags = dict(tags)
49
+ else:
50
+ self._tags = {}
51
+
52
+ if credential_names is not None:
53
+ self._credential_names = list(credential_names)
54
+ else:
55
+ self._credential_names = []
56
+
57
+ if self._name is None:
58
+ errmsg = "A Tool name is required. Set TOOL_NAME on the class or pass name to constructor."
59
+ raise SemanticError(errmsg)
60
+
61
+ if self.INPUT_SCHEMA is not None:
62
+ try:
63
+ self._input_schema = ToolSchema.from_dict(schema_data=self.INPUT_SCHEMA)
64
+ except Exception as schema_err:
65
+ errmsg_lines = [
66
+ "Invalid INPUT_SCHEMA on tool '{}'.".format(self._name),
67
+ "Schema validation detail: {}".format(str(schema_err)),
68
+ ]
69
+ errmsg = os.linesep.join(errmsg_lines)
70
+ raise SemanticError(errmsg) from schema_err
71
+
72
+ if self.OUTPUT_SCHEMA is not None:
73
+ try:
74
+ self._output_schema = ToolSchema.from_dict(schema_data=self.OUTPUT_SCHEMA)
75
+ except Exception as schema_err:
76
+ errmsg_lines = [
77
+ "Invalid OUTPUT_SCHEMA on tool '{}'.".format(self._name),
78
+ "Schema validation detail: {}".format(str(schema_err)),
79
+ ]
80
+ errmsg = os.linesep.join(errmsg_lines)
81
+ raise SemanticError(errmsg) from schema_err
82
+
83
+ return
84
+
85
+ @property
86
+ def name(self) -> str:
87
+ """
88
+ The tool name.
89
+
90
+ :returns: The name string.
91
+ """
92
+ rtnval = self._name
93
+ return rtnval
94
+
95
+ @property
96
+ def description(self) -> str:
97
+ """
98
+ A human-readable description of the tool.
99
+
100
+ :returns: The description string.
101
+ """
102
+ rtnval = self._description
103
+ return rtnval
104
+
105
+ @property
106
+ def version(self) -> str:
107
+ """
108
+ The version string for the tool.
109
+
110
+ :returns: The version string.
111
+ """
112
+ rtnval = self._version
113
+ return rtnval
114
+
115
+ @property
116
+ def input_schema(self) -> ToolSchema:
117
+ """
118
+ The ToolSchema defining the tool's input parameters.
119
+
120
+ :returns: A ToolSchema instance, or None if no input schema is defined.
121
+ """
122
+ rtnval = self._input_schema
123
+ return rtnval
124
+
125
+ @property
126
+ def output_schema(self) -> ToolSchema:
127
+ """
128
+ The ToolSchema defining the tool's output parameters.
129
+
130
+ :returns: A ToolSchema instance, or None if no output schema is defined.
131
+ """
132
+ rtnval = self._output_schema
133
+ return rtnval
134
+
135
+ @property
136
+ def tags(self) -> dict:
137
+ """
138
+ A defensive copy of the tags dictionary.
139
+
140
+ :returns: A dictionary of key-value tags.
141
+ """
142
+ rtnval = dict(self._tags)
143
+ return rtnval
144
+
145
+ @property
146
+ def credential_names(self) -> list:
147
+ """
148
+ A defensive copy of the credential names list.
149
+
150
+ :returns: A list of credential reference name strings.
151
+ """
152
+ rtnval = list(self._credential_names)
153
+ return rtnval
154
+
155
+ @property
156
+ def descriptor(self) -> ToolDescriptor:
157
+ """
158
+ Returns a ToolDescriptor containing this Tool's metadata.
159
+
160
+ :returns: A ToolDescriptor instance.
161
+ """
162
+ rtnval = ToolDescriptor(
163
+ name=self._name,
164
+ description=self._description,
165
+ version=self._version,
166
+ input_schema=self._input_schema,
167
+ output_schema=self._output_schema,
168
+ tags=self._tags,
169
+ )
170
+ return rtnval
171
+
172
+ @abc.abstractmethod
173
+ def execute(self, inputs: dict, credentials: dict = None) -> dict:
174
+ """
175
+ Executes the tool with the given validated inputs.
176
+
177
+ :param inputs: Validated input dictionary.
178
+ :param credentials: Optional dictionary of resolved credentials.
179
+
180
+ :returns: Output dictionary.
181
+ """
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 = "{}(name='{}', version='{}')".format(
191
+ self.__class__.__name__, 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 = "{} - {}".format(self._name, self._description)
203
+ return rtnval
@@ -0,0 +1,268 @@
1
+ """
2
+ Defines the ToolChain class for composing ordered tool pipelines
3
+ in the codecraftai-agent-kit framework.
4
+
5
+ A ToolChain is an ordered sequence of tool references with optional
6
+ input-output mappings that define how the output fields of one step
7
+ map to the input fields of the next step. The chain validates tool
8
+ existence and schema compatibility at construction time.
9
+ """
10
+
11
+ import os
12
+
13
+ from ccai.agentkit.errors import SemanticError
14
+ from ccai.agentkit.toolerrors import ToolValidationError
15
+ from ccai.agentkit.toolregistry import ToolRegistry
16
+
17
+
18
+ class ToolChain:
19
+ """
20
+ An ordered sequence of tool references with input-output mappings
21
+ defining a pipeline.
22
+
23
+ Each step is a dictionary with:
24
+ - tool_name (str, required): Name of the tool to execute.
25
+ - mappings (dict, optional): Maps output field names from the
26
+ previous step to input field names for this step.
27
+
28
+ :param name: Name of the chain.
29
+ :param steps: List of dicts with 'tool_name' and optional 'mappings' keys.
30
+ :param registry: The ToolRegistry to resolve tool names against.
31
+
32
+ :raises: SemanticError if a tool name is not found.
33
+ :raises: ToolValidationError if schema compatibility fails.
34
+ """
35
+
36
+ def __init__(self, name: str, steps: list, registry: ToolRegistry) -> None:
37
+ self._name = name
38
+ self._steps = list(steps)
39
+ self._registry = registry
40
+
41
+ self._validate_steps()
42
+
43
+ return
44
+
45
+ @property
46
+ def name(self) -> str:
47
+ """
48
+ The name of this tool chain.
49
+
50
+ :returns: The chain name string.
51
+ """
52
+ rtnval = self._name
53
+ return rtnval
54
+
55
+ @property
56
+ def tool_names(self) -> list:
57
+ """
58
+ The ordered list of tool names in the chain.
59
+
60
+ :returns: A list of tool name strings.
61
+ """
62
+ names = []
63
+
64
+ for step in self._steps:
65
+ tool_name = step["tool_name"]
66
+ names.append(tool_name)
67
+
68
+ rtnval = names
69
+ return rtnval
70
+
71
+ @property
72
+ def step_count(self) -> int:
73
+ """
74
+ The number of steps in the chain.
75
+
76
+ :returns: The count of steps.
77
+ """
78
+ rtnval = len(self._steps)
79
+ return rtnval
80
+
81
+ def _validate_steps(self) -> None:
82
+ """
83
+ Validates that all tool names exist in the registry and that
84
+ schema compatibility holds between consecutive steps according
85
+ to the provided mappings.
86
+
87
+ :raises: SemanticError if a tool name is not found in the registry.
88
+ :raises: ToolValidationError if schema compatibility fails between steps.
89
+ """
90
+ self._validate_tool_existence()
91
+ self._validate_schema_compatibility()
92
+
93
+ return
94
+
95
+ def _validate_tool_existence(self) -> None:
96
+ """
97
+ Validates that every tool name referenced in the steps exists
98
+ in the registry.
99
+
100
+ :raises: SemanticError if a tool name is not found.
101
+ """
102
+ for step in self._steps:
103
+ tool_name = step["tool_name"]
104
+ tool = self._registry.lookup(tool_name=tool_name)
105
+
106
+ if tool is None:
107
+ errmsg_lines = [
108
+ "ToolChain '{}' references a tool that is not registered.".format(self._name),
109
+ " Missing tool name: '{}'".format(tool_name),
110
+ ]
111
+ errmsg = os.linesep.join(errmsg_lines)
112
+ raise SemanticError(errmsg)
113
+
114
+ return
115
+
116
+ def _validate_schema_compatibility(self) -> None:
117
+ """
118
+ Validates schema compatibility between consecutive steps based
119
+ on the mappings defined in each step.
120
+
121
+ For each consecutive pair of steps where the second step has
122
+ mappings, checks that:
123
+ 1. Each source field in the mappings exists in the output schema
124
+ of the previous step.
125
+ 2. The type of the source field in the output schema is compatible
126
+ with the type of the target field in the input schema of the
127
+ next step.
128
+
129
+ :raises: ToolValidationError if schema compatibility fails.
130
+ """
131
+ step_count = len(self._steps)
132
+
133
+ if step_count < 2:
134
+ return
135
+
136
+ for step_index in range(1, step_count):
137
+ current_step = self._steps[step_index]
138
+ previous_step = self._steps[step_index - 1]
139
+
140
+ mappings = current_step.get("mappings", None)
141
+
142
+ if mappings is None:
143
+ continue
144
+
145
+ has_mappings = len(mappings) > 0
146
+
147
+ if has_mappings is not True:
148
+ continue
149
+
150
+ previous_tool_name = previous_step["tool_name"]
151
+ current_tool_name = current_step["tool_name"]
152
+
153
+ previous_tool = self._registry.lookup(tool_name=previous_tool_name)
154
+ current_tool = self._registry.lookup(tool_name=current_tool_name)
155
+
156
+ previous_output_schema = previous_tool.output_schema
157
+ current_input_schema = current_tool.input_schema
158
+
159
+ self._check_mapping_compatibility(
160
+ previous_tool_name=previous_tool_name,
161
+ current_tool_name=current_tool_name,
162
+ previous_output_schema=previous_output_schema,
163
+ current_input_schema=current_input_schema,
164
+ mappings=mappings,
165
+ step_index=step_index,
166
+ )
167
+
168
+ return
169
+
170
+ def _check_mapping_compatibility(
171
+ self,
172
+ previous_tool_name: str,
173
+ current_tool_name: str,
174
+ previous_output_schema,
175
+ current_input_schema,
176
+ mappings: dict,
177
+ step_index: int,
178
+ ) -> None:
179
+ """
180
+ Checks that the mappings between two consecutive steps are
181
+ compatible with the output schema of the previous step and
182
+ the input schema of the next step.
183
+
184
+ :param previous_tool_name: The name of the previous step's tool.
185
+ :param current_tool_name: The name of the current step's tool.
186
+ :param previous_output_schema: The output ToolSchema of the previous step.
187
+ :param current_input_schema: The input ToolSchema of the current step.
188
+ :param mappings: The mappings dict from the current step.
189
+ :param step_index: The index of the current step in the chain.
190
+
191
+ :raises: ToolValidationError if a mapping is incompatible.
192
+ """
193
+ output_params_by_name = self._build_param_lookup(schema=previous_output_schema)
194
+ input_params_by_name = self._build_param_lookup(schema=current_input_schema)
195
+
196
+ errors = []
197
+
198
+ for source_field, target_field in mappings.items():
199
+ source_exists = source_field in output_params_by_name
200
+ target_exists = target_field in input_params_by_name
201
+
202
+ if source_exists is not True:
203
+ errmsg = "Mapping source field '{}' not found in output schema of tool '{}'.".format(
204
+ source_field, previous_tool_name
205
+ )
206
+ errors.append(errmsg)
207
+ continue
208
+
209
+ if target_exists is not True:
210
+ errmsg = "Mapping target field '{}' not found in input schema of tool '{}'.".format(
211
+ target_field, current_tool_name
212
+ )
213
+ errors.append(errmsg)
214
+ continue
215
+
216
+ source_type = output_params_by_name[source_field]["type"]
217
+ target_type = input_params_by_name[target_field]["type"]
218
+
219
+ types_match = source_type == target_type
220
+
221
+ if types_match is not True:
222
+ errmsg = "Type mismatch in mapping '{}' -> '{}': output type '{}' from tool '{}' is incompatible with input type '{}' on tool '{}'.".format(
223
+ source_field, target_field, source_type, previous_tool_name, target_type, current_tool_name
224
+ )
225
+ errors.append(errmsg)
226
+
227
+ has_errors = len(errors) > 0
228
+
229
+ if has_errors is True:
230
+ error_context_lines = [
231
+ "Schema compatibility check failed between step {} ('{}') and step {} ('{}') in chain '{}'.".format(
232
+ step_index - 1, previous_tool_name, step_index, current_tool_name, self._name
233
+ )
234
+ ]
235
+
236
+ for error_item in errors:
237
+ error_context_lines.append(" - {}".format(error_item))
238
+
239
+ direction = "input"
240
+ raise ToolValidationError(
241
+ tool_name=current_tool_name,
242
+ direction=direction,
243
+ errors=errors,
244
+ )
245
+
246
+ return
247
+
248
+ def _build_param_lookup(self, schema) -> dict:
249
+ """
250
+ Builds a dictionary mapping parameter names to their definitions
251
+ from a ToolSchema instance.
252
+
253
+ :param schema: A ToolSchema instance, or None.
254
+
255
+ :returns: A dictionary mapping parameter name strings to parameter
256
+ definition dictionaries.
257
+ """
258
+ params_by_name = {}
259
+
260
+ if schema is not None:
261
+ parameters = schema.parameters
262
+
263
+ for param_def in parameters:
264
+ param_name = param_def["name"]
265
+ params_by_name[param_name] = param_def
266
+
267
+ rtnval = params_by_name
268
+ return rtnval