mirror of
https://github.com/coder/coder.git
synced 2026-06-04 13:38:21 +00:00
ef6969dd70
Agents can already see workspace files and take screenshots, but users could not download those artifacts from chat. This PR adds durable chat attachments to chatd. `attach_file`, explicit `computer` screenshot actions (not the automatic post-action screenshots), and `propose_plan` now fetch bytes over the agent connection, store them in `chat_files`, link them to the chat, and carry attachment metadata in tool responses so `buildAssistantPartsForPersist` can materialize ordinary `type:"file"` assistant parts that the chat file APIs serve. The same storage helpers are reused for other artifact-producing paths. `wait_agent` recordings and thumbnails are stored as chat files and linked back to the parent chat, with best-effort relinking so parent chats retain those artifacts without leaving orphaned rows when chat-file caps reject links. `storeChatAttachment` wraps insert + link in one transaction, files are capped at 10 MB each and 20 per chat, and serving defaults to `Content-Disposition: attachment` with an explicit inline-safe allowlist. This PR also consolidates chat-file media policy in `coderd/chatfiles`. Uploads and tool-generated attachments share byte-based MIME detection, SVG blocking, inline-safety rules, and compatible `text/plain` refinement for JSON, CSV, and Markdown. Prompt construction still only inlines synthetic pasted text for model consumption; assistant-created attachments are persisted for the user and intentionally not replayed into later LLM turns. UI follow-up lives in #24281. Relates to CODAGT-91
128 lines
4.2 KiB
Go
128 lines
4.2 KiB
Go
package chattool
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"charm.land/fantasy"
|
|
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
)
|
|
|
|
const maxProposePlanSize = 32 * 1024 // 32 KiB
|
|
|
|
// ProposePlanOptions configures the propose_plan tool.
|
|
type ProposePlanOptions struct {
|
|
GetWorkspaceConn func(context.Context) (workspacesdk.AgentConn, error)
|
|
ResolvePlanPath func(context.Context) (chatPath string, home string, err error)
|
|
StoreFile StoreFileFunc
|
|
IsPlanTurn bool
|
|
}
|
|
|
|
// ProposePlanArgs are the arguments for the propose_plan tool.
|
|
type ProposePlanArgs struct {
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
// ProposePlan returns a tool that presents a Markdown plan file from the
|
|
// workspace for user review.
|
|
func ProposePlan(options ProposePlanOptions) fantasy.AgentTool {
|
|
return fantasy.NewAgentTool(
|
|
"propose_plan",
|
|
"Present a Markdown plan file from the workspace for user review. "+
|
|
"The file must already exist with a .md extension. Use write_file to create it or edit_files to refine it before calling this tool. "+
|
|
"Pass the absolute file path to the plan. Important: use the chat-specific absolute plan path, not a generic path like PLAN.md in the home directory. "+
|
|
"The tool reads the content from the workspace.",
|
|
func(ctx context.Context, args ProposePlanArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
|
|
if options.IsPlanTurn {
|
|
planPath, err := resolvePlanTurnPath(ctx, options.ResolvePlanPath)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
path := strings.TrimSpace(args.Path)
|
|
switch {
|
|
case path == "":
|
|
args.Path = planPath
|
|
case path != planPath:
|
|
return fantasy.NewTextErrorResponse("during plan turns, propose_plan path must be " + planPath), nil
|
|
default:
|
|
args.Path = path
|
|
}
|
|
}
|
|
if options.GetWorkspaceConn == nil {
|
|
return fantasy.NewTextErrorResponse("workspace connection resolver is not configured"), nil
|
|
}
|
|
if options.StoreFile == nil {
|
|
return fantasy.NewTextErrorResponse("file storage is not configured"), nil
|
|
}
|
|
conn, err := options.GetWorkspaceConn(ctx)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
return executeProposePlanTool(ctx, conn, args, options.ResolvePlanPath, options.StoreFile)
|
|
},
|
|
)
|
|
}
|
|
|
|
func executeProposePlanTool(
|
|
ctx context.Context,
|
|
conn workspacesdk.AgentConn,
|
|
args ProposePlanArgs,
|
|
resolvePlanPath func(context.Context) (chatPath string, home string, err error),
|
|
storeFile StoreFileFunc,
|
|
) (fantasy.ToolResponse, error) {
|
|
requestedPath := strings.TrimSpace(args.Path)
|
|
if requestedPath == "" {
|
|
return fantasy.NewTextErrorResponse("path is required (use the chat-specific absolute plan path)"), nil
|
|
}
|
|
if !strings.HasSuffix(requestedPath, ".md") {
|
|
return fantasy.NewTextErrorResponse("path must end with .md"), nil
|
|
}
|
|
|
|
hasPlanFileName := looksLikePlanFileName(requestedPath)
|
|
if hasPlanFileName && !isAbsolutePath(requestedPath) {
|
|
return fantasy.NewTextErrorResponse(
|
|
"plan files must use absolute paths; use the chat-specific absolute plan path",
|
|
), nil
|
|
}
|
|
|
|
if resolvePlanPath != nil && hasPlanFileName {
|
|
chatPath, home, err := resolvePlanPath(ctx)
|
|
if resp, rejected := rejectSharedPlanPath(requestedPath, home, chatPath, err); rejected {
|
|
return resp, nil
|
|
}
|
|
}
|
|
|
|
rc, _, err := conn.ReadFile(ctx, requestedPath, 0, maxProposePlanSize+1)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
defer rc.Close()
|
|
|
|
data, err := io.ReadAll(rc)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
if len(data) == 0 || strings.TrimSpace(string(data)) == "" {
|
|
return fantasy.NewTextErrorResponse("plan file is empty; write your plan to " + requestedPath + " before proposing"), nil
|
|
}
|
|
if int64(len(data)) > maxProposePlanSize {
|
|
return fantasy.NewTextErrorResponse("plan file exceeds 32 KiB size limit"), nil
|
|
}
|
|
|
|
attachment, err := storeFile(ctx, filepath.Base(requestedPath), requestedPath, data)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse("failed to store plan file: " + err.Error()), nil
|
|
}
|
|
|
|
return WithAttachments(toolResponse(map[string]any{
|
|
"ok": true,
|
|
"path": requestedPath,
|
|
"kind": "plan",
|
|
"file_id": attachment.FileID.String(),
|
|
"media_type": attachment.MediaType,
|
|
}), attachment), nil
|
|
}
|