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.
Files changed (230) hide show
  1. ccai/agentkit/__init__.py +304 -0
  2. ccai/agentkit/addressresolver.py +177 -0
  3. ccai/agentkit/agent.py +2945 -0
  4. ccai/agentkit/agentaddress.py +187 -0
  5. ccai/agentkit/agentconfiguration.py +581 -0
  6. ccai/agentkit/agentidentity.py +152 -0
  7. ccai/agentkit/agentregistry.py +221 -0
  8. ccai/agentkit/agentstatemachine.py +134 -0
  9. ccai/agentkit/alternateproviderstrategy.py +36 -0
  10. ccai/agentkit/approvalgate.py +238 -0
  11. ccai/agentkit/approvalrequest.py +250 -0
  12. ccai/agentkit/asyncagent.py +187 -0
  13. ccai/agentkit/breakpoint.py +150 -0
  14. ccai/agentkit/builtinagentmetrics.py +114 -0
  15. ccai/agentkit/builtins/__init__.py +11 -0
  16. ccai/agentkit/builtins/filereadtool.py +90 -0
  17. ccai/agentkit/builtins/filewritetool.py +93 -0
  18. ccai/agentkit/builtins/httprequesttool.py +169 -0
  19. ccai/agentkit/builtins/shellcommandtool.py +190 -0
  20. ccai/agentkit/cachedresultstrategy.py +43 -0
  21. ccai/agentkit/checkpointmanager.py +219 -0
  22. ccai/agentkit/circuitbreaker.py +230 -0
  23. ccai/agentkit/circuitbreakerconfiguration.py +158 -0
  24. ccai/agentkit/circuitbreakerevent.py +158 -0
  25. ccai/agentkit/circuitbreakerhealthcheck.py +32 -0
  26. ccai/agentkit/circuitbreakerregistry.py +116 -0
  27. ccai/agentkit/circuitbreakerstate.py +44 -0
  28. ccai/agentkit/clearcacheaction.py +46 -0
  29. ccai/agentkit/concurrencybridge.py +143 -0
  30. ccai/agentkit/concurrencymodel.py +19 -0
  31. ccai/agentkit/conditionevaluator.py +594 -0
  32. ccai/agentkit/configurationchangeevent.py +179 -0
  33. ccai/agentkit/configurationerrors.py +152 -0
  34. ccai/agentkit/configurationlayer.py +160 -0
  35. ccai/agentkit/configurationlayersource.py +46 -0
  36. ccai/agentkit/configurationmanager.py +680 -0
  37. ccai/agentkit/configurationschema.py +224 -0
  38. ccai/agentkit/configurationschemafield.py +217 -0
  39. ccai/agentkit/configurationserializer.py +163 -0
  40. ccai/agentkit/configurationvalidationresult.py +118 -0
  41. ccai/agentkit/contextsummarizer.py +170 -0
  42. ccai/agentkit/contextwindow.py +264 -0
  43. ccai/agentkit/contextwindowstrategy.py +57 -0
  44. ccai/agentkit/correlationcontext.py +156 -0
  45. ccai/agentkit/costestimate.py +139 -0
  46. ccai/agentkit/costtracker.py +121 -0
  47. ccai/agentkit/deadletterentry.py +134 -0
  48. ccai/agentkit/deadletterquery.py +119 -0
  49. ccai/agentkit/deadletterqueue.py +123 -0
  50. ccai/agentkit/deadletterstore.py +86 -0
  51. ccai/agentkit/debuginspector.py +211 -0
  52. ccai/agentkit/debugsession.py +223 -0
  53. ccai/agentkit/defaultvaluestrategy.py +43 -0
  54. ccai/agentkit/degradationchangeevent.py +120 -0
  55. ccai/agentkit/degradationlevel.py +44 -0
  56. ccai/agentkit/degradationmanager.py +212 -0
  57. ccai/agentkit/degradationpolicy.py +130 -0
  58. ccai/agentkit/degradationstrategy.py +66 -0
  59. ccai/agentkit/embeddingprovider.py +106 -0
  60. ccai/agentkit/errors.py +95 -0
  61. ccai/agentkit/executionrecord.py +213 -0
  62. ccai/agentkit/executionrecorder.py +180 -0
  63. ccai/agentkit/executionrecording.py +230 -0
  64. ccai/agentkit/executionreplayer.py +187 -0
  65. ccai/agentkit/expirationpolicy.py +116 -0
  66. ccai/agentkit/fallbackchain.py +82 -0
  67. ccai/agentkit/featureflag.py +154 -0
  68. ccai/agentkit/featureflagevaluator.py +109 -0
  69. ccai/agentkit/featureflagregistry.py +258 -0
  70. ccai/agentkit/filedeadletterstore.py +112 -0
  71. ccai/agentkit/filememorystore.py +291 -0
  72. ccai/agentkit/healthcheck.py +46 -0
  73. ccai/agentkit/healthcheckregistry.py +51 -0
  74. ccai/agentkit/healthmonitor.py +258 -0
  75. ccai/agentkit/healthstatus.py +105 -0
  76. ccai/agentkit/hotreloadmonitor.py +214 -0
  77. ccai/agentkit/hotreloadpolicy.py +160 -0
  78. ccai/agentkit/inmemorydeadletterstore.py +61 -0
  79. ccai/agentkit/inmemorystore.py +173 -0
  80. ccai/agentkit/jsonmessageserializer.py +89 -0
  81. ccai/agentkit/jsonoutputparser.py +50 -0
  82. ccai/agentkit/lifecyclestate.py +22 -0
  83. ccai/agentkit/llmconversation.py +153 -0
  84. ccai/agentkit/llmerrors.py +193 -0
  85. ccai/agentkit/llmmessage.py +158 -0
  86. ccai/agentkit/llmprovider.py +158 -0
  87. ccai/agentkit/llmproviderregistry.py +200 -0
  88. ccai/agentkit/llmresponse.py +190 -0
  89. ccai/agentkit/llmstreamchunk.py +65 -0
  90. ccai/agentkit/llmstreamhandler.py +48 -0
  91. ccai/agentkit/llmtoolbridge.py +154 -0
  92. ccai/agentkit/logexporter.py +316 -0
  93. ccai/agentkit/logformatter.py +169 -0
  94. ccai/agentkit/memoryconfiguration.py +243 -0
  95. ccai/agentkit/memoryentry.py +254 -0
  96. ccai/agentkit/memoryerrors.py +135 -0
  97. ccai/agentkit/memoryindex.py +213 -0
  98. ccai/agentkit/memoryquery.py +248 -0
  99. ccai/agentkit/memoryqueryresult.py +86 -0
  100. ccai/agentkit/memoryscope.py +54 -0
  101. ccai/agentkit/memorystore.py +260 -0
  102. ccai/agentkit/memorystorehealthcheck.py +33 -0
  103. ccai/agentkit/memorystoreregistry.py +106 -0
  104. ccai/agentkit/message.py +310 -0
  105. ccai/agentkit/messageenvelope.py +144 -0
  106. ccai/agentkit/messagepattern.py +50 -0
  107. ccai/agentkit/messagerouter.py +290 -0
  108. ccai/agentkit/messageserializer.py +106 -0
  109. ccai/agentkit/messageserializerregistry.py +163 -0
  110. ccai/agentkit/messagingconfiguration.py +397 -0
  111. ccai/agentkit/messagingerrors.py +338 -0
  112. ccai/agentkit/metricdefinition.py +164 -0
  113. ccai/agentkit/metricmeasurement.py +178 -0
  114. ccai/agentkit/metricscollector.py +258 -0
  115. ccai/agentkit/metricsexporter.py +303 -0
  116. ccai/agentkit/metricsregistry.py +130 -0
  117. ccai/agentkit/metricssnapshot.py +155 -0
  118. ccai/agentkit/metrictype.py +51 -0
  119. ccai/agentkit/msgpackmessageserializer.py +97 -0
  120. ccai/agentkit/observabilityconfiguration.py +546 -0
  121. ccai/agentkit/observabilityerror.py +67 -0
  122. ccai/agentkit/observabilitypipeline.py +252 -0
  123. ccai/agentkit/outputparser.py +82 -0
  124. ccai/agentkit/outputschema.py +142 -0
  125. ccai/agentkit/parallelbranch.py +87 -0
  126. ccai/agentkit/platform/__init__.py +43 -0
  127. ccai/agentkit/platform/adapters/__init__.py +18 -0
  128. ccai/agentkit/platform/adapters/awsplatformadapter.py +1272 -0
  129. ccai/agentkit/platform/adapters/azureplatformadapter.py +1227 -0
  130. ccai/agentkit/platform/adapters/gcpplatformadapter.py +1257 -0
  131. ccai/agentkit/platform/adapters/localplatformadapter.py +956 -0
  132. ccai/agentkit/platform/adapters/platformadapter.py +139 -0
  133. ccai/agentkit/platform/deployment/__init__.py +15 -0
  134. ccai/agentkit/platform/deployment/deploymentdescriptor.py +234 -0
  135. ccai/agentkit/platform/deployment/deploymentdescriptorparser.py +178 -0
  136. ccai/agentkit/platform/deployment/deploymentdescriptorprinter.py +97 -0
  137. ccai/agentkit/platform/errors.py +224 -0
  138. ccai/agentkit/platform/platformconfiguration.py +412 -0
  139. ccai/agentkit/platform/platformdetector.py +125 -0
  140. ccai/agentkit/platform/platformruntime.py +331 -0
  141. ccai/agentkit/platform/platformserviceregistry.py +149 -0
  142. ccai/agentkit/platform/platformtype.py +60 -0
  143. ccai/agentkit/platform/services/__init__.py +19 -0
  144. ccai/agentkit/platform/services/computeservice.py +112 -0
  145. ccai/agentkit/platform/services/invocationresult.py +173 -0
  146. ccai/agentkit/platform/services/messagingservice.py +135 -0
  147. ccai/agentkit/platform/services/platformservice.py +111 -0
  148. ccai/agentkit/platform/services/queuemessage.py +193 -0
  149. ccai/agentkit/platform/services/storageobject.py +195 -0
  150. ccai/agentkit/platform/services/storageservice.py +159 -0
  151. ccai/agentkit/pluginconfiguration.py +241 -0
  152. ccai/agentkit/plugindependency.py +129 -0
  153. ccai/agentkit/plugindescriptor.py +225 -0
  154. ccai/agentkit/pluginlifecyclehook.py +41 -0
  155. ccai/agentkit/pluginmanager.py +360 -0
  156. ccai/agentkit/pluginstatus.py +47 -0
  157. ccai/agentkit/pooledagent.py +116 -0
  158. ccai/agentkit/prompttemplate.py +221 -0
  159. ccai/agentkit/prompttemplateregistry.py +145 -0
  160. ccai/agentkit/protobufmessageserializer.py +144 -0
  161. ccai/agentkit/pubsubbroker.py +311 -0
  162. ccai/agentkit/py.typed +0 -0
  163. ccai/agentkit/reconnecttransportaction.py +47 -0
  164. ccai/agentkit/regexoutputparser.py +68 -0
  165. ccai/agentkit/requestresponsehandler.py +274 -0
  166. ccai/agentkit/resetcircuitbreakeraction.py +48 -0
  167. ccai/agentkit/resilienceconfiguration.py +191 -0
  168. ccai/agentkit/resilienceerrors.py +103 -0
  169. ccai/agentkit/resiliencepipeline.py +201 -0
  170. ccai/agentkit/retryattempt.py +158 -0
  171. ccai/agentkit/retryexecutor.py +157 -0
  172. ccai/agentkit/retrypolicy.py +195 -0
  173. ccai/agentkit/retryresult.py +186 -0
  174. ccai/agentkit/selfhealingaction.py +42 -0
  175. ccai/agentkit/selfhealingpolicy.py +67 -0
  176. ccai/agentkit/selfhealingresult.py +130 -0
  177. ccai/agentkit/stepexecutionstatus.py +52 -0
  178. ccai/agentkit/stepresult.py +198 -0
  179. ccai/agentkit/steps/__init__.py +9 -0
  180. ccai/agentkit/steps/agentstep.py +247 -0
  181. ccai/agentkit/steps/approvalstep.py +276 -0
  182. ccai/agentkit/steps/llmstep.py +403 -0
  183. ccai/agentkit/steps/subworkflowstep.py +270 -0
  184. ccai/agentkit/steps/toolstep.py +265 -0
  185. ccai/agentkit/structuredlogentry.py +222 -0
  186. ccai/agentkit/structuredlogger.py +189 -0
  187. ccai/agentkit/subscription.py +141 -0
  188. ccai/agentkit/threadedagent.py +94 -0
  189. ccai/agentkit/tokenusage.py +136 -0
  190. ccai/agentkit/tool.py +203 -0
  191. ccai/agentkit/toolchain.py +268 -0
  192. ccai/agentkit/toolchainexecutor.py +134 -0
  193. ccai/agentkit/toolchainresult.py +166 -0
  194. ccai/agentkit/tooldescriptor.py +203 -0
  195. ccai/agentkit/toolerrors.py +160 -0
  196. ccai/agentkit/toolexecutor.py +168 -0
  197. ccai/agentkit/toolfactory.py +60 -0
  198. ccai/agentkit/toolregistry.py +184 -0
  199. ccai/agentkit/toolresult.py +142 -0
  200. ccai/agentkit/toolschema.py +197 -0
  201. ccai/agentkit/tracecontext.py +294 -0
  202. ccai/agentkit/traceevent.py +166 -0
  203. ccai/agentkit/traceexporter.py +305 -0
  204. ccai/agentkit/tracer.py +234 -0
  205. ccai/agentkit/tracespan.py +349 -0
  206. ccai/agentkit/transitionrecord.py +84 -0
  207. ccai/agentkit/transport.py +334 -0
  208. ccai/agentkit/transportconfiguration.py +436 -0
  209. ccai/agentkit/transporthealthcheck.py +36 -0
  210. ccai/agentkit/transportregistry.py +224 -0
  211. ccai/agentkit/transports/__init__.py +28 -0
  212. ccai/agentkit/transports/grpctransport.py +410 -0
  213. ccai/agentkit/transports/httptransport.py +423 -0
  214. ccai/agentkit/transports/messagequeuetransport.py +489 -0
  215. ccai/agentkit/transports/websockettransport.py +473 -0
  216. ccai/agentkit/workflowcheckpoint.py +208 -0
  217. ccai/agentkit/workflowconfiguration.py +295 -0
  218. ccai/agentkit/workflowcontext.py +249 -0
  219. ccai/agentkit/workflowdefinition.py +790 -0
  220. ccai/agentkit/workflowedge.py +128 -0
  221. ccai/agentkit/workflowerrors.py +295 -0
  222. ccai/agentkit/workflowexecutionstatus.py +48 -0
  223. ccai/agentkit/workflowexecutor.py +1121 -0
  224. ccai/agentkit/workflowregistry.py +185 -0
  225. ccai/agentkit/workflowresult.py +250 -0
  226. ccai/agentkit/workflowstep.py +195 -0
  227. codecraftai_agent_kit-1.3.0.dist-info/METADATA +99 -0
  228. codecraftai_agent_kit-1.3.0.dist-info/RECORD +230 -0
  229. codecraftai_agent_kit-1.3.0.dist-info/WHEEL +4 -0
  230. codecraftai_agent_kit-1.3.0.dist-info/licenses/LICENSE.md +204 -0
@@ -0,0 +1,158 @@
1
+ """
2
+ Defines the LLMMessage class for immutable conversation messages.
3
+
4
+ LLMMessage encapsulates a role, content string, and optional metadata
5
+ dictionary for a single message in an LLM conversation. The role must
6
+ be one of the valid roles: system, user, assistant, or tool.
7
+ """
8
+
9
+ import json
10
+
11
+ from ccai.agentkit.errors import SemanticError
12
+
13
+
14
+ class LLMMessage:
15
+ """
16
+ Immutable message in a conversation with a role, content, and optional metadata.
17
+
18
+ Provides read-only access to the role, content, and metadata fields.
19
+ Validates that the role is one of the valid roles at construction time.
20
+ Supports serialization to and from JSON and dictionary representations.
21
+
22
+ :param role: The message role (system, user, assistant, tool).
23
+ :param content: The message content string.
24
+ :param metadata: Optional metadata dictionary (e.g. tool call info).
25
+ """
26
+
27
+ VALID_ROLES = {"system", "user", "assistant", "tool"}
28
+
29
+ def __init__(self, role: str, content: str, metadata: dict = None) -> None:
30
+ if role not in self.VALID_ROLES:
31
+ errmsg = "Invalid message role {!r}. Valid roles are: {}".format(
32
+ role, ", ".join(sorted(self.VALID_ROLES))
33
+ )
34
+ raise SemanticError(errmsg)
35
+
36
+ self._role = role
37
+ self._content = content
38
+ self._metadata = dict(metadata) if metadata is not None else {}
39
+ return
40
+
41
+ @property
42
+ def role(self) -> str:
43
+ """
44
+ The message role string.
45
+
46
+ :returns: The role as a string.
47
+ """
48
+ rtnval = self._role
49
+ return rtnval
50
+
51
+ @property
52
+ def content(self) -> str:
53
+ """
54
+ The message content string.
55
+
56
+ :returns: The content as a string.
57
+ """
58
+ rtnval = self._content
59
+ return rtnval
60
+
61
+ @property
62
+ def metadata(self) -> dict:
63
+ """
64
+ A copy of the metadata dictionary.
65
+
66
+ :returns: A dictionary copy of the metadata.
67
+ """
68
+ rtnval = dict(self._metadata)
69
+ return rtnval
70
+
71
+ def as_dict(self) -> dict:
72
+ """
73
+ Returns a dictionary representation of the message.
74
+
75
+ :returns: A dictionary with role, content, and metadata keys.
76
+ """
77
+ rtnval = {
78
+ "role": self._role,
79
+ "content": self._content,
80
+ "metadata": dict(self._metadata)
81
+ }
82
+ return rtnval
83
+
84
+ def to_json(self) -> str:
85
+ """
86
+ Returns a JSON string representation of the message.
87
+
88
+ :returns: A JSON string.
89
+ """
90
+ rtnval = json.dumps(self.as_dict())
91
+ return rtnval
92
+
93
+ @classmethod
94
+ def from_dict(cls, data: dict) -> "LLMMessage":
95
+ """
96
+ Constructs an LLMMessage from a dictionary.
97
+
98
+ :param data: A dictionary with role, content, and optional metadata keys.
99
+
100
+ :returns: An LLMMessage instance.
101
+ """
102
+ rtnval = cls(
103
+ role=data["role"],
104
+ content=data["content"],
105
+ metadata=data.get("metadata")
106
+ )
107
+ return rtnval
108
+
109
+ @classmethod
110
+ def from_json(cls, json_str: str) -> "LLMMessage":
111
+ """
112
+ Deserializes a JSON string into an LLMMessage.
113
+
114
+ :param json_str: A JSON string.
115
+
116
+ :returns: An LLMMessage instance.
117
+ """
118
+ data = json.loads(json_str)
119
+ rtnval = cls.from_dict(data)
120
+ return rtnval
121
+
122
+ def __repr__(self) -> str:
123
+ """
124
+ Returns a detailed string representation including the class name,
125
+ role, and a truncated content preview at 50 characters.
126
+
127
+ :returns: A repr string for debugging.
128
+ """
129
+ content_preview = self._content[:50] + "..." if len(self._content) > 50 else self._content
130
+ rtnval = "{}(role={!r}, content={!r})".format(
131
+ self.__class__.__name__, self._role, content_preview
132
+ )
133
+ return rtnval
134
+
135
+ def __str__(self) -> str:
136
+ """
137
+ Returns a human-readable summary including the role and content.
138
+
139
+ :returns: A human-readable string.
140
+ """
141
+ rtnval = "[{}] {}".format(self._role, self._content)
142
+ return rtnval
143
+
144
+ def __eq__(self, other) -> bool:
145
+ """
146
+ Checks equality based on role, content, and metadata fields.
147
+
148
+ :param other: The object to compare against.
149
+
150
+ :returns: True if all fields are equal, False otherwise.
151
+ """
152
+ if not isinstance(other, LLMMessage):
153
+ return False
154
+
155
+ rtnval = (self._role == other._role and
156
+ self._content == other._content and
157
+ self._metadata == other._metadata)
158
+ return rtnval
@@ -0,0 +1,158 @@
1
+ """
2
+ Defines the LLMProvider abstract base class for Large Language Model providers.
3
+
4
+ LLMProvider establishes the common interface that all LLM provider
5
+ implementations must follow. Subclasses implement the complete and
6
+ complete_stream methods for a specific LLM service. The base class
7
+ manages provider identity, configuration, credential references, and
8
+ tags for categorization and discovery.
9
+ """
10
+
11
+ import abc
12
+
13
+ from ccai.agentkit.errors import SemanticError
14
+
15
+
16
+ class LLMProvider(abc.ABC):
17
+ """
18
+ Abstract base class for Large Language Model providers.
19
+
20
+ Subclasses implement the complete and complete_stream methods
21
+ for a specific LLM service. The base class validates that a
22
+ provider name and model name are supplied at construction time,
23
+ and exposes read-only properties for all provider metadata.
24
+
25
+ :param provider_name: The name of the LLM provider (e.g. "openai").
26
+ :param model_name: The name of the model (e.g. "gpt-4").
27
+ :param config: Optional provider configuration dictionary.
28
+ :param credential_names: Optional list of CredentialReference names.
29
+ :param tags: Optional dictionary of tags for categorization.
30
+ """
31
+
32
+ def __init__(self, provider_name: str, model_name: str,
33
+ config: dict = None, credential_names: list = None,
34
+ tags: dict = None) -> None:
35
+ if provider_name is None or len(str(provider_name).strip()) == 0:
36
+ errmsg = "A provider name is required when constructing an LLMProvider."
37
+ raise SemanticError(errmsg)
38
+
39
+ if model_name is None or len(str(model_name).strip()) == 0:
40
+ errmsg = "A model name is required when constructing an LLMProvider."
41
+ raise SemanticError(errmsg)
42
+
43
+ self._provider_name = provider_name
44
+ self._model_name = model_name
45
+ self._config = dict(config) if config is not None else {}
46
+ self._credential_names = list(credential_names) if credential_names is not None else []
47
+ self._tags = dict(tags) if tags is not None else {}
48
+ return
49
+
50
+ @property
51
+ def provider_name(self) -> str:
52
+ """
53
+ The name of the LLM provider.
54
+
55
+ :returns: The provider name as a string.
56
+ """
57
+ rtnval = self._provider_name
58
+ return rtnval
59
+
60
+ @property
61
+ def model_name(self) -> str:
62
+ """
63
+ The name of the model.
64
+
65
+ :returns: The model name as a string.
66
+ """
67
+ rtnval = self._model_name
68
+ return rtnval
69
+
70
+ @property
71
+ def config(self) -> dict:
72
+ """
73
+ A copy of the provider configuration dictionary.
74
+
75
+ :returns: A dictionary copy of the configuration.
76
+ """
77
+ rtnval = dict(self._config)
78
+ return rtnval
79
+
80
+ @property
81
+ def credential_names(self) -> list:
82
+ """
83
+ A copy of the list of CredentialReference names.
84
+
85
+ :returns: A list copy of the credential names.
86
+ """
87
+ rtnval = list(self._credential_names)
88
+ return rtnval
89
+
90
+ @property
91
+ def tags(self) -> dict:
92
+ """
93
+ A copy of the tags dictionary.
94
+
95
+ :returns: A dictionary copy of the tags.
96
+ """
97
+ rtnval = dict(self._tags)
98
+ return rtnval
99
+
100
+ @abc.abstractmethod
101
+ def complete(self, conversation, **kwargs):
102
+ """
103
+ Executes a synchronous completion request.
104
+
105
+ :param conversation: The conversation history as an LLMConversation.
106
+
107
+ :returns: An LLMResponse with the completion result.
108
+
109
+ :raises: LLMRequestError on provider-side failure.
110
+ """
111
+
112
+ @abc.abstractmethod
113
+ def complete_stream(self, conversation, handler, **kwargs):
114
+ """
115
+ Executes a streaming completion request.
116
+
117
+ :param conversation: The conversation history as an LLMConversation.
118
+ :param handler: The LLMStreamHandler to receive chunks.
119
+
120
+ :returns: The final assembled LLMResponse.
121
+
122
+ :raises: LLMRequestError on provider-side failure.
123
+ """
124
+
125
+ def get_model_info(self) -> dict:
126
+ """
127
+ Returns provider and model metadata.
128
+
129
+ :returns: A dictionary with provider_name, model_name, and config keys.
130
+ """
131
+ rtnval = {
132
+ "provider_name": self._provider_name,
133
+ "model_name": self._model_name,
134
+ "config": dict(self._config)
135
+ }
136
+ return rtnval
137
+
138
+ def __repr__(self) -> str:
139
+ """
140
+ Returns a detailed string representation including the class name,
141
+ provider name, and model name.
142
+
143
+ :returns: A repr string for debugging.
144
+ """
145
+ rtnval = "{}(provider_name={!r}, model_name={!r})".format(
146
+ self.__class__.__name__, self._provider_name, self._model_name
147
+ )
148
+ return rtnval
149
+
150
+ def __str__(self) -> str:
151
+ """
152
+ Returns a human-readable summary including the provider name
153
+ and model name.
154
+
155
+ :returns: A human-readable string.
156
+ """
157
+ rtnval = "LLMProvider: {} / {}".format(self._provider_name, self._model_name)
158
+ return rtnval
@@ -0,0 +1,200 @@
1
+ """
2
+ Defines the LLMProviderRegistry class for tracking available LLMProvider
3
+ instances within an Agent in the codecraftai-agent-kit framework.
4
+
5
+ LLMProviderRegistry provides registration, deregistration, lookup by
6
+ provider name and model name, lookup by provider name only, lookup by
7
+ tags, enumeration, and count operations for LLMProvider instances.
8
+ Each Agent maintains its own LLMProviderRegistry instance.
9
+ """
10
+
11
+ from ccai.agentkit.errors import SemanticError
12
+ from ccai.agentkit.llmprovider import LLMProvider
13
+
14
+
15
+ class LLMProviderRegistry:
16
+ """
17
+ Registry tracking available LLMProvider instances for an Agent.
18
+
19
+ Provides lookup by provider name + model name, by provider name only,
20
+ by tags, and enumeration. Uses a composite key of (provider_name,
21
+ model_name) for unique identification of registered providers.
22
+ """
23
+
24
+ def __init__(self) -> None:
25
+ self._providers: dict = {}
26
+ return
27
+
28
+ def register(self, provider: LLMProvider) -> None:
29
+ """
30
+ Registers an LLMProvider using provider_name + model_name as
31
+ a composite key.
32
+
33
+ :param provider: The LLMProvider instance to register.
34
+
35
+ :raises: SemanticError if a provider with the same name+model
36
+ is already registered.
37
+ """
38
+ provider_name = provider.provider_name
39
+ model_name = provider.model_name
40
+ key = (provider_name, model_name)
41
+
42
+ is_duplicate = key in self._providers
43
+
44
+ if is_duplicate is True:
45
+ errmsg = "LLMProvider {!r} model {!r} is already registered.".format(
46
+ provider_name, model_name
47
+ )
48
+ raise SemanticError(errmsg)
49
+
50
+ self._providers[key] = provider
51
+
52
+ return
53
+
54
+ def deregister(self, provider_name: str, model_name: str) -> None:
55
+ """
56
+ Removes an LLMProvider by provider name and model name.
57
+
58
+ If the provider is not found, the operation is a no-op.
59
+
60
+ :param provider_name: The provider name.
61
+ :param model_name: The model name.
62
+ """
63
+ key = (provider_name, model_name)
64
+
65
+ is_present = key in self._providers
66
+
67
+ if is_present is True:
68
+ del self._providers[key]
69
+
70
+ return
71
+
72
+ def lookup(self, provider_name: str, model_name: str) -> LLMProvider:
73
+ """
74
+ Looks up a registered LLMProvider by provider name and model name.
75
+
76
+ :param provider_name: The provider name.
77
+ :param model_name: The model name.
78
+
79
+ :returns: The LLMProvider instance, or None if not found.
80
+ """
81
+ key = (provider_name, model_name)
82
+ rtnval = self._providers.get(key, None)
83
+ return rtnval
84
+
85
+ def lookup_by_provider(self, provider_name: str) -> list:
86
+ """
87
+ Returns all LLMProvider instances for a given provider name.
88
+
89
+ :param provider_name: The provider name.
90
+
91
+ :returns: A list of LLMProvider instances matching the provider name.
92
+ """
93
+ matching_providers = []
94
+
95
+ for key, provider in self._providers.items():
96
+ key_provider_name = key[0]
97
+ is_match = key_provider_name == provider_name
98
+
99
+ if is_match is True:
100
+ matching_providers.append(provider)
101
+
102
+ rtnval = matching_providers
103
+ return rtnval
104
+
105
+ def lookup_by_tags(self, **tags) -> list:
106
+ """
107
+ Returns LLMProvider instances matching all provided tag criteria.
108
+
109
+ Returns providers whose tags dictionary contains all the specified
110
+ key-value pairs.
111
+
112
+ :param tags: Keyword arguments representing tag key-value pairs to match.
113
+
114
+ :returns: A list of matching LLMProvider instances.
115
+ """
116
+ matching_providers = []
117
+
118
+ for provider in self._providers.values():
119
+ provider_tags = provider.tags
120
+ all_match = True
121
+
122
+ for tag_key, tag_value in tags.items():
123
+ has_key = tag_key in provider_tags
124
+
125
+ if has_key is not True:
126
+ all_match = False
127
+ break
128
+
129
+ provider_tag_value = provider_tags[tag_key]
130
+ values_match = provider_tag_value == tag_value
131
+
132
+ if values_match is not True:
133
+ all_match = False
134
+ break
135
+
136
+ if all_match is True:
137
+ matching_providers.append(provider)
138
+
139
+ rtnval = matching_providers
140
+ return rtnval
141
+
142
+ def list_all(self) -> list:
143
+ """
144
+ Returns all registered LLMProvider instances.
145
+
146
+ :returns: A list of all registered LLMProvider instances.
147
+ """
148
+ rtnval = list(self._providers.values())
149
+ return rtnval
150
+
151
+ def has_provider(self, provider_name: str, model_name: str) -> bool:
152
+ """
153
+ Checks whether a provider with the given name and model is registered.
154
+
155
+ :param provider_name: The provider name.
156
+ :param model_name: The model name.
157
+
158
+ :returns: True if registered, False otherwise.
159
+ """
160
+ key = (provider_name, model_name)
161
+ rtnval = key in self._providers
162
+ return rtnval
163
+
164
+ def clear(self) -> None:
165
+ """
166
+ Removes all registered providers from the registry.
167
+ """
168
+ self._providers.clear()
169
+ return
170
+
171
+ @property
172
+ def count(self) -> int:
173
+ """
174
+ The number of currently registered providers.
175
+
176
+ :returns: The count of registered providers.
177
+ """
178
+ rtnval = len(self._providers)
179
+ return rtnval
180
+
181
+ def __repr__(self) -> str:
182
+ """
183
+ Returns a detailed string representation including the class name
184
+ and the count of registered providers.
185
+
186
+ :returns: A repr string for debugging.
187
+ """
188
+ rtnval = "{}(count={})".format(self.__class__.__name__, len(self._providers))
189
+ return rtnval
190
+
191
+ def __str__(self) -> str:
192
+ """
193
+ Returns a human-readable summary listing the registered
194
+ provider name / model name pairs.
195
+
196
+ :returns: A human-readable string.
197
+ """
198
+ pairs = ["{}/{}".format(k[0], k[1]) for k in self._providers.keys()]
199
+ rtnval = "LLMProviderRegistry: [{}]".format(", ".join(pairs))
200
+ return rtnval
@@ -0,0 +1,190 @@
1
+ """
2
+ Defines the LLMResponse class for immutable LLM completion results.
3
+
4
+ LLMResponse encapsulates the response message, token usage, provider name,
5
+ model name, and request duration for a single LLM completion request.
6
+ Supports serialization to and from JSON representations.
7
+ """
8
+
9
+ import json
10
+
11
+ from ccai.agentkit.llmmessage import LLMMessage
12
+ from ccai.agentkit.tokenusage import TokenUsage
13
+
14
+
15
+ class LLMResponse:
16
+ """
17
+ Immutable result of an LLM completion request.
18
+
19
+ Provides read-only access to the response message, token usage,
20
+ provider name, model name, and request duration. Supports
21
+ serialization to dictionary and JSON representations, and
22
+ deserialization from JSON.
23
+
24
+ :param message: The response LLMMessage.
25
+ :param token_usage: The TokenUsage for this request.
26
+ :param provider_name: The provider name string.
27
+ :param model_name: The model name string.
28
+ :param duration: The request duration in seconds.
29
+ """
30
+
31
+ def __init__(self, message: LLMMessage, token_usage: TokenUsage,
32
+ provider_name: str, model_name: str, duration: float) -> None:
33
+ self._message = message
34
+ self._token_usage = token_usage
35
+ self._provider_name = provider_name
36
+ self._model_name = model_name
37
+ self._duration = duration
38
+ return
39
+
40
+ @property
41
+ def message(self) -> LLMMessage:
42
+ """
43
+ The response LLMMessage.
44
+
45
+ :returns: The LLMMessage instance.
46
+ """
47
+ rtnval = self._message
48
+ return rtnval
49
+
50
+ @property
51
+ def token_usage(self) -> TokenUsage:
52
+ """
53
+ The TokenUsage for this request.
54
+
55
+ :returns: The TokenUsage instance.
56
+ """
57
+ rtnval = self._token_usage
58
+ return rtnval
59
+
60
+ @property
61
+ def provider_name(self) -> str:
62
+ """
63
+ The provider name string.
64
+
65
+ :returns: The provider name as a string.
66
+ """
67
+ rtnval = self._provider_name
68
+ return rtnval
69
+
70
+ @property
71
+ def model_name(self) -> str:
72
+ """
73
+ The model name string.
74
+
75
+ :returns: The model name as a string.
76
+ """
77
+ rtnval = self._model_name
78
+ return rtnval
79
+
80
+ @property
81
+ def duration(self) -> float:
82
+ """
83
+ The request duration in seconds.
84
+
85
+ :returns: The duration as a float.
86
+ """
87
+ rtnval = self._duration
88
+ return rtnval
89
+
90
+ def as_dict(self) -> dict:
91
+ """
92
+ Returns a dictionary representation of the response fields.
93
+
94
+ :returns: A dictionary with message, token_usage, provider_name,
95
+ model_name, and duration keys.
96
+ """
97
+ rtnval = {
98
+ "message": self._message.as_dict(),
99
+ "token_usage": self._token_usage.as_dict(),
100
+ "provider_name": self._provider_name,
101
+ "model_name": self._model_name,
102
+ "duration": self._duration
103
+ }
104
+ return rtnval
105
+
106
+ def to_json(self) -> str:
107
+ """
108
+ Returns a JSON string representation of the response fields.
109
+
110
+ :returns: A JSON string.
111
+ """
112
+ rtnval = json.dumps(self.as_dict())
113
+ return rtnval
114
+
115
+ @classmethod
116
+ def from_json(cls, json_str: str) -> "LLMResponse":
117
+ """
118
+ Deserializes a JSON string into an LLMResponse instance.
119
+
120
+ Reconstructs the LLMMessage and TokenUsage from their
121
+ dictionary representations contained in the JSON data.
122
+
123
+ :param json_str: A JSON string.
124
+
125
+ :returns: An LLMResponse instance.
126
+ """
127
+ data = json.loads(json_str)
128
+
129
+ message = LLMMessage.from_dict(data["message"])
130
+
131
+ token_usage = TokenUsage(
132
+ prompt_tokens=data["token_usage"]["prompt_tokens"],
133
+ completion_tokens=data["token_usage"]["completion_tokens"],
134
+ total_tokens=data["token_usage"]["total_tokens"]
135
+ )
136
+
137
+ rtnval = cls(
138
+ message=message,
139
+ token_usage=token_usage,
140
+ provider_name=data["provider_name"],
141
+ model_name=data["model_name"],
142
+ duration=data["duration"]
143
+ )
144
+ return rtnval
145
+
146
+ def __repr__(self) -> str:
147
+ """
148
+ Returns a detailed string representation including the class name,
149
+ provider name, model name, and total token count.
150
+
151
+ :returns: A repr string for debugging.
152
+ """
153
+ rtnval = "{}(provider={!r}, model={!r}, tokens={})".format(
154
+ self.__class__.__name__, self._provider_name,
155
+ self._model_name, self._token_usage.total_tokens
156
+ )
157
+ return rtnval
158
+
159
+ def __str__(self) -> str:
160
+ """
161
+ Returns a human-readable summary including the provider name,
162
+ model name, token usage, and duration.
163
+
164
+ :returns: A human-readable string.
165
+ """
166
+ rtnval = "LLMResponse: {} / {} | {} | {:.2f}s".format(
167
+ self._provider_name, self._model_name,
168
+ str(self._token_usage), self._duration
169
+ )
170
+ return rtnval
171
+
172
+ def __eq__(self, other) -> bool:
173
+ """
174
+ Checks equality based on all response fields.
175
+
176
+ Duration comparison uses a floating-point tolerance of 1e-9.
177
+
178
+ :param other: The object to compare against.
179
+
180
+ :returns: True if all fields are equal, False otherwise.
181
+ """
182
+ if not isinstance(other, LLMResponse):
183
+ return False
184
+
185
+ rtnval = (self._message == other._message and
186
+ self._token_usage == other._token_usage and
187
+ self._provider_name == other._provider_name and
188
+ self._model_name == other._model_name and
189
+ abs(self._duration - other._duration) < 1e-9)
190
+ return rtnval