Skip to content

Commit

Permalink
feat: add docs to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj319 committed Oct 28, 2024
1 parent 4978f9a commit 658e06a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
6 changes: 6 additions & 0 deletions core/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type llmClient struct {
anthropicClient *anthropic.Client
}

// NewLlmClient creates a new LLM client based on the specified provider, model, and API key.
// The `provider` parameter specifies the LLM provider (options: "openai" or "anthropic").
// The `model` parameter is the name of the model to use for the completion (e.g., "gpt-4o").
// The `apiKey` is the API key associated with the chosen provider.
// Returns an initialized *llmClient or an error if validation or provider initialization fails.
func NewLlmClient(provider string, model string, apiKey string) (*llmClient, error) {
client := &llmClient{
apiKey: apiKey,
Expand All @@ -45,6 +50,7 @@ func NewLlmClient(provider string, model string, apiKey string) (*llmClient, err
return client, nil
}

// ChatCompletion sends a prompt to the LLM model and returns the completion string or and error.
func (c *llmClient) ChatCompletion(prompt string) (string, error) {
switch c.provider {
case "openai":
Expand Down
22 changes: 15 additions & 7 deletions core/locatr.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,18 @@ type BaseLocatr struct {
initilized bool
}

// BaseLocatrOptions is a struct that holds all the options for the locatr package
type BaseLocatrOptions struct {
// CachePath is the path to the cache file
CachePath string
UseCache bool
// UseCache is a flag to enable/disable cache
UseCache bool
}

// NewBaseLocatr creates a new instance of BaseLocatr
// plugin: (playwright, puppeteer, etc)
// llmClient: struct that are returned by NewLlmClient
// options: All the options for the locatr package
func NewBaseLocatr(plugin PluginInterface, llmClient LlmClient, options BaseLocatrOptions) *BaseLocatr {
if len(options.CachePath) == 0 {
options.CachePath = DEFAULT_CACHE_PATH
Expand Down Expand Up @@ -146,8 +153,9 @@ func writeLocatorsToCache(cachePath string, cacheString []byte) error {
return nil
}

func (l *BaseLocatr) GetLocatorStr(userReq string) (string, error) {
if err := l.plugin.EvaluateJsScript(HTML_MINIFIER_JS_CONTENTT); err != nil {
// getLocatorStr returns the locator string for the given user request
func (l *BaseLocatr) getLocatorStr(userReq string) (string, error) {
if err := l.plugin.evaluateJsScript(HTML_MINIFIER_JS_CONTENTT); err != nil {
return "", ErrUnableToLoadJsScripts
}
l.initilizeState()
Expand Down Expand Up @@ -200,20 +208,20 @@ func (l *BaseLocatr) GetLocatorStr(userReq string) (string, error) {

}
func (l *BaseLocatr) getCurrentUrl() string {
if value, err := l.plugin.EvaluateJsFunction("window.location.href"); err == nil {
if value, err := l.plugin.evaluateJsFunction("window.location.href"); err == nil {
return value
}
return ""
}

func (l *BaseLocatr) getMinifiedDomAndLocatorMap() (*ElementSpec, *IdToLocatorMap, error) {
result, _ := l.plugin.EvaluateJsFunction("minifyHTML()")
result, _ := l.plugin.evaluateJsFunction("minifyHTML()")
elementSpec := &ElementSpec{}
if err := json.Unmarshal([]byte(result), elementSpec); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal ElementSpec json: %v", err)
}

result, _ = l.plugin.EvaluateJsFunction("mapElementsToJson()")
result, _ = l.plugin.evaluateJsFunction("mapElementsToJson()")
idLocatorMap := &IdToLocatorMap{}
if err := json.Unmarshal([]byte(result), idLocatorMap); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal IdToLocatorMap json: %v", err)
Expand All @@ -224,7 +232,7 @@ func (l *BaseLocatr) getMinifiedDomAndLocatorMap() (*ElementSpec, *IdToLocatorMa

func (l *BaseLocatr) getValidLocator(locators []string) (string, error) {
for _, locator := range locators {
if value, _ := l.plugin.EvaluateJsFunction(fmt.Sprintf("isValidLocator('%s')", locator)); value == "true" {
if value, _ := l.plugin.evaluateJsFunction(fmt.Sprintf("isValidLocator('%s')", locator)); value == "true" {
return locator, nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/locatr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

type MockPlugin struct{}

func (m *MockPlugin) EvaluateJsFunction(js string) (string, error) {
func (m *MockPlugin) evaluateJsFunction(js string) (string, error) {
return "", nil
}

func (m *MockPlugin) EvaluateJsScript(js string) error {
func (m *MockPlugin) evaluateJsScript(js string) error {
return nil
}

Expand Down
10 changes: 7 additions & 3 deletions core/playwright.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type playwrightLocator struct {
locatr *BaseLocatr
}

// NewPlaywrightLocatr creates a new playwright locator. Use the returned struct methods to get locators.
func NewPlaywrightLocatr(page playwright.Page, llmClient LlmClient, options BaseLocatrOptions) *playwrightLocator {
pwPlugin := &playwrightPlugin{page: page}

Expand All @@ -25,7 +26,8 @@ func NewPlaywrightLocatr(page playwright.Page, llmClient LlmClient, options Base
}
}

func (pl *playwrightPlugin) EvaluateJsFunction(function string) (string, error) {
// evaluateJsFunction runs the given javascript function in the browser and returns the result as a string.
func (pl *playwrightPlugin) evaluateJsFunction(function string) (string, error) {
result, err := pl.page.Evaluate(function)
if err != nil {
return "", fmt.Errorf("error evaluating js function: %v", err)
Expand All @@ -46,19 +48,21 @@ func (pl *playwrightPlugin) EvaluateJsFunction(function string) (string, error)
return "", fmt.Errorf("error evaluating js function: result is not string, number or boolean")
}

func (pl *playwrightPlugin) EvaluateJsScript(scriptContent string) error {
// evaluateJsScript runs the given javascript script in the browser.
func (pl *playwrightPlugin) evaluateJsScript(scriptContent string) error {
if _, err := pl.page.Evaluate(string(scriptContent)); err != nil {
return fmt.Errorf("error evaluating js script: %v", err)
}
return nil
}

// GetLocatr returns a pywright locator object for the given user request.
func (pl *playwrightLocator) GetLocatr(userReq string) (playwright.Locator, error) {
if err := pl.page.WaitForLoadState(playwright.PageWaitForLoadStateOptions{State: playwright.LoadStateDomcontentloaded}); err != nil {
return nil, fmt.Errorf("error waiting for load state: %v", err)
}

locatorStr, err := pl.locatr.GetLocatorStr(userReq)
locatorStr, err := pl.locatr.getLocatorStr(userReq)
if err != nil {
return nil, fmt.Errorf("error getting locator string: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package core

type PluginInterface interface {
EvaluateJsScript(scriptContent string) error
EvaluateJsFunction(function string) (string, error)
evaluateJsScript(scriptContent string) error
evaluateJsFunction(function string) (string, error)
}

0 comments on commit 658e06a

Please sign in to comment.