msgflux 0.1.0__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.
Files changed (165) hide show
  1. msgflux/__init__.py +104 -0
  2. msgflux/_private/__init__.py +0 -0
  3. msgflux/_private/client.py +105 -0
  4. msgflux/_private/core.py +14 -0
  5. msgflux/_private/executor.py +134 -0
  6. msgflux/_private/response.py +230 -0
  7. msgflux/auto/__init__.py +5 -0
  8. msgflux/auto/params.py +167 -0
  9. msgflux/cache.py +54 -0
  10. msgflux/core/__init__.py +5 -0
  11. msgflux/core/dotdict.py +264 -0
  12. msgflux/core/examples.py +169 -0
  13. msgflux/core/message.py +45 -0
  14. msgflux/data/__init__.py +0 -0
  15. msgflux/data/audio.py +1 -0
  16. msgflux/data/dbs/__init__.py +16 -0
  17. msgflux/data/dbs/base.py +124 -0
  18. msgflux/data/dbs/db.py +65 -0
  19. msgflux/data/dbs/providers/__init__.py +0 -0
  20. msgflux/data/dbs/providers/cachetools.py +57 -0
  21. msgflux/data/dbs/providers/diskcache.py +48 -0
  22. msgflux/data/dbs/providers/faiss.py +220 -0
  23. msgflux/data/dbs/registry.py +19 -0
  24. msgflux/data/dbs/response.py +10 -0
  25. msgflux/data/dbs/types.py +26 -0
  26. msgflux/data/parsers/__init__.py +16 -0
  27. msgflux/data/parsers/base.py +325 -0
  28. msgflux/data/parsers/parser.py +101 -0
  29. msgflux/data/parsers/providers/__init__.py +32 -0
  30. msgflux/data/parsers/providers/csv_parser.py +278 -0
  31. msgflux/data/parsers/providers/email_parser.py +264 -0
  32. msgflux/data/parsers/providers/html_parser.py +265 -0
  33. msgflux/data/parsers/providers/openpyxl.py +333 -0
  34. msgflux/data/parsers/providers/pypdf.py +186 -0
  35. msgflux/data/parsers/providers/python_docx.py +334 -0
  36. msgflux/data/parsers/providers/python_pptx.py +197 -0
  37. msgflux/data/parsers/registry.py +19 -0
  38. msgflux/data/parsers/response.py +32 -0
  39. msgflux/data/parsers/types.py +110 -0
  40. msgflux/data/retrievers/__init__.py +16 -0
  41. msgflux/data/retrievers/base.py +185 -0
  42. msgflux/data/retrievers/providers/__init__.py +0 -0
  43. msgflux/data/retrievers/providers/bm25.py +230 -0
  44. msgflux/data/retrievers/providers/bm25s.py +221 -0
  45. msgflux/data/retrievers/providers/rank_bm25.py +148 -0
  46. msgflux/data/retrievers/providers/rapidfuzz.py +125 -0
  47. msgflux/data/retrievers/providers/wikipedia.py +281 -0
  48. msgflux/data/retrievers/registry.py +19 -0
  49. msgflux/data/retrievers/retriever.py +83 -0
  50. msgflux/data/retrievers/types.py +24 -0
  51. msgflux/data/types/__init__.py +29 -0
  52. msgflux/data/types/audio.py +112 -0
  53. msgflux/data/types/base.py +70 -0
  54. msgflux/data/types/file.py +114 -0
  55. msgflux/data/types/image.py +134 -0
  56. msgflux/data/types/video.py +121 -0
  57. msgflux/dsl/__init__.py +0 -0
  58. msgflux/dsl/inline.py +484 -0
  59. msgflux/dsl/signature.py +393 -0
  60. msgflux/dsl/typed_parsers/__init__.py +19 -0
  61. msgflux/dsl/typed_parsers/base.py +24 -0
  62. msgflux/dsl/typed_parsers/providers/__init__.py +0 -0
  63. msgflux/dsl/typed_parsers/providers/xml.py +259 -0
  64. msgflux/dsl/typed_parsers/registry.py +14 -0
  65. msgflux/envs.py +100 -0
  66. msgflux/exceptions.py +84 -0
  67. msgflux/generation/__init__.py +0 -0
  68. msgflux/generation/control_flow.py +163 -0
  69. msgflux/generation/reasoning/__init__.py +5 -0
  70. msgflux/generation/reasoning/cot.py +7 -0
  71. msgflux/generation/reasoning/react.py +289 -0
  72. msgflux/generation/reasoning/self_consistency.py +16 -0
  73. msgflux/generation/templates.py +53 -0
  74. msgflux/logger.py +159 -0
  75. msgflux/models/__init__.py +16 -0
  76. msgflux/models/base.py +39 -0
  77. msgflux/models/cache.py +125 -0
  78. msgflux/models/gateway.py +466 -0
  79. msgflux/models/httpx.py +81 -0
  80. msgflux/models/model.py +122 -0
  81. msgflux/models/profiles/__init__.py +48 -0
  82. msgflux/models/profiles/base.py +122 -0
  83. msgflux/models/profiles/loader.py +264 -0
  84. msgflux/models/profiles/registry.py +207 -0
  85. msgflux/models/providers/__init__.py +0 -0
  86. msgflux/models/providers/cerebras.py +30 -0
  87. msgflux/models/providers/groq.py +52 -0
  88. msgflux/models/providers/imagerouter.py +33 -0
  89. msgflux/models/providers/jinaai.py +331 -0
  90. msgflux/models/providers/ollama.py +51 -0
  91. msgflux/models/providers/openai.py +1884 -0
  92. msgflux/models/providers/openrouter.py +64 -0
  93. msgflux/models/providers/replicate.py +218 -0
  94. msgflux/models/providers/sambanova.py +38 -0
  95. msgflux/models/providers/together.py +57 -0
  96. msgflux/models/providers/vllm.py +131 -0
  97. msgflux/models/registry.py +19 -0
  98. msgflux/models/response.py +26 -0
  99. msgflux/models/tool_call_agg.py +85 -0
  100. msgflux/models/types.py +196 -0
  101. msgflux/nn/__init__.py +57 -0
  102. msgflux/nn/functional.py +645 -0
  103. msgflux/nn/hooks/__init__.py +24 -0
  104. msgflux/nn/hooks/guard.py +109 -0
  105. msgflux/nn/hooks/hook.py +123 -0
  106. msgflux/nn/modules/__init__.py +68 -0
  107. msgflux/nn/modules/agent.py +1927 -0
  108. msgflux/nn/modules/container.py +672 -0
  109. msgflux/nn/modules/embedder.py +261 -0
  110. msgflux/nn/modules/generator.py +21 -0
  111. msgflux/nn/modules/mediamaker.py +285 -0
  112. msgflux/nn/modules/module.py +2106 -0
  113. msgflux/nn/modules/predictor.py +238 -0
  114. msgflux/nn/modules/searcher.py +383 -0
  115. msgflux/nn/modules/speaker.py +222 -0
  116. msgflux/nn/modules/tool.py +839 -0
  117. msgflux/nn/modules/transcriber.py +273 -0
  118. msgflux/nn/parameter.py +77 -0
  119. msgflux/protocols/__init__.py +1 -0
  120. msgflux/protocols/mcp/__init__.py +61 -0
  121. msgflux/protocols/mcp/auth/__init__.py +17 -0
  122. msgflux/protocols/mcp/auth/apikey.py +70 -0
  123. msgflux/protocols/mcp/auth/base.py +83 -0
  124. msgflux/protocols/mcp/auth/basic.py +57 -0
  125. msgflux/protocols/mcp/auth/bearer.py +97 -0
  126. msgflux/protocols/mcp/auth/custom.py +76 -0
  127. msgflux/protocols/mcp/auth/oauth2.py +133 -0
  128. msgflux/protocols/mcp/client.py +454 -0
  129. msgflux/protocols/mcp/exceptions.py +25 -0
  130. msgflux/protocols/mcp/integration.py +95 -0
  131. msgflux/protocols/mcp/loglevels.py +16 -0
  132. msgflux/protocols/mcp/transports.py +435 -0
  133. msgflux/protocols/mcp/types.py +51 -0
  134. msgflux/py.typed +0 -0
  135. msgflux/telemetry/__init__.py +22 -0
  136. msgflux/telemetry/config.py +104 -0
  137. msgflux/telemetry/span.py +245 -0
  138. msgflux/tools/__init__.py +10 -0
  139. msgflux/tools/builtin/__init__.py +5 -0
  140. msgflux/tools/builtin/web_fetch.py +121 -0
  141. msgflux/tools/config.py +196 -0
  142. msgflux/tools/definitions.py +11 -0
  143. msgflux/utils/__init__.py +0 -0
  144. msgflux/utils/chat.py +682 -0
  145. msgflux/utils/common.py +36 -0
  146. msgflux/utils/console.py +55 -0
  147. msgflux/utils/convert.py +20 -0
  148. msgflux/utils/encode.py +192 -0
  149. msgflux/utils/html.py +171 -0
  150. msgflux/utils/imports.py +115 -0
  151. msgflux/utils/inspect.py +81 -0
  152. msgflux/utils/logging.py +15 -0
  153. msgflux/utils/mermaid.py +20 -0
  154. msgflux/utils/msgspec.py +1116 -0
  155. msgflux/utils/pooling.py +51 -0
  156. msgflux/utils/templates.py +51 -0
  157. msgflux/utils/tenacity.py +38 -0
  158. msgflux/utils/torch.py +7 -0
  159. msgflux/utils/validation.py +31 -0
  160. msgflux/utils/xml.py +7 -0
  161. msgflux/version.py +1 -0
  162. msgflux-0.1.0.dist-info/METADATA +170 -0
  163. msgflux-0.1.0.dist-info/RECORD +165 -0
  164. msgflux-0.1.0.dist-info/WHEEL +4 -0
  165. msgflux-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,97 @@
1
+ """Bearer token authentication for MCP."""
2
+
3
+ from datetime import datetime
4
+ from typing import Awaitable, Callable, Dict, Optional
5
+
6
+ from .base import BaseAuth
7
+
8
+
9
+ class BearerTokenAuth(BaseAuth):
10
+ """Bearer token authentication (JWT, etc).
11
+
12
+ This auth provider adds an Authorization header with a Bearer token.
13
+ Supports automatic token refresh via callback.
14
+
15
+ Example:
16
+ ```python
17
+ auth = BearerTokenAuth(token="your-jwt-token")
18
+
19
+ # With auto-refresh
20
+ async def get_new_token():
21
+ # Your token refresh logic
22
+ return "new-token"
23
+
24
+ auth = BearerTokenAuth(
25
+ token="initial-token",
26
+ expires_in=3600,
27
+ refresh_callback=get_new_token
28
+ )
29
+ ```
30
+ """
31
+
32
+ def __init__(
33
+ self,
34
+ token: str,
35
+ expires_in: Optional[int] = None,
36
+ refresh_callback: Optional[Callable[[], Awaitable[str]]] = None,
37
+ token_type: str = "Bearer", # noqa: S107,
38
+ ):
39
+ """Initialize bearer token authentication.
40
+
41
+ Args:
42
+ token: The bearer token.
43
+ expires_in: Token expiration time in seconds (optional).
44
+ refresh_callback: Async callback to get a new token when expired.
45
+ token_type: Type of token (default: "Bearer").
46
+ """
47
+ super().__init__()
48
+ self._token = token
49
+ self._token_type = token_type
50
+ self._refresh_callback = refresh_callback
51
+
52
+ if expires_in:
53
+ self.set_expiration(expires_in)
54
+
55
+ def apply_auth(self, headers: Dict[str, str]) -> Dict[str, str]:
56
+ """Apply bearer token to Authorization header.
57
+
58
+ Args:
59
+ headers: Existing request headers.
60
+
61
+ Returns:
62
+ Headers with Authorization added.
63
+ """
64
+ headers = headers.copy()
65
+ headers["Authorization"] = f"{self._token_type} {self._token}"
66
+ return headers
67
+
68
+ async def refresh(self) -> bool:
69
+ """Refresh the bearer token using the callback.
70
+
71
+ Returns:
72
+ True if refresh succeeded, False otherwise.
73
+ """
74
+ if not self._refresh_callback:
75
+ return False
76
+
77
+ try:
78
+ new_token = await self._refresh_callback()
79
+ if new_token:
80
+ self._token = new_token
81
+ self._last_refresh = datetime.now() # noqa: DTZ005
82
+ return True
83
+ except Exception: # noqa: S110
84
+ pass
85
+
86
+ return False
87
+
88
+ def update_token(self, token: str, expires_in: Optional[int] = None) -> None:
89
+ """Update the bearer token.
90
+
91
+ Args:
92
+ token: New bearer token.
93
+ expires_in: New expiration time in seconds (optional).
94
+ """
95
+ self._token = token
96
+ if expires_in:
97
+ self.set_expiration(expires_in)
@@ -0,0 +1,76 @@
1
+ """Custom header authentication for MCP."""
2
+
3
+ from typing import Callable, Dict, Optional
4
+
5
+ from .base import BaseAuth
6
+
7
+
8
+ class CustomHeaderAuth(BaseAuth):
9
+ """Custom header-based authentication.
10
+
11
+ Allows arbitrary headers to be added for authentication.
12
+ Useful for proprietary auth schemes or multiple headers.
13
+
14
+ Example:
15
+ ```python
16
+ # Static headers
17
+ auth = CustomHeaderAuth({
18
+ "X-Custom-Auth": "secret-value",
19
+ "X-Request-ID": "unique-id"
20
+ })
21
+
22
+ # Dynamic headers via callback
23
+ def get_headers():
24
+ return {
25
+ "X-Signature": compute_signature(),
26
+ "X-Timestamp": str(int(time.time()))
27
+ }
28
+
29
+ auth = CustomHeaderAuth(headers_callback=get_headers)
30
+ ```
31
+ """
32
+
33
+ def __init__(
34
+ self,
35
+ headers: Optional[Dict[str, str]] = None,
36
+ headers_callback: Optional[Callable[[], Dict[str, str]]] = None,
37
+ ):
38
+ """Initialize custom header authentication.
39
+
40
+ Args:
41
+ headers: Static headers to add (optional).
42
+ headers_callback: Callback that returns headers dynamically (optional).
43
+ Takes precedence over static headers if both provided.
44
+ """
45
+ super().__init__()
46
+ self._headers = headers or {}
47
+ self._headers_callback = headers_callback
48
+
49
+ def apply_auth(self, headers: Dict[str, str]) -> Dict[str, str]:
50
+ """Apply custom headers to request.
51
+
52
+ Args:
53
+ headers: Existing request headers.
54
+
55
+ Returns:
56
+ Headers with custom auth headers added.
57
+ """
58
+ headers = headers.copy()
59
+
60
+ # Use callback if provided, otherwise use static headers
61
+ if self._headers_callback:
62
+ auth_headers = self._headers_callback()
63
+ else:
64
+ auth_headers = self._headers
65
+
66
+ # Merge auth headers
67
+ headers.update(auth_headers)
68
+ return headers
69
+
70
+ def update_headers(self, headers: Dict[str, str]) -> None:
71
+ """Update static headers.
72
+
73
+ Args:
74
+ headers: New headers to use.
75
+ """
76
+ self._headers = headers
@@ -0,0 +1,133 @@
1
+ """OAuth2 authentication for MCP."""
2
+
3
+ from datetime import datetime
4
+ from typing import Any, Awaitable, Callable, Dict, Optional
5
+
6
+ from msgflux.protocols.mcp.auth.base import BaseAuth
7
+
8
+
9
+ class OAuth2Auth(BaseAuth):
10
+ """OAuth2 authentication with automatic token refresh.
11
+
12
+ Manages OAuth2 access tokens and refresh tokens, automatically
13
+ refreshing when expired.
14
+
15
+ Example:
16
+ ```python
17
+ async def refresh_token_callback(refresh_token: str) -> dict:
18
+ # Call your OAuth2 token endpoint
19
+ response = await http_client.post(
20
+ "https://auth.example.com/oauth/token",
21
+ data={
22
+ "grant_type": "refresh_token",
23
+ "refresh_token": refresh_token,
24
+ "client_id": "your-client-id",
25
+ }
26
+ )
27
+ return {
28
+ "access_token": response["access_token"],
29
+ "refresh_token": response.get("refresh_token"),
30
+ "expires_in": response["expires_in"],
31
+ }
32
+
33
+ auth = OAuth2Auth(
34
+ access_token="initial-token",
35
+ refresh_token="refresh-token",
36
+ expires_in=3600,
37
+ refresh_callback=refresh_token_callback
38
+ )
39
+ ```
40
+ """
41
+
42
+ def __init__(
43
+ self,
44
+ access_token: str,
45
+ refresh_token: Optional[str] = None,
46
+ expires_in: Optional[int] = None,
47
+ refresh_callback: Optional[Callable[[str], Awaitable[Dict[str, Any]]]] = None,
48
+ token_type: str = "Bearer", # noqa: S107,
49
+ ):
50
+ """Initialize OAuth2 authentication.
51
+
52
+ Args:
53
+ access_token: The OAuth2 access token.
54
+ refresh_token: The refresh token (required for auto-refresh).
55
+ expires_in: Token expiration time in seconds.
56
+ refresh_callback: Async callback that takes refresh_token and returns
57
+ dict with 'access_token', 'refresh_token' (optional),
58
+ and 'expires_in'.
59
+ token_type: Type of token (default: "Bearer").
60
+ """
61
+ super().__init__()
62
+ self._access_token = access_token
63
+ self._refresh_token = refresh_token
64
+ self._token_type = token_type
65
+ self._refresh_callback = refresh_callback
66
+
67
+ if expires_in:
68
+ self.set_expiration(expires_in)
69
+
70
+ def apply_auth(self, headers: Dict[str, str]) -> Dict[str, str]:
71
+ """Apply OAuth2 access token to Authorization header.
72
+
73
+ Args:
74
+ headers: Existing request headers.
75
+
76
+ Returns:
77
+ Headers with Authorization added.
78
+ """
79
+ headers = headers.copy()
80
+ headers["Authorization"] = f"{self._token_type} {self._access_token}"
81
+ return headers
82
+
83
+ async def refresh(self) -> bool:
84
+ """Refresh the OAuth2 tokens using the refresh token.
85
+
86
+ Returns:
87
+ True if refresh succeeded, False otherwise.
88
+ """
89
+ if not self._refresh_token or not self._refresh_callback:
90
+ return False
91
+
92
+ try:
93
+ # Call refresh callback with current refresh token
94
+ result = await self._refresh_callback(self._refresh_token)
95
+
96
+ # Update tokens
97
+ if "access_token" in result:
98
+ self._access_token = result["access_token"]
99
+
100
+ # Update refresh token if provided
101
+ if "refresh_token" in result:
102
+ self._refresh_token = result["refresh_token"]
103
+
104
+ # Update expiration
105
+ if "expires_in" in result:
106
+ self.set_expiration(result["expires_in"])
107
+
108
+ self._last_refresh = datetime.now() # noqa: DTZ005
109
+ return True
110
+
111
+ except Exception: # noqa: S110
112
+ pass
113
+
114
+ return False
115
+
116
+ def update_tokens(
117
+ self,
118
+ access_token: str,
119
+ refresh_token: Optional[str] = None,
120
+ expires_in: Optional[int] = None,
121
+ ) -> None:
122
+ """Update OAuth2 tokens.
123
+
124
+ Args:
125
+ access_token: New access token.
126
+ refresh_token: New refresh token (optional).
127
+ expires_in: New expiration time in seconds (optional).
128
+ """
129
+ self._access_token = access_token
130
+ if refresh_token:
131
+ self._refresh_token = refresh_token
132
+ if expires_in:
133
+ self.set_expiration(expires_in)