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 ---
|
// --- 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) {
|
func (s *Manager) Complete(cwd, filePath, prefix, suffix, extraContext string) (string, error) {
|
||||||
if cwd == "" {
|
if cwd == "" {
|
||||||
cwd, _ = os.Getwd()
|
cwd, _ = os.Getwd()
|
||||||
|
|
@ -1162,18 +1162,6 @@ func (s *Manager) Complete(cwd, filePath, prefix, suffix, extraContext string) (
|
||||||
suffix = suffix[:suffixMax]
|
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 := ""
|
fileHint := ""
|
||||||
if filePath != "" {
|
if filePath != "" {
|
||||||
fileHint = " in " + filePath
|
fileHint = " in " + filePath
|
||||||
|
|
@ -1191,79 +1179,20 @@ func (s *Manager) Complete(cwd, filePath, prefix, suffix, extraContext string) (
|
||||||
%s
|
%s
|
||||||
</suffix>`, fileHint, contextBlock, prefix, suffix)
|
</suffix>`, fileHint, contextBlock, prefix, suffix)
|
||||||
|
|
||||||
sess.Core.Submit(sess.Ctx, "/clear")
|
result, err := backend.Generate(context.Background(), backend.GenerateRequest{
|
||||||
sess.Core.Submit(sess.Ctx, prompt)
|
Prompt: prompt,
|
||||||
|
Backend: backendName,
|
||||||
for {
|
Model: model,
|
||||||
state := sess.Core.State()
|
})
|
||||||
if state == "idle" {
|
if err != nil {
|
||||||
break
|
return "", err
|
||||||
}
|
|
||||||
next, ok := sess.Core.WaitChange(sess.Ctx, agent.WatchState, state)
|
|
||||||
if !ok {
|
|
||||||
return "", fmt.Errorf("copilot session cancelled")
|
|
||||||
}
|
|
||||||
if next == "idle" {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result := sess.Core.Reply()
|
|
||||||
result = stripCompletionNoise(result)
|
result = stripCompletionNoise(result)
|
||||||
result = stripPrefixEcho(prefix, result)
|
result = stripPrefixEcho(prefix, result)
|
||||||
return result, nil
|
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 {
|
func stripCompletionNoise(s string) string {
|
||||||
var lines []string
|
var lines []string
|
||||||
|
|
|
||||||
Reference in New Issue