Skip to content

Commit

Permalink
Use new viewsets for Discord, Slack, Twilio
Browse files Browse the repository at this point in the history
  • Loading branch information
vegito22 committed Oct 17, 2024
1 parent c8f491b commit 5f2e8d1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
80 changes: 80 additions & 0 deletions llmstack/apps/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from llmstack.apps.runner.app_runner import (
AppRunner,
AppRunnerRequest,
DiscordAppRunnerSource,
SlackAppRunnerSource,
TwilioSMSAppRunnerSource,
WebAppRunnerSource,
Expand Down Expand Up @@ -1364,6 +1365,85 @@ def generate_job_id(cls):
return "{}".format(str(uuid.uuid4()))


def run_discord_app(request, uid, input_data, source, app_data, session_id, stream=False):
DiscordViewSet()._run(request, uid, input_data, source, app_data, session_id, stream)


class DiscordViewSet(viewsets.ViewSet):
permission_classes = [AllowAny]

def _run(self, request, uid, input_data, source, app_data, session_id, stream=False):
app = get_object_or_404(App, uuid=uuid.UUID(uid))
if app.discord_config:
response = APIViewSet().run_internal(
request=request,
app_uuid=uid,
input_data=input_data,
source=source,
app_data=app_data,
session_id=session_id,
stream=stream,
)
response_text = (
response.data.get("data", {}).get("output", {}).get("output")
or (" ".join(response.data.get("data", {}).get("output", {}).get("errors", [])) or "An error occurred.")
if response.status_code == 200
else "An error occurred."
)
token = input_data.get("_request", {}).get("token")
app_id = app.discord_config.get("app_id")

prequests.post(
url=f"https://discord.com/api/v10/webhooks/{app_id}/{token}",
headers={
"Authorization": f"Bot {token}",
"Content-Type": "application/json",
},
json={
"content": response_text,
},
)

def run_async(self, request, uid):
if request.data.get("type") == 1:
return DRFResponse(status=200, data={"type": 1})

if request.data.get("type") == 2:
app = get_object_or_404(App, uuid=uuid.UUID(uid))
if app.visibility == AppVisibility.PUBLIC:
app_data_obj = AppData.objects.filter(app_uuid=app.uuid, is_draft=False).order_by("-created_at").first()
if app_data_obj:
if app.discord_config and request.data["data"]["name"] == app.discord_config["slash_command_name"]:
session_id = str(uuid.uuid5(uuid.NAMESPACE_URL, request.data["id"]))
request_user = AnonymousUser()
discord_input = {}
for option in request.data["data"]["options"]:
discord_input[option["name"]] = option["value"]

input_data = {
**discord_input,
"_request": {
"user": request.data["member"]["user"]["id"],
"username": request.data["member"]["user"]["username"],
"global_name": request.data["member"]["user"]["global_name"],
"channel": request.data["channel_id"],
"guild_id": request.data["guild_id"],
"token": request.data["token"],
},
}
new_request = RequestFactory().post("/api/apps/{}/run".format(uid))
new_request.user = request_user
new_request.data = {"input": input_data, "session_id": session_id}
source = DiscordAppRunnerSource(request_user_email=None, app_uuid=uid)

RunAppAsyncJob.create(
func="llmstack.apps.apis.run_discord_app",
args=[new_request, uid, input_data, source, app_data_obj.data, session_id, False],
).add_to_queue()

return DRFResponse(status=200, data={"type": 5})


def run_slack_app(request, uid, input_data, source, app_data, session_id, stream=False):
SlackViewSet._run(request, uid, input_data, source, app_data, session_id, stream)

Expand Down
6 changes: 3 additions & 3 deletions llmstack/apps/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
),
path(
"api/apps/<str:uid>/discord/run",
apis.AppViewSet.as_view({"post": "run_discord"}),
apis.DiscordViewSet.as_view({"post": "run_async"}),
),
path(
"api/apps/<str:uid>/slack/run",
apis.AppViewSet.as_view({"post": "run_slack"}),
apis.SlackViewSet.as_view({"post": "run_async"}),
),
path(
"api/apps/<str:uid>/twiliosms/run",
apis.AppViewSet.as_view({"post": "run_twiliosms"}),
apis.TwilioSMSViewSet.as_view({"post": "run_async"}),
),
path(
"api/apps/<str:uid>/twiliovoice/run",
Expand Down

0 comments on commit 5f2e8d1

Please sign in to comment.