feat(plugin_system): 引入高级Prompt注入规则系统以取代旧注入点机制

引入了一套全新的、基于规则的Prompt注入系统,以取代原有的 `injection_point` 机制。这套新系统提供了更强大、更灵活的Prompt内容注入能力。

主要变更包括:
- **引入 `InjectionRule` 和 `InjectionType`**:定义了注入规则的数据结构和注入类型(如 `PREPEND`, `APPEND`, `REPLACE`, `REMOVE`, `INSERT_AFTER`),允许插件开发者精确控制注入行为。
- **重构 `PromptComponentManager`**:核心逻辑从简单地拼接字符串 (`execute_components_for`) 重构为按优先级应用注入规则 (`apply_injections`),支持正则表达式匹配和更复杂的注入操作。
- **向后兼容**:`PromptInfo` 中增加了兼容逻辑,能自动将旧的 `injection_point` 定义转换为新的 `injection_rules`,确保现有插件无需立即修改即可正常工作。
- **更新 `BasePrompt`**:废弃了 `injection_point` 属性,并推荐使用新的 `injection_rules` 属性。
- **更新示例插件**:`hello_world_plugin` 已更新,展示了新注入规则的使用方法。

BREAKING CHANGE: `BasePrompt` 中的 `injection_point` 属性已被废弃。虽然目前存在向后兼容逻辑,但未来版本将移除该属性。所有Prompt组件都应迁移至使用 `injection_rules` 以获得更强的控制力和未来的兼容性。
This commit is contained in:
minecraft1024a
2025-10-24 19:51:35 +08:00
committed by Windpicker-owo
parent c96434299f
commit 92f5f9bbd9
7 changed files with 200 additions and 83 deletions

View File

@@ -205,15 +205,19 @@ class PromptManager:
# 确保我们有有效的parameters实例
params_for_injection = parameters or original_prompt.parameters
components_prefix = await prompt_component_manager.execute_components_for(
injection_point=original_prompt.name, params=params_for_injection
# 应用所有匹配的注入规则,获取修改后的模板
modified_template = await prompt_component_manager.apply_injections(
target_prompt_name=original_prompt.name,
original_template=original_prompt.template,
params=params_for_injection,
)
if components_prefix:
logger.info(f"'{name}'注入插件内容: \n{components_prefix}")
# 如果模板被修改了就创建一个新的临时Prompt实例
if modified_template != original_prompt.template:
logger.info(f"'{name}'应用了Prompt注入规则")
# 创建一个新的临时Prompt实例不进行注册
new_template = f"{components_prefix}\n\n{original_prompt.template}"
temp_prompt = Prompt(
template=new_template,
template=modified_template,
name=original_prompt.name,
parameters=original_prompt.parameters,
should_register=False, # 确保不重新注册
@@ -1158,12 +1162,12 @@ async def create_prompt_async(
# 动态注入插件内容
if name:
components_prefix = await prompt_component_manager.execute_components_for(
injection_point=name, params=final_params
modified_template = await prompt_component_manager.apply_injections(
target_prompt_name=name, original_template=template, params=final_params
)
if components_prefix:
logger.debug(f"'{name}'注入插件内容: \n{components_prefix}")
template = f"{components_prefix}\n\n{template}"
if modified_template != template:
logger.debug(f"'{name}'应用了Prompt注入规则")
template = modified_template
# 使用可能已修改的模板创建实例
prompt = create_prompt(template, name, final_params)