-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(tools/looker-query-url): Add looker-query-url tool #1015
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --- | ||
| title: "looker-query-url" | ||
| type: docs | ||
| weight: 1 | ||
| description: > | ||
| "looker-query-url" generates a url link to a Looker explore. | ||
| aliases: | ||
| - /resources/tools/looker-query-url | ||
| --- | ||
|
|
||
| ## About | ||
|
|
||
| The `looker-query-url` generates a url link to an explore in | ||
| Looker so the query can be investigated further. | ||
|
|
||
| It's compatible with the following sources: | ||
|
|
||
| - [looker](../../sources/looker.md) | ||
|
|
||
| `looker-query-url` takes eight parameters: | ||
|
|
||
| 1. the `model` | ||
| 2. the `explore` | ||
| 3. the `fields` list | ||
| 4. an optional set of `filters` | ||
| 5. an optional set of `pivots` | ||
| 6. an optional set of `sorts` | ||
| 7. an optional `limit` | ||
| 8. an optional `tz` | ||
|
|
||
| ## Example | ||
|
|
||
| ```yaml | ||
| tools: | ||
| query_url: | ||
| kind: looker-query-url | ||
| source: looker-source | ||
| description: | | ||
| Query URL Tool | ||
|
|
||
| This tool is used to generate the URL of a query in Looker. | ||
| The user can then explore the query further inside Looker. | ||
| The tool also returns the query_id and slug. The parameters | ||
| are the same as the `looker-query` tool. | ||
| ``` | ||
|
|
||
| ## Reference | ||
|
|
||
| | **field** | **type** | **required** | **description** | | ||
| |-------------|:------------------------------------------:|:------------:|--------------------------------------------------------------------------------------------------| | ||
| | kind | string | true | Must be "looker-query-url" | | ||
| | source | string | true | Name of the source the SQL should execute on. | | ||
| | description | string | true | Description of the tool that is passed to the LLM. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| package lookerqueryurl | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| yaml "github.com/goccy/go-yaml" | ||
| "github.com/googleapis/genai-toolbox/internal/sources" | ||
| lookersrc "github.com/googleapis/genai-toolbox/internal/sources/looker" | ||
| "github.com/googleapis/genai-toolbox/internal/tools" | ||
| "github.com/googleapis/genai-toolbox/internal/util" | ||
|
|
||
| "github.com/looker-open-source/sdk-codegen/go/rtl" | ||
| v4 "github.com/looker-open-source/sdk-codegen/go/sdk/v4" | ||
|
|
||
| "github.com/thlib/go-timezone-local/tzlocal" | ||
| ) | ||
|
|
||
| const kind string = "looker-query-url" | ||
|
|
||
| func init() { | ||
| if !tools.Register(kind, newConfig) { | ||
| panic(fmt.Sprintf("tool kind %q already registered", kind)) | ||
| } | ||
| } | ||
|
|
||
| func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (tools.ToolConfig, error) { | ||
| actual := Config{Name: name} | ||
| if err := decoder.DecodeContext(ctx, &actual); err != nil { | ||
| return nil, err | ||
| } | ||
| return actual, nil | ||
| } | ||
|
|
||
| type Config struct { | ||
| Name string `yaml:"name" validate:"required"` | ||
| Kind string `yaml:"kind" validate:"required"` | ||
| Source string `yaml:"source" validate:"required"` | ||
| Description string `yaml:"description" validate:"required"` | ||
| AuthRequired []string `yaml:"authRequired"` | ||
| } | ||
|
|
||
| // validate interface | ||
| var _ tools.ToolConfig = Config{} | ||
|
|
||
| func (cfg Config) ToolConfigKind() string { | ||
| return kind | ||
| } | ||
|
|
||
| func (cfg Config) Initialize(srcs map[string]sources.Source) (tools.Tool, error) { | ||
| // verify source exists | ||
| rawS, ok := srcs[cfg.Source] | ||
| if !ok { | ||
| return nil, fmt.Errorf("no source named %q configured", cfg.Source) | ||
| } | ||
|
|
||
| // verify the source is compatible | ||
| s, ok := rawS.(*lookersrc.Source) | ||
| if !ok { | ||
| return nil, fmt.Errorf("invalid source for %q tool: source kind must be `looker`", kind) | ||
| } | ||
|
|
||
| modelParameter := tools.NewStringParameter("model", "The model containing the explore.") | ||
| exploreParameter := tools.NewStringParameter("explore", "The explore to be queried.") | ||
| fieldsParameter := tools.NewArrayParameter("fields", | ||
| "The fields to be retrieved.", | ||
| tools.NewStringParameter("field", "A field to be returned in the query"), | ||
| ) | ||
| filtersParameter := tools.NewMapParameterWithDefault("filters", | ||
| map[string]any{}, | ||
| "The filters for the query", | ||
| "", | ||
| ) | ||
| pivotsParameter := tools.NewArrayParameterWithDefault("pivots", | ||
| []any{}, | ||
| "The query pivots (must be included in fields as well).", | ||
| tools.NewStringParameter("pivot_field", "A field to be used as a pivot in the query"), | ||
| ) | ||
| sortsParameter := tools.NewArrayParameterWithDefault("sorts", | ||
| []any{}, | ||
| "The sorts like \"field.id desc 0\".", | ||
| tools.NewStringParameter("sort_field", "A field to be used as a sort in the query"), | ||
| ) | ||
| limitParameter := tools.NewIntParameterWithDefault("limit", 500, "The row limit.") | ||
| tzParameter := tools.NewStringParameterWithRequired("tz", "The query timezone.", false) | ||
|
|
||
| parameters := tools.Parameters{ | ||
| modelParameter, | ||
| exploreParameter, | ||
| fieldsParameter, | ||
| filtersParameter, | ||
| pivotsParameter, | ||
| sortsParameter, | ||
| limitParameter, | ||
| tzParameter, | ||
| } | ||
|
|
||
| mcpManifest := tools.McpManifest{ | ||
| Name: cfg.Name, | ||
| Description: cfg.Description, | ||
| InputSchema: parameters.McpManifest(), | ||
| } | ||
|
|
||
| // finish tool setup | ||
| return Tool{ | ||
| Name: cfg.Name, | ||
| Kind: kind, | ||
| Parameters: parameters, | ||
| AuthRequired: cfg.AuthRequired, | ||
| Client: s.Client, | ||
| ApiSettings: s.ApiSettings, | ||
| manifest: tools.Manifest{ | ||
| Description: cfg.Description, | ||
| Parameters: parameters.Manifest(), | ||
| AuthRequired: cfg.AuthRequired, | ||
| }, | ||
| mcpManifest: mcpManifest, | ||
| }, nil | ||
| } | ||
|
|
||
| // validate interface | ||
| var _ tools.Tool = Tool{} | ||
|
|
||
| type Tool struct { | ||
| Name string `yaml:"name"` | ||
| Kind string `yaml:"kind"` | ||
| Client *v4.LookerSDK | ||
| ApiSettings *rtl.ApiSettings | ||
| AuthRequired []string `yaml:"authRequired"` | ||
| Parameters tools.Parameters `yaml:"parameters"` | ||
| manifest tools.Manifest | ||
| mcpManifest tools.McpManifest | ||
| } | ||
|
|
||
| func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) (any, error) { | ||
| logger, err := util.LoggerFromContext(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to get logger from ctx: %s", err) | ||
| } | ||
| logger.DebugContext(ctx, "params = ", params) | ||
| paramsMap := params.AsMap() | ||
|
|
||
| f, err := tools.ConvertAnySliceToTyped(paramsMap["fields"].([]any), "string") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("can't convert fields to array of strings: %s", err) | ||
| } | ||
| fields := f.([]string) | ||
| filters := paramsMap["filters"].(map[string]any) | ||
| // Sometimes filters come as "'field.id'": "expression" so strip extra '' | ||
| for k, v := range filters { | ||
| if len(k) > 0 && k[0] == '\'' && k[len(k)-1] == '\'' { | ||
| delete(filters, k) | ||
| filters[k[1:len(k)-1]] = v | ||
| } | ||
| } | ||
| p, err := tools.ConvertAnySliceToTyped(paramsMap["pivots"].([]any), "string") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("can't convert pivots to array of strings: %s", err) | ||
| } | ||
| pivots := p.([]string) | ||
| s, err := tools.ConvertAnySliceToTyped(paramsMap["sorts"].([]any), "string") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("can't convert sorts to array of strings: %s", err) | ||
| } | ||
| sorts := s.([]string) | ||
| limit := fmt.Sprintf("%d", paramsMap["limit"].(int)) | ||
|
|
||
| var tz string | ||
| if paramsMap["tz"] != nil { | ||
| tz = paramsMap["tz"].(string) | ||
| } else { | ||
| tzname, err := tzlocal.RuntimeTZ() | ||
| if err != nil { | ||
| logger.ErrorContext(ctx, fmt.Sprintf("Error getting local timezone: %s", err)) | ||
| tzname = "Etc/UTC" | ||
| } | ||
| tz = tzname | ||
| } | ||
|
|
||
| wq := v4.WriteQuery{ | ||
| Model: paramsMap["model"].(string), | ||
| View: paramsMap["explore"].(string), | ||
| Fields: &fields, | ||
| Pivots: &pivots, | ||
| Filters: &filters, | ||
| Sorts: &sorts, | ||
| Limit: &limit, | ||
| QueryTimezone: &tz, | ||
| } | ||
|
|
||
| respFields := "id,slug,share_url,expanded_share_url" | ||
| resp, err := t.Client.CreateQuery(wq, respFields, t.ApiSettings) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error making query request: %s", err) | ||
| } | ||
| logger.DebugContext(ctx, "resp = ", resp) | ||
|
|
||
| data := make(map[string]any) | ||
| if resp.Id != nil { | ||
| data["id"] = *resp.Id | ||
| } | ||
| if resp.Slug != nil { | ||
| data["slug"] = *resp.Slug | ||
| } | ||
| if resp.ShareUrl != nil { | ||
| data["url"] = *resp.ShareUrl | ||
| } | ||
| if resp.ExpandedShareUrl != nil { | ||
| data["long_url"] = *resp.ExpandedShareUrl | ||
| } | ||
| logger.DebugContext(ctx, "data = %v", data) | ||
|
|
||
| return data, nil | ||
| } | ||
|
|
||
| func (t Tool) ParseParams(data map[string]any, claims map[string]map[string]any) (tools.ParamValues, error) { | ||
| return tools.ParseParams(t.Parameters, data, claims) | ||
| } | ||
|
|
||
| func (t Tool) Manifest() tools.Manifest { | ||
| return t.manifest | ||
| } | ||
|
|
||
| func (t Tool) McpManifest() tools.McpManifest { | ||
| return t.mcpManifest | ||
| } | ||
|
|
||
| func (t Tool) Authorized(verifiedAuthServices []string) bool { | ||
| return true | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.