91 lines
3.4 KiB
Go
91 lines
3.4 KiB
Go
package codex
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"image"
|
|
_ "image/gif"
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
"io"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
)
|
|
|
|
const maxPromptFileBytes = 1 << 20
|
|
|
|
// Checked against rust-v0.163.0's v2/UserInput.ts: localImage takes a
|
|
// filesystem path; mention is only a reference, not loaded file contents.
|
|
func promptInput(text string, files []opencode.PromptFile) ([]any, error) {
|
|
if strings.TrimSpace(text) == "" && len(files) == 0 {
|
|
return nil, errors.New("empty chat prompt")
|
|
}
|
|
if len(files) > 8 {
|
|
return nil, errors.New("at most 8 attachments are allowed")
|
|
}
|
|
input := []any{}
|
|
if text != "" {
|
|
input = append(input, textInput(text))
|
|
}
|
|
var total int64
|
|
for _, file := range files {
|
|
u, err := url.Parse(file.URI)
|
|
if err != nil || !file.ServerPrepared() || u.Scheme != "file" || u.Host != "" || u.User != nil || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || u.Opaque != "" || !filepath.IsAbs(u.Path) || filepath.Clean(u.Path) != u.Path {
|
|
return nil, errors.New("attachment must be an unchanged server-prepared local file URI")
|
|
}
|
|
info, err := os.Lstat(u.Path)
|
|
if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0o077 != 0 {
|
|
return nil, errors.New("attachment must be a private regular file")
|
|
}
|
|
if info.Size() <= 0 || info.Size() > maxPromptFileBytes-total {
|
|
return nil, errors.New("attachments exceed the 1 MiB limit or are empty")
|
|
}
|
|
f, err := os.Open(u.Path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("open attachment: %w", err)
|
|
}
|
|
actual, statErr := f.Stat()
|
|
data, readErr := io.ReadAll(io.LimitReader(f, maxPromptFileBytes-total+1))
|
|
closeErr := f.Close()
|
|
if statErr != nil || !os.SameFile(info, actual) || readErr != nil || closeErr != nil || int64(len(data)) != info.Size() {
|
|
return nil, errors.New("attachment changed or could not be read")
|
|
}
|
|
total += int64(len(data))
|
|
name := file.Name
|
|
if name == "" {
|
|
name = filepath.Base(u.Path)
|
|
}
|
|
// Verify bytes rather than trusting the browser MIME or filename. Other
|
|
// formats remain available intact through the ordinary file reference.
|
|
if isPromptImage(data) {
|
|
input = append(input, textInput(fmt.Sprintf("Uploaded image: %q", name)), map[string]any{"type": "localImage", "path": u.Path})
|
|
} else {
|
|
// Mention metadata alone need not reach the model (Codex also uses
|
|
// mentions for app/plugin targets). Include the exact server path in
|
|
// the explanatory text so normal file tools can actually inspect it.
|
|
input = append(input, textInput(fmt.Sprintf("Uploaded server-local file reference: name=%q path=%q (contents are not loaded by this reference; inspect the file with your normal tools if needed).", name, u.Path)), map[string]any{"type": "mention", "name": name, "path": u.Path})
|
|
}
|
|
}
|
|
return input, nil
|
|
}
|
|
|
|
func isPromptImage(data []byte) bool {
|
|
config, _, err := image.DecodeConfig(bytes.NewReader(data))
|
|
// Avoid allocating arbitrarily large decompressed images. Unrecognized,
|
|
// corrupt or oversized images still travel intact as local file references.
|
|
if err != nil || config.Width <= 0 || config.Height <= 0 || int64(config.Width) > (16<<20)/int64(config.Height) {
|
|
return false
|
|
}
|
|
_, _, err = image.Decode(bytes.NewReader(data))
|
|
return err == nil
|
|
}
|
|
|
|
func textInput(text string) map[string]any {
|
|
return map[string]any{"type": "text", "text": text, "text_elements": []any{}}
|
|
}
|