-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
dialogs.lua
173 lines (144 loc) · 4.96 KB
/
dialogs.lua
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
local InputDialog = require("ui/widget/inputdialog")
local ChatGPTViewer = require("chatgptviewer")
local UIManager = require("ui/uimanager")
local InfoMessage = require("ui/widget/infomessage")
local _ = require("gettext")
local queryChatGPT = require("gpt_query")
local CONFIGURATION = nil
local buttons, input_dialog
local success, result = pcall(function() return require("configuration") end)
if success then
CONFIGURATION = result
else
print("configuration.lua not found, skipping...")
end
local function translateText(text, target_language)
local translation_message = {
role = "user",
content = "Translate the following text to " .. target_language .. ": " .. text
}
local translation_history = {
{
role = "system",
content = "You are a helpful translation assistant. Provide direct translations without additional commentary."
},
translation_message
}
return queryChatGPT(translation_history)
end
local function createResultText(highlightedText, message_history)
local result_text = _("Highlighted text: ") .. "\"" .. highlightedText .. "\"\n\n"
for i = 3, #message_history do
if message_history[i].role == "user" then
result_text = result_text .. _("User: ") .. message_history[i].content .. "\n\n"
else
result_text = result_text .. _("ChatGPT: ") .. message_history[i].content .. "\n\n"
end
end
return result_text
end
local function showLoadingDialog()
local loading = InfoMessage:new{
text = _("Loading..."),
timeout = 0.1
}
UIManager:show(loading)
end
local function showChatGPTDialog(ui, highlightedText, message_history)
local title, author =
ui.document:getProps().title or _("Unknown Title"),
ui.document:getProps().authors or _("Unknown Author")
local message_history = message_history or {{
role = "system",
content = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly. Answer as concisely as possible."
}}
local function handleNewQuestion(chatgpt_viewer, question)
table.insert(message_history, {
role = "user",
content = question
})
local answer = queryChatGPT(message_history)
table.insert(message_history, {
role = "assistant",
content = answer
})
local result_text = createResultText(highlightedText, message_history)
chatgpt_viewer:update(result_text)
end
buttons = {
{
text = _("Cancel"),
callback = function()
UIManager:close(input_dialog)
end
},
{
text = _("Ask"),
callback = function()
local question = input_dialog:getInputText()
UIManager:close(input_dialog)
showLoadingDialog()
UIManager:scheduleIn(0.1, function()
local context_message = {
role = "user",
content = "I'm reading something titled '" .. title .. "' by " .. author ..
". I have a question about the following highlighted text: " .. highlightedText
}
table.insert(message_history, context_message)
local question_message = {
role = "user",
content = question
}
table.insert(message_history, question_message)
local answer = queryChatGPT(message_history)
local answer_message = {
role = "assistant",
content = answer
}
table.insert(message_history, answer_message)
local result_text = createResultText(highlightedText, message_history)
local chatgpt_viewer = ChatGPTViewer:new {
title = _("AskGPT"),
text = result_text,
onAskQuestion = handleNewQuestion
}
UIManager:show(chatgpt_viewer)
end)
end
}
}
if CONFIGURATION and CONFIGURATION.features and CONFIGURATION.features.translate_to then
table.insert(buttons, {
text = _("Translate"),
callback = function()
showLoadingDialog()
UIManager:scheduleIn(0.1, function()
local translated_text = translateText(highlightedText, CONFIGURATION.features.translate_to)
table.insert(message_history, {
role = "user",
content = "Translate to " .. CONFIGURATION.features.translate_to .. ": " .. highlightedText
})
table.insert(message_history, {
role = "assistant",
content = translated_text
})
local result_text = createResultText(highlightedText, message_history)
local chatgpt_viewer = ChatGPTViewer:new {
title = _("Translation"),
text = result_text,
onAskQuestion = handleNewQuestion
}
UIManager:show(chatgpt_viewer)
end)
end
})
end
input_dialog = InputDialog:new{
title = _("Ask a question about the highlighted text"),
input_hint = _("Type your question here..."),
input_type = "text",
buttons = {buttons}
}
UIManager:show(input_dialog)
end
return showChatGPTDialog