-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprompt_loader.go
More file actions
145 lines (123 loc) · 3.54 KB
/
prompt_loader.go
File metadata and controls
145 lines (123 loc) · 3.54 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
)
// LoadContent intelligently loads content from text, file, or URL without template rendering
// It detects the input type automatically:
// - If starts with http:// or https:// -> loads from URL
// - If starts with file:// or is a valid file path -> loads from file
// - Otherwise -> returns as plain text
func LoadContent(input string) (string, error) {
if input == "" {
return "", nil
}
// Determine source type and load content
switch {
case isURL(input):
return loadFromURL(input)
case isFilePath(input):
return loadFromFile(input)
default:
// Return as plain text
return input, nil
}
}
// LoadPrompt intelligently loads prompt content from text, file, or URL
// It detects the input type automatically:
// - If starts with http:// or https:// -> loads from URL
// - If starts with file:// or is a valid file path -> loads from file
// - Otherwise -> returns as plain text
// After loading, it renders the content as a Go template with environment variables
func LoadPrompt(input string) (string, error) {
if input == "" {
return "", nil
}
var content string
var err error
// Determine source type and load content
switch {
case isURL(input):
content, err = loadFromURL(input)
if err != nil {
return "", err
}
case isFilePath(input):
content, err = loadFromFile(input)
if err != nil {
return "", err
}
default:
// Return as plain text
content = input
}
// Render template with environment variables
rendered, err := RenderTemplate(content)
if err != nil {
return "", fmt.Errorf("failed to render template: %w", err)
}
return rendered, nil
}
// isURL checks if the input string is a URL
func isURL(input string) bool {
return strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")
}
// isFilePath checks if the input string is a file path
func isFilePath(input string) bool {
// Remove file:// prefix if present
path := strings.TrimPrefix(input, "file://")
// Check if file exists
if _, err := os.Stat(path); err == nil {
return true
}
// If it starts with file://, treat it as a file path even if it doesn't exist
// This will allow proper error reporting
return strings.HasPrefix(input, "file://")
}
// loadFromURL loads content from a URL
func loadFromURL(url string) (string, error) {
// Create HTTP client with timeout
client := &http.Client{
Timeout: 30 * time.Second,
}
// Create request with context
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return "", fmt.Errorf("failed to create request for URL %s: %w", url, err)
}
// Add User-Agent header
req.Header.Set("User-Agent", "LLM-Action/1.0")
// Send request
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("failed to fetch URL %s: %w", url, err)
}
defer resp.Body.Close()
// Check status code
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to fetch URL %s: status code %d", url, resp.StatusCode)
}
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response from URL %s: %w", url, err)
}
return string(body), nil
}
// loadFromFile loads content from a local file
func loadFromFile(path string) (string, error) {
// Remove file:// prefix if present
cleanPath := strings.TrimPrefix(path, "file://")
// Read file
content, err := os.ReadFile(cleanPath)
if err != nil {
return "", fmt.Errorf("failed to read file %s: %w", cleanPath, err)
}
return string(content), nil
}