codecraftai-agent-kit 1.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ccai/agentkit/__init__.py +304 -0
- ccai/agentkit/addressresolver.py +177 -0
- ccai/agentkit/agent.py +2945 -0
- ccai/agentkit/agentaddress.py +187 -0
- ccai/agentkit/agentconfiguration.py +581 -0
- ccai/agentkit/agentidentity.py +152 -0
- ccai/agentkit/agentregistry.py +221 -0
- ccai/agentkit/agentstatemachine.py +134 -0
- ccai/agentkit/alternateproviderstrategy.py +36 -0
- ccai/agentkit/approvalgate.py +238 -0
- ccai/agentkit/approvalrequest.py +250 -0
- ccai/agentkit/asyncagent.py +187 -0
- ccai/agentkit/breakpoint.py +150 -0
- ccai/agentkit/builtinagentmetrics.py +114 -0
- ccai/agentkit/builtins/__init__.py +11 -0
- ccai/agentkit/builtins/filereadtool.py +90 -0
- ccai/agentkit/builtins/filewritetool.py +93 -0
- ccai/agentkit/builtins/httprequesttool.py +169 -0
- ccai/agentkit/builtins/shellcommandtool.py +190 -0
- ccai/agentkit/cachedresultstrategy.py +43 -0
- ccai/agentkit/checkpointmanager.py +219 -0
- ccai/agentkit/circuitbreaker.py +230 -0
- ccai/agentkit/circuitbreakerconfiguration.py +158 -0
- ccai/agentkit/circuitbreakerevent.py +158 -0
- ccai/agentkit/circuitbreakerhealthcheck.py +32 -0
- ccai/agentkit/circuitbreakerregistry.py +116 -0
- ccai/agentkit/circuitbreakerstate.py +44 -0
- ccai/agentkit/clearcacheaction.py +46 -0
- ccai/agentkit/concurrencybridge.py +143 -0
- ccai/agentkit/concurrencymodel.py +19 -0
- ccai/agentkit/conditionevaluator.py +594 -0
- ccai/agentkit/configurationchangeevent.py +179 -0
- ccai/agentkit/configurationerrors.py +152 -0
- ccai/agentkit/configurationlayer.py +160 -0
- ccai/agentkit/configurationlayersource.py +46 -0
- ccai/agentkit/configurationmanager.py +680 -0
- ccai/agentkit/configurationschema.py +224 -0
- ccai/agentkit/configurationschemafield.py +217 -0
- ccai/agentkit/configurationserializer.py +163 -0
- ccai/agentkit/configurationvalidationresult.py +118 -0
- ccai/agentkit/contextsummarizer.py +170 -0
- ccai/agentkit/contextwindow.py +264 -0
- ccai/agentkit/contextwindowstrategy.py +57 -0
- ccai/agentkit/correlationcontext.py +156 -0
- ccai/agentkit/costestimate.py +139 -0
- ccai/agentkit/costtracker.py +121 -0
- ccai/agentkit/deadletterentry.py +134 -0
- ccai/agentkit/deadletterquery.py +119 -0
- ccai/agentkit/deadletterqueue.py +123 -0
- ccai/agentkit/deadletterstore.py +86 -0
- ccai/agentkit/debuginspector.py +211 -0
- ccai/agentkit/debugsession.py +223 -0
- ccai/agentkit/defaultvaluestrategy.py +43 -0
- ccai/agentkit/degradationchangeevent.py +120 -0
- ccai/agentkit/degradationlevel.py +44 -0
- ccai/agentkit/degradationmanager.py +212 -0
- ccai/agentkit/degradationpolicy.py +130 -0
- ccai/agentkit/degradationstrategy.py +66 -0
- ccai/agentkit/embeddingprovider.py +106 -0
- ccai/agentkit/errors.py +95 -0
- ccai/agentkit/executionrecord.py +213 -0
- ccai/agentkit/executionrecorder.py +180 -0
- ccai/agentkit/executionrecording.py +230 -0
- ccai/agentkit/executionreplayer.py +187 -0
- ccai/agentkit/expirationpolicy.py +116 -0
- ccai/agentkit/fallbackchain.py +82 -0
- ccai/agentkit/featureflag.py +154 -0
- ccai/agentkit/featureflagevaluator.py +109 -0
- ccai/agentkit/featureflagregistry.py +258 -0
- ccai/agentkit/filedeadletterstore.py +112 -0
- ccai/agentkit/filememorystore.py +291 -0
- ccai/agentkit/healthcheck.py +46 -0
- ccai/agentkit/healthcheckregistry.py +51 -0
- ccai/agentkit/healthmonitor.py +258 -0
- ccai/agentkit/healthstatus.py +105 -0
- ccai/agentkit/hotreloadmonitor.py +214 -0
- ccai/agentkit/hotreloadpolicy.py +160 -0
- ccai/agentkit/inmemorydeadletterstore.py +61 -0
- ccai/agentkit/inmemorystore.py +173 -0
- ccai/agentkit/jsonmessageserializer.py +89 -0
- ccai/agentkit/jsonoutputparser.py +50 -0
- ccai/agentkit/lifecyclestate.py +22 -0
- ccai/agentkit/llmconversation.py +153 -0
- ccai/agentkit/llmerrors.py +193 -0
- ccai/agentkit/llmmessage.py +158 -0
- ccai/agentkit/llmprovider.py +158 -0
- ccai/agentkit/llmproviderregistry.py +200 -0
- ccai/agentkit/llmresponse.py +190 -0
- ccai/agentkit/llmstreamchunk.py +65 -0
- ccai/agentkit/llmstreamhandler.py +48 -0
- ccai/agentkit/llmtoolbridge.py +154 -0
- ccai/agentkit/logexporter.py +316 -0
- ccai/agentkit/logformatter.py +169 -0
- ccai/agentkit/memoryconfiguration.py +243 -0
- ccai/agentkit/memoryentry.py +254 -0
- ccai/agentkit/memoryerrors.py +135 -0
- ccai/agentkit/memoryindex.py +213 -0
- ccai/agentkit/memoryquery.py +248 -0
- ccai/agentkit/memoryqueryresult.py +86 -0
- ccai/agentkit/memoryscope.py +54 -0
- ccai/agentkit/memorystore.py +260 -0
- ccai/agentkit/memorystorehealthcheck.py +33 -0
- ccai/agentkit/memorystoreregistry.py +106 -0
- ccai/agentkit/message.py +310 -0
- ccai/agentkit/messageenvelope.py +144 -0
- ccai/agentkit/messagepattern.py +50 -0
- ccai/agentkit/messagerouter.py +290 -0
- ccai/agentkit/messageserializer.py +106 -0
- ccai/agentkit/messageserializerregistry.py +163 -0
- ccai/agentkit/messagingconfiguration.py +397 -0
- ccai/agentkit/messagingerrors.py +338 -0
- ccai/agentkit/metricdefinition.py +164 -0
- ccai/agentkit/metricmeasurement.py +178 -0
- ccai/agentkit/metricscollector.py +258 -0
- ccai/agentkit/metricsexporter.py +303 -0
- ccai/agentkit/metricsregistry.py +130 -0
- ccai/agentkit/metricssnapshot.py +155 -0
- ccai/agentkit/metrictype.py +51 -0
- ccai/agentkit/msgpackmessageserializer.py +97 -0
- ccai/agentkit/observabilityconfiguration.py +546 -0
- ccai/agentkit/observabilityerror.py +67 -0
- ccai/agentkit/observabilitypipeline.py +252 -0
- ccai/agentkit/outputparser.py +82 -0
- ccai/agentkit/outputschema.py +142 -0
- ccai/agentkit/parallelbranch.py +87 -0
- ccai/agentkit/platform/__init__.py +43 -0
- ccai/agentkit/platform/adapters/__init__.py +18 -0
- ccai/agentkit/platform/adapters/awsplatformadapter.py +1272 -0
- ccai/agentkit/platform/adapters/azureplatformadapter.py +1227 -0
- ccai/agentkit/platform/adapters/gcpplatformadapter.py +1257 -0
- ccai/agentkit/platform/adapters/localplatformadapter.py +956 -0
- ccai/agentkit/platform/adapters/platformadapter.py +139 -0
- ccai/agentkit/platform/deployment/__init__.py +15 -0
- ccai/agentkit/platform/deployment/deploymentdescriptor.py +234 -0
- ccai/agentkit/platform/deployment/deploymentdescriptorparser.py +178 -0
- ccai/agentkit/platform/deployment/deploymentdescriptorprinter.py +97 -0
- ccai/agentkit/platform/errors.py +224 -0
- ccai/agentkit/platform/platformconfiguration.py +412 -0
- ccai/agentkit/platform/platformdetector.py +125 -0
- ccai/agentkit/platform/platformruntime.py +331 -0
- ccai/agentkit/platform/platformserviceregistry.py +149 -0
- ccai/agentkit/platform/platformtype.py +60 -0
- ccai/agentkit/platform/services/__init__.py +19 -0
- ccai/agentkit/platform/services/computeservice.py +112 -0
- ccai/agentkit/platform/services/invocationresult.py +173 -0
- ccai/agentkit/platform/services/messagingservice.py +135 -0
- ccai/agentkit/platform/services/platformservice.py +111 -0
- ccai/agentkit/platform/services/queuemessage.py +193 -0
- ccai/agentkit/platform/services/storageobject.py +195 -0
- ccai/agentkit/platform/services/storageservice.py +159 -0
- ccai/agentkit/pluginconfiguration.py +241 -0
- ccai/agentkit/plugindependency.py +129 -0
- ccai/agentkit/plugindescriptor.py +225 -0
- ccai/agentkit/pluginlifecyclehook.py +41 -0
- ccai/agentkit/pluginmanager.py +360 -0
- ccai/agentkit/pluginstatus.py +47 -0
- ccai/agentkit/pooledagent.py +116 -0
- ccai/agentkit/prompttemplate.py +221 -0
- ccai/agentkit/prompttemplateregistry.py +145 -0
- ccai/agentkit/protobufmessageserializer.py +144 -0
- ccai/agentkit/pubsubbroker.py +311 -0
- ccai/agentkit/py.typed +0 -0
- ccai/agentkit/reconnecttransportaction.py +47 -0
- ccai/agentkit/regexoutputparser.py +68 -0
- ccai/agentkit/requestresponsehandler.py +274 -0
- ccai/agentkit/resetcircuitbreakeraction.py +48 -0
- ccai/agentkit/resilienceconfiguration.py +191 -0
- ccai/agentkit/resilienceerrors.py +103 -0
- ccai/agentkit/resiliencepipeline.py +201 -0
- ccai/agentkit/retryattempt.py +158 -0
- ccai/agentkit/retryexecutor.py +157 -0
- ccai/agentkit/retrypolicy.py +195 -0
- ccai/agentkit/retryresult.py +186 -0
- ccai/agentkit/selfhealingaction.py +42 -0
- ccai/agentkit/selfhealingpolicy.py +67 -0
- ccai/agentkit/selfhealingresult.py +130 -0
- ccai/agentkit/stepexecutionstatus.py +52 -0
- ccai/agentkit/stepresult.py +198 -0
- ccai/agentkit/steps/__init__.py +9 -0
- ccai/agentkit/steps/agentstep.py +247 -0
- ccai/agentkit/steps/approvalstep.py +276 -0
- ccai/agentkit/steps/llmstep.py +403 -0
- ccai/agentkit/steps/subworkflowstep.py +270 -0
- ccai/agentkit/steps/toolstep.py +265 -0
- ccai/agentkit/structuredlogentry.py +222 -0
- ccai/agentkit/structuredlogger.py +189 -0
- ccai/agentkit/subscription.py +141 -0
- ccai/agentkit/threadedagent.py +94 -0
- ccai/agentkit/tokenusage.py +136 -0
- ccai/agentkit/tool.py +203 -0
- ccai/agentkit/toolchain.py +268 -0
- ccai/agentkit/toolchainexecutor.py +134 -0
- ccai/agentkit/toolchainresult.py +166 -0
- ccai/agentkit/tooldescriptor.py +203 -0
- ccai/agentkit/toolerrors.py +160 -0
- ccai/agentkit/toolexecutor.py +168 -0
- ccai/agentkit/toolfactory.py +60 -0
- ccai/agentkit/toolregistry.py +184 -0
- ccai/agentkit/toolresult.py +142 -0
- ccai/agentkit/toolschema.py +197 -0
- ccai/agentkit/tracecontext.py +294 -0
- ccai/agentkit/traceevent.py +166 -0
- ccai/agentkit/traceexporter.py +305 -0
- ccai/agentkit/tracer.py +234 -0
- ccai/agentkit/tracespan.py +349 -0
- ccai/agentkit/transitionrecord.py +84 -0
- ccai/agentkit/transport.py +334 -0
- ccai/agentkit/transportconfiguration.py +436 -0
- ccai/agentkit/transporthealthcheck.py +36 -0
- ccai/agentkit/transportregistry.py +224 -0
- ccai/agentkit/transports/__init__.py +28 -0
- ccai/agentkit/transports/grpctransport.py +410 -0
- ccai/agentkit/transports/httptransport.py +423 -0
- ccai/agentkit/transports/messagequeuetransport.py +489 -0
- ccai/agentkit/transports/websockettransport.py +473 -0
- ccai/agentkit/workflowcheckpoint.py +208 -0
- ccai/agentkit/workflowconfiguration.py +295 -0
- ccai/agentkit/workflowcontext.py +249 -0
- ccai/agentkit/workflowdefinition.py +790 -0
- ccai/agentkit/workflowedge.py +128 -0
- ccai/agentkit/workflowerrors.py +295 -0
- ccai/agentkit/workflowexecutionstatus.py +48 -0
- ccai/agentkit/workflowexecutor.py +1121 -0
- ccai/agentkit/workflowregistry.py +185 -0
- ccai/agentkit/workflowresult.py +250 -0
- ccai/agentkit/workflowstep.py +195 -0
- codecraftai_agent_kit-1.3.0.dist-info/METADATA +99 -0
- codecraftai_agent_kit-1.3.0.dist-info/RECORD +230 -0
- codecraftai_agent_kit-1.3.0.dist-info/WHEEL +4 -0
- codecraftai_agent_kit-1.3.0.dist-info/licenses/LICENSE.md +204 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the ToolExecutor class for validated tool invocation with
|
|
3
|
+
credential resolution and structured logging.
|
|
4
|
+
|
|
5
|
+
ToolExecutor validates inputs against a tool's input schema, resolves
|
|
6
|
+
credential references via a credential resolver, invokes the tool's
|
|
7
|
+
execute method, validates outputs against the tool's output schema,
|
|
8
|
+
and returns structured ToolResult objects.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import logging
|
|
12
|
+
import os
|
|
13
|
+
import time
|
|
14
|
+
|
|
15
|
+
from ccai.agentkit.errors import ConfigurationError
|
|
16
|
+
from ccai.agentkit.errors import SemanticError
|
|
17
|
+
from ccai.agentkit.tool import Tool
|
|
18
|
+
from ccai.agentkit.toolerrors import ToolValidationError
|
|
19
|
+
from ccai.agentkit.toolresult import ToolResult
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ToolExecutor:
|
|
23
|
+
"""
|
|
24
|
+
Validates inputs, resolves credentials, invokes tools, validates outputs,
|
|
25
|
+
and returns structured ToolResult objects.
|
|
26
|
+
|
|
27
|
+
:param credential_resolver: An optional credential resolver used to resolve
|
|
28
|
+
credential references declared by tools. The resolver must support a
|
|
29
|
+
resolve(name) method that returns the credential value or raises an
|
|
30
|
+
exception on failure.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def __init__(self, credential_resolver=None) -> None:
|
|
34
|
+
self._credential_resolver = credential_resolver
|
|
35
|
+
self._logger = logging.getLogger("ccai.agentkit.tools")
|
|
36
|
+
return
|
|
37
|
+
|
|
38
|
+
def execute(self, tool: Tool, inputs: dict, agent_identifier: str = None) -> ToolResult:
|
|
39
|
+
"""
|
|
40
|
+
Executes a tool with validated inputs and credential resolution.
|
|
41
|
+
|
|
42
|
+
Validates the input dictionary against the tool's input schema,
|
|
43
|
+
resolves any credential references declared by the tool, invokes
|
|
44
|
+
the tool's execute method, validates the output against the tool's
|
|
45
|
+
output schema, and returns a ToolResult with the execution outcome.
|
|
46
|
+
|
|
47
|
+
:param tool: The Tool instance to execute.
|
|
48
|
+
:param inputs: The input dictionary.
|
|
49
|
+
:param agent_identifier: Optional agent identifier for logging.
|
|
50
|
+
|
|
51
|
+
:returns: A ToolResult with the execution outcome.
|
|
52
|
+
:raises: ToolValidationError, ConfigurationError
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
self._logger.info(
|
|
56
|
+
"Executing tool '%s' for agent '%s'",
|
|
57
|
+
tool.name,
|
|
58
|
+
agent_identifier,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
# Step 1: Validate inputs against the tool's input schema
|
|
62
|
+
input_schema = tool.input_schema
|
|
63
|
+
if input_schema is not None:
|
|
64
|
+
input_errors = input_schema.validate(values=inputs)
|
|
65
|
+
if len(input_errors) > 0:
|
|
66
|
+
self._logger.warning(
|
|
67
|
+
"Tool '%s' input validation failed: %s",
|
|
68
|
+
tool.name,
|
|
69
|
+
input_errors,
|
|
70
|
+
)
|
|
71
|
+
raise ToolValidationError(
|
|
72
|
+
tool_name=tool.name,
|
|
73
|
+
direction="input",
|
|
74
|
+
errors=input_errors,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# Step 2: Resolve credential references
|
|
78
|
+
credentials = {}
|
|
79
|
+
credential_names = tool.credential_names
|
|
80
|
+
if len(credential_names) > 0:
|
|
81
|
+
if self._credential_resolver is not None:
|
|
82
|
+
for cred_name in credential_names:
|
|
83
|
+
try:
|
|
84
|
+
resolved_value = self._credential_resolver.resolve(cred_name)
|
|
85
|
+
credentials[cred_name] = resolved_value
|
|
86
|
+
except Exception as resolve_err:
|
|
87
|
+
errmsg_lines = [
|
|
88
|
+
"Failed to resolve credential '{}' for tool '{}'.".format(
|
|
89
|
+
cred_name, tool.name
|
|
90
|
+
),
|
|
91
|
+
"CAUSE: {}".format(str(resolve_err)),
|
|
92
|
+
]
|
|
93
|
+
errmsg = os.linesep.join(errmsg_lines)
|
|
94
|
+
raise ConfigurationError(errmsg, cause=resolve_err) from resolve_err
|
|
95
|
+
else:
|
|
96
|
+
errmsg_lines = [
|
|
97
|
+
"Tool '{}' requires credentials {} but no credential resolver is configured.".format(
|
|
98
|
+
tool.name, credential_names
|
|
99
|
+
),
|
|
100
|
+
]
|
|
101
|
+
errmsg = os.linesep.join(errmsg_lines)
|
|
102
|
+
raise ConfigurationError(errmsg)
|
|
103
|
+
|
|
104
|
+
# Step 3: Execute the tool and measure duration
|
|
105
|
+
start_time = time.time()
|
|
106
|
+
|
|
107
|
+
try:
|
|
108
|
+
output = tool.execute(inputs=inputs, credentials=credentials)
|
|
109
|
+
end_time = time.time()
|
|
110
|
+
duration = end_time - start_time
|
|
111
|
+
|
|
112
|
+
# Step 4: Validate output against the tool's output schema
|
|
113
|
+
output_schema = tool.output_schema
|
|
114
|
+
if output_schema is not None:
|
|
115
|
+
output_errors = output_schema.validate(values=output)
|
|
116
|
+
if len(output_errors) > 0:
|
|
117
|
+
self._logger.warning(
|
|
118
|
+
"Tool '%s' output validation failed: %s",
|
|
119
|
+
tool.name,
|
|
120
|
+
output_errors,
|
|
121
|
+
)
|
|
122
|
+
raise ToolValidationError(
|
|
123
|
+
tool_name=tool.name,
|
|
124
|
+
direction="output",
|
|
125
|
+
errors=output_errors,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
# Step 5: Return success result
|
|
129
|
+
self._logger.info(
|
|
130
|
+
"Tool '%s' executed successfully in %.3f seconds",
|
|
131
|
+
tool.name,
|
|
132
|
+
duration,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
rtnval = ToolResult(
|
|
136
|
+
tool_name=tool.name,
|
|
137
|
+
status=ToolResult.STATUS_SUCCESS,
|
|
138
|
+
output=output,
|
|
139
|
+
duration=duration,
|
|
140
|
+
)
|
|
141
|
+
return rtnval
|
|
142
|
+
|
|
143
|
+
except ToolValidationError:
|
|
144
|
+
raise
|
|
145
|
+
|
|
146
|
+
except SemanticError:
|
|
147
|
+
# SemanticError signals API misuse and must never be swallowed into
|
|
148
|
+
# a failure ToolResult -- let it bubble to the caller.
|
|
149
|
+
raise
|
|
150
|
+
|
|
151
|
+
except Exception as exec_err:
|
|
152
|
+
end_time = time.time()
|
|
153
|
+
duration = end_time - start_time
|
|
154
|
+
|
|
155
|
+
self._logger.warning(
|
|
156
|
+
"Tool '%s' execution failed after %.3f seconds: %s",
|
|
157
|
+
tool.name,
|
|
158
|
+
duration,
|
|
159
|
+
str(exec_err),
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
rtnval = ToolResult(
|
|
163
|
+
tool_name=tool.name,
|
|
164
|
+
status=ToolResult.STATUS_FAILURE,
|
|
165
|
+
duration=duration,
|
|
166
|
+
error=exec_err,
|
|
167
|
+
)
|
|
168
|
+
return rtnval
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Provides the :func:`create_tool` factory function for creating Tool instances
|
|
3
|
+
without requiring subclassing.
|
|
4
|
+
|
|
5
|
+
The factory function creates a dynamic Tool subclass internally that delegates
|
|
6
|
+
the execute method to a provided callable, allowing quick tool creation with
|
|
7
|
+
minimal boilerplate.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from ccai.agentkit.tool import Tool
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def create_tool(name: str, description: str, input_schema: dict,
|
|
14
|
+
output_schema: dict, execute_fn: callable,
|
|
15
|
+
version: str = "1.0.0", tags: dict = None) -> Tool:
|
|
16
|
+
"""
|
|
17
|
+
Factory function that creates a Tool instance without requiring subclassing.
|
|
18
|
+
|
|
19
|
+
Internally creates a dynamic Tool subclass with the provided metadata and
|
|
20
|
+
an execute method that delegates to the provided callable.
|
|
21
|
+
|
|
22
|
+
:param name: Tool name.
|
|
23
|
+
:param description: Tool description.
|
|
24
|
+
:param input_schema: Input schema as a dictionary.
|
|
25
|
+
:param output_schema: Output schema as a dictionary.
|
|
26
|
+
:param execute_fn: Callable implementing the tool logic. Must accept
|
|
27
|
+
(inputs, credentials) and return a dictionary.
|
|
28
|
+
:param version: Tool version string.
|
|
29
|
+
:param tags: Optional tags dictionary.
|
|
30
|
+
|
|
31
|
+
:returns: A Tool instance with the specified configuration.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
bound_execute_fn = execute_fn
|
|
35
|
+
|
|
36
|
+
class DynamicTool(Tool):
|
|
37
|
+
"""
|
|
38
|
+
A dynamically created Tool subclass generated by the create_tool factory.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
TOOL_NAME = name
|
|
42
|
+
TOOL_DESCRIPTION = description
|
|
43
|
+
TOOL_VERSION = version
|
|
44
|
+
INPUT_SCHEMA = input_schema
|
|
45
|
+
OUTPUT_SCHEMA = output_schema
|
|
46
|
+
|
|
47
|
+
def execute(self, inputs: dict, credentials: dict = None) -> dict:
|
|
48
|
+
"""
|
|
49
|
+
Delegates execution to the callable provided to create_tool.
|
|
50
|
+
|
|
51
|
+
:param inputs: Validated input dictionary.
|
|
52
|
+
:param credentials: Optional dictionary of resolved credentials.
|
|
53
|
+
|
|
54
|
+
:returns: Output dictionary from the delegate callable.
|
|
55
|
+
"""
|
|
56
|
+
rtnval = bound_execute_fn(inputs, credentials)
|
|
57
|
+
return rtnval
|
|
58
|
+
|
|
59
|
+
rtnval = DynamicTool(tags=tags)
|
|
60
|
+
return rtnval
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the ToolRegistry class for tracking available Tool instances
|
|
3
|
+
within an Agent in the codecraftai-agent-kit framework.
|
|
4
|
+
|
|
5
|
+
ToolRegistry provides registration, deregistration, lookup by name,
|
|
6
|
+
lookup by tags, enumeration, and descriptor listing operations for
|
|
7
|
+
Tool instances. Each Agent maintains its own ToolRegistry instance.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
|
|
12
|
+
from ccai.agentkit.errors import SemanticError
|
|
13
|
+
from ccai.agentkit.tool import Tool
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ToolRegistry:
|
|
17
|
+
"""
|
|
18
|
+
Registry tracking available Tool instances for an Agent.
|
|
19
|
+
Provides lookup by name, tags, and enumeration.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self) -> None:
|
|
23
|
+
self._tools: dict = {}
|
|
24
|
+
return
|
|
25
|
+
|
|
26
|
+
def register(self, tool: Tool) -> None:
|
|
27
|
+
"""
|
|
28
|
+
Registers a Tool instance using its name as the key.
|
|
29
|
+
|
|
30
|
+
:param tool: The Tool instance to register.
|
|
31
|
+
|
|
32
|
+
:raises: SemanticError if a tool with the same name is already registered.
|
|
33
|
+
"""
|
|
34
|
+
tool_name = tool.name
|
|
35
|
+
is_duplicate = tool_name in self._tools
|
|
36
|
+
|
|
37
|
+
if is_duplicate is True:
|
|
38
|
+
errmsg_lines = [
|
|
39
|
+
"Duplicate tool registration attempted.",
|
|
40
|
+
" Tool name: '{}'".format(tool_name),
|
|
41
|
+
" A tool with this name is already registered.",
|
|
42
|
+
]
|
|
43
|
+
errmsg = os.linesep.join(errmsg_lines)
|
|
44
|
+
raise SemanticError(errmsg)
|
|
45
|
+
|
|
46
|
+
self._tools[tool_name] = tool
|
|
47
|
+
|
|
48
|
+
return
|
|
49
|
+
|
|
50
|
+
def deregister(self, tool_name: str) -> None:
|
|
51
|
+
"""
|
|
52
|
+
Removes a Tool from the registry by its name.
|
|
53
|
+
|
|
54
|
+
If the tool name is not found, the operation is a no-op.
|
|
55
|
+
|
|
56
|
+
:param tool_name: The name of the tool to remove.
|
|
57
|
+
"""
|
|
58
|
+
is_present = tool_name in self._tools
|
|
59
|
+
|
|
60
|
+
if is_present is True:
|
|
61
|
+
del self._tools[tool_name]
|
|
62
|
+
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
def lookup(self, tool_name: str) -> Tool:
|
|
66
|
+
"""
|
|
67
|
+
Looks up a registered Tool by its name.
|
|
68
|
+
|
|
69
|
+
:param tool_name: The name of the tool to look up.
|
|
70
|
+
|
|
71
|
+
:returns: The Tool instance if found, or None if not found.
|
|
72
|
+
"""
|
|
73
|
+
rtnval = self._tools.get(tool_name, None)
|
|
74
|
+
return rtnval
|
|
75
|
+
|
|
76
|
+
def lookup_by_tags(self, **tags: str) -> list:
|
|
77
|
+
"""
|
|
78
|
+
Looks up registered Tools by tag key-value pairs.
|
|
79
|
+
|
|
80
|
+
Returns tools that match ALL provided tag criteria.
|
|
81
|
+
|
|
82
|
+
:param tags: Keyword arguments representing tag key-value pairs to match.
|
|
83
|
+
|
|
84
|
+
:returns: A list of Tool instances matching all tag criteria.
|
|
85
|
+
"""
|
|
86
|
+
matching_tools = []
|
|
87
|
+
|
|
88
|
+
for tool in self._tools.values():
|
|
89
|
+
tool_tags = tool.tags
|
|
90
|
+
all_match = True
|
|
91
|
+
|
|
92
|
+
for tag_key, tag_value in tags.items():
|
|
93
|
+
has_key = tag_key in tool_tags
|
|
94
|
+
|
|
95
|
+
if has_key is not True:
|
|
96
|
+
all_match = False
|
|
97
|
+
break
|
|
98
|
+
|
|
99
|
+
tool_tag_value = tool_tags[tag_key]
|
|
100
|
+
values_match = tool_tag_value == tag_value
|
|
101
|
+
|
|
102
|
+
if values_match is not True:
|
|
103
|
+
all_match = False
|
|
104
|
+
break
|
|
105
|
+
|
|
106
|
+
if all_match is True:
|
|
107
|
+
matching_tools.append(tool)
|
|
108
|
+
|
|
109
|
+
rtnval = matching_tools
|
|
110
|
+
return rtnval
|
|
111
|
+
|
|
112
|
+
def list_all(self) -> list:
|
|
113
|
+
"""
|
|
114
|
+
Returns a list of all registered Tool instances.
|
|
115
|
+
|
|
116
|
+
:returns: A list of all registered Tool instances.
|
|
117
|
+
"""
|
|
118
|
+
rtnval = list(self._tools.values())
|
|
119
|
+
return rtnval
|
|
120
|
+
|
|
121
|
+
def list_descriptors(self) -> list:
|
|
122
|
+
"""
|
|
123
|
+
Returns a list of ToolDescriptor instances for all registered Tools.
|
|
124
|
+
|
|
125
|
+
Provides discovery without exposing Tool implementations.
|
|
126
|
+
|
|
127
|
+
:returns: A list of ToolDescriptor instances.
|
|
128
|
+
"""
|
|
129
|
+
descriptors = []
|
|
130
|
+
|
|
131
|
+
for tool in self._tools.values():
|
|
132
|
+
descriptor = tool.descriptor
|
|
133
|
+
descriptors.append(descriptor)
|
|
134
|
+
|
|
135
|
+
rtnval = descriptors
|
|
136
|
+
return rtnval
|
|
137
|
+
|
|
138
|
+
def has_tool(self, tool_name: str) -> bool:
|
|
139
|
+
"""
|
|
140
|
+
Checks whether a Tool with the given name is registered.
|
|
141
|
+
|
|
142
|
+
:param tool_name: The name of the tool to check.
|
|
143
|
+
|
|
144
|
+
:returns: True if a tool with the given name is registered, False otherwise.
|
|
145
|
+
"""
|
|
146
|
+
rtnval = tool_name in self._tools
|
|
147
|
+
return rtnval
|
|
148
|
+
|
|
149
|
+
def clear(self) -> None:
|
|
150
|
+
"""
|
|
151
|
+
Removes all registered Tools from the registry.
|
|
152
|
+
"""
|
|
153
|
+
self._tools.clear()
|
|
154
|
+
return
|
|
155
|
+
|
|
156
|
+
@property
|
|
157
|
+
def count(self) -> int:
|
|
158
|
+
"""
|
|
159
|
+
The number of currently registered tools.
|
|
160
|
+
|
|
161
|
+
:returns: The count of registered tools.
|
|
162
|
+
"""
|
|
163
|
+
rtnval = len(self._tools)
|
|
164
|
+
return rtnval
|
|
165
|
+
|
|
166
|
+
def __repr__(self) -> str:
|
|
167
|
+
"""
|
|
168
|
+
Returns a detailed string representation including the class name
|
|
169
|
+
and the count of registered tools.
|
|
170
|
+
|
|
171
|
+
:returns: A repr string for debugging.
|
|
172
|
+
"""
|
|
173
|
+
rtnval = "ToolRegistry(count={})".format(self.count)
|
|
174
|
+
return rtnval
|
|
175
|
+
|
|
176
|
+
def __str__(self) -> str:
|
|
177
|
+
"""
|
|
178
|
+
Returns a human-readable summary listing the registered tool names.
|
|
179
|
+
|
|
180
|
+
:returns: A human-readable string.
|
|
181
|
+
"""
|
|
182
|
+
tool_names = list(self._tools.keys())
|
|
183
|
+
rtnval = "ToolRegistry: [{}]".format(", ".join(tool_names))
|
|
184
|
+
return rtnval
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the ToolResult class for immutable tool execution results.
|
|
3
|
+
|
|
4
|
+
ToolResult encapsulates the outcome of a single tool execution,
|
|
5
|
+
including the tool name, execution status, output value, execution
|
|
6
|
+
duration, and any error information.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ToolResult:
|
|
11
|
+
"""
|
|
12
|
+
Immutable result of a single tool execution.
|
|
13
|
+
|
|
14
|
+
Provides read-only access to the tool name, execution status,
|
|
15
|
+
output dictionary, execution duration in seconds, and any
|
|
16
|
+
error that occurred during execution.
|
|
17
|
+
|
|
18
|
+
:param tool_name: The name of the executed tool.
|
|
19
|
+
:param status: The execution status, either STATUS_SUCCESS or STATUS_FAILURE.
|
|
20
|
+
:param output: An optional dictionary containing the tool output on success.
|
|
21
|
+
:param duration: The execution duration in seconds as a float.
|
|
22
|
+
:param error: An optional exception that occurred during execution.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
STATUS_SUCCESS = "success"
|
|
26
|
+
STATUS_FAILURE = "failure"
|
|
27
|
+
|
|
28
|
+
def __init__(self, tool_name: str, status: str, output: dict = None,
|
|
29
|
+
duration: float = 0.0, error: Exception = None) -> None:
|
|
30
|
+
self._tool_name = tool_name
|
|
31
|
+
self._status = status
|
|
32
|
+
self._output = output
|
|
33
|
+
self._duration = duration
|
|
34
|
+
self._error = error
|
|
35
|
+
return
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def tool_name(self) -> str:
|
|
39
|
+
"""
|
|
40
|
+
The name of the executed tool.
|
|
41
|
+
|
|
42
|
+
:returns: The tool name string.
|
|
43
|
+
"""
|
|
44
|
+
rtnval = self._tool_name
|
|
45
|
+
return rtnval
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def status(self) -> str:
|
|
49
|
+
"""
|
|
50
|
+
The execution status of the tool.
|
|
51
|
+
|
|
52
|
+
:returns: The status string, either STATUS_SUCCESS or STATUS_FAILURE.
|
|
53
|
+
"""
|
|
54
|
+
rtnval = self._status
|
|
55
|
+
return rtnval
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def output(self) -> dict:
|
|
59
|
+
"""
|
|
60
|
+
The output dictionary from the tool execution.
|
|
61
|
+
|
|
62
|
+
:returns: A dictionary containing the tool output, or None if no output.
|
|
63
|
+
"""
|
|
64
|
+
rtnval = self._output
|
|
65
|
+
return rtnval
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def duration(self) -> float:
|
|
69
|
+
"""
|
|
70
|
+
The execution duration in seconds.
|
|
71
|
+
|
|
72
|
+
:returns: The duration as a float.
|
|
73
|
+
"""
|
|
74
|
+
rtnval = self._duration
|
|
75
|
+
return rtnval
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def error(self) -> Exception:
|
|
79
|
+
"""
|
|
80
|
+
The exception that occurred during execution, if any.
|
|
81
|
+
|
|
82
|
+
:returns: An Exception instance, or None if execution succeeded.
|
|
83
|
+
"""
|
|
84
|
+
rtnval = self._error
|
|
85
|
+
return rtnval
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def is_success(self) -> bool:
|
|
89
|
+
"""
|
|
90
|
+
Whether the tool execution completed successfully.
|
|
91
|
+
|
|
92
|
+
:returns: True when the status equals STATUS_SUCCESS, False otherwise.
|
|
93
|
+
"""
|
|
94
|
+
rtnval = self._status == self.STATUS_SUCCESS
|
|
95
|
+
return rtnval
|
|
96
|
+
|
|
97
|
+
def as_dict(self) -> dict:
|
|
98
|
+
"""
|
|
99
|
+
Returns a dictionary representation of the result fields.
|
|
100
|
+
|
|
101
|
+
The error field is included as its string representation,
|
|
102
|
+
or None if no error occurred.
|
|
103
|
+
|
|
104
|
+
:returns: A dictionary containing all result fields.
|
|
105
|
+
"""
|
|
106
|
+
if self._error is not None:
|
|
107
|
+
error_value = str(self._error)
|
|
108
|
+
else:
|
|
109
|
+
error_value = None
|
|
110
|
+
|
|
111
|
+
rtnval = {
|
|
112
|
+
"tool_name": self._tool_name,
|
|
113
|
+
"status": self._status,
|
|
114
|
+
"output": self._output,
|
|
115
|
+
"duration": self._duration,
|
|
116
|
+
"error": error_value,
|
|
117
|
+
}
|
|
118
|
+
return rtnval
|
|
119
|
+
|
|
120
|
+
def __repr__(self) -> str:
|
|
121
|
+
"""
|
|
122
|
+
Returns a detailed string representation including the class name,
|
|
123
|
+
tool name, and execution status.
|
|
124
|
+
|
|
125
|
+
:returns: A repr string for debugging.
|
|
126
|
+
"""
|
|
127
|
+
rtnval = "ToolResult(tool_name='{}', status='{}')".format(
|
|
128
|
+
self._tool_name, self._status
|
|
129
|
+
)
|
|
130
|
+
return rtnval
|
|
131
|
+
|
|
132
|
+
def __str__(self) -> str:
|
|
133
|
+
"""
|
|
134
|
+
Returns a human-readable summary including the tool name,
|
|
135
|
+
status, and duration.
|
|
136
|
+
|
|
137
|
+
:returns: A human-readable string.
|
|
138
|
+
"""
|
|
139
|
+
rtnval = "ToolResult: {} - {} (duration: {:.3f}s)".format(
|
|
140
|
+
self._tool_name, self._status, self._duration
|
|
141
|
+
)
|
|
142
|
+
return rtnval
|