-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
95 lines (81 loc) · 2.07 KB
/
input.go
File metadata and controls
95 lines (81 loc) · 2.07 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
package codexsdk
import (
"encoding/base64"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
)
// Text creates text-only user content.
func Text(text string) UserMessageContent {
return NewUserMessageContent(text)
}
// TextInput creates a text block for block-based user content.
func TextInput(text string) *TextBlock {
return &TextBlock{
Type: BlockTypeText,
Text: text,
}
}
// Blocks creates block-based user content.
func Blocks(blocks ...ContentBlock) UserMessageContent {
return NewUserMessageContentBlocks(blocks)
}
// ImageInput creates an app-server image block from a URL or data URL.
func ImageInput(url string) *InputImageBlock {
url = strings.TrimSpace(url)
return &InputImageBlock{
Type: BlockTypeImage,
URL: url,
}
}
// PathInput creates a generic local path mention block.
func PathInput(path string) *InputMentionBlock {
return &InputMentionBlock{
Type: BlockTypeMention,
Name: filepath.Base(path),
Path: path,
}
}
// LocalImageInput creates a local image-path input block.
func LocalImageInput(path string) *InputLocalImageBlock {
return &InputLocalImageBlock{
Type: BlockTypeLocalImage,
Path: path,
}
}
// ImageFileInput reads a local image file and creates an inline data-URL image block.
func ImageFileInput(path string) (*InputImageBlock, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read image file: %w", err)
}
mediaType := detectImageMediaType(path, data)
if !strings.HasPrefix(mediaType, "image/") {
return nil, fmt.Errorf("unsupported image media type %q", mediaType)
}
return ImageInput(
fmt.Sprintf("data:%s;base64,%s", mediaType, base64.StdEncoding.EncodeToString(data)),
), nil
}
func detectImageMediaType(path string, data []byte) string {
switch strings.ToLower(filepath.Ext(path)) {
case ".png":
return "image/png"
case ".jpg", ".jpeg":
return "image/jpeg"
case ".gif":
return "image/gif"
case ".webp":
return "image/webp"
default:
return http.DetectContentType(data)
}
}
func formatPathMention(path string) string {
if path == "" {
return "@"
}
return "@" + path
}