-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
AI-Tools.ahk
474 lines (399 loc) · 14.2 KB
/
AI-Tools.ahk
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
; ai-tools-ahk - AutoHotkey scripts for AI tools
; https://github.com/ecornell/ai-tools-ahk
; MIT License
#Requires AutoHotkey v2.0
#singleInstance force
#Include "_jxon.ahk"
#include "_Cursor.ahk"
#Include "_MD2HTML.ahk"
Persistent
SendMode "Input"
;# init setup
if not (FileExist("settings.ini")) {
api_key := InputBox("Enter your OpenAI API key", "AI-Tools-AHK : Setup", "W400 H100").value
if (api_key == "") {
MsgBox("To use this script, you need to enter an OpenAI key. Please restart the script and try again.")
ExitApp
}
FileCopy("settings.ini.default", "settings.ini")
IniWrite(api_key, ".\settings.ini", "settings", "default_api_key")
}
RestoreCursor()
;# globals
_running := false
_settingsCache := Map()
_lastModified := fileGetTime("./settings.ini")
_displayResponse := false
_activeWin := ""
_oldClipboard := ""
_debug := GetSetting("settings", "debug", false)
_reload_on_change := GetSetting("settings", "reload_on_change", false)
;#
CheckSettings()
;# menu
InitPopupMenu()
InitTrayMenu()
;# hotkeys
HotKey GetSetting("settings", "hotkey_1"), (*) => (
SelectText()
PromptHandler(GetSetting("settings", "hotkey_1_prompt")))
HotKey GetSetting("settings", "hotkey_2"), (*) => (
SelectText()
ShowPopupMenu())
HotKey GetSetting("settings", "menu_hotkey"), (*) => (
ShowPopupMenu())
;###
ShowPopupMenu() {
_iMenu.Show()
}
PromptHandler(promptName, append := false) {
try {
if (_running) {
;MsgBox "Already running. Please wait for the current request to finish."
Reload
return
}
global _running := true
global _startTime := A_TickCount
ShowWaitTooltip()
SetSystemCursor(GetSetting("settings", "cursor_wait_file", "wait"))
prompt := GetSetting(promptName, "prompt")
promptEnd := GetSetting(promptName, "prompt_end")
mode := GetSetting(promptName, "mode", GetSetting("settings", "default_mode"))
try {
input := GetTextFromClip()
} catch {
global _running := false
RestoreCursor()
return
}
CallAPI(mode, promptName, prompt, input, promptEnd)
} catch as err {
global _running := false
RestoreCursor()
MsgBox Format("{1}: {2}.`n`nFile:`t{3}`nLine:`t{4}`nWhat:`t{5}", type(err), err.Message, err.File, err.Line, err.What), , 16
;throw err
}
}
;###
SelectText() {
global _oldClipboard := A_Clipboard
A_Clipboard := ""
Send "^c"
ClipWait(2)
text := A_Clipboard
if WinActive("ahk_exe WINWORD.EXE") or WinActive("ahk_exe OUTLOOK.EXE") {
; In Word/Outlook select the current paragraph
Send "^{Up}^+{Down}+{Left}" ; Move to para start, select para, move left to not include para end
} else if WinActive("ahk_exe notepad++.exe") or WinActive("ahk_exe Code.exe") {
; In Notepad++ select the current line
Send "{End}{End}+{Home}+{Home}"
} else {
; Select all text if no text is selected
if StrLen(text) < 1 {
Send "^a"
}
}
sleep 50
}
GetTextFromClip() {
global _activeWin := WinGetTitle("A")
if _oldClipboard == "" {
global _oldClipboard := A_Clipboard
}
A_Clipboard := ""
Send "^c"
ClipWait(2)
text := A_Clipboard
if StrLen(text) < 1 {
throw ValueError("No text selected", -1)
} else if StrLen(text) > 16000 {
throw ValueError("Text is too long", -1)
}
return text
}
ShowWarning(message) {
MsgBox message
}
GetSetting(section, key, defaultValue := "") {
global _settingsCache
if (_settingsCache.Has(section . key . defaultValue)) {
return _settingsCache.Get(section . key . defaultValue)
} else {
value := IniRead(".\settings.ini", section, key, defaultValue)
if IsNumber(value) {
value := Number(value)
} else {
value := UnescapeSetting(value)
}
_settingsCache.Set(section . key . defaultValue, value)
return value
}
}
GetBody(mode, promptName, prompt, input, promptEnd) {
body := Map()
;; load mode defaults
model := GetSetting(mode, "model")
max_tokens := GetSetting(mode, "max_tokens")
temperature := GetSetting(mode, "temperature")
frequency_penalty := GetSetting(mode, "frequency_penalty")
presence_penalty := GetSetting(mode, "presence_penalty")
top_p := GetSetting(mode, "top_p")
best_of := GetSetting(mode, "best_of")
stop := GetSetting(mode, "stop", "")
;; load prompt overrides
model := GetSetting(promptName, "model", model)
max_tokens := GetSetting(promptName, "max_tokens", max_tokens)
temperature := GetSetting(promptName, "temperature", temperature)
frequency_penalty := GetSetting(promptName, "frequency_penalty", frequency_penalty)
presence_penalty := GetSetting(promptName, "presence_penalty", presence_penalty)
top_p := GetSetting(promptName, "top_p", top_p)
best_of := GetSetting(promptName, "best_of", best_of)
stop := GetSetting(promptName, "stop", stop)
;
content := prompt . input . promptEnd
messages := []
prompt_system := GetSetting(promptName, "prompt_system", "")
if (prompt_system != "") {
messages.Push(Map("role", "system", "content", prompt_system))
}
messages.Push(Map("role", "user", "content", content))
body["messages"] := messages
body["max_tokens"] := max_tokens
body["temperature"] := temperature
body["frequency_penalty"] := frequency_penalty
body["presence_penalty"] := presence_penalty
body["top_p"] := top_p
body["model"] := model
return body
}
CallAPI(mode, promptName, prompt, input, promptEnd) {
body := GetBody(mode, promptName, prompt, input, promptEnd)
bodyJson := Jxon_dump(body, 4)
LogDebug "bodyJson ->`n" bodyJson
endpoint := GetSetting(mode, "endpoint")
apiKey := GetSetting(mode, "api_key", GetSetting("settings", "default_api_key"))
req := ComObject("Msxml2.ServerXMLHTTP")
req.open("POST", endpoint, true)
req.SetRequestHeader("Content-Type", "application/json")
req.SetRequestHeader("Authorization", "Bearer " apiKey) ; openai
req.SetRequestHeader("api-key", apiKey) ; azure
req.SetRequestHeader('Content-Length', StrLen(bodyJson))
req.SetRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
req.SetTimeouts(0, 0, 0, GetSetting("settings", "timeout", 120) * 1000) ; read, connect, send, receive
try {
req.send(bodyJson)
req.WaitForResponse()
if (req.status == 0) {
RestoreCursor()
global _running := false
MsgBox "Error: Unable to connect to the API. Please check your internet connection and try again.", , 16
return
} else if (req.status == 200) { ; OK.
data := req.responseText
HandleResponse(data, mode, promptName, input)
} else {
RestoreCursor()
global _running := false
MsgBox "Error: Status " req.status " - " req.responseText, , 16
return
}
} catch as e {
RestoreCursor()
global _running := false
MsgBox "Error: " "Exception thrown!`n`nwhat: " e.what "`nfile: " e.file
. "`nline: " e.line "`nmessage: " e.message "`nextra: " e.extra, , 16
return
}
}
HandleResponse(data, mode, promptName, input) {
global _oldClipboard
Gui_Size(thisGui, MinMax, Width, Height)
{
if MinMax = -1 ; The window has been minimized. No action needed.
return
; Otherwise, the window has been resized or maximized. Resize the Edit control to match.
;xEdit.Move(,, Width-30, Height-55)
ogcActiveXWBC.Move(,, Width-30, Height-55)
xClose.Move(Width/2 - 40,Height-40,,)
}
try {
LogDebug "data ->`n" data
var := Jxon_Load(&data)
text := var.Get("choices")[1].Get("message").Get("content")
if text == "" {
MsgBox "No text was generated. Consider modifying your input."
return
}
;; Clean up response text
text := StrReplace(text, '`r', "") ; remove carriage returns
replaceSelected := GetSetting(promptName, "replace_selected")
if StrLower(replaceSelected) == "false" {
responseStart := GetSetting(promptName, "response_start", "")
responseEnd := GetSetting(promptName, "response_end", "")
text := input . responseStart . text . responseEnd
} else {
;# Remove leading newlines
while SubStr(text, 1, 1) == '`n' {
text := SubStr(text, 2)
}
text := Trim(text)
;# Remove enclosing quotes
if SubStr(text, 1, 1) == '"' and SubStr(text, -1) == '"' {
text := SubStr(text, 2, -1)
}
}
response_type := GetSetting(promptName, "response_type", "")
if _displayResponse or response_type == "popup" {
MyGui := Gui(, "Response")
MyGui.SetFont("s13")
MyGui.Opt("+AlwaysOnTop +Owner +Resize") ; +Owner avoids a taskbar button.
ogcActiveXWBC := MyGui.Add("ActiveX", "xm w800 h480 vIE", "Shell.Explorer")
WB := ogcActiveXWBC.Value
WB.Navigate("about:blank")
css := FileRead("style.css")
options := {css:css
, font_name:"Segoe UI"
, font_size:16
, font_weight:400
, line_height:"1.6"} ; 1.6em - put decimals in "" for easier accuracy/handling.
html := make_html(text, options, false)
WB.document.write(html)
;xEdit := MyGui.Add("Edit", "r10 vMyEdit w800 Wrap", text)
;xEdit.Value .= "`n`n----`n`n" html
xClose := MyGui.Add("Button", "Default w80", "Close")
xClose.OnEvent("Click", (*) => WinClose())
MyGui.Show("NoActivate AutoSize Center")
MyGui.GetPos(&x,&y,&w,&h)
xClose.Move(w/2 - 40,,,)
MyGui.OnEvent("Size", Gui_Size)
} else {
WinActivate _activeWin
A_Clipboard := text
send "^v"
}
global _running := false
Sleep 500
} finally {
global _running := false
A_Clipboard := _oldClipboard
global _oldClipboard := ""
RestoreCursor()
}
}
InitPopupMenu() {
global _iMenu := Menu()
iMenuItemParms := Map()
_iMenu.add "&`` - Display response in new window", NewWindowCheckHandler
_iMenu.Add ; Add a separator line.
menu_items := IniRead("./settings.ini", "popup_menu")
id := 1
loop parse menu_items, "`n" {
v_promptName := A_LoopField
if (v_promptName != "" and SubStr(v_promptName, 1, 1) != "#") {
if (v_promptName = "-") {
_iMenu.Add ; Add a separator line.
} else {
menu_text := GetSetting(v_promptName, "menu_text", v_promptName)
if (RegExMatch(menu_text, "^[^&]*&[^&]*$") == 0) {
if (id == 10)
keyboard_shortcut := "&0 - "
else if (id > 10)
keyboard_shortcut := "&" Chr(id + 86) " - "
else
keyboard_shortcut := "&" id " - "
menu_text := keyboard_shortcut menu_text
id++
}
_iMenu.Add menu_text, MenuHandler
item_count := DllCall("GetMenuItemCount", "ptr", _iMenu.Handle)
iMenuItemParms[item_count] := v_promptName
}
}
}
MenuHandler(ItemName, ItemPos, MyMenu) {
PromptHandler(iMenuItemParms[ItemPos])
}
NewWindowCheckHandler(*) {
_iMenu.ToggleCheck "&`` - Display response in new window"
global _displayResponse := !_displayResponse
_iMenu.Show()
}
}
InitTrayMenu() {
tray := A_TrayMenu
tray.add
tray.add "Open settings", OpenSettings
tray.add "Reload settings", ReloadSettings
tray.add
tray.add "Github readme", OpenGithub
TrayAddStartWithWindows(tray)
}
TrayAddStartWithWindows(tray) {
tray.add "Start with Windows", StartWithWindowsAction
SplitPath a_scriptFullPath, , , , &script_name
_sww_shortcut := a_startup "\" script_name ".lnk"
if FileExist(_sww_shortcut) {
fileGetShortcut _sww_shortcut, &target ;# update if script has moved
if (target != a_scriptFullPath) {
fileCreateShortcut a_scriptFullPath, _sww_shortcut
}
tray.Check("Start with Windows")
} else {
tray.Uncheck("Start with Windows")
}
StartWithWindowsAction(*) {
if FileExist(_sww_shortcut) {
fileDelete(_sww_shortcut)
tray.Uncheck("Start with Windows")
trayTip("Start With Windows", "Shortcut removed", 5)
} else {
fileCreateShortcut(a_scriptFullPath, _sww_shortcut)
tray.Check("Start with Windows")
trayTip("Start With Windows", "Shortcut created", 5)
}
}
}
OpenGithub(*) {
Run "https://github.com/ecornell/ai-tools-ahk#usage"
}
OpenSettings(*) {
Run A_ScriptDir . "\settings.ini"
}
ReloadSettings(*) {
TrayTip("Reload Settings", "Reloading settings...", 5)
_settingsCache.Clear()
InitPopupMenu()
}
UnescapeSetting(obj) {
obj := StrReplace(obj, "\n", "`n")
return obj
}
ShowWaitTooltip() {
if (_running) {
elapsedTime := (A_TickCount - _startTime) / 1000
ToolTip "Generating response... " Format("{:0.2f}", elapsedTime) "s"
SetTimer () => ShowWaitTooltip(), -50
} else {
ToolTip()
}
}
CheckSettings() {
if (_reload_on_change and FileExist("./settings.ini")) {
lastModified := fileGetTime("./settings.ini")
if (lastModified != _lastModified) {
global _lastModified := lastModified
TrayTip("Settings Updated", "Restarting...", 5)
Sleep 2000
Reload
}
SetTimer () => CheckSettings(), -10000 ; Check every 10 seconds
}
}
LogDebug(msg) {
if (_debug != false) {
now := FormatTime(A_Now, "yyyy-MM-dd HH:mm:ss")
logMsg := "[" . now . "] " . msg . "`n"
FileAppend(logMsg, "./debug.log")
}
}