Buckets:
| import csv | |
| import sys | |
| from pathlib import Path | |
| from typing import Tuple, Optional | |
| import click | |
| from rich import print as rprint | |
| from .config_resolution import resolve_effective_config | |
| from .construct_paths import construct_paths | |
| from .preprocess import preprocess | |
| from .context_snapshot import start_snapshot_run | |
| from .context_snapshot_policy import declared_nondeterministic_tags | |
| from .xml_tagger import xml_tagger | |
| from .architecture_sync import ( | |
| get_architecture_entry_for_prompt, | |
| generate_tags_from_architecture, | |
| has_pdd_tags, | |
| ) | |
| def preprocess_main( | |
| ctx: click.Context, prompt_file: str, output: Optional[str], xml: bool, recursive: bool, double: bool, exclude: list, pdd_tags: bool = False, snapshot: bool = False | |
| ) -> Tuple[str, float, str]: | |
| """ | |
| CLI wrapper for preprocessing prompts. | |
| :param ctx: Click context object containing CLI options and parameters. | |
| :param prompt_file: Path to the prompt file to preprocess. | |
| :param output: Optional path where to save the preprocessed prompt. | |
| :param xml: If True, insert XML delimiters for better structure. | |
| :param recursive: If True, recursively preprocess all prompt files in the prompt file. | |
| :param double: If True, curly brackets will be doubled. | |
| :param exclude: List of keys to exclude from curly bracket doubling. | |
| :return: Tuple containing the preprocessed prompt, total cost, and model name used. | |
| :param pdd_tags: If True, inject PDD metadata tags from architecture.json. | |
| :param snapshot: If True, write replayable expanded prompt context artifacts. | |
| """ | |
| try: | |
| # Construct file paths | |
| input_file_paths = {"prompt_file": prompt_file} | |
| command_options = {"output": output} | |
| resolved_config, input_strings, output_file_paths, _ = construct_paths( | |
| input_file_paths=input_file_paths, | |
| force=ctx.obj.get("force", False), | |
| quiet=ctx.obj.get("quiet", False), | |
| command="preprocess", | |
| command_options=command_options, | |
| context_override=ctx.obj.get('context') | |
| ) | |
| resolve_effective_config(ctx, resolved_config) | |
| # Load prompt file | |
| prompt = input_strings["prompt_file"] | |
| # Inject PDD metadata tags from architecture.json if requested | |
| pdd_tags_injected = False | |
| if pdd_tags: | |
| prompt_filename = Path(prompt_file).name | |
| arch_entry = get_architecture_entry_for_prompt(prompt_filename) | |
| if arch_entry: | |
| if has_pdd_tags(prompt): | |
| if not ctx.obj.get("quiet", False): | |
| rprint(f"[yellow]Prompt already has PDD tags, skipping injection.[/yellow]") | |
| else: | |
| generated_tags = generate_tags_from_architecture(arch_entry) | |
| if generated_tags: | |
| prompt = generated_tags + '\n\n' + prompt | |
| pdd_tags_injected = True | |
| if not ctx.obj.get("quiet", False): | |
| rprint(f"[green]Injected PDD tags from architecture.json[/green]") | |
| else: | |
| if not ctx.obj.get("quiet", False): | |
| rprint(f"[yellow]No architecture entry found for '{prompt_filename}', skipping PDD tags.[/yellow]") | |
| if xml: | |
| if snapshot: | |
| raise click.UsageError("--snapshot is not supported with --xml.") | |
| # Use xml_tagger to add XML delimiters | |
| # Use centralized config resolution with proper priority: CLI > pddrc > defaults | |
| effective_config = resolve_effective_config(ctx, resolved_config) | |
| strength = effective_config["strength"] | |
| temperature = effective_config["temperature"] | |
| time = effective_config["time"] | |
| verbose = ctx.obj.get("verbose", False) | |
| xml_tagged, total_cost, model_name = xml_tagger( | |
| prompt, | |
| strength, | |
| temperature, | |
| verbose, | |
| time=time | |
| ) | |
| processed_prompt = xml_tagged | |
| else: | |
| # Preprocess the prompt | |
| recorder = None | |
| if snapshot: | |
| recorder = start_snapshot_run(prompt_file, command="pdd preprocess") | |
| recorder.record_prompt_source(prompt) | |
| initial_seen = {str(Path(prompt_file).resolve())} | |
| processed_prompt = preprocess( | |
| prompt, | |
| recursive, | |
| double, | |
| exclude_keys=exclude, | |
| _seen=initial_seen, | |
| snapshot_recorder=recorder, | |
| ) | |
| if snapshot and recursive: | |
| deferred_tags = declared_nondeterministic_tags(processed_prompt) | |
| if deferred_tags: | |
| tag_list = ", ".join(deferred_tags) | |
| raise click.UsageError( | |
| "--snapshot --recursive left active nondeterministic tags " | |
| f"({tag_list}) in the expanded prompt (for example nested " | |
| "<shell>/<web>/query= includes). Run without --recursive " | |
| "so dynamic context is executed and recorded." | |
| ) | |
| total_cost, model_name = 0.0, "N/A" | |
| # Save the preprocessed prompt | |
| output_path = Path(output_file_paths["output"]) | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| output_path.write_text(processed_prompt, encoding="utf-8") | |
| snapshot_manifest = None | |
| if snapshot and not xml: | |
| snapshot_manifest = recorder.finalize( | |
| expanded_prompt=processed_prompt, | |
| prompt_text=prompt, | |
| output_files=[output_file_paths["output"]], | |
| model=model_name, | |
| ) | |
| # Provide user feedback | |
| if not ctx.obj.get("quiet", False): | |
| rprint("[bold green]Prompt preprocessing completed successfully.[/bold green]") | |
| if pdd_tags_injected: | |
| rprint("[bold]PDD metadata tags: injected from architecture.json[/bold]") | |
| if xml: | |
| rprint(f"[bold]XML Tagging used: {model_name}[/bold]") | |
| else: | |
| rprint(f"[bold]Model used: {model_name}[/bold]") | |
| rprint(f"[bold]Total cost: ${total_cost:.6f}[/bold]") | |
| rprint(f"[bold]Preprocessed prompt saved to:[/bold] {output_file_paths['output']}") | |
| if snapshot_manifest: | |
| rprint(f"[bold]Context snapshot:[/bold] {snapshot_manifest['manifest_path']}") | |
| return processed_prompt, total_cost, model_name | |
| except click.UsageError: | |
| raise | |
| except Exception as e: | |
| if not ctx.obj.get("quiet", False): | |
| rprint(f"[bold red]Error during preprocessing:[/bold red] {e}") | |
| sys.exit(1) | |
Xet Storage Details
- Size:
- 6.86 kB
- Xet hash:
- e0d85cd7c767c16e60722a9e3259cc7ae8e5766985bfac4617bba002c02d7fae
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.