Use ollie agent helpers and tool Decls; drop local duplicates
Replace local agentConfigPath, newSessionID, home paths with agent.AgentConfigPath, agent.NewSessionID, agent.DefaultAgentsDir, agent.DefaultSessionsDir. Replace dispatcher wiring with tools.NewDispatcherFunc using execute.Decl and file.Decl. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
1158efffe3
commit
f97b9d355c
|
|
@ -18,7 +18,6 @@ package p9
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"io"
|
"io"
|
||||||
|
|
@ -26,7 +25,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"9fans.net/go/plan9"
|
"9fans.net/go/plan9"
|
||||||
"ollie/pkg/agent"
|
"ollie/pkg/agent"
|
||||||
|
|
@ -105,11 +103,10 @@ type Server struct {
|
||||||
|
|
||||||
// New creates a new Server.
|
// New creates a new Server.
|
||||||
func New() *Server {
|
func New() *Server {
|
||||||
home, _ := os.UserHomeDir()
|
|
||||||
return &Server{
|
return &Server{
|
||||||
sessions: make(map[string]*session),
|
sessions: make(map[string]*session),
|
||||||
agentsDir: home + "/.config/ollie/agents",
|
agentsDir: agent.DefaultAgentsDir(),
|
||||||
sessionsDir: home + "/.config/ollie/sessions",
|
sessionsDir: agent.DefaultSessionsDir(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -646,8 +643,6 @@ func (s *Server) handleRootCtl(input string) {
|
||||||
// Recognised keys: backend, model, agent. Unknown keys are rejected.
|
// Recognised keys: backend, model, agent. Unknown keys are rejected.
|
||||||
// Example: new backend=ollama model=qwen3:8b agent=myagent
|
// Example: new backend=ollama model=qwen3:8b agent=myagent
|
||||||
func (s *Server) createSession(args []string) error {
|
func (s *Server) createSession(args []string) error {
|
||||||
home, _ := os.UserHomeDir()
|
|
||||||
|
|
||||||
backendOverride := ""
|
backendOverride := ""
|
||||||
modelOverride := ""
|
modelOverride := ""
|
||||||
agentName := "default"
|
agentName := "default"
|
||||||
|
|
@ -693,21 +688,16 @@ func (s *Server) createSession(args []string) error {
|
||||||
return fmt.Errorf("sessions dir: %w", err)
|
return fmt.Errorf("sessions dir: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfgPath := agentConfigPath(s.agentsDir, agentName)
|
cfgPath := agent.AgentConfigPath(s.agentsDir, agentName)
|
||||||
cfg, _ := config.Load(cfgPath) // nil cfg is handled by BuildAgentEnv
|
cfg, _ := config.Load(cfgPath) // nil cfg is handled by BuildAgentEnv
|
||||||
|
|
||||||
newDisp := func() tools.Dispatcher {
|
newDisp := tools.NewDispatcherFunc(map[string]func() tools.Server{
|
||||||
d := tools.NewDispatcher()
|
"execute": execute.Decl,
|
||||||
d.AddServer("execute", execute.New(
|
"file": file.Decl,
|
||||||
home+"/.local/state/ollie",
|
})
|
||||||
home+"/.cache/ollie/exec",
|
|
||||||
))
|
|
||||||
d.AddServer("file", file.New())
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
|
|
||||||
env := agent.BuildAgentEnv(cfg, newDisp())
|
env := agent.BuildAgentEnv(cfg, newDisp())
|
||||||
sessID := newSessionID()
|
sessID := agent.NewSessionID()
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
core := agent.NewAgentCore(agent.AgentCoreConfig{
|
core := agent.NewAgentCore(agent.AgentCoreConfig{
|
||||||
|
|
@ -751,24 +741,7 @@ func (s *Server) killSession(id string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// agentConfigPath resolves the config file for a named agent.
|
|
||||||
func agentConfigPath(agentsDir, name string) string {
|
|
||||||
p := agentsDir + "/" + name + ".json"
|
|
||||||
if _, err := os.Stat(p); err == nil {
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
if name == "default" {
|
|
||||||
home, _ := os.UserHomeDir()
|
|
||||||
return home + "/.config/ollie/config.json"
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func newSessionID() string {
|
|
||||||
b := make([]byte, 3)
|
|
||||||
rand.Read(b) //nolint:errcheck
|
|
||||||
return time.Now().Format("20060102-150405") + "-" + fmt.Sprintf("%06x", b)
|
|
||||||
}
|
|
||||||
|
|
||||||
// formatEvent converts an agent Event to bytes for appending to the chat log.
|
// formatEvent converts an agent Event to bytes for appending to the chat log.
|
||||||
// Output matches ollie-tui's MakeOutputFn so the chat file looks identical to
|
// Output matches ollie-tui's MakeOutputFn so the chat file looks identical to
|
||||||
|
|
|
||||||
Reference in New Issue