Files
mc-god/internal/pkg/tools/tools.go

67 lines
1.6 KiB
Go
Raw Normal View History

2026-02-15 21:25:17 -08:00
// Package tools collects tools
package tools
import (
"context"
"fmt"
2026-02-17 09:01:41 -08:00
"log/slog"
2026-02-15 21:25:17 -08:00
"github.com/ollama/ollama/api"
"tipsy.codes/charles/mc-god/v2/internal/pkg/rcon"
2026-02-17 09:01:41 -08:00
"tipsy.codes/charles/mc-god/v2/internal/pkg/tools/give"
"tipsy.codes/charles/mc-god/v2/internal/pkg/tools/say"
"tipsy.codes/charles/mc-god/v2/internal/pkg/tools/teleport"
"tipsy.codes/charles/mc-god/v2/internal/pkg/tools/time"
"tipsy.codes/charles/mc-god/v2/internal/pkg/tools/weather"
"tipsy.codes/charles/mc-god/v2/internal/pkg/tools/zombie"
2026-02-15 21:25:17 -08:00
)
type Tool interface {
Do(ctx context.Context, toolCall api.ToolCall, client *rcon.Client) error
Desc() api.Tool
Name() string
}
type Tools map[string]Tool
func New(tools ...Tool) Tools {
ret := make(map[string]Tool)
for _, tool := range tools {
ret[tool.Name()] = tool
}
return ret
}
2026-02-17 09:01:41 -08:00
// DefaultTools returns the default set of tools for mc-god
func DefaultTools() Tools {
return New(
say.Get(),
teleport.Get(),
give.Get(),
weather.Get(),
time.Get(),
zombie.Get(),
)
}
2026-02-15 21:25:17 -08:00
func (t Tools) AsAPI() api.Tools {
var ret api.Tools
for _, tool := range t {
ret = append(ret, tool.Desc())
}
return ret
}
func (t Tools) Do(ctx context.Context, toolCall api.ToolCall, client *rcon.Client) error {
tool, found := t[toolCall.Function.Name]
if !found {
return fmt.Errorf("unknown tool %q", toolCall.Function.Name)
}
2026-02-17 09:01:41 -08:00
args, err := toolCall.Function.Arguments.MarshalJSON()
if err != nil {
return fmt.Errorf("failed to marshal args: %v", err)
}
slog.Info("calling function", "name", toolCall.Function.Name, "args", string(args))
2026-02-15 21:25:17 -08:00
return tool.Do(ctx, toolCall, client)
}