-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocuments.go
More file actions
116 lines (101 loc) · 3.94 KB
/
documents.go
File metadata and controls
116 lines (101 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ai
import (
"encoding/base64"
"fmt"
"os"
"path/filepath"
"strings"
)
// ═══════════════════════════════════════════════════════════════════════════
// Document / PDF Input
// ═══════════════════════════════════════════════════════════════════════════
// DocumentInput represents a document (PDF) included in a request.
type DocumentInput struct {
Data string // base64-encoded data
URL string // or URL (mutually exclusive with Data)
MimeType string // "application/pdf"
Name string // optional filename for context
}
// ═══════════════════════════════════════════════════════════════════════════
// Builder Methods for Documents
// ═══════════════════════════════════════════════════════════════════════════
// PDF adds a PDF document from a local file path.
func (b *Builder) PDF(path string) *Builder {
data, err := os.ReadFile(path)
if err != nil {
fmt.Printf("%s Error loading PDF %s: %v\n", colorRed("✗"), path, err)
return b
}
encoded := base64.StdEncoding.EncodeToString(data)
b.documents = append(b.documents, DocumentInput{
Data: encoded,
MimeType: detectDocMimeType(path),
Name: filepath.Base(path),
})
return b
}
// PDFURL adds a PDF document from a URL.
func (b *Builder) PDFURL(url string) *Builder {
b.documents = append(b.documents, DocumentInput{
URL: url,
MimeType: "application/pdf",
})
return b
}
// PDFBase64 adds a PDF document from base64-encoded data.
func (b *Builder) PDFBase64(data string) *Builder {
b.documents = append(b.documents, DocumentInput{
Data: data,
MimeType: "application/pdf",
})
return b
}
// PDFs adds multiple PDF documents at once.
func (b *Builder) PDFs(paths ...string) *Builder {
for _, path := range paths {
b.PDF(path)
}
return b
}
// Document adds a document with auto-detected type (PDF, etc.).
func (b *Builder) Document(path string) *Builder {
ext := strings.ToLower(filepath.Ext(path))
switch ext {
case ".pdf":
return b.PDF(path)
default:
fmt.Printf("%s Unsupported document type: %s (only PDF supported)\n", colorYellow("⚠"), ext)
return b
}
}
// DocumentURL adds a document from URL with auto-detected type.
func (b *Builder) DocumentURL(url string) *Builder {
// Check URL extension
lower := strings.ToLower(url)
if strings.HasSuffix(lower, ".pdf") || strings.Contains(lower, ".pdf?") {
return b.PDFURL(url)
}
// Default to PDF
return b.PDFURL(url)
}
// ═══════════════════════════════════════════════════════════════════════════
// Internal Helpers
// ═══════════════════════════════════════════════════════════════════════════
// detectDocMimeType returns the MIME type based on file extension.
func detectDocMimeType(path string) string {
ext := strings.ToLower(filepath.Ext(path))
switch ext {
case ".pdf":
return "application/pdf"
default:
return "application/pdf" // fallback
}
}
// HasDocuments reports whether the builder has documents attached.
func (b *Builder) HasDocuments() bool {
return len(b.documents) > 0
}
// GetDocuments returns the attached documents.
func (b *Builder) GetDocuments() []DocumentInput {
return b.documents
}