System Prompt 构造器。 通过链式调用逐步添加内容,最后 build() 组装成完整的 prompt。 对应源码: prompt.rs:84-185
| 296 | |
| 297 | |
| 298 | class SystemPromptBuilder: |
| 299 | """ |
| 300 | System Prompt 构造器。 |
| 301 | |
| 302 | 通过链式调用逐步添加内容,最后 build() 组装成完整的 prompt。 |
| 303 | 对应源码: prompt.rs:84-185 |
| 304 | """ |
| 305 | |
| 306 | def __init__(self): |
| 307 | self._os_name: Optional[str] = None |
| 308 | self._os_version: Optional[str] = None |
| 309 | self._project_context: Optional[ProjectContext] = None |
| 310 | self._extra_sections: list[str] = [] |
| 311 | |
| 312 | def with_os(self, os_name: str, os_version: str) -> "SystemPromptBuilder": |
| 313 | """添加操作系统信息""" |
| 314 | self._os_name = os_name |
| 315 | self._os_version = os_version |
| 316 | return self |
| 317 | |
| 318 | def with_project_context(self, ctx: ProjectContext) -> "SystemPromptBuilder": |
| 319 | """添加项目上下文""" |
| 320 | self._project_context = ctx |
| 321 | return self |
| 322 | |
| 323 | def append_section(self, section: str) -> "SystemPromptBuilder": |
| 324 | """追加自定义段落""" |
| 325 | self._extra_sections.append(section) |
| 326 | return self |
| 327 | |
| 328 | def build(self) -> list[str]: |
| 329 | """ |
| 330 | 组装所有部分,生成最终的 system prompt。 |
| 331 | |
| 332 | 返回一个字符串列表,每个元素是一个"段落"。 |
| 333 | 对应源码: prompt.rs:134-156 |
| 334 | """ |
| 335 | sections = [] |
| 336 | |
| 337 | # 第 1 层:角色介绍(固定) |
| 338 | sections.append( |
| 339 | "You are an interactive agent that helps users with software engineering tasks. " |
| 340 | "Use the instructions below and the tools available to you to assist the user." |
| 341 | ) |
| 342 | |
| 343 | # 第 2 层:系统规则(固定) |
| 344 | sections.append(self._system_section()) |
| 345 | |
| 346 | # 第 3 层:任务准则(固定) |
| 347 | sections.append(self._doing_tasks_section()) |
| 348 | |
| 349 | # 第 4 层:操作安全(固定) |
| 350 | sections.append(self._actions_section()) |
| 351 | |
| 352 | # ═══ 动态分界线 ═══ |
| 353 | sections.append(DYNAMIC_BOUNDARY) |
| 354 | |
| 355 | # 第 5 层:环境信息(动态) |