Repository navigation
Expand file tree
/
Copy pathsofa.py
More file actions
151 lines (142 loc) · 5.71 KB
/
Copy pathsofa.py
File metadata and controls
151 lines (142 loc) · 5.71 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
from __future__ import annotations
import asyncio
import httpx
from config import NV_EMBED_MODEL, SOFA_BASE, SOFA_KEY, _cached, _set_cache, get_http_client, api_error
from httpretry import retry_request
_session_id: str | None = None
_session_lock = asyncio.Lock()
async def _get_session() -> tuple[str | None, int]:
global _session_id
if not SOFA_KEY:
return None, 0
if _session_id:
return _session_id, 0
async with _session_lock:
if _session_id:
return _session_id, 0
r, retries = await retry_request(
"POST", f"{SOFA_BASE}/sessions",
headers={
"Authorization": f"Bearer {SOFA_KEY}",
"X-Sofa-Client-Name": "mcp-codesearch",
"X-Sofa-Model-Name": NV_EMBED_MODEL,
},
)
if r.status_code != 201:
_session_id = None
return None, retries
_session_id = r.json()["session_id"]
return _session_id, retries
async def search_sofa(query: str, count: int = 5, content_type: str = "question",
page: int = 1, post_id: str = "",
steering: str = "") -> dict:
if post_id:
return await _sofa_get_post(post_id)
cache_key = f"sofa:{query}:{count}:{content_type}:{page}:{steering}"
cached = await _cached(cache_key)
if cached is not None:
return cached
try:
if not SOFA_KEY:
return {"success": False, "error": "SOFA_KEY not configured", "retries": 0}
session_id, retries = await _get_session()
if not session_id:
return {"success": False, "error": "SOFA: no session", "retries": retries}
params: dict[str, str | int] = {
"search": query, "per_page": min(count, 10),
"content_type": content_type, "page": page,
}
if steering:
params["steering"] = steering
r, r_retries = await retry_request(
"GET", f"{SOFA_BASE}/posts",
params=params,
headers={"Authorization": f"Bearer {SOFA_KEY}", "X-Sofa-Session": session_id},
)
retries += r_retries
if r.status_code == 401:
_session_id = None
session_id, s_retries = await _get_session()
retries += s_retries
if not session_id:
return {"success": False, "error": "SOFA: session expired", "retries": retries}
r, r_retries = await retry_request(
"GET", f"{SOFA_BASE}/posts",
params=params,
headers={"Authorization": f"Bearer {SOFA_KEY}", "X-Sofa-Session": session_id},
)
retries += r_retries
if r.status_code != 200:
return {"success": False, "error": api_error("SOFA search:", r), "retries": retries}
data = r.json()
items = data.get("items", [])
results = []
for item in items:
results.append({
"id": item.get("id"),
"title": item.get("title", ""),
"body": (item.get("body_markdown") or item.get("body", ""))[:1000],
"tags": item.get("tags", []),
"score": item.get("score"),
"answer_count": item.get("answer_count"),
"url": item.get("public_url"),
"author": item.get("owner", {}).get("display_name") if isinstance(item.get("owner"), dict) else None,
"content_type": item.get("content_type", content_type),
})
result = {
"success": True,
"results": results,
"steering": data.get("steering", ""),
"total": data.get("total", len(results)),
"retries": retries,
}
await _set_cache(cache_key, result)
return result
except (httpx.HTTPError, ValueError, KeyError) as e:
return {"success": False, "error": str(e), "retries": 0}
async def _sofa_get_post(post_id: str) -> dict:
session_id = None
retries = 0
try:
if not SOFA_KEY:
return {"success": False, "error": "SOFA_KEY not configured", "retries": 0}
r, rr = await retry_request(
"POST", f"{SOFA_BASE}/sessions",
headers={
"Authorization": f"Bearer {SOFA_KEY}",
"X-Sofa-Client-Name": "mcp-codesearch",
},
)
retries += rr
if r.status_code != 201:
return {"success": False, "error": api_error("SOFA session:", r), "retries": retries}
session_id = r.json()["session_id"]
r, rr = await retry_request(
"GET", f"{SOFA_BASE}/posts/{post_id}",
headers={"Authorization": f"Bearer {SOFA_KEY}", "X-Sofa-Session": session_id},
)
retries += rr
if r.status_code != 200:
return {"success": False, "error": api_error("SOFA post:", r), "retries": retries}
item = r.json()
return {
"success": True,
"id": item.get("id"),
"title": item.get("title", ""),
"body": (item.get("body_markdown") or item.get("body", ""))[:3000],
"tags": item.get("tags", []),
"score": item.get("score"),
"content_type": item.get("content_type", ""),
"url": item.get("public_url"),
"retries": retries,
}
except (httpx.HTTPError, ValueError, KeyError) as e:
return {"success": False, "error": str(e), "retries": retries}
finally:
if session_id:
try:
c = get_http_client()
await c.delete(f"{SOFA_BASE}/sessions/{session_id}",
headers={"Authorization": f"Bearer {SOFA_KEY}"})
except (httpx.HTTPError, ValueError):
pass