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,54 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Enumeration defining the visibility scope of a MemoryEntry.
|
|
3
|
+
|
|
4
|
+
Provides AGENT, SHARED, and GLOBAL scope values for controlling
|
|
5
|
+
memory entry visibility across agents.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import enum
|
|
9
|
+
|
|
10
|
+
from ccai.agentkit.errors import SemanticError
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class MemoryScope(enum.Enum):
|
|
14
|
+
"""
|
|
15
|
+
Enumeration defining the visibility scope of a MemoryEntry.
|
|
16
|
+
|
|
17
|
+
AGENT — visible only to the owning agent.
|
|
18
|
+
SHARED — visible to all agents that reference the same MemoryStore instance.
|
|
19
|
+
GLOBAL — visible to all agents in the runtime.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
AGENT = "AGENT"
|
|
23
|
+
SHARED = "SHARED"
|
|
24
|
+
GLOBAL = "GLOBAL"
|
|
25
|
+
|
|
26
|
+
@classmethod
|
|
27
|
+
def from_string(cls, value: str) -> "MemoryScope":
|
|
28
|
+
"""
|
|
29
|
+
Returns the MemoryScope member matching the given string value.
|
|
30
|
+
|
|
31
|
+
:param value: A string value to match against MemoryScope members.
|
|
32
|
+
|
|
33
|
+
:returns: The matching MemoryScope member.
|
|
34
|
+
:raises: SemanticError if the value does not match any member.
|
|
35
|
+
"""
|
|
36
|
+
upper_value = value.upper().strip()
|
|
37
|
+
for member in cls:
|
|
38
|
+
if member.value == upper_value:
|
|
39
|
+
rtnval = member
|
|
40
|
+
return rtnval
|
|
41
|
+
|
|
42
|
+
valid_values = [m.value for m in cls]
|
|
43
|
+
errmsg = "Invalid MemoryScope value {!r}. Valid values are: {}".format(
|
|
44
|
+
value, ", ".join(valid_values)
|
|
45
|
+
)
|
|
46
|
+
raise SemanticError(errmsg)
|
|
47
|
+
|
|
48
|
+
def __repr__(self) -> str:
|
|
49
|
+
rtnval = "{}({!r})".format(self.__class__.__name__, self.value)
|
|
50
|
+
return rtnval
|
|
51
|
+
|
|
52
|
+
def __str__(self) -> str:
|
|
53
|
+
rtnval = self.value
|
|
54
|
+
return rtnval
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Abstract base class for memory storage backends.
|
|
3
|
+
|
|
4
|
+
Provides a common interface for storing, retrieving, searching,
|
|
5
|
+
updating, and deleting MemoryEntry objects. Concrete implementations
|
|
6
|
+
wrap specific backends (in-memory, file-based, vector store, database).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import abc
|
|
10
|
+
import logging
|
|
11
|
+
from datetime import datetime, timedelta, timezone
|
|
12
|
+
from typing import Optional
|
|
13
|
+
|
|
14
|
+
from ccai.agentkit.embeddingprovider import EmbeddingProvider
|
|
15
|
+
from ccai.agentkit.errors import SemanticError
|
|
16
|
+
from ccai.agentkit.expirationpolicy import ExpirationPolicy
|
|
17
|
+
from ccai.agentkit.memoryentry import MemoryEntry
|
|
18
|
+
from ccai.agentkit.memoryquery import MemoryQuery
|
|
19
|
+
from ccai.agentkit.memoryqueryresult import MemoryQueryResult
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger("ccai.agentkit.memory")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class MemoryStore(abc.ABC):
|
|
26
|
+
"""
|
|
27
|
+
Abstract base class for memory storage backends.
|
|
28
|
+
Provides a common interface for storing, retrieving, searching,
|
|
29
|
+
updating, and deleting MemoryEntry objects.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, store_name: str, embedding_provider: EmbeddingProvider = None,
|
|
33
|
+
expiration_policy: ExpirationPolicy = None,
|
|
34
|
+
credential_names: list = None) -> None:
|
|
35
|
+
"""
|
|
36
|
+
Constructs a MemoryStore.
|
|
37
|
+
|
|
38
|
+
:param store_name: The name of this memory store.
|
|
39
|
+
:param embedding_provider: Optional EmbeddingProvider for generating embeddings.
|
|
40
|
+
:param expiration_policy: Optional ExpirationPolicy for entry lifecycle.
|
|
41
|
+
:param credential_names: Optional list of CredentialReference names.
|
|
42
|
+
|
|
43
|
+
:raises: SemanticError if store_name is empty.
|
|
44
|
+
"""
|
|
45
|
+
if store_name is None or len(str(store_name).strip()) == 0:
|
|
46
|
+
errmsg = "A store name is required when constructing a MemoryStore."
|
|
47
|
+
raise SemanticError(errmsg)
|
|
48
|
+
|
|
49
|
+
self._store_name = store_name
|
|
50
|
+
self._embedding_provider = embedding_provider
|
|
51
|
+
self._expiration_policy = expiration_policy
|
|
52
|
+
self._credential_names = list(credential_names) if credential_names is not None else []
|
|
53
|
+
return
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def store_name(self) -> str:
|
|
57
|
+
"""
|
|
58
|
+
The name of this memory store.
|
|
59
|
+
|
|
60
|
+
:returns: The store name string.
|
|
61
|
+
"""
|
|
62
|
+
return self._store_name
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def embedding_provider(self) -> Optional[EmbeddingProvider]:
|
|
66
|
+
"""
|
|
67
|
+
The optional EmbeddingProvider for generating embeddings.
|
|
68
|
+
|
|
69
|
+
:returns: The EmbeddingProvider instance or None.
|
|
70
|
+
"""
|
|
71
|
+
return self._embedding_provider
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def expiration_policy(self) -> Optional[ExpirationPolicy]:
|
|
75
|
+
"""
|
|
76
|
+
The optional ExpirationPolicy for entry lifecycle management.
|
|
77
|
+
|
|
78
|
+
:returns: The ExpirationPolicy instance or None.
|
|
79
|
+
"""
|
|
80
|
+
return self._expiration_policy
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
@abc.abstractmethod
|
|
84
|
+
def count(self) -> int:
|
|
85
|
+
"""
|
|
86
|
+
Returns the count of stored MemoryEntry objects.
|
|
87
|
+
|
|
88
|
+
:returns: The entry count as an integer.
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
@abc.abstractmethod
|
|
92
|
+
def store(self, entry: MemoryEntry) -> MemoryEntry:
|
|
93
|
+
"""
|
|
94
|
+
Stores a MemoryEntry. If the entry has no embedding and an
|
|
95
|
+
EmbeddingProvider is configured, generates an embedding first.
|
|
96
|
+
|
|
97
|
+
:param entry: The MemoryEntry to store.
|
|
98
|
+
|
|
99
|
+
:returns: The stored MemoryEntry (potentially with generated embedding).
|
|
100
|
+
:raises: MemoryStoreError, SemanticError
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
@abc.abstractmethod
|
|
104
|
+
def retrieve(self, identifier: str) -> Optional[MemoryEntry]:
|
|
105
|
+
"""
|
|
106
|
+
Retrieves a MemoryEntry by identifier.
|
|
107
|
+
|
|
108
|
+
:param identifier: The entry identifier.
|
|
109
|
+
|
|
110
|
+
:returns: The MemoryEntry, or None if not found.
|
|
111
|
+
:raises: MemoryStoreError
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
@abc.abstractmethod
|
|
115
|
+
def search(self, query: MemoryQuery) -> MemoryQueryResult:
|
|
116
|
+
"""
|
|
117
|
+
Executes a MemoryQuery against the store.
|
|
118
|
+
|
|
119
|
+
:param query: The MemoryQuery with filter criteria.
|
|
120
|
+
|
|
121
|
+
:returns: A MemoryQueryResult with matching entries.
|
|
122
|
+
:raises: MemoryStoreError
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
@abc.abstractmethod
|
|
126
|
+
def update(self, identifier: str, entry: MemoryEntry) -> MemoryEntry:
|
|
127
|
+
"""
|
|
128
|
+
Replaces a MemoryEntry by identifier.
|
|
129
|
+
|
|
130
|
+
:param identifier: The identifier of the entry to replace.
|
|
131
|
+
:param entry: The new MemoryEntry.
|
|
132
|
+
|
|
133
|
+
:returns: The updated MemoryEntry.
|
|
134
|
+
:raises: MemoryStoreError
|
|
135
|
+
"""
|
|
136
|
+
|
|
137
|
+
@abc.abstractmethod
|
|
138
|
+
def delete(self, identifier: str) -> None:
|
|
139
|
+
"""
|
|
140
|
+
Removes a MemoryEntry by identifier.
|
|
141
|
+
|
|
142
|
+
:param identifier: The entry identifier to remove.
|
|
143
|
+
:raises: MemoryStoreError
|
|
144
|
+
"""
|
|
145
|
+
|
|
146
|
+
@abc.abstractmethod
|
|
147
|
+
def clear(self) -> None:
|
|
148
|
+
"""
|
|
149
|
+
Removes all entries from the store.
|
|
150
|
+
|
|
151
|
+
:raises: MemoryStoreError
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
def contains(self, identifier: str) -> bool:
|
|
155
|
+
"""
|
|
156
|
+
Returns True if an entry with the given identifier exists.
|
|
157
|
+
|
|
158
|
+
:param identifier: The entry identifier.
|
|
159
|
+
|
|
160
|
+
:returns: True if the entry exists, False otherwise.
|
|
161
|
+
"""
|
|
162
|
+
entry = self.retrieve(identifier=identifier)
|
|
163
|
+
rtnval = entry is not None
|
|
164
|
+
return rtnval
|
|
165
|
+
|
|
166
|
+
def cleanup_expired(self, reference_time: datetime = None) -> int:
|
|
167
|
+
"""
|
|
168
|
+
Removes all MemoryEntry objects whose expiration timestamp has passed.
|
|
169
|
+
|
|
170
|
+
:param reference_time: Optional reference timestamp. Defaults to current UTC time.
|
|
171
|
+
|
|
172
|
+
:returns: The count of removed entries.
|
|
173
|
+
"""
|
|
174
|
+
if reference_time is None:
|
|
175
|
+
reference_time = datetime.now(timezone.utc)
|
|
176
|
+
|
|
177
|
+
removed_count = 0
|
|
178
|
+
all_result = self.search(MemoryQuery())
|
|
179
|
+
for entry in all_result.entries:
|
|
180
|
+
if entry.is_expired(reference_time=reference_time):
|
|
181
|
+
self.delete(identifier=entry.identifier)
|
|
182
|
+
removed_count = removed_count + 1
|
|
183
|
+
|
|
184
|
+
if removed_count > 0:
|
|
185
|
+
logger.info(
|
|
186
|
+
"MemoryStore %r cleaned up %d expired entries.",
|
|
187
|
+
self._store_name, removed_count
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
rtnval = removed_count
|
|
191
|
+
return rtnval
|
|
192
|
+
|
|
193
|
+
def _generate_embedding_if_needed(self, entry: MemoryEntry) -> MemoryEntry:
|
|
194
|
+
"""
|
|
195
|
+
If the entry has no embedding and an EmbeddingProvider is configured,
|
|
196
|
+
generates an embedding and returns a new MemoryEntry with the embedding.
|
|
197
|
+
|
|
198
|
+
:param entry: The MemoryEntry to check.
|
|
199
|
+
|
|
200
|
+
:returns: The original entry or a new entry with embedding.
|
|
201
|
+
"""
|
|
202
|
+
if entry.embedding is not None:
|
|
203
|
+
return entry
|
|
204
|
+
|
|
205
|
+
if self._embedding_provider is None:
|
|
206
|
+
return entry
|
|
207
|
+
|
|
208
|
+
embedding = self._embedding_provider.embed(text=entry.content)
|
|
209
|
+
rtnval = MemoryEntry(
|
|
210
|
+
content=entry.content,
|
|
211
|
+
scope=entry.scope,
|
|
212
|
+
identifier=entry.identifier,
|
|
213
|
+
embedding=embedding,
|
|
214
|
+
metadata=entry.metadata,
|
|
215
|
+
tags=entry.tags,
|
|
216
|
+
created_at=entry.created_at,
|
|
217
|
+
expires_at=entry.expires_at,
|
|
218
|
+
)
|
|
219
|
+
return rtnval
|
|
220
|
+
|
|
221
|
+
def _apply_expiration_policy(self, entry: MemoryEntry) -> MemoryEntry:
|
|
222
|
+
"""
|
|
223
|
+
If the entry has no expiration and the ExpirationPolicy defines a
|
|
224
|
+
default TTL, returns a new MemoryEntry with the expiration set.
|
|
225
|
+
|
|
226
|
+
:param entry: The MemoryEntry to check.
|
|
227
|
+
|
|
228
|
+
:returns: The original entry or a new entry with expiration.
|
|
229
|
+
"""
|
|
230
|
+
if entry.expires_at is not None:
|
|
231
|
+
return entry
|
|
232
|
+
|
|
233
|
+
if self._expiration_policy is None:
|
|
234
|
+
return entry
|
|
235
|
+
|
|
236
|
+
if self._expiration_policy.default_ttl is None:
|
|
237
|
+
return entry
|
|
238
|
+
|
|
239
|
+
expires_at = entry.created_at + timedelta(seconds=self._expiration_policy.default_ttl)
|
|
240
|
+
rtnval = MemoryEntry(
|
|
241
|
+
content=entry.content,
|
|
242
|
+
scope=entry.scope,
|
|
243
|
+
identifier=entry.identifier,
|
|
244
|
+
embedding=entry.embedding,
|
|
245
|
+
metadata=entry.metadata,
|
|
246
|
+
tags=entry.tags,
|
|
247
|
+
created_at=entry.created_at,
|
|
248
|
+
expires_at=expires_at,
|
|
249
|
+
)
|
|
250
|
+
return rtnval
|
|
251
|
+
|
|
252
|
+
def __repr__(self) -> str:
|
|
253
|
+
rtnval = "{}(store_name={!r}, count={})".format(
|
|
254
|
+
self.__class__.__name__, self._store_name, self.count
|
|
255
|
+
)
|
|
256
|
+
return rtnval
|
|
257
|
+
|
|
258
|
+
def __str__(self) -> str:
|
|
259
|
+
rtnval = "MemoryStore: {} ({} entries)".format(self._store_name, self.count)
|
|
260
|
+
return rtnval
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the MemoryStoreHealthCheck for checking MemoryStore availability.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
from ccai.agentkit.healthcheck import HealthCheck
|
|
8
|
+
from ccai.agentkit.healthstatus import HealthStatus
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class MemoryStoreHealthCheck(HealthCheck):
|
|
12
|
+
"""
|
|
13
|
+
Checks MemoryStore availability via a lightweight read.
|
|
14
|
+
|
|
15
|
+
:param name: The health check name.
|
|
16
|
+
:param memory_store: The memory store instance to check.
|
|
17
|
+
:param check_interval: The interval between checks in seconds.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self, name: str, memory_store, check_interval: float = 30.0) -> None:
|
|
21
|
+
super().__init__(name, check_interval)
|
|
22
|
+
self._memory_store = memory_store
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
def check(self) -> HealthStatus:
|
|
26
|
+
start = time.monotonic()
|
|
27
|
+
try:
|
|
28
|
+
self._memory_store.ping()
|
|
29
|
+
elapsed = time.monotonic() - start
|
|
30
|
+
return HealthStatus(self._name, "healthy", response_time=elapsed)
|
|
31
|
+
except Exception as exc:
|
|
32
|
+
elapsed = time.monotonic() - start
|
|
33
|
+
return HealthStatus(self._name, "unhealthy", detail=str(exc), response_time=elapsed)
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Registry that tracks all available MemoryStore instances within
|
|
3
|
+
a runtime, providing lookup by name and enumeration.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Optional
|
|
7
|
+
|
|
8
|
+
from ccai.agentkit.errors import SemanticError
|
|
9
|
+
from ccai.agentkit.memorystore import MemoryStore
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class MemoryStoreRegistry:
|
|
13
|
+
"""
|
|
14
|
+
Registry that tracks all available MemoryStore instances,
|
|
15
|
+
providing lookup by name and enumeration.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def __init__(self) -> None:
|
|
19
|
+
"""
|
|
20
|
+
Constructs a MemoryStoreRegistry with an empty stores dictionary.
|
|
21
|
+
"""
|
|
22
|
+
self._stores = {}
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
def register(self, store: MemoryStore) -> None:
|
|
26
|
+
"""
|
|
27
|
+
Registers a MemoryStore instance using the store name as key.
|
|
28
|
+
|
|
29
|
+
:param store: The MemoryStore to register.
|
|
30
|
+
|
|
31
|
+
:raises: SemanticError if a store with the same name is already registered.
|
|
32
|
+
"""
|
|
33
|
+
name = store.store_name
|
|
34
|
+
if name in self._stores:
|
|
35
|
+
errmsg = "A MemoryStore with name {!r} is already registered.".format(name)
|
|
36
|
+
raise SemanticError(errmsg)
|
|
37
|
+
|
|
38
|
+
self._stores[name] = store
|
|
39
|
+
return
|
|
40
|
+
|
|
41
|
+
def deregister(self, name: str) -> None:
|
|
42
|
+
"""
|
|
43
|
+
Deregisters a MemoryStore by name.
|
|
44
|
+
|
|
45
|
+
:param name: The store name to deregister.
|
|
46
|
+
"""
|
|
47
|
+
if name in self._stores:
|
|
48
|
+
del self._stores[name]
|
|
49
|
+
return
|
|
50
|
+
|
|
51
|
+
def lookup(self, name: str) -> Optional[MemoryStore]:
|
|
52
|
+
"""
|
|
53
|
+
Looks up a registered MemoryStore by name.
|
|
54
|
+
|
|
55
|
+
:param name: The store name.
|
|
56
|
+
|
|
57
|
+
:returns: The MemoryStore instance, or None if not found.
|
|
58
|
+
"""
|
|
59
|
+
rtnval = self._stores.get(name)
|
|
60
|
+
return rtnval
|
|
61
|
+
|
|
62
|
+
def list_all(self) -> list:
|
|
63
|
+
"""
|
|
64
|
+
Returns a list of all registered MemoryStore instances.
|
|
65
|
+
|
|
66
|
+
:returns: A list of MemoryStore instances.
|
|
67
|
+
"""
|
|
68
|
+
rtnval = list(self._stores.values())
|
|
69
|
+
return rtnval
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def count(self) -> int:
|
|
73
|
+
"""
|
|
74
|
+
The count of currently registered stores.
|
|
75
|
+
|
|
76
|
+
:returns: The store count as an integer.
|
|
77
|
+
"""
|
|
78
|
+
rtnval = len(self._stores)
|
|
79
|
+
return rtnval
|
|
80
|
+
|
|
81
|
+
def has_store(self, name: str) -> bool:
|
|
82
|
+
"""
|
|
83
|
+
Returns True if a MemoryStore with the given name is registered.
|
|
84
|
+
|
|
85
|
+
:param name: The store name.
|
|
86
|
+
|
|
87
|
+
:returns: True if registered, False otherwise.
|
|
88
|
+
"""
|
|
89
|
+
rtnval = name in self._stores
|
|
90
|
+
return rtnval
|
|
91
|
+
|
|
92
|
+
def clear(self) -> None:
|
|
93
|
+
"""
|
|
94
|
+
Removes all registered stores.
|
|
95
|
+
"""
|
|
96
|
+
self._stores = {}
|
|
97
|
+
return
|
|
98
|
+
|
|
99
|
+
def __repr__(self) -> str:
|
|
100
|
+
rtnval = "{}(count={})".format(self.__class__.__name__, len(self._stores))
|
|
101
|
+
return rtnval
|
|
102
|
+
|
|
103
|
+
def __str__(self) -> str:
|
|
104
|
+
names = list(self._stores.keys())
|
|
105
|
+
rtnval = "MemoryStoreRegistry: [{}]".format(", ".join(names))
|
|
106
|
+
return rtnval
|