mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-02 21:19:12 +00:00
Show validation errors in UX if agent creation, update fails
Previously we only showed unsafe prompt errors to user when creating/updating agent. Errors in name collision were not shown on the web app ux. This change ensures that such validation errors are bubbled up to the user in the UX. So they can resolve the agent create/update error on their end.
This commit is contained in:
@@ -5,6 +5,7 @@ from datetime import datetime, timedelta, timezone
|
|||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
from asgiref.sync import sync_to_async
|
from asgiref.sync import sync_to_async
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from fastapi import APIRouter, Request
|
from fastapi import APIRouter, Request
|
||||||
from fastapi.responses import Response
|
from fastapi.responses import Response
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -389,22 +390,29 @@ async def create_agent(
|
|||||||
else:
|
else:
|
||||||
agent_chat_model = None
|
agent_chat_model = None
|
||||||
|
|
||||||
agent = await AgentAdapters.aupdate_agent(
|
try:
|
||||||
user,
|
agent = await AgentAdapters.aupdate_agent(
|
||||||
body.name,
|
user,
|
||||||
body.persona,
|
body.name,
|
||||||
body.privacy_level,
|
body.persona,
|
||||||
body.icon,
|
body.privacy_level,
|
||||||
body.color,
|
body.icon,
|
||||||
agent_chat_model,
|
body.color,
|
||||||
body.files,
|
agent_chat_model,
|
||||||
body.input_tools,
|
body.files,
|
||||||
body.output_modes,
|
body.input_tools,
|
||||||
body.slug,
|
body.output_modes,
|
||||||
body.is_hidden,
|
body.slug,
|
||||||
)
|
body.is_hidden,
|
||||||
agent.chat_model = await AgentAdapters.aget_agent_chat_model(agent, user)
|
)
|
||||||
|
except ValidationError as e:
|
||||||
|
return Response(
|
||||||
|
content=json.dumps({"error": e.message}),
|
||||||
|
media_type="application/json",
|
||||||
|
status_code=400,
|
||||||
|
)
|
||||||
|
|
||||||
|
agent.chat_model = await AgentAdapters.aget_agent_chat_model(agent, user)
|
||||||
agents_packet = {
|
agents_packet = {
|
||||||
"slug": agent.slug,
|
"slug": agent.slug,
|
||||||
"name": agent.name,
|
"name": agent.name,
|
||||||
@@ -462,21 +470,28 @@ async def update_agent(
|
|||||||
else:
|
else:
|
||||||
agent_chat_model = None
|
agent_chat_model = None
|
||||||
|
|
||||||
agent = await AgentAdapters.aupdate_agent(
|
try:
|
||||||
user,
|
agent = await AgentAdapters.aupdate_agent(
|
||||||
body.name,
|
user,
|
||||||
body.persona,
|
body.name,
|
||||||
body.privacy_level,
|
body.persona,
|
||||||
body.icon,
|
body.privacy_level,
|
||||||
body.color,
|
body.icon,
|
||||||
agent_chat_model,
|
body.color,
|
||||||
body.files,
|
agent_chat_model,
|
||||||
body.input_tools,
|
body.files,
|
||||||
body.output_modes,
|
body.input_tools,
|
||||||
body.slug,
|
body.output_modes,
|
||||||
)
|
body.slug,
|
||||||
agent.chat_model = await AgentAdapters.aget_agent_chat_model(agent, user)
|
)
|
||||||
|
except ValidationError as e:
|
||||||
|
return Response(
|
||||||
|
content=json.dumps({"error": e.message}),
|
||||||
|
media_type="application/json",
|
||||||
|
status_code=400,
|
||||||
|
)
|
||||||
|
|
||||||
|
agent.chat_model = await AgentAdapters.aget_agent_chat_model(agent, user)
|
||||||
agents_packet = {
|
agents_packet = {
|
||||||
"slug": agent.slug,
|
"slug": agent.slug,
|
||||||
"name": agent.name,
|
"name": agent.name,
|
||||||
|
|||||||
Reference in New Issue
Block a user