mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-02-05 12:45:11 -05:00
## Description Update error log to more details when initialization fails. * When a toolset name is not valid, show the toolset name (similar with promptset). * When tool does not exist, show the tool name that is missing (similar with prompt). ## PR Checklist > Thank you for opening a Pull Request! Before submitting your PR, there are a > few things you can do to make sure it goes smoothly: - [x] Make sure you reviewed [CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md) - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) - [x] Make sure to add `!` if this involve a breaking change 🛠️ Fixes #1579
74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
// Copyright 2024 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package tools
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
type ToolsetConfig struct {
|
|
Name string `yaml:"name"`
|
|
ToolNames []string `yaml:",inline"`
|
|
}
|
|
|
|
type Toolset struct {
|
|
ToolsetConfig
|
|
Tools []*Tool `yaml:",inline"`
|
|
Manifest ToolsetManifest `yaml:",inline"`
|
|
McpManifest []McpManifest `yaml:",inline"`
|
|
}
|
|
|
|
func (t Toolset) ToConfig() ToolsetConfig {
|
|
return t.ToolsetConfig
|
|
}
|
|
|
|
type ToolsetManifest struct {
|
|
ServerVersion string `json:"serverVersion"`
|
|
ToolsManifest map[string]Manifest `json:"tools"`
|
|
}
|
|
|
|
func (t ToolsetConfig) Initialize(serverVersion string, toolsMap map[string]Tool) (Toolset, error) {
|
|
// finish toolset setup
|
|
// Check each declared tool name exists
|
|
var toolset Toolset
|
|
toolset.Name = t.Name
|
|
if !IsValidName(toolset.Name) {
|
|
return toolset, fmt.Errorf("invalid toolset name: %s", toolset.Name)
|
|
}
|
|
toolset.Tools = make([]*Tool, 0, len(t.ToolNames))
|
|
toolset.Manifest = ToolsetManifest{
|
|
ServerVersion: serverVersion,
|
|
ToolsManifest: make(map[string]Manifest),
|
|
}
|
|
for _, toolName := range t.ToolNames {
|
|
tool, ok := toolsMap[toolName]
|
|
if !ok {
|
|
return toolset, fmt.Errorf("tool does not exist: %s", toolName)
|
|
}
|
|
toolset.Tools = append(toolset.Tools, &tool)
|
|
toolset.Manifest.ToolsManifest[toolName] = tool.Manifest()
|
|
toolset.McpManifest = append(toolset.McpManifest, tool.McpManifest())
|
|
}
|
|
|
|
return toolset, nil
|
|
}
|
|
|
|
var validName = regexp.MustCompile(`^[a-zA-Z0-9_-]*$`)
|
|
|
|
func IsValidName(s string) bool {
|
|
return validName.MatchString(s)
|
|
}
|