MCPcopy Create free account
hub / github.com/Louisym/MiniCC / SystemPromptBuilder

Class SystemPromptBuilder

tutorials/18_design_patterns_for_agents.py:397–467  ·  view source on GitHub ↗

Builder 模式三要素: 1. 字段全部可选 (None 默认值) 2. with_*() 方法返回 self (链式调用) 3. build() 方法组装最终结果

Source from the content-addressed store, hash-verified

395 # 对应 prompt.rs:85-93 的 SystemPromptBuilder
396 @dataclass
397 class SystemPromptBuilder:
398 """
399 Builder 模式三要素:
400 1. 字段全部可选 (None 默认值)
401 2. with_*() 方法返回 self (链式调用)
402 3. build() 方法组装最终结果
403 """
404 # 所有字段可选 — 对应 prompt.rs:86-92 的 Option<String>
405 _os_name: str | None = None
406 _os_version: str | None = None
407 _style_name: str | None = None
408 _style_prompt: str | None = None
409 _project_cwd: str | None = None
410 _current_date: str | None = None
411 _extra_sections: list[str] = field(default_factory=list)
412
413 # 对应 prompt.rs:108-113
414 # pub fn with_os(mut self, name, version) -> Self { ... self }
415 def with_os(self, name: str, version: str) -> SystemPromptBuilder:
416 self._os_name = name
417 self._os_version = version
418 return self # 返回 self → 支持链式调用
419
420 # 对应 prompt.rs:101-106
421 def with_style(self, name: str, prompt: str) -> SystemPromptBuilder:
422 self._style_name = name
423 self._style_prompt = prompt
424 return self
425
426 def with_project(self, cwd: str, date: str) -> SystemPromptBuilder:
427 self._project_cwd = cwd
428 self._current_date = date
429 return self
430
431 # 对应 prompt.rs:128-131 的 append_section
432 def append_section(self, section: str) -> SystemPromptBuilder:
433 self._extra_sections.append(section)
434 return self
435
436 # 对应 prompt.rs:134-156 的 build()
437 def build(self) -> list[str]:
438 """组装最终的 system prompt 段落列表"""
439 sections = []
440
441 # 固定段落 (对应 get_simple_intro_section 等)
442 sections.append("You are Claude, an AI assistant by Anthropic.")
443
444 # 可选: 输出风格
445 if self._style_name and self._style_prompt:
446 sections.append(f"# Output Style: {self._style_name}\n{self._style_prompt}")
447
448 sections.append("# System\n- Follow instructions carefully.")
449 sections.append("# Doing tasks\n- Complete tasks step by step.")
450
451 # 动态分界线 (对应 SYSTEM_PROMPT_DYNAMIC_BOUNDARY)
452 # 分界线以上是静态内容 (API 缓存), 以下是动态内容 (每次不同)
453 sections.append("__SYSTEM_PROMPT_DYNAMIC_BOUNDARY__")
454

Callers 1

lesson_3_builder_patternFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected