copilotkit 0.1.74__py3-none-any.whl → 0.1.75a0__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,197 @@
1
+ """
2
+ AG-UI Middleware for LangGraph agents.
3
+
4
+ Enables:
5
+ - Dynamic frontend tools from state['ag-ui']['tools']
6
+ - Frontend tool routing (intercept calls, skip backend execution)
7
+
8
+ Works with any agent (prebuilt or custom).
9
+
10
+ Example:
11
+ from langchain.agents import create_agent
12
+ from copilotkit import CopilotKitMiddleware
13
+
14
+ agent = create_agent(
15
+ model="openai:gpt-4o",
16
+ tools=[backend_tool],
17
+ middleware=[CopilotKitMiddleware()],
18
+ )
19
+ """
20
+
21
+ from typing import Any, Callable, Awaitable, Annotated
22
+ from typing_extensions import TypedDict, NotRequired
23
+
24
+ from langchain_core.messages import AIMessage
25
+ from langchain.agents.middleware import (
26
+ AgentMiddleware,
27
+ AgentState,
28
+ ModelRequest,
29
+ ModelResponse,
30
+ )
31
+ from langgraph.runtime import Runtime
32
+
33
+
34
+ class CopilotKitState(AgentState):
35
+ """Extended state schema for AG-UI middleware."""
36
+
37
+ # AG-UI frontend tools passed via state
38
+ actions: List[Any]
39
+ context: List[CopilotContextItem]
40
+
41
+ # Private state for CopilotKit middleware
42
+ copilotkit: NotRequired[dict[str, Any]]
43
+
44
+
45
+ class CopilotKitMiddleware(AgentMiddleware[AGUIState, Any]):
46
+ """AG-UI Middleware for LangGraph agents.
47
+
48
+ Handles frontend tool injection and interception for AG-UI protocol.
49
+ """
50
+
51
+ state_schema = AGUIState
52
+ tools = []
53
+
54
+ @property
55
+ def name(self) -> str:
56
+ return "CopilotKitMiddleware"
57
+
58
+ # Inject frontend tools before model call
59
+ def wrap_model_call(
60
+ self,
61
+ request: ModelRequest,
62
+ handler: Callable[[ModelRequest], ModelResponse],
63
+ ) -> ModelResponse:
64
+ frontend_tools = request.state.get("ag-ui", {}).get("tools", [])
65
+
66
+ if not frontend_tools:
67
+ return handler(request)
68
+
69
+ # Merge frontend tools with existing tools
70
+ merged_tools = [*request.tools, *frontend_tools]
71
+
72
+ return handler(request.override(tools=merged_tools))
73
+
74
+ async def awrap_model_call(
75
+ self,
76
+ request: ModelRequest,
77
+ handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
78
+ ) -> ModelResponse:
79
+ frontend_tools = request.state.get("ag-ui", {}).get("tools", [])
80
+
81
+ if not frontend_tools:
82
+ return await handler(request)
83
+
84
+ # Merge frontend tools with existing tools
85
+ merged_tools = [*request.tools, *frontend_tools]
86
+
87
+ return await handler(request.override(tools=merged_tools))
88
+
89
+ # Intercept frontend tool calls after model returns, before ToolNode executes
90
+ def after_model(
91
+ self,
92
+ state: AGUIState,
93
+ runtime: Runtime[Any],
94
+ ) -> dict[str, Any] | None:
95
+ frontend_tools = state.get("ag-ui", {}).get("tools", [])
96
+ if not frontend_tools:
97
+ return None
98
+
99
+ frontend_tool_names = {
100
+ t.get("function", {}).get("name") or t.get("name")
101
+ for t in frontend_tools
102
+ }
103
+
104
+ # Find last AI message with tool calls
105
+ messages = state.get("messages", [])
106
+ if not messages:
107
+ return None
108
+
109
+ last_message = messages[-1]
110
+ if not isinstance(last_message, AIMessage):
111
+ return None
112
+
113
+ tool_calls = getattr(last_message, "tool_calls", None) or []
114
+ if not tool_calls:
115
+ return None
116
+
117
+ backend_tool_calls = []
118
+ frontend_tool_calls = []
119
+
120
+ for call in tool_calls:
121
+ if call.get("name") in frontend_tool_names:
122
+ frontend_tool_calls.append(call)
123
+ else:
124
+ backend_tool_calls.append(call)
125
+
126
+ if not frontend_tool_calls:
127
+ return None
128
+
129
+ # Create updated AIMessage with only backend tool calls
130
+ updated_ai_message = AIMessage(
131
+ content=last_message.content,
132
+ tool_calls=backend_tool_calls,
133
+ id=last_message.id,
134
+ )
135
+
136
+ return {
137
+ "messages": [*messages[:-1], updated_ai_message],
138
+ "copilotkit": {
139
+ "intercepted_tool_calls": frontend_tool_calls,
140
+ "original_ai_message_id": last_message.id,
141
+ },
142
+ }
143
+
144
+ async def aafter_model(
145
+ self,
146
+ state: AGUIState,
147
+ runtime: Runtime[Any],
148
+ ) -> dict[str, Any] | None:
149
+ # Delegate to sync implementation
150
+ return self.after_model(state, runtime)
151
+
152
+ # Restore frontend tool calls to AIMessage before agent exits
153
+ def after_agent(
154
+ self,
155
+ state: AGUIState,
156
+ runtime: Runtime[Any],
157
+ ) -> dict[str, Any] | None:
158
+ copilotkit_state = state.get("copilotkit", {})
159
+ intercepted_tool_calls = copilotkit_state.get("intercepted_tool_calls")
160
+ original_message_id = copilotkit_state.get("original_ai_message_id")
161
+
162
+ if not intercepted_tool_calls or not original_message_id:
163
+ return None
164
+
165
+ messages = state.get("messages", [])
166
+ updated_messages = []
167
+
168
+ for msg in messages:
169
+ if isinstance(msg, AIMessage) and msg.id == original_message_id:
170
+ existing_tool_calls = getattr(msg, "tool_calls", None) or []
171
+ updated_messages.append(AIMessage(
172
+ content=msg.content,
173
+ tool_calls=[*existing_tool_calls, *intercepted_tool_calls],
174
+ id=msg.id,
175
+ ))
176
+ else:
177
+ updated_messages.append(msg)
178
+
179
+ return {
180
+ "messages": updated_messages,
181
+ "copilotkit": {
182
+ "intercepted_tool_calls": None,
183
+ "original_ai_message_id": None,
184
+ },
185
+ }
186
+
187
+ async def aafter_agent(
188
+ self,
189
+ state: AGUIState,
190
+ runtime: Runtime[Any],
191
+ ) -> dict[str, Any] | None:
192
+ # Delegate to sync implementation
193
+ return self.after_agent(state, runtime)
194
+
195
+
196
+ # Pre-created instance for convenience
197
+ copilotkit_middleware = CopilotKitMiddleware()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: copilotkit
3
- Version: 0.1.74
3
+ Version: 0.1.75a0
4
4
  Summary: CopilotKit python SDK
5
5
  License: MIT
6
6
  Keywords: copilot,copilotkit,langgraph,langchain,ai,langsmith,langserve
@@ -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=_G71STMN6Kqi-b_VZIeY6XX189GQpD7GjhI5AnFMwv8,5900
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.74.dist-info/METADATA,sha256=aMvkZADSsgNwGJhqe33kyxpeNhGQ0HgowqzrJ7ekiAg,2604
24
- copilotkit-0.1.74.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
25
- copilotkit-0.1.74.dist-info/RECORD,,
24
+ copilotkit-0.1.75a0.dist-info/METADATA,sha256=8lYI9IdjJPpLdB3AWOtj5zPgYMEe89a2zp1-amB7l2c,2606
25
+ copilotkit-0.1.75a0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
26
+ copilotkit-0.1.75a0.dist-info/RECORD,,