Added browse command and project handlers
This commit is contained in:
+41
-25
@@ -2,35 +2,54 @@ import os
|
||||
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
||||
from telegram.ext import ContextTypes
|
||||
|
||||
async def browse_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
browse_command_bot = None
|
||||
|
||||
async def browse_command(update: Update, context: ContextTypes.DEFAULT_TYPE, bot) -> None:
|
||||
global browse_command_bot
|
||||
browse_command_bot = bot
|
||||
prompts_dir = "prompts"
|
||||
|
||||
await navigate_to(prompts_dir, update.message.reply_text)
|
||||
|
||||
async def navigate_to(directory: str, updateMethod) -> None:
|
||||
try:
|
||||
# Get all items in the prompts directory
|
||||
items = os.listdir(prompts_dir)
|
||||
subdirs, files = await get_files_and_directories(directory)
|
||||
|
||||
existing_buttons = []
|
||||
|
||||
if (directory != "prompts"):
|
||||
parent_directory = os.path.split(directory)[0]
|
||||
existing_buttons.append([InlineKeyboardButton("↩️ Back", callback_data=f"browse:{parent_directory}")])
|
||||
|
||||
# Separate and sort subdirectories and files
|
||||
subdirs = sorted([item for item in items if os.path.isdir(os.path.join(prompts_dir, item))])
|
||||
files = sorted([item for item in items if os.path.isfile(os.path.join(prompts_dir, item))])
|
||||
reply_markup = await create_keyboard(subdirs, files, existing_buttons)
|
||||
|
||||
# Create keyboard layout
|
||||
keyboard = []
|
||||
for subdir in subdirs:
|
||||
keyboard.append([InlineKeyboardButton(f"[{subdir}]", callback_data=f"browse:{subdir}")])
|
||||
for file in files:
|
||||
keyboard.append([InlineKeyboardButton(file, callback_data=f"file:{file}")])
|
||||
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
||||
await update.message.reply_text("Browse files and directories:", reply_markup=reply_markup)
|
||||
await updateMethod("Browse files and directories:", reply_markup=reply_markup)
|
||||
|
||||
except FileNotFoundError:
|
||||
await update.message.reply_text("The 'prompts' directory does not exist.")
|
||||
await updateMethod(f"The {directory} directory does not exist.")
|
||||
except Exception as e:
|
||||
await update.message.reply_text(f"An error occurred: {str(e)}")
|
||||
await updateMethod(f"An error occurred: {str(e)}")
|
||||
|
||||
async def create_keyboard(subdirs, files, existing_buttons) -> InlineKeyboardMarkup:
|
||||
keyboard = existing_buttons
|
||||
for subdir in subdirs:
|
||||
keyboard.append([InlineKeyboardButton(subdir, callback_data=f"browse:{subdir}")])
|
||||
for file in files:
|
||||
keyboard.append([InlineKeyboardButton(file, callback_data=f"file:{file}")])
|
||||
return InlineKeyboardMarkup(keyboard)
|
||||
|
||||
async def get_files_and_directories(directory: str) -> list:
|
||||
# Get all items in the prompts directory
|
||||
items = os.listdir(directory)
|
||||
|
||||
# Separate and sort subdirectories and files
|
||||
subdirs = sorted([os.path.join(directory, item) for item in items if os.path.isdir(os.path.join(directory, item))])
|
||||
files = sorted([os.path.join(directory, item) for item in items if os.path.isfile(os.path.join(directory, item))])
|
||||
return subdirs, files
|
||||
|
||||
# This function will need to be called when a button is pressed
|
||||
async def button_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
global browse_command_bot
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
|
||||
@@ -38,13 +57,10 @@ async def button_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
if data.startswith("browse:"):
|
||||
# Handle subdirectory navigation (for future implementation)
|
||||
subdir = data.split(":")[1]
|
||||
await query.edit_message_text(f"Navigating to [{subdir}] is not yet implemented.")
|
||||
await navigate_to(subdir, query.edit_message_text)
|
||||
elif data.startswith("file:"):
|
||||
# Handle file selection
|
||||
file = data.split(":")[1]
|
||||
file_path = os.path.join('prompts', file)
|
||||
file_path = data.split(":")[1]
|
||||
os.environ['SYSTEM_PROMPT_PATH'] = file_path
|
||||
await query.edit_message_text(f"Selected: {file}. SYSTEM_PROMPT_PATH updated.")
|
||||
|
||||
# Optionally, you may want to reload the system prompt here if needed.
|
||||
# bot.system_prompt = bot.load_system_prompt() # Uncomment if you have access to the bot instance.
|
||||
browse_command_bot.system_prompt = browse_command_bot.load_system_prompt()
|
||||
await query.edit_message_text(f"Selected: {file_path}. System prompt updated.")
|
||||
Reference in New Issue
Block a user