package claude import ( "encoding/base64" "errors" "fmt" "mime" "net/url" "os" "path/filepath" "strings" "unicode/utf8" "git.a-wan.net/alan/pipelines/internal/opencode" ) const ( maxInlineAttachment = 256 << 10 maxImageAttachment = 5 << 20 inlineBudget = 512 << 10 ) // promptContent builds Claude's stream-json user content. Images become native // image blocks; small text files are inlined so the agent does not need a tool // permission merely to read what the operator already attached. Anything larger // is named by absolute path, which the CLI can still read with its own tools. func promptContent(text string, files []opencode.PromptFile) ([]any, error) { content := []any{} if strings.TrimSpace(text) != "" { content = append(content, map[string]any{"type": "text", "text": text}) } budget := inlineBudget for _, file := range files { if !file.ServerPrepared() { return nil, errors.New("refusing an attachment that was not prepared by the server") } path, err := promptFilePath(file.URI) if err != nil { return nil, err } info, err := os.Stat(path) if err != nil || !info.Mode().IsRegular() { return nil, fmt.Errorf("attached file %q is no longer available", file.Name) } if info.Size() > maxImageAttachment { content = append(content, map[string]any{"type": "text", "text": fmt.Sprintf("Attached file %s is too large to inline; read it at %s", displayName(file), path)}) continue } data, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("read attached file %q: %w", file.Name, err) } mediaType := mime.TypeByExtension(strings.ToLower(filepath.Ext(file.Name))) if mediaType == "" { mediaType = mime.TypeByExtension(strings.ToLower(filepath.Ext(path))) } if strings.HasPrefix(mediaType, "image/") { content = append(content, map[string]any{ "type": "image", "source": map[string]any{ "type": "base64", "media_type": mediaType, "data": base64.StdEncoding.EncodeToString(data), }, }) continue } if utf8.Valid(data) && len(data) <= maxInlineAttachment && len(data) <= budget { budget -= len(data) content = append(content, map[string]any{ "type": "text", "text": fmt.Sprintf("Attached file: %s\n\n%s", displayName(file), data), }) continue } content = append(content, map[string]any{"type": "text", "text": fmt.Sprintf("Attached file %s is available at %s", displayName(file), path)}) } if len(content) == 0 { return nil, errors.New("a nonempty chat prompt is required") } return content, nil } func displayName(file opencode.PromptFile) string { name := strings.TrimSpace(file.Name) if name == "" { name = filepath.Base(file.URI) } return name } // promptFilePath converts the server's file:// reference to a local path and // refuses anything that is not a plain absolute file URL. func promptFilePath(uri string) (string, error) { parsed, err := url.Parse(uri) if err != nil || parsed.Scheme != "file" || parsed.Host != "" || parsed.Opaque != "" { return "", errors.New("invalid attachment reference") } path, err := url.PathUnescape(parsed.Path) if err != nil { return "", errors.New("invalid attachment reference") } if !filepath.IsAbs(path) { return "", errors.New("attachment reference must be absolute") } return filepath.Clean(path), nil }