Skip to content

Commit

Permalink
[AHDA Plugin] improved web design & fixed bug (#1133)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Increased command timeout from 5 to 8 seconds for improved command
finalization.
- Enhanced user input handling by capturing `url` and `os` from URL
query parameters.
  
- **Bug Fixes**
- Improved error handling for missing `uid` parameter during form
submission.
- Updated response handling for configuration requests to provide
clearer feedback.

- **Improvements**
- Refined command processing and timing logic for more robust
functionality.
- Streamlined user interface by modifying header instructions and form
submission logic.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
beastoin authored Oct 22, 2024
2 parents 58b17d4 + 5e8d2c8 commit 47643a0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
44 changes: 25 additions & 19 deletions plugins/example/ahda/client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from fastapi import APIRouter, Request, HTTPException, Form, Query, BackgroundTasks, Body
from fastapi import APIRouter, HTTPException, Query, BackgroundTasks, Body, Request, Form
from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
from db import get_ahda_url, store_ahda, get_ahda_os
import os
import requests
from models import RealtimePluginRequest, EndpointResponse
import time
import asyncio
import logging
Expand All @@ -14,7 +13,7 @@
active_sessions = {}

KEYWORD = "computer"
COMMAND_TIMEOUT = 5 # Seconds to wait after the last word to finalize the command
COMMAND_TIMEOUT = 8 # Seconds to wait after the last word to finalize the command

# Path to the directory containing `index.html`
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -39,13 +38,12 @@ def sendToPC(uid, response):
'response': response
}
try:
resp = requests.post(ahda_url+"/recieve", json=payload)
resp = requests.post(ahda_url + "/recieve", json=payload)
resp.raise_for_status()
return {'message': 'Webhook sent successfully'}
except requests.RequestException as e:
logger.error(f"Error sending webhook: {e}")
raise
return {'message': 'Webhook sent successfully'}

return {'message': f'Failed to send webhook: {e}'}

@router.post('/ahda/send-webhook', tags=['ahda', 'realtime'])
async def send_ahda_webhook(
Expand All @@ -71,7 +69,8 @@ async def send_ahda_webhook(

async def schedule_finalize_command(uid, delay):
await asyncio.sleep(delay)
await finalize_command(uid)
if time.time() - active_sessions[uid]["last_received_time"] >= delay:
await finalize_command(uid)

async def finalize_command(uid):
final_command = active_sessions[uid]["command"].strip()
Expand All @@ -82,48 +81,54 @@ async def finalize_command(uid):
active_sessions[uid]["active"] = False
active_sessions[uid]["timer"] = None

# Adjusted to handle segments as dictionaries
# Process each segment
for segment in segments:
text = segment.get("text", "").strip().lower()
logger.info(f"Received segment: {text} (session_id: {uid})")

if KEYWORD in text:
logger.info("Activation keyword detected!")
active_sessions[uid]["active"] = True

# Reset command aggregation and update last received time
active_sessions[uid]["last_received_time"] = time.time()
active_sessions[uid]["command"] = text

# Cancel the previous timer if any
if active_sessions[uid]["timer"]:
pass
active_sessions[uid]["timer"].cancel()

active_sessions[uid]["timer"] = background_tasks.add_task(
schedule_finalize_command, uid, COMMAND_TIMEOUT
# Schedule a new timer for finalizing the command
active_sessions[uid]["timer"] = asyncio.create_task(
schedule_finalize_command(uid, COMMAND_TIMEOUT)
)
continue

# Append to the existing command if active
if active_sessions[uid]["active"]:
active_sessions[uid]["command"] += " " + text
active_sessions[uid]["last_received_time"] = time.time()
logger.info(f"Aggregating command: {active_sessions[uid]['command'].strip()}")

# Cancel the previous timer and set a new one
if active_sessions[uid]["timer"]:
pass
active_sessions[uid]["timer"].cancel()

active_sessions[uid]["timer"] = background_tasks.add_task(
schedule_finalize_command, uid, COMMAND_TIMEOUT
active_sessions[uid]["timer"] = asyncio.create_task(
schedule_finalize_command(uid, COMMAND_TIMEOUT)
)

return {"status": "success"}


async def call_chatgpt_to_generate_code(command, uid):
try:
ahda_os = get_ahda_os(uid)
messages = [
("system", prompt.replace("{os_name}",ahda_os)),
("system", prompt.replace("{os_name}", ahda_os)),
("human", command),
]
ai_msg = chat.invoke(messages)
sendToPC(uid, ai_msg)
ai_msg = await chat.invoke(messages) # Ensure this is awaited
return sendToPC(uid, ai_msg)
except Exception as e:
logger.error(f"Error calling ChatGPT-4: {e}")
return {"type": "error", "content": str(e)}
Expand All @@ -132,6 +137,7 @@ async def call_chatgpt_to_generate_code(command, uid):
async def get_ahda_index(request: Request, uid: str = Query(None)):
if not uid:
raise HTTPException(status_code=400, detail="UID is required")

return FileResponse(INDEX_PATH)

@router.post('/ahda/configure', tags=['ahda'])
Expand Down
8 changes: 6 additions & 2 deletions plugins/example/ahda/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
</div>
<p class="plugin-text">AHDA Integration Plugin</p>
<h1>AHDA Integration</h1>
<h1>Install https://github.com/ActuallyAdvanced/OMI-AHDA first</h1>
Install https://github.com/ActuallyAdvanced/OMI-AHDA first
<br>
<form id="ahda-form">
<input type="text" id="url" placeholder="Enter AHDA URL" required>
<input type="text" id="os" placeholder="Enter Operating System" required>
Expand All @@ -103,6 +104,9 @@ <h1>Install https://github.com/ActuallyAdvanced/OMI-AHDA first</h1>
const urlParams = new URLSearchParams(window.location.search);
const uid = urlParams.get('uid');

const url = urlParams.get('url');
const os = urlParams.get('os');

if (!uid) {
document.getElementById('response-message').textContent = "UID is missing. Please check the URL.";
document.getElementById('response-message').style.color = 'red';
Expand Down Expand Up @@ -135,4 +139,4 @@ <h1>Install https://github.com/ActuallyAdvanced/OMI-AHDA first</h1>
</script>
</body>

</html>
</html>

0 comments on commit 47643a0

Please sign in to comment.