From 9559347f82529f43a0a2f239b7830722c66b93f3 Mon Sep 17 00:00:00 2001
From: Javalar <39832337+Javalar@users.noreply.github.com>
Date: Tue, 15 Jun 2021 00:59:08 -0400
Subject: [PATCH 01/14] Update or remove targeted chunks in Game Screen (#2)
---
.gitignore | 8 +++++-
aiserver.py | 59 +++++++++++++++++++++++++++++++++----------
static/application.js | 33 ++++++++++++++++++++++++
3 files changed, 86 insertions(+), 14 deletions(-)
diff --git a/.gitignore b/.gitignore
index ff83d6e7..33d3c719 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,10 @@ client.settings
# Ignore stories file except for test_story
stories/*
-!stories/sample_story.json
\ No newline at end of file
+!stories/sample_story.json
+
+# Ignore PyCharm project files.
+.idea
+
+# Ignore compiled Python files.
+*.pyc
diff --git a/aiserver.py b/aiserver.py
index 8dcec2af..c6b27672 100644
--- a/aiserver.py
+++ b/aiserver.py
@@ -9,6 +9,8 @@ from os import path, getcwd
import tkinter as tk
from tkinter import messagebox
import json
+from typing import Literal, Union
+
import requests
import html
@@ -666,12 +668,12 @@ def actionsubmit(data):
data = applyinputformatting(data)
# Store the result in the Action log
vars.actions.append(data)
-
+ update_story_chunk('last')
+
if(not vars.noai):
# Off to the tokenizer!
calcsubmit(data)
else:
- refresh_story()
set_aibusy(0)
#==================================================================#
@@ -687,7 +689,7 @@ def actionretry(data):
# Remove last action if possible and resubmit
if(len(vars.actions) > 0):
vars.actions.pop()
- refresh_story()
+ remove_story_chunk(len(vars.actions) + 1)
calcsubmit('')
#==================================================================#
@@ -698,8 +700,9 @@ def actionback():
return
# Remove last index of actions and refresh game screen
if(len(vars.actions) > 0):
+ action_index = len(vars.actions)
vars.actions.pop()
- refresh_story()
+ remove_story_chunk(len(vars.actions) + 1)
#==================================================================#
# Take submitted text and build the text to be given to generator
@@ -936,7 +939,7 @@ def genresult(genout):
# Add formatted text to Actions array and refresh the game screen
vars.actions.append(genout)
- refresh_story()
+ update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)})
#==================================================================#
@@ -955,9 +958,6 @@ def genselect(genout):
# Send sequences to UI for selection
emit('from_server', {'cmd': 'genseqs', 'data': genout})
-
- # Refresh story for any input text
- refresh_story()
#==================================================================#
# Send selected sequence to action log and refresh UI
@@ -966,7 +966,7 @@ def selectsequence(n):
if(len(vars.genseqs) == 0):
return
vars.actions.append(vars.genseqs[int(n)]["generated_text"])
- refresh_story()
+ update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)})
emit('from_server', {'cmd': 'hidegenseqs', 'data': ''})
vars.genseqs = []
@@ -1096,6 +1096,39 @@ def refresh_story():
text_parts.extend(('', html.escape(item), ''))
emit('from_server', {'cmd': 'updatescreen', 'data': formatforhtml(''.join(text_parts))})
+
+#==================================================================#
+# Signals the Game Screen to update one of the chunks
+#==================================================================#
+def update_story_chunk(idx: Union[int, Literal['last']]):
+ if idx == 'last':
+ if len(vars.actions) <= 1:
+ # In this case, we are better off just refreshing the whole thing as the
+ # prompt might not have been shown yet (with a "Generating story..."
+ # messsage instead).
+ refresh_story()
+ return
+
+ idx = len(vars.actions)
+
+ if idx == 0:
+ text = vars.prompt
+ else:
+ # Actions are 0 based, but in chunks 0 is the prompt.
+ # So the chunk index is one more than the corresponding action index.
+ text = vars.actions[idx - 1]
+
+ chunk_text = f'{formatforhtml(html.escape(text))}'
+ emit('from_server', {'cmd': 'updatechunk', 'data': {'index': idx, 'html': chunk_text, 'last': (idx == len(vars.actions))}})
+
+
+#==================================================================#
+# Signals the Game Screen to remove one of the chunks
+#==================================================================#
+def remove_story_chunk(idx: int):
+ emit('from_server', {'cmd': 'removechunk', 'data': idx})
+
+
#==================================================================#
# Sends the current generator settings to the Game Menu
#==================================================================#
@@ -1161,7 +1194,7 @@ def editsubmit(data):
vars.actions[vars.editln-1] = data
vars.mode = "play"
- refresh_story()
+ update_story_chunk(vars.editln)
emit('from_server', {'cmd': 'texteffect', 'data': vars.editln})
emit('from_server', {'cmd': 'editmode', 'data': 'false'})
@@ -1176,7 +1209,7 @@ def deleterequest():
else:
del vars.actions[vars.editln-1]
vars.mode = "play"
- refresh_story()
+ remove_story_chunk(vars.editln)
emit('from_server', {'cmd': 'editmode', 'data': 'false'})
#==================================================================#
@@ -1382,7 +1415,7 @@ def ikrequest(txt):
genout = req.json()["data"]["text"]
print("{0}{1}{2}".format(colors.CYAN, genout, colors.END))
vars.actions.append(genout)
- refresh_story()
+ update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)})
set_aibusy(0)
@@ -1432,7 +1465,7 @@ def oairequest(txt, min, max):
genout = req.json()["choices"][0]["text"]
print("{0}{1}{2}".format(colors.CYAN, genout, colors.END))
vars.actions.append(genout)
- refresh_story()
+ update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)})
set_aibusy(0)
diff --git a/static/application.js b/static/application.js
index e5ebc806..2cf66dfe 100644
--- a/static/application.js
+++ b/static/application.js
@@ -599,6 +599,39 @@ $(document).ready(function(){
setTimeout(function () {
$('#gamescreen').animate({scrollTop: $('#gamescreen').prop('scrollHeight')}, 1000);
}, 5);
+ } else if(msg.cmd == "updatechunk") {
+ const {index, html, last} = msg.data;
+ const existingChunk = game_text.children(`#n${index}`)
+ const newChunk = $(html);
+ if (existingChunk.length > 0) {
+ // Update existing chunk
+ existingChunk.before(newChunk);
+ existingChunk.remove();
+ } else {
+ // Append at the end
+ game_text.append(newChunk);
+ }
+ if(last) {
+ // Scroll to bottom of text if it's the last element
+ setTimeout(function () {
+ $('#gamescreen').animate({scrollTop: $('#gamescreen').prop('scrollHeight')}, 1000);
+ }, 5);
+ }
+ } else if(msg.cmd == "removechunk") {
+ let index = msg.data;
+ // Remove the chunk
+ game_text.children(`#n${index}`).remove()
+ // Shift all existing chunks by 1
+ index++;
+ while (true) {
+ const chunk = game_text.children(`#n${index}`)
+ if(chunk.length === 0) {
+ break;
+ }
+ const newIndex = index - 1;
+ chunk.attr('n', newIndex.toString()).attr('id', `n${newIndex}`);
+ index++;
+ }
} else if(msg.cmd == "setgamestate") {
// Enable or Disable buttons
if(msg.data == "ready") {
From 81aba7cba8070426ae09361763caa634a325030e Mon Sep 17 00:00:00 2001
From: Yves Dubois
Date: Tue, 15 Jun 2021 01:02:11 -0400
Subject: [PATCH 02/14] Fix typo
---
aiserver.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/aiserver.py b/aiserver.py
index c6b27672..6703fb53 100644
--- a/aiserver.py
+++ b/aiserver.py
@@ -1105,7 +1105,7 @@ def update_story_chunk(idx: Union[int, Literal['last']]):
if len(vars.actions) <= 1:
# In this case, we are better off just refreshing the whole thing as the
# prompt might not have been shown yet (with a "Generating story..."
- # messsage instead).
+ # message instead).
refresh_story()
return
From 62ad2f0228f199c668b5499bfc3344649cf97cac Mon Sep 17 00:00:00 2001
From: Gnome Ann <>
Date: Tue, 24 Aug 2021 18:32:48 -0400
Subject: [PATCH 03/14] Code indentation consistency
---
static/application.js | 24 ++++++++++-----------
static/custom.css | 50 +++++++++++++++++++++----------------------
templates/index.html | 2 +-
3 files changed, 38 insertions(+), 38 deletions(-)
diff --git a/static/application.js b/static/application.js
index 014cf86d..eb93a51c 100644
--- a/static/application.js
+++ b/static/application.js
@@ -813,11 +813,11 @@ $(document).ready(function(){
seqselmenu = $("#seqselmenu");
seqselcontents = $("#seqselcontents");
- // Connect to SocketIO server
- socket = io.connect(window.document.origin);
+ // Connect to SocketIO server
+ socket = io.connect(window.document.origin);
socket.on('from_server', function(msg) {
- if(msg.cmd == "connected") {
+ if(msg.cmd == "connected") {
// Connected to Server Actions
connected = true;
connect_status.html("Connected to KoboldAI Process!");
@@ -882,19 +882,19 @@ $(document).ready(function(){
}, 5);
}
} else if(msg.cmd == "removechunk") {
- let index = msg.data;
- // Remove the chunk
- game_text.children(`#n${index}`).remove()
+ let index = msg.data;
+ // Remove the chunk
+ game_text.children(`#n${index}`).remove()
// Shift all existing chunks by 1
index++;
- while (true) {
- const chunk = game_text.children(`#n${index}`)
+ while(true) {
+ const chunk = game_text.children(`#n${index}`)
if(chunk.length === 0) {
break;
}
- const newIndex = index - 1;
- chunk.attr('n', newIndex.toString()).attr('id', `n${newIndex}`);
- index++;
+ const newIndex = index - 1;
+ chunk.attr('n', newIndex.toString()).attr('id', `n${newIndex}`);
+ index++;
}
} else if(msg.cmd == "setgamestate") {
// Enable or Disable buttons
@@ -1093,7 +1093,7 @@ $(document).ready(function(){
} else if(msg.cmd == "runs_remotely") {
hide([button_loadfrfile, button_savetofile, button_import, button_importwi]);
}
- });
+ });
socket.on('disconnect', function() {
connected = false;
diff --git a/static/custom.css b/static/custom.css
index 70a23124..15ee3521 100644
--- a/static/custom.css
+++ b/static/custom.css
@@ -355,8 +355,8 @@ chunk, chunk * {
.colorfade, .colorfade * {
-moz-transition:color 1s ease-in;
- -o-transition:color 1s ease-in;
- -webkit-transition:color 1s ease-in;
+ -o-transition:color 1s ease-in;
+ -webkit-transition:color 1s ease-in;
transition:color 1s ease-in;
}
@@ -443,21 +443,21 @@ chunk, chunk * {
}
.helpicon {
- display: inline-block;
- font-family: sans-serif;
- font-weight: bold;
- text-align: center;
- width: 2.2ex;
- height: 2.4ex;
- font-size: 1.4ex;
- line-height: 1.8ex;
- border-radius: 1.2ex;
- margin-right: 4px;
- padding: 1px;
- color: #295071;
- background: #ffffff;
- border: 1px solid white;
- text-decoration: none;
+ display: inline-block;
+ font-family: sans-serif;
+ font-weight: bold;
+ text-align: center;
+ width: 2.2ex;
+ height: 2.4ex;
+ font-size: 1.4ex;
+ line-height: 1.8ex;
+ border-radius: 1.2ex;
+ margin-right: 4px;
+ padding: 1px;
+ color: #295071;
+ background: #ffffff;
+ border: 1px solid white;
+ text-decoration: none;
}
.helpicon:hover {
@@ -521,8 +521,8 @@ chunk, chunk * {
color: #ffffff;
-moz-transition: background-color 0.25s ease-in;
- -o-transition: background-color 0.25s ease-in;
- -webkit-transition: background-color 0.25s ease-in;
+ -o-transition: background-color 0.25s ease-in;
+ -webkit-transition: background-color 0.25s ease-in;
transition: background-color 0.25s ease-in;
}
@@ -532,12 +532,12 @@ chunk, chunk * {
}
.navbar .navbar-nav .nav-link:hover {
- border-radius: 5px;
+ border-radius: 5px;
background-color: #98bcdb;
}
.navbar .navbar-nav .nav-link:focus {
- border-radius: 5px;
+ border-radius: 5px;
background-color: #98bcdb;
}
@@ -603,8 +603,8 @@ chunk, chunk * {
color: #ffffff;
-moz-transition: background-color 0.25s ease-in;
- -o-transition: background-color 0.25s ease-in;
- -webkit-transition: background-color 0.25s ease-in;
+ -o-transition: background-color 0.25s ease-in;
+ -webkit-transition: background-color 0.25s ease-in;
transition: background-color 0.25s ease-in;
}
@@ -654,8 +654,8 @@ chunk, chunk * {
padding: 5px;
color: #ffffff;
-moz-transition: all 0.15s ease-in;
- -o-transition: all 0.15s ease-in;
- -webkit-transition: all 0.15s ease-in;
+ -o-transition: all 0.15s ease-in;
+ -webkit-transition: all 0.15s ease-in;
transition: all 0.15s ease-in;
}
diff --git a/templates/index.html b/templates/index.html
index 97eee72d..196b6038 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -246,7 +246,7 @@
Unsaved data will be lost.
Below you can input a genre suggestion for the AI to loosely base the story on (For example Horror or Cowboy).
-