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 989090f..d2f7873 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__ @@ -17,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 @@ -51,19 +48,19 @@ 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 cls.host: + cls.host = host + if not 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') @@ -79,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. @@ -88,19 +85,27 @@ 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_name = model + cls.model_id = models.get(str(cls.model_name)) if not cls.model_id: cls.model_id = models["GPT-3.5-turbo Chat Model"] @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) @@ -124,15 +129,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