luclima commited on
Commit
395d32f
·
verified ·
1 Parent(s): 3e10658

Upload folder using huggingface_hub (part 2)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +5 -0
  2. western_ai/canada/cohere-toolkit/src/backend/pytest_integration.ini +6 -0
  3. western_ai/canada/cohere-toolkit/src/backend/routers/__init__.py +2 -0
  4. western_ai/canada/cohere-toolkit/src/backend/routers/agent.py +617 -0
  5. western_ai/canada/cohere-toolkit/src/backend/routers/auth.py +348 -0
  6. western_ai/canada/cohere-toolkit/src/backend/routers/chat.py +187 -0
  7. western_ai/canada/cohere-toolkit/src/backend/routers/conversation.py +544 -0
  8. western_ai/canada/cohere-toolkit/src/backend/routers/deployment.py +164 -0
  9. western_ai/canada/cohere-toolkit/src/backend/routers/experimental_features.py +25 -0
  10. western_ai/canada/cohere-toolkit/src/backend/routers/model.py +108 -0
  11. western_ai/canada/cohere-toolkit/src/backend/routers/organization.py +125 -0
  12. western_ai/canada/cohere-toolkit/src/backend/routers/scim.py +346 -0
  13. western_ai/canada/cohere-toolkit/src/backend/routers/snapshot.py +164 -0
  14. western_ai/canada/cohere-toolkit/src/backend/routers/tool.py +67 -0
  15. western_ai/canada/cohere-toolkit/src/backend/routers/user.py +144 -0
  16. western_ai/canada/cohere-toolkit/src/backend/routers/utils.py +57 -0
  17. western_ai/canada/cohere-toolkit/src/backend/schemas/__init__.py +4 -0
  18. western_ai/canada/cohere-toolkit/src/backend/schemas/agent.py +421 -0
  19. western_ai/canada/cohere-toolkit/src/backend/schemas/auth.py +70 -0
  20. western_ai/canada/cohere-toolkit/src/backend/schemas/chat.py +496 -0
  21. western_ai/canada/cohere-toolkit/src/backend/schemas/citation.py +30 -0
  22. western_ai/canada/cohere-toolkit/src/backend/schemas/cohere_chat.py +130 -0
  23. western_ai/canada/cohere-toolkit/src/backend/schemas/context.py +195 -0
  24. western_ai/canada/cohere-toolkit/src/backend/schemas/conversation.py +163 -0
  25. western_ai/canada/cohere-toolkit/src/backend/schemas/deployment.py +151 -0
  26. western_ai/canada/cohere-toolkit/src/backend/schemas/document.py +43 -0
  27. western_ai/canada/cohere-toolkit/src/backend/schemas/file.py +131 -0
  28. western_ai/canada/cohere-toolkit/src/backend/schemas/message.py +114 -0
  29. western_ai/canada/cohere-toolkit/src/backend/schemas/model.py +69 -0
  30. western_ai/canada/cohere-toolkit/src/backend/schemas/organization.py +65 -0
  31. western_ai/canada/cohere-toolkit/src/backend/schemas/params/__init__.py +0 -0
  32. western_ai/canada/cohere-toolkit/src/backend/schemas/params/agent.py +28 -0
  33. western_ai/canada/cohere-toolkit/src/backend/schemas/params/auth.py +16 -0
  34. western_ai/canada/cohere-toolkit/src/backend/schemas/params/conversation.py +16 -0
  35. western_ai/canada/cohere-toolkit/src/backend/schemas/params/deployment.py +16 -0
  36. western_ai/canada/cohere-toolkit/src/backend/schemas/params/file.py +11 -0
  37. western_ai/canada/cohere-toolkit/src/backend/schemas/params/message.py +11 -0
  38. western_ai/canada/cohere-toolkit/src/backend/schemas/params/model.py +16 -0
  39. western_ai/canada/cohere-toolkit/src/backend/schemas/params/organization.py +16 -0
  40. western_ai/canada/cohere-toolkit/src/backend/schemas/params/scim.py +44 -0
  41. western_ai/canada/cohere-toolkit/src/backend/schemas/params/shared.py +33 -0
  42. western_ai/canada/cohere-toolkit/src/backend/schemas/params/snapshot.py +16 -0
  43. western_ai/canada/cohere-toolkit/src/backend/schemas/params/tool.py +11 -0
  44. western_ai/canada/cohere-toolkit/src/backend/schemas/params/user.py +11 -0
  45. western_ai/canada/cohere-toolkit/src/backend/schemas/scim.py +320 -0
  46. western_ai/canada/cohere-toolkit/src/backend/schemas/search_query.py +20 -0
  47. western_ai/canada/cohere-toolkit/src/backend/schemas/snapshot.py +272 -0
  48. western_ai/canada/cohere-toolkit/src/backend/schemas/tool.py +150 -0
  49. western_ai/canada/cohere-toolkit/src/backend/schemas/tool_auth.py +51 -0
  50. western_ai/canada/cohere-toolkit/src/backend/schemas/user.py +106 -0
.gitattributes CHANGED
@@ -1013,3 +1013,8 @@ western_ai/canada/cohere-toolkit/docs/assets/vscode_debug.png filter=lfs diff=lf
1013
  western_ai/canada/cohere-toolkit/docs/walkthrough/assets/default_set_up.png filter=lfs diff=lfs merge=lfs -text
1014
  western_ai/canada/cohere-toolkit/docs/walkthrough/assets/toolkit.png filter=lfs diff=lfs merge=lfs -text
1015
  western_ai/canada/cohere-toolkit/docs/walkthrough/assets/whole_set_up.png filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
1013
  western_ai/canada/cohere-toolkit/docs/walkthrough/assets/default_set_up.png filter=lfs diff=lfs merge=lfs -text
1014
  western_ai/canada/cohere-toolkit/docs/walkthrough/assets/toolkit.png filter=lfs diff=lfs merge=lfs -text
1015
  western_ai/canada/cohere-toolkit/docs/walkthrough/assets/whole_set_up.png filter=lfs diff=lfs merge=lfs -text
1016
+ western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/26mb.pdf filter=lfs diff=lfs merge=lfs -text
1017
+ western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Cardistry.pdf filter=lfs diff=lfs merge=lfs -text
1018
+ western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Mariana_Trench.pdf filter=lfs diff=lfs merge=lfs -text
1019
+ western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Mount_Everest.pdf filter=lfs diff=lfs merge=lfs -text
1020
+ western_ai/canada/cohere-toolkit/src/backend/tests/unit/test_data/Tapas.pdf filter=lfs diff=lfs merge=lfs -text
western_ai/canada/cohere-toolkit/src/backend/pytest_integration.ini ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [pytest]
2
+ env =
3
+ DATABASE_URL=postgresql://postgres:postgres@localhost:5433
4
+ filterwarnings =
5
+ ignore::UserWarning:pydantic.*
6
+ ignore::DeprecationWarning
western_ai/canada/cohere-toolkit/src/backend/routers/__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # ruff: noqa
2
+ from backend.routers.chat import *
western_ai/canada/cohere-toolkit/src/backend/routers/agent.py ADDED
@@ -0,0 +1,617 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+
3
+ from fastapi import APIRouter, Depends, HTTPException
4
+ from fastapi import File as RequestFile
5
+ from fastapi import UploadFile as FastAPIUploadFile
6
+
7
+ from backend.config.auth import is_authentication_enabled
8
+ from backend.config.default_agent import DEFAULT_AGENT_ID, get_default_agent
9
+ from backend.config.routers import RouterName
10
+ from backend.crud import agent as agent_crud
11
+ from backend.crud import agent_tool_metadata as agent_tool_metadata_crud
12
+ from backend.crud import file as file_crud
13
+ from backend.crud import snapshot as snapshot_crud
14
+ from backend.database_models.agent import Agent as AgentModel
15
+ from backend.database_models.agent_tool_metadata import (
16
+ AgentToolMetadata as AgentToolMetadataModel,
17
+ )
18
+ from backend.database_models.database import DBSessionDep
19
+ from backend.routers.utils import (
20
+ get_default_deployment_model,
21
+ get_deployment_model_from_agent,
22
+ )
23
+ from backend.schemas.agent import (
24
+ Agent,
25
+ AgentPublic,
26
+ AgentToolMetadata,
27
+ AgentToolMetadataPublic,
28
+ AgentVisibility,
29
+ CreateAgentRequest,
30
+ CreateAgentToolMetadataRequest,
31
+ DeleteAgent,
32
+ DeleteAgentToolMetadata,
33
+ UpdateAgentDB,
34
+ UpdateAgentRequest,
35
+ UpdateAgentToolMetadataRequest,
36
+ )
37
+ from backend.schemas.context import Context
38
+ from backend.schemas.deployment import DeploymentDefinition
39
+ from backend.schemas.file import (
40
+ DeleteAgentFileResponse,
41
+ FileMetadata,
42
+ UploadAgentFileResponse,
43
+ )
44
+ from backend.schemas.params.agent import (
45
+ AgentIdPathParam,
46
+ AgentToolMetadataIdPathParam,
47
+ VisibilityQueryParam,
48
+ )
49
+ from backend.schemas.params.file import FileIdPathParam
50
+ from backend.schemas.params.organization import OrganizationIdQueryParam
51
+ from backend.schemas.params.shared import PaginationQueryParams
52
+ from backend.services.agent import (
53
+ raise_db_error,
54
+ validate_agent_exists,
55
+ validate_agent_tool_metadata_exists,
56
+ )
57
+ from backend.services.context import get_context
58
+ from backend.services.file import (
59
+ get_file_service,
60
+ validate_file,
61
+ )
62
+ from backend.services.request_validators import (
63
+ validate_create_agent_request,
64
+ validate_update_agent_request,
65
+ validate_user_header,
66
+ )
67
+
68
+ router = APIRouter(
69
+ prefix="/v1/agents",
70
+ tags=[RouterName.AGENT],
71
+ )
72
+ router.name = RouterName.AGENT
73
+
74
+ auth_dependencies = [] if is_authentication_enabled() else [Depends(validate_user_header)]
75
+
76
+
77
+ @router.post(
78
+ "",
79
+ response_model=AgentPublic,
80
+ dependencies=[
81
+ *auth_dependencies,
82
+ Depends(validate_create_agent_request),
83
+ ],
84
+ )
85
+ async def create_agent(
86
+ agent: CreateAgentRequest,
87
+ session: DBSessionDep,
88
+ ctx: Context = Depends(get_context),
89
+ ) -> AgentPublic:
90
+ """
91
+ Create an agent.
92
+
93
+ Raises:
94
+ HTTPException: If the agent creation fails.
95
+ """
96
+ ctx.with_user(session)
97
+ user_id = ctx.get_user_id()
98
+ logger = ctx.get_logger()
99
+ logger.debug(event="Creating agent", user_id=user_id, agent=agent.model_dump())
100
+
101
+ deployment_db, model_db = get_deployment_model_from_agent(agent, session)
102
+ default_deployment_db, default_model_db = get_default_deployment_model(session)
103
+ logger.debug(event="Deployment and model", deployment=deployment_db, model=model_db)
104
+
105
+ if not deployment_db or not model_db:
106
+ logger.error(event="Unable to find deployment or model, using defaults", agent=agent)
107
+
108
+ if not default_deployment_db or not default_model_db:
109
+ logger.error(event="Unable to find default deployment or model", agent=agent)
110
+ raise HTTPException(status_code=400, detail="Unable to find deployment or model")
111
+
112
+ deployment_db = default_deployment_db
113
+ model_db = default_model_db
114
+
115
+ try:
116
+ agent_data = AgentModel(
117
+ name=agent.name,
118
+ description=agent.description,
119
+ preamble=agent.preamble,
120
+ temperature=agent.temperature,
121
+ user_id=user_id,
122
+ organization_id=agent.organization_id,
123
+ tools=agent.tools,
124
+ is_private=agent.is_private,
125
+ deployment_id=deployment_db.id,
126
+ model_id=model_db.id,
127
+ )
128
+
129
+ created_agent = agent_crud.create_agent(session, agent_data)
130
+
131
+ if not created_agent:
132
+ raise HTTPException(status_code=500, detail="Failed to create Agent")
133
+
134
+ if agent.tools_metadata:
135
+ for tool_metadata in agent.tools_metadata:
136
+ await update_or_create_tool_metadata(
137
+ created_agent, tool_metadata, session, ctx
138
+ )
139
+
140
+ agent_schema = Agent.model_validate(created_agent)
141
+ ctx.with_agent(agent_schema)
142
+ logger.debug(event="Agent created", agent=created_agent)
143
+ return created_agent
144
+ except Exception as e:
145
+ logger.exception(event=e)
146
+ raise HTTPException(status_code=500, detail=str(e))
147
+
148
+
149
+ @router.get("", response_model=list[AgentPublic])
150
+ async def list_agents(
151
+ *,
152
+ page_params: PaginationQueryParams,
153
+ visibility: VisibilityQueryParam = AgentVisibility.ALL,
154
+ org_id: OrganizationIdQueryParam = None,
155
+ session: DBSessionDep,
156
+ ctx: Context = Depends(get_context),
157
+ ) -> list[AgentPublic]:
158
+ """
159
+ List all agents.
160
+ """
161
+ logger = ctx.get_logger()
162
+ # TODO: get organization_id from user
163
+ user_id = ctx.get_user_id()
164
+ logger = ctx.get_logger()
165
+ # request organization_id is used for filtering agents instead of header Organization-Id if enabled
166
+ if org_id:
167
+ logger.debug(event="Request limited to organization", organization_id=org_id)
168
+ ctx.without_global_filtering()
169
+
170
+ try:
171
+ agents = agent_crud.get_agents(
172
+ session,
173
+ user_id=user_id,
174
+ offset=page_params.offset,
175
+ limit=page_params.limit,
176
+ visibility=visibility,
177
+ organization_id=org_id,
178
+ )
179
+ # Tradeoff: This appends the default Agent regardless of pagination
180
+ agents.append(get_default_agent())
181
+ logger.debug(event="Returning agents:", agents=agents)
182
+ return agents
183
+ except Exception as e:
184
+ logger.exception(event=e)
185
+ raise HTTPException(status_code=500, detail=str(e))
186
+
187
+
188
+ @router.get("/{agent_id}", response_model=AgentPublic)
189
+ async def get_agent_by_id(
190
+ agent_id: AgentIdPathParam,
191
+ session: DBSessionDep,
192
+ ctx: Context = Depends(get_context)
193
+ ) -> AgentPublic:
194
+ """
195
+ Return an agent by ID.
196
+
197
+ Raises:
198
+ HTTPException: If the agent with the given ID is not found.
199
+ """
200
+ user_id = ctx.get_user_id()
201
+ agent = None
202
+
203
+ try:
204
+ # Intentionally not adding Default Agent to DB so it's more flexible
205
+ if agent_id == DEFAULT_AGENT_ID:
206
+ agent = get_default_agent()
207
+ else:
208
+ agent = agent_crud.get_agent_by_id(session, agent_id, user_id)
209
+ except Exception as e:
210
+ raise HTTPException(status_code=500, detail=str(e))
211
+
212
+ if not agent:
213
+ raise HTTPException(
214
+ status_code=404,
215
+ detail=f"Agent with ID {agent_id} not found.",
216
+ )
217
+
218
+ agent_schema = Agent.model_validate(agent)
219
+ ctx.with_agent(agent_schema)
220
+ return agent
221
+
222
+
223
+ @router.get("/{agent_id}/deployments", response_model=list[DeploymentDefinition])
224
+ async def get_agent_deployment(
225
+ agent_id: AgentIdPathParam,
226
+ session: DBSessionDep,
227
+ ctx: Context = Depends(get_context)
228
+ ) -> list[DeploymentDefinition]:
229
+ """
230
+ Get the deployment for an agent
231
+
232
+ Raises:
233
+ HTTPException: If the agent with the given ID is not found.
234
+ """
235
+ user_id = ctx.get_user_id()
236
+ agent = validate_agent_exists(session, agent_id, user_id)
237
+
238
+ agent_schema = Agent.model_validate(agent)
239
+ ctx.with_agent(agent_schema)
240
+
241
+ return [
242
+ DeploymentDefinition.from_db_deployment(deployment)
243
+ for deployment in agent.deployments
244
+ ]
245
+
246
+
247
+ @router.put(
248
+ "/{agent_id}",
249
+ response_model=AgentPublic,
250
+ dependencies=[
251
+ *auth_dependencies,
252
+ Depends(validate_update_agent_request),
253
+ ],
254
+ )
255
+ async def update_agent(
256
+ agent_id: AgentIdPathParam,
257
+ new_agent: UpdateAgentRequest,
258
+ session: DBSessionDep,
259
+ ctx: Context = Depends(get_context),
260
+ ) -> AgentPublic:
261
+ """
262
+ Update an agent by ID.
263
+
264
+ Raises:
265
+ HTTPException: If the agent with the given ID is not found.
266
+ """
267
+ user_id = ctx.get_user_id()
268
+ ctx.with_user(session)
269
+ agent = validate_agent_exists(session, agent_id, user_id)
270
+
271
+ if new_agent.tools_metadata is not None:
272
+ agent = await handle_tool_metadata_update(agent, new_agent, session, ctx)
273
+
274
+ # If the agent was public and is now private, we need to delete all snapshots associated with it
275
+ snapshot_deletion_task = None
276
+ if not agent.is_private and new_agent.is_private:
277
+ snapshot_deletion_task = asyncio.create_task(
278
+ snapshot_crud.delete_snapshots_by_agent_id(session, agent_id)
279
+ )
280
+
281
+ try:
282
+ db_deployment, db_model = get_deployment_model_from_agent(new_agent, session)
283
+ new_agent_db = UpdateAgentDB(**new_agent.model_dump())
284
+ if db_deployment and db_model:
285
+ new_agent_db.model_id = db_model.id
286
+ new_agent_db.deployment_id = db_deployment.id
287
+ agent = agent_crud.update_agent(
288
+ session, agent, new_agent_db, user_id
289
+ )
290
+ agent_schema = Agent.model_validate(agent)
291
+ ctx.with_agent(agent_schema)
292
+ except Exception as e:
293
+ raise HTTPException(status_code=500, detail=str(e))
294
+ finally:
295
+ # Wait for snapshot deletion task to finish
296
+ if snapshot_deletion_task:
297
+ await snapshot_deletion_task
298
+
299
+ return agent
300
+
301
+
302
+ @router.delete("/{agent_id}", response_model=DeleteAgent)
303
+ async def delete_agent(
304
+ agent_id: AgentIdPathParam,
305
+ session: DBSessionDep,
306
+ ctx: Context = Depends(get_context),
307
+ ) -> DeleteAgent:
308
+ """
309
+ Delete an agent by ID.
310
+
311
+ Raises:
312
+ HTTPException: If the agent with the given ID is not found.
313
+ """
314
+ user_id = ctx.get_user_id()
315
+ agent = validate_agent_exists(session, agent_id, user_id)
316
+ agent_schema = Agent.model_validate(agent)
317
+ ctx.with_agent(agent_schema)
318
+
319
+ deleted = agent_crud.delete_agent(session, agent_id, user_id)
320
+ if not deleted:
321
+ raise HTTPException(status_code=401, detail="Could not delete Agent.")
322
+
323
+ return DeleteAgent()
324
+
325
+
326
+ # Agent Tool Metadata endpoints
327
+
328
+
329
+ async def handle_tool_metadata_update(
330
+ agent: Agent,
331
+ new_agent: Agent,
332
+ session: DBSessionDep,
333
+ ctx: Context = Depends(get_context),
334
+ ) -> Agent:
335
+ """Update or create tool metadata for an agent.
336
+
337
+ Args:
338
+ agent (Agent): Agent.
339
+ new_agent (Agent): New agent data.
340
+ session (DBSessionDep): Database session.
341
+ ctx (Context): Context object.
342
+
343
+ Returns:
344
+ Agent: Agent.
345
+ """
346
+ user_id = ctx.get_user_id()
347
+ validate_agent_exists(session, agent.id, user_id)
348
+
349
+ # Delete tool metadata that are not in the request
350
+ new_tools_names = [metadata.tool_name for metadata in new_agent.tools_metadata]
351
+ for tool_metadata in agent.tools_metadata:
352
+ if tool_metadata.tool_name not in new_tools_names:
353
+ agent_tool_metadata_crud.delete_agent_tool_metadata_by_id(
354
+ session, tool_metadata.id
355
+ )
356
+
357
+ # Create or update tool metadata from the request
358
+ for tool_metadata in new_agent.tools_metadata:
359
+ try:
360
+ await update_or_create_tool_metadata(agent, tool_metadata, session, ctx)
361
+ except Exception as e:
362
+ raise_db_error(e, "Tool name", tool_metadata.tool_name)
363
+
364
+ # Remove tools_metadata from new_agent to avoid updating it in the agent
365
+ new_agent.tools_metadata = None
366
+ agent = agent_crud.get_agent_by_id(session, agent.id, user_id)
367
+ return agent
368
+
369
+
370
+ async def update_or_create_tool_metadata(
371
+ agent: Agent,
372
+ new_tool_metadata: AgentToolMetadata,
373
+ session: DBSessionDep,
374
+ ctx: Context = Depends(get_context),
375
+ ) -> None:
376
+ """Update or create tool metadata for an agent.
377
+
378
+ Args:
379
+ agent (Agent): Agent.
380
+ new_tool_metadata (AgentToolMetadata): New tool metadata.
381
+ session (DBSessionDep): Database session.
382
+ ctx (Context): Context object.
383
+ """
384
+
385
+ existing_tools_names = [metadata.tool_name for metadata in agent.tools_metadata]
386
+ if new_tool_metadata.tool_name in existing_tools_names or new_tool_metadata.id:
387
+ await update_agent_tool_metadata(
388
+ agent.id, new_tool_metadata.id, new_tool_metadata, session, ctx
389
+ )
390
+ else:
391
+ create_metadata_req = CreateAgentToolMetadataRequest(
392
+ **new_tool_metadata.model_dump(exclude_none=True)
393
+ )
394
+ create_agent_tool_metadata(agent.id, create_metadata_req, session, ctx)
395
+
396
+
397
+ @router.get("/{agent_id}/tool-metadata", response_model=list[AgentToolMetadataPublic])
398
+ async def list_agent_tool_metadata(
399
+ agent_id: AgentIdPathParam,
400
+ session: DBSessionDep,
401
+ ctx: Context = Depends(get_context)
402
+ ) -> list[AgentToolMetadataPublic]:
403
+ """
404
+ List all agent tool metadata by agent ID.
405
+
406
+ Raises:
407
+ HTTPException: If the agent tool metadata retrieval fails.
408
+ """
409
+ user_id = ctx.get_user_id()
410
+ validate_agent_exists(session, agent_id, user_id)
411
+
412
+ try:
413
+ return agent_tool_metadata_crud.get_all_agent_tool_metadata_by_agent_id(
414
+ session, agent_id
415
+ )
416
+ except Exception as e:
417
+ raise HTTPException(status_code=500, detail=str(e))
418
+
419
+
420
+ @router.post(
421
+ "/{agent_id}/tool-metadata",
422
+ response_model=AgentToolMetadataPublic,
423
+ )
424
+ def create_agent_tool_metadata(
425
+ agent_id: AgentIdPathParam,
426
+ agent_tool_metadata: CreateAgentToolMetadataRequest,
427
+ session: DBSessionDep,
428
+ ctx: Context = Depends(get_context),
429
+ ) -> AgentToolMetadataPublic:
430
+ """
431
+ Create an agent tool metadata.
432
+
433
+ Raises:
434
+ HTTPException: If the agent tool metadata creation fails.
435
+ """
436
+ user_id = ctx.get_user_id()
437
+ agent = validate_agent_exists(session, agent_id, user_id)
438
+ agent_schema = Agent.model_validate(agent)
439
+ ctx.with_agent(agent_schema)
440
+
441
+ agent_tool_metadata_data = AgentToolMetadataModel(
442
+ user_id=user_id,
443
+ agent_id=agent_id,
444
+ tool_name=agent_tool_metadata.tool_name,
445
+ artifacts=agent_tool_metadata.artifacts,
446
+ )
447
+
448
+ try:
449
+ created_agent_tool_metadata = (
450
+ agent_tool_metadata_crud.create_agent_tool_metadata(
451
+ session, agent_tool_metadata_data
452
+ )
453
+ )
454
+
455
+ metadata_schema = AgentToolMetadata.model_validate(created_agent_tool_metadata)
456
+ ctx.with_agent_tool_metadata(metadata_schema)
457
+ except Exception as e:
458
+ raise_db_error(e, "Tool name", agent_tool_metadata.tool_name)
459
+
460
+ return created_agent_tool_metadata
461
+
462
+
463
+ @router.put("/{agent_id}/tool-metadata/{agent_tool_metadata_id}")
464
+ async def update_agent_tool_metadata(
465
+ agent_id: AgentIdPathParam,
466
+ agent_tool_metadata_id: AgentToolMetadataIdPathParam,
467
+ new_agent_tool_metadata: UpdateAgentToolMetadataRequest,
468
+ session: DBSessionDep,
469
+ ctx: Context = Depends(get_context),
470
+ ) -> AgentToolMetadata:
471
+ """
472
+ Update an agent tool metadata by ID.
473
+
474
+ Raises:
475
+ HTTPException: If the agent tool metadata with the given ID is not found.
476
+ HTTPException: If the agent tool metadata update fails.
477
+ """
478
+ user_id = ctx.get_user_id()
479
+ validate_agent_exists(session, agent_id, user_id)
480
+
481
+ agent_tool_metadata = validate_agent_tool_metadata_exists(
482
+ session, agent_tool_metadata_id
483
+ )
484
+
485
+ try:
486
+ agent_tool_metadata_crud.update_agent_tool_metadata(
487
+ session, agent_tool_metadata, new_agent_tool_metadata
488
+ )
489
+ metadata_schema = AgentToolMetadata.model_validate(agent_tool_metadata)
490
+ ctx.with_agent_tool_metadata(metadata_schema)
491
+ except Exception as e:
492
+ raise_db_error(e, "Tool name", agent_tool_metadata.tool_name)
493
+
494
+ return agent_tool_metadata
495
+
496
+
497
+ @router.delete("/{agent_id}/tool-metadata/{agent_tool_metadata_id}")
498
+ async def delete_agent_tool_metadata(
499
+ agent_id: AgentIdPathParam,
500
+ agent_tool_metadata_id: AgentToolMetadataIdPathParam,
501
+ session: DBSessionDep,
502
+ ctx: Context = Depends(get_context),
503
+ ) -> DeleteAgentToolMetadata:
504
+ """
505
+ Delete an agent tool metadata by ID.
506
+
507
+ Raises:
508
+ HTTPException: If the agent tool metadata with the given ID is not found.
509
+ HTTPException: If the agent tool metadata deletion fails.
510
+ """
511
+ user_id = ctx.get_user_id()
512
+ validate_agent_exists(session, agent_id, user_id)
513
+
514
+ agent_tool_metadata = validate_agent_tool_metadata_exists(
515
+ session, agent_tool_metadata_id
516
+ )
517
+
518
+ metadata_schema = AgentToolMetadata.model_validate(agent_tool_metadata)
519
+ ctx.with_agent_tool_metadata(metadata_schema)
520
+ try:
521
+ agent_tool_metadata_crud.delete_agent_tool_metadata_by_id(
522
+ session, agent_tool_metadata_id
523
+ )
524
+ except Exception as e:
525
+ raise HTTPException(status_code=500, detail=str(e))
526
+
527
+ return DeleteAgentToolMetadata()
528
+
529
+
530
+ @router.post("/batch_upload_file", response_model=list[UploadAgentFileResponse])
531
+ async def batch_upload_file(
532
+ *,
533
+ files: list[FastAPIUploadFile] = RequestFile(...),
534
+ session: DBSessionDep,
535
+ ctx: Context = Depends(get_context),
536
+ ) -> UploadAgentFileResponse:
537
+ """
538
+ Upload a batch of files
539
+ """
540
+ user_id = ctx.get_user_id()
541
+
542
+ uploaded_files = []
543
+ try:
544
+ uploaded_files = await get_file_service().create_agent_files(
545
+ session,
546
+ files,
547
+ user_id,
548
+ ctx,
549
+ )
550
+ except Exception as e:
551
+ raise HTTPException(
552
+ status_code=500, detail=f"Error while uploading agent file(s): {e}."
553
+ )
554
+
555
+ return uploaded_files
556
+
557
+
558
+ @router.get("/{agent_id}/files/{file_id}", response_model=FileMetadata)
559
+ async def get_agent_file(
560
+ agent_id: AgentIdPathParam,
561
+ file_id: FileIdPathParam,
562
+ session: DBSessionDep,
563
+ ctx: Context = Depends(get_context),
564
+ ) -> FileMetadata:
565
+ """
566
+ Get an agent file by ID.
567
+
568
+ Raises:
569
+ HTTPException: If the agent or file with the given ID is not found, or if the file does not belong to the agent.
570
+ """
571
+ user_id = ctx.get_user_id()
572
+
573
+ if file_id not in get_file_service().get_file_ids_by_agent_id(session, user_id, agent_id, ctx):
574
+ raise HTTPException(
575
+ status_code=404,
576
+ detail=f"File with ID: {file_id} does not belong to the agent with ID: {agent_id}."
577
+ )
578
+
579
+ file = file_crud.get_file(session, file_id)
580
+
581
+ if not file:
582
+ raise HTTPException(
583
+ status_code=404,
584
+ detail=f"File with ID: {file_id} not found.",
585
+ )
586
+
587
+ return FileMetadata(
588
+ id=file.id,
589
+ file_name=file.file_name,
590
+ file_content=file.file_content,
591
+ file_size=file.file_size,
592
+ created_at=file.created_at,
593
+ updated_at=file.updated_at,
594
+ )
595
+
596
+
597
+ @router.delete("/{agent_id}/files/{file_id}")
598
+ async def delete_agent_file(
599
+ agent_id: AgentIdPathParam,
600
+ file_id: FileIdPathParam,
601
+ session: DBSessionDep,
602
+ ctx: Context = Depends(get_context),
603
+ ) -> DeleteAgentFileResponse:
604
+ """
605
+ Delete an agent file by ID.
606
+
607
+ Raises:
608
+ HTTPException: If the agent with the given ID is not found.
609
+ """
610
+ user_id = ctx.get_user_id()
611
+ _ = validate_agent_exists(session, agent_id, user_id)
612
+ validate_file(session, file_id, user_id)
613
+
614
+ # Delete the File DB object
615
+ get_file_service().delete_agent_file_by_id(session, agent_id, file_id, user_id, ctx)
616
+
617
+ return DeleteAgentFileResponse()
western_ai/canada/cohere-toolkit/src/backend/routers/auth.py ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from typing import Union
3
+ from urllib.parse import quote
4
+
5
+ from fastapi import APIRouter, Depends, HTTPException
6
+ from fastapi.responses import RedirectResponse
7
+ from starlette.requests import Request
8
+
9
+ from backend.config.auth import ENABLED_AUTH_STRATEGY_MAPPING
10
+ from backend.config.routers import RouterName
11
+ from backend.config.settings import Settings
12
+ from backend.config.tools import Tool, get_available_tools
13
+ from backend.crud import blacklist as blacklist_crud
14
+ from backend.database_models import Blacklist
15
+ from backend.database_models.database import DBSessionDep
16
+ from backend.schemas.auth import JWTResponse, ListAuthStrategy, Login, Logout
17
+ from backend.schemas.context import Context
18
+ from backend.schemas.params.auth import CodeQueryParam, StrategyPathParam
19
+ from backend.schemas.params.tool import ToolIdPathParam
20
+ from backend.schemas.tool_auth import DeleteToolAuth
21
+ from backend.services.auth.jwt import JWTService
22
+ from backend.services.auth.request_validators import validate_authorization
23
+ from backend.services.auth.utils import (
24
+ get_or_create_user,
25
+ is_enabled_authentication_strategy,
26
+ )
27
+ from backend.services.cache import cache_get_dict
28
+ from backend.services.context import get_context
29
+
30
+ router = APIRouter(
31
+ prefix="/v1",
32
+ tags=[RouterName.AUTH],
33
+ )
34
+ router.name = RouterName.AUTH
35
+
36
+
37
+ @router.get("/auth_strategies", response_model=list[ListAuthStrategy])
38
+ def get_strategies(
39
+ ctx: Context = Depends(get_context),
40
+ ) -> list[ListAuthStrategy]:
41
+ """
42
+ Retrieves the currently enabled list of Authentication strategies.
43
+ """
44
+ strategies = []
45
+ for strategy_name, strategy_instance in ENABLED_AUTH_STRATEGY_MAPPING.items():
46
+ strategies.append(
47
+ {
48
+ "strategy": strategy_name,
49
+ "client_id": (
50
+ strategy_instance.get_client_id()
51
+ if hasattr(strategy_instance, "get_client_id")
52
+ else None
53
+ ),
54
+ "authorization_endpoint": (
55
+ strategy_instance.get_authorization_endpoint()
56
+ if hasattr(strategy_instance, "get_authorization_endpoint")
57
+ else None
58
+ ),
59
+ "pkce_enabled": (
60
+ strategy_instance.get_pkce_enabled()
61
+ if hasattr(strategy_instance, "get_pkce_enabled")
62
+ else False
63
+ ),
64
+ }
65
+ )
66
+
67
+ return strategies
68
+
69
+
70
+ @router.post("/login", response_model=Union[JWTResponse, None])
71
+ async def login(
72
+ login: Login, session: DBSessionDep, ctx: Context = Depends(get_context)
73
+ ):
74
+ """
75
+ Logs user in, performing basic email/password auth.
76
+ Verifies their credentials, retrieves the user and returns a JWT token.
77
+
78
+ Raises:
79
+ HTTPException: If the strategy or payload are invalid, or if the login fails.
80
+ """
81
+ logger = ctx.get_logger()
82
+ strategy_name = login.strategy
83
+ payload = login.payload
84
+
85
+ if not is_enabled_authentication_strategy(strategy_name):
86
+ logger.error(
87
+ event=f"[Auth] Error logging in: Invalid authentication strategy {strategy_name}",
88
+ )
89
+ raise HTTPException(
90
+ status_code=422, detail=f"Invalid Authentication strategy: {strategy_name}."
91
+ )
92
+
93
+ # Check that the payload required is given
94
+ strategy = ENABLED_AUTH_STRATEGY_MAPPING[strategy_name]
95
+ strategy_payload = strategy.get_required_payload()
96
+ if not set(strategy_payload).issubset(payload.keys()):
97
+ missing_keys = [key for key in strategy_payload if key not in payload.keys()]
98
+ logger.error(
99
+ event=f"[Auth] Error logging in: Keys {missing_keys} missing from payload",
100
+ )
101
+ raise HTTPException(
102
+ status_code=422,
103
+ detail=f"Missing the following keys in the payload: {missing_keys}.",
104
+ )
105
+
106
+ user = strategy.login(session, payload)
107
+ if not user:
108
+ logger.error(
109
+ event=f"[Auth] Error logging in: Invalid credentials in payload {payload}",
110
+ )
111
+ raise HTTPException(
112
+ status_code=401,
113
+ detail=f"Error performing {strategy_name} authentication with payload: {payload}.",
114
+ )
115
+
116
+ token = JWTService().create_and_encode_jwt(user)
117
+
118
+ return {"token": token}
119
+
120
+
121
+ @router.post("/{strategy}/auth", response_model=JWTResponse)
122
+ async def authorize(
123
+ *,
124
+ strategy: StrategyPathParam,
125
+ request: Request,
126
+ code: CodeQueryParam = None,
127
+ session: DBSessionDep,
128
+ ctx: Context = Depends(get_context),
129
+ ):
130
+ """
131
+ Callback authorization endpoint used for OAuth providers after authenticating on the provider's login screen.
132
+
133
+ Raises:
134
+ HTTPException: If authentication fails, or strategy is invalid.
135
+ """
136
+ logger = ctx.get_logger()
137
+
138
+ if not code:
139
+ logger.error(
140
+ event="[Auth] Error authorizing login: No code provided",
141
+ )
142
+ raise HTTPException(
143
+ status_code=400,
144
+ detail="Error calling /auth with invalid code query parameter.",
145
+ )
146
+
147
+ strategy_name = None
148
+ for enabled_strategy_name in ENABLED_AUTH_STRATEGY_MAPPING.keys():
149
+ if enabled_strategy_name.lower() == strategy.lower():
150
+ strategy_name = enabled_strategy_name
151
+
152
+ if not strategy_name:
153
+ logger.error(
154
+ event=f"[Auth] Error authorizing login: Invalid strategy {strategy_name}",
155
+ )
156
+ raise HTTPException(
157
+ status_code=400,
158
+ detail=f"Error calling /auth with invalid strategy name: {strategy_name}.",
159
+ )
160
+
161
+ if not is_enabled_authentication_strategy(strategy_name):
162
+ logger.error(
163
+ event=f"[Auth] Error authorizing login: Strategy {strategy_name} not enabled",
164
+ )
165
+ raise HTTPException(
166
+ status_code=404, detail=f"Invalid Authentication strategy: {strategy_name}."
167
+ )
168
+
169
+ strategy = ENABLED_AUTH_STRATEGY_MAPPING[strategy_name]
170
+
171
+ try:
172
+ userinfo = await strategy.authorize(request)
173
+ except Exception as e:
174
+ raise HTTPException(
175
+ status_code=400,
176
+ detail=f"Could not fetch access token from provider, failed with error: {str(e)}",
177
+ )
178
+
179
+ if not userinfo:
180
+ logger.error(
181
+ event="[Auth] Error authorizing login: Invalid token",
182
+ )
183
+ raise HTTPException(
184
+ status_code=401, detail="Could not get user from auth token."
185
+ )
186
+
187
+ # Get or create user, then set session user
188
+ user = get_or_create_user(session, userinfo)
189
+
190
+ token = JWTService().create_and_encode_jwt(user)
191
+
192
+ return {"token": token}
193
+
194
+
195
+ @router.get("/logout", response_model=Logout)
196
+ async def logout(
197
+ session: DBSessionDep,
198
+ token: dict | None = Depends(validate_authorization),
199
+ ctx: Context = Depends(get_context),
200
+ ):
201
+ """
202
+ Logs out the current user, adding the given JWT token to the blacklist.
203
+ """
204
+ if token is not None:
205
+ db_blacklist = Blacklist(token_id=token["jti"])
206
+ blacklist_crud.create_blacklist(session, db_blacklist)
207
+
208
+ return {}
209
+
210
+
211
+ @router.get("/tool/auth")
212
+ async def tool_auth(
213
+ request: Request,
214
+ session: DBSessionDep,
215
+ ctx: Context = Depends(get_context),
216
+ ) -> RedirectResponse:
217
+ """
218
+ Endpoint for Tool Authentication. Note: The flow is different from
219
+ the regular login OAuth flow, the backend initiates it and redirects to the frontend
220
+ after completion.
221
+
222
+ If completed, a ToolAuth is stored in the DB containing the access token for the tool.
223
+
224
+ Returns:
225
+ RedirectResponse: A redirect pointing to the frontend, contains an error query parameter if
226
+ an unexpected error happens during the authentication.
227
+
228
+ Raises:
229
+ HTTPException: If no redirect_uri set.
230
+ """
231
+ logger = ctx.get_logger()
232
+ redirect_uri = Settings().get('auth.frontend_hostname')
233
+
234
+ if not redirect_uri:
235
+ raise HTTPException(
236
+ status_code=400,
237
+ detail="auth.frontend_hostname in configuration.yaml is required for Tool Auth.",
238
+ )
239
+
240
+ def log_and_redirect_err(error_message: str):
241
+ logger.error(event=error_message)
242
+ redirect_err = f"{redirect_uri}?error={quote(error_message)}"
243
+ return RedirectResponse(redirect_err)
244
+
245
+ # Get key from state and retrieve cache for user_id and tool_id
246
+ try:
247
+ state = json.loads(request.query_params.get("state"))
248
+ cache_key = state["key"]
249
+ tool_auth_cache = cache_get_dict(cache_key)
250
+
251
+ # Get optional frontend redirect
252
+ if "frontend_redirect" in state:
253
+ redirect_uri = state["frontend_redirect"]
254
+ except Exception as e:
255
+ log_and_redirect_err(str(e))
256
+
257
+ user_id = tool_auth_cache.get("user_id")
258
+ tool_id = tool_auth_cache.get("tool_id")
259
+
260
+ if not tool_auth_cache:
261
+ err = f"Error retrieving cache for Tool Auth with key: {cache_key}"
262
+ log_and_redirect_err(err)
263
+
264
+ if user_id is None or tool_id is None:
265
+ err = f"Tool Auth cache {tool_auth_cache} does not contain user_id or tool_id."
266
+ log_and_redirect_err(err)
267
+
268
+ available_tools = get_available_tools()
269
+ if tool_id in available_tools:
270
+ tool = available_tools.get(tool_id)
271
+ err = None
272
+
273
+ # Tool not found
274
+ if not tool:
275
+ err = f"Tool {tool_id} does not exist or is not available."
276
+ log_and_redirect_err(err)
277
+
278
+ # Tool does not have Auth implemented
279
+ if tool.auth_implementation is None:
280
+ err = f"Tool {tool.name} does not have an auth_implementation required for Tool Auth."
281
+ log_and_redirect_err(err)
282
+
283
+ try:
284
+ tool_auth_service = tool.auth_implementation()
285
+ err = tool_auth_service.retrieve_auth_token(request, session, user_id)
286
+ except Exception as e:
287
+ log_and_redirect_err(str(e))
288
+
289
+ if err:
290
+ log_and_redirect_err(err)
291
+
292
+ response = RedirectResponse(redirect_uri)
293
+
294
+ return response
295
+
296
+
297
+ @router.delete("/tool/auth/{tool_id}")
298
+ async def delete_tool_auth(
299
+ tool_id: ToolIdPathParam,
300
+ request: Request,
301
+ session: DBSessionDep,
302
+ ctx: Context = Depends(get_context),
303
+ ) -> DeleteToolAuth:
304
+ """
305
+ Endpoint to delete Tool Authentication.
306
+
307
+ If completed, the corresponding ToolAuth for the requesting user is removed from the DB.
308
+
309
+ Raises:
310
+ HTTPException: If there was an error deleting the tool auth.
311
+ """
312
+
313
+ logger = ctx.get_logger()
314
+
315
+ user_id = ctx.get_user_id()
316
+ tool_id = tool_id.lower()
317
+
318
+ if user_id is None or user_id == "" or user_id == "default":
319
+ logger.error_and_raise_http_exception(event="User ID not found.")
320
+
321
+ if tool_id not in [tool_name.value.ID for tool_name in Tool]:
322
+ logger.error_and_raise_http_exception(
323
+ event="tool_id must be present in the path of the request and must be a member of the Tool string enum class.",
324
+ )
325
+
326
+ tool = get_available_tools().get(tool_id)
327
+
328
+ if tool is None:
329
+ logger.error_and_raise_http_exception(
330
+ event=f"Tool {tool_id} is not available."
331
+ )
332
+
333
+ if tool.auth_implementation is None:
334
+ logger.error_and_raise_http_exception(
335
+ event=f"Tool {tool.name} does not have an auth_implementation required for Tool Auth Deletion.",
336
+ )
337
+
338
+ try:
339
+ tool_auth_service = tool.auth_implementation()
340
+ is_deleted = tool_auth_service.delete_tool_auth(session, user_id)
341
+
342
+ if not is_deleted:
343
+ logger.error_and_raise_http_exception(event="Error deleting Tool Auth.")
344
+
345
+ except Exception as e:
346
+ logger.error_and_raise_http_exception(event=str(e))
347
+
348
+ return DeleteToolAuth()
western_ai/canada/cohere-toolkit/src/backend/routers/chat.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Generator
2
+
3
+ from fastapi import APIRouter, Depends
4
+ from sse_starlette.sse import EventSourceResponse
5
+
6
+ from backend.chat.custom.custom import CustomChat
7
+ from backend.config.routers import RouterName
8
+ from backend.crud import agent_tool_metadata as agent_tool_metadata_crud
9
+ from backend.database_models.database import DBSessionDep
10
+ from backend.schemas.agent import Agent, AgentToolMetadata
11
+ from backend.schemas.chat import ChatResponseEvent, NonStreamedChatResponse
12
+ from backend.schemas.cohere_chat import CohereChatRequest
13
+ from backend.schemas.context import Context
14
+ from backend.services.agent import validate_agent_exists
15
+ from backend.services.chat import (
16
+ generate_chat_response,
17
+ generate_chat_stream,
18
+ process_chat,
19
+ process_message_regeneration,
20
+ )
21
+ from backend.services.context import get_context
22
+ from backend.services.request_validators import validate_deployment_header
23
+
24
+ router = APIRouter(
25
+ prefix="/v1",
26
+ tags=[RouterName.CHAT],
27
+ )
28
+ router.name = RouterName.CHAT
29
+
30
+
31
+ @router.post("/chat-stream", dependencies=[Depends(validate_deployment_header)])
32
+ async def chat_stream(
33
+ chat_request: CohereChatRequest,
34
+ session: DBSessionDep,
35
+ ctx: Context = Depends(get_context),
36
+ ) -> Generator[ChatResponseEvent, Any, None]:
37
+ """
38
+ Stream chat endpoint to handle user messages and return chatbot responses.
39
+ """
40
+ ctx.with_model(chat_request.model)
41
+ agent_id = chat_request.agent_id
42
+ ctx.with_agent_id(agent_id)
43
+
44
+ (
45
+ session,
46
+ chat_request,
47
+ response_message,
48
+ should_store,
49
+ managed_tools,
50
+ next_message_position,
51
+ ctx,
52
+ ) = process_chat(session, chat_request, ctx)
53
+
54
+ return EventSourceResponse(
55
+ generate_chat_stream(
56
+ session,
57
+ CustomChat().chat(
58
+ chat_request,
59
+ stream=True,
60
+ managed_tools=managed_tools,
61
+ session=session,
62
+ ctx=ctx,
63
+ ),
64
+ response_message,
65
+ should_store=should_store,
66
+ next_message_position=next_message_position,
67
+ ctx=ctx,
68
+ ),
69
+ media_type="text/event-stream",
70
+ headers={"Connection": "keep-alive"},
71
+ send_timeout=300,
72
+ ping=5,
73
+ )
74
+
75
+
76
+ @router.post("/chat-stream/regenerate", dependencies=[Depends(validate_deployment_header)])
77
+ async def regenerate_chat_stream(
78
+ chat_request: CohereChatRequest,
79
+ session: DBSessionDep,
80
+ ctx: Context = Depends(get_context),
81
+ ) -> EventSourceResponse:
82
+ """
83
+ Endpoint to regenerate stream chat response for the last user message.
84
+ """
85
+ ctx.with_model(chat_request.model)
86
+
87
+ agent_id = chat_request.agent_id
88
+ ctx.with_agent_id(agent_id)
89
+
90
+ if agent_id:
91
+ agent = validate_agent_exists(session, agent_id, ctx.get_user_id())
92
+ ctx.with_agent(Agent.model_validate(agent))
93
+
94
+ agent_tool_metadata = (
95
+ agent_tool_metadata_crud.get_all_agent_tool_metadata_by_agent_id(
96
+ session, agent_id
97
+ )
98
+ )
99
+ agent_tool_metadata_schema = [
100
+ AgentToolMetadata.model_validate(x) for x in agent_tool_metadata
101
+ ]
102
+ ctx.with_agent_tool_metadata(agent_tool_metadata_schema)
103
+
104
+ (
105
+ session,
106
+ chat_request,
107
+ new_response_message,
108
+ previous_response_message_ids,
109
+ managed_tools,
110
+ ctx,
111
+ ) = process_message_regeneration(session, chat_request, ctx)
112
+
113
+ return EventSourceResponse(
114
+ generate_chat_stream(
115
+ session,
116
+ CustomChat().chat(
117
+ chat_request,
118
+ stream=True,
119
+ managed_tools=managed_tools,
120
+ session=session,
121
+ ctx=ctx,
122
+ ),
123
+ new_response_message,
124
+ next_message_position=new_response_message.position,
125
+ previous_response_message_ids=previous_response_message_ids,
126
+ ctx=ctx,
127
+ ),
128
+ media_type="text/event-stream",
129
+ headers={"Connection": "keep-alive"},
130
+ send_timeout=300,
131
+ ping=5,
132
+ )
133
+
134
+
135
+ @router.post("/chat", dependencies=[Depends(validate_deployment_header)])
136
+ async def chat(
137
+ chat_request: CohereChatRequest,
138
+ session: DBSessionDep,
139
+ ctx: Context = Depends(get_context),
140
+ ) -> NonStreamedChatResponse:
141
+ """
142
+ Chat endpoint to handle user messages and return chatbot responses.
143
+ """
144
+ ctx.with_model(chat_request.model)
145
+ agent_id = chat_request.agent_id
146
+ ctx.with_agent_id(agent_id)
147
+ user_id = ctx.get_user_id()
148
+
149
+ if agent_id:
150
+ agent = validate_agent_exists(session, agent_id, user_id)
151
+ agent_schema = Agent.model_validate(agent)
152
+ ctx.with_agent(agent_schema)
153
+ agent_tool_metadata = (
154
+ agent_tool_metadata_crud.get_all_agent_tool_metadata_by_agent_id(
155
+ session, agent_id
156
+ )
157
+ )
158
+ agent_tool_metadata_schema = [
159
+ AgentToolMetadata.model_validate(x) for x in agent_tool_metadata
160
+ ]
161
+ ctx.with_agent_tool_metadata(agent_tool_metadata_schema)
162
+
163
+ (
164
+ session,
165
+ chat_request,
166
+ response_message,
167
+ should_store,
168
+ managed_tools,
169
+ next_message_position,
170
+ ctx,
171
+ ) = process_chat(session, chat_request, ctx)
172
+
173
+ response = await generate_chat_response(
174
+ session,
175
+ CustomChat().chat(
176
+ chat_request,
177
+ session=session,
178
+ stream=False,
179
+ managed_tools=managed_tools,
180
+ ctx=ctx,
181
+ ),
182
+ response_message,
183
+ should_store=should_store,
184
+ next_message_position=next_message_position,
185
+ ctx=ctx,
186
+ )
187
+ return response
western_ai/canada/cohere-toolkit/src/backend/routers/conversation.py ADDED
@@ -0,0 +1,544 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends, Form, HTTPException
2
+ from fastapi import File as RequestFile
3
+ from fastapi import UploadFile as FastAPIUploadFile
4
+ from starlette.responses import Response
5
+
6
+ from backend.chat.custom.utils import get_deployment
7
+ from backend.config.routers import RouterName
8
+ from backend.crud import agent as agent_crud
9
+ from backend.crud import conversation as conversation_crud
10
+ from backend.crud import message as message_crud
11
+ from backend.database_models import Conversation as ConversationModel
12
+ from backend.database_models.database import DBSessionDep
13
+ from backend.schemas.agent import Agent
14
+ from backend.schemas.context import Context
15
+ from backend.schemas.conversation import (
16
+ ConversationPublic,
17
+ ConversationWithoutMessages,
18
+ DeleteConversationResponse,
19
+ GenerateTitleResponse,
20
+ ToggleConversationPinRequest,
21
+ UpdateConversationRequest,
22
+ )
23
+ from backend.schemas.file import (
24
+ DeleteConversationFileResponse,
25
+ FileMetadata,
26
+ ListConversationFile,
27
+ UploadConversationFileResponse,
28
+ )
29
+ from backend.schemas.params.agent import AgentIdQueryParam
30
+ from backend.schemas.params.conversation import ConversationIdPathParam, QueryQueryParam
31
+ from backend.schemas.params.file import FileIdPathParam
32
+ from backend.schemas.params.message import MessageIdPathParam
33
+ from backend.schemas.params.model import ModelQueryParam
34
+ from backend.schemas.params.shared import OrderByQueryParam, PaginationQueryParams
35
+ from backend.services.agent import validate_agent_exists
36
+ from backend.services.context import get_context
37
+ from backend.services.conversation import (
38
+ filter_conversations,
39
+ generate_conversation_title,
40
+ get_documents_to_rerank,
41
+ get_messages_with_files,
42
+ validate_conversation,
43
+ )
44
+ from backend.services.file import (
45
+ attach_conversation_id_to_files,
46
+ get_file_service,
47
+ validate_file,
48
+ )
49
+ from backend.services.synthesizer import synthesize
50
+
51
+ router = APIRouter(
52
+ prefix="/v1/conversations",
53
+ tags=[RouterName.CONVERSATION],
54
+ )
55
+ router.name = RouterName.CONVERSATION
56
+
57
+
58
+ # CONVERSATIONS
59
+ @router.get("/{conversation_id}", response_model=ConversationPublic)
60
+ async def get_conversation(
61
+ conversation_id: ConversationIdPathParam,
62
+ session: DBSessionDep,
63
+ ctx: Context = Depends(get_context),
64
+ ) -> ConversationPublic:
65
+ """
66
+ Get a conversation by ID.
67
+
68
+ Raises:
69
+ HTTPException: If the conversation with the given ID is not found.
70
+ """
71
+ user_id = ctx.get_user_id()
72
+ conversation = conversation_crud.get_conversation(
73
+ session, conversation_id, user_id)
74
+
75
+ if not conversation:
76
+ raise HTTPException(
77
+ status_code=404,
78
+ detail=f"Conversation with ID: {conversation_id} not found.",
79
+ )
80
+
81
+ files = get_file_service().get_files_by_conversation_id(
82
+ session, user_id, conversation.id, ctx
83
+ )
84
+ files_with_conversation_id = attach_conversation_id_to_files(
85
+ conversation.id, files)
86
+ messages = get_messages_with_files(
87
+ session, user_id, conversation.messages, ctx)
88
+ _ = validate_conversation(session, conversation_id, user_id)
89
+
90
+ conversation = ConversationPublic(
91
+ id=conversation.id,
92
+ user_id=user_id,
93
+ created_at=conversation.created_at,
94
+ updated_at=conversation.updated_at,
95
+ title=conversation.title,
96
+ messages=messages,
97
+ files=files_with_conversation_id,
98
+ description=conversation.description,
99
+ agent_id=conversation.agent_id,
100
+ organization_id=conversation.organization_id,
101
+ is_pinned=conversation.is_pinned,
102
+ )
103
+
104
+ _ = validate_conversation(session, conversation_id, user_id)
105
+ return conversation
106
+
107
+
108
+ @router.get("", response_model=list[ConversationWithoutMessages])
109
+ async def list_conversations(
110
+ *,
111
+ page_params: PaginationQueryParams,
112
+ order_by: OrderByQueryParam = None,
113
+ agent_id: AgentIdQueryParam = None,
114
+ session: DBSessionDep,
115
+ ctx: Context = Depends(get_context),
116
+ ) -> list[ConversationWithoutMessages]:
117
+ """
118
+ List all conversations.
119
+ """
120
+ user_id = ctx.get_user_id()
121
+
122
+ conversations = conversation_crud.get_conversations(
123
+ session, offset=page_params.offset, limit=page_params.limit, order_by=order_by, user_id=user_id, agent_id=agent_id
124
+ )
125
+
126
+ results = []
127
+ for conversation in conversations:
128
+ files = get_file_service().get_files_by_conversation_id(
129
+ session, user_id, conversation.id, ctx
130
+ )
131
+ files_with_conversation_id = attach_conversation_id_to_files(
132
+ conversation.id, files
133
+ )
134
+ results.append(
135
+ ConversationWithoutMessages(
136
+ id=conversation.id,
137
+ user_id=user_id,
138
+ created_at=conversation.created_at,
139
+ updated_at=conversation.updated_at,
140
+ title=conversation.title,
141
+ files=files_with_conversation_id,
142
+ description=conversation.description,
143
+ agent_id=conversation.agent_id,
144
+ messages=[],
145
+ organization_id=conversation.organization_id,
146
+ is_pinned=conversation.is_pinned,
147
+ )
148
+ )
149
+
150
+ return results
151
+
152
+
153
+ @router.put("/{conversation_id}", response_model=ConversationPublic)
154
+ async def update_conversation(
155
+ conversation_id: ConversationIdPathParam,
156
+ new_conversation: UpdateConversationRequest,
157
+ session: DBSessionDep,
158
+ ctx: Context = Depends(get_context),
159
+ ) -> ConversationPublic:
160
+ """
161
+ Update a conversation by ID.
162
+
163
+ Raises:
164
+ HTTPException: If the conversation with the given ID is not found.
165
+ """
166
+ user_id = ctx.get_user_id()
167
+ conversation = validate_conversation(session, conversation_id, user_id)
168
+ conversation = conversation_crud.update_conversation(
169
+ session, conversation, new_conversation
170
+ )
171
+
172
+ files = get_file_service().get_files_by_conversation_id(
173
+ session, user_id, conversation.id, ctx
174
+ )
175
+ messages = get_messages_with_files(
176
+ session, user_id, conversation.messages, ctx)
177
+ files_with_conversation_id = attach_conversation_id_to_files(
178
+ conversation.id, files)
179
+ return ConversationPublic(
180
+ id=conversation.id,
181
+ user_id=user_id,
182
+ created_at=conversation.created_at,
183
+ updated_at=conversation.updated_at,
184
+ title=conversation.title,
185
+ messages=messages,
186
+ files=files_with_conversation_id,
187
+ description=conversation.description,
188
+ agent_id=conversation.agent_id,
189
+ organization_id=conversation.organization_id,
190
+ is_pinned=conversation.is_pinned,
191
+ )
192
+
193
+
194
+ @router.put("/{conversation_id}/toggle-pin", response_model=ConversationWithoutMessages)
195
+ async def toggle_conversation_pin(
196
+ conversation_id: ConversationIdPathParam,
197
+ new_conversation_pin: ToggleConversationPinRequest,
198
+ session: DBSessionDep,
199
+ ctx: Context = Depends(get_context),
200
+ ) -> ConversationWithoutMessages:
201
+ """
202
+ Toggle whether a conversation is pinned or not
203
+ """
204
+ user_id = ctx.get_user_id()
205
+ conversation = validate_conversation(session, conversation_id, user_id)
206
+ conversation = conversation_crud.toggle_conversation_pin(
207
+ session, conversation, new_conversation_pin
208
+ )
209
+ files = get_file_service().get_files_by_conversation_id(
210
+ session, user_id, conversation.id, ctx
211
+ )
212
+ files_with_conversation_id = attach_conversation_id_to_files(
213
+ conversation.id, files
214
+ )
215
+ return ConversationWithoutMessages(
216
+ id=conversation.id,
217
+ user_id=user_id,
218
+ created_at=conversation.created_at,
219
+ updated_at=conversation.updated_at,
220
+ title=conversation.title,
221
+ files=files_with_conversation_id,
222
+ description=conversation.description,
223
+ agent_id=conversation.agent_id,
224
+ messages=[],
225
+ organization_id=conversation.organization_id,
226
+ is_pinned=conversation.is_pinned,
227
+ )
228
+
229
+
230
+ @router.delete("/{conversation_id}")
231
+ async def delete_conversation(
232
+ conversation_id: ConversationIdPathParam,
233
+ session: DBSessionDep,
234
+ ctx: Context = Depends(get_context),
235
+ ) -> DeleteConversationResponse:
236
+ """
237
+ Delete a conversation by ID.
238
+
239
+ Raises:
240
+ HTTPException: If the conversation with the given ID is not found.
241
+ """
242
+ user_id = ctx.get_user_id()
243
+ conversation = validate_conversation(session, conversation_id, user_id)
244
+
245
+ get_file_service().delete_all_conversation_files(
246
+ session, conversation.id, conversation.file_ids, user_id, ctx
247
+ )
248
+ conversation_crud.delete_conversation(session, conversation_id, user_id)
249
+
250
+ return DeleteConversationResponse()
251
+
252
+
253
+ @router.get(":search", response_model=list[ConversationWithoutMessages])
254
+ async def search_conversations(
255
+ *,
256
+ query: QueryQueryParam,
257
+ page_params: PaginationQueryParams,
258
+ order_by: OrderByQueryParam = None,
259
+ agent_id: AgentIdQueryParam = None,
260
+ session: DBSessionDep,
261
+ ctx: Context = Depends(get_context),
262
+ ) -> list[ConversationWithoutMessages]:
263
+ """
264
+ Search conversations by title.
265
+ """
266
+ user_id = ctx.get_user_id()
267
+ deployment_name = ctx.get_deployment_name()
268
+ model_deployment = get_deployment(deployment_name, session, ctx)
269
+
270
+ agent = None
271
+ if agent_id:
272
+ agent = validate_agent_exists(session, agent_id, user_id)
273
+
274
+ if agent_id:
275
+ agent_schema = Agent.model_validate(agent)
276
+ ctx.with_agent(agent_schema)
277
+
278
+ conversations = conversation_crud.get_conversations(
279
+ session, offset=page_params.offset, limit=page_params.limit, order_by=order_by, user_id=user_id, agent_id=agent_id
280
+ )
281
+
282
+ if not conversations:
283
+ return []
284
+
285
+ rerank_documents = get_documents_to_rerank(conversations)
286
+ filtered_documents = await filter_conversations(
287
+ query,
288
+ conversations,
289
+ rerank_documents,
290
+ model_deployment,
291
+ ctx,
292
+ )
293
+
294
+ results = []
295
+ for conversation in filtered_documents:
296
+ files = get_file_service().get_files_by_conversation_id(
297
+ session, user_id, conversation.id, ctx
298
+ )
299
+ files_with_conversation_id = attach_conversation_id_to_files(
300
+ conversation.id, files
301
+ )
302
+ results.append(
303
+ ConversationWithoutMessages(
304
+ id=conversation.id,
305
+ user_id=user_id,
306
+ created_at=conversation.created_at,
307
+ updated_at=conversation.updated_at,
308
+ title=conversation.title,
309
+ files=files_with_conversation_id,
310
+ description=conversation.description,
311
+ agent_id=conversation.agent_id,
312
+ messages=[],
313
+ organization_id=conversation.organization_id,
314
+ is_pinned=conversation.is_pinned,
315
+ )
316
+ )
317
+ return results
318
+
319
+
320
+ # FILES
321
+ @router.post("/batch_upload_file", response_model=list[UploadConversationFileResponse])
322
+ async def batch_upload_file(
323
+ *,
324
+ conversation_id: str = Form(None),
325
+ files: list[FastAPIUploadFile] = RequestFile(...),
326
+ session: DBSessionDep,
327
+ ctx: Context = Depends(get_context),
328
+ ) -> UploadConversationFileResponse:
329
+ """
330
+ Uploads and creates a batch of File object.
331
+ If no conversation_id is provided, a new Conversation is created as well.
332
+
333
+ Raises:
334
+ HTTPException: If the conversation with the given ID is not found. Status code 404.
335
+ HTTPException: If the file wasn't uploaded correctly. Status code 500.
336
+ """
337
+
338
+ user_id = ctx.get_user_id()
339
+
340
+ # Create new conversation
341
+ if not conversation_id:
342
+ conversation = conversation_crud.create_conversation(
343
+ session,
344
+ ConversationModel(user_id=user_id),
345
+ )
346
+ # Check for existing conversation
347
+ else:
348
+ conversation = conversation_crud.get_conversation(
349
+ session, conversation_id, user_id
350
+ )
351
+
352
+ # Fail if user_id is not provided when conversation DNE
353
+ if not conversation:
354
+ if not user_id:
355
+ raise HTTPException(
356
+ status_code=400,
357
+ detail="user_id is required if no valid conversation is provided.",
358
+ )
359
+
360
+ # Create new conversation
361
+ conversation = conversation_crud.create_conversation(
362
+ session,
363
+ ConversationModel(user_id=user_id),
364
+ )
365
+
366
+ # TODO: check if file already exists in DB once we have files per agents
367
+
368
+ try:
369
+ uploaded_files = await get_file_service().create_conversation_files(
370
+ session,
371
+ files,
372
+ user_id,
373
+ conversation.id,
374
+ ctx,
375
+ )
376
+ except Exception as e:
377
+ raise HTTPException(
378
+ status_code=500, detail=f"Error while uploading file(s): {e}."
379
+ )
380
+
381
+ files_with_conversation_id = attach_conversation_id_to_files(
382
+ conversation.id, uploaded_files
383
+ )
384
+ return files_with_conversation_id
385
+
386
+
387
+ @router.get("/{conversation_id}/files", response_model=list[ListConversationFile])
388
+ async def list_files(
389
+ conversation_id: ConversationIdPathParam,
390
+ session: DBSessionDep, ctx:
391
+ Context = Depends(get_context),
392
+ ) -> list[ListConversationFile]:
393
+ """
394
+ List all files from a conversation. Important - no pagination support yet.
395
+
396
+ Raises:
397
+ HTTPException: If the conversation with the given ID is not found.
398
+ """
399
+ user_id = ctx.get_user_id()
400
+ _ = validate_conversation(session, conversation_id, user_id)
401
+
402
+ files = get_file_service().get_files_by_conversation_id(
403
+ session, user_id, conversation_id, ctx
404
+ )
405
+ files_with_conversation_id = attach_conversation_id_to_files(
406
+ conversation_id, files)
407
+ return files_with_conversation_id
408
+
409
+
410
+ @router.get("/{conversation_id}/files/{file_id}", response_model=FileMetadata)
411
+ async def get_file(
412
+ conversation_id: ConversationIdPathParam,
413
+ file_id: FileIdPathParam,
414
+ session: DBSessionDep,
415
+ ctx: Context = Depends(get_context),
416
+ ) -> FileMetadata:
417
+ """
418
+ Get a conversation file by ID.
419
+
420
+ Raises:
421
+ HTTPException: If the conversation or file with the given ID is not found, or if the file does not belong to the conversation.
422
+ """
423
+ user_id = ctx.get_user_id()
424
+
425
+ conversation = validate_conversation(session, conversation_id, user_id)
426
+
427
+ if file_id not in conversation.file_ids:
428
+ raise HTTPException(
429
+ status_code=404,
430
+ detail=f"File with ID: {file_id} does not belong to the conversation with ID: {conversation.id}."
431
+ )
432
+
433
+ file = validate_file(session, file_id, user_id)
434
+
435
+ return FileMetadata(
436
+ id=file.id,
437
+ file_name=file.file_name,
438
+ file_content=file.file_content,
439
+ file_size=file.file_size,
440
+ created_at=file.created_at,
441
+ updated_at=file.updated_at,
442
+ )
443
+
444
+
445
+ @router.delete("/{conversation_id}/files/{file_id}")
446
+ async def delete_file(
447
+ conversation_id: ConversationIdPathParam,
448
+ file_id: FileIdPathParam,
449
+ session: DBSessionDep,
450
+ ctx: Context = Depends(get_context),
451
+ ) -> DeleteConversationFileResponse:
452
+ """
453
+ Delete a file by ID.
454
+
455
+ Raises:
456
+ HTTPException: If the conversation with the given ID is not found.
457
+ """
458
+ user_id = ctx.get_user_id()
459
+ _ = validate_conversation(session, conversation_id, user_id)
460
+ validate_file(session, file_id, user_id)
461
+ # Delete the File DB object
462
+ get_file_service().delete_conversation_file_by_id(
463
+ session, conversation_id, file_id, user_id, ctx
464
+ )
465
+
466
+ return DeleteConversationFileResponse()
467
+
468
+
469
+ # MISC
470
+ @router.post("/{conversation_id}/generate-title", response_model=GenerateTitleResponse)
471
+ async def generate_title(
472
+ *,
473
+ conversation_id: ConversationIdPathParam,
474
+ model: ModelQueryParam = "command-r",
475
+ session: DBSessionDep,
476
+ ctx: Context = Depends(get_context),
477
+ ) -> GenerateTitleResponse:
478
+ """
479
+ Generate a title for a conversation and update the conversation with the generated title.
480
+
481
+ Raises:
482
+ HTTPException: If the conversation with the given ID is not found.
483
+ """
484
+ user_id = ctx.get_user_id()
485
+ ctx.with_deployment_config()
486
+ ctx.with_model(model)
487
+
488
+ conversation = validate_conversation(session, conversation_id, user_id)
489
+ agent_id = conversation.agent_id if conversation.agent_id else None
490
+
491
+ if agent_id:
492
+ agent = agent_crud.get_agent_by_id(session, agent_id, user_id)
493
+ agent_schema = Agent.model_validate(agent)
494
+ ctx.with_agent(agent_schema)
495
+
496
+ title, error = await generate_conversation_title(
497
+ session,
498
+ conversation,
499
+ agent_id,
500
+ ctx,
501
+ model,
502
+ )
503
+
504
+ conversation_crud.update_conversation(
505
+ session, conversation, UpdateConversationRequest(title=title)
506
+ )
507
+
508
+ return GenerateTitleResponse(
509
+ title=title,
510
+ error=error,
511
+ )
512
+
513
+
514
+ # SYNTHESIZE
515
+ @router.get("/{conversation_id}/synthesize/{message_id}")
516
+ async def synthesize_message(
517
+ conversation_id: ConversationIdPathParam,
518
+ message_id: MessageIdPathParam,
519
+ session: DBSessionDep,
520
+ ctx: Context = Depends(get_context),
521
+ ) -> Response:
522
+ """
523
+ Generate a synthesized audio for a specific message in a conversation.
524
+
525
+ Raises:
526
+ HTTPException: If the message with the given ID is not found or synthesis fails.
527
+ """
528
+ user_id = ctx.get_user_id()
529
+ message = message_crud.get_conversation_message(session, conversation_id, message_id, user_id)
530
+
531
+ if not message:
532
+ raise HTTPException(
533
+ status_code=404,
534
+ detail=f"Message with ID: {message_id} not found.",
535
+ )
536
+
537
+ try:
538
+ synthesized_audio = synthesize(message.text)
539
+ except Exception as e:
540
+ raise HTTPException(
541
+ status_code=500, detail=f"Error while message synthesis: {e}"
542
+ )
543
+
544
+ return Response(synthesized_audio, media_type="audio/mp3")
western_ai/canada/cohere-toolkit/src/backend/routers/deployment.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends, HTTPException
2
+
3
+ from backend.config.routers import RouterName
4
+ from backend.crud import deployment as deployment_crud
5
+ from backend.database_models.database import DBSessionDep
6
+ from backend.exceptions import DeploymentNotFoundError
7
+ from backend.schemas.context import Context
8
+ from backend.schemas.deployment import (
9
+ DeleteDeployment,
10
+ DeploymentCreate,
11
+ DeploymentDefinition,
12
+ DeploymentUpdate,
13
+ UpdateDeploymentEnv,
14
+ )
15
+ from backend.schemas.params.deployment import (
16
+ AllQueryParam,
17
+ DeploymentIdPathParam,
18
+ )
19
+ from backend.services import deployment as deployment_service
20
+ from backend.services.context import get_context
21
+ from backend.services.logger.utils import LoggerFactory
22
+ from backend.services.request_validators import (
23
+ validate_create_deployment_request,
24
+ validate_env_vars,
25
+ )
26
+
27
+ logger = LoggerFactory().get_logger()
28
+
29
+ router = APIRouter(
30
+ prefix="/v1/deployments",
31
+ tags=[RouterName.DEPLOYMENT],
32
+ )
33
+ router.name = RouterName.DEPLOYMENT
34
+
35
+
36
+ @router.post(
37
+ "",
38
+ response_model=DeploymentDefinition,
39
+ dependencies=[Depends(validate_create_deployment_request)],
40
+ )
41
+ def create_deployment(
42
+ deployment: DeploymentCreate,
43
+ session: DBSessionDep,
44
+ ) -> DeploymentDefinition:
45
+ """
46
+ Create a new deployment.
47
+ """
48
+ try:
49
+ created = DeploymentDefinition.from_db_deployment(
50
+ deployment_crud.create_deployment(session, deployment)
51
+ )
52
+ except Exception as e:
53
+ raise HTTPException(status_code=400, detail=str(e))
54
+
55
+ return mask_deployment_secrets(created)
56
+
57
+
58
+ @router.put("/{deployment_id}", response_model=DeploymentDefinition)
59
+ def update_deployment(
60
+ deployment_id: DeploymentIdPathParam,
61
+ new_deployment: DeploymentUpdate,
62
+ session: DBSessionDep,
63
+ ) -> DeploymentDefinition:
64
+ """
65
+ Update a deployment.
66
+
67
+ Raises:
68
+ HTTPException: If deployment not found.
69
+ """
70
+ deployment = deployment_crud.get_deployment(session, deployment_id)
71
+ if not deployment:
72
+ raise DeploymentNotFoundError(deployment_id=deployment_id)
73
+
74
+ return mask_deployment_secrets(DeploymentDefinition.from_db_deployment(
75
+ deployment_crud.update_deployment(session, deployment, new_deployment)
76
+ ))
77
+
78
+
79
+ @router.get("/{deployment_id}", response_model=DeploymentDefinition)
80
+ def get_deployment(
81
+ deployment_id: DeploymentIdPathParam,
82
+ session: DBSessionDep,
83
+ ) -> DeploymentDefinition:
84
+ """
85
+ Get a deployment by ID.
86
+ """
87
+ return mask_deployment_secrets(
88
+ deployment_service.get_deployment_definition(session, deployment_id)
89
+ )
90
+
91
+
92
+ @router.get("", response_model=list[DeploymentDefinition])
93
+ def list_deployments(
94
+ *,
95
+ all: AllQueryParam = False,
96
+ session: DBSessionDep,
97
+ ctx: Context = Depends(get_context),
98
+ ) -> list[DeploymentDefinition]:
99
+ """
100
+ List all available deployments and their models.
101
+ """
102
+ logger = ctx.get_logger()
103
+
104
+ installed_deployments = deployment_service.get_deployment_definitions(session)
105
+ available_deployments = [
106
+ mask_deployment_secrets(deployment) for deployment in installed_deployments if deployment.is_available or all
107
+ ]
108
+
109
+ if not available_deployments:
110
+ logger.warning(
111
+ event="[Deployment] No deployments available to list.",
112
+ )
113
+ raise HTTPException(
114
+ status_code=404,
115
+ detail=(
116
+ "No available deployments found. Please ensure that the required variables in your configuration.yaml "
117
+ "and secrets.yaml are set up correctly. "
118
+ "Refer to the README.md for detailed instructions.",
119
+ ),
120
+ )
121
+
122
+ return available_deployments
123
+
124
+
125
+ @router.delete("/{deployment_id}")
126
+ async def delete_deployment(
127
+ deployment_id: DeploymentIdPathParam,
128
+ session: DBSessionDep,
129
+ ) -> DeleteDeployment:
130
+ """
131
+ Delete a deployment by ID.
132
+
133
+ Raises:
134
+ HTTPException: If the deployment with the given ID is not found.
135
+ """
136
+ deployment = deployment_crud.get_deployment(session, deployment_id)
137
+
138
+ if not deployment:
139
+ raise DeploymentNotFoundError(deployment_id=deployment_id)
140
+
141
+ deployment_crud.delete_deployment(session, deployment_id)
142
+
143
+ return DeleteDeployment()
144
+
145
+
146
+ @router.post("/{deployment_id}/update_config", response_model=DeploymentDefinition)
147
+ async def update_config(
148
+ *,
149
+ deployment_id: DeploymentIdPathParam,
150
+ env_vars: UpdateDeploymentEnv,
151
+ valid_env_vars = Depends(validate_env_vars),
152
+ session: DBSessionDep,
153
+ ) -> DeploymentDefinition:
154
+ """
155
+ Set environment variables for the deployment.
156
+ """
157
+ return mask_deployment_secrets(
158
+ deployment_service.update_config(session, deployment_id, valid_env_vars)
159
+ )
160
+
161
+
162
+ def mask_deployment_secrets(deployment: DeploymentDefinition) -> DeploymentDefinition:
163
+ deployment.config = {key: "*****" if val else "" for [key, val] in deployment.config.items()}
164
+ return deployment
western_ai/canada/cohere-toolkit/src/backend/routers/experimental_features.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends
2
+
3
+ from backend.config.routers import RouterName
4
+ from backend.config.settings import Settings
5
+ from backend.schemas.context import Context
6
+ from backend.services.context import get_context
7
+
8
+ router = APIRouter(
9
+ prefix="/v1/experimental_features",
10
+ tags=[RouterName.EXPERIMENTAL_FEATURES],
11
+ )
12
+
13
+ router.name = RouterName.EXPERIMENTAL_FEATURES
14
+
15
+
16
+ @router.get("/")
17
+ def list_experimental_features(ctx: Context = Depends(get_context)) -> dict[str, bool]:
18
+ """
19
+ List all experimental features and if they are enabled
20
+ """
21
+ experimental_features = {
22
+ "USE_AGENTS_VIEW": Settings().get('feature_flags.use_agents_view'),
23
+ "USE_TEXT_TO_SPEECH_SYNTHESIS": bool(Settings().get('google_cloud.api_key')),
24
+ }
25
+ return experimental_features
western_ai/canada/cohere-toolkit/src/backend/routers/model.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends, HTTPException
2
+
3
+ from backend.config.routers import RouterName
4
+ from backend.crud import model as model_crud
5
+ from backend.database_models import Model
6
+ from backend.database_models.database import DBSessionDep
7
+ from backend.schemas.model import DeleteModel, ModelCreate, ModelUpdate
8
+ from backend.schemas.model import Model as ModelSchema
9
+ from backend.schemas.params.model import ModelIdPathParam
10
+ from backend.schemas.params.shared import PaginationQueryParams
11
+ from backend.services.request_validators import validate_create_update_model_request
12
+
13
+ router = APIRouter(
14
+ prefix="/v1/models",
15
+ tags=[RouterName.MODEL],
16
+ )
17
+
18
+ router.name = RouterName.MODEL
19
+
20
+
21
+ @router.post(
22
+ "",
23
+ response_model=ModelSchema,
24
+ dependencies=[
25
+ Depends(validate_create_update_model_request),
26
+ ],
27
+ )
28
+ def create_model(
29
+ model: ModelCreate,
30
+ session: DBSessionDep
31
+ ) -> Model:
32
+ """
33
+ Create a new model.
34
+ """
35
+ return model_crud.create_model(session, model)
36
+
37
+
38
+ @router.put(
39
+ "/{model_id}",
40
+ response_model=ModelSchema,
41
+ dependencies=[
42
+ Depends(validate_create_update_model_request),
43
+ ],
44
+ )
45
+ def update_model(
46
+ model_id: ModelIdPathParam,
47
+ new_model: ModelUpdate,
48
+ session: DBSessionDep,
49
+ ) -> ModelSchema:
50
+ """
51
+ Update a model by ID.
52
+
53
+ Raises:
54
+ HTTPException: If the model with the given ID is not found.
55
+ """
56
+ model = model_crud.get_model(session, model_id)
57
+ if not model:
58
+ raise HTTPException(status_code=404, detail="Model not found")
59
+
60
+ return model_crud.update_model(session, model, new_model)
61
+
62
+
63
+ @router.get("/{model_id}", response_model=ModelSchema)
64
+ def get_model(
65
+ model_id: ModelIdPathParam,
66
+ session: DBSessionDep,
67
+ ) -> ModelSchema:
68
+ """
69
+ Get a model by ID.
70
+ """
71
+ model = model_crud.get_model(session, model_id)
72
+ if not model:
73
+ raise HTTPException(status_code=404, detail="Model not found")
74
+ return model
75
+
76
+
77
+ @router.get("", response_model=list[ModelSchema])
78
+ def list_models(
79
+ page_params: PaginationQueryParams,
80
+ session: DBSessionDep,
81
+ ) -> list[ModelSchema]:
82
+ """
83
+ List all available models
84
+ """
85
+ return model_crud.get_models(session, page_params.offset, page_params.limit)
86
+
87
+
88
+ @router.delete("/{model_id}")
89
+ async def delete_model(
90
+ model_id: ModelIdPathParam,
91
+ session: DBSessionDep,
92
+ ) -> DeleteModel:
93
+ """
94
+ Delete a model by ID.
95
+
96
+ Raises:
97
+ HTTPException: If the model with the given ID is not found.
98
+ """
99
+ model = model_crud.get_model(session, model_id)
100
+
101
+ if not model:
102
+ raise HTTPException(
103
+ status_code=404, detail=f"Model with ID: {model_id} not found."
104
+ )
105
+
106
+ model_crud.delete_model(session, model_id)
107
+
108
+ return DeleteModel()
western_ai/canada/cohere-toolkit/src/backend/routers/organization.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends, HTTPException
2
+
3
+ from backend.config.routers import RouterName
4
+ from backend.crud import organization as organization_crud
5
+ from backend.database_models.database import DBSessionDep
6
+ from backend.database_models.organization import Organization as OrganizationModel
7
+ from backend.schemas import (
8
+ CreateOrganization,
9
+ DeleteOrganization,
10
+ Organization,
11
+ UpdateOrganization,
12
+ )
13
+ from backend.schemas.context import Context
14
+ from backend.schemas.params.organization import OrganizationIdPathParam
15
+ from backend.schemas.user import User
16
+ from backend.services.context import get_context
17
+ from backend.services.request_validators import validate_organization_request
18
+
19
+ router = APIRouter(
20
+ prefix="/v1/organizations",
21
+ tags=[RouterName.ORGANIZATION],
22
+ )
23
+ router.name = RouterName.ORGANIZATION
24
+
25
+
26
+ @router.post(
27
+ "",
28
+ response_model=Organization,
29
+ dependencies=[
30
+ Depends(validate_organization_request),
31
+ ],
32
+ )
33
+ def create_organization(
34
+ organization: CreateOrganization,
35
+ session: DBSessionDep,
36
+ ctx: Context = Depends(get_context),
37
+ ) -> Organization:
38
+ """
39
+ Create a new organization.
40
+ """
41
+ organization_data = OrganizationModel(**organization.model_dump())
42
+
43
+ return organization_crud.create_organization(session, organization_data)
44
+
45
+
46
+ @router.put(
47
+ "/{org_id}",
48
+ response_model=Organization,
49
+ dependencies=[
50
+ Depends(validate_organization_request),
51
+ ],
52
+ )
53
+ def update_organization(
54
+ org_id: OrganizationIdPathParam,
55
+ new_organization: UpdateOrganization,
56
+ session: DBSessionDep,
57
+ ctx: Context = Depends(get_context),
58
+ ) -> Organization:
59
+ """
60
+ Update organization by ID.
61
+ """
62
+ organization = organization_crud.get_organization(session, org_id)
63
+ return organization_crud.update_organization(
64
+ session, organization, new_organization
65
+ )
66
+
67
+
68
+ @router.get("/{org_id}", response_model=Organization)
69
+ def get_organization(
70
+ org_id: OrganizationIdPathParam,
71
+ session: DBSessionDep,
72
+ ctx: Context = Depends(get_context),
73
+ ) -> Organization:
74
+ """
75
+ Get a organization by ID.
76
+ """
77
+ organization = organization_crud.get_organization(session, org_id)
78
+ if not organization:
79
+ raise HTTPException(status_code=404, detail="Organization not found")
80
+ return organization
81
+
82
+
83
+ @router.delete("/{org_id}", response_model=DeleteOrganization)
84
+ def delete_organization(
85
+ org_id: OrganizationIdPathParam,
86
+ session: DBSessionDep,
87
+ ctx: Context = Depends(get_context),
88
+ ) -> DeleteOrganization:
89
+ """
90
+ Delete a organization by ID.
91
+ """
92
+ organization = organization_crud.get_organization(session, org_id)
93
+ if not organization:
94
+ raise HTTPException(status_code=404, detail="Organization not found")
95
+ organization_crud.delete_organization(session, org_id)
96
+
97
+ return DeleteOrganization()
98
+
99
+
100
+ @router.get("", response_model=list[Organization])
101
+ def list_organizations(
102
+ session: DBSessionDep,
103
+ ctx: Context = Depends(get_context),
104
+ ) -> list[Organization]:
105
+ """
106
+ List all available organizations.
107
+ """
108
+ all_organizations = organization_crud.get_organizations(session)
109
+ return all_organizations
110
+
111
+
112
+ @router.get("/{org_id}/users", response_model=list[User])
113
+ def get_organization_users(
114
+ org_id: OrganizationIdPathParam,
115
+ session: DBSessionDep,
116
+ ctx: Context = Depends(get_context),
117
+ ) -> list[User]:
118
+ """
119
+ Get organization users by ID.
120
+ """
121
+ organization = organization_crud.get_organization(session, org_id)
122
+ if not organization:
123
+ raise HTTPException(status_code=404, detail="Organization not found")
124
+
125
+ return organization.users
western_ai/canada/cohere-toolkit/src/backend/routers/scim.py ADDED
@@ -0,0 +1,346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends
2
+ from starlette.requests import Request
3
+ from starlette.responses import JSONResponse, Response
4
+
5
+ import backend.crud.group as group_crud
6
+ import backend.crud.user as user_crud
7
+ from backend.config import Settings
8
+ from backend.config.routers import RouterName
9
+ from backend.database_models import DBSessionDep, UserGroupAssociation
10
+ from backend.database_models import Group as DBGroup
11
+ from backend.database_models import User as DBUser
12
+ from backend.schemas.context import Context
13
+ from backend.schemas.params.scim import (
14
+ GroupIdPathParam,
15
+ ScimPaginationQueryParams,
16
+ UserIdPathParam,
17
+ )
18
+ from backend.schemas.scim import (
19
+ CreateGroup,
20
+ CreateUser,
21
+ Group,
22
+ ListGroupResponse,
23
+ ListUserResponse,
24
+ PatchGroup,
25
+ PatchUser,
26
+ UpdateUser,
27
+ User,
28
+ )
29
+ from backend.services.context import get_context
30
+
31
+ scim_auth = Settings().get('auth.scim')
32
+ router = APIRouter(
33
+ prefix="/scim/v2",
34
+ tags=[RouterName.SCIM]
35
+ )
36
+ router.name = RouterName.SCIM
37
+
38
+
39
+ class SCIMException(Exception):
40
+ def __init__(self, status_code: int, detail: str):
41
+ self.status_code = status_code
42
+ self.detail = detail
43
+
44
+
45
+ async def scim_exception_handler(request: Request, exc: SCIMException) -> Response:
46
+ return JSONResponse(
47
+ status_code=exc.status_code,
48
+ content={
49
+ "detail": exc.detail,
50
+ "schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"],
51
+ },
52
+ )
53
+
54
+
55
+ @router.get("/Users")
56
+ async def get_users(
57
+ page_params: ScimPaginationQueryParams,
58
+ session: DBSessionDep,
59
+ ) -> ListUserResponse:
60
+ """
61
+ Return users
62
+ """
63
+ if page_params.filter:
64
+ try:
65
+ display_name = _parse_filter(page_params.filter)
66
+ except ValueError:
67
+ raise SCIMException(status_code=400, detail="filter not supported")
68
+ db_user = user_crud.get_user_by_user_name(session, display_name)
69
+ if not db_user:
70
+ return ListUserResponse(
71
+ total_results=0,
72
+ start_index=page_params.start_index,
73
+ items_per_page=page_params.count,
74
+ resources=[],
75
+ )
76
+ return ListUserResponse(
77
+ total_results=1,
78
+ start_index=page_params.start_index,
79
+ items_per_page=page_params.count,
80
+ resources=[User.from_db_user(db_user)],
81
+ )
82
+
83
+ db_users = user_crud.get_external_users(
84
+ session, offset=page_params.start_index - 1, limit=page_params.count
85
+ )
86
+ users = [User.from_db_user(db_user) for db_user in db_users]
87
+ return ListUserResponse(
88
+ total_results=len(users),
89
+ start_index=page_params.start_index,
90
+ items_per_page=page_params.count,
91
+ resources=users,
92
+ )
93
+
94
+
95
+ @router.get("/Users/{user_id}")
96
+ async def get_user(
97
+ user_id: UserIdPathParam,
98
+ session: DBSessionDep,
99
+ ctx: Context = Depends(get_context),
100
+ ):
101
+ """
102
+ Get user by User ID
103
+ """
104
+ logger = ctx.get_logger()
105
+ db_user = user_crud.get_user(session, user_id)
106
+ if not db_user:
107
+ logger.error(event="[SCIM] user not found", user_id=user_id)
108
+ raise SCIMException(status_code=404, detail="User not found")
109
+
110
+ return User.from_db_user(db_user)
111
+
112
+
113
+ @router.post("/Users", status_code=201)
114
+ async def create_user(
115
+ user: CreateUser,
116
+ session: DBSessionDep,
117
+ ):
118
+ """
119
+ Create a new user
120
+ """
121
+ db_user = user_crud.get_user_by_external_id(session, user.externalId)
122
+ if not db_user:
123
+ db_user = DBUser()
124
+
125
+ db_user.user_name = user.user_name
126
+ db_user.fullname = f"{user.name.given_name} {user.name.family_name}"
127
+ db_user.email = _get_email_from_scim_user(user)
128
+ db_user.active = user.active
129
+ db_user.external_id = user.externalId
130
+
131
+ db_user = user_crud.create_user(session, db_user)
132
+ return User.from_db_user(db_user)
133
+
134
+
135
+ @router.put("/Users/{user_id}")
136
+ async def update_user(
137
+ user_id: UserIdPathParam,
138
+ user: UpdateUser,
139
+ session: DBSessionDep,
140
+ ctx: Context = Depends(get_context),
141
+ ):
142
+ """
143
+ Update a user
144
+ """
145
+ logger = ctx.get_logger()
146
+ db_user = user_crud.get_user(session, user_id)
147
+ if not db_user:
148
+ logger.error(event="[SCIM] user not found", user_id=user_id)
149
+ raise SCIMException(status_code=404, detail="User not found")
150
+
151
+ db_user.user_name = user.user_name
152
+ db_user.fullname = f"{user.name.given_name} {user.name.family_name}"
153
+ db_user.active = user.active
154
+ email_update = _get_email_from_scim_user(user)
155
+ db_user.email = email_update
156
+
157
+ db_user = user_crud.create_user(session, db_user)
158
+
159
+ return User.from_db_user(db_user)
160
+
161
+
162
+ @router.patch("/Users/{user_id}")
163
+ async def patch_user(
164
+ user_id: UserIdPathParam,
165
+ patch: PatchUser,
166
+ session: DBSessionDep,
167
+ ctx: Context = Depends(get_context),
168
+ ):
169
+ """
170
+ Patch a user
171
+ """
172
+ logger = ctx.get_logger()
173
+ db_user = user_crud.get_user(session, user_id)
174
+ if not db_user:
175
+ logger.error(event="[SCIM] user not found", user_id=user_id)
176
+ raise SCIMException(status_code=404, detail="User not found")
177
+
178
+ for operation in patch.Operations:
179
+ k, v = list(operation.value.items())[0]
180
+ setattr(db_user, k, v)
181
+
182
+ db_user = user_crud.create_user(session, db_user)
183
+
184
+ return User.from_db_user(db_user)
185
+
186
+
187
+ @router.get("/Groups")
188
+ async def get_groups(
189
+ *,
190
+ page_params: ScimPaginationQueryParams,
191
+ session: DBSessionDep,
192
+ ) -> ListGroupResponse:
193
+ """
194
+ Return Groups
195
+ """
196
+ if page_params.filter:
197
+ try:
198
+ display_name = _parse_filter(page_params.filter)
199
+ except ValueError:
200
+ raise SCIMException(status_code=400, detail="filter not supported")
201
+ db_group = group_crud.get_group_by_name(session, display_name)
202
+ if not db_group:
203
+ return ListGroupResponse(
204
+ total_results=0,
205
+ start_index=page_params.start_index,
206
+ items_per_page=page_params.count,
207
+ resources=[],
208
+ )
209
+ return ListGroupResponse(
210
+ total_results=1,
211
+ start_index=page_params.start_index,
212
+ items_per_page=page_params.count,
213
+ resources=[Group.from_db_group(db_group)],
214
+ )
215
+
216
+ db_groups = group_crud.get_groups(session, offset=page_params.start_index - 1, limit=page_params.count)
217
+ groups = [Group.from_db_group(db_group) for db_group in db_groups]
218
+ return ListGroupResponse(
219
+ total_results=len(groups),
220
+ start_index=page_params.start_index,
221
+ items_per_page=page_params.count,
222
+ resources=groups,
223
+ )
224
+
225
+
226
+ @router.get("/Groups/{group_id}")
227
+ async def get_group(
228
+ group_id: GroupIdPathParam,
229
+ session: DBSessionDep,
230
+ ctx: Context = Depends(get_context),
231
+ ):
232
+ """
233
+ Get group by group ID
234
+ """
235
+ logger = ctx.get_logger()
236
+ db_group = group_crud.get_group(session, group_id)
237
+ if not db_group:
238
+ logger.error(event="[SCIM] group not found", group_id=group_id)
239
+ raise SCIMException(status_code=404, detail="Group not found")
240
+
241
+ return Group.from_db_group(db_group)
242
+
243
+
244
+ @router.post("/Groups", status_code=201)
245
+ async def create_group(
246
+ group: CreateGroup,
247
+ session: DBSessionDep,
248
+ ):
249
+ """
250
+ Create a group
251
+ """
252
+ db_group = group_crud.get_group_by_name(session, group.display_name)
253
+ if not db_group:
254
+ db_group = DBGroup()
255
+
256
+ db_group.display_name = group.display_name
257
+
258
+ db_group = group_crud.create_group(session, db_group)
259
+ return Group.from_db_group(db_group)
260
+
261
+
262
+ @router.patch("/Groups/{group_id}")
263
+ async def patch_group(
264
+ group_id: GroupIdPathParam,
265
+ patch: PatchGroup,
266
+ session: DBSessionDep,
267
+ ctx: Context = Depends(get_context),
268
+ ):
269
+ """
270
+ Patch a group
271
+ """
272
+ logger = ctx.get_logger()
273
+ db_group = group_crud.get_group(session, group_id)
274
+ if not db_group:
275
+ logger.error(event="[SCIM] group not found", group_id=group_id)
276
+ raise SCIMException(status_code=404, detail="Group not found")
277
+
278
+ for operation in patch.Operations:
279
+ if operation.op == "replace" and "displayName" in operation.value:
280
+ db_group.display_name = operation.value["displayName"]
281
+ db_group = group_crud.update_group(session, db_group)
282
+ if operation.op == "add" and operation.path == "members":
283
+ associations = []
284
+ for value in operation.value:
285
+ association = UserGroupAssociation(
286
+ user_id=value["value"],
287
+ group_id=db_group.id,
288
+ display=value["display"],
289
+ )
290
+ associations.append(association)
291
+ db_group = group_crud.add_users(session, db_group, associations)
292
+ if operation.op == "replace" and operation.path == "members":
293
+ associations = []
294
+ for value in operation.value:
295
+ association = UserGroupAssociation(
296
+ user_id=value["value"],
297
+ group_id=db_group.id,
298
+ display=value["display"],
299
+ )
300
+ associations.append(association)
301
+ db_group = group_crud.set_users(session, db_group, associations)
302
+
303
+ return Group.from_db_group(db_group)
304
+
305
+
306
+ @router.delete("/Groups/{group_id}", status_code=204)
307
+ async def delete_group(
308
+ group_id: GroupIdPathParam,
309
+ session: DBSessionDep,
310
+ ctx: Context = Depends(get_context),
311
+ ):
312
+ """
313
+ Delete a group
314
+ """
315
+ logger = ctx.get_logger()
316
+ db_group = group_crud.get_group(session, group_id)
317
+ if not db_group:
318
+ logger.error(event="[SCIM] group not found", group_id=group_id)
319
+ raise SCIMException(status_code=404, detail="Group not found")
320
+ group_crud.delete_group(session, group_id)
321
+
322
+
323
+ def _get_email_from_scim_user(user: CreateUser | UpdateUser) -> str | None:
324
+ """
325
+ Return the primary email or a SCIM user
326
+ """
327
+ for email_obj in user.emails:
328
+ if email_obj.primary:
329
+ return email_obj.value
330
+ return None
331
+
332
+
333
+ def _parse_filter(filter: str) -> str:
334
+ """
335
+ Parse a filter like `displayName eq "test"` into a value. Raises a ValueError if the filter is not valid.
336
+ """
337
+ try:
338
+ filter_column, operator, value = filter.split(" ", maxsplit=2)
339
+ if filter_column not in ["displayName", "userName"]:
340
+ raise ValueError(f"filter not supported: {filter}")
341
+ if operator != "eq":
342
+ raise ValueError(f"filter not supported: {filter}")
343
+ filter_value = value.strip('"')
344
+ return filter_value
345
+ except Exception as e:
346
+ raise ValueError(f"Invalid filter: {filter}") from e
western_ai/canada/cohere-toolkit/src/backend/routers/snapshot.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends, HTTPException
2
+
3
+ from backend.chat.collate import to_dict
4
+ from backend.config.routers import RouterName
5
+ from backend.crud import snapshot as snapshot_crud
6
+ from backend.database_models.database import DBSessionDep
7
+ from backend.schemas.context import Context
8
+ from backend.schemas.params.snapshot import LinkIdPathParam, SnapshotIdPathParam
9
+ from backend.schemas.snapshot import (
10
+ CreateSnapshotRequest,
11
+ CreateSnapshotResponse,
12
+ DeleteSnapshotLinkResponse,
13
+ DeleteSnapshotResponse,
14
+ Snapshot,
15
+ SnapshotPublic,
16
+ SnapshotWithLinks,
17
+ )
18
+ from backend.services.context import get_context
19
+ from backend.services.conversation import validate_conversation
20
+ from backend.services.snapshot import (
21
+ validate_last_message,
22
+ validate_snapshot_exists,
23
+ validate_snapshot_link,
24
+ wrap_create_snapshot,
25
+ wrap_create_snapshot_access,
26
+ wrap_create_snapshot_link,
27
+ )
28
+
29
+ router = APIRouter(
30
+ prefix="/v1/snapshots",
31
+ tags=[RouterName.SNAPSHOT],
32
+ )
33
+ router.name = RouterName.SNAPSHOT
34
+
35
+ PRIVATE_KEYS = ["organization_id", "user_id", "conversation_id"]
36
+
37
+
38
+ @router.post("", response_model=CreateSnapshotResponse)
39
+ async def create_snapshot(
40
+ snapshot_request: CreateSnapshotRequest,
41
+ session: DBSessionDep,
42
+ ctx: Context = Depends(get_context),
43
+ ) -> CreateSnapshotResponse:
44
+ """
45
+ Create a new snapshot and snapshot link to share the conversation.
46
+ """
47
+ user_id = ctx.get_user_id()
48
+ conversation_id = snapshot_request.conversation_id
49
+
50
+ # Check if conversation exists, if it has messages and if a snapshot already exists
51
+ conversation = validate_conversation(session, conversation_id, user_id)
52
+
53
+ last_message_id = conversation.messages[-1].id if conversation.messages else None
54
+ validate_last_message(last_message_id)
55
+
56
+ snapshot = snapshot_crud.get_snapshot_by_last_message_id(session, last_message_id)
57
+
58
+ if not snapshot:
59
+ snapshot = wrap_create_snapshot(
60
+ session, last_message_id, user_id, conversation, ctx
61
+ )
62
+
63
+ snapshot_link = wrap_create_snapshot_link(session, snapshot.id, user_id)
64
+
65
+ return CreateSnapshotResponse(
66
+ link_id=snapshot_link.id,
67
+ snapshot_id=snapshot.id,
68
+ user_id=user_id,
69
+ messages=snapshot.snapshot.get("messages", []),
70
+ )
71
+
72
+
73
+ @router.get("", response_model=list[SnapshotWithLinks])
74
+ async def list_snapshots(
75
+ session: DBSessionDep,
76
+ ctx: Context = Depends(get_context),
77
+ ) -> list[SnapshotWithLinks]:
78
+ """
79
+ List all snapshots.
80
+ """
81
+ user_id = ctx.get_user_id()
82
+
83
+ snapshots = snapshot_crud.list_snapshots(session, user_id)
84
+
85
+ # Get snapshot links for each snapshot
86
+ response = []
87
+ for snapshot in snapshots:
88
+ snapshot_links = snapshot_crud.list_snapshot_links(session, snapshot.id)
89
+ snapshot_links = [link.id for link in snapshot_links]
90
+ snapshot_dict = to_dict(Snapshot.model_validate(snapshot))
91
+ snapshot_with_links = SnapshotWithLinks.model_validate(
92
+ snapshot_dict | {"links": snapshot_links}
93
+ )
94
+ response.append(snapshot_with_links)
95
+
96
+ return response
97
+
98
+
99
+ @router.get("/link/{link_id}", response_model=SnapshotPublic)
100
+ async def get_snapshot(
101
+ link_id: LinkIdPathParam,
102
+ session: DBSessionDep,
103
+ ctx: Context = Depends(get_context),
104
+ ) -> SnapshotPublic:
105
+ """
106
+ Get a snapshot by link ID.
107
+ """
108
+ user_id = ctx.get_user_id()
109
+
110
+ snapshot = validate_snapshot_link(session, link_id)
111
+
112
+ wrap_create_snapshot_access(session, snapshot.id, user_id, link_id, ctx)
113
+
114
+ return snapshot
115
+
116
+
117
+ @router.delete("/link/{link_id}")
118
+ async def delete_snapshot_link(
119
+ link_id: LinkIdPathParam,
120
+ session: DBSessionDep,
121
+ ctx: Context = Depends(get_context),
122
+ ) -> DeleteSnapshotLinkResponse:
123
+ """
124
+ Delete a snapshot link by ID.
125
+ """
126
+ user_id = ctx.get_user_id()
127
+
128
+ snapshot = validate_snapshot_link(session, link_id)
129
+
130
+ # Anyone can get the snapshot link, but only the user that created it can delete it
131
+ if user_id != snapshot.user_id:
132
+ raise HTTPException(
133
+ status_code=403,
134
+ detail="User does not have permission to delete this snapshot link",
135
+ )
136
+
137
+ snapshot_crud.delete_snapshot_link(session, link_id, user_id)
138
+
139
+ return DeleteSnapshotLinkResponse()
140
+
141
+
142
+ @router.delete("/{snapshot_id}")
143
+ async def delete_snapshot(
144
+ snapshot_id: SnapshotIdPathParam,
145
+ session: DBSessionDep,
146
+ ctx: Context = Depends(get_context),
147
+ ) -> DeleteSnapshotResponse:
148
+ """
149
+ Delete a snapshot by ID.
150
+ """
151
+ user_id = ctx.get_user_id()
152
+
153
+ snapshot = validate_snapshot_exists(session, snapshot_id)
154
+
155
+ # Anyone can get the snapshot, but only the user that created it can delete it
156
+ if user_id != snapshot.user_id:
157
+ raise HTTPException(
158
+ status_code=403,
159
+ detail="User does not have permission to delete this snapshot",
160
+ )
161
+
162
+ snapshot_crud.delete_snapshot(session, snapshot_id, user_id)
163
+
164
+ return DeleteSnapshotResponse()
western_ai/canada/cohere-toolkit/src/backend/routers/tool.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends
2
+
3
+ from backend.config.routers import RouterName
4
+ from backend.config.tools import get_available_tools
5
+ from backend.database_models.database import DBSessionDep
6
+ from backend.schemas.context import Context
7
+ from backend.schemas.params.agent import AgentIdQueryParam
8
+ from backend.schemas.tool import ToolDefinition
9
+ from backend.services.agent import validate_agent_exists
10
+ from backend.services.context import get_context
11
+
12
+ router = APIRouter(
13
+ prefix="/v1/tools",
14
+ tags=[RouterName.TOOL],
15
+ )
16
+ router.name = RouterName.TOOL
17
+
18
+
19
+ @router.get("", response_model=list[ToolDefinition])
20
+ def list_tools(
21
+ *,
22
+ agent_id: AgentIdQueryParam = None,
23
+ session: DBSessionDep,
24
+ ctx: Context = Depends(get_context),
25
+ ) -> list[ToolDefinition]:
26
+ """
27
+ List all available tools.
28
+ """
29
+ user_id = ctx.get_user_id()
30
+ logger = ctx.get_logger()
31
+
32
+ available_tools = get_available_tools()
33
+ all_tools = list(available_tools.values())
34
+
35
+ if agent_id is not None:
36
+ agent_tools = []
37
+ agent = validate_agent_exists(session, agent_id, user_id)
38
+
39
+ for tool in agent.tools:
40
+ agent_tools.append(available_tools[tool])
41
+ all_tools = agent_tools
42
+
43
+ for tool in all_tools:
44
+ # Tools with auth implementation can be enabled and visible but not accessible (e.g., if secrets are not set).
45
+ # Therefore, we need to set is_auth_required for these types of tools as well for the frontend.
46
+ if (tool.is_available or tool.is_visible) and tool.auth_implementation is not None:
47
+ try:
48
+ tool_auth_service = tool.auth_implementation()
49
+
50
+ tool.is_auth_required = tool_auth_service.is_auth_required(
51
+ session, user_id
52
+ )
53
+ tool.auth_url = tool_auth_service.get_auth_url(user_id)
54
+
55
+ # Return access token to client when required by frontend
56
+ # e.g: to enable Google Drive picker in client
57
+ if tool.should_return_token:
58
+ tool.token = tool_auth_service.get_token(session, user_id)
59
+ except Exception as e:
60
+ logger.error(event=f"Error while fetching Tool Auth: {str(e)}")
61
+ tool.is_auth_required = True
62
+ tool.is_available = False
63
+ tool.error_message = (
64
+ f"Error while calling Tool Auth implementation {str(e)}"
65
+ )
66
+
67
+ return all_tools
western_ai/canada/cohere-toolkit/src/backend/routers/user.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends, HTTPException
2
+
3
+ from backend.config.auth import is_authentication_enabled
4
+ from backend.config.routers import RouterName
5
+ from backend.crud import user as user_crud
6
+ from backend.database_models import User as UserModel
7
+ from backend.database_models.database import DBSessionDep
8
+ from backend.schemas.context import Context
9
+ from backend.schemas.params.shared import PaginationQueryParams
10
+ from backend.schemas.params.user import UserIdPathParam
11
+ from backend.schemas.user import CreateUser, DeleteUser, UpdateUser, User
12
+ from backend.schemas.user import User as UserSchema
13
+ from backend.services.auth.request_validators import validate_authorization
14
+ from backend.services.context import get_context
15
+
16
+ router = APIRouter(
17
+ prefix="/v1/users",
18
+ tags=[RouterName.USER],
19
+ )
20
+ router.name = RouterName.USER
21
+
22
+ auth_dependencies = [Depends(validate_authorization)] if is_authentication_enabled() else []
23
+
24
+
25
+ @router.post("", response_model=User)
26
+ async def create_user(
27
+ user: CreateUser,
28
+ session: DBSessionDep,
29
+ ctx: Context = Depends(get_context),
30
+ ) -> User:
31
+ """
32
+ Create a new user.
33
+ """
34
+ db_user = UserModel(**user.model_dump(exclude_none=True))
35
+ db_user = user_crud.create_user(session, db_user)
36
+
37
+ user_schema = UserSchema.model_validate(db_user)
38
+ ctx.with_user(user=user_schema)
39
+
40
+ return db_user
41
+
42
+
43
+ @router.get(
44
+ "",
45
+ response_model=list[User],
46
+ dependencies=auth_dependencies,
47
+ )
48
+ async def list_users(
49
+ page_params: PaginationQueryParams,
50
+ session: DBSessionDep,
51
+ ctx: Context = Depends(get_context),
52
+ ) -> list[User]:
53
+ """
54
+ List all users.
55
+ """
56
+ return user_crud.get_users(session, offset=page_params.offset, limit=page_params.limit)
57
+
58
+
59
+ @router.get(
60
+ "/{user_id}",
61
+ response_model=User,
62
+ dependencies=auth_dependencies,
63
+ )
64
+ async def get_user(
65
+ user_id: UserIdPathParam,
66
+ session: DBSessionDep,
67
+ ctx: Context = Depends(get_context),
68
+ ) -> User:
69
+ """
70
+ Get a user by ID.
71
+
72
+ Raises:
73
+ HTTPException: If the user with the given ID is not found.
74
+ """
75
+
76
+ user = user_crud.get_user(session, user_id)
77
+ if not user:
78
+ raise HTTPException(
79
+ status_code=404, detail=f"User with ID: {user_id} not found."
80
+ )
81
+
82
+ user_schema = UserSchema.model_validate(user)
83
+ ctx.with_user(user=user_schema)
84
+ return user
85
+
86
+
87
+ @router.put(
88
+ "/{user_id}",
89
+ response_model=User,
90
+ dependencies=auth_dependencies,
91
+ )
92
+ async def update_user(
93
+ user_id: UserIdPathParam,
94
+ new_user: UpdateUser,
95
+ session: DBSessionDep,
96
+ ctx: Context = Depends(get_context),
97
+ ) -> User:
98
+ """
99
+ Update a user by ID.
100
+
101
+ Raises:
102
+ HTTPException: If the user with the given ID is not found.
103
+ """
104
+ user = user_crud.get_user(session, user_id)
105
+
106
+ if not user:
107
+ raise HTTPException(
108
+ status_code=404, detail=f"User with ID: {user_id} not found."
109
+ )
110
+
111
+ user = user_crud.update_user(session, user, new_user)
112
+ user_schema = UserSchema.model_validate(user)
113
+ ctx.with_user(user=user_schema)
114
+
115
+ return user
116
+
117
+
118
+ @router.delete(
119
+ "/{user_id}",
120
+ dependencies=auth_dependencies,
121
+ )
122
+ async def delete_user(
123
+ user_id: UserIdPathParam,
124
+ session: DBSessionDep,
125
+ ctx: Context = Depends(get_context),
126
+ ) -> DeleteUser:
127
+ """
128
+ Delete a user by ID.
129
+
130
+ Raises:
131
+ HTTPException: If the user with the given ID is not found.
132
+ """
133
+ user = user_crud.get_user(session, user_id)
134
+
135
+ if not user:
136
+ raise HTTPException(
137
+ status_code=404, detail=f"User with ID: {user_id} not found."
138
+ )
139
+
140
+ user_schema = UserSchema.model_validate(user)
141
+ ctx.with_user(user=user_schema)
142
+ user_crud.delete_user(session, user_id)
143
+
144
+ return DeleteUser()
western_ai/canada/cohere-toolkit/src/backend/routers/utils.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import backend.services.deployment as deployment_service
2
+ from backend.database_models.database import DBSessionDep
3
+ from backend.exceptions import DeploymentNotFoundError
4
+ from backend.model_deployments.cohere_platform import CohereDeployment
5
+ from backend.schemas.agent import Agent
6
+ from backend.services.logger.utils import LoggerFactory
7
+
8
+
9
+ def get_deployment_for_agent(session: DBSessionDep, deployment, model) -> tuple[CohereDeployment, str | None]:
10
+ try:
11
+ deployment = deployment_service.get_deployment_instance_by_name(session, deployment)
12
+ except DeploymentNotFoundError:
13
+ deployment = deployment_service.get_default_deployment_instance()
14
+
15
+ model = next((m for m in deployment.models() if m.name == model), None)
16
+
17
+ return deployment, model
18
+
19
+ def get_deployment_model_from_agent(agent: Agent, session: DBSessionDep):
20
+ from backend.crud import deployment as deployment_crud
21
+ logger = LoggerFactory().get_logger()
22
+ logger.debug(event="get_deployment_model_from_agent", agent=agent.model_dump())
23
+
24
+ model_db = None
25
+ deployment_db = None
26
+ if agent.deployment:
27
+ deployment_db = deployment_crud.get_deployment_by_name(session, agent.deployment)
28
+ if not deployment_db:
29
+ deployment_db = deployment_crud.get_deployment(session, agent.deployment)
30
+ logger.debug(event="deployment models:", deployment_id=deployment_db.id, models=list(d.name for d in deployment_db.models))
31
+ if deployment_db:
32
+ model_db = next(
33
+ (
34
+ model
35
+ for model in deployment_db.models
36
+ if model.name == agent.model or model.id == agent.model
37
+ ),
38
+ None,
39
+ )
40
+ return deployment_db, model_db
41
+
42
+
43
+ def get_default_deployment_model(session: DBSessionDep):
44
+ from backend.crud import deployment as deployment_crud
45
+
46
+ deployment_db = deployment_crud.get_deployment_by_name(session, CohereDeployment.name())
47
+ model_db = None
48
+ if deployment_db:
49
+ model_db = next(
50
+ (
51
+ model
52
+ for model in deployment_db.models
53
+ if model.name == 'command-r-plus'
54
+ ),
55
+ None,
56
+ )
57
+ return deployment_db, model_db
western_ai/canada/cohere-toolkit/src/backend/schemas/__init__.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ # ruff: noqa
2
+ from backend.schemas.cohere_chat import *
3
+ from backend.schemas.message import *
4
+ from backend.schemas.organization import *
western_ai/canada/cohere-toolkit/src/backend/schemas/agent.py ADDED
@@ -0,0 +1,421 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ from abc import ABC
3
+ from enum import StrEnum
4
+ from typing import Optional
5
+
6
+ from pydantic import BaseModel, Field
7
+
8
+
9
+ class AgentVisibility(StrEnum):
10
+ """
11
+ Supported values for Agent Visibility
12
+ """
13
+ PRIVATE = "private"
14
+ PUBLIC = "public"
15
+ ALL = "all"
16
+
17
+
18
+ class AgentBase(ABC, BaseModel):
19
+ """
20
+ Abstract base class for Agent Schemas
21
+ """
22
+ user_id: str = Field(
23
+ ...,
24
+ title="User ID",
25
+ description="User ID for the Agent",
26
+ )
27
+ organization_id: Optional[str] = Field(
28
+ None,
29
+ title="Organization ID",
30
+ description="Organization ID for the Agent",
31
+ )
32
+
33
+
34
+ class AgentToolMetadata(BaseModel):
35
+ """
36
+ Agent tool metadata schema
37
+ """
38
+ id: str = Field(
39
+ ...,
40
+ title="ID",
41
+ description="Agent tool metadata ID",
42
+ )
43
+ created_at: datetime.datetime = Field(
44
+ ...,
45
+ title="Created At Timestamp",
46
+ description="When the agent tool metadata was created",
47
+ )
48
+ updated_at: datetime.datetime = Field(
49
+ ...,
50
+ title="Updated At Timestamp",
51
+ description="When the agent tool metadata was updated",
52
+ )
53
+
54
+ user_id: Optional[str] = Field(
55
+ None,
56
+ title="User ID",
57
+ description="User ID for the agent tool metadata",
58
+ )
59
+ agent_id: str = Field(
60
+ ...,
61
+ title="Agent ID",
62
+ description="Agent ID for the agent tool metadata",
63
+ )
64
+ tool_name: str = Field(
65
+ ...,
66
+ title="Tool Name",
67
+ description="Tool Name for the agent tool metadata",
68
+ )
69
+ artifacts: list[dict] = Field(
70
+ ...,
71
+ title="Artifacts",
72
+ description="Artifacts for the agent tool metadata",
73
+ )
74
+
75
+ class Config:
76
+ from_attributes = True
77
+ use_enum_values = True
78
+
79
+
80
+ class AgentToolMetadataPublic(AgentToolMetadata):
81
+ """
82
+ Public agent tool metadata schema
83
+ """
84
+ user_id: Optional[str] = Field(
85
+ None,
86
+ title="User ID",
87
+ description="User ID for the agent tool metadata",
88
+ exclude=True,
89
+ )
90
+
91
+ class Config:
92
+ from_attributes = True
93
+
94
+
95
+ class AgentToolMedatadataBaseRequest(ABC, BaseModel):
96
+ """
97
+ Abstract class for creating/updating Agent Tool Metadata
98
+ """
99
+ id: Optional[str] = Field(
100
+ None,
101
+ title="ID",
102
+ description="Agent Tool Metadata ID",
103
+ )
104
+
105
+
106
+ class CreateAgentToolMetadataRequest(AgentToolMedatadataBaseRequest):
107
+ """
108
+ Request to create Agent Tool Metadata
109
+ """
110
+ tool_name: str = Field(
111
+ ...,
112
+ title="Tool Name",
113
+ description="Tool Name for the agent tool metadata",
114
+ )
115
+ artifacts: list[dict] = Field(
116
+ ...,
117
+ title="Artifacts",
118
+ description="Artifacts for the agent tool metadata",
119
+ )
120
+
121
+
122
+ class UpdateAgentToolMetadataRequest(AgentToolMedatadataBaseRequest):
123
+ """
124
+ Request to update Agent Tool Metadata
125
+ """
126
+ tool_name: Optional[str] = Field(
127
+ None,
128
+ title="Tool Name",
129
+ description="Tool Name for the agent tool metadata",
130
+ )
131
+ artifacts: Optional[list[dict]] = Field(
132
+ None,
133
+ title="Artifacts",
134
+ description="Artifacts for the agent tool metadata",
135
+ )
136
+
137
+
138
+ class DeleteAgentToolMetadata(BaseModel):
139
+ """
140
+ Delete agent tool metadata response
141
+ """
142
+ pass
143
+
144
+
145
+ # Agent
146
+ class Agent(AgentBase):
147
+ """
148
+ Agent schema
149
+ """
150
+ id: str = Field(
151
+ ...,
152
+ title="ID",
153
+ description="Agent ID",
154
+ )
155
+ created_at: datetime.datetime = Field(
156
+ ...,
157
+ title="Created At Timestamp",
158
+ description="When the agent was created",
159
+ )
160
+ updated_at: datetime.datetime = Field(
161
+ ...,
162
+ title="Updated At Timestamp",
163
+ description="When the agent was updated",
164
+ )
165
+
166
+ name: str = Field(
167
+ ...,
168
+ title="Name",
169
+ description="Name of the Agent",
170
+ )
171
+ version: int = Field(
172
+ ...,
173
+ title="Version",
174
+ description="Version of the Agent",
175
+ )
176
+ description: Optional[str] = Field(
177
+ None,
178
+ title="Description",
179
+ description="Agent Description",
180
+ )
181
+ preamble: Optional[str] = Field(
182
+ None,
183
+ title="Preamble",
184
+ description="The preamble for the Agent",
185
+ )
186
+ temperature: float = Field(
187
+ ...,
188
+ title="Temperature",
189
+ description="The temperature for the Agent",
190
+ )
191
+ tools: Optional[list[str]] = Field(
192
+ None,
193
+ title="Tools",
194
+ description="List of tools for the Agent",
195
+ )
196
+ tools_metadata: list[AgentToolMetadataPublic] = Field(
197
+ ...,
198
+ title="Tools Metadata",
199
+ description="List of tool metadata for the Agent",
200
+ )
201
+ deployment: Optional[str] = Field(
202
+ None,
203
+ title="Deployment",
204
+ description="Deployment for the Agent",
205
+ )
206
+ model: Optional[str] = Field(
207
+ None,
208
+ title="Model",
209
+ description="Model for the Agent",
210
+ )
211
+ is_private: Optional[bool] = Field(
212
+ None,
213
+ title="Is Private",
214
+ description="If the Agent is private",
215
+ )
216
+
217
+ class Config:
218
+ from_attributes = True
219
+ use_enum_values = True
220
+
221
+
222
+ class AgentPublic(Agent):
223
+ """
224
+ Public agent schema
225
+ """
226
+ organization_id: Optional[str] = Field(
227
+ None,
228
+ title="Organization ID",
229
+ description="Organization ID for the Agent",
230
+ exclude=True,
231
+ )
232
+ tools_metadata: Optional[list[AgentToolMetadataPublic]] = Field(
233
+ None,
234
+ title="Tools Metadata",
235
+ description="List of tool metadata for the Agent",
236
+ )
237
+
238
+
239
+ class CreateAgentRequest(BaseModel):
240
+ """
241
+ Schema to create an agent
242
+ """
243
+ name: str = Field(
244
+ ...,
245
+ title="Name",
246
+ description="Name of the Agent",
247
+ )
248
+ version: Optional[int] = Field(
249
+ None,
250
+ title="Version",
251
+ description="Version of the Agent",
252
+ )
253
+ description: Optional[str] = Field(
254
+ None,
255
+ title="Description",
256
+ description="Agent Description",
257
+ )
258
+ preamble: Optional[str] = Field(
259
+ None,
260
+ title="Preamble",
261
+ description="The preamble for the Agent",
262
+ )
263
+ temperature: Optional[float] = Field(
264
+ None,
265
+ title="Temperature",
266
+ description="The temperature for the Agent",
267
+ )
268
+ tools: Optional[list[str]] = Field(
269
+ None,
270
+ title="Tools",
271
+ description="List of tools for the Agent",
272
+ )
273
+ tools_metadata: Optional[list[CreateAgentToolMetadataRequest]] = Field(
274
+ None,
275
+ title="Tools Metadata",
276
+ description="Tools metadata for the Agent",
277
+ )
278
+ # deployment_id or deployment_name
279
+ deployment: str = Field(
280
+ ...,
281
+ title="Deployment",
282
+ description="Deployment for the Agent",
283
+ )
284
+ deployment_config: Optional[dict[str, str]] = Field(
285
+ None,
286
+ title="Deployment Config",
287
+ description="Deployment config for the Agent",
288
+ )
289
+ # model_id or model_name
290
+ model: str = Field(
291
+ ...,
292
+ title="Model",
293
+ description="Model for the Agent",
294
+ )
295
+ organization_id: Optional[str] = Field(
296
+ None,
297
+ title="Organization ID",
298
+ description="Organization ID for the Agent",
299
+ )
300
+ is_private: Optional[bool] = Field(
301
+ False,
302
+ title="Is Private",
303
+ description="If the Agent is private",
304
+ )
305
+
306
+ class Config:
307
+ from_attributes = True
308
+ use_enum_values = True
309
+
310
+
311
+ class ListAgentsResponse(BaseModel):
312
+ """
313
+ Response schema for listing agents
314
+ """
315
+ agents: list[Agent] = Field(
316
+ ...,
317
+ title="Agents",
318
+ description="List of Agents",
319
+ )
320
+
321
+
322
+ class UpdateAgentNoDeploymentModel(ABC, BaseModel):
323
+ """
324
+ Abstract class for updating agents
325
+ """
326
+ name: Optional[str] = Field(
327
+ None,
328
+ title="Name",
329
+ description="Name of the Agent",
330
+ )
331
+ version: Optional[int] = Field(
332
+ None,
333
+ title="Version",
334
+ description="Version of the Agent",
335
+ )
336
+ description: Optional[str] = Field(
337
+ None,
338
+ title="Description",
339
+ description="Agent Description",
340
+ )
341
+ preamble: Optional[str] = Field(
342
+ None,
343
+ title="Preamble",
344
+ description="The preamble for the Agent",
345
+ )
346
+ temperature: Optional[float] = Field(
347
+ None,
348
+ title="Temperature",
349
+ description="The temperature for the Agent",
350
+ )
351
+ tools: Optional[list[str]] = Field(
352
+ None,
353
+ title="Tools",
354
+ description="List of tools for the Agent",
355
+ )
356
+ organization_id: Optional[str] = Field(
357
+ None,
358
+ title="Organization ID",
359
+ description="Organization ID for the Agent",
360
+ )
361
+ is_private: Optional[bool] = Field(
362
+ None,
363
+ title="Is Private",
364
+ description="If the Agent is private",
365
+ )
366
+
367
+ class Config:
368
+ from_attributes = True
369
+ use_enum_values = True
370
+
371
+ class UpdateAgentDB(UpdateAgentNoDeploymentModel):
372
+ """
373
+ Model for updating agents
374
+ """
375
+ model_id: Optional[str] = Field(
376
+ None,
377
+ title="Model ID",
378
+ description="Model ID",
379
+ )
380
+ deployment_id: Optional[str] = Field(
381
+ None,
382
+ title="Deployment ID",
383
+ description="Deployment ID",
384
+ )
385
+
386
+ model_config = {
387
+ "from_attributes": True,
388
+ "use_enum_values": True,
389
+ "protected_namespaces": (),
390
+ }
391
+
392
+
393
+ class UpdateAgentRequest(UpdateAgentNoDeploymentModel):
394
+ """
395
+ Schema to update an agent
396
+ """
397
+ deployment: Optional[str] = Field(
398
+ None,
399
+ title="Deployment",
400
+ description="Deployment for the Agent",
401
+ )
402
+ model: Optional[str] = Field(
403
+ None,
404
+ title="Model",
405
+ description="Model for the Agent",
406
+ )
407
+ tools_metadata: Optional[list[CreateAgentToolMetadataRequest]] = Field(
408
+ None,
409
+ title="Tools Metadata",
410
+ description="Tools metadata for the Agent",
411
+ )
412
+ class Config:
413
+ from_attributes = True
414
+ use_enum_values = True
415
+
416
+
417
+ class DeleteAgent(BaseModel):
418
+ """
419
+ Response for deleting an agent
420
+ """
421
+ pass
western_ai/canada/cohere-toolkit/src/backend/schemas/auth.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from abc import ABC
2
+ from typing import Optional
3
+
4
+ from pydantic import BaseModel, Field
5
+
6
+
7
+ class Auth(ABC, BaseModel):
8
+ """
9
+ Abstract class for Auth Schemas
10
+ """
11
+ strategy: str = Field(
12
+ ...,
13
+ title="Strategy",
14
+ description="Auth strategy to use",
15
+ )
16
+
17
+
18
+ class Login(Auth):
19
+ """
20
+ Login Request
21
+ """
22
+ payload: Optional[dict[str, str]] = Field(
23
+ None,
24
+ title="Payload",
25
+ description="Login payload depending on strategy used",
26
+ )
27
+
28
+
29
+ class Logout(BaseModel):
30
+ """
31
+ Logout Request
32
+ """
33
+ pass
34
+
35
+
36
+ class ListAuthStrategy(BaseModel):
37
+ """
38
+ List Auth Strategy
39
+ """
40
+ strategy: str = Field(
41
+ ...,
42
+ title="Strategy",
43
+ description="Auth strategy name",
44
+ )
45
+ client_id: Optional[str] = Field(
46
+ None,
47
+ title="Client ID",
48
+ description="Client ID to be used",
49
+ )
50
+ authorization_endpoint: Optional[str] = Field(
51
+ None,
52
+ title="Authorization Endpoint",
53
+ description="The endpoint for authorization",
54
+ )
55
+ pkce_enabled: bool = Field(
56
+ ...,
57
+ title="PKCE Enabled",
58
+ description="If PKCE is enabled",
59
+ )
60
+
61
+
62
+ class JWTResponse(BaseModel):
63
+ """
64
+ JWT Response
65
+ """
66
+ token: str = Field(
67
+ ...,
68
+ title="Token",
69
+ description="JSON Web Token",
70
+ )
western_ai/canada/cohere-toolkit/src/backend/schemas/chat.py ADDED
@@ -0,0 +1,496 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from abc import ABC
2
+ from dataclasses import dataclass
3
+ from enum import StrEnum
4
+ from typing import Any, ClassVar, Optional, Union
5
+ from uuid import uuid4
6
+
7
+ from pydantic import BaseModel, Field
8
+
9
+ from backend.chat.enums import StreamEvent
10
+ from backend.schemas.citation import Citation
11
+ from backend.schemas.document import Document
12
+ from backend.schemas.search_query import SearchQuery
13
+ from backend.schemas.tool import Tool, ToolCall, ToolCallDelta
14
+
15
+
16
+ @dataclass
17
+ class EventState:
18
+ distances_plans: list
19
+ distances_actions: list
20
+ previous_plan: str
21
+ previous_action: str
22
+
23
+
24
+ class ChatRole(StrEnum):
25
+ """
26
+ One of CHATBOT|USER|SYSTEM to identify who the message is coming from.
27
+ """
28
+ CHATBOT = "CHATBOT"
29
+ USER = "USER"
30
+ SYSTEM = "SYSTEM"
31
+ TOOL = "TOOL"
32
+
33
+
34
+ class ChatCitationQuality(StrEnum):
35
+ """
36
+ Dictates the approach taken to generating citations by allowing the user to specify
37
+ whether they want "accurate" results or "fast" results. Defaults to "accurate".
38
+ """
39
+ FAST = "FAST"
40
+ ACCURATE = "ACCURATE"
41
+
42
+
43
+ class ToolInputType(StrEnum):
44
+ """
45
+ Type of input passed to the tool
46
+ """
47
+ QUERY = "QUERY"
48
+ CODE = "CODE"
49
+
50
+
51
+ class ChatMessage(BaseModel):
52
+ """
53
+ A list of previous messages between the user and the model, meant to give the mode
54
+ conversational context for responding to the user's message.
55
+ """
56
+ role: ChatRole = Field(
57
+ ...,
58
+ title="Role",
59
+ description="One of CHATBOT|USER|SYSTEM to identify who the message is coming from.",
60
+ )
61
+ message: Optional[str] = Field(
62
+ None,
63
+ title="Message",
64
+ description="Contents of the chat message.",
65
+ )
66
+ tool_plan: Optional[str] = Field(
67
+ None,
68
+ title="Tool Plan",
69
+ description="Contents of the tool plan.",
70
+ )
71
+ tool_results: Optional[list[dict[str, Any]]] = Field(
72
+ None,
73
+ title="Tool Results",
74
+ description="Results from the tool call.",
75
+ )
76
+ tool_calls: Optional[list[dict[str, Any]]] = Field(
77
+ None,
78
+ title="Tool Calls",
79
+ description="List of tool calls generated for custom tools",
80
+ )
81
+
82
+ def to_dict(self) -> dict[str, Any]:
83
+ return {
84
+ "role": self.role,
85
+ "message": self.message,
86
+ "tool_results": self.tool_results,
87
+ "tool_calls": self.tool_calls,
88
+ }
89
+
90
+
91
+ # TODO: fix titles of these types
92
+ class ChatResponse(ABC, BaseModel):
93
+ """
94
+ Abstract class for Chat Responses
95
+ """
96
+ event_type: ClassVar[StreamEvent] = Field(
97
+ ...,
98
+ title="Event Type",
99
+ description="Chat response event type",
100
+ )
101
+
102
+
103
+ class StreamStart(ChatResponse):
104
+ """Stream start event"""
105
+ event_type: ClassVar[StreamEvent] = StreamEvent.STREAM_START
106
+ generation_id: Optional[str] = Field(
107
+ None,
108
+ title="Generation ID",
109
+ description="Generation ID for the event",
110
+ )
111
+ conversation_id: Optional[str] = Field(
112
+ None,
113
+ title="Conversation ID",
114
+ description="Conversation ID for the event",
115
+ )
116
+
117
+
118
+ class StreamTextGeneration(ChatResponse):
119
+ """Stream text generation event"""
120
+ event_type: ClassVar[StreamEvent] = StreamEvent.TEXT_GENERATION
121
+ text: str = Field(
122
+ ...,
123
+ title="Text",
124
+ description="Contents of the chat message",
125
+ )
126
+
127
+
128
+ class StreamCitationGeneration(ChatResponse):
129
+ """Stream citation generation event"""
130
+ event_type: ClassVar[StreamEvent] = StreamEvent.CITATION_GENERATION
131
+ citations: list[Citation] = Field(
132
+ [],
133
+ title="Citations",
134
+ description="Citations for the chat message",
135
+ )
136
+
137
+
138
+ class StreamQueryGeneration(ChatResponse):
139
+ """Stream query generation event"""
140
+ event_type: ClassVar[StreamEvent] = StreamEvent.SEARCH_QUERIES_GENERATION
141
+ query: str = Field(
142
+ ...,
143
+ title="Query",
144
+ description="Search query used to generate grounded response with citations",
145
+ )
146
+
147
+
148
+ class StreamSearchResults(ChatResponse):
149
+ """Stream search generation event"""
150
+ event_type: ClassVar[StreamEvent] = StreamEvent.SEARCH_RESULTS
151
+ search_results: list[dict[str, Any]] = Field(
152
+ [],
153
+ title="Search Results",
154
+ description="Search results used to generate grounded response with citations",
155
+ )
156
+ documents: list[Document] = Field(
157
+ [],
158
+ title="Documents",
159
+ description="Documents used to generate grounded response with citations",
160
+ )
161
+
162
+
163
+ class StreamToolInput(ChatResponse):
164
+ """Stream tool input generation event"""
165
+ event_type: ClassVar[StreamEvent] = StreamEvent.TOOL_INPUT
166
+ input_type: ToolInputType = Field(
167
+ ...,
168
+ title="Input Type",
169
+ description="Tool input type",
170
+ )
171
+ tool_name: str = Field(
172
+ ...,
173
+ title="Tool Name",
174
+ description="Name of the tool to be used",
175
+ )
176
+ input: str = Field(
177
+ ...,
178
+ title="Input",
179
+ description="Tool input",
180
+ )
181
+ text: str = Field(
182
+ ...,
183
+ title="Text",
184
+ description="Contents of the chat message",
185
+ )
186
+
187
+
188
+ class StreamToolResult(ChatResponse):
189
+ """Stream tool result generation event"""
190
+ event_type: ClassVar[StreamEvent] = StreamEvent.TOOL_RESULT
191
+ result: Any = Field(
192
+ ...,
193
+ title="Result",
194
+ description="Result from the tool",
195
+ )
196
+ tool_name: str = Field(
197
+ ...,
198
+ title="Tool Name",
199
+ description="Name of tool that generated the result",
200
+ )
201
+ documents: list[Document] = Field(
202
+ [],
203
+ title="Documents",
204
+ description="Documents used to generate grounded response with citations",
205
+ )
206
+
207
+
208
+ class StreamSearchQueriesGeneration(ChatResponse):
209
+ """Stream queries generation event"""
210
+ event_type: ClassVar[StreamEvent] = StreamEvent.SEARCH_QUERIES_GENERATION
211
+ search_queries: list[SearchQuery] = Field(
212
+ [],
213
+ title="Search Queries",
214
+ description="Search query used to generate grounded response with citations",
215
+ )
216
+
217
+
218
+ class StreamToolCallsGeneration(ChatResponse):
219
+ """Stream tool calls generation event"""
220
+ event_type: ClassVar[StreamEvent] = StreamEvent.TOOL_CALLS_GENERATION
221
+ stream_search_results: Optional[StreamSearchResults] = Field(
222
+ None,
223
+ title="Stream Search Results",
224
+ description="Search results used to generate grounded response with citations",
225
+ )
226
+ tool_calls: Optional[list[ToolCall]] = Field(
227
+ [],
228
+ title="Tool Calls",
229
+ description="List of tool calls generated for custom tools",
230
+ )
231
+ text: str | None = Field(
232
+ None,
233
+ title="Text",
234
+ description="Contents of the chat message",
235
+ )
236
+
237
+
238
+ class StreamEnd(ChatResponse):
239
+ """Stream end generation event"""
240
+ event_type: ClassVar[StreamEvent] = StreamEvent.STREAM_END
241
+ message_id: Optional[str] = Field(
242
+ None,
243
+ title="Message ID",
244
+ description="Unique identifier for the message",
245
+ )
246
+ response_id: Optional[str] = Field(
247
+ None,
248
+ title="Response ID",
249
+ description="Unique identifier for the response",
250
+ )
251
+ generation_id: Optional[str] = Field(
252
+ None,
253
+ title="Generation ID",
254
+ description="Unique identifier for the generation",
255
+ )
256
+ conversation_id: Optional[str] = Field(
257
+ None,
258
+ title="Conversation ID",
259
+ description="Unique identifier for the conversation",
260
+ )
261
+ text: str = Field(
262
+ ...,
263
+ title="Text",
264
+ description="Contents of the chat message",
265
+ )
266
+ citations: list[Citation] = Field(
267
+ [],
268
+ title="Citations",
269
+ description="Citations for the chat messae.",
270
+ )
271
+ documents: list[Document] = Field(
272
+ [],
273
+ title="Documents",
274
+ description="Documents used to generate grounded response with citations",
275
+ )
276
+ search_results: list[dict[str, Any]] = Field(
277
+ [],
278
+ title="Search Results",
279
+ description="Search results used to generate grounded response with citations",
280
+ )
281
+ search_queries: list[SearchQuery] = Field(
282
+ [],
283
+ title="Search Queries",
284
+ description="List of generated search queries",
285
+ )
286
+ tool_calls: list[ToolCall] = Field(
287
+ [],
288
+ title="Tool Calls",
289
+ description="List of tool calls generated for custom tools",
290
+ )
291
+ finish_reason: Optional[str] = Field(
292
+ None,
293
+ title="Finish Reason",
294
+ description="Reson why the model finished the request",
295
+ )
296
+ chat_history: Optional[list[ChatMessage]] = Field(
297
+ None,
298
+ title="Chat History",
299
+ description="A list of entries used to construct the conversation. If provided, these messages will be used to build the prompt and the conversation_id will be ignored so no data will be stored to maintain state.",
300
+ )
301
+ error: Optional[str] = Field(
302
+ None,
303
+ title="Error",
304
+ description="Error message if the response is an error",
305
+ )
306
+
307
+
308
+ class NonStreamedChatResponse(ChatResponse):
309
+ """Non streamed chat response"""
310
+ response_id: Optional[str] = Field(
311
+ None,
312
+ title="Response ID",
313
+ description="Unique identifier for the response",
314
+ )
315
+ generation_id: Optional[str] = Field(
316
+ None,
317
+ title="Generation ID",
318
+ description="Unique identifier for the generation",
319
+ )
320
+ chat_history: Optional[list[ChatMessage]] = Field(
321
+ None,
322
+ title="Chat History",
323
+ description="A list of previous messages between the user and the model, meant to give the model conversational context for responding to the user's message.",
324
+ )
325
+ finish_reason: str = Field(
326
+ ...,
327
+ title="Finish Reason",
328
+ description="Reason the chat stream ended",
329
+ )
330
+ text: str = Field(
331
+ ...,
332
+ title="Text",
333
+ description="Contents of the chat message",
334
+ )
335
+ citations: list[Citation] | None = Field(
336
+ [],
337
+ title="Citations",
338
+ description="Citations for the chat message",
339
+ )
340
+ documents: list[Document] | None = Field(
341
+ [],
342
+ title="Documents",
343
+ description="Documents used to generate grounded response with citations",
344
+ )
345
+ search_results: list[dict[str, Any]] | None = Field(
346
+ [],
347
+ title="Search Results",
348
+ description="Search results used to generate grounded response with citations",
349
+ )
350
+ search_queries: list[SearchQuery] | None = Field(
351
+ [],
352
+ title="Search Queries",
353
+ description="List of generated search queries.",
354
+ )
355
+ conversation_id: Optional[str] = Field(
356
+ None,
357
+ title="Conversation ID",
358
+ description="To store a conversation then create a conversation id and use it for every related request",
359
+ )
360
+ tool_calls: list[ToolCall] | None = Field(
361
+ [],
362
+ title="Tool Calls",
363
+ description="List of tool calls generated for custom tools",
364
+ )
365
+ error: Optional[str] = Field(
366
+ None,
367
+ title="Error",
368
+ description="Error message if the response is an error",
369
+ )
370
+
371
+
372
+ class StreamToolCallsChunk(ChatResponse):
373
+ """Stream tool call chunk generated event"""
374
+ event_type: ClassVar[StreamEvent] = StreamEvent.TOOL_CALLS_CHUNK
375
+ tool_call_delta: Optional[ToolCallDelta] = Field(
376
+ ToolCallDelta(
377
+ name=None,
378
+ index=None,
379
+ parameters=None,
380
+ ),
381
+ title="Tool Call Delta",
382
+ description="Partial tool call",
383
+ )
384
+ text: Optional[str] = Field(
385
+ None,
386
+ title="Text",
387
+ description="Contents of the chat message",
388
+ )
389
+
390
+
391
+ StreamEventType = Union[
392
+ StreamStart,
393
+ StreamTextGeneration,
394
+ StreamCitationGeneration,
395
+ StreamQueryGeneration,
396
+ StreamSearchResults,
397
+ StreamEnd,
398
+ StreamToolInput,
399
+ StreamToolResult,
400
+ StreamSearchQueriesGeneration,
401
+ StreamToolCallsGeneration,
402
+ StreamToolCallsChunk,
403
+ NonStreamedChatResponse,
404
+ ]
405
+
406
+
407
+ class ChatResponseEvent(BaseModel):
408
+ """
409
+ Chat Response Event
410
+ """
411
+ event: StreamEvent = Field(
412
+ ...,
413
+ title="Event",
414
+ description="Type of stream event",
415
+ )
416
+ data: StreamEventType = Field(
417
+ ...,
418
+ title="Data",
419
+ description="Data returned from chat response of a given event type",
420
+ )
421
+
422
+
423
+ class BaseChatRequest(BaseModel):
424
+ """
425
+ Base Chat Request
426
+ """
427
+ message: str = Field(
428
+ ...,
429
+ title="Message",
430
+ description="The message to send to the chatbot",
431
+ )
432
+ chat_history: list[ChatMessage] | None = Field(
433
+ None,
434
+ title="Chat History",
435
+ description="A list of entries used to construct the conversation. If provided, these messages will be used to build the prompt and the conversation_id will be ignored so no data will be stored to maintain state.",
436
+ )
437
+ conversation_id: str = Field(
438
+ default_factory=lambda: str(uuid4()),
439
+ title="Conversation ID",
440
+ description="To store a conversation then create a conversation id and use it for every related request",
441
+ )
442
+ tools: Optional[list[Tool]] = Field(
443
+ default_factory=list,
444
+ title="Tools",
445
+ description="""
446
+ List of custom or managed tools to use for the response.
447
+ If passing in managed tools, you only need to provide the name of the tool.
448
+ If passing in custom tools, you need to provide the name, description, and optionally parameter defintions of the tool.
449
+ Passing a mix of custom and managed tools is not supported.
450
+
451
+ Managed Tools Examples:
452
+ tools=[
453
+ {
454
+ "name": "Wiki Retriever - LangChain",
455
+ },
456
+ {
457
+ "name": "Calculator",
458
+ }
459
+ ]
460
+
461
+ Custom Tools Examples:
462
+ tools=[
463
+ {
464
+ "name": "movie_title_generator",
465
+ "description": "tool to generate a cool movie title",
466
+ "parameter_definitions": {
467
+ "synopsis": {
468
+ "description": "short synopsis of the movie",
469
+ "type": "str",
470
+ "required": true
471
+ }
472
+ }
473
+ },
474
+ {
475
+ "name": "random_number_generator",
476
+ "description": "tool to generate a random number between min and max",
477
+ "parameter_definitions": {
478
+ "min": {
479
+ "description": "minimum number",
480
+ "type": "int",
481
+ "required": true
482
+ },
483
+ "max": {
484
+ "description": "maximum number",
485
+ "type": "int",
486
+ "required": true
487
+ }
488
+ }
489
+ },
490
+ {
491
+ "name": "joke_generator",
492
+ "description": "tool to generate a random joke",
493
+ }
494
+ ]
495
+ """,
496
+ )
western_ai/canada/cohere-toolkit/src/backend/schemas/citation.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel, Field
2
+
3
+
4
+ class Citation(BaseModel):
5
+ """
6
+ Schema for a citation
7
+ """
8
+ text: str = Field(
9
+ ...,
10
+ title="Text",
11
+ description="Citation text",
12
+ )
13
+ start: int = Field(
14
+ ...,
15
+ title="Start",
16
+ description="Start position for the citation",
17
+ )
18
+ end: int = Field(
19
+ ...,
20
+ title="End",
21
+ description="End position for the citation",
22
+ )
23
+ document_ids: list[str] = Field(
24
+ ...,
25
+ title="Document IDs",
26
+ description="Documents used for the citation",
27
+ )
28
+
29
+ class Config:
30
+ from_attributes = True
western_ai/canada/cohere-toolkit/src/backend/schemas/cohere_chat.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from enum import StrEnum
2
+ from typing import Any, Optional
3
+
4
+ from pydantic import Field
5
+
6
+ from backend.schemas.chat import BaseChatRequest
7
+
8
+
9
+ class CohereChatPromptTruncation(StrEnum):
10
+ """Dictates how the prompt will be constructed. Defaults to "AUTO_PRESERVE_ORDER"."""
11
+ OFF = "OFF"
12
+ AUTO_PRESERVE_ORDER = "AUTO_PRESERVE_ORDER"
13
+
14
+
15
+ class CohereChatRequest(BaseChatRequest):
16
+ """
17
+ Request shape for Cohere Python SDK Streamed Chat.
18
+ See: https://github.com/cohere-ai/cohere-python/blob/main/src/cohere/base_client.py#L1629
19
+ """
20
+ documents: list[dict[str, Any]] = Field(
21
+ default_factory=list,
22
+ title="Documents",
23
+ description="""Documents to use to generate grounded response with citations. Example:
24
+ documents=[
25
+ {
26
+ "id": "national_geographic_everest",
27
+ "title": "Height of Mount Everest",
28
+ "text": "The height of Mount Everest is 29,035 feet",
29
+ "url": "https://education.nationalgeographic.org/resource/mount-everest/",
30
+ },
31
+ {
32
+ "id": "national_geographic_mariana",
33
+ "title": "Depth of the Mariana Trench",
34
+ "text": "The depth of the Mariana Trench is 36,070 feet",
35
+ "url": "https://www.nationalgeographic.org/activity/mariana-trench-deepest-place-earth",
36
+ },
37
+ ]
38
+ """,
39
+ )
40
+ model: Optional[str] = Field(
41
+ "command-r-plus",
42
+ title="Model",
43
+ description="The model to use for generating the response.",
44
+ )
45
+ temperature: Optional[float] = Field(
46
+ None,
47
+ title="Temperature",
48
+ description="A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations.",
49
+ ge=0,
50
+ )
51
+ k: Optional[int] = Field(
52
+ None,
53
+ title="Top-K",
54
+ description="Ensures only the top k most likely tokens are considered for generation at each step.",
55
+ le=500,
56
+ ge=0,
57
+ )
58
+ p: Optional[float] = Field(
59
+ None,
60
+ title="Top-P",
61
+ description="Ensures that only the most likely tokens, with total probability mass of p, are considered for generation at each step. If both k and p are enabled, p acts after k.",
62
+ le=0.99,
63
+ ge=0,
64
+ )
65
+ preamble: Optional[str] = Field(
66
+ None,
67
+ title="Preamble",
68
+ description="A string to override the preamble.",
69
+ )
70
+ file_ids: Optional[list[str]] = Field(
71
+ None,
72
+ title="File IDs",
73
+ description="List of File IDs for PDFs used in RAG for the response.",
74
+ exclude=True,
75
+ )
76
+ search_queries_only: Optional[bool] = Field(
77
+ False,
78
+ title="Search Queries Only",
79
+ description="When set to true a list of search queries are generated. No search will occur nor replies to the user's message.",
80
+ )
81
+ max_tokens: Optional[int] = Field(
82
+ None,
83
+ title="Max Tokens",
84
+ description="The maximum number of tokens the model will generate as part of the response. Note: Setting a low value may result in incomplete generations.",
85
+ ge=1,
86
+ )
87
+ seed: Optional[float] = Field(
88
+ None,
89
+ title="Seed",
90
+ description="If specified, the backend will make a best effort to sample tokens deterministically, such that repeated requests with the same seed and parameters should return the same result. However, determinism cannot be totally guaranteed.",
91
+ )
92
+ stop_sequences: Optional[list[str]] = Field(
93
+ None,
94
+ title="Stop Sequences",
95
+ description="A list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence.",
96
+ )
97
+ presence_penalty: Optional[float] = Field(
98
+ None,
99
+ title="Presence Penalty",
100
+ description="Used to reduce repetitiveness of generated tokens. Similar to frequency_penalty, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.",
101
+ ge=0,
102
+ le=1,
103
+ )
104
+ frequency_penalty: Optional[float] = Field(
105
+ None,
106
+ title="Frequency Penalty",
107
+ description="Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.",
108
+ ge=0,
109
+ le=1,
110
+ )
111
+ prompt_truncation: Optional[CohereChatPromptTruncation] = Field(
112
+ CohereChatPromptTruncation.AUTO_PRESERVE_ORDER,
113
+ title="Prompt Truncation",
114
+ description="Dictates how the prompt will be constructed. Defaults to 'AUTO_PRESERVE_ORDER'.",
115
+ )
116
+ tool_results: Optional[list[dict[str, Any]]] = Field(
117
+ None,
118
+ title="Tool Results",
119
+ description="A list of results from invoking tools recommended by the model in the previous chat turn. Results are used to produce a text response and will be referenced in citations.",
120
+ )
121
+ force_single_step: Optional[bool] = Field(
122
+ None,
123
+ title="Force Single Step",
124
+ description="If set to true, the model will generate a single response in a single step. This is useful for generating a response to a single message.",
125
+ )
126
+ agent_id: Optional[str] = Field(
127
+ None,
128
+ title="Agent ID",
129
+ description="The agent ID to use for the chat.",
130
+ )
western_ai/canada/cohere-toolkit/src/backend/schemas/context.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Optional, Self
2
+
3
+ from pydantic import BaseModel
4
+
5
+ from backend.crud import organization as organization_crud
6
+ from backend.crud import user as user_crud
7
+ from backend.database_models.database import DBSessionDep
8
+ from backend.schemas import Organization
9
+ from backend.schemas.agent import Agent, AgentToolMetadata
10
+ from backend.schemas.user import User
11
+ from backend.services.logger.utils import LoggerFactory
12
+ from backend.services.utils import get_deployment_config
13
+
14
+
15
+ class Context(BaseModel):
16
+ """
17
+ Context for a request
18
+ """
19
+ request: Optional[dict] = {}
20
+ response: Optional[dict] = {}
21
+ receive: Optional[dict] = {}
22
+ trace_id: str = "default"
23
+ user_id: str = "default"
24
+ user: Optional[User] = None
25
+ agent: Optional[Agent] = None
26
+ agent_tool_metadata: Optional[AgentToolMetadata] = None
27
+ model: Optional[str] = None
28
+ deployment_name: Optional[str] = None
29
+ deployment_config: Optional[dict] = None
30
+ conversation_id: Optional[str] = None
31
+ agent_id: Optional[str] = None
32
+ stream_start_ms: Optional[float] = None
33
+ logger: Optional[Any] = None
34
+ organization_id: Optional[str] = None
35
+ organization: Optional[Organization] = None
36
+ use_global_filtering: Optional[bool] = False
37
+
38
+ def __init__(self):
39
+ super().__init__()
40
+ self.with_logger()
41
+
42
+ def set_request(self, request):
43
+ self.request = request
44
+
45
+ def set_response(self, response):
46
+ self.response = response
47
+
48
+ def set_receive(self, receive):
49
+ self.receive = receive
50
+
51
+ def with_logger(self):
52
+ if self.logger is not None:
53
+ return self
54
+
55
+ logger = LoggerFactory().get_logger()
56
+ logger.bind(trace_id=self.trace_id, user_id=self.user_id)
57
+ self.logger = logger
58
+ return self
59
+
60
+ def with_trace_id(self, trace_id: str):
61
+ self.trace_id = trace_id
62
+
63
+ def with_user_id(self, user_id: str):
64
+ self.user_id = user_id
65
+
66
+ def with_deployment_name(self, deployment_name: str):
67
+ self.deployment_name = deployment_name
68
+
69
+ def with_user(
70
+ self, session: DBSessionDep | None = None, user: User | None = None
71
+ ) -> Self:
72
+ if not user and not session:
73
+ return self
74
+
75
+ if not user:
76
+ user = user_crud.get_user(session, self.user_id)
77
+ user = User.model_validate(user)
78
+
79
+ if user:
80
+ self.user = user
81
+
82
+ return self
83
+
84
+ def with_agent(self, agent: Agent | None) -> Self:
85
+ self.agent = agent
86
+ return self
87
+
88
+ def with_agent_tool_metadata(
89
+ self, agent_tool_metadata: AgentToolMetadata
90
+ ) -> Self:
91
+ self.agent_tool_metadata = agent_tool_metadata
92
+ return self
93
+
94
+ def with_model(self, model: str) -> Self:
95
+ self.model = model
96
+ return self
97
+
98
+ def with_deployment_config(self, deployment_config={}) -> Self:
99
+ if deployment_config:
100
+ self.deployment_config = deployment_config
101
+ else:
102
+ self.deployment_config = get_deployment_config(self.request)
103
+ return self
104
+
105
+ def with_conversation_id(self, conversation_id: str) -> Self:
106
+ self.conversation_id = conversation_id
107
+ return self
108
+
109
+ def with_stream_start_ms(self, now_ms: float) -> Self:
110
+ self.stream_start_ms = now_ms
111
+
112
+ def with_agent_id(self, agent_id: str) -> Self:
113
+ if not agent_id:
114
+ return self
115
+
116
+ self.agent_id = agent_id
117
+ return self
118
+
119
+ def with_organization_id(self, organization_id: str) -> Self:
120
+ self.organization_id = organization_id
121
+ return self
122
+
123
+ def with_organization(
124
+ self,
125
+ session: DBSessionDep | None = None,
126
+ organization: Organization | None = None,
127
+ ) -> Self:
128
+ if not organization and not session:
129
+ return self
130
+
131
+ if not organization:
132
+ organization = organization_crud.get_organization(
133
+ session, self.organization_id
134
+ )
135
+ organization = (
136
+ Organization.model_validate(organization) if organization else None
137
+ )
138
+
139
+ if organization:
140
+ self.organization = organization
141
+
142
+ return self
143
+
144
+ def with_global_filtering(self) -> Self:
145
+ self.use_global_filtering = True
146
+ return self
147
+
148
+ def without_global_filtering(self) -> Self:
149
+ self.use_global_filtering = False
150
+ return self
151
+
152
+ def get_organization(self):
153
+ return self.organization
154
+
155
+ def get_stream_start_ms(self):
156
+ return self.stream_start_ms
157
+
158
+ def get_request(self):
159
+ return self.request
160
+
161
+ def get_response(self):
162
+ return self.response
163
+
164
+ def get_receive(self):
165
+ return self.receive
166
+
167
+ def get_trace_id(self):
168
+ return self.trace_id
169
+
170
+ def get_user_id(self):
171
+ return self.user_id
172
+
173
+ def get_event_type(self):
174
+ return self.event_type
175
+
176
+ def get_model(self):
177
+ return self.model
178
+
179
+ def get_deployment_name(self):
180
+ return self.deployment_name
181
+
182
+ def get_model_config(self):
183
+ return self.model_config
184
+
185
+ def get_conversation_id(self):
186
+ return self.conversation_id
187
+
188
+ def get_agent_id(self):
189
+ return self.agent_id
190
+
191
+ def get_logger(self) -> Any:
192
+ return self.logger
193
+
194
+ def get_agent_tool_metadata(self):
195
+ return self.agent_tool_metadata
western_ai/canada/cohere-toolkit/src/backend/schemas/conversation.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ from abc import ABC
3
+ from typing import Optional
4
+
5
+ from pydantic import BaseModel, Field, computed_field
6
+
7
+ from backend.schemas.file import ConversationFilePublic
8
+ from backend.schemas.message import Message
9
+
10
+
11
+ class ConversationBase(ABC, BaseModel):
12
+ """
13
+ Abstract class for Conversation Schemas
14
+ """
15
+ user_id: str = Field(
16
+ ...,
17
+ title="User ID",
18
+ description="User ID for the conversation",
19
+ )
20
+ organization_id: Optional[str] = Field(
21
+ None,
22
+ title="Organization ID",
23
+ description="Organization ID for the conversation",
24
+ )
25
+
26
+
27
+ class Conversation(ConversationBase):
28
+ """
29
+ Schema for a conversation
30
+ """
31
+ id: str = Field(
32
+ ...,
33
+ title="ID",
34
+ description="Unique identifier for the conversation",
35
+ )
36
+ created_at: datetime.datetime = Field(
37
+ ...,
38
+ title="Created At Timestamp",
39
+ description="When the conversation was created",
40
+ )
41
+ updated_at: datetime.datetime = Field(
42
+ ...,
43
+ title="Updated At Timestamp",
44
+ description="When the conversation was updated",
45
+ )
46
+
47
+ title: str = Field(
48
+ ...,
49
+ title="Title",
50
+ description="Title of the conversation",
51
+ )
52
+ messages: list[Message] = Field(
53
+ ...,
54
+ title="Messages",
55
+ description="The conversation messages",
56
+ )
57
+ files: list[ConversationFilePublic] = Field(
58
+ ...,
59
+ title="Files",
60
+ description="List of files for the conversation",
61
+ )
62
+ description: Optional[str] = Field(
63
+ None,
64
+ title="Description",
65
+ description="Description of the conversation",
66
+ )
67
+ agent_id: Optional[str] = Field(
68
+ None,
69
+ title="Agent ID",
70
+ description="Unique identifier for the agent used in the conversation",
71
+ )
72
+ is_pinned: bool = Field(
73
+ ...,
74
+ title="Is Pinned",
75
+ description="If conversation is pinned",
76
+ )
77
+
78
+ @computed_field(return_type=int)
79
+ def total_file_size(self):
80
+ return sum([getattr(file, "file_size", 0) for file in self.files])
81
+
82
+ class Config:
83
+ from_attributes = True
84
+
85
+
86
+ class ConversationPublic(Conversation):
87
+ """
88
+ A public conversation which removes the User ID and Organization ID
89
+ """
90
+ user_id: Optional[str] = Field(
91
+ exclude=True,
92
+ title="User ID",
93
+ description="User ID for the conversation",
94
+ )
95
+ organization_id: Optional[str] = Field(
96
+ exclude=True,
97
+ title="Organization ID",
98
+ description="Organization ID for the conversation",
99
+ )
100
+
101
+
102
+ class ConversationWithoutMessages(ConversationPublic):
103
+ """
104
+ A public conversation without messages attached
105
+ """
106
+ messages: list[Message] = Field(
107
+ exclude=True,
108
+ title="Messages",
109
+ description="The conversation messages",
110
+ )
111
+
112
+
113
+ class UpdateConversationRequest(BaseModel):
114
+ """
115
+ Request to update a conversation
116
+ """
117
+ title: Optional[str] = Field(
118
+ None,
119
+ title="Title",
120
+ description="Title of the conversation",
121
+ )
122
+ description: Optional[str] = Field(
123
+ None,
124
+ title="Description",
125
+ description="Description of the conversation",
126
+ )
127
+
128
+ class Config:
129
+ from_attributes = True
130
+
131
+
132
+ class ToggleConversationPinRequest(BaseModel):
133
+ """
134
+ Request to toggle pinning a conversation
135
+ """
136
+ is_pinned: bool = Field(
137
+ ...,
138
+ title="Is Pinned",
139
+ description="If conversation is pinned",
140
+ )
141
+
142
+
143
+ class DeleteConversationResponse(BaseModel):
144
+ """
145
+ Response for deleting a conversation
146
+ """
147
+ pass
148
+
149
+
150
+ class GenerateTitleResponse(BaseModel):
151
+ """
152
+ Response for generating a title
153
+ """
154
+ title: str = Field(
155
+ ...,
156
+ title="Title",
157
+ description="Title generated for the conversation",
158
+ )
159
+ error: Optional[str] = Field(
160
+ None,
161
+ title="Error",
162
+ description="Error message if the response is an error",
163
+ )
western_ai/canada/cohere-toolkit/src/backend/schemas/deployment.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+
3
+ from pydantic import BaseModel, Field
4
+
5
+
6
+ class DeploymentDefinition(BaseModel):
7
+ """
8
+ Deployment Definition
9
+ """
10
+ id: str = Field(
11
+ ...,
12
+ title="ID",
13
+ description="Unique Identifier for the Deployment",
14
+ )
15
+ name: str = Field(
16
+ ...,
17
+ title="Name",
18
+ description="Name of the Deployment",
19
+ )
20
+ description: Optional[str] = Field(
21
+ None,
22
+ title="Description",
23
+ description="Description of the deployment",
24
+ )
25
+ config: dict[str, str] = Field(
26
+ {},
27
+ title="Config",
28
+ description="Config for the deployment",
29
+ )
30
+ is_available: bool = Field(
31
+ False,
32
+ title="Is Available",
33
+ description="Is deployment is available",
34
+ )
35
+ is_community: Optional[bool] = Field(
36
+ False,
37
+ title="Is Community",
38
+ description="Is the deployment from the commmunity",
39
+ )
40
+ models: list[str] = Field(
41
+ ...,
42
+ title="Models",
43
+ description="List of models for the deployment",
44
+ )
45
+ class_name: str = Field(
46
+ ...,
47
+ title="Class Name",
48
+ description="Deployment class name",
49
+ )
50
+
51
+ class Config:
52
+ from_attributes = True
53
+
54
+ @classmethod
55
+ def from_db_deployment(cls, obj):
56
+ data = {
57
+ "id": obj.id,
58
+ "name": obj.name,
59
+ "description": obj.description if obj.description else None,
60
+ "models": [model.name for model in obj.models],
61
+ "is_community": obj.is_community,
62
+ "is_available": obj.is_available,
63
+ "config": obj.default_deployment_config,
64
+ "class_name": obj.deployment_class_name,
65
+ }
66
+ return cls(**data)
67
+
68
+
69
+ class DeploymentCreate(BaseModel):
70
+ """
71
+ Deployment Create Schema
72
+ """
73
+ id: Optional[str] = Field(
74
+ None,
75
+ title="ID",
76
+ description="Unique Identifier for the Deployment",
77
+ )
78
+ name: str = Field(
79
+ ...,
80
+ title="Name",
81
+ description="Name of the Deployment",
82
+ )
83
+ description: Optional[str] = Field(
84
+ None,
85
+ title="Description",
86
+ description="Description of the deployment",
87
+ )
88
+ deployment_class_name: str = Field(
89
+ ...,
90
+ title="Deployment Class Name",
91
+ description="Deployment Class Name",
92
+ )
93
+ is_community: bool = Field(
94
+ False,
95
+ title="Is Community",
96
+ description="Is the deployment from the commmunity",
97
+ )
98
+ default_deployment_config: dict[str, str] = Field(
99
+ ...,
100
+ title="Default Deployment Config",
101
+ description="The default deployment configuration",
102
+ )
103
+
104
+
105
+ class DeploymentUpdate(BaseModel):
106
+ """
107
+ Deployment Update Schema
108
+ """
109
+ name: Optional[str] = Field(
110
+ None,
111
+ title="Name",
112
+ description="Name of the Deployment",
113
+ )
114
+ description: Optional[str] = Field(
115
+ None,
116
+ title="Description",
117
+ description="Description of the deployment",
118
+ )
119
+ deployment_class_name: Optional[str] = Field(
120
+ None,
121
+ title="Deployment Class Name",
122
+ description="Deployment Class Name",
123
+ )
124
+ is_community: Optional[bool] = Field(
125
+ None,
126
+ title="Is Community",
127
+ description="Is the deployment from the commmunity",
128
+ )
129
+ default_deployment_config: Optional[dict[str, str]] = Field(
130
+ None,
131
+ title="Default Deployment Config",
132
+ description="The default deployment configuration",
133
+ )
134
+
135
+
136
+ class DeleteDeployment(BaseModel):
137
+ """
138
+ Delete Deployment Response
139
+ """
140
+ pass
141
+
142
+
143
+ class UpdateDeploymentEnv(BaseModel):
144
+ """
145
+ Request to update Deployment Environment Variables
146
+ """
147
+ env_vars: dict[str, str] = Field(
148
+ ...,
149
+ title="Env Vars",
150
+ description="Environment Variables for the Deployment",
151
+ )
western_ai/canada/cohere-toolkit/src/backend/schemas/document.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+
3
+ from pydantic import BaseModel, Field
4
+
5
+
6
+ class Document(BaseModel):
7
+ """
8
+ Schema for a Document
9
+ """
10
+ text: str = Field(
11
+ ...,
12
+ title="Text",
13
+ description="Document text",
14
+ )
15
+ document_id: str = Field(
16
+ ...,
17
+ title="Document_Id",
18
+ description="Unique Identifier for the document",
19
+ )
20
+
21
+ title: Optional[str] = Field(
22
+ None,
23
+ title="Title",
24
+ description="Document title",
25
+ )
26
+ url: Optional[str] = Field(
27
+ None,
28
+ title="URL",
29
+ description="Document URL",
30
+ )
31
+ fields: Optional[dict] = Field(
32
+ None,
33
+ title="Fields",
34
+ description="Document Fields",
35
+ )
36
+ tool_name: Optional[str] = Field(
37
+ None,
38
+ title="Tool Name",
39
+ description="Tool name for the document",
40
+ )
41
+
42
+ class Config:
43
+ from_attributes = True
western_ai/canada/cohere-toolkit/src/backend/schemas/file.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ from abc import ABC
3
+ from typing import Optional
4
+
5
+ from pydantic import BaseModel, Field
6
+
7
+
8
+ class FileBase(ABC, BaseModel):
9
+ """
10
+ Abstract class for File schemas
11
+ """
12
+ id: str = Field(
13
+ ...,
14
+ title="ID",
15
+ description="Unique identifier of the file",
16
+ )
17
+ created_at: datetime.datetime = Field(
18
+ ...,
19
+ title="Created At Timestamp",
20
+ description="When file was created",
21
+ )
22
+ updated_at: datetime.datetime = Field(
23
+ ...,
24
+ title="Updated At Timestamp",
25
+ description="When file was updated",
26
+ )
27
+ file_name: str = Field(
28
+ ...,
29
+ title="File Name",
30
+ description="Name of the file",
31
+ )
32
+ file_size: int = Field(
33
+ 0,
34
+ title="File Size",
35
+ description="Size of the file in bytes",
36
+ ge=0
37
+ )
38
+
39
+
40
+
41
+ class File(FileBase):
42
+ """
43
+ Schema for a File
44
+ """
45
+ user_id: str = Field(
46
+ ...,
47
+ title="User ID",
48
+ description="Unique identifier for who created the file",
49
+ )
50
+ conversation_id: Optional[str] = Field(
51
+ "",
52
+ title="Conversation ID",
53
+ description="Unique identifier for the conversation the file is associated to",
54
+ )
55
+ file_content: Optional[str] = Field(
56
+ "",
57
+ title="File Content",
58
+ description="The contents of the file",
59
+ )
60
+
61
+ class Config:
62
+ from_attributes = True
63
+
64
+
65
+ class ConversationFilePublic(FileBase):
66
+ """
67
+ Schema for a public conversation file
68
+ """
69
+ user_id: str = Field(
70
+ ...,
71
+ title="User ID",
72
+ description="Unique identifier for who created the file",
73
+ )
74
+ conversation_id: str = Field(
75
+ ...,
76
+ title="Conversation ID",
77
+ description="Unique identifier for the conversation the file is associated to",
78
+ )
79
+
80
+
81
+ class AgentFilePublic(FileBase):
82
+ """
83
+ Schema for a public agent file
84
+ """
85
+ pass
86
+
87
+
88
+ class FileMetadata(FileBase):
89
+ """
90
+ Schema for file metadata
91
+ """
92
+ file_content: str = Field(
93
+ ...,
94
+ title="File Content",
95
+ description="The contents of the file",
96
+ )
97
+
98
+
99
+ class ListConversationFile(ConversationFilePublic):
100
+ """
101
+ Listing conversation files
102
+ """
103
+ pass
104
+
105
+
106
+ class UploadConversationFileResponse(ConversationFilePublic):
107
+ """
108
+ Response for uploading a conversation file
109
+ """
110
+ pass
111
+
112
+
113
+ class UploadAgentFileResponse(AgentFilePublic):
114
+ """
115
+ Reponse for uploading an agent file
116
+ """
117
+ pass
118
+
119
+
120
+ class DeleteConversationFileResponse(BaseModel):
121
+ """
122
+ Response for deleting a conversation file
123
+ """
124
+ pass
125
+
126
+
127
+ class DeleteAgentFileResponse(BaseModel):
128
+ """
129
+ Response for deleting an agent file
130
+ """
131
+ pass
western_ai/canada/cohere-toolkit/src/backend/schemas/message.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ from abc import ABC
3
+ from typing import Optional
4
+
5
+ from pydantic import BaseModel, Field
6
+
7
+ from backend.database_models.message import MessageAgent
8
+ from backend.schemas.citation import Citation
9
+ from backend.schemas.document import Document
10
+ from backend.schemas.file import ConversationFilePublic
11
+ from backend.schemas.tool import ToolCall
12
+
13
+
14
+ class MessageBase(ABC, BaseModel):
15
+ """
16
+ Abstract class for Message schemas
17
+ """
18
+ text: str = Field(
19
+ ...,
20
+ title="Text",
21
+ description="The text content of the message",
22
+ )
23
+
24
+
25
+ class Message(MessageBase):
26
+ """
27
+ Message Schema
28
+ """
29
+ id: str = Field(
30
+ ...,
31
+ title="ID",
32
+ description="Unique identifier of the message",
33
+ )
34
+ created_at: datetime.datetime = Field(
35
+ ...,
36
+ title="Created At Timestamp",
37
+ description="When message was created",
38
+ )
39
+ updated_at: datetime.datetime = Field(
40
+ ...,
41
+ title="Updated At Timestamp",
42
+ description="When message was updated",
43
+ )
44
+
45
+ generation_id: Optional[str] = Field(
46
+ None,
47
+ title="Generation ID",
48
+ description="Generation ID for the message",
49
+ )
50
+
51
+ position: int = Field(
52
+ ...,
53
+ title="Position",
54
+ description="Position in the conversation",
55
+ )
56
+ is_active: bool = Field(
57
+ ...,
58
+ title="Is Active",
59
+ description="Is the message active",
60
+ )
61
+
62
+ documents: list[Document] = Field(
63
+ ...,
64
+ title="Documents",
65
+ description="Documents associated with the message",
66
+ )
67
+ citations: list[Citation] = Field(
68
+ ...,
69
+ title="Citations",
70
+ description="Citations associated with the message",
71
+ )
72
+ files: list[ConversationFilePublic] = Field(
73
+ ...,
74
+ title="Files",
75
+ description="Files associated with the message",
76
+ )
77
+ tool_calls: list[ToolCall] = Field(
78
+ ...,
79
+ title="Tool Calls",
80
+ description="Tool calls associated with the message",
81
+ )
82
+ tool_plan: Optional[str] = Field(
83
+ None,
84
+ title="Tool Plan",
85
+ description="Tool plan associated with the message",
86
+ )
87
+
88
+ agent: MessageAgent = Field(
89
+ ...,
90
+ title="Agent",
91
+ description="Agent associated with the message",
92
+ )
93
+
94
+ class Config:
95
+ from_attributes = True
96
+
97
+
98
+ class UpdateMessage(BaseModel):
99
+ """
100
+ Request to update a message
101
+ """
102
+ text: Optional[str] = Field(
103
+ None,
104
+ title="Text",
105
+ description="The text content of the message",
106
+ )
107
+ title: Optional[str] = Field(
108
+ None,
109
+ title="Title",
110
+ description="The title of the message",
111
+ )
112
+
113
+ class Config:
114
+ from_attributes = True
western_ai/canada/cohere-toolkit/src/backend/schemas/model.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from abc import ABC
2
+ from typing import Optional
3
+
4
+ from pydantic import BaseModel, Field
5
+
6
+
7
+ class BaseModelSchema(ABC, BaseModel):
8
+ """
9
+ Abstract class for Model Schemas
10
+ """
11
+ name: str = Field(
12
+ ...,
13
+ title="Name",
14
+ description="Model name",
15
+ )
16
+ cohere_name: Optional[str] = Field(
17
+ None,
18
+ title="Cohere Name",
19
+ description="Cohere model name",
20
+ )
21
+ description: Optional[str] = Field(
22
+ None,
23
+ title="Description",
24
+ description="Model description",
25
+ )
26
+
27
+
28
+ class Model(BaseModelSchema):
29
+ id: str = Field(
30
+ ...,
31
+ title="ID",
32
+ description="Unique identifier for the model"
33
+ )
34
+ deployment_id: str = Field(
35
+ ...,
36
+ title="Deployment ID",
37
+ description="Unique identifier for the deployment"
38
+ )
39
+
40
+ class Config:
41
+ from_attributes = True
42
+
43
+
44
+ class ModelCreate(BaseModelSchema):
45
+ deployment_id: str = Field(
46
+ ...,
47
+ title="Deployment ID",
48
+ description="Unique identifier for the deployment"
49
+ )
50
+
51
+
52
+ class ModelUpdate(BaseModelSchema):
53
+ name: Optional[str] = Field(
54
+ None,
55
+ title="Name",
56
+ description="Model name",
57
+ )
58
+ deployment_id: Optional[str] = Field(
59
+ None,
60
+ title="Deployment ID",
61
+ description="Unique identifier for the deployment"
62
+ )
63
+
64
+
65
+ class DeleteModel(BaseModel):
66
+ """
67
+ Response for deleting a model
68
+ """
69
+ pass
western_ai/canada/cohere-toolkit/src/backend/schemas/organization.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ from abc import ABC
3
+ from typing import Optional
4
+
5
+ from pydantic import BaseModel, Field
6
+
7
+
8
+ class OrganizationBase(ABC, BaseModel):
9
+ """
10
+ Abstract class for organization schemas
11
+ """
12
+ name: str = Field(
13
+ ...,
14
+ title="Name",
15
+ description="Name of the organization",
16
+ )
17
+
18
+
19
+ class Organization(OrganizationBase):
20
+ """
21
+ Schema for an organization
22
+ """
23
+ id: str = Field(
24
+ ...,
25
+ title="ID",
26
+ description="Unique identifier of the organization",
27
+ )
28
+ created_at: datetime.datetime = Field(
29
+ ...,
30
+ title="Created At Timestamp",
31
+ description="When organization was created",
32
+ )
33
+ updated_at: datetime.datetime = Field(
34
+ ...,
35
+ title="Updated At Timestamp",
36
+ description="When organization was updated",
37
+ )
38
+
39
+ class Config:
40
+ from_attributes = True
41
+
42
+
43
+ class CreateOrganization(OrganizationBase):
44
+ """
45
+ Request to create an organization
46
+ """
47
+ pass
48
+
49
+
50
+ class UpdateOrganization(OrganizationBase):
51
+ """
52
+ Request to update an organization
53
+ """
54
+ name: Optional[str] = Field(
55
+ None,
56
+ title="Name",
57
+ description="Name of the organization",
58
+ )
59
+
60
+
61
+ class DeleteOrganization(BaseModel):
62
+ """
63
+ Response when deleting organization
64
+ """
65
+ pass
western_ai/canada/cohere-toolkit/src/backend/schemas/params/__init__.py ADDED
File without changes
western_ai/canada/cohere-toolkit/src/backend/schemas/params/agent.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Agents
3
+ """
4
+ from typing import Annotated, Optional
5
+
6
+ from fastapi import Path, Query
7
+
8
+ from backend.schemas.agent import AgentVisibility
9
+
10
+ VisibilityQueryParam = Annotated[AgentVisibility, Query(
11
+ title="Visibility",
12
+ description="Agent visibility",
13
+ )]
14
+
15
+ AgentIdQueryParam = Annotated[Optional[str], Query(
16
+ title="Agent ID",
17
+ description="Agent ID to filter results by",
18
+ )]
19
+
20
+ AgentIdPathParam = Annotated[str, Path(
21
+ title="Agent ID",
22
+ description="Agent ID for agent in question",
23
+ )]
24
+
25
+ AgentToolMetadataIdPathParam = Annotated[str, Path(
26
+ title="Agent Tool Metadata ID",
27
+ description="Agent Tool Metadata ID for tool metadata in question",
28
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/auth.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Auth
3
+ """
4
+ from typing import Annotated, Optional
5
+
6
+ from fastapi import Path, Query
7
+
8
+ StrategyPathParam = Annotated[str, Path(
9
+ title="Strategy Name",
10
+ description="Name of strategy in question",
11
+ )]
12
+
13
+ CodeQueryParam = Annotated[Optional[str], Query(
14
+ title="Code",
15
+ description="OAuth Code",
16
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/conversation.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Conversations
3
+ """
4
+ from typing import Annotated
5
+
6
+ from fastapi import Path, Query
7
+
8
+ ConversationIdPathParam = Annotated[str, Path(
9
+ title="Conversation ID",
10
+ description="Conversation ID for conversation in question",
11
+ )]
12
+
13
+ QueryQueryParam = Annotated[str, Query(
14
+ title="Query",
15
+ description="Query string to search for in a conversation title",
16
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/deployment.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Deployments
3
+ """
4
+ from typing import Annotated, Optional
5
+
6
+ from fastapi import Path, Query
7
+
8
+ DeploymentIdPathParam = Annotated[str, Path(
9
+ title="Deployment ID",
10
+ description="Deployment ID for deployment in question",
11
+ )]
12
+
13
+ AllQueryParam = Annotated[Optional[bool], Query(
14
+ title="All",
15
+ description="Include all deployments, regardless of availability.",
16
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/file.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Files
3
+ """
4
+ from typing import Annotated
5
+
6
+ from fastapi import Path
7
+
8
+ FileIdPathParam = Annotated[str, Path(
9
+ title="File ID",
10
+ description="File ID for file in question",
11
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/message.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Messages
3
+ """
4
+ from typing import Annotated
5
+
6
+ from fastapi import Path
7
+
8
+ MessageIdPathParam = Annotated[str, Path(
9
+ title="Message ID",
10
+ description="Message ID for message in question",
11
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/model.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Models
3
+ """
4
+ from typing import Annotated, Optional
5
+
6
+ from fastapi import Path, Query
7
+
8
+ ModelIdPathParam = Annotated[str, Path(
9
+ title="Model ID",
10
+ description="Model ID for the model in question",
11
+ )]
12
+
13
+ ModelQueryParam = Annotated[Optional[str], Query(
14
+ title="Model",
15
+ description="Model to filter results by",
16
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/organization.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Organizations
3
+ """
4
+ from typing import Annotated, Optional
5
+
6
+ from fastapi import Path, Query
7
+
8
+ OrganizationIdPathParam = Annotated[str, Path(
9
+ title="Organization ID",
10
+ description="Organization ID for the organization in question",
11
+ )]
12
+
13
+ OrganizationIdQueryParam = Annotated[Optional[str], Query(
14
+ title="Organization ID",
15
+ description="Organization ID to filter results by",
16
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/scim.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for SCIM
3
+ """
4
+ from typing import Annotated, Optional
5
+
6
+ from fastapi import Depends, Path, Query
7
+
8
+
9
+ class _ScimPaginationParams:
10
+ """
11
+ Common pagination query parameters
12
+ """
13
+ def __init__(
14
+ self,
15
+ start_index: Annotated[int, Query(
16
+ title="Start Index",
17
+ description="Start Index for request",
18
+ )] = 1,
19
+ count: Annotated[int, Query(
20
+ title="Count",
21
+ description="Maximum number of records to return per request",
22
+ )] = 100,
23
+ filter: Annotated[Optional[str], Query(
24
+ title="Filter",
25
+ description="Filter to use when filtering response",
26
+ )] = None
27
+ ) -> None:
28
+ self.start_index = start_index
29
+ self.count = count
30
+ self.filter = filter
31
+
32
+ ScimPaginationQueryParams = Annotated[_ScimPaginationParams, Depends()]
33
+
34
+
35
+ UserIdPathParam = Annotated[str, Path(
36
+ title="User ID",
37
+ description="User ID for the user in question",
38
+ )]
39
+
40
+
41
+ GroupIdPathParam = Annotated[str, Path(
42
+ title="Group ID",
43
+ description="Group ID for the group in question",
44
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/shared.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Shared Query and Path Parameters for Routers
3
+ """
4
+ from typing import Annotated, Optional
5
+
6
+ from fastapi import Depends, Query
7
+
8
+
9
+ class _PaginationParams:
10
+ """
11
+ Common pagination query parameters
12
+ """
13
+ def __init__(
14
+ self,
15
+ offset: Annotated[int, Query(
16
+ title="Pagination Offset",
17
+ description="Offset for where request should start returning records from",
18
+ )] = 0,
19
+ limit: Annotated[int, Query(
20
+ title="Pagination Limit",
21
+ description="Maximum number of records to return per request",
22
+ )] = 100,
23
+ ) -> None:
24
+ self.offset = offset
25
+ self.limit = limit
26
+
27
+ PaginationQueryParams = Annotated[_PaginationParams, Depends()]
28
+
29
+
30
+ OrderByQueryParam = Annotated[Optional[str], Query(
31
+ title="Orber By",
32
+ description="Field to sorts results by",
33
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/snapshot.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Snapshots
3
+ """
4
+ from typing import Annotated
5
+
6
+ from fastapi import Path
7
+
8
+ LinkIdPathParam = Annotated[str, Path(
9
+ title="Link ID",
10
+ description="Link ID for the snapshot link in question",
11
+ )]
12
+
13
+ SnapshotIdPathParam = Annotated[str, Path(
14
+ title="Snapshot ID",
15
+ description="Snapshot ID for the snapshot in question",
16
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/tool.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Auth
3
+ """
4
+ from typing import Annotated
5
+
6
+ from fastapi import Path
7
+
8
+ ToolIdPathParam = Annotated[str, Path(
9
+ title="Tool ID",
10
+ description="Tool ID for tool in question",
11
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/params/user.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Query and Path Parameters for Users
3
+ """
4
+ from typing import Annotated
5
+
6
+ from fastapi import Path
7
+
8
+ UserIdPathParam = Annotated[str, Path(
9
+ title="User ID",
10
+ description="User ID for the user in question",
11
+ )]
western_ai/canada/cohere-toolkit/src/backend/schemas/scim.py ADDED
@@ -0,0 +1,320 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from abc import ABC
2
+ from typing import ClassVar, Optional
3
+
4
+ from pydantic import BaseModel, ConfigDict, Field
5
+ from pydantic.alias_generators import to_camel
6
+
7
+ from backend.database_models import Group as DBGroup
8
+ from backend.database_models import User as DBUser
9
+
10
+
11
+ class BaseSchema(ABC, BaseModel):
12
+ """
13
+ Abstract class for other schemas
14
+ """
15
+ model_config = ConfigDict(
16
+ alias_generator=to_camel,
17
+ populate_by_name=True,
18
+ from_attributes=True,
19
+ )
20
+
21
+
22
+ class Meta(BaseSchema):
23
+ """
24
+ Schema for metadata
25
+ """
26
+ resource_type: str = Field(
27
+ ...,
28
+ title="Resource Type",
29
+ description="Type of resource the metadata is for",
30
+ )
31
+ created: str = Field(
32
+ ...,
33
+ title="Created",
34
+ description="When metadata was created",
35
+ )
36
+ last_modified: str = Field(
37
+ ...,
38
+ title="Last Modified",
39
+ description="When metadata was last modified",
40
+ )
41
+
42
+
43
+ class Name(BaseSchema):
44
+ given_name: str = Field(
45
+ ...,
46
+ title="Given Name",
47
+ description="User's given name",
48
+ )
49
+ family_name: str = Field(
50
+ ...,
51
+ title="Family Name",
52
+ description="User's family name",
53
+ )
54
+
55
+
56
+ class BaseUser(BaseSchema):
57
+ user_name: Optional[str] = Field(
58
+ None,
59
+ title="User Name",
60
+ description="User name",
61
+ )
62
+ active: Optional[bool] = Field(
63
+ None,
64
+ title="Active",
65
+ description="Is user active",
66
+ )
67
+
68
+ schemas: list[str] = Field(
69
+ ...,
70
+ title="Schemas",
71
+ description="Schemas for the user",
72
+ )
73
+
74
+
75
+ class GroupMember(BaseSchema):
76
+ value: str = Field(
77
+ ...,
78
+ title="Value",
79
+ description="Value",
80
+ )
81
+ display: str = Field(
82
+ ...,
83
+ title="Display",
84
+ description="Display",
85
+ )
86
+
87
+
88
+ class BaseGroup(BaseSchema):
89
+ schemas: list[str] = Field(
90
+ ...,
91
+ title="Schemas",
92
+ description="Schemas for the group",
93
+ )
94
+ members: list[GroupMember] = Field(
95
+ ...,
96
+ title="Members",
97
+ description="Members of the group",
98
+ )
99
+ display_name: str = Field(
100
+ ...,
101
+ title="Display Name",
102
+ description="Display name for the group",
103
+ )
104
+
105
+
106
+ class CreateGroup(BaseGroup):
107
+ pass
108
+
109
+
110
+ class Email(BaseSchema):
111
+ primary: bool = Field(
112
+ ...,
113
+ title="Primary",
114
+ description="Is email the primary email",
115
+ )
116
+ value: Optional[str] = Field(
117
+ None,
118
+ title="Value",
119
+ description="Email value",
120
+ )
121
+ type: str = Field(
122
+ ...,
123
+ title="Type",
124
+ description="Type of email",
125
+ )
126
+
127
+
128
+ class CreateUser(BaseUser):
129
+ name: Name = Field(
130
+ ...,
131
+ title="Name",
132
+ description="Name of user",
133
+ )
134
+ emails: list[Email] = Field(
135
+ ...,
136
+ title="Emails",
137
+ description="List of emails for user",
138
+ )
139
+ externalId: str = Field(
140
+ ...,
141
+ title="External ID",
142
+ description="External ID for the user",
143
+ )
144
+
145
+
146
+ class UpdateUser(BaseUser):
147
+ name: Name = Field(
148
+ ...,
149
+ title="Name",
150
+ description="Name of user",
151
+ )
152
+ emails: list[Email] = Field(
153
+ ...,
154
+ title="Emails",
155
+ description="List of emails for user",
156
+ )
157
+
158
+
159
+ class Operation(BaseSchema):
160
+ op: str = Field(
161
+ ...,
162
+ title="Op",
163
+ description="Op",
164
+ )
165
+ value: dict[str, bool] = Field(
166
+ ...,
167
+ title="Value",
168
+ description="Value",
169
+ )
170
+
171
+
172
+ class GroupOperation(BaseSchema):
173
+ op: str = Field(
174
+ ...,
175
+ title="Op",
176
+ description="Op",
177
+ )
178
+ path: Optional[str] = Field(
179
+ None,
180
+ title="Path",
181
+ description="Path",
182
+ )
183
+ value: dict[str, str]|list[dict[str, str]] = Field(
184
+ ...,
185
+ title="Value",
186
+ description="Value",
187
+ )
188
+
189
+
190
+ class PatchUser(BaseSchema):
191
+ schemas: list[str] = Field(
192
+ ...,
193
+ title="Schemas",
194
+ description="Schemas for user",
195
+ )
196
+ Operations: list[Operation] = Field(
197
+ ...,
198
+ title="Operations",
199
+ description="Operations for the user",
200
+ )
201
+
202
+
203
+ class PatchGroup(BaseSchema):
204
+ schemas: list[str] = Field(
205
+ ...,
206
+ title="Schemas",
207
+ description="Schemas for group",
208
+ )
209
+ Operations: list[GroupOperation] = Field(
210
+ ...,
211
+ title="Operations",
212
+ description="Operations for the group",
213
+ )
214
+
215
+
216
+ class Group(BaseGroup):
217
+ id: str = Field(
218
+ ...,
219
+ title="ID",
220
+ description="Unique identifier for the group",
221
+ )
222
+ display_name: str = Field(
223
+ ...,
224
+ title="Display Name",
225
+ description="Display name for the group",
226
+ )
227
+ meta: Meta = Field(
228
+ ...,
229
+ title="Meta",
230
+ description="Metadata for the group",
231
+ )
232
+
233
+ @staticmethod
234
+ def from_db_group(db_group: DBGroup) -> "Group":
235
+ return Group(
236
+ id=db_group.id,
237
+ display_name=db_group.display_name,
238
+ members=[
239
+ GroupMember(value=ua.user_id, display=ua.display)
240
+ for ua in db_group.user_associations
241
+ ],
242
+ meta=Meta(
243
+ resourceType="Group",
244
+ created=db_group.created_at.isoformat(),
245
+ lastModified=db_group.updated_at.isoformat(),
246
+ ),
247
+ schemas=["urn:ietf:params:scim:schemas:core:2.0:Group"],
248
+ )
249
+
250
+
251
+ class User(BaseUser):
252
+ id: str = Field(
253
+ ...,
254
+ title="ID",
255
+ description="Unique identifier for the user",
256
+ )
257
+ external_id: str = Field(
258
+ ...,
259
+ title="External ID",
260
+ description="External ID for the user",
261
+ )
262
+ meta: Meta = Field(
263
+ ...,
264
+ title="Meta",
265
+ description="Metadata for the user",
266
+ )
267
+
268
+ @staticmethod
269
+ def from_db_user(db_user: DBUser) -> "User":
270
+ return User(
271
+ id=db_user.id,
272
+ user_name=db_user.user_name,
273
+ active=db_user.active,
274
+ external_id=db_user.external_id,
275
+ meta=Meta(
276
+ resourceType="User",
277
+ created=db_user.created_at.isoformat(),
278
+ lastModified=db_user.updated_at.isoformat(),
279
+ ),
280
+ schemas=["urn:ietf:params:scim:schemas:core:2.0:User"],
281
+ )
282
+
283
+
284
+ class BaseListResponse(BaseSchema):
285
+ schemas: ClassVar[list[str]] = [
286
+ "urn:ietf:params:scim:api:messages:2.0:ListResponse"
287
+ ]
288
+ total_results: int = Field(
289
+ ...,
290
+ title="Total Results",
291
+ description="Total results available",
292
+ )
293
+ start_index: int = Field(
294
+ ...,
295
+ title="Start Index",
296
+ description="Start index for returned results",
297
+ )
298
+ items_per_page: int = Field(
299
+ ...,
300
+ title="Items Per Page",
301
+ description="Total results returned in the request",
302
+ )
303
+
304
+
305
+ class ListUserResponse(BaseListResponse):
306
+ resources: list[User] = Field(
307
+ ...,
308
+ title="Resources",
309
+ description="List of Users",
310
+ alias="Resources",
311
+ )
312
+
313
+
314
+ class ListGroupResponse(BaseListResponse):
315
+ resources: list[Group] = Field(
316
+ ...,
317
+ title="Resources",
318
+ description="List of Groups",
319
+ alias="Resources",
320
+ )
western_ai/canada/cohere-toolkit/src/backend/schemas/search_query.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel, Field
2
+
3
+
4
+ class SearchQuery(BaseModel):
5
+ """
6
+ Schema for search query
7
+ """
8
+ text: str = Field(
9
+ ...,
10
+ title="Text",
11
+ description="Text for the search",
12
+ )
13
+ generation_id: str = Field(
14
+ ...,
15
+ title="Generation ID",
16
+ description="Unique identifier for the generation",
17
+ )
18
+
19
+ class Config:
20
+ from_attributes = True
western_ai/canada/cohere-toolkit/src/backend/schemas/snapshot.py ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ from typing import Optional
3
+
4
+ from pydantic import BaseModel, Field
5
+
6
+ from backend.schemas.agent import AgentToolMetadata
7
+ from backend.schemas.message import Message
8
+
9
+
10
+ class SnapshotAgent(BaseModel):
11
+ """
12
+ Agent Snapshot
13
+ """
14
+ id: str = Field(
15
+ ...,
16
+ title="ID",
17
+ description="Unique identifier for the agent snapshot",
18
+ )
19
+ name: str = Field(
20
+ ...,
21
+ title="Name",
22
+ description="Name of the snapshot",
23
+ )
24
+ description: Optional[str] = Field(
25
+ None,
26
+ title="Description",
27
+ description="Description of the snapshot",
28
+ )
29
+ preamble: Optional[str] = Field(
30
+ None,
31
+ title="Preamble",
32
+ description="Peamble for the snapshot",
33
+ )
34
+ tools_metadata: Optional[list[AgentToolMetadata]] = Field(
35
+ None,
36
+ title="Tools Metadata",
37
+ description="Tools metadata for the snapshot",
38
+ )
39
+
40
+ class Config:
41
+ from_attributes = True
42
+
43
+
44
+ class SnapshotData(BaseModel):
45
+ """
46
+ Snapshot data
47
+ """
48
+ title: str = Field(
49
+ ...,
50
+ title="Title",
51
+ description="Title of the snapshot",
52
+ )
53
+ description: str = Field(
54
+ ...,
55
+ title="Description",
56
+ description="Description of the snapshot",
57
+ )
58
+ messages: list[Message] = Field(
59
+ ...,
60
+ title="Messages",
61
+ description="List of messages",
62
+ )
63
+ agent: Optional[SnapshotAgent] = Field(
64
+ None,
65
+ title="Agent",
66
+ description="Agent for the snapshot",
67
+ exclude=True,
68
+ )
69
+
70
+ class Config:
71
+ from_attributes = True
72
+
73
+
74
+ class Snapshot(BaseModel):
75
+ """
76
+ Snapshot
77
+ """
78
+ conversation_id: str = Field(
79
+ ...,
80
+ title="Conversation ID",
81
+ description="Unique identifier for the conversation",
82
+ )
83
+ id: str = Field(
84
+ ...,
85
+ title="ID",
86
+ description="Unique identifier for the snapshot",
87
+ )
88
+ last_message_id: str = Field(
89
+ ...,
90
+ title="Last Message ID",
91
+ description="Unique identifier for the last message",
92
+ )
93
+ user_id: str = Field(
94
+ ...,
95
+ title="User ID",
96
+ description="Unique identifier for the user",
97
+ )
98
+ organization_id: Optional[str] = Field(
99
+ None,
100
+ title="Organization ID",
101
+ description="Unique identifier for the organization",
102
+ )
103
+
104
+ version: int = Field(
105
+ ...,
106
+ title="Version",
107
+ description="Snapshot version",
108
+ )
109
+ created_at: datetime.datetime = Field(
110
+ ...,
111
+ title="Created At Timestamp",
112
+ description="When snapshot was creted",
113
+ )
114
+ updated_at: datetime.datetime = Field(
115
+ ...,
116
+ title="Updated At Timestamp",
117
+ description="When snapshot was updated",
118
+ )
119
+ snapshot: SnapshotData = Field(
120
+ ...,
121
+ title="Snapshot Data",
122
+ description="Data for the snapshot",
123
+ )
124
+ agent_id: Optional[str] = Field(
125
+ None,
126
+ title="Agent ID",
127
+ description="Unique identifier for the agent",
128
+ exclude=True,
129
+ )
130
+
131
+ class Config:
132
+ from_attributes = True
133
+
134
+
135
+ class SnapshotPublic(Snapshot):
136
+ """
137
+ Public snapshot
138
+ """
139
+ user_id: Optional[str] = Field(
140
+ None,
141
+ title="User ID",
142
+ description="Unique identifier for the user",
143
+ exclude=True,
144
+ )
145
+ organization_id: Optional[str] = Field(
146
+ None,
147
+ title="Organization ID",
148
+ description="Unique identifier for the organization",
149
+ exclude=True,
150
+ )
151
+
152
+ class Config:
153
+ from_attributes = True
154
+
155
+
156
+ class SnapshotLink(BaseModel):
157
+ """
158
+ Snapshot link
159
+ """
160
+ snapshot_id: str = Field(
161
+ ...,
162
+ title="Snapshot ID",
163
+ description="Unique identifier for the snapshot",
164
+ )
165
+ user_id: str = Field(
166
+ ...,
167
+ title="User ID",
168
+ description="Unique identifier for the user",
169
+ )
170
+
171
+ class Config:
172
+ from_attributes = True
173
+
174
+
175
+ class SnapshotLinkPublic(SnapshotLink):
176
+ """
177
+ Public snapshot link
178
+ """
179
+ user_id: Optional[str] = Field(
180
+ None,
181
+ title="User ID",
182
+ description="Unique identifier for the user",
183
+ exclude=True,
184
+ )
185
+
186
+ class Config:
187
+ from_attributes = True
188
+
189
+
190
+ class SnapshotAccess(BaseModel):
191
+ """
192
+ Snapshot access
193
+ """
194
+ user_id: str = Field(
195
+ ...,
196
+ title="User ID",
197
+ description="Unique identifier for the user",
198
+ )
199
+ snapshot_id: str = Field(
200
+ ...,
201
+ title="Snapshot ID",
202
+ description="Unique identifier for the snapshot",
203
+ )
204
+ link_id: str = Field(
205
+ ...,
206
+ title="Link ID",
207
+ description="Unique identifier for the link",
208
+ )
209
+
210
+ class Config:
211
+ from_attributes = True
212
+
213
+
214
+ class SnapshotWithLinks(SnapshotPublic):
215
+ """
216
+ Snapshot with links
217
+ """
218
+ links: list[str] = Field(
219
+ ...,
220
+ title="Links",
221
+ description="List of links",
222
+ )
223
+
224
+ class Config:
225
+ from_attributes = True
226
+
227
+
228
+ class CreateSnapshotRequest(BaseModel):
229
+ """
230
+ Request to create a snapshot
231
+ """
232
+ conversation_id: str = Field(
233
+ ...,
234
+ title="Conversation ID",
235
+ description="Unique identifier for the conversation",
236
+ )
237
+
238
+ class Config:
239
+ from_attributes = True
240
+
241
+
242
+ class CreateSnapshotResponse(SnapshotLinkPublic):
243
+ """
244
+ Response for creating a snapshot
245
+ """
246
+ link_id: str = Field(
247
+ ...,
248
+ title="Link ID",
249
+ description="Unique identifier for the link",
250
+ )
251
+ messages: list[Message] = Field(
252
+ ...,
253
+ title="Messages",
254
+ description="List of messages",
255
+ )
256
+
257
+ class Config:
258
+ from_attributes = True
259
+
260
+
261
+ class DeleteSnapshotLinkResponse(BaseModel):
262
+ """
263
+ Response for deleting a snapshot link
264
+ """
265
+ pass
266
+
267
+
268
+ class DeleteSnapshotResponse(BaseModel):
269
+ """
270
+ Response for deleting a snapshot
271
+ """
272
+ pass
western_ai/canada/cohere-toolkit/src/backend/schemas/tool.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from enum import StrEnum
2
+ from typing import Any, Optional
3
+
4
+ from pydantic import BaseModel, Field
5
+
6
+
7
+ class ToolCategory(StrEnum):
8
+ """
9
+ Supported Tool Categories
10
+ """
11
+ DataLoader = "Data loader"
12
+ FileLoader = "File loader"
13
+ Function = "Function"
14
+ WebSearch = "Web search"
15
+
16
+
17
+ class Tool(BaseModel):
18
+ """
19
+ Tool Schema
20
+ """
21
+ name: Optional[str] = Field(
22
+ "",
23
+ title="Name",
24
+ description="Name of the Tool",
25
+ )
26
+ parameter_definitions: Optional[dict] = Field(
27
+ {},
28
+ title="Parameter Definitions",
29
+ description="Parameters definitions for the tool",
30
+ )
31
+
32
+ class ToolDefinition(Tool):
33
+ """
34
+ Tool Definition Schema
35
+ """
36
+ display_name: str = Field(
37
+ "",
38
+ title="Display Name",
39
+ description="Display name for the tool",
40
+ )
41
+ description: str = Field(
42
+ "",
43
+ title="Description",
44
+ description="Description of the tool",
45
+ )
46
+ error_message: Optional[str] = Field(
47
+ "",
48
+ title="Error Message",
49
+ description="Error message",
50
+ )
51
+ kwargs: dict = Field(
52
+ {},
53
+ title="kwargs",
54
+ description="kwags for the tool",
55
+ )
56
+ is_visible: bool = Field(
57
+ False,
58
+ title="Is Visible",
59
+ description="Is the tool visible",
60
+ )
61
+ is_available: bool = Field(
62
+ False,
63
+ title="Is Available",
64
+ description="Is the tool available",
65
+ )
66
+ category: ToolCategory = Field(
67
+ ToolCategory.DataLoader,
68
+ title="Category",
69
+ description="Tool category",
70
+ )
71
+
72
+ is_auth_required: bool = Field(
73
+ False,
74
+ title="Is Auth Required",
75
+ description="Is auth required for the tool",
76
+ ) # Per user
77
+ auth_url: Optional[str] = Field(
78
+ "",
79
+ title="Auth Url",
80
+ description="Auth url for the tool",
81
+ ) # Per user
82
+ token: Optional[str] = Field(
83
+ "",
84
+ title="Token",
85
+ description="Token for the tool",
86
+ ) # Per user
87
+ should_return_token: bool = Field(
88
+ False,
89
+ title="Should Return Token",
90
+ description="If the tool returns a token",
91
+ )
92
+
93
+ implementation: Any = Field(
94
+ ...,
95
+ title="Implementation",
96
+ description="Implementation for the tool",
97
+ exclude=True,
98
+ )
99
+ auth_implementation: Optional[Any] = Field(
100
+ None,
101
+ title="Auth Implementation",
102
+ description="Auth implementation for the tool",
103
+ exclude=True,
104
+ )
105
+
106
+ class Config:
107
+ from_attributes = True
108
+
109
+
110
+ class ToolCall(BaseModel):
111
+ """
112
+ Schema for Tool Call
113
+ """
114
+ name: str = Field(
115
+ ...,
116
+ title="Name",
117
+ description="Name of the Tool",
118
+ )
119
+ parameters: dict = Field(
120
+ {},
121
+ title="Parameters",
122
+ description="Parameters for the tool call",
123
+ )
124
+
125
+ class Config:
126
+ from_attributes = True
127
+
128
+
129
+ class ToolCallDelta(BaseModel):
130
+ """
131
+ Schema for Tool Call Delta
132
+ """
133
+ name: Optional[str] = Field(
134
+ None,
135
+ title="Name",
136
+ description="Name of the Tool",
137
+ )
138
+ index: Optional[int] = Field(
139
+ None,
140
+ title="Index",
141
+ description="Index",
142
+ )
143
+ parameters: Optional[str] = Field(
144
+ None,
145
+ title="Parameters",
146
+ description="Parameters for the tool call",
147
+ )
148
+
149
+ class Config:
150
+ from_attributes = True
western_ai/canada/cohere-toolkit/src/backend/schemas/tool_auth.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ from typing import Optional
3
+
4
+ from pydantic import BaseModel, Field
5
+
6
+
7
+ class UpdateToolAuth(BaseModel):
8
+ """
9
+ Schema for Tool Authentication
10
+ """
11
+ user_id: Optional[str] = Field(
12
+ None,
13
+ title="User ID",
14
+ description="Unique identifier for the user",
15
+ )
16
+ tool_id: Optional[str] = Field(
17
+ None,
18
+ title="Tool ID",
19
+ description="Unique identifier for the tool",
20
+ )
21
+ token_type: Optional[str] = Field(
22
+ None,
23
+ title="Token Type",
24
+ description="Type of token",
25
+ )
26
+ encrypted_access_token: Optional[bytes] = Field(
27
+ None,
28
+ title="Encrypted Access Token",
29
+ description="Token for authentication",
30
+ )
31
+ encrypted_refresh_token: Optional[bytes] = Field(
32
+ None,
33
+ title="Encrypted Refresh Token",
34
+ description="The token that can be used to refresh the access token",
35
+ )
36
+ expires_at: Optional[datetime.datetime] = Field(
37
+ None,
38
+ title="Expires At",
39
+ description="When the access token expires",
40
+ )
41
+
42
+ class Config:
43
+ from_attributes = True
44
+ use_enum_values = True
45
+
46
+
47
+ class DeleteToolAuth(BaseModel):
48
+ """
49
+ Response when deleting a tool auth
50
+ """
51
+ pass
western_ai/canada/cohere-toolkit/src/backend/schemas/user.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ from abc import ABC
3
+ from typing import Optional
4
+
5
+ from pydantic import BaseModel, Field
6
+
7
+ from backend.services.auth import BasicAuthentication
8
+
9
+ DEFAULT_USER_ID = "user-id"
10
+ DEFAULT_USER_NAME = "Default User"
11
+
12
+
13
+ class UserBase(ABC, BaseModel):
14
+ """
15
+ Abstract class for User Schemas
16
+ """
17
+ fullname: str = Field(
18
+ ...,
19
+ title="Full Name",
20
+ description="User's Full Name",
21
+ )
22
+ email: Optional[str] = Field(
23
+ None,
24
+ title="Email",
25
+ description="User's email address",
26
+ )
27
+
28
+
29
+ class User(UserBase):
30
+ """
31
+ User schema
32
+ """
33
+ id: str = Field(
34
+ ...,
35
+ title="ID",
36
+ description="",
37
+ )
38
+ created_at: datetime.datetime = Field(
39
+ ...,
40
+ title="Created At Timestamp",
41
+ description="When the user was created",
42
+ )
43
+ updated_at: datetime.datetime = Field(
44
+ ...,
45
+ title="Updated At Timestamp",
46
+ description="When the user was updated",
47
+ )
48
+
49
+ class Config:
50
+ from_attributes = True
51
+
52
+
53
+ class UserPassword(ABC, BaseModel):
54
+ """
55
+ Adstract class for user schemas
56
+ """
57
+ password: Optional[str] = Field(
58
+ None,
59
+ title="Password",
60
+ description="Password for the user",
61
+ )
62
+ hashed_password: Optional[bytes] = Field(
63
+ None,
64
+ title="Hashed Password",
65
+ description="The user's password hashed",
66
+ )
67
+
68
+ def __init__(self, **data):
69
+ password = data.pop("password", None)
70
+
71
+ if password is not None:
72
+ data["hashed_password"] = BasicAuthentication.hash_and_salt_password(
73
+ password
74
+ )
75
+
76
+ super().__init__(**data)
77
+
78
+
79
+ class CreateUser(UserBase, UserPassword):
80
+ """
81
+ Request to create a user
82
+ """
83
+ pass
84
+
85
+
86
+ class UpdateUser(UserPassword):
87
+ """
88
+ Request to update a user
89
+ """
90
+ fullname: Optional[str] = Field(
91
+ None,
92
+ title="Full Name",
93
+ description="User's Full Name",
94
+ )
95
+ email: Optional[str] = Field(
96
+ None,
97
+ title="Email",
98
+ description="User's email address",
99
+ )
100
+
101
+
102
+ class DeleteUser(BaseModel):
103
+ """
104
+ Response when deleting a user
105
+ """
106
+ pass