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,221 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the PromptTemplate class for named, versioned prompt templates.
|
|
3
|
+
|
|
4
|
+
PromptTemplate encapsulates a name, version, template string with
|
|
5
|
+
placeholder variables using Python str.format syntax, and an optional
|
|
6
|
+
description. Placeholder variable names are extracted automatically
|
|
7
|
+
from the template string. The render method replaces placeholders
|
|
8
|
+
with provided values, raising SemanticError for missing variables.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import string
|
|
13
|
+
|
|
14
|
+
from ccai.agentkit.errors import SemanticError
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class PromptTemplate:
|
|
18
|
+
"""
|
|
19
|
+
A named, versioned template string with placeholder variables
|
|
20
|
+
using Python str.format syntax.
|
|
21
|
+
|
|
22
|
+
Provides read-only access to the name, version, template, description,
|
|
23
|
+
and extracted variable names. Supports rendering with variable values,
|
|
24
|
+
serialization to and from JSON and dictionary representations.
|
|
25
|
+
|
|
26
|
+
:param name: The template name.
|
|
27
|
+
:param version: The template version string.
|
|
28
|
+
:param template: The template string with {placeholder} variables.
|
|
29
|
+
:param description: Optional description.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, name: str, version: str, template: str,
|
|
33
|
+
description: str = "") -> None:
|
|
34
|
+
self._name = name
|
|
35
|
+
self._version = version
|
|
36
|
+
self._template = template
|
|
37
|
+
self._description = description
|
|
38
|
+
self._variables = self._extract_variables()
|
|
39
|
+
return
|
|
40
|
+
|
|
41
|
+
def _extract_variables(self) -> list:
|
|
42
|
+
"""
|
|
43
|
+
Extracts placeholder variable names from the template string
|
|
44
|
+
using string.Formatter to parse the template.
|
|
45
|
+
|
|
46
|
+
:returns: A list of unique variable name strings in order of appearance.
|
|
47
|
+
"""
|
|
48
|
+
formatter = string.Formatter()
|
|
49
|
+
variables = []
|
|
50
|
+
|
|
51
|
+
for _, field_name, _, _ in formatter.parse(self._template):
|
|
52
|
+
if field_name is not None and field_name not in variables:
|
|
53
|
+
variables.append(field_name)
|
|
54
|
+
|
|
55
|
+
rtnval = variables
|
|
56
|
+
return rtnval
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def name(self) -> str:
|
|
60
|
+
"""
|
|
61
|
+
The template name string.
|
|
62
|
+
|
|
63
|
+
:returns: The name as a string.
|
|
64
|
+
"""
|
|
65
|
+
rtnval = self._name
|
|
66
|
+
return rtnval
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def version(self) -> str:
|
|
70
|
+
"""
|
|
71
|
+
The template version string.
|
|
72
|
+
|
|
73
|
+
:returns: The version as a string.
|
|
74
|
+
"""
|
|
75
|
+
rtnval = self._version
|
|
76
|
+
return rtnval
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def template(self) -> str:
|
|
80
|
+
"""
|
|
81
|
+
The template string with placeholder variables.
|
|
82
|
+
|
|
83
|
+
:returns: The template as a string.
|
|
84
|
+
"""
|
|
85
|
+
rtnval = self._template
|
|
86
|
+
return rtnval
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def description(self) -> str:
|
|
90
|
+
"""
|
|
91
|
+
The optional description string.
|
|
92
|
+
|
|
93
|
+
:returns: The description as a string.
|
|
94
|
+
"""
|
|
95
|
+
rtnval = self._description
|
|
96
|
+
return rtnval
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def variables(self) -> list:
|
|
100
|
+
"""
|
|
101
|
+
A copy of the list of placeholder variable names extracted
|
|
102
|
+
from the template string.
|
|
103
|
+
|
|
104
|
+
:returns: A list of variable name strings.
|
|
105
|
+
"""
|
|
106
|
+
rtnval = list(self._variables)
|
|
107
|
+
return rtnval
|
|
108
|
+
|
|
109
|
+
def render(self, values: dict) -> str:
|
|
110
|
+
"""
|
|
111
|
+
Renders the template with the provided variable values.
|
|
112
|
+
|
|
113
|
+
:param values: A dictionary mapping variable names to values.
|
|
114
|
+
|
|
115
|
+
:returns: The rendered prompt string.
|
|
116
|
+
:raises: SemanticError if required variables are missing.
|
|
117
|
+
"""
|
|
118
|
+
missing = [v for v in self._variables if v not in values]
|
|
119
|
+
|
|
120
|
+
if len(missing) > 0:
|
|
121
|
+
errmsg = "PromptTemplate {!r} v{} is missing required variables: {}".format(
|
|
122
|
+
self._name, self._version, ", ".join(missing)
|
|
123
|
+
)
|
|
124
|
+
raise SemanticError(errmsg)
|
|
125
|
+
|
|
126
|
+
rtnval = self._template.format(**values)
|
|
127
|
+
return rtnval
|
|
128
|
+
|
|
129
|
+
def as_dict(self) -> dict:
|
|
130
|
+
"""
|
|
131
|
+
Returns a dictionary representation of the template fields.
|
|
132
|
+
|
|
133
|
+
:returns: A dictionary with name, version, template, and description keys.
|
|
134
|
+
"""
|
|
135
|
+
rtnval = {
|
|
136
|
+
"name": self._name,
|
|
137
|
+
"version": self._version,
|
|
138
|
+
"template": self._template,
|
|
139
|
+
"description": self._description
|
|
140
|
+
}
|
|
141
|
+
return rtnval
|
|
142
|
+
|
|
143
|
+
def to_json(self) -> str:
|
|
144
|
+
"""
|
|
145
|
+
Returns a JSON string representation of the template fields.
|
|
146
|
+
|
|
147
|
+
:returns: A JSON string.
|
|
148
|
+
"""
|
|
149
|
+
rtnval = json.dumps(self.as_dict())
|
|
150
|
+
return rtnval
|
|
151
|
+
|
|
152
|
+
@classmethod
|
|
153
|
+
def from_dict(cls, data: dict) -> "PromptTemplate":
|
|
154
|
+
"""
|
|
155
|
+
Constructs a PromptTemplate from a dictionary.
|
|
156
|
+
|
|
157
|
+
:param data: A dictionary with name, version, template, and optional description keys.
|
|
158
|
+
|
|
159
|
+
:returns: A PromptTemplate instance.
|
|
160
|
+
"""
|
|
161
|
+
rtnval = cls(
|
|
162
|
+
name=data["name"],
|
|
163
|
+
version=data["version"],
|
|
164
|
+
template=data["template"],
|
|
165
|
+
description=data.get("description", "")
|
|
166
|
+
)
|
|
167
|
+
return rtnval
|
|
168
|
+
|
|
169
|
+
@classmethod
|
|
170
|
+
def from_json(cls, json_str: str) -> "PromptTemplate":
|
|
171
|
+
"""
|
|
172
|
+
Deserializes a JSON string into a PromptTemplate.
|
|
173
|
+
|
|
174
|
+
:param json_str: A JSON string.
|
|
175
|
+
|
|
176
|
+
:returns: A PromptTemplate instance.
|
|
177
|
+
"""
|
|
178
|
+
data = json.loads(json_str)
|
|
179
|
+
rtnval = cls.from_dict(data)
|
|
180
|
+
return rtnval
|
|
181
|
+
|
|
182
|
+
def __repr__(self) -> str:
|
|
183
|
+
"""
|
|
184
|
+
Returns a detailed string representation including the class name,
|
|
185
|
+
name, and version.
|
|
186
|
+
|
|
187
|
+
:returns: A repr string for debugging.
|
|
188
|
+
"""
|
|
189
|
+
rtnval = "{}(name={!r}, version={!r})".format(
|
|
190
|
+
self.__class__.__name__, self._name, self._version
|
|
191
|
+
)
|
|
192
|
+
return rtnval
|
|
193
|
+
|
|
194
|
+
def __str__(self) -> str:
|
|
195
|
+
"""
|
|
196
|
+
Returns a human-readable summary including the name, version,
|
|
197
|
+
and variable names.
|
|
198
|
+
|
|
199
|
+
:returns: A human-readable string.
|
|
200
|
+
"""
|
|
201
|
+
rtnval = "PromptTemplate: {} v{} [variables: {}]".format(
|
|
202
|
+
self._name, self._version, ", ".join(self._variables)
|
|
203
|
+
)
|
|
204
|
+
return rtnval
|
|
205
|
+
|
|
206
|
+
def __eq__(self, other) -> bool:
|
|
207
|
+
"""
|
|
208
|
+
Checks equality based on name, version, template, and description fields.
|
|
209
|
+
|
|
210
|
+
:param other: The object to compare against.
|
|
211
|
+
|
|
212
|
+
:returns: True if all fields are equal, False otherwise.
|
|
213
|
+
"""
|
|
214
|
+
if not isinstance(other, PromptTemplate):
|
|
215
|
+
return False
|
|
216
|
+
|
|
217
|
+
rtnval = (self._name == other._name and
|
|
218
|
+
self._version == other._version and
|
|
219
|
+
self._template == other._template and
|
|
220
|
+
self._description == other._description)
|
|
221
|
+
return rtnval
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the PromptTemplateRegistry class for tracking available
|
|
3
|
+
PromptTemplate instances in the codecraftai-agent-kit framework.
|
|
4
|
+
|
|
5
|
+
PromptTemplateRegistry provides registration, lookup by name and
|
|
6
|
+
optional version, enumeration, count, and loading from JSON and
|
|
7
|
+
YAML files. Templates are keyed by a composite key of (name, version).
|
|
8
|
+
When looking up by name without a version, the registry returns the
|
|
9
|
+
latest version based on lexicographic sort of version strings.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
|
|
14
|
+
from ccai.agentkit.errors import SemanticError
|
|
15
|
+
from ccai.agentkit.prompttemplate import PromptTemplate
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class PromptTemplateRegistry:
|
|
19
|
+
"""
|
|
20
|
+
Registry for PromptTemplate instances, keyed by name and version.
|
|
21
|
+
|
|
22
|
+
Provides registration with duplicate detection, lookup by name
|
|
23
|
+
and optional version, enumeration of all templates, a count
|
|
24
|
+
property, and loading from JSON and YAML file paths.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(self) -> None:
|
|
28
|
+
self._templates: dict = {}
|
|
29
|
+
return
|
|
30
|
+
|
|
31
|
+
def register(self, template: PromptTemplate) -> None:
|
|
32
|
+
"""
|
|
33
|
+
Registers a PromptTemplate using name + version as a composite key.
|
|
34
|
+
|
|
35
|
+
:param template: The PromptTemplate instance to register.
|
|
36
|
+
|
|
37
|
+
:raises: SemanticError if a template with the same name and version
|
|
38
|
+
is already registered.
|
|
39
|
+
"""
|
|
40
|
+
template_name = template.name
|
|
41
|
+
template_version = template.version
|
|
42
|
+
key = (template_name, template_version)
|
|
43
|
+
|
|
44
|
+
is_duplicate = key in self._templates
|
|
45
|
+
|
|
46
|
+
if is_duplicate is True:
|
|
47
|
+
errmsg = "PromptTemplate {!r} version {!r} is already registered.".format(
|
|
48
|
+
template_name, template_version
|
|
49
|
+
)
|
|
50
|
+
raise SemanticError(errmsg)
|
|
51
|
+
|
|
52
|
+
self._templates[key] = template
|
|
53
|
+
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
def lookup(self, name: str, version: str = None) -> PromptTemplate:
|
|
57
|
+
"""
|
|
58
|
+
Looks up a PromptTemplate by name and optional version.
|
|
59
|
+
|
|
60
|
+
When version is provided, returns the exact match or None.
|
|
61
|
+
When version is not provided, returns the latest version
|
|
62
|
+
for the given name based on lexicographic sort of version
|
|
63
|
+
strings, or None if no templates match the name.
|
|
64
|
+
|
|
65
|
+
:param name: The template name.
|
|
66
|
+
:param version: Optional version string.
|
|
67
|
+
|
|
68
|
+
:returns: The PromptTemplate instance, or None if not found.
|
|
69
|
+
"""
|
|
70
|
+
if version is not None:
|
|
71
|
+
key = (name, version)
|
|
72
|
+
rtnval = self._templates.get(key, None)
|
|
73
|
+
return rtnval
|
|
74
|
+
|
|
75
|
+
matching = [(k, t) for k, t in self._templates.items() if k[0] == name]
|
|
76
|
+
|
|
77
|
+
if len(matching) == 0:
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
matching.sort(key=lambda item: item[0][1])
|
|
81
|
+
rtnval = matching[-1][1]
|
|
82
|
+
return rtnval
|
|
83
|
+
|
|
84
|
+
def list_all(self) -> list:
|
|
85
|
+
"""
|
|
86
|
+
Returns all registered PromptTemplate instances.
|
|
87
|
+
|
|
88
|
+
:returns: A list of PromptTemplate objects.
|
|
89
|
+
"""
|
|
90
|
+
rtnval = list(self._templates.values())
|
|
91
|
+
return rtnval
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def count(self) -> int:
|
|
95
|
+
"""
|
|
96
|
+
The number of currently registered templates.
|
|
97
|
+
|
|
98
|
+
:returns: The count of registered templates.
|
|
99
|
+
"""
|
|
100
|
+
rtnval = len(self._templates)
|
|
101
|
+
return rtnval
|
|
102
|
+
|
|
103
|
+
def load_from_json_file(self, file_path: str) -> None:
|
|
104
|
+
"""
|
|
105
|
+
Loads PromptTemplate definitions from a JSON file.
|
|
106
|
+
|
|
107
|
+
The JSON file is expected to contain a top-level object with
|
|
108
|
+
a "templates" key whose value is a list of template definition
|
|
109
|
+
dictionaries, each compatible with PromptTemplate.from_dict.
|
|
110
|
+
|
|
111
|
+
:param file_path: Path to the JSON file.
|
|
112
|
+
"""
|
|
113
|
+
with open(file_path, "r") as fhandle:
|
|
114
|
+
data = json.load(fhandle)
|
|
115
|
+
|
|
116
|
+
templates = data.get("templates", [])
|
|
117
|
+
|
|
118
|
+
for tdata in templates:
|
|
119
|
+
template = PromptTemplate.from_dict(tdata)
|
|
120
|
+
self.register(template)
|
|
121
|
+
|
|
122
|
+
return
|
|
123
|
+
|
|
124
|
+
def load_from_yaml_file(self, file_path: str) -> None:
|
|
125
|
+
"""
|
|
126
|
+
Loads PromptTemplate definitions from a YAML file.
|
|
127
|
+
|
|
128
|
+
The YAML file is expected to contain a top-level mapping with
|
|
129
|
+
a "templates" key whose value is a list of template definition
|
|
130
|
+
mappings, each compatible with PromptTemplate.from_dict.
|
|
131
|
+
|
|
132
|
+
:param file_path: Path to the YAML file.
|
|
133
|
+
"""
|
|
134
|
+
import yaml
|
|
135
|
+
|
|
136
|
+
with open(file_path, "r") as fhandle:
|
|
137
|
+
data = yaml.safe_load(fhandle)
|
|
138
|
+
|
|
139
|
+
templates = data.get("templates", [])
|
|
140
|
+
|
|
141
|
+
for tdata in templates:
|
|
142
|
+
template = PromptTemplate.from_dict(tdata)
|
|
143
|
+
self.register(template)
|
|
144
|
+
|
|
145
|
+
return
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the ProtobufMessageSerializer class for serializing
|
|
3
|
+
Protobuf message objects to Protobuf-encoded bytes and
|
|
4
|
+
deserializing Protobuf-encoded bytes to Protobuf message objects.
|
|
5
|
+
|
|
6
|
+
Uses the google.protobuf library for encoding and decoding. The
|
|
7
|
+
protobuf package must be installed for this serializer to function.
|
|
8
|
+
If google.protobuf is not available, importing this module will
|
|
9
|
+
succeed but instantiation will raise an ImportError.
|
|
10
|
+
|
|
11
|
+
The serializer is constructed with a message_type parameter that
|
|
12
|
+
specifies the expected Protobuf message class for deserialization.
|
|
13
|
+
|
|
14
|
+
.. module:: ccai.agentkit.protobufmessageserializer
|
|
15
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
16
|
+
:synopsis: Protobuf serialization implementation for message payloads.
|
|
17
|
+
|
|
18
|
+
Requirements: 9.7, 9.11
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from ccai.agentkit.messageserializer import MessageSerializer
|
|
22
|
+
from ccai.agentkit.messagingerrors import MessageSerializationError
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
from google.protobuf import message as protobuf_message
|
|
26
|
+
_PROTOBUF_AVAILABLE = True
|
|
27
|
+
except ImportError:
|
|
28
|
+
_PROTOBUF_AVAILABLE = False
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ProtobufMessageSerializer(MessageSerializer):
|
|
32
|
+
"""
|
|
33
|
+
Serializes Protobuf message objects to Protobuf-encoded bytes
|
|
34
|
+
and deserializes Protobuf-encoded bytes to Protobuf message
|
|
35
|
+
objects.
|
|
36
|
+
|
|
37
|
+
Requires the google-protobuf package to be installed. Raises
|
|
38
|
+
ImportError at construction time if google.protobuf is not
|
|
39
|
+
available.
|
|
40
|
+
|
|
41
|
+
:param message_type: The expected Protobuf message class used
|
|
42
|
+
for deserialization. Must be a subclass of
|
|
43
|
+
google.protobuf.message.Message.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def __init__(self, message_type=None) -> None:
|
|
47
|
+
if _PROTOBUF_AVAILABLE is not True:
|
|
48
|
+
raise ImportError(
|
|
49
|
+
"The 'protobuf' package is required for ProtobufMessageSerializer. "
|
|
50
|
+
"Install it with: pip install protobuf"
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
super().__init__(format_name="protobuf", content_type="application/x-protobuf")
|
|
54
|
+
self._message_type = message_type
|
|
55
|
+
return
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def message_type(self):
|
|
59
|
+
"""
|
|
60
|
+
The expected Protobuf message class for deserialization.
|
|
61
|
+
|
|
62
|
+
:returns: The Protobuf message class, or None if not set.
|
|
63
|
+
"""
|
|
64
|
+
return self._message_type
|
|
65
|
+
|
|
66
|
+
def serialize(self, obj: object) -> bytes:
|
|
67
|
+
"""
|
|
68
|
+
Serializes a Protobuf message object to bytes.
|
|
69
|
+
|
|
70
|
+
:param obj: The Protobuf message object to serialize. Must
|
|
71
|
+
be an instance of google.protobuf.message.Message.
|
|
72
|
+
|
|
73
|
+
:returns: The Protobuf-encoded bytes.
|
|
74
|
+
:raises MessageSerializationError: If the object is not a
|
|
75
|
+
valid Protobuf message or cannot be serialized.
|
|
76
|
+
"""
|
|
77
|
+
is_protobuf_message = isinstance(obj, protobuf_message.Message)
|
|
78
|
+
|
|
79
|
+
if is_protobuf_message is not True:
|
|
80
|
+
object_type = type(obj).__name__
|
|
81
|
+
detail = "Object of type '{}' is not a Protobuf message".format(
|
|
82
|
+
object_type
|
|
83
|
+
)
|
|
84
|
+
raise MessageSerializationError(
|
|
85
|
+
format_name="protobuf",
|
|
86
|
+
direction="serialize",
|
|
87
|
+
detail=detail
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
try:
|
|
91
|
+
rtnval = obj.SerializeToString()
|
|
92
|
+
except Exception as ex:
|
|
93
|
+
object_type = type(obj).__name__
|
|
94
|
+
detail = "Failed to serialize Protobuf message of type '{}': {}".format(
|
|
95
|
+
object_type, ex
|
|
96
|
+
)
|
|
97
|
+
raise MessageSerializationError(
|
|
98
|
+
format_name="protobuf",
|
|
99
|
+
direction="serialize",
|
|
100
|
+
detail=detail
|
|
101
|
+
) from ex
|
|
102
|
+
|
|
103
|
+
return rtnval
|
|
104
|
+
|
|
105
|
+
def deserialize(self, data: bytes) -> object:
|
|
106
|
+
"""
|
|
107
|
+
Deserializes Protobuf-encoded bytes to a Protobuf message
|
|
108
|
+
object.
|
|
109
|
+
|
|
110
|
+
Uses the message_type provided at construction time to
|
|
111
|
+
create and populate the message instance.
|
|
112
|
+
|
|
113
|
+
:param data: The Protobuf-encoded bytes to deserialize.
|
|
114
|
+
|
|
115
|
+
:returns: The deserialized Protobuf message object.
|
|
116
|
+
:raises MessageSerializationError: If the bytes are not
|
|
117
|
+
valid Protobuf for the expected message type. The error
|
|
118
|
+
detail includes the expected message type.
|
|
119
|
+
"""
|
|
120
|
+
if self._message_type is None:
|
|
121
|
+
detail = "No message_type configured for deserialization"
|
|
122
|
+
raise MessageSerializationError(
|
|
123
|
+
format_name="protobuf",
|
|
124
|
+
direction="deserialize",
|
|
125
|
+
detail=detail
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
expected_type_name = self._message_type.DESCRIPTOR.full_name
|
|
129
|
+
|
|
130
|
+
try:
|
|
131
|
+
message_instance = self._message_type()
|
|
132
|
+
message_instance.ParseFromString(data)
|
|
133
|
+
rtnval = message_instance
|
|
134
|
+
except Exception as ex:
|
|
135
|
+
detail = "Invalid Protobuf input for expected message type '{}': {}".format(
|
|
136
|
+
expected_type_name, ex
|
|
137
|
+
)
|
|
138
|
+
raise MessageSerializationError(
|
|
139
|
+
format_name="protobuf",
|
|
140
|
+
direction="deserialize",
|
|
141
|
+
detail=detail
|
|
142
|
+
) from ex
|
|
143
|
+
|
|
144
|
+
return rtnval
|