mirror of
https://github.com/coder/coder.git
synced 2026-11-14 20:31:53 +08:00
Follows similarly to the bash tool (and some code to connect to an agent was extracted from it). There are two main parts: a new agent endpoint, and then a new MCP tool that consumes that endpoint.
97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"mime"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"cdr.dev/slog"
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
func (a *agent) HandleReadFile(rw http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
query := r.URL.Query()
|
|
parser := httpapi.NewQueryParamParser().RequiredNotEmpty("path")
|
|
path := parser.String(query, "", "path")
|
|
offset := parser.PositiveInt64(query, 0, "offset")
|
|
limit := parser.PositiveInt64(query, 0, "limit")
|
|
parser.ErrorExcessParams(query)
|
|
if len(parser.Errors) > 0 {
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
|
Message: "Query parameters have invalid values.",
|
|
Validations: parser.Errors,
|
|
})
|
|
return
|
|
}
|
|
|
|
status, err := a.streamFile(ctx, rw, path, offset, limit)
|
|
if err != nil {
|
|
httpapi.Write(ctx, rw, status, codersdk.Response{
|
|
Message: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
}
|
|
|
|
func (a *agent) streamFile(ctx context.Context, rw http.ResponseWriter, path string, offset, limit int64) (int, error) {
|
|
if !filepath.IsAbs(path) {
|
|
return http.StatusBadRequest, xerrors.Errorf("file path must be absolute: %q", path)
|
|
}
|
|
|
|
f, err := a.filesystem.Open(path)
|
|
if err != nil {
|
|
status := http.StatusInternalServerError
|
|
switch {
|
|
case errors.Is(err, os.ErrNotExist):
|
|
status = http.StatusNotFound
|
|
case errors.Is(err, os.ErrPermission):
|
|
status = http.StatusForbidden
|
|
}
|
|
return status, err
|
|
}
|
|
defer f.Close()
|
|
|
|
stat, err := f.Stat()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
if stat.IsDir() {
|
|
return http.StatusBadRequest, xerrors.Errorf("open %s: not a file", path)
|
|
}
|
|
|
|
size := stat.Size()
|
|
if limit == 0 {
|
|
limit = size
|
|
}
|
|
bytesRemaining := max(size-offset, 0)
|
|
bytesToRead := min(bytesRemaining, limit)
|
|
|
|
// Relying on just the file name for the mime type for now.
|
|
mimeType := mime.TypeByExtension(filepath.Ext(path))
|
|
if mimeType == "" {
|
|
mimeType = "application/octet-stream"
|
|
}
|
|
rw.Header().Set("Content-Type", mimeType)
|
|
rw.Header().Set("Content-Length", strconv.FormatInt(bytesToRead, 10))
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
reader := io.NewSectionReader(f, offset, bytesToRead)
|
|
_, err = io.Copy(rw, reader)
|
|
if err != nil && !errors.Is(err, io.EOF) && ctx.Err() == nil {
|
|
a.logger.Error(ctx, "workspace agent read file", slog.Error(err))
|
|
}
|
|
|
|
return 0, nil
|
|
}
|