Repository: ErikBjare/gptme
File: gptme/util/output_storage.py (Python)

Code before:
        logdir: Conversation directory for saving
        output_type: Type of tool output (e.g., "shell", "python")
        command_info: Optional command that generated output
        original_tokens: Optional token count of original content

    Returns:
        Tuple of (summary_text, saved_path)
    """
    # Create output directory
    output_dir = logdir / "tool-outputs" / output_type
    output_dir.mkdir(parents=True, exist_ok=True)

    # Generate filename with timestamp and content hash
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    content_hash = hashlib.sha256(content.encode()).hexdigest()[:8]
    filename = f"{timestamp}-{content_hash}.txt"
    saved_path = output_dir / filename

    # Save content to file
    try:
        saved_path.write_text(content)
        logger.info(f"Saved large output to {saved_path}")
    except Exception as e:
        logger.error(f"Failed to save large output: {e}")
        # Return empty summary and path even if save failed
        return ("", saved_path)

    # Create summary text
    summary_parts = [f"[Large {output_type} output saved]"]

    if command_info:
        summary_parts.append(f"Command: {command_info}")

    if original_tokens:
        summary_parts.append(f"Original: {original_tokens} tokens")

    summary_parts.append(f"Full output: {saved_path}")

    summary_text = "\n".join(summary_parts)

    return (summary_text, saved_path)


def create_tool_result_summary(
    content: str,
    original_tokens: int,
    logdir: Path | None,
    tool_name: str = "tool",
) -> str:
    """
    Create a summary of a large tool result, with content saved to file.

Review comment (suggestion):
```suggestion
    saved_path.write_text(content)
    logger.info(f"Saved large output to {saved_path}")
```

Code after:
        logdir: Conversation directory for saving
        output_type: Type of tool output (e.g., "shell", "python")
        command_info: Optional command that generated output
        original_tokens: Optional token count of original content

    Returns:
        Tuple of (summary_text, saved_path)
    """
    # Create output directory
    output_dir = logdir / "tool-outputs" / output_type
    output_dir.mkdir(parents=True, exist_ok=True)

    # Generate filename with timestamp and content hash
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    content_hash = hashlib.sha256(content.encode()).hexdigest()[:8]
    filename = f"{timestamp}-{content_hash}.txt"
    saved_path = output_dir / filename

    # Save content to file
    saved_path.write_text(content)
    logger.info(f"Saved large output to {saved_path}")

    # Create summary text
    summary_parts = [f"[Large {output_type} output saved]"]

    if command_info:
        summary_parts.append(f"Command: {command_info}")

    if original_tokens:
        summary_parts.append(f"Original: {original_tokens} tokens")

    summary_parts.append(f"Full output: {saved_path}")

    summary_text = "\n".join(summary_parts)

    return (summary_text, saved_path)


def create_tool_result_summary(
    content: str,
    original_tokens: int,
    logdir: Path | None,
    tool_name: str = "tool",
) -> str:
    """
    Create a summary of a large tool result, with content saved to file.

    This is used by autocompact to summarize massive tool outputs.

    Args:
        content: Full tool result content