-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (178 loc) · 4.23 KB
/
main.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/deanishe/awgo"
"github.com/spf13/pflag"
)
type Config struct {
PreComputedRowsJSON string
MessagePreviewLen int
AlfredSubtitleMaxLength int
PreComputedAlfredJSON string
AlfredWorkflowCacheKey string
}
type Row struct {
SearchKey string `json:"_search_key"`
ConcatenatedMessages string `json:"concatenated_messages"`
MessagePreview string `json:"_message_preview"`
Title string `json:"_title"`
QuicklookURL string `json:"_quicklookurl"`
ChatGPTURL string `json:"_chatgpt_url"`
TypingMindURL string `json:"_typingmind_url"`
Item3Kwargs map[string]interface{} `json:"_item3_kwargs"`
}
var (
wf *aw.Workflow
config Config
)
func loadConfig() Config {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
config := Config{
PreComputedRowsJSON: filepath.Join(dir, "generated/pre_computed_rows.json"),
MessagePreviewLen: 100,
AlfredSubtitleMaxLength: 108,
PreComputedAlfredJSON: filepath.Join(dir, "generated/pre_computed_alfred.json"),
AlfredWorkflowCacheKey: "chatgpt-alfred-workflow",
}
return config
}
func loadRows() []Row {
file, err := ioutil.ReadFile(config.PreComputedRowsJSON)
if err != nil {
panic(err)
}
var rows []Row
if err := json.Unmarshal(file, &rows); err != nil {
panic(err)
}
return rows
}
func filterQuery(rows []Row, query string) []Row {
newRows := []Row{}
for _, row := range rows {
longStr := row.SearchKey
match := true
for _, subquery := range strings.Split(strings.ToLower(query), "|") {
if strings.Contains(subquery, "=") {
parts := strings.SplitN(subquery, "=", 2)
k, v := parts[0], parts[1]
if val, ok := row.Item3Kwargs[k]; ok {
if !strings.Contains(strings.ToLower(fmt.Sprintf("%v", val)), strings.ToLower(v)) {
match = false
break
}
} else {
match = false
break
}
} else {
if !fuzzyMatch(subquery, longStr) {
match = false
break
}
}
}
if match {
newRows = append(newRows, row)
}
}
return newRows
}
func fuzzyMatch(query, target string) bool {
query = strings.ToLower(query)
target = strings.ToLower(target)
queryIndex := 0
for i := 0; i < len(target) && queryIndex < len(query); i++ {
if query[queryIndex] == target[i] {
queryIndex++
}
}
return queryIndex == len(query)
}
func prepareWfItems(query string, rows []Row) {
for _, row := range rows {
messagePreview := row.MessagePreview
if query != "" {
messagePreview = searchAndExtractPreview(query, row.ConcatenatedMessages, config.MessagePreviewLen, false)
}
item := wf.NewItem(row.Title).
Subtitle(messagePreview).
Arg(row.ChatGPTURL).
Valid(true)
item.NewModifier("cmd").
Subtitle("Open on TypingMind").
Arg(row.TypingMindURL).
Valid(true)
}
}
func searchAndExtractPreview(query, message string, returnLen int, caseSensitive bool) string {
if !caseSensitive {
query = strings.ToLower(query)
message = strings.ToLower(message)
}
if strings.Contains(message, query) {
queryIndex := strings.Index(message, query)
startIndex := max(0, queryIndex-returnLen/2)
endIndex := min(len(message), queryIndex+len(query)+returnLen/2)
for endIndex-startIndex < returnLen {
if startIndex > 0 {
startIndex--
} else if endIndex < len(message) {
endIndex++
} else {
break
}
}
return message[startIndex:endIndex]
}
return ""
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func run() {
var generateAlfredJSON bool
pflag.BoolVarP(&generateAlfredJSON, "generate-alfred-json", "g", false, "Generate Alfred JSON")
pflag.Parse()
config = loadConfig()
rows := loadRows()
if generateAlfredJSON {
prepareWfItems("", rows)
wf.SendFeedback()
} else {
query := ""
if len(wf.Args()) > 0 {
query = wf.Args()[0]
}
if query != "" {
rows = filterQuery(rows, query)
}
if len(rows) == 0 {
wf.NewItem("No matching results found")
} else {
prepareWfItems(query, rows)
}
wf.SendFeedback()
}
}
func main() {
wf = aw.New()
wf.Run(run)
}