zot/packages/core/tool.go
patriceckhart fa7d8d8be5 refactor: split source into packages/{provider,core,tui,agent}
Single Go module, four top-level packages under packages/. Import
paths become github.com/patriceckhart/zot/packages/<name>; downstream
consumers can depend on individual packages without pulling the rest.

Layout:
  packages/provider/     LLM clients + catalog
  packages/provider/auth/ credential store + OAuth + login server
  packages/core/         agent loop, sessions, cost
  packages/tui/          terminal toolkit + chat view
  packages/agent/        CLI wiring, system prompt
    extensions/ extproto/ modes/ tools/ skills/ swarm/
    sdk/  (was pkg/zotcore, package renamed zotcore -> sdk)
    ext/  (was pkg/zotext, package renamed zotext -> ext)

internal/ and pkg/ removed. The internal/assets logo moved into
packages/provider/auth/assets.

Public Go SDK identifiers renamed:
  pkg/zotcore (package zotcore) -> packages/agent/sdk (package sdk)
  pkg/zotext  (package zotext)  -> packages/agent/ext (package ext)

This breaks Go-based extensions and embedders; the JSON wire protocol
for extensions and RPC is unchanged, so non-Go extensions, already-
built extension binaries, and zot rpc consumers are unaffected.

Docs, examples, and the built-in write-zot-extension skill updated
for the new paths and identifiers. Shadow-bug fixes in code samples
(ext := ext.New -> e := ext.New).
2026-05-27 09:07:15 +02:00

81 lines
2.2 KiB
Go

// Package core implements the agent loop, tool runtime, and session
// persistence. It is provider-agnostic: it talks to an LLM only through
// the provider.Client interface.
package core
import (
"context"
"encoding/json"
"fmt"
"sort"
"github.com/patriceckhart/zot/packages/provider"
)
// Tool is a capability the agent can invoke.
type Tool interface {
// Name is the unique tool id shown to the LLM.
Name() string
// Description is a one-line summary shown to the LLM.
Description() string
// Schema is a JSON Schema object for Execute's args.
Schema() json.RawMessage
// Execute runs the tool. progress may be called any number of times
// with partial textual output (for UIs); it is not sent to the LLM.
Execute(ctx context.Context, args json.RawMessage, progress func(string)) (ToolResult, error)
}
// ToolResult is the outcome of Tool.Execute.
type ToolResult struct {
// Content is sent back to the LLM (text and/or images).
Content []provider.Content
// IsError marks this result as an error to the LLM.
IsError bool
// Details is arbitrary data for UIs and logs; not sent to the LLM.
Details any
}
// Registry is a name->Tool map.
type Registry map[string]Tool
// NewRegistry builds a Registry from a list of tools.
func NewRegistry(tools ...Tool) Registry {
r := Registry{}
for _, t := range tools {
r[t.Name()] = t
}
return r
}
// Specs returns the tool definitions to advertise to the LLM.
// Sorted by tool name so the order is stable across requests. This
// is load-bearing for provider-side prompt caching: providers
// prefix-match tool definitions, and Go's map iteration order is
// randomized per call, which would otherwise bust the cache every
// single turn.
func (r Registry) Specs() []provider.Tool {
names := make([]string, 0, len(r))
for name := range r {
names = append(names, name)
}
sort.Strings(names)
out := make([]provider.Tool, 0, len(r))
for _, name := range names {
t := r[name]
out = append(out, provider.Tool{
Name: t.Name(),
Description: t.Description(),
Schema: t.Schema(),
})
}
return out
}
// Get looks up a tool by name.
func (r Registry) Get(name string) (Tool, error) {
t, ok := r[name]
if !ok {
return nil, fmt.Errorf("unknown tool %q", name)
}
return t, nil
}