Skip to content

Commit

Permalink
fix portrait issue (due to deferred actions)
Browse files Browse the repository at this point in the history
brim and shanda are autonomous
  • Loading branch information
neph1 committed Dec 27, 2023
1 parent 68f25d7 commit 9e8b6cc
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 21 deletions.
19 changes: 8 additions & 11 deletions tale/llm/LivingNpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,12 @@ def autonomous_action(self) -> str:
if not action:
return None

print(f"Performing free form action: {action}")
defered_actions = []
if action.get('text', ''):
text = action['text']
tell_hash = llm_cache.cache_tell('{actor.title} says: "{response}"'.format(actor=self, response=text))
self._conversations.append(tell_hash)
if mud_context.config.custom_resources:
text = pad_text_for_avatar(text, self.title)
#if mud_context.config.custom_resources:
if action.get('target'):
target = self.location.search_living(action['target'])
if target:
Expand Down Expand Up @@ -249,20 +247,19 @@ def autonomous_action(self) -> str:


def _defer_result(self, action: str, verb: str="idle-action"):
self.deferred_actions.add(action)
if mud_context.config.custom_resources:
action = pad_text_for_avatar(action, self.title)
action = pad_text_for_avatar(text=action, npc_name=self.title)
else:
action = f"{self.title} : {action}"
self.deferred_actions.add(action)
mud_context.driver.defer(1.0, self.tell_action_deferred)

def tell_action_deferred(self):
if len(self.deferred_actions):
actions = '\n'.join(self.deferred_actions)
deferred_action = ParseResult(verb='idle-action', unparsed=actions, who_info=None)
self.tell_others(actions + '\n')
self.location._notify_action_all(deferred_action, actor=self)
self.deferred_actions.clear()
actions = '\n'.join(self.deferred_actions)
deferred_action = ParseResult(verb='idle-action', unparsed=actions, who_info=None)
self.tell_others(actions + '\n')
self.location._notify_action_all(deferred_action, actor=self)
self.deferred_actions.clear()

def _clear_quest(self):
self.quest = None
Expand Down
5 changes: 1 addition & 4 deletions tale/resources_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

from tale.image_gen.base_gen import ImageGeneratorBase


image_token = '<:>'

def pad_text_for_avatar(text: str, npc_name: str) -> str:
"""Pad text for NPC output."""
return npc_name +' <:> ' + text
return npc_name + ' <:> ' + text if npc_name else text
4 changes: 2 additions & 2 deletions tale/web/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function create_chat_container(npc_name) {

var userImage = document.createElement('img');
userImage.className = 'user-image';
userImage.src = npc_name.replace(/ /g, '_') + '.png';
userImage.src = npc_name.replace(/ /g, '_') + '.jpg';
userImage.alt = 'User Avatar';

var userNameDiv = document.createElement('div');
Expand All @@ -168,7 +168,7 @@ function create_chat_container(npc_name) {

function set_location_image(location_name) {
var locationImage = document.getElementById('location-image');
locationImage.src = 'resources/' + location_name.replace(/ /g, '_') + '.png';
locationImage.src = 'resources/' + location_name.replace(/ /g, '_') + '.jpg';
}

function updateImage() {
Expand Down
1 change: 0 additions & 1 deletion tale/web/story.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<link rel="stylesheet" type="text/css" href="static/style.css" />
<script type="application/javascript" src="static/eventsource.min.js"></script>
<script type="application/javascript" src="static/script.js"></script>
<meta charset="UTF-8">
</head>
<body onload="setup()">
<div id="tale-page">
Expand Down
4 changes: 4 additions & 0 deletions tale/web/web_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

def create_chat_container(text: str) -> str:
separated = text.split(dialogue_splitter)
if len(separated) != 2:
separated = text.split(' <:> ')
if len(separated) != 2:
return text
name = separated[0]
content = separated[1]
image_file = separated[0].lower().replace(' ', '_') + '.jpg'
Expand Down
9 changes: 8 additions & 1 deletion tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from wsgiref.simple_server import WSGIServer
from tale.player import PlayerConnection
from tale.tio.if_browser_io import HttpIo
from tale.web.web_utils import create_chat_container


class TestHttpIo:
Expand Down Expand Up @@ -34,4 +35,10 @@ def test_render_output_dialogue_token(self):
assert "chat-container" in result
assert '<div class="user-name" content="Bloated Murklin"></div>' in result
assert '<div class="text-field" type="text">Hello World!</div>' in result


def test_create_chat_container(self):
result = create_chat_container("Bloated Murklin <:> Hello World!")

assert "chat-container" in result
assert '<div class="user-name" content="Bloated Murklin"></div>' in result
assert '<div class="text-field" type="text">Hello World!</div>' in result
4 changes: 2 additions & 2 deletions tests/test_llm_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_idle_action(self):
json={'results':[{'text':'"sits down on a chair"'}]}, status=200)
self.llm_util._character.io_util.response = []
action = npc.idle_action()
assert(action == 'sits down on a chair\n')
assert(action == 'test : sits down on a chair\n')
assert(npc.deferred_actions.pop() == 'sits down on a chair\n')

@responses.activate
Expand All @@ -156,7 +156,7 @@ def test_do_react(self):
responses.add(responses.POST, self.dummy_backend_config['URL'] + self.dummy_backend_config['ENDPOINT'],
json={'results':[{'text':'"test happens back!"'}]}, status=200)
npc._do_react(action, npc2)
assert(npc.deferred_actions.unparsed == 'test happens back\n')
assert(npc.deferred_actions == {'test : test happens back\n'})

@responses.activate
def test_take_action(self):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_resource_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@



from tale.resources_utils import pad_text_for_avatar


class TestResourceUtils:

def test_pad_for_avatar(self):
"""Test padding for avatar."""
text = "This is a test."
npc_name = "Test NPC"
expected = "Test NPC <:> This is a test."
assert pad_text_for_avatar(text, npc_name) == expected

def test_pad_for_avatar_empty(self):
"""Test padding for avatar with empty text."""
text = ""
npc_name = "Test NPC"
expected = "Test NPC <:> "
assert pad_text_for_avatar(text, npc_name) == expected

def test_pad_for_avatar_empty_npc(self):
"""Test padding for avatar with empty NPC name."""
text = "This is a test."
npc_name = ""
expected = "This is a test."
assert pad_text_for_avatar(text, npc_name) == expected

0 comments on commit 9e8b6cc

Please sign in to comment.