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,270 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the SubWorkflowStep class, a concrete WorkflowStep that
|
|
3
|
+
executes a nested WorkflowDefinition as a sub-workflow within
|
|
4
|
+
the parent workflow execution.
|
|
5
|
+
|
|
6
|
+
SubWorkflowStep resolves an input mapping against the parent
|
|
7
|
+
WorkflowContext, creates a new WorkflowExecutor for independent
|
|
8
|
+
execution, runs the sub-workflow, and returns a StepResult
|
|
9
|
+
containing the sub-workflow's final output on success or a
|
|
10
|
+
FAILED status on failure.
|
|
11
|
+
|
|
12
|
+
.. module:: ccai.agentkit.steps.subworkflowstep
|
|
13
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
14
|
+
:synopsis: Built-in workflow step for sub-workflow execution.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import logging
|
|
18
|
+
import time
|
|
19
|
+
|
|
20
|
+
from typing import Optional
|
|
21
|
+
|
|
22
|
+
from ccai.agentkit.stepexecutionstatus import StepExecutionStatus
|
|
23
|
+
from ccai.agentkit.stepresult import StepResult
|
|
24
|
+
from ccai.agentkit.workflowcontext import WorkflowContext
|
|
25
|
+
from ccai.agentkit.workflowdefinition import WorkflowDefinition
|
|
26
|
+
from ccai.agentkit.workflowexecutionstatus import WorkflowExecutionStatus
|
|
27
|
+
from ccai.agentkit.workflowexecutor import WorkflowExecutor
|
|
28
|
+
from ccai.agentkit.workflowstep import WorkflowStep
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class SubWorkflowStep(WorkflowStep):
|
|
32
|
+
"""
|
|
33
|
+
Executes a nested WorkflowDefinition as a sub-workflow.
|
|
34
|
+
|
|
35
|
+
SubWorkflowStep is a concrete WorkflowStep subclass that
|
|
36
|
+
composes workflows hierarchically. At execution time, it
|
|
37
|
+
resolves the input mapping against the parent WorkflowContext
|
|
38
|
+
to build the sub-workflow input dictionary, creates a new
|
|
39
|
+
WorkflowExecutor instance for independent execution, runs
|
|
40
|
+
the sub-workflow, and wraps the result in a StepResult.
|
|
41
|
+
|
|
42
|
+
The sub-workflow gets its own WorkflowExecutor instance,
|
|
43
|
+
which supports independent checkpointing of the sub-workflow
|
|
44
|
+
execution separately from the parent workflow.
|
|
45
|
+
|
|
46
|
+
The input_mapping dictionary maps sub-workflow input names
|
|
47
|
+
to context references. Each value in the mapping is a string
|
|
48
|
+
of the form "inputs.<field_name>" or
|
|
49
|
+
"steps.<step_id>.<field_name>", which is resolved against
|
|
50
|
+
the parent WorkflowContext at execution time.
|
|
51
|
+
|
|
52
|
+
:param step_id: The unique identifier for this step within
|
|
53
|
+
the workflow. Must not be None or empty.
|
|
54
|
+
:param name: The human-readable name for this step. Must
|
|
55
|
+
not be None or empty.
|
|
56
|
+
:param sub_workflow: The WorkflowDefinition representing
|
|
57
|
+
the nested sub-workflow to execute.
|
|
58
|
+
:param input_mapping: An optional dictionary mapping
|
|
59
|
+
sub-workflow input names to parent WorkflowContext
|
|
60
|
+
reference strings. Defaults to an empty dictionary.
|
|
61
|
+
:param kwargs: Additional keyword arguments passed to the
|
|
62
|
+
WorkflowStep base class constructor (e.g. input_schema,
|
|
63
|
+
output_schema, tags, retry_policy).
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
def __init__(self, step_id: str, name: str,
|
|
67
|
+
sub_workflow: WorkflowDefinition,
|
|
68
|
+
input_mapping: Optional[dict] = None, **kwargs) -> None:
|
|
69
|
+
super().__init__(step_id=step_id, name=name, step_type="sub_workflow", **kwargs)
|
|
70
|
+
self._sub_workflow = sub_workflow
|
|
71
|
+
self._input_mapping = dict(input_mapping) if input_mapping is not None else {}
|
|
72
|
+
self._logger = logging.getLogger("ccai.agentkit.workflow.steps.subworkflow")
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def sub_workflow(self) -> WorkflowDefinition:
|
|
77
|
+
"""
|
|
78
|
+
The WorkflowDefinition representing the nested
|
|
79
|
+
sub-workflow to execute.
|
|
80
|
+
|
|
81
|
+
:returns: The sub-workflow WorkflowDefinition instance.
|
|
82
|
+
"""
|
|
83
|
+
return self._sub_workflow
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def input_mapping(self) -> dict:
|
|
87
|
+
"""
|
|
88
|
+
The dictionary mapping sub-workflow input names to parent
|
|
89
|
+
WorkflowContext reference strings.
|
|
90
|
+
|
|
91
|
+
Each key is a sub-workflow input parameter name. Each
|
|
92
|
+
value is a string of the form "inputs.<field_name>" or
|
|
93
|
+
"steps.<step_id>.<field_name>" that is resolved against
|
|
94
|
+
the parent WorkflowContext at execution time.
|
|
95
|
+
|
|
96
|
+
:returns: A dictionary of input mapping entries.
|
|
97
|
+
"""
|
|
98
|
+
return self._input_mapping
|
|
99
|
+
|
|
100
|
+
def _resolve_input_mapping(self, context: WorkflowContext) -> dict:
|
|
101
|
+
"""
|
|
102
|
+
Resolves the input mapping against the parent
|
|
103
|
+
WorkflowContext.
|
|
104
|
+
|
|
105
|
+
For each entry in the input_mapping dictionary, the
|
|
106
|
+
context reference string is parsed and the corresponding
|
|
107
|
+
value is retrieved from the parent WorkflowContext.
|
|
108
|
+
References of the form "inputs.<field_name>" are resolved
|
|
109
|
+
via context.get_input. References of the form
|
|
110
|
+
"steps.<step_id>.<field_name>" are resolved via
|
|
111
|
+
context.get_step_output.
|
|
112
|
+
|
|
113
|
+
:param context: The parent WorkflowContext to resolve
|
|
114
|
+
references against.
|
|
115
|
+
|
|
116
|
+
:returns: A dictionary of resolved sub-workflow input
|
|
117
|
+
values.
|
|
118
|
+
"""
|
|
119
|
+
resolved_inputs = {}
|
|
120
|
+
|
|
121
|
+
for param_name, reference in self._input_mapping.items():
|
|
122
|
+
parts = reference.split(".")
|
|
123
|
+
|
|
124
|
+
if len(parts) >= 2 and parts[0] == "inputs":
|
|
125
|
+
field_name = ".".join(parts[1:])
|
|
126
|
+
resolved_value = context.get_input(field_name)
|
|
127
|
+
resolved_inputs[param_name] = resolved_value
|
|
128
|
+
|
|
129
|
+
elif len(parts) >= 3 and parts[0] == "steps":
|
|
130
|
+
step_id = parts[1]
|
|
131
|
+
field_name = ".".join(parts[2:])
|
|
132
|
+
step_output = context.get_step_output(step_id)
|
|
133
|
+
|
|
134
|
+
if step_output is not None:
|
|
135
|
+
resolved_value = step_output.get(field_name, None)
|
|
136
|
+
resolved_inputs[param_name] = resolved_value
|
|
137
|
+
else:
|
|
138
|
+
resolved_inputs[param_name] = None
|
|
139
|
+
|
|
140
|
+
else:
|
|
141
|
+
resolved_inputs[param_name] = None
|
|
142
|
+
|
|
143
|
+
return resolved_inputs
|
|
144
|
+
|
|
145
|
+
def execute(self, context: WorkflowContext) -> StepResult:
|
|
146
|
+
"""
|
|
147
|
+
Executes this SubWorkflowStep using the provided parent
|
|
148
|
+
workflow context.
|
|
149
|
+
|
|
150
|
+
Resolves the input mapping against the parent
|
|
151
|
+
WorkflowContext, creates a new WorkflowExecutor for
|
|
152
|
+
independent sub-workflow execution, runs the sub-workflow
|
|
153
|
+
with the resolved inputs and the agent from the parent
|
|
154
|
+
context, and returns a StepResult with the execution
|
|
155
|
+
outcome.
|
|
156
|
+
|
|
157
|
+
If the sub-workflow completes successfully, the StepResult
|
|
158
|
+
contains COMPLETED status and the sub-workflow's
|
|
159
|
+
final_output. If the sub-workflow fails, the StepResult
|
|
160
|
+
contains FAILED status and the sub-workflow's error
|
|
161
|
+
information.
|
|
162
|
+
|
|
163
|
+
:param context: The parent WorkflowContext with inputs
|
|
164
|
+
and prior step outputs.
|
|
165
|
+
|
|
166
|
+
:returns: A StepResult with the execution outcome.
|
|
167
|
+
"""
|
|
168
|
+
start_time = time.monotonic()
|
|
169
|
+
|
|
170
|
+
# Resolve input mapping against the parent WorkflowContext
|
|
171
|
+
resolved_inputs = self._resolve_input_mapping(context)
|
|
172
|
+
|
|
173
|
+
# Retrieve the agent from the parent context
|
|
174
|
+
agent = context.agent
|
|
175
|
+
|
|
176
|
+
# Create a new WorkflowExecutor for independent sub-workflow
|
|
177
|
+
# execution. This supports independent checkpointing of the
|
|
178
|
+
# sub-workflow separately from the parent workflow.
|
|
179
|
+
sub_executor = WorkflowExecutor()
|
|
180
|
+
|
|
181
|
+
self._logger.info(
|
|
182
|
+
"SubWorkflowStep '%s' executing sub-workflow '%s'.",
|
|
183
|
+
self._name, self._sub_workflow.name
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
try:
|
|
187
|
+
workflow_result = sub_executor.execute(
|
|
188
|
+
workflow=self._sub_workflow,
|
|
189
|
+
inputs=resolved_inputs,
|
|
190
|
+
agent=agent
|
|
191
|
+
)
|
|
192
|
+
except Exception as exec_err:
|
|
193
|
+
duration = time.monotonic() - start_time
|
|
194
|
+
errmsg = "SubWorkflowStep '{}' failed during execution of sub-workflow '{}': {}".format(
|
|
195
|
+
self._name, self._sub_workflow.name, str(exec_err)
|
|
196
|
+
)
|
|
197
|
+
self._logger.warning(errmsg)
|
|
198
|
+
|
|
199
|
+
result = StepResult(
|
|
200
|
+
step_id=self._step_id,
|
|
201
|
+
step_name=self._name,
|
|
202
|
+
status=StepExecutionStatus.FAILED,
|
|
203
|
+
duration=duration,
|
|
204
|
+
error=errmsg
|
|
205
|
+
)
|
|
206
|
+
return result
|
|
207
|
+
|
|
208
|
+
duration = time.monotonic() - start_time
|
|
209
|
+
|
|
210
|
+
if workflow_result.status == WorkflowExecutionStatus.COMPLETED:
|
|
211
|
+
self._logger.info(
|
|
212
|
+
"SubWorkflowStep '%s' sub-workflow '%s' completed "
|
|
213
|
+
"successfully in %.3fs.",
|
|
214
|
+
self._name, self._sub_workflow.name, duration
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
result = StepResult(
|
|
218
|
+
step_id=self._step_id,
|
|
219
|
+
step_name=self._name,
|
|
220
|
+
status=StepExecutionStatus.COMPLETED,
|
|
221
|
+
output=workflow_result.final_output,
|
|
222
|
+
duration=duration
|
|
223
|
+
)
|
|
224
|
+
return result
|
|
225
|
+
else:
|
|
226
|
+
error_detail = workflow_result.error if workflow_result.error is not None else (
|
|
227
|
+
"Sub-workflow '{}' finished with status '{}'.".format(
|
|
228
|
+
self._sub_workflow.name, workflow_result.status.value
|
|
229
|
+
)
|
|
230
|
+
)
|
|
231
|
+
self._logger.warning(
|
|
232
|
+
"SubWorkflowStep '%s' sub-workflow '%s' failed: %s",
|
|
233
|
+
self._name, self._sub_workflow.name, error_detail
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
result = StepResult(
|
|
237
|
+
step_id=self._step_id,
|
|
238
|
+
step_name=self._name,
|
|
239
|
+
status=StepExecutionStatus.FAILED,
|
|
240
|
+
duration=duration,
|
|
241
|
+
error=error_detail
|
|
242
|
+
)
|
|
243
|
+
return result
|
|
244
|
+
|
|
245
|
+
def __repr__(self) -> str:
|
|
246
|
+
"""
|
|
247
|
+
Returns a detailed string representation including the
|
|
248
|
+
class name, step identifier, step name, and sub-workflow
|
|
249
|
+
name.
|
|
250
|
+
|
|
251
|
+
:returns: A repr string for debugging.
|
|
252
|
+
"""
|
|
253
|
+
repr_str = "{}(step_id={!r}, name={!r}, sub_workflow={!r})".format(
|
|
254
|
+
self.__class__.__name__, self._step_id, self._name,
|
|
255
|
+
self._sub_workflow.name
|
|
256
|
+
)
|
|
257
|
+
return repr_str
|
|
258
|
+
|
|
259
|
+
def __str__(self) -> str:
|
|
260
|
+
"""
|
|
261
|
+
Returns a human-readable summary including the step name
|
|
262
|
+
and sub-workflow name.
|
|
263
|
+
|
|
264
|
+
:returns: A human-readable string describing the
|
|
265
|
+
sub-workflow step.
|
|
266
|
+
"""
|
|
267
|
+
str_val = "SubWorkflowStep: {} [sub_workflow: {}]".format(
|
|
268
|
+
self._name, self._sub_workflow.name
|
|
269
|
+
)
|
|
270
|
+
return str_val
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the ToolStep class, a concrete WorkflowStep that executes
|
|
3
|
+
a named Tool from the Agent's ToolRegistry during workflow
|
|
4
|
+
execution.
|
|
5
|
+
|
|
6
|
+
ToolStep resolves an input mapping against the WorkflowContext,
|
|
7
|
+
looks up the tool by name in the Agent's ToolRegistry, executes
|
|
8
|
+
the tool through the Agent's execute_tool method, and returns a
|
|
9
|
+
StepResult containing the ToolResult output on success or a
|
|
10
|
+
FAILED status on failure.
|
|
11
|
+
|
|
12
|
+
.. module:: ccai.agentkit.steps.toolstep
|
|
13
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
14
|
+
:synopsis: Built-in workflow step for tool execution.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import logging
|
|
18
|
+
import time
|
|
19
|
+
|
|
20
|
+
from typing import Optional
|
|
21
|
+
|
|
22
|
+
from ccai.agentkit.stepexecutionstatus import StepExecutionStatus
|
|
23
|
+
from ccai.agentkit.stepresult import StepResult
|
|
24
|
+
from ccai.agentkit.workflowcontext import WorkflowContext
|
|
25
|
+
from ccai.agentkit.workflowstep import WorkflowStep
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ToolStep(WorkflowStep):
|
|
29
|
+
"""
|
|
30
|
+
Executes a named Tool from the Agent's ToolRegistry.
|
|
31
|
+
|
|
32
|
+
ToolStep is a concrete WorkflowStep subclass that integrates
|
|
33
|
+
with the Tool & Capability System. At execution time, it
|
|
34
|
+
resolves the input mapping against the WorkflowContext to
|
|
35
|
+
build the tool input dictionary, looks up the tool by name
|
|
36
|
+
in the Agent's ToolRegistry, executes the tool, and wraps
|
|
37
|
+
the result in a StepResult.
|
|
38
|
+
|
|
39
|
+
The input_mapping dictionary maps tool parameter names to
|
|
40
|
+
context references. Each value in the mapping is a string
|
|
41
|
+
of the form "inputs.<field_name>" or
|
|
42
|
+
"steps.<step_id>.<field_name>", which is resolved against
|
|
43
|
+
the WorkflowContext at execution time.
|
|
44
|
+
|
|
45
|
+
:param step_id: The unique identifier for this step within
|
|
46
|
+
the workflow. Must not be None or empty.
|
|
47
|
+
:param name: The human-readable name for this step. Must
|
|
48
|
+
not be None or empty.
|
|
49
|
+
:param tool_name: The name of the Tool to look up in the
|
|
50
|
+
Agent's ToolRegistry.
|
|
51
|
+
:param input_mapping: An optional dictionary mapping tool
|
|
52
|
+
parameter names to WorkflowContext reference strings.
|
|
53
|
+
Defaults to an empty dictionary.
|
|
54
|
+
:param kwargs: Additional keyword arguments passed to the
|
|
55
|
+
WorkflowStep base class constructor (e.g. input_schema,
|
|
56
|
+
output_schema, tags, retry_policy).
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
def __init__(self, step_id: str, name: str, tool_name: str,
|
|
60
|
+
input_mapping: Optional[dict] = None, **kwargs) -> None:
|
|
61
|
+
super().__init__(step_id=step_id, name=name, step_type="tool", **kwargs)
|
|
62
|
+
self._tool_name = tool_name
|
|
63
|
+
self._input_mapping = dict(input_mapping) if input_mapping is not None else {}
|
|
64
|
+
self._logger = logging.getLogger("ccai.agentkit.workflow.steps.tool")
|
|
65
|
+
return
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def tool_name(self) -> str:
|
|
69
|
+
"""
|
|
70
|
+
The name of the Tool to execute from the Agent's ToolRegistry.
|
|
71
|
+
|
|
72
|
+
:returns: The tool name string.
|
|
73
|
+
"""
|
|
74
|
+
return self._tool_name
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def input_mapping(self) -> dict:
|
|
78
|
+
"""
|
|
79
|
+
The dictionary mapping tool parameter names to WorkflowContext
|
|
80
|
+
reference strings.
|
|
81
|
+
|
|
82
|
+
Each key is a tool input parameter name. Each value is a
|
|
83
|
+
string of the form "inputs.<field_name>" or
|
|
84
|
+
"steps.<step_id>.<field_name>" that is resolved against
|
|
85
|
+
the WorkflowContext at execution time.
|
|
86
|
+
|
|
87
|
+
:returns: A dictionary of input mapping entries.
|
|
88
|
+
"""
|
|
89
|
+
return self._input_mapping
|
|
90
|
+
|
|
91
|
+
def _resolve_input_mapping(self, context: WorkflowContext) -> dict:
|
|
92
|
+
"""
|
|
93
|
+
Resolves the input mapping against the WorkflowContext.
|
|
94
|
+
|
|
95
|
+
For each entry in the input_mapping dictionary, the
|
|
96
|
+
context reference string is parsed and the corresponding
|
|
97
|
+
value is retrieved from the WorkflowContext. References
|
|
98
|
+
of the form "inputs.<field_name>" are resolved via
|
|
99
|
+
context.get_input. References of the form
|
|
100
|
+
"steps.<step_id>.<field_name>" are resolved via
|
|
101
|
+
context.get_step_output.
|
|
102
|
+
|
|
103
|
+
:param context: The WorkflowContext to resolve references
|
|
104
|
+
against.
|
|
105
|
+
|
|
106
|
+
:returns: A dictionary of resolved tool input values.
|
|
107
|
+
"""
|
|
108
|
+
resolved_inputs = {}
|
|
109
|
+
|
|
110
|
+
for param_name, reference in self._input_mapping.items():
|
|
111
|
+
parts = reference.split(".")
|
|
112
|
+
|
|
113
|
+
if len(parts) >= 2 and parts[0] == "inputs":
|
|
114
|
+
field_name = ".".join(parts[1:])
|
|
115
|
+
resolved_value = context.get_input(field_name)
|
|
116
|
+
resolved_inputs[param_name] = resolved_value
|
|
117
|
+
|
|
118
|
+
elif len(parts) >= 3 and parts[0] == "steps":
|
|
119
|
+
step_id = parts[1]
|
|
120
|
+
field_name = ".".join(parts[2:])
|
|
121
|
+
step_output = context.get_step_output(step_id)
|
|
122
|
+
|
|
123
|
+
if step_output is not None:
|
|
124
|
+
resolved_value = step_output.get(field_name, None)
|
|
125
|
+
resolved_inputs[param_name] = resolved_value
|
|
126
|
+
else:
|
|
127
|
+
resolved_inputs[param_name] = None
|
|
128
|
+
|
|
129
|
+
else:
|
|
130
|
+
resolved_inputs[param_name] = None
|
|
131
|
+
|
|
132
|
+
return resolved_inputs
|
|
133
|
+
|
|
134
|
+
def execute(self, context: WorkflowContext) -> StepResult:
|
|
135
|
+
"""
|
|
136
|
+
Executes this ToolStep using the provided workflow context.
|
|
137
|
+
|
|
138
|
+
Resolves the input mapping against the WorkflowContext,
|
|
139
|
+
retrieves the Agent from the context, looks up the tool
|
|
140
|
+
by name in the Agent's ToolRegistry, executes the tool
|
|
141
|
+
through the Agent's execute_tool method, and returns a
|
|
142
|
+
StepResult with the execution outcome.
|
|
143
|
+
|
|
144
|
+
If the Agent is None or the tool is not found in the
|
|
145
|
+
Agent's ToolRegistry, a StepResult with FAILED status
|
|
146
|
+
is returned. If the tool execution raises an exception,
|
|
147
|
+
a StepResult with FAILED status and the error message
|
|
148
|
+
is returned.
|
|
149
|
+
|
|
150
|
+
:param context: The WorkflowContext with inputs and prior
|
|
151
|
+
step outputs.
|
|
152
|
+
|
|
153
|
+
:returns: A StepResult with the execution outcome.
|
|
154
|
+
"""
|
|
155
|
+
start_time = time.monotonic()
|
|
156
|
+
|
|
157
|
+
agent = context.agent
|
|
158
|
+
|
|
159
|
+
if agent is None:
|
|
160
|
+
duration = time.monotonic() - start_time
|
|
161
|
+
errmsg = "ToolStep '{}' failed: no agent available in the workflow context.".format(
|
|
162
|
+
self._name
|
|
163
|
+
)
|
|
164
|
+
self._logger.warning(errmsg)
|
|
165
|
+
|
|
166
|
+
result = StepResult(
|
|
167
|
+
step_id=self._step_id,
|
|
168
|
+
step_name=self._name,
|
|
169
|
+
status=StepExecutionStatus.FAILED,
|
|
170
|
+
duration=duration,
|
|
171
|
+
error=errmsg
|
|
172
|
+
)
|
|
173
|
+
return result
|
|
174
|
+
|
|
175
|
+
# Check that the tool exists in the Agent's ToolRegistry
|
|
176
|
+
tool_registry = agent.tools
|
|
177
|
+
tool = tool_registry.lookup(self._tool_name)
|
|
178
|
+
|
|
179
|
+
if tool is None:
|
|
180
|
+
duration = time.monotonic() - start_time
|
|
181
|
+
errmsg = "ToolStep '{}' failed: tool '{}' not found in the agent's ToolRegistry.".format(
|
|
182
|
+
self._name, self._tool_name
|
|
183
|
+
)
|
|
184
|
+
self._logger.warning(errmsg)
|
|
185
|
+
|
|
186
|
+
result = StepResult(
|
|
187
|
+
step_id=self._step_id,
|
|
188
|
+
step_name=self._name,
|
|
189
|
+
status=StepExecutionStatus.FAILED,
|
|
190
|
+
duration=duration,
|
|
191
|
+
error=errmsg
|
|
192
|
+
)
|
|
193
|
+
return result
|
|
194
|
+
|
|
195
|
+
# Resolve input mapping against the WorkflowContext
|
|
196
|
+
resolved_inputs = self._resolve_input_mapping(context)
|
|
197
|
+
|
|
198
|
+
# Execute the tool through the Agent
|
|
199
|
+
try:
|
|
200
|
+
tool_result = agent.execute_tool(self._tool_name, resolved_inputs)
|
|
201
|
+
duration = time.monotonic() - start_time
|
|
202
|
+
|
|
203
|
+
if tool_result.is_success:
|
|
204
|
+
output = tool_result.output if tool_result.output is not None else {}
|
|
205
|
+
|
|
206
|
+
result = StepResult(
|
|
207
|
+
step_id=self._step_id,
|
|
208
|
+
step_name=self._name,
|
|
209
|
+
status=StepExecutionStatus.COMPLETED,
|
|
210
|
+
output=output,
|
|
211
|
+
duration=duration
|
|
212
|
+
)
|
|
213
|
+
return result
|
|
214
|
+
else:
|
|
215
|
+
error_detail = str(tool_result.error) if tool_result.error is not None else "Tool execution failed."
|
|
216
|
+
|
|
217
|
+
result = StepResult(
|
|
218
|
+
step_id=self._step_id,
|
|
219
|
+
step_name=self._name,
|
|
220
|
+
status=StepExecutionStatus.FAILED,
|
|
221
|
+
duration=duration,
|
|
222
|
+
error=error_detail
|
|
223
|
+
)
|
|
224
|
+
return result
|
|
225
|
+
|
|
226
|
+
except Exception as exec_err:
|
|
227
|
+
duration = time.monotonic() - start_time
|
|
228
|
+
errmsg = "ToolStep '{}' failed during execution of tool '{}': {}".format(
|
|
229
|
+
self._name, self._tool_name, str(exec_err)
|
|
230
|
+
)
|
|
231
|
+
self._logger.warning(errmsg)
|
|
232
|
+
|
|
233
|
+
result = StepResult(
|
|
234
|
+
step_id=self._step_id,
|
|
235
|
+
step_name=self._name,
|
|
236
|
+
status=StepExecutionStatus.FAILED,
|
|
237
|
+
duration=duration,
|
|
238
|
+
error=errmsg
|
|
239
|
+
)
|
|
240
|
+
return result
|
|
241
|
+
|
|
242
|
+
def __repr__(self) -> str:
|
|
243
|
+
"""
|
|
244
|
+
Returns a detailed string representation including the
|
|
245
|
+
class name, step identifier, step name, and tool name.
|
|
246
|
+
|
|
247
|
+
:returns: A repr string for debugging.
|
|
248
|
+
"""
|
|
249
|
+
repr_str = "{}(step_id={!r}, name={!r}, tool_name={!r})".format(
|
|
250
|
+
self.__class__.__name__, self._step_id, self._name,
|
|
251
|
+
self._tool_name
|
|
252
|
+
)
|
|
253
|
+
return repr_str
|
|
254
|
+
|
|
255
|
+
def __str__(self) -> str:
|
|
256
|
+
"""
|
|
257
|
+
Returns a human-readable summary including the step name
|
|
258
|
+
and tool name.
|
|
259
|
+
|
|
260
|
+
:returns: A human-readable string describing the tool step.
|
|
261
|
+
"""
|
|
262
|
+
str_val = "ToolStep: {} [tool: {}]".format(
|
|
263
|
+
self._name, self._tool_name
|
|
264
|
+
)
|
|
265
|
+
return str_val
|