Skip to content

Commit

Permalink
fix the reload and changes to the loading settings
Browse files Browse the repository at this point in the history
  • Loading branch information
bishoy-at-pieces committed Jun 10, 2024
1 parent 151072d commit ccd0b11
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Pieces.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
// "host": "http:localhost/1000"
"host": "",
"model":"GPT-3.5-turbo Chat Model"
// "GPT-4o Chat Model",
// "GPT-4 Chat Model",
Expand Down
23 changes: 13 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from .settings import PiecesSettings

import sublime
import asyncio

# load the commands
from .assets import *
Expand All @@ -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()


Expand All @@ -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:
Expand All @@ -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():
Expand Down
3 changes: 1 addition & 2 deletions misc/reload_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
Expand Down
34 changes: 19 additions & 15 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -89,19 +85,27 @@ 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:
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)



Expand Down

0 comments on commit ccd0b11

Please sign in to comment.