Skip to content

Commit

Permalink
command_endpoints: sanitize result
Browse files Browse the repository at this point in the history
A command endpoint that inserts newlines in the speech result cause the
result to be printed outside of the display. We do not want to deal with
this in the Willow C code, and as we will eventually make WAS command
mode the default and rip out endpoint support from Willow, we can simply
do this in WAS.

As we're using a pydantic model for the command endpoint result, we can
easily do this with a sanitize function in the CommandEndpointResult
model, and call this when we initialize the CommandEndpointResponse.
We can't do it when we initialize the CommandEndpointResult, as we
sometimes change the speech attribute after init.
  • Loading branch information
stintel committed Dec 12, 2023
1 parent 19e4066 commit e46788e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/internal/command_endpoints/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict

class CommandEndpointConfigException(Exception):
"""Raised when an the command endpoint configuration is invalid
Expand Down Expand Up @@ -30,10 +30,19 @@ class CommandEndpointResult(BaseModel):
ok: bool = False
speech: str = "Error!"

def sanitize(self):
self.speech = self.speech.replace("\n", " ")
self.speech = self.speech.replace("\r", " ")
self.speech = self.speech.lstrip()


class CommandEndpointResponse(BaseModel):
result: CommandEndpointResult = None

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.result.sanitize()


class CommandEndpoint():
name = "WAS CommandEndpoint"
Expand Down

0 comments on commit e46788e

Please sign in to comment.