Files
ai-agent-book/chapter3/user-memory/memory_operation_formatter.py
liqiang b119135836
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
ai-agent-book 精选快照(<2MB 代码与文档,来自 github.com/bojieli/ai-agent-book)
2026-08-20 13:12:50 +00:00

143 lines
3.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Formatter for memory operations output
Provides consistent formatting for memory operation lists
"""
from typing import List, Dict, Any
import json
def format_memory_operations(operations: List[Dict[str, Any]], verbose: bool = False) -> str:
"""
Format memory operations for display
Args:
operations: List of memory operations
verbose: Whether to show detailed output
Returns:
Formatted string representation of operations
"""
if not operations:
return "📝 Memory Operations: None (no updates needed)"
lines = []
lines.append(f"📝 Memory Operations ({len(operations)} total):")
lines.append("-" * 50)
for i, op in enumerate(operations, 1):
# Choose icon based on action
icon_map = {
'add': '',
'update': '📝',
'delete': '🗑️'
}
action = str(op.get('action') or 'unknown').lower()
icon = icon_map.get(action, '❓')
# Main operation line
lines.append(f"{i}. {icon} {action.upper()}")
if op.get('memory_id'):
lines.append(f" Memory ID: {op['memory_id']}")
if op.get('content'):
content = op['content']
# Truncate if too long and not verbose
if not verbose and len(content) > 100:
content = content[:97] + "..."
lines.append(f" Content: {content}")
# Reason
if op.get('reason'):
lines.append(f" Reason: {op['reason']}")
# Tags
if op.get('tags'):
lines.append(f" Tags: {', '.join(op['tags'])}")
lines.append("") # Empty line between operations
return "\n".join(lines)
def format_operation_summary(summary: Dict[str, int]) -> str:
"""
Format operation summary statistics
Args:
summary: Dictionary with counts of operations
Returns:
Formatted summary string
"""
added = summary.get('added', 0)
updated = summary.get('updated', 0)
deleted = summary.get('deleted', 0)
failed = summary.get('failed', 0)
parts = []
if added > 0:
parts.append(f"{added} added")
if updated > 0:
parts.append(f"{updated} updated")
if deleted > 0:
parts.append(f"{deleted} deleted")
if failed > 0:
parts.append(f"{failed} failed")
if not parts:
return "No operations performed"
return "Summary: " + ", ".join(parts)
def display_memory_operations(results: Dict[str, Any], verbose: bool = False):
"""
Display memory operations from processing results
Args:
results: Processing results containing operations
verbose: Whether to show detailed output
"""
operations = results.get('operations', [])
summary = results.get('summary', {})
# Display operations
print(format_memory_operations(operations, verbose))
# Display summary
print(format_operation_summary(summary))
print("-" * 50)
def operations_to_json(operations: List[Dict[str, Any]], pretty: bool = True) -> str:
"""
Convert operations to JSON string
Args:
operations: List of memory operations
pretty: Whether to pretty-print the JSON
Returns:
JSON string representation
"""
if pretty:
return json.dumps(operations, indent=2, ensure_ascii=False)
else:
return json.dumps(operations, ensure_ascii=False)
def filter_operations_by_action(operations: List[Dict[str, Any]], action: str) -> List[Dict[str, Any]]:
"""
Filter operations by action type
Args:
operations: List of memory operations
action: Action type to filter ('add', 'update', 'delete')
Returns:
Filtered list of operations
"""
return [op for op in operations if op.get('action') == action]