Skip to content

Commit 67caae4

Browse files
liaobwRock-520
authored andcommitted
修复附件归属的问题
1 parent 2bd8f93 commit 67caae4

File tree

2 files changed

+104
-74
lines changed

2 files changed

+104
-74
lines changed

backend/super-magic/app/command/storage_uploader_tool.py

Lines changed: 49 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -86,78 +86,68 @@ async def _load_credentials(self) -> bool:
8686
加载凭证文件,确定平台,并初始化/更新存储服务及其凭证。
8787
"""
8888
try:
89-
default_creds_paths = []
90-
if PathManager._initialized:
91-
project_root = PathManager.get_project_root()
92-
default_creds_paths.append(project_root / ".credentials" / "upload_credentials.json")
93-
default_creds_paths.append(project_root / "config" / "upload_credentials.json")
94-
else:
95-
logger.warning("PathManager not initialized. Relying on CWD for default credential paths.")
96-
default_creds_paths.append(Path(".credentials/upload_credentials.json").resolve())
97-
default_creds_paths.append(Path("config/upload_credentials.json").resolve())
98-
99-
89+
# 优先使用指定的凭证文件,否则使用默认路径
10090
credentials_path_to_load = None
10191
if self.credentials_file and Path(self.credentials_file).exists():
10292
credentials_path_to_load = Path(self.credentials_file)
10393
logger.info(f"使用指定的凭证文件: {credentials_path_to_load}")
10494
else:
105-
for p in default_creds_paths:
106-
if p.exists():
107-
credentials_path_to_load = p
108-
logger.info(f"使用默认凭证文件: {credentials_path_to_load}")
109-
break
110-
95+
default_path = Path(".credentials/upload_credentials.json").resolve()
96+
if default_path.exists():
97+
credentials_path_to_load = default_path
98+
logger.info(f"使用默认凭证文件: {credentials_path_to_load}")
99+
111100
if not credentials_path_to_load:
112-
logger.error("未找到任何可用的凭证文件 (检查了指定路径和默认路径)")
101+
logger.error("未找到可用的凭证文件")
113102
return False
114103

115104
with open(credentials_path_to_load, "r") as f:
116105
credentials_data = json.load(f)
117106

107+
# 读取并打印batch_id
108+
batch_id = credentials_data.get("batch_id", "未设置")
109+
logger.info(f"当前操作批次ID: {batch_id}")
110+
118111
upload_config_dict = credentials_data.get("upload_config")
119112
if not upload_config_dict:
120113
logger.error(f"凭证文件 {credentials_path_to_load} 中未找到 'upload_config' 键")
121114
return False
122-
115+
116+
# 读取 sandbox_id 和 organization_code(如果未设置)
123117
if self.sandbox_id is None and "sandbox_id" in credentials_data:
124118
self.sandbox_id = credentials_data.get("sandbox_id")
125119
logger.info(f"从凭证文件加载了 sandbox_id: {self.sandbox_id}")
126120
if self.organization_code is None and "organization_code" in credentials_data:
127121
self.organization_code = credentials_data.get("organization_code")
128122
logger.info(f"从凭证文件加载了 organization_code: {self.organization_code}")
129123

130-
# 每次重新加载凭证后也重新初始化存储服务
124+
# 直接从凭证中获取平台类型
125+
platform_type = None
126+
if 'platform' in upload_config_dict:
127+
try:
128+
platform_type = PlatformType(upload_config_dict['platform'])
129+
logger.info(f"从凭证文件中确定平台类型: {platform_type.value}")
130+
except (ValueError, TypeError):
131+
logger.warning(f"无法将凭证中的 platform 值 '{upload_config_dict['platform']}' 转换为 PlatformType 枚举")
132+
133+
# 初始化存储服务
131134
self.storage_service = await StorageFactory.get_storage(
132135
sts_token_refresh=None,
133-
metadata=None
136+
metadata=None,
137+
platform=platform_type
134138
)
135139

140+
# 设置凭证
136141
self.storage_service.set_credentials(upload_config_dict)
137-
138-
if hasattr(self.storage_service.credentials, 'platform') and isinstance(self.storage_service.credentials.platform, PlatformType):
139-
self.platform = self.storage_service.credentials.platform
140-
else:
141-
current_service_platform_name = self.storage_service.get_platform_name()
142-
try:
143-
self.platform = PlatformType(current_service_platform_name)
144-
logger.info(f"StorageFactory 初始化了基于 '{current_service_platform_name}' (环境变量/默认) 的服务.")
145-
except ValueError:
146-
logger.warning(f"无法从存储服务确定的平台名称 '{current_service_platform_name}' 转换为 PlatformType 枚举")
147-
self.platform = None
148142

149-
logger.info(f"凭证加载和存储服务准备完成,实际使用平台: {self.platform.value if self.platform else '未知'}")
143+
# 设置平台类型
144+
self.platform = platform_type
145+
146+
logger.info(f"凭证加载和存储服务准备完成,使用平台: {self.platform.value if self.platform else '未知'}")
150147
return True
151148

152-
except InitException as e:
153-
logger.error(f"存储服务初始化失败: {e}")
154-
return False
155-
except ValueError as e:
156-
logger.error(f"加载或设置凭证失败 (格式错误): {e}")
157-
return False
158149
except Exception as e:
159-
logger.error(f"加载凭证或初始化存储服务时发生未知错误: {e}", exc_info=True)
160-
logger.error(traceback.format_exc())
150+
logger.error(f"加载凭证或初始化存储服务时发生错误: {e}", exc_info=True)
161151
return False
162152

163153
def _get_file_hash(self, file_path: Path) -> str:
@@ -174,7 +164,7 @@ def _get_file_hash(self, file_path: Path) -> str:
174164
async def upload_file(self, file_path: Path, workspace_dir: Path) -> bool:
175165
# 每次都重新加载凭证以确保使用最新的凭证
176166
await self._load_credentials()
177-
167+
178168
try:
179169
if not file_path.exists():
180170
logger.warning(f"文件不存在,无法上传: {file_path}")
@@ -190,6 +180,7 @@ async def upload_file(self, file_path: Path, workspace_dir: Path) -> bool:
190180

191181
base_dir = self.storage_service.credentials.get_dir()
192182

183+
# 简化对象键构造逻辑,移除沙盒ID,直接使用base_dir和相对路径
193184
object_key = f"{base_dir}{relative_path_str}"
194185

195186
cached_hash = self.uploaded_files_cache.get_hash(object_key)
@@ -198,7 +189,7 @@ async def upload_file(self, file_path: Path, workspace_dir: Path) -> bool:
198189
return True
199190

200191
logger.info(f"开始上传文件到平台 {self.platform.value if self.platform else 'N/A'}: {relative_path_str}, 存储键: {object_key}")
201-
192+
202193
await self.storage_service.upload(file=str(file_path), key=object_key)
203194
self.uploaded_files_cache.set_hash(object_key, file_hash)
204195
# 更新最后上传时间
@@ -237,7 +228,7 @@ async def register_uploaded_files(self) -> bool:
237228
if not self.sandbox_id:
238229
logger.info("未设置沙盒ID,无法注册文件")
239230
return True
240-
231+
241232
if not self.uploaded_files_for_registration:
242233
logger.info("没有需要注册的新文件,跳过注册")
243234
return True
@@ -247,21 +238,21 @@ async def register_uploaded_files(self) -> bool:
247238
return False
248239

249240
api_url = f"{self.api_base_url.strip('/')}/api/v1/super-agent/file/process-attachments"
250-
241+
251242
request_data = {
252243
"attachments": self.uploaded_files_for_registration,
253244
"sandbox_id": self.sandbox_id
254245
}
255246
# 添加组织编码(如果有)
256247
if self.organization_code:
257248
request_data["organization_code"] = self.organization_code
258-
249+
259250
# 如果有task_id也加上(虽然较少使用)
260251
if self.task_id:
261252
request_data["task_id"] = self.task_id
262-
253+
263254
headers = {"Content-Type": "application/json", "User-Agent": "StorageUploaderTool/2.0"}
264-
255+
265256
logger.info(f"========= 文件注册请求信息 =========")
266257
logger.info(f"准备向API注册 {len(self.uploaded_files_for_registration)} 个文件 (沙盒ID: {self.sandbox_id}) ...")
267258
logger.info(f"请求URL: {api_url}")
@@ -301,7 +292,7 @@ async def scan_existing_files(self, workspace_dir: Path, refresh: bool = False):
301292
if refresh:
302293
self.uploaded_files_cache.clear()
303294
logger.info("强制刷新模式:已清空本地文件哈希缓存。")
304-
295+
305296
logger.info(f"开始扫描已存在文件于目录: {workspace_dir}")
306297
for item in workspace_dir.rglob('*'):
307298
if item.is_file():
@@ -317,10 +308,10 @@ async def _periodic_register(self):
317308
try:
318309
# 等待30秒后尝试注册
319310
await asyncio.sleep(30)
320-
311+
321312
# 如果有上传的文件且距上次上传超过20秒,则注册
322313
current_time = time.time()
323-
if (self.uploaded_files_for_registration and
314+
if (self.uploaded_files_for_registration and
324315
self.sandbox_id and
325316
current_time - self.last_upload_time > 20):
326317
logger.info("检测到30秒内无新上传,开始注册已上传文件")
@@ -336,7 +327,7 @@ async def watch_command(self, workspace_dir: Path, once: bool, refresh: bool):
336327
return
337328

338329
logger.info(f"监控命令启动,监控目录: {workspace_dir}, 一次性扫描: {once}, 强制刷新: {refresh}")
339-
330+
340331
await self.scan_existing_files(workspace_dir, refresh)
341332
if once:
342333
logger.info("已完成一次性扫描,程序退出。")
@@ -379,10 +370,10 @@ async def _process_upload_queue(self):
379370
file_path_to_upload = await self.upload_queue.get()
380371
try:
381372
# 延迟1秒,等待文件操作完成(与原TOSUploader一致)
382-
await asyncio.sleep(1)
373+
await asyncio.sleep(1)
383374
logger.info(f"队列处理器: 开始处理文件 {file_path_to_upload}")
384375
success = await self.tool.upload_file(file_path_to_upload, self.workspace_dir)
385-
376+
386377
# 更加精确的立即注册逻辑判断,与原TOSUploader保持一致
387378
if success and self.tool.uploaded_files_for_registration and self.tool.sandbox_id:
388379
logger.info(f"文件上传成功,尝试立即注册,已上传文件数: {len(self.tool.uploaded_files_for_registration)}")
@@ -398,7 +389,7 @@ def _schedule_upload(self, file_path_str: str):
398389
file_path = Path(file_path_str)
399390
if not file_path.is_absolute():
400391
file_path = self.workspace_dir / file_path
401-
392+
402393
asyncio.run_coroutine_threadsafe(self.upload_queue.put(file_path), self.loop)
403394
logger.debug(f"已将文件 {file_path} 添加到上传队列。")
404395

@@ -474,7 +465,7 @@ def start_storage_uploader_watcher(
474465
cmd_logger.warning(f"'--use-context' is True, but context credentials file not found at: {context_creds_path}")
475466
else:
476467
cmd_logger.warning("PathManager not initialized. Cannot resolve context credentials path for '--use-context'.")
477-
468+
478469
cmd_logger.info(f"Final credentials file to be used by StorageUploaderTool: {final_credentials_file or 'Default lookup in StorageUploaderTool'}")
479470

480471
try:
@@ -484,7 +475,7 @@ def start_storage_uploader_watcher(
484475
task_id=task_id,
485476
organization_code=organization_code
486477
)
487-
478+
488479
asyncio.run(
489480
_run_storage_uploader_watch_async(
490481
tool=tool_instance,
@@ -507,4 +498,4 @@ def start_storage_uploader_watcher(
507498
else:
508499
print(f"PathManager already initialized. Project root: {PathManager.get_project_root()}")
509500

510-
cli_app()
501+
cli_app()

0 commit comments

Comments
 (0)