Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensuring Accurate Text Selection #9

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 57 additions & 23 deletions AI-Tools.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,20 @@ PromptHandler(promptName, append := false) {
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"))
input := GetTextFromClip()

try {
input := GetTextFromClip()
} catch {
global _running := false
RestoreCursor()
return
}

ShowWaitTooltip()
SetSystemCursor(GetSetting("settings", "cursor_wait_file", "wait"))
CallAPI(mode, promptName, prompt, input, promptEnd)

} catch as err {
Expand All @@ -93,16 +99,21 @@ PromptHandler(promptName, append := false) {
;###

SelectText() {
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 {
Send "^a"
A_Clipboard := ""
Send "^c"
ClipWait(2)
text := A_Clipboard
if StrLen(text) < 1 {
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
Send "^a"
}
}
sleep 50
}
Expand All @@ -118,6 +129,7 @@ GetTextFromClip() {
text := A_Clipboard

if StrLen(text) < 1 {
ShowWarning("No text selected. Please select text and try again.")
throw ValueError("No text selected", -1)
} else if StrLen(text) > 16000 {
throw ValueError("Text is too long", -1)
Expand All @@ -126,6 +138,10 @@ GetTextFromClip() {
return text
}

ShowWarning(message) {
MsgBox message
}

GetSetting(section, key, defaultValue := "") {
global _settingsCache
if (_settingsCache.Has(section . key . defaultValue)) {
Expand Down Expand Up @@ -186,6 +202,10 @@ GetBody(mode, promptName, prompt, input, promptEnd) {
}

CallAPI(mode, promptName, prompt, input, promptEnd) {
if (StrLen(input) < 1) {
; Input is too short. No request will be made to the API.
return
}

body := GetBody(mode, promptName, prompt, input, promptEnd)
bodyJson := Jxon_dump(body, 4)
Expand All @@ -204,16 +224,30 @@ CallAPI(mode, promptName, prompt, input, promptEnd) {
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

req.send(bodyJson)
try {
req.send(bodyJson)
req.WaitForResponse()

req.WaitForResponse()
if (req.status == 200) { ; OK.
data := req.responseText
HandleResponse(data, mode, promptName, input)
} else {
MsgBox "Status " req.status " " req.responseText, , 16
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 {
RestoreCursor()
global _running := false
MsgBox "Error: Unable to connect to the API. Please check your internet connection and try again.", , 16
return
}

}

HandleResponse(data, mode, promptName, input) {
Expand Down Expand Up @@ -432,4 +466,4 @@ LogDebug(msg) {
logMsg := "[" . now . "] " . msg . "`n"
FileAppend(logMsg, "./debug.log")
}
}
}