From 658e06a8e541efbb711ac66f7e2ed174651a858b Mon Sep 17 00:00:00 2001 From: Neeraj310 Date: Mon, 28 Oct 2024 10:12:56 +0545 Subject: [PATCH] feat: add docs to functions --- core/llm.go | 6 ++++++ core/locatr.go | 22 +++++++++++++++------- core/locatr_test.go | 4 ++-- core/playwright.go | 10 +++++++--- core/plugin.go | 4 ++-- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/core/llm.go b/core/llm.go index c22beb4..b0a825e 100644 --- a/core/llm.go +++ b/core/llm.go @@ -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, @@ -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": diff --git a/core/locatr.go b/core/locatr.go index 8754c3e..7d08f05 100644 --- a/core/locatr.go +++ b/core/locatr.go @@ -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 @@ -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() @@ -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) @@ -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 } } diff --git a/core/locatr_test.go b/core/locatr_test.go index 2293285..2fd81c3 100644 --- a/core/locatr_test.go +++ b/core/locatr_test.go @@ -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 } diff --git a/core/playwright.go b/core/playwright.go index b79dfe5..4885121 100644 --- a/core/playwright.go +++ b/core/playwright.go @@ -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} @@ -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) @@ -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) } diff --git a/core/plugin.go b/core/plugin.go index d539ff5..67904f7 100644 --- a/core/plugin.go +++ b/core/plugin.go @@ -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) }