143 lines
3.7 KiB
Go
143 lines
3.7 KiB
Go
package fs
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"ollie/agent"
|
|
"ollie/backend"
|
|
"ollie/paths"
|
|
"ollie/toolsrv"
|
|
)
|
|
|
|
// ── Root-level handlers ──────────────────────────────────────────
|
|
|
|
func readBackends(ctx HandlerCtx) ([]byte, error) {
|
|
return []byte(strings.Join(backend.Backends(), "\n") + "\n"), nil
|
|
}
|
|
|
|
func readHelp(ctx HandlerCtx) ([]byte, error) {
|
|
helpPath := paths.CfgDir() + "/help.md"
|
|
data, err := os.ReadFile(helpPath)
|
|
if err != nil {
|
|
return []byte("help not available\n"), nil
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func readModels(ctx HandlerCtx) ([]byte, error) {
|
|
if ctx.Models != nil {
|
|
return ctx.Models.Get(), nil
|
|
}
|
|
return []byte("(no model cache)\n"), nil
|
|
}
|
|
|
|
func readAgents(ctx HandlerCtx) ([]byte, error) {
|
|
var sb strings.Builder
|
|
for _, dir := range agent.AgentsDirs() {
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, e := range entries {
|
|
if !e.IsDir() && strings.HasSuffix(e.Name(), ".json") {
|
|
sb.WriteString(strings.TrimSuffix(e.Name(), ".json"))
|
|
sb.WriteByte('\n')
|
|
}
|
|
}
|
|
}
|
|
return []byte(sb.String()), nil
|
|
}
|
|
|
|
// readTools lists all discoverable tools from disk.
|
|
// This is a global catalog — the same for all sessions and agents.
|
|
func readTools(ctx HandlerCtx) ([]byte, error) {
|
|
tools := toolsrv.DiscoverTools()
|
|
if len(tools) == 0 {
|
|
return []byte("(no tools found)\n"), nil
|
|
}
|
|
var sb strings.Builder
|
|
for _, ti := range tools {
|
|
fmt.Fprintf(&sb, "%-24s %s\n", ti.Name, ti.Description)
|
|
}
|
|
return []byte(sb.String()), nil
|
|
}
|
|
|
|
func writeRootCtl(ctx HandlerCtx, data []byte) error {
|
|
cmd := strings.TrimSpace(string(data))
|
|
switch cmd {
|
|
case "invalidate":
|
|
if ctx.Models != nil {
|
|
ctx.Models.Invalidate()
|
|
}
|
|
if ctx.Invalidate != nil {
|
|
ctx.Invalidate()
|
|
}
|
|
return nil
|
|
case "kill":
|
|
if ctx.Shutdown != nil {
|
|
ctx.Shutdown()
|
|
}
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("unknown ctl command: %s (valid: invalidate, kill)", cmd)
|
|
}
|
|
}
|
|
|
|
// ── Eventwait ────────────────────────────────────────────────────
|
|
|
|
func readEventwait(ctx HandlerCtx) ([]byte, error) {
|
|
return nil, fmt.Errorf("eventwait: use blocking read")
|
|
}
|
|
|
|
func blockEventwait(ctx HandlerCtx, cctx context.Context, base string) ([]byte, string, error) {
|
|
return WaitEvent(cctx, base)
|
|
}
|
|
|
|
// ── Request-response handlers (complete, generate, route) ──────
|
|
|
|
func requestComplete(ctx HandlerCtx, data []byte) ([]byte, error) {
|
|
var req struct {
|
|
CWD string `json:"cwd"`
|
|
FilePath string `json:"file"`
|
|
Prefix string `json:"prefix"`
|
|
Suffix string `json:"suffix"`
|
|
ExtraContext string `json:"context"`
|
|
}
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
req.Prefix = strings.TrimSpace(string(data))
|
|
}
|
|
result, err := agent.Complete(req.CWD, req.FilePath, req.Prefix, req.Suffix, req.ExtraContext)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []byte(result), nil
|
|
}
|
|
|
|
func requestGenerate(ctx HandlerCtx, data []byte) ([]byte, error) {
|
|
var req backend.GenerateRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
req.Prompt = strings.TrimSpace(string(data))
|
|
}
|
|
result, err := backend.Generate(context.Background(), req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []byte(result), nil
|
|
}
|
|
|
|
func requestRoute(ctx HandlerCtx, data []byte) ([]byte, error) {
|
|
var req backend.RouteRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
req.Task = strings.TrimSpace(string(data))
|
|
}
|
|
result, err := backend.Route(context.Background(), req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []byte(fmt.Sprintf("backend=%s model=%s", result.Backend, result.Model)), nil
|
|
}
|