@@ -3,9 +3,10 @@
import os
import threading
import time
import shutil
import tkinter as tk
import webbrowser
from tkinter import filedialog , messagebox
from tkinter import filedialog , messagebox , simpledialog
import customtkinter as ctk
@@ -1213,6 +1214,9 @@ class SettingsWindow(ctk.CTkToplevel):
self . model_download_dir . pack ( side = " left " , fill = " x " , expand = True )
ctk . CTkButton ( row , text = " Обзор " , width = 74 , height = 32 ,
command = self . _browse_download_dir ) . pack ( side = " left " , padx = ( 8 , 0 ) )
self . models_space_label = ctk . CTkLabel (
body , text = " " , font = ui_font ( 9 ) , text_color = c ( " text_faint " ) , anchor = " w " )
self . models_space_label . pack ( fill = " x " , padx = 20 , pady = ( 0 , 8 ) )
self . _section_title ( body , " Параметры загрузки по умолчанию " ,
" Используются, пока для модели не сохранён свой пресет " )
@@ -1300,6 +1304,7 @@ class SettingsWindow(ctk.CTkToplevel):
self . model_download_dir . insert (
0 , str ( conf . get ( " download_dir " )
or os . path . expanduser ( " ~/.local/share/xenith-studio/models " ) ) )
self . _refresh_models_space ( )
if hasattr ( self , " hf_token_entry " ) :
self . hf_token_entry . delete ( 0 , " end " )
self . hf_token_entry . insert ( 0 , str ( conf . get ( " hf_token " ) or " " ) )
@@ -1408,10 +1413,39 @@ class SettingsWindow(ctk.CTkToplevel):
threading . Thread ( target = task , daemon = True ) . start ( )
def _refresh_models_space ( self ) :
if not hasattr ( self , " models_space_label " ) :
return
path = ( self . model_download_dir . get ( ) . strip ( )
or os . path . expanduser ( " ~/.local/share/xenith-studio/models " ) )
path = os . path . abspath ( os . path . expanduser ( path ) )
try :
usage = shutil . disk_usage ( path )
free = usage . free / ( 1024 * * 3 )
total = usage . total / ( 1024 * * 3 )
self . models_space_label . configure (
text = f " Свободно { free : .1f } ГБ из { total : .1f } ГБ " ,
text_color = ( c ( " warn " ) if free < 2 else c ( " text_faint " ) ) ,
)
except Exception as e :
self . models_space_label . configure ( text = f " Папка недоступна: { e } " ,
text_color = c ( " danger " ) )
def _add_model_dir ( self ) :
path = filedialog . askdirectory ( title = " Папка с GGUF-моделями " )
try :
path = filedialog . askdirectory ( title = " Папка с GGUF-моделями " ,
parent = self , mustexist = True )
except Exception as e :
self . models_status . configure ( text = f " Не удалось выбрать папку: { e } " ,
text_color = c ( " danger " ) )
return
if not path :
return
path = os . path . abspath ( path )
if not os . access ( path , os . R_OK ) :
self . models_status . configure ( text = " Нет прав на чтение этой папки " ,
text_color = c ( " danger " ) )
return
current = self . model_dirs_box . get ( " 1.0 " , " end " ) . strip ( )
if path not in [ p . strip ( ) for p in current . splitlines ( ) if p . strip ( ) ] :
if current :
@@ -1420,11 +1454,18 @@ class SettingsWindow(ctk.CTkToplevel):
self . model_dirs_box . insert ( " 1.0 " , path )
def _browse_download_dir ( self ) :
path = filedialog . askdirectory ( title = " Каталог загрузок моделей " )
try :
path = filedialog . askdirectory ( title = " Каталог загрузок моделей " ,
parent = self , mustexist = True )
except Exception as e :
self . models_status . configure ( text = f " Не удалось выбрать папку: { e } " ,
text_color = c ( " danger " ) )
return
if not path :
return
self . model_download_dir . delete ( 0 , " end " )
self . model_download_dir . insert ( 0 , path )
self . model_download_dir . insert ( 0 , os . path . abspath ( path ) )
self . _refresh_models_space ( )
def _open_download_dir ( self ) :
path = os . path . expanduser ( self . model_download_dir . get ( ) . strip ( ) )
@@ -1462,8 +1503,22 @@ class SettingsWindow(ctk.CTkToplevel):
except Exception :
defaults [ " threads " ] = 0
defaults [ " flash_attn " ] = bool ( self . model_flash . get ( ) )
conf [ " download_dir " ] = ( self . model_download_dir . get ( ) . strip ( )
or os . path . expanduser ( " ~/.local/share/xenith-studio/models " ) )
download_dir = ( self . model_download_dir . get ( ) . strip ( )
or os . path . expanduser ( " ~/.local/share/xenith-studio/models " ) )
download_dir = os . path . abspath ( os . path . expanduser ( download_dir ) )
try :
os . makedirs ( download_dir , exist_ok = True )
if not os . path . isdir ( download_dir ) :
raise OSError ( " это не каталог " )
if not os . access ( download_dir , os . W_OK ) :
raise OSError ( " нет прав на запись " )
except Exception as e :
self . models_status . configure (
text = f " Папка загрузок недоступна: { e } " , text_color = c ( " danger " ) )
return
conf [ " download_dir " ] = download_dir
self . model_download_dir . delete ( 0 , " end " )
self . model_download_dir . insert ( 0 , download_dir )
conf [ " hf_token " ] = self . hf_token_entry . get ( ) . strip ( )
mode = self . hf_proxy_mode . get ( )
if mode == " Напрямую " :
@@ -1474,7 +1529,14 @@ class SettingsWindow(ctk.CTkToplevel):
conf [ " hf_proxy " ] = " auto "
try :
self . app . engine . save_config ( )
self . models_status . configure ( text = " Сохранено " , text_color = c ( " success " ) )
free = " "
try :
usage = shutil . disk_usage ( download_dir )
free = f " · свободно { usage . free / ( 1024 * * 3 ) : .1f } ГБ "
except Exception :
pass
self . models_status . configure ( text = " Сохранено " + free , text_color = c ( " success " ) )
self . _refresh_models_space ( )
except Exception as e :
self . models_status . configure ( text = f " Ошибка: { e } " , text_color = c ( " danger " ) )
return
@@ -1512,15 +1574,39 @@ class SettingsWindow(ctk.CTkToplevel):
wraplength = 560 )
self . mcp_hint . pack ( anchor = " w " , padx = 20 , pady = ( 0 , 10 ) )
ctk . CTkLabel ( frame , text = " Разрешённы е папки " , font = ui_font ( 11 ) ,
text_color = c ( " text_dim " ) ) . pack ( anchor = " w " , padx = 20 , pady = ( 8 , 4 ) )
self . roots_box = ctk . CTkTextbox ( frame , height = 120 , wrap = " none " , font = mono_font ( 10 ) )
self . roots_box . pack ( fill = " x " , padx = 20 , pady = ( 0 , 8 ) )
ctk . CTkLabel ( frame , text = " Рабочи е папки " , font = ui_font ( 11 ) ,
text_color = c ( " text_dim " ) ) . pack ( anchor = " w " , padx = 20 , pady = ( 8 , 2 ) )
ctk . CTkLabel (
frame ,
text = " Агент может читать и изменять файлы только внутри этих папок. "
" Пути можно вводить вручную, по одному в строке. " ,
font = ui_font ( 10 ) , text_color = c ( " text_faint " ) , anchor = " w " ,
justify = " left " , wraplength = 600 ,
) . pack ( anchor = " w " , padx = 20 , pady = ( 0 , 4 ) )
self . roots_box = ctk . CTkTextbox ( frame , height = 110 , wrap = " none " , font = mono_font ( 10 ) )
self . roots_box . pack ( fill = " x " , padx = 20 , pady = ( 0 , 4 ) )
self . mcp_folders_status = ctk . CTkLabel (
frame , text = " " , font = ui_font ( 9 ) , text_color = c ( " text_faint " ) ,
anchor = " w " , justify = " left " , wraplength = 600 ,
)
self . mcp_folders_status . pack ( fill = " x " , padx = 20 , pady = ( 0 , 8 ) )
row = ctk . CTkFrame ( frame , fg_color = " transparent " )
row . pack ( fill = " x " , padx = 20 , pady = ( 0 , 16 ) )
ctk . CTkButton ( row , text = " Добавить папку " , height = 32 ,
row . pack ( fill = " x " , padx = 20 , pady = ( 0 , 4 ) )
ctk . CTkButton ( row , text = " Добавить папку " , height = 30 ,
command = self . _add_mcp_root ) . pack ( side = " left " )
ctk . CTkButton ( row , text = " Сохранить " , height = 32 ,
ctk . CTkButton ( row , text = " Создать папку " , height = 30 ,
command = self . _create_mcp_root ) . pack ( side = " left " , padx = 6 )
ctk . CTkButton ( row , text = " Открыть " , height = 30 , width = 92 ,
command = self . _open_mcp_root ) . pack ( side = " left " )
ctk . CTkButton ( row , text = " Удалить " , height = 30 , width = 92 ,
command = self . _remove_mcp_root ) . pack ( side = " left " , padx = 6 )
row2 = ctk . CTkFrame ( frame , fg_color = " transparent " )
row2 . pack ( fill = " x " , padx = 20 , pady = ( 0 , 16 ) )
ctk . CTkButton ( row2 , text = " Проверить папки " , height = 30 , width = 140 ,
command = self . _validate_mcp_roots ) . pack ( side = " left " )
ctk . CTkButton ( row2 , text = " Сохранить " , height = 30 , width = 120 ,
fg_color = c ( " action_bg " ) , hover_color = c ( " action_hover " ) ,
text_color = c ( " action_fg " ) , font = ui_font ( 12 ) ,
command = self . _save_mcp ) . pack ( side = " left " , padx = 8 )
@@ -1543,37 +1629,182 @@ class SettingsWindow(ctk.CTkToplevel):
}
self . mcp_hint . configure ( text = hints . get ( mode , " " ) )
roots = conf . get ( " roots " ) or [ ]
self . roots_box . configure ( state = " normal " )
self . roots_box . delete ( " 1.0 " , " end " )
self . roots_box . insert ( " 1.0 " , " \n " . join ( str ( r ) for r in roots ) )
self . roots_box . configure ( state = " disabled " )
self . _validate_mcp_roots ( show = False )
except Exception :
pass
def _mcp_root_lines ( self ) :
return [ line . strip ( ) for line in
self . roots_box . get ( " 1.0 " , " end " ) . splitlines ( ) if line . strip ( ) ]
def _append_mcp_root ( self , path ) :
path = os . path . abspath ( os . path . expanduser ( str ( path ) ) )
current = [ os . path . abspath ( os . path . expanduser ( p ) )
for p in self . _mcp_root_lines ( ) ]
if path in current :
self . mcp_folders_status . configure ( text = " Эта папка уже добавлена " ,
text_color = c ( " text_faint " ) )
return
text = self . roots_box . get ( " 1.0 " , " end " ) . strip ( )
if text :
self . roots_box . insert ( " end " , " \n " + path )
else :
self . roots_box . insert ( " 1.0 " , path )
self . _save_mcp ( )
def _add_mcp_root ( self ) :
d = filedialog . askdirectory ( title = " Разрешённая папка для MCP " , parent = self )
try :
d = filedialog . askdirectory ( title = " Рабочая папка для файлового агента " ,
parent = self , mustexist = True )
except Exception as e :
self . mcp_folders_status . configure ( text = f " Не удалось открыть выбор папки: { e } " ,
text_color = c ( " danger " ) )
return
if not d :
return
d = os . path . abspath ( d )
conf = self . app . engine . config . setdefault ( " mcp " , { } )
roots = conf . setdefault ( " roots " , [ ] )
if d not in roots :
roots . append ( d )
if not os . access ( d , os . R_OK ) :
self . mcp_folders_status . configure ( text = " Нет прав на чтение этой папки " ,
text_color = c ( " danger " ) )
return
self . _append_mcp_root ( d )
def _create_mcp_root ( self ) :
try :
parent = filedialog . askdirectory ( title = " Где создать рабочую папку " ,
parent = self , mustexist = True )
if not parent :
return
name = simpledialog . askstring ( " Новая рабочая папка " , " Имя папки: " ,
initialvalue = " XenithWorkspace " ,
parent = self )
if not name :
return
name = name . strip ( ) . replace ( " / " , " " ) . replace ( " \\ " , " " )
if not name :
return
path = os . path . join ( parent , name )
os . makedirs ( path , exist_ok = True )
except Exception as e :
self . mcp_folders_status . configure ( text = f " Не удалось создать папку: { e } " ,
text_color = c ( " danger " ) )
return
self . _append_mcp_root ( path )
def _selected_mcp_root ( self ) :
try :
textbox = getattr ( self . roots_box , " _textbox " , None )
if textbox is not None :
ranges = textbox . tag_ranges ( " sel " )
if ranges :
value = textbox . get ( " sel.first " , " sel.last " ) . strip ( )
if value :
return value . splitlines ( ) [ 0 ] . strip ( )
index = textbox . index ( " insert " )
line = int ( str ( index ) . split ( " . " ) [ 0 ] )
value = textbox . get ( f " { line } .0 " , f " { line } .end " ) . strip ( )
if value :
return value
except Exception :
pass
return " "
def _remove_mcp_root ( self ) :
target = self . _selected_mcp_root ( )
lines = self . _mcp_root_lines ( )
if not lines :
return
if not target :
target = lines [ - 1 ]
remaining = [ p for p in lines
if os . path . abspath ( os . path . expanduser ( p ) ) !=
os . path . abspath ( os . path . expanduser ( target ) ) ]
if len ( remaining ) == len ( lines ) :
remaining = lines [ : - 1 ]
self . roots_box . delete ( " 1.0 " , " end " )
self . roots_box . insert ( " 1.0 " , " \n " . join ( remaining ) )
self . _save_mcp ( )
def _open_mcp_root ( self ) :
target = self . _selected_mcp_root ( ) or ( self . _mcp_root_lines ( ) or [ " " ] ) [ 0 ]
if not target :
return
path = os . path . abspath ( os . path . expanduser ( target ) )
try :
import subprocess
subprocess . Popen ( [ " xdg-open " , path ] )
except Exception as e :
self . mcp_folders_status . configure ( text = f " Не удалось открыть: { e } " ,
text_color = c ( " danger " ) )
def _validate_mcp_roots ( self , show = True ) :
roots = [ ]
errors = [ ]
warnings = [ ]
for raw in self . _mcp_root_lines ( ) :
path = os . path . abspath ( os . path . expanduser ( raw ) )
if path in roots :
continue
if not os . path . exists ( path ) :
errors . append ( f " нет папки: { raw } " )
continue
if not os . path . isdir ( path ) :
errors . append ( f " не каталог: { raw } " )
continue
if not os . access ( path , os . R_OK ) :
errors . append ( f " нет чтения: { raw } " )
continue
if not os . access ( path , os . W_OK ) :
warnings . append ( f " только чтение: { raw } " )
roots . append ( path )
if not self . _mcp_root_lines ( ) :
text = " Папки не выбраны — файловый агент выключен. "
color = c ( " warn " )
elif errors :
text = " Ошибки: " + " ; " . join ( errors [ : 3 ] )
color = c ( " danger " )
elif warnings :
text = " Можно читать, но не записывать: " + " ; " . join ( warnings [ : 2 ] )
color = c ( " warn " )
else :
text = f " Проверено папок: { len ( roots ) } · запись разрешена "
color = c ( " success " )
if show :
self . mcp_folders_status . configure ( text = text , text_color = color )
else :
self . mcp_folders_status . configure (
text = text , text_color = color if color != c ( " success " ) else c ( " text_faint " ) )
return roots , errors , warnings
def _save_mcp ( self , * _ ) :
try :
roots , errors , warnings = self . _validate_mcp_roots ( show = True )
if errors :
self . mcp_folders_status . configure (
text = " Не сохранено: " + " ; " . join ( errors [ : 2 ] ) ,
text_color = c ( " danger " ) )
return
for label , key in MCP_MODES . items ( ) :
if label == self . mcp_mode . get ( ) :
self . app . engine . config . setdefault ( " mcp " , { } ) [ " mode " ] = key
break
roots = [ line . strip ( ) for line in self . roots_box . get ( " 1.0 " , " end " ) . splitlines ( ) if line . strip ( ) ]
self . app . engine . config . setdefault ( " mcp " , { } ) [ " roots " ] = roots
self . app . engine . save_config ( )
self . roots_box . delete ( " 1.0 " , " end " )
self . roots_box . insert ( " 1.0 " , " \n " . join ( roots ) )
self . _refresh_mcp ( )
self . engine_status . configure ( text = " MCP сохранён " , text_color = c ( " text_faint " ) )
if warnings :
self . mcp_folders_status . configure (
text = " Сохранено, но часть папок доступна только для чтения " ,
text_color = c ( " warn " ) )
else :
self . mcp_folders_status . configure (
text = f " Сохранено · папок: { len ( roots ) } " ,
text_color = c ( " success " ) )
except Exception as e :
self . engine_status . configure ( text = f " Ошибка MCP : { e } " , text_color = c ( " danger " ) )
self . mcp_folders_status . configure ( text = f " Ошибка сохранения : { e } " ,
text_color = c ( " danger " ) )
# ==================================================================
# Обновления