2026-01-16 18:03:17 -08:00
|
|
|
package agentfiles
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
|
|
|
|
|
|
"cdr.dev/slog/v3"
|
2026-03-06 10:47:55 +01:00
|
|
|
"github.com/coder/coder/v2/agent/agentgit"
|
2026-01-16 18:03:17 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// API exposes file-related operations performed through the agent.
|
|
|
|
|
type API struct {
|
|
|
|
|
logger slog.Logger
|
|
|
|
|
filesystem afero.Fs
|
2026-03-06 10:47:55 +01:00
|
|
|
pathStore *agentgit.PathStore
|
2026-01-16 18:03:17 -08:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:47:55 +01:00
|
|
|
func NewAPI(logger slog.Logger, filesystem afero.Fs, pathStore *agentgit.PathStore) *API {
|
2026-01-16 18:03:17 -08:00
|
|
|
api := &API{
|
|
|
|
|
logger: logger,
|
|
|
|
|
filesystem: filesystem,
|
2026-03-06 10:47:55 +01:00
|
|
|
pathStore: pathStore,
|
2026-01-16 18:03:17 -08:00
|
|
|
}
|
|
|
|
|
return api
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Routes returns the HTTP handler for file-related routes.
|
|
|
|
|
func (api *API) Routes() http.Handler {
|
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
|
|
r.Post("/list-directory", api.HandleLS)
|
2026-04-16 11:12:01 +02:00
|
|
|
r.Get("/resolve-path", api.HandleResolvePath)
|
2026-01-16 18:03:17 -08:00
|
|
|
r.Get("/read-file", api.HandleReadFile)
|
2026-02-27 15:12:56 -05:00
|
|
|
r.Get("/read-file-lines", api.HandleReadFileLines)
|
2026-01-16 18:03:17 -08:00
|
|
|
r.Post("/write-file", api.HandleWriteFile)
|
|
|
|
|
r.Post("/edit-files", api.HandleEditFiles)
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
}
|