Refactor EnhancedContextPlugin to include clipboard copy and paste functionality

This commit is contained in:
2024-11-07 07:50:33 -05:00
parent 33f8fcde11
commit f44ec977a4
2 changed files with 65 additions and 31 deletions
+64 -31
View File
@@ -31,6 +31,8 @@ from prompt_toolkit import PromptSession
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.keys import Keys
import xerox
DB_PATH = "enhanced_context.db"
AVAILABLE_PROVIDERS = ["xai", "openai", "anthropic", "ollama"]
@@ -696,7 +698,7 @@ class EnhancedContextPlugin(sm.BasePlugin):
def get_multiline_input() -> str:
"""Get input from user. Press Enter to send."""
session = PromptSession()
return session.prompt("", multiline=False)
return session.prompt("> ", multiline=False)
def main():
@@ -729,51 +731,45 @@ Commands:
- `/topics`: Show detailed list of all conversation topics
- `/essence`: Show user characteristics and preferences
- `/perspectives`: Show LLM perspectives on the conversation
- `/copy`: Copy last assistant response to clipboard
- `/paste`: Paste clipboard content as user message
"""
console.print(Markdown(md))
try:
while True:
# Simple prompt with > indicator
console.print("\n[bold blue]>[/] ", end="")
user_input = get_multiline_input().strip()
# Get user input first
user_input = get_multiline_input()
if not user_input: # Skip empty messages
# Skip empty messages
if not user_input:
continue
# Handle exit commands
if user_input.lower() in ["quit", "exit", "q"]:
console.print(Markdown("**Goodbye!**"))
break
# Handle commands
if user_input.lower() == "/essence":
markers = plugin.retrieve_essence_markers()
if not markers:
console.print(Markdown("*No essence markers found.*"))
# Handle all commands before any conversation processing
if user_input.startswith("/"):
# Handle copy command
if user_input.lower() == "/copy":
last_response = conversation.get_last_message(role="assistant")
if last_response:
clean_text = last_response.text.replace("### Response\n", "")
xerox.copy(clean_text)
console.print(Markdown("*Last response copied to clipboard*"))
else:
console.print(Markdown("*No response to copy*"))
continue
# Group markers by type
markers_by_type = {}
for marker_type, marker_text in markers:
if marker_type not in markers_by_type:
markers_by_type[marker_type] = []
markers_by_type[marker_type].append(marker_text)
# Handle other commands...
elif user_input.lower() == "/perspectives":
# ... existing perspectives code ...
continue
# ... other command handlers ...
# Format output as markdown
output = ["## User Characteristics"]
for marker_type, markers in markers_by_type.items():
output.append(f"\n### {marker_type.title()}")
for marker in markers:
output.append(f"- {marker}")
console.print(Markdown("\n".join(output)))
continue
# Easter egg handling
if user_input.lower() == "go go go":
console.print(Markdown("*Tip: Use /perspectives instead!*"))
continue
# Regular conversation handling
# Regular conversation handling only happens if no commands were processed
conversation.add_message(role="user", text=user_input)
should_continue = plugin.pre_send_hook(conversation)
@@ -809,6 +805,43 @@ Commands:
console.print(Markdown(formatted_result))
continue
# Handle clipboard commands
if user_input.lower() == "/paste":
try:
clipboard_content = xerox.paste()
if clipboard_content:
# Print the pasted content
console.print() # Add blank line
console.print(
Panel.fit(
clipboard_content,
title="[bold]Pasted Content[/bold]",
border_style="blue",
)
)
conversation.add_message(role="user", text=clipboard_content)
should_continue = plugin.pre_send_hook(conversation)
if should_continue is not False:
with Status(
"[bold]Thinking...[/]", spinner="dots"
) as status:
response = conversation.send()
formatted_response = (
f"""### Response\n{response.text}"""
)
response.text = formatted_response
plugin.post_response_hook(conversation)
console.print() # Add blank line
console.print(Markdown(response.text))
else:
console.print(Markdown("*Clipboard is empty*"))
except Exception as e:
console.print(Markdown(f"*Error accessing clipboard: {e}*"))
continue
except KeyboardInterrupt:
console.print(Markdown("**Goodbye!**"))
return
+1
View File
@@ -8,3 +8,4 @@ rich
nltk
spacy
docopt
xerox