copilotkit 0.1.85__tar.gz → 0.1.86__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 (27) hide show
  1. {copilotkit-0.1.85 → copilotkit-0.1.86}/PKG-INFO +3 -3
  2. copilotkit-0.1.86/copilotkit/a2ui.py +246 -0
  3. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/copilotkit_lg_middleware.py +17 -2
  4. {copilotkit-0.1.85 → copilotkit-0.1.86}/pyproject.toml +3 -1
  5. {copilotkit-0.1.85 → copilotkit-0.1.86}/README.md +0 -0
  6. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/__init__.py +0 -0
  7. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/action.py +0 -0
  8. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/agent.py +0 -0
  9. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/crewai/__init__.py +0 -0
  10. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/crewai/copilotkit_integration.py +0 -0
  11. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/crewai/crewai_agent.py +0 -0
  12. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/crewai/crewai_sdk.py +0 -0
  13. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/exc.py +0 -0
  14. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/html.py +0 -0
  15. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/integrations/__init__.py +0 -0
  16. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/integrations/fastapi.py +0 -0
  17. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/langchain.py +0 -0
  18. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/langgraph.py +0 -0
  19. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/langgraph_agent.py +0 -0
  20. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/langgraph_agui_agent.py +0 -0
  21. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/logging.py +0 -0
  22. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/parameter.py +0 -0
  23. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/protocol.py +0 -0
  24. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/runloop.py +0 -0
  25. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/sdk.py +0 -0
  26. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/types.py +0 -0
  27. {copilotkit-0.1.85 → copilotkit-0.1.86}/copilotkit/utils.py +0 -0
@@ -1,7 +1,8 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.1
2
2
  Name: copilotkit
3
- Version: 0.1.85
3
+ Version: 0.1.86
4
4
  Summary: CopilotKit python SDK
5
+ Home-page: https://copilotkit.ai
5
6
  License: MIT
6
7
  Keywords: copilot,copilotkit,langgraph,langchain,ai,langsmith,langserve
7
8
  Author: Markus Ecker
@@ -21,7 +22,6 @@ Requires-Dist: langchain (>=0.3.0)
21
22
  Requires-Dist: langgraph (>=0.3.25,<2)
22
23
  Requires-Dist: partialjson (>=0.0.8,<0.0.9)
23
24
  Requires-Dist: toml (>=0.10.2,<0.11.0)
24
- Project-URL: Homepage, https://copilotkit.ai
25
25
  Project-URL: Repository, https://github.com/CopilotKit/CopilotKit/tree/main/sdk-python
26
26
  Description-Content-Type: text/markdown
27
27
 
@@ -0,0 +1,246 @@
1
+ """
2
+ A2UI helpers — build v0.9 A2UI operations from schema + data.
3
+
4
+ Usage:
5
+ from copilotkit import a2ui
6
+
7
+ schema = a2ui.load_schema("flight_card.json")
8
+
9
+ @tool
10
+ def search_flights(flights: list[Flight]) -> str:
11
+ return a2ui.render([
12
+ a2ui.create_surface("my-surface"),
13
+ a2ui.update_components("my-surface", schema),
14
+ a2ui.update_data_model("my-surface", {"flights": flights}),
15
+ ])
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import json
21
+ from typing import Any
22
+
23
+
24
+ def load_schema(path: str | Path) -> list[dict[str, Any]]:
25
+ """Load an A2UI component schema from a JSON file."""
26
+ with open(path) as f:
27
+ return json.load(f)
28
+
29
+
30
+ def update_components(
31
+ surface_id: str,
32
+ components: list[dict[str, Any]],
33
+ ) -> dict[str, Any]:
34
+ """Build a v0.9 updateComponents operation."""
35
+ return {
36
+ "version": "v0.9",
37
+ "updateComponents": {
38
+ "surfaceId": surface_id,
39
+ "components": components,
40
+ }
41
+ }
42
+
43
+
44
+ def update_data_model(
45
+ surface_id: str,
46
+ data: Any,
47
+ path: str = "/",
48
+ ) -> dict[str, Any]:
49
+ """Build a v0.9 updateDataModel operation with plain JSON value."""
50
+ return {
51
+ "version": "v0.9",
52
+ "updateDataModel": {
53
+ "surfaceId": surface_id,
54
+ "path": path,
55
+ "value": data,
56
+ }
57
+ }
58
+
59
+
60
+ BASIC_CATALOG_ID = "https://a2ui.org/specification/v0_9/basic_catalog.json"
61
+ """The catalog ID for the standard v0.9 basic catalog."""
62
+
63
+
64
+ def create_surface(
65
+ surface_id: str,
66
+ catalog_id: str = BASIC_CATALOG_ID,
67
+ ) -> dict[str, Any]:
68
+ """Build a v0.9 createSurface operation."""
69
+ return {
70
+ "version": "v0.9",
71
+ "createSurface": {
72
+ "surfaceId": surface_id,
73
+ "catalogId": catalog_id,
74
+ }
75
+ }
76
+
77
+
78
+ A2UI_OPERATIONS_KEY = "a2ui_operations"
79
+ """The container key used to wrap A2UI operations for explicit detection."""
80
+
81
+
82
+ def render(
83
+ operations: list[dict[str, Any]]
84
+ ) -> str:
85
+ """Wrap operations in the a2ui_operations container and serialize to JSON.
86
+
87
+ Args:
88
+ operations: The A2UI v0.9 operations (createSurface, updateComponents, updateDataModel).
89
+
90
+ Example::
91
+ render(
92
+ operations=[...],
93
+ )
94
+ """
95
+ result: dict[str, Any] = {A2UI_OPERATIONS_KEY: operations}
96
+ return json.dumps(result)
97
+
98
+
99
+ # ---------------------------------------------------------------------------
100
+ # Dynamic A2UI prompt builder
101
+ # ---------------------------------------------------------------------------
102
+
103
+ DEFAULT_GENERATION_GUIDELINES = """\
104
+ Generate A2UI v0.9 JSON.
105
+
106
+ ## A2UI Protocol Instructions
107
+
108
+ A2UI (Agent to UI) is a protocol for rendering rich UI surfaces from agent responses.
109
+
110
+ CRITICAL: You MUST call the render_a2ui tool with ALL of these arguments:
111
+ - surfaceId: A unique ID for the surface (e.g. "product-comparison")
112
+ - components: REQUIRED — the A2UI component array. NEVER omit this. Use a List with
113
+ children: { componentId: "card-id", path: "/items" } for repeating cards.
114
+ - data: OPTIONAL — a JSON object written to the root of the surface data model.
115
+ Use for pre-filling form values or providing data for path-bound components.
116
+ - every component must have the "component" field specifying the component type (e.g. "Text", "Image", "Row", "Column", "List", "Button", etc.)
117
+
118
+ COMPONENT ID RULES:
119
+ - Every component ID must be unique within the surface.
120
+ - A component MUST NOT reference itself as child/children. This causes a
121
+ circular dependency error. For example, if a component has id="avatar",
122
+ its child must be a DIFFERENT id (e.g. "avatar-img"), never "avatar".
123
+ - The child/children tree must be a DAG — no cycles allowed.
124
+
125
+ PATH RULES FOR TEMPLATES:
126
+ Components inside a repeating List use RELATIVE paths (no leading slash).
127
+ The path is resolved relative to each array item automatically.
128
+ If List has children: { componentId: "card", path: "/items" } and item has key "name",
129
+ use { "path": "name" } (NO leading slash — relative to item).
130
+ CRITICAL: Do NOT use "/name" (absolute) inside templates — use "name" (relative).
131
+ The List's own path ("/items") uses a leading slash (absolute), but all
132
+ components INSIDE the template card use paths WITHOUT leading slash.
133
+ Do NOT use "/items/0/name" or "/items/{@key}/name" — just "name".
134
+
135
+ DATA MODEL:
136
+ The "data" key in the tool args is a plain JSON object that initializes the surface
137
+ data model. Components bound to paths (e.g. "value": { "path": "/form/name" })
138
+ read from and write to this data model. Examples:
139
+ For forms: "data": { "form": { "name": "Alice", "email": "" } }
140
+ For lists: "data": { "items": [{"name": "Product A"}, {"name": "Product B"}] }
141
+ For mixed: "data": { "form": { "query": "" }, "results": [...] }
142
+
143
+ FORMS AND TWO-WAY DATA BINDING:
144
+ To create editable forms, bind input components to data model paths using { "path": "..." }.
145
+ The client automatically writes user input back to the data model at the bound path.
146
+ CRITICAL: Using a literal value (e.g. "value": "") makes the field READ-ONLY.
147
+ You MUST use { "path": "..." } to make inputs editable.
148
+
149
+ All input components use "value" as the binding property:
150
+ - TextField: "value": { "path": "/form/fieldName" }
151
+ - CheckBox: "value": { "path": "/form/isChecked" }
152
+ - Slider: "value": { "path": "/form/sliderVal" }
153
+ - DateTimeInput: "value": { "path": "/form/date" }
154
+ - ChoicePicker: "value": { "path": "/form/choices" }
155
+
156
+ To retrieve form values when a button is clicked, include "context" with path references
157
+ in the button's action. Paths are resolved to their current values at click time:
158
+ "action": { "event": { "name": "submit", "context": { "userName": { "path": "/form/name" } } } }
159
+
160
+ To pre-fill form values, pass initial data via the "data" tool argument:
161
+ "data": { "form": { "name": "Markus" } }
162
+
163
+ FORM EXAMPLE (editable text field with pre-filled value + submit button):
164
+ "components": [
165
+ { "id": "root", "component": "Card", "child": "form-col" },
166
+ { "id": "form-col", "component": "Column", "children": ["name-field", "submit-row"] },
167
+ { "id": "name-field", "component": "TextField", "label": "Name", "value": { "path": "/form/name" } },
168
+ { "id": "submit-row", "component": "Row", "justify": "end", "children": ["submit-btn"] },
169
+ { "id": "submit-btn", "component": "Button", "child": "btn-text", "variant": "primary",
170
+ "action": { "event": { "name": "submit", "context": { "userName": { "path": "/form/name" } } } } },
171
+ { "id": "btn-text", "component": "Text", "text": "Submit" }
172
+ ],
173
+ "data": { "form": { "name": "Markus" } }"""
174
+
175
+ DEFAULT_DESIGN_GUIDELINES = """\
176
+ Create polished, visually appealing interfaces:
177
+ - Always include a title heading (h2) for the surface, outside the List.
178
+ Wrap in a Column: [title, list] as root.
179
+ - For card templates, create clear visual hierarchy:
180
+ - h3 for primary text (names, titles)
181
+ - h2 for featured numbers (prices, scores) — makes them stand out
182
+ - caption for secondary info (ratings, categories, metadata)
183
+ - body for descriptions
184
+ - Use Divider between logical sections within cards.
185
+ - Use Row with justify="spaceBetween" for label-value pairs
186
+ (e.g. "Rating" on left, "4.5/5" on right).
187
+ - Include images when relevant (logos, icons, product photos):
188
+ - Use Image component with variant="smallFeature" or "avatar"
189
+ - Prefer company logos for branded products — Google favicons are reliable:
190
+ https://www.google.com/s2/favicons?domain=sony.com&sz=128
191
+ https://www.google.com/s2/favicons?domain=bose.com&sz=128
192
+ - For generic icons: https://placehold.co/128x128/EEE/999?text=🎧
193
+ - Do NOT invent Unsplash photo-IDs — they will 404. Only use real, known URLs.
194
+ - Use horizontal List direction for side-by-side comparison cards.
195
+ - Keep cards clean — avoid clutter. Whitespace is good.
196
+ - Use consistent surfaceIds (lowercase, hyphenated).
197
+ - NEVER use the same ID for a component and its child — this creates a
198
+ circular dependency. E.g. if id="avatar", child must NOT be "avatar".
199
+ - Both Row and Column support "justify" and "align".
200
+ - Add Button for interactivity. Button needs child (Text ID) + action.
201
+ Action MUST use this exact nested format:
202
+ "action": { "event": { "name": "myAction", "context": { "key": "value" } } }
203
+ The "event" key holds an OBJECT with "name" (required) and "context" (optional).
204
+ Do NOT use a flat format like {"event": "name"} — "event" must be an object.
205
+ Use variant="primary" for main action buttons, variant="borderless" for links.
206
+ - For forms: wrap fields in a Card with a Column. Place the submit button in a
207
+ Row with justify="end". Every input MUST use path binding on the "value" property
208
+ (e.g. "value": { "path": "/form/name" }) to be editable. The submit button's action
209
+ context MUST reference the same paths to capture the user's input.
210
+
211
+ Use the SAME surfaceId as the main surface. Match action names to Button action event names."""
212
+
213
+
214
+ def a2ui_prompt(
215
+ component_schema: str,
216
+ generation_guidelines: str = DEFAULT_GENERATION_GUIDELINES,
217
+ design_guidelines: str = DEFAULT_DESIGN_GUIDELINES,
218
+ ) -> str:
219
+ """Build the system prompt for dynamic A2UI generation.
220
+
221
+ Args:
222
+ component_schema: JSON string of available components and their props.
223
+ Read from state["ag-ui"]["a2ui_schema"].
224
+ generation_guidelines: Instructions for how to call the render_a2ui
225
+ tool, path rules, and data format.
226
+ design_guidelines: Visual design rules, component hierarchy tips,
227
+ and action handler patterns.
228
+
229
+ Returns:
230
+ Complete system prompt string.
231
+ """
232
+ return f"""\
233
+ {generation_guidelines}
234
+
235
+ ## DESIGN GUIDELINES:
236
+ {design_guidelines}
237
+
238
+ ## AVAILABLE COMPONENTS:
239
+ The following components are available for building UI surfaces.
240
+ Use ONLY these components with the specified props.
241
+
242
+ {component_schema}
243
+ """
244
+
245
+
246
+ return None
@@ -262,6 +262,14 @@ class CopilotKitMiddleware(AgentMiddleware[StateSchema, Any]):
262
262
  if isinstance(app_context, str):
263
263
  context_content = app_context
264
264
  else:
265
+ # Handle Pydantic models (e.g. ag_ui Context)
266
+ if hasattr(app_context, "model_dump"):
267
+ app_context = app_context.model_dump()
268
+ elif isinstance(app_context, list):
269
+ app_context = [
270
+ item.model_dump() if hasattr(item, "model_dump") else item
271
+ for item in app_context
272
+ ]
265
273
  context_content = json.dumps(app_context, indent=2)
266
274
 
267
275
  context_message_content = f"App Context:\n{context_content}"
@@ -300,8 +308,15 @@ class CopilotKitMiddleware(AgentMiddleware[StateSchema, Any]):
300
308
  existing_context_index = i
301
309
  break
302
310
 
303
- # Create the context message
304
- context_message = SystemMessage(content=context_message_content)
311
+ # Create the context message.
312
+ # When replacing an existing context message, reuse its ID so the
313
+ # add_messages reducer updates in-place instead of appending a
314
+ # duplicate at the end of the message list.
315
+ if existing_context_index != -1:
316
+ existing_id = getattr(messages[existing_context_index], "id", None)
317
+ context_message = SystemMessage(content=context_message_content, id=existing_id)
318
+ else:
319
+ context_message = SystemMessage(content=context_message_content)
305
320
 
306
321
  if existing_context_index != -1:
307
322
  # Replace existing context message
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "copilotkit"
3
- version = "0.1.85"
3
+ version = "0.1.86"
4
4
  description = "CopilotKit python SDK"
5
5
  authors = ["Markus Ecker <markus.ecker@gmail.com>"]
6
6
  license = "MIT"
@@ -16,6 +16,8 @@ keywords = [
16
16
  "langsmith",
17
17
  "langserve",
18
18
  ]
19
+ packages = [{ include = "copilotkit" }]
20
+ include = ["copilotkit/*.json"]
19
21
 
20
22
  [tool.poetry.dependencies]
21
23
  python = ">=3.10,<3.13"
File without changes