39 lines
1.3 KiB
Plaintext
39 lines
1.3 KiB
Plaintext
# workspace_file.dspy - 读取/保存工作空间文件
|
|
|
|
action = (params_kw or {}).get('action', 'read')
|
|
filepath = (params_kw or {}).get('path', '').strip()
|
|
workspace_base = '/d/pipeline/workspaces'
|
|
|
|
# 安全检查
|
|
if not filepath or not filepath.startswith(workspace_base):
|
|
return json.dumps({"error": "路径不合法"}, ensure_ascii=False)
|
|
|
|
import os
|
|
|
|
if action == 'read':
|
|
if not os.path.isfile(filepath):
|
|
return json.dumps({"error": "文件不存在"}, ensure_ascii=False)
|
|
try:
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
|
content = f.read(50000)
|
|
return json.dumps({
|
|
"path": filepath,
|
|
"name": os.path.basename(filepath),
|
|
"size": os.path.getsize(filepath),
|
|
"content": content,
|
|
}, ensure_ascii=False)
|
|
except Exception as e:
|
|
return json.dumps({"error": str(e)}, ensure_ascii=False)
|
|
|
|
elif action == 'save':
|
|
content = (params_kw or {}).get('content', '')
|
|
try:
|
|
with open(filepath, 'w', encoding='utf-8') as f:
|
|
f.write(content)
|
|
return json.dumps({"success": True, "path": filepath}, ensure_ascii=False)
|
|
except Exception as e:
|
|
return json.dumps({"error": str(e)}, ensure_ascii=False)
|
|
|
|
else:
|
|
return json.dumps({"error": "未知操作"}, ensure_ascii=False)
|