Files
LocalAI/pkg/addons/contract/resourcelist.go
T
Ettore Di Giacinto 1834d4fd72 refactor(addons): make manifest api 2 the only version
No release ever shipped add-ons, so there is no installed base on api 1.
The manifest must now declare api {"min": 2, "max": 2}; a manifest with
api 1, no api or a wider range is refused with a message that names the
range.

Every field that was gated on api 2 is now part of the contract, so the
Min*API constants and their gates are gone, and so are HostAPIMin and
the apiMin host field. Three older-manifest shims go with them: the
resource listing the host implied for one add-on, the legacyArgs hint
for arguments an older version of a tool took, and the tests that
checked the gates. All shipped manifests and the test fixtures declare
api 2.

Assisted-by: Claude:claude-sonnet-5-5 go test
2026-11-03 16:15:07 +00:00

56 lines
1.7 KiB
Go

// SPDX-License-Identifier: MIT
package contract
import (
"fmt"
"slices"
)
// MaxListedResources bounds the entries of the listing the host builds. The
// answer says when it is cut short.
const MaxListedResources = 100
// DefaultResourceListItems is the key of the result array when the add-on
// names none.
const DefaultResourceListItems = "resources"
// ResourceList declares that the host offers a read tool listing the
// resources of the add-on the caller may use. The add-on writes no code for
// it: the host builds the tool from its own record of the grants. The model
// then finds the IDs that every tool of a resource takes.
type ResourceList struct {
// Tool is the name of the tool, in the form of any tool name. It must not
// name a tool the add-on declares.
Tool string `json:"tool"`
// Items is the key of the result array. It defaults to
// DefaultResourceListItems.
Items string `json:"items,omitempty"`
}
// ItemsKey returns the key of the result array.
func (l ResourceList) ItemsKey() string {
if l.Items == "" {
return DefaultResourceListItems
}
return l.Items
}
func validateResourceList(c Capabilities) error {
if c.Resources == nil || c.Resources.List == nil {
return nil
}
l := c.Resources.List
if !namespace.MatchString(l.Tool) || (l.Items != "" && !toolPropertyName.MatchString(l.Items)) {
return fmt.Errorf("invalid resources list")
}
if slices.Contains(c.Tools, l.Tool) || slices.Contains(c.AdminTools, l.Tool) {
return fmt.Errorf("resources list tool %q is also a declared tool", l.Tool)
}
for _, d := range c.ToolDefinitions {
if d.Name == l.Tool {
return fmt.Errorf("resources list tool %q is also a declared tool", l.Tool)
}
}
return nil
}