-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathsetup.go
More file actions
124 lines (107 loc) · 3.02 KB
/
setup.go
File metadata and controls
124 lines (107 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package httpapi
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"time"
"github.com/coder/agentapi/lib/logctx"
mf "github.com/coder/agentapi/lib/msgfmt"
"github.com/coder/agentapi/lib/termexec"
"github.com/coder/agentapi/x/acpio"
"github.com/coder/quartz"
)
type SetupProcessConfig struct {
Program string
ProgramArgs []string
TerminalWidth uint16
TerminalHeight uint16
AgentType mf.AgentType
}
func SetupProcess(ctx context.Context, config SetupProcessConfig) (*termexec.Process, error) {
logger := logctx.From(ctx)
logger.Info(fmt.Sprintf("Running: %s %s", config.Program, strings.Join(config.ProgramArgs, " ")))
process, err := termexec.StartProcess(ctx, termexec.StartProcessConfig{
Program: config.Program,
Args: config.ProgramArgs,
TerminalWidth: config.TerminalWidth,
TerminalHeight: config.TerminalHeight,
})
if err != nil {
logger.Error(fmt.Sprintf("Error starting process: %v", err))
os.Exit(1)
}
// Hack for sourcegraph amp to stop the animation.
if config.AgentType == mf.AgentTypeAmp {
_, err = process.Write([]byte(" \b"))
if err != nil {
return nil, err
}
}
return process, nil
}
type SetupACPConfig struct {
Program string
ProgramArgs []string
MCPFilePath string
Clock quartz.Clock
}
// SetupACPResult contains the result of setting up an ACP process.
type SetupACPResult struct {
AgentIO *acpio.ACPAgentIO
Wait func() error // Calls cmd.Wait() and returns exit error
Done chan struct{} // Close this when Wait() returns to clean up goroutine
}
func SetupACP(ctx context.Context, config SetupACPConfig) (*SetupACPResult, error) {
logger := logctx.From(ctx)
if config.Clock == nil {
config.Clock = quartz.NewReal()
}
args := config.ProgramArgs
logger.Info(fmt.Sprintf("Running (ACP): %s %s", config.Program, strings.Join(args, " ")))
cmd := exec.CommandContext(ctx, config.Program, args...)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("failed to create stdin pipe: %w", err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("failed to create stdout pipe: %w", err)
}
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed to start process: %w", err)
}
agentIO, err := acpio.NewWithPipes(ctx, stdin, stdout, logger, os.Getwd, config.MCPFilePath)
if err != nil {
_ = cmd.Process.Kill()
return nil, fmt.Errorf("failed to initialize ACP connection: %w", err)
}
done := make(chan struct{})
go func() {
select {
case <-ctx.Done():
logger.Info("Context done, closing ACP agent")
// Try graceful shutdown first
_ = cmd.Process.Signal(syscall.SIGTERM)
// Then close pipes
_ = stdin.Close()
_ = stdout.Close()
// Force kill after timeout
config.Clock.AfterFunc(5*time.Second, func() {
_ = cmd.Process.Kill()
})
return
case <-done:
// Process exited normally, nothing to clean up
return
}
}()
return &SetupACPResult{
AgentIO: agentIO,
Wait: cmd.Wait,
Done: done,
}, nil
}