Skip to content

web search done right

Make Alice configurable in frontend

Mikkel Denker62eb00804efd
diff --git a/core/src/api/about.rs b/core/src/api/about.rs
index 053e9a65..7cd071f1 100644
--- a/core/src/api/about.rs
+++ b/core/src/api/about.rs
@@ -14,16 +14,22 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use super::HtmlTemplate;
+use std::sync::Arc;
+
+use super::{HtmlTemplate, State};
use askama::Template;
-use axum::response::IntoResponse;
+use axum::{extract, response::IntoResponse};
#[allow(clippy::unused_async)]
-pub async fn route() -> impl IntoResponse {
- let template = AboutTemplate {};
+pub async fn route(extract::State(state): extract::State<Arc<State>>) -> impl IntoResponse {
+ let template = AboutTemplate {
+ with_alice: state.config.with_alice,
+ };
HtmlTemplate(template)
}
#[derive(Template)]
#[template(path = "about/index.html")]
-struct AboutTemplate {}
+struct AboutTemplate {
+ with_alice: Option<bool>,
+}
diff --git a/core/src/api/alice.rs b/core/src/api/alice.rs
index cdba6cc4..36090134 100644
--- a/core/src/api/alice.rs
+++ b/core/src/api/alice.rs
@@ -40,6 +40,10 @@ pub async fn save_state(
extract::State(state): extract::State<Arc<super::State>>,
extract::Json(params): extract::Json<SaveStateParams>,
) -> Result<impl IntoResponse, http::StatusCode> {
+ if !state.config.with_alice.unwrap_or(true) {
+ return Err(StatusCode::NOT_FOUND);
+ }
+
let client = reqwest::ClientBuilder::default()
.build()
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
@@ -119,6 +123,10 @@ pub async fn route(
Sse<impl Stream<Item = std::result::Result<axum::response::sse::Event, Infallible>>>,
StatusCode,
> {
+ if !state.config.with_alice.unwrap_or(true) {
+ return Err(StatusCode::NOT_FOUND);
+ }
+
let mut saved_state: Option<SavedState> = None;
if let Some(prev_state) = params.prev_state {
@@ -187,70 +195,3 @@ pub async fn route(
Ok(Sse::new(stream))
}
-
-// for openai alice
-// #[derive(serde::Serialize, serde::Deserialize, Debug)]
-// #[serde(rename_all = "camelCase")]
-// pub struct Params {
-// optic: Option<String>,
-// conv: EncodedSavedState,
-// }
-//
-// pub async fn route(
-// extract::State(state): extract::State<Arc<super::State>>,
-// extract::Query(params): extract::Query<Params>,
-// ) -> std::result::Result<
-// Sse<impl Stream<Item = std::result::Result<axum::response::sse::Event, Infallible>>>,
-// StatusCode,
-// > {
-// let saved_state: SavedState = params.conv.decode().map_err(|e| {
-// tracing::error!("Error decoding saved state: {}", e);
-// StatusCode::BAD_REQUEST
-// })?;
-//
-// let client = reqwest::ClientBuilder::default()
-// .build()
-// .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
-//
-// let mut members = state.cluster.members().await;
-//
-// members.shuffle(&mut rand::thread_rng());
-//
-// let id = saved_state.id.clone();
-// let alice_addr = members
-// .into_iter()
-// .find_map(|m| {
-// if m.id == id {
-// if let Service::Alice { host } = m.service {
-// Some(host)
-// } else {
-// None
-// }
-// } else {
-// None
-// }
-// })
-// .ok_or(StatusCode::INTERNAL_SERVER_ERROR)?;
-//
-// let params = entrypoint::alice::openai::Params {
-// conversation: saved_state.uuid,
-// optic: params.optic,
-// };
-//
-// let mut events = client
-// .get(format!("http://{}", alice_addr))
-// .query(&params)
-// .send()
-// .await
-// .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
-// .bytes_stream()
-// .eventsource();
-//
-// let stream = async_stream::stream! {
-// while let Some(Ok(item)) = events.next().await {
-// yield Ok(Event::default().data(item.data));
-// }
-// };
-//
-// Ok(Sse::new(stream))
-// }
diff --git a/core/src/api/chat.rs b/core/src/api/chat.rs
index 0b5302e4..412a6e1c 100644
--- a/core/src/api/chat.rs
+++ b/core/src/api/chat.rs
@@ -14,24 +14,30 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use std::collections::HashMap;
+use std::{collections::HashMap, sync::Arc};
use super::{
optics::{OpticLink, DEFAULT_OPTICS},
- HtmlTemplate,
+ HtmlTemplate, State,
};
use askama::Template;
use axum::{extract, response::IntoResponse};
#[allow(clippy::unused_async)]
pub async fn route(
+ extract::State(state): extract::State<Arc<State>>,
extract::Query(params): extract::Query<HashMap<String, String>>,
-) -> impl IntoResponse {
+) -> Result<impl IntoResponse, http::StatusCode> {
+ if !state.config.with_alice.unwrap_or(true) {
+ return Err(http::StatusCode::NOT_FOUND);
+ }
+
let template = ChatTemplate {
query_url_part: serde_urlencoded::to_string(params).unwrap(),
default_optics: DEFAULT_OPTICS.to_vec(),
+ with_alice: state.config.with_alice,
};
- HtmlTemplate(template)
+ Ok(HtmlTemplate(template))
}
#[derive(Template)]
@@ -39,4 +45,5 @@ pub async fn route(
struct ChatTemplate {
query_url_part: String,
default_optics: Vec<OpticLink>,
+ with_alice: Option<bool>,
}
diff --git a/core/src/api/crawler.rs b/core/src/api/crawler.rs
index f2dcb117..775fec34 100644
--- a/core/src/api/crawler.rs
+++ b/core/src/api/crawler.rs
@@ -14,16 +14,22 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use super::HtmlTemplate;
+use std::sync::Arc;
+
+use super::{HtmlTemplate, State};
use askama::Template;
-use axum::response::IntoResponse;
+use axum::{extract, response::IntoResponse};
#[allow(clippy::unused_async)]
-pub async fn info_route() -> impl IntoResponse {
- let template = CrawlerTemplate {};
+pub async fn info_route(extract::State(state): extract::State<Arc<State>>) -> impl IntoResponse {
+ let template = CrawlerTemplate {
+ with_alice: state.config.with_alice,
+ };
HtmlTemplate(template)
}
#[derive(Template)]
#[template(path = "crawler/index.html")]
-struct CrawlerTemplate {}
+struct CrawlerTemplate {
+ with_alice: Option<bool>,
+}
diff --git a/core/src/api/explore.rs b/core/src/api/explore.rs
index 3dabea96..f9115ea1 100644
--- a/core/src/api/explore.rs
+++ b/core/src/api/explore.rs
@@ -14,21 +14,23 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use std::collections::HashMap;
+use std::{collections::HashMap, sync::Arc};
use askama::Template;
use axum::{extract, response::IntoResponse};
use http::StatusCode;
use optics::{Optic, SiteRankings};
-use super::HtmlTemplate;
+use super::{HtmlTemplate, State};
#[allow(clippy::unused_async)]
pub async fn route(
+ extract::State(state): extract::State<Arc<State>>,
extract::Query(params): extract::Query<HashMap<String, String>>,
) -> impl IntoResponse {
let template = ExploreTemplate {
query_url_part: serde_urlencoded::to_string(params).unwrap(),
+ with_alice: state.config.with_alice,
};
HtmlTemplate(template)
}
@@ -37,6 +39,7 @@ pub async fn route(
#[template(path = "explore/index.html")]
struct ExploreTemplate {
pub query_url_part: String,
+ with_alice: Option<bool>,
}
#[derive(serde::Deserialize)]
diff --git a/core/src/api/improvement.rs b/core/src/api/improvement.rs
index a993807a..71cbbd7b 100644
--- a/core/src/api/improvement.rs
+++ b/core/src/api/improvement.rs
@@ -100,12 +100,16 @@ pub async fn store_chat(
}
#[allow(clippy::unused_async)]
-pub async fn settings() -> impl IntoResponse {
- let template = SettingsTemplate {};
+pub async fn settings(extract::State(state): extract::State<Arc<State>>) -> impl IntoResponse {
+ let template = SettingsTemplate {
+ with_alice: state.config.with_alice,
+ };
HtmlTemplate(template)
}
#[derive(Template)]
#[template(path = "settings/privacy/index.html")]
-struct SettingsTemplate {}
+struct SettingsTemplate {
+ with_alice: Option<bool>,
+}
diff --git a/core/src/api/index.rs b/core/src/api/index.rs
index a4058b6a..52b10c98 100644
--- a/core/src/api/index.rs
+++ b/core/src/api/index.rs
@@ -14,17 +14,20 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+use std::sync::Arc;
+
use super::{
optics::{OpticLink, DEFAULT_OPTICS},
- HtmlTemplate,
+ HtmlTemplate, State,
};
use askama::Template;
-use axum::response::IntoResponse;
+use axum::{extract, response::IntoResponse};
#[allow(clippy::unused_async)]
-pub async fn route() -> impl IntoResponse {
+pub async fn route(extract::State(state): extract::State<Arc<State>>) -> impl IntoResponse {
let template = IndexTemplate {
default_optics: DEFAULT_OPTICS.to_vec(),
+ with_alice: state.config.with_alice,
};
HtmlTemplate(template)
}
@@ -33,4 +36,5 @@ pub async fn route() -> impl IntoResponse {
#[template(path = "index.html")]
struct IndexTemplate {
default_optics: Vec<OpticLink>,
+ with_alice: Option<bool>,
}
diff --git a/core/src/api/mod.rs b/core/src/api/mod.rs
index 5e7576c8..4f1ca64a 100644
--- a/core/src/api/mod.rs
+++ b/core/src/api/mod.rs
@@ -68,6 +68,7 @@ mod webgraph;
pub struct HtmlTemplate<T>(T);
pub struct State {
+ pub config: FrontendConfig,
pub searcher: FrontendSearcher,
pub remote_webgraph: RemoteWebgraph,
pub autosuggest: Autosuggest,
@@ -147,6 +148,7 @@ pub async fn router(
FrontendSearcher::new(cluster.clone(), crossencoder, lambda_model, qa_model, bangs);
let state = Arc::new(State {
+ config: config.clone(),
searcher,
autosuggest,
search_counter_success,
@@ -170,8 +172,8 @@ pub async fn router(
.route("/autosuggest/browser", get(autosuggest::browser))
.route("/favicon.ico", get(favicon))
.route("/explore", get(explore::route))
- .route("/chat", get(chat::route))
.route("/explore/export", get(explore::export))
+ .route("/chat", get(chat::route))
.route("/about", get(about::route))
.route("/settings", get(optics::route))
.route("/settings/optics", get(optics::route))
diff --git a/core/src/api/optics.rs b/core/src/api/optics.rs
index 27c0ace9..0d0f3cfb 100644
--- a/core/src/api/optics.rs
+++ b/core/src/api/optics.rs
@@ -14,9 +14,11 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use super::HtmlTemplate;
+use std::sync::Arc;
+
+use super::{HtmlTemplate, State};
use askama::Template;
-use axum::response::IntoResponse;
+use axum::{extract, response::IntoResponse};
pub const DEFAULT_OPTICS: [OpticLink; 7] = [
OpticLink {
@@ -65,9 +67,10 @@ pub struct OpticLink {
}
#[allow(clippy::unused_async)]
-pub async fn route() -> impl IntoResponse {
+pub async fn route(extract::State(state): extract::State<Arc<State>>) -> impl IntoResponse {
let template = OpticsTemplate {
default_optics: DEFAULT_OPTICS.to_vec(),
+ with_alice: state.config.with_alice,
};
HtmlTemplate(template)
}
@@ -76,4 +79,5 @@ pub async fn route() -> impl IntoResponse {
#[template(path = "settings/index.html")]
struct OpticsTemplate {
default_optics: Vec<OpticLink>,
+ with_alice: Option<bool>,
}
diff --git a/core/src/api/privacy.rs b/core/src/api/privacy.rs
index 918c7209..5388bf69 100644
--- a/core/src/api/privacy.rs
+++ b/core/src/api/privacy.rs
@@ -14,16 +14,22 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use super::HtmlTemplate;
+use std::sync::Arc;
+
+use super::{HtmlTemplate, State};
use askama::Template;
-use axum::response::IntoResponse;
+use axum::{extract, response::IntoResponse};
#[allow(clippy::unused_async)]
-pub async fn route() -> impl IntoResponse {
- let template = PrivacyTemplate {};
+pub async fn route(extract::State(state): extract::State<Arc<State>>) -> impl IntoResponse {
+ let template = PrivacyTemplate {
+ with_alice: state.config.with_alice,
+ };
HtmlTemplate(template)
}
#[derive(Template)]
#[template(path = "privacy-and-happy-lawyers/index.html")]
-struct PrivacyTemplate {}
+struct PrivacyTemplate {
+ with_alice: Option<bool>,
+}
diff --git a/core/src/api/search.rs b/core/src/api/search.rs
index 2909a465..60f31e90 100644
--- a/core/src/api/search.rs
+++ b/core/src/api/search.rs
@@ -64,6 +64,7 @@ struct SearchTemplate {
has_code: bool,
alerts: Vec<String>,
query_url_part: String,
+ with_alice: Option<bool>,
}
enum RegionSelection {
@@ -174,6 +175,7 @@ pub async fn route(
let current_page = skip_pages + 1;
let template = SearchTemplate {
+ with_alice: state.config.with_alice,
search_result: result.webpages,
discussions: result.discussions,
query,
diff --git a/core/src/api/sites.rs b/core/src/api/sites.rs
index 3c7a8fe8..6f718374 100644
--- a/core/src/api/sites.rs
+++ b/core/src/api/sites.rs
@@ -14,17 +14,23 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use super::HtmlTemplate;
+use std::sync::Arc;
+
+use super::{HtmlTemplate, State};
use askama::Template;
-use axum::response::IntoResponse;
+use axum::{extract, response::IntoResponse};
#[allow(clippy::unused_async)]
-pub async fn route() -> impl IntoResponse {
- let template = SitesTemplate {};
+pub async fn route(extract::State(state): extract::State<Arc<State>>) -> impl IntoResponse {
+ let template = SitesTemplate {
+ with_alice: state.config.with_alice,
+ };
HtmlTemplate(template)
}
#[derive(Template)]
#[template(path = "settings/sites/index.html")]
-struct SitesTemplate {}
+struct SitesTemplate {
+ with_alice: Option<bool>,
+}
diff --git a/core/src/lib.rs b/core/src/lib.rs
index b38b2aea..65ab5e20 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -170,6 +170,7 @@ pub struct FrontendConfig {
pub crossencoder_model_path: String,
pub lambda_model_path: Option<String>,
pub qa_model_path: Option<String>,
+ pub with_alice: Option<bool>,
pub bangs_path: String,
pub summarizer_path: String,
pub fact_check_model_path: String,
diff --git a/frontend/src/components/Header.astro b/frontend/src/components/Header.astro
index efec9865..46cf77bd 100644
--- a/frontend/src/components/Header.astro
+++ b/frontend/src/components/Header.astro
@@ -4,12 +4,13 @@ import IndexBar from "./IndexBar.astro";
export interface Props {
activeIndex?: number | null;
+ withAlice?: string;
showDivider?: boolean;
showLogo?: boolean;
queryUrlPart?: string;
}
-const { activeIndex, queryUrlPart, showDivider = false, showLogo = true } = Astro.props;
+let { activeIndex, queryUrlPart, withAlice, showDivider = false, showLogo = true } = Astro.props;
---
<div
@@ -17,7 +18,7 @@ const { activeIndex, queryUrlPart, showDivider = false, showLogo = true } = Astr
>
<div>
<div class="flex md:space-x-2 lg:space-x-4 text-sm relative z-0 bottom-0">
- <IndexBar active={activeIndex} queryUrlPart={queryUrlPart} />
+ <IndexBar active={activeIndex} queryUrlPart={queryUrlPart} withAlice={withAlice} />
</div>
</div>
{showLogo && (
diff --git a/frontend/src/components/IndexBar.astro b/frontend/src/components/IndexBar.astro
index 289886c2..d2a63768 100644
--- a/frontend/src/components/IndexBar.astro
+++ b/frontend/src/components/IndexBar.astro
@@ -1,27 +1,44 @@
---
-const links = [
- { url: "/search", title: "Search" },
- { url: "/explore", title: "Explore" },
- { url: "/chat", title: "Chat" },
-];
+import { askama } from "../askama";
export interface Props {
active?: number | null;
queryUrlPart?: string;
+ withAlice?: string,
}
-const { queryUrlPart: query, active = null } = Astro.props;
+const { queryUrlPart: query, withAlice, active = null } = Astro.props;
+
+const links = [
+ { url: "/search", title: "Search" },
+ { url: "/explore", title: "Explore" },
+ { url: "/chat", title: "Chat" }
+];
---
{
links.map((l, idx) => (
- <div class="inline relative z-0">
- <a class="link px-2 py-1 rounded-full text-sm" href={l.url + (query ? "?" + query : "")}>
- {l.title}
- </a>
- {active === idx && (
- <div class="w-full h-[1px] absolute left-0 right-0 bottom-[-11px] bg-brand_contrast"></div>
- )}
- </div>
+ <>
+ {l.url == "/chat" && (askama.if_("with_alice.unwrap_or(true)", () => (
+ <div class="inline relative z-0">
+ <a class="link px-2 py-1 rounded-full text-sm" href={l.url + (query ? "?" + query : "")}>
+ {l.title}
+ </a>
+ {active === idx && (
+ <div class="w-full h-[1px] absolute left-0 right-0 bottom-[-11px] bg-brand_contrast"></div>
+ )}
+ </div>
+ ))
+ ) || (
+ <div class="inline relative z-0">
+ <a class="link px-2 py-1 rounded-full text-sm" href={l.url + (query ? "?" + query : "")}>
+ {l.title}
+ </a>
+ {active === idx && (
+ <div class="w-full h-[1px] absolute left-0 right-0 bottom-[-11px] bg-brand_contrast"></div>
+ )}
+ </div>
+ )}
+ </>
))
}
<style>
diff --git a/frontend/src/layouts/Markdown.astro b/frontend/src/layouts/Markdown.astro
index e496566b..698851a2 100644
--- a/frontend/src/layouts/Markdown.astro
+++ b/frontend/src/layouts/Markdown.astro
@@ -1,4 +1,5 @@
---
+import { askama } from "../askama";
import Footer from "../components/Footer.astro";
import Header from "../components/Header.astro";
import Layout from "../layouts/Layout.astro";
@@ -12,7 +13,7 @@ const { frontmatter } = Astro.props;
<Layout title={frontmatter.title}>
<div class="flex h-full w-full flex-col">
- <Header />
+ <Header withAlice={askama`with_alice`} />
<div class="flex h-fit w-full flex-col items-center pt-10 px-5">
<div
diff --git a/frontend/src/layouts/Settings.astro b/frontend/src/layouts/Settings.astro
index b8c43c74..a76fec88 100644
--- a/frontend/src/layouts/Settings.astro
+++ b/frontend/src/layouts/Settings.astro
@@ -1,4 +1,5 @@
---
+import { askama } from "../askama";
import Footer from "../components/Footer.astro";
import Layout from "./Layout.astro";
import Header from "../components/Header.astro";
@@ -6,7 +7,7 @@ import SettingsMenu from "../components/SettingsMenu.astro";
---
<Layout title="Stract">
<div class="flex h-full w-full flex-col">
- <Header />
+ <Header withAlice={askama`with_alice`} />
<div class="flex h-fit w-full justify-center pt-10">
<SettingsMenu />
diff --git a/frontend/src/pages/chat.astro b/frontend/src/pages/chat.astro
index a1f6ced6..f3425ec2 100644
--- a/frontend/src/pages/chat.astro
+++ b/frontend/src/pages/chat.astro
@@ -11,7 +11,7 @@ import OpticSelector from "../components/OpticSelector.astro";
<Layout title="Stract">
<div class="relative grid h-full grid-rows-[auto_1fr_auto]">
<div id="header" class="row-start-1">
- <Header showDivider={true} activeIndex={2} queryUrlPart={askama`query_url_part $ ""`} />
+ <Header withAlice={askama`with_alice`} showDivider={true} activeIndex={2} queryUrlPart={askama`query_url_part $ ""`} />
</div>
<div class="flex flex-col row-start-2 mt-10 px-2 items-center">
<div class="max-w-3xl w-full gap-y-5 flex flex-col h-full">
diff --git a/frontend/src/pages/explore.astro b/frontend/src/pages/explore.astro
index 82ca0949..4f0ef457 100644
--- a/frontend/src/pages/explore.astro
+++ b/frontend/src/pages/explore.astro
@@ -9,7 +9,7 @@ import { askama } from "../askama";
<Layout title="Stract">
<div class="relative grid h-full grid-rows-[auto_1fr_auto]">
<div class="row-start-1">
- <Header showDivider={true} activeIndex={1} queryUrlPart={askama`query_url_part $ ""`} />
+ <Header withAlice={askama`with_alice`} showDivider={true} activeIndex={1} queryUrlPart={askama`query_url_part $ ""`} />
</div>
<div class="row-start-2 flex mt-10 px-5 justify-center">
<div class="flex flex-col grow max-w-3xl">
diff --git a/frontend/src/pages/index.astro b/frontend/src/pages/index.astro
index 7b749424..054e54aa 100644
--- a/frontend/src/pages/index.astro
+++ b/frontend/src/pages/index.astro
@@ -1,4 +1,5 @@
---
+import { askama } from "../askama";
import Footer from "../components/Footer.astro";
import Header from "../components/Header.astro";
import Searchbar from "../components/Searchbar.astro";
@@ -8,7 +9,7 @@ import OpticSelector from "../components/OpticSelector.astro";
<Layout title="Stract">
<div class="grid h-full grid-rows-[auto_1fr_auto]">
- <Header showLogo={false} />
+ <Header withAlice={askama`with_alice`} showLogo={false} />
<div class="absolute top-1/4 grid w-full place-items-center">
<div
class="flex w-full flex-col items-center justify-center px-2 md:max-w-2xl md:px-0"
diff --git a/frontend/src/pages/search.astro b/frontend/src/pages/search.astro
index 79c0aa8a..fd730ac2 100644
--- a/frontend/src/pages/search.astro
+++ b/frontend/src/pages/search.astro
@@ -16,7 +16,7 @@ import Searchbar from "../components/Searchbar.astro";
<Layout title="{{ query }} - Stract">
<main class="flex w-full flex-col">
- <Header showDivider={true} activeIndex={0} queryUrlPart={askama`query_url_part $ ""`} />
+ <Header withAlice={askama`with_alice`} showDivider={true} activeIndex={0} queryUrlPart={askama`query_url_part $ ""`} />
<div
class="search-content flex w-screen m-0 grid gap-y-6 pt-4 px-5 md:grid-cols-[minmax(50ch,48rem)_1fr] md:grid-rows-[auto_1fr] md:gap-x-12 md:pl-20 lg:px-36"