forked from KoboldAI/KoboldAI-Client
-
Notifications
You must be signed in to change notification settings - Fork 135
/
bridge.lua
2091 lines (1859 loc) · 68.7 KB
/
bridge.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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- KoboldAI Lua 5.4 Bridge
---@param _python? table<string, any>
---@param _bridged? table<string, any>
---@return KoboldLib, KoboldCoreLib?
return function(_python, _bridged)
--==========================================================================
-- Globally allows using a _kobold_next metamethod for "Kobold" classes only
--==========================================================================
local old_next = next
---@generic K, V
---@param t table<K, V>
---@param k? K
---@return K?, V?
function next(t, k)
local meta = getmetatable(t)
return ((meta ~= nil and type(rawget(t, "_name")) == "string" and string.match(rawget(t, "_name"), "^Kobold") and type(meta._kobold_next) == "function") and meta._kobold_next or old_next)(t, k)
end
--==========================================================================
-- General utility functions
--==========================================================================
---@generic T
---@param original T
---@return T
local function deepcopy(original)
if type(original) == "table" then
local copy = {}
for k, v in old_next, original, nil do
copy[k] = deepcopy(v)
end
setmetatable(copy, deepcopy(getmetatable(original)))
return copy
end
return original
end
---@param paths string|table<integer, string>
---@return nil
local function set_require_path(paths)
if type(paths) == "string" then
paths = {paths}
end
local config = {}
local i = 1
for substring in string.gmatch(package.config, "[^\n]+") do
config[i] = substring
i = i + 1
end
local _paths = {}
for i, path in ipairs(paths) do
_paths[i] = path .. config[1] .. config[3] .. ".lua" .. config[2] .. path .. config[1] .. config[3] .. config[1] .. "init.lua"
end
package.path = table.concat(_paths, config[2])
package.cpath = ""
end
---@param path string
---@param filename string
---@return string
local function join_folder_and_filename(path, filename)
return path .. string.match(package.config, "[^\n]+") .. filename
end
--==========================================================================
-- _bridged preprocessing
--==========================================================================
local bridged = {}
for k in _python.iter(_bridged) do
local v = _bridged[k]
bridged[k] = type(v) == "userdata" and _python.as_attrgetter(v) or v
end
set_require_path(bridged.lib_paths)
--==========================================================================
-- Wraps most functions in this file so that they restore original
-- metatables prior to executing the function body
--==========================================================================
local wrapped = false
---@class Metatables
local metatables = {}
local type_map = {
_nil = nil,
_boolean = false,
_number = 0,
_string = "",
_function = type,
_thread = coroutine.create(function() end),
}
function metatables:overwrite()
for k, v in pairs(type_map) do
self[k] = debug.getmetatable(v)
end
end
function metatables:restore()
for k, v in pairs(type_map) do
debug.setmetatable(v, self[k])
end
end
local metatables_original = deepcopy(metatables)
metatables_original:overwrite()
local metawrapper = {}
---@generic T : table
---@param t T
---@return T
function metawrapper.__newindex(t, k, wrapped_func)
if type(wrapped_func) == "function" then
return rawset(t, k, function(...)
local _needs_unwrap = false
if not wrapped then
metatables:overwrite()
metatables_original:restore()
_needs_unwrap = true
wrapped = true
end
local r = table.pack(wrapped_func(...))
if _needs_unwrap then
metatables:restore()
wrapped = false
end
return table.unpack(r, 1, r.n)
end)
else
return rawset(t, k, wrapped_func)
end
end
--==========================================================================
-- Modules
--==========================================================================
---@class KoboldLib
---@field API_VERSION number
---@field authorsnote string
---@field authorsnotetemplate string
---@field memory string
---@field submission string
---@field model string
---@field modeltype "'readonly'"|"'api'"|"'unknown'"|"'gpt2'"|"'gpt2-medium'"|"'gpt2-large'"|"'gpt2-xl'"|"'gpt-neo-125M'"|"'gpt-neo-1.3B'"|"'gpt-neo-2.7B'"|"'gpt-j-6B'"
---@field modelbackend "'readonly'"|"'api'"|"'transformers'"|"'mtj'"
---@field is_custommodel boolean
---@field custmodpth string
---@field logits table<integer, table<integer, number>>
---@field logits_rows integer
---@field logits_cols integer
---@field generated table<integer, table<integer, integer>>
---@field generated_rows integer
---@field generated_cols integer
---@field outputs table<integer, string>
---@field num_outputs integer
---@field feedback string
---@field is_config_file_open boolean
local kobold = setmetatable({API_VERSION = 1.2}, metawrapper)
local KoboldLib_mt = setmetatable({}, metawrapper)
local KoboldLib_getters = setmetatable({}, metawrapper)
local KoboldLib_setters = setmetatable({}, metawrapper)
---@param t KoboldLib
function KoboldLib_mt.__index(t, k)
local getter = KoboldLib_getters[k]
if getter ~= nil then
return getter(t)
end
return rawget(t, k)
end
---@param t KoboldLib
function KoboldLib_mt.__newindex(t, k, v)
local setter = KoboldLib_setters[k]
if setter ~= nil then
return setter(t, v)
end
return rawset(t, k, v)
end
---@class KoboldCoreLib
---@field userscripts KoboldUserScriptList
local koboldcore = setmetatable({}, metawrapper)
local KoboldCoreLib_mt = setmetatable({}, metawrapper)
local KoboldCoreLib_getters = setmetatable({}, metawrapper)
local KoboldCoreLib_setters = setmetatable({}, metawrapper)
---@param t KoboldCoreLib
function KoboldCoreLib_mt.__index(t, k)
local getter = KoboldCoreLib_getters[k]
if getter ~= nil then
return getter(t, k)
end
return rawget(t, k)
end
---@param t KoboldCoreLib
function KoboldCoreLib_mt.__newindex(t, k, v)
local setter = KoboldCoreLib_setters[k]
if setter ~= nil then
return setter(t, k, v)
end
return rawset(t, k, v)
end
---@class KoboldBridgeLib
local koboldbridge = {}
koboldbridge.regeneration_required = false
koboldbridge.resend_settings_required = false
koboldbridge.generating = true
koboldbridge.restart_sequence = nil
koboldbridge.userstate = nil
koboldbridge.logits = {}
koboldbridge.vocab_size = 0
koboldbridge.generated = {}
koboldbridge.generated_cols = 0
koboldbridge.outputs = {}
koboldbridge.feedback = nil ---@type string?
function koboldbridge:clear_userscript_metadata()
self.logging_name = nil
self.filename = nil
end
---@return nil
local function maybe_require_regeneration()
if koboldbridge.userstate == "genmod" or koboldbridge.userstate == "outmod" then
koboldbridge.regeneration_required = true
end
end
--==========================================================================
-- Userscript API: Configuration
--==========================================================================
local config_files = {} ---@type table<string, file*>
local config_file_filename_map = {} ---@type table<file*, string>
---@return file*?
local function open_and_handle_errors(...)
local file, err_msg = io.open(...)
if err_msg ~= nil then
koboldbridge.obliterate_multiverse()
error(err_msg)
return
end
return file
end
---@param file? file*
local function new_close_pre(file)
if file == nil then
file = io.output()
end
local filename = config_file_filename_map[file]
if filename ~= nil then
config_file_filename_map[file] = nil
config_files[filename] = nil
end
end
---@param f fun(file?: file*)
local function _new_close(f)
---@param file? file*
return function(file)
new_close_pre(file)
return f(file)
end
end
debug.getmetatable(io.stdout).__index.close = _new_close(io.stdout.close)
debug.getmetatable(io.stdout).__close = _new_close(io.stdout.close)
---@param filename string
---@return boolean
local function is_config_file_open(filename)
return config_files[filename] ~= nil
end
---@param filename string
---@param clear? boolean
---@return file*
local function get_config_file(filename, clear)
if not is_config_file_open(filename) then
local config_filepath = join_folder_and_filename(bridged.config_path, filename .. ".conf")
open_and_handle_errors(config_filepath, "a"):close()
config_files[filename] = open_and_handle_errors(config_filepath, clear and "w+b" or "r+b")
config_file_filename_map[config_files[filename]] = filename
end
return config_files[filename]
end
---@param clear? boolean
---@return file*
function kobold.get_config_file(clear)
return get_config_file(koboldbridge.filename, clear)
end
---@param t KoboldLib
---@return boolean
function KoboldLib_getters.is_config_file_open(t)
return is_config_file_open(koboldbridge.filename)
end
---@param t KoboldLib
---@param v boolean
function KoboldLib_setters.is_config_file_open(t, v)
error("`KoboldLib.is_config_file_open` is a read-only attribute")
end
--==========================================================================
-- Userscript API: World Info
--==========================================================================
local fields = setmetatable({}, metawrapper)
---@param t KoboldWorldInfoEntry|KoboldWorldInfoFolder|KoboldWorldInfo|KoboldWorldInfoFolderSelector
---@return boolean
local function check_validity(t)
if not t:is_valid() then
error("Attempted to use a nonexistent/deleted `"..rawget(t, "_name").."`")
return false
end
return true
end
----------------------------------------------------------------------------
---@class KoboldWorldInfoEntry_base
---@type table<integer, nil>
local _ = {}
---@class KoboldWorldInfoEntry : KoboldWorldInfoEntry_base
---@field key string
---@field keysecondary string
---@field content string
---@field comment string
---@field folder integer
---@field num integer
---@field selective boolean
---@field constant boolean
---@field uid integer
local KoboldWorldInfoEntry = setmetatable({
_name = "KoboldWorldInfoEntry",
}, metawrapper)
fields.KoboldWorldInfoEntry = {
"key",
"keysecondary",
"content",
"comment",
"folder",
"num",
"selective",
"constant",
"uid",
}
local KoboldWorldInfoEntry_mt = setmetatable({}, metawrapper)
local KoboldWorldInfoEntry_fieldtypes = {
key = "string",
keysecondary = "string",
content = "string",
comment = "string",
selective = "boolean",
constant = "boolean",
}
---@return boolean
function KoboldWorldInfoEntry:is_valid()
return _python.as_attrgetter(bridged.koboldai_vars.worldinfo_u).get(rawget(self, "_uid")) ~= nil
end
---@param submission? string
---@param kwargs? table<string, any>
---@return string
function KoboldWorldInfoEntry:compute_context(submission, kwargs)
if not check_validity(self) then
return ""
elseif submission == nil then
submission = kobold.submission
elseif type(submission) ~= "string" then
error("`compute_context` takes a string or nil as argument #1, but got a " .. type(submission))
return ""
end
return bridged.compute_context(submission, {self.uid}, nil, kwargs)
end
---@generic K
---@param t KoboldWorldInfoEntry|KoboldWorldInfoFolder|KoboldWorldInfo|KoboldWorldInfoFolderSelector
---@param k K
---@return K, any
function KoboldWorldInfoEntry_mt._kobold_next(t, k)
local _t = fields[rawget(t, "_name")]
if _t == nil then
return
end
return next(_t, k)
end
---@param t KoboldWorldInfoEntry|KoboldWorldInfoFolder|KoboldWorldInfo|KoboldWorldInfoFolderSelector
---@return function, KoboldWorldInfoEntry|KoboldWorldInfoFolder|KoboldWorldInfo|KoboldWorldInfoFolderSelector, nil
function KoboldWorldInfoEntry_mt.__pairs(t)
return next, t, nil
end
---@param t KoboldWorldInfoEntry
function KoboldWorldInfoEntry_mt.__index(t, k)
if not check_validity(t) then
return
elseif k == "uid" then
return rawget(t, "_uid")
elseif type(k) == "string" then
return bridged.get_attr(t.uid, k)
end
end
---@param t KoboldWorldInfoEntry
---@return KoboldWorldInfoEntry
function KoboldWorldInfoEntry_mt.__newindex(t, k, v)
if not check_validity(t) then
return
elseif fields[rawget(t, "_name")] then
if type(k) == "string" and KoboldWorldInfoEntry_fieldtypes[k] == nil then
error("`"..rawget(t, "_name").."."..k.."` is a read-only attribute")
return
elseif type(k) == "string" and type(v) ~= KoboldWorldInfoEntry_fieldtypes[k] then
error("`"..rawget(t, "_name").."."..k.."` must be a "..KoboldWorldInfoEntry_fieldtypes[k].."; you attempted to set it to a "..type(v))
return
else
if k ~= "comment" and not (t.selective and k == "keysecondary") then
maybe_require_regeneration()
end
bridged.set_attr(t.uid, k, v)
return t
end
end
return rawset(t, k, v)
end
----------------------------------------------------------------------------
---@class KoboldWorldInfoFolder_base
---@type table<integer, KoboldWorldInfoEntry>
local _ = {}
---@class KoboldWorldInfoFolder : KoboldWorldInfoFolder_base
---@field uid integer
---@field name string
local KoboldWorldInfoFolder = setmetatable({
_name = "KoboldWorldInfoFolder",
}, metawrapper)
fields.KoboldWorldInfoFolder = {
"uid",
}
local KoboldWorldInfoFolder_mt = setmetatable({}, metawrapper)
---@param u integer
---@return KoboldWorldInfoEntry?
function KoboldWorldInfoFolder:finduid(u)
if not check_validity(self) or type(u) ~= "number" then
return
end
local query = _python.as_attrgetter(bridged.koboldai_vars.worldinfo_u).get(u)
if query == nil or (rawget(self, "_name") == "KoboldWorldInfoFolder" and self.uid ~= _python.as_attrgetter(query).get("folder")) then
return
end
local entry = deepcopy(KoboldWorldInfoEntry)
rawset(entry, "_uid", u)
return entry
end
---@param submission? string
---@param entries? KoboldWorldInfoEntry|table<any, KoboldWorldInfoEntry>
---@param kwargs? table<string, any>
---@return string
function KoboldWorldInfoFolder:compute_context(submission, entries, kwargs)
if not check_validity(self) then
return ""
elseif submission == nil then
submission = kobold.submission
elseif type(submission) ~= "string" then
error("`compute_context` takes a string or nil as argument #1, but got a " .. type(submission))
return ""
end
local _entries
if entries ~= nil then
if type(entries) ~= "table" or (entries.name ~= nil and entries.name ~= "KoboldWorldInfoEntry") then
error("`compute_context` takes a KoboldWorldInfoEntry, table of KoboldWorldInfoEntries or nil as argument #2, but got a " .. type(entries))
return ""
elseif entries.name == "KoboldWorldInfoEntry" then
_entries = {entries}
else
_entries = {}
for k, v in pairs(entries) do
if type(v) == "table" and v.name == "KoboldWorldInfoEntry" and v:is_valid() then
_entries[k] = v.uid
end
end
end
end
local folders
if self.name == "KoboldWorldInfoFolder" then
folders = {rawget(self, "_uid")}
end
return bridged.compute_context(submission, _entries, folders, kwargs)
end
---@return boolean
function KoboldWorldInfoFolder:is_valid()
return _python.as_attrgetter(bridged.koboldai_vars.wifolders_d).get(rawget(self, "_uid")) ~= nil
end
---@param t KoboldWorldInfoFolder
---@return integer
function KoboldWorldInfoFolder_mt.__len(t)
if not check_validity(t) then
return 0
end
return math.tointeger(_python.builtins.len(_python.as_attrgetter(bridged.koboldai_vars.wifolders_u).get(t.uid))) - 1
end
KoboldWorldInfoFolder_mt._kobold_next = KoboldWorldInfoEntry_mt._kobold_next
KoboldWorldInfoFolder_mt.__pairs = KoboldWorldInfoEntry_mt.__pairs
---@param t KoboldWorldInfoFolder|KoboldWorldInfo
---@return KoboldWorldInfoEntry?
function KoboldWorldInfoFolder_mt.__index(t, k)
if not check_validity(t) then
return
elseif rawget(t, "_name") == "KoboldWorldInfoFolder" and k == "uid" then
return rawget(t, "_uid")
elseif rawget(t, "_name") == "KoboldWorldInfoFolder" and k == "name" then
return bridged.folder_get_attr(t.uid, k)
elseif type(k) == "number" then
local query = rawget(t, "_name") == "KoboldWorldInfoFolder" and _python.as_attrgetter(bridged.koboldai_vars.wifolders_u).get(t.uid) or bridged.koboldai_vars.worldinfo_i
k = math.tointeger(k)
if k == nil or k < 1 or k > #t then
return
end
local entry = deepcopy(KoboldWorldInfoEntry)
rawset(entry, "_uid", math.tointeger(query[k-1].uid))
return entry
end
end
---@param t KoboldWorldInfoFolder|KoboldWorldInfo
---@return KoboldWorldInfoFolder|KoboldWorldInfo
function KoboldWorldInfoFolder_mt.__newindex(t, k, v)
if not check_validity(t) then
return
elseif type(k) == "number" and math.tointeger(k) ~= nil then
error("Cannot write to integer indices of `"..rawget(t, "_name").."`")
elseif rawget(t, "_name") == "KoboldWorldInfoFolder" and k == "uid" then
error("`"..rawget(t, "_name").."."..k.."` is a read-only attribute")
elseif t == "name" then
if type(v) ~= "string" then
error("`"..rawget(t, "_name").."."..k.."` must be a string; you attempted to set it to a "..type(v))
return
end
bridged.folder_set_attr(t.uid, k, v)
return t
else
return rawset(t, k, v)
end
end
----------------------------------------------------------------------------
---@class KoboldWorldInfoFolderSelector_base
---@type table<integer, KoboldWorldInfoFolder>
local _ = {}
---@class KoboldWorldInfoFolderSelector : KoboldWorldInfoFolderSelector_base
local KoboldWorldInfoFolderSelector = setmetatable({
_name = "KoboldWorldInfoFolderSelector",
}, metawrapper)
local KoboldWorldInfoFolderSelector_mt = setmetatable({}, metawrapper)
---@param u integer
---@return KoboldWorldInfoFolder?
function KoboldWorldInfoFolderSelector:finduid(u)
if not check_validity(self) or type(u) ~= "number" then
return
end
local query = _python.as_attrgetter(bridged.koboldai_vars.wifolders_d).get(u)
if query == nil then
return
end
local folder = deepcopy(KoboldWorldInfoFolder)
rawset(folder, "_uid", u)
return folder
end
---@return boolean
function KoboldWorldInfoFolderSelector:is_valid()
return true
end
---@param t KoboldWorldInfoFolderSelector
---@return integer
function KoboldWorldInfoFolderSelector_mt.__len(t)
if not check_validity(t) then
return 0
end
return _python.builtins.len(bridged.koboldai_vars.wifolders_l)
end
KoboldWorldInfoFolderSelector_mt._kobold_next = KoboldWorldInfoEntry_mt._kobold_next
KoboldWorldInfoFolderSelector_mt.__pairs = KoboldWorldInfoEntry_mt.__pairs
---@param t KoboldWorldInfoFolderSelector
---@return KoboldWorldInfoFolder?
function KoboldWorldInfoFolderSelector_mt.__index(t, k)
if not check_validity(t) or type(k) ~= "number" or math.tointeger(k) == nil or k < 1 or k > #t then
return
end
local folder = deepcopy(KoboldWorldInfoFolder)
rawset(folder, "_uid", math.tointeger(bridged.koboldai_vars.wifolders_l[k-1]))
return folder
end
---@param t KoboldWorldInfoFolderSelector
---@return KoboldWorldInfoFolderSelector
function KoboldWorldInfoFolderSelector_mt.__newindex(t, k, v)
if check_validity(t) or (type(k) == "number" and math.tointeger(k) ~= nil) then
error("Cannot write to integer indices of `"..rawget(t, "_name").."`")
end
return rawset(t, k, v)
end
----------------------------------------------------------------------------
---@class KoboldWorldInfo : KoboldWorldInfoFolder_base
local KoboldWorldInfo = setmetatable({
_name = "KoboldWorldInfo",
}, metawrapper)
local KoboldWorldInfo_mt = setmetatable({}, metawrapper)
KoboldWorldInfo.folders = KoboldWorldInfoFolderSelector
KoboldWorldInfo.finduid = KoboldWorldInfoFolder.finduid
KoboldWorldInfo.compute_context = KoboldWorldInfoFolder.compute_context
---@return boolean
function KoboldWorldInfo:is_valid()
return true
end
---@param t KoboldWorldInfo
---@return integer
function KoboldWorldInfo_mt.__len(t)
if not check_validity(t) then
return 0
end
return math.tointeger(_python.builtins.len(bridged.koboldai_vars.worldinfo)) - math.tointeger(_python.builtins.len(bridged.koboldai_vars.wifolders_l)) - 1
end
KoboldWorldInfo_mt._kobold_next = KoboldWorldInfoEntry_mt._kobold_next
KoboldWorldInfo_mt.__pairs = KoboldWorldInfoEntry_mt.__pairs
KoboldWorldInfo_mt.__index = KoboldWorldInfoFolder_mt.__index
KoboldWorldInfo_mt.__newindex = KoboldWorldInfoFolder_mt.__newindex
kobold.worldinfo = KoboldWorldInfo
--==========================================================================
-- Userscript API: Story chunks
--==========================================================================
---@class KoboldStoryChunk
---@field num integer
---@field content string
local KoboldStoryChunk = setmetatable({
_name = "KoboldStoryChunk",
}, metawrapper)
local KoboldStoryChunk_mt = setmetatable({}, metawrapper)
local KoboldStoryChunk_fields = {
num = false,
content = false,
}
---@generic K
---@param t KoboldStoryChunk
---@param k K
---@return K, any
function KoboldStoryChunk_mt._kobold_next(t, k)
k = (next(KoboldStoryChunk_fields, k))
return k, t[k]
end
---@param t KoboldStoryChunk
---@return function, KoboldStoryChunk, nil
function KoboldStoryChunk_mt.__pairs(t)
return next, t, nil
end
---@param t KoboldStoryChunk
function KoboldStoryChunk_mt.__index(t, k)
if k == "num" then
return rawget(t, "_num")
end
if k == "content" then
if rawget(t, "_num") == 0 then
if bridged.koboldai_vars.gamestarted then
local prompt = koboldbridge.userstate == "genmod" and bridged.koboldai_vars._prompt or bridged.koboldai_vars.prompt
return prompt
end
end
local actions = koboldbridge.userstate == "genmod" and bridged.koboldai_vars.actions
return _python.as_attrgetter(actions).get(math.tointeger(rawget(t, "_num")) - 1)
end
end
---@param t KoboldStoryChunk
function KoboldStoryChunk_mt.__newindex(t, k, v)
if k == "num" then
error("`"..rawget(t, "_name").."."..k.."` is a read-only attribute")
return
elseif k == "content" then
if type(v) ~= "string" then
error("`"..rawget(t, "_name").."."..k.."` must be a string; you attempted to set it to a "..type(v))
return
end
local _k = math.tointeger(rawget(t, "_num"))
if _k == nil or _k < 0 then
return
elseif _k == 0 and v == "" then
error("Attempted to set the prompt chunk's content to the empty string; this is not allowed")
return
end
local actions = koboldbridge.userstate == "genmod" and bridged.koboldai_vars.actions
if _k ~= 0 and _python.as_attrgetter(actions).get(_k-1) == nil then
return
end
bridged.set_chunk(_k, v)
maybe_require_regeneration()
return t
end
end
----------------------------------------------------------------------------
---@class KoboldStory_base
---@type table<integer, KoboldStoryChunk>
local _ = {}
---@class KoboldStory : KoboldStory_base
local KoboldStory = setmetatable({
_name = "KoboldStory",
}, metawrapper)
local KoboldStory_mt = setmetatable({}, metawrapper)
---@return fun(): KoboldStoryChunk, table, nil
function KoboldStory:forward_iter()
local actions = koboldbridge.userstate == "genmod" and bridged.koboldai_vars.actions
local nxt, iterator = _python.iter(actions)
local run_once = false
local function f()
if not bridged.koboldai_vars.gamestarted then
return
end
local chunk = deepcopy(KoboldStoryChunk)
local _k
if not run_once then
_k = -1
run_once = true
else
_k = nxt(iterator)
end
if _k == nil then
return
else
_k = math.tointeger(_k) + 1
end
rawset(chunk, "_num", _k)
return chunk
end
return f, {}, nil
end
---@return fun(): KoboldStoryChunk, table, nil
function KoboldStory:reverse_iter()
local actions = koboldbridge.userstate == "genmod" and bridged.koboldai_vars.actions
local nxt, iterator = _python.iter(_python.builtins.reversed(actions))
local last_run = false
local function f()
if not bridged.koboldai_vars.gamestarted or last_run then
return
end
local chunk = deepcopy(KoboldStoryChunk)
local _k = nxt(iterator)
if _k == nil then
_k = 0
last_run = true
else
_k = math.tointeger(_k) + 1
end
rawset(chunk, "_num", _k)
return chunk
end
return f, {}, nil
end
---@param t KoboldStory
function KoboldStory_mt.__pairs(t)
return function() return nil end, t, nil
end
---@param t KoboldStory
function KoboldStory_mt.__index(t, k)
if type(k) == "number" and math.tointeger(k) ~= nil then
local chunk = deepcopy(KoboldStoryChunk)
rawset(chunk, "_num", math.tointeger(k))
if chunk.content == nil then
return nil
end
return chunk
end
end
---@param t KoboldStory
function KoboldStory_mt.__newindex(t, k, v)
error("`"..rawget(t, "_name").."` is a read-only class")
end
kobold.story = KoboldStory
--==========================================================================
-- Userscript API: Settings
--==========================================================================
---@class KoboldSettings_base
---@type table<string, any>
local _ = {}
---@class KoboldSettings : KoboldSettings_base
---@field numseqs integer
---@field genamt integer
---@field anotedepth integer
---@field settemp number
---@field settopp number
---@field settopk integer
---@field settfs number
---@field settypical number
---@field settopa number
---@field setreppen number
---@field setreppenslope number
---@field setreppenrange number
---@field settknmax integer
---@field setwidepth integer
---@field setuseprompt boolean
---@field setadventure boolean
---@field setdynamicscan boolean
---@field setnopromptgen boolean
---@field setrngpersist boolean
---@field temp number
---@field topp number
---@field topk integer
---@field top_p number
---@field top_k integer
---@field tfs number
---@field typical number
---@field topa number
---@field reppen number
---@field reppenslope number
---@field reppenrange number
---@field tknmax integer
---@field widepth integer
---@field useprompt boolean
---@field adventure boolean
---@field dynamicscan boolean
---@field nopromptgen boolean
---@field rngpersist boolean
---@field frmttriminc boolean
---@field frmtrmblln boolean
---@field frmtrmspch boolean
---@field frmtadsnsp boolean
---@field frmtsingleline boolean
---@field triminc boolean
---@field rmblln boolean
---@field rmspch boolean
---@field adsnsp boolean
---@field singleline boolean
---@field chatmode boolean
---@field chatname string
local KoboldSettings = setmetatable({
_name = "KoboldSettings",
}, metawrapper)
local KoboldSettings_mt = setmetatable({}, metawrapper)
---@generic K
---@param t KoboldSettings
---@param k K
---@return K, any
function KoboldSettings_mt._kobold_next(t, k)
local v
repeat
k, v = next()
until type(k) ~= "string" or k ~= "_name"
return k, v
end
---@param t KoboldSettings
---@return function, KoboldSettings, nil
function KoboldSettings_mt.__pairs(t)
return next, t, nil
end
---@param t KoboldSettings
---@return any, boolean
function KoboldSettings_mt.__index(t, k)
if type(k) ~= "string" then
return
end
if k == "genamt" or k == "output" or k == "setoutput" then
return math.tointeger(bridged.get_genamt()), true
elseif k == "numseqs" or k == "numseq" or k == "setnumseq" then
return math.tointeger(bridged.get_numseqs()), true
elseif bridged.has_setting(k) then
return bridged.get_setting(k), true
else
return nil, false
end
end
---@param t KoboldSettings_base
function KoboldSettings_mt.__newindex(t, k, v)
if (k == "genamt" or k == "output" or k == "setoutput") and type(v) == "number" and math.tointeger(v) ~= nil and v >= 0 then
bridged.set_genamt(v)
koboldbridge.resend_settings_required = true
elseif (k == "numseqs" or k == "numseq" or k == "setnumseq") and type(v) == "number" and math.tointeger(v) ~= nil and v >= 1 then
if koboldbridge.userstate == "genmod" then
error("Cannot set numseqs from a generation modifier")
return
end
bridged.set_numseqs(v)
koboldbridge.resend_settings_required = true
elseif type(k) == "string" and bridged.has_setting(k) and type(v) == type(bridged.get_setting(k)) then
if bridged.set_setting(k, v) == true then
maybe_require_regeneration()
end
koboldbridge.resend_settings_required = true
end
return t
end
kobold.settings = KoboldSettings
--==========================================================================
-- Userscript API: Memory / Author's Note
--==========================================================================
---@param t KoboldLib
---@return string
function KoboldLib_getters.memory(t)
return bridged.get_memory()
end
---@param t KoboldLib
---@param v string
---@return KoboldLib
function KoboldLib_setters.memory(t, v)
if type(v) ~= "string" then
error("`KoboldLib.memory` must be a string; you attempted to set it to a "..type(v))
return
end
maybe_require_regeneration()
bridged.set_memory(v)
end
---@param t KoboldLib
---@return string
function KoboldLib_getters.authorsnote(t)