NewWithPipes creates an ACPAgentIO connected via the provided pipes
(ctx context.Context, toAgent io.Writer, fromAgent io.Reader, logger *slog.Logger, getwd func() (string, error))
| 132 | |
| 133 | // NewWithPipes creates an ACPAgentIO connected via the provided pipes |
| 134 | func NewWithPipes(ctx context.Context, toAgent io.Writer, fromAgent io.Reader, logger *slog.Logger, getwd func() (string, error)) (*ACPAgentIO, error) { |
| 135 | if logger == nil { |
| 136 | logger = slog.Default() |
| 137 | } |
| 138 | agentIO := &ACPAgentIO{ctx: ctx, logger: logger} |
| 139 | client := &acpClient{agentIO: agentIO} |
| 140 | |
| 141 | conn := acp.NewClientSideConnection(client, toAgent, fromAgent) |
| 142 | agentIO.conn = conn |
| 143 | |
| 144 | logger.Debug("Initializing ACP connection") |
| 145 | |
| 146 | // Initialize the connection |
| 147 | initResp, err := conn.Initialize(ctx, acp.InitializeRequest{ |
| 148 | ProtocolVersion: acp.ProtocolVersionNumber, |
| 149 | ClientCapabilities: acp.ClientCapabilities{}, |
| 150 | }) |
| 151 | if err != nil { |
| 152 | logger.Error("Failed to initialize ACP connection", "error", err) |
| 153 | return nil, err |
| 154 | } |
| 155 | logger.Debug("ACP initialized", "protocolVersion", initResp.ProtocolVersion) |
| 156 | |
| 157 | // Create a session |
| 158 | cwd, err := getwd() |
| 159 | if err != nil { |
| 160 | logger.Error("Failed to get working directory", "error", err) |
| 161 | return nil, err |
| 162 | } |
| 163 | sessResp, err := conn.NewSession(ctx, acp.NewSessionRequest{ |
| 164 | Cwd: cwd, |
| 165 | McpServers: []acp.McpServer{}, |
| 166 | }) |
| 167 | if err != nil { |
| 168 | logger.Error("Failed to create ACP session", "error", err) |
| 169 | return nil, err |
| 170 | } |
| 171 | agentIO.sessionID = sessResp.SessionId |
| 172 | logger.Debug("ACP session created", "sessionId", sessResp.SessionId) |
| 173 | |
| 174 | return agentIO, nil |
| 175 | } |
| 176 | |
| 177 | // Write sends a message to the agent via ACP prompt |
| 178 | func (a *ACPAgentIO) Write(data []byte) (int, error) { |