-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
40 lines (35 loc) · 800 Bytes
/
util.go
File metadata and controls
40 lines (35 loc) · 800 Bytes
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
package main
import (
"fmt"
"strings"
"time"
)
type RetriableError struct {
Err error
RetryAfter time.Duration
}
// Error returns error message and a Retry-After duration.
func (e *RetriableError) Error() string {
return fmt.Sprintf("%s (retry after %v)", e.Err.Error(), e.RetryAfter)
}
// Fixes AI output bullshit
func SanitizeJSONString(s string) string {
replacer := strings.NewReplacer(
"“", "\"",
"”", "\"",
"‘", "'",
"’", "'",
)
return replacer.Replace(s)
}
// Returns just the last 20 characters with whitespace characters removed
// from both ends.
func Last20Characters(s string) string {
// Remove newlines and trim leading spaces
s = strings.ReplaceAll(s, "\n", " ")
s = strings.TrimSpace(s)
if len(s) <= 20 {
return s
}
return s[len(s)-20:]
}