|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package lookergetalllookmltests |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + |
| 20 | + yaml "github.com/goccy/go-yaml" |
| 21 | + "github.com/googleapis/genai-toolbox/internal/embeddingmodels" |
| 22 | + "github.com/googleapis/genai-toolbox/internal/sources" |
| 23 | + "github.com/googleapis/genai-toolbox/internal/tools" |
| 24 | + "github.com/googleapis/genai-toolbox/internal/util/parameters" |
| 25 | + |
| 26 | + "github.com/looker-open-source/sdk-codegen/go/rtl" |
| 27 | + v4 "github.com/looker-open-source/sdk-codegen/go/sdk/v4" |
| 28 | +) |
| 29 | + |
| 30 | +const resourceType string = "looker-get-all-lookml-tests" |
| 31 | + |
| 32 | +func init() { |
| 33 | + if !tools.Register(resourceType, newConfig) { |
| 34 | + panic(fmt.Sprintf("tool type %q already registered", resourceType)) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (tools.ToolConfig, error) { |
| 39 | + actual := Config{Name: name} |
| 40 | + if err := decoder.DecodeContext(ctx, &actual); err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + return actual, nil |
| 44 | +} |
| 45 | + |
| 46 | +type compatibleSource interface { |
| 47 | + UseClientAuthorization() bool |
| 48 | + GetAuthTokenHeaderName() string |
| 49 | + LookerApiSettings() *rtl.ApiSettings |
| 50 | + GetLookerSDK(string) (*v4.LookerSDK, error) |
| 51 | +} |
| 52 | + |
| 53 | +type Config struct { |
| 54 | + Name string `yaml:"name" validate:"required"` |
| 55 | + Type string `yaml:"type" validate:"required"` |
| 56 | + Source string `yaml:"source" validate:"required"` |
| 57 | + Description string `yaml:"description" validate:"required"` |
| 58 | + AuthRequired []string `yaml:"authRequired"` |
| 59 | + Annotations *tools.ToolAnnotations `yaml:"annotations,omitempty"` |
| 60 | +} |
| 61 | + |
| 62 | +// validate interface |
| 63 | +var _ tools.ToolConfig = Config{} |
| 64 | + |
| 65 | +func (cfg Config) ToolConfigType() string { |
| 66 | + return resourceType |
| 67 | +} |
| 68 | + |
| 69 | +func (cfg Config) Initialize(srcs map[string]sources.Source) (tools.Tool, error) { |
| 70 | + projectIdParameter := parameters.NewStringParameter("project_id", "The id of the project to retrieve LookML tests for.") |
| 71 | + fileIdParameter := parameters.NewStringParameterWithRequired("file_id", "Optional id of the file to filter tests by.", false) |
| 72 | + params := parameters.Parameters{projectIdParameter, fileIdParameter} |
| 73 | + |
| 74 | + annotations := cfg.Annotations |
| 75 | + if annotations == nil { |
| 76 | + readOnlyHint := true |
| 77 | + annotations = &tools.ToolAnnotations{ |
| 78 | + ReadOnlyHint: &readOnlyHint, |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + mcpManifest := tools.GetMcpManifest(cfg.Name, cfg.Description, cfg.AuthRequired, params, annotations) |
| 83 | + |
| 84 | + // finish tool setup |
| 85 | + return Tool{ |
| 86 | + Config: cfg, |
| 87 | + Parameters: params, |
| 88 | + manifest: tools.Manifest{ |
| 89 | + Description: cfg.Description, |
| 90 | + Parameters: params.Manifest(), |
| 91 | + AuthRequired: cfg.AuthRequired, |
| 92 | + }, |
| 93 | + mcpManifest: mcpManifest, |
| 94 | + }, nil |
| 95 | +} |
| 96 | + |
| 97 | +// validate interface |
| 98 | +var _ tools.Tool = Tool{} |
| 99 | + |
| 100 | +type Tool struct { |
| 101 | + Config |
| 102 | + Parameters parameters.Parameters `yaml:"parameters"` |
| 103 | + manifest tools.Manifest |
| 104 | + mcpManifest tools.McpManifest |
| 105 | +} |
| 106 | + |
| 107 | +func (t Tool) ToConfig() tools.ToolConfig { |
| 108 | + return t.Config |
| 109 | +} |
| 110 | + |
| 111 | +func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) { |
| 112 | + source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + sdk, err := source.GetLookerSDK(string(accessToken)) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("error getting sdk: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + mapParams := params.AsMap() |
| 123 | + projectId, ok := mapParams["project_id"].(string) |
| 124 | + if !ok { |
| 125 | + return nil, fmt.Errorf("'project_id' must be a string, got %T", mapParams["project_id"]) |
| 126 | + } |
| 127 | + |
| 128 | + var fileId string |
| 129 | + if val, ok := mapParams["file_id"].(string); ok { |
| 130 | + fileId = val |
| 131 | + } |
| 132 | + |
| 133 | + resp, err := sdk.AllLookmlTests(projectId, fileId, source.LookerApiSettings()) |
| 134 | + if err != nil { |
| 135 | + return nil, fmt.Errorf("error retrieving lookml tests: %w", err) |
| 136 | + } |
| 137 | + |
| 138 | + return resp, nil |
| 139 | +} |
| 140 | + |
| 141 | +func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) { |
| 142 | + return parameters.EmbedParams(ctx, t.Parameters, paramValues, embeddingModelsMap, nil) |
| 143 | +} |
| 144 | + |
| 145 | +func (t Tool) Manifest() tools.Manifest { |
| 146 | + return t.manifest |
| 147 | +} |
| 148 | + |
| 149 | +func (t Tool) McpManifest() tools.McpManifest { |
| 150 | + return t.mcpManifest |
| 151 | +} |
| 152 | + |
| 153 | +func (t Tool) RequiresClientAuthorization(resourceMgr tools.SourceProvider) (bool, error) { |
| 154 | + source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type) |
| 155 | + if err != nil { |
| 156 | + return false, err |
| 157 | + } |
| 158 | + return source.UseClientAuthorization(), nil |
| 159 | +} |
| 160 | + |
| 161 | +func (t Tool) Authorized(verifiedAuthServices []string) bool { |
| 162 | + return tools.IsAuthorized(t.AuthRequired, verifiedAuthServices) |
| 163 | +} |
| 164 | + |
| 165 | +func (t Tool) GetAuthTokenHeaderName(resourceMgr tools.SourceProvider) (string, error) { |
| 166 | + source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type) |
| 167 | + if err != nil { |
| 168 | + return "", err |
| 169 | + } |
| 170 | + return source.GetAuthTokenHeaderName(), nil |
| 171 | +} |
| 172 | + |
| 173 | +func (t Tool) GetParameters() parameters.Parameters { |
| 174 | + return t.Parameters |
| 175 | +} |
0 commit comments