Skip to content

Commit 05b8768

Browse files
committed
refac
1 parent 173d563 commit 05b8768

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

backend/open_webui/tools/builtin.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,69 @@ async def search_knowledge_files(
15321532
return json.dumps({"error": str(e)})
15331533

15341534

1535+
async def view_file(
1536+
file_id: str,
1537+
__request__: Request = None,
1538+
__user__: dict = None,
1539+
__model_knowledge__: Optional[list[dict]] = None,
1540+
) -> str:
1541+
"""
1542+
Get the full content of a file by its ID.
1543+
1544+
:param file_id: The ID of the file to retrieve
1545+
:return: JSON with the file's id, filename, and full text content
1546+
"""
1547+
if __request__ is None:
1548+
return json.dumps({"error": "Request context not available"})
1549+
1550+
if not __user__:
1551+
return json.dumps({"error": "User context not available"})
1552+
1553+
try:
1554+
from open_webui.models.files import Files
1555+
from open_webui.routers.files import has_access_to_file
1556+
1557+
user_id = __user__.get("id")
1558+
user_role = __user__.get("role", "user")
1559+
1560+
file = Files.get_file_by_id(file_id)
1561+
if not file:
1562+
return json.dumps({"error": "File not found"})
1563+
1564+
if (
1565+
file.user_id != user_id
1566+
and user_role != "admin"
1567+
and not any(
1568+
item.get("type") == "file" and item.get("id") == file_id
1569+
for item in (__model_knowledge__ or [])
1570+
)
1571+
and not has_access_to_file(
1572+
file_id=file_id,
1573+
access_type="read",
1574+
user=UserModel(**__user__),
1575+
)
1576+
):
1577+
return json.dumps({"error": "File not found"})
1578+
1579+
content = ""
1580+
if file.data:
1581+
content = file.data.get("content", "")
1582+
1583+
return json.dumps(
1584+
{
1585+
"id": file.id,
1586+
"filename": file.filename,
1587+
"content": content,
1588+
"updated_at": file.updated_at,
1589+
"created_at": file.created_at,
1590+
},
1591+
ensure_ascii=False,
1592+
)
1593+
except Exception as e:
1594+
log.exception(f"view_file error: {e}")
1595+
return json.dumps({"error": str(e)})
1596+
1597+
15351598
async def view_knowledge_file(
15361599
file_id: str,
15371600
__request__: Request = None,

backend/open_webui/utils/tools.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
query_knowledge_bases,
7878
search_knowledge_files,
7979
query_knowledge_files,
80+
view_file,
8081
view_knowledge_file,
8182
view_skill,
8283
)
@@ -444,7 +445,15 @@ def is_builtin_tool_enabled(category: str) -> bool:
444445
if is_builtin_tool_enabled("knowledge"):
445446
if model_knowledge:
446447
# Model has attached knowledge - only allow semantic search within it
447-
builtin_functions.extend([query_knowledge_files, view_knowledge_file])
448+
builtin_functions.append(query_knowledge_files)
449+
450+
knowledge_types = {
451+
item.get("type") for item in model_knowledge
452+
}
453+
if "file" in knowledge_types or "collection" in knowledge_types:
454+
builtin_functions.append(view_file)
455+
if "note" in knowledge_types:
456+
builtin_functions.append(view_note)
448457
else:
449458
# No model knowledge - allow full KB browsing
450459
builtin_functions.extend(

0 commit comments

Comments
 (0)