copilotkit 0.1.75__py3-none-any.whl → 0.1.75a1__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.
copilotkit/__init__.py CHANGED
@@ -6,6 +6,7 @@ from .parameter import Parameter
6
6
  from .agent import Agent
7
7
  from .langgraph_agent import LangGraphAgent
8
8
  from .langgraph_agui_agent import LangGraphAGUIAgent
9
+ from .copilotkit_lg_middleware import copilotkit_middleware
9
10
 
10
11
 
11
12
 
@@ -20,5 +21,6 @@ __all__ = [
20
21
  'CopilotKitSDKContext',
21
22
  'CrewAIAgent', # pyright: ignore[reportUnsupportedDunderAll] pylint: disable=undefined-all-variable
22
23
  'LangGraphAgent', # pyright: ignore[reportUnsupportedDunderAll] pylint: disable=undefined-all-variable
23
- "LangGraphAGUIAgent"
24
+ "LangGraphAGUIAgent",
25
+ "copilotkit_middleware"
24
26
  ]
@@ -0,0 +1,177 @@
1
+ from typing import Any, Callable, Awaitable, Annotated
2
+ from typing_extensions import TypedDict, NotRequired
3
+
4
+ from langchain_core.messages import AIMessage
5
+ from langchain.agents.middleware import (
6
+ AgentMiddleware,
7
+ AgentState,
8
+ ModelRequest,
9
+ ModelResponse,
10
+ )
11
+ from langgraph.runtime import Runtime
12
+
13
+
14
+ class CopilotKitState(AgentState):
15
+ """Extended state schema for CopilotKit middleware."""
16
+
17
+ # CopilotKit frontend tools passed via state
18
+ actions: List[Any]
19
+ context: List[CopilotContextItem]
20
+
21
+ # Private state for CopilotKit middleware
22
+ copilotkit: NotRequired[dict[str, Any]]
23
+
24
+
25
+ class CopilotKitMiddleware(AgentMiddleware[CopilotKitState, Any]):
26
+ """CopilotKit Middleware for LangGraph agents.
27
+
28
+ Handles frontend tool injection and interception for CopilotKit.
29
+ """
30
+
31
+ state_schema = CopilotKitState
32
+ tools = []
33
+
34
+ @property
35
+ def name(self) -> str:
36
+ return "CopilotKitMiddleware"
37
+
38
+ # Inject frontend tools before model call
39
+ def wrap_model_call(
40
+ self,
41
+ request: ModelRequest,
42
+ handler: Callable[[ModelRequest], ModelResponse],
43
+ ) -> ModelResponse:
44
+ frontend_tools = request.state.get("copilotkit", {}).get("tools", [])
45
+
46
+ if not frontend_tools:
47
+ return handler(request)
48
+
49
+ # Merge frontend tools with existing tools
50
+ merged_tools = [*request.tools, *frontend_tools]
51
+
52
+ return handler(request.override(tools=merged_tools))
53
+
54
+ async def awrap_model_call(
55
+ self,
56
+ request: ModelRequest,
57
+ handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
58
+ ) -> ModelResponse:
59
+ frontend_tools = request.state.get("copilotkit", {}).get("tools", [])
60
+
61
+ if not frontend_tools:
62
+ return await handler(request)
63
+
64
+ # Merge frontend tools with existing tools
65
+ merged_tools = [*request.tools, *frontend_tools]
66
+
67
+ return await handler(request.override(tools=merged_tools))
68
+
69
+ # Intercept frontend tool calls after model returns, before ToolNode executes
70
+ def after_model(
71
+ self,
72
+ state: CopilotKitState,
73
+ runtime: Runtime[Any],
74
+ ) -> dict[str, Any] | None:
75
+ frontend_tools = state.get("copilotkit", {}).get("tools", [])
76
+ if not frontend_tools:
77
+ return None
78
+
79
+ frontend_tool_names = {
80
+ t.get("function", {}).get("name") or t.get("name")
81
+ for t in frontend_tools
82
+ }
83
+
84
+ # Find last AI message with tool calls
85
+ messages = state.get("messages", [])
86
+ if not messages:
87
+ return None
88
+
89
+ last_message = messages[-1]
90
+ if not isinstance(last_message, AIMessage):
91
+ return None
92
+
93
+ tool_calls = getattr(last_message, "tool_calls", None) or []
94
+ if not tool_calls:
95
+ return None
96
+
97
+ backend_tool_calls = []
98
+ frontend_tool_calls = []
99
+
100
+ for call in tool_calls:
101
+ if call.get("name") in frontend_tool_names:
102
+ frontend_tool_calls.append(call)
103
+ else:
104
+ backend_tool_calls.append(call)
105
+
106
+ if not frontend_tool_calls:
107
+ return None
108
+
109
+ # Create updated AIMessage with only backend tool calls
110
+ updated_ai_message = AIMessage(
111
+ content=last_message.content,
112
+ tool_calls=backend_tool_calls,
113
+ id=last_message.id,
114
+ )
115
+
116
+ return {
117
+ "messages": [*messages[:-1], updated_ai_message],
118
+ "copilotkit": {
119
+ "intercepted_tool_calls": frontend_tool_calls,
120
+ "original_ai_message_id": last_message.id,
121
+ },
122
+ }
123
+
124
+ async def aafter_model(
125
+ self,
126
+ state: CopilotKitState,
127
+ runtime: Runtime[Any],
128
+ ) -> dict[str, Any] | None:
129
+ # Delegate to sync implementation
130
+ return self.after_model(state, runtime)
131
+
132
+ # Restore frontend tool calls to AIMessage before agent exits
133
+ def after_agent(
134
+ self,
135
+ state: CopilotKitState,
136
+ runtime: Runtime[Any],
137
+ ) -> dict[str, Any] | None:
138
+ copilotkit_state = state.get("copilotkit", {})
139
+ intercepted_tool_calls = copilotkit_state.get("intercepted_tool_calls")
140
+ original_message_id = copilotkit_state.get("original_ai_message_id")
141
+
142
+ if not intercepted_tool_calls or not original_message_id:
143
+ return None
144
+
145
+ messages = state.get("messages", [])
146
+ updated_messages = []
147
+
148
+ for msg in messages:
149
+ if isinstance(msg, AIMessage) and msg.id == original_message_id:
150
+ existing_tool_calls = getattr(msg, "tool_calls", None) or []
151
+ updated_messages.append(AIMessage(
152
+ content=msg.content,
153
+ tool_calls=[*existing_tool_calls, *intercepted_tool_calls],
154
+ id=msg.id,
155
+ ))
156
+ else:
157
+ updated_messages.append(msg)
158
+
159
+ return {
160
+ "messages": updated_messages,
161
+ "copilotkit": {
162
+ "intercepted_tool_calls": None,
163
+ "original_ai_message_id": None,
164
+ },
165
+ }
166
+
167
+ async def aafter_agent(
168
+ self,
169
+ state: CopilotKitState,
170
+ runtime: Runtime[Any],
171
+ ) -> dict[str, Any] | None:
172
+ # Delegate to sync implementation
173
+ return self.after_agent(state, runtime)
174
+
175
+
176
+ # Pre-created instance for convenience
177
+ copilotkit_middleware = CopilotKitMiddleware()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: copilotkit
3
- Version: 0.1.75
3
+ Version: 0.1.75a1
4
4
  Summary: CopilotKit python SDK
5
5
  License: MIT
6
6
  Keywords: copilot,copilotkit,langgraph,langchain,ai,langsmith,langserve
@@ -13,7 +13,7 @@ Classifier: Programming Language :: Python :: 3.10
13
13
  Classifier: Programming Language :: Python :: 3.11
14
14
  Classifier: Programming Language :: Python :: 3.12
15
15
  Provides-Extra: crewai
16
- Requires-Dist: ag-ui-langgraph[fastapi] (>=0.0.23)
16
+ Requires-Dist: ag-ui-langgraph[fastapi] (>=0.0.22)
17
17
  Requires-Dist: crewai (==0.118.0) ; extra == "crewai"
18
18
  Requires-Dist: fastapi (>=0.115.0,<0.116.0)
19
19
  Requires-Dist: langchain (>=0.3.0)
@@ -1,6 +1,7 @@
1
- copilotkit/__init__.py,sha256=mCLfmjwqHyVQT_FbaICtcLvGSijSMuyTw5RN8LwwoWo,772
1
+ copilotkit/__init__.py,sha256=pK1UmbqWsd9blnIZ0p3wYHjkLthAavI2-gCyI5ulJzE,861
2
2
  copilotkit/action.py,sha256=bg_bPusEfN8TMQV1B3lVyMLbHyLn1E7yVYAvfE2_PXA,1691
3
3
  copilotkit/agent.py,sha256=e-jPsUQo18FOYUPdE3YNmSLcb5FEfWL6ARScOntyvb8,1663
4
+ copilotkit/copilotkit_lg_middleware.py,sha256=CHPtSJnc_wEA-pVsq-TwNexA3K8x87dCEORADGxCiSY,5497
4
5
  copilotkit/crewai/__init__.py,sha256=RAkBTwDpFuvD5prULUnoWdl5d8W373cm1ZlqOMeSGbY,1034
5
6
  copilotkit/crewai/copilotkit_integration.py,sha256=vd1LVhXo-cpA4PQ_-cZHdKgm67MvrHpx4VJweUvCzPE,14550
6
7
  copilotkit/crewai/crewai_agent.py,sha256=maoMnl0WIHUnFAK5ALUKD9z0GvjtfNGi31zi37LyyaE,15323
@@ -20,6 +21,6 @@ copilotkit/runloop.py,sha256=9pBqWeu4QCRPOizz00se1d0_BvCX3ZIABzNeiXhdyNM,10641
20
21
  copilotkit/sdk.py,sha256=VEQqA9QAXJS5MDLxtJJdtfrApX9EawCHzp-IXnat5vw,11882
21
22
  copilotkit/types.py,sha256=_YQluiHpJGGXYB4eWLk0EocDZp5C8rs3ZsMNFhmXEYU,972
22
23
  copilotkit/utils.py,sha256=rOAaaFBXgb3iv_cLH9S2steyGwHmnTjaxQTA53qdtQo,203
23
- copilotkit-0.1.75.dist-info/METADATA,sha256=YDl0-TnbUPcAVThbSwTwRO8inLzLj_HTr7ygR5oOAkQ,2604
24
- copilotkit-0.1.75.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
25
- copilotkit-0.1.75.dist-info/RECORD,,
24
+ copilotkit-0.1.75a1.dist-info/METADATA,sha256=N8BPWhy0GIjqbXa5JZF7nSEuJRTDHwDsuVGMrgex3dU,2606
25
+ copilotkit-0.1.75a1.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
26
+ copilotkit-0.1.75a1.dist-info/RECORD,,