Checkpoint commit covering concurrent work across several archie-core-abg epics to avoid losing in-progress state: - Memory manager (18.7-18.16): background sync pipeline, prefetch with timeout, memory-write notification, threat scanning, shutdown drain with abandoned-write tracking, frozen snapshot pattern. Wired ScanContent and NotifyMemoryToolWrite into HandleToolCall so scanning and notification actually apply to writes, not just exist as unused primitives. - Fixed a literal unresolved merge-conflict that had been committed to internal/memory/scrubber/scrubber.go. - Tool provider/registry, MCP client/transport (stdio, HTTP, SSE), multimodal results, disclosure. - Pairing device auth store, rate limiting. - Gateway compression, session store, tasks. - Various workflow, plugin host, and agentexec updates.
94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package agentexec
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
aicore "github.com/samcharles93/ai-sdk/core"
|
|
|
|
"github.com/samcharles93/archie-core/internal/tools"
|
|
)
|
|
|
|
// BuildToolSet converts every currently-available entry in reg into an
|
|
// ai-sdk core.ToolSet, ready to pass as core.GenerateOptions.Tools for a
|
|
// multi-step chat turn. A nil registry (memory/tools disabled) returns
|
|
// an empty set. Invalid availability callbacks and dynamic schemas return
|
|
// errors instead of panicking or silently hiding an advertised tool.
|
|
//
|
|
// core.Tool.Execute exchanges JSON-encoded strings with the model;
|
|
// tools.Handler exchanges a decoded map. The returned Execute functions
|
|
// do the JSON <-> map translation and surface decode failures as errors
|
|
// rather than silently dropping malformed model input.
|
|
func BuildToolSet(reg *tools.Registry) (aicore.ToolSet, error) {
|
|
set := aicore.ToolSet{}
|
|
if reg == nil {
|
|
return set, nil
|
|
}
|
|
for _, entry := range reg.All() {
|
|
available, err := safeToolAvailable(entry)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("tool %s availability: %w", entry.Name, err)
|
|
}
|
|
if !available {
|
|
continue
|
|
}
|
|
schema, err := safeResolvedSchema(entry)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("tool %s schema: %w", entry.Name, err)
|
|
}
|
|
if schema == nil {
|
|
schema = tools.JSONSchema{
|
|
"type": "object",
|
|
"properties": map[string]any{},
|
|
}
|
|
}
|
|
params, err := json.Marshal(schema)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("tool %s schema: %w", entry.Name, err)
|
|
}
|
|
set[entry.Name] = aicore.NewTool(entry.Name, entry.Description, params, toolExecute(entry))
|
|
}
|
|
return set, nil
|
|
}
|
|
|
|
func safeToolAvailable(entry tools.ToolEntry) (available bool, err error) {
|
|
defer func() {
|
|
if recovered := recover(); recovered != nil {
|
|
err = fmt.Errorf("panic: %v", recovered)
|
|
}
|
|
}()
|
|
return entry.Available(), nil
|
|
}
|
|
|
|
func safeResolvedSchema(entry tools.ToolEntry) (schema tools.JSONSchema, err error) {
|
|
defer func() {
|
|
if recovered := recover(); recovered != nil {
|
|
err = fmt.Errorf("panic: %v", recovered)
|
|
}
|
|
}()
|
|
return entry.ResolvedSchema(), nil
|
|
}
|
|
|
|
// toolExecute adapts a tools.ToolEntry's Handler to core.Tool's
|
|
// Execute(ctx, jsonInput string) (jsonOutput string, err error) shape.
|
|
func toolExecute(entry tools.ToolEntry) func(context.Context, string) (string, error) {
|
|
return func(ctx context.Context, input string) (string, error) {
|
|
var args map[string]any
|
|
if input != "" {
|
|
if err := json.Unmarshal([]byte(input), &args); err != nil {
|
|
return "", fmt.Errorf("tool %s: decode input: %w", entry.Name, err)
|
|
}
|
|
}
|
|
out, err := entry.Handler(ctx, args)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
data, err := json.Marshal(out)
|
|
if err != nil {
|
|
return "", fmt.Errorf("tool %s: encode output: %w", entry.Name, err)
|
|
}
|
|
return string(data), nil
|
|
}
|
|
}
|