|
5 | 5 | import json |
6 | 6 | import sys |
7 | 7 | import click |
8 | | -from typing import Optional |
| 8 | +from typing import Optional, List |
9 | 9 |
|
10 | 10 | from codewiki.cli.config_manager import ConfigManager |
| 11 | +from codewiki.cli.models.config import AgentInstructions |
11 | 12 | from codewiki.cli.utils.errors import ( |
12 | 13 | ConfigurationError, |
13 | 14 | handle_error, |
|
23 | 24 | ) |
24 | 25 |
|
25 | 26 |
|
| 27 | +def parse_patterns(patterns_str: str) -> List[str]: |
| 28 | + """Parse comma-separated patterns into a list.""" |
| 29 | + if not patterns_str: |
| 30 | + return [] |
| 31 | + return [p.strip() for p in patterns_str.split(',') if p.strip()] |
| 32 | + |
| 33 | + |
26 | 34 | @click.group(name="config") |
27 | 35 | def config_group(): |
28 | 36 | """Manage CodeWiki configuration (API credentials and settings).""" |
@@ -207,6 +215,7 @@ def config_show(output_json: bool): |
207 | 215 | "cluster_model": config.cluster_model if config else "", |
208 | 216 | "fallback_model": config.fallback_model if config else "glm-4p5", |
209 | 217 | "default_output": config.default_output if config else "docs", |
| 218 | + "agent_instructions": config.agent_instructions.to_dict() if config and config.agent_instructions else {}, |
210 | 219 | "config_file": str(manager.config_file_path) |
211 | 220 | } |
212 | 221 | click.echo(json.dumps(output, indent=2)) |
@@ -239,6 +248,23 @@ def config_show(output_json: bool): |
239 | 248 | if config: |
240 | 249 | click.echo(f" Default Output: {config.default_output}") |
241 | 250 |
|
| 251 | + click.echo() |
| 252 | + click.secho("Agent Instructions", fg="cyan", bold=True) |
| 253 | + if config and config.agent_instructions and not config.agent_instructions.is_empty(): |
| 254 | + agent = config.agent_instructions |
| 255 | + if agent.include_patterns: |
| 256 | + click.echo(f" Include patterns: {', '.join(agent.include_patterns)}") |
| 257 | + if agent.exclude_patterns: |
| 258 | + click.echo(f" Exclude patterns: {', '.join(agent.exclude_patterns)}") |
| 259 | + if agent.focus_modules: |
| 260 | + click.echo(f" Focus modules: {', '.join(agent.focus_modules)}") |
| 261 | + if agent.doc_type: |
| 262 | + click.echo(f" Doc type: {agent.doc_type}") |
| 263 | + if agent.custom_instructions: |
| 264 | + click.echo(f" Custom instructions: {agent.custom_instructions[:50]}...") |
| 265 | + else: |
| 266 | + click.secho(" Using defaults (no custom settings)", fg="yellow") |
| 267 | + |
242 | 268 | click.echo() |
243 | 269 | click.echo(f"Configuration file: {manager.config_file_path}") |
244 | 270 | click.echo() |
@@ -396,3 +422,170 @@ def config_validate(quick: bool, verbose: bool): |
396 | 422 | except Exception as e: |
397 | 423 | sys.exit(handle_error(e, verbose=verbose)) |
398 | 424 |
|
| 425 | + |
| 426 | +@config_group.command(name="agent") |
| 427 | +@click.option( |
| 428 | + "--include", |
| 429 | + "-i", |
| 430 | + type=str, |
| 431 | + default=None, |
| 432 | + help="Comma-separated file patterns to include (e.g., '*.cs,*.py')", |
| 433 | +) |
| 434 | +@click.option( |
| 435 | + "--exclude", |
| 436 | + "-e", |
| 437 | + type=str, |
| 438 | + default=None, |
| 439 | + help="Comma-separated patterns to exclude (e.g., '*Tests*,*Specs*')", |
| 440 | +) |
| 441 | +@click.option( |
| 442 | + "--focus", |
| 443 | + "-f", |
| 444 | + type=str, |
| 445 | + default=None, |
| 446 | + help="Comma-separated modules/paths to focus on (e.g., 'src/core,src/api')", |
| 447 | +) |
| 448 | +@click.option( |
| 449 | + "--doc-type", |
| 450 | + "-t", |
| 451 | + type=click.Choice(['api', 'architecture', 'user-guide', 'developer'], case_sensitive=False), |
| 452 | + default=None, |
| 453 | + help="Default type of documentation to generate", |
| 454 | +) |
| 455 | +@click.option( |
| 456 | + "--instructions", |
| 457 | + type=str, |
| 458 | + default=None, |
| 459 | + help="Custom instructions for the documentation agent", |
| 460 | +) |
| 461 | +@click.option( |
| 462 | + "--clear", |
| 463 | + is_flag=True, |
| 464 | + help="Clear all agent instructions", |
| 465 | +) |
| 466 | +def config_agent( |
| 467 | + include: Optional[str], |
| 468 | + exclude: Optional[str], |
| 469 | + focus: Optional[str], |
| 470 | + doc_type: Optional[str], |
| 471 | + instructions: Optional[str], |
| 472 | + clear: bool |
| 473 | +): |
| 474 | + """ |
| 475 | + Configure default agent instructions for documentation generation. |
| 476 | + |
| 477 | + These settings are used as defaults when running 'codewiki generate'. |
| 478 | + Runtime options (--include, --exclude, etc.) override these defaults. |
| 479 | + |
| 480 | + Examples: |
| 481 | + |
| 482 | + \b |
| 483 | + # Set include patterns for C# projects |
| 484 | + $ codewiki config agent --include "*.cs" |
| 485 | + |
| 486 | + \b |
| 487 | + # Exclude test projects |
| 488 | + $ codewiki config agent --exclude "*Tests*,*Specs*,test_*" |
| 489 | + |
| 490 | + \b |
| 491 | + # Focus on specific modules |
| 492 | + $ codewiki config agent --focus "src/core,src/api" |
| 493 | + |
| 494 | + \b |
| 495 | + # Set default doc type |
| 496 | + $ codewiki config agent --doc-type architecture |
| 497 | + |
| 498 | + \b |
| 499 | + # Add custom instructions |
| 500 | + $ codewiki config agent --instructions "Focus on public APIs and include usage examples" |
| 501 | + |
| 502 | + \b |
| 503 | + # Clear all agent instructions |
| 504 | + $ codewiki config agent --clear |
| 505 | + """ |
| 506 | + try: |
| 507 | + manager = ConfigManager() |
| 508 | + |
| 509 | + if not manager.load(): |
| 510 | + click.secho("\n✗ Configuration not found.", fg="red", err=True) |
| 511 | + click.echo("\nPlease run 'codewiki config set' first to configure your API credentials.") |
| 512 | + sys.exit(EXIT_CONFIG_ERROR) |
| 513 | + |
| 514 | + config = manager.get_config() |
| 515 | + |
| 516 | + if clear: |
| 517 | + # Clear all agent instructions |
| 518 | + config.agent_instructions = AgentInstructions() |
| 519 | + manager.save() |
| 520 | + click.echo() |
| 521 | + click.secho("✓ Agent instructions cleared", fg="green") |
| 522 | + click.echo() |
| 523 | + return |
| 524 | + |
| 525 | + # Check if at least one option is provided |
| 526 | + if not any([include, exclude, focus, doc_type, instructions]): |
| 527 | + # Display current settings |
| 528 | + click.echo() |
| 529 | + click.secho("Agent Instructions", fg="blue", bold=True) |
| 530 | + click.echo("━" * 40) |
| 531 | + click.echo() |
| 532 | + |
| 533 | + agent = config.agent_instructions |
| 534 | + if agent and not agent.is_empty(): |
| 535 | + if agent.include_patterns: |
| 536 | + click.echo(f" Include patterns: {', '.join(agent.include_patterns)}") |
| 537 | + if agent.exclude_patterns: |
| 538 | + click.echo(f" Exclude patterns: {', '.join(agent.exclude_patterns)}") |
| 539 | + if agent.focus_modules: |
| 540 | + click.echo(f" Focus modules: {', '.join(agent.focus_modules)}") |
| 541 | + if agent.doc_type: |
| 542 | + click.echo(f" Doc type: {agent.doc_type}") |
| 543 | + if agent.custom_instructions: |
| 544 | + click.echo(f" Custom instructions: {agent.custom_instructions}") |
| 545 | + else: |
| 546 | + click.secho(" No agent instructions configured (using defaults)", fg="yellow") |
| 547 | + |
| 548 | + click.echo() |
| 549 | + click.echo("Use 'codewiki config agent --help' for usage information.") |
| 550 | + click.echo() |
| 551 | + return |
| 552 | + |
| 553 | + # Update agent instructions |
| 554 | + current = config.agent_instructions or AgentInstructions() |
| 555 | + |
| 556 | + if include is not None: |
| 557 | + current.include_patterns = parse_patterns(include) if include else None |
| 558 | + if exclude is not None: |
| 559 | + current.exclude_patterns = parse_patterns(exclude) if exclude else None |
| 560 | + if focus is not None: |
| 561 | + current.focus_modules = parse_patterns(focus) if focus else None |
| 562 | + if doc_type is not None: |
| 563 | + current.doc_type = doc_type if doc_type else None |
| 564 | + if instructions is not None: |
| 565 | + current.custom_instructions = instructions if instructions else None |
| 566 | + |
| 567 | + config.agent_instructions = current |
| 568 | + manager.save() |
| 569 | + |
| 570 | + # Display success messages |
| 571 | + click.echo() |
| 572 | + if include: |
| 573 | + click.secho(f"✓ Include patterns: {parse_patterns(include)}", fg="green") |
| 574 | + if exclude: |
| 575 | + click.secho(f"✓ Exclude patterns: {parse_patterns(exclude)}", fg="green") |
| 576 | + if focus: |
| 577 | + click.secho(f"✓ Focus modules: {parse_patterns(focus)}", fg="green") |
| 578 | + if doc_type: |
| 579 | + click.secho(f"✓ Doc type: {doc_type}", fg="green") |
| 580 | + if instructions: |
| 581 | + click.secho(f"✓ Custom instructions set", fg="green") |
| 582 | + |
| 583 | + click.echo("\n" + click.style("Agent instructions updated successfully.", fg="green", bold=True)) |
| 584 | + click.echo() |
| 585 | + |
| 586 | + except ConfigurationError as e: |
| 587 | + click.secho(f"\n✗ Configuration error: {e.message}", fg="red", err=True) |
| 588 | + sys.exit(e.exit_code) |
| 589 | + except Exception as e: |
| 590 | + sys.exit(handle_error(e)) |
| 591 | + |
0 commit comments