Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions examples/agentic_mouse_action.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import asyncio

from narada import Narada
from narada import Agent, BrowserEnvironment


async def main() -> None:
async with Narada() as narada:
window = await narada.open_and_initialize_browser_window()
env = BrowserEnvironment()
agent = Agent(environment=env)

await window.go_to_url(url="https://www.google.com")
await window.agentic_mouse_action(
try:
await agent.go_to_url(url="https://www.google.com")
await agent.agentic_mouse_action(
action={"type": "click"},
recorded_click={
"x": 500,
Expand All @@ -21,7 +22,7 @@ async def main() -> None:
fallback_operator_query="click on the search box",
)

await window.agentic_mouse_action(
await agent.agentic_mouse_action(
action={"type": "fill", "text": "Narada AI", "press_enter": False},
recorded_click={
"x": 500,
Expand All @@ -34,7 +35,7 @@ async def main() -> None:
fallback_operator_query='type "Narada AI" in the search box',
)

await window.agentic_mouse_action(
await agent.agentic_mouse_action(
action={"type": "scroll", "horizontal": 0, "vertical": 500},
recorded_click={
"x": 640,
Expand All @@ -46,6 +47,8 @@ async def main() -> None:
},
fallback_operator_query="scroll down the page",
)
finally:
await env.close()


if __name__ == "__main__":
Expand Down
17 changes: 9 additions & 8 deletions examples/agentic_selector.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import asyncio

from narada import Narada
from narada import Agent, BrowserEnvironment


async def main() -> None:
# Initialize the Narada client.
async with Narada() as narada:
# Open a new browser window and initialize the Narada UI agent.
window = await narada.open_and_initialize_browser_window()
env = BrowserEnvironment()
agent = Agent(environment=env)

await window.go_to_url(url="https://www.google.com")
try:
await agent.go_to_url(url="https://www.google.com")

await window.agentic_selector(
await agent.agentic_selector(
action={"type": "fill", "value": "Narada AI"},
selectors={
"tag_name": "textarea",
Expand All @@ -20,13 +19,15 @@ async def main() -> None:
fallback_operator_query='type "Narada AI" in the search box',
)

await window.agentic_selector(
await agent.agentic_selector(
action={"type": "click"},
selectors={
"xpath": "/html/body/div[2]/div[4]/form/div[1]/div[1]/div[2]/div[4]/div[6]/center/input[1]",
},
fallback_operator_query="click on the Google Search button",
)
finally:
await env.close()


if __name__ == "__main__":
Expand Down
19 changes: 11 additions & 8 deletions examples/agentic_selector_get_property.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import asyncio

from narada import Narada
from narada import Agent, BrowserEnvironment


async def main() -> None:
async with Narada() as narada:
window = await narada.open_and_initialize_browser_window()
env = BrowserEnvironment()
agent = Agent(environment=env)

await window.go_to_url(url="https://app.narada.ai", timeout=60)
property_response = await window.agentic_selector(
try:
await agent.go_to_url(url="https://app.narada.ai", timeout=60)
property_response = await agent.agentic_selector(
action={"type": "get_property", "property_name": "className"},
selectors={"data_testid": "create-new-agent-button"},
fallback_operator_query="get className from create button",
timeout=60,
)
print(f"Class Name: {property_response['value']}")
print(f"Class Name: {property_response.value}")

print("\nTest 2: Getting text content...")
text_response = await window.agentic_selector(
text_response = await agent.agentic_selector(
action={"type": "get_text"},
selectors={"data_testid": "create-new-agent-button"},
fallback_operator_query="get text from create button",
timeout=60,
)
print(f"Text: {text_response['value']}")
print(f"Text: {text_response.value}")
finally:
await env.close()


if __name__ == "__main__":
Expand Down
26 changes: 13 additions & 13 deletions examples/attachment.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import asyncio
from pathlib import Path

from narada import Agent, Narada
from narada import Agent, AgentKind, BrowserEnvironment


async def main() -> None:
async with Narada() as narada:
window = await narada.open_and_initialize_browser_window()
env = BrowserEnvironment()
agent = Agent(environment=env, kind=AgentKind.CORE_AGENT)

# Upload a file to be later used as an attachment.
try:
# Pass a file-like object as an attachment. The SDK uploads it automatically
# before dispatching the request.
current_dir = Path(__file__).parent
with open(current_dir / "demo_attachment_file.txt") as f:
file = await window.upload_file(file=f)

# Ask the agent to use the attachment.
response = await window.agent(
prompt="Summarize the attached file.",
agent=Agent.CORE_AGENT,
attachment=file,
)
with open(current_dir / "demo_attachment_file.txt", "rb") as f:
response = await agent.run(
prompt="Summarize the attached file.",
attachment=f,
)

print("Response:", response.model_dump_json(indent=2))
finally:
await env.close()


if __name__ == "__main__":
Expand Down
13 changes: 7 additions & 6 deletions examples/choose_agent.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import asyncio

from narada import Agent, Narada
from narada import Agent, AgentKind, BrowserEnvironment


async def main() -> None:
# Initialize the Narada client.
async with Narada() as narada:
# Open a new browser window and initialize the Narada UI agent.
window = await narada.open_and_initialize_browser_window()
env = BrowserEnvironment()
agent = Agent(environment=env, kind=AgentKind.CORE_AGENT)

try:
# Choose a specific agent to handle the task. By default, the Operator agent is used.
response = await window.agent(prompt="Tell me a joke.", agent=Agent.CORE_AGENT)
response = await agent.run(prompt="Tell me a joke.")

print("Response:", response.model_dump_json(indent=2))
finally:
await env.close()


if __name__ == "__main__":
Expand Down
87 changes: 44 additions & 43 deletions examples/cloud_browser.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,59 @@
import asyncio

from narada import Narada
from narada.window import RemoteBrowserWindow
from narada import Agent, CloudBrowserEnvironment, RemoteBrowserEnvironment


async def main() -> None:
# Initialize the Narada client.
async with Narada() as narada:
# Open a cloud browser window and initialize the Narada UI agent.
window = await narada.open_and_initialize_cloud_browser_window(
session_name="my-cloud-browser-session", # Optional: label the session
session_timeout=3600, # Optional: session timeout in seconds
)
# Create a cloud browser environment. It initializes lazily on the first action.
env = CloudBrowserEnvironment(
session_name="my-cloud-browser-session", # Optional: label the session
session_timeout=3600, # Optional: session timeout in seconds
)
agent = Agent(environment=env)

# Run a task in this browser window.
response = await window.agent(
prompt=(
'Search for "LLM Compiler" on Google and open the first arXiv paper on the results '
"page, then tell me who the authors are."
cloud_browser_session_id = None
browser_window_id = None

try:
# Run a task in this cloud browser.
response = await agent.run(
prompt=(
'Search for "LLM Compiler" on Google and open the first arXiv paper on the results '
"page, then tell me who the authors are."
)
)
)

print("Response:", response.model_dump_json(indent=2))
print("Response:", response.model_dump_json(indent=2))

# The cloud session is still running after exiting the context manager.
# You can save the session ID for later reconnection or management.
cloud_browser_session_id = window.cloud_browser_session_id
browser_window_id = window.browser_window_id
# The cloud session keeps running until explicitly stopped or it times out.
# Save these IDs for later reconnection or management.
cloud_browser_session_id = env.cloud_browser_session_id
browser_window_id = env.browser_window_id

# Change these to test the different options below.
stop_session_now = False
# Get files downloaded during the session.
downloaded_files = await env.get_downloaded_files()
print(f"Downloaded files {downloaded_files}")

# The cloud session runs independently. If you want to stop it after the task is
# complete, you can explicitly close it. The session will also auto-expire after the
# configured session_timeout.
if stop_session_now:
print(
f"Stopping cloud session {cloud_browser_session_id} through original window"
)
await window.close()
else:
# Create a `RemoteBrowserWindow` instance with the session ID to manage the session later.
print(
f"Stopping cloud session {cloud_browser_session_id} through RemoteBrowserWindow"
)
remote_window = RemoteBrowserWindow(
cloud_browser_session_id=cloud_browser_session_id,
browser_window_id=browser_window_id,
)
await remote_window.close() # This will stop the cloud session.
finally:
# Change this to test stopping through the original environment versus
# reconnecting with a remote environment.
stop_session_through_original_environment = False

# Get files downloaded during the session
downloaded_files = await window.get_downloaded_files()
print(f"Downloaded files {downloaded_files}")
if cloud_browser_session_id is None or browser_window_id is None:
await env.close()
elif stop_session_through_original_environment:
print(f"Stopping cloud session {cloud_browser_session_id}")
await env.close()
else:
# Create a RemoteBrowserEnvironment with the session ID to manage the session later.
print(
f"Stopping cloud session {cloud_browser_session_id} through RemoteBrowserEnvironment"
)
remote_env = RemoteBrowserEnvironment(
cloud_browser_session_id=cloud_browser_session_id,
browser_window_id=browser_window_id,
)
await remote_env.close() # This will stop the cloud session.

############################################################################
# IMPORTANT: The cloud browser continues accruing costs until the session #
Expand Down
23 changes: 12 additions & 11 deletions examples/complex_workflow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio

from narada import Agent, Narada
from narada import Agent, AgentKind, BrowserEnvironment
from pydantic import BaseModel


Expand All @@ -14,16 +14,15 @@ class Papers(BaseModel):


async def main() -> None:
# Initialize the Narada client.
async with Narada() as narada:
# Open a new browser window and initialize the Narada UI agent.
window = await narada.open_and_initialize_browser_window()
env = BrowserEnvironment()
core_agent = Agent(environment=env, kind=AgentKind.CORE_AGENT)
operator = Agent(environment=env)

await window.go_to_url(url="https://arxiv.org/list/cs.AI/recent")
try:
await core_agent.go_to_url(url="https://arxiv.org/list/cs.AI/recent")

resp = await window.agent(
resp = await core_agent.run(
prompt="What are the top 2 AI papers based on the current page?",
agent=Agent.CORE_AGENT,
output_schema=Papers,
)

Expand All @@ -33,10 +32,12 @@ async def main() -> None:
print("Top 2 AI papers:", papers.model_dump_json(indent=2))

for paper in papers.papers:
await window.go_to_url(url=paper.url)
await window.agent(prompt="Click 'View PDF' then download the PDF")
await operator.go_to_url(url=paper.url)
await operator.run(prompt="Click 'View PDF' then download the PDF")

await window.print_message(message="All done!")
await operator.print_message(message="All done!")
finally:
await env.close()


if __name__ == "__main__":
Expand Down
30 changes: 12 additions & 18 deletions examples/conversation.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
import asyncio

from narada import Agent, Narada
from narada import Agent, AgentKind, BrowserEnvironment


async def main() -> None:
# Initialize the Narada client.
async with Narada() as narada:
# Open a new browser window and initialize the Narada UI agent.
window = await narada.open_and_initialize_browser_window()
env = BrowserEnvironment()
agent = Agent(environment=env, kind=AgentKind.CORE_AGENT)

resp = await window.agent(
try:
resp = await agent.run(
prompt="Pick a lucky number for me between 1 and 100",
agent=Agent.CORE_AGENT,
# By default, the chat history is cleared when an agent is invoked so that the agent can
# start fresh.
clear_chat=True,
)
print(resp.text)

resp = await window.agent(
resp = await agent.run(
prompt="What did you pick again?",
agent=Agent.CORE_AGENT,
# By not clearing the chat history, we can continue the conversation.
clear_chat=False,
# Pass the previous request ID to continue from the earlier response.
previous_request_id=resp.request_id,
)
print(resp.text)

resp = await window.agent(
resp = await agent.run(
prompt="What's double that number?",
agent=Agent.CORE_AGENT,
# By not clearing the chat history, we can continue the conversation.
clear_chat=False,
previous_request_id=resp.request_id,
)
print(resp.text)
finally:
await env.close()


if __name__ == "__main__":
Expand Down
Loading
Loading