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,152 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the AgentIdentity class representing the identity and metadata
|
|
3
|
+
of an agent in the codecraftai-agent-kit framework.
|
|
4
|
+
|
|
5
|
+
AgentIdentity contains a unique identifier, human-readable name,
|
|
6
|
+
version string, and descriptive tags. It supports serialization
|
|
7
|
+
to and from JSON and dictionary representations.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import uuid
|
|
12
|
+
|
|
13
|
+
from typing import Dict
|
|
14
|
+
from typing import Optional
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class AgentIdentity:
|
|
18
|
+
"""
|
|
19
|
+
Identity and metadata for an agent.
|
|
20
|
+
|
|
21
|
+
Contains a unique identifier, human-readable name, version string,
|
|
22
|
+
and a dictionary of descriptive tags. Supports serialization to
|
|
23
|
+
dictionary and JSON formats, and deserialization from JSON.
|
|
24
|
+
|
|
25
|
+
:param name: The human-readable name of the agent.
|
|
26
|
+
:param version: The version string of the agent.
|
|
27
|
+
:param tags: Optional dictionary of descriptive key-value tags.
|
|
28
|
+
:param identifier: Optional unique identifier. Auto-generates a UUID if not provided.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, name: str, version: str, tags: Optional[Dict[str, str]] = None,
|
|
32
|
+
identifier: Optional[str] = None) -> None:
|
|
33
|
+
|
|
34
|
+
if identifier is not None:
|
|
35
|
+
self._identifier = identifier
|
|
36
|
+
else:
|
|
37
|
+
self._identifier = str(uuid.uuid4())
|
|
38
|
+
|
|
39
|
+
self._name = name
|
|
40
|
+
self._version = version
|
|
41
|
+
|
|
42
|
+
if tags is not None:
|
|
43
|
+
self._tags = dict(tags)
|
|
44
|
+
else:
|
|
45
|
+
self._tags = {}
|
|
46
|
+
|
|
47
|
+
return
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def identifier(self) -> str:
|
|
51
|
+
"""
|
|
52
|
+
The unique identifier of the agent.
|
|
53
|
+
|
|
54
|
+
:returns: The identifier string.
|
|
55
|
+
"""
|
|
56
|
+
return self._identifier
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def name(self) -> str:
|
|
60
|
+
"""
|
|
61
|
+
The human-readable name of the agent.
|
|
62
|
+
|
|
63
|
+
:returns: The name string.
|
|
64
|
+
"""
|
|
65
|
+
return self._name
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def version(self) -> str:
|
|
69
|
+
"""
|
|
70
|
+
The version string of the agent.
|
|
71
|
+
|
|
72
|
+
:returns: The version string.
|
|
73
|
+
"""
|
|
74
|
+
return self._version
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def tags(self) -> Dict[str, str]:
|
|
78
|
+
"""
|
|
79
|
+
A defensive copy of the descriptive tags dictionary.
|
|
80
|
+
|
|
81
|
+
:returns: A dictionary copy of the tags.
|
|
82
|
+
"""
|
|
83
|
+
return dict(self._tags)
|
|
84
|
+
|
|
85
|
+
def as_dict(self) -> dict:
|
|
86
|
+
"""
|
|
87
|
+
Returns a dictionary representation of the identity fields.
|
|
88
|
+
|
|
89
|
+
:returns: A dictionary with identifier, name, version, and tags.
|
|
90
|
+
"""
|
|
91
|
+
result = {
|
|
92
|
+
"identifier": self._identifier,
|
|
93
|
+
"name": self._name,
|
|
94
|
+
"version": self._version,
|
|
95
|
+
"tags": dict(self._tags)
|
|
96
|
+
}
|
|
97
|
+
return result
|
|
98
|
+
|
|
99
|
+
def to_json(self) -> str:
|
|
100
|
+
"""
|
|
101
|
+
Returns a JSON string representation of the identity fields.
|
|
102
|
+
|
|
103
|
+
:returns: A JSON string containing identifier, name, version, and tags.
|
|
104
|
+
"""
|
|
105
|
+
data = self.as_dict()
|
|
106
|
+
json_str = json.dumps(data)
|
|
107
|
+
return json_str
|
|
108
|
+
|
|
109
|
+
@classmethod
|
|
110
|
+
def from_json(cls, json_str: str) -> "AgentIdentity":
|
|
111
|
+
"""
|
|
112
|
+
Deserializes a JSON string into an AgentIdentity instance.
|
|
113
|
+
|
|
114
|
+
:param json_str: A JSON string containing identity fields.
|
|
115
|
+
|
|
116
|
+
:returns: An AgentIdentity instance reconstructed from the JSON data.
|
|
117
|
+
"""
|
|
118
|
+
data = json.loads(json_str)
|
|
119
|
+
|
|
120
|
+
identifier = data["identifier"]
|
|
121
|
+
name = data["name"]
|
|
122
|
+
version = data["version"]
|
|
123
|
+
tags = data.get("tags", {})
|
|
124
|
+
|
|
125
|
+
identity = cls(
|
|
126
|
+
name=name,
|
|
127
|
+
version=version,
|
|
128
|
+
tags=tags,
|
|
129
|
+
identifier=identifier
|
|
130
|
+
)
|
|
131
|
+
return identity
|
|
132
|
+
|
|
133
|
+
def __repr__(self) -> str:
|
|
134
|
+
"""
|
|
135
|
+
Returns a detailed string representation including class name,
|
|
136
|
+
identifier, name, and version.
|
|
137
|
+
|
|
138
|
+
:returns: A repr string for debugging.
|
|
139
|
+
"""
|
|
140
|
+
repr_str = "AgentIdentity(identifier={!r}, name={!r}, version={!r})".format(
|
|
141
|
+
self._identifier, self._name, self._version
|
|
142
|
+
)
|
|
143
|
+
return repr_str
|
|
144
|
+
|
|
145
|
+
def __str__(self) -> str:
|
|
146
|
+
"""
|
|
147
|
+
Returns a human-readable summary including the name and version.
|
|
148
|
+
|
|
149
|
+
:returns: A human-readable string.
|
|
150
|
+
"""
|
|
151
|
+
str_val = "AgentIdentity: {} v{}".format(self._name, self._version)
|
|
152
|
+
return str_val
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the AgentRegistry singleton class that tracks all agent instances
|
|
3
|
+
within a runtime process for the codecraftai-agent-kit framework.
|
|
4
|
+
|
|
5
|
+
The registry provides registration, deregistration, and lookup operations
|
|
6
|
+
for agents by identifier, name, and tags. All operations are thread-safe.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import threading
|
|
11
|
+
|
|
12
|
+
from typing import Dict
|
|
13
|
+
from typing import List
|
|
14
|
+
from typing import Optional
|
|
15
|
+
|
|
16
|
+
from ccai.agentkit.errors import SemanticError
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class AgentRegistry:
|
|
20
|
+
"""
|
|
21
|
+
Singleton registry that tracks all agent instances within a runtime process.
|
|
22
|
+
|
|
23
|
+
Provides registration, deregistration, and lookup operations for agents
|
|
24
|
+
by unique identifier, name, and tag criteria. All operations are
|
|
25
|
+
protected by a threading lock for thread-safe access.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
_instance: Optional["AgentRegistry"] = None
|
|
29
|
+
_instance_lock: threading.Lock = threading.Lock()
|
|
30
|
+
|
|
31
|
+
@classmethod
|
|
32
|
+
def instance(cls) -> "AgentRegistry":
|
|
33
|
+
"""
|
|
34
|
+
Returns the singleton AgentRegistry instance, creating it if necessary.
|
|
35
|
+
|
|
36
|
+
:returns: The singleton AgentRegistry instance.
|
|
37
|
+
"""
|
|
38
|
+
with cls._instance_lock:
|
|
39
|
+
if cls._instance is None:
|
|
40
|
+
cls._instance = cls()
|
|
41
|
+
return cls._instance
|
|
42
|
+
|
|
43
|
+
def __init__(self) -> None:
|
|
44
|
+
self._agents: Dict[str, object] = {}
|
|
45
|
+
self._lock: threading.Lock = threading.Lock()
|
|
46
|
+
return
|
|
47
|
+
|
|
48
|
+
def register(self, agent: object) -> None:
|
|
49
|
+
"""
|
|
50
|
+
Registers an agent in the registry using its unique identifier.
|
|
51
|
+
|
|
52
|
+
:param agent: The agent instance to register. Must have an identity
|
|
53
|
+
property with an identifier attribute.
|
|
54
|
+
|
|
55
|
+
:raises: SemanticError if an agent with the same identifier is already registered.
|
|
56
|
+
"""
|
|
57
|
+
with self._lock:
|
|
58
|
+
identifier = agent.identity.identifier
|
|
59
|
+
is_duplicate = identifier in self._agents
|
|
60
|
+
|
|
61
|
+
if is_duplicate is True:
|
|
62
|
+
err_msg_lines = [
|
|
63
|
+
"Duplicate agent registration attempted."
|
|
64
|
+
]
|
|
65
|
+
err_msg_lines.append(" Identifier: {}".format(identifier))
|
|
66
|
+
err_msg_lines.append(" An agent with this identifier is already registered.")
|
|
67
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
68
|
+
raise SemanticError(err_msg)
|
|
69
|
+
|
|
70
|
+
self._agents[identifier] = agent
|
|
71
|
+
|
|
72
|
+
return
|
|
73
|
+
|
|
74
|
+
def deregister(self, identifier: str) -> None:
|
|
75
|
+
"""
|
|
76
|
+
Removes an agent from the registry by its unique identifier.
|
|
77
|
+
|
|
78
|
+
If the identifier is not found, the operation is a no-op.
|
|
79
|
+
|
|
80
|
+
:param identifier: The unique identifier of the agent to remove.
|
|
81
|
+
"""
|
|
82
|
+
with self._lock:
|
|
83
|
+
is_present = identifier in self._agents
|
|
84
|
+
|
|
85
|
+
if is_present is True:
|
|
86
|
+
del self._agents[identifier]
|
|
87
|
+
|
|
88
|
+
return
|
|
89
|
+
|
|
90
|
+
def lookup_by_id(self, identifier: str) -> Optional[object]:
|
|
91
|
+
"""
|
|
92
|
+
Looks up a registered agent by its unique identifier.
|
|
93
|
+
|
|
94
|
+
:param identifier: The unique identifier to search for.
|
|
95
|
+
|
|
96
|
+
:returns: The agent instance if found, or None if not found.
|
|
97
|
+
"""
|
|
98
|
+
with self._lock:
|
|
99
|
+
agent = self._agents.get(identifier, None)
|
|
100
|
+
|
|
101
|
+
return agent
|
|
102
|
+
|
|
103
|
+
def lookup_by_name(self, name: str) -> List[object]:
|
|
104
|
+
"""
|
|
105
|
+
Looks up registered agents by name.
|
|
106
|
+
|
|
107
|
+
:param name: The agent name to search for.
|
|
108
|
+
|
|
109
|
+
:returns: A list of agent instances whose identity name matches.
|
|
110
|
+
"""
|
|
111
|
+
with self._lock:
|
|
112
|
+
matching_agents = []
|
|
113
|
+
|
|
114
|
+
for agent in self._agents.values():
|
|
115
|
+
agent_name = agent.identity.name
|
|
116
|
+
is_match = agent_name == name
|
|
117
|
+
|
|
118
|
+
if is_match is True:
|
|
119
|
+
matching_agents.append(agent)
|
|
120
|
+
|
|
121
|
+
return matching_agents
|
|
122
|
+
|
|
123
|
+
def lookup_by_tags(self, **tags: str) -> List[object]:
|
|
124
|
+
"""
|
|
125
|
+
Looks up registered agents by tag key-value pairs.
|
|
126
|
+
|
|
127
|
+
Returns agents that match ALL provided tag criteria.
|
|
128
|
+
|
|
129
|
+
:param tags: Keyword arguments representing tag key-value pairs to match.
|
|
130
|
+
|
|
131
|
+
:returns: A list of agent instances matching all tag criteria.
|
|
132
|
+
"""
|
|
133
|
+
with self._lock:
|
|
134
|
+
matching_agents = []
|
|
135
|
+
|
|
136
|
+
for agent in self._agents.values():
|
|
137
|
+
agent_tags = agent.identity.tags
|
|
138
|
+
all_match = True
|
|
139
|
+
|
|
140
|
+
for tag_key, tag_value in tags.items():
|
|
141
|
+
has_key = tag_key in agent_tags
|
|
142
|
+
|
|
143
|
+
if has_key is not True:
|
|
144
|
+
all_match = False
|
|
145
|
+
break
|
|
146
|
+
|
|
147
|
+
agent_tag_value = agent_tags[tag_key]
|
|
148
|
+
values_match = agent_tag_value == tag_value
|
|
149
|
+
|
|
150
|
+
if values_match is not True:
|
|
151
|
+
all_match = False
|
|
152
|
+
break
|
|
153
|
+
|
|
154
|
+
if all_match is True:
|
|
155
|
+
matching_agents.append(agent)
|
|
156
|
+
|
|
157
|
+
return matching_agents
|
|
158
|
+
|
|
159
|
+
def list_all(self) -> List[object]:
|
|
160
|
+
"""
|
|
161
|
+
Returns a list of all registered agent instances.
|
|
162
|
+
|
|
163
|
+
:returns: A list of all registered agents.
|
|
164
|
+
"""
|
|
165
|
+
with self._lock:
|
|
166
|
+
all_agents = list(self._agents.values())
|
|
167
|
+
|
|
168
|
+
return all_agents
|
|
169
|
+
|
|
170
|
+
@property
|
|
171
|
+
def count(self) -> int:
|
|
172
|
+
"""
|
|
173
|
+
The number of currently registered agents.
|
|
174
|
+
|
|
175
|
+
:returns: The count of registered agents.
|
|
176
|
+
"""
|
|
177
|
+
with self._lock:
|
|
178
|
+
agent_count = len(self._agents)
|
|
179
|
+
|
|
180
|
+
return agent_count
|
|
181
|
+
|
|
182
|
+
def _reset(self) -> None:
|
|
183
|
+
"""
|
|
184
|
+
Resets the registry by clearing all registered agents.
|
|
185
|
+
|
|
186
|
+
This method is intended for testing purposes only.
|
|
187
|
+
"""
|
|
188
|
+
with self._lock:
|
|
189
|
+
self._agents.clear()
|
|
190
|
+
|
|
191
|
+
return
|
|
192
|
+
|
|
193
|
+
@classmethod
|
|
194
|
+
def _reset_singleton(cls) -> None:
|
|
195
|
+
"""
|
|
196
|
+
Resets the singleton instance to None.
|
|
197
|
+
|
|
198
|
+
This method is intended for testing purposes only.
|
|
199
|
+
"""
|
|
200
|
+
with cls._instance_lock:
|
|
201
|
+
cls._instance = None
|
|
202
|
+
|
|
203
|
+
return
|
|
204
|
+
|
|
205
|
+
def __repr__(self) -> str:
|
|
206
|
+
"""
|
|
207
|
+
Returns a detailed string representation including class name and count.
|
|
208
|
+
|
|
209
|
+
:returns: A repr string for debugging.
|
|
210
|
+
"""
|
|
211
|
+
repr_str = "AgentRegistry(count={})".format(self.count)
|
|
212
|
+
return repr_str
|
|
213
|
+
|
|
214
|
+
def __str__(self) -> str:
|
|
215
|
+
"""
|
|
216
|
+
Returns a human-readable summary of the registry.
|
|
217
|
+
|
|
218
|
+
:returns: A human-readable string.
|
|
219
|
+
"""
|
|
220
|
+
str_val = "AgentRegistry: {} agents registered".format(self.count)
|
|
221
|
+
return str_val
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the AgentStateMachine class that governs valid lifecycle
|
|
3
|
+
state transitions for agents in the codecraftai-agent-kit framework.
|
|
4
|
+
|
|
5
|
+
The state machine enforces a well-defined transition table, records
|
|
6
|
+
transition history, and provides thread-safe state management.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import threading
|
|
11
|
+
|
|
12
|
+
from datetime import datetime
|
|
13
|
+
from datetime import timezone
|
|
14
|
+
from typing import Dict
|
|
15
|
+
from typing import List
|
|
16
|
+
|
|
17
|
+
from ccai.agentkit.errors import SemanticError
|
|
18
|
+
from ccai.agentkit.lifecyclestate import LifecycleState
|
|
19
|
+
from ccai.agentkit.transitionrecord import TransitionRecord
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class AgentStateMachine:
|
|
23
|
+
"""
|
|
24
|
+
State machine that enforces valid lifecycle transitions for an agent.
|
|
25
|
+
|
|
26
|
+
Manages the current lifecycle state, validates transitions against
|
|
27
|
+
a predefined transition table, and records a history of all
|
|
28
|
+
transitions with timestamps.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
VALID_TRANSITIONS: Dict[LifecycleState, List[LifecycleState]] = {
|
|
32
|
+
LifecycleState.CREATED: [LifecycleState.INITIALIZED],
|
|
33
|
+
LifecycleState.INITIALIZED: [LifecycleState.RUNNING, LifecycleState.STOPPED],
|
|
34
|
+
LifecycleState.RUNNING: [LifecycleState.PAUSED, LifecycleState.STOPPED, LifecycleState.ERROR],
|
|
35
|
+
LifecycleState.PAUSED: [LifecycleState.RUNNING, LifecycleState.STOPPED],
|
|
36
|
+
LifecycleState.ERROR: [LifecycleState.STOPPED],
|
|
37
|
+
LifecycleState.STOPPED: [LifecycleState.INITIALIZED],
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
def __init__(self) -> None:
|
|
41
|
+
self._current_state: LifecycleState = LifecycleState.CREATED
|
|
42
|
+
self._history: List[TransitionRecord] = []
|
|
43
|
+
self._lock: threading.Lock = threading.Lock()
|
|
44
|
+
return
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def current_state(self) -> LifecycleState:
|
|
48
|
+
"""
|
|
49
|
+
The current lifecycle state of the state machine.
|
|
50
|
+
|
|
51
|
+
:returns: The current LifecycleState.
|
|
52
|
+
"""
|
|
53
|
+
return self._current_state
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def transition_history(self) -> List[TransitionRecord]:
|
|
57
|
+
"""
|
|
58
|
+
The history of all state transitions as a list of TransitionRecord instances.
|
|
59
|
+
|
|
60
|
+
:returns: A list of TransitionRecord instances in chronological order.
|
|
61
|
+
"""
|
|
62
|
+
return list(self._history)
|
|
63
|
+
|
|
64
|
+
def get_valid_transitions(self) -> List[LifecycleState]:
|
|
65
|
+
"""
|
|
66
|
+
Returns the list of valid target states from the current state.
|
|
67
|
+
|
|
68
|
+
:returns: A list of LifecycleState values that are valid targets.
|
|
69
|
+
"""
|
|
70
|
+
valid_targets = list(self.VALID_TRANSITIONS[self._current_state])
|
|
71
|
+
return valid_targets
|
|
72
|
+
|
|
73
|
+
def transition_to(self, target_state: LifecycleState, metadata: dict = None) -> TransitionRecord:
|
|
74
|
+
"""
|
|
75
|
+
Attempts to transition the state machine to the specified target state.
|
|
76
|
+
|
|
77
|
+
Validates the transition against the transition table, records the
|
|
78
|
+
transition in history, and returns the transition record.
|
|
79
|
+
|
|
80
|
+
:param target_state: The desired target LifecycleState.
|
|
81
|
+
:param metadata: Optional dictionary of metadata for the transition.
|
|
82
|
+
|
|
83
|
+
:returns: A TransitionRecord capturing the transition details.
|
|
84
|
+
:raises: SemanticError
|
|
85
|
+
"""
|
|
86
|
+
with self._lock:
|
|
87
|
+
valid_targets = self.VALID_TRANSITIONS[self._current_state]
|
|
88
|
+
is_valid = target_state in valid_targets
|
|
89
|
+
|
|
90
|
+
if is_valid is not True:
|
|
91
|
+
valid_names = [state.value for state in valid_targets]
|
|
92
|
+
err_msg_lines = [
|
|
93
|
+
"Invalid state transition requested."
|
|
94
|
+
]
|
|
95
|
+
err_msg_lines.append(" Current state: {}".format(self._current_state.value))
|
|
96
|
+
err_msg_lines.append(" Target state: {}".format(target_state.value))
|
|
97
|
+
err_msg_lines.append(" Valid transitions from {}: {}".format(
|
|
98
|
+
self._current_state.value, valid_names
|
|
99
|
+
))
|
|
100
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
101
|
+
raise SemanticError(err_msg)
|
|
102
|
+
|
|
103
|
+
previous_state = self._current_state
|
|
104
|
+
now = datetime.now(tz=timezone.utc)
|
|
105
|
+
|
|
106
|
+
self._current_state = target_state
|
|
107
|
+
|
|
108
|
+
record = TransitionRecord(
|
|
109
|
+
previous_state=previous_state,
|
|
110
|
+
new_state=target_state,
|
|
111
|
+
timestamp=now,
|
|
112
|
+
metadata=metadata
|
|
113
|
+
)
|
|
114
|
+
self._history.append(record)
|
|
115
|
+
|
|
116
|
+
return record
|
|
117
|
+
|
|
118
|
+
def __repr__(self) -> str:
|
|
119
|
+
"""
|
|
120
|
+
Returns a detailed string representation including class name and current state.
|
|
121
|
+
|
|
122
|
+
:returns: A repr string for debugging.
|
|
123
|
+
"""
|
|
124
|
+
repr_str = "AgentStateMachine(current_state={!r})".format(self._current_state)
|
|
125
|
+
return repr_str
|
|
126
|
+
|
|
127
|
+
def __str__(self) -> str:
|
|
128
|
+
"""
|
|
129
|
+
Returns a human-readable string of the current state.
|
|
130
|
+
|
|
131
|
+
:returns: A string describing the current state.
|
|
132
|
+
"""
|
|
133
|
+
str_val = "AgentStateMachine: {}".format(self._current_state.value)
|
|
134
|
+
return str_val
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the AlternateProviderStrategy for routing operations to an
|
|
3
|
+
alternative provider when the primary dependency is unavailable.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Callable
|
|
7
|
+
from typing import Optional
|
|
8
|
+
|
|
9
|
+
from ccai.agentkit.degradationstrategy import DegradationStrategy
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class AlternateProviderStrategy(DegradationStrategy):
|
|
13
|
+
"""
|
|
14
|
+
Routes the operation to an alternative provider or backend when
|
|
15
|
+
the primary dependency is unavailable.
|
|
16
|
+
|
|
17
|
+
:param alternate_callable: The callable to invoke as the alternate provider.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self, alternate_callable: Callable) -> None:
|
|
21
|
+
super().__init__("alternate_provider")
|
|
22
|
+
self._alternate_callable = alternate_callable
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
def degrade(self, dependency_name: str, exception: Exception,
|
|
26
|
+
context: Optional[dict] = None):
|
|
27
|
+
"""
|
|
28
|
+
Invokes the alternate callable.
|
|
29
|
+
|
|
30
|
+
:param dependency_name: Name of the unavailable dependency.
|
|
31
|
+
:param exception: The exception that triggered degradation.
|
|
32
|
+
:param context: Optional context dictionary.
|
|
33
|
+
|
|
34
|
+
:returns: The result from the alternate callable.
|
|
35
|
+
"""
|
|
36
|
+
return self._alternate_callable()
|