Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix settings issue #84

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 26 additions & 21 deletions settings.py
Original file line number Diff line number Diff line change
@@ -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__

Expand All @@ -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
Expand Down Expand Up @@ -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')

Expand All @@ -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.

Expand All @@ -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)



Expand All @@ -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
Expand Down