From eca26bf0264666fadf90223abf6a684507a651c5 Mon Sep 17 00:00:00 2001 From: Bishoy-at-pieces Date: Mon, 10 Jun 2024 19:28:16 +0300 Subject: [PATCH 1/3] fix settings issue --- settings.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/settings.py b/settings.py index 989090f..861d270 100644 --- a/settings.py +++ b/settings.py @@ -1,8 +1,6 @@ import pieces_os_client as pos_client import sublime -from typing import Optional,Dict,Union -import urllib -import json +from typing import Dict from . import __version__ @@ -59,6 +57,9 @@ def host_init(cls): It then creates the WebSocket base URL and defines the WebSocket URLs for different API endpoints. """ cls.host = cls.settings.get('host') + if not isinstance(cls.host,str): + return sublime.error_message("Invalid host") + if not cls.host: if 'linux' == sublime.platform(): cls.host = "http://localhost:5323" @@ -89,7 +90,7 @@ def models_init(cls): models = cls.get_models_ids() cls.model_name = cls.settings.get("model") - cls.model_id = models.get(cls.model_name,None) + cls.model_id = models.get(str(cls.model_name)) if not cls.model_id: cls.model_id = models["GPT-3.5-turbo Chat Model"] @@ -124,15 +125,15 @@ def get_application(cls)-> pos_client.Application: @classmethod def get_models_ids(cls) -> Dict[str, str]: if cls.models: - return models + return cls.models api_instance = pos_client.ModelsApi(cls.api_client) api_response = api_instance.models_snapshot() - models = {model.name: model.id for model in api_response.iterable if model.cloud or model.downloaded} # getting the models that are available in the cloud or is downloaded + cls.models = {model.name: model.id for model in api_response.iterable if model.cloud or model.downloaded} # getting the models that are available in the cloud or is downloaded - return models + return cls.models @classmethod From 151072d70314f8cb273942c6e7043156d825bc92 Mon Sep 17 00:00:00 2001 From: Bishoy-at-pieces Date: Mon, 10 Jun 2024 20:00:14 +0300 Subject: [PATCH 2/3] change localhost --- settings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/settings.py b/settings.py index 861d270..d415f67 100644 --- a/settings.py +++ b/settings.py @@ -62,9 +62,9 @@ def host_init(cls): if not cls.host: if 'linux' == sublime.platform(): - cls.host = "http://localhost:5323" + cls.host = "http://127.0.0.1:5323" else: - cls.host = "http://localhost:1000" + cls.host = "http://127.0.0.1:1000" ws_base_url = cls.host.replace('http','ws') From ccd0b119fefd60e3f79be57150a960b522c9ff3a Mon Sep 17 00:00:00 2001 From: Bishoy-at-pieces Date: Mon, 10 Jun 2024 20:31:57 +0300 Subject: [PATCH 3/3] fix the reload and changes to the loading settings --- Pieces.sublime-settings | 2 +- main.py | 23 +++++++++++++---------- misc/reload_command.py | 3 +-- settings.py | 34 +++++++++++++++++++--------------- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/Pieces.sublime-settings b/Pieces.sublime-settings index 84123cd..ea19279 100644 --- a/Pieces.sublime-settings +++ b/Pieces.sublime-settings @@ -1,5 +1,5 @@ { - // "host": "http:localhost/1000" + "host": "", "model":"GPT-3.5-turbo Chat Model" // "GPT-4o Chat Model", // "GPT-4 Chat Model", diff --git a/main.py b/main.py index 6956ea5..6e42879 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,6 @@ from .settings import PiecesSettings import sublime -import asyncio # load the commands from .assets import * @@ -13,9 +12,9 @@ from .misc import * from .base_websocket import BaseWebsocket -PiecesSettings.host_init() # Intilize the hosts url -def startup(): + +def startup(settings_model): pieces_version = open_pieces_os() @@ -24,15 +23,11 @@ def startup(): else: if version_check(): PiecesSettings.is_loaded = True - PiecesSettings.models_init() # initilize the models PiecesSettings.get_application() print_version_details(pieces_version, __version__) - - settings = sublime.load_settings('Pieces.sublime-settings') - - settings.add_on_change("PIECES_SETTINGS",PiecesSettings.on_settings_change) - + + PiecesSettings.models_init(settings_model) # Intilize the models # WEBSOCKETS: @@ -45,7 +40,15 @@ def startup(): def plugin_loaded(): - sublime.set_timeout_async(startup,0) + global settings # Set it to global to use + + settings = sublime.load_settings('Pieces.sublime-settings') + host = settings.get("host") + model = settings.get('model') + settings.add_on_change("PIECES_SETTINGS",PiecesSettings.on_settings_change) + PiecesSettings.host_init(host) # Intilize the hosts url + + sublime.set_timeout_async(lambda : startup(model) ,0) def plugin_unloaded(): diff --git a/misc/reload_command.py b/misc/reload_command.py index f793921..b5e63f7 100644 --- a/misc/reload_command.py +++ b/misc/reload_command.py @@ -13,8 +13,7 @@ def run(self): def reload_async(): if PiecesSettings.get_health(): try: - PiecesSettings.models_init() - PiecesSettings.host_init() + PiecesSettings.on_settings_change(all = True) BaseWebsocket.reconnect_all() PiecesSettings.is_loaded = True sublime.status_message(f"Reloading [completed]") diff --git a/settings.py b/settings.py index d415f67..d2f7873 100644 --- a/settings.py +++ b/settings.py @@ -15,8 +15,7 @@ class PiecesSettings: api_client = None _is_loaded = False # is the plugin loaded - # Load the settings from 'pieces.sublime-settings' file using Sublime Text API - settings = sublime.load_settings('Pieces.sublime-settings') + @property @@ -49,18 +48,15 @@ def get_health(cls): @classmethod - def host_init(cls): + def host_init(cls,host): """ Initialize the host URL for the API connection. This method sets the host URL based on the configuration settings. If the host URL is not provided in the settings, it defaults to a specific URL based on the platform. It then creates the WebSocket base URL and defines the WebSocket URLs for different API endpoints. """ - cls.host = cls.settings.get('host') - if not isinstance(cls.host,str): - return sublime.error_message("Invalid host") - - if not cls.host: + cls.host = host + if not host: if 'linux' == sublime.platform(): cls.host = "http://127.0.0.1:5323" else: @@ -80,7 +76,7 @@ def host_init(cls): @classmethod - def models_init(cls): + def models_init(cls,model): """ Initialize the model ID for the class using the specified settings. @@ -89,7 +85,7 @@ def models_init(cls): """ models = cls.get_models_ids() - cls.model_name = cls.settings.get("model") + cls.model_name = model cls.model_id = models.get(str(cls.model_name)) if not cls.model_id: @@ -97,11 +93,19 @@ def models_init(cls): @classmethod - def on_settings_change(cls): - if cls.host != cls.settings.get('host'): - cls.host_init() - if cls.model_name != cls.settings.get("model"): - cls.models_init() + def on_settings_change(cls,all = False): + """ + all parameter means to update everything not the changes + """ + settings = sublime.load_settings("Pieces.sublime-settings") # Reload the settings + host = settings.get('host') + model = settings.get("model") + if cls.host != host or all: + cls.host_init(host = host) + cls.models_init(model = model) + + if cls.model_name != model or all: + cls.models_init(model = model)