complete: use backend.Generate instead of copilot session
This commit is contained in:
parent
b410133759
commit
f630c644e1
|
|
@ -1140,7 +1140,7 @@ func squashWhitespace(s string) string {
|
|||
|
||||
// --- Code Completion ---
|
||||
|
||||
// Complete performs a single-shot code completion using a dedicated copilot session.
|
||||
// Complete performs a stateless one-shot code completion via backend.Generate.
|
||||
func (s *Manager) Complete(cwd, filePath, prefix, suffix, extraContext string) (string, error) {
|
||||
if cwd == "" {
|
||||
cwd, _ = os.Getwd()
|
||||
|
|
@ -1162,18 +1162,6 @@ func (s *Manager) Complete(cwd, filePath, prefix, suffix, extraContext string) (
|
|||
suffix = suffix[:suffixMax]
|
||||
}
|
||||
|
||||
sessID := s.findOrCreateCopilot(cwd, backendName, model)
|
||||
if sessID == "" {
|
||||
return "", fmt.Errorf("failed to create copilot session")
|
||||
}
|
||||
|
||||
s.mu.RLock()
|
||||
sess, ok := s.sessions[sessID]
|
||||
s.mu.RUnlock()
|
||||
if !ok {
|
||||
return "", fmt.Errorf("copilot session disappeared")
|
||||
}
|
||||
|
||||
fileHint := ""
|
||||
if filePath != "" {
|
||||
fileHint = " in " + filePath
|
||||
|
|
@ -1191,79 +1179,20 @@ func (s *Manager) Complete(cwd, filePath, prefix, suffix, extraContext string) (
|
|||
%s
|
||||
</suffix>`, fileHint, contextBlock, prefix, suffix)
|
||||
|
||||
sess.Core.Submit(sess.Ctx, "/clear")
|
||||
sess.Core.Submit(sess.Ctx, prompt)
|
||||
|
||||
for {
|
||||
state := sess.Core.State()
|
||||
if state == "idle" {
|
||||
break
|
||||
}
|
||||
next, ok := sess.Core.WaitChange(sess.Ctx, agent.WatchState, state)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("copilot session cancelled")
|
||||
}
|
||||
if next == "idle" {
|
||||
break
|
||||
}
|
||||
result, err := backend.Generate(context.Background(), backend.GenerateRequest{
|
||||
Prompt: prompt,
|
||||
Backend: backendName,
|
||||
Model: model,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
result := sess.Core.Reply()
|
||||
result = stripCompletionNoise(result)
|
||||
result = stripPrefixEcho(prefix, result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Manager) findOrCreateCopilot(cwd, backendName, modelName string) string {
|
||||
sessID := fmt.Sprintf("%d-copilot", crc32Str(cwd))
|
||||
|
||||
s.mu.RLock()
|
||||
_, exists := s.sessions[sessID]
|
||||
s.mu.RUnlock()
|
||||
if exists {
|
||||
return sessID
|
||||
}
|
||||
|
||||
cfg := LoadAgentConfig(s.cfg.AgentsDir, "copilot", nil)
|
||||
be, err := backend.NewWithName(backendName)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if modelName != "" {
|
||||
be.SetModel(modelName)
|
||||
}
|
||||
|
||||
uname := s.nextUname()
|
||||
newDisp := tools.NewDispatcherFunc(map[string]func() tools.Server{
|
||||
"execute": execute.Decl(cwd),
|
||||
})
|
||||
rt := agent.BuildRuntime(cfg, newDisp(), cwd, []string{"OLLIE_SESSION_ID=" + sessID})
|
||||
|
||||
core := agent.NewAgentCore(agent.AgentCoreConfig{
|
||||
Backend: be,
|
||||
AgentName: "copilot",
|
||||
AgentsDir: s.cfg.AgentsDir,
|
||||
SessionsDir: s.cfg.SessionsDir,
|
||||
SessionID: sessID,
|
||||
Uname: uname,
|
||||
CWD: cwd,
|
||||
Runtime: rt,
|
||||
NewDispatcher: newDisp,
|
||||
Log: s.cfg.Sink.NewLogger("copilot"),
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
sess := NewSession(sessID, core, ctx, cancel)
|
||||
sess.uname = uname
|
||||
|
||||
s.mu.Lock()
|
||||
s.sessions[sessID] = sess
|
||||
s.mu.Unlock()
|
||||
if s.cfg.OnSessionCreated != nil {
|
||||
s.cfg.OnSessionCreated(sessID, sess)
|
||||
}
|
||||
return sessID
|
||||
}
|
||||
|
||||
func stripCompletionNoise(s string) string {
|
||||
var lines []string
|
||||
|
|
|
|||
Reference in New Issue