feat(chat): files upload over HTTP before the prompt, up to 100 MiB each
This commit is contained in:
@@ -18,7 +18,7 @@ import (
|
||||
// Files survive process restarts and asynchronous harness inbox admission.
|
||||
// Only unleased files older than seven days are collected, on the next upload.
|
||||
const chatAttachmentRetention = 7 * 24 * time.Hour
|
||||
const chatAttachmentQuota = 64 << 20
|
||||
const chatAttachmentQuota = 1 << 30
|
||||
const chatAttachmentFileQuota = 512
|
||||
|
||||
var chatAttachmentStore = struct {
|
||||
@@ -59,69 +59,54 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
|
||||
if len(attachments) > maxChatAttachments {
|
||||
return nil, nil, fmt.Errorf("at most %d attachments are allowed", maxChatAttachments)
|
||||
}
|
||||
dir := os.Getenv("PIPELINES_CHAT_ATTACHMENT_DIR")
|
||||
if dir == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
dir = filepath.Join(home, ".pipelines-chat-attachments")
|
||||
}
|
||||
if !filepath.IsAbs(dir) {
|
||||
return nil, nil, fmt.Errorf("chat attachment storage must be an absolute path")
|
||||
}
|
||||
dir = filepath.Clean(dir)
|
||||
chatAttachmentStore.Lock()
|
||||
defer chatAttachmentStore.Unlock()
|
||||
if err := os.Mkdir(dir, 0o700); err != nil && !os.IsExist(err) {
|
||||
return nil, nil, fmt.Errorf("prepare attachment directory: %w", err)
|
||||
}
|
||||
info, err := os.Lstat(dir)
|
||||
if err != nil || !info.IsDir() || info.Mode().Perm()&0o077 != 0 {
|
||||
return nil, nil, fmt.Errorf("attachment storage must be a private directory, not a symlink")
|
||||
}
|
||||
root, err := os.OpenRoot(dir)
|
||||
root, dir, stored, storedFiles, err := openChatAttachmentStore()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer root.Close()
|
||||
actual, err := root.Stat(".")
|
||||
if err != nil || !os.SameFile(info, actual) {
|
||||
return nil, nil, fmt.Errorf("chat attachment storage changed while opening")
|
||||
}
|
||||
entries, err := fs.ReadDir(root.FS(), ".")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var stored int64
|
||||
var storedFiles int
|
||||
for _, entry := range entries {
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
continue
|
||||
}
|
||||
if !chatAttachmentStore.leased[filepath.Join(dir, entry.Name())] && time.Since(info.ModTime()) > chatAttachmentRetention {
|
||||
if err := root.Remove(entry.Name()); err == nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
stored += info.Size()
|
||||
storedFiles++
|
||||
}
|
||||
var files []opencode.PromptFile
|
||||
var parts []protocol.ChatPart
|
||||
var created []string
|
||||
var created, referenced []string
|
||||
cleanup := func() {
|
||||
for _, path := range created {
|
||||
_ = root.Remove(filepath.Base(path))
|
||||
delete(chatAttachmentStore.leased, path)
|
||||
}
|
||||
for _, path := range referenced {
|
||||
delete(chatAttachmentStore.leased, path)
|
||||
}
|
||||
}
|
||||
total := 0
|
||||
for index, attachment := range attachments {
|
||||
if attachment.Upload != "" {
|
||||
// A file sent earlier through POST /chat-uploads: only its stored
|
||||
// name crosses the websocket.
|
||||
upload := attachment.Upload
|
||||
info, err := root.Lstat(upload)
|
||||
if !chatUploadName.MatchString(upload) || err != nil || !info.Mode().IsRegular() {
|
||||
cleanup()
|
||||
return nil, nil, fmt.Errorf("attachment %q was not uploaded or has expired", attachment.Name)
|
||||
}
|
||||
path := filepath.Join(dir, upload)
|
||||
if !chatAttachmentStore.leased[path] {
|
||||
referenced = append(referenced, path)
|
||||
}
|
||||
chatAttachmentStore.leased[path] = true
|
||||
name := safeChatAttachmentName(attachment.Name)
|
||||
if strings.TrimSpace(attachment.Name) == "" {
|
||||
name = safeChatAttachmentName(strings.SplitN(upload, "-", 3)[2])
|
||||
}
|
||||
mime := strings.TrimSpace(attachment.Mime)
|
||||
if mime == "" {
|
||||
mime = "application/octet-stream"
|
||||
}
|
||||
uri := (&url.URL{Scheme: "file", Path: path}).String()
|
||||
files = append(files, opencode.NewPreparedPromptFile(uri, name))
|
||||
parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: name, Mime: mime, URI: uri})
|
||||
continue
|
||||
}
|
||||
data := strings.TrimSpace(attachment.Data)
|
||||
if len(data) > base64.StdEncoding.EncodedLen(maxChatAttachmentBytes) {
|
||||
cleanup()
|
||||
@@ -204,3 +189,62 @@ func safeChatAttachmentName(value string) string {
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// openChatAttachmentStore opens the private attachment directory, collects
|
||||
// unleased files past retention and reports what the rest occupy. The caller
|
||||
// holds chatAttachmentStore and closes the root.
|
||||
func openChatAttachmentStore() (*os.Root, string, int64, int, error) {
|
||||
dir := os.Getenv("PIPELINES_CHAT_ATTACHMENT_DIR")
|
||||
if dir == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, "", 0, 0, err
|
||||
}
|
||||
dir = filepath.Join(home, ".pipelines-chat-attachments")
|
||||
}
|
||||
if !filepath.IsAbs(dir) {
|
||||
return nil, "", 0, 0, fmt.Errorf("chat attachment storage must be an absolute path")
|
||||
}
|
||||
dir = filepath.Clean(dir)
|
||||
if err := os.Mkdir(dir, 0o700); err != nil && !os.IsExist(err) {
|
||||
return nil, "", 0, 0, fmt.Errorf("prepare attachment directory: %w", err)
|
||||
}
|
||||
info, err := os.Lstat(dir)
|
||||
if err != nil || !info.IsDir() || info.Mode().Perm()&0o077 != 0 {
|
||||
return nil, "", 0, 0, fmt.Errorf("attachment storage must be a private directory, not a symlink")
|
||||
}
|
||||
root, err := os.OpenRoot(dir)
|
||||
if err != nil {
|
||||
return nil, "", 0, 0, err
|
||||
}
|
||||
actual, err := root.Stat(".")
|
||||
if err != nil || !os.SameFile(info, actual) {
|
||||
root.Close()
|
||||
return nil, "", 0, 0, fmt.Errorf("chat attachment storage changed while opening")
|
||||
}
|
||||
entries, err := fs.ReadDir(root.FS(), ".")
|
||||
if err != nil {
|
||||
root.Close()
|
||||
return nil, "", 0, 0, err
|
||||
}
|
||||
var stored int64
|
||||
var storedFiles int
|
||||
for _, entry := range entries {
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
root.Close()
|
||||
return nil, "", 0, 0, err
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
continue
|
||||
}
|
||||
if !chatAttachmentStore.leased[filepath.Join(dir, entry.Name())] && time.Since(info.ModTime()) > chatAttachmentRetention {
|
||||
if err := root.Remove(entry.Name()); err == nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
stored += info.Size()
|
||||
storedFiles++
|
||||
}
|
||||
return root, dir, stored, storedFiles, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user