Added some features
This commit is contained in:
42
oai.py
42
oai.py
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/python3 -W ignore::DeprecationWarning
|
||||
import sys
|
||||
import os
|
||||
import requests
|
||||
from pathlib import Path
|
||||
@@ -14,6 +15,7 @@ import mimetypes
|
||||
import base64
|
||||
import re
|
||||
import sqlite3 # Added for SQLite DB integration
|
||||
from prompt_toolkit import PromptSession
|
||||
|
||||
app = typer.Typer()
|
||||
console = Console()
|
||||
@@ -68,6 +70,17 @@ except Exception as e:
|
||||
models_data = []
|
||||
text_models = []
|
||||
|
||||
# **NEW: Function to clear the screen using ANSI escape sequences (cross-platform)**
|
||||
def clear_screen():
|
||||
"""Clear the terminal screen using ANSI escape codes, with fallback to newlines."""
|
||||
try:
|
||||
# ANSI sequence to clear screen and move cursor to top-left
|
||||
# Cf. [usavps.com](https://usavps.com/blog/46203/) and [phoenixnap.de](https://phoenixnap.de/kb/clear-terminal) for ANSI usage
|
||||
print("\033[H\033[J", end="", flush=True)
|
||||
except Exception:
|
||||
# Fallback: Fill with newlines (simpler, per [medium.com](https://medium.com/@ryan_forrester_/c-screen-clearing-how-to-guide-cff5bf764ccd))
|
||||
print("\n" * 100)
|
||||
|
||||
@app.command()
|
||||
def chat():
|
||||
"""Start the OAI chat app with OpenRouter models."""
|
||||
@@ -99,9 +112,13 @@ def chat():
|
||||
if not selected_model:
|
||||
console.print("[bold yellow]No model selected. Use '/model' to choose one.[/]")
|
||||
|
||||
# NEW: Initialize PromptSession for input history and arrow keys
|
||||
session = PromptSession(history=None) # 'None' uses InMemoryHistory by default; can be customized later for disk persistence
|
||||
|
||||
while True:
|
||||
try:
|
||||
user_input = typer.prompt("You").strip()
|
||||
# REPLACED: Use session.prompt() instead of typer.prompt() for history/arrow key support
|
||||
user_input = session.prompt("You> ").strip()
|
||||
if user_input.lower() in ["exit", "quit", "bye"]:
|
||||
console.print("[bold yellow]Goodbye![/]")
|
||||
return
|
||||
@@ -181,7 +198,13 @@ def chat():
|
||||
console.print(Panel(table, title="[bold green]Current Configurations[/]", title_align="left"))
|
||||
continue
|
||||
|
||||
# Handle /help command (new: display examples for each command)
|
||||
# **NEW: Handle /clear command to clear the screen**
|
||||
if user_input.lower() == "/clear":
|
||||
clear_screen()
|
||||
console.print("[bold cyan]Screen cleared. Ready for your next input![/]")
|
||||
continue
|
||||
|
||||
# Handle /help command (updated: now includes /clear)
|
||||
if user_input.lower() == "/help":
|
||||
help_table = Table("Command", "Description", "Example", show_header=True, header_style="bold cyan")
|
||||
help_table.add_row(
|
||||
@@ -209,6 +232,11 @@ def chat():
|
||||
"View all current configurations.",
|
||||
"/config\n(Displays table of API Key, Base URL, etc.)"
|
||||
)
|
||||
help_table.add_row( # **NEW: Added /clear to help**
|
||||
"/clear",
|
||||
"Clear the terminal screen for a clean interface.",
|
||||
"/clear\n[bold cyan]Screen cleared. Ready for your next input![/bold cyan]"
|
||||
)
|
||||
help_table.add_row(
|
||||
"/help",
|
||||
"Show this help menu with examples.",
|
||||
@@ -217,7 +245,7 @@ def chat():
|
||||
help_table.add_row(
|
||||
"Chatting with files",
|
||||
"Attach images or plain text files to messages using '@path'.",
|
||||
"Explain this @/Users/me/image.jpg\n(Attaches image.jpg if supported by the model)"
|
||||
"Explain this @/Users/me/image.jpg\n(Attaches image.jpg if supported by model)"
|
||||
)
|
||||
help_table.add_row(
|
||||
"Exiting",
|
||||
@@ -337,6 +365,14 @@ def chat():
|
||||
else:
|
||||
console.print("[bold red]No response received.[/]")
|
||||
|
||||
except KeyboardInterrupt:
|
||||
# NEW: Handle Ctrl+C during prompt input (continue loop instead of crashing)
|
||||
console.print("\n[bold yellow]Input interrupted. Continuing...[/]")
|
||||
continue
|
||||
except EOFError:
|
||||
# NEW: Handle Ctrl+D (exit loop gracefully)
|
||||
console.print("\n[bold yellow]Goodbye![/]")
|
||||
return
|
||||
except Exception as e:
|
||||
console.print(f"[bold red]Error: {e}[/]")
|
||||
console.print("[bold yellow]Try again or use '/model' to select a different model.[/]")
|
||||
|
||||
Reference in New Issue
Block a user