copilotkit 0.1.34a1__tar.gz

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.
@@ -0,0 +1,30 @@
1
+ Metadata-Version: 2.1
2
+ Name: copilotkit
3
+ Version: 0.1.34a1
4
+ Summary: CopilotKit python SDK
5
+ Home-page: https://copilotkit.ai
6
+ License: MIT
7
+ Keywords: copilot,copilotkit,langgraph,langchain,ai,langsmith,langserve
8
+ Author: Markus Ecker
9
+ Author-email: markus.ecker@gmail.com
10
+ Requires-Python: >=3.9,<4.0
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Requires-Dist: fastapi (>=0.115.0,<0.116.0)
18
+ Requires-Dist: httpx (>=0.27.2,<0.28.0)
19
+ Requires-Dist: langchain (>=0.3.3,<0.4.0)
20
+ Requires-Dist: langchain-anthropic (>=0.2.3,<0.4.0)
21
+ Requires-Dist: langchain-openai (>=0.2.2,<0.3.0)
22
+ Requires-Dist: langgraph (>=0.2.50)
23
+ Requires-Dist: langgraph-sdk (>=0.1.32,<0.2.0)
24
+ Requires-Dist: partialjson (>=0.0.8,<0.0.9)
25
+ Requires-Dist: toml (>=0.10.2,<0.11.0)
26
+ Project-URL: Repository, https://github.com/CopilotKit/sdk-python
27
+ Description-Content-Type: text/markdown
28
+
29
+ # CopilotKit python SDK
30
+
@@ -0,0 +1 @@
1
+ # CopilotKit python SDK
@@ -0,0 +1,19 @@
1
+ """CopilotKit SDK"""
2
+ from .sdk import CopilotKitRemoteEndpoint, CopilotKitContext, CopilotKitSDK, CopilotKitSDKContext
3
+ from .action import Action
4
+ from .langgraph_agent import LangGraphAgent
5
+ from .state import CopilotKitState
6
+ from .parameter import Parameter
7
+ from .agent import Agent
8
+
9
+ __all__ = [
10
+ 'CopilotKitRemoteEndpoint',
11
+ 'CopilotKitSDK',
12
+ 'Action',
13
+ 'LangGraphAgent',
14
+ 'CopilotKitState',
15
+ 'Parameter',
16
+ 'Agent',
17
+ 'CopilotKitContext',
18
+ 'CopilotKitSDKContext'
19
+ ]
@@ -0,0 +1,51 @@
1
+ """Actions"""
2
+
3
+
4
+ from inspect import iscoroutinefunction
5
+ from typing import Optional, List, Callable, TypedDict, Any, cast
6
+ from .parameter import Parameter, normalize_parameters
7
+
8
+ class ActionDict(TypedDict):
9
+ """Dict representation of an action"""
10
+ name: str
11
+ description: str
12
+ parameters: List[Parameter]
13
+
14
+ class ActionResultDict(TypedDict):
15
+ """Dict representation of an action result"""
16
+ result: Any
17
+
18
+ class Action: # pylint: disable=too-few-public-methods
19
+ """Action class for CopilotKit"""
20
+ def __init__(
21
+ self,
22
+ *,
23
+ name: str,
24
+ handler: Callable,
25
+ description: Optional[str] = None,
26
+ parameters: Optional[List[Parameter]] = None,
27
+ ):
28
+ self.name = name
29
+ self.description = description
30
+ self.parameters = parameters
31
+ self.handler = handler
32
+
33
+ async def execute(
34
+ self,
35
+ *,
36
+ arguments: dict
37
+ ) -> ActionResultDict:
38
+ """Execute the action"""
39
+ result = self.handler(**arguments)
40
+
41
+ return {
42
+ "result": await result if iscoroutinefunction(self.handler) else result
43
+ }
44
+
45
+ def dict_repr(self) -> ActionDict:
46
+ """Dict representation of the action"""
47
+ return {
48
+ 'name': self.name,
49
+ 'description': self.description or '',
50
+ 'parameters': normalize_parameters(cast(Any, self.parameters)),
51
+ }
@@ -0,0 +1,50 @@
1
+ """Agents"""
2
+
3
+ from typing import Optional, List, TypedDict
4
+ from abc import ABC, abstractmethod
5
+ from .types import Message
6
+ from .action import ActionDict
7
+
8
+ class AgentDict(TypedDict):
9
+ """Agent dictionary"""
10
+ name: str
11
+ description: Optional[str]
12
+
13
+ class Agent(ABC):
14
+ """Agent class for CopilotKit"""
15
+ def __init__(
16
+ self,
17
+ *,
18
+ name: str,
19
+ description: Optional[str] = None,
20
+ ):
21
+ self.name = name
22
+ self.description = description
23
+
24
+ @abstractmethod
25
+ def execute( # pylint: disable=too-many-arguments
26
+ self,
27
+ *,
28
+ state: dict,
29
+ messages: List[Message],
30
+ thread_id: Optional[str] = None,
31
+ node_name: Optional[str] = None,
32
+ actions: Optional[List[ActionDict]] = None,
33
+ ):
34
+ """Execute the agent"""
35
+
36
+ @abstractmethod
37
+ def get_state(
38
+ self,
39
+ *,
40
+ thread_id: str,
41
+ ):
42
+ """Get agent state"""
43
+
44
+
45
+ def dict_repr(self) -> AgentDict:
46
+ """Dict representation of the action"""
47
+ return {
48
+ 'name': self.name,
49
+ 'description': self.description or ''
50
+ }
@@ -0,0 +1,31 @@
1
+ """Exceptions for CopilotKit."""
2
+
3
+ class ActionNotFoundException(Exception):
4
+ """Exception raised when an action or agent is not found."""
5
+
6
+ def __init__(self, name: str):
7
+ self.name = name
8
+ super().__init__(f"Action '{name}' not found.")
9
+
10
+ class AgentNotFoundException(Exception):
11
+ """Exception raised when an agent is not found."""
12
+
13
+ def __init__(self, name: str):
14
+ self.name = name
15
+ super().__init__(f"Agent '{name}' not found.")
16
+
17
+ class ActionExecutionException(Exception):
18
+ """Exception raised when an action fails to execute."""
19
+
20
+ def __init__(self, name: str, error: Exception):
21
+ self.name = name
22
+ self.error = error
23
+ super().__init__(f"Action '{name}' failed to execute: {error}")
24
+
25
+ class AgentExecutionException(Exception):
26
+ """Exception raised when an agent fails to execute."""
27
+
28
+ def __init__(self, name: str, error: Exception):
29
+ self.name = name
30
+ self.error = error
31
+ super().__init__(f"Agent '{name}' failed to execute: {error}")
@@ -0,0 +1,210 @@
1
+ """FastAPI integration"""
2
+
3
+ import logging
4
+ import asyncio
5
+ #
6
+ from concurrent.futures import ThreadPoolExecutor
7
+ from typing import List, Any, cast
8
+ from fastapi import FastAPI, Request, HTTPException
9
+ from fastapi.responses import JSONResponse, StreamingResponse
10
+ from ..sdk import CopilotKitRemoteEndpoint, CopilotKitContext
11
+ from ..types import Message
12
+ from ..exc import (
13
+ ActionNotFoundException,
14
+ ActionExecutionException,
15
+ AgentNotFoundException,
16
+ AgentExecutionException,
17
+ )
18
+ from ..action import ActionDict
19
+
20
+ logging.basicConfig(level=logging.ERROR)
21
+ logger = logging.getLogger(__name__)
22
+
23
+ def add_fastapi_endpoint(
24
+ fastapi_app: FastAPI,
25
+ sdk: CopilotKitRemoteEndpoint,
26
+ prefix: str,
27
+ *,
28
+ max_workers: int = 10,
29
+ ):
30
+ """Add FastAPI endpoint with configurable ThreadPoolExecutor size"""
31
+ executor = ThreadPoolExecutor(max_workers=max_workers)
32
+
33
+ def run_handler_in_thread(request: Request, sdk: CopilotKitRemoteEndpoint):
34
+ # Run the handler coroutine in the event loop
35
+ loop = asyncio.new_event_loop()
36
+ asyncio.set_event_loop(loop)
37
+ return loop.run_until_complete(handler(request, sdk))
38
+
39
+ async def make_handler(request: Request):
40
+ loop = asyncio.get_event_loop()
41
+ future = loop.run_in_executor(executor, run_handler_in_thread, request, sdk)
42
+ return await future
43
+
44
+ # Ensure the prefix starts with a slash and remove trailing slashes
45
+ normalized_prefix = '/' + prefix.strip('/')
46
+
47
+ fastapi_app.add_api_route(
48
+ f"{normalized_prefix}/{{path:path}}",
49
+ make_handler,
50
+ methods=['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
51
+ )
52
+
53
+ def body_get_or_raise(body: Any, key: str):
54
+ """Get value from body or raise an error"""
55
+ value = body.get(key)
56
+ if value is None:
57
+ raise HTTPException(status_code=400, detail=f"{key} is required")
58
+ return value
59
+
60
+
61
+ async def handler(request: Request, sdk: CopilotKitRemoteEndpoint):
62
+ """Handle FastAPI request"""
63
+
64
+ try:
65
+ body = await request.json()
66
+ except Exception as exc:
67
+ raise HTTPException(status_code=400, detail="Request body is required") from exc
68
+
69
+ path = request.path_params.get('path')
70
+ method = request.method
71
+ context = cast(
72
+ CopilotKitContext,
73
+ {
74
+ "properties": body.get("properties", {}),
75
+ "frontend_url": body.get("frontendUrl", None)
76
+ }
77
+ )
78
+
79
+ if method == 'POST' and path == 'info':
80
+ return await handle_info(sdk=sdk, context=context)
81
+
82
+ if method == 'POST' and path == 'actions/execute':
83
+ name = body_get_or_raise(body, "name")
84
+ arguments = body.get("arguments", {})
85
+
86
+ return await handle_execute_action(
87
+ sdk=sdk,
88
+ context=context,
89
+ name=name,
90
+ arguments=arguments,
91
+ )
92
+
93
+ if method == 'POST' and path == 'agents/execute':
94
+ thread_id = body.get("threadId")
95
+ node_name = body.get("nodeName")
96
+
97
+ name = body_get_or_raise(body, "name")
98
+ state = body_get_or_raise(body, "state")
99
+ messages = body_get_or_raise(body, "messages")
100
+ actions = cast(List[ActionDict], body.get("actions", []))
101
+
102
+ return handle_execute_agent(
103
+ sdk=sdk,
104
+ context=context,
105
+ thread_id=thread_id,
106
+ node_name=node_name,
107
+ name=name,
108
+ state=state,
109
+ messages=messages,
110
+ actions=actions,
111
+ )
112
+
113
+ if method == 'POST' and path == 'agents/state':
114
+ thread_id = body_get_or_raise(body, "threadId")
115
+ name = body_get_or_raise(body, "name")
116
+
117
+ return handle_get_agent_state(
118
+ sdk=sdk,
119
+ context=context,
120
+ thread_id=thread_id,
121
+ name=name,
122
+ )
123
+
124
+ raise HTTPException(status_code=404, detail="Not found")
125
+
126
+
127
+ async def handle_info(*, sdk: CopilotKitRemoteEndpoint, context: CopilotKitContext):
128
+ """Handle info request with FastAPI"""
129
+ result = sdk.info(context=context)
130
+ return JSONResponse(content=result)
131
+
132
+ async def handle_execute_action(
133
+ *,
134
+ sdk: CopilotKitRemoteEndpoint,
135
+ context: CopilotKitContext,
136
+ name: str,
137
+ arguments: dict,
138
+ ):
139
+ """Handle execute action request with FastAPI"""
140
+ try:
141
+ result = await sdk.execute_action(
142
+ context=context,
143
+ name=name,
144
+ arguments=arguments
145
+ )
146
+ return JSONResponse(content=result)
147
+ except ActionNotFoundException as exc:
148
+ logger.error("Action not found: %s", exc)
149
+ return JSONResponse(content={"error": str(exc)}, status_code=404)
150
+ except ActionExecutionException as exc:
151
+ logger.error("Action execution error: %s", exc)
152
+ return JSONResponse(content={"error": str(exc)}, status_code=500)
153
+ except Exception as exc: # pylint: disable=broad-except
154
+ logger.error("Action execution error: %s", exc)
155
+ return JSONResponse(content={"error": str(exc)}, status_code=500)
156
+
157
+ def handle_execute_agent( # pylint: disable=too-many-arguments
158
+ *,
159
+ sdk: CopilotKitRemoteEndpoint,
160
+ context: CopilotKitContext,
161
+ thread_id: str,
162
+ node_name: str,
163
+ name: str,
164
+ state: dict,
165
+ messages: List[Message],
166
+ actions: List[ActionDict],
167
+ ):
168
+ """Handle continue agent execution request with FastAPI"""
169
+ try:
170
+ events = sdk.execute_agent(
171
+ context=context,
172
+ thread_id=thread_id,
173
+ name=name,
174
+ node_name=node_name,
175
+ state=state,
176
+ messages=messages,
177
+ actions=actions,
178
+ )
179
+ return StreamingResponse(events, media_type="application/json")
180
+ except AgentNotFoundException as exc:
181
+ logger.error("Agent not found: %s", exc, exc_info=True)
182
+ return JSONResponse(content={"error": str(exc)}, status_code=404)
183
+ except AgentExecutionException as exc:
184
+ logger.error("Agent execution error: %s", exc, exc_info=True)
185
+ return JSONResponse(content={"error": str(exc)}, status_code=500)
186
+ except Exception as exc: # pylint: disable=broad-except
187
+ logger.error("Agent execution error: %s", exc, exc_info=True)
188
+ return JSONResponse(content={"error": str(exc)}, status_code=500)
189
+
190
+ def handle_get_agent_state(
191
+ *,
192
+ sdk: CopilotKitRemoteEndpoint,
193
+ context: CopilotKitContext,
194
+ thread_id: str,
195
+ name: str,
196
+ ):
197
+ """Handle get agent state request with FastAPI"""
198
+ try:
199
+ result = sdk.get_agent_state(
200
+ context=context,
201
+ thread_id=thread_id,
202
+ name=name,
203
+ )
204
+ return JSONResponse(content=result)
205
+ except AgentNotFoundException as exc:
206
+ logger.error("Agent not found: %s", exc, exc_info=True)
207
+ return JSONResponse(content={"error": str(exc)}, status_code=404)
208
+ except Exception as exc: # pylint: disable=broad-except
209
+ logger.error("Agent get state error: %s", exc, exc_info=True)
210
+ return JSONResponse(content={"error": str(exc)}, status_code=500)
@@ -0,0 +1,30 @@
1
+ """
2
+ copilotkit.langchain is deprecated. Use copilotkit.langgraph instead.
3
+ """
4
+ import warnings
5
+ from copilotkit.langgraph import (
6
+ langchain_messages_to_copilotkit,
7
+ copilotkit_messages_to_langchain,
8
+ copilotkit_customize_config,
9
+ copilotkit_exit,
10
+ copilotkit_emit_state,
11
+ copilotkit_emit_message,
12
+ copilotkit_emit_tool_call,
13
+
14
+ )
15
+
16
+ warnings.warn(
17
+ "copilotkit.langchain is deprecated. Use copilotkit.langgraph instead.",
18
+ DeprecationWarning,
19
+ stacklevel=2
20
+ )
21
+
22
+ __all__ = [
23
+ "langchain_messages_to_copilotkit",
24
+ "copilotkit_messages_to_langchain",
25
+ "copilotkit_customize_config",
26
+ "copilotkit_exit",
27
+ "copilotkit_emit_state",
28
+ "copilotkit_emit_message",
29
+ "copilotkit_emit_tool_call",
30
+ ]