-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(core): configurable hardcoded injections and i18n sync #6544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8e5b18c
b839de2
12cdf45
ad3e5c4
b301ff2
51eff66
73ce759
b8811c8
1f17ddd
dd99021
e6abc77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -231,6 +231,7 @@ def _get_runtime_computer_tools( | |
| cls, | ||
| runtime: str, | ||
| sandbox_cfg: dict | None = None, | ||
| local_cfg: dict | None = None, | ||
| session_id: str = "", | ||
| ) -> dict[str, FunctionTool]: | ||
| from astrbot.core.computer.computer_tool_provider import ComputerToolProvider | ||
|
|
@@ -240,6 +241,7 @@ def _get_runtime_computer_tools( | |
| ctx = ToolProviderContext( | ||
| computer_use_runtime=runtime, | ||
| sandbox_cfg=sandbox_cfg, | ||
| local_cfg=local_cfg, | ||
| session_id=session_id, | ||
| ) | ||
| tools = provider.get_tools(ctx) | ||
|
|
@@ -264,9 +266,11 @@ def _build_handoff_toolset( | |
| provider_settings = cfg.get("provider_settings", {}) | ||
| runtime = str(provider_settings.get("computer_use_runtime", "local")) | ||
| sandbox_cfg = provider_settings.get("sandbox", {}) | ||
| local_cfg = provider_settings.get("local", {}) | ||
| runtime_computer_tools = cls._get_runtime_computer_tools( | ||
| runtime, | ||
| sandbox_cfg=sandbox_cfg, | ||
| local_cfg=local_cfg, | ||
| session_id=event.unified_msg_origin, | ||
| ) | ||
|
|
||
|
|
@@ -525,6 +529,8 @@ async def _wake_main_agent_for_background_result( | |
|
|
||
| event = run_context.context.event | ||
| ctx = run_context.context.context | ||
| cfg = ctx.get_config(umo=event.unified_msg_origin) | ||
| proactive_cfg = cfg.get("provider_settings", {}).get("proactive_capability", {}) | ||
|
|
||
| task_result = { | ||
| "task_id": task_id, | ||
|
|
@@ -563,13 +569,54 @@ async def _wake_main_agent_for_background_result( | |
| req.contexts = context | ||
| context_dump = req._print_friendly_context() | ||
| req.contexts = [] | ||
| req.system_prompt += CONVERSATION_HISTORY_INJECT_PREFIX + context_dump | ||
| history_wrap_prompt = proactive_cfg.get( | ||
| "background_history_wrap_prompt", | ||
| CONVERSATION_HISTORY_INJECT_PREFIX | ||
| ) | ||
| if history_wrap_prompt: | ||
| try: | ||
| req.system_prompt += history_wrap_prompt.format( | ||
| context_dump=context_dump | ||
| ) | ||
| except Exception: | ||
| logger.error( | ||
| "background_history_wrap_prompt 格式化失败,回退到默认模板", | ||
| exc_info=True, | ||
| ) | ||
| req.system_prompt += CONVERSATION_HISTORY_INJECT_PREFIX.format( | ||
| context_dump=context_dump | ||
| ) | ||
| else: | ||
| req.system_prompt += CONVERSATION_HISTORY_INJECT_PREFIX.format( | ||
| context_dump=context_dump | ||
| ) | ||
|
|
||
| bg = json.dumps(extras["background_task_result"], ensure_ascii=False) | ||
| req.system_prompt += BACKGROUND_TASK_RESULT_WOKE_SYSTEM_PROMPT.format( | ||
| background_task_result=bg | ||
| background_execution_prompt = proactive_cfg.get( | ||
| "background_execution_prompt", | ||
| BACKGROUND_TASK_RESULT_WOKE_SYSTEM_PROMPT | ||
| ) | ||
| if background_execution_prompt: | ||
| try: | ||
| req.system_prompt += background_execution_prompt.format( | ||
| background_task_result=bg | ||
| ) | ||
| except Exception: | ||
| logger.error( | ||
| "background_execution_prompt 格式化失败,回退到默认模板", | ||
| exc_info=True, | ||
| ) | ||
| req.system_prompt += BACKGROUND_TASK_RESULT_WOKE_SYSTEM_PROMPT.format( | ||
| background_task_result=bg | ||
| ) | ||
| else: | ||
| req.system_prompt += BACKGROUND_TASK_RESULT_WOKE_SYSTEM_PROMPT.format( | ||
| background_task_result=bg | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的 为了提高代码的可维护性并减少重复,建议将这个逻辑提取到一个辅助函数中。例如: def _format_prompt_with_fallback(
prompt_template: str | None,
default_template: str,
logger,
log_message: str,
**kwargs,
) -> str:
"""Helper to format a prompt with a fallback to a default template."""
template_to_use = prompt_template or default_template
try:
return template_to_use.format(**kwargs)
except Exception:
logger.error(log_message, exc_info=True)
return default_template.format(**kwargs)
# 使用示例:
# req.system_prompt += _format_prompt_with_fallback(
# proactive_cfg.get("background_history_wrap_prompt"),
# CONVERSATION_HISTORY_INJECT_PREFIX,
# logger,
# "background_history_wrap_prompt 格式化失败,回退到默认模板",
# context_dump=context_dump,
# )这样可以使代码更简洁,也更容易维护。 |
||
| req.prompt = proactive_cfg.get( | ||
| "background_task_work_user_prompt", | ||
| BACKGROUND_TASK_WOKE_USER_PROMPT | ||
| ) | ||
| req.prompt = BACKGROUND_TASK_WOKE_USER_PROMPT | ||
| if not req.func_tool: | ||
| req.func_tool = ToolSet() | ||
| req.func_tool.add_tool(SEND_MESSAGE_TO_USER_TOOL) | ||
|
|
@@ -587,15 +634,29 @@ async def _wake_main_agent_for_background_result( | |
| pass | ||
| llm_resp = runner.get_final_llm_resp() | ||
| task_meta = extras.get("background_task_result", {}) | ||
| summary_note = ( | ||
| f"[BackgroundTask] {summary_name} " | ||
| f"(task_id={task_meta.get('task_id', task_id)}) finished. " | ||
| f"Result: {task_meta.get('result') or result_text or 'no content'}" | ||
| ) | ||
|
|
||
| background_task_summary_note = proactive_cfg.get("background_task_summary_note", "") | ||
| try: | ||
| summary_note = background_task_summary_note.format( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Undefined In |
||
| summary_name=summary_name, | ||
| task_id=task_id, | ||
| result=result, | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里存在一个 这会导致在 为了修复这个问题,你应该在 result_text = llm_resp.completion_text if llm_resp and llm_resp.completion_text else ""
result_for_summary = task_meta.get('result') or result_text or 'no content'
try:
summary_note = background_task_summary_note.format(
summary_name=summary_name,
task_id=task_id,
result=result_for_summary,
) |
||
| except Exception: | ||
| summary_note = ( | ||
| f"[BackgroundTask] {summary_name} " | ||
| f"(task_id={task_meta.get('task_id', task_id)}) finished. " | ||
| f"Result: {task_meta.get('result') or result_text or 'no content'}" | ||
| ) | ||
| if llm_resp and llm_resp.completion_text: | ||
| summary_note += ( | ||
| f"I finished the task, here is the result: {llm_resp.completion_text}" | ||
| background_task_summary_note_result = proactive_cfg.get( | ||
| "background_task_summary_note_result", "" | ||
| ) | ||
| try: | ||
| result = background_task_summary_note_result.format(result=llm_resp.completion_text) | ||
| except Exception: | ||
| result = f"I finished the task, here is the result: {llm_resp.completion_text}" | ||
| summary_note += result | ||
| await persist_agent_history( | ||
| ctx.conversation_manager, | ||
| event=cron_event, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.