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,222 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the StructuredLogEntry class representing an immutable
|
|
3
|
+
structured log record containing a timestamp, log level, message,
|
|
4
|
+
logger name, correlation identifier, agent identifier, and a
|
|
5
|
+
metadata dictionary of additional key-value fields.
|
|
6
|
+
|
|
7
|
+
StructuredLogEntry objects are created by the StructuredLogger
|
|
8
|
+
and consumed by LogFormatter and LogExporter implementations.
|
|
9
|
+
All fields are exposed as read-only properties to preserve
|
|
10
|
+
immutability after construction.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import json
|
|
14
|
+
|
|
15
|
+
from datetime import datetime
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class StructuredLogEntry:
|
|
19
|
+
"""
|
|
20
|
+
An immutable object representing a single structured log record.
|
|
21
|
+
|
|
22
|
+
Contains a timestamp, log level, message, logger name, correlation
|
|
23
|
+
identifier, agent identifier, and a metadata dictionary. All fields
|
|
24
|
+
are read-only after construction.
|
|
25
|
+
|
|
26
|
+
:param timestamp: The timestamp when the log entry was created.
|
|
27
|
+
:param level: The log level string (e.g. "debug", "info", "warning",
|
|
28
|
+
"error", "critical").
|
|
29
|
+
:param message: The log message string.
|
|
30
|
+
:param logger_name: The name of the logger that produced this entry.
|
|
31
|
+
:param correlation_id: The correlation identifier string linking
|
|
32
|
+
this entry to a logical operation.
|
|
33
|
+
:param agent_id: The agent identifier string for the agent that
|
|
34
|
+
produced this entry.
|
|
35
|
+
:param metadata: Optional dictionary of additional key-value fields.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(self, timestamp: datetime, level: str, message: str,
|
|
39
|
+
logger_name: str, correlation_id: str, agent_id: str,
|
|
40
|
+
metadata: dict = None) -> None:
|
|
41
|
+
|
|
42
|
+
self._timestamp = timestamp
|
|
43
|
+
self._level = level
|
|
44
|
+
self._message = message
|
|
45
|
+
self._logger_name = logger_name
|
|
46
|
+
self._correlation_id = correlation_id
|
|
47
|
+
self._agent_id = agent_id
|
|
48
|
+
|
|
49
|
+
if metadata is not None:
|
|
50
|
+
self._metadata = dict(metadata)
|
|
51
|
+
else:
|
|
52
|
+
self._metadata = {}
|
|
53
|
+
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def timestamp(self) -> datetime:
|
|
58
|
+
"""
|
|
59
|
+
The timestamp when the log entry was created.
|
|
60
|
+
|
|
61
|
+
:returns: The datetime timestamp.
|
|
62
|
+
"""
|
|
63
|
+
return self._timestamp
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def level(self) -> str:
|
|
67
|
+
"""
|
|
68
|
+
The log level string for this entry.
|
|
69
|
+
|
|
70
|
+
:returns: The log level string.
|
|
71
|
+
"""
|
|
72
|
+
return self._level
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def message(self) -> str:
|
|
76
|
+
"""
|
|
77
|
+
The log message string.
|
|
78
|
+
|
|
79
|
+
:returns: The message string.
|
|
80
|
+
"""
|
|
81
|
+
return self._message
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def logger_name(self) -> str:
|
|
85
|
+
"""
|
|
86
|
+
The name of the logger that produced this entry.
|
|
87
|
+
|
|
88
|
+
:returns: The logger name string.
|
|
89
|
+
"""
|
|
90
|
+
return self._logger_name
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def correlation_id(self) -> str:
|
|
94
|
+
"""
|
|
95
|
+
The correlation identifier string linking this entry to a
|
|
96
|
+
logical operation.
|
|
97
|
+
|
|
98
|
+
:returns: The correlation identifier string.
|
|
99
|
+
"""
|
|
100
|
+
return self._correlation_id
|
|
101
|
+
|
|
102
|
+
@property
|
|
103
|
+
def agent_id(self) -> str:
|
|
104
|
+
"""
|
|
105
|
+
The agent identifier string for the agent that produced
|
|
106
|
+
this entry.
|
|
107
|
+
|
|
108
|
+
:returns: The agent identifier string.
|
|
109
|
+
"""
|
|
110
|
+
return self._agent_id
|
|
111
|
+
|
|
112
|
+
@property
|
|
113
|
+
def metadata(self) -> dict:
|
|
114
|
+
"""
|
|
115
|
+
A copy of the metadata dictionary of additional key-value
|
|
116
|
+
fields. Returns a copy to preserve immutability.
|
|
117
|
+
|
|
118
|
+
:returns: A dictionary copy of the metadata fields.
|
|
119
|
+
"""
|
|
120
|
+
result = dict(self._metadata)
|
|
121
|
+
return result
|
|
122
|
+
|
|
123
|
+
def as_dict(self) -> dict:
|
|
124
|
+
"""
|
|
125
|
+
Returns a dictionary representation of the log entry fields.
|
|
126
|
+
|
|
127
|
+
The timestamp is serialized as an ISO format string for
|
|
128
|
+
consistent representation.
|
|
129
|
+
|
|
130
|
+
:returns: A dictionary containing all log entry fields.
|
|
131
|
+
"""
|
|
132
|
+
result = {
|
|
133
|
+
"timestamp": self._timestamp.isoformat(),
|
|
134
|
+
"level": self._level,
|
|
135
|
+
"message": self._message,
|
|
136
|
+
"logger_name": self._logger_name,
|
|
137
|
+
"correlation_id": self._correlation_id,
|
|
138
|
+
"agent_id": self._agent_id,
|
|
139
|
+
"metadata": dict(self._metadata)
|
|
140
|
+
}
|
|
141
|
+
return result
|
|
142
|
+
|
|
143
|
+
def to_json(self) -> str:
|
|
144
|
+
"""
|
|
145
|
+
Returns a JSON string representation of the log entry fields.
|
|
146
|
+
|
|
147
|
+
:returns: A JSON string containing all log entry fields.
|
|
148
|
+
"""
|
|
149
|
+
data = self.as_dict()
|
|
150
|
+
json_str = json.dumps(data)
|
|
151
|
+
return json_str
|
|
152
|
+
|
|
153
|
+
@classmethod
|
|
154
|
+
def from_dict(cls, data: dict) -> "StructuredLogEntry":
|
|
155
|
+
"""
|
|
156
|
+
Constructs a StructuredLogEntry from a dictionary.
|
|
157
|
+
|
|
158
|
+
The timestamp field is expected to be an ISO format string
|
|
159
|
+
and is parsed back into a datetime object.
|
|
160
|
+
|
|
161
|
+
:param data: A dictionary containing log entry fields.
|
|
162
|
+
|
|
163
|
+
:returns: A StructuredLogEntry instance reconstructed from
|
|
164
|
+
the dictionary.
|
|
165
|
+
"""
|
|
166
|
+
timestamp = datetime.fromisoformat(data["timestamp"])
|
|
167
|
+
level = data["level"]
|
|
168
|
+
message = data["message"]
|
|
169
|
+
logger_name = data["logger_name"]
|
|
170
|
+
correlation_id = data["correlation_id"]
|
|
171
|
+
agent_id = data["agent_id"]
|
|
172
|
+
metadata = data.get("metadata", None)
|
|
173
|
+
|
|
174
|
+
entry = cls(
|
|
175
|
+
timestamp=timestamp,
|
|
176
|
+
level=level,
|
|
177
|
+
message=message,
|
|
178
|
+
logger_name=logger_name,
|
|
179
|
+
correlation_id=correlation_id,
|
|
180
|
+
agent_id=agent_id,
|
|
181
|
+
metadata=metadata
|
|
182
|
+
)
|
|
183
|
+
return entry
|
|
184
|
+
|
|
185
|
+
@classmethod
|
|
186
|
+
def from_json(cls, json_str: str) -> "StructuredLogEntry":
|
|
187
|
+
"""
|
|
188
|
+
Deserializes a JSON string into a StructuredLogEntry instance.
|
|
189
|
+
|
|
190
|
+
:param json_str: A JSON string containing log entry fields.
|
|
191
|
+
|
|
192
|
+
:returns: A StructuredLogEntry instance reconstructed from
|
|
193
|
+
the JSON data.
|
|
194
|
+
"""
|
|
195
|
+
data = json.loads(json_str)
|
|
196
|
+
entry = cls.from_dict(data)
|
|
197
|
+
return entry
|
|
198
|
+
|
|
199
|
+
def __repr__(self) -> str:
|
|
200
|
+
"""
|
|
201
|
+
Returns a detailed string representation including class name,
|
|
202
|
+
log level, logger name, and correlation identifier.
|
|
203
|
+
|
|
204
|
+
:returns: A repr string for debugging.
|
|
205
|
+
"""
|
|
206
|
+
repr_str = "StructuredLogEntry(level={!r}, logger_name={!r}, correlation_id={!r})".format(
|
|
207
|
+
self._level, self._logger_name, self._correlation_id
|
|
208
|
+
)
|
|
209
|
+
return repr_str
|
|
210
|
+
|
|
211
|
+
def __str__(self) -> str:
|
|
212
|
+
"""
|
|
213
|
+
Returns a human-readable summary including the timestamp,
|
|
214
|
+
log level, logger name, message, and correlation identifier.
|
|
215
|
+
|
|
216
|
+
:returns: A human-readable string.
|
|
217
|
+
"""
|
|
218
|
+
str_val = "StructuredLogEntry: timestamp={}, level={}, logger_name={}, message={}, correlation_id={}".format(
|
|
219
|
+
self._timestamp.isoformat(), self._level, self._logger_name,
|
|
220
|
+
self._message, self._correlation_id
|
|
221
|
+
)
|
|
222
|
+
return str_val
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the StructuredLogger class that wraps the Python standard
|
|
3
|
+
logging module and enriches log records with correlation identifiers,
|
|
4
|
+
agent identity, and structured metadata fields.
|
|
5
|
+
|
|
6
|
+
The StructuredLogger produces StructuredLogEntry objects for each
|
|
7
|
+
log call and delegates to a standard Python logger under the
|
|
8
|
+
``ccai.agentkit.observability`` hierarchy. It accepts an
|
|
9
|
+
agent identifier and a CorrelationContext at construction time,
|
|
10
|
+
and provides methods for each standard log level: debug, info,
|
|
11
|
+
warning, error, and critical.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import logging
|
|
15
|
+
|
|
16
|
+
from datetime import datetime
|
|
17
|
+
|
|
18
|
+
from ccai.agentkit.correlationcontext import CorrelationContext
|
|
19
|
+
from ccai.agentkit.structuredlogentry import StructuredLogEntry
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class StructuredLogger:
|
|
23
|
+
"""
|
|
24
|
+
A logger wrapper that enriches standard Python log records with
|
|
25
|
+
correlation identifiers, agent identity, and structured metadata
|
|
26
|
+
fields.
|
|
27
|
+
|
|
28
|
+
The StructuredLogger produces StructuredLogEntry objects and
|
|
29
|
+
delegates to the Python standard logging module under the
|
|
30
|
+
``ccai.agentkit.observability`` logger hierarchy.
|
|
31
|
+
|
|
32
|
+
:param agent_id: The agent identifier string for the agent
|
|
33
|
+
that owns this logger.
|
|
34
|
+
:param correlation_context: The CorrelationContext providing
|
|
35
|
+
the correlation identifier for log entries.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(self, agent_id: str, correlation_context: CorrelationContext) -> None:
|
|
39
|
+
|
|
40
|
+
self._agent_id = agent_id
|
|
41
|
+
self._correlation_context = correlation_context
|
|
42
|
+
self._logger_name = "ccai.agentkit.observability"
|
|
43
|
+
self._logger = logging.getLogger(self._logger_name)
|
|
44
|
+
|
|
45
|
+
return
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def correlation_context(self) -> CorrelationContext:
|
|
49
|
+
"""
|
|
50
|
+
The current CorrelationContext used for enriching log entries.
|
|
51
|
+
|
|
52
|
+
:returns: The active CorrelationContext instance.
|
|
53
|
+
"""
|
|
54
|
+
return self._correlation_context
|
|
55
|
+
|
|
56
|
+
def set_correlation_context(self, context: CorrelationContext) -> None:
|
|
57
|
+
"""
|
|
58
|
+
Updates the active correlation context for subsequent log entries.
|
|
59
|
+
|
|
60
|
+
:param context: The new CorrelationContext to use for
|
|
61
|
+
enriching log entries.
|
|
62
|
+
"""
|
|
63
|
+
self._correlation_context = context
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
def debug(self, message: str, **metadata) -> StructuredLogEntry:
|
|
67
|
+
"""
|
|
68
|
+
Creates a structured log entry at the DEBUG level and delegates
|
|
69
|
+
to the standard Python logger.
|
|
70
|
+
|
|
71
|
+
:param message: The log message string.
|
|
72
|
+
:param metadata: Additional key-value pairs to include in the
|
|
73
|
+
log entry metadata.
|
|
74
|
+
|
|
75
|
+
:returns: The StructuredLogEntry created for this log call.
|
|
76
|
+
"""
|
|
77
|
+
entry = self._create_entry(level="debug", message=message, metadata=metadata)
|
|
78
|
+
self._logger.debug(message)
|
|
79
|
+
return entry
|
|
80
|
+
|
|
81
|
+
def info(self, message: str, **metadata) -> StructuredLogEntry:
|
|
82
|
+
"""
|
|
83
|
+
Creates a structured log entry at the INFO level and delegates
|
|
84
|
+
to the standard Python logger.
|
|
85
|
+
|
|
86
|
+
:param message: The log message string.
|
|
87
|
+
:param metadata: Additional key-value pairs to include in the
|
|
88
|
+
log entry metadata.
|
|
89
|
+
|
|
90
|
+
:returns: The StructuredLogEntry created for this log call.
|
|
91
|
+
"""
|
|
92
|
+
entry = self._create_entry(level="info", message=message, metadata=metadata)
|
|
93
|
+
self._logger.info(message)
|
|
94
|
+
return entry
|
|
95
|
+
|
|
96
|
+
def warning(self, message: str, **metadata) -> StructuredLogEntry:
|
|
97
|
+
"""
|
|
98
|
+
Creates a structured log entry at the WARNING level and delegates
|
|
99
|
+
to the standard Python logger.
|
|
100
|
+
|
|
101
|
+
:param message: The log message string.
|
|
102
|
+
:param metadata: Additional key-value pairs to include in the
|
|
103
|
+
log entry metadata.
|
|
104
|
+
|
|
105
|
+
:returns: The StructuredLogEntry created for this log call.
|
|
106
|
+
"""
|
|
107
|
+
entry = self._create_entry(level="warning", message=message, metadata=metadata)
|
|
108
|
+
self._logger.warning(message)
|
|
109
|
+
return entry
|
|
110
|
+
|
|
111
|
+
def error(self, message: str, **metadata) -> StructuredLogEntry:
|
|
112
|
+
"""
|
|
113
|
+
Creates a structured log entry at the ERROR level and delegates
|
|
114
|
+
to the standard Python logger.
|
|
115
|
+
|
|
116
|
+
:param message: The log message string.
|
|
117
|
+
:param metadata: Additional key-value pairs to include in the
|
|
118
|
+
log entry metadata.
|
|
119
|
+
|
|
120
|
+
:returns: The StructuredLogEntry created for this log call.
|
|
121
|
+
"""
|
|
122
|
+
entry = self._create_entry(level="error", message=message, metadata=metadata)
|
|
123
|
+
self._logger.error(message)
|
|
124
|
+
return entry
|
|
125
|
+
|
|
126
|
+
def critical(self, message: str, **metadata) -> StructuredLogEntry:
|
|
127
|
+
"""
|
|
128
|
+
Creates a structured log entry at the CRITICAL level and delegates
|
|
129
|
+
to the standard Python logger.
|
|
130
|
+
|
|
131
|
+
:param message: The log message string.
|
|
132
|
+
:param metadata: Additional key-value pairs to include in the
|
|
133
|
+
log entry metadata.
|
|
134
|
+
|
|
135
|
+
:returns: The StructuredLogEntry created for this log call.
|
|
136
|
+
"""
|
|
137
|
+
entry = self._create_entry(level="critical", message=message, metadata=metadata)
|
|
138
|
+
self._logger.critical(message)
|
|
139
|
+
return entry
|
|
140
|
+
|
|
141
|
+
def _create_entry(self, level: str, message: str, metadata: dict) -> StructuredLogEntry:
|
|
142
|
+
"""
|
|
143
|
+
Creates a StructuredLogEntry with the current timestamp,
|
|
144
|
+
correlation identifier, agent identifier, and provided
|
|
145
|
+
metadata.
|
|
146
|
+
|
|
147
|
+
:param level: The log level string.
|
|
148
|
+
:param message: The log message string.
|
|
149
|
+
:param metadata: Dictionary of additional key-value fields.
|
|
150
|
+
|
|
151
|
+
:returns: A new StructuredLogEntry instance.
|
|
152
|
+
"""
|
|
153
|
+
timestamp = datetime.utcnow()
|
|
154
|
+
correlation_id = self._correlation_context.correlation_id
|
|
155
|
+
|
|
156
|
+
entry = StructuredLogEntry(
|
|
157
|
+
timestamp=timestamp,
|
|
158
|
+
level=level,
|
|
159
|
+
message=message,
|
|
160
|
+
logger_name=self._logger_name,
|
|
161
|
+
correlation_id=correlation_id,
|
|
162
|
+
agent_id=self._agent_id,
|
|
163
|
+
metadata=metadata
|
|
164
|
+
)
|
|
165
|
+
return entry
|
|
166
|
+
|
|
167
|
+
def __repr__(self) -> str:
|
|
168
|
+
"""
|
|
169
|
+
Returns a detailed string representation including class name,
|
|
170
|
+
agent identifier, and logger name.
|
|
171
|
+
|
|
172
|
+
:returns: A repr string for debugging.
|
|
173
|
+
"""
|
|
174
|
+
repr_str = "StructuredLogger(agent_id={!r}, logger_name={!r})".format(
|
|
175
|
+
self._agent_id, self._logger_name
|
|
176
|
+
)
|
|
177
|
+
return repr_str
|
|
178
|
+
|
|
179
|
+
def __str__(self) -> str:
|
|
180
|
+
"""
|
|
181
|
+
Returns a human-readable summary including the agent identifier
|
|
182
|
+
and logger name.
|
|
183
|
+
|
|
184
|
+
:returns: A human-readable string.
|
|
185
|
+
"""
|
|
186
|
+
str_val = "StructuredLogger: agent_id={}, logger_name={}".format(
|
|
187
|
+
self._agent_id, self._logger_name
|
|
188
|
+
)
|
|
189
|
+
return str_val
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the Subscription class representing an immutable agent
|
|
3
|
+
subscription to a topic in the pub/sub messaging system.
|
|
4
|
+
|
|
5
|
+
A Subscription contains a unique identifier, the subscriber agent
|
|
6
|
+
identifier, the topic string, a MessageHandler callback, and an
|
|
7
|
+
optional message filter callable. The identifier is auto-generated
|
|
8
|
+
as a UUID when not provided explicitly.
|
|
9
|
+
|
|
10
|
+
.. module:: ccai.agentkit.subscription
|
|
11
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
12
|
+
:synopsis: Immutable subscription object for pub/sub messaging.
|
|
13
|
+
|
|
14
|
+
Requirements: 15.1-15.6, 26.19, 26.20
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import uuid
|
|
18
|
+
|
|
19
|
+
from typing import Callable
|
|
20
|
+
from typing import Optional
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Subscription:
|
|
24
|
+
"""
|
|
25
|
+
Immutable object representing an agent's subscription to a topic.
|
|
26
|
+
|
|
27
|
+
Contains a unique identifier, the subscriber agent identifier,
|
|
28
|
+
the topic string (which may include wildcard patterns), a
|
|
29
|
+
MessageHandler callback, and an optional message filter callable.
|
|
30
|
+
|
|
31
|
+
When created without an explicit identifier, a UUID-based
|
|
32
|
+
identifier is generated automatically.
|
|
33
|
+
|
|
34
|
+
:param subscriber_agent_id: The identifier of the subscribing agent.
|
|
35
|
+
:param topic: The topic string (supports wildcard patterns).
|
|
36
|
+
:param handler: The MessageHandler callback to invoke on matching messages.
|
|
37
|
+
:param message_filter: Optional filter callable for additional message filtering.
|
|
38
|
+
:param identifier: Optional unique identifier. If not provided, a UUID is generated.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
def __init__(self, subscriber_agent_id: str, topic: str,
|
|
42
|
+
handler: Callable, message_filter: Optional[Callable] = None,
|
|
43
|
+
identifier: Optional[str] = None) -> None:
|
|
44
|
+
|
|
45
|
+
if identifier is not None:
|
|
46
|
+
self._identifier = identifier
|
|
47
|
+
else:
|
|
48
|
+
self._identifier = str(uuid.uuid4())
|
|
49
|
+
|
|
50
|
+
self._subscriber_agent_id = subscriber_agent_id
|
|
51
|
+
self._topic = topic
|
|
52
|
+
self._handler = handler
|
|
53
|
+
self._message_filter = message_filter
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def identifier(self) -> str:
|
|
58
|
+
"""
|
|
59
|
+
The unique identifier of the subscription.
|
|
60
|
+
|
|
61
|
+
:returns: The identifier string.
|
|
62
|
+
"""
|
|
63
|
+
return self._identifier
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def subscriber_agent_id(self) -> str:
|
|
67
|
+
"""
|
|
68
|
+
The identifier of the subscribing agent.
|
|
69
|
+
|
|
70
|
+
:returns: The subscriber agent identifier string.
|
|
71
|
+
"""
|
|
72
|
+
return self._subscriber_agent_id
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def topic(self) -> str:
|
|
76
|
+
"""
|
|
77
|
+
The topic string for this subscription.
|
|
78
|
+
|
|
79
|
+
:returns: The topic string.
|
|
80
|
+
"""
|
|
81
|
+
return self._topic
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def handler(self) -> Callable:
|
|
85
|
+
"""
|
|
86
|
+
The MessageHandler callback for this subscription.
|
|
87
|
+
|
|
88
|
+
:returns: The handler callable.
|
|
89
|
+
"""
|
|
90
|
+
return self._handler
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def message_filter(self) -> Optional[Callable]:
|
|
94
|
+
"""
|
|
95
|
+
The optional message filter callable for this subscription.
|
|
96
|
+
|
|
97
|
+
:returns: The filter callable or None.
|
|
98
|
+
"""
|
|
99
|
+
return self._message_filter
|
|
100
|
+
|
|
101
|
+
def as_dict(self) -> dict:
|
|
102
|
+
"""
|
|
103
|
+
Returns a dictionary representation containing the identifier,
|
|
104
|
+
subscriber agent identifier, and topic string.
|
|
105
|
+
|
|
106
|
+
The handler and message filter callables are excluded from
|
|
107
|
+
the dictionary representation as they are not serializable.
|
|
108
|
+
|
|
109
|
+
:returns: A dictionary with subscription metadata fields.
|
|
110
|
+
"""
|
|
111
|
+
result = {
|
|
112
|
+
"identifier": self._identifier,
|
|
113
|
+
"subscriber_agent_id": self._subscriber_agent_id,
|
|
114
|
+
"topic": self._topic,
|
|
115
|
+
}
|
|
116
|
+
return result
|
|
117
|
+
|
|
118
|
+
def __repr__(self) -> str:
|
|
119
|
+
"""
|
|
120
|
+
Returns a detailed string representation including the class
|
|
121
|
+
name, identifier, subscriber agent identifier, and topic.
|
|
122
|
+
|
|
123
|
+
:returns: A repr string for debugging.
|
|
124
|
+
"""
|
|
125
|
+
repr_str = "{}(identifier={!r}, subscriber_agent_id={!r}, topic={!r})".format(
|
|
126
|
+
self.__class__.__name__, self._identifier,
|
|
127
|
+
self._subscriber_agent_id, self._topic
|
|
128
|
+
)
|
|
129
|
+
return repr_str
|
|
130
|
+
|
|
131
|
+
def __str__(self) -> str:
|
|
132
|
+
"""
|
|
133
|
+
Returns a human-readable summary including the subscriber
|
|
134
|
+
agent identifier and topic.
|
|
135
|
+
|
|
136
|
+
:returns: A human-readable string describing the subscription.
|
|
137
|
+
"""
|
|
138
|
+
str_val = "Subscription: subscriber={}, topic={}".format(
|
|
139
|
+
self._subscriber_agent_id, self._topic
|
|
140
|
+
)
|
|
141
|
+
return str_val
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the ThreadedAgent abstract base class for agents that run
|
|
3
|
+
on a dedicated thread in the codecraftai-agent-kit framework.
|
|
4
|
+
|
|
5
|
+
ThreadedAgent extends Agent and provides a run() method that starts
|
|
6
|
+
the agent's lifecycle on a dedicated thread. Subclasses implement
|
|
7
|
+
the abstract lifecycle methods which are invoked on the agent's thread.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import threading
|
|
11
|
+
|
|
12
|
+
from typing import Optional
|
|
13
|
+
|
|
14
|
+
from ccai.agentkit.agent import Agent
|
|
15
|
+
from ccai.agentkit.agentconfiguration import AgentConfiguration
|
|
16
|
+
from ccai.agentkit.concurrencymodel import ConcurrencyModel
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ThreadedAgent(Agent):
|
|
20
|
+
"""
|
|
21
|
+
Abstract base class for agents that run on a dedicated thread.
|
|
22
|
+
|
|
23
|
+
Each ThreadedAgent instance manages its own thread. The run()
|
|
24
|
+
method starts the agent's thread, which initializes and starts
|
|
25
|
+
the agent. Subclasses must implement the abstract lifecycle
|
|
26
|
+
methods: initialize, start, pause, resume, stop, and reset.
|
|
27
|
+
|
|
28
|
+
:param configuration: The AgentConfiguration for this agent.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, configuration: AgentConfiguration) -> None:
|
|
32
|
+
super().__init__(configuration)
|
|
33
|
+
self._concurrency_model: ConcurrencyModel = ConcurrencyModel.THREAD
|
|
34
|
+
self._thread: Optional[threading.Thread] = None
|
|
35
|
+
self._thread_started: bool = False
|
|
36
|
+
return
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def concurrency_model(self) -> ConcurrencyModel:
|
|
40
|
+
"""
|
|
41
|
+
The concurrency model used by this agent.
|
|
42
|
+
|
|
43
|
+
:returns: ConcurrencyModel.THREAD
|
|
44
|
+
"""
|
|
45
|
+
return self._concurrency_model
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def thread(self) -> Optional[threading.Thread]:
|
|
49
|
+
"""
|
|
50
|
+
The dedicated thread for this agent, or None if not started.
|
|
51
|
+
|
|
52
|
+
:returns: The threading.Thread instance or None.
|
|
53
|
+
"""
|
|
54
|
+
return self._thread
|
|
55
|
+
|
|
56
|
+
def run(self) -> None:
|
|
57
|
+
"""
|
|
58
|
+
Starts the agent's dedicated thread.
|
|
59
|
+
|
|
60
|
+
Creates a daemon thread that calls the _thread_entry method.
|
|
61
|
+
The thread is named after the agent's identifier for
|
|
62
|
+
debuggability.
|
|
63
|
+
"""
|
|
64
|
+
thread_name = "ThreadedAgent-{}".format(self._identity.identifier)
|
|
65
|
+
self._thread = threading.Thread(
|
|
66
|
+
target=self._thread_entry,
|
|
67
|
+
name=thread_name,
|
|
68
|
+
daemon=True
|
|
69
|
+
)
|
|
70
|
+
self._thread_started = True
|
|
71
|
+
self._thread.start()
|
|
72
|
+
return
|
|
73
|
+
|
|
74
|
+
def _thread_entry(self) -> None:
|
|
75
|
+
"""
|
|
76
|
+
Entry point for the agent's dedicated thread.
|
|
77
|
+
|
|
78
|
+
Calls initialize and start on the agent. Subclasses can
|
|
79
|
+
override this method to customize the thread behavior.
|
|
80
|
+
"""
|
|
81
|
+
self.initialize()
|
|
82
|
+
self.start()
|
|
83
|
+
return
|
|
84
|
+
|
|
85
|
+
def join(self, timeout: Optional[float] = None) -> None:
|
|
86
|
+
"""
|
|
87
|
+
Waits for the agent's thread to complete.
|
|
88
|
+
|
|
89
|
+
:param timeout: Optional timeout in seconds to wait for the thread.
|
|
90
|
+
"""
|
|
91
|
+
if self._thread is not None:
|
|
92
|
+
self._thread.join(timeout=timeout)
|
|
93
|
+
|
|
94
|
+
return
|