-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdialog.lua.1
More file actions
450 lines (431 loc) · 13.7 KB
/
dialog.lua.1
File metadata and controls
450 lines (431 loc) · 13.7 KB
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
local Common = ... -- name, utils, tempdir, cfgpath
local State = Common.State
local utils = Common.utils -- HelpTopic, HK
local O = Common.O
local F = far.Flags
local DIF_HOMEITEM = F.DIF_HOMEITEM or 0 --far2m
local newDlgInfo = utils.load"DlgHelper.lua.1"
local X2 = 3 -- coords indices
local SELECTED, FLAGS, DATA = 6,9,10
local function populateHistory (item, hDlg, values, source)
if not item then return end
if type(values)~="table" then values = {values} end
local success, history = pcall(utils.readHistory, item.History)
if not success then
far.Message(history, "Error", nil, "wl")
return
end
local inHistory = {}
for _,v in ipairs(history) do inHistory[v] = true end
local added
for i=#values,1,-1 do
if not inHistory[values[i]] then
hDlg:AddHistory(item.idx, values[i])
added = true
end
end
if added and history[1] then -- preserve lasthistory
hDlg:AddHistory(item.idx, history[#history])
end
if source=="init" and #history==0 then
hDlg:SetText(item.idx, values[1] or "")
end
end
local function isOpened (pathname)
for i=1,actl.GetWindowCount() do
if actl.GetWindowInfo(i).Name==pathname then
return true
end
end
end
local idInput = win.Uuid"58DD9ECD-CFFA-472E-BFD7-042295C86CAE"
local SESSION = "default"
local _prompt = "Ask any &question (editor selection will be included with request)"
local function main (opts)
local json_module = O.json_module
if not json_module then
for _,name in ipairs {"cjson.safe", "rsjson", "ljson", "dkjson", "lunajson", "json"} do
if pcall(require, name) then json_module = name; break end
end
end
local json = json_module and require(json_module)
if json then
json.pretty_encode = json.encode
if json_module=="dkjson" then
json = json.use_lpeg()
json.pretty_encode = function(data)
return json.encode(data, {indent=true})
end
end
Common.json = json
if pcall(require, "ssl.https") then
Common.restapi = utils.cached(function()
local restapi = utils.loadMoon("restapi.moon.1", {json=json}) "restapi"
restapi.Plain.__base.formatErr = function (meta)
return table.concat({meta.statusline or meta.status_code, meta.data_str}, "\n\1\n")
end
return restapi
end)"restapi"
end
end
local configs = utils.load("configs.lua.1",main)
local function fmtProfile (fmt)
return opts.profile=="default" and "" or fmt:format(opts.profile)
end
local cfg = opts.cfg
cfg = configs.getCfg(opts.profile, cfg) or cfg
if not cfg or cfg=="" then
opts.compact = false
opts.nodialog = false
return configs.chooseCfg(opts)
end
opts.cfg = cfg
if not cfg.params then
cfg.params = {}
for i=1,debug.getinfo(cfg.applyParamsFn, "u").nparams do
local name = debug.getlocal(cfg.applyParamsFn, i)
cfg.params[i] = name
cfg.params[name] = i
end
end
local canSession = cfg.params.session or cfg.info.clearSession
local width = 86
local flags = O.smallDlg and F.FDLG_SMALLDIALOG or 0
local maxW = Far.Width
if width+(O.smallDlg and 0 or 3*2)>maxW then
flags = F.FDLG_SMALLDIALOG
width = math.min(maxW, width)
end
local data = {
width=width,
Guid=idInput,
Flags=flags,
HelpTopic=utils.HelpTopic"Dialog",
}
local II = newDlgInfo(data)
if opts.compact then
local hide
II.ln = (function (ln)
return function (self, cutpoint)
hide = hide or cutpoint
if not hide then ln(self) end
end
end)(II.ln)
II.add = (function (add)
return function (self,item)
if hide then
item[4] = bit64.bor(F.DIF_HIDDEN, item.Flags or item[4] or 0)
end
return add(self,item)
end
end)(II.add)
opts.compact = false
opts._hidden = true
else
opts._hidden = false
end
local F_HISTORY = F.DIF_HISTORY +F.DIF_USELASTHISTORY +F.DIF_MANUALADDHISTORY
II:add {F.DI_DOUBLEBOX, Common.name, {[X2]=-1}}
local dialog = { II=II, opts=opts, populateHistory=populateHistory, } --processSetsHistories
utils.load"profiles.lua.1"(dialog)
II:add {F.DI_TEXT, _prompt}
II:ln() -- -- -- --
II:add {F.DI_EDIT, opts.prompt, {[X2]=-1},
DIF_HOMEITEM +F.DIF_FOCUS
+F_HISTORY, History=("AskAI%s prompt"):format(fmtProfile":%s"),
name="prompt"
}
II:ln() -- -- -- --
II:add {F.DI_TEXT, cfg.info.name, {}, F.DIF_SEPARATOR +F.DIF_CENTERGROUP,
oncolor=function(_,_,_,Param2)
local HiText = Param2[2]
Param2[1] = HiText
return Param2
end
}
II:ln() -- -- -- --
II:add {F.DI_CHECKBOX, "&Session", nil, canSession and F.DIF_3STATE or F.DIF_DISABLE,
canSession and State.useSession or 0,
onkey=function(_,_,_,key)
if key=="F3" then
if cfg.info.showSession then
cfg.info:showSession(SESSION)
end
return true
end
end,
name="chkUseSession"
}
local sessionExists = cfg.info:sessionExists(SESSION)
II:add {F.DI_BUTTON, "‹&-›", nil, F.DIF_BTNNOCLOSE +F.DIF_NOBRACKETS +(sessionExists and 0 or F.DIF_DISABLE),
onclick=function(_,hDlg,idx)
cfg.info:clearSession(SESSION)
local item = hDlg:GetDlgItem(idx)
item[FLAGS] = bit64.bor(item[FLAGS], F.DIF_DISABLE)
hDlg:SetDlgItem(idx, item)
end, name="btnClrSession"
}
local x2 = 18
II:add {F.DI_CHECKBOX, "st&ream", {x2}, cfg.params.stream and 0 or F.DIF_DISABLE, State.useStream,
name="chkStream"
}
if cfg.info.exe==true then
local function setTimeout (timeout)
require"ssl.https".TIMEOUT = timeout
require"socket.http".TIMEOUT = timeout
end
if O.timeout then
setTimeout(O.timeout)
O.timeout = false
end
II:add {F.DI_TEXT, "Time&out", {interval=4}, 0}
II:add {F.DI_FIXEDIT, require"ssl.https".TIMEOUT, nil, F.DIF_MASKEDIT, Mask="999",
onclose=function(_,hDlg,idx,item)
if item then
setTimeout(tonumber(hDlg:GetText(idx)))
end
end,
}
end
II:add {F.DI_CHECKBOX, "Deb&ug", {interval=4}, O.debug==nil and F.DIF_HIDDEN, O.debug and 1 or 0,
onclick=function(_,hDlg,idx)
O.debug = hDlg:GetCheck(idx)==1
end, name="chkDebug"
}
II:add {F.DI_CHECKBOX, "&Wrap", {-12}, F.DIF_3STATE, State.useWrap,
onclick=function(_,hDlg,idx)
hDlg:Enable(idx+1, hDlg:GetCheck(idx)==0 and 0 or 1)
end, name="chkWrap"
}
II:add {F.DI_FIXEDIT, State.wrapAt, {[X2]=-1},
(State.useWrap~=0 and 0 or F.DIF_DISABLE) +F.DIF_MASKEDIT, Mask="999",
name="edtWrap"
}
II:ln("cutpoint") -- -- -- --
local function makeHistoryName (param, name)
name = name or cfg.info.histories and cfg.info.histories[param]
local prefix = ("AskAI%s"):format(fmtProfile":%s")
local history = prefix.." param "..(name or param)
if not (name or O.sharedParams[param]) then
local baseName = cfg.info.name:match"[^[]+":match"^(.-)%s*$"
history = history.." - "..baseName
end
return history
end
local function processSetsHistories (hDlg, text, dependants)
local env = cfg.info.env
for _,subparam in ipairs(dependants) do
local items = {II[subparam]}
items[#items+1] = env and env[subparam] and II[env[subparam]]
for _,item in ipairs(items) do
local idx = item.idx
hDlg:SetText(idx, "")
local History = makeHistoryName(nil, subparam..":"..text)
II[item.name].History = History
hDlg:SetHistory(idx, History)
hDlg:EditUnchangedFlag(idx, 1)
item[DATA] = hDlg:GetText(idx)
end
end
end
dialog.processSetsHistories = processSetsHistories
local function processSetsDependants (self, hDlg, idx, text)
text = text or hDlg:GetText(idx)
local dependants = cfg.info.sets[self.name]
hDlg:EnableRedraw(0)
processSetsHistories(hDlg, text, dependants)
hDlg:EnableRedraw(1)
end
local hk = utils.HK.new("")
for _,param in ipairs(cfg.params) do
if not ({context=1, prompt=1, session=1, stream=1})[param] then
if hk.i==0 then
II:add {F.DI_TEXT, "Parameters", {[X2]=-1}, F.DIF_SEPARATOR +F.DIF_CENTERTEXT}
II:ln() -- -- -- --
end
local col2 = math.max(x2, param:len()+1)
local visibility = cfg.info.hidden and cfg.info.hidden[param] and F.DIF_HIDDEN or F.DIF_NONE
II:add {F.DI_TEXT, hk:iter(visibility==F.DIF_HIDDEN)..param, {"-1", [X2]=col2-1}, visibility}
local dependants = cfg.info.sets and cfg.info.sets[param]
II:add {F.DI_EDIT, nil, {col2, nil, -1}, F_HISTORY + visibility,
History=makeHistoryName(param),
oninit=dependants and processSetsDependants,
onedit=dependants and processSetsDependants,
name=param
}
if visibility~=F.DIF_HIDDEN then II:ln() end -- -- -- --
end
end
if cfg.info.env then
local function colorLabel (_,hDlg,idx,Param2)
local Next = II[idx+1][DATA]
if Next~="" and Next~=hDlg:GetText(idx+1) then
local Title = Param2[1]
if Title.Flags then
Title.Flags = bit64.bor(Title.Flags, F.FCF_FG_ITALIC)
else -- far2m
Param2[1] = bit64.bor(Param2[1], F.COMMON_LVB_REVERSE_VIDEO)
end
return Param2
end
end
local function resetEnvValue (self,hDlg,idx,key)
if key=="Del" then
if self[DATA]~="" and hDlg:GetText(idx)=="" then
hDlg:EnableRedraw(0)
hDlg:SetText(idx, self[DATA])
hDlg:EditUnchangedFlag(idx, 1)
hDlg:EnableRedraw(1)
return true
end
return false
end
end
II:add {F.DI_TEXT, "Environment &variables", {[X2]=-1}, F.DIF_SEPARATOR +F.DIF_CENTERTEXT}
II:ln() -- -- -- --
for k,var in pairs(cfg.info.env) do
local value = win.GetEnv(var)
local col2 = math.max(x2, var:len()+1)
II:add {F.DI_TEXT, hk:iter()..var, {"-1", [X2]=col2-1},
oncolor=colorLabel,
}
II:add {F.DI_EDIT, value or "", {col2, nil, -1}, F_HISTORY,
History=makeHistoryName(k),
onkey=resetEnvValue,
name=var
}
II:ln() -- -- -- --
end
end
II:add {F.DI_TEXT, nil, nil, F.DIF_SEPARATOR}
II:ln() -- -- -- --
if cfg.info.exe~=true then
local vFlags = cfg.info.config and 0 or F.DIF_DISABLE
II:add {F.DI_BUTTON, "Utilit&y cfg", nil, F.DIF_BTNNOCLOSE +F.DIF_CENTERGROUP +vFlags,
onclick=function()
if cfg.info.config then
editor.Editor(cfg.info.config)
end
end,
}
end
if cfg.authFn then
II:add {F.DI_BUTTON, "Au&thorization", nil, F.DIF_BTNNOCLOSE +F.DIF_CENTERGROUP,
onclick=function(_,hDlg)
local success, ret, err = pcall(cfg.authFn)
actl.RedrawAll()
if not success or err then
if err~="User break" then
far.Message(err or ret,"Authorization Failed",nil,"wl")
end
return
end
local idx = II.apikey.idx
hDlg:SetText(idx, ret.refresh_token)
hDlg:AddHistory(idx, ret.refresh_token)
end,
}
end
utils.load"presets.lua.1"(dialog)
utils.load"models.lua.1"(dialog)
local function editConfig (hDlg,idx)
if F.EEC_MODIFIED==editor.Editor(cfg.pathname) then
hDlg:Close(idx)
utils.synchro(main,opts)
end
return true
end
II:add {F.DI_BUTTON, "Pro&viders - ^B", nil, F.DIF_CENTERGROUP,
onkey=function(_,hDlg,idx,key)
if key=="F4" then
return editConfig(hDlg,idx)
end
end,
keys={
AltF4=function(_,hDlg,idx)
return editConfig(hDlg,idx)
end
},
name="btnSwitch"
}
II:add {F.DI_BUTTON, "Go&!", nil, F.DIF_CENTERGROUP +F.DIF_DEFAULTBUTTON,
name="btnGo"
}
data.DlgProc = utils.load"dlgproc.lua.1"(dialog)
local res = far.Dialog(II:params())
if res==-1 then return end
if II[res].name=="btnGo" then
if cfg.info.env then
for _,var in pairs(cfg.info.env) do
local v = II[var][DATA]
win.SetEnv(var, v~="" and v or nil)
end
end
if canSession then
State.useSession = II.chkUseSession[SELECTED]
end
State.useStream = II.chkStream[SELECTED]
State.useWrap = II.chkWrap[SELECTED]
State.wrapAt = II.edtWrap[DATA]
local outputFilename = utils.pathjoin(Common.tempdir, ("Ask AI%s.md")
:format(fmtProfile".%s"))
local context = opts.context
if not cfg.params.prompt then
local p = II.prompt[DATA]
if context~="" then
local n
p,n = p:gsub("{{%input%}}", context)
if n==0 then
p = p.."\n"..context
end
end
context = p
end
local session = SESSION
if State.useSession==0 then
session = ""
elseif State.useSession==2 and not isOpened(outputFilename) then
if cfg.info.clearSession then
cfg.info:clearSession(SESSION)
else
session = ""
end
end
local base = {
context=context,
session=session,
stream=State.useStream==1,
}
local args = {}
for i=1,#cfg.params do
local param = cfg.params[i]
local value = base[param]==nil and II[param][DATA] or base[param]
args[i] = value~="" and value or nil
end
if context~="" and not cfg.params.context then
cfg.info._context = context
end
local success, processFn = pcall(cfg.applyParamsFn, unpack(args,1,#cfg.params))
if not success then
far.Message(processFn, Common.name, nil, "w")
return main(opts)
end
local linewrap = ({[0]=nil, tonumber(II.edtWrap[DATA]), "dynamic"})[State.useWrap]
utils.load"output.lua.1"(
outputFilename,
processFn,
II.prompt[DATA],
II.model and II.model[DATA],
linewrap,
cfg.params.stream and State.useStream==1
)
State.lastOutputFilename = outputFilename
elseif II[res].name=="btnSwitch" then
opts.prompt = II.prompt[DATA]
configs.chooseCfg(opts)
elseif II[res].name=="profile" then
opts.prompt = II.prompt[DATA]
main(opts) --switch profile
end
end
return main