Skip to content

Commit 93407ba

Browse files
committed
refac
1 parent 78dbad5 commit 93407ba

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

backend/open_webui/models/functions.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import time
33
from typing import Optional
44

5-
from sqlalchemy.orm import Session
5+
from sqlalchemy.orm import Session, defer
66
from open_webui.internal.db import Base, JSONField, get_db, get_db_context
7-
from open_webui.models.users import Users, UserModel
7+
from open_webui.models.users import Users, UserModel, UserResponse
88
from pydantic import BaseModel, ConfigDict
99
from sqlalchemy import BigInteger, Boolean, Column, String, Text, Index
1010

@@ -75,10 +75,6 @@ class FunctionWithValvesModel(BaseModel):
7575
####################
7676

7777

78-
class FunctionUserResponse(FunctionModel):
79-
user: Optional[UserModel] = None
80-
81-
8278
class FunctionResponse(BaseModel):
8379
id: str
8480
user_id: str
@@ -91,6 +87,10 @@ class FunctionResponse(BaseModel):
9187
created_at: int # timestamp in epoch
9288

9389

90+
class FunctionUserResponse(FunctionResponse):
91+
user: Optional[UserResponse] = None
92+
93+
9494
class FunctionForm(BaseModel):
9595
id: str
9696
name: str
@@ -224,7 +224,12 @@ def get_functions(
224224

225225
def get_function_list(self, db: Optional[Session] = None) -> list[FunctionUserResponse]:
226226
with get_db_context(db) as db:
227-
functions = db.query(Function).order_by(Function.updated_at.desc()).all()
227+
functions = (
228+
db.query(Function)
229+
.options(defer(Function.content))
230+
.order_by(Function.updated_at.desc())
231+
.all()
232+
)
228233
user_ids = list(set(func.user_id for func in functions))
229234

230235
users = Users.get_users_by_user_ids(user_ids, db=db) if user_ids else []
@@ -233,8 +238,17 @@ def get_function_list(self, db: Optional[Session] = None) -> list[FunctionUserRe
233238
return [
234239
FunctionUserResponse.model_validate(
235240
{
236-
**FunctionModel.model_validate(func).model_dump(),
237-
'user': (users_dict.get(func.user_id).model_dump() if func.user_id in users_dict else None),
241+
**FunctionResponse.model_validate(func).model_dump(),
242+
'user': (
243+
UserResponse(
244+
id=users_dict[func.user_id].id,
245+
name=users_dict[func.user_id].name,
246+
role=users_dict[func.user_id].role,
247+
email=users_dict[func.user_id].email,
248+
).model_dump()
249+
if func.user_id in users_dict
250+
else None
251+
),
238252
}
239253
)
240254
for func in functions

0 commit comments

Comments
 (0)