jira-webhook-llm/app/handlers.py

68 lines
2.5 KiB
Python

from datetime import datetime, timezone
from fastapi import APIRouter, Request, HTTPException, Depends
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from llm.models import JiraWebhookPayload
from shared_store import requests_queue, ProcessingRequest
from loguru import logger
router = APIRouter(
prefix="/api",
tags=["API"]
)
webhook_router = APIRouter(
prefix="/webhooks",
tags=["Webhooks"]
)
@router.post("/jira_webhook", status_code=201)
async def receive_jira_webhook(payload: JiraWebhookPayload):
"""Handle incoming Jira webhook and store request"""
request_id = requests_queue.add_request(payload.model_dump())
return {"request_id": request_id}
@router.get("/pending_requests")
async def get_pending_requests():
"""Return all pending requests"""
all_requests = requests_queue.get_all_requests()
pending = [req for req in all_requests if req.status == "pending"]
return {"requests": pending}
@router.delete("/requests/{request_id}")
async def delete_specific_request(request_id: int):
"""Delete specific request by ID"""
if requests_queue.delete_request_by_id(request_id):
return {"deleted": True}
raise HTTPException(status_code=404, detail="Request not found")
@router.delete("/requests")
async def delete_all_requests():
"""Clear all requests"""
requests_queue.clear_all_requests()
return {"status": "cleared"}
@router.get("/requests/{request_id}/response")
async def get_request_response(request_id: int):
"""Get response for specific request"""
matched_request = requests_queue.get_request_by_id(request_id)
if not matched_request:
raise HTTPException(status_code=404, detail="Request not found")
return matched_request.response if matched_request.response else "No response yet"
@webhook_router.post("/jira")
async def handle_jira_webhook():
return {"status": "webhook received"}
@webhook_router.post("/ollama")
async def handle_ollama_webhook(request: Request):
"""Handle incoming Ollama webhook and capture raw output"""
try:
raw_body = await request.body()
response_data = raw_body.decode('utf-8')
logger.info(f"Received raw Ollama webhook response: {response_data}")
# Here you would process the raw_body, e.g., store it or pass it to another component
return {"status": "ollama webhook received", "data": response_data}
except Exception as e:
logger.error(f"Error processing Ollama webhook: {e}")
raise HTTPException(status_code=500, detail=f"Error processing webhook: {e}")