|
19 | 19 | from astrbot.core.platform.message_type import MessageType |
20 | 20 | from astrbot.core.provider.entites import ProviderRequest |
21 | 21 | from astrbot.core.utils.history_saver import persist_agent_history |
| 22 | +from astrbot.core.utils.session_lock import session_lock_manager |
22 | 23 |
|
23 | 24 | if TYPE_CHECKING: |
24 | 25 | from astrbot.core.star.context import Context |
@@ -448,66 +449,67 @@ async def _woke_main_agent( |
448 | 449 | streaming_response=False, |
449 | 450 | provider_settings=provider_settings, |
450 | 451 | ) |
451 | | - req = ProviderRequest() |
452 | | - conv = await _get_session_conv(event=cron_event, plugin_context=self.ctx) |
453 | | - req.conversation = conv |
454 | | - # finetine the messages |
455 | | - context = json.loads(conv.history) |
456 | | - if context: |
457 | | - req.contexts = context |
458 | | - context_dump = req._print_friendly_context() |
459 | | - req.contexts = [] |
460 | | - req.system_prompt += ( |
461 | | - "\n\nBellow is you and user previous conversation history:\n" |
462 | | - f"---\n" |
463 | | - f"{context_dump}\n" |
464 | | - f"---\n" |
| 452 | + async with session_lock_manager.acquire_lock(umo): |
| 453 | + req = ProviderRequest() |
| 454 | + conv = await _get_session_conv(event=cron_event, plugin_context=self.ctx) |
| 455 | + req.conversation = conv |
| 456 | + # finetine the messages |
| 457 | + context = json.loads(conv.history) |
| 458 | + if context: |
| 459 | + req.contexts = context |
| 460 | + context_dump = req._print_friendly_context() |
| 461 | + req.contexts = [] |
| 462 | + req.system_prompt += ( |
| 463 | + "\n\nBellow is you and user previous conversation history:\n" |
| 464 | + f"---\n" |
| 465 | + f"{context_dump}\n" |
| 466 | + f"---\n" |
| 467 | + ) |
| 468 | + cron_job_str = json.dumps(extras.get("cron_job", {}), ensure_ascii=False) |
| 469 | + req.system_prompt += PROACTIVE_AGENT_CRON_WOKE_SYSTEM_PROMPT.format( |
| 470 | + cron_job=cron_job_str |
465 | 471 | ) |
466 | | - cron_job_str = json.dumps(extras.get("cron_job", {}), ensure_ascii=False) |
467 | | - req.system_prompt += PROACTIVE_AGENT_CRON_WOKE_SYSTEM_PROMPT.format( |
468 | | - cron_job=cron_job_str |
469 | | - ) |
470 | | - req.prompt = ( |
471 | | - "You are now responding to a scheduled task. " |
472 | | - "Proceed according to your system instructions. " |
473 | | - "Output using same language as previous conversation. " |
474 | | - "After completing your task, summarize and output your actions and results." |
475 | | - ) |
476 | | - if delivery_session_str: |
477 | | - if not req.func_tool: |
478 | | - req.func_tool = ToolSet() |
479 | | - req.func_tool.add_tool( |
480 | | - self.ctx.get_llm_tool_manager().get_builtin_tool(SendMessageToUserTool) |
| 472 | + req.prompt = ( |
| 473 | + "You are now responding to a scheduled task. " |
| 474 | + "Proceed according to your system instructions. " |
| 475 | + "Output using same language as previous conversation. " |
| 476 | + "After completing your task, summarize and output your actions and results." |
481 | 477 | ) |
| 478 | + if delivery_session_str: |
| 479 | + if not req.func_tool: |
| 480 | + req.func_tool = ToolSet() |
| 481 | + req.func_tool.add_tool( |
| 482 | + self.ctx.get_llm_tool_manager().get_builtin_tool( |
| 483 | + SendMessageToUserTool |
| 484 | + ) |
| 485 | + ) |
482 | 486 |
|
483 | | - result = await build_main_agent( |
484 | | - event=cron_event, plugin_context=self.ctx, config=config, req=req |
485 | | - ) |
486 | | - if not result: |
487 | | - logger.error("Failed to build main agent for cron job.") |
488 | | - return |
489 | | - |
490 | | - runner = result.agent_runner |
491 | | - async for _ in runner.step_until_done(30): |
492 | | - # agent will send message to user via using tools |
493 | | - pass |
494 | | - llm_resp = runner.get_final_llm_resp() |
495 | | - cron_meta = extras.get("cron_job", {}) if extras else {} |
496 | | - summary_note = ( |
497 | | - f"[CronJob] {cron_meta.get('name') or cron_meta.get('id', 'unknown')}: {cron_meta.get('description', '')} " |
498 | | - f" triggered at {cron_meta.get('run_started_at', 'unknown time')}, " |
499 | | - ) |
500 | | - if llm_resp and llm_resp.role == "assistant": |
501 | | - summary_note += ( |
502 | | - f"I finished this job, here is the result: {llm_resp.completion_text}" |
| 487 | + result = await build_main_agent( |
| 488 | + event=cron_event, plugin_context=self.ctx, config=config, req=req |
503 | 489 | ) |
| 490 | + if not result: |
| 491 | + logger.error("Failed to build main agent for cron job.") |
| 492 | + return |
504 | 493 |
|
505 | | - await persist_agent_history( |
506 | | - self.ctx.conversation_manager, |
507 | | - event=cron_event, |
508 | | - req=req, |
509 | | - summary_note=summary_note, |
510 | | - ) |
| 494 | + runner = result.agent_runner |
| 495 | + async for _ in runner.step_until_done(30): |
| 496 | + # agent will send message to user via using tools |
| 497 | + pass |
| 498 | + llm_resp = runner.get_final_llm_resp() |
| 499 | + cron_meta = extras.get("cron_job", {}) if extras else {} |
| 500 | + summary_note = ( |
| 501 | + f"[CronJob] {cron_meta.get('name') or cron_meta.get('id', 'unknown')}: {cron_meta.get('description', '')} " |
| 502 | + f" triggered at {cron_meta.get('run_started_at', 'unknown time')}, " |
| 503 | + ) |
| 504 | + if llm_resp and llm_resp.role == "assistant": |
| 505 | + summary_note += f"I finished this job, here is the result: {llm_resp.completion_text}" |
| 506 | + |
| 507 | + await persist_agent_history( |
| 508 | + self.ctx.conversation_manager, |
| 509 | + event=cron_event, |
| 510 | + req=req, |
| 511 | + summary_note=summary_note, |
| 512 | + ) |
511 | 513 | if not llm_resp: |
512 | 514 | logger.warning("Cron job agent got no response") |
513 | 515 | return |
|
0 commit comments