browser-agent-protocol 0.1.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,149 @@
1
+ """
2
+ Command-line interface for BAP Python SDK.
3
+
4
+ Provides utilities for connecting to BAP servers and testing connectivity.
5
+ """
6
+
7
+ import argparse
8
+ import asyncio
9
+ import json
10
+ import sys
11
+ from typing import Any
12
+
13
+ from browseragentprotocol import __version__, BAPClient
14
+
15
+
16
+ def main() -> None:
17
+ """Main entry point for the BAP CLI."""
18
+ parser = argparse.ArgumentParser(
19
+ prog="bap",
20
+ description="Browser Agent Protocol (BAP) Python SDK CLI",
21
+ )
22
+ parser.add_argument(
23
+ "--version",
24
+ action="version",
25
+ version=f"browseragentprotocol {__version__}",
26
+ )
27
+
28
+ subparsers = parser.add_subparsers(dest="command", help="Available commands")
29
+
30
+ # Connect command
31
+ connect_parser = subparsers.add_parser(
32
+ "connect",
33
+ help="Test connection to a BAP server",
34
+ )
35
+ connect_parser.add_argument(
36
+ "url",
37
+ help="WebSocket URL of the BAP server (e.g., ws://localhost:9222)",
38
+ )
39
+ connect_parser.add_argument(
40
+ "--token",
41
+ help="Authentication token",
42
+ )
43
+ connect_parser.add_argument(
44
+ "--timeout",
45
+ type=float,
46
+ default=10.0,
47
+ help="Connection timeout in seconds (default: 10)",
48
+ )
49
+
50
+ # Info command
51
+ info_parser = subparsers.add_parser(
52
+ "info",
53
+ help="Get server info and capabilities",
54
+ )
55
+ info_parser.add_argument(
56
+ "url",
57
+ help="WebSocket URL of the BAP server",
58
+ )
59
+ info_parser.add_argument(
60
+ "--token",
61
+ help="Authentication token",
62
+ )
63
+ info_parser.add_argument(
64
+ "--json",
65
+ action="store_true",
66
+ dest="json_output",
67
+ help="Output as JSON",
68
+ )
69
+
70
+ # Version command (just prints version)
71
+ subparsers.add_parser(
72
+ "version",
73
+ help="Show version information",
74
+ )
75
+
76
+ args = parser.parse_args()
77
+
78
+ if args.command is None:
79
+ parser.print_help()
80
+ sys.exit(0)
81
+
82
+ if args.command == "version":
83
+ print(f"browseragentprotocol {__version__}")
84
+ sys.exit(0)
85
+
86
+ if args.command == "connect":
87
+ asyncio.run(connect_command(args.url, args.token, args.timeout))
88
+ elif args.command == "info":
89
+ asyncio.run(info_command(args.url, args.token, args.json_output))
90
+
91
+
92
+ async def connect_command(url: str, token: str | None, timeout: float) -> None:
93
+ """Test connection to a BAP server."""
94
+ print(f"Connecting to {url}...")
95
+
96
+ try:
97
+ client = BAPClient(url, token=token, timeout=timeout)
98
+ result = await client.connect()
99
+ print(f"Connected successfully!")
100
+ print(f" Protocol version: {result.protocol_version}")
101
+ print(f" Server: {result.server_info.name} v{result.server_info.version}")
102
+ await client.close()
103
+ except Exception as e:
104
+ print(f"Connection failed: {e}", file=sys.stderr)
105
+ sys.exit(1)
106
+
107
+
108
+ async def info_command(url: str, token: str | None, json_output: bool) -> None:
109
+ """Get server info and capabilities."""
110
+ try:
111
+ client = BAPClient(url, token=token, timeout=10.0)
112
+ result = await client.connect()
113
+
114
+ if json_output:
115
+ info: dict[str, Any] = {
116
+ "protocolVersion": result.protocol_version,
117
+ "serverInfo": {
118
+ "name": result.server_info.name,
119
+ "version": result.server_info.version,
120
+ },
121
+ }
122
+ if result.capabilities:
123
+ info["capabilities"] = result.capabilities.model_dump(
124
+ by_alias=True, exclude_none=True
125
+ )
126
+ print(json.dumps(info, indent=2))
127
+ else:
128
+ print(f"BAP Server Information")
129
+ print(f"=" * 40)
130
+ print(f"Protocol Version: {result.protocol_version}")
131
+ print(f"Server Name: {result.server_info.name}")
132
+ print(f"Server Version: {result.server_info.version}")
133
+ if result.capabilities:
134
+ print(f"\nCapabilities:")
135
+ caps = result.capabilities.model_dump(by_alias=True, exclude_none=True)
136
+ for key, value in caps.items():
137
+ print(f" {key}: {value}")
138
+
139
+ await client.close()
140
+ except Exception as e:
141
+ if json_output:
142
+ print(json.dumps({"error": str(e)}), file=sys.stderr)
143
+ else:
144
+ print(f"Error: {e}", file=sys.stderr)
145
+ sys.exit(1)
146
+
147
+
148
+ if __name__ == "__main__":
149
+ main()