Files
genai-toolbox/internal/tools/cloud_sql_pg.go
Kurtis Van Gent 27edd3b5f6 feat: add preliminary parsing of parameters (#13)
This PR adds preliminary parsing of parameters. Currently it only
supports 4 types: string, int, float32, and bool. Almost certainly we
will need to introduce more complicated parsing configuration (to handle
objects and arrays), but my initial attempts got quickly complicated, so
I simplified in the short term.

This also makes 2 breaking changes to config.yaml:
- changes "parameters" to be a list over object -- this is because
parameter ordering is important, and needs to be preserved
- removed the "required" field from parameter objects -- we need to
determine how to handle optional parameters in SQL queries
2024-10-08 15:18:49 -06:00

81 lines
2.3 KiB
Go

// Copyright 2024 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 tools
import (
"fmt"
"github.com/googleapis/genai-toolbox/internal/sources"
)
const CloudSQLPgSQLGenericKind string = "cloud-sql-postgres-generic"
// validate interface
var _ Config = CloudSQLPgGenericConfig{}
type CloudSQLPgGenericConfig struct {
Name string `yaml:"name"`
Kind string `yaml:"kind"`
Source string `yaml:"source"`
Description string `yaml:"description"`
Statement string `yaml:"statement"`
Parameters []Parameter `yaml:"parameters"`
}
func (r CloudSQLPgGenericConfig) toolKind() string {
return CloudSQLPgSQLGenericKind
}
func (r CloudSQLPgGenericConfig) Initialize(srcs map[string]sources.Source) (Tool, error) {
// verify source exists
rawS, ok := srcs[r.Source]
if !ok {
return nil, fmt.Errorf("No source named %q configured!", r.Source)
}
// verify the source is the right kind
s, ok := rawS.(sources.CloudSQLPgSource)
if !ok {
return nil, fmt.Errorf("Sources for %q tools must be of kind %q!", CloudSQLPgSQLGenericKind, sources.CloudSQLPgKind)
}
// finish tool setup
t := CloudSQLPgGenericTool{
Name: r.Name,
Kind: CloudSQLPgSQLGenericKind,
Source: s,
Parameters: r.Parameters,
}
return t, nil
}
// validate interface
var _ Tool = CloudSQLPgGenericTool{}
type CloudSQLPgGenericTool struct {
Name string `yaml:"name"`
Kind string `yaml:"kind"`
Source sources.CloudSQLPgSource
Parameters []Parameter `yaml:"parameters"`
}
func (t CloudSQLPgGenericTool) Invoke(params []any) (string, error) {
return fmt.Sprintf("Stub tool call for %q! Parameters parsed: %q", t.Name, params), nil
}
func (t CloudSQLPgGenericTool) ParseParams(data map[string]any) ([]any, error) {
return parseParams(t.Parameters, data)
}