Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: solve some issues #248

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"lottie-react": "^2.4.0",
"next": "14.0.1",
"openai": "^4.24.7",
"petercat-lui": "^0.1.19",
"petercat-lui": "^0.1.20",
"postcss": "^8.4.41",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
8 changes: 4 additions & 4 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8504,10 +8504,10 @@ path-type@^4.0.0:
resolved "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==

petercat-lui@^0.1.19:
version "0.1.19"
resolved "https://registry.npmjs.org/petercat-lui/-/petercat-lui-0.1.19.tgz#4d910fb664107ea694fbdfeffdfc3060ba5172cc"
integrity sha512-8MMy1NarNcY/dwl2P5YbDNy1NdmYNRPOe0mJgkp0IQRnodV44KFNaLJ9HDyXxagRYylXPF/MGlGd00GYgX6bSg==
petercat-lui@^0.1.20:
version "0.1.20"
resolved "https://registry.npmjs.org/petercat-lui/-/petercat-lui-0.1.20.tgz#3b38a0f946ff78f189f4d443505f55bd60f3cd2f"
integrity sha512-vpq24RW5wWqgGIXplJ+3KbjfFLnCXFvO8f2cp5TytL4WAKwtl6KwUhz6f66ZSQmkw3NxGLxXTC7e194/83GP2A==
dependencies:
"@ant-design/icons" "^5.3.5"
"@ant-design/pro-chat" "^1.9.0"
Expand Down
2 changes: 1 addition & 1 deletion lui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "petercat-lui",
"version": "0.1.19",
"version": "0.1.20",
"description": "A react library developed with dumi",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion lui/src/Chat/components/ChatItemRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ChatItemRender: FC<IProps> = ({
}}
/>
)}
<div className="ant-pro-chat-list-item-message-container">
<div className="ant-pro-chat-list-item-message-container flex-1">
{title}
{content}
</div>
Expand Down
175 changes: 127 additions & 48 deletions server/routers/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,105 +13,184 @@
responses={404: {"description": "Not found"}},
)


@router.get("/list")
def get_bot_list(personal: Optional[str] = Query(None, description="Filter bots by personal category"), name: Optional[str] = Query(None, description="Filter bots by name"), user_id: Annotated[str | None, Depends(get_user_id)] = None):
def get_bot_list(
personal: Optional[str] = Query(
None, description="Filter bots by personal category"
),
name: Optional[str] = Query(None, description="Filter bots by name"),
user_id: Annotated[str | None, Depends(get_user_id)] = None,
):
try:
supabase = get_client()
query = supabase.table("bots").select("id, created_at, updated_at, avatar, description, name, public, starters, uid")
if personal == 'true':
query = supabase.table("bots").select(
"id, created_at, updated_at, avatar, description, name, public, starters, uid"
)
if personal == "true":
if not user_id:
return {"data": [], "personal": personal}
query = query.eq('uid', user_id)
query = query.eq("uid", user_id).order("updated_at", desc=True)
if name:
query = supabase.table("bots").select("id, created_at, updated_at, avatar, description, name, public, starters, uid").filter("name", "like", f"%{name}%")

query = query.eq('public', True) if not personal or personal != 'true' else query

query = (
supabase.table("bots")
.select(
"id, created_at, updated_at, avatar, description, name, public, starters, uid"
)
.filter("name", "like", f"%{name}%")
)

query = (
query.eq("public", True).order("updated_at", desc=True)
if not personal or personal != "true"
else query
)

data = query.execute()
if not data or not data.data:
return {"data": [], "personal": personal}
return {"data": data.data, "personal": personal}

except Exception as e:
return JSONResponse(content={"success": False, "errorMessage": str(e)}, status_code=500)
return JSONResponse(
content={"success": False, "errorMessage": str(e)}, status_code=500
)


@router.get("/detail")
def get_bot_detail(id: Optional[str] = Query(None, description="Filter bots by personal category")):
if not id:
return{
"error": "Incomplete parameters",
"status": 400
}
def get_bot_detail(
id: Optional[str] = Query(None, description="Filter bots by personal category")
):
if not id:
return {"error": "Incomplete parameters", "status": 400}
else:
try:
supabase = get_client()
data = supabase.table("bots").select('id, created_at, updated_at, avatar, description, name, starters, public, hello_message').eq('id', id).execute()
return { "data": data.data, "status": 200}
data = (
supabase.table("bots")
.select(
"id, created_at, updated_at, avatar, description, name, starters, public, hello_message"
)
.eq("id", id)
.execute()
)
return {"data": data.data, "status": 200}
except Exception as e:
return JSONResponse(content={"success": False, "errorMessage": str(e)}, status_code=500)
return JSONResponse(
content={"success": False, "errorMessage": str(e)}, status_code=500
)


@router.get("/config")
def get_bot_config(id: Optional[str] = Query(None, description="Filter bots by personal category"), user_id: Annotated[str | None, Depends(get_user_id)] = None):
def get_bot_config(
id: Optional[str] = Query(None, description="Filter bots by personal category"),
user_id: Annotated[str | None, Depends(get_user_id)] = None,
):
try:
supabase = get_client()
data = supabase.table("bots").select('*').eq('id', id).eq('uid', user_id).execute()
return { "data": data.data, "status": 200}
data = (
supabase.table("bots").select("*").eq("id", id).eq("uid", user_id).execute()
)
return {"data": data.data, "status": 200}
except Exception as e:
return JSONResponse(content={"success": False, "errorMessage": e})


@router.post("/create", status_code=200)
async def create_bot(bot_data: BotCreateRequest, user_id: Annotated[str | None, Depends(get_user_id)] = None):
async def create_bot(
bot_data: BotCreateRequest,
user_id: Annotated[str | None, Depends(get_user_id)] = None,
):
try:
res = await bot_builder(user_id, **bot_data.model_dump())
if not res:
return JSONResponse(content={"success": False, "errorMessage": "仓库不存在,生成失败"}, status_code=500)
return JSONResponse(
content={"success": False, "errorMessage": "仓库不存在,生成失败"},
status_code=500,
)
return res
except Exception as e:
return JSONResponse(content={"success": False, "errorMessage": str(e)}, status_code=500)
return JSONResponse(
content={"success": False, "errorMessage": str(e)}, status_code=500
)


@router.post("/config/generator", status_code=200)
async def bot_generator(bot_data: BotCreateRequest, user_id: Annotated[str | None, Depends(get_user_id)] = None):
async def bot_generator(
bot_data: BotCreateRequest,
user_id: Annotated[str | None, Depends(get_user_id)] = None,
):
try:
res = await bot_info_generator(user_id, **bot_data.model_dump())
res = await bot_info_generator(user_id, **bot_data.model_dump())
if not res:
return JSONResponse(content={"success": False, "errorMessage": "仓库不存在,生成失败"})
return JSONResponse(
content={"success": False, "errorMessage": "仓库不存在,生成失败"}
)
return JSONResponse(content={"success": True, "data": res})
except Exception as e:
return JSONResponse(content={"success": False, "errorMessage": str(e)}, status_code=500)
return JSONResponse(
content={"success": False, "errorMessage": str(e)}, status_code=500
)


@router.put("/update/{id}", status_code=200)
def update_bot(id: str, bot_data: BotUpdateRequest, user_id: Annotated[str | None, Depends(get_user_id)] = None):
def update_bot(
id: str,
bot_data: BotUpdateRequest,
user_id: Annotated[str | None, Depends(get_user_id)] = None,
):
if not id:
return {
"error": "Incomplete parameters",
"status": 400
}
return {"error": "Incomplete parameters", "status": 400}
try:
update_fields = {key: value for key, value in bot_data.model_dump(exclude_unset=True).items() if value is not None}

update_fields = {
key: value
for key, value in bot_data.model_dump(exclude_unset=True).items()
if value is not None
}

if not update_fields:
return JSONResponse(content={"success": False, "errorMessage": "No fields to update"}, status_code=400)

return JSONResponse(
content={"success": False, "errorMessage": "No fields to update"},
status_code=400,
)

supabase = get_client()
response = supabase.table("bots").update(update_fields).eq("id", id).eq("uid", user_id).execute()

response = (
supabase.table("bots")
.update(update_fields)
.eq("id", id)
.eq("uid", user_id)
.execute()
)

if not response.data:
return JSONResponse(content={"success": False, "errorMessage": "bot 不存在,更新失败"})

return JSONResponse(
content={"success": False, "errorMessage": "bot 不存在,更新失败"}
)

return JSONResponse(content={"success": True})
except Exception as e:
return JSONResponse(content={"success": False, "errorMessage": str(e)}, status_code=500)
return JSONResponse(
content={"success": False, "errorMessage": str(e)}, status_code=500
)


@router.delete("/delete/{id}", status_code=status.HTTP_200_OK)
async def delete_bot(
id: str = Path(..., description="The ID of the bot to delete"),
user_id: Annotated[str | None, Depends(get_user_id)] = None
id: str = Path(..., description="The ID of the bot to delete"),
user_id: Annotated[str | None, Depends(get_user_id)] = None,
):
try:
supabase = get_client()
response = supabase.table("bots").delete().eq("id", id).eq("uid", user_id).execute()
response = (
supabase.table("bots").delete().eq("id", id).eq("uid", user_id).execute()
)
if not response.data:
return JSONResponse(content={"success": False, "errorMessage": "bot 不存在,删除失败"})
return JSONResponse(
content={"success": False, "errorMessage": "bot 不存在,删除失败"}
)
return JSONResponse(content={"success": True})
except Exception as e:
return JSONResponse(content={"success": False, "errorMessage": str(e)}, status_code=500)
return JSONResponse(
content={"success": False, "errorMessage": str(e)}, status_code=500
)