-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathstart_tool_server.py
More file actions
546 lines (455 loc) · 16 KB
/
start_tool_server.py
File metadata and controls
546 lines (455 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# Copyright 2025 Pokee AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Tool Server Main Module
FastAPI server providing web reading and search capabilities
with caching and graceful shutdown handling.
"""
import argparse
import atexit
import signal
from contextlib import asynccontextmanager
from pathlib import Path
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from logging_utils import setup_colored_logger
from tool_server.cache_manager import CacheManager
from tool_server.read import ReadResult, WebReadAgent
from tool_server.search import SearchResult, WebSearchAgent
load_dotenv()
# Create logs directory if it doesn't exist
log_dir = Path("logs")
try:
log_dir.mkdir(exist_ok=True)
log_file = log_dir / "tool_server.log"
logger = setup_colored_logger("tool_server", log_file=log_file)
except Exception as e:
# Fallback to console-only logging if log file creation fails
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("tool_server")
logger.error(f"Failed to create log directory/file: {e}")
logger.info("Falling back to console-only logging")
class SearchRequest(BaseModel):
"""Request model for web search."""
query: str
class ReadRequest(BaseModel):
"""Request model for reading a webpage."""
url: str
question: str
# Parse command-line arguments
argparser = argparse.ArgumentParser(description="Tool Server for web operations")
# search args
argparser.add_argument(
"--search-timeout", type=int, default=30, help="Default search timeout in seconds"
)
argparser.add_argument(
"--max-concurrent-search-requests",
type=int,
default=300,
help="Max concurrent search requests per agent",
)
argparser.add_argument(
"--top-k", type=int, default=10, help="Default number of search results to return"
)
# read args
argparser.add_argument(
"--read-timeout", type=int, default=30, help="Default read timeout in seconds"
)
argparser.add_argument(
"--max-concurrent-read-requests",
type=int,
default=500,
help="Max concurrent read requests per agent",
)
argparser.add_argument(
"--max-content-words",
type=int,
default=10000,
help="Max words to extract from webpage content",
)
argparser.add_argument(
"--max-summary-words",
type=int,
default=2048,
help="Max words in the generated summary",
)
argparser.add_argument(
"--max-summary-retries",
type=int,
default=3,
help="Max retries for summary generation on failure",
)
# cache args
argparser.add_argument(
"--cache-dir",
type=str,
default="cache",
help="Directory to store cache files",
)
argparser.add_argument(
"--cache-save-frequency",
type=int,
default=100,
help="Save cache to disk every N operations",
)
argparser.add_argument(
"--cache-stats-interval",
type=int,
default=1200,
help="Report cache statistics every N seconds (0 to disable)",
)
argparser.add_argument(
"--enable-cache",
action="store_true",
help="Enable caching for search and read operations",
)
# server args
argparser.add_argument(
"--port",
type=int,
default=8888,
help="Port to run the server on",
)
args = argparser.parse_args()
# Initialize agents with configuration
search_config = {
"timeout": args.search_timeout,
"top_k": args.top_k,
"max_concurrent_requests": args.max_concurrent_search_requests,
}
read_config = {
"timeout": args.read_timeout,
"max_concurrent_requests": args.max_concurrent_read_requests,
"max_content_words": args.max_content_words,
"max_summary_words": args.max_summary_words,
"max_summary_retries": args.max_summary_retries,
}
# Initialize cache manager
cache_manager = None
if args.enable_cache:
try:
cache_manager = CacheManager(
cache_dir=args.cache_dir,
search_config=search_config,
read_config=read_config,
save_frequency=args.cache_save_frequency,
stats_report_interval=args.cache_stats_interval,
)
logger.info("Cache manager enabled")
except Exception as e:
logger.error(f"Failed to initialize cache manager: {e}", exc_info=True)
logger.warning("Continuing without cache support")
cache_manager = None
else:
logger.info("Cache manager disabled")
try:
search_agent = WebSearchAgent(config=search_config)
read_agent = WebReadAgent(config=read_config)
except Exception as e:
logger.error(f"Failed to initialize agents: {e}", exc_info=True)
raise
def cleanup_handler(signum=None, frame=None):
"""Handle cleanup on shutdown signals"""
if signum is not None:
signal_name = signal.Signals(signum).name
logger.info(
f"Received signal {signal_name} ({signum}), initiating graceful shutdown..."
)
else:
logger.info("Initiating graceful shutdown...")
if cache_manager:
try:
logger.info("Flushing cache to disk...")
cache_manager.shutdown()
logger.info("Cache successfully flushed to disk")
except Exception as e:
logger.error(f"Error during cache shutdown: {e}", exc_info=True)
logger.info("Cleanup complete")
# Register signal handlers for graceful shutdown
def setup_signal_handlers():
"""Setup signal handlers for graceful shutdown"""
# Handle Ctrl+C (SIGINT) and kill (SIGTERM)
signal.signal(signal.SIGINT, cleanup_handler)
signal.signal(signal.SIGTERM, cleanup_handler)
# Register atexit handler as fallback
atexit.register(cleanup_handler)
logger.debug("Signal handlers registered for graceful shutdown")
# Setup signal handlers
try:
setup_signal_handlers()
except Exception as e:
logger.error(f"Failed to setup signal handlers: {e}", exc_info=True)
logger.warning("Continuing without signal handlers")
@asynccontextmanager
async def lifespan(app: FastAPI):
# --- Startup ---
try:
logger.info("=" * 60)
logger.info("Tool Server Starting")
logger.info("=" * 60)
logger.info("Server Configuration:")
logger.info(f" Port: {args.port}")
logger.info(" Authentication: Disabled")
logger.info(f" Cache: {'Enabled' if args.enable_cache else 'Disabled'}")
logger.info("")
if args.enable_cache:
logger.info("Cache Configuration:")
logger.info(f" Cache Directory: {args.cache_dir}")
logger.info(
f" Save Frequency: every {args.cache_save_frequency} operations"
)
if args.cache_stats_interval > 0:
logger.info(
f" Stats Report Interval: every {args.cache_stats_interval} seconds"
)
else:
logger.info(" Stats Report: Disabled")
logger.info("")
logger.info("Search Agent Configuration:")
logger.info(f" Max Concurrent Requests: {args.max_concurrent_search_requests}")
logger.info(f" Timeout: {args.search_timeout}s")
logger.info(f" Top K Results: {args.top_k}")
logger.info("")
logger.info("Read Agent Configuration:")
logger.info(f" Max Concurrent Requests: {args.max_concurrent_read_requests}")
logger.info(f" Timeout: {args.read_timeout}s")
logger.info(f" Max Content Words: {args.max_content_words}")
logger.info(f" Max Summary Words: {args.max_summary_words}")
logger.info(f" Max Summary Retries: {args.max_summary_retries}")
logger.info("=" * 60)
logger.info(f"Tool Server ready at http://0.0.0.0:{args.port}")
logger.info("=" * 60)
except Exception as e:
logger.error(f"Error during startup logging: {e}", exc_info=True)
# Continue anyway - logging errors shouldn't prevent startup
try:
yield # <-- run the app
except Exception as e:
logger.error(f"Error during app lifecycle: {e}", exc_info=True)
raise
finally:
# --- Shutdown ---
try:
logger.info("=" * 60)
logger.info("Tool Server shutting down...")
logger.info("=" * 60)
# Shutdown cache manager
if cache_manager:
logger.info("Shutting down cache manager...")
try:
cache_manager.shutdown()
logger.info("Cache manager shutdown complete")
except Exception as e:
logger.error(
f"Error shutting down cache manager: {e}", exc_info=True
)
except Exception as e:
logger.error(f"Error during shutdown logging: {e}", exc_info=True)
# FastAPI app
app = FastAPI(
title="Web Tools API",
description="AI-powered web reading and search service with caching",
version="1.0.0",
lifespan=lifespan,
)
@app.get("/")
async def root():
"""Root endpoint with service information"""
return {
"message": "Tool Server is running",
"status": "healthy",
"services": ["read", "search"],
"cache_enabled": args.enable_cache,
"version": "1.0.0",
}
@app.get("/health")
async def health_check():
"""Health check endpoint"""
health_data = {
"status": "healthy",
"service": "Tool Server",
"agents": {
"read": "operational",
"search": "operational",
},
}
# Add cache stats if enabled
if cache_manager:
health_data["cache"] = cache_manager.get_stats()
return health_data
@app.get("/cache/stats")
async def cache_stats():
"""Get cache statistics"""
if not cache_manager:
raise HTTPException(
status_code=400,
detail="Cache is not enabled. Start server with --enable-cache",
)
return cache_manager.get_stats()
@app.post("/cache/flush")
async def cache_flush():
"""Force flush all caches to disk"""
if not cache_manager:
raise HTTPException(
status_code=400,
detail="Cache is not enabled. Start server with --enable-cache",
)
cache_manager.flush_all()
return {"message": "All caches flushed to disk", "success": True}
@app.post("/cache/clear")
async def cache_clear():
"""Clear all caches (use with caution!)"""
if not cache_manager:
raise HTTPException(
status_code=400,
detail="Cache is not enabled. Start server with --enable-cache",
)
cache_manager.clear_all()
return {"message": "All caches cleared", "success": True}
@app.post("/cache/invalidate")
async def cache_invalidate(max_age_days: int = 30):
"""Invalidate cache entries older than specified days"""
if not cache_manager:
raise HTTPException(
status_code=400,
detail="Cache is not enabled. Start server with --enable-cache",
)
removed = cache_manager.invalidate_old_entries(max_age_days=max_age_days)
return {
"message": f"Removed {removed} entries older than {max_age_days} days",
"removed_count": removed,
"success": True,
}
@app.post("/search", response_model=SearchResult)
async def search_web(request: SearchRequest) -> SearchResult:
"""
Perform web search and return structured results.
This endpoint uses the Serper API to perform web searches and returns
structured results with URLs, titles, and descriptions.
Args:
request: SearchRequest containing query
Returns:
SearchResult with search results and metadata
Raises:
HTTPException: If search fails with status 500
"""
try:
logger.debug(f"Search request: {request.query}")
# Check cache first
if cache_manager:
cached_result = cache_manager.get_search(request.query)
if cached_result:
logger.debug(f"Returning cached search result for: {request.query}")
return SearchResult(**cached_result)
# Perform search
result = await search_agent.search(query=request.query)
# Cache successful results
if cache_manager and result.success:
cache_manager.set_search(request.query, result.model_dump())
if not result.success:
logger.warning(
f"Search failed for '{request.query}': {result.error}",
)
return result
except Exception as e:
logger.error(f"Error searching for '{request.query}': {str(e)}", exc_info=True)
raise HTTPException(
status_code=500,
detail={
"error": str(e),
"success": False,
"query": request.query,
},
)
@app.post("/read", response_model=ReadResult)
async def read_webpage(request: ReadRequest) -> ReadResult:
"""
Read and analyze a webpage with LLM-powered summarization.
This endpoint fetches webpage content and generates a context-aware summary
based on the provided question.
Args:
request: ReadRequest containing url and question
Returns:
ReadResult with content, summary, and discovered URLs
Raises:
HTTPException: If reading fails with status 500
"""
try:
logger.debug(f"Read request: {request.url} - {request.question[:50]}...")
# Check cache first
if cache_manager:
cached_result = cache_manager.get_read(request.url, request.question)
if cached_result:
logger.debug(f"Returning cached read result for: {request.url}")
return ReadResult(**cached_result)
# Perform read
result = await read_agent.read(
question=request.question,
url=request.url,
)
# Cache successful results
if cache_manager and result.success:
cache_manager.set_read(request.url, request.question, result.model_dump())
if not result.success:
logger.warning(
f"Read failed for {request.url}: {result.error}",
)
return result
except Exception as e:
logger.error(f"Error reading {request.url}: {str(e)}", exc_info=True)
raise HTTPException(
status_code=500,
detail={
"error": str(e),
"success": False,
"url": request.url,
},
)
if __name__ == "__main__":
import uvicorn
logger.info(f"Starting Tool Server on port {args.port}")
# Write the actual port to a file that clients can read
port_file = Path(".server_port")
try:
port_file.write_text(str(args.port))
logger.info(f"Server port {args.port} written to {port_file}")
except Exception as e:
logger.error(f"Could not write port file: {e}", exc_info=True)
try:
uvicorn.run(
app,
host="0.0.0.0",
port=args.port,
log_level="info",
access_log=True,
)
except KeyboardInterrupt:
logger.info("Received keyboard interrupt")
except Exception as e:
logger.error(f"Server error: {e}", exc_info=True)
raise # Re-raise to ensure proper exit code
finally:
try:
cleanup_handler()
except Exception as e:
logger.error(f"Error during cleanup: {e}", exc_info=True)
# Clean up port file on shutdown
if port_file.exists():
try:
port_file.unlink()
logger.debug("Port file cleaned up")
except Exception as e:
logger.warning(f"Could not delete port file on shutdown: {e}")