-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissue_api.py
More file actions
155 lines (138 loc) · 6.25 KB
/
Copy pathissue_api.py
File metadata and controls
155 lines (138 loc) · 6.25 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
from fastapi import APIRouter, HTTPException, Query
from models import Issue
from memory_store import save_issue, get_issue, get_all_issues, search_issues
from llm_clients.mock import MockLLMClient
import logging, asyncio
router = APIRouter()
# Can be replaced with a real client
llm_client = MockLLMClient()
# Receive issue events from external systems
@router.post("/events")
def receive_event(issue: Issue):
logging.info(f"Received issue: {issue.id}")
save_issue(issue)
return {"status": "received"}
# List all issues, with optional text search
@router.get("/issues")
def list_issues(search: str = Query(None)):
return search_issues(search) if search else get_all_issues(active_only=True)
# Retrieve specific isue details
@router.get("/issues/{issue_id}")
def issue_detail(issue_id: str):
issue = get_issue(issue_id)
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
return issue
# Trigger LLM analysis with retry and timeout
@router.post("/analyze/{issue_id}")
async def analyze(issue_id: str):
# Retrieve the issue from the in-memory store
issue = get_issue(issue_id)
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
# Retry logic: attempt LLM analysis up to 3 times with timeout
for attempt in range(3):
try:
# Call the mock LLM client with a timeout to simulate real-world latency
result = await asyncio.wait_for(llm_client.analyze_issue(issue), timeout=3.0)
# Update issue fields with LLM response
issue.labels = result["labels"]
issue.assignedTo = result["assignedTo"]
issue.confidence = result["confidence"]
issue.priority = result.get("priority")
# Save updated issue back to store
save_issue(issue)
return result
except asyncio.TimeoutError:
# Log timeout event with retry count
logging.warning(f"Analyze timeout for issue {issue_id}, attempt {attempt+1}")
# Raise error if all retries fail
raise HTTPException(status_code=500, detail="LLM analyze failed after retries")
issue = get_issue(issue_id)
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
for attempt in range(3):
try:
result = await asyncio.wait_for(llm_client.analyze_issue(issue), timeout=3.0)
issue.labels = result["labels"]
issue.assignedTo = result["assignedTo"]
issue.confidence = result["confidence"]
issue.priority = result.get("priority")
save_issue(issue)
return result
except asyncio.TimeoutError:
logging.warning(f"Analyze timeout for issue {issue_id}, attempt {attempt+1}")
raise HTTPException(status_code=500, detail="LLM analyze failed after retries")
# Trigger LLM plan generation with retry and timeout
@router.post("/plan/{issue_id}")
async def plan(issue_id: str):
# Retrieve the issue from the in-memory store
issue = get_issue(issue_id)
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
# Retry logic: attempt LLM plan generation up to 3 times
for attempt in range(3):
try:
# Simulate latency and failure using mock LLMClient with timeout
result = await asyncio.wait_for(llm_client.plan_issue(issue), timeout=3.0)
# Store the generated plan in the issue
issue.plan = result["plan"]
save_issue(issue)
return result
except asyncio.TimeoutError:
# Log timeout per retry to help with debugging
logging.warning(f"Plan timeout for issue {issue_id}, attempt {attempt+1}")
# Final failure after retries
raise HTTPException(status_code=500, detail="LLM plan failed after retries")
issue = get_issue(issue_id)
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
for attempt in range(3):
try:
result = await asyncio.wait_for(llm_client.plan_issue(issue), timeout=3.0)
issue.plan = result["plan"]
save_issue(issue)
return result
except asyncio.TimeoutError:
logging.warning(f"Plan timeout for issue {issue_id}, attempt {attempt+1}")
raise HTTPException(status_code=500, detail="LLM plan failed after retries")
# Trigger LLM analysis with retry and timeout
@router.post("/analyze/{issue_id}")
async def analyze(issue_id: str):
# Retrieve the issue from the in-memory store
issue = get_issue(issue_id)
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
# Retry logic: attempt LLM analysis up to 3 times with timeout
for attempt in range(3):
try:
# Call the mock LLM client with a timeout to simulate real-world latency
result = await asyncio.wait_for(llm_client.analyze_issue(issue), timeout=3.0)
# Update issue fields with LLM response
issue.labels = result["labels"]
issue.assignedTo = result["assignedTo"]
issue.confidence = result["confidence"]
issue.priority = result.get("priority")
# Save updated issue back to store
save_issue(issue)
return result
except asyncio.TimeoutError:
# Log timeout event with retry count
logging.warning(f"Analyze timeout for issue {issue_id}, attempt {attempt+1}")
# Raise error if all retries fail
raise HTTPException(status_code=500, detail="LLM analyze failed after retries")
issue = get_issue(issue_id)
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
for attempt in range(3):
try:
result = await asyncio.wait_for(llm_client.analyze_issue(issue), timeout=3.0)
issue.labels = result["labels"]
issue.assignedTo = result["assignedTo"]
issue.confidence = result["confidence"]
issue.priority = result.get("priority")
save_issue(issue)
return result
except asyncio.TimeoutError:
logging.warning(f"Analyze timeout for issue {issue_id}, attempt {attempt+1}")
raise HTTPException(status_code=500, detail="LLM analyze failed after retries")