77from invariant_sdk .client import Client as InvariantClient
88from dotenv import load_dotenv
99import sys
10+ import os
1011load_dotenv ()
1112
1213anthropic_client = Anthropic ()
13- invariant_client = InvariantClient ()
14+ invariant_client = InvariantClient () if "INVARIANT_API_KEY" in os . environ else None
1415
1516async def run (playwright : Playwright , prompt : str ):
1617 browser = await playwright .firefox .launch (headless = False )
1718 context = await browser .new_context ()
1819 page = await context .new_page ()
20+ await page .set_viewport_size ({"width" : 1024 , "height" : 768 }) # Computer-use default
1921 await page .goto ("https://www.google.com" )
2022 playwright_tools = PlaywrightToolbox (page , use_cursor = True )
2123 messages = await sampling_loop (
@@ -27,12 +29,15 @@ async def run(playwright: Playwright, prompt: str):
2729 verbose = True
2830 )
2931 print (messages [- 1 ]["content" ][0 ]["text" ])
30- response = invariant_client .create_request_and_push_trace (
31- messages = [anthropic_to_invariant (messages )],
32- dataset = "playwright_computer_use_trace"
33- )
34- url = f"{ invariant_client .api_url } /trace/{ response .id [0 ]} "
35- print (f"View the trace at { url } " )
32+ if invariant_client is not None :
33+ response = invariant_client .create_request_and_push_trace (
34+ messages = [anthropic_to_invariant (messages )],
35+ dataset = "playwright_computer_use_trace"
36+ )
37+ url = f"{ invariant_client .api_url } /trace/{ response .id [0 ]} "
38+ print (f"View the trace at { url } " )
39+ else :
40+ print ("No INVARIANT_API_KEY found. Add it to your .env file to push the trace to Invariant explorer https://explorer.invariantlabs.ai." )
3641 await browser .close ()
3742
3843prompt = sys .argv [1 ]
0 commit comments