feat: initial make-space full-stack release with weighted multi-provider LLM and Dockerfile
This commit is contained in:
commit
37b323c9ac
55 files changed
+17087
No files matched your search
@@ -0,0 +1,32 @@
|
||||
[package]
|
||||
name = "server"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "server"
|
||||
path = "src/main.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "precompute"
|
||||
path = "src/bin/precompute.rs"
|
||||
|
||||
[dependencies]
|
||||
app = { workspace = true, features = ["ssr"] }
|
||||
domain = { workspace = true }
|
||||
pipeline = { workspace = true }
|
||||
storage = { workspace = true }
|
||||
providers = { workspace = true }
|
||||
llm = { workspace = true }
|
||||
leptos = { workspace = true, features = ["ssr"] }
|
||||
leptos_axum = { workspace = true }
|
||||
axum = { workspace = true }
|
||||
tower-http = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
tokio-stream = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
tracing-subscriber = { workspace = true }
|
||||
dotenvy = { workspace = true }
|
||||
@@ -0,0 +1,49 @@
|
||||
//! Precompute and cache 3D layouts for demo queries.
|
||||
//!
|
||||
//! Usage:
|
||||
//! cargo run --bin precompute -- "성수 대림창고" "어니언 성수" "블루보틀 성수"
|
||||
|
||||
#[path = "../config.rs"]
|
||||
mod config;
|
||||
|
||||
use domain::Place;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let _ = dotenvy::dotenv();
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| "info,pipeline=debug,providers=debug".into()),
|
||||
)
|
||||
.init();
|
||||
|
||||
let pipeline = config::init_pipeline_from_env().await?;
|
||||
pipeline.store.health_check().await?;
|
||||
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let queries: Vec<String> = if args.is_empty() {
|
||||
vec!["성수 대림창고".into(), "어니언 성수".into(), "블루보틀 성수".into()]
|
||||
} else {
|
||||
args
|
||||
};
|
||||
|
||||
for q in queries {
|
||||
tracing::info!(query = %q, "searching place");
|
||||
let candidates = pipeline.search_places(&q).await?;
|
||||
let Some(first) = candidates.into_iter().next() else {
|
||||
tracing::warn!(query = %q, "no candidates found");
|
||||
continue;
|
||||
};
|
||||
let place = Place::from_candidate(&first);
|
||||
tracing::info!(place_id = %place.id, name = %place.name, "running full analysis");
|
||||
let layout = pipeline.run_sync(place).await?;
|
||||
tracing::info!(
|
||||
place_id = %layout.place_id,
|
||||
floors = layout.floors.len(),
|
||||
confidence = layout.confidence,
|
||||
"layout saved"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
use llm::{LlmClient, LlmProviderConfig, ProviderKind};
|
||||
use pipeline::{Pipeline, PipelineConfig};
|
||||
use storage::{StorageConfig, Store};
|
||||
|
||||
fn env_or(key: &str, default: &str) -> String {
|
||||
std::env::var(key)
|
||||
.ok()
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or_else(|| default.to_string())
|
||||
}
|
||||
|
||||
fn init_llm_from_env() -> anyhow::Result<LlmClient> {
|
||||
if let Ok(raw_json) = std::env::var("LLM_PROVIDERS") {
|
||||
let trimmed = raw_json.trim();
|
||||
if !trimmed.is_empty() {
|
||||
let client = LlmClient::from_json_str(trimmed)?;
|
||||
let summary: Vec<String> = client
|
||||
.providers()
|
||||
.iter()
|
||||
.map(|p| format!("{}:{} (weight={})", p.provider.as_str(), p.model, p.weight))
|
||||
.collect();
|
||||
tracing::info!(providers = ?summary, "initialized multi-provider LLM client from LLM_PROVIDERS");
|
||||
return Ok(client);
|
||||
}
|
||||
}
|
||||
|
||||
let provider_str = env_or("LLM_PROVIDER", &env_or("PROVIDER", "cpa"));
|
||||
let provider = match provider_str.to_lowercase().as_str() {
|
||||
"strata" => ProviderKind::Strata,
|
||||
"cpa" | "cliproxy" | "cli-proxy" => ProviderKind::Cpa,
|
||||
other => ProviderKind::Custom(other.to_string()),
|
||||
};
|
||||
let weight: u32 = env_or("LLM_WEIGHT", &env_or("WEIGHT", "1"))
|
||||
.parse()
|
||||
.unwrap_or(1);
|
||||
|
||||
LlmClient::from_providers(vec![LlmProviderConfig {
|
||||
provider,
|
||||
endpoint: env_or(
|
||||
"OPENAI_COMPATIBLE_API_ENDPOINT",
|
||||
"http://126.dahyeon.us:8317",
|
||||
),
|
||||
api_key: env_or("OPENAI_COMPATIBLE_API_KEY", ""),
|
||||
model: env_or("LLM_MODEL", &env_or("MODEL", "gpt-6.1-sol")),
|
||||
weight,
|
||||
}])
|
||||
}
|
||||
|
||||
pub async fn init_pipeline_from_env() -> anyhow::Result<Pipeline> {
|
||||
let _ = dotenvy::dotenv();
|
||||
|
||||
let storage_cfg = StorageConfig {
|
||||
mongodb_uri: env_or(
|
||||
"MONGODB_CONNECTION_STRING",
|
||||
&env_or("MONGODB_URI", "mongodb://localhost:27017"),
|
||||
),
|
||||
mongodb_db: env_or("MONGODB_DB", "make_space"),
|
||||
minio_endpoint: env_or("MINIO_ENDPOINT", "http://localhost:9000"),
|
||||
minio_access_key: env_or("MINIO_ACCESS_KEY", "minioadmin"),
|
||||
minio_secret_key: env_or("MINIO_SECRET_KEY", "minioadmin"),
|
||||
minio_bucket: env_or("MINIO_BUCKET", "make-space-photos"),
|
||||
};
|
||||
let store = Store::connect(&storage_cfg).await?;
|
||||
if let Err(e) = store.ensure_indexes().await {
|
||||
tracing::warn!("MongoDB index creation skipped (is MongoDB reachable?): {e}");
|
||||
}
|
||||
|
||||
let llm = init_llm_from_env()?;
|
||||
let health_results = llm.health_check_all().await;
|
||||
for h in &health_results {
|
||||
if h.healthy {
|
||||
tracing::info!(
|
||||
provider = h.provider.as_str(),
|
||||
endpoint = %h.endpoint,
|
||||
model = %h.model,
|
||||
weight = h.weight,
|
||||
latency_ms = h.latency_ms,
|
||||
detail = %h.detail,
|
||||
"LLM provider health check passed"
|
||||
);
|
||||
} else {
|
||||
tracing::warn!(
|
||||
provider = h.provider.as_str(),
|
||||
endpoint = %h.endpoint,
|
||||
model = %h.model,
|
||||
weight = h.weight,
|
||||
latency_ms = h.latency_ms,
|
||||
error = %h.detail,
|
||||
"LLM provider health check failed (will remain in failover pool)"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let pipe_cfg = PipelineConfig {
|
||||
kakao_api_key: std::env::var("KAKAO_REST_API_KEY").unwrap_or_default(),
|
||||
ncp_client_id: env_or(
|
||||
"X-NCP-APIGW-API-KEY-ID",
|
||||
&env_or("X_NCP_APIGW_API_KEY_ID", &env_or("NCP_CLIENT_ID", "")),
|
||||
),
|
||||
ncp_client_secret: env_or(
|
||||
"X-NCP-APIGW-API-KEY",
|
||||
&env_or("X_NCP_APIGW_API_KEY", &env_or("NCP_CLIENT_SECRET", "")),
|
||||
),
|
||||
browser_scraper_url: std::env::var("BROWSER_SCRAPER_URL")
|
||||
.ok()
|
||||
.filter(|s| !s.trim().is_empty()),
|
||||
max_photos_per_source: env_or("MAX_PHOTOS_PER_SOURCE", "30").parse().unwrap_or(30),
|
||||
max_photos_total: env_or("MAX_PHOTOS_TOTAL", "80").parse().unwrap_or(80),
|
||||
};
|
||||
|
||||
Ok(Pipeline::new(store, llm, pipe_cfg))
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
mod config;
|
||||
|
||||
use std::convert::Infallible;
|
||||
use std::time::Duration;
|
||||
|
||||
use axum::extract::{Path, State};
|
||||
use axum::http::{header, StatusCode};
|
||||
use axum::response::sse::{Event, KeepAlive, Sse};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
use domain::JobEvent;
|
||||
use futures::stream::{self, StreamExt};
|
||||
use leptos::config::get_configuration;
|
||||
use leptos::prelude::provide_context;
|
||||
use leptos_axum::{generate_route_list, LeptosRoutes};
|
||||
use pipeline::Pipeline;
|
||||
use tokio_stream::wrappers::BroadcastStream;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let _ = dotenvy::dotenv();
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| "info,server=debug,pipeline=debug,providers=debug".into()),
|
||||
)
|
||||
.init();
|
||||
|
||||
let pipeline = config::init_pipeline_from_env().await?;
|
||||
let conf = get_configuration(None)?;
|
||||
let leptos_options = conf.leptos_options;
|
||||
let addr = leptos_options.site_addr;
|
||||
let routes = generate_route_list(app::App);
|
||||
|
||||
let pipe_for_ctx = pipeline.clone();
|
||||
let app_router = Router::new()
|
||||
.route("/health/live", get(health_live))
|
||||
.route("/health/ready", get(health_check))
|
||||
.route("/api/health", get(health_check))
|
||||
.route("/api/jobs/{id}/events", get(job_events_sse))
|
||||
.route("/api/photos/{id}", get(serve_photo))
|
||||
.with_state(pipeline)
|
||||
.leptos_routes_with_context(
|
||||
&leptos_options,
|
||||
routes,
|
||||
move || {
|
||||
provide_context(pipe_for_ctx.clone());
|
||||
},
|
||||
{
|
||||
let opts = leptos_options.clone();
|
||||
move || app::shell(opts.clone())
|
||||
},
|
||||
)
|
||||
.fallback(leptos_axum::file_and_error_handler(app::shell))
|
||||
.with_state(leptos_options);
|
||||
|
||||
tracing::info!("listening on http://{addr}");
|
||||
let listener = tokio::net::TcpListener::bind(&addr).await?;
|
||||
axum::serve(listener, app_router.into_make_service()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn job_events_sse(
|
||||
State(pipeline): State<Pipeline>,
|
||||
Path(job_id): Path<String>,
|
||||
) -> Sse<impl futures::Stream<Item = Result<Event, Infallible>>> {
|
||||
let (past, rx_opt) = prepare_job_subscription(pipeline.subscribe_job(&job_id).await);
|
||||
let already_done = past.iter().any(|e| e.is_terminal());
|
||||
|
||||
let past_stream = stream::iter(past).map(|ev| {
|
||||
let json = serde_json::to_string(&ev).unwrap_or_default();
|
||||
Ok::<_, Infallible>(Event::default().data(json))
|
||||
});
|
||||
|
||||
let live_stream = match rx_opt {
|
||||
Some(rx) if !already_done => BroadcastStream::new(rx)
|
||||
.filter_map(|item| async move { item.ok() })
|
||||
.map(|ev| {
|
||||
let done = ev.is_terminal();
|
||||
let json = serde_json::to_string(&ev).unwrap_or_default();
|
||||
(done, Ok::<_, Infallible>(Event::default().data(json)))
|
||||
})
|
||||
.scan(false, |finished, (done, ev)| {
|
||||
let emit = if *finished { None } else { Some(ev) };
|
||||
if done {
|
||||
*finished = true;
|
||||
}
|
||||
futures::future::ready(emit)
|
||||
})
|
||||
.left_stream(),
|
||||
_ => stream::empty().right_stream(),
|
||||
};
|
||||
|
||||
Sse::new(past_stream.chain(live_stream)).keep_alive(
|
||||
KeepAlive::new()
|
||||
.interval(Duration::from_secs(10))
|
||||
.text("keep-alive"),
|
||||
)
|
||||
}
|
||||
|
||||
type JobSubscription = (
|
||||
Vec<JobEvent>,
|
||||
Option<tokio::sync::broadcast::Receiver<JobEvent>>,
|
||||
);
|
||||
|
||||
fn prepare_job_subscription(subscription: anyhow::Result<JobSubscription>) -> JobSubscription {
|
||||
let (mut past, receiver) = match subscription {
|
||||
Ok(subscription) => subscription,
|
||||
Err(error) => {
|
||||
tracing::warn!(%error, "could not subscribe to analysis job");
|
||||
return (
|
||||
vec![JobEvent::Error {
|
||||
message: "분석 진행 상태를 불러오지 못했습니다. 잠시 후 다시 시도해 주세요."
|
||||
.into(),
|
||||
}],
|
||||
None,
|
||||
);
|
||||
}
|
||||
};
|
||||
// A stored job may outlive its process. Replaying only its nonterminal history
|
||||
// would make EventSource reconnect forever, resetting the client's retry counter.
|
||||
if receiver.is_none() && !past.iter().any(JobEvent::is_terminal) {
|
||||
let message = if past.is_empty() {
|
||||
"분석 작업을 찾을 수 없습니다. 장소에서 분석을 다시 시작해 주세요."
|
||||
} else {
|
||||
"분석 작업이 중단됐습니다. 장소에서 분석을 다시 시작해 주세요."
|
||||
};
|
||||
past.push(JobEvent::Error {
|
||||
message: message.into(),
|
||||
});
|
||||
}
|
||||
(past, receiver)
|
||||
}
|
||||
|
||||
async fn health_live() -> Response {
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(header::CONTENT_TYPE, "application/json")],
|
||||
r#"{"status":"ok"}"#,
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
async fn health_check(State(pipeline): State<Pipeline>) -> Response {
|
||||
let storage_ok = pipeline.store.health_check().await.is_ok();
|
||||
let llm_providers = pipeline.llm.health_check_all().await;
|
||||
let any_llm_healthy = llm_providers.iter().any(|p| p.healthy);
|
||||
let status = if storage_ok && any_llm_healthy {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::SERVICE_UNAVAILABLE
|
||||
};
|
||||
let body = serde_json::json!({
|
||||
"status": if status == StatusCode::OK { "ok" } else { "degraded" },
|
||||
"storage_healthy": storage_ok,
|
||||
"llm_providers": llm_providers,
|
||||
});
|
||||
(
|
||||
status,
|
||||
[(header::CONTENT_TYPE, "application/json")],
|
||||
body.to_string(),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
async fn serve_photo(State(pipeline): State<Pipeline>, Path(photo_id): Path<String>) -> Response {
|
||||
let Ok(Some(photo)) = pipeline.store.get_photo(&photo_id).await else {
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
};
|
||||
match pipeline.store.get_blob(&photo.object_key).await {
|
||||
Ok(bytes) => (
|
||||
StatusCode::OK,
|
||||
[
|
||||
(header::CONTENT_TYPE, "image/jpeg"),
|
||||
(header::CACHE_CONTROL, "public, max-age=86400"),
|
||||
],
|
||||
bytes,
|
||||
)
|
||||
.into_response(),
|
||||
Err(_) => StatusCode::NOT_FOUND.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod job_events_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn missing_job_returns_a_terminal_error() {
|
||||
let (events, receiver) = prepare_job_subscription(Ok((Vec::new(), None)));
|
||||
assert!(receiver.is_none());
|
||||
assert_eq!(events.len(), 1);
|
||||
assert!(
|
||||
matches!(&events[0], JobEvent::Error { message } if message.contains("찾을 수 없습니다"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interrupted_job_keeps_history_then_terminates() {
|
||||
let past = vec![
|
||||
JobEvent::Started {
|
||||
place_id: "place-1".into(),
|
||||
},
|
||||
JobEvent::LayoutDraft { attempt: 1 },
|
||||
];
|
||||
let (events, receiver) = prepare_job_subscription(Ok((past, None)));
|
||||
assert!(receiver.is_none());
|
||||
assert_eq!(events.len(), 3);
|
||||
assert!(matches!(events[0], JobEvent::Started { .. }));
|
||||
assert!(matches!(events[1], JobEvent::LayoutDraft { .. }));
|
||||
assert!(matches!(&events[2], JobEvent::Error { message } if message.contains("중단")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subscription_failure_returns_a_user_facing_terminal_error() {
|
||||
let (events, receiver) =
|
||||
prepare_job_subscription(Err(anyhow::anyhow!("private storage error")));
|
||||
assert!(receiver.is_none());
|
||||
assert_eq!(events.len(), 1);
|
||||
assert!(matches!(&events[0], JobEvent::Error { message } if !message.contains("private")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_job_and_finished_job_are_not_marked_as_interrupted() {
|
||||
let (_sender, receiver) = tokio::sync::broadcast::channel(2);
|
||||
let (events, receiver) = prepare_job_subscription(Ok((Vec::new(), Some(receiver))));
|
||||
assert!(events.is_empty());
|
||||
assert!(receiver.is_some());
|
||||
for terminal in [
|
||||
JobEvent::Done {
|
||||
place_id: "place-1".into(),
|
||||
},
|
||||
JobEvent::Error {
|
||||
message: "analysis failed".into(),
|
||||
},
|
||||
] {
|
||||
let (events, receiver) = prepare_job_subscription(Ok((vec![terminal], None)));
|
||||
assert!(receiver.is_none());
|
||||
assert_eq!(events.len(), 1);
|
||||
assert!(events[0].is_terminal());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user