-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.py
More file actions
92 lines (75 loc) · 3.34 KB
/
Copy pathplugin.py
File metadata and controls
92 lines (75 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
"""串门 - 多智能体聊天后端"""
from qwenpaw.plugins.api import PluginApi
from fastapi import APIRouter
from pathlib import Path
import logging
import os
logger = logging.getLogger("qwenpaw.chuanmen")
# 插件目录(用于读取二维码图片)
PLUGIN_DIR = Path(__file__).parent
# 获取工作目录基础路径
try:
from qwenpaw.constant import WORKING_DIR
workspaces_base = Path(WORKING_DIR).expanduser() / "workspaces"
except Exception:
workspaces_base = Path.home() / ".copaw" / "workspaces"
def create_router() -> APIRouter:
router = APIRouter()
@router.get("/files")
async def list_files(agent_id: str = "default", search: str = ""):
"""递归扫描指定智能体的工作目录,返回文件列表(支持搜索)"""
base_dir = workspaces_base / agent_id
files = []
if base_dir.exists():
# 递归遍历所有子目录
for item in base_dir.rglob("*"):
if item.is_file():
# 跳过隐藏文件(文件名以.开头)和缓存目录
if item.name.startswith('.'):
continue
# 跳过缓存目录
if '__pycache__' in item.parts or 'node_modules' in item.parts:
continue
# 跳过 .git 目录中的文件
if '.git' in item.parts:
continue
stat = item.stat()
# 相对路径
rel_path = item.relative_to(base_dir)
# 搜索过滤
if search and search.lower() not in item.name.lower() and search.lower() not in str(rel_path).lower():
continue
files.append({
"name": item.name,
"path": str(item),
"rel_path": str(rel_path),
"size": stat.st_size,
"modified": stat.st_mtime,
"type": "file"
})
# 按修改时间倒序
files.sort(key=lambda x: x["modified"], reverse=True)
return {"files": files, "base_dir": str(base_dir), "total": len(files)}
@router.get("/files/download")
async def download_file(path: str):
"""下载文件"""
from fastapi.responses import FileResponse
file_path = Path(path)
if file_path.exists() and file_path.is_file():
return FileResponse(path=str(file_path), filename=file_path.name)
return {"error": "文件不存在"}
@router.get("/qrcode")
async def get_qrcode():
"""返回打赏二维码图片"""
from fastapi.responses import FileResponse
qr_path = PLUGIN_DIR / "qrcode.jpg"
if qr_path.exists():
return FileResponse(str(qr_path), media_type="image/jpeg")
return {"error": "未找到二维码图片"}
return router
class ChuanmenPlugin:
def register(self, api: PluginApi) -> None:
logger.info("串门插件后端已注册")
api.register_http_router(create_router(), prefix="/chuanmen", tags=["chuanmen"])
plugin = ChuanmenPlugin()