copilotkit 0.1.43a0__tar.gz → 0.2.0a0__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.
Files changed (28) hide show
  1. copilotkit-0.2.0a0/PKG-INFO +219 -0
  2. copilotkit-0.2.0a0/README.md +193 -0
  3. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/crewai/__init__.py +8 -0
  4. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/crewai/crewai_agent.py +14 -6
  5. copilotkit-0.2.0a0/copilotkit/crewai/crewai_event_listener.py +242 -0
  6. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/crewai/crewai_sdk.py +75 -54
  7. copilotkit-0.2.0a0/copilotkit/crewai/imports.py +130 -0
  8. copilotkit-0.2.0a0/copilotkit/crewai/utils.py +166 -0
  9. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/langgraph.py +2 -2
  10. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/langgraph_agent.py +10 -2
  11. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/pyproject.toml +3 -3
  12. copilotkit-0.1.43a0/PKG-INFO +0 -90
  13. copilotkit-0.1.43a0/README.md +0 -63
  14. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/__init__.py +0 -0
  15. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/action.py +0 -0
  16. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/agent.py +0 -0
  17. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/exc.py +0 -0
  18. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/html.py +0 -0
  19. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/integrations/__init__.py +0 -0
  20. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/integrations/fastapi.py +0 -0
  21. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/langchain.py +0 -0
  22. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/logging.py +0 -0
  23. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/parameter.py +0 -0
  24. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/protocol.py +0 -0
  25. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/runloop.py +0 -0
  26. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/sdk.py +0 -0
  27. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/types.py +0 -0
  28. {copilotkit-0.1.43a0 → copilotkit-0.2.0a0}/copilotkit/utils.py +0 -0
@@ -0,0 +1,219 @@
1
+ Metadata-Version: 2.3
2
+ Name: copilotkit
3
+ Version: 0.2.0a0
4
+ Summary: CopilotKit python SDK
5
+ License: MIT
6
+ Keywords: copilot,copilotkit,langgraph,langchain,ai,langsmith,langserve
7
+ Author: Markus Ecker
8
+ Author-email: markus.ecker@gmail.com
9
+ Requires-Python: >=3.10,<3.13
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Provides-Extra: crewai
16
+ Requires-Dist: crewai (==0.114.0) ; extra == "crewai"
17
+ Requires-Dist: fastapi (>=0.115.0,<0.116.0)
18
+ Requires-Dist: langchain (>=0.3.3,<0.4.0)
19
+ Requires-Dist: langgraph (>=0.2.50)
20
+ Requires-Dist: partialjson (>=0.0.8,<0.0.9)
21
+ Requires-Dist: toml (>=0.10.2,<0.11.0)
22
+ Project-URL: Homepage, https://copilotkit.ai
23
+ Project-URL: Repository, https://github.com/CopilotKit/CopilotKit/tree/main/sdk-python
24
+ Description-Content-Type: text/markdown
25
+
26
+ # CopilotKit Python SDK
27
+
28
+ [![PyPI version](https://badge.fury.io/py/copilotkit.svg)](https://badge.fury.io/py/copilotkit)
29
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
30
+
31
+ The official Python SDK for CopilotKit - build AI copilots and agents into your applications.
32
+
33
+ ## Features
34
+
35
+ - 🚀 Integration with LangGraph and LangChain
36
+ - 🔄 Support for stateful conversations
37
+ - 🔌 FastAPI integration for serving endpoints
38
+ - 🤝 Optional CrewAI integration (requires CrewAI 0.114.0+)
39
+
40
+ ## Version 0.2.0 Breaking Changes
41
+
42
+ ⚠️ As of version 0.2.0, CopilotKit Python SDK now requires CrewAI 0.114.0 or higher.
43
+ If you're using CrewAI with CopilotKit, you must update your CrewAI version.
44
+
45
+ ## Installation
46
+
47
+ ```bash
48
+ pip install copilotkit
49
+ ```
50
+
51
+ With CrewAI support:
52
+
53
+ ```bash
54
+ pip install "copilotkit[crewai]"
55
+ ```
56
+
57
+ ## Quick Start
58
+
59
+ ```python
60
+ from copilotkit import CopilotKitRemoteEndpoint, Action
61
+ from copilotkit.integrations.fastapi import add_fastapi_endpoint
62
+ from fastapi import FastAPI
63
+
64
+ # Define an action handler function
65
+ def greet_user_handler(name: str):
66
+ return f"Hello, {name}!"
67
+
68
+ # Initialize the SDK endpoint
69
+ endpoint = CopilotKitRemoteEndpoint(
70
+ actions=[
71
+ Action(
72
+ name="greet_user",
73
+ handler=greet_user_handler,
74
+ description="Greet the user",
75
+ parameters=[
76
+ {
77
+ "name": "name",
78
+ "type": "string",
79
+ "description": "The name of the user"
80
+ }
81
+ ]
82
+ )
83
+ ]
84
+ )
85
+
86
+ # Create FastAPI app and add CopilotKit endpoint
87
+ app = FastAPI()
88
+ add_fastapi_endpoint(app, endpoint, "/copilotkit")
89
+ ```
90
+
91
+ ## LangGraph Integration
92
+
93
+ CopilotKit provides seamless integration with LangGraph for building sophisticated agent workflows.
94
+
95
+ ### Using LangGraphAgent
96
+
97
+ ```python
98
+ from copilotkit import CopilotKitRemoteEndpoint, LangGraphAgent
99
+ from langgraph.graph import StateGraph, MessagesState
100
+
101
+ # Create your LangGraph
102
+ def create_graph():
103
+ workflow = StateGraph(MessagesState)
104
+ # ... define your graph nodes and edges
105
+ return workflow.compile()
106
+
107
+ # Initialize the SDK with a LangGraph agent
108
+ endpoint = CopilotKitRemoteEndpoint(
109
+ agents=[
110
+ LangGraphAgent(
111
+ name="email_agent",
112
+ description="This agent sends emails",
113
+ graph=create_graph(),
114
+ )
115
+ ]
116
+ )
117
+ ```
118
+
119
+ ### Customizing LangGraph Behavior
120
+
121
+ CopilotKit provides utilities to customize how your LangGraph agents work:
122
+
123
+ ```python
124
+ from copilotkit.langgraph import copilotkit_customize_config, copilotkit_emit_state
125
+
126
+ # Customize configuration
127
+ config = copilotkit_customize_config(
128
+ base_config=None, # or your existing config
129
+ emit_messages=True,
130
+ emit_tool_calls=True,
131
+ emit_intermediate_state=[
132
+ {
133
+ "state_key": "progress",
134
+ "tool": "ProgressTool",
135
+ "tool_argument": "steps"
136
+ },
137
+ ]
138
+ )
139
+
140
+ # Emit intermediate state
141
+ async def long_running_node(state):
142
+ for i in range(10):
143
+ await some_operation(i)
144
+ await copilotkit_emit_state(config, {"progress": i})
145
+ return state
146
+ ```
147
+
148
+ ## CrewAI Integration
149
+
150
+ CopilotKit provides integration with CrewAI for building multi-agent systems.
151
+
152
+ ### Intercepting CopilotKit Actions in CrewAI Flows
153
+
154
+ When building flows that use CopilotKit actions, you need to properly intercept any tool calls that should be handled by the frontend:
155
+
156
+ ```python
157
+ from copilotkit.crewai import create_copilotkit_tool_handlers, check_for_intercepted_actions
158
+
159
+ @router(start_flow)
160
+ async def chat(self):
161
+ # Your existing code...
162
+
163
+ # 1. Create wrapped tool handlers
164
+ wrapped_handlers = create_copilotkit_tool_handlers(
165
+ original_handlers=tool_handlers, # Your original tool handlers
166
+ copilotkit_actions=self.state.copilotkit.actions, # From CopilotKitState
167
+ state=self.state # Flow state for flagging
168
+ )
169
+
170
+ # 2. Use wrapped handlers with LLM call
171
+ response = llm.call(
172
+ messages=[...],
173
+ tools=[
174
+ *self.state.copilotkit.actions, # Include CopilotKit actions
175
+ YOUR_OTHER_TOOLS...
176
+ ],
177
+ available_functions=wrapped_handlers # Use wrapped handlers
178
+ )
179
+
180
+ # 3. Create response message as usual
181
+ message = {"role": "assistant", "content": response}
182
+ self.state.messages.append(message)
183
+
184
+ # 4. Check if a CopilotKit action was intercepted
185
+ if check_for_intercepted_actions(self.state):
186
+ # Return to frontend to handle the action
187
+ return "route_end"
188
+
189
+ # Continue with normal flow...
190
+ ```
191
+
192
+ This pattern ensures that:
193
+
194
+ 1. CopilotKit actions are properly identified
195
+ 2. The frontend gets control when needed
196
+ 3. Other tools continue to work normally
197
+
198
+ ## Documentation
199
+
200
+ For detailed documentation and examples, visit [copilotkit.ai](https://copilotkit.ai)
201
+
202
+ ## Contributing
203
+
204
+ We welcome contributions! Please see our [Contributing Guidelines](https://github.com/CopilotKit/CopilotKit/blob/main/CONTRIBUTING.md) for details.
205
+
206
+ ## License
207
+
208
+ This project is licensed under the MIT License - see the [LICENSE](https://github.com/CopilotKit/CopilotKit/blob/main/LICENSE) file for details.
209
+
210
+ ## Support
211
+
212
+ - 📚 [Documentation](https://docs.copilotkit.ai)
213
+ - 💬 [Discord Community](https://discord.gg/6dffbvGU)
214
+ - 🐛 [Issue Tracker](https://github.com/CopilotKit/CopilotKit/issues)
215
+
216
+ ---
217
+
218
+ Built with ❤️ by the CopilotKit team
219
+
@@ -0,0 +1,193 @@
1
+ # CopilotKit Python SDK
2
+
3
+ [![PyPI version](https://badge.fury.io/py/copilotkit.svg)](https://badge.fury.io/py/copilotkit)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ The official Python SDK for CopilotKit - build AI copilots and agents into your applications.
7
+
8
+ ## Features
9
+
10
+ - 🚀 Integration with LangGraph and LangChain
11
+ - 🔄 Support for stateful conversations
12
+ - 🔌 FastAPI integration for serving endpoints
13
+ - 🤝 Optional CrewAI integration (requires CrewAI 0.114.0+)
14
+
15
+ ## Version 0.2.0 Breaking Changes
16
+
17
+ ⚠️ As of version 0.2.0, CopilotKit Python SDK now requires CrewAI 0.114.0 or higher.
18
+ If you're using CrewAI with CopilotKit, you must update your CrewAI version.
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ pip install copilotkit
24
+ ```
25
+
26
+ With CrewAI support:
27
+
28
+ ```bash
29
+ pip install "copilotkit[crewai]"
30
+ ```
31
+
32
+ ## Quick Start
33
+
34
+ ```python
35
+ from copilotkit import CopilotKitRemoteEndpoint, Action
36
+ from copilotkit.integrations.fastapi import add_fastapi_endpoint
37
+ from fastapi import FastAPI
38
+
39
+ # Define an action handler function
40
+ def greet_user_handler(name: str):
41
+ return f"Hello, {name}!"
42
+
43
+ # Initialize the SDK endpoint
44
+ endpoint = CopilotKitRemoteEndpoint(
45
+ actions=[
46
+ Action(
47
+ name="greet_user",
48
+ handler=greet_user_handler,
49
+ description="Greet the user",
50
+ parameters=[
51
+ {
52
+ "name": "name",
53
+ "type": "string",
54
+ "description": "The name of the user"
55
+ }
56
+ ]
57
+ )
58
+ ]
59
+ )
60
+
61
+ # Create FastAPI app and add CopilotKit endpoint
62
+ app = FastAPI()
63
+ add_fastapi_endpoint(app, endpoint, "/copilotkit")
64
+ ```
65
+
66
+ ## LangGraph Integration
67
+
68
+ CopilotKit provides seamless integration with LangGraph for building sophisticated agent workflows.
69
+
70
+ ### Using LangGraphAgent
71
+
72
+ ```python
73
+ from copilotkit import CopilotKitRemoteEndpoint, LangGraphAgent
74
+ from langgraph.graph import StateGraph, MessagesState
75
+
76
+ # Create your LangGraph
77
+ def create_graph():
78
+ workflow = StateGraph(MessagesState)
79
+ # ... define your graph nodes and edges
80
+ return workflow.compile()
81
+
82
+ # Initialize the SDK with a LangGraph agent
83
+ endpoint = CopilotKitRemoteEndpoint(
84
+ agents=[
85
+ LangGraphAgent(
86
+ name="email_agent",
87
+ description="This agent sends emails",
88
+ graph=create_graph(),
89
+ )
90
+ ]
91
+ )
92
+ ```
93
+
94
+ ### Customizing LangGraph Behavior
95
+
96
+ CopilotKit provides utilities to customize how your LangGraph agents work:
97
+
98
+ ```python
99
+ from copilotkit.langgraph import copilotkit_customize_config, copilotkit_emit_state
100
+
101
+ # Customize configuration
102
+ config = copilotkit_customize_config(
103
+ base_config=None, # or your existing config
104
+ emit_messages=True,
105
+ emit_tool_calls=True,
106
+ emit_intermediate_state=[
107
+ {
108
+ "state_key": "progress",
109
+ "tool": "ProgressTool",
110
+ "tool_argument": "steps"
111
+ },
112
+ ]
113
+ )
114
+
115
+ # Emit intermediate state
116
+ async def long_running_node(state):
117
+ for i in range(10):
118
+ await some_operation(i)
119
+ await copilotkit_emit_state(config, {"progress": i})
120
+ return state
121
+ ```
122
+
123
+ ## CrewAI Integration
124
+
125
+ CopilotKit provides integration with CrewAI for building multi-agent systems.
126
+
127
+ ### Intercepting CopilotKit Actions in CrewAI Flows
128
+
129
+ When building flows that use CopilotKit actions, you need to properly intercept any tool calls that should be handled by the frontend:
130
+
131
+ ```python
132
+ from copilotkit.crewai import create_copilotkit_tool_handlers, check_for_intercepted_actions
133
+
134
+ @router(start_flow)
135
+ async def chat(self):
136
+ # Your existing code...
137
+
138
+ # 1. Create wrapped tool handlers
139
+ wrapped_handlers = create_copilotkit_tool_handlers(
140
+ original_handlers=tool_handlers, # Your original tool handlers
141
+ copilotkit_actions=self.state.copilotkit.actions, # From CopilotKitState
142
+ state=self.state # Flow state for flagging
143
+ )
144
+
145
+ # 2. Use wrapped handlers with LLM call
146
+ response = llm.call(
147
+ messages=[...],
148
+ tools=[
149
+ *self.state.copilotkit.actions, # Include CopilotKit actions
150
+ YOUR_OTHER_TOOLS...
151
+ ],
152
+ available_functions=wrapped_handlers # Use wrapped handlers
153
+ )
154
+
155
+ # 3. Create response message as usual
156
+ message = {"role": "assistant", "content": response}
157
+ self.state.messages.append(message)
158
+
159
+ # 4. Check if a CopilotKit action was intercepted
160
+ if check_for_intercepted_actions(self.state):
161
+ # Return to frontend to handle the action
162
+ return "route_end"
163
+
164
+ # Continue with normal flow...
165
+ ```
166
+
167
+ This pattern ensures that:
168
+
169
+ 1. CopilotKit actions are properly identified
170
+ 2. The frontend gets control when needed
171
+ 3. Other tools continue to work normally
172
+
173
+ ## Documentation
174
+
175
+ For detailed documentation and examples, visit [copilotkit.ai](https://copilotkit.ai)
176
+
177
+ ## Contributing
178
+
179
+ We welcome contributions! Please see our [Contributing Guidelines](https://github.com/CopilotKit/CopilotKit/blob/main/CONTRIBUTING.md) for details.
180
+
181
+ ## License
182
+
183
+ This project is licensed under the MIT License - see the [LICENSE](https://github.com/CopilotKit/CopilotKit/blob/main/LICENSE) file for details.
184
+
185
+ ## Support
186
+
187
+ - 📚 [Documentation](https://docs.copilotkit.ai)
188
+ - 💬 [Discord Community](https://discord.gg/6dffbvGU)
189
+ - 🐛 [Issue Tracker](https://github.com/CopilotKit/CopilotKit/issues)
190
+
191
+ ---
192
+
193
+ Built with ❤️ by the CopilotKit team
@@ -12,6 +12,11 @@ from .crewai_sdk import (
12
12
  copilotkit_exit,
13
13
  copilotkit_predict_state,
14
14
  )
15
+ from .utils import (
16
+ create_copilotkit_tool_handlers,
17
+ check_for_intercepted_actions,
18
+ COPILOTKIT_ACTION_INTERCEPTED_MARKER
19
+ )
15
20
  __all__ = [
16
21
  "CrewAIAgent",
17
22
  "CopilotKitProperties",
@@ -22,4 +27,7 @@ __all__ = [
22
27
  "copilotkit_stream",
23
28
  "copilotkit_exit",
24
29
  "copilotkit_predict_state",
30
+ "create_copilotkit_tool_handlers",
31
+ "check_for_intercepted_actions",
32
+ "COPILOTKIT_ACTION_INTERCEPTED_MARKER",
25
33
  ]
@@ -5,9 +5,9 @@ CrewAI Agent
5
5
  import uuid
6
6
  import json
7
7
  from copy import deepcopy
8
- from pydantic import BaseModel
9
8
  from typing import Optional, List, Callable
10
9
  from typing_extensions import TypedDict, NotRequired, Any, Dict, cast
10
+ from pydantic import BaseModel
11
11
  from crewai import Crew, Flow
12
12
  from crewai.flow import start
13
13
  from crewai.cli.crew_chat import (
@@ -25,12 +25,13 @@ from copilotkit.protocol import (
25
25
  emit_runtime_events,
26
26
  agent_state_message,
27
27
  )
28
- from .crewai_sdk import (
28
+ from copilotkit.crewai.crewai_sdk import (
29
29
  copilotkit_messages_to_crewai_flow,
30
30
  crewai_flow_messages_to_copilotkit,
31
31
  crewai_flow_async_runner,
32
32
  copilotkit_stream,
33
- copilotkit_exit
33
+ copilotkit_exit,
34
+ logger
34
35
  )
35
36
 
36
37
  from copilotkit.runloop import copilotkit_run, CopilotKitRunExecution
@@ -296,14 +297,21 @@ class CrewAIAgent(Agent):
296
297
  if self.flow and self.flow._persistence: # pylint: disable=protected-access
297
298
  try:
298
299
  stored_state = self.flow._persistence.load_state(thread_id) # pylint: disable=protected-access
300
+ messages = []
301
+ if "messages" in stored_state and stored_state["messages"]:
302
+ try:
303
+ messages = crewai_flow_messages_to_copilotkit(stored_state["messages"])
304
+ except Exception as e: # pylint: disable=broad-except
305
+ # If conversion fails, we'll return empty messages
306
+ logger.warning(f"Failed to convert messages from stored state: {str(e)}")
299
307
  return {
300
308
  "threadId": thread_id,
301
309
  "threadExists": True,
302
310
  "state": stored_state,
303
- "messages": []
311
+ "messages": messages
304
312
  }
305
- except: # pylint: disable=bare-except
306
- pass
313
+ except Exception as e: # pylint: disable=broad-except
314
+ logger.warning(f"Failed to load state for thread {thread_id}: {str(e)}")
307
315
 
308
316
  return {
309
317
  "threadId": thread_id,