()
| 458 | # ============================================================ |
| 459 | |
| 460 | def main(): |
| 461 | print("=" * 60) |
| 462 | print("Tutorial 06: System Prompt Builder 演示") |
| 463 | print("=" * 60) |
| 464 | |
| 465 | # --- 1. 发现当前项目的 CLAUDE.md --- |
| 466 | cwd = str(Path(__file__).resolve().parent.parent) # 项目根目录 |
| 467 | print(f"\n项目根目录: {cwd}") |
| 468 | |
| 469 | ctx = ProjectContext.discover_with_git(cwd, "2026-04-02") |
| 470 | print(f"发现的指令文件: {len(ctx.instruction_files)} 个") |
| 471 | for f in ctx.instruction_files: |
| 472 | print(f" - {f.path} ({len(f.content)} 字符)") |
| 473 | |
| 474 | if ctx.git_status: |
| 475 | print(f"\nGit status 快照 (前 200 字符):") |
| 476 | print(f" {ctx.git_status[:200]}") |
| 477 | |
| 478 | # --- 2. 用 Builder 构造 system prompt --- |
| 479 | print("\n--- 构造 System Prompt ---") |
| 480 | import platform |
| 481 | prompt = ( |
| 482 | SystemPromptBuilder() |
| 483 | .with_os(platform.system(), platform.release()) |
| 484 | .with_project_context(ctx) |
| 485 | .append_section("# Language\nAlways respond in Chinese.") |
| 486 | .build() |
| 487 | ) |
| 488 | |
| 489 | print(f"总共 {len(prompt)} 个段落:") |
| 490 | for i, section in enumerate(prompt): |
| 491 | # 显示每个段落的前 60 个字符 |
| 492 | preview = section.replace('\n', ' ')[:60] |
| 493 | is_boundary = section == DYNAMIC_BOUNDARY |
| 494 | marker = " ═══ 分界线 ═══" if is_boundary else "" |
| 495 | print(f" [{i}] {preview}...{marker}") |
| 496 | |
| 497 | # --- 3. 看看完整的 prompt 有多长 --- |
| 498 | full_prompt = "\n\n".join(prompt) |
| 499 | print(f"\n完整 prompt 长度: {len(full_prompt)} 字符") |
| 500 | print(f"估算 token 数: ~{len(full_prompt) // 4} tokens") |
| 501 | |
| 502 | # --- 4. 看看 CLAUDE.md 怎么被注入的 --- |
| 503 | print("\n--- CLAUDE.md 注入位置 ---") |
| 504 | for i, section in enumerate(prompt): |
| 505 | if "Claude instructions" in section: |
| 506 | print(f" 在第 [{i}] 段落中找到 Claude instructions:") |
| 507 | print(f" {section[:200]}...") |
| 508 | break |
| 509 | |
| 510 | # 解说 |
| 511 | print("\n" + "=" * 60) |
| 512 | print("关键理解要点:") |
| 513 | print("=" * 60) |
| 514 | print(""" |
| 515 | 1. System Prompt 是分层构建的: |
| 516 | 固定层(角色/规则/安全) + 动态层(环境/项目/CLAUDE.md/配置) |
| 517 | 中间有一条 DYNAMIC_BOUNDARY 分界线 |
no test coverage detected