@@ -76,13 +76,14 @@ def add_output(self, output: str, message_type: str = "response"):
7676 "type" : message_type
7777 }
7878 self .output_queue .put (timestamped_output , block = False )
79- except :
79+ except Exception as e :
8080 # Queue is full, remove oldest item
81+ logger .warning (f"Output queue full, attempting to remove oldest item: { e } " )
8182 try :
8283 self .output_queue .get_nowait ()
8384 self .output_queue .put (timestamped_output , block = False )
84- except :
85- pass
85+ except Exception as e2 :
86+ logger . warning ( f"Failed to add output to queue even after removing oldest item: { e2 } " )
8687
8788 def update_activity (self ):
8889 """Update last activity timestamp"""
@@ -125,7 +126,7 @@ class ChatterlangServer:
125126
126127 def __init__ (
127128 self ,
128- host : str = "0.0.0.0 " ,
129+ host : str = "localhost " ,
129130 port : int = 9999 ,
130131 api_key : str = "your-secret-key-here" ,
131132 require_auth : bool = False ,
@@ -637,11 +638,11 @@ def _get_stream_interface(self) -> str:
637638
638639 form_fields = self ._generate_form_fields ()
639640
640- return f '''
641+ template = '''
641642 <!DOCTYPE html>
642643 <html>
643644 <head>
644- <title>{ self . title } - Stream</title>
645+ <title>{title} - Stream</title>
645646 <style>
646647 * {{
647648 margin: 0;
@@ -983,7 +984,7 @@ def _get_stream_interface(self) -> str:
983984 </head>
984985 <body>
985986 <div class="header">
986- <h1>{ self . form_config . title } </h1>
987+ <h1>{form_config_title }</h1>
987988 </div>
988989
989990 <div class="main-container">
@@ -1169,7 +1170,7 @@ def _get_stream_interface(self) -> str:
11691170 }}
11701171
11711172 // Add user message to chat immediately for instant feedback
1172- const displayProperty = '{ self . display_property } ' || Object.keys(data)[0];
1173+ const displayProperty = '{display_property}' || Object.keys(data)[0];
11731174 const userMessage = data[displayProperty] || JSON.stringify(data);
11741175 lastUserMessage = userMessage; // Store to detect duplicates from server
11751176 addMessage(userMessage, 'user', new Date().toISOString());
@@ -1228,6 +1229,34 @@ def _get_stream_interface(self) -> str:
12281229 </body>
12291230 </html>
12301231 '''
1232+ return template .format (
1233+ title = self .title ,
1234+ height = height ,
1235+ form_style = form_style ,
1236+ input_bg = input_bg ,
1237+ text_color = text_color ,
1238+ border_color = border_color ,
1239+ button_bg = button_bg ,
1240+ button_hover = button_hover ,
1241+ form_fields = form_fields ,
1242+ auth_header = auth_header ,
1243+ form_config_title = self .form_config .title ,
1244+ bg_color = bg_color ,
1245+ output_bg = output_bg ,
1246+ main_container_style = main_container_style ,
1247+ form_panel_width = form_panel_width ,
1248+ form_panel_style = form_panel_style ,
1249+ chat_panel_style = chat_panel_style ,
1250+ user_msg_bg = user_msg_bg ,
1251+ response_msg_bg = response_msg_bg ,
1252+ output_text = output_text ,
1253+ error_msg_bg = error_msg_bg ,
1254+ controls_style = controls_style ,
1255+ form_panel_class = form_panel_class ,
1256+ chat_controls = chat_controls ,
1257+ standalone_controls = standalone_controls ,
1258+ display_property = self .display_property
1259+ )
12311260
12321261 def _get_html_interface (self ) -> str :
12331262 """Generate HTML interface with configurable form"""
@@ -1270,11 +1299,11 @@ def _get_html_interface(self) -> str:
12701299
12711300 form_fields = self ._generate_form_fields ()
12721301
1273- return f '''
1302+ template2 = '''
12741303 <!DOCTYPE html>
12751304 <html>
12761305 <head>
1277- <title>{ self . title } </title>
1306+ <title>{title}</title>
12781307 <style>
12791308 * {{
12801309 margin: 0;
@@ -1483,10 +1512,10 @@ def _get_html_interface(self) -> str:
14831512 <body>
14841513 <div class="main-content">
14851514 <div class="info-section">
1486- <h1>{ self . title } </h1>
1515+ <h1>{title}</h1>
14871516 <p>Submit JSON data using the form below or send POST requests to:</p>
1488- <div class="endpoint-info">POST http://{ self . host } :{ self . port } /process</div>
1489- { "<p>Authentication required: Include 'X-API-Key' header</p>" if self . require_auth else "" }
1517+ <div class="endpoint-info">POST http://{host}:{port}/process</div>
1518+ {require_auth_text }
14901519 <p>View API documentation at: <a href="/docs">/docs</a></p>
14911520 <p>View streaming interface at: <a href="/stream">/stream</a></p>
14921521 </div>
@@ -1502,7 +1531,7 @@ def _get_html_interface(self) -> str:
15021531 <div class="form-panel" id="formPanel">
15031532 <div class="form-container">
15041533 <div class="form-header">
1505- <h3>{ self . form_config . title } </h3>
1534+ <h3>{form_config_title }</h3>
15061535 </div>
15071536
15081537 {auth_header}
@@ -1643,6 +1672,22 @@ def _get_html_interface(self) -> str:
16431672 </body>
16441673 </html>
16451674 '''
1675+ return template2 .format (
1676+ title = self .title ,
1677+ height = height ,
1678+ form_style = form_style ,
1679+ input_bg = input_bg ,
1680+ text_color = text_color ,
1681+ border_color = border_color ,
1682+ button_bg = button_bg ,
1683+ button_hover = button_hover ,
1684+ form_fields = form_fields ,
1685+ auth_header = auth_header ,
1686+ form_config_title = self .form_config .title ,
1687+ host = self .host ,
1688+ port = self .port ,
1689+ require_auth_text = '<p>Authentication required: Include "X-API-Key" header</p>' if self .require_auth else ''
1690+ )
16461691
16471692 def set_processor_function (self , func : Callable [[Dict [str , Any ]], Any ]):
16481693 """Set the function used to process incoming JSON data"""
@@ -1696,7 +1741,7 @@ def load_form_config(config_path: str) -> Dict[str, Any]:
16961741class ChatterlangServerSegment (AbstractSource ):
16971742 """Segment for receiving JSON data via FastAPI with configurable form"""
16981743
1699- def __init__ (self , port : Union [int ,str ] = 9999 , host : str = "0.0.0.0 " ,
1744+ def __init__ (self , port : Union [int ,str ] = 9999 , host : str = "localhost " ,
17001745 api_key : str = None , require_auth : bool = False ,
17011746 form_config : Union [str , Dict [str , Any ]] = None ):
17021747 super ().__init__ ()
@@ -1776,8 +1821,8 @@ def go():
17761821 parser = argparse .ArgumentParser (description = 'FastAPI JSON Data Receiver with Configurable Form' )
17771822 parser .add_argument ('-p' , '--port' , type = int , default = 2025 ,
17781823 help = 'Port to listen on (default: 2025)' )
1779- parser .add_argument ('-o' , '--host' , default = '0.0.0.0 ' ,
1780- help = 'Host to bind to (default: 0.0.0.0 )' )
1824+ parser .add_argument ('-o' , '--host' , default = 'localhost ' ,
1825+ help = 'Host to bind to (default: localhost )' )
17811826 parser .add_argument ('--api-key' , help = 'Set API key for authentication' )
17821827 parser .add_argument ('--require-auth' , action = 'store_true' ,
17831828 help = 'Require API key authentication' )
0 commit comments