This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathv1.py
More file actions
643 lines (538 loc) · 22.1 KB
/
v1.py
File metadata and controls
643 lines (538 loc) · 22.1 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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
from typing import List, Optional
from uuid import UUID
import requests
import structlog
from fastapi import APIRouter, Depends, HTTPException, Response
from fastapi.responses import StreamingResponse
from fastapi.routing import APIRoute
from pydantic import BaseModel, ValidationError
import codegate.muxing.models as mux_models
from codegate import __version__
from codegate.api import v1_models, v1_processing
from codegate.db.connection import AlreadyExistsError, DbReader
from codegate.db.models import AlertSeverity, WorkspaceWithModel
from codegate.providers import crud as provendcrud
from codegate.workspaces import crud
logger = structlog.get_logger("codegate")
v1 = APIRouter()
wscrud = crud.WorkspaceCrud()
pcrud = provendcrud.ProviderCrud()
# This is a singleton object
dbreader = DbReader()
def uniq_name(route: APIRoute):
return f"v1_{route.name}"
class FilterByNameParams(BaseModel):
name: Optional[str] = None
@v1.get("/provider-endpoints", tags=["Providers"], generate_unique_id_function=uniq_name)
async def list_provider_endpoints(
filter_query: FilterByNameParams = Depends(),
) -> List[v1_models.ProviderEndpoint]:
"""List all provider endpoints."""
if filter_query.name is None:
try:
return await pcrud.list_endpoints()
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
try:
provend = await pcrud.get_endpoint_by_name(filter_query.name)
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
if provend is None:
raise HTTPException(status_code=404, detail="Provider endpoint not found")
return [provend]
# This needs to be above /provider-endpoints/{provider_id} to avoid conflict
@v1.get(
"/provider-endpoints/models",
tags=["Providers"],
generate_unique_id_function=uniq_name,
)
async def list_all_models_for_all_providers() -> List[v1_models.ModelByProvider]:
"""List all models for all providers."""
try:
return await pcrud.get_all_models()
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
@v1.get(
"/provider-endpoints/{provider_id}/models",
tags=["Providers"],
generate_unique_id_function=uniq_name,
)
async def list_models_by_provider(
provider_id: UUID,
) -> List[v1_models.ModelByProvider]:
"""List models by provider."""
try:
return await pcrud.models_by_provider(provider_id)
except provendcrud.ProviderNotFoundError:
raise HTTPException(status_code=404, detail="Provider not found")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@v1.get(
"/provider-endpoints/{provider_id}", tags=["Providers"], generate_unique_id_function=uniq_name
)
async def get_provider_endpoint(
provider_id: UUID,
) -> v1_models.ProviderEndpoint:
"""Get a provider endpoint by ID."""
try:
provend = await pcrud.get_endpoint_by_id(provider_id)
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
if provend is None:
raise HTTPException(status_code=404, detail="Provider endpoint not found")
return provend
@v1.post(
"/provider-endpoints",
tags=["Providers"],
generate_unique_id_function=uniq_name,
status_code=201,
)
async def add_provider_endpoint(
request: v1_models.AddProviderEndpointRequest,
) -> v1_models.ProviderEndpoint:
"""Add a provider endpoint."""
try:
provend = await pcrud.add_endpoint(request)
except AlreadyExistsError:
raise HTTPException(status_code=409, detail="Provider endpoint already exists")
except ValueError as e:
raise HTTPException(
status_code=400,
detail=str(e),
)
except provendcrud.ProviderModelsNotFoundError:
raise HTTPException(status_code=401, detail="Provider models could not be found")
except provendcrud.ProviderInvalidAuthConfigError:
raise HTTPException(status_code=400, detail="Invalid auth configuration")
except ValidationError as e:
# TODO: This should be more specific
raise HTTPException(
status_code=400,
detail=str(e),
)
except Exception:
logger.exception("Error while adding provider endpoint")
raise HTTPException(status_code=500, detail="Internal server error")
return provend
@v1.put(
"/provider-endpoints/{provider_id}/auth-material",
tags=["Providers"],
generate_unique_id_function=uniq_name,
status_code=204,
)
async def configure_auth_material(
provider_id: UUID,
request: v1_models.ConfigureAuthMaterial,
):
"""Configure auth material for a provider."""
try:
await pcrud.configure_auth_material(provider_id, request)
except provendcrud.ProviderNotFoundError:
raise HTTPException(status_code=404, detail="Provider endpoint not found")
except provendcrud.ProviderModelsNotFoundError:
raise HTTPException(status_code=401, detail="Provider models could not be found")
except provendcrud.ProviderInvalidAuthConfigError:
raise HTTPException(status_code=400, detail="Invalid auth configuration")
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
return Response(status_code=204)
@v1.put(
"/provider-endpoints/{provider_id}", tags=["Providers"], generate_unique_id_function=uniq_name
)
async def update_provider_endpoint(
provider_id: UUID,
request: v1_models.ProviderEndpoint,
) -> v1_models.ProviderEndpoint:
"""Update a provider endpoint by ID."""
try:
request.id = str(provider_id)
provend = await pcrud.update_endpoint(request)
except provendcrud.ProviderNotFoundError:
raise HTTPException(status_code=404, detail="Provider endpoint not found")
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except ValidationError as e:
# TODO: This should be more specific
raise HTTPException(
status_code=400,
detail=str(e),
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
return provend
@v1.delete(
"/provider-endpoints/{provider_id}", tags=["Providers"], generate_unique_id_function=uniq_name
)
async def delete_provider_endpoint(
provider_id: UUID,
):
"""Delete a provider endpoint by id."""
try:
await pcrud.delete_endpoint(provider_id)
except provendcrud.ProviderNotFoundError:
raise HTTPException(status_code=404, detail="Provider endpoint not found")
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
return Response(status_code=204)
@v1.get("/workspaces", tags=["Workspaces"], generate_unique_id_function=uniq_name)
async def list_workspaces() -> v1_models.ListWorkspacesResponse:
"""List all workspaces."""
wslist = await wscrud.get_workspaces()
resp = v1_models.ListWorkspacesResponse.from_db_workspaces_with_sessioninfo(wslist)
return resp
@v1.get("/workspaces/active", tags=["Workspaces"], generate_unique_id_function=uniq_name)
async def list_active_workspaces() -> v1_models.ListActiveWorkspacesResponse:
"""List all active workspaces.
In it's current form, this function will only return one workspace. That is,
the globally active workspace."""
activews = await wscrud.get_active_workspace()
resp = v1_models.ListActiveWorkspacesResponse.from_db_workspaces(activews)
return resp
@v1.post("/workspaces/active", tags=["Workspaces"], generate_unique_id_function=uniq_name)
async def activate_workspace(request: v1_models.ActivateWorkspaceRequest, status_code=204):
"""Activate a workspace by name."""
try:
await wscrud.activate_workspace(request.name)
except crud.WorkspaceAlreadyActiveError:
raise HTTPException(status_code=409, detail="Workspace already active")
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
return Response(status_code=204)
@v1.post("/workspaces", tags=["Workspaces"], generate_unique_id_function=uniq_name, status_code=201)
async def create_workspace(
request: v1_models.CreateOrRenameWorkspaceRequest,
) -> v1_models.Workspace:
"""Create a new workspace."""
if request.rename_to is not None:
return await rename_workspace(request)
return await create_new_workspace(request)
async def create_new_workspace(
request: v1_models.CreateOrRenameWorkspaceRequest,
) -> v1_models.Workspace:
# Input validation is done in the model
try:
_ = await wscrud.add_workspace(request.name)
except AlreadyExistsError:
raise HTTPException(status_code=409, detail="Workspace already exists")
except ValidationError:
raise HTTPException(
status_code=400,
detail=(
"Invalid workspace name. "
"Please use only alphanumeric characters, hyphens, or underscores."
),
)
except crud.WorkspaceCrudError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
return v1_models.Workspace(name=request.name, is_active=False)
async def rename_workspace(
request: v1_models.CreateOrRenameWorkspaceRequest,
) -> v1_models.Workspace:
try:
_ = await wscrud.rename_workspace(request.name, request.rename_to)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except AlreadyExistsError:
raise HTTPException(status_code=409, detail="Workspace already exists")
except ValidationError:
raise HTTPException(
status_code=400,
detail=(
"Invalid workspace name. "
"Please use only alphanumeric characters, hyphens, or underscores."
),
)
except crud.WorkspaceCrudError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
return v1_models.Workspace(name=request.rename_to, is_active=False)
@v1.delete(
"/workspaces/{workspace_name}",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
)
async def delete_workspace(workspace_name: str):
"""Delete a workspace by name."""
try:
_ = await wscrud.soft_delete_workspace(workspace_name)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except crud.WorkspaceCrudError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
return Response(status_code=204)
@v1.get("/workspaces/archive", tags=["Workspaces"], generate_unique_id_function=uniq_name)
async def list_archived_workspaces() -> v1_models.ListWorkspacesResponse:
"""List all archived workspaces."""
wslist = await wscrud.get_archived_workspaces()
resp = v1_models.ListWorkspacesResponse.from_db_workspaces(wslist)
return resp
@v1.post(
"/workspaces/archive/{workspace_name}/recover",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
status_code=204,
)
async def recover_workspace(workspace_name: str):
"""Recover an archived workspace by name."""
try:
_ = await wscrud.recover_workspace(workspace_name)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except crud.WorkspaceCrudError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
return Response(status_code=204)
@v1.delete(
"/workspaces/archive/{workspace_name}",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
)
async def hard_delete_workspace(workspace_name: str):
"""Hard delete an archived workspace by name."""
try:
_ = await wscrud.hard_delete_workspace(workspace_name)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except crud.WorkspaceCrudError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
return Response(status_code=204)
@v1.get(
"/workspaces/{workspace_name}/alerts",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
)
async def get_workspace_alerts(workspace_name: str) -> List[Optional[v1_models.AlertConversation]]:
"""Get alerts for a workspace."""
try:
ws = await wscrud.get_workspace_by_name(workspace_name)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
logger.exception("Error while getting workspace")
raise HTTPException(status_code=500, detail="Internal server error")
try:
alerts = await dbreader.get_alerts_by_workspace(ws.id, AlertSeverity.CRITICAL.value)
prompts_outputs = await dbreader.get_prompts_with_output(ws.id)
return await v1_processing.parse_get_alert_conversation(alerts, prompts_outputs)
except Exception:
logger.exception("Error while getting alerts and messages")
raise HTTPException(status_code=500, detail="Internal server error")
@v1.get(
"/workspaces/{workspace_name}/alerts-summary",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
)
async def get_workspace_alerts_summary(workspace_name: str) -> v1_models.AlertSummary:
"""Get alert summary for a workspace."""
try:
ws = await wscrud.get_workspace_by_name(workspace_name)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
logger.exception("Error while getting workspace")
raise HTTPException(status_code=500, detail="Internal server error")
try:
summary = await dbreader.get_alerts_summary_by_workspace(ws.id)
return v1_models.AlertSummary(
malicious_packages=summary["codegate_context_retriever_count"],
pii=summary["codegate_pii_count"],
secrets=summary["codegate_secrets_count"],
)
except Exception:
logger.exception("Error while getting alerts summary")
raise HTTPException(status_code=500, detail="Internal server error")
@v1.get(
"/workspaces/{workspace_name}/messages",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
)
async def get_workspace_messages(workspace_name: str) -> List[v1_models.Conversation]:
"""Get messages for a workspace."""
try:
ws = await wscrud.get_workspace_by_name(workspace_name)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
logger.exception("Error while getting workspace")
raise HTTPException(status_code=500, detail="Internal server error")
try:
prompts_with_output_alerts_usage = (
await dbreader.get_prompts_with_output_alerts_usage_by_workspace_id(
ws.id, AlertSeverity.CRITICAL.value
)
)
conversations, _ = await v1_processing.parse_messages_in_conversations(
prompts_with_output_alerts_usage
)
return conversations
except Exception:
logger.exception("Error while getting messages")
raise HTTPException(status_code=500, detail="Internal server error")
@v1.get(
"/workspaces/{workspace_name}/custom-instructions",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
)
async def get_workspace_custom_instructions(workspace_name: str) -> v1_models.CustomInstructions:
"""Get the custom instructions of a workspace."""
try:
ws = await wscrud.get_workspace_by_name(workspace_name)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
if ws.custom_instructions is None:
return v1_models.CustomInstructions(prompt="")
return v1_models.CustomInstructions(prompt=ws.custom_instructions)
@v1.put(
"/workspaces/{workspace_name}/custom-instructions",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
status_code=204,
)
async def set_workspace_custom_instructions(
workspace_name: str, request: v1_models.CustomInstructions
):
try:
# This already checks if the workspace exists
await wscrud.update_workspace_custom_instructions(workspace_name, [request.prompt])
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
return Response(status_code=204)
@v1.delete(
"/workspaces/{workspace_name}/custom-instructions",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
status_code=204,
)
async def delete_workspace_custom_instructions(workspace_name: str):
try:
# This already checks if the workspace exists
await wscrud.update_workspace_custom_instructions(workspace_name, [])
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
raise HTTPException(status_code=500, detail="Internal server error")
return Response(status_code=204)
@v1.get(
"/workspaces/{workspace_name}/muxes",
tags=["Workspaces", "Muxes"],
generate_unique_id_function=uniq_name,
)
async def get_workspace_muxes(
workspace_name: str,
) -> List[mux_models.MuxRule]:
"""Get the mux rules of a workspace.
The list is ordered in order of priority. That is, the first rule in the list
has the highest priority."""
try:
muxes = await wscrud.get_muxes(workspace_name)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
logger.exception("Error while getting workspace")
raise HTTPException(status_code=500, detail="Internal server error")
return muxes
@v1.put(
"/workspaces/{workspace_name}/muxes",
tags=["Workspaces", "Muxes"],
generate_unique_id_function=uniq_name,
status_code=204,
)
async def set_workspace_muxes(
workspace_name: str,
request: List[mux_models.MuxRule],
):
"""Set the mux rules of a workspace."""
try:
await wscrud.set_muxes(workspace_name, request)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except crud.WorkspaceCrudError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception:
logger.exception("Error while setting muxes")
raise HTTPException(status_code=500, detail="Internal server error")
return Response(status_code=204)
@v1.get(
"/workspaces/{provider_id}",
tags=["Workspaces"],
generate_unique_id_function=uniq_name,
)
async def list_workspaces_by_provider(
provider_id: UUID,
) -> List[WorkspaceWithModel]:
"""List workspaces by provider ID."""
try:
return await wscrud.workspaces_by_provider(provider_id)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@v1.get("/alerts_notification", tags=["Dashboard"], generate_unique_id_function=uniq_name)
async def stream_sse():
"""
Send alerts event
"""
return StreamingResponse(v1_processing.generate_sse_events(), media_type="text/event-stream")
@v1.get("/version", tags=["Dashboard"], generate_unique_id_function=uniq_name)
def version_check():
try:
latest_version = v1_processing.fetch_latest_version()
# normalize the versions as github will return them with a 'v' prefix
current_version = __version__.lstrip("v")
latest_version_stripped = latest_version.lstrip("v")
is_latest: bool = latest_version_stripped == current_version
return {
"current_version": current_version,
"latest_version": latest_version_stripped,
"is_latest": is_latest,
"error": None,
}
except requests.RequestException as e:
logger.error(f"RequestException: {str(e)}")
return {
"current_version": __version__,
"latest_version": "unknown",
"is_latest": None,
"error": "An error occurred while fetching the latest version",
}
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
return {
"current_version": __version__,
"latest_version": "unknown",
"is_latest": None,
"error": "An unexpected error occurred",
}
@v1.get(
"/workspaces/{workspace_name}/token-usage",
tags=["Workspaces", "Token Usage"],
generate_unique_id_function=uniq_name,
)
async def get_workspace_token_usage(workspace_name: str) -> v1_models.TokenUsageAggregate:
"""Get the token usage of a workspace."""
try:
ws = await wscrud.get_workspace_by_name(workspace_name)
except crud.WorkspaceDoesNotExistError:
raise HTTPException(status_code=404, detail="Workspace does not exist")
except Exception:
logger.exception("Error while getting workspace")
raise HTTPException(status_code=500, detail="Internal server error")
try:
prompts_outputs = await dbreader.get_prompts_with_output(ws.id)
ws_token_usage = await v1_processing.parse_workspace_token_usage(prompts_outputs)
return ws_token_usage
except Exception:
logger.exception("Error while getting messages")
raise HTTPException(status_code=500, detail="Internal server error")