Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

browser automation support #668

Merged
merged 8 commits into from
Aug 28, 2024
186 changes: 184 additions & 2 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 23 additions & 11 deletions docs/src/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
Extension](/genaiscript/getting-started/installation/) to get started.
</Card>
<Card title="Configure your LLMs" icon="setting">
Configure the [secrets](/genaiscript/getting-started/configuration) to access your
LLMs.
Configure the [secrets](/genaiscript/getting-started/configuration) to
access your LLMs.
</Card>
<Card title="Write your first script" icon="pencil">
Follow [Getting
Expand Down Expand Up @@ -190,9 +190,19 @@
Grep or fuzz search [files](/genaiscript/referen/script/files)

```js wrap
const { files } = await workspace.grep(
/[a-z][a-z0-9]+/,
"**/*.md")
const { files } = await workspace.grep(/[a-z][a-z0-9]+/, "**/*.md")

Check warning on line 193 in docs/src/content/docs/index.mdx

View workflow job for this annotation

GitHub Actions / build

There seems to be a typo in the word "weahter". It should be "weather".

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a typo in the word "weahter". It should be "weather".

generated by pr-docs-review-commit typo

```

</Card>

<Card title="Browser automation" icon="document">

Browse and scrape the web with [Playwright](/genaiscript/reference/scripts/browse).

```js
const page = await host.browse("https://...")
const table = await page.locator("table[...]").innerHTML()
def("TABLE", HTML.convertToMarkdown(table))
```

</Card>
Expand Down Expand Up @@ -233,17 +243,19 @@
<Card title="LLM Tools" icon="setting">

Register JavaScript functions as [LLM tools](/genaiscript/reference/scripts/tools/)

```js wrap
defTool("weather", "live weahter",
{ city: "Paris" }, // schema
async ({ city }) => // callback
defTool("weather", "live weahter",

Check warning on line 248 in docs/src/content/docs/index.mdx

View workflow job for this annotation

GitHub Actions / build

There seems to be a typo in the word "weahter". It should be "weather".

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a typo in the word "weahter". It should be "weather".

generated by pr-docs-review-commit typo

{ city: "Paris" }, // schema
async ({ city }) => // callback
{ ... "sunny" }
)
```

or use built-in [@agentic tools](/genaiscript/guides/agentic-tools/)

```js wrap
import { WeatherClient }
from "@agentic/weather"
import { WeatherClient } from "@agentic/weather"
defTool(new WeatherClient())
```

Expand All @@ -254,7 +266,7 @@
Let the LLM run code in a sandboxed execution environment.

```js wrap
script({ tools: ["python_code_interpreter"]})
script({ tools: ["python_code_interpreter"] })
```

</Card>
Expand Down
43 changes: 43 additions & 0 deletions docs/src/content/docs/reference/scripts/browse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: Browse
sidebar:
order: 30
---

GenAIScript provides a simplified API to interact with a headless browser using [Playwright](https://playwright.dev/) .
This allows you to interact with web pages, scrape data, and automate tasks.

```js
const page = await host.browse(
"https://github.com/microsoft/genaiscript/blob/main/packages/sample/src/penguins.csv"
)
const table = page.locator('table[data-testid="csv-table"]')
const csv = parsers.HTMLToMarkdown(await table.innerHTML())
def("DATA", csv)
$`Analyze DATA.`
```

# `host.browse`

This function launches a new browser instance and optionally navigates to the page.

```js
const page = await host.browse(url)
```

You can configure a number of options for the browser instance:

```js
const page = await host.browse(url, { incognito: true })
```

## (Advanced) Native Playwright APIs

The `page` instance returned is a native [Playwright Page](https://playwright.dev/docs/api/class-page) object.
You can import `playwright` and case the instance back to the native playwright object.

```js
import { Page } from "playwright"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a typo here. The correct word should be "cast" not "case".

generated by pr-docs-review-commit typo

const page = await host.browse(url) as Page
```

Check notice on line 43 in docs/src/content/docs/reference/scripts/browse.md

View workflow job for this annotation

GitHub Actions / build

A new file "browse.md" has been added in the "scripts" reference section. This file provides documentation for interacting with a headless browser using Playwright.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new file "browse.md" has been added in the "scripts" reference section. This file provides documentation for interacting with a headless browser using Playwright.

generated by pr-docs-review-commit new_file

Loading
Loading