-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_client.py
More file actions
424 lines (362 loc) · 15.6 KB
/
Copy pathapi_client.py
File metadata and controls
424 lines (362 loc) · 15.6 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
"""
CodeAlive API Client
Handles authentication and HTTP requests to the CodeAlive API.
"""
import os
import urllib.parse
import sys
import json
import urllib.request
import urllib.error
import urllib.parse
from typing import Optional, Dict, Any, List
class CodeAliveClient:
"""Client for interacting with the CodeAlive API."""
@staticmethod
def _get_key_from_keychain() -> Optional[str]:
"""Try to read the API key from OS credential store."""
import platform
system = platform.system()
try:
if system == "Darwin":
import subprocess
result = subprocess.run(
["security", "find-generic-password", "-a", os.getenv("USER", ""), "-s", "codealive-api-key", "-w"],
capture_output=True, text=True, timeout=5
)
if result.returncode == 0 and result.stdout.strip():
return result.stdout.strip()
elif system == "Linux":
import subprocess
result = subprocess.run(
["secret-tool", "lookup", "service", "codealive-api-key"],
capture_output=True, text=True, timeout=5
)
if result.returncode == 0 and result.stdout.strip():
return result.stdout.strip()
elif system == "Windows":
return CodeAliveClient._read_windows_credential("codealive-api-key")
except (FileNotFoundError, Exception):
pass
return None
@staticmethod
def _read_windows_credential(target_name: str) -> Optional[str]:
"""Read a generic credential from Windows Credential Manager via ctypes."""
import ctypes
import ctypes.wintypes
CRED_TYPE_GENERIC = 1
class CREDENTIAL(ctypes.Structure):
"""Windows CREDENTIALW structure — layout handled by ctypes automatically."""
_fields_ = [
("Flags", ctypes.wintypes.DWORD),
("Type", ctypes.wintypes.DWORD),
("TargetName", ctypes.wintypes.LPWSTR),
("Comment", ctypes.wintypes.LPWSTR),
("LastWritten", ctypes.wintypes.FILETIME),
("CredentialBlobSize", ctypes.wintypes.DWORD),
("CredentialBlob", ctypes.POINTER(ctypes.c_char)),
("Persist", ctypes.wintypes.DWORD),
("AttributeCount", ctypes.wintypes.DWORD),
("Attributes", ctypes.c_void_p),
("TargetAlias", ctypes.wintypes.LPWSTR),
("UserName", ctypes.wintypes.LPWSTR),
]
advapi32 = ctypes.WinDLL("advapi32", use_last_error=True)
advapi32.CredReadW.restype = ctypes.wintypes.BOOL
advapi32.CredReadW.argtypes = [
ctypes.wintypes.LPCWSTR, ctypes.wintypes.DWORD,
ctypes.wintypes.DWORD, ctypes.POINTER(ctypes.POINTER(CREDENTIAL))
]
advapi32.CredFree.restype = None
advapi32.CredFree.argtypes = [ctypes.c_void_p]
cred_ptr = ctypes.POINTER(CREDENTIAL)()
if not advapi32.CredReadW(target_name, CRED_TYPE_GENERIC, 0, ctypes.byref(cred_ptr)):
return None
try:
cred = cred_ptr.contents
if cred.CredentialBlobSize > 0 and cred.CredentialBlob:
raw = cred.CredentialBlob[:cred.CredentialBlobSize]
return bytes(raw).decode("utf-16-le")
return None
finally:
advapi32.CredFree(cred_ptr)
@staticmethod
def _normalize_base_url(base_url: Optional[str]) -> str:
"""Normalize a CodeAlive base URL to the deployment origin."""
raw = (base_url or "https://app.codealive.ai").strip()
if not raw:
raw = "https://app.codealive.ai"
if "://" not in raw:
normalized = raw.rstrip("/")
if normalized.endswith("/api"):
normalized = normalized[:-4]
return normalized
parts = urllib.parse.urlsplit(raw)
path = parts.path.rstrip("/")
if path.endswith("/api"):
path = path[:-4]
return urllib.parse.urlunsplit((parts.scheme, parts.netloc, path, parts.query, parts.fragment)).rstrip("/")
def __init__(self, api_key: Optional[str] = None, base_url: Optional[str] = None):
"""
Initialize the CodeAlive API client.
Args:
api_key: CodeAlive API key. Resolution order:
1. Explicit api_key parameter
2. CODEALIVE_API_KEY environment variable
3. macOS Keychain (service: codealive-api-key)
base_url: Base URL for the API. Defaults to https://app.codealive.ai
"""
self.api_key = api_key or os.getenv("CODEALIVE_API_KEY") or self._get_key_from_keychain()
if not self.api_key:
resolved_base_url = self._normalize_base_url(base_url or os.getenv("CODEALIVE_BASE_URL", "https://app.codealive.ai"))
skill_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
setup_path = os.path.join(skill_dir, "setup.py")
raise ValueError(
"CodeAlive API key not configured.\n"
"\n"
"Option 1 (recommended): Run the interactive setup:\n"
f" python {setup_path}\n"
"\n"
"Option 2 (not recommended — key visible in chat history):\n"
" Ask the user to paste their API key, then run:\n"
f" python {setup_path} --key THE_KEY\n"
"\n"
f"Get API key at: {resolved_base_url}/settings/api-keys"
)
self.base_url = self._normalize_base_url(base_url or os.getenv("CODEALIVE_BASE_URL", "https://app.codealive.ai"))
self.timeout = 60
def _make_request(
self,
method: str,
endpoint: str,
params: Optional[Dict[str, Any]] = None,
body: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""
Make an HTTP request to the CodeAlive API.
Args:
method: HTTP method (GET, POST, etc.)
endpoint: API endpoint path
params: URL query parameters
body: Request body for POST requests
Returns:
Parsed JSON response
"""
url = f"{self.base_url}{endpoint}"
# Add query parameters
if params:
query_string = urllib.parse.urlencode(params, doseq=True)
url = f"{url}?{query_string}"
# Prepare request
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = None
if body:
data = json.dumps(body).encode("utf-8")
request = urllib.request.Request(url, data=data, headers=headers, method=method)
# Make request
try:
with urllib.request.urlopen(request, timeout=self.timeout) as response:
response_data = response.read().decode("utf-8")
return json.loads(response_data) if response_data else {}
except urllib.error.HTTPError as e:
error_body = e.read().decode("utf-8")
try:
error_data = json.loads(error_body)
error_msg = error_data.get("message") or error_data.get("error") or error_body
except json.JSONDecodeError:
error_msg = error_body
# Provide actionable messages for common HTTP errors
if e.code == 401:
detail = f": {error_msg}" if error_msg.strip() else ""
raise Exception(
f"Authentication failed (401){detail}. "
f"Your API key may be invalid or expired. "
f"Get a new key at: {self.base_url}/settings/api-keys"
)
elif e.code == 403:
raise Exception(
f"Access denied (403): {error_msg}. "
f"Your API key may lack permissions for this operation."
)
elif e.code == 404:
raise Exception(f"Not found (404): {error_msg}")
elif e.code == 429:
raise Exception(
f"Rate limit exceeded (429). Please wait before retrying."
)
elif e.code >= 500:
raise Exception(
f"Server error ({e.code}): {error_msg}. "
f"The CodeAlive service may be temporarily unavailable."
)
else:
raise Exception(f"API request failed ({e.code}): {error_msg}")
except urllib.error.URLError as e:
raise Exception(
f"Cannot connect to {self.base_url}: {e.reason}. "
f"Check your network connection and CODEALIVE_BASE_URL setting."
)
def get_datasources(self, alive_only: bool = True) -> List[Dict[str, Any]]:
"""
Get available data sources (repositories and workspaces).
Args:
alive_only: If True, only return data sources ready for use. If False, return all.
Returns:
List of data source objects with id, name, description, type, etc.
"""
endpoint = "/api/datasources/ready" if alive_only else "/api/datasources/all"
return self._make_request("GET", endpoint)
def search(
self,
query: str,
data_sources: List[str],
mode: str = "auto",
description_detail: str = "short"
) -> Dict[str, Any]:
"""
Search for code using natural language queries.
Args:
query: Natural language description of what to find
data_sources: List of repository or workspace names to search
mode: Search mode - "auto" (default), "fast", or "deep"
description_detail: Detail level for descriptions - "short" (default) or "full"
Returns:
Search results with file paths, line numbers, descriptions, and identifiers
"""
detail_map = {"short": "Short", "full": "Full"}
params = {
"Query": query,
"Mode": mode,
"IncludeContent": "false",
"DescriptionDetail": detail_map.get(description_detail.lower(), "Short"),
"Names": data_sources
}
return self._make_request("GET", "/api/search", params=params)
def fetch_artifacts(
self,
identifiers: List[str],
) -> Dict[str, Any]:
"""
Retrieve full content for code artifacts by their identifiers.
Use after search() to get the complete source code for results you need to inspect.
The identifier values come from search results.
Identifier format: {owner/repo}::{path}::{symbol} (for symbols/chunks)
{owner/repo}::{path} (for files)
Args:
identifiers: List of artifact identifiers from search results (max 20)
Returns:
Dict with 'artifacts' list containing identifier, content, contentByteSize, found
"""
body: Dict[str, Any] = {"identifiers": identifiers}
return self._make_request("POST", "/api/search/artifacts", body=body)
def chat(
self,
question: str,
data_sources: Optional[List[str]] = None,
conversation_id: Optional[str] = None
) -> Dict[str, Any]:
"""
Ask questions about the codebase to an AI consultant.
Args:
question: Question about the codebase
data_sources: List of repository or workspace names to analyze
conversation_id: ID to continue a previous conversation
Returns:
Response with answer and conversation_id for follow-up questions
"""
body: Dict[str, Any] = {
"messages": [{"role": "user", "content": question}],
"stream": True
}
if conversation_id:
body["conversationId"] = conversation_id
elif data_sources:
body["names"] = data_sources
else:
raise ValueError("Either conversation_id or data_sources must be provided")
# Note: This is a simplified version. The real implementation would handle streaming.
# For now, we'll make a non-streaming request.
body["stream"] = False
response = self._make_request("POST", "/api/chat/completions", body=body)
return {
"answer": response.get("choices", [{}])[0].get("message", {}).get("content", ""),
"conversation_id": response.get("id"),
"full_response": response
}
def main():
"""CLI interface for testing the client."""
if len(sys.argv) < 2:
print("Usage: python api_client.py <command> [args...]")
print("Commands:")
print(" datasources [--all]")
print(" search <query> <data_source1> [data_source2...] [--mode auto|fast|deep] [--description-detail short|full]")
print(" fetch <identifier1> [identifier2...]")
print(" chat <question> <data_source1> [data_source2...] [--conversation-id ID]")
sys.exit(1)
client = CodeAliveClient()
command = sys.argv[1]
try:
if command == "datasources":
alive_only = "--all" not in sys.argv
result = client.get_datasources(alive_only=alive_only)
print(json.dumps(result, indent=2))
elif command == "search":
if len(sys.argv) < 4:
print("Usage: search <query> <data_source1> [data_source2...] [--mode MODE] [--description-detail short|full]")
sys.exit(1)
query = sys.argv[2]
mode = "auto"
description_detail = "short"
data_sources = []
i = 3
while i < len(sys.argv):
arg = sys.argv[i]
if arg == "--mode" and i + 1 < len(sys.argv):
mode = sys.argv[i + 1]
i += 2
elif arg == "--description-detail" and i + 1 < len(sys.argv):
description_detail = sys.argv[i + 1]
i += 2
else:
data_sources.append(arg)
i += 1
result = client.search(query, data_sources, mode, description_detail)
print(json.dumps(result, indent=2))
elif command == "fetch":
if len(sys.argv) < 3:
print("Usage: fetch <identifier1> [identifier2...]")
sys.exit(1)
identifiers = sys.argv[2:]
result = client.fetch_artifacts(identifiers)
print(json.dumps(result, indent=2))
elif command == "chat":
if len(sys.argv) < 4:
print("Usage: chat <question> <data_source1> [data_source2...] [--conversation-id ID]")
sys.exit(1)
question = sys.argv[2]
conversation_id = None
data_sources = []
i = 3
while i < len(sys.argv):
arg = sys.argv[i]
if arg == "--conversation-id" and i + 1 < len(sys.argv):
conversation_id = sys.argv[i + 1]
i += 2
else:
data_sources.append(arg)
i += 1
result = client.chat(question, data_sources if data_sources else None, conversation_id)
print(result["answer"])
if result.get("conversation_id"):
print(f"\nConversation ID: {result['conversation_id']}")
else:
print(f"Unknown command: {command}")
sys.exit(1)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()