78e6858751
RegisterStore(reg, StoreDeps) registers the persistent-memory tools over the host's KV and/or File backends: - kv_get/set/list/delete (KVStorage seam) - file_save/get/get_text/get_metadata/list/delete (FileStorage seam), plus file_search (FileSearcher) and create_file_url (FileTokenMinter) when wired. Near-zero-config: Quota defaults to a generous static cap (staticQuota), the per-value/per-file caps default, and the kv vs file groups register independently (a host can take just one). Seams moved clean (interface-only): kv_storage.go, quota_provider.go, file_descendant_grant.go. The default in-memory KV/File backends come with contrib/store at P4. Core go.sum still free of gorm/redis/discordgo/sqlite. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
// file_delete removes a saved file by its file_id. Decrements the
|
|
// underlying blob's refcount in storage; the blob row is removed when
|
|
// refcount hits zero.
|
|
//
|
|
// Why scope is checked POST-fetch (mirrors file_get): file_id is the
|
|
// only key the caller has; we must read the row to know the scope.
|
|
package tools
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"gitea.stevedudenhoeffer.com/steve/executus/tool"
|
|
)
|
|
|
|
type fileDeleteArgs struct {
|
|
FileID string `json:"file_id" description:"Opaque file ID returned by file_save or file_list."`
|
|
}
|
|
|
|
// NewFileDelete constructs the file_delete tool. storage nil → "not
|
|
// configured" at execute time.
|
|
func NewFileDelete(storage FileStorage) tool.Tool {
|
|
return tool.NewGatedTool[fileDeleteArgs](
|
|
"file_delete",
|
|
"Remove a saved file by file_id. Returns 'ok' on success or 'not_found' if no file matched.",
|
|
tool.Permission{
|
|
AuthoringRequirement: tool.RequirementAnyone,
|
|
OperatesOn: tool.ScopeCaller,
|
|
SafeForShare: true,
|
|
Categories: []string{"storage", "write"},
|
|
},
|
|
func(ctx context.Context, inv tool.Invocation, args fileDeleteArgs) (string, error) {
|
|
if storage == nil {
|
|
return "", fmt.Errorf("file_delete: not configured")
|
|
}
|
|
if args.FileID == "" {
|
|
return "", fmt.Errorf("file_delete: file_id required")
|
|
}
|
|
|
|
// Fetch first so we can validate scope before deleting. The
|
|
// extra read is acceptable for a write path that's not in
|
|
// the hot loop, and it preserves the cross-skill /
|
|
// cross-user safety story.
|
|
meta, _, err := storage.FileGet(ctx, args.FileID)
|
|
if err != nil {
|
|
if errors.Is(err, ErrFileNotFound) {
|
|
return "not_found", nil
|
|
}
|
|
return "", fmt.Errorf("file_delete: %w", err)
|
|
}
|
|
if meta.SkillID != inv.SkillID {
|
|
return "", fmt.Errorf("file_delete: file does not belong to this skill")
|
|
}
|
|
if err := ValidateScope(inv, meta.Scope, false); err != nil {
|
|
return "", fmt.Errorf("file_delete: %w", err)
|
|
}
|
|
|
|
if err := storage.FileDelete(ctx, args.FileID); err != nil {
|
|
if errors.Is(err, ErrFileNotFound) {
|
|
// Race: row was deleted between FileGet and
|
|
// FileDelete. Surface as a clean miss.
|
|
return "not_found", nil
|
|
}
|
|
return "", fmt.Errorf("file_delete: %w", err)
|
|
}
|
|
return "ok", nil
|
|
},
|
|
)
|
|
}
|