(
event: AstrMessageEvent,
req: ProviderRequest,
config: MainAgentBuildConfig,
)
| 299 | |
| 300 | |
| 301 | async def _apply_file_extract( |
| 302 | event: AstrMessageEvent, |
| 303 | req: ProviderRequest, |
| 304 | config: MainAgentBuildConfig, |
| 305 | ) -> None: |
| 306 | file_paths = [] |
| 307 | file_names = [] |
| 308 | for comp in event.message_obj.message: |
| 309 | if isinstance(comp, File): |
| 310 | file_paths.append(await comp.get_file()) |
| 311 | file_names.append(comp.name) |
| 312 | elif isinstance(comp, Reply) and comp.chain: |
| 313 | for reply_comp in comp.chain: |
| 314 | if isinstance(reply_comp, File): |
| 315 | file_paths.append(await reply_comp.get_file()) |
| 316 | file_names.append(reply_comp.name) |
| 317 | if not file_paths: |
| 318 | return |
| 319 | if not req.prompt: |
| 320 | req.prompt = "总结一下文件里面讲了什么?" |
| 321 | if config.file_extract_prov == "moonshotai": |
| 322 | if not config.file_extract_msh_api_key: |
| 323 | logger.error("Moonshot AI API key for file extract is not set") |
| 324 | return |
| 325 | file_contents = await asyncio.gather( |
| 326 | *[ |
| 327 | extract_file_moonshotai( |
| 328 | file_path, |
| 329 | config.file_extract_msh_api_key, |
| 330 | ) |
| 331 | for file_path in file_paths |
| 332 | ] |
| 333 | ) |
| 334 | else: |
| 335 | logger.error("Unsupported file extract provider: %s", config.file_extract_prov) |
| 336 | return |
| 337 | |
| 338 | for file_content, file_name in zip(file_contents, file_names): |
| 339 | req.contexts.append( |
| 340 | { |
| 341 | "role": "system", |
| 342 | "content": ( |
| 343 | "File Extract Results of user uploaded files:\n" |
| 344 | f"{file_content}\nFile Name: {file_name or 'Unknown'}" |
| 345 | ), |
| 346 | }, |
| 347 | ) |
| 348 | |
| 349 | |
| 350 | def _apply_prompt_prefix(req: ProviderRequest, cfg: dict) -> None: |
no test coverage detected