feat(tools/io): 内置 IO/网络工具(file(路径安全)/http(SSRF防护)/web-search/web-fetch/knowledge)+ 大小截断

This commit is contained in:
Aska committed 2026-09-06 06:29:03 +08:00
1 parent 488cf9c79c
commit 3ce9489f8e
17 files changed
+2223

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
package io
import (
"io"
"log/slog"
"net/http/httptest"
"net/url"
"testing"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
"git.yeij.top/AskaEth/Cyrene_v2/internal/logger"
"git.yeij.top/AskaEth/Cyrene_v2/pkg/plugins/sdk"
)
// silentLogger is the base logger used by test fixtures so that expected
// security-denial WARN records are not dumped into the test output.
func silentLogger() *slog.Logger {
return logger.New(logger.Options{Out: io.Discard})
}
// buildTools builds a tool set over cfg with a silent logger, applying opts on
// top (caller options win over the default discard logger).
func buildTools(t *testing.T, cfg *config.Config, opts ...Option) []sdk.Tool {
t.Helper()
all := append([]Option{WithLogger(silentLogger())}, opts...)
set, err := New(cfg, all...)
if err != nil {
t.Fatalf("New: %v", err)
}
return set
}
// findTool returns the tool with the given name from a set, failing the test
// when it is absent.
func findTool(t *testing.T, set []sdk.Tool, name string) sdk.Tool {
t.Helper()
for _, tool := range set {
if tool.Name() == name {
return tool
}
}
t.Fatalf("tool %q not found in set", name)
return nil
}
// serverHost returns the bare host (no port, no brackets) of a test server, the
// value to put on the SSRF allowlist so the tools may reach it.
func serverHost(srv *httptest.Server) string {
u, err := url.Parse(srv.URL)
if err != nil {
return ""
}
return u.Hostname()
}