Initial setup for IPC Client and logging

This commit is contained in:
Zvonimir Sabljic
2023-08-30 23:16:17 +02:00
parent 3db30d7904
commit 1418704186
4 changed files with 80 additions and 4 deletions

5
pilot/const/ipc.py Normal file
View File

@@ -0,0 +1,5 @@
MESSAGE_TYPE = {
'verbose': 'verbose',
'gpt_stream': 'gpt_stream',
'user_input_request': 'user_input_request'
}

View File

@@ -1,9 +1,12 @@
import os
import time
from termcolor import colored
from const.common import IGNORE_FOLDERS
from database.models.app import App
from database.database import get_app, delete_unconnected_steps_from, delete_all_app_development_data
from helpers.ipc import IPCClient
from const.ipc import MESSAGE_TYPE
from utils.questionary import styled_text
from helpers.files import get_files_content, clear_directory, update_file
from helpers.cli import build_directory_tree
@@ -51,6 +54,15 @@ class Project:
# if development_plan is not None:
# self.development_plan = development_plan
if '--external-log-process' in args:
self.ipc_client_instance = IPCClient()
print('..'*20)
self.log('\n------------------ STARTING NEW PROJECT ----------------------', 'verbose')
self.log(f"If you wish to continue with this project in future run:", 'verbose')
self.log(f'python main.py app_id={args["app_id"]}', 'verbose')
self.log('--------------------------------------------------------------\n', 'verbose')
def start(self):
self.project_manager = ProductOwner(self)
self.project_manager.get_project_description()
@@ -195,3 +207,14 @@ class Project:
return cbs[answer]()
elif answer != '':
return answer
def log(self, text, message_type, cb=None):
if self.ipc_client_instance is None or self.ipc_client_instance.client is None:
print(text)
else:
self.ipc_client_instance.send({
'type': MESSAGE_TYPE[message_type],
'content': str(text),
})
if cb is not None:
self.ipc_client_instance.listen(lambda response: cb(response['content']))

52
pilot/helpers/ipc.py Normal file
View File

@@ -0,0 +1,52 @@
# ipc.py
import socket
import json
import time
class IPCClient:
def __init__(self):
self.ready = False
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Connecting to the external process...")
try:
client.connect(('localhost', 8124))
self.client = client
print("Connected!")
except ConnectionRefusedError:
self.client = None
print("Connection refused, make sure you started the external process")
def handle_request(self, message_content):
print(f"Received request from the external process: {message_content}")
return message_content # For demonstration, we're just echoing back the content
def listen(self, cb):
if self.client is None:
print("Not connected to the external process!")
return
try:
while True:
data = self.client.recv(4096)
message = json.loads(data)
if message['type'] == 'request':
cb(message['content'])
if message['type'] == 'request':
response_content = self.handle_request(message['content'])
response = {
'type': 'response',
'content': response_content
}
self.client.sendall(json.dumps(response).encode('utf-8'))
time.sleep(0.1)
serialized_data = json.dumps(data)
self.client.sendall(serialized_data.encode('utf-8'))
finally:
self.client.close()
def send(self, data):
serialized_data = json.dumps(data)
self.client.sendall(serialized_data.encode('utf-8'))
time.sleep(0.1)

View File

@@ -49,8 +49,4 @@ def get_arguments():
if 'step' not in arguments:
arguments['step'] = None
print(colored('\n------------------ STARTING NEW PROJECT ----------------------', 'green', attrs=['bold']))
print(f"If you wish to continue with this project in future run:")
print(colored(f'python main.py app_id={arguments["app_id"]}', 'green', attrs=['bold']))
print(colored('--------------------------------------------------------------\n', 'green', attrs=['bold']))
return arguments