Skip to content

Commit

Permalink
Chore: Refactor tool indexing
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <daishan@acorn.io>
  • Loading branch information
StrongMonkey committed Feb 5, 2025
1 parent 285d246 commit 42dd7fa
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 325 deletions.
15 changes: 9 additions & 6 deletions apiclient/types/toolreference.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ const (
type ToolReferenceManifest struct {
Name string `json:"name"`
ToolType ToolReferenceType `json:"toolType"`
Commit string `json:"commit,omitempty"`
Reference string `json:"reference,omitempty"`
Active bool `json:"active,omitempty"`
}

type ToolReference struct {
Metadata
ToolReferenceManifest
Resolved bool `json:"resolved,omitempty"`
Error string `json:"error,omitempty"`
Builtin bool `json:"builtin,omitempty"`
Description string `json:"description,omitempty"`
Credentials []string `json:"credentials,omitempty"`
Params map[string]string `json:"params,omitempty"`
Resolved bool `json:"resolved,omitempty"`
Error string `json:"error,omitempty"`
Builtin bool `json:"builtin,omitempty"`
Description string `json:"description,omitempty"`
Credentials []string `json:"credentials,omitempty"`
Params map[string]string `json:"params,omitempty"`
Bundle bool `json:"bundle,omitempty"`
BundleToolName string `json:"bundleToolName,omitempty"`
}

type ToolReferenceList List[ToolReference]
34 changes: 19 additions & 15 deletions pkg/api/handlers/toolreferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/obot-platform/obot/apiclient/types"
"github.com/obot-platform/obot/pkg/api"
v1 "github.com/obot-platform/obot/pkg/storage/apis/obot.obot.ai/v1"
"github.com/obot-platform/obot/pkg/tool"

Check failure on line 15 in pkg/api/handlers/toolreferences.go

View workflow job for this annotation

GitHub Actions / lint

no required module provides package github.com/obot-platform/obot/pkg/tool; to add it:
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -33,10 +34,13 @@ func convertToolReference(toolRef v1.ToolReference) types.ToolReference {
Name: toolRef.Name,
ToolType: toolRef.Spec.Type,
Reference: toolRef.Spec.Reference,
Commit: toolRef.Status.Commit,
},
Builtin: toolRef.Spec.Builtin,
Error: toolRef.Status.Error,
Resolved: toolRef.Generation == toolRef.Status.ObservedGeneration,
Builtin: toolRef.Spec.Builtin,
Bundle: toolRef.Spec.Bundle,
BundleToolName: toolRef.Spec.BundleToolName,
Error: toolRef.Status.Error,
Resolved: toolRef.Generation == toolRef.Status.ObservedGeneration,
}
if toolRef.Spec.Active == nil {
tf.Active = true
Expand Down Expand Up @@ -123,22 +127,22 @@ func (a *ToolReferenceHandler) Create(req api.Context) (err error) {
return apierrors.NewBadRequest(fmt.Sprintf("invalid tool type %s", newToolReference.ToolType))
}

toolRef := &v1.ToolReference{
ObjectMeta: metav1.ObjectMeta{
Name: newToolReference.Name,
Namespace: req.Namespace(),
},
Spec: v1.ToolReferenceSpec{
Type: newToolReference.ToolType,
Reference: newToolReference.Reference,
},
toolRefs, err := tool.ResolveToolReferences(req.Context(), a.gptscript, newToolReference.Name, newToolReference.Reference, false, newToolReference.ToolType)
if err != nil {
return apierrors.NewBadRequest(fmt.Sprintf("failed to resolve tool references for %s: %v", newToolReference.Reference, err))
}

if err = req.Create(toolRef); err != nil {
return err
if len(toolRefs) == 0 {
return apierrors.NewBadRequest(fmt.Sprintf("no tool references found for %s", newToolReference.Reference))
}

for _, toolRef := range toolRefs {
if err := req.Create(toolRef); err != nil && !apierrors.IsAlreadyExists(err) {
return apierrors.NewInternalError(fmt.Errorf("failed to create tool reference %s: %w", toolRef.Name, err))
}
}

return req.Write(convertToolReference(*toolRef))
return req.Write(convertToolReference(*toolRefs[0]))
}

func (a *ToolReferenceHandler) Delete(req api.Context) error {
Expand Down
14 changes: 7 additions & 7 deletions pkg/controller/data/agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ spec:
modify the files in the workspace. Obot collaborates with the user on the files in the workspace.
alias: obot
tools:
- workspace-files
- time
- knowledge
- database
- tasks
- workspace-files
- time-bundle
- knowledge
- database
- tasks
defaultThreadTools:
- images-bundle
- google-search-bundle
- images-bundle
- google-search-bundle
100 changes: 30 additions & 70 deletions pkg/controller/handlers/toolreference/toolreference.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/obot-platform/obot/pkg/gateway/server/dispatcher"
v1 "github.com/obot-platform/obot/pkg/storage/apis/obot.obot.ai/v1"
"github.com/obot-platform/obot/pkg/system"
"github.com/obot-platform/obot/pkg/tool"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -83,78 +84,19 @@ func (h *Handler) toolsToToolReferences(ctx context.Context, toolType types.Tool
if ref, ok := strings.CutPrefix(entry.Reference, "./"); ok {
entry.Reference = registryURL + "/" + ref
}
if !h.supportDocker && name == system.ShellTool {
continue
}

if entry.All {
prg, err := h.gptClient.LoadFile(ctx, "* from "+entry.Reference)
if err != nil {
log.Errorf("Failed to load tool %s: %v", entry.Reference, err)
continue
}
toolRefs, err := tool.ResolveToolReferences(ctx, h.gptClient, name, entry.Reference, true, toolType)
if err != nil {
log.Errorf("Failed to resolve tool references for %s: %v", entry.Reference, err)
continue
}

tool := prg.ToolSet[prg.EntryToolID]
if isValidTool(tool) {
toolName := tool.Name
if tool.MetaData["bundle"] == "true" {
toolName = "bundle"
}
result = append(result, &v1.ToolReference{
ObjectMeta: metav1.ObjectMeta{
Name: normalize(name, toolName),
Namespace: system.DefaultNamespace,
Finalizers: []string{v1.ToolReferenceFinalizer},
Annotations: annotations,
},
Spec: v1.ToolReferenceSpec{
Type: toolType,
Reference: entry.Reference,
Builtin: true,
},
})
}
for _, peerToolID := range tool.LocalTools {
// If this is the entry tool, then we already added it or skipped it above.
if peerToolID == prg.EntryToolID {
continue
}

peerTool := prg.ToolSet[peerToolID]
if isValidTool(peerTool) {
toolName := peerTool.Name
if peerTool.MetaData["bundle"] == "true" {
toolName += "-bundle"
}
result = append(result, &v1.ToolReference{
ObjectMeta: metav1.ObjectMeta{
Name: normalize(name, toolName),
Namespace: system.DefaultNamespace,
Finalizers: []string{v1.ToolReferenceFinalizer},
Annotations: annotations,
},
Spec: v1.ToolReferenceSpec{
Type: toolType,
Reference: fmt.Sprintf("%s from %s", peerTool.Name, entry.Reference),
Builtin: true,
},
})
}
}
} else {
if !h.supportDocker && name == system.ShellTool {
continue
}
result = append(result, &v1.ToolReference{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: system.DefaultNamespace,
Finalizers: []string{v1.ToolReferenceFinalizer},
Annotations: annotations,
},
Spec: v1.ToolReferenceSpec{
Type: toolType,
Reference: entry.Reference,
Builtin: true,
},
})
for _, toolRef := range toolRefs {
toolRef.Annotations = annotations
result = append(result, toolRef)
}
}

Expand Down Expand Up @@ -251,6 +193,7 @@ func (h *Handler) Populate(req router.Request, resp router.Response) error {
toolRef.Status.LastReferenceCheck = metav1.Now()
toolRef.Status.ObservedGeneration = toolRef.Generation
toolRef.Status.Reference = toolRef.Spec.Reference
toolRef.Status.Commit = ""
toolRef.Status.Tool = nil
toolRef.Status.Error = ""

Expand All @@ -269,6 +212,9 @@ func (h *Handler) Populate(req router.Request, resp router.Response) error {
Metadata: tool.MetaData,
Params: map[string]string{},
}
if tool.Source.Repo != nil {
toolRef.Status.Commit = tool.Source.Repo.Revision
}
if tool.Arguments != nil {
for name, param := range tool.Arguments.Properties {
if param.Value != nil {
Expand Down Expand Up @@ -502,3 +448,17 @@ func (h *Handler) CleanupModelProvider(req router.Request, _ router.Response) er
func modelName(modelProviderName, modelName string) string {
return name.SafeConcatName(system.ModelPrefix, modelProviderName, fmt.Sprintf("%x", sha256.Sum256([]byte(modelName))))
}

func resolveToolReferenceName(toolType types.ToolReferenceType, isBundle bool, isCapability bool, toolName string, subToolName string) string {
if toolType == types.ToolReferenceTypeTool {
if isBundle {
if isCapability {
return toolName
}
return toolName + "-bundle"
}
return normalize(toolName, subToolName)
}

return toolName
}
13 changes: 8 additions & 5 deletions pkg/storage/apis/obot.obot.ai/v1/toolreference.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ func (in *ToolReference) GetColumns() [][]string {
}

type ToolReferenceSpec struct {
Type types.ToolReferenceType `json:"type,omitempty"`
Builtin bool `json:"builtin,omitempty"`
Reference string `json:"reference,omitempty"`
Active *bool `json:"active,omitempty"`
ForceRefresh metav1.Time `json:"forceRefresh,omitempty"`
Type types.ToolReferenceType `json:"type,omitempty"`
Builtin bool `json:"builtin,omitempty"`
Reference string `json:"reference,omitempty"`
Active *bool `json:"active,omitempty"`
Bundle bool `json:"bundle,omitempty"`
BundleToolName string `json:"bundleToolName,omitempty"`
ForceRefresh metav1.Time `json:"forceRefresh,omitempty"`
}

type ToolShortDescription struct {
Expand All @@ -70,6 +72,7 @@ type ToolShortDescription struct {

type ToolReferenceStatus struct {
Reference string `json:"reference,omitempty"`
Commit string `json:"commit,omitempty"`
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
LastReferenceCheck metav1.Time `json:"lastReferenceCheck,omitempty"`
Tool *ToolShortDescription `json:"tool,omitempty"`
Expand Down
24 changes: 24 additions & 0 deletions pkg/storage/openapi/generated/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions tools/devmode-kubeconfig
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
apiVersion: v1
clusters:
- cluster:
server: https://localhost:8443
insecure-skip-tls-verify: true
server: https://localhost:8443
name: local-cluster
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUVMRENDQXBTZ0F3SUJBZ0lRTmxGZkZseVJxU1NsWUlZbG5oQ2lJekFOQmdrcWhraUc5dzBCQVFzRkFEQXYKTVMwd0t3WURWUVFERXlSaVl6QmtOak0zWmkxaE0yVTN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Z2ZUxEeUVJSUZUeHBxRC9QbW93RFFZSgpLb1pJaHZjTkFRRUxCUUFEZ2dHQkFEazRnaDUrK3FNcjNVbEhDODNlTWJWbERmOEpBbHBzSkd0bEs1QzBjOVdSCk12bzRWMkFoNHpzeUpPS0pTNlRhd2RYMVlGQmxVUzJZNitkWTRBaDc0NXQwa29XWEs1R2JxNnJvNGROL3BteEEKZ2Q3N1JHNGFxZHUyK2NOYkRlOHkrSW9rSEJvN091MnJ3Zk1YeENWNSs0YWlJQUpaQlE5T3RyaVo5UFRBaFNiYwpJdUQ2TllxUTBrZE5Day9iZUhWZW01WCtiNGJ4aVJ6RUJZQkl4SHdac3VZT1NmR1lBVFE2QlZMOVJqQ0FKMi8zClNZS2JoSlJUQWxRN2kvYXhBWjZIZ1ROS25oeUNodk05UkZZV3BLVW5scEFMek9zR2RoU25LaU5PQXF0eW9JdWkKdVBzZHhNenloNGV1Z3cyeGtSQ2wxZ2ZxZXVJTnU1UjVWejk0Zm5LcFhleGFSd1FHdWlFejJ2bmpHTG9zV0QzTQpiUllkQjV1bEM5b1kzamc1c3RnTU5wTmw5Qyt3M3VLajd6WlhSQnBmNWFZMHpjUXZrdVJNdjFEYmNva3pFR2xiCjlzRVJBOGM5M3dmaERUK3lUUnZFSm5wNGY0YjFNcHlFN0RMSzZ0Z0VrVTduVXM3MDRBYXJzSUVUNTdpMDhqTlAKSjZZQzFDUURsQ0FXYTFudW14cFB5Zz09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
server: https://34.67.1.138
name: gke_obot-infra_us-central1-a_obot
contexts:
- context:
cluster: local-cluster
user: local-user
name: local-context
current-context: local-context
- context:
cluster: gke_obot-infra_us-central1-a_obot
user: gke_obot-infra_us-central1-a_obot
name: gke_obot-infra_us-central1-a_obot
current-context: gke_obot-infra_us-central1-a_obot
kind: Config
preferences: {}
users:
- name: local-user
user:
token: adminpass
- name: gke_obot-infra_us-central1-a_obot
user:
exec:
apiVersion: client.authentication.k8s.io/v1beta1
command: /opt/homebrew/share/google-cloud-sdk/bin/gke-gcloud-auth-plugin
installHint: Install gke-gcloud-auth-plugin for use with kubectl by following
https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl#install_plugin
provideClusterInfo: true
Loading

0 comments on commit 42dd7fa

Please sign in to comment.