Adds a minimal but real ACP stack for the opencode process: - pkg/config: process.args default ["acp"] (opencodeCommand still "opencode") - internal/process: NewManager(command, args) — exec.Command uses args - internal/acp (new): NDJSON transport + JSON-RPC client over the existing process stdio. Implements initialize / session/new / session/prompt / session/cancel. Serves fs/read_text_file and fs/write_text_file from the workspace's fs.FileSystem. terminal/* requests get MethodNotFound. - internal/service/acp_service: per-workspace Client + mutex; starts the process on first prompt; transparently re-init on restart. - internal/api/acp_handler: GET /acp/status, GET /acp/history, POST /acp/prompt, POST /acp/cancel. - internal/model/acp: API DTOs. - internal/acp/client_test: NDJSON split-lines, request/response correlation, notification dispatch, agent-initiated request handling (fs + terminal). Existing process WS endpoint and Shell subsystem are unchanged. Conversation: 019f354c-a51b-7ec3-83ad-c647e9b50b19
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"codespace/internal/api"
|
|
"codespace/internal/process"
|
|
"codespace/internal/service"
|
|
"codespace/internal/shell"
|
|
"codespace/internal/workspace"
|
|
"codespace/pkg/config"
|
|
"codespace/pkg/logger"
|
|
)
|
|
|
|
func main() {
|
|
startupLogger := slog.New(slog.NewJSONHandler(os.Stderr, nil))
|
|
|
|
cfg, err := config.Load("configs/config.yaml")
|
|
if err != nil {
|
|
startupLogger.Error("failed to load config", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
lg, err := logger.New(cfg.Log)
|
|
if err != nil {
|
|
startupLogger.Error("failed to initialize logger", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
workspaces := workspace.NewLocalManager(cfg.Workspace.Root)
|
|
processes := process.NewManager(cfg.Process.OpenCodeCommand, cfg.Process.Args)
|
|
shellMgr := shell.NewManager(cfg.Shell.Command, cfg.Shell.Args)
|
|
|
|
workspaceSvc := service.NewWorkspaceService(workspaces, processes, shellMgr, lg)
|
|
fileSvc := service.NewFileService(workspaces, cfg.File.MaxWriteBytes)
|
|
processSvc := service.NewProcessService(workspaces, processes, lg)
|
|
shellSvc := service.NewShellService(workspaces, shellMgr, lg)
|
|
acpSvc := service.NewAcpService(processes, workspaces, lg)
|
|
|
|
router := api.NewRouter(workspaceSvc, fileSvc, processSvc, shellSvc, acpSvc, lg, cfg.Gin.Mode)
|
|
|
|
server := &http.Server{
|
|
Addr: cfg.Server.Addr,
|
|
Handler: router,
|
|
ReadTimeout: cfg.Server.ReadTimeout,
|
|
WriteTimeout: cfg.Server.WriteTimeout,
|
|
IdleTimeout: cfg.Server.IdleTimeout,
|
|
MaxHeaderBytes: cfg.Server.MaxHeaderBytes,
|
|
}
|
|
|
|
lg.Info("server starting",
|
|
"addr", cfg.Server.Addr,
|
|
"read_timeout", cfg.Server.ReadTimeout.String(),
|
|
"write_timeout", cfg.Server.WriteTimeout.String(),
|
|
"idle_timeout", cfg.Server.IdleTimeout.String(),
|
|
"max_header_bytes", cfg.Server.MaxHeaderBytes,
|
|
)
|
|
|
|
go func() {
|
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
lg.Error("server failed", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
<-quit
|
|
|
|
lg.Info("server shutting down")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := server.Shutdown(ctx); err != nil {
|
|
lg.Error("server forced shutdown", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|