Files
coder/coderd/x/chatd/chattool/attachfile.go
T
Ethan ef6969dd70 feat(coderd/x/chatd): agent-created file attachments in chat (#24280)
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
2026-04-20 18:04:35 +10:00

79 lines
2.3 KiB
Go

package chattool
import (
"context"
"strings"
"charm.land/fantasy"
"github.com/coder/coder/v2/codersdk/workspacesdk"
)
// AttachFileOptions configures the attach_file tool.
type AttachFileOptions struct {
GetWorkspaceConn func(context.Context) (workspacesdk.AgentConn, error)
StoreFile StoreFileFunc
}
// AttachFileArgs are the arguments for the attach_file tool.
type AttachFileArgs struct {
Path string `json:"path"`
Name string `json:"name,omitempty"`
}
// AttachFile returns a tool that stores a workspace file as a durable chat
// attachment so the user can download it directly from the conversation.
func AttachFile(options AttachFileOptions) fantasy.AgentTool {
return fantasy.NewAgentTool(
"attach_file",
"Attach a workspace file to the current chat so the user can download it directly from the conversation. "+
"Use this when the user should receive an artifact such as a screenshot, log, patch, or document. "+
"Pass an absolute file path. The file must already exist in the workspace.",
func(ctx context.Context, args AttachFileArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
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 executeAttachFileTool(ctx, conn, args, options.StoreFile)
},
)
}
func executeAttachFileTool(
ctx context.Context,
conn workspacesdk.AgentConn,
args AttachFileArgs,
storeFile StoreFileFunc,
) (fantasy.ToolResponse, error) {
path := strings.TrimSpace(args.Path)
if path == "" {
return fantasy.NewTextErrorResponse("path is required (use an absolute path, e.g. /home/coder/build.log)"), nil
}
attachment, size, err := storeWorkspaceAttachment(
ctx,
conn,
path,
strings.TrimSpace(args.Name),
storeFile,
)
if err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
return WithAttachments(toolResponse(map[string]any{
"ok": true,
"path": path,
"file_id": attachment.FileID.String(),
"name": attachment.Name,
"media_type": attachment.MediaType,
"size": size,
}), attachment), nil
}