flock-core 0.3.23__py3-none-any.whl → 0.3.30__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.

Potentially problematic release.


This version of flock-core might be problematic. Click here for more details.

flock/core/flock_api.py DELETED
@@ -1,214 +0,0 @@
1
- """REST API server for Flock."""
2
-
3
- import uuid
4
- from datetime import datetime
5
- from typing import Any
6
-
7
- import uvicorn
8
- from fastapi import BackgroundTasks, FastAPI, HTTPException
9
- from pydantic import BaseModel, Field
10
-
11
- from flock.core.flock import Flock
12
- from flock.core.logging.logging import get_logger
13
-
14
- logger = get_logger("api")
15
-
16
-
17
- class FlockAPIRequest(BaseModel):
18
- """Request model for running an agent."""
19
-
20
- agent_name: str = Field(..., description="Name of the agent to run")
21
- inputs: dict[str, Any] = Field(
22
- default_factory=dict, description="Input data for the agent"
23
- )
24
- async_run: bool = Field(
25
- default=False, description="Whether to run asynchronously"
26
- )
27
-
28
-
29
- class FlockAPIResponse(BaseModel):
30
- """Response model for run requests."""
31
-
32
- run_id: str = Field(..., description="Unique ID for this run")
33
- status: str = Field(..., description="Status of the run")
34
- result: dict[str, Any] | None = Field(
35
- None, description="Run result if completed"
36
- )
37
- started_at: datetime = Field(..., description="When the run started")
38
- completed_at: datetime | None = Field(
39
- None, description="When the run completed"
40
- )
41
- error: str | None = Field(None, description="Error message if failed")
42
-
43
-
44
- class FlockAPI:
45
- """REST API server for Flock.
46
-
47
- Provides HTTP endpoints for:
48
- - Running agents
49
- - Checking run status
50
- - Getting run results
51
- """
52
-
53
- def __init__(self, flock: Flock):
54
- self.flock = flock
55
- self.app = FastAPI(title="Flock API")
56
- self.runs: dict[str, FlockAPIResponse] = {}
57
-
58
- # Register routes
59
- self._setup_routes()
60
-
61
- def _setup_routes(self):
62
- """Set up API routes."""
63
-
64
- @self.app.post("/run/flock", response_model=FlockAPIResponse)
65
- async def run_flock(
66
- request: FlockAPIRequest, background_tasks: BackgroundTasks
67
- ):
68
- """Run an agent with the provided inputs."""
69
- try:
70
- # Generate run ID
71
- run_id = str(uuid.uuid4())
72
-
73
- # Create initial response
74
- response = FlockAPIResponse(
75
- run_id=run_id, status="starting", started_at=datetime.now()
76
- )
77
- self.runs[run_id] = response
78
-
79
- if request.async_run:
80
- # Start run in background
81
- background_tasks.add_task(
82
- self._run_flock,
83
- run_id,
84
- request.agent_name,
85
- request.inputs,
86
- )
87
- response.status = "running"
88
- else:
89
- # Run synchronously
90
- await self._run_flock(
91
- run_id, request.agent_name, request.inputs
92
- )
93
-
94
- return self.runs[run_id]
95
-
96
- except Exception as e:
97
- logger.error(f"Error starting run: {e!s}")
98
- raise HTTPException(status_code=500, detail=str(e))
99
-
100
- @self.app.post("/run/agent", response_model=FlockAPIResponse)
101
- async def run_agent(
102
- request: FlockAPIRequest, background_tasks: BackgroundTasks
103
- ):
104
- """Run an agent with the provided inputs."""
105
- try:
106
- # Generate run ID
107
- run_id = str(uuid.uuid4())
108
-
109
- # Create initial response
110
- response = FlockAPIResponse(
111
- run_id=run_id, status="starting", started_at=datetime.now()
112
- )
113
- self.runs[run_id] = response
114
-
115
- if request.async_run:
116
- # Start run in background
117
- background_tasks.add_task(
118
- self._run_agent,
119
- run_id,
120
- request.agent_name,
121
- request.inputs,
122
- )
123
- response.status = "running"
124
- else:
125
- # Run synchronously
126
- await self._run_agent(
127
- run_id, request.agent_name, request.inputs
128
- )
129
-
130
- return self.runs[run_id]
131
-
132
- except Exception as e:
133
- logger.error(f"Error starting run: {e!s}")
134
- raise HTTPException(status_code=500, detail=str(e))
135
-
136
- @self.app.get("/run/{run_id}", response_model=FlockAPIResponse)
137
- async def get_run_status(run_id: str):
138
- """Get the status of a run."""
139
- if run_id not in self.runs:
140
- raise HTTPException(status_code=404, detail="Run not found")
141
- return self.runs[run_id]
142
-
143
- @self.app.get("/agents")
144
- async def list_agents():
145
- """List all available agents."""
146
- return {
147
- "agents": [
148
- {
149
- "name": agent.name,
150
- "description": agent.description,
151
- "input_schema": agent.input,
152
- "output_schema": agent.output,
153
- }
154
- for agent in self.flock.agents.values()
155
- ]
156
- }
157
-
158
- async def _run_agent(
159
- self, run_id: str, agent_name: str, inputs: dict[str, Any]
160
- ):
161
- """Execute an agent run."""
162
- try:
163
- # Get the agent
164
- if agent_name not in self.flock.agents:
165
- raise ValueError(f"Agent '{agent_name}' not found")
166
-
167
- agent = self.flock.agents[agent_name]
168
-
169
- # Run the agent
170
- result = await agent.run_async(inputs)
171
-
172
- # Update run status
173
- self.runs[run_id].status = "completed"
174
- self.runs[run_id].result = result
175
- self.runs[run_id].completed_at = datetime.now()
176
-
177
- except Exception as e:
178
- logger.error(f"Error in run {run_id}: {e!s}")
179
- self.runs[run_id].status = "failed"
180
- self.runs[run_id].error = str(e)
181
- self.runs[run_id].completed_at = datetime.now()
182
-
183
- async def _run_flock(
184
- self, run_id: str, agent_name: str, inputs: dict[str, Any]
185
- ):
186
- """Execute an agent run."""
187
- try:
188
- # Get the agent
189
- if agent_name not in self.flock.agents:
190
- raise ValueError(f"Agent '{agent_name}' not found")
191
-
192
- result = await self.flock.run_async(
193
- start_agent=agent_name, input=inputs
194
- )
195
-
196
- # Update run status
197
- self.runs[run_id].status = "completed"
198
- self.runs[run_id].result = result
199
- self.runs[run_id].completed_at = datetime.now()
200
-
201
- except Exception as e:
202
- logger.error(f"Error in run {run_id}: {e!s}")
203
- self.runs[run_id].status = "failed"
204
- self.runs[run_id].error = str(e)
205
- self.runs[run_id].completed_at = datetime.now()
206
-
207
- def start(self, host: str = "0.0.0.0", port: int = 8344):
208
- """Start the API server."""
209
- uvicorn.run(self.app, host=host, port=port)
210
-
211
- async def stop(self):
212
- """Stop the API server."""
213
- # Cleanup if needed
214
- pass
@@ -1,120 +0,0 @@
1
- """Registry for storing and managing agents and tools with logging and tracing integration."""
2
-
3
- from collections.abc import Callable
4
-
5
- from opentelemetry import trace
6
-
7
- from flock.core.flock_agent import FlockAgent
8
- from flock.core.flock_module import FlockModule
9
- from flock.core.logging.logging import get_logger
10
-
11
- logger = get_logger("registry")
12
- tracer = trace.get_tracer(__name__)
13
-
14
-
15
- class Registry:
16
- """Registry for storing and managing agents and tools.
17
-
18
- This singleton class maintains a centralized registry of agents and tools,
19
- which is particularly important for Temporal workflows where only basic Python
20
- types can be passed between activities.
21
- """
22
-
23
- _instance = None
24
-
25
- def __new__(cls):
26
- with tracer.start_as_current_span("Registry.__new__") as span:
27
- if cls._instance is None:
28
- cls._instance = super().__new__(cls)
29
- cls._instance._initialize()
30
- logger.info("Registry instance created")
31
- span.set_attribute("instance.created", True)
32
- return cls._instance
33
-
34
- def _initialize(self):
35
- with tracer.start_as_current_span("Registry._initialize"):
36
- self._agents: list[FlockAgent] = []
37
- self._tools: list[tuple[str, Callable]] = []
38
- self._modules: list[FlockModule] = []
39
- logger.info("Registry initialized", agents_count=0, tools_count=0)
40
-
41
- def register_tool(self, tool_name: str, tool: Callable) -> None:
42
- with tracer.start_as_current_span("Registry.register_tool") as span:
43
- span.set_attribute("tool_name", tool_name)
44
- try:
45
- self._tools.append((tool_name, tool))
46
- logger.info("Tool registered", tool_name=tool_name)
47
- except Exception as e:
48
- logger.error(
49
- "Error registering tool", tool_name=tool_name, error=str(e)
50
- )
51
- span.record_exception(e)
52
- raise
53
-
54
- def register_agent(self, agent: FlockAgent) -> None:
55
- with tracer.start_as_current_span("Registry.register_agent") as span:
56
- span.set_attribute("agent_name", agent.name)
57
- try:
58
- self._agents.append(agent)
59
- logger.info("Agent registered", agent=agent.name)
60
- except Exception as e:
61
- logger.error(
62
- "Error registering agent", agent=agent.name, error=str(e)
63
- )
64
- span.record_exception(e)
65
- raise
66
-
67
- def get_agent(self, name: str) -> FlockAgent | None:
68
- with tracer.start_as_current_span("Registry.get_agent") as span:
69
- span.set_attribute("search_agent_name", name)
70
- try:
71
- for agent in self._agents:
72
- if agent.name == name:
73
- logger.info("Agent found", agent=name)
74
- span.set_attribute("found", True)
75
- return agent
76
- logger.warning("Agent not found", agent=name)
77
- span.set_attribute("found", False)
78
- return None
79
- except Exception as e:
80
- logger.error("Error retrieving agent", agent=name, error=str(e))
81
- span.record_exception(e)
82
- raise
83
-
84
- def get_tool(self, name: str) -> Callable | None:
85
- with tracer.start_as_current_span("Registry.get_tool") as span:
86
- span.set_attribute("search_tool_name", name)
87
- try:
88
- for tool_name, tool in self._tools:
89
- if tool_name == name:
90
- logger.info("Tool found", tool=name)
91
- span.set_attribute("found", True)
92
- return tool
93
- logger.warning("Tool not found", tool=name)
94
- span.set_attribute("found", False)
95
- return None
96
- except Exception as e:
97
- logger.error("Error retrieving tool", tool=name, error=str(e))
98
- span.record_exception(e)
99
- raise
100
-
101
- def get_tools(self, names: list[str] | None) -> list[Callable]:
102
- with tracer.start_as_current_span("Registry.get_tools") as span:
103
- span.set_attribute("search_tool_names", str(names))
104
- try:
105
- if not names:
106
- logger.info("No tool names provided")
107
- return []
108
- tools = [self.get_tool(name) for name in names]
109
- found_tools = [tool for tool in tools if tool is not None]
110
- logger.info(
111
- "Tools retrieved", requested=names, found=len(found_tools)
112
- )
113
- span.set_attribute("found_tools_count", len(found_tools))
114
- return found_tools
115
- except Exception as e:
116
- logger.error(
117
- "Error retrieving tools", names=str(names), error=str(e)
118
- )
119
- span.record_exception(e)
120
- raise