67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package toolsrv
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// --- Exported accessors for builtin tool handlers ---
|
|
|
|
// CWD returns the current working directory for this server.
|
|
func (e *Server) CWD() string {
|
|
e.wdMu.RLock()
|
|
defer e.wdMu.RUnlock()
|
|
return e.cwd
|
|
}
|
|
|
|
// ToolRegistry returns the attached tool registry, or nil.
|
|
func (e *Server) ToolRegistry() *Registry { return e.toolRegistry }
|
|
|
|
// SessionID returns the session ID associated with this server.
|
|
func (e *Server) SessionID() string { return e.sessionID }
|
|
|
|
// InjectContent calls the OnInjection hook if set.
|
|
func (e *Server) InjectContent(content string) {
|
|
if e.OnInjection != nil {
|
|
e.OnInjection(content)
|
|
}
|
|
}
|
|
|
|
// SetOnToolsChanged sets the callback for tool directory changes.
|
|
func (e *Server) SetOnToolsChanged(fn func(string)) {
|
|
e.OnToolsChanged = fn
|
|
}
|
|
|
|
// ToolRegistryRevision returns the current registry revision for this server's session.
|
|
// Returns 0 if no registry is configured.
|
|
func (e *Server) ToolRegistryRevision() uint64 {
|
|
if e.toolRegistry == nil || e.sessionID == "" {
|
|
return 0
|
|
}
|
|
return e.toolRegistry.Revision(e.sessionID)
|
|
}
|
|
|
|
// ExecuteInSandbox runs a command in the sandbox. Exported for builtin tools.
|
|
func (e *Server) ExecuteInSandbox(ctx context.Context, cmd, language string, timeout int, sandbox string, trusted bool, stdin string, detach bool) (string, error) {
|
|
return e.executeWithStdin(ctx, cmd, language, timeout, sandbox, trusted, stdin, detach)
|
|
}
|
|
|
|
// ExecuteElevated runs a command outside the sandbox via the elevation broker.
|
|
func (e *Server) ExecuteElevated(ctx context.Context, cmd, dir string, timeout int, detach bool) (string, error) {
|
|
return e.executeElevated(ctx, cmd, dir, timeout, detach)
|
|
}
|
|
|
|
// buildToolListing formats a slice of ToolInfo into the preamble listing
|
|
// format consumed by agent.refreshToolListing: "- **name** — description\n" per entry.
|
|
// Only local tools (Server == "") with a non-empty description are included.
|
|
func buildToolListing(infos []ToolInfo) string {
|
|
var sb strings.Builder
|
|
for _, ti := range infos {
|
|
if ti.Description != "" && ti.Server == "" {
|
|
fmt.Fprintf(&sb, "- **%s** — %s\n", ti.Name, ti.Description)
|
|
}
|
|
}
|
|
return sb.String()
|
|
}
|