Skip to content

Commit

Permalink
fixed AHDA Plugin (#1137)
Browse files Browse the repository at this point in the history
  • Loading branch information
beastoin authored Oct 22, 2024
2 parents 4d43232 + 0d1a363 commit 9b47cdd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 38 deletions.
9 changes: 5 additions & 4 deletions community-plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,13 @@
"author": "Neo",
"description": "This Plugin translates your memories into Gen Z or A slang.",
"image": "/plugins/logos/gen-z-a-translator.jpg",
"memories": false,
"memories": true,
"chat": true,
"capabilities": [
"memories"
"memories",
"chat"
],
"memory_prompt": "Please transform the following sentence into Gen Z/Gen Alpha slang, incorporating as many of the following terms as possible (and more): *bet, fax, W, vibe, bussin', rizz, lit, drip, mid, cap, salty, slay, extra, flex, yeet, mood, glow up, big L, bop, Gucci, period, sus, sheesh, lowkey, snack, snatched, stan, thirsty, tea, ghost, cringe, delulu, fire, basic, GOAT, ick, peep, sleeper, low-vibrational, it's giving,* and *vibin'*, cooked. The goal is to maintain the original meaning of the sentence but make it sound as if it's being spoken by a Gen Z or Gen Alpha person. Feel free to get creative with the slang, keeping it short and casual, while giving the sentence that trendy, social-media-ready vibe. Also, ensure the use of terms fits contextually and naturally into the sentence.",
"memory_prompt": "Please transform the following sentence into Gen Z/Gen Alpha slang, incorporating as many of the following terms as possible (and more): *bet, fax, W, vibe, bussin', rizz, lit, drip, mid, cap, salty, slay, extra, flex, yeet, mood, glow up, big L, Gucci, period, sus, sheesh, lowkey, snack, snatched, stan, ghost, cringe, delulu, fire, basic, GOAT, ick, peep, sleeper, it's giving,* and *vibin'*, cooked.",
"deleted": false
},
{
Expand All @@ -668,7 +669,7 @@
"external_integration": {
"triggers_on": "transcript_processed",
"webhook_url": "https://based-hardware--plugins-api.modal.run/ahda/send-webhook",
"setup_completed_url": null,
"setup_completed_url": "https://based-hardware--plugins-api.modal.run/ahda/completion",
"setup_instructions_file_path": "/plugins/instructions/ahda/README.md",
"auth_steps": [
{
Expand Down
101 changes: 67 additions & 34 deletions plugins/example/ahda/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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
from fastapi import APIRouter, HTTPException, Query, Body, Request, Form
from fastapi.responses import HTMLResponse, FileResponse
import os
import requests
import time
import asyncio
import logging
from langchain_openai import ChatOpenAI
from db import get_ahda_url, get_ahda_os, store_ahda

router = APIRouter()

Expand Down Expand Up @@ -38,7 +37,23 @@ def sendToPC(uid, response):
'response': response
}
try:
resp = requests.post(ahda_url + "/recieve", json=payload)
resp = requests.post(ahda_url + "/receive", json=payload)
resp.raise_for_status()
return {'message': 'Webhook sent successfully'}
except requests.RequestException as e:
logger.error(f"Error sending webhook: {e}")
return {'message': f'Failed to send webhook: {e}'}

def sendLiveTranscriptToPC(uid, response):
ahda_url = get_ahda_url(uid)
if not ahda_url:
raise ValueError('AHDA URL not configured for this UID')
payload = {
'uid': uid,
'response': response
}
try:
resp = requests.post(ahda_url + "/transcript", json=payload)
resp.raise_for_status()
return {'message': 'Webhook sent successfully'}
except requests.RequestException as e:
Expand All @@ -49,7 +64,6 @@ def sendToPC(uid, response):
async def send_ahda_webhook(
uid: str = Query(...),
data: dict = Body(...),
background_tasks: BackgroundTasks = BackgroundTasks()
):
segments = data.get("segments")
if not uid:
Expand All @@ -62,57 +76,69 @@ async def send_ahda_webhook(
logger.info(f"New session started: {uid}")
active_sessions[uid] = {
"command": "",
"last_received_time": time.time(),
"last_received_time": asyncio.get_event_loop().time(),
"active": False,
"timer": None
}

async def schedule_finalize_command(uid, delay):
await asyncio.sleep(delay)
if time.time() - active_sessions[uid]["last_received_time"] >= delay:
await finalize_command(uid)
try:
await asyncio.sleep(delay)
if asyncio.get_event_loop().time() - active_sessions[uid]["last_received_time"] >= delay:
await finalize_command(uid)
except asyncio.CancelledError:
logger.info(f"Timer for session {uid} was cancelled")

async def finalize_command(uid):
final_command = active_sessions[uid]["command"].strip()
if final_command:
logger.info(f"Final command for session {uid}: {final_command}")
await call_chatgpt_to_generate_code(final_command, uid)
# Reset session
active_sessions[uid]["command"] = ""
active_sessions[uid]["active"] = False
active_sessions[uid]["timer"] = None

# Process each segment
for segment in segments:
text = segment.get("text", "").strip().lower()
sendLiveTranscriptToPC(uid, text)
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"]:
active_sessions[uid]["timer"].cancel()

# 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"]:
if not active_sessions[uid]["active"]:
if KEYWORD in text:
logger.info("Activation keyword detected!")
active_sessions[uid]["active"] = True
active_sessions[uid]["command"] = text
active_sessions[uid]["last_received_time"] = asyncio.get_event_loop().time()

# Cancel the previous timer if any
if active_sessions[uid]["timer"]:
active_sessions[uid]["timer"].cancel()
try:
await active_sessions[uid]["timer"]
except asyncio.CancelledError:
pass

# Schedule a new timer for finalizing the command
active_sessions[uid]["timer"] = asyncio.create_task(
schedule_finalize_command(uid, COMMAND_TIMEOUT)
)
else:
# Not active and keyword not detected, ignore
continue
else:
# Append to the existing command
active_sessions[uid]["command"] += " " + text
active_sessions[uid]["last_received_time"] = time.time()
logger.info(f"Aggregating command: {active_sessions[uid]['command'].strip()}")
active_sessions[uid]["last_received_time"] = asyncio.get_event_loop().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"]:
active_sessions[uid]["timer"].cancel()
try:
await active_sessions[uid]["timer"]
except asyncio.CancelledError:
pass

active_sessions[uid]["timer"] = asyncio.create_task(
schedule_finalize_command(uid, COMMAND_TIMEOUT)
Expand All @@ -127,7 +153,7 @@ async def call_chatgpt_to_generate_code(command, uid):
("system", prompt.replace("{os_name}", ahda_os)),
("human", command),
]
ai_msg = await chat.invoke(messages) # Ensure this is awaited
ai_msg = chat.invoke(messages)
return sendToPC(uid, ai_msg)
except Exception as e:
logger.error(f"Error calling ChatGPT-4: {e}")
Expand All @@ -140,6 +166,13 @@ async def get_ahda_index(request: Request, uid: str = Query(None)):

return FileResponse(INDEX_PATH)

@router.get("/ahda/completion", tags=['ahda'])
def is_setup_completed(uid: str):
ahda_url = get_ahda_url(uid)
ahda_os = get_ahda_os(uid)

return {'is_setup_completed': ahda_url is not None and ahda_os is not None}

@router.post('/ahda/configure', tags=['ahda'])
def configure_ahda(uid: str = Form(...), url: str = Form(...), os: str = Form(...)):
if not uid or not url:
Expand Down

0 comments on commit 9b47cdd

Please sign in to comment.