Repository navigation
Expand file tree
/
Copy pathreport.py
More file actions
43 lines (37 loc) · 2.4 KB
/
Copy pathreport.py
File metadata and controls
43 lines (37 loc) · 2.4 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
import asyncio
import sys
from urllib.parse import urlsplit
from copilot import CopilotClient
from copilot.session_events import AssistantMessageData, AssistantMessageDeltaData, SessionErrorData, SessionIdleData, ToolExecutionCompleteData, ToolExecutionStartData
from workshop import accessibility_rule_lookup, create_snapshot_reader, permission_for_target, report_prompt
async def main() -> None:
target = sys.argv[1] if len(sys.argv) == 2 else input("Enter URL to analyze: ").strip()
target = target if "://" in target else f"https://{target}"
if urlsplit(target).scheme not in {"http", "https"}:
raise ValueError("Enter an absolute HTTP or HTTPS URL.")
async with CopilotClient() as client:
async with await client.create_session(streaming=True, on_permission_request=permission_for_target(target), tools=[accessibility_rule_lookup, create_snapshot_reader(".")], available_tools=["accessibility_rule_lookup", "read_latest_accessibility_snapshot", "playwright-browser_navigate"], mcp_servers={"playwright": {"command": "npx", "args": ["-y", "@playwright/mcp@0.0.78", "--browser=msedge", "--output-dir", ".playwright-mcp", "--output-mode", "file"], "working_directory": ".", "tools": ["browser_navigate"]}}) as session:
done = asyncio.Event()
error: RuntimeError | None = None
received_delta = False
def on_event(event) -> None:
nonlocal error, received_delta
match event.data:
case AssistantMessageDeltaData(delta_content=delta) if delta:
received_delta = True
print(delta, end="", flush=True)
case AssistantMessageData(content=content) if content and not received_delta:
print(content)
case ToolExecutionStartData(tool_name=name): print(f"\n[tool:start] {name}")
case ToolExecutionCompleteData(success=success): print(f"[tool:done] success={success}")
case SessionErrorData(message=message):
error = RuntimeError(message)
done.set()
case SessionIdleData(): done.set()
session.on(on_event)
await session.send(report_prompt(target))
await done.wait()
if error is not None:
raise error
if __name__ == "__main__":
asyncio.run(main())