mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-02-04 04:05:22 -05:00
Compare commits
13 Commits
err-api
...
refactor-t
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
101dcb4411 | ||
|
|
1d692af160 | ||
|
|
919787562a | ||
|
|
2583c5adc9 | ||
|
|
9ff6fd6830 | ||
|
|
c6e1889b74 | ||
|
|
89a9321932 | ||
|
|
4ca592c366 | ||
|
|
9208b2b237 | ||
|
|
3dce8678f7 | ||
|
|
7fb56bfe67 | ||
|
|
99b71589b0 | ||
|
|
3f85c16f32 |
@@ -30,6 +30,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/server/resources"
|
"github.com/googleapis/genai-toolbox/internal/server/resources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/telemetry"
|
"github.com/googleapis/genai-toolbox/internal/telemetry"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -51,7 +52,7 @@ type MockTool struct {
|
|||||||
requiresClientAuthrorization bool
|
requiresClientAuthrorization bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t MockTool) Invoke(context.Context, tools.SourceProvider, parameters.ParamValues, tools.AccessToken) (any, error) {
|
func (t MockTool) Invoke(context.Context, tools.SourceProvider, parameters.ParamValues, tools.AccessToken) (any, util.ToolboxError) {
|
||||||
mock := []any{t.Name}
|
mock := []any{t.Name}
|
||||||
return mock, nil
|
return mock, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -122,44 +123,49 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", 500, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok || project == "" {
|
if !ok || project == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'project' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing 'project' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
location, ok := paramsMap["location"].(string)
|
location, ok := paramsMap["location"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid 'location' parameter; expected a string")
|
return nil, util.NewAgentError("invalid 'location' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
clusterID, ok := paramsMap["cluster"].(string)
|
clusterID, ok := paramsMap["cluster"].(string)
|
||||||
if !ok || clusterID == "" {
|
if !ok || clusterID == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'cluster' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing 'cluster' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
password, ok := paramsMap["password"].(string)
|
password, ok := paramsMap["password"].(string)
|
||||||
if !ok || password == "" {
|
if !ok || password == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'password' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing 'password' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
network, ok := paramsMap["network"].(string)
|
network, ok := paramsMap["network"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid 'network' parameter; expected a string")
|
return nil, util.NewAgentError("invalid 'network' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
user, ok := paramsMap["user"].(string)
|
user, ok := paramsMap["user"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid 'user' parameter; expected a string")
|
return nil, util.NewAgentError("invalid 'user' parameter; expected a string", nil)
|
||||||
|
}
|
||||||
|
resp, err := source.CreateCluster(ctx, project, location, network, user, password, clusterID, string(accessToken))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.CreateCluster(ctx, project, location, network, user, password, clusterID, string(accessToken))
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -123,36 +124,36 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", 500, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok || project == "" {
|
if !ok || project == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'project' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing 'project' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
location, ok := paramsMap["location"].(string)
|
location, ok := paramsMap["location"].(string)
|
||||||
if !ok || location == "" {
|
if !ok || location == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'location' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing 'location' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
cluster, ok := paramsMap["cluster"].(string)
|
cluster, ok := paramsMap["cluster"].(string)
|
||||||
if !ok || cluster == "" {
|
if !ok || cluster == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'cluster' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing 'cluster' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
instanceID, ok := paramsMap["instance"].(string)
|
instanceID, ok := paramsMap["instance"].(string)
|
||||||
if !ok || instanceID == "" {
|
if !ok || instanceID == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'instance' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing 'instance' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
instanceType, ok := paramsMap["instanceType"].(string)
|
instanceType, ok := paramsMap["instanceType"].(string)
|
||||||
if !ok || (instanceType != "READ_POOL" && instanceType != "PRIMARY") {
|
if !ok || (instanceType != "READ_POOL" && instanceType != "PRIMARY") {
|
||||||
return nil, fmt.Errorf("invalid 'instanceType' parameter; expected 'PRIMARY' or 'READ_POOL'")
|
return nil, util.NewAgentError("invalid 'instanceType' parameter; expected 'PRIMARY' or 'READ_POOL'", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
displayName, _ := paramsMap["displayName"].(string)
|
displayName, _ := paramsMap["displayName"].(string)
|
||||||
@@ -161,11 +162,15 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if instanceType == "READ_POOL" {
|
if instanceType == "READ_POOL" {
|
||||||
nodeCount, ok = paramsMap["nodeCount"].(int)
|
nodeCount, ok = paramsMap["nodeCount"].(int)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid 'nodeCount' parameter; expected an integer for READ_POOL")
|
return nil, util.NewAgentError("invalid 'nodeCount' parameter; expected an integer for READ_POOL", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.CreateInstance(ctx, project, location, cluster, instanceID, instanceType, displayName, nodeCount, string(accessToken))
|
resp, err := source.CreateInstance(ctx, project, location, cluster, instanceID, instanceType, displayName, nodeCount, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -122,43 +123,43 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", 500, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok || project == "" {
|
if !ok || project == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'project' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing 'project' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
location, ok := paramsMap["location"].(string)
|
location, ok := paramsMap["location"].(string)
|
||||||
if !ok || location == "" {
|
if !ok || location == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing'location' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing'location' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
cluster, ok := paramsMap["cluster"].(string)
|
cluster, ok := paramsMap["cluster"].(string)
|
||||||
if !ok || cluster == "" {
|
if !ok || cluster == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'cluster' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing 'cluster' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
userID, ok := paramsMap["user"].(string)
|
userID, ok := paramsMap["user"].(string)
|
||||||
if !ok || userID == "" {
|
if !ok || userID == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'user' parameter; expected a non-empty string")
|
return nil, util.NewAgentError("invalid or missing 'user' parameter; expected a non-empty string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
userType, ok := paramsMap["userType"].(string)
|
userType, ok := paramsMap["userType"].(string)
|
||||||
if !ok || (userType != "ALLOYDB_BUILT_IN" && userType != "ALLOYDB_IAM_USER") {
|
if !ok || (userType != "ALLOYDB_BUILT_IN" && userType != "ALLOYDB_IAM_USER") {
|
||||||
return nil, fmt.Errorf("invalid or missing 'userType' parameter; expected 'ALLOYDB_BUILT_IN' or 'ALLOYDB_IAM_USER'")
|
return nil, util.NewAgentError("invalid or missing 'userType' parameter; expected 'ALLOYDB_BUILT_IN' or 'ALLOYDB_IAM_USER'", nil)
|
||||||
}
|
}
|
||||||
var password string
|
var password string
|
||||||
|
|
||||||
if userType == "ALLOYDB_BUILT_IN" {
|
if userType == "ALLOYDB_BUILT_IN" {
|
||||||
password, ok = paramsMap["password"].(string)
|
password, ok = paramsMap["password"].(string)
|
||||||
if !ok || password == "" {
|
if !ok || password == "" {
|
||||||
return nil, fmt.Errorf("password is required when userType is ALLOYDB_BUILT_IN")
|
return nil, util.NewAgentError("password is required when userType is ALLOYDB_BUILT_IN", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,7 +171,11 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.CreateUser(ctx, userType, password, roles, string(accessToken), project, location, cluster, userID)
|
resp, err := source.CreateUser(ctx, userType, password, roles, string(accessToken), project, location, cluster, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -120,28 +121,32 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", 500, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok || project == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'project' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'project' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
location, ok := paramsMap["location"].(string)
|
location, ok := paramsMap["location"].(string)
|
||||||
if !ok {
|
if !ok || location == "" {
|
||||||
return nil, fmt.Errorf("invalid 'location' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'location' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
cluster, ok := paramsMap["cluster"].(string)
|
cluster, ok := paramsMap["cluster"].(string)
|
||||||
if !ok {
|
if !ok || cluster == "" {
|
||||||
return nil, fmt.Errorf("invalid 'cluster' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'cluster' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.GetCluster(ctx, project, location, cluster, string(accessToken))
|
resp, err := source.GetCluster(ctx, project, location, cluster, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -120,32 +121,36 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", 500, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok || project == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'project' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'project' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
location, ok := paramsMap["location"].(string)
|
location, ok := paramsMap["location"].(string)
|
||||||
if !ok {
|
if !ok || location == "" {
|
||||||
return nil, fmt.Errorf("invalid 'location' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'location' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
cluster, ok := paramsMap["cluster"].(string)
|
cluster, ok := paramsMap["cluster"].(string)
|
||||||
if !ok {
|
if !ok || cluster == "" {
|
||||||
return nil, fmt.Errorf("invalid 'cluster' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'cluster' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
instance, ok := paramsMap["instance"].(string)
|
instance, ok := paramsMap["instance"].(string)
|
||||||
if !ok {
|
if !ok || instance == "" {
|
||||||
return nil, fmt.Errorf("invalid 'instance' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'instance' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.GetInstance(ctx, project, location, cluster, instance, string(accessToken))
|
resp, err := source.GetInstance(ctx, project, location, cluster, instance, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -120,32 +121,36 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", 500, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok || project == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'project' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'project' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
location, ok := paramsMap["location"].(string)
|
location, ok := paramsMap["location"].(string)
|
||||||
if !ok {
|
if !ok || location == "" {
|
||||||
return nil, fmt.Errorf("invalid 'location' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'location' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
cluster, ok := paramsMap["cluster"].(string)
|
cluster, ok := paramsMap["cluster"].(string)
|
||||||
if !ok {
|
if !ok || cluster == "" {
|
||||||
return nil, fmt.Errorf("invalid 'cluster' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'cluster' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
user, ok := paramsMap["user"].(string)
|
user, ok := paramsMap["user"].(string)
|
||||||
if !ok {
|
if !ok || user == "" {
|
||||||
return nil, fmt.Errorf("invalid 'user' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'user' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.GetUsers(ctx, project, location, cluster, user, string(accessToken))
|
resp, err := source.GetUsers(ctx, project, location, cluster, user, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -118,24 +119,28 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", 500, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok || project == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'project' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'project' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
location, ok := paramsMap["location"].(string)
|
location, ok := paramsMap["location"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid 'location' parameter; expected a string")
|
return nil, util.NewAgentError("invalid 'location' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.ListCluster(ctx, project, location, string(accessToken))
|
resp, err := source.ListCluster(ctx, project, location, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -119,28 +120,32 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", 500, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok || project == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'project' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'project' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
location, ok := paramsMap["location"].(string)
|
location, ok := paramsMap["location"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid 'location' parameter; expected a string")
|
return nil, util.NewAgentError("invalid 'location' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
cluster, ok := paramsMap["cluster"].(string)
|
cluster, ok := paramsMap["cluster"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid 'cluster' parameter; expected a string")
|
return nil, util.NewAgentError("invalid 'cluster' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.ListInstance(ctx, project, location, cluster, string(accessToken))
|
resp, err := source.ListInstance(ctx, project, location, cluster, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -119,28 +120,32 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", 500, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok || project == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing 'project' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'project' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
location, ok := paramsMap["location"].(string)
|
location, ok := paramsMap["location"].(string)
|
||||||
if !ok {
|
if !ok || location == "" {
|
||||||
return nil, fmt.Errorf("invalid 'location' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'location' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
cluster, ok := paramsMap["cluster"].(string)
|
cluster, ok := paramsMap["cluster"].(string)
|
||||||
if !ok {
|
if !ok || cluster == "" {
|
||||||
return nil, fmt.Errorf("invalid 'cluster' parameter; expected a string")
|
return nil, util.NewAgentError("invalid or missing 'cluster' parameter; expected a string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.ListUsers(ctx, project, location, cluster, string(accessToken))
|
resp, err := source.ListUsers(ctx, project, location, cluster, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -213,25 +214,25 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", 500, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'project' parameter")
|
return nil, util.NewAgentError("missing 'project' parameter", nil)
|
||||||
}
|
}
|
||||||
location, ok := paramsMap["location"].(string)
|
location, ok := paramsMap["location"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'location' parameter")
|
return nil, util.NewAgentError("missing 'location' parameter", nil)
|
||||||
}
|
}
|
||||||
operation, ok := paramsMap["operation"].(string)
|
operation, ok := paramsMap["operation"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'operation' parameter")
|
return nil, util.NewAgentError("missing 'operation' parameter", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
||||||
@@ -246,14 +247,15 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
for retries < maxRetries {
|
for retries < maxRetries {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil, fmt.Errorf("timed out waiting for operation: %w", ctx.Err())
|
return nil, util.NewAgentError("timed out waiting for operation %s", ctx.Err())
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
op, err := source.GetOperations(ctx, project, location, operation, alloyDBConnectionMessageTemplate, delay, string(accessToken))
|
op, err := source.GetOperations(ctx, project, location, operation, alloyDBConnectionMessageTemplate, delay, string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.ProcessGcpError(err)
|
||||||
} else if op != nil {
|
}
|
||||||
|
if op != nil {
|
||||||
return op, nil
|
return op, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,7 +266,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
retries++
|
retries++
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("exceeded max retries waiting for operation")
|
return nil, util.NewAgentError("exceeded max retries waiting for operation", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package alloydbainl
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
@@ -127,10 +129,10 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sliceParams := params.AsSlice()
|
sliceParams := params.AsSlice()
|
||||||
@@ -143,7 +145,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
resp, err := source.RunSQL(ctx, t.Statement, allParamValues)
|
resp, err := source.RunSQL(ctx, t.Statement, allParamValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%w. Query: %v , Values: %v. Toolbox v0.19.0+ is only compatible with AlloyDB AI NL v1.0.3+. Please ensure that you are using the latest AlloyDB AI NL extension", err, t.Statement, allParamValues)
|
return nil, util.NewClientServerError(fmt.Sprintf("error running SQL query: %v. Query: %v , Values: %v. Toolbox v0.19.0+ is only compatible with AlloyDB AI NL v1.0.3+. Please ensure that you are using the latest AlloyDB AI NL extension", err, t.Statement, allParamValues), http.StatusBadRequest, err)
|
||||||
}
|
}
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package bigqueryanalyzecontribution
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
bigqueryapi "cloud.google.com/go/bigquery"
|
bigqueryapi "cloud.google.com/go/bigquery"
|
||||||
@@ -27,6 +28,7 @@ import (
|
|||||||
bigqueryds "github.com/googleapis/genai-toolbox/internal/sources/bigquery"
|
bigqueryds "github.com/googleapis/genai-toolbox/internal/sources/bigquery"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
bqutil "github.com/googleapis/genai-toolbox/internal/tools/bigquery/bigquerycommon"
|
bqutil "github.com/googleapis/genai-toolbox/internal/tools/bigquery/bigquerycommon"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
||||||
)
|
)
|
||||||
@@ -154,21 +156,21 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke runs the contribution analysis.
|
// Invoke runs the contribution analysis.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
inputData, ok := paramsMap["input_data"].(string)
|
inputData, ok := paramsMap["input_data"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to cast input_data parameter %s", paramsMap["input_data"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast input_data parameter %s", paramsMap["input_data"]), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
bqClient, restService, err := source.RetrieveClientAndService(accessToken)
|
bqClient, restService, err := source.RetrieveClientAndService(accessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("failed to retrieve BigQuery client", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
modelID := fmt.Sprintf("contribution_analysis_model_%s", strings.ReplaceAll(uuid.New().String(), "-", ""))
|
modelID := fmt.Sprintf("contribution_analysis_model_%s", strings.ReplaceAll(uuid.New().String(), "-", ""))
|
||||||
@@ -186,7 +188,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
options = append(options, fmt.Sprintf("DIMENSION_ID_COLS = [%s]", strings.Join(strCols, ", ")))
|
options = append(options, fmt.Sprintf("DIMENSION_ID_COLS = [%s]", strings.Join(strCols, ", ")))
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("unable to cast dimension_id_cols parameter %s", paramsMap["dimension_id_cols"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast dimension_id_cols parameter %s", paramsMap["dimension_id_cols"]), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if val, ok := paramsMap["top_k_insights_by_apriori_support"]; ok {
|
if val, ok := paramsMap["top_k_insights_by_apriori_support"]; ok {
|
||||||
@@ -195,7 +197,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if val, ok := paramsMap["pruning_method"].(string); ok {
|
if val, ok := paramsMap["pruning_method"].(string); ok {
|
||||||
upperVal := strings.ToUpper(val)
|
upperVal := strings.ToUpper(val)
|
||||||
if upperVal != "NO_PRUNING" && upperVal != "PRUNE_REDUNDANT_INSIGHTS" {
|
if upperVal != "NO_PRUNING" && upperVal != "PRUNE_REDUNDANT_INSIGHTS" {
|
||||||
return nil, fmt.Errorf("invalid pruning_method: %s", val)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid pruning_method: %s", val), nil)
|
||||||
}
|
}
|
||||||
options = append(options, fmt.Sprintf("PRUNING_METHOD = '%s'", upperVal))
|
options = append(options, fmt.Sprintf("PRUNING_METHOD = '%s'", upperVal))
|
||||||
}
|
}
|
||||||
@@ -207,7 +209,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
var connProps []*bigqueryapi.ConnectionProperty
|
var connProps []*bigqueryapi.ConnectionProperty
|
||||||
session, err := source.BigQuerySession()(ctx)
|
session, err := source.BigQuerySession()(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get BigQuery session: %w", err)
|
return nil, util.NewClientServerError("failed to get BigQuery session", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
if session != nil {
|
if session != nil {
|
||||||
connProps = []*bigqueryapi.ConnectionProperty{
|
connProps = []*bigqueryapi.ConnectionProperty{
|
||||||
@@ -216,22 +218,22 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
dryRunJob, err := bqutil.DryRunQuery(ctx, restService, source.BigQueryClient().Project(), source.BigQueryClient().Location, inputData, nil, connProps)
|
dryRunJob, err := bqutil.DryRunQuery(ctx, restService, source.BigQueryClient().Project(), source.BigQueryClient().Location, inputData, nil, connProps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("query validation failed: %w", err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
statementType := dryRunJob.Statistics.Query.StatementType
|
statementType := dryRunJob.Statistics.Query.StatementType
|
||||||
if statementType != "SELECT" {
|
if statementType != "SELECT" {
|
||||||
return nil, fmt.Errorf("the 'input_data' parameter only supports a table ID or a SELECT query. The provided query has statement type '%s'", statementType)
|
return nil, util.NewAgentError(fmt.Sprintf("the 'input_data' parameter only supports a table ID or a SELECT query. The provided query has statement type '%s'", statementType), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
queryStats := dryRunJob.Statistics.Query
|
queryStats := dryRunJob.Statistics.Query
|
||||||
if queryStats != nil {
|
if queryStats != nil {
|
||||||
for _, tableRef := range queryStats.ReferencedTables {
|
for _, tableRef := range queryStats.ReferencedTables {
|
||||||
if !source.IsDatasetAllowed(tableRef.ProjectId, tableRef.DatasetId) {
|
if !source.IsDatasetAllowed(tableRef.ProjectId, tableRef.DatasetId) {
|
||||||
return nil, fmt.Errorf("query in input_data accesses dataset '%s.%s', which is not in the allowed list", tableRef.ProjectId, tableRef.DatasetId)
|
return nil, util.NewAgentError(fmt.Sprintf("query in input_data accesses dataset '%s.%s', which is not in the allowed list", tableRef.ProjectId, tableRef.DatasetId), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("could not analyze query in input_data to validate against allowed datasets")
|
return nil, util.NewAgentError("could not analyze query in input_data to validate against allowed datasets", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
inputDataSource = fmt.Sprintf("(%s)", inputData)
|
inputDataSource = fmt.Sprintf("(%s)", inputData)
|
||||||
@@ -245,10 +247,10 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
case 2: // dataset.table
|
case 2: // dataset.table
|
||||||
projectID, datasetID = source.BigQueryClient().Project(), parts[0]
|
projectID, datasetID = source.BigQueryClient().Project(), parts[0]
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid table ID format for 'input_data': %q. Expected 'dataset.table' or 'project.dataset.table'", inputData)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid table ID format for 'input_data': %q. Expected 'dataset.table' or 'project.dataset.table'", inputData), nil)
|
||||||
}
|
}
|
||||||
if !source.IsDatasetAllowed(projectID, datasetID) {
|
if !source.IsDatasetAllowed(projectID, datasetID) {
|
||||||
return nil, fmt.Errorf("access to dataset '%s.%s' (from table '%s') is not allowed", projectID, datasetID, inputData)
|
return nil, util.NewAgentError(fmt.Sprintf("access to dataset '%s.%s' (from table '%s') is not allowed", projectID, datasetID, inputData), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
inputDataSource = fmt.Sprintf("SELECT * FROM `%s`", inputData)
|
inputDataSource = fmt.Sprintf("SELECT * FROM `%s`", inputData)
|
||||||
@@ -268,7 +270,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Otherwise, a new session will be created by the first query.
|
// Otherwise, a new session will be created by the first query.
|
||||||
session, err := source.BigQuerySession()(ctx)
|
session, err := source.BigQuerySession()(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get BigQuery session: %w", err)
|
return nil, util.NewClientServerError("failed to get BigQuery session", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if session != nil {
|
if session != nil {
|
||||||
@@ -281,15 +283,15 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
createModelJob, err := createModelQuery.Run(ctx)
|
createModelJob, err := createModelQuery.Run(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to start create model job: %w", err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := createModelJob.Wait(ctx)
|
status, err := createModelJob.Wait(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to wait for create model job: %w", err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
if err := status.Err(); err != nil {
|
if err := status.Err(); err != nil {
|
||||||
return nil, fmt.Errorf("create model job failed: %w", err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the session ID to use for subsequent queries.
|
// Determine the session ID to use for subsequent queries.
|
||||||
@@ -300,12 +302,17 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
} else if status.Statistics != nil && status.Statistics.SessionInfo != nil {
|
} else if status.Statistics != nil && status.Statistics.SessionInfo != nil {
|
||||||
sessionID = status.Statistics.SessionInfo.SessionID
|
sessionID = status.Statistics.SessionInfo.SessionID
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("failed to get or create a BigQuery session ID")
|
return nil, util.NewClientServerError("failed to get or create a BigQuery session ID", http.StatusInternalServerError, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
getInsightsSQL := fmt.Sprintf("SELECT * FROM ML.GET_INSIGHTS(MODEL %s)", modelID)
|
getInsightsSQL := fmt.Sprintf("SELECT * FROM ML.GET_INSIGHTS(MODEL %s)", modelID)
|
||||||
connProps := []*bigqueryapi.ConnectionProperty{{Key: "session_id", Value: sessionID}}
|
connProps := []*bigqueryapi.ConnectionProperty{{Key: "session_id", Value: sessionID}}
|
||||||
return source.RunSQL(ctx, bqClient, getInsightsSQL, "SELECT", nil, connProps)
|
|
||||||
|
resp, err := source.RunSQL(ctx, bqClient, getInsightsSQL, "SELECT", nil, connProps)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -172,10 +172,10 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
@@ -188,22 +188,22 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Get a token source for the Gemini Data Analytics API.
|
// Get a token source for the Gemini Data Analytics API.
|
||||||
tokenSource, err := source.BigQueryTokenSourceWithScope(ctx, nil)
|
tokenSource, err := source.BigQueryTokenSourceWithScope(ctx, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get token source: %w", err)
|
return nil, util.NewClientServerError("failed to get token source", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use cloud-platform token source for Gemini Data Analytics API
|
// Use cloud-platform token source for Gemini Data Analytics API
|
||||||
if tokenSource == nil {
|
if tokenSource == nil {
|
||||||
return nil, fmt.Errorf("cloud-platform token source is missing")
|
return nil, util.NewClientServerError("cloud-platform token source is missing", http.StatusInternalServerError, nil)
|
||||||
}
|
}
|
||||||
token, err := tokenSource.Token()
|
token, err := tokenSource.Token()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get token from cloud-platform token source: %w", err)
|
return nil, util.NewClientServerError("failed to get token from cloud-platform token source", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
tokenStr = token.AccessToken
|
tokenStr = token.AccessToken
|
||||||
}
|
}
|
||||||
@@ -218,14 +218,14 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
var tableRefs []BQTableReference
|
var tableRefs []BQTableReference
|
||||||
if tableRefsJSON != "" {
|
if tableRefsJSON != "" {
|
||||||
if err := json.Unmarshal([]byte(tableRefsJSON), &tableRefs); err != nil {
|
if err := json.Unmarshal([]byte(tableRefsJSON), &tableRefs); err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse 'table_references' JSON string: %w", err)
|
return nil, util.NewAgentError("failed to parse 'table_references' JSON string", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(source.BigQueryAllowedDatasets()) > 0 {
|
if len(source.BigQueryAllowedDatasets()) > 0 {
|
||||||
for _, tableRef := range tableRefs {
|
for _, tableRef := range tableRefs {
|
||||||
if !source.IsDatasetAllowed(tableRef.ProjectID, tableRef.DatasetID) {
|
if !source.IsDatasetAllowed(tableRef.ProjectID, tableRef.DatasetID) {
|
||||||
return nil, fmt.Errorf("access to dataset '%s.%s' (from table '%s') is not allowed", tableRef.ProjectID, tableRef.DatasetID, tableRef.TableID)
|
return nil, util.NewAgentError(fmt.Sprintf("access to dataset '%s.%s' (from table '%s') is not allowed", tableRef.ProjectID, tableRef.DatasetID, tableRef.TableID), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,7 +258,8 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Call the streaming API
|
// Call the streaming API
|
||||||
response, err := getStream(caURL, payload, headers, source.GetMaxQueryResultRows())
|
response, err := getStream(caURL, payload, headers, source.GetMaxQueryResultRows())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get response from conversational analytics API: %w", err)
|
// getStream wraps network errors or non-200 responses
|
||||||
|
return nil, util.NewClientServerError("failed to get response from conversational analytics API", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return response, nil
|
return response, nil
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
bigqueryapi "cloud.google.com/go/bigquery"
|
bigqueryapi "cloud.google.com/go/bigquery"
|
||||||
@@ -152,25 +153,25 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
sql, ok := paramsMap["sql"].(string)
|
sql, ok := paramsMap["sql"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to cast sql parameter %s", paramsMap["sql"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast sql parameter %s", paramsMap["sql"]), nil)
|
||||||
}
|
}
|
||||||
dryRun, ok := paramsMap["dry_run"].(bool)
|
dryRun, ok := paramsMap["dry_run"].(bool)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to cast dry_run parameter %s", paramsMap["dry_run"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast dry_run parameter %s", paramsMap["dry_run"]), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
bqClient, restService, err := source.RetrieveClientAndService(accessToken)
|
bqClient, restService, err := source.RetrieveClientAndService(accessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("failed to retrieve BigQuery client", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var connProps []*bigqueryapi.ConnectionProperty
|
var connProps []*bigqueryapi.ConnectionProperty
|
||||||
@@ -178,7 +179,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if source.BigQueryWriteMode() == bigqueryds.WriteModeProtected {
|
if source.BigQueryWriteMode() == bigqueryds.WriteModeProtected {
|
||||||
session, err = source.BigQuerySession()(ctx)
|
session, err = source.BigQuerySession()(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get BigQuery session for protected mode: %w", err)
|
return nil, util.NewClientServerError("failed to get BigQuery session for protected mode", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
connProps = []*bigqueryapi.ConnectionProperty{
|
connProps = []*bigqueryapi.ConnectionProperty{
|
||||||
{Key: "session_id", Value: session.ID},
|
{Key: "session_id", Value: session.ID},
|
||||||
@@ -187,7 +188,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
dryRunJob, err := bqutil.DryRunQuery(ctx, restService, bqClient.Project(), bqClient.Location, sql, nil, connProps)
|
dryRunJob, err := bqutil.DryRunQuery(ctx, restService, bqClient.Project(), bqClient.Location, sql, nil, connProps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("query validation failed: %w", err)
|
return nil, util.NewClientServerError("query validation failed", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
statementType := dryRunJob.Statistics.Query.StatementType
|
statementType := dryRunJob.Statistics.Query.StatementType
|
||||||
@@ -195,13 +196,13 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
switch source.BigQueryWriteMode() {
|
switch source.BigQueryWriteMode() {
|
||||||
case bigqueryds.WriteModeBlocked:
|
case bigqueryds.WriteModeBlocked:
|
||||||
if statementType != "SELECT" {
|
if statementType != "SELECT" {
|
||||||
return nil, fmt.Errorf("write mode is 'blocked', only SELECT statements are allowed")
|
return nil, util.NewAgentError("write mode is 'blocked', only SELECT statements are allowed", nil)
|
||||||
}
|
}
|
||||||
case bigqueryds.WriteModeProtected:
|
case bigqueryds.WriteModeProtected:
|
||||||
if dryRunJob.Configuration != nil && dryRunJob.Configuration.Query != nil {
|
if dryRunJob.Configuration != nil && dryRunJob.Configuration.Query != nil {
|
||||||
if dest := dryRunJob.Configuration.Query.DestinationTable; dest != nil && dest.DatasetId != session.DatasetID {
|
if dest := dryRunJob.Configuration.Query.DestinationTable; dest != nil && dest.DatasetId != session.DatasetID {
|
||||||
return nil, fmt.Errorf("protected write mode only supports SELECT statements, or write operations in the anonymous "+
|
return nil, util.NewAgentError(fmt.Sprintf("protected write mode only supports SELECT statements, or write operations in the anonymous "+
|
||||||
"dataset of a BigQuery session, but destination was %q", dest.DatasetId)
|
"dataset of a BigQuery session, but destination was %q", dest.DatasetId), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -209,11 +210,11 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if len(source.BigQueryAllowedDatasets()) > 0 {
|
if len(source.BigQueryAllowedDatasets()) > 0 {
|
||||||
switch statementType {
|
switch statementType {
|
||||||
case "CREATE_SCHEMA", "DROP_SCHEMA", "ALTER_SCHEMA":
|
case "CREATE_SCHEMA", "DROP_SCHEMA", "ALTER_SCHEMA":
|
||||||
return nil, fmt.Errorf("dataset-level operations like '%s' are not allowed when dataset restrictions are in place", statementType)
|
return nil, util.NewAgentError(fmt.Sprintf("dataset-level operations like '%s' are not allowed when dataset restrictions are in place", statementType), nil)
|
||||||
case "CREATE_FUNCTION", "CREATE_TABLE_FUNCTION", "CREATE_PROCEDURE":
|
case "CREATE_FUNCTION", "CREATE_TABLE_FUNCTION", "CREATE_PROCEDURE":
|
||||||
return nil, fmt.Errorf("creating stored routines ('%s') is not allowed when dataset restrictions are in place, as their contents cannot be safely analyzed", statementType)
|
return nil, util.NewAgentError(fmt.Sprintf("creating stored routines ('%s') is not allowed when dataset restrictions are in place, as their contents cannot be safely analyzed", statementType), nil)
|
||||||
case "CALL":
|
case "CALL":
|
||||||
return nil, fmt.Errorf("calling stored procedures ('%s') is not allowed when dataset restrictions are in place, as their contents cannot be safely analyzed", statementType)
|
return nil, util.NewAgentError(fmt.Sprintf("calling stored procedures ('%s') is not allowed when dataset restrictions are in place, as their contents cannot be safely analyzed", statementType), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use a map to avoid duplicate table names.
|
// Use a map to avoid duplicate table names.
|
||||||
@@ -244,7 +245,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
parsedTables, parseErr := bqutil.TableParser(sql, source.BigQueryClient().Project())
|
parsedTables, parseErr := bqutil.TableParser(sql, source.BigQueryClient().Project())
|
||||||
if parseErr != nil {
|
if parseErr != nil {
|
||||||
// If parsing fails (e.g., EXECUTE IMMEDIATE), we cannot guarantee safety, so we must fail.
|
// If parsing fails (e.g., EXECUTE IMMEDIATE), we cannot guarantee safety, so we must fail.
|
||||||
return nil, fmt.Errorf("could not parse tables from query to validate against allowed datasets: %w", parseErr)
|
return nil, util.NewAgentError("could not parse tables from query to validate against allowed datasets", parseErr)
|
||||||
}
|
}
|
||||||
tableNames = parsedTables
|
tableNames = parsedTables
|
||||||
}
|
}
|
||||||
@@ -254,7 +255,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if len(parts) == 3 {
|
if len(parts) == 3 {
|
||||||
projectID, datasetID := parts[0], parts[1]
|
projectID, datasetID := parts[0], parts[1]
|
||||||
if !source.IsDatasetAllowed(projectID, datasetID) {
|
if !source.IsDatasetAllowed(projectID, datasetID) {
|
||||||
return nil, fmt.Errorf("query accesses dataset '%s.%s', which is not in the allowed list", projectID, datasetID)
|
return nil, util.NewAgentError(fmt.Sprintf("query accesses dataset '%s.%s', which is not in the allowed list", projectID, datasetID), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -264,7 +265,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if dryRunJob != nil {
|
if dryRunJob != nil {
|
||||||
jobJSON, err := json.MarshalIndent(dryRunJob, "", " ")
|
jobJSON, err := json.MarshalIndent(dryRunJob, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to marshal dry run job to JSON: %w", err)
|
return nil, util.NewClientServerError("failed to marshal dry run job to JSON", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
return string(jobJSON), nil
|
return string(jobJSON), nil
|
||||||
}
|
}
|
||||||
@@ -275,10 +276,14 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Log the query executed for debugging.
|
// Log the query executed for debugging.
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting logger: %s", err)
|
return nil, util.NewClientServerError("error getting logger", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, fmt.Sprintf("executing `%s` tool query: %s", resourceType, sql))
|
logger.DebugContext(ctx, fmt.Sprintf("executing `%s` tool query: %s", resourceType, sql))
|
||||||
return source.RunSQL(ctx, bqClient, sql, statementType, nil, connProps)
|
resp, err := source.RunSQL(ctx, bqClient, sql, statementType, nil, connProps)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.NewClientServerError("error running sql", http.StatusInternalServerError, err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package bigqueryforecast
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
bigqueryapi "cloud.google.com/go/bigquery"
|
bigqueryapi "cloud.google.com/go/bigquery"
|
||||||
@@ -133,34 +134,34 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
historyData, ok := paramsMap["history_data"].(string)
|
historyData, ok := paramsMap["history_data"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to cast history_data parameter %v", paramsMap["history_data"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast history_data parameter %v", paramsMap["history_data"]), nil)
|
||||||
}
|
}
|
||||||
timestampCol, ok := paramsMap["timestamp_col"].(string)
|
timestampCol, ok := paramsMap["timestamp_col"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to cast timestamp_col parameter %v", paramsMap["timestamp_col"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast timestamp_col parameter %v", paramsMap["timestamp_col"]), nil)
|
||||||
}
|
}
|
||||||
dataCol, ok := paramsMap["data_col"].(string)
|
dataCol, ok := paramsMap["data_col"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to cast data_col parameter %v", paramsMap["data_col"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast data_col parameter %v", paramsMap["data_col"]), nil)
|
||||||
}
|
}
|
||||||
idColsRaw, ok := paramsMap["id_cols"].([]any)
|
idColsRaw, ok := paramsMap["id_cols"].([]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to cast id_cols parameter %v", paramsMap["id_cols"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast id_cols parameter %v", paramsMap["id_cols"]), nil)
|
||||||
}
|
}
|
||||||
var idCols []string
|
var idCols []string
|
||||||
for _, v := range idColsRaw {
|
for _, v := range idColsRaw {
|
||||||
s, ok := v.(string)
|
s, ok := v.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("id_cols contains non-string value: %v", v)
|
return nil, util.NewAgentError(fmt.Sprintf("id_cols contains non-string value: %v", v), nil)
|
||||||
}
|
}
|
||||||
idCols = append(idCols, s)
|
idCols = append(idCols, s)
|
||||||
}
|
}
|
||||||
@@ -169,13 +170,13 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if h, ok := paramsMap["horizon"].(float64); ok {
|
if h, ok := paramsMap["horizon"].(float64); ok {
|
||||||
horizon = int(h)
|
horizon = int(h)
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("unable to cast horizon parameter %v", paramsMap["horizon"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast horizon parameter %v", paramsMap["horizon"]), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bqClient, restService, err := source.RetrieveClientAndService(accessToken)
|
bqClient, restService, err := source.RetrieveClientAndService(accessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("failed to retrieve BigQuery client", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var historyDataSource string
|
var historyDataSource string
|
||||||
@@ -185,7 +186,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
var connProps []*bigqueryapi.ConnectionProperty
|
var connProps []*bigqueryapi.ConnectionProperty
|
||||||
session, err := source.BigQuerySession()(ctx)
|
session, err := source.BigQuerySession()(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get BigQuery session: %w", err)
|
return nil, util.NewClientServerError("failed to get BigQuery session", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
if session != nil {
|
if session != nil {
|
||||||
connProps = []*bigqueryapi.ConnectionProperty{
|
connProps = []*bigqueryapi.ConnectionProperty{
|
||||||
@@ -194,22 +195,22 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
dryRunJob, err := bqutil.DryRunQuery(ctx, restService, source.BigQueryClient().Project(), source.BigQueryClient().Location, historyData, nil, connProps)
|
dryRunJob, err := bqutil.DryRunQuery(ctx, restService, source.BigQueryClient().Project(), source.BigQueryClient().Location, historyData, nil, connProps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("query validation failed: %w", err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
statementType := dryRunJob.Statistics.Query.StatementType
|
statementType := dryRunJob.Statistics.Query.StatementType
|
||||||
if statementType != "SELECT" {
|
if statementType != "SELECT" {
|
||||||
return nil, fmt.Errorf("the 'history_data' parameter only supports a table ID or a SELECT query. The provided query has statement type '%s'", statementType)
|
return nil, util.NewAgentError(fmt.Sprintf("the 'history_data' parameter only supports a table ID or a SELECT query. The provided query has statement type '%s'", statementType), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
queryStats := dryRunJob.Statistics.Query
|
queryStats := dryRunJob.Statistics.Query
|
||||||
if queryStats != nil {
|
if queryStats != nil {
|
||||||
for _, tableRef := range queryStats.ReferencedTables {
|
for _, tableRef := range queryStats.ReferencedTables {
|
||||||
if !source.IsDatasetAllowed(tableRef.ProjectId, tableRef.DatasetId) {
|
if !source.IsDatasetAllowed(tableRef.ProjectId, tableRef.DatasetId) {
|
||||||
return nil, fmt.Errorf("query in history_data accesses dataset '%s.%s', which is not in the allowed list", tableRef.ProjectId, tableRef.DatasetId)
|
return nil, util.NewAgentError(fmt.Sprintf("query in history_data accesses dataset '%s.%s', which is not in the allowed list", tableRef.ProjectId, tableRef.DatasetId), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("could not analyze query in history_data to validate against allowed datasets")
|
return nil, util.NewAgentError("could not analyze query in history_data to validate against allowed datasets", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
historyDataSource = fmt.Sprintf("(%s)", historyData)
|
historyDataSource = fmt.Sprintf("(%s)", historyData)
|
||||||
@@ -226,11 +227,11 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
projectID = source.BigQueryClient().Project()
|
projectID = source.BigQueryClient().Project()
|
||||||
datasetID = parts[0]
|
datasetID = parts[0]
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid table ID format for 'history_data': %q. Expected 'dataset.table' or 'project.dataset.table'", historyData)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid table ID format for 'history_data': %q. Expected 'dataset.table' or 'project.dataset.table'", historyData), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !source.IsDatasetAllowed(projectID, datasetID) {
|
if !source.IsDatasetAllowed(projectID, datasetID) {
|
||||||
return nil, fmt.Errorf("access to dataset '%s.%s' (from table '%s') is not allowed", projectID, datasetID, historyData)
|
return nil, util.NewAgentError(fmt.Sprintf("access to dataset '%s.%s' (from table '%s') is not allowed", projectID, datasetID, historyData), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
historyDataSource = fmt.Sprintf("TABLE `%s`", historyData)
|
historyDataSource = fmt.Sprintf("TABLE `%s`", historyData)
|
||||||
@@ -243,15 +244,15 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
sql := fmt.Sprintf(`SELECT *
|
sql := fmt.Sprintf(`SELECT *
|
||||||
FROM AI.FORECAST(
|
FROM AI.FORECAST(
|
||||||
%s,
|
%s,
|
||||||
data_col => '%s',
|
data_col => '%s',
|
||||||
timestamp_col => '%s',
|
timestamp_col => '%s',
|
||||||
horizon => %d%s)`,
|
horizon => %d%s)`,
|
||||||
historyDataSource, dataCol, timestampCol, horizon, idColsArg)
|
historyDataSource, dataCol, timestampCol, horizon, idColsArg)
|
||||||
|
|
||||||
session, err := source.BigQuerySession()(ctx)
|
session, err := source.BigQuerySession()(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get BigQuery session: %w", err)
|
return nil, util.NewClientServerError("failed to get BigQuery session", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
var connProps []*bigqueryapi.ConnectionProperty
|
var connProps []*bigqueryapi.ConnectionProperty
|
||||||
if session != nil {
|
if session != nil {
|
||||||
@@ -264,11 +265,15 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Log the query executed for debugging.
|
// Log the query executed for debugging.
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting logger: %s", err)
|
return nil, util.NewClientServerError("error getting logger", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, fmt.Sprintf("executing `%s` tool query: %s", resourceType, sql))
|
logger.DebugContext(ctx, fmt.Sprintf("executing `%s` tool query: %s", resourceType, sql))
|
||||||
|
|
||||||
return source.RunSQL(ctx, bqClient, sql, "SELECT", nil, connProps)
|
resp, err := source.RunSQL(ctx, bqClient, sql, "SELECT", nil, connProps)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package bigquerygetdatasetinfo
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
bigqueryapi "cloud.google.com/go/bigquery"
|
bigqueryapi "cloud.google.com/go/bigquery"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
@@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
bqutil "github.com/googleapis/genai-toolbox/internal/tools/bigquery/bigquerycommon"
|
bqutil "github.com/googleapis/genai-toolbox/internal/tools/bigquery/bigquerycommon"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
||||||
)
|
)
|
||||||
@@ -120,38 +122,38 @@ type Tool struct {
|
|||||||
func (t Tool) ToConfig() tools.ToolConfig {
|
func (t Tool) ToConfig() tools.ToolConfig {
|
||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
projectId, ok := mapParams[projectKey].(string)
|
projectId, ok := mapParams[projectKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", projectKey)
|
// Updated: Use fmt.Sprintf for formatting, pass nil as cause
|
||||||
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", projectKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
datasetId, ok := mapParams[datasetKey].(string)
|
datasetId, ok := mapParams[datasetKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", datasetKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", datasetKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
bqClient, _, err := source.RetrieveClientAndService(accessToken)
|
bqClient, _, err := source.RetrieveClientAndService(accessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("failed to retrieve BigQuery client", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !source.IsDatasetAllowed(projectId, datasetId) {
|
if !source.IsDatasetAllowed(projectId, datasetId) {
|
||||||
return nil, fmt.Errorf("access denied to dataset '%s' because it is not in the configured list of allowed datasets for project '%s'", datasetId, projectId)
|
return nil, util.NewClientServerError(fmt.Sprintf("access denied to dataset '%s' because it is not in the configured list of allowed datasets for project '%s'", datasetId, projectId), http.StatusInternalServerError, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
dsHandle := bqClient.DatasetInProject(projectId, datasetId)
|
dsHandle := bqClient.DatasetInProject(projectId, datasetId)
|
||||||
|
|
||||||
metadata, err := dsHandle.Metadata(ctx)
|
metadata, err := dsHandle.Metadata(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get metadata for dataset %s (in project %s): %w", datasetId, projectId, err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return metadata, nil
|
return metadata, nil
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package bigquerygettableinfo
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
bigqueryapi "cloud.google.com/go/bigquery"
|
bigqueryapi "cloud.google.com/go/bigquery"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
@@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
bqutil "github.com/googleapis/genai-toolbox/internal/tools/bigquery/bigquerycommon"
|
bqutil "github.com/googleapis/genai-toolbox/internal/tools/bigquery/bigquerycommon"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
||||||
)
|
)
|
||||||
@@ -125,35 +127,35 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
projectId, ok := mapParams[projectKey].(string)
|
projectId, ok := mapParams[projectKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", projectKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", projectKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
datasetId, ok := mapParams[datasetKey].(string)
|
datasetId, ok := mapParams[datasetKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", datasetKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", datasetKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
tableId, ok := mapParams[tableKey].(string)
|
tableId, ok := mapParams[tableKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", tableKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", tableKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !source.IsDatasetAllowed(projectId, datasetId) {
|
if !source.IsDatasetAllowed(projectId, datasetId) {
|
||||||
return nil, fmt.Errorf("access denied to dataset '%s' because it is not in the configured list of allowed datasets for project '%s'", datasetId, projectId)
|
return nil, util.NewClientServerError(fmt.Sprintf("access denied to dataset '%s' because it is not in the configured list of allowed datasets for project '%s'", datasetId, projectId), http.StatusInternalServerError, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
bqClient, _, err := source.RetrieveClientAndService(accessToken)
|
bqClient, _, err := source.RetrieveClientAndService(accessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("failed to retrieve BigQuery client", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dsHandle := bqClient.DatasetInProject(projectId, datasetId)
|
dsHandle := bqClient.DatasetInProject(projectId, datasetId)
|
||||||
@@ -161,7 +163,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
metadata, err := tableHandle.Metadata(ctx)
|
metadata, err := tableHandle.Metadata(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get metadata for table %s.%s.%s: %w", projectId, datasetId, tableId, err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return metadata, nil
|
return metadata, nil
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package bigquerylistdatasetids
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
bigqueryapi "cloud.google.com/go/bigquery"
|
bigqueryapi "cloud.google.com/go/bigquery"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
||||||
"google.golang.org/api/iterator"
|
"google.golang.org/api/iterator"
|
||||||
@@ -120,10 +122,10 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(source.BigQueryAllowedDatasets()) > 0 {
|
if len(source.BigQueryAllowedDatasets()) > 0 {
|
||||||
@@ -132,12 +134,12 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
projectId, ok := mapParams[projectKey].(string)
|
projectId, ok := mapParams[projectKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", projectKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", projectKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
bqClient, _, err := source.RetrieveClientAndService(accessToken)
|
bqClient, _, err := source.RetrieveClientAndService(accessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("failed to retrieve BigQuery client", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
datasetIterator := bqClient.Datasets(ctx)
|
datasetIterator := bqClient.Datasets(ctx)
|
||||||
datasetIterator.ProjectID = projectId
|
datasetIterator.ProjectID = projectId
|
||||||
@@ -149,7 +151,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to iterate through datasets: %w", err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove leading and trailing quotes
|
// Remove leading and trailing quotes
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package bigquerylisttableids
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
bigqueryapi "cloud.google.com/go/bigquery"
|
bigqueryapi "cloud.google.com/go/bigquery"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
@@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
bqutil "github.com/googleapis/genai-toolbox/internal/tools/bigquery/bigquerycommon"
|
bqutil "github.com/googleapis/genai-toolbox/internal/tools/bigquery/bigquerycommon"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
||||||
"google.golang.org/api/iterator"
|
"google.golang.org/api/iterator"
|
||||||
@@ -123,31 +125,30 @@ type Tool struct {
|
|||||||
func (t Tool) ToConfig() tools.ToolConfig {
|
func (t Tool) ToConfig() tools.ToolConfig {
|
||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
projectId, ok := mapParams[projectKey].(string)
|
projectId, ok := mapParams[projectKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", projectKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", projectKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
datasetId, ok := mapParams[datasetKey].(string)
|
datasetId, ok := mapParams[datasetKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", datasetKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", datasetKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !source.IsDatasetAllowed(projectId, datasetId) {
|
if !source.IsDatasetAllowed(projectId, datasetId) {
|
||||||
return nil, fmt.Errorf("access denied to dataset '%s' because it is not in the configured list of allowed datasets for project '%s'", datasetId, projectId)
|
return nil, util.NewClientServerError(fmt.Sprintf("access denied to dataset '%s' because it is not in the configured list of allowed datasets for project '%s'", datasetId, projectId), http.StatusInternalServerError, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
bqClient, _, err := source.RetrieveClientAndService(accessToken)
|
bqClient, _, err := source.RetrieveClientAndService(accessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("failed to retrieve BigQuery client", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dsHandle := bqClient.DatasetInProject(projectId, datasetId)
|
dsHandle := bqClient.DatasetInProject(projectId, datasetId)
|
||||||
@@ -160,7 +161,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to iterate through tables in dataset %s.%s: %w", projectId, datasetId, err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove leading and trailing quotes
|
// Remove leading and trailing quotes
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package bigquerysearchcatalog
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
dataplexapi "cloud.google.com/go/dataplex/apiv1"
|
dataplexapi "cloud.google.com/go/dataplex/apiv1"
|
||||||
@@ -26,6 +27,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
bigqueryds "github.com/googleapis/genai-toolbox/internal/sources/bigquery"
|
bigqueryds "github.com/googleapis/genai-toolbox/internal/sources/bigquery"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/iterator"
|
"google.golang.org/api/iterator"
|
||||||
)
|
)
|
||||||
@@ -186,28 +188,31 @@ func ExtractType(resourceString string) string {
|
|||||||
return typeMap[resourceString[lastIndex+1:]]
|
return typeMap[resourceString[lastIndex+1:]]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
pageSize := int32(paramsMap["pageSize"].(int))
|
pageSize := int32(paramsMap["pageSize"].(int))
|
||||||
prompt, _ := paramsMap["prompt"].(string)
|
prompt, _ := paramsMap["prompt"].(string)
|
||||||
|
|
||||||
projectIdSlice, err := parameters.ConvertAnySliceToTyped(paramsMap["projectIds"].([]any), "string")
|
projectIdSlice, err := parameters.ConvertAnySliceToTyped(paramsMap["projectIds"].([]any), "string")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't convert projectIds to array of strings: %s", err)
|
return nil, util.NewAgentError(fmt.Sprintf("can't convert projectIds to array of strings: %s", err), err)
|
||||||
}
|
}
|
||||||
projectIds := projectIdSlice.([]string)
|
projectIds := projectIdSlice.([]string)
|
||||||
|
|
||||||
datasetIdSlice, err := parameters.ConvertAnySliceToTyped(paramsMap["datasetIds"].([]any), "string")
|
datasetIdSlice, err := parameters.ConvertAnySliceToTyped(paramsMap["datasetIds"].([]any), "string")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't convert datasetIds to array of strings: %s", err)
|
return nil, util.NewAgentError(fmt.Sprintf("can't convert datasetIds to array of strings: %s", err), err)
|
||||||
}
|
}
|
||||||
datasetIds := datasetIdSlice.([]string)
|
datasetIds := datasetIdSlice.([]string)
|
||||||
|
|
||||||
typesSlice, err := parameters.ConvertAnySliceToTyped(paramsMap["types"].([]any), "string")
|
typesSlice, err := parameters.ConvertAnySliceToTyped(paramsMap["types"].([]any), "string")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't convert types to array of strings: %s", err)
|
return nil, util.NewAgentError(fmt.Sprintf("can't convert types to array of strings: %s", err), err)
|
||||||
}
|
}
|
||||||
types := typesSlice.([]string)
|
types := typesSlice.([]string)
|
||||||
|
|
||||||
@@ -223,17 +228,17 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err := accessToken.ParseBearerToken()
|
tokenStr, err := accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
catalogClient, err = dataplexClientCreator(tokenStr)
|
catalogClient, err = dataplexClientCreator(tokenStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating client from OAuth access token: %w", err)
|
return nil, util.NewClientServerError("error creating client from OAuth access token", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
it := catalogClient.SearchEntries(ctx, req)
|
it := catalogClient.SearchEntries(ctx, req)
|
||||||
if it == nil {
|
if it == nil {
|
||||||
return nil, fmt.Errorf("failed to create search entries iterator for project %q", source.BigQueryProject())
|
return nil, util.NewClientServerError(fmt.Sprintf("failed to create search entries iterator for project %q", source.BigQueryProject()), http.StatusInternalServerError, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
var results []Response
|
var results []Response
|
||||||
@@ -243,7 +248,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
entrySource := entry.DataplexEntry.GetEntrySource()
|
entrySource := entry.DataplexEntry.GetEntrySource()
|
||||||
resp := Response{
|
resp := Response{
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package bigquerysql
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -27,6 +28,7 @@ import (
|
|||||||
bigqueryds "github.com/googleapis/genai-toolbox/internal/sources/bigquery"
|
bigqueryds "github.com/googleapis/genai-toolbox/internal/sources/bigquery"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
bqutil "github.com/googleapis/genai-toolbox/internal/tools/bigquery/bigquerycommon"
|
bqutil "github.com/googleapis/genai-toolbox/internal/tools/bigquery/bigquerycommon"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
bigqueryrestapi "google.golang.org/api/bigquery/v2"
|
||||||
)
|
)
|
||||||
@@ -103,11 +105,10 @@ type Tool struct {
|
|||||||
func (t Tool) ToConfig() tools.ToolConfig {
|
func (t Tool) ToConfig() tools.ToolConfig {
|
||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
highLevelParams := make([]bigqueryapi.QueryParameter, 0, len(t.Parameters))
|
highLevelParams := make([]bigqueryapi.QueryParameter, 0, len(t.Parameters))
|
||||||
@@ -116,7 +117,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract template params %w", err)
|
return nil, util.NewAgentError("unable to extract template params", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, p := range t.Parameters {
|
for _, p := range t.Parameters {
|
||||||
@@ -127,13 +128,13 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if arrayParam, ok := p.(*parameters.ArrayParameter); ok {
|
if arrayParam, ok := p.(*parameters.ArrayParameter); ok {
|
||||||
arrayParamValue, ok := value.([]any)
|
arrayParamValue, ok := value.([]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to convert parameter `%s` to []any", name)
|
return nil, util.NewAgentError(fmt.Sprintf("unable to convert parameter `%s` to []any", name), nil)
|
||||||
}
|
}
|
||||||
itemType := arrayParam.GetItems().GetType()
|
itemType := arrayParam.GetItems().GetType()
|
||||||
var err error
|
var err error
|
||||||
value, err = parameters.ConvertAnySliceToTyped(arrayParamValue, itemType)
|
value, err = parameters.ConvertAnySliceToTyped(arrayParamValue, itemType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to convert parameter `%s` from []any to typed slice: %w", name, err)
|
return nil, util.NewAgentError(fmt.Sprintf("unable to convert parameter `%s` from []any to typed slice", name), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,7 +162,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
lowLevelParam.ParameterType.Type = "ARRAY"
|
lowLevelParam.ParameterType.Type = "ARRAY"
|
||||||
itemType, err := bqutil.BQTypeStringFromToolType(arrayParam.GetItems().GetType())
|
itemType, err := bqutil.BQTypeStringFromToolType(arrayParam.GetItems().GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("unable to get BigQuery type from tool parameter type", err)
|
||||||
}
|
}
|
||||||
lowLevelParam.ParameterType.ArrayType = &bigqueryrestapi.QueryParameterType{Type: itemType}
|
lowLevelParam.ParameterType.ArrayType = &bigqueryrestapi.QueryParameterType{Type: itemType}
|
||||||
|
|
||||||
@@ -178,7 +179,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Handle scalar types based on their defined type.
|
// Handle scalar types based on their defined type.
|
||||||
bqType, err := bqutil.BQTypeStringFromToolType(p.GetType())
|
bqType, err := bqutil.BQTypeStringFromToolType(p.GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("unable to get BigQuery type from tool parameter type", err)
|
||||||
}
|
}
|
||||||
lowLevelParam.ParameterType.Type = bqType
|
lowLevelParam.ParameterType.Type = bqType
|
||||||
lowLevelParam.ParameterValue.Value = fmt.Sprintf("%v", value)
|
lowLevelParam.ParameterValue.Value = fmt.Sprintf("%v", value)
|
||||||
@@ -190,7 +191,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if source.BigQuerySession() != nil {
|
if source.BigQuerySession() != nil {
|
||||||
session, err := source.BigQuerySession()(ctx)
|
session, err := source.BigQuerySession()(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get BigQuery session: %w", err)
|
return nil, util.NewClientServerError("failed to get BigQuery session", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
if session != nil {
|
if session != nil {
|
||||||
// Add session ID to the connection properties for subsequent calls.
|
// Add session ID to the connection properties for subsequent calls.
|
||||||
@@ -200,17 +201,20 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
bqClient, restService, err := source.RetrieveClientAndService(accessToken)
|
bqClient, restService, err := source.RetrieveClientAndService(accessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("failed to retrieve BigQuery client", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dryRunJob, err := bqutil.DryRunQuery(ctx, restService, bqClient.Project(), bqClient.Location, newStatement, lowLevelParams, connProps)
|
dryRunJob, err := bqutil.DryRunQuery(ctx, restService, bqClient.Project(), bqClient.Location, newStatement, lowLevelParams, connProps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("query validation failed: %w", err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
statementType := dryRunJob.Statistics.Query.StatementType
|
statementType := dryRunJob.Statistics.Query.StatementType
|
||||||
|
resp, err := source.RunSQL(ctx, bqClient, newStatement, statementType, highLevelParams, connProps)
|
||||||
return source.RunSQL(ctx, bqClient, newStatement, statementType, highLevelParams, connProps)
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package bigtable
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"cloud.google.com/go/bigtable"
|
"cloud.google.com/go/bigtable"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -96,24 +98,28 @@ type Tool struct {
|
|||||||
func (t Tool) ToConfig() tools.ToolConfig {
|
func (t Tool) ToConfig() tools.ToolConfig {
|
||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract template params %w", err)
|
return nil, util.NewAgentError("unable to extract template params", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
newParams, err := parameters.GetParams(t.Parameters, paramsMap)
|
newParams, err := parameters.GetParams(t.Parameters, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract standard params %w", err)
|
return nil, util.NewAgentError("unable to extract standard params", err)
|
||||||
}
|
}
|
||||||
return source.RunSQL(ctx, newStatement, t.Parameters, newParams)
|
|
||||||
|
resp, err := source.RunSQL(ctx, newStatement, t.Parameters, newParams)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
//
|
//
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
//
|
//
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
@@ -17,12 +17,14 @@ package cassandracql
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
gocql "github.com/apache/cassandra-gocql-driver/v2"
|
gocql "github.com/apache/cassandra-gocql-driver/v2"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -107,23 +109,27 @@ func (t Tool) Authorized(verifiedAuthServices []string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke implements tools.Tool.
|
// Invoke implements tools.Tool.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract template params %w", err)
|
return nil, util.NewAgentError("unable to extract template params", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
newParams, err := parameters.GetParams(t.Parameters, paramsMap)
|
newParams, err := parameters.GetParams(t.Parameters, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract standard params %w", err)
|
return nil, util.NewAgentError("unable to extract standard params", err)
|
||||||
}
|
}
|
||||||
return source.RunSQL(ctx, newStatement, newParams)
|
resp, err := source.RunSQL(ctx, newStatement, newParams)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Manifest implements tools.Tool.
|
// Manifest implements tools.Tool.
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package clickhouse
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -87,18 +89,22 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, token tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, token tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
sql, ok := paramsMap["sql"].(string)
|
sql, ok := paramsMap["sql"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to cast sql parameter %s", paramsMap["sql"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast sql parameter %s", paramsMap["sql"]), nil)
|
||||||
}
|
}
|
||||||
return source.RunSQL(ctx, sql, nil)
|
resp, err := source.RunSQL(ctx, sql, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package clickhouse
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -86,10 +88,10 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, token tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, token tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query to list all databases
|
// Query to list all databases
|
||||||
@@ -97,7 +99,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
out, err := source.RunSQL(ctx, query, nil)
|
out, err := source.RunSQL(ctx, query, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.ProcessGeneralError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return out, nil
|
return out, nil
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package clickhouse
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -90,34 +92,37 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, token tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, token tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
database, ok := mapParams[databaseKey].(string)
|
database, ok := mapParams[databaseKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", databaseKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", databaseKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query to list all tables in the specified database
|
// Query to list all tables in the specified database
|
||||||
|
// Note: formatting identifier directly is risky if input is untrusted, but standard for this tool structure.
|
||||||
query := fmt.Sprintf("SHOW TABLES FROM %s", database)
|
query := fmt.Sprintf("SHOW TABLES FROM %s", database)
|
||||||
|
|
||||||
out, err := source.RunSQL(ctx, query, nil)
|
out, err := source.RunSQL(ctx, query, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.ProcessGeneralError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, ok := out.([]any)
|
res, ok := out.([]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to convert result to list")
|
return nil, util.NewClientServerError("unable to convert result to list", http.StatusInternalServerError, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tables []map[string]any
|
var tables []map[string]any
|
||||||
for _, item := range res {
|
for _, item := range res {
|
||||||
tableMap, ok := item.(map[string]any)
|
tableMap, ok := item.(map[string]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unexpected type in result: got %T, want map[string]any", item)
|
return nil, util.NewClientServerError(fmt.Sprintf("unexpected type in result: got %T, want map[string]any", item), http.StatusInternalServerError, nil)
|
||||||
}
|
}
|
||||||
tableMap["database"] = database
|
tableMap["database"] = database
|
||||||
tables = append(tables, tableMap)
|
tables = append(tables, tableMap)
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package clickhouse
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -88,24 +90,28 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, token tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, token tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract template params: %w", err)
|
return nil, util.NewAgentError("unable to extract template params", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
newParams, err := parameters.GetParams(t.Parameters, paramsMap)
|
newParams, err := parameters.GetParams(t.Parameters, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract standard params: %w", err)
|
return nil, util.NewAgentError("unable to extract standard params", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.RunSQL(ctx, newStatement, newParams)
|
resp, err := source.RunSQL(ctx, newStatement, newParams)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -18,11 +18,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -119,17 +121,16 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool logic
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
query, ok := paramsMap["query"].(string)
|
query, ok := paramsMap["query"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("query parameter not found or not a string")
|
return nil, util.NewAgentError("query parameter not found or not a string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the access token if provided
|
// Parse the access token if provided
|
||||||
@@ -138,7 +139,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
var err error
|
var err error
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,9 +155,14 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
bodyBytes, err := json.Marshal(payload)
|
bodyBytes, err := json.Marshal(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to marshal request payload: %w", err)
|
return nil, util.NewClientServerError("failed to marshal request payload", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
return source.RunQuery(ctx, tokenStr, bodyBytes)
|
|
||||||
|
resp, err := source.RunQuery(ctx, tokenStr, bodyBytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package fhirfetchpage
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -93,24 +95,31 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
url, ok := params.AsMap()[pageURLKey].(string)
|
url, ok := params.AsMap()[pageURLKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", pageURLKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", pageURLKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.FHIRFetchPage(ctx, url, tokenStr)
|
|
||||||
|
resp, err := source.FHIRFetchPage(ctx, url, tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package fhirpatienteverything
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
@@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/googleapi"
|
"google.golang.org/api/googleapi"
|
||||||
)
|
)
|
||||||
@@ -116,26 +118,27 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedFHIRStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedFHIRStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
// ValidateAndFetchStoreID usually returns input validation errors
|
||||||
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
patientID, ok := params.AsMap()[patientIDKey].(string)
|
patientID, ok := params.AsMap()[patientIDKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", patientIDKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", patientIDKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,11 +146,11 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if val, ok := params.AsMap()[typeFilterKey]; ok {
|
if val, ok := params.AsMap()[typeFilterKey]; ok {
|
||||||
types, ok := val.([]any)
|
types, ok := val.([]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid '%s' parameter; expected a string array", typeFilterKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' parameter; expected a string array", typeFilterKey), nil)
|
||||||
}
|
}
|
||||||
typeFilterSlice, err := parameters.ConvertAnySliceToTyped(types, "string")
|
typeFilterSlice, err := parameters.ConvertAnySliceToTyped(types, "string")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't convert '%s' to array of strings: %s", typeFilterKey, err)
|
return nil, util.NewAgentError(fmt.Sprintf("can't convert '%s' to array of strings: %s", typeFilterKey, err), err)
|
||||||
}
|
}
|
||||||
if len(typeFilterSlice.([]string)) != 0 {
|
if len(typeFilterSlice.([]string)) != 0 {
|
||||||
opts = append(opts, googleapi.QueryParameter("_type", strings.Join(typeFilterSlice.([]string), ",")))
|
opts = append(opts, googleapi.QueryParameter("_type", strings.Join(typeFilterSlice.([]string), ",")))
|
||||||
@@ -156,13 +159,18 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if since, ok := params.AsMap()[sinceFilterKey]; ok {
|
if since, ok := params.AsMap()[sinceFilterKey]; ok {
|
||||||
sinceStr, ok := since.(string)
|
sinceStr, ok := since.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid '%s' parameter; expected a string", sinceFilterKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' parameter; expected a string", sinceFilterKey), nil)
|
||||||
}
|
}
|
||||||
if sinceStr != "" {
|
if sinceStr != "" {
|
||||||
opts = append(opts, googleapi.QueryParameter("_since", sinceStr))
|
opts = append(opts, googleapi.QueryParameter("_since", sinceStr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.FHIRPatientEverything(storeID, patientID, tokenStr, opts)
|
|
||||||
|
resp, err := source.FHIRPatientEverything(storeID, patientID, tokenStr, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package fhirpatientsearch
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
@@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/googleapi"
|
"google.golang.org/api/googleapi"
|
||||||
)
|
)
|
||||||
@@ -150,22 +152,22 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedFHIRStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedFHIRStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,14 +181,14 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
var ok bool
|
var ok bool
|
||||||
summary, ok = v.(bool)
|
summary, ok = v.(bool)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid '%s' parameter; expected a boolean", summaryKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' parameter; expected a boolean", summaryKey), nil)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
val, ok := v.(string)
|
val, ok := v.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid parameter '%s'; expected a string", k)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid parameter '%s'; expected a string", k), nil)
|
||||||
}
|
}
|
||||||
if val == "" {
|
if val == "" {
|
||||||
continue
|
continue
|
||||||
@@ -205,7 +207,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
parts := strings.Split(val, "/")
|
parts := strings.Split(val, "/")
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
return nil, fmt.Errorf("invalid '%s' format; expected YYYY-MM-DD/YYYY-MM-DD", k)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' format; expected YYYY-MM-DD/YYYY-MM-DD", k), nil)
|
||||||
}
|
}
|
||||||
var values []string
|
var values []string
|
||||||
if parts[0] != "" {
|
if parts[0] != "" {
|
||||||
@@ -229,13 +231,17 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
case familyNameKey:
|
case familyNameKey:
|
||||||
opts = append(opts, googleapi.QueryParameter("family", val))
|
opts = append(opts, googleapi.QueryParameter("family", val))
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unexpected parameter key %q", k)
|
return nil, util.NewAgentError(fmt.Sprintf("unexpected parameter key %q", k), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if summary {
|
if summary {
|
||||||
opts = append(opts, googleapi.QueryParameter("_summary", "text"))
|
opts = append(opts, googleapi.QueryParameter("_summary", "text"))
|
||||||
}
|
}
|
||||||
return source.FHIRPatientSearch(storeID, tokenStr, opts)
|
resp, err := source.FHIRPatientSearch(storeID, tokenStr, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package gethealthcaredataset
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/healthcare/v1"
|
"google.golang.org/api/healthcare/v1"
|
||||||
)
|
)
|
||||||
@@ -90,19 +92,23 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.GetDataset(tokenStr)
|
resp, err := source.GetDataset(tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package getdicomstore
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/healthcare/v1"
|
"google.golang.org/api/healthcare/v1"
|
||||||
)
|
)
|
||||||
@@ -107,23 +109,27 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.GetDICOMStore(storeID, tokenStr)
|
resp, err := source.GetDICOMStore(storeID, tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package getdicomstoremetrics
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/healthcare/v1"
|
"google.golang.org/api/healthcare/v1"
|
||||||
)
|
)
|
||||||
@@ -107,23 +109,27 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.GetDICOMStoreMetrics(storeID, tokenStr)
|
resp, err := source.GetDICOMStoreMetrics(storeID, tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package getfhirresource
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -112,32 +114,36 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedFHIRStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedFHIRStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
resType, ok := params.AsMap()[typeKey].(string)
|
resType, ok := params.AsMap()[typeKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", typeKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", typeKey), nil)
|
||||||
}
|
}
|
||||||
resID, ok := params.AsMap()[idKey].(string)
|
resID, ok := params.AsMap()[idKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected a string", idKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected a string", idKey), nil)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.GetFHIRResource(storeID, resType, resID, tokenStr)
|
resp, err := source.GetFHIRResource(storeID, resType, resID, tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package getfhirstore
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/healthcare/v1"
|
"google.golang.org/api/healthcare/v1"
|
||||||
)
|
)
|
||||||
@@ -107,23 +109,27 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedFHIRStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedFHIRStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.GetFHIRStore(storeID, tokenStr)
|
resp, err := source.GetFHIRStore(storeID, tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package getfhirstoremetrics
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/healthcare/v1"
|
"google.golang.org/api/healthcare/v1"
|
||||||
)
|
)
|
||||||
@@ -107,23 +109,27 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedFHIRStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedFHIRStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.GetFHIRStoreMetrics(storeID, tokenStr)
|
resp, err := source.GetFHIRStoreMetrics(storeID, tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package listdicomstores
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/healthcare/v1"
|
"google.golang.org/api/healthcare/v1"
|
||||||
)
|
)
|
||||||
@@ -90,19 +92,23 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.ListDICOMStores(tokenStr)
|
resp, err := source.ListDICOMStores(tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package listfhirstores
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/healthcare/v1"
|
"google.golang.org/api/healthcare/v1"
|
||||||
)
|
)
|
||||||
@@ -90,19 +92,23 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.ListFHIRStores(tokenStr)
|
resp, err := source.ListFHIRStores(tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package retrieverendereddicominstance
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -117,40 +119,44 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
study, ok := params.AsMap()[studyInstanceUIDKey].(string)
|
study, ok := params.AsMap()[studyInstanceUIDKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid '%s' parameter; expected a string", studyInstanceUIDKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' parameter; expected a string", studyInstanceUIDKey), nil)
|
||||||
}
|
}
|
||||||
series, ok := params.AsMap()[seriesInstanceUIDKey].(string)
|
series, ok := params.AsMap()[seriesInstanceUIDKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid '%s' parameter; expected a string", seriesInstanceUIDKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' parameter; expected a string", seriesInstanceUIDKey), nil)
|
||||||
}
|
}
|
||||||
sop, ok := params.AsMap()[sopInstanceUIDKey].(string)
|
sop, ok := params.AsMap()[sopInstanceUIDKey].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid '%s' parameter; expected a string", sopInstanceUIDKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' parameter; expected a string", sopInstanceUIDKey), nil)
|
||||||
}
|
}
|
||||||
frame, ok := params.AsMap()[frameNumberKey].(int)
|
frame, ok := params.AsMap()[frameNumberKey].(int)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid '%s' parameter; expected an integer", frameNumberKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' parameter; expected an integer", frameNumberKey), nil)
|
||||||
}
|
}
|
||||||
return source.RetrieveRenderedDICOMInstance(storeID, study, series, sop, frame, tokenStr)
|
resp, err := source.RetrieveRenderedDICOMInstance(storeID, study, series, sop, frame, tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package searchdicominstances
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
@@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/googleapi"
|
"google.golang.org/api/googleapi"
|
||||||
)
|
)
|
||||||
@@ -131,33 +133,33 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
opts, err := common.ParseDICOMSearchParameters(params, []string{sopInstanceUIDKey, patientNameKey, patientIDKey, accessionNumberKey, referringPhysicianNameKey, studyDateKey, modalityKey})
|
opts, err := common.ParseDICOMSearchParameters(params, []string{sopInstanceUIDKey, patientNameKey, patientIDKey, accessionNumberKey, referringPhysicianNameKey, studyDateKey, modalityKey})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to parse DICOM search parameters", err)
|
||||||
}
|
}
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
dicomWebPath := "instances"
|
dicomWebPath := "instances"
|
||||||
if studyInstanceUID, ok := paramsMap[studyInstanceUIDKey]; ok {
|
if studyInstanceUID, ok := paramsMap[studyInstanceUIDKey]; ok {
|
||||||
id, ok := studyInstanceUID.(string)
|
id, ok := studyInstanceUID.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid '%s' parameter; expected a string", studyInstanceUIDKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' parameter; expected a string", studyInstanceUIDKey), nil)
|
||||||
}
|
}
|
||||||
if id != "" {
|
if id != "" {
|
||||||
dicomWebPath = fmt.Sprintf("studies/%s/instances", id)
|
dicomWebPath = fmt.Sprintf("studies/%s/instances", id)
|
||||||
@@ -166,7 +168,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if seriesInstanceUID, ok := paramsMap[seriesInstanceUIDKey]; ok {
|
if seriesInstanceUID, ok := paramsMap[seriesInstanceUIDKey]; ok {
|
||||||
id, ok := seriesInstanceUID.(string)
|
id, ok := seriesInstanceUID.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid '%s' parameter; expected a string", seriesInstanceUIDKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' parameter; expected a string", seriesInstanceUIDKey), nil)
|
||||||
}
|
}
|
||||||
if id != "" {
|
if id != "" {
|
||||||
if dicomWebPath != "instances" {
|
if dicomWebPath != "instances" {
|
||||||
@@ -176,7 +178,11 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.SearchDICOM(t.Type, storeID, dicomWebPath, tokenStr, opts)
|
resp, err := source.SearchDICOM(t.Type, storeID, dicomWebPath, tokenStr, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package searchdicomseries
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/googleapi"
|
"google.golang.org/api/googleapi"
|
||||||
)
|
)
|
||||||
@@ -128,40 +130,44 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
opts, err := common.ParseDICOMSearchParameters(params, []string{seriesInstanceUIDKey, patientNameKey, patientIDKey, accessionNumberKey, referringPhysicianNameKey, studyDateKey, modalityKey})
|
opts, err := common.ParseDICOMSearchParameters(params, []string{seriesInstanceUIDKey, patientNameKey, patientIDKey, accessionNumberKey, referringPhysicianNameKey, studyDateKey, modalityKey})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to parse DICOM search parameters", err)
|
||||||
}
|
}
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
dicomWebPath := "series"
|
dicomWebPath := "series"
|
||||||
if studyInstanceUID, ok := paramsMap[studyInstanceUIDKey]; ok {
|
if studyInstanceUID, ok := paramsMap[studyInstanceUIDKey]; ok {
|
||||||
id, ok := studyInstanceUID.(string)
|
id, ok := studyInstanceUID.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid '%s' parameter; expected a string", studyInstanceUIDKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid '%s' parameter; expected a string", studyInstanceUIDKey), nil)
|
||||||
}
|
}
|
||||||
if id != "" {
|
if id != "" {
|
||||||
dicomWebPath = fmt.Sprintf("studies/%s/series", id)
|
dicomWebPath = fmt.Sprintf("studies/%s/series", id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.SearchDICOM(t.Type, storeID, dicomWebPath, tokenStr, opts)
|
resp, err := source.SearchDICOM(t.Type, storeID, dicomWebPath, tokenStr, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package searchdicomstudies
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
"github.com/googleapis/genai-toolbox/internal/tools/cloudhealthcare/common"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/googleapi"
|
"google.golang.org/api/googleapi"
|
||||||
)
|
)
|
||||||
@@ -124,28 +126,32 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
storeID, err := common.ValidateAndFetchStoreID(params, source.AllowedDICOMStores())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to validate store ID", err)
|
||||||
}
|
}
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenStr, err = accessToken.ParseBearerToken()
|
tokenStr, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing access token: %w", err)
|
return nil, util.NewClientServerError("error parsing access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
opts, err := common.ParseDICOMSearchParameters(params, []string{studyInstanceUIDKey, patientNameKey, patientIDKey, accessionNumberKey, referringPhysicianNameKey, studyDateKey})
|
opts, err := common.ParseDICOMSearchParameters(params, []string{studyInstanceUIDKey, patientNameKey, patientIDKey, accessionNumberKey, referringPhysicianNameKey, studyDateKey})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError("failed to parse DICOM search parameters", err)
|
||||||
}
|
}
|
||||||
dicomWebPath := "studies"
|
dicomWebPath := "studies"
|
||||||
return source.SearchDICOM(t.Type, storeID, dicomWebPath, tokenStr, opts)
|
resp, err := source.SearchDICOM(t.Type, storeID, dicomWebPath, tokenStr, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -16,11 +16,13 @@ package cloudloggingadminlistlognames
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -89,10 +91,10 @@ type Tool struct {
|
|||||||
Parameters parameters.Parameters `yaml:"parameters"`
|
Parameters parameters.Parameters `yaml:"parameters"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
limit := defaultLimit
|
limit := defaultLimit
|
||||||
@@ -100,18 +102,22 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if val, ok := paramsMap["limit"].(int); ok && val > 0 {
|
if val, ok := paramsMap["limit"].(int); ok && val > 0 {
|
||||||
limit = val
|
limit = val
|
||||||
} else if ok && val < 0 {
|
} else if ok && val < 0 {
|
||||||
return nil, fmt.Errorf("limit must be greater than or equal to 1")
|
return nil, util.NewAgentError("limit must be greater than or equal to 1", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenString := ""
|
tokenString := ""
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenString, err = accessToken.ParseBearerToken()
|
tokenString, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse access token: %w", err)
|
return nil, util.NewClientServerError("failed to parse access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.ListLogNames(ctx, limit, tokenString)
|
resp, err := source.ListLogNames(ctx, limit, tokenString)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) ParseParams(data map[string]any, claimsMap map[string]map[string]any) (parameters.ParamValues, error) {
|
func (t Tool) ParseParams(data map[string]any, claimsMap map[string]map[string]any) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -16,11 +16,13 @@ package cloudloggingadminlistresourcetypes
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -84,21 +86,25 @@ type Tool struct {
|
|||||||
mcpManifest tools.McpManifest
|
mcpManifest tools.McpManifest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenString := ""
|
tokenString := ""
|
||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenString, err = accessToken.ParseBearerToken()
|
tokenString, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse access token: %w", err)
|
return nil, util.NewClientServerError("failed to parse access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.ListResourceTypes(ctx, tokenString)
|
resp, err := source.ListResourceTypes(ctx, tokenString)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) ParseParams(data map[string]any, claimsMap map[string]map[string]any) (parameters.ParamValues, error) {
|
func (t Tool) ParseParams(data map[string]any, claimsMap map[string]map[string]any) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package cloudloggingadminquerylogs
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
@@ -23,6 +24,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
cla "github.com/googleapis/genai-toolbox/internal/sources/cloudloggingadmin"
|
cla "github.com/googleapis/genai-toolbox/internal/sources/cloudloggingadmin"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -104,10 +106,10 @@ type Tool struct {
|
|||||||
mcpManifest tools.McpManifest
|
mcpManifest tools.McpManifest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse parameters
|
// Parse parameters
|
||||||
@@ -119,7 +121,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if val, ok := paramsMap["limit"].(int); ok && val > 0 {
|
if val, ok := paramsMap["limit"].(int); ok && val > 0 {
|
||||||
limit = val
|
limit = val
|
||||||
} else if ok && val < 0 {
|
} else if ok && val < 0 {
|
||||||
return nil, fmt.Errorf("limit must be greater than or equal to 1")
|
return nil, util.NewAgentError("limit must be greater than or equal to 1", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for verbosity of output
|
// Check for verbosity of output
|
||||||
@@ -129,7 +131,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
var filter string
|
var filter string
|
||||||
if f, ok := paramsMap["filter"].(string); ok {
|
if f, ok := paramsMap["filter"].(string); ok {
|
||||||
if len(f) == 0 {
|
if len(f) == 0 {
|
||||||
return nil, fmt.Errorf("filter cannot be empty if provided")
|
return nil, util.NewAgentError("filter cannot be empty if provided", nil)
|
||||||
}
|
}
|
||||||
filter = f
|
filter = f
|
||||||
}
|
}
|
||||||
@@ -138,7 +140,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
var startTime string
|
var startTime string
|
||||||
if val, ok := paramsMap["startTime"].(string); ok && val != "" {
|
if val, ok := paramsMap["startTime"].(string); ok && val != "" {
|
||||||
if _, err := time.Parse(time.RFC3339, val); err != nil {
|
if _, err := time.Parse(time.RFC3339, val); err != nil {
|
||||||
return nil, fmt.Errorf("startTime must be in RFC3339 format (e.g., 2025-12-09T00:00:00Z): %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("startTime must be in RFC3339 format (e.g., 2025-12-09T00:00:00Z): %v", err), err)
|
||||||
}
|
}
|
||||||
startTime = val
|
startTime = val
|
||||||
} else {
|
} else {
|
||||||
@@ -149,7 +151,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
var endTime string
|
var endTime string
|
||||||
if val, ok := paramsMap["endTime"].(string); ok && val != "" {
|
if val, ok := paramsMap["endTime"].(string); ok && val != "" {
|
||||||
if _, err := time.Parse(time.RFC3339, val); err != nil {
|
if _, err := time.Parse(time.RFC3339, val); err != nil {
|
||||||
return nil, fmt.Errorf("endTime must be in RFC3339 format (e.g., 2025-12-09T23:59:59Z): %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("endTime must be in RFC3339 format (e.g., 2025-12-09T23:59:59Z): %v", err), err)
|
||||||
}
|
}
|
||||||
endTime = val
|
endTime = val
|
||||||
}
|
}
|
||||||
@@ -158,7 +160,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if source.UseClientAuthorization() {
|
if source.UseClientAuthorization() {
|
||||||
tokenString, err = accessToken.ParseBearerToken()
|
tokenString, err = accessToken.ParseBearerToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse access token: %w", err)
|
return nil, util.NewClientServerError("failed to parse access token", http.StatusUnauthorized, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +173,11 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
Limit: limit,
|
Limit: limit,
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.QueryLogs(ctx, queryParams, tokenString)
|
resp, err := source.QueryLogs(ctx, queryParams, tokenString)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) ParseParams(data map[string]any, claimsMap map[string]map[string]any) (parameters.ParamValues, error) {
|
func (t Tool) ParseParams(data map[string]any, claimsMap map[string]map[string]any) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -93,22 +94,26 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
projectID, ok := paramsMap["projectId"].(string)
|
projectID, ok := paramsMap["projectId"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("projectId parameter not found or not a string")
|
return nil, util.NewAgentError("projectId parameter not found or not a string", nil)
|
||||||
}
|
}
|
||||||
query, ok := paramsMap["query"].(string)
|
query, ok := paramsMap["query"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("query parameter not found or not a string")
|
return nil, util.NewAgentError("query parameter not found or not a string", nil)
|
||||||
}
|
}
|
||||||
return source.RunQuery(projectID, query)
|
resp, err := source.RunQuery(projectID, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package cloudsqlcloneinstance
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
sqladmin "google.golang.org/api/sqladmin/v1"
|
sqladmin "google.golang.org/api/sqladmin/v1"
|
||||||
)
|
)
|
||||||
@@ -124,31 +126,35 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'project' parameter: %v", paramsMap["project"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'project' parameter: %v", paramsMap["project"]), nil)
|
||||||
}
|
}
|
||||||
sourceInstanceName, ok := paramsMap["sourceInstanceName"].(string)
|
sourceInstanceName, ok := paramsMap["sourceInstanceName"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'sourceInstanceName' parameter: %v", paramsMap["sourceInstanceName"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'sourceInstanceName' parameter: %v", paramsMap["sourceInstanceName"]), nil)
|
||||||
}
|
}
|
||||||
destinationInstanceName, ok := paramsMap["destinationInstanceName"].(string)
|
destinationInstanceName, ok := paramsMap["destinationInstanceName"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'destinationInstanceName' parameter: %v", paramsMap["destinationInstanceName"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'destinationInstanceName' parameter: %v", paramsMap["destinationInstanceName"]), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
pointInTime, _ := paramsMap["pointInTime"].(string)
|
pointInTime, _ := paramsMap["pointInTime"].(string)
|
||||||
preferredZone, _ := paramsMap["preferredZone"].(string)
|
preferredZone, _ := paramsMap["preferredZone"].(string)
|
||||||
preferredSecondaryZone, _ := paramsMap["preferredSecondaryZone"].(string)
|
preferredSecondaryZone, _ := paramsMap["preferredSecondaryZone"].(string)
|
||||||
|
|
||||||
return source.CloneInstance(ctx, project, sourceInstanceName, destinationInstanceName, pointInTime, preferredZone, preferredSecondaryZone, string(accessToken))
|
resp, err := source.CloneInstance(ctx, project, sourceInstanceName, destinationInstanceName, pointInTime, preferredZone, preferredSecondaryZone, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package cloudsqlcreatebackup
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/sqladmin/v1"
|
"google.golang.org/api/sqladmin/v1"
|
||||||
)
|
)
|
||||||
@@ -120,26 +122,30 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'project' parameter: %v", paramsMap["project"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'project' parameter: %v", paramsMap["project"]), nil)
|
||||||
}
|
}
|
||||||
instance, ok := paramsMap["instance"].(string)
|
instance, ok := paramsMap["instance"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'instance' parameter: %v", paramsMap["instance"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'instance' parameter: %v", paramsMap["instance"]), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
location, _ := paramsMap["location"].(string)
|
location, _ := paramsMap["location"].(string)
|
||||||
description, _ := paramsMap["backup_description"].(string)
|
description, _ := paramsMap["backup_description"].(string)
|
||||||
|
|
||||||
return source.InsertBackupRun(ctx, project, instance, location, description, string(accessToken))
|
resp, err := source.InsertBackupRun(ctx, project, instance, location, description, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package cloudsqlcreatedatabase
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -117,27 +119,31 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'project' parameter")
|
return nil, util.NewAgentError("missing 'project' parameter", nil)
|
||||||
}
|
}
|
||||||
instance, ok := paramsMap["instance"].(string)
|
instance, ok := paramsMap["instance"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'instance' parameter")
|
return nil, util.NewAgentError("missing 'instance' parameter", nil)
|
||||||
}
|
}
|
||||||
name, ok := paramsMap["name"].(string)
|
name, ok := paramsMap["name"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'name' parameter")
|
return nil, util.NewAgentError("missing 'name' parameter", nil)
|
||||||
}
|
}
|
||||||
return source.CreateDatabase(ctx, name, project, instance, string(accessToken))
|
resp, err := source.CreateDatabase(ctx, name, project, instance, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package cloudsqlcreateusers
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -119,30 +121,34 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'project' parameter")
|
return nil, util.NewAgentError("missing 'project' parameter", nil)
|
||||||
}
|
}
|
||||||
instance, ok := paramsMap["instance"].(string)
|
instance, ok := paramsMap["instance"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'instance' parameter")
|
return nil, util.NewAgentError("missing 'instance' parameter", nil)
|
||||||
}
|
}
|
||||||
name, ok := paramsMap["name"].(string)
|
name, ok := paramsMap["name"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'name' parameter")
|
return nil, util.NewAgentError("missing 'name' parameter", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
iamUser, _ := paramsMap["iamUser"].(bool)
|
iamUser, _ := paramsMap["iamUser"].(bool)
|
||||||
password, _ := paramsMap["password"].(string)
|
password, _ := paramsMap["password"].(string)
|
||||||
return source.CreateUsers(ctx, project, instance, name, password, iamUser, string(accessToken))
|
resp, err := source.CreateUsers(ctx, project, instance, name, password, iamUser, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package cloudsqlgetinstances
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -117,23 +119,27 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
projectId, ok := paramsMap["projectId"].(string)
|
projectId, ok := paramsMap["projectId"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'projectId' parameter")
|
return nil, util.NewAgentError("missing 'projectId' parameter", nil)
|
||||||
}
|
}
|
||||||
instanceId, ok := paramsMap["instanceId"].(string)
|
instanceId, ok := paramsMap["instanceId"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'instanceId' parameter")
|
return nil, util.NewAgentError("missing 'instanceId' parameter", nil)
|
||||||
}
|
}
|
||||||
return source.GetInstance(ctx, projectId, instanceId, string(accessToken))
|
resp, err := source.GetInstance(ctx, projectId, instanceId, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package cloudsqllistdatabases
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -116,23 +118,27 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'project' parameter")
|
return nil, util.NewAgentError("missing 'project' parameter", nil)
|
||||||
}
|
}
|
||||||
instance, ok := paramsMap["instance"].(string)
|
instance, ok := paramsMap["instance"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'instance' parameter")
|
return nil, util.NewAgentError("missing 'instance' parameter", nil)
|
||||||
}
|
}
|
||||||
return source.ListDatabase(ctx, project, instance, string(accessToken))
|
resp, err := source.ListDatabase(ctx, project, instance, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package cloudsqllistinstances
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -115,19 +117,23 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'project' parameter")
|
return nil, util.NewAgentError("missing 'project' parameter", nil)
|
||||||
}
|
}
|
||||||
return source.ListInstance(ctx, project, string(accessToken))
|
resp, err := source.ListInstance(ctx, project, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package cloudsqlrestorebackup
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/sqladmin/v1"
|
"google.golang.org/api/sqladmin/v1"
|
||||||
)
|
)
|
||||||
@@ -120,29 +122,33 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
targetProject, ok := paramsMap["target_project"].(string)
|
targetProject, ok := paramsMap["target_project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'target_project' parameter: %v", paramsMap["target_project"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'target_project' parameter: %v", paramsMap["target_project"]), nil)
|
||||||
}
|
}
|
||||||
targetInstance, ok := paramsMap["target_instance"].(string)
|
targetInstance, ok := paramsMap["target_instance"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'target_instance' parameter: %v", paramsMap["target_instance"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'target_instance' parameter: %v", paramsMap["target_instance"]), nil)
|
||||||
}
|
}
|
||||||
backupID, ok := paramsMap["backup_id"].(string)
|
backupID, ok := paramsMap["backup_id"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'backup_id' parameter: %v", paramsMap["backup_id"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'backup_id' parameter: %v", paramsMap["backup_id"]), nil)
|
||||||
}
|
}
|
||||||
sourceProject, _ := paramsMap["source_project"].(string)
|
sourceProject, _ := paramsMap["source_project"].(string)
|
||||||
sourceInstance, _ := paramsMap["source_instance"].(string)
|
sourceInstance, _ := paramsMap["source_instance"].(string)
|
||||||
|
|
||||||
return source.RestoreBackup(ctx, targetProject, targetInstance, sourceProject, sourceInstance, backupID, string(accessToken))
|
resp, err := source.RestoreBackup(ctx, targetProject, targetInstance, sourceProject, sourceInstance, backupID, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package cloudsqlwaitforoperation
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/sqladmin/v1"
|
"google.golang.org/api/sqladmin/v1"
|
||||||
)
|
)
|
||||||
@@ -210,21 +212,21 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'project' parameter")
|
return nil, util.NewAgentError("missing 'project' parameter", nil)
|
||||||
}
|
}
|
||||||
operationID, ok := paramsMap["operation"].(string)
|
operationID, ok := paramsMap["operation"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'operation' parameter")
|
return nil, util.NewAgentError("missing 'operation' parameter", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
||||||
@@ -232,7 +234,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
service, err := source.GetService(ctx, string(accessToken))
|
service, err := source.GetService(ctx, string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
delay := t.Delay
|
delay := t.Delay
|
||||||
@@ -244,13 +246,13 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
for retries < maxRetries {
|
for retries < maxRetries {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil, fmt.Errorf("timed out waiting for operation: %w", ctx.Err())
|
return nil, util.NewClientServerError("timed out waiting for operation", http.StatusRequestTimeout, ctx.Err())
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
op, err := source.GetWaitForOperations(ctx, service, project, operationID, cloudSQLConnectionMessageTemplate, delay)
|
op, err := source.GetWaitForOperations(ctx, service, project, operationID, cloudSQLConnectionMessageTemplate, delay)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.ProcessGcpError(err)
|
||||||
} else if op != nil {
|
} else if op != nil {
|
||||||
return op, nil
|
return op, nil
|
||||||
}
|
}
|
||||||
@@ -262,7 +264,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
retries++
|
retries++
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("exceeded max retries waiting for operation")
|
return nil, util.NewClientServerError("exceeded max retries waiting for operation", http.StatusGatewayTimeout, fmt.Errorf("exceeded max retries"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package cloudsqlmssqlcreateinstance
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/sqladmin/v1"
|
"google.golang.org/api/sqladmin/v1"
|
||||||
)
|
)
|
||||||
@@ -121,33 +123,33 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'project' parameter: %s", paramsMap["project"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'project' parameter: %s", paramsMap["project"]), nil)
|
||||||
}
|
}
|
||||||
name, ok := paramsMap["name"].(string)
|
name, ok := paramsMap["name"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'name' parameter: %s", paramsMap["name"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'name' parameter: %s", paramsMap["name"]), nil)
|
||||||
}
|
}
|
||||||
dbVersion, ok := paramsMap["databaseVersion"].(string)
|
dbVersion, ok := paramsMap["databaseVersion"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'databaseVersion' parameter: %s", paramsMap["databaseVersion"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'databaseVersion' parameter: %s", paramsMap["databaseVersion"]), nil)
|
||||||
}
|
}
|
||||||
rootPassword, ok := paramsMap["rootPassword"].(string)
|
rootPassword, ok := paramsMap["rootPassword"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'rootPassword' parameter: %s", paramsMap["rootPassword"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'rootPassword' parameter: %s", paramsMap["rootPassword"]), nil)
|
||||||
}
|
}
|
||||||
editionPreset, ok := paramsMap["editionPreset"].(string)
|
editionPreset, ok := paramsMap["editionPreset"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error casting 'editionPreset' parameter: %s", paramsMap["editionPreset"])
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'editionPreset' parameter: %s", paramsMap["editionPreset"]), nil)
|
||||||
}
|
}
|
||||||
settings := sqladmin.Settings{}
|
settings := sqladmin.Settings{}
|
||||||
switch strings.ToLower(editionPreset) {
|
switch strings.ToLower(editionPreset) {
|
||||||
@@ -164,9 +166,13 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
settings.DataDiskSizeGb = 100
|
settings.DataDiskSizeGb = 100
|
||||||
settings.DataDiskType = "PD_SSD"
|
settings.DataDiskType = "PD_SSD"
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid 'editionPreset': %q. Must be either 'Production' or 'Development'", editionPreset)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid 'editionPreset': %q. Must be either 'Production' or 'Development'", editionPreset), nil)
|
||||||
}
|
}
|
||||||
return source.CreateInstance(ctx, project, name, dbVersion, rootPassword, settings, string(accessToken))
|
resp, err := source.CreateInstance(ctx, project, name, dbVersion, rootPassword, settings, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package cloudsqlmysqlcreateinstance
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
sqladmin "google.golang.org/api/sqladmin/v1"
|
sqladmin "google.golang.org/api/sqladmin/v1"
|
||||||
)
|
)
|
||||||
@@ -121,33 +123,33 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'project' parameter")
|
return nil, util.NewAgentError("missing 'project' parameter", nil)
|
||||||
}
|
}
|
||||||
name, ok := paramsMap["name"].(string)
|
name, ok := paramsMap["name"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'name' parameter")
|
return nil, util.NewAgentError("missing 'name' parameter", nil)
|
||||||
}
|
}
|
||||||
dbVersion, ok := paramsMap["databaseVersion"].(string)
|
dbVersion, ok := paramsMap["databaseVersion"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'databaseVersion' parameter")
|
return nil, util.NewAgentError("missing 'databaseVersion' parameter", nil)
|
||||||
}
|
}
|
||||||
rootPassword, ok := paramsMap["rootPassword"].(string)
|
rootPassword, ok := paramsMap["rootPassword"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'rootPassword' parameter")
|
return nil, util.NewAgentError("missing 'rootPassword' parameter", nil)
|
||||||
}
|
}
|
||||||
editionPreset, ok := paramsMap["editionPreset"].(string)
|
editionPreset, ok := paramsMap["editionPreset"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'editionPreset' parameter")
|
return nil, util.NewAgentError("missing 'editionPreset' parameter", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
settings := sqladmin.Settings{}
|
settings := sqladmin.Settings{}
|
||||||
@@ -165,10 +167,14 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
settings.DataDiskSizeGb = 100
|
settings.DataDiskSizeGb = 100
|
||||||
settings.DataDiskType = "PD_SSD"
|
settings.DataDiskType = "PD_SSD"
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid 'editionPreset': %q. Must be either 'Production' or 'Development'", editionPreset)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid 'editionPreset': %q. Must be either 'Production' or 'Development'", editionPreset), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.CreateInstance(ctx, project, name, dbVersion, rootPassword, settings, string(accessToken))
|
resp, err := source.CreateInstance(ctx, project, name, dbVersion, rootPassword, settings, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package cloudsqlpgcreateinstances
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
sqladmin "google.golang.org/api/sqladmin/v1"
|
sqladmin "google.golang.org/api/sqladmin/v1"
|
||||||
)
|
)
|
||||||
@@ -121,33 +123,33 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'project' parameter")
|
return nil, util.NewAgentError("missing 'project' parameter", nil)
|
||||||
}
|
}
|
||||||
name, ok := paramsMap["name"].(string)
|
name, ok := paramsMap["name"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'name' parameter")
|
return nil, util.NewAgentError("missing 'name' parameter", nil)
|
||||||
}
|
}
|
||||||
dbVersion, ok := paramsMap["databaseVersion"].(string)
|
dbVersion, ok := paramsMap["databaseVersion"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'databaseVersion' parameter")
|
return nil, util.NewAgentError("missing 'databaseVersion' parameter", nil)
|
||||||
}
|
}
|
||||||
rootPassword, ok := paramsMap["rootPassword"].(string)
|
rootPassword, ok := paramsMap["rootPassword"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'rootPassword' parameter")
|
return nil, util.NewAgentError("missing 'rootPassword' parameter", nil)
|
||||||
}
|
}
|
||||||
editionPreset, ok := paramsMap["editionPreset"].(string)
|
editionPreset, ok := paramsMap["editionPreset"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing 'editionPreset' parameter")
|
return nil, util.NewAgentError("missing 'editionPreset' parameter", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
settings := sqladmin.Settings{}
|
settings := sqladmin.Settings{}
|
||||||
@@ -165,9 +167,13 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
settings.DataDiskSizeGb = 100
|
settings.DataDiskSizeGb = 100
|
||||||
settings.DataDiskType = "PD_SSD"
|
settings.DataDiskType = "PD_SSD"
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid 'editionPreset': %q. Must be either 'Production' or 'Development'", editionPreset)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid 'editionPreset': %q. Must be either 'Production' or 'Development'", editionPreset), nil)
|
||||||
}
|
}
|
||||||
return source.CreateInstance(ctx, project, name, dbVersion, rootPassword, settings, string(accessToken))
|
resp, err := source.CreateInstance(ctx, project, name, dbVersion, rootPassword, settings, string(accessToken))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package cloudsqlpgupgradeprecheck
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
sqladmin "google.golang.org/api/sqladmin/v1"
|
sqladmin "google.golang.org/api/sqladmin/v1"
|
||||||
)
|
)
|
||||||
@@ -132,31 +134,31 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's logic.
|
// Invoke executes the tool's logic.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
project, ok := paramsMap["project"].(string)
|
project, ok := paramsMap["project"].(string)
|
||||||
if !ok || project == "" {
|
if !ok || project == "" {
|
||||||
return nil, fmt.Errorf("missing or empty 'project' parameter")
|
return nil, util.NewAgentError("missing or empty 'project' parameter", nil)
|
||||||
}
|
}
|
||||||
instanceName, ok := paramsMap["instance"].(string)
|
instanceName, ok := paramsMap["instance"].(string)
|
||||||
if !ok || instanceName == "" {
|
if !ok || instanceName == "" {
|
||||||
return nil, fmt.Errorf("missing or empty 'instance' parameter")
|
return nil, util.NewAgentError("missing or empty 'instance' parameter", nil)
|
||||||
}
|
}
|
||||||
targetVersion, ok := paramsMap["targetDatabaseVersion"].(string)
|
targetVersion, ok := paramsMap["targetDatabaseVersion"].(string)
|
||||||
if !ok || targetVersion == "" {
|
if !ok || targetVersion == "" {
|
||||||
// This should not happen due to the default value
|
// This should not happen due to the default value
|
||||||
return nil, fmt.Errorf("missing or empty 'targetDatabaseVersion' parameter")
|
return nil, util.NewAgentError("missing or empty 'targetDatabaseVersion' parameter", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
service, err := source.GetService(ctx, string(accessToken))
|
service, err := source.GetService(ctx, string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get HTTP client from source: %w", err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
reqBody := &sqladmin.InstancesPreCheckMajorVersionUpgradeRequest{
|
reqBody := &sqladmin.InstancesPreCheckMajorVersionUpgradeRequest{
|
||||||
@@ -168,7 +170,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
call := service.Instances.PreCheckMajorVersionUpgrade(project, instanceName, reqBody).Context(ctx)
|
call := service.Instances.PreCheckMajorVersionUpgrade(project, instanceName, reqBody).Context(ctx)
|
||||||
op, err := call.Do()
|
op, err := call.Do()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to start pre-check operation: %w", err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
const pollTimeout = 20 * time.Second
|
const pollTimeout = 20 * time.Second
|
||||||
@@ -177,7 +179,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
for time.Now().Before(cutoffTime) {
|
for time.Now().Before(cutoffTime) {
|
||||||
currentOp, err := service.Operations.Get(project, op.Name).Context(ctx).Do()
|
currentOp, err := service.Operations.Get(project, op.Name).Context(ctx).Do()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get operation status: %w", err)
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if currentOp.Status == "DONE" {
|
if currentOp.Status == "DONE" {
|
||||||
@@ -186,7 +188,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if currentOp.Error.Errors[0].Code != "" {
|
if currentOp.Error.Errors[0].Code != "" {
|
||||||
errMsg = fmt.Sprintf("%s (Code: %s)", errMsg, currentOp.Error.Errors[0].Code)
|
errMsg = fmt.Sprintf("%s (Code: %s)", errMsg, currentOp.Error.Errors[0].Code)
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("%s", errMsg)
|
return nil, util.NewClientServerError(errMsg, http.StatusInternalServerError, fmt.Errorf("pre-check operation failed with error: %s", errMsg))
|
||||||
}
|
}
|
||||||
|
|
||||||
var preCheckItems []*sqladmin.PreCheckResponse
|
var preCheckItems []*sqladmin.PreCheckResponse
|
||||||
@@ -199,7 +201,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil, ctx.Err()
|
return nil, util.NewClientServerError("timed out waiting for operation", http.StatusRequestTimeout, ctx.Err())
|
||||||
case <-time.After(5 * time.Second):
|
case <-time.After(5 * time.Second):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package couchbase
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/couchbase/gocb/v2"
|
"github.com/couchbase/gocb/v2"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -58,7 +60,6 @@ type Config struct {
|
|||||||
TemplateParameters parameters.Parameters `yaml:"templateParameters"`
|
TemplateParameters parameters.Parameters `yaml:"templateParameters"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate interface
|
|
||||||
var _ tools.ToolConfig = Config{}
|
var _ tools.ToolConfig = Config{}
|
||||||
|
|
||||||
func (cfg Config) ToolConfigType() string {
|
func (cfg Config) ToolConfigType() string {
|
||||||
@@ -72,7 +73,6 @@ func (cfg Config) Initialize(srcs map[string]sources.Source) (tools.Tool, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
mcpManifest := tools.GetMcpManifest(cfg.Name, cfg.Description, cfg.AuthRequired, allParameters, nil)
|
mcpManifest := tools.GetMcpManifest(cfg.Name, cfg.Description, cfg.AuthRequired, allParameters, nil)
|
||||||
// finish tool setup
|
|
||||||
t := Tool{
|
t := Tool{
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
AllParams: allParameters,
|
AllParams: allParameters,
|
||||||
@@ -82,7 +82,6 @@ func (cfg Config) Initialize(srcs map[string]sources.Source) (tools.Tool, error)
|
|||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate interface
|
|
||||||
var _ tools.Tool = Tool{}
|
var _ tools.Tool = Tool{}
|
||||||
|
|
||||||
type Tool struct {
|
type Tool struct {
|
||||||
@@ -96,23 +95,28 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
namedParamsMap := params.AsMap()
|
namedParamsMap := params.AsMap()
|
||||||
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, namedParamsMap)
|
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, namedParamsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract template params %w", err)
|
return nil, util.NewAgentError("unable to extract template params", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
newParams, err := parameters.GetParams(t.Parameters, namedParamsMap)
|
newParams, err := parameters.GetParams(t.Parameters, namedParamsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract standard params %w", err)
|
return nil, util.NewAgentError("unable to extract standard params", err)
|
||||||
}
|
}
|
||||||
return source.RunSQL(newStatement, newParams)
|
|
||||||
|
resp, err := source.RunSQL(newStatement, newParams)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -86,18 +87,19 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
|
|
||||||
projectDir, ok := paramsMap["project_dir"].(string)
|
projectDir, ok := paramsMap["project_dir"].(string)
|
||||||
if !ok || projectDir == "" {
|
if !ok || projectDir == "" {
|
||||||
return nil, fmt.Errorf("error casting 'project_dir' to string or invalid value")
|
return nil, util.NewAgentError("error casting 'project_dir' to string or invalid value", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.CommandContext(ctx, "dataform", "compile", projectDir, "--json")
|
cmd := exec.CommandContext(ctx, "dataform", "compile", projectDir, "--json")
|
||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error executing dataform compile: %w\nOutput: %s", err, string(output))
|
// Compilation failures are considered AgentErrors (invalid user code/project)
|
||||||
|
return nil, util.NewAgentError(fmt.Sprintf("error executing dataform compile: %v\nOutput: %s", err, string(output)), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.TrimSpace(string(output)), nil
|
return strings.TrimSpace(string(output)), nil
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package dataplexlookupentry
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
dataplexpb "cloud.google.com/go/dataplex/apiv1/dataplexpb"
|
dataplexpb "cloud.google.com/go/dataplex/apiv1/dataplexpb"
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -110,10 +112,10 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
@@ -122,10 +124,14 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
view, _ := paramsMap["view"].(int)
|
view, _ := paramsMap["view"].(int)
|
||||||
aspectTypeSlice, err := parameters.ConvertAnySliceToTyped(paramsMap["aspectTypes"].([]any), "string")
|
aspectTypeSlice, err := parameters.ConvertAnySliceToTyped(paramsMap["aspectTypes"].([]any), "string")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't convert aspectTypes to array of strings: %s", err)
|
return nil, util.NewAgentError(fmt.Sprintf("can't convert aspectTypes to array of strings: %s", err), err)
|
||||||
}
|
}
|
||||||
aspectTypes := aspectTypeSlice.([]string)
|
aspectTypes := aspectTypeSlice.([]string)
|
||||||
return source.LookupEntry(ctx, name, view, aspectTypes, entry)
|
resp, err := source.LookupEntry(ctx, name, view, aspectTypes, entry)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package dataplexsearchaspecttypes
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"cloud.google.com/go/dataplex/apiv1/dataplexpb"
|
"cloud.google.com/go/dataplex/apiv1/dataplexpb"
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -93,16 +95,29 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
query, _ := paramsMap["query"].(string)
|
query, ok := paramsMap["query"].(string)
|
||||||
pageSize, _ := paramsMap["pageSize"].(int)
|
if !ok {
|
||||||
orderBy, _ := paramsMap["orderBy"].(string)
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'query' parameter: %v", paramsMap["query"]), nil)
|
||||||
return source.SearchAspectTypes(ctx, query, pageSize, orderBy)
|
}
|
||||||
|
pageSize, ok := paramsMap["pageSize"].(int)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'pageSize' parameter: %v", paramsMap["pageSize"]), nil)
|
||||||
|
}
|
||||||
|
orderBy, ok := paramsMap["orderBy"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'orderBy' parameter: %v", paramsMap["orderBy"]), nil)
|
||||||
|
}
|
||||||
|
resp, err := source.SearchAspectTypes(ctx, query, pageSize, orderBy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package dataplexsearchentries
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"cloud.google.com/go/dataplex/apiv1/dataplexpb"
|
"cloud.google.com/go/dataplex/apiv1/dataplexpb"
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -93,16 +95,29 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
query, _ := paramsMap["query"].(string)
|
query, ok := paramsMap["query"].(string)
|
||||||
pageSize, _ := paramsMap["pageSize"].(int)
|
if !ok {
|
||||||
orderBy, _ := paramsMap["orderBy"].(string)
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'query' parameter: %v", paramsMap["query"]), nil)
|
||||||
return source.SearchEntries(ctx, query, pageSize, orderBy)
|
}
|
||||||
|
pageSize, ok := paramsMap["pageSize"].(int)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'pageSize' parameter: %v", paramsMap["pageSize"]), nil)
|
||||||
|
}
|
||||||
|
orderBy, ok := paramsMap["orderBy"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError(fmt.Sprintf("error casting 'orderBy' parameter: %v", paramsMap["orderBy"]), nil)
|
||||||
|
}
|
||||||
|
resp, err := source.SearchEntries(ctx, query, pageSize, orderBy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package dgraph
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources/dgraph"
|
"github.com/googleapis/genai-toolbox/internal/sources/dgraph"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -91,12 +93,16 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
return source.RunSQL(t.Statement, params, t.IsQuery, t.Timeout)
|
resp, err := source.RunSQL(t.Statement, params, t.IsQuery, t.Timeout)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -90,25 +91,30 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
sql, ok := paramsMap["sql"].(string)
|
sqlStr, ok := paramsMap["sql"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to get cast %s", paramsMap["sql"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast parameter 'sql' to string: %v", paramsMap["sql"]), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the query executed for debugging.
|
// Log the query executed for debugging.
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting logger: %s", err)
|
return nil, util.NewClientServerError("error getting logger", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, fmt.Sprintf("executing `%s` tool query: %s", resourceType, sql))
|
logger.DebugContext(ctx, fmt.Sprintf("executing `%s` tool query: %s", resourceType, sqlStr))
|
||||||
return source.RunSQL(ctx, sql, nil)
|
|
||||||
|
resp, err := source.RunSQL(ctx, sqlStr, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -18,12 +18,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -98,21 +100,21 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
statement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
statement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract template params: %w", err)
|
return nil, util.NewAgentError("unable to extract template params", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
newParams, err := parameters.GetParams(t.Parameters, paramsMap)
|
newParams, err := parameters.GetParams(t.Parameters, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract standard params: %w", err)
|
return nil, util.NewAgentError("unable to extract standard params", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
namedArgs := make([]any, 0, len(newParams))
|
namedArgs := make([]any, 0, len(newParams))
|
||||||
@@ -127,7 +129,12 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
namedArgs = append(namedArgs, value)
|
namedArgs = append(namedArgs, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.RunSQL(ctx, statement, namedArgs)
|
|
||||||
|
resp, err := source.RunSQL(ctx, statement, namedArgs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,13 +17,15 @@ package firestoreadddocuments
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
firestoreapi "cloud.google.com/go/firestore"
|
firestoreapi "cloud.google.com/go/firestore"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
fsUtil "github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -128,32 +130,32 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
// Get collection path
|
// Get collection path
|
||||||
collectionPath, ok := mapParams[collectionPathKey].(string)
|
collectionPath, ok := mapParams[collectionPathKey].(string)
|
||||||
if !ok || collectionPath == "" {
|
if !ok || collectionPath == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter", collectionPathKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter", collectionPathKey), nil)
|
||||||
}
|
}
|
||||||
// Validate collection path
|
// Validate collection path
|
||||||
if err := util.ValidateCollectionPath(collectionPath); err != nil {
|
if err := fsUtil.ValidateCollectionPath(collectionPath); err != nil {
|
||||||
return nil, fmt.Errorf("invalid collection path: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid collection path: %v", err), err)
|
||||||
}
|
}
|
||||||
// Get document data
|
// Get document data
|
||||||
documentDataRaw, ok := mapParams[documentDataKey]
|
documentDataRaw, ok := mapParams[documentDataKey]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter", documentDataKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter", documentDataKey), nil)
|
||||||
}
|
}
|
||||||
// Convert the document data from JSON format to Firestore format
|
// Convert the document data from JSON format to Firestore format
|
||||||
// The client is passed to handle referenceValue types
|
// The client is passed to handle referenceValue types
|
||||||
documentData, err := util.JSONToFirestoreValue(documentDataRaw, source.FirestoreClient())
|
documentData, err := fsUtil.JSONToFirestoreValue(documentDataRaw, source.FirestoreClient())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert document data: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("failed to convert document data: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get return document data flag
|
// Get return document data flag
|
||||||
@@ -161,7 +163,11 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if val, ok := mapParams[returnDocumentDataKey].(bool); ok {
|
if val, ok := mapParams[returnDocumentDataKey].(bool); ok {
|
||||||
returnData = val
|
returnData = val
|
||||||
}
|
}
|
||||||
return source.AddDocuments(ctx, collectionPath, documentData, returnData)
|
resp, err := source.AddDocuments(ctx, collectionPath, documentData, returnData)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,13 +17,15 @@ package firestoredeletedocuments
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
firestoreapi "cloud.google.com/go/firestore"
|
firestoreapi "cloud.google.com/go/firestore"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
fsUtil "github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -94,39 +96,43 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
documentPathsRaw, ok := mapParams[documentPathsKey].([]any)
|
documentPathsRaw, ok := mapParams[documentPathsKey].([]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected an array", documentPathsKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected an array", documentPathsKey), nil)
|
||||||
}
|
}
|
||||||
if len(documentPathsRaw) == 0 {
|
if len(documentPathsRaw) == 0 {
|
||||||
return nil, fmt.Errorf("'%s' parameter cannot be empty", documentPathsKey)
|
return nil, util.NewAgentError(fmt.Sprintf("'%s' parameter cannot be empty", documentPathsKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use ConvertAnySliceToTyped to convert the slice
|
// Use ConvertAnySliceToTyped to convert the slice
|
||||||
typedSlice, err := parameters.ConvertAnySliceToTyped(documentPathsRaw, "string")
|
typedSlice, err := parameters.ConvertAnySliceToTyped(documentPathsRaw, "string")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert document paths: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("failed to convert document paths: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
documentPaths, ok := typedSlice.([]string)
|
documentPaths, ok := typedSlice.([]string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unexpected type conversion error for document paths")
|
return nil, util.NewAgentError("unexpected type conversion error for document paths", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate each document path
|
// Validate each document path
|
||||||
for i, path := range documentPaths {
|
for i, path := range documentPaths {
|
||||||
if err := util.ValidateDocumentPath(path); err != nil {
|
if err := fsUtil.ValidateDocumentPath(path); err != nil {
|
||||||
return nil, fmt.Errorf("invalid document path at index %d: %w", i, err)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid document path at index %d: %v", i, err), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.DeleteDocuments(ctx, documentPaths)
|
resp, err := source.DeleteDocuments(ctx, documentPaths)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,13 +17,15 @@ package firestoregetdocuments
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
firestoreapi "cloud.google.com/go/firestore"
|
firestoreapi "cloud.google.com/go/firestore"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
fsUtil "github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -94,40 +96,44 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
documentPathsRaw, ok := mapParams[documentPathsKey].([]any)
|
documentPathsRaw, ok := mapParams[documentPathsKey].([]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter; expected an array", documentPathsKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter; expected an array", documentPathsKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(documentPathsRaw) == 0 {
|
if len(documentPathsRaw) == 0 {
|
||||||
return nil, fmt.Errorf("'%s' parameter cannot be empty", documentPathsKey)
|
return nil, util.NewAgentError(fmt.Sprintf("'%s' parameter cannot be empty", documentPathsKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use ConvertAnySliceToTyped to convert the slice
|
// Use ConvertAnySliceToTyped to convert the slice
|
||||||
typedSlice, err := parameters.ConvertAnySliceToTyped(documentPathsRaw, "string")
|
typedSlice, err := parameters.ConvertAnySliceToTyped(documentPathsRaw, "string")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert document paths: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("failed to convert document paths: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
documentPaths, ok := typedSlice.([]string)
|
documentPaths, ok := typedSlice.([]string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unexpected type conversion error for document paths")
|
return nil, util.NewAgentError("unexpected type conversion error for document paths", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate each document path
|
// Validate each document path
|
||||||
for i, path := range documentPaths {
|
for i, path := range documentPaths {
|
||||||
if err := util.ValidateDocumentPath(path); err != nil {
|
if err := fsUtil.ValidateDocumentPath(path); err != nil {
|
||||||
return nil, fmt.Errorf("invalid document path at index %d: %w", i, err)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid document path at index %d: %v", i, err), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.GetDocuments(ctx, documentPaths)
|
resp, err := source.GetDocuments(ctx, documentPaths)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package firestoregetrules
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/firebaserules/v1"
|
"google.golang.org/api/firebaserules/v1"
|
||||||
)
|
)
|
||||||
@@ -92,12 +94,16 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
return source.GetRules(ctx)
|
resp, err := source.GetRules(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,13 +17,15 @@ package firestorelistcollections
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
firestoreapi "cloud.google.com/go/firestore"
|
firestoreapi "cloud.google.com/go/firestore"
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
fsUtil "github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -95,10 +97,10 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
@@ -107,11 +109,15 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
parentPath, _ := mapParams[parentPathKey].(string)
|
parentPath, _ := mapParams[parentPathKey].(string)
|
||||||
if parentPath != "" {
|
if parentPath != "" {
|
||||||
// Validate parent document path
|
// Validate parent document path
|
||||||
if err := util.ValidateDocumentPath(parentPath); err != nil {
|
if err := fsUtil.ValidateDocumentPath(parentPath); err != nil {
|
||||||
return nil, fmt.Errorf("invalid parent document path: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid parent document path: %v", err), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return source.ListCollections(ctx, parentPath)
|
resp, err := source.ListCollections(ctx, parentPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -26,7 +27,8 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
fsUtil "github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -158,16 +160,16 @@ var validOperators = map[string]bool{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the Firestore query based on the provided parameters
|
// Invoke executes the Firestore query based on the provided parameters
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
// Process collection path with template substitution
|
// Process collection path with template substitution
|
||||||
collectionPath, err := parameters.PopulateTemplate("collectionPath", t.CollectionPath, paramsMap)
|
collectionPath, err := parameters.PopulateTemplate("collectionPath", t.CollectionPath, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to process collection path: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("failed to process collection path: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var filter firestoreapi.EntityFilter
|
var filter firestoreapi.EntityFilter
|
||||||
@@ -176,13 +178,13 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Apply template substitution to filters
|
// Apply template substitution to filters
|
||||||
filtersJSON, err := parameters.PopulateTemplateWithJSON("filters", t.Filters, paramsMap)
|
filtersJSON, err := parameters.PopulateTemplateWithJSON("filters", t.Filters, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to process filters template: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("failed to process filters template: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the simplified filter format
|
// Parse the simplified filter format
|
||||||
var simplifiedFilter SimplifiedFilter
|
var simplifiedFilter SimplifiedFilter
|
||||||
if err := json.Unmarshal([]byte(filtersJSON), &simplifiedFilter); err != nil {
|
if err := json.Unmarshal([]byte(filtersJSON), &simplifiedFilter); err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse filters: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("failed to parse filters: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert simplified filter to Firestore filter
|
// Convert simplified filter to Firestore filter
|
||||||
@@ -191,17 +193,17 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Process and apply ordering
|
// Process and apply ordering
|
||||||
orderBy, err := t.getOrderBy(paramsMap)
|
orderBy, err := t.getOrderBy(paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError(fmt.Sprintf("failed to process order by: %v", err), err)
|
||||||
}
|
}
|
||||||
// Process select fields
|
// Process select fields
|
||||||
selectFields, err := t.processSelectFields(paramsMap)
|
selectFields, err := t.processSelectFields(paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError(fmt.Sprintf("failed to process select fields: %v", err), err)
|
||||||
}
|
}
|
||||||
// Process and apply limit
|
// Process and apply limit
|
||||||
limit, err := t.getLimit(paramsMap)
|
limit, err := t.getLimit(paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError(fmt.Sprintf("failed to process limit: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// prevent panic when accessing orderBy incase it is nil
|
// prevent panic when accessing orderBy incase it is nil
|
||||||
@@ -215,10 +217,14 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Build the query
|
// Build the query
|
||||||
query, err := source.BuildQuery(collectionPath, filter, selectFields, orderByField, orderByDirection, limit, t.AnalyzeQuery)
|
query, err := source.BuildQuery(collectionPath, filter, selectFields, orderByField, orderByDirection, limit, t.AnalyzeQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
// Execute the query and return results
|
// Execute the query and return results
|
||||||
return source.ExecuteQuery(ctx, query, t.AnalyzeQuery)
|
resp, err := source.ExecuteQuery(ctx, query, t.AnalyzeQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertToFirestoreFilter converts simplified filter format to Firestore EntityFilter
|
// convertToFirestoreFilter converts simplified filter format to Firestore EntityFilter
|
||||||
@@ -255,7 +261,7 @@ func (t Tool) convertToFirestoreFilter(source compatibleSource, filter Simplifie
|
|||||||
if filter.Field != "" && filter.Op != "" && filter.Value != nil {
|
if filter.Field != "" && filter.Op != "" && filter.Value != nil {
|
||||||
if validOperators[filter.Op] {
|
if validOperators[filter.Op] {
|
||||||
// Convert the value using the Firestore native JSON converter
|
// Convert the value using the Firestore native JSON converter
|
||||||
convertedValue, err := util.JSONToFirestoreValue(filter.Value, source.FirestoreClient())
|
convertedValue, err := fsUtil.JSONToFirestoreValue(filter.Value, source.FirestoreClient())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If conversion fails, use the original value
|
// If conversion fails, use the original value
|
||||||
convertedValue = filter.Value
|
convertedValue = filter.Value
|
||||||
@@ -367,7 +373,7 @@ func (t Tool) getLimit(params map[string]any) (int, error) {
|
|||||||
if processedValue != "" {
|
if processedValue != "" {
|
||||||
parsedLimit, err := strconv.Atoi(processedValue)
|
parsedLimit, err := strconv.Atoi(processedValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("failed to parse limit value '%s': %w", processedValue, err)
|
return 0, err
|
||||||
}
|
}
|
||||||
limit = parsedLimit
|
limit = parsedLimit
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
firestoreapi "cloud.google.com/go/firestore"
|
firestoreapi "cloud.google.com/go/firestore"
|
||||||
@@ -25,7 +26,8 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
fsUtil "github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -230,16 +232,16 @@ func (o *OrderByConfig) GetDirection() firestoreapi.Direction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the Firestore query based on the provided parameters
|
// Invoke executes the Firestore query based on the provided parameters
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse parameters
|
// Parse parameters
|
||||||
queryParams, err := t.parseQueryParameters(params)
|
queryParams, err := t.parseQueryParameters(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewAgentError(fmt.Sprintf("failed to parse query parameters: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var filter firestoreapi.EntityFilter
|
var filter firestoreapi.EntityFilter
|
||||||
@@ -270,9 +272,13 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Build the query
|
// Build the query
|
||||||
query, err := source.BuildQuery(queryParams.CollectionPath, filter, nil, orderByField, orderByDirection, queryParams.Limit, queryParams.AnalyzeQuery)
|
query, err := source.BuildQuery(queryParams.CollectionPath, filter, nil, orderByField, orderByDirection, queryParams.Limit, queryParams.AnalyzeQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.ProcessGcpError(err)
|
||||||
}
|
}
|
||||||
return source.ExecuteQuery(ctx, query, queryParams.AnalyzeQuery)
|
resp, err := source.ExecuteQuery(ctx, query, queryParams.AnalyzeQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// queryParameters holds all parsed query parameters
|
// queryParameters holds all parsed query parameters
|
||||||
@@ -295,7 +301,7 @@ func (t Tool) parseQueryParameters(params parameters.ParamValues) (*queryParamet
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate collection path
|
// Validate collection path
|
||||||
if err := util.ValidateCollectionPath(collectionPath); err != nil {
|
if err := fsUtil.ValidateCollectionPath(collectionPath); err != nil {
|
||||||
return nil, fmt.Errorf("invalid collection path: %w", err)
|
return nil, fmt.Errorf("invalid collection path: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package firestoreupdatedocument
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
firestoreapi "cloud.google.com/go/firestore"
|
firestoreapi "cloud.google.com/go/firestore"
|
||||||
@@ -24,7 +25,8 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
fsUtil "github.com/googleapis/genai-toolbox/internal/tools/firestore/util"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -138,10 +140,10 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
@@ -149,18 +151,18 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Get document path
|
// Get document path
|
||||||
documentPath, ok := mapParams[documentPathKey].(string)
|
documentPath, ok := mapParams[documentPathKey].(string)
|
||||||
if !ok || documentPath == "" {
|
if !ok || documentPath == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter", documentPathKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter", documentPathKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate document path
|
// Validate document path
|
||||||
if err := util.ValidateDocumentPath(documentPath); err != nil {
|
if err := fsUtil.ValidateDocumentPath(documentPath); err != nil {
|
||||||
return nil, fmt.Errorf("invalid document path: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid document path: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get document data
|
// Get document data
|
||||||
documentDataRaw, ok := mapParams[documentDataKey]
|
documentDataRaw, ok := mapParams[documentDataKey]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter", documentDataKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter", documentDataKey), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get update mask if provided
|
// Get update mask if provided
|
||||||
@@ -170,11 +172,11 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Use ConvertAnySliceToTyped to convert the slice
|
// Use ConvertAnySliceToTyped to convert the slice
|
||||||
typedSlice, err := parameters.ConvertAnySliceToTyped(updateMaskArray, "string")
|
typedSlice, err := parameters.ConvertAnySliceToTyped(updateMaskArray, "string")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert update mask: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("failed to convert update mask: %v", err), err)
|
||||||
}
|
}
|
||||||
updatePaths, ok = typedSlice.([]string)
|
updatePaths, ok = typedSlice.([]string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unexpected type conversion error for update mask")
|
return nil, util.NewAgentError("unexpected type conversion error for update mask", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,15 +186,15 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if len(updatePaths) > 0 {
|
if len(updatePaths) > 0 {
|
||||||
|
|
||||||
// Convert document data without delete markers
|
// Convert document data without delete markers
|
||||||
dataMap, err := util.JSONToFirestoreValue(documentDataRaw, source.FirestoreClient())
|
dataMap, err := fsUtil.JSONToFirestoreValue(documentDataRaw, source.FirestoreClient())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert document data: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("failed to convert document data: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure it's a map
|
// Ensure it's a map
|
||||||
dataMapTyped, ok := dataMap.(map[string]interface{})
|
dataMapTyped, ok := dataMap.(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("document data must be a map")
|
return nil, util.NewAgentError("document data must be a map", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range updatePaths {
|
for _, path := range updatePaths {
|
||||||
@@ -210,9 +212,9 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Update all fields in the document data (merge)
|
// Update all fields in the document data (merge)
|
||||||
documentData, err = util.JSONToFirestoreValue(documentDataRaw, source.FirestoreClient())
|
documentData, err = fsUtil.JSONToFirestoreValue(documentDataRaw, source.FirestoreClient())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert document data: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("failed to convert document data: %v", err), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,7 +223,11 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if val, ok := mapParams[returnDocumentDataKey].(bool); ok {
|
if val, ok := mapParams[returnDocumentDataKey].(bool); ok {
|
||||||
returnData = val
|
returnData = val
|
||||||
}
|
}
|
||||||
return source.UpdateDocument(ctx, documentPath, updates, documentData, returnData)
|
resp, err := source.UpdateDocument(ctx, documentPath, updates, documentData, returnData)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getFieldValue retrieves a value from a nested map using a dot-separated path
|
// getFieldValue retrieves a value from a nested map using a dot-separated path
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package firestorevalidaterules
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/api/firebaserules/v1"
|
"google.golang.org/api/firebaserules/v1"
|
||||||
)
|
)
|
||||||
@@ -106,10 +108,10 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
@@ -117,9 +119,13 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Get source parameter
|
// Get source parameter
|
||||||
sourceParam, ok := mapParams[sourceKey].(string)
|
sourceParam, ok := mapParams[sourceKey].(string)
|
||||||
if !ok || sourceParam == "" {
|
if !ok || sourceParam == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing '%s' parameter", sourceKey)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid or missing '%s' parameter", sourceKey), nil)
|
||||||
}
|
}
|
||||||
return source.ValidateRules(ctx, sourceParam)
|
resp, err := source.ValidateRules(ctx, sourceParam)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,18 +17,18 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"maps"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"maps"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -226,10 +226,10 @@ func getHeaders(headerParams parameters.Parameters, defaultHeaders map[string]st
|
|||||||
return allHeaders, nil
|
return allHeaders, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
@@ -237,27 +237,35 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Calculate request body
|
// Calculate request body
|
||||||
requestBody, err := getRequestBody(t.BodyParams, t.RequestBody, paramsMap)
|
requestBody, err := getRequestBody(t.BodyParams, t.RequestBody, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error populating request body: %s", err)
|
return nil, util.NewAgentError("error populating request body", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate URL
|
// Calculate URL
|
||||||
urlString, err := getURL(source.HttpBaseURL(), t.Path, t.PathParams, t.QueryParams, source.HttpQueryParams(), paramsMap)
|
urlString, err := getURL(source.HttpBaseURL(), t.Path, t.PathParams, t.QueryParams, source.HttpQueryParams(), paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error populating path parameters: %s", err)
|
return nil, util.NewAgentError("error populating path parameters", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, _ := http.NewRequest(string(t.Method), urlString, strings.NewReader(requestBody))
|
req, err := http.NewRequestWithContext(ctx, string(t.Method), urlString, strings.NewReader(requestBody))
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.NewClientServerError("error creating http request", http.StatusInternalServerError, err)
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate request headers
|
// Calculate request headers
|
||||||
allHeaders, err := getHeaders(t.HeaderParams, t.Headers, paramsMap)
|
allHeaders, err := getHeaders(t.HeaderParams, t.Headers, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error populating request headers: %s", err)
|
return nil, util.NewAgentError("error populating request headers", err)
|
||||||
}
|
}
|
||||||
// Set request headers
|
// Set request headers
|
||||||
for k, v := range allHeaders {
|
for k, v := range allHeaders {
|
||||||
req.Header.Set(k, v)
|
req.Header.Set(k, v)
|
||||||
}
|
}
|
||||||
return source.RunRequest(req)
|
|
||||||
|
resp, err := source.RunRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package lookeradddashboardelement
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -134,58 +135,74 @@ var (
|
|||||||
visType string = "vis"
|
visType string = "vis"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.DebugContext(ctx, "params = ", params)
|
logger.DebugContext(ctx, "params = ", params)
|
||||||
|
|
||||||
wq, err := lookercommon.ProcessQueryArgs(ctx, params)
|
wq, err := lookercommon.ProcessQueryArgs(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error building query request: %w", err)
|
return nil, util.NewAgentError("error building query request", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
dashboard_id := paramsMap["dashboard_id"].(string)
|
|
||||||
title := paramsMap["title"].(string)
|
|
||||||
|
|
||||||
visConfig := paramsMap["vis_config"].(map[string]any)
|
dashboard_id, ok := paramsMap["dashboard_id"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("dashboard_id parameter missing or invalid", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
title, ok := paramsMap["title"].(string)
|
||||||
|
if !ok {
|
||||||
|
title = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
visConfig, ok := paramsMap["vis_config"].(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
visConfig = make(map[string]any)
|
||||||
|
}
|
||||||
wq.VisConfig = &visConfig
|
wq.VisConfig = &visConfig
|
||||||
|
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError("error getting sdk", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
qresp, err := sdk.CreateQuery(*wq, "id", source.LookerApiSettings())
|
qresp, err := sdk.CreateQuery(*wq, "id", source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making create query request: %w", err)
|
return nil, util.ProcessGeneralError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dashFilters := []any{}
|
dashFilters := []any{}
|
||||||
if v, ok := paramsMap["dashboard_filters"]; ok {
|
if v, ok := paramsMap["dashboard_filters"]; ok {
|
||||||
if v != nil {
|
if v != nil {
|
||||||
dashFilters = paramsMap["dashboard_filters"].([]any)
|
if df, ok := v.([]any); ok {
|
||||||
|
dashFilters = df
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var filterables []v4.ResultMakerFilterables
|
var filterables []v4.ResultMakerFilterables
|
||||||
for _, m := range dashFilters {
|
for _, m := range dashFilters {
|
||||||
f := m.(map[string]any)
|
f, ok := m.(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("invalid dashboard filter structure", nil)
|
||||||
|
}
|
||||||
name, ok := f["dashboard_filter_name"].(string)
|
name, ok := f["dashboard_filter_name"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error processing dashboard filter: %w", err)
|
return nil, util.NewAgentError("error processing dashboard filter: missing dashboard_filter_name", nil)
|
||||||
}
|
}
|
||||||
field, ok := f["field"].(string)
|
field, ok := f["field"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("error processing dashboard filter: %w", err)
|
return nil, util.NewAgentError("error processing dashboard filter: missing field", nil)
|
||||||
}
|
}
|
||||||
listener := v4.ResultMakerFilterablesListen{
|
listener := v4.ResultMakerFilterablesListen{
|
||||||
DashboardFilterName: &name,
|
DashboardFilterName: &name,
|
||||||
@@ -233,7 +250,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
resp, err := sdk.CreateDashboardElement(req, source.LookerApiSettings())
|
resp, err := sdk.CreateDashboardElement(req, source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making create dashboard element request: %w", err)
|
return nil, util.ProcessGeneralError(err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, "resp = %v", resp)
|
logger.DebugContext(ctx, "resp = %v", resp)
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package lookeradddashboardfilter
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -128,33 +129,54 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, "params = ", params)
|
logger.DebugContext(ctx, "params = ", params)
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
dashboard_id := paramsMap["dashboard_id"].(string)
|
dashboard_id, ok := paramsMap["dashboard_id"].(string)
|
||||||
name := paramsMap["name"].(string)
|
if !ok {
|
||||||
title := paramsMap["title"].(string)
|
return nil, util.NewAgentError("dashboard_id parameter missing or invalid", nil)
|
||||||
filterType := paramsMap["filter_type"].(string)
|
}
|
||||||
|
name, ok := paramsMap["name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("name parameter missing or invalid", nil)
|
||||||
|
}
|
||||||
|
title, ok := paramsMap["title"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("title parameter missing or invalid", nil)
|
||||||
|
}
|
||||||
|
filterType, ok := paramsMap["filter_type"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("filter_type parameter missing or invalid", nil)
|
||||||
|
}
|
||||||
|
|
||||||
switch filterType {
|
switch filterType {
|
||||||
case "date_filter":
|
case "date_filter":
|
||||||
case "number_filter":
|
case "number_filter":
|
||||||
case "string_filter":
|
case "string_filter":
|
||||||
case "field_filter":
|
case "field_filter":
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid filter type: %s. Must be one of date_filter, number_filter, string_filter, field_filter", filterType)
|
return nil, util.NewAgentError(fmt.Sprintf("invalid filter type: %s. Must be one of date_filter, number_filter, string_filter, field_filter", filterType), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
allowMultipleValues, ok := paramsMap["allow_multiple_values"].(bool)
|
||||||
|
if !ok {
|
||||||
|
// defaults should handle this, but safe fallback
|
||||||
|
allowMultipleValues = true
|
||||||
|
}
|
||||||
|
required, ok := paramsMap["required"].(bool)
|
||||||
|
if !ok {
|
||||||
|
required = false
|
||||||
}
|
}
|
||||||
allowMultipleValues := paramsMap["allow_multiple_values"].(bool)
|
|
||||||
required := paramsMap["required"].(bool)
|
|
||||||
|
|
||||||
req := v4.WriteCreateDashboardFilter{
|
req := v4.WriteCreateDashboardFilter{
|
||||||
DashboardId: dashboard_id,
|
DashboardId: dashboard_id,
|
||||||
@@ -165,9 +187,8 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
Required: &required,
|
Required: &required,
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := paramsMap["default_value"]; ok {
|
if v, ok := paramsMap["default_value"]; ok && v != nil {
|
||||||
if v != nil {
|
if defaultValue, ok := v.(string); ok {
|
||||||
defaultValue := paramsMap["default_value"].(string)
|
|
||||||
req.DefaultValue = &defaultValue
|
req.DefaultValue = &defaultValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,15 +196,15 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
if filterType == "field_filter" {
|
if filterType == "field_filter" {
|
||||||
model, ok := paramsMap["model"].(string)
|
model, ok := paramsMap["model"].(string)
|
||||||
if !ok || model == "" {
|
if !ok || model == "" {
|
||||||
return nil, fmt.Errorf("model must be specified for field_filter type")
|
return nil, util.NewAgentError("model must be specified for field_filter type", nil)
|
||||||
}
|
}
|
||||||
explore, ok := paramsMap["explore"].(string)
|
explore, ok := paramsMap["explore"].(string)
|
||||||
if !ok || explore == "" {
|
if !ok || explore == "" {
|
||||||
return nil, fmt.Errorf("explore must be specified for field_filter type")
|
return nil, util.NewAgentError("explore must be specified for field_filter type", nil)
|
||||||
}
|
}
|
||||||
dimension, ok := paramsMap["dimension"].(string)
|
dimension, ok := paramsMap["dimension"].(string)
|
||||||
if !ok || dimension == "" {
|
if !ok || dimension == "" {
|
||||||
return nil, fmt.Errorf("dimension must be specified for field_filter type")
|
return nil, util.NewAgentError("dimension must be specified for field_filter type", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Model = &model
|
req.Model = &model
|
||||||
@@ -193,12 +214,12 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError("error getting sdk", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := sdk.CreateDashboardFilter(req, "name", source.LookerApiSettings())
|
resp, err := sdk.CreateDashboardFilter(req, "name", source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making create dashboard filter request: %s", err)
|
return nil, util.ProcessGeneralError(err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, "resp = %v", resp)
|
logger.DebugContext(ctx, "resp = %v", resp)
|
||||||
|
|
||||||
|
|||||||
@@ -215,10 +215,10 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
@@ -226,11 +226,11 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Get credentials for the API call
|
// Get credentials for the API call
|
||||||
// Use cloud-platform token source for Gemini Data Analytics API
|
// Use cloud-platform token source for Gemini Data Analytics API
|
||||||
if t.TokenSource == nil {
|
if t.TokenSource == nil {
|
||||||
return nil, fmt.Errorf("cloud-platform token source is missing")
|
return nil, util.NewClientServerError("cloud-platform token source is missing", http.StatusInternalServerError, nil)
|
||||||
}
|
}
|
||||||
token, err := t.TokenSource.Token()
|
token, err := t.TokenSource.Token()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get token from cloud-platform token source: %w", err)
|
return nil, util.NewClientServerError("failed to get token from cloud-platform token source", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
tokenStr = token.AccessToken
|
tokenStr = token.AccessToken
|
||||||
|
|
||||||
@@ -286,7 +286,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
// Call the streaming API
|
// Call the streaming API
|
||||||
response, err := getStream(ctx, caURL, payload, headers)
|
response, err := getStream(ctx, caURL, payload, headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get response from conversational analytics API: %w", err)
|
return nil, util.NewClientServerError("failed to get response from conversational analytics API", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return response, nil
|
return response, nil
|
||||||
|
|||||||
@@ -16,12 +16,14 @@ package lookercreateprojectfile
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools/looker/lookercommon"
|
"github.com/googleapis/genai-toolbox/internal/tools/looker/lookercommon"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
|
|
||||||
"github.com/looker-open-source/sdk-codegen/go/rtl"
|
"github.com/looker-open-source/sdk-codegen/go/rtl"
|
||||||
@@ -110,29 +112,29 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError("error getting sdk", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
projectId, ok := mapParams["project_id"].(string)
|
projectId, ok := mapParams["project_id"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("'project_id' must be a string, got %T", mapParams["project_id"])
|
return nil, util.NewAgentError(fmt.Sprintf("'project_id' must be a string, got %T", mapParams["project_id"]), nil)
|
||||||
}
|
}
|
||||||
filePath, ok := mapParams["file_path"].(string)
|
filePath, ok := mapParams["file_path"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("'file_path' must be a string, got %T", mapParams["file_path"])
|
return nil, util.NewAgentError(fmt.Sprintf("'file_path' must be a string, got %T", mapParams["file_path"]), nil)
|
||||||
}
|
}
|
||||||
fileContent, ok := mapParams["file_content"].(string)
|
fileContent, ok := mapParams["file_content"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("'file_content' must be a string, got %T", mapParams["file_content"])
|
return nil, util.NewAgentError(fmt.Sprintf("'file_content' must be a string, got %T", mapParams["file_content"]), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
req := lookercommon.FileContent{
|
req := lookercommon.FileContent{
|
||||||
@@ -142,7 +144,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
err = lookercommon.CreateProjectFile(sdk, projectId, req, source.LookerApiSettings())
|
err = lookercommon.CreateProjectFile(sdk, projectId, req, source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making create_project_file request: %s", err)
|
return nil, util.ProcessGeneralError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := make(map[string]any)
|
data := make(map[string]any)
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package lookergetfilters
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -109,25 +110,25 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
model, explore, err := lookercommon.ProcessFieldArgs(ctx, params)
|
model, explore, err := lookercommon.ProcessFieldArgs(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error processing model or explore: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("error processing model or explore: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := lookercommon.FiltersFields
|
fields := lookercommon.FiltersFields
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error getting sdk: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
req := v4.RequestLookmlModelExplore{
|
req := v4.RequestLookmlModelExplore{
|
||||||
LookmlModelName: *model,
|
LookmlModelName: *model,
|
||||||
@@ -136,16 +137,16 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
resp, err := sdk.LookmlModelExplore(req, source.LookerApiSettings())
|
resp, err := sdk.LookmlModelExplore(req, source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making get_filters request: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error making get_filters request: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := lookercommon.CheckLookerExploreFields(&resp); err != nil {
|
if err := lookercommon.CheckLookerExploreFields(&resp); err != nil {
|
||||||
return nil, fmt.Errorf("error processing get_filters response: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error processing get_filters response: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := lookercommon.ExtractLookerFieldProperties(ctx, resp.Fields.Filters, source.LookerShowHiddenFields())
|
data, err := lookercommon.ExtractLookerFieldProperties(ctx, resp.Fields.Filters, source.LookerShowHiddenFields())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error extracting get_filters response: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error extracting get_filters response: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, "data = ", data)
|
logger.DebugContext(ctx, "data = ", data)
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package lookergetlooks
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -116,23 +117,29 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
title := paramsMap["title"].(string)
|
title, ok := paramsMap["title"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("missing or invalid 'title' parameter", nil)
|
||||||
|
}
|
||||||
title_ptr := &title
|
title_ptr := &title
|
||||||
if *title_ptr == "" {
|
if *title_ptr == "" {
|
||||||
title_ptr = nil
|
title_ptr = nil
|
||||||
}
|
}
|
||||||
desc := paramsMap["desc"].(string)
|
desc, ok := paramsMap["desc"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("missing or invalid 'desc' parameter", nil)
|
||||||
|
}
|
||||||
desc_ptr := &desc
|
desc_ptr := &desc
|
||||||
if *desc_ptr == "" {
|
if *desc_ptr == "" {
|
||||||
desc_ptr = nil
|
desc_ptr = nil
|
||||||
@@ -142,7 +149,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error getting sdk: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
req := v4.RequestSearchLooks{
|
req := v4.RequestSearchLooks{
|
||||||
Title: title_ptr,
|
Title: title_ptr,
|
||||||
@@ -152,7 +159,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
resp, err := sdk.SearchLooks(req, source.LookerApiSettings())
|
resp, err := sdk.SearchLooks(req, source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making get_looks request: %s", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error making get_looks request: %s", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var data []any
|
var data []any
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package lookergetmeasures
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -109,25 +110,25 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
model, explore, err := lookercommon.ProcessFieldArgs(ctx, params)
|
model, explore, err := lookercommon.ProcessFieldArgs(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error processing model or explore: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("error processing model or explore: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := lookercommon.MeasuresFields
|
fields := lookercommon.MeasuresFields
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error getting sdk: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
req := v4.RequestLookmlModelExplore{
|
req := v4.RequestLookmlModelExplore{
|
||||||
LookmlModelName: *model,
|
LookmlModelName: *model,
|
||||||
@@ -136,16 +137,16 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
resp, err := sdk.LookmlModelExplore(req, source.LookerApiSettings())
|
resp, err := sdk.LookmlModelExplore(req, source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making get_measures request: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error making get_measures request: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := lookercommon.CheckLookerExploreFields(&resp); err != nil {
|
if err := lookercommon.CheckLookerExploreFields(&resp); err != nil {
|
||||||
return nil, fmt.Errorf("error processing get_measures response: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error processing get_measures response: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := lookercommon.ExtractLookerFieldProperties(ctx, resp.Fields.Measures, source.LookerShowHiddenFields())
|
data, err := lookercommon.ExtractLookerFieldProperties(ctx, resp.Fields.Measures, source.LookerShowHiddenFields())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error extracting get_measures response: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error extracting get_measures response: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, "data = ", data)
|
logger.DebugContext(ctx, "data = ", data)
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package lookergetmodels
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -108,15 +109,15 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
excludeEmpty := false
|
excludeEmpty := false
|
||||||
@@ -125,7 +126,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error getting sdk: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
req := v4.RequestAllLookmlModels{
|
req := v4.RequestAllLookmlModels{
|
||||||
ExcludeEmpty: &excludeEmpty,
|
ExcludeEmpty: &excludeEmpty,
|
||||||
@@ -134,7 +135,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
resp, err := sdk.AllLookmlModels(req, source.LookerApiSettings())
|
resp, err := sdk.AllLookmlModels(req, source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making get_models request: %s", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error making get_models request: %s", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var data []any
|
var data []any
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
//
|
//
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
// distributed under the License is distributed under an "AS IS" BASIS,
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
@@ -16,6 +16,7 @@ package lookergetparameters
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -109,25 +110,25 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
model, explore, err := lookercommon.ProcessFieldArgs(ctx, params)
|
model, explore, err := lookercommon.ProcessFieldArgs(ctx, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error processing model or explore: %w", err)
|
return nil, util.NewAgentError(fmt.Sprintf("error processing model or explore: %v", err), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := lookercommon.ParametersFields
|
fields := lookercommon.ParametersFields
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error getting sdk: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
req := v4.RequestLookmlModelExplore{
|
req := v4.RequestLookmlModelExplore{
|
||||||
LookmlModelName: *model,
|
LookmlModelName: *model,
|
||||||
@@ -136,16 +137,16 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
}
|
}
|
||||||
resp, err := sdk.LookmlModelExplore(req, source.LookerApiSettings())
|
resp, err := sdk.LookmlModelExplore(req, source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making get_parameters request: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error making get_parameters request: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := lookercommon.CheckLookerExploreFields(&resp); err != nil {
|
if err := lookercommon.CheckLookerExploreFields(&resp); err != nil {
|
||||||
return nil, fmt.Errorf("error processing get_parameters response: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error processing get_parameters response: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := lookercommon.ExtractLookerFieldProperties(ctx, resp.Fields.Parameters, source.LookerShowHiddenFields())
|
data, err := lookercommon.ExtractLookerFieldProperties(ctx, resp.Fields.Parameters, source.LookerShowHiddenFields())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error extracting get_parameters response: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error extracting get_parameters response: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, "data = ", data)
|
logger.DebugContext(ctx, "data = ", data)
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package lookergetprojectfile
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -110,35 +111,35 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error getting sdk: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
projectId, ok := mapParams["project_id"].(string)
|
projectId, ok := mapParams["project_id"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("'project_id' must be a string, got %T", mapParams["project_id"])
|
return nil, util.NewAgentError(fmt.Sprintf("'project_id' must be a string, got %T", mapParams["project_id"]), nil)
|
||||||
}
|
}
|
||||||
filePath, ok := mapParams["file_path"].(string)
|
filePath, ok := mapParams["file_path"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("'file_path' must be a string, got %T", mapParams["file_path"])
|
return nil, util.NewAgentError(fmt.Sprintf("'file_path' must be a string, got %T", mapParams["file_path"]), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := lookercommon.GetProjectFileContent(sdk, projectId, filePath, source.LookerApiSettings())
|
resp, err := lookercommon.GetProjectFileContent(sdk, projectId, filePath, source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making get_project_file request: %s", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error making get_project_file request: %s", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.DebugContext(ctx, "Got response of %v\n", resp)
|
logger.DebugContext(ctx, "Got response of %v\n", resp)
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package lookergetprojectfiles
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -108,31 +109,31 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error getting sdk: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
projectId, ok := mapParams["project_id"].(string)
|
projectId, ok := mapParams["project_id"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("'project_id' must be a string, got %T", mapParams["project_id"])
|
return nil, util.NewAgentError(fmt.Sprintf("'project_id' must be a string, got %T", mapParams["project_id"]), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := sdk.AllProjectFiles(projectId, "", source.LookerApiSettings())
|
resp, err := sdk.AllProjectFiles(projectId, "", source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making get_project_files request: %s", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error making get_project_files request: %s", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var data []any
|
var data []any
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package lookergetprojects
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -107,25 +108,25 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error getting sdk: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := sdk.AllProjects("id,name", source.LookerApiSettings())
|
resp, err := sdk.AllProjects("id,name", source.LookerApiSettings())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error making get_models request: %s", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error making get_models request: %s", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var data []any
|
var data []any
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -125,20 +126,20 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
return t.Config
|
return t.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sdk, err := source.GetLookerSDK(string(accessToken))
|
sdk, err := source.GetLookerSDK(string(accessToken))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error getting sdk: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
@@ -159,7 +160,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
|
|
||||||
action, ok := paramsMap["action"].(string)
|
action, ok := paramsMap["action"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("action parameter not found")
|
return nil, util.NewAgentError("action parameter not found", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
@@ -167,7 +168,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
projectId, _ := paramsMap["project"].(string)
|
projectId, _ := paramsMap["project"].(string)
|
||||||
result, err := analyzeTool.projects(ctx, projectId)
|
result, err := analyzeTool.projects(ctx, projectId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error analyzing projects: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error analyzing projects: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, "result = ", result)
|
logger.DebugContext(ctx, "result = ", result)
|
||||||
return result, nil
|
return result, nil
|
||||||
@@ -176,7 +177,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
modelName, _ := paramsMap["model"].(string)
|
modelName, _ := paramsMap["model"].(string)
|
||||||
result, err := analyzeTool.models(ctx, projectName, modelName)
|
result, err := analyzeTool.models(ctx, projectName, modelName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error analyzing models: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error analyzing models: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, "result = ", result)
|
logger.DebugContext(ctx, "result = ", result)
|
||||||
return result, nil
|
return result, nil
|
||||||
@@ -185,12 +186,12 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
|
|||||||
exploreName, _ := paramsMap["explore"].(string)
|
exploreName, _ := paramsMap["explore"].(string)
|
||||||
result, err := analyzeTool.explores(ctx, modelName, exploreName)
|
result, err := analyzeTool.explores(ctx, modelName, exploreName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error analyzing explores: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error analyzing explores: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, "result = ", result)
|
logger.DebugContext(ctx, "result = ", result)
|
||||||
return result, nil
|
return result, nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unknown action: %s", action)
|
return nil, util.NewAgentError(fmt.Sprintf("unknown action: %s", action), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,23 +232,23 @@ type analyzeTool struct {
|
|||||||
minQueries int
|
minQueries int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *analyzeTool) projects(ctx context.Context, id string) ([]map[string]interface{}, error) {
|
func (t *analyzeTool) projects(ctx context.Context, id string) ([]map[string]interface{}, util.ToolboxError) {
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var projects []*v4.Project
|
var projects []*v4.Project
|
||||||
if id != "" {
|
if id != "" {
|
||||||
p, err := t.SdkClient.Project(id, "", nil)
|
p, err := t.SdkClient.Project(id, "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error fetching project %s: %w", id, err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error fetching project %s: %v", id, err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
projects = append(projects, &p)
|
projects = append(projects, &p)
|
||||||
} else {
|
} else {
|
||||||
allProjects, err := t.SdkClient.AllProjects("", nil)
|
allProjects, err := t.SdkClient.AllProjects("", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error fetching all projects: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error fetching all projects: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
for i := range allProjects {
|
for i := range allProjects {
|
||||||
projects = append(projects, &allProjects[i])
|
projects = append(projects, &allProjects[i])
|
||||||
@@ -256,13 +257,19 @@ func (t *analyzeTool) projects(ctx context.Context, id string) ([]map[string]int
|
|||||||
|
|
||||||
var results []map[string]interface{}
|
var results []map[string]interface{}
|
||||||
for _, p := range projects {
|
for _, p := range projects {
|
||||||
|
if p.Name == nil {
|
||||||
|
return nil, util.NewAgentError("project name is nil", nil)
|
||||||
|
}
|
||||||
pName := *p.Name
|
pName := *p.Name
|
||||||
|
if p.Id == nil {
|
||||||
|
return nil, util.NewAgentError("project ID is nil", nil)
|
||||||
|
}
|
||||||
pID := *p.Id
|
pID := *p.Id
|
||||||
logger.InfoContext(ctx, fmt.Sprintf("Analyzing project: %s", pName))
|
logger.InfoContext(ctx, fmt.Sprintf("Analyzing project: %s", pName))
|
||||||
|
|
||||||
projectFiles, err := t.SdkClient.AllProjectFiles(pID, "", nil)
|
projectFiles, err := t.SdkClient.AllProjectFiles(pID, "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error fetching files for project %s: %w", pName, err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error fetching files for project %s: %v", pName, err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
modelCount := 0
|
modelCount := 0
|
||||||
@@ -285,6 +292,13 @@ func (t *analyzeTool) projects(ctx context.Context, id string) ([]map[string]int
|
|||||||
gitConnectionStatus = "Bare repo, no tests required"
|
gitConnectionStatus = "Bare repo, no tests required"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if p.PullRequestMode == nil {
|
||||||
|
return nil, util.NewAgentError("pull request mode is nil", nil)
|
||||||
|
}
|
||||||
|
if p.ValidationRequired == nil {
|
||||||
|
return nil, util.NewAgentError("validation required is nil", nil)
|
||||||
|
}
|
||||||
|
|
||||||
results = append(results, map[string]interface{}{
|
results = append(results, map[string]interface{}{
|
||||||
"Project": pName,
|
"Project": pName,
|
||||||
"# Models": modelCount,
|
"# Models": modelCount,
|
||||||
@@ -297,21 +311,21 @@ func (t *analyzeTool) projects(ctx context.Context, id string) ([]map[string]int
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *analyzeTool) models(ctx context.Context, project, model string) ([]map[string]interface{}, error) {
|
func (t *analyzeTool) models(ctx context.Context, project, model string) ([]map[string]interface{}, util.ToolboxError) {
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.InfoContext(ctx, "Analyzing models...")
|
logger.InfoContext(ctx, "Analyzing models...")
|
||||||
|
|
||||||
usedModels, err := t.getUsedModels(ctx)
|
usedModels, err := t.getUsedModels(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("error getting used models", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lookmlModels, err := t.SdkClient.AllLookmlModels(v4.RequestAllLookmlModels{}, nil)
|
lookmlModels, err := t.SdkClient.AllLookmlModels(v4.RequestAllLookmlModels{}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error fetching LookML models: %w", err)
|
return nil, util.NewClientServerError("error fetching LookML models", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var results []map[string]interface{}
|
var results []map[string]interface{}
|
||||||
@@ -329,6 +343,13 @@ func (t *analyzeTool) models(ctx context.Context, project, model string) ([]map[
|
|||||||
exploreCount = len(*m.Explores)
|
exploreCount = len(*m.Explores)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if m.ProjectName == nil {
|
||||||
|
return nil, util.NewAgentError("model project name is nil", nil)
|
||||||
|
}
|
||||||
|
if m.Name == nil {
|
||||||
|
return nil, util.NewAgentError("model name is nil", nil)
|
||||||
|
}
|
||||||
|
|
||||||
results = append(results, map[string]interface{}{
|
results = append(results, map[string]interface{}{
|
||||||
"Project": *m.ProjectName,
|
"Project": *m.ProjectName,
|
||||||
"Model": *m.Name,
|
"Model": *m.Name,
|
||||||
@@ -340,7 +361,7 @@ func (t *analyzeTool) models(ctx context.Context, project, model string) ([]map[
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *analyzeTool) getUsedModels(ctx context.Context) (map[string]int, error) {
|
func (t *analyzeTool) getUsedModels(ctx context.Context) (map[string]int, util.ToolboxError) {
|
||||||
limit := "5000"
|
limit := "5000"
|
||||||
query := &v4.WriteQuery{
|
query := &v4.WriteQuery{
|
||||||
Model: "system__activity",
|
Model: "system__activity",
|
||||||
@@ -356,22 +377,30 @@ func (t *analyzeTool) getUsedModels(ctx context.Context) (map[string]int, error)
|
|||||||
}
|
}
|
||||||
raw, err := lookercommon.RunInlineQuery(ctx, t.SdkClient, query, "json", nil)
|
raw, err := lookercommon.RunInlineQuery(ctx, t.SdkClient, query, "json", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError(fmt.Sprintf("error running inline query for used models: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var data []map[string]interface{}
|
var data []map[string]interface{}
|
||||||
_ = json.Unmarshal([]byte(raw), &data)
|
if err := json.Unmarshal([]byte(raw), &data); err != nil {
|
||||||
|
return nil, util.NewClientServerError(fmt.Sprintf("error unmarshaling used models data: %v", err), http.StatusInternalServerError, err)
|
||||||
|
}
|
||||||
|
|
||||||
results := make(map[string]int)
|
results := make(map[string]int)
|
||||||
for _, row := range data {
|
for _, row := range data {
|
||||||
model, _ := row["query.model"].(string)
|
model, ok := row["query.model"].(string)
|
||||||
count, _ := row["history.query_run_count"].(float64)
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("error casting 'query.model' in used models data", nil)
|
||||||
|
}
|
||||||
|
count, ok := row["history.query_run_count"].(float64)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("error casting 'history.query_run_count' in used models data", nil)
|
||||||
|
}
|
||||||
results[model] = int(count)
|
results[model] = int(count)
|
||||||
}
|
}
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *analyzeTool) getUsedExploreFields(ctx context.Context, model, explore string) (map[string]int, error) {
|
func (t *analyzeTool) getUsedExploreFields(ctx context.Context, model, explore string) (map[string]int, util.ToolboxError) {
|
||||||
limit := "5000"
|
limit := "5000"
|
||||||
query := &v4.WriteQuery{
|
query := &v4.WriteQuery{
|
||||||
Model: "system__activity",
|
Model: "system__activity",
|
||||||
@@ -388,19 +417,30 @@ func (t *analyzeTool) getUsedExploreFields(ctx context.Context, model, explore s
|
|||||||
}
|
}
|
||||||
raw, err := lookercommon.RunInlineQuery(ctx, t.SdkClient, query, "json", nil)
|
raw, err := lookercommon.RunInlineQuery(ctx, t.SdkClient, query, "json", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError(fmt.Sprintf("error running inline query for used explore fields: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var data []map[string]interface{}
|
var data []map[string]interface{}
|
||||||
_ = json.Unmarshal([]byte(raw), &data)
|
if err := json.Unmarshal([]byte(raw), &data); err != nil {
|
||||||
|
return nil, util.NewClientServerError(fmt.Sprintf("error unmarshaling used explore fields data: %v", err), http.StatusInternalServerError, err)
|
||||||
|
}
|
||||||
|
|
||||||
results := make(map[string]int)
|
results := make(map[string]int)
|
||||||
fieldRegex := regexp.MustCompile(`(\w+\.\w+)`)
|
fieldRegex := regexp.MustCompile(`(\w+\.\w+)`)
|
||||||
|
|
||||||
for _, row := range data {
|
for _, row := range data {
|
||||||
count, _ := row["history.query_run_count"].(float64)
|
count, ok := row["history.query_run_count"].(float64)
|
||||||
formattedFields, _ := row["query.formatted_fields"].(string)
|
if !ok {
|
||||||
filters, _ := row["query.filters"].(string)
|
return nil, util.NewAgentError("error casting 'history.query_run_count' in used explore fields data", nil)
|
||||||
|
}
|
||||||
|
formattedFields, ok := row["query.formatted_fields"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("error casting 'query.formatted_fields' in used explore fields data", nil)
|
||||||
|
}
|
||||||
|
filters, ok := row["query.filters"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, util.NewAgentError("error casting 'query.filters' in used explore fields data", nil)
|
||||||
|
}
|
||||||
|
|
||||||
usedFields := make(map[string]bool)
|
usedFields := make(map[string]bool)
|
||||||
|
|
||||||
@@ -418,21 +458,24 @@ func (t *analyzeTool) getUsedExploreFields(ctx context.Context, model, explore s
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *analyzeTool) explores(ctx context.Context, model, explore string) ([]map[string]interface{}, error) {
|
func (t *analyzeTool) explores(ctx context.Context, model, explore string) ([]map[string]interface{}, util.ToolboxError) {
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get logger from ctx: %s", err)
|
return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.InfoContext(ctx, "Analyzing explores...")
|
logger.InfoContext(ctx, "Analyzing explores...")
|
||||||
|
|
||||||
lookmlModels, err := t.SdkClient.AllLookmlModels(v4.RequestAllLookmlModels{}, nil)
|
lookmlModels, err := t.SdkClient.AllLookmlModels(v4.RequestAllLookmlModels{}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error fetching LookML models: %w", err)
|
return nil, util.NewClientServerError(fmt.Sprintf("error fetching LookML models: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var results []map[string]interface{}
|
var results []map[string]interface{}
|
||||||
for _, m := range lookmlModels {
|
for _, m := range lookmlModels {
|
||||||
if model != "" && (m.Name == nil || *m.Name != model) {
|
if m.Name == nil {
|
||||||
|
return nil, util.NewAgentError("model name is nil", nil)
|
||||||
|
}
|
||||||
|
if model != "" && *m.Name != model {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if m.Explores == nil {
|
if m.Explores == nil {
|
||||||
@@ -440,10 +483,10 @@ func (t *analyzeTool) explores(ctx context.Context, model, explore string) ([]ma
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, e := range *m.Explores {
|
for _, e := range *m.Explores {
|
||||||
if explore != "" && (e.Name == nil || *e.Name != explore) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if e.Name == nil {
|
if e.Name == nil {
|
||||||
|
return nil, util.NewAgentError("explore name is nil", nil)
|
||||||
|
}
|
||||||
|
if explore != "" && *e.Name != explore {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -461,6 +504,12 @@ func (t *analyzeTool) explores(ctx context.Context, model, explore string) ([]ma
|
|||||||
|
|
||||||
fieldCount := 0
|
fieldCount := 0
|
||||||
if exploreDetail.Fields != nil {
|
if exploreDetail.Fields != nil {
|
||||||
|
if exploreDetail.Fields.Dimensions == nil {
|
||||||
|
return nil, util.NewAgentError("explore dimensions are nil", nil)
|
||||||
|
}
|
||||||
|
if exploreDetail.Fields.Measures == nil {
|
||||||
|
return nil, util.NewAgentError("explore measures are nil", nil)
|
||||||
|
}
|
||||||
fieldCount = len(*exploreDetail.Fields.Dimensions) + len(*exploreDetail.Fields.Measures)
|
fieldCount = len(*exploreDetail.Fields.Dimensions) + len(*exploreDetail.Fields.Measures)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -477,12 +526,30 @@ func (t *analyzeTool) explores(ctx context.Context, model, explore string) ([]ma
|
|||||||
|
|
||||||
allFields := []string{}
|
allFields := []string{}
|
||||||
if exploreDetail.Fields != nil {
|
if exploreDetail.Fields != nil {
|
||||||
|
if exploreDetail.Fields.Dimensions == nil {
|
||||||
|
return nil, util.NewAgentError("explore dimensions are nil", nil)
|
||||||
|
}
|
||||||
for _, d := range *exploreDetail.Fields.Dimensions {
|
for _, d := range *exploreDetail.Fields.Dimensions {
|
||||||
|
if d.Hidden == nil {
|
||||||
|
return nil, util.NewAgentError("dimension hidden status is nil", nil)
|
||||||
|
}
|
||||||
|
if d.Name == nil {
|
||||||
|
return nil, util.NewAgentError("dimension name is nil", nil)
|
||||||
|
}
|
||||||
if !*d.Hidden {
|
if !*d.Hidden {
|
||||||
allFields = append(allFields, *d.Name)
|
allFields = append(allFields, *d.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if exploreDetail.Fields.Measures == nil {
|
||||||
|
return nil, util.NewAgentError("explore measures are nil", nil)
|
||||||
|
}
|
||||||
for _, ms := range *exploreDetail.Fields.Measures {
|
for _, ms := range *exploreDetail.Fields.Measures {
|
||||||
|
if ms.Hidden == nil {
|
||||||
|
return nil, util.NewAgentError("measure hidden status is nil", nil)
|
||||||
|
}
|
||||||
|
if ms.Name == nil {
|
||||||
|
return nil, util.NewAgentError("measure name is nil", nil)
|
||||||
|
}
|
||||||
if !*ms.Hidden {
|
if !*ms.Hidden {
|
||||||
allFields = append(allFields, *ms.Name)
|
allFields = append(allFields, *ms.Name)
|
||||||
}
|
}
|
||||||
@@ -503,6 +570,9 @@ func (t *analyzeTool) explores(ctx context.Context, model, explore string) ([]ma
|
|||||||
joinStats[join] += queryCount
|
joinStats[join] += queryCount
|
||||||
}
|
}
|
||||||
for _, join := range *exploreDetail.Joins {
|
for _, join := range *exploreDetail.Joins {
|
||||||
|
if join.Name == nil {
|
||||||
|
return nil, util.NewAgentError("join name is nil", nil)
|
||||||
|
}
|
||||||
if _, ok := joinStats[*join.Name]; !ok {
|
if _, ok := joinStats[*join.Name]; !ok {
|
||||||
joinStats[*join.Name] = 0
|
joinStats[*join.Name] = 0
|
||||||
}
|
}
|
||||||
@@ -534,22 +604,32 @@ func (t *analyzeTool) explores(ctx context.Context, model, explore string) ([]ma
|
|||||||
|
|
||||||
rawQueryCount, err := lookercommon.RunInlineQuery(ctx, t.SdkClient, queryCountQueryBody, "json", nil)
|
rawQueryCount, err := lookercommon.RunInlineQuery(ctx, t.SdkClient, queryCountQueryBody, "json", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError(fmt.Sprintf("error running inline query for query count: %v", err), http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
queryCount := 0
|
queryCount := 0
|
||||||
var data []map[string]interface{}
|
var data []map[string]interface{}
|
||||||
_ = json.Unmarshal([]byte(rawQueryCount), &data)
|
if err := json.Unmarshal([]byte(rawQueryCount), &data); err != nil {
|
||||||
|
return nil, util.NewClientServerError(fmt.Sprintf("error unmarshaling query count data: %v", err), http.StatusInternalServerError, err)
|
||||||
|
}
|
||||||
if len(data) > 0 {
|
if len(data) > 0 {
|
||||||
if count, ok := data[0]["history.query_run_count"].(float64); ok {
|
if count, ok := data[0]["history.query_run_count"].(float64); ok {
|
||||||
queryCount = int(count)
|
queryCount = int(count)
|
||||||
|
} else {
|
||||||
|
return nil, util.NewAgentError("error casting 'history.query_run_count' in query count data", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if e.Hidden == nil {
|
||||||
|
return nil, util.NewAgentError("explore hidden status is nil", nil)
|
||||||
|
}
|
||||||
|
if e.Description == nil {
|
||||||
|
return nil, util.NewAgentError("explore description is nil", nil)
|
||||||
|
}
|
||||||
|
|
||||||
results = append(results, map[string]interface{}{
|
results = append(results, map[string]interface{}{
|
||||||
"Model": *m.Name,
|
"Model": *m.Name,
|
||||||
"Explore": *e.Name,
|
"Explore": *e.Name,
|
||||||
"Is Hidden": *e.Hidden,
|
"Is Hidden": *e.Hidden,
|
||||||
"Has Description": e.Description != nil && *e.Description != "",
|
"Has Description": *e.Description != "",
|
||||||
"# Joins": joinCount,
|
"# Joins": joinCount,
|
||||||
"# Unused Joins": unusedJoinsCount,
|
"# Unused Joins": unusedJoinsCount,
|
||||||
"# Unused Fields": unusedFieldsCount,
|
"# Unused Fields": unusedFieldsCount,
|
||||||
|
|||||||
@@ -16,12 +16,14 @@ package redis
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
redissrc "github.com/googleapis/genai-toolbox/internal/sources/redis"
|
redissrc "github.com/googleapis/genai-toolbox/internal/sources/redis"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -84,17 +86,21 @@ type Tool struct {
|
|||||||
mcpManifest tools.McpManifest
|
mcpManifest tools.McpManifest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmds, err := replaceCommandsParams(t.Commands, t.Parameters, params)
|
cmds, err := replaceCommandsParams(t.Commands, t.Parameters, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error replacing commands' parameters: %s", err)
|
return nil, util.NewAgentError("error replacing commands' parameters", err)
|
||||||
}
|
}
|
||||||
return source.RunCommand(ctx, cmds)
|
resp, err := source.RunCommand(ctx, cmds)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ package createbatch
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
dataprocpb "cloud.google.com/go/dataproc/v2/apiv1/dataprocpb"
|
dataprocpb "cloud.google.com/go/dataproc/v2/apiv1/dataprocpb"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
@@ -65,15 +67,18 @@ type Tool struct {
|
|||||||
Parameters parameters.Parameters
|
Parameters parameters.Parameters
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t *Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
batch, err := t.Builder.BuildBatch(params)
|
batch, err := t.Builder.BuildBatch(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to build batch: %w", err)
|
if tbErr, ok := err.(util.ToolboxError); ok {
|
||||||
|
return nil, tbErr
|
||||||
|
}
|
||||||
|
return nil, util.NewAgentError("failed to build batch", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.RuntimeConfig != nil {
|
if t.RuntimeConfig != nil {
|
||||||
@@ -92,11 +97,20 @@ func (t *Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, par
|
|||||||
}
|
}
|
||||||
batch.RuntimeConfig.Version = version
|
batch.RuntimeConfig.Version = version
|
||||||
}
|
}
|
||||||
return source.CreateBatch(ctx, batch)
|
|
||||||
|
resp, err := source.CreateBatch(ctx, batch)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
return parameters.EmbedParams(ctx, t.Parameters, paramValues, embeddingModelsMap, nil)
|
newParamValues, err := parameters.EmbedParams(ctx, t.Parameters, paramValues, embeddingModelsMap, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.NewClientServerError(fmt.Sprintf("error embedding parameters: %v", err), http.StatusInternalServerError, err)
|
||||||
|
}
|
||||||
|
return newParamValues, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tool) Manifest() tools.Manifest {
|
func (t *Tool) Manifest() tools.Manifest {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package serverlesssparkcancelbatch
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
dataproc "cloud.google.com/go/dataproc/v2/apiv1"
|
dataproc "cloud.google.com/go/dataproc/v2/apiv1"
|
||||||
@@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -99,20 +101,26 @@ type Tool struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's operation.
|
// Invoke executes the tool's operation.
|
||||||
func (t *Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t *Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramMap := params.AsMap()
|
paramMap := params.AsMap()
|
||||||
operation, ok := paramMap["operation"].(string)
|
operation, ok := paramMap["operation"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing required parameter: operation")
|
return nil, util.NewAgentError("missing required parameter: operation", nil)
|
||||||
}
|
}
|
||||||
if strings.Contains(operation, "/") {
|
if strings.Contains(operation, "/") {
|
||||||
return nil, fmt.Errorf("operation must be a short operation name without '/': %s", operation)
|
return nil, util.NewAgentError(fmt.Sprintf("operation must be a short operation name without '/': %s", operation), nil)
|
||||||
}
|
}
|
||||||
return source.CancelOperation(ctx, operation)
|
|
||||||
|
resp, err := source.CancelOperation(ctx, operation)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package serverlesssparkgetbatch
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
dataproc "cloud.google.com/go/dataproc/v2/apiv1"
|
dataproc "cloud.google.com/go/dataproc/v2/apiv1"
|
||||||
@@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -99,20 +101,25 @@ type Tool struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's operation.
|
// Invoke executes the tool's operation.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
paramMap := params.AsMap()
|
paramMap := params.AsMap()
|
||||||
name, ok := paramMap["name"].(string)
|
name, ok := paramMap["name"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("missing required parameter: name")
|
return nil, util.NewAgentError("missing required parameter: name", nil)
|
||||||
}
|
}
|
||||||
if strings.Contains(name, "/") {
|
if strings.Contains(name, "/") {
|
||||||
return nil, fmt.Errorf("name must be a short batch name without '/': %s", name)
|
return nil, util.NewAgentError(fmt.Sprintf("name must be a short batch name without '/': %s", name), nil)
|
||||||
}
|
}
|
||||||
return source.GetBatch(ctx, name)
|
|
||||||
|
resp, err := source.GetBatch(ctx, name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ package serverlesssparklistbatches
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
dataproc "cloud.google.com/go/dataproc/v2/apiv1"
|
dataproc "cloud.google.com/go/dataproc/v2/apiv1"
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -100,23 +102,39 @@ type Tool struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the tool's operation.
|
// Invoke executes the tool's operation.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramMap := params.AsMap()
|
paramMap := params.AsMap()
|
||||||
var pageSize *int
|
var pageSize *int
|
||||||
if ps, ok := paramMap["pageSize"]; ok && ps != nil {
|
if ps, ok := paramMap["pageSize"]; ok && ps != nil {
|
||||||
pageSizeV := ps.(int)
|
pageSizeV, ok := ps.(int)
|
||||||
|
if !ok {
|
||||||
|
// Handle float64 case if unmarshaled from JSON usually
|
||||||
|
if f, ok := ps.(float64); ok {
|
||||||
|
pageSizeV = int(f)
|
||||||
|
} else {
|
||||||
|
return nil, util.NewAgentError("pageSize must be an integer", nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if pageSizeV <= 0 {
|
if pageSizeV <= 0 {
|
||||||
return nil, fmt.Errorf("pageSize must be positive: %d", pageSizeV)
|
return nil, util.NewAgentError(fmt.Sprintf("pageSize must be positive: %d", pageSizeV), nil)
|
||||||
}
|
}
|
||||||
pageSize = &pageSizeV
|
pageSize = &pageSizeV
|
||||||
}
|
}
|
||||||
|
|
||||||
pt, _ := paramMap["pageToken"].(string)
|
pt, _ := paramMap["pageToken"].(string)
|
||||||
filter, _ := paramMap["filter"].(string)
|
filter, _ := paramMap["filter"].(string)
|
||||||
return source.ListBatches(ctx, pageSize, pt, filter)
|
|
||||||
|
resp, err := source.ListBatches(ctx, pageSize, pt, filter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGcpError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -98,25 +99,30 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke executes the provided SQL query using the tool's database connection and returns the results.
|
// Invoke executes the provided SQL query using the tool's database connection and returns the results.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
sql, ok := paramsMap["sql"].(string)
|
sqlStr, ok := paramsMap["sql"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unable to get cast %s", paramsMap["sql"])
|
return nil, util.NewAgentError(fmt.Sprintf("unable to cast parameter 'sql' to string: %v", paramsMap["sql"]), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the query executed for debugging.
|
// Log the query executed for debugging.
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting logger: %s", err)
|
return nil, util.NewClientServerError("error getting logger", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, "executing `%s` tool query: %s", resourceType, sql)
|
logger.DebugContext(ctx, fmt.Sprintf("executing `%s` tool query: %s", resourceType, sqlStr))
|
||||||
return source.RunSQL(ctx, sql, nil)
|
|
||||||
|
resp, err := source.RunSQL(ctx, sqlStr, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -18,11 +18,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||||
|
"github.com/googleapis/genai-toolbox/internal/util"
|
||||||
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
"github.com/googleapis/genai-toolbox/internal/util/parameters"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -126,25 +128,29 @@ func (t Tool) ToConfig() tools.ToolConfig {
|
|||||||
// Returns:
|
// Returns:
|
||||||
// - A slice of maps, where each map represents a row with column names as keys.
|
// - A slice of maps, where each map represents a row with column names as keys.
|
||||||
// - An error if template resolution, parameter extraction, query execution, or result processing fails.
|
// - An error if template resolution, parameter extraction, query execution, or result processing fails.
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
paramsMap := params.AsMap()
|
paramsMap := params.AsMap()
|
||||||
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
newStatement, err := parameters.ResolveTemplateParams(t.TemplateParameters, t.Statement, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract template params %w", err)
|
return nil, util.NewAgentError("unable to extract template params", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
newParams, err := parameters.GetParams(t.Parameters, paramsMap)
|
newParams, err := parameters.GetParams(t.Parameters, paramsMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to extract standard params %w", err)
|
return nil, util.NewAgentError("unable to extract standard params", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sliceParams := newParams.AsSlice()
|
sliceParams := newParams.AsSlice()
|
||||||
return source.RunSQL(ctx, newStatement, sliceParams)
|
resp, err := source.RunSQL(ctx, newStatement, sliceParams)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package snowflakeexecutesql
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
"github.com/googleapis/genai-toolbox/internal/embeddingmodels"
|
||||||
@@ -89,26 +90,30 @@ type Tool struct {
|
|||||||
mcpManifest tools.McpManifest
|
mcpManifest tools.McpManifest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, error) {
|
func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) {
|
||||||
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapParams := params.AsMap()
|
mapParams := params.AsMap()
|
||||||
sql, ok := mapParams["sql"].(string)
|
sqlStr, ok := mapParams["sql"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("invalid parameters: sql parameter is not a string")
|
return nil, util.NewAgentError("invalid parameters: sql parameter is not a string", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the query executed for debugging.
|
// Log the query executed for debugging.
|
||||||
logger, err := util.LoggerFromContext(ctx)
|
logger, err := util.LoggerFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting logger: %s", err)
|
return nil, util.NewClientServerError("error getting logger", http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
logger.DebugContext(ctx, fmt.Sprintf("executing `%s` tool query: %s", resourceType, sql))
|
logger.DebugContext(ctx, fmt.Sprintf("executing `%s` tool query: %s", resourceType, sqlStr))
|
||||||
return source.RunSQL(ctx, sql, nil)
|
|
||||||
|
|
||||||
|
resp, err := source.RunSQL(ctx, sqlStr, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ProcessGeneralError(err)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user