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-07-16 13:00:32 +03:00
|
|
|
"github.com/coder/coder/v2/agent/usershell"
|
|
|
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
2026-01-16 18:03:17 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// API exposes file-related operations performed through the agent.
|
|
|
|
|
type API struct {
|
2026-07-16 13:00:32 +03:00
|
|
|
logger slog.Logger
|
|
|
|
|
filesystem afero.Fs
|
|
|
|
|
pathStore *agentgit.PathStore
|
|
|
|
|
envInfo usershell.EnvInfoer
|
|
|
|
|
bundleFilesLimits workspacesdk.BundleFilesLimits
|
2026-01-16 18:03:17 -08:00
|
|
|
}
|
|
|
|
|
|
2026-07-16 13:00:32 +03:00
|
|
|
// Option configures the API.
|
|
|
|
|
type Option func(*API)
|
|
|
|
|
|
|
|
|
|
// WithBundleFilesLimits overrides the bundle files collection limits.
|
|
|
|
|
func WithBundleFilesLimits(limits workspacesdk.BundleFilesLimits) Option {
|
|
|
|
|
return func(api *API) {
|
|
|
|
|
api.bundleFilesLimits = limits
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithEnvInfo overrides how the agent user's home directory is resolved.
|
|
|
|
|
func WithEnvInfo(envInfo usershell.EnvInfoer) Option {
|
|
|
|
|
return func(api *API) {
|
|
|
|
|
api.envInfo = envInfo
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewAPI(logger slog.Logger, filesystem afero.Fs, pathStore *agentgit.PathStore, opts ...Option) *API {
|
2026-01-16 18:03:17 -08:00
|
|
|
api := &API{
|
2026-07-16 13:00:32 +03:00
|
|
|
logger: logger,
|
|
|
|
|
filesystem: filesystem,
|
|
|
|
|
pathStore: pathStore,
|
|
|
|
|
envInfo: usershell.SystemEnvInfo{},
|
|
|
|
|
bundleFilesLimits: defaultBundleFilesLimits,
|
|
|
|
|
}
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
opt(api)
|
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)
|
2026-07-16 13:00:32 +03:00
|
|
|
r.Post("/bundle-files", api.HandleBundleFiles)
|
2026-01-16 18:03:17 -08:00
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
}
|