# workspace_file.dspy - 读取/保存工作空间文件 action = (params_kw or {}).get('action', 'read') filepath = (params_kw or {}).get('path', '').strip() import os if not filepath or not os.path.isfile(filepath): return json.dumps({"error": "文件不存在: " + filepath}, ensure_ascii=False) if action == 'read': 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)