-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbrowse_cmd.py
More file actions
114 lines (93 loc) · 4.22 KB
/
browse_cmd.py
File metadata and controls
114 lines (93 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
Browse command for CLI.
"""
import asyncio
from typing import Annotated
import typer
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TextColumn
from ra_mcp_browse_lib.browse_operations import BrowseOperations
from ra_mcp_common.http_client import get_http_client
from ra_mcp_common.telemetry import get_tracer
from .formatter import RichConsoleFormatter
_tracer = get_tracer("ra_mcp.cli.browse")
console = Console()
def _print_renderables(renderables, console: Console) -> None:
for item in renderables:
console.print(item)
def browse(
reference_code: Annotated[str, typer.Argument(help="Document reference code from search results (e.g., 'SE/RA/420422/01')")],
pages: Annotated[
str | None,
typer.Option(help='Page specification: single ("5"), range ("1-10"), or list ("5,7,9"). Alias: --page'),
] = None,
page: Annotated[
str | None,
typer.Option(help='Page specification: single ("5"), range ("1-10"), or list ("5,7,9"). Shorthand for --pages'),
] = None,
search_term: Annotated[str | None, typer.Option("--search-term", help="Highlight keyword in transcribed text (case-insensitive)")] = None,
max_display: Annotated[int, typer.Option("--max-display", help="Maximum number of pages to display in output")] = 20,
log: Annotated[bool, typer.Option("--log", help="Enable detailed API request/response logging to ra_mcp_api.log file")] = False,
show_links: Annotated[
bool,
typer.Option("--show-links", help="Display direct links to ALTO XML, IIIF images, and Bildvisaren viewer"),
] = False,
) -> None:
"""Browse pages by reference code.
You can specify pages using either --pages or --page (they work the same way).
If both are provided, --page takes precedence.
Examples:
ra browse "SE/RA/123" --page 5
ra browse "SE/RA/123" --pages "1-10"
ra browse "SE/RA/123" --page "5,7,9"
ra browse "SE/RA/123" --page 1 --log # With API logging
"""
http_client = get_http_client(log)
browse_operations = BrowseOperations(http_client=http_client)
formatter = RichConsoleFormatter(console)
console.print(f"\n[bold cyan]📖 Browsing document: {reference_code}[/bold cyan]\n")
if log:
console.print("[dim]API logging enabled - check ra_mcp_api.log[/dim]")
requested_pages = page if page is not None else pages
with _tracer.start_as_current_span(
"cli.browse",
attributes={
"browse.reference_code": reference_code,
"browse.pages": requested_pages or "1-20",
},
):
try:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
progress.add_task("Loading document...", total=None)
browse_result = asyncio.run(
browse_operations.browse_document(
reference_code=reference_code,
pages=requested_pages or "1-20",
highlight_term=search_term,
max_pages=max_display,
)
)
if not browse_result.contexts and not browse_result.oai_metadata:
console.print(f"[yellow]No pages found for '{reference_code}'[/yellow]")
console.print("\n[dim]Suggestions:[/dim]")
console.print("[dim]- Check the reference code format[/dim]")
console.print("[dim]- Verify the document has transcribed pages[/dim]")
console.print("[dim]- Try different page numbers[/dim]")
raise typer.Exit(code=1)
has_pages = bool(browse_result.contexts)
renderables = formatter.format_browse_results(
browse_result,
highlight_term=search_term,
show_links=show_links,
show_success_message=has_pages,
)
_print_renderables(renderables, console)
except typer.Exit:
raise
except Exception as error:
console.print(f"[red]Browse failed: {error}[/red]")
raise typer.Exit(code=1) from error