quraite 0.1.3__py3-none-any.whl → 0.1.4__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 (49) hide show
  1. quraite/__init__.py +4 -0
  2. quraite/adapters/agno_adapter.py +50 -92
  3. quraite/adapters/base.py +26 -76
  4. quraite/adapters/bedrock_agents_adapter.py +23 -76
  5. quraite/adapters/flowise_adapter.py +31 -72
  6. quraite/adapters/google_adk_adapter.py +28 -94
  7. quraite/adapters/http_adapter.py +28 -44
  8. quraite/adapters/langchain_adapter.py +51 -118
  9. quraite/adapters/langchain_server_adapter.py +37 -89
  10. quraite/adapters/langflow_adapter.py +15 -60
  11. quraite/adapters/n8n_adapter.py +19 -63
  12. quraite/adapters/openai_agents_adapter.py +35 -59
  13. quraite/adapters/pydantic_ai_adapter.py +27 -97
  14. quraite/adapters/smolagents_adapter.py +21 -82
  15. quraite/constants/framework.py +14 -0
  16. quraite/schema/__init__.py +4 -0
  17. quraite/schema/invoke.py +46 -0
  18. quraite/schema/message.py +20 -21
  19. quraite/serve/__init__.py +4 -0
  20. quraite/serve/cloudflared.py +3 -2
  21. quraite/serve/server.py +305 -0
  22. quraite/tracing/__init__.py +8 -5
  23. quraite/tracing/constants.py +0 -14
  24. quraite/tracing/setup.py +129 -0
  25. quraite/tracing/span_exporter.py +6 -6
  26. quraite/tracing/span_processor.py +6 -7
  27. quraite/tracing/tool_extractors.py +1 -1
  28. quraite/tracing/trace.py +36 -24
  29. quraite/utils/json_utils.py +2 -2
  30. {quraite-0.1.3.dist-info → quraite-0.1.4.dist-info}/METADATA +54 -62
  31. quraite-0.1.4.dist-info/RECORD +37 -0
  32. quraite/schema/response.py +0 -16
  33. quraite/serve/local_agent.py +0 -361
  34. quraite/traces/traces_adk_openinference.json +0 -379
  35. quraite/traces/traces_agno_multi_agent.json +0 -669
  36. quraite/traces/traces_agno_openinference.json +0 -321
  37. quraite/traces/traces_crewai_openinference.json +0 -155
  38. quraite/traces/traces_langgraph_openinference.json +0 -349
  39. quraite/traces/traces_langgraph_openinference_multi_agent.json +0 -2705
  40. quraite/traces/traces_langgraph_traceloop.json +0 -510
  41. quraite/traces/traces_openai_agents_multi_agent_1.json +0 -402
  42. quraite/traces/traces_openai_agents_openinference.json +0 -341
  43. quraite/traces/traces_pydantic_openinference.json +0 -286
  44. quraite/traces/traces_pydantic_openinference_multi_agent_1.json +0 -399
  45. quraite/traces/traces_pydantic_openinference_multi_agent_2.json +0 -398
  46. quraite/traces/traces_smol_agents_openinference.json +0 -397
  47. quraite/traces/traces_smol_agents_tool_calling_openinference.json +0 -704
  48. quraite-0.1.3.dist-info/RECORD +0 -49
  49. {quraite-0.1.3.dist-info → quraite-0.1.4.dist-info}/WHEEL +0 -0
@@ -1,361 +0,0 @@
1
- import asyncio
2
- import os
3
- from contextlib import asynccontextmanager
4
- from typing import List, Literal, Optional
5
-
6
- import httpx
7
- import uvicorn
8
- from fastapi import FastAPI, HTTPException
9
- from fastapi.middleware.cors import CORSMiddleware
10
- from pydantic import BaseModel
11
-
12
- from quraite.adapters.base import BaseAdapter
13
- from quraite.logger import get_logger
14
- from quraite.schema.message import AgentMessage
15
- from quraite.schema.response import AgentInvocationResponse
16
-
17
- logger = get_logger(__name__)
18
-
19
-
20
- class InvokeRequest(BaseModel):
21
- """
22
- Request model for agent invocation endpoints.
23
-
24
- Attributes:
25
- input: List of AgentMessage objects
26
- session_id: Optional conversation thread identifier
27
- """
28
-
29
- input: List[AgentMessage]
30
- session_id: Optional[str] = None
31
-
32
-
33
- class InvokeResponse(BaseModel):
34
- """
35
- Response model for agent invocation endpoints.
36
-
37
- Attributes:
38
- agent_response: AgentInvocationResponse object representing agent responses
39
- """
40
-
41
- agent_response: Optional[AgentInvocationResponse] = None
42
-
43
-
44
- class LocalAgentServer:
45
- """
46
- SDK for creating local agent servers that expose agents via HTTP.
47
-
48
- Usage:
49
- ```python
50
- from quraite.serve.local_agent_server import LocalAgentServer
51
- from quraite.adapters import LangChainAdapter
52
-
53
- sdk = LocalAgentServer(wrapped_agent=LangChainAdapter(agent_graph=agent_graph))
54
- sdk.start(host="0.0.0.0", port=8000, reload=False)
55
- ```
56
- """
57
-
58
- def __init__(
59
- self,
60
- wrapped_agent: BaseAdapter = None,
61
- agent_id: Optional[str] = None,
62
- ):
63
- """
64
- Initialize the Local Agent Server SDK.
65
-
66
- Args:
67
- wrapped_agent: Optional pre-wrapped local agent instance to register immediately
68
- agent_id: Optional Quraite platform agent ID. Falls back to QURAITE_AGENT_ID env var.
69
- quraite_endpoint: Optional Quraite endpoint for updating agent config. Falls back to QURAITE_ENDPOINT env var.
70
- """
71
-
72
- self._agent = wrapped_agent
73
- self.public_url = None
74
- self._tunnel = None
75
- self.agent_id = agent_id or os.getenv("QURAITE_AGENT_ID")
76
- self._quraite_endpoint = (
77
- os.getenv("QURAITE_ENDPOINT") or "https://api.quraite.ai"
78
- )
79
- self.agent_url = None
80
- # Tunnel configuration (set when create_app is called with tunnel params)
81
- self._tunnel_config = None
82
-
83
- if self._agent is None:
84
- raise RuntimeError("No local agent provided. Please provide a local agent.")
85
-
86
- def _setup_tunnel_sync(
87
- self,
88
- port: int,
89
- host: str = "0.0.0.0",
90
- tunnel: Literal["ngrok", "cloudflare"] = "cloudflare",
91
- ):
92
- """Synchronous tunnel setup (called from async context)."""
93
- # Prevent creating multiple tunnels if one already exists
94
- if self._tunnel is not None:
95
- return
96
-
97
- if tunnel == "ngrok":
98
- # TODO: Add debug info if ngrok fails to connect or auth token is not set
99
-
100
- try:
101
- from pyngrok import ngrok
102
- except ImportError as e:
103
- raise ImportError(
104
- "Failed to import pyngrok. Please install the 'pyngrok' optional dependency: pip install 'quraite[pyngrok]'"
105
- ) from e
106
-
107
- try:
108
- ngrok_tunnel = ngrok.connect(port)
109
- self.public_url = ngrok_tunnel.public_url
110
- self._tunnel = ngrok_tunnel
111
- logger.info("Ngrok tunnel established: %s", self.public_url)
112
- except Exception as e:
113
- logger.error(
114
- "Failed to create ngrok tunnel: %s. "
115
- "Make sure ngrok is installed and authenticated: https://ngrok.com/download",
116
- e,
117
- )
118
- raise
119
-
120
- elif tunnel == "cloudflare":
121
- from quraite.serve.cloudflared import connect
122
-
123
- cloudflared_tunnel = connect(
124
- port, host=host if host != "0.0.0.0" else "localhost"
125
- )
126
- self.public_url = cloudflared_tunnel.public_url
127
- self._tunnel = cloudflared_tunnel
128
- logger.info("Cloudflare tunnel established: %s", self.public_url)
129
-
130
- self.agent_url = self.public_url
131
-
132
- async def start(
133
- self,
134
- host: str = "0.0.0.0",
135
- port: int = 8000,
136
- tunnel: Literal["none", "ngrok", "cloudflare"] = "none",
137
- **uvicorn_kwargs,
138
- ):
139
- """
140
- Start the local agent server.
141
- """
142
-
143
- if tunnel == "none":
144
- self.agent_url = f"http://{host}:{port}"
145
-
146
- app = self.create_app(port=port, host=host, tunnel=tunnel)
147
-
148
- loop = asyncio.get_event_loop()
149
- if loop.is_running():
150
- config = uvicorn.Config(app, host=host, port=port, **uvicorn_kwargs)
151
- server = uvicorn.Server(config)
152
- await server.serve()
153
- else:
154
- uvicorn.run(app, host=host, port=port, **uvicorn_kwargs)
155
-
156
- async def _update_backend_agent_url(self) -> None:
157
- """
158
- Update the backend server with the agent URL configuration.
159
-
160
- Makes a PATCH request to /agents/{agent_id}/config/url to update
161
- the agent's URL in the Quraite platform. Sends the full URL including
162
- the /v1/agents/completions path.
163
- """
164
- if not self.agent_id or not self._quraite_endpoint or not self.agent_url:
165
- return
166
-
167
- quraite_endpoint = self._quraite_endpoint.rstrip("/")
168
- endpoint = f"{quraite_endpoint}/agents/{self.agent_id}/config/url"
169
- # Construct full URL with path
170
- full_url = f"{self.agent_url.rstrip('/')}/v1/agents/completions"
171
- payload = {"config": {"url": full_url}}
172
-
173
- try:
174
- async with httpx.AsyncClient(timeout=10.0) as client:
175
- response = await client.patch(endpoint, json=payload)
176
- response.raise_for_status()
177
- logger.info("Agent URL registered with Quraite platform: %s", full_url)
178
- except httpx.HTTPStatusError as e:
179
- logger.warning(
180
- "Failed to update agent URL in Quraite platform: HTTP %s - %s. Update manually with URL: %s",
181
- e.response.status_code,
182
- e.response.text,
183
- full_url,
184
- )
185
- except httpx.RequestError as e:
186
- logger.warning(
187
- "Failed to connect to Quraite backend at %s: %s",
188
- quraite_endpoint,
189
- e,
190
- )
191
- except Exception as e:
192
- logger.warning("Unexpected error updating agent URL: %s", e)
193
-
194
- def create_app(
195
- self,
196
- port: Optional[int] = None,
197
- host: str = "0.0.0.0",
198
- tunnel: Literal["none", "ngrok", "cloudflare"] = "none",
199
- ) -> FastAPI:
200
- """
201
- Create FastAPI app with local agent invocation endpoints.
202
-
203
- Args:
204
- port: Optional port number (for tunnel setup)
205
- host: Host address (for tunnel setup)
206
- tunnel: Tunnel type to use ("none", "ngrok", or "cloudflare")
207
-
208
- Returns:
209
- FastAPI application instance
210
- """
211
-
212
- @asynccontextmanager
213
- async def lifespan(app: FastAPI):
214
- # Startup: Set up tunnel if requested
215
- if tunnel != "none" and port is not None:
216
- logger.info("Setting up %s tunnel on port %s...", tunnel, port)
217
- # Run tunnel setup in thread pool since it's blocking
218
- loop = asyncio.get_event_loop()
219
- await loop.run_in_executor(
220
- None, self._setup_tunnel_sync, port, host, tunnel
221
- )
222
- # Update backend after tunnel is created
223
- if self.agent_id and self._quraite_endpoint:
224
- await self._update_backend_agent_url()
225
- elif tunnel == "none" and port is not None:
226
- self.agent_url = f"http://{host}:{port}"
227
-
228
- logger.info("Local Agent Server started successfully")
229
- if self.public_url:
230
- path = "/v1/agents/completions"
231
- logger.info(f"Agent publicly available at {self.public_url}{path}")
232
-
233
- if not self.agent_id or not self._quraite_endpoint:
234
- logger.info(
235
- f"Add this URL to your agent in the Quraite platform: {self.public_url}{path}"
236
- )
237
- else:
238
- logger.info(
239
- f"Agent running locally and available at {self.agent_url}{path}. Use a tunnel option to make it publicly available."
240
- )
241
- yield
242
-
243
- # Shutdown: Clean up tunnel
244
- if self._tunnel is not None:
245
- logger.info("Closing %s tunnel...", tunnel)
246
- if tunnel == "ngrok":
247
- try:
248
- from pyngrok import ngrok
249
-
250
- ngrok.disconnect(self._tunnel.public_url)
251
- ngrok.kill()
252
- logger.info("Ngrok tunnel closed")
253
- except Exception as e:
254
- logger.warning("Error closing ngrok tunnel: %s", e)
255
- elif tunnel == "cloudflare":
256
- try:
257
- loop = asyncio.get_event_loop()
258
- if hasattr(self._tunnel, "disconnect"):
259
- await loop.run_in_executor(None, self._tunnel.disconnect)
260
- elif hasattr(self._tunnel, "stop"):
261
- await loop.run_in_executor(None, self._tunnel.stop)
262
- elif hasattr(self._tunnel, "close"):
263
- await loop.run_in_executor(None, self._tunnel.close)
264
- logger.info("Cloudflare tunnel closed")
265
- except Exception as e:
266
- logger.warning("Error closing cloudflare tunnel: %s", e)
267
-
268
- app = FastAPI(title="Quraite Local Agent Server", lifespan=lifespan)
269
-
270
- # Add CORS middleware
271
- app.add_middleware(
272
- CORSMiddleware,
273
- allow_origins=["*"],
274
- allow_credentials=True,
275
- allow_methods=["*"],
276
- allow_headers=["*"],
277
- )
278
-
279
- # Health check endpoint
280
- @app.get("/")
281
- def health_check():
282
- """Health check endpoint."""
283
- return {
284
- "status": "ok",
285
- "message": "Local Agent Server is running",
286
- "local_agent_registered": self._agent is not None,
287
- }
288
-
289
- @app.post(
290
- "/v1/agents/completions",
291
- response_model=InvokeResponse,
292
- tags=["agent_invocation"],
293
- )
294
- async def invoke_agent(request: InvokeRequest) -> InvokeResponse:
295
- """
296
- Agent invocation endpoint (async by default).
297
-
298
- This endpoint receives an invocation request, deserializes it,
299
- invokes the registered agent asynchronously, and returns the serialized response.
300
-
301
- Args:
302
- request: InvokeRequest containing input and parameters
303
-
304
- Returns:
305
- InvokeResponse with list of serialized messages
306
-
307
- Raises:
308
- HTTPException 400: If no agent is registered
309
- HTTPException 422: If request format is invalid
310
- HTTPException 500: If agent invocation fails
311
-
312
- Example:
313
- ```python
314
- POST /v1/agents/completions
315
- {
316
- "input": [{
317
- "role": "user",
318
- "content": [{
319
- "type": "text",
320
- "text": "Hello"
321
- }]
322
- }],
323
- "session_id": "session_123",
324
- }
325
- ```
326
- """
327
- if not self._agent:
328
- raise HTTPException(
329
- status_code=400,
330
- detail="No agent registered. Please register an agent before invoking.",
331
- )
332
-
333
- try:
334
- # Invoke agent (asynchronously)
335
- response: AgentInvocationResponse = await self._agent.ainvoke(
336
- input=request.input,
337
- session_id=request.session_id,
338
- )
339
-
340
- return InvokeResponse(agent_response=response)
341
-
342
- except HTTPException:
343
- raise
344
- except Exception as e:
345
- raise HTTPException(
346
- status_code=500,
347
- detail=f"Agent invocation failed: {str(e)}",
348
- ) from e
349
-
350
- return app
351
-
352
-
353
- if __name__ == "__main__":
354
- import asyncio
355
-
356
- from quraite.adapters.base import DummyAdapter
357
-
358
- server = LocalAgentServer(wrapped_agent=DummyAdapter())
359
- asyncio.run(
360
- server.start(host="0.0.0.0", port=8000, reload=False, tunnel="cloudflare")
361
- )