Skip to content

Commit

Permalink
feat: Automatically get explained response if URL can be provided
Browse files Browse the repository at this point in the history
  • Loading branch information
nikromen committed Jan 28, 2025
1 parent 255171b commit 13a7827
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
6 changes: 5 additions & 1 deletion backend/src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from datetime import datetime
from http import HTTPStatus
from pathlib import Path
from typing import Optional

import requests

Expand Down Expand Up @@ -133,7 +134,10 @@ def review(request: Request):


@app.get("/explain", response_class=HTMLResponse)
def explain(request: Request):
def explain(request: Request, url: Optional[str] = None):
# the URL is parsed by frontend, but having it mentioned here
# means we have it documented
_ = url
mapping = {"name": "app-prompt", "request": request}
return template_response("app.html", mapping)

Expand Down
46 changes: 28 additions & 18 deletions frontend/src/app/prompt/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
[:prompt [:maybe :string]]])

(defn send []
(let [data {:prompt @prompt-value}]
(let [query-url (-> js/window .-location .-search js/URLSearchParams. (.get "url"))
data (if query-url
{:prompt query-url}
{:prompt @prompt-value})]
(if (m/validate OutputSchema data)
(do
(reset! status "waiting")
Expand All @@ -78,9 +81,11 @@
"Invalid data"
"Got invalid data from the backend. This is likely a bug.")))))

(handle-backend-error
"Client error"
"Something went wrong when preparing a request to server"))))
(if (not query-url)
(reset! status "No URL provided. Please enter a URL.")
(handle-backend-error
"Client error"
"Something went wrong when preparing a request to server")))))

(defn on-change-prompt [event]
(let [target (.-target event)
Expand Down Expand Up @@ -269,17 +274,22 @@
"it. You won't need to open the logs at all."))]])

(defn prompt []
(cond
(= @status "error")
(render-error @error-title @error-description)

;; TODO If we are already in a two-column-layout, we should print only
;; a small loading icon somewhere, not a jumbotron
(= @status "waiting")
(loading-screen "Getting a response from the server")

@form
(two-column-layout)

:else
(prompt-only)))
(let [query-url (-> js/window .-location .-search js/URLSearchParams. (.get "url"))]
(if query-url
(do
(send)
(loading-screen "Getting a response from the server"))
(cond
(= @status "error")
(render-error @error-title @error-description)

;; TODO If we are already in a two-column-layout, we should print only
;; a small loading icon somewhere, not a jumbotron
(= @status "waiting")
(loading-screen "Getting a response from the server")

@form
(two-column-layout)

:else
(prompt-only)))))

0 comments on commit 13a7827

Please sign in to comment.