mirror of
https://github.com/KoboldAI/KoboldAI-Client.git
synced 2025-06-05 21:59:24 +02:00
Allow user input to be modified from Lua
Also adds some handlers in the Lua code for when the game is not started yet
This commit is contained in:
15
aiserver.py
15
aiserver.py
@@ -1746,9 +1746,9 @@ def actionsubmit(data, actionmode=0, force_submit=False):
|
|||||||
if(not vars.gamestarted):
|
if(not vars.gamestarted):
|
||||||
vars.submission = data
|
vars.submission = data
|
||||||
execute_inmod()
|
execute_inmod()
|
||||||
|
data = vars.submission
|
||||||
if(not force_submit and len(data.strip()) == 0):
|
if(not force_submit and len(data.strip()) == 0):
|
||||||
set_aibusy(0)
|
assert False
|
||||||
return
|
|
||||||
# Start the game
|
# Start the game
|
||||||
vars.gamestarted = True
|
vars.gamestarted = True
|
||||||
if(not vars.noai):
|
if(not vars.noai):
|
||||||
@@ -1776,13 +1776,14 @@ def actionsubmit(data, actionmode=0, force_submit=False):
|
|||||||
set_aibusy(0)
|
set_aibusy(0)
|
||||||
emit('from_server', {'cmd': 'scrolldown', 'data': ''}, broadcast=True)
|
emit('from_server', {'cmd': 'scrolldown', 'data': ''}, broadcast=True)
|
||||||
else:
|
else:
|
||||||
|
# Apply input formatting & scripts before sending to tokenizer
|
||||||
|
if(vars.actionmode == 0):
|
||||||
|
data = applyinputformatting(data)
|
||||||
|
vars.submission = data
|
||||||
|
execute_inmod()
|
||||||
|
data = vars.submission
|
||||||
# Dont append submission if it's a blank/continue action
|
# Dont append submission if it's a blank/continue action
|
||||||
if(data != ""):
|
if(data != ""):
|
||||||
# Apply input formatting & scripts before sending to tokenizer
|
|
||||||
if(vars.actionmode == 0):
|
|
||||||
data = applyinputformatting(data)
|
|
||||||
vars.submission = data
|
|
||||||
execute_inmod()
|
|
||||||
# Store the result in the Action log
|
# Store the result in the Action log
|
||||||
if(len(vars.prompt.strip()) == 0):
|
if(len(vars.prompt.strip()) == 0):
|
||||||
vars.prompt = data
|
vars.prompt = data
|
||||||
|
26
bridge.lua
26
bridge.lua
@@ -606,7 +606,9 @@ return function(_python, _bridged)
|
|||||||
end
|
end
|
||||||
if k == "content" then
|
if k == "content" then
|
||||||
if rawget(t, "_num") == 0 then
|
if rawget(t, "_num") == 0 then
|
||||||
return bridged.vars.prompt
|
if bridged.vars.gamestarted then
|
||||||
|
return bridged.vars.prompt
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return _python.as_attrgetter(bridged.vars.actions).get(math.tointeger(rawget(t, "_num")) - 1)
|
return _python.as_attrgetter(bridged.vars.actions).get(math.tointeger(rawget(t, "_num")) - 1)
|
||||||
end
|
end
|
||||||
@@ -656,6 +658,9 @@ return function(_python, _bridged)
|
|||||||
local nxt, iterator = _python.iter(bridged.vars.actions)
|
local nxt, iterator = _python.iter(bridged.vars.actions)
|
||||||
local run_once = false
|
local run_once = false
|
||||||
local f = function()
|
local f = function()
|
||||||
|
if not bridged.vars.gamestarted then
|
||||||
|
return
|
||||||
|
end
|
||||||
local chunk = deepcopy(KoboldStoryChunk)
|
local chunk = deepcopy(KoboldStoryChunk)
|
||||||
local _k
|
local _k
|
||||||
if not run_once then
|
if not run_once then
|
||||||
@@ -665,7 +670,7 @@ return function(_python, _bridged)
|
|||||||
_k = nxt(iterator)
|
_k = nxt(iterator)
|
||||||
end
|
end
|
||||||
if _k == nil then
|
if _k == nil then
|
||||||
return nil
|
return
|
||||||
else
|
else
|
||||||
_k = math.tointeger(_k) + 1
|
_k = math.tointeger(_k) + 1
|
||||||
end
|
end
|
||||||
@@ -680,8 +685,8 @@ return function(_python, _bridged)
|
|||||||
local nxt, iterator = _python.iter(_python.builtins.reversed(bridged.vars.actions))
|
local nxt, iterator = _python.iter(_python.builtins.reversed(bridged.vars.actions))
|
||||||
local last_run = false
|
local last_run = false
|
||||||
local f = function()
|
local f = function()
|
||||||
if last_run then
|
if not bridged.vars.gamestarted or last_run then
|
||||||
return nil
|
return
|
||||||
end
|
end
|
||||||
local chunk = deepcopy(KoboldStoryChunk)
|
local chunk = deepcopy(KoboldStoryChunk)
|
||||||
local _k = nxt(iterator)
|
local _k = nxt(iterator)
|
||||||
@@ -704,7 +709,7 @@ return function(_python, _bridged)
|
|||||||
|
|
||||||
---@param t KoboldStory
|
---@param t KoboldStory
|
||||||
function KoboldStory_mt.__index(t, k)
|
function KoboldStory_mt.__index(t, k)
|
||||||
if k == nil or (type(k) == "number" and math.tointeger(k) ~= nil) then
|
if type(k) == "number" and math.tointeger(k) ~= nil then
|
||||||
local chunk = deepcopy(KoboldStoryChunk)
|
local chunk = deepcopy(KoboldStoryChunk)
|
||||||
rawset(chunk, "_num", math.tointeger(k))
|
rawset(chunk, "_num", math.tointeger(k))
|
||||||
if chunk.content == nil then
|
if chunk.content == nil then
|
||||||
@@ -851,7 +856,16 @@ return function(_python, _bridged)
|
|||||||
---@param t KoboldLib
|
---@param t KoboldLib
|
||||||
---@param v string
|
---@param v string
|
||||||
function KoboldLib_setters.submission(t, v)
|
function KoboldLib_setters.submission(t, v)
|
||||||
error("`KoboldLib.submission` is a read-only attribute")
|
if koboldbridge.userstate ~= "inmod" then
|
||||||
|
error("Cannot write to `KoboldLib.submission` from outside of an input modifier")
|
||||||
|
return
|
||||||
|
elseif type(v) ~= "string" then
|
||||||
|
error("`KoboldLib.submission` must be a string; you attempted to set it to a " .. type(v))
|
||||||
|
return
|
||||||
|
elseif not bridged.vars.gamestarted and v == "" then
|
||||||
|
error("`KoboldLib.submission` must not be set to the empty string when the story is empty")
|
||||||
|
end
|
||||||
|
bridged.vars.submission = v
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user