copilotkit 0.1.88__tar.gz → 0.1.89__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.1.88 → copilotkit-0.1.89}/PKG-INFO +1 -1
  2. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/__init__.py +4 -0
  3. copilotkit-0.1.89/copilotkit/header_propagation.py +58 -0
  4. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/integrations/fastapi.py +4 -0
  5. {copilotkit-0.1.88 → copilotkit-0.1.89}/pyproject.toml +1 -1
  6. {copilotkit-0.1.88 → copilotkit-0.1.89}/README.md +0 -0
  7. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/a2ui.py +0 -0
  8. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/action.py +0 -0
  9. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/agent.py +0 -0
  10. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/copilotkit_lg_middleware.py +0 -0
  11. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/crewai/__init__.py +0 -0
  12. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/crewai/copilotkit_integration.py +0 -0
  13. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/crewai/crewai_agent.py +0 -0
  14. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/crewai/crewai_sdk.py +0 -0
  15. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/exc.py +0 -0
  16. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/html.py +0 -0
  17. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/integrations/__init__.py +0 -0
  18. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/langchain.py +0 -0
  19. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/langgraph.py +0 -0
  20. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/langgraph_agui_agent.py +0 -0
  21. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/logging.py +0 -0
  22. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/parameter.py +0 -0
  23. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/protocol.py +0 -0
  24. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/py.typed +0 -0
  25. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/runloop.py +0 -0
  26. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/sdk.py +0 -0
  27. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/types.py +0 -0
  28. {copilotkit-0.1.88 → copilotkit-0.1.89}/copilotkit/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: copilotkit
3
- Version: 0.1.88
3
+ Version: 0.1.89
4
4
  Summary: CopilotKit python SDK
5
5
  License: MIT
6
6
  Keywords: copilot,copilotkit,langgraph,langchain,ai,langsmith,langserve
@@ -7,6 +7,7 @@ from .agent import Agent
7
7
  from .langgraph_agui_agent import LangGraphAGUIAgent
8
8
  from .copilotkit_lg_middleware import CopilotKitMiddleware
9
9
  from ag_ui_langgraph.middlewares.state_streaming import StateStreamingMiddleware, StateItem
10
+ from .header_propagation import set_forwarded_headers, get_forwarded_headers, install_httpx_hook
10
11
 
11
12
 
12
13
 
@@ -24,4 +25,7 @@ __all__ = [
24
25
  "CopilotKitMiddleware",
25
26
  "StateStreamingMiddleware",
26
27
  "StateItem",
28
+ "set_forwarded_headers",
29
+ "get_forwarded_headers",
30
+ "install_httpx_hook",
27
31
  ]
@@ -0,0 +1,58 @@
1
+ """Header propagation for forwarding x-* prefixed headers to outgoing LLM calls.
2
+
3
+ Uses Python contextvars for per-request ambient state in async FastAPI handlers.
4
+ An httpx event hook reads the ContextVar and injects headers on outgoing requests.
5
+ Matches the CopilotKit runtime's extractForwardableHeaders() behavior.
6
+ """
7
+
8
+ import contextvars
9
+ import warnings
10
+ from typing import Dict
11
+
12
+ # Ambient per-request state for headers to forward to LLM calls
13
+ _forwarded_headers: contextvars.ContextVar[Dict[str, str]] = contextvars.ContextVar(
14
+ 'copilotkit_forwarded_headers'
15
+ )
16
+
17
+
18
+ def set_forwarded_headers(headers: Dict[str, str]) -> None:
19
+ """Store headers to forward to outgoing LLM calls.
20
+ Filters to x-* prefixed headers only."""
21
+ filtered = {k.lower(): v for k, v in headers.items() if k.lower().startswith('x-')}
22
+ _forwarded_headers.set(filtered)
23
+
24
+
25
+ def get_forwarded_headers() -> Dict[str, str]:
26
+ """Get headers that should be forwarded to outgoing LLM calls."""
27
+ return _forwarded_headers.get({})
28
+
29
+
30
+ def install_httpx_hook(client) -> None:
31
+ """Append an event hook to an httpx client that injects forwarded headers.
32
+
33
+ Works with OpenAI and Anthropic Python SDKs (both use httpx internally).
34
+ No-op when no headers are set (demo traffic).
35
+
36
+ Parameters
37
+ ----------
38
+ client : object
39
+ An OpenAI/Anthropic client instance, or a raw httpx.Client/AsyncClient.
40
+ For SDK clients the underlying transport lives at ``client._client``.
41
+ """
42
+ def _inject_headers(request):
43
+ headers = get_forwarded_headers()
44
+ for key, value in headers.items():
45
+ request.headers[key] = value
46
+
47
+ # OpenAI / Anthropic SDKs wrap an httpx client at client._client
48
+ if hasattr(client, '_client') and hasattr(client._client, 'event_hooks'):
49
+ client._client.event_hooks['request'].append(_inject_headers)
50
+ elif hasattr(client, 'event_hooks'):
51
+ # Raw httpx.Client / httpx.AsyncClient
52
+ client.event_hooks['request'].append(_inject_headers)
53
+ else:
54
+ warnings.warn(
55
+ f"install_httpx_hook: client of type {type(client).__name__} has no "
56
+ "recognized event_hooks attribute; x-* headers will not be forwarded",
57
+ stacklevel=2,
58
+ )
@@ -20,6 +20,7 @@ from ..exc import (
20
20
  )
21
21
  from ..action import ActionDict
22
22
  from ..html import generate_info_html
23
+ from ..header_propagation import set_forwarded_headers
23
24
  logging.basicConfig(level=logging.ERROR)
24
25
  logger = logging.getLogger(__name__)
25
26
 
@@ -79,6 +80,9 @@ async def handler(request: Request, sdk: CopilotKitRemoteEndpoint):
79
80
  except: # pylint: disable=bare-except
80
81
  body = None
81
82
 
83
+ # Propagate x-aimock-* headers to outgoing LLM calls via ContextVar
84
+ set_forwarded_headers(dict(request.headers))
85
+
82
86
  path = request.path_params.get('path')
83
87
  method = request.method
84
88
  context = cast(
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "copilotkit"
3
- version = "0.1.88"
3
+ version = "0.1.89"
4
4
  description = "CopilotKit python SDK"
5
5
  authors = ["Markus Ecker <markus.ecker@gmail.com>"]
6
6
  license = "MIT"
File without changes