feat: initial make-space full-stack release with weighted multi-provider LLM and Dockerfile

This commit is contained in:
dahyeon committed 2026-11-04 20:49:24 +09:00
commit 37b323c9ac
55 files changed
+17087

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
[package]
name = "domain"
edition.workspace = true
version.workspace = true
[dependencies]
serde = { workspace = true }
+174
View File
@@ -0,0 +1,174 @@
//! Layout JSON: the central artifact rendered by three.js.
//!
//! Coordinate system per floor (meters): origin at the south-west corner,
//! `x` grows east (0..width_m), `y` grows north (0..depth_m).
//! The street/entrance side is conventionally south.
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Layout {
pub place_id: String,
pub generated_at: String,
pub model: String,
pub confidence: f32,
pub floors: Vec<Floor>,
pub summary: Summary,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Floor {
pub level: i32,
pub label: String,
pub confidence: f32,
pub width_m: f32,
pub depth_m: f32,
pub openings: Vec<Opening>,
pub zones: Vec<Zone>,
pub verticals: Vec<Vertical>,
pub notes: String,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Wall {
North,
South,
East,
West,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum OpeningKind {
Entrance,
Window,
Door,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Opening {
pub kind: OpeningKind,
pub wall: Wall,
/// Distance from the wall's start (west end for N/S walls, south end for E/W walls).
pub offset_m: f32,
pub length_m: f32,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ZoneKind {
Seating,
Counter,
Bar,
Kitchen,
Restroom,
Terrace,
Entrance,
Other,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Zone {
pub kind: ZoneKind,
pub label: String,
pub x: f32,
pub y: f32,
pub w: f32,
pub d: f32,
pub furniture: Vec<FurnitureGroup>,
pub evidence_photos: Vec<String>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum FurnitureKind {
Table2,
Table4,
Table6,
CommunalTable,
BarSeat,
Sofa,
CounterBar,
Plant,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Arrangement {
Grid,
AlongWall,
Row,
Cluster,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FurnitureGroup {
pub kind: FurnitureKind,
pub count: u32,
pub arrangement: Arrangement,
pub along_wall: Option<Wall>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum VerticalKind {
Stairs,
Elevator,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Vertical {
pub kind: VerticalKind,
pub x: f32,
pub y: f32,
pub w: f32,
pub d: f32,
pub to_level: i32,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Tri {
Yes,
Likely,
No,
Unknown,
}
impl Tri {
pub fn label(&self) -> &'static str {
match self {
Tri::Yes => "있음",
Tri::Likely => "있을 가능성 높음",
Tri::No => "없음",
Tri::Unknown => "확인 안 됨",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Accessibility {
pub elevator: Tri,
pub step_free_entrance: Tri,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Summary {
pub area_pyeong_min: f32,
pub area_pyeong_max: f32,
pub seat_estimate: u32,
pub seat_types: Vec<String>,
pub highlights: Vec<String>,
pub accessibility: Accessibility,
pub caveats: Vec<String>,
}
/// Everything the place page needs in one round-trip.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PlaceView {
pub place: crate::Place,
pub layout: Option<Layout>,
pub photos: Vec<crate::PhotoSummary>,
pub running_job: Option<String>,
}
+63
View File
@@ -0,0 +1,63 @@
//! Shared domain types. Compiles for both native (server) and wasm32 (hydrate),
//! so keep dependencies limited to `serde`.
mod layout;
mod photo;
mod place;
pub use layout::*;
pub use photo::*;
pub use place::*;
use serde::{Deserialize, Serialize};
/// Progress events streamed to the browser over SSE while a place is analyzed.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum JobEvent {
Started { place_id: String },
PlaceResolved { name: String, naver_place_id: Option<String> },
SourceDone { source: PhotoSourceId, photos: usize, texts: usize, error: Option<String> },
PhotosStored { stored: usize, duplicates: usize, failed: usize },
Classified { interior: usize, exterior: usize, food: usize, other: usize },
FloorsDetected { floors: Vec<FloorEvidence> },
LayoutDraft { attempt: u32 },
Log { message: String },
Done { place_id: String },
Error { message: String },
}
impl JobEvent {
pub fn is_terminal(&self) -> bool {
matches!(self, JobEvent::Done { .. } | JobEvent::Error { .. })
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum JobStatus {
Running,
Done,
Failed,
}
/// Aggregated evidence that a given floor exists.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FloorEvidence {
/// 1 = ground floor, 2 = second floor, -1 = B1, 99 = rooftop.
pub level: i32,
pub label: String,
pub score: f32,
pub photo_ids: Vec<String>,
pub text_snippets: Vec<String>,
}
pub const ROOFTOP_LEVEL: i32 = 99;
pub fn floor_label(level: i32) -> String {
match level {
ROOFTOP_LEVEL => "루프탑".to_string(),
l if l < 0 => format!("B{}", -l),
l => format!("{l}F"),
}
}
+144
View File
@@ -0,0 +1,144 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum PhotoSourceId {
NaverPlace,
NaverBlog,
KakaoPlace,
GoogleMaps,
Instagram,
User,
}
impl PhotoSourceId {
pub fn label(&self) -> &'static str {
match self {
Self::NaverPlace => "네이버 플레이스",
Self::NaverBlog => "네이버 블로그",
Self::KakaoPlace => "카카오맵",
Self::GoogleMaps => "구글 지도",
Self::Instagram => "인스타그램",
Self::User => "사용자 업로드",
}
}
}
/// A photo reference returned by a source before download.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RawPhoto {
pub source: PhotoSourceId,
/// Direct image URL.
pub image_url: String,
/// Page where the image was found (for attribution).
pub page_url: String,
/// Text directly associated with the image (alt, caption, nearby paragraph).
pub caption: String,
/// Referer header required by some CDNs (e.g. postfiles.pstatic.net).
pub referer: Option<String>,
}
/// Free text found by a source (blog body, place description, reviews).
/// Used as evidence for floors / seating.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TextSnippet {
pub source: PhotoSourceId,
pub url: String,
pub text: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct SourceOutput {
pub photos: Vec<RawPhoto>,
pub texts: Vec<TextSnippet>,
}
/// Stored photo (MongoDB `photos`). Bytes live in MinIO under `object_key`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Photo {
#[serde(rename = "_id")]
pub id: String,
pub place_id: String,
pub source: PhotoSourceId,
pub image_url: String,
pub page_url: String,
pub caption: String,
pub object_key: String,
/// 64-bit difference hash, hex encoded (BSON has no u64).
pub dhash: String,
pub width: u32,
pub height: u32,
pub tag: Option<PhotoTag>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum PhotoKind {
Interior,
Exterior,
Food,
Menu,
Person,
Other,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum PhotoZone {
Entrance,
Counter,
Hall,
WindowSide,
Terrace,
Restroom,
Stairs,
Kitchen,
Unknown,
}
/// VLM classification of one photo.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PhotoTag {
pub kind: PhotoKind,
pub zone: PhotoZone,
/// Same convention as `FloorEvidence::level`; `None` = unknown.
pub floor_level: Option<i32>,
pub floor_evidence: String,
pub furniture: Vec<FurnitureCount>,
pub quality: f32,
pub description: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FurnitureCount {
pub kind: crate::FurnitureKind,
pub count: u32,
}
/// Lightweight photo info sent to the UI.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PhotoSummary {
pub id: String,
pub source: PhotoSourceId,
pub page_url: String,
pub caption: String,
pub kind: Option<PhotoKind>,
pub zone: Option<PhotoZone>,
pub floor_level: Option<i32>,
pub description: String,
}
impl From<&Photo> for PhotoSummary {
fn from(p: &Photo) -> Self {
Self {
id: p.id.clone(),
source: p.source,
page_url: p.page_url.clone(),
caption: p.caption.clone(),
kind: p.tag.as_ref().map(|t| t.kind),
zone: p.tag.as_ref().map(|t| t.zone),
floor_level: p.tag.as_ref().and_then(|t| t.floor_level),
description: p.tag.as_ref().map(|t| t.description.clone()).unwrap_or_default(),
}
}
}
+81
View File
@@ -0,0 +1,81 @@
use serde::{Deserialize, Serialize};
/// A search result shown to the user before they confirm the place.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PlaceCandidate {
/// Provider that produced this candidate (`kakao`, `naver`).
pub provider: String,
/// Provider-native id (Kakao place id, Naver place id).
pub provider_id: String,
pub name: String,
pub category: String,
pub address: String,
pub road_address: String,
pub phone: String,
pub lat: f64,
pub lng: f64,
pub place_url: String,
}
impl PlaceCandidate {
/// Stable internal id, e.g. `kakao:12345`.
pub fn place_id(&self) -> String {
format!("{}:{}", self.provider, self.provider_id)
}
}
/// Canonical, normalized place persisted in MongoDB.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Place {
#[serde(rename = "_id")]
pub id: String,
pub kakao_id: Option<String>,
pub naver_place_id: Option<String>,
pub name: String,
pub category: String,
pub address: String,
pub road_address: String,
pub phone: String,
pub lat: f64,
pub lng: f64,
pub place_url: String,
}
/// A saved analysis that can be reopened without starting another analysis job.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RecentPlace {
pub place: Place,
pub analyzed_at: String,
pub last_viewed_at: Option<String>,
pub floor_labels: Vec<String>,
pub seat_estimate: u32,
}
impl Place {
pub fn from_candidate(c: &PlaceCandidate) -> Self {
let (kakao_id, naver_place_id) = match c.provider.as_str() {
"kakao" => (Some(c.provider_id.clone()), None),
"naver" => (None, Some(c.provider_id.clone())),
_ => (None, None),
};
Self {
id: c.place_id(),
kakao_id,
naver_place_id,
name: c.name.clone(),
category: c.category.clone(),
address: c.address.clone(),
road_address: c.road_address.clone(),
phone: c.phone.clone(),
lat: c.lat,
lng: c.lng,
place_url: c.place_url.clone(),
}
}
/// Short region hint (e.g. "성동구 성수동") used to disambiguate web searches.
pub fn region_hint(&self) -> String {
let addr = if self.address.is_empty() { &self.road_address } else { &self.address };
addr.split_whitespace().skip(1).take(2).collect::<Vec<_>>().join(" ")
}
}
+13
View File
@@ -0,0 +1,13 @@
[package]
name = "llm"
edition.workspace = true
version.workspace = true
[dependencies]
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
anyhow = { workspace = true }
tracing = { workspace = true }
base64 = { workspace = true }
tokio = { workspace = true }
+746
View File
@@ -0,0 +1,746 @@
//! OpenAI-compatible chat client with vision input, strict JSON-schema output,
//! weighted multi-provider routing (`strata` / `cpa`), and automatic failover.
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use base64::Engine;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
/// Health probe result for a single configured LLM provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderHealth {
pub provider: ProviderKind,
pub endpoint: String,
pub model: String,
pub weight: u32,
pub healthy: bool,
pub latency_ms: u64,
pub detail: String,
}
/// Identifier for the underlying LLM inference backend.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProviderKind {
/// Local/self-hosted Strata inference engine (Niko1221/Strata).
Strata,
/// CLIProxyAPI (`cpa`) OpenAI-compatible proxy.
#[serde(alias = "cliproxy", alias = "cli-proxy")]
Cpa,
/// Fallback for any other custom OpenAI-compatible provider name.
#[serde(untagged)]
Custom(String),
}
impl ProviderKind {
pub fn as_str(&self) -> &str {
match self {
ProviderKind::Strata => "strata",
ProviderKind::Cpa => "cpa",
ProviderKind::Custom(s) => s.as_str(),
}
}
}
impl Default for ProviderKind {
fn default() -> Self {
ProviderKind::Cpa
}
}
/// Configuration for a single weighted LLM capacity provider.
/// Accepts both uppercase env-style keys (`PROVIDER`, `OPENAI_COMPATIBLE_API_ENDPOINT`,
/// `OPENAI_COMPATIBLE_API_KEY`, `MODEL`, `WEIGHT`) and lowercase keys (`provider`,
/// `endpoint`, `api_key`, `model`, `weight`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmProviderConfig {
#[serde(default, alias = "PROVIDER", alias = "provider")]
pub provider: ProviderKind,
#[serde(
alias = "OPENAI_COMPATIBLE_API_ENDPOINT",
alias = "endpoint",
alias = "ENDPOINT",
alias = "url"
)]
pub endpoint: String,
#[serde(
default,
alias = "OPENAI_COMPATIBLE_API_KEY",
alias = "api_key",
alias = "API_KEY",
alias = "key",
alias = "KEY"
)]
pub api_key: String,
#[serde(alias = "MODEL", alias = "LLM_MODEL", alias = "model")]
pub model: String,
#[serde(default = "default_weight", alias = "WEIGHT", alias = "weight")]
pub weight: u32,
}
fn default_weight() -> u32 {
1
}
impl LlmProviderConfig {
/// Display tag such as `strata/qwen3.8-flash-next` or `cpa/gpt-6.1-sol`.
pub fn display_model(&self) -> String {
format!("{}/{}", self.provider.as_str(), self.model)
}
fn normalized(mut self) -> Self {
let ep = self.endpoint.trim().trim_end_matches('/');
self.endpoint = ep.strip_suffix("/v1").unwrap_or(ep).to_string();
self.api_key = self.api_key.trim().to_string();
self.model = self.model.trim().to_string();
self
}
}
/// Successful JSON response together with the provider/model that served it.
#[derive(Debug, Clone)]
pub struct ChatJsonOutput<T> {
pub data: T,
pub provider: ProviderKind,
pub model: String,
}
impl<T> ChatJsonOutput<T> {
/// Formatted tag (`{provider}/{model}`) suitable for storing in `Layout.model`.
pub fn model_tag(&self) -> String {
format!("{}/{}", self.provider.as_str(), self.model)
}
}
#[derive(Clone)]
pub struct LlmClient {
client: reqwest::Client,
providers: Arc<Vec<LlmProviderConfig>>,
/// Latest known health flag per provider (`true` = healthy, initialized to `true`).
health_flags: Arc<Vec<AtomicBool>>,
/// Weighted schedule table of indices into `providers` (e.g. `[0, 0, 1]` for weights 2, 1).
schedule: Arc<Vec<usize>>,
cursor: Arc<AtomicUsize>,
/// Primary model name kept for backwards compatibility (`{provider}/{model}`).
pub model: String,
}
/// One part of a multimodal user message.
#[derive(Debug, Clone)]
pub enum Part {
Text(String),
/// Raw image bytes + mime type; sent as a base64 data URL.
Image { bytes: Vec<u8>, mime: String },
}
impl Part {
pub fn text(s: impl Into<String>) -> Self {
Part::Text(s.into())
}
fn to_json(&self) -> Value {
match self {
Part::Text(t) => json!({ "type": "text", "text": t }),
Part::Image { bytes, mime } => {
let b64 = base64::engine::general_purpose::STANDARD.encode(bytes);
json!({ "type": "image_url", "image_url": { "url": format!("data:{mime};base64,{b64}") } })
}
}
}
}
#[derive(Debug, Serialize, Clone)]
struct Message {
role: &'static str,
content: Value,
}
impl LlmClient {
/// Construct a single-provider client (weight = 1).
pub fn new(endpoint: impl Into<String>, key: impl Into<String>, model: impl Into<String>) -> Self {
Self::from_providers(vec![LlmProviderConfig {
provider: ProviderKind::Cpa,
endpoint: endpoint.into(),
api_key: key.into(),
model: model.into(),
weight: 1,
}])
.expect("single provider config is always non-empty")
}
/// Construct a weighted multi-provider client from a list of `LlmProviderConfig`s.
/// Providers with `weight == 0` are kept only as failover backups unless all weights are 0.
pub fn from_providers(configs: Vec<LlmProviderConfig>) -> anyhow::Result<Self> {
let providers: Vec<LlmProviderConfig> = configs
.into_iter()
.map(LlmProviderConfig::normalized)
.filter(|p| !p.endpoint.is_empty() && !p.model.is_empty())
.collect();
if providers.is_empty() {
anyhow::bail!("at least one valid LLM provider must be configured");
}
let schedule = build_interleaved_schedule(&providers);
let primary_idx = schedule.first().copied().unwrap_or(0);
let model = providers[primary_idx].display_model();
let health_flags = providers.iter().map(|_| AtomicBool::new(true)).collect();
Ok(Self {
client: reqwest::Client::builder()
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(240))
.build()
.expect("reqwest client"),
providers: Arc::new(providers),
health_flags: Arc::new(health_flags),
schedule: Arc::new(schedule),
cursor: Arc::new(AtomicUsize::new(0)),
model,
})
}
/// Parse a JSON array string of `LlmProviderConfig`s.
pub fn from_json_str(raw: &str) -> anyhow::Result<Self> {
let configs: Vec<LlmProviderConfig> = serde_json::from_str(raw.trim())
.map_err(|e| anyhow::anyhow!("invalid LLM_PROVIDERS JSON: {e}"))?;
Self::from_providers(configs)
}
pub fn providers(&self) -> &[LlmProviderConfig] {
&self.providers
}
/// Run provider-specific health checks across all configured LLM providers in parallel
/// and update their internal health flags used by weighted routing:
/// - `strata`: probes `GET {endpoint}/health` (verifies `status == "ok"` and `loaded != false`,
/// with fallback to `GET {endpoint}/v1/models` if `/health` returns 404).
/// - `cpa` (CLIProxyAPI) / custom: probes `GET {endpoint}/v1/models` with Bearer token
/// (verifies HTTP 2xx and checks whether the configured model is registered).
pub async fn health_check_all(&self) -> Vec<ProviderHealth> {
let mut results = Vec::with_capacity(self.providers.len());
for (idx, provider) in self.providers.iter().enumerate() {
let status = self.check_provider_health(provider).await;
self.health_flags[idx].store(status.healthy, Ordering::Relaxed);
results.push(status);
}
results
}
/// Probe a single provider's health according to its `ProviderKind`.
pub async fn check_provider_health(&self, provider: &LlmProviderConfig) -> ProviderHealth {
let started = Instant::now();
let probe_res = match provider.provider {
ProviderKind::Strata => self.probe_strata_health(provider).await,
ProviderKind::Cpa | ProviderKind::Custom(_) => self.probe_cpa_health(provider).await,
};
let latency_ms = started.elapsed().as_millis() as u64;
match probe_res {
Ok(detail) => ProviderHealth {
provider: provider.provider.clone(),
endpoint: provider.endpoint.clone(),
model: provider.model.clone(),
weight: provider.weight,
healthy: true,
latency_ms,
detail,
},
Err(err) => ProviderHealth {
provider: provider.provider.clone(),
endpoint: provider.endpoint.clone(),
model: provider.model.clone(),
weight: provider.weight,
healthy: false,
latency_ms,
detail: err.to_string(),
},
}
}
/// Strata health check:
/// 1. `GET {endpoint}/health` (Strata native endpoint returning `{"status":"ok","loaded":true,"model":...,"images":true}`)
/// 2. Falls back to `/v1/models` if `/health` is not exposed (404).
async fn probe_strata_health(&self, provider: &LlmProviderConfig) -> anyhow::Result<String> {
let url = format!("{}/health", provider.endpoint);
let mut req = self.client.get(&url).timeout(Duration::from_secs(5));
if !provider.api_key.is_empty() {
req = req.bearer_auth(&provider.api_key);
}
let resp = req.send().await.map_err(|e| anyhow::anyhow!("Strata /health unreachable: {e}"))?;
let status = resp.status();
if status == reqwest::StatusCode::NOT_FOUND {
return self.probe_cpa_health(provider).await;
}
let text = resp.text().await.unwrap_or_default();
if !status.is_success() {
anyhow::bail!("Strata /health HTTP {status}: {}", truncate(&text, 200));
}
if let Ok(v) = serde_json::from_str::<Value>(&text) {
if let Some(st) = v["status"].as_str() {
if !st.eq_ignore_ascii_case("ok") && !st.eq_ignore_ascii_case("healthy") && !st.eq_ignore_ascii_case("ready") {
anyhow::bail!("Strata status is '{st}': {}", truncate(&text, 200));
}
}
if v["loaded"].as_bool() == Some(false) {
anyhow::bail!("Strata engine running but model not loaded yet (loaded=false)");
}
let engine_model = v["model"].as_str().unwrap_or(&provider.model);
let vision = v["images"].as_bool().map(|b| format!(", images={b}")).unwrap_or_default();
return Ok(format!("Strata ready (model={engine_model}{vision})"));
}
Ok("Strata /health ok".into())
}
/// CLIProxyAPI (`cpa`) health check:
/// Queries `GET {endpoint}/v1/models` with Bearer auth and verifies the model list response.
async fn probe_cpa_health(&self, provider: &LlmProviderConfig) -> anyhow::Result<String> {
let url = format!("{}/v1/models", provider.endpoint);
let mut req = self.client.get(&url).timeout(Duration::from_secs(5));
if !provider.api_key.is_empty() {
req = req.bearer_auth(&provider.api_key);
}
let resp = req.send().await.map_err(|e| {
anyhow::anyhow!("{} /v1/models unreachable: {e}", provider.provider.as_str())
})?;
let status = resp.status();
let text = resp.text().await.unwrap_or_default();
if !status.is_success() {
anyhow::bail!(
"{} /v1/models HTTP {status}: {}",
provider.provider.as_str(),
truncate(&text, 200)
);
}
if let Ok(v) = serde_json::from_str::<Value>(&text) {
if let Some(arr) = v["data"].as_array() {
let ids: Vec<&str> = arr.iter().filter_map(|m| m["id"].as_str()).collect();
if !ids.is_empty() && !ids.iter().any(|&id| id == provider.model) {
return Ok(format!(
"/v1/models ok ({} models online; '{}' not listed in registry, will route directly)",
ids.len(),
provider.model
));
}
return Ok(format!("/v1/models ok ({} models, '{}' available)", ids.len(), provider.model));
}
}
Ok("/v1/models ok".into())
}
/// Pick the ordered list of provider indices to try for the next request:
/// starts with the weighted round-robin winner among currently healthy providers,
/// followed by remaining healthy providers, and finally any unhealthy providers as last-resort failover.
fn pick_provider_order(&self) -> Vec<usize> {
let step = self.cursor.fetch_add(1, Ordering::Relaxed);
let mut healthy_order = Vec::with_capacity(self.providers.len());
let mut unhealthy_order = Vec::new();
// Walk the weighted schedule starting from `step` so the 2:1 ratio is preserved.
for offset in 0..self.schedule.len() {
let idx = self.schedule[(step + offset) % self.schedule.len()];
if self.health_flags[idx].load(Ordering::Relaxed) {
if !healthy_order.contains(&idx) {
healthy_order.push(idx);
}
} else if !unhealthy_order.contains(&idx) {
unhealthy_order.push(idx);
}
}
// Include any 0-weight backup providers not present in `schedule`.
for idx in 0..self.providers.len() {
if self.health_flags[idx].load(Ordering::Relaxed) {
if !healthy_order.contains(&idx) {
healthy_order.push(idx);
}
} else if !unhealthy_order.contains(&idx) {
unhealthy_order.push(idx);
}
}
healthy_order.extend(unhealthy_order);
healthy_order
}
/// Send `system` + multimodal `user` parts and parse the reply into `T`
/// using a strict JSON schema.
pub async fn chat_json<T: DeserializeOwned>(
&self,
system: &str,
user: &[Part],
schema_name: &str,
schema: &Value,
) -> anyhow::Result<T> {
Ok(self
.chat_json_with_meta(system, user, schema_name, schema)
.await?
.data)
}
/// Same as `chat_json`, but also returns which provider (`strata` / `cpa`) and model served the request.
/// Automatically fails over to the next configured provider if the chosen provider fails.
pub async fn chat_json_with_meta<T: DeserializeOwned>(
&self,
system: &str,
user: &[Part],
schema_name: &str,
schema: &Value,
) -> anyhow::Result<ChatJsonOutput<T>> {
let order = self.pick_provider_order();
let mut last_err: Option<anyhow::Error> = None;
for (attempt_idx, &prov_idx) in order.iter().enumerate() {
let provider = &self.providers[prov_idx];
match self
.chat_json_on_provider::<T>(provider, system, user, schema_name, schema)
.await
{
Ok(data) => {
self.health_flags[prov_idx].store(true, Ordering::Relaxed);
return Ok(ChatJsonOutput {
data,
provider: provider.provider.clone(),
model: provider.model.clone(),
});
}
Err(err) => {
self.health_flags[prov_idx].store(false, Ordering::Relaxed);
if attempt_idx + 1 < order.len() {
let next_prov = &self.providers[order[attempt_idx + 1]];
tracing::warn!(
failed_provider = provider.provider.as_str(),
failed_model = %provider.model,
failover_provider = next_prov.provider.as_str(),
failover_model = %next_prov.model,
error = %err,
"LLM provider call failed, failing over to next provider"
);
}
last_err = Some(err);
}
}
}
Err(last_err.unwrap_or_else(|| anyhow::anyhow!("no LLM providers available")))
}
async fn chat_json_on_provider<T: DeserializeOwned>(
&self,
provider: &LlmProviderConfig,
system: &str,
user: &[Part],
schema_name: &str,
schema: &Value,
) -> anyhow::Result<T> {
let messages = vec![
Message {
role: "system",
content: Value::String(system.to_string()),
},
Message {
role: "user",
content: Value::Array(user.iter().map(Part::to_json).collect()),
},
];
let strict = json!({
"type": "json_schema",
"json_schema": { "name": schema_name, "strict": true, "schema": schema }
});
let raw = match self.complete(provider, &messages, Some(strict)).await {
Ok(r) => r,
Err(e) if should_fallback_response_format(&e, &provider.provider) => {
tracing::warn!(
provider = provider.provider.as_str(),
model = %provider.model,
"json_schema unsupported, falling back to json_object / prompt schema: {e}"
);
let mut msgs = messages;
msgs[0].content = Value::String(format!(
"{system}\n\nRespond ONLY with valid JSON matching this schema (no markdown fences, no extra prose):\n{}",
serde_json::to_string(schema)?
));
match self
.complete(provider, &msgs, Some(json!({ "type": "json_object" })))
.await
{
Ok(r) => r,
Err(e2) if should_fallback_response_format(&e2, &provider.provider) => {
self.complete(provider, &msgs, None).await?
}
Err(e2) => return Err(e2),
}
}
Err(e) => return Err(e),
};
let cleaned = strip_fences(&raw);
serde_json::from_str(cleaned).map_err(|e| {
anyhow::anyhow!(
"LLM JSON parse error ({}/{}): {e}; raw: {}",
provider.provider.as_str(),
provider.model,
truncate(&raw, 500)
)
})
}
async fn complete(
&self,
provider: &LlmProviderConfig,
messages: &[Message],
response_format: Option<Value>,
) -> anyhow::Result<String> {
let mut body = json!({
"model": provider.model,
"messages": messages,
});
if let Some(rf) = response_format {
body["response_format"] = rf;
}
let started = std::time::Instant::now();
let mut req = self
.client
.post(format!("{}/v1/chat/completions", provider.endpoint))
.json(&body);
if !provider.api_key.is_empty() {
req = req.bearer_auth(&provider.api_key);
}
let resp = req.send().await?;
let status = resp.status();
let text = resp.text().await?;
if !status.is_success() {
anyhow::bail!(
"LLM HTTP {status} ({}/{}): {}",
provider.provider.as_str(),
provider.model,
truncate(&text, 800)
);
}
let v: Value = serde_json::from_str(&text)?;
let raw_content = v["choices"][0]["message"]["content"]
.as_str()
.ok_or_else(|| {
anyhow::anyhow!(
"LLM response without content ({}/{}): {}",
provider.provider.as_str(),
provider.model,
truncate(&text, 500)
)
})?;
let content = strip_thinking_blocks(raw_content).to_string();
tracing::debug!(
provider = provider.provider.as_str(),
model = %provider.model,
elapsed_ms = started.elapsed().as_millis() as u64,
prompt_tokens = v["usage"]["prompt_tokens"].as_u64(),
completion_tokens = v["usage"]["completion_tokens"].as_u64(),
"llm completion"
);
Ok(content)
}
}
fn should_fallback_response_format(err: &anyhow::Error, provider: &ProviderKind) -> bool {
let msg = err.to_string().to_lowercase();
msg.contains("response_format")
|| msg.contains("json_schema")
|| msg.contains("structured")
|| (matches!(provider, ProviderKind::Strata)
&& (msg.contains("http 400") || msg.contains("http 422") || msg.contains("unsupported")))
}
/// Build a smooth interleaved weighted schedule (Smooth Weighted Round-Robin)
/// so e.g. weights `[2, 1]` produce `[0, 0, 1]` and `[4, 2]` produce `[0, 0, 1]` (reduced by GCD).
fn build_interleaved_schedule(providers: &[LlmProviderConfig]) -> Vec<usize> {
let mut weights: Vec<u32> = providers.iter().map(|p| p.weight.min(100)).collect();
let total: u32 = weights.iter().sum();
if total == 0 {
return (0..providers.len()).collect();
}
let g = weights.iter().copied().fold(0u32, gcd);
if g > 1 {
for w in &mut weights {
*w /= g;
}
}
let total_steps: usize = weights.iter().map(|&w| w as usize).sum();
let total_w = total_steps as i64;
let mut current = vec![0i64; providers.len()];
let mut schedule = Vec::with_capacity(total_steps);
for _ in 0..total_steps {
let mut best_idx = 0;
for (i, &w) in weights.iter().enumerate() {
if w == 0 {
continue;
}
current[i] += w as i64;
if weights[best_idx] == 0 || current[i] > current[best_idx] {
best_idx = i;
}
}
current[best_idx] -= total_w;
schedule.push(best_idx);
}
schedule
}
fn gcd(a: u32, b: u32) -> u32 {
if b == 0 {
a
} else {
gcd(b, a % b)
}
}
/// Strip `<think>...</think>` blocks that local reasoning/MoE models (e.g. Qwen3.8 on Strata) may emit.
fn strip_thinking_blocks(s: &str) -> &str {
let mut rest = s.trim();
while let Some(start) = rest.find("<think>") {
if start == 0 {
if let Some(end) = rest.find("</think>") {
rest = rest[end + "</think>".len()..].trim();
continue;
}
}
break;
}
rest
}
fn strip_fences(s: &str) -> &str {
let t = strip_thinking_blocks(s);
let t = t.strip_prefix("```json").or_else(|| t.strip_prefix("```")).unwrap_or(t);
t.strip_suffix("```").unwrap_or(t).trim()
}
fn truncate(s: &str, n: usize) -> String {
if s.chars().count() <= n {
s.to_string()
} else {
format!("{}…", s.chars().take(n).collect::<String>())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fences_and_thinking() {
assert_eq!(super::strip_fences("```json\n{\"a\":1}\n```"), "{\"a\":1}");
assert_eq!(super::strip_fences("{\"a\":1}"), "{\"a\":1}");
assert_eq!(
super::strip_fences("<think>\nreasoning...\n</think>\n```json\n{\"a\":1}\n```"),
"{\"a\":1}"
);
}
#[test]
fn parse_uppercase_and_lowercase_provider_json() {
let raw = r#"[
{
"PROVIDER": "strata",
"OPENAI_COMPATIBLE_API_ENDPOINT": "http://127.0.0.1:8080/v1/",
"OPENAI_COMPATIBLE_API_KEY": "",
"MODEL": "qwen3.8-flash-next",
"WEIGHT": 2
},
{
"provider": "cpa",
"endpoint": "http://126.dahyeon.us:8317",
"api_key": "secret-jwt",
"model": "gpt-6.1-sol",
"weight": 1
}
]"#;
let client = LlmClient::from_json_str(raw).expect("valid json");
assert_eq!(client.providers().len(), 2);
assert_eq!(client.providers()[0].provider, ProviderKind::Strata);
assert_eq!(client.providers()[0].endpoint, "http://127.0.0.1:8080");
assert_eq!(client.providers()[0].weight, 2);
assert_eq!(client.providers()[1].provider, ProviderKind::Cpa);
assert_eq!(client.providers()[1].weight, 1);
// Weighted 2:1 schedule should pick provider 0 twice and provider 1 once every 3 calls.
let picks: Vec<usize> = (0..6)
.map(|_| client.pick_provider_order()[0])
.collect();
let count_0 = picks.iter().filter(|&&i| i == 0).count();
let count_1 = picks.iter().filter(|&&i| i == 1).count();
assert_eq!(count_0, 4);
assert_eq!(count_1, 2);
// If provider 0 (strata) is marked unhealthy, pick_provider_order should promote provider 1 (cpa) first
// while keeping provider 0 at the end as last-resort failover.
client.health_flags[0].store(false, Ordering::Relaxed);
let failover_order = client.pick_provider_order();
assert_eq!(failover_order, vec![1, 0]);
}
#[tokio::test]
#[ignore]
async fn live_env_providers_health_and_routing() -> anyhow::Result<()> {
let env_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.env");
let content = std::fs::read_to_string(&env_path)?;
let mut raw_providers = String::new();
for line in content.lines() {
let line = line.trim();
if let Some(rest) = line.strip_prefix("LLM_PROVIDERS=") {
raw_providers = rest.trim_matches('\'').trim_matches('"').to_string();
}
}
assert!(!raw_providers.is_empty(), "LLM_PROVIDERS not found in .env");
let client = LlmClient::from_json_str(&raw_providers)?;
println!("Loaded {} providers from .env", client.providers().len());
let health = client.health_check_all().await;
for h in &health {
println!(
"[HEALTH] provider={} endpoint={} model={} weight={} healthy={} latency={}ms detail={}",
h.provider.as_str(),
h.endpoint,
h.model,
h.weight,
h.healthy,
h.latency_ms,
h.detail
);
}
assert!(health.iter().all(|h| h.healthy), "Expected all providers to be healthy");
#[derive(Debug, Deserialize)]
struct PingResp {
ok: bool,
provider_echo: String,
}
let schema = serde_json::json!({
"type": "object",
"additionalProperties": false,
"required": ["ok", "provider_echo"],
"properties": {
"ok": { "type": "boolean" },
"provider_echo": { "type": "string" }
}
});
for i in 1..=3 {
let out: ChatJsonOutput<PingResp> = client
.chat_json_with_meta(
"Respond with JSON matching the schema. Set ok=true and provider_echo='pong'.",
&[Part::text("ping")],
"ping_check",
&schema,
)
.await?;
println!(
"[CALL #{i}] served_by={} ({}) -> ok={}, echo={}",
out.provider.as_str(),
out.model,
out.data.ok,
out.data.provider_echo
);
}
Ok(())
}
}
+22
View File
@@ -0,0 +1,22 @@
[package]
name = "pipeline"
edition.workspace = true
version.workspace = true
[dependencies]
domain = { workspace = true }
llm = { workspace = true }
providers = { workspace = true }
storage = { workspace = true }
reqwest = { workspace = true }
image = { workspace = true }
bytes = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
futures = { workspace = true }
dashmap = { workspace = true }
uuid = { workspace = true }
chrono = { workspace = true }
anyhow = { workspace = true }
tracing = { workspace = true }
+250
View File
@@ -0,0 +1,250 @@
//! Step 4 & 5 of the pipeline:
//! - VLM batch photo classification (`PhotoTag` per photo)
//! - Multi-evidence floor voting (photo `floor_level` + text `floor_mentions`)
use std::collections::BTreeMap;
use domain::{
floor_label, FloorEvidence, FurnitureCount, FurnitureKind, Photo, PhotoKind, PhotoTag,
PhotoZone,
};
use llm::{LlmClient, Part};
use providers::text::floor_mentions;
use serde::Deserialize;
use serde_json::{json, Value};
use storage::TextDoc;
const SYSTEM_PROMPT: &str = "\
당신은 한국 식당·카페의 사진을 분류하고 공간 단서를 추출하는 비전 분석가입니다.
주어진 각 사진(index 0..N-1)과 캡션 단서를 보고 다음을 판별하세요:
- kind: interior(실내 공간/좌석/카운터/계단), exterior(건물 외관/간판), food(음식/음료 클로즈업), menu(메뉴판/가격표), person(인물 위주), other
- zone: entrance, counter, hall, window_side, terrace, restroom, stairs, kitchen, unknown
- floor_level: 사진이나 캡션에서 층수가 확인되면 정수(1=1층, 2=2층, -1=지하1층, 99=루프탑/옥상), 알 수 없으면 null
- floor_evidence: 층수를 판단한 근거(계단, 표지판, 캡션 문구 등, 없으면 빈 문자열)
- furniture: 사진에 보이는 가구 종류(table2, table4, table6, communal_table, bar_seat, sofa, counter_bar, plant)와 대략적 개수
- quality: 공간 배치 파악에 도움이 되는 정도 (0.0~1.0)
- description: 공간 구조/좌석/창문/계단 중심 한국어 1문장 요약";
pub async fn classify_batch(
llm: &LlmClient,
batch: &[(&Photo, Vec<u8>)],
) -> anyhow::Result<Vec<(String, PhotoTag)>> {
if batch.is_empty() {
return Ok(Vec::new());
}
let mut parts = Vec::with_capacity(batch.len() * 2 + 1);
parts.push(Part::text(format!(
"총 {}장의 사진입니다. 각 사진의 index(0..{})에 맞춰 분류 결과를 반환하세요.",
batch.len(),
batch.len() - 1
)));
for (idx, (photo, bytes)) in batch.iter().enumerate() {
parts.push(Part::text(format!(
"Photo index={idx}, id={}, source={:?}, caption={:?}",
photo.id, photo.source, photo.caption
)));
parts.push(Part::Image {
bytes: bytes.clone(),
mime: "image/jpeg".into(),
});
}
let resp: BatchResp = llm
.chat_json(SYSTEM_PROMPT, &parts, "photo_classification", &schema())
.await?;
let mut out = Vec::new();
for item in resp.items {
if let Some((photo, _)) = batch.get(item.index) {
out.push((
photo.id.clone(),
PhotoTag {
kind: item.kind,
zone: item.zone,
floor_level: item.floor_level.filter(|&l| l == 99 || (-5..=20).contains(&l) && l != 0),
floor_evidence: item.floor_evidence,
furniture: item.furniture,
quality: item.quality.clamp(0.0, 1.0),
description: item.description,
},
));
}
}
Ok(out)
}
#[derive(Deserialize)]
struct BatchResp {
items: Vec<BatchItem>,
}
#[derive(Deserialize)]
struct BatchItem {
index: usize,
kind: PhotoKind,
zone: PhotoZone,
floor_level: Option<i32>,
floor_evidence: String,
furniture: Vec<FurnitureCount>,
quality: f32,
description: String,
}
fn schema() -> Value {
json!({
"type": "object",
"additionalProperties": false,
"required": ["items"],
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["index", "kind", "zone", "floor_level", "floor_evidence", "furniture", "quality", "description"],
"properties": {
"index": { "type": "integer" },
"kind": { "type": "string", "enum": ["interior", "exterior", "food", "menu", "person", "other"] },
"zone": { "type": "string", "enum": ["entrance", "counter", "hall", "window_side", "terrace", "restroom", "stairs", "kitchen", "unknown"] },
"floor_level": { "type": ["integer", "null"] },
"floor_evidence": { "type": "string" },
"furniture": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["kind", "count"],
"properties": {
"kind": { "type": "string", "enum": ["table2", "table4", "table6", "communal_table", "bar_seat", "sofa", "counter_bar", "plant"] },
"count": { "type": "integer" }
}
}
},
"quality": { "type": "number" },
"description": { "type": "string" }
}
}
}
}
})
}
/// Combine photo `floor_level` hints, photo captions, and text snippets into a ranked list of floors.
/// Always includes at least `1F` so single-floor venues have a ground floor.
pub fn detect_floors(photos: &[Photo], texts: &[TextDoc]) -> Vec<FloorEvidence> {
let mut map: BTreeMap<i32, FloorEvidence> = BTreeMap::new();
let ensure = |map: &mut BTreeMap<i32, FloorEvidence>, level: i32| {
map.entry(level).or_insert_with(|| FloorEvidence {
level,
label: floor_label(level),
score: 0.0,
photo_ids: Vec::new(),
text_snippets: Vec::new(),
});
};
for p in photos {
if let Some(tag) = &p.tag {
if let Some(lvl) = tag.floor_level {
ensure(&mut map, lvl);
let entry = map.get_mut(&lvl).unwrap();
entry.score += if tag.kind == PhotoKind::Interior { 1.5 } else { 0.8 };
if !entry.photo_ids.contains(&p.id) {
entry.photo_ids.push(p.id.clone());
}
if !tag.floor_evidence.is_empty() && entry.text_snippets.len() < 4 {
entry.text_snippets.push(tag.floor_evidence.clone());
}
}
}
for (lvl, snip) in floor_mentions(&p.caption) {
ensure(&mut map, lvl);
let entry = map.get_mut(&lvl).unwrap();
entry.score += 0.7;
if !entry.photo_ids.contains(&p.id) {
entry.photo_ids.push(p.id.clone());
}
if entry.text_snippets.len() < 4 && !entry.text_snippets.contains(&snip) {
entry.text_snippets.push(snip);
}
}
}
for t in texts {
for (lvl, snip) in floor_mentions(&t.text) {
ensure(&mut map, lvl);
let entry = map.get_mut(&lvl).unwrap();
entry.score += 0.5;
if entry.text_snippets.len() < 4 && !entry.text_snippets.contains(&snip) {
entry.text_snippets.push(snip);
}
}
}
// Filter out weak stray mentions (e.g., a single "3층" referring to another tenant in the building)
// unless it's 1F.
let mut floors: Vec<FloorEvidence> = map
.into_values()
.filter(|f| f.level == 1 || f.score >= 1.0)
.collect();
if floors.is_empty() {
floors.push(FloorEvidence {
level: 1,
label: "1F".into(),
score: 1.0,
photo_ids: photos
.iter()
.filter(|p| p.tag.as_ref().map(|t| t.kind == PhotoKind::Interior).unwrap_or(false))
.take(4)
.map(|p| p.id.clone())
.collect(),
text_snippets: Vec::new(),
});
}
floors.sort_by_key(|f| f.level);
let _ = FurnitureKind::Table2;
floors
}
#[cfg(test)]
mod tests {
use super::*;
use domain::PhotoSourceId;
#[test]
fn floor_voting_combines_photo_and_text() {
let p = Photo {
id: "p1".into(),
place_id: "kakao:1".into(),
source: PhotoSourceId::NaverBlog,
image_url: String::new(),
page_url: String::new(),
caption: "2층 창가석".into(),
object_key: String::new(),
dhash: String::new(),
width: 800,
height: 600,
tag: Some(PhotoTag {
kind: PhotoKind::Interior,
zone: PhotoZone::WindowSide,
floor_level: Some(2),
floor_evidence: "계단 위 2층 홀".into(),
furniture: vec![],
quality: 0.9,
description: "2층 창가".into(),
}),
};
let t = TextDoc {
place_id: "kakao:1".into(),
source: PhotoSourceId::NaverBlog,
url: String::new(),
text: "1층에서 주문하고 2층으로 올라가면 루프탑도 있어요. 루프탑 뷰가 최고".into(),
};
let floors = detect_floors(&[p], &[t]);
let levels: Vec<i32> = floors.iter().map(|f| f.level).collect();
assert!(levels.contains(&2));
assert!(levels.contains(&domain::ROOFTOP_LEVEL));
}
}
+79
View File
@@ -0,0 +1,79 @@
//! Image decoding, resizing, and 64-bit perceptual difference hash (dHash).
use image::imageops::FilterType;
use image::{DynamicImage, GenericImageView};
use std::io::Cursor;
pub struct ProcessedImage {
pub jpeg_bytes: Vec<u8>,
pub width: u32,
pub height: u32,
pub dhash: u64,
}
/// Decode raw image bytes, reject tiny icons (< 160px), downscale to at most `max_dim`
/// on the longest side, re-encode as JPEG, and compute a 9x8 dHash.
pub fn process_image(bytes: &[u8], max_dim: u32) -> anyhow::Result<ProcessedImage> {
let img = image::load_from_memory(bytes)?;
let (w, h) = img.dimensions();
if w < 160 || h < 160 {
anyhow::bail!("image too small ({w}x{h})");
}
let dhash = compute_dhash(&img);
let resized = if w > max_dim || h > max_dim {
img.resize(max_dim, max_dim, FilterType::Triangle)
} else {
img
};
let (rw, rh) = resized.dimensions();
let mut out = Vec::new();
resized.to_rgb8().write_to(&mut Cursor::new(&mut out), image::ImageFormat::Jpeg)?;
Ok(ProcessedImage {
jpeg_bytes: out,
width: rw,
height: rh,
dhash,
})
}
pub fn compute_dhash(img: &DynamicImage) -> u64 {
let gray = img.resize_exact(9, 8, FilterType::Triangle).to_luma8();
let mut hash = 0u64;
let mut bit = 0;
for y in 0..8 {
for x in 0..8 {
if gray.get_pixel(x, y)[0] > gray.get_pixel(x + 1, y)[0] {
hash |= 1u64 << bit;
}
bit += 1;
}
}
hash
}
pub fn hamming(a: u64, b: u64) -> u32 {
(a ^ b).count_ones()
}
pub fn is_near_duplicate(existing: &[u64], candidate: u64) -> bool {
existing.iter().any(|&h| hamming(h, candidate) <= 6)
}
#[cfg(test)]
mod tests {
use super::*;
use image::{Rgb, RgbImage};
#[test]
fn identical_images_match() {
let mut buf = RgbImage::new(200, 200);
for (x, y, p) in buf.enumerate_pixels_mut() {
*p = Rgb([(x % 255) as u8, (y % 255) as u8, 120]);
}
let dyn_img = DynamicImage::ImageRgb8(buf);
let h1 = compute_dhash(&dyn_img);
let h2 = compute_dhash(&dyn_img);
assert_eq!(hamming(h1, h2), 0);
assert!(is_near_duplicate(&[h1], h2));
}
}
+430
View File
@@ -0,0 +1,430 @@
//! Step 6 & 7 of the pipeline:
//! - VLM spatial layout inference -> `domain::Layout`
//! - Validation & clamping (bounds, non-zero dimensions, valid `evidence_photos` IDs)
//! - Single retry with validation feedback if needed
use std::collections::HashSet;
use domain::{
floor_label, Accessibility, Arrangement, Floor, FloorEvidence, FurnitureGroup, FurnitureKind,
Layout, Opening, OpeningKind, Photo, PhotoKind, Place, Summary, Tri, Vertical, VerticalKind,
Wall, Zone, ZoneKind,
};
use llm::{LlmClient, Part};
use serde::Deserialize;
use serde_json::{json, Value};
use storage::TextDoc;
const SYSTEM_PROMPT: &str = "\
당신은 식당·카페의 실내 사진들과 텍스트 리뷰를 종합해 three.js 미니어처 3D 뷰어용 공간 배치(Layout JSON)를 추론하는 공간 건축 AI입니다.
좌표 규칙 (층별 미터 단위):
- 각 층은 직사각형 바닥(width_m: 동서 폭, depth_m: 남북 깊이, 보통 6~22m 범위)입니다.
- 원점 (0, 0)은 남서쪽(South-West) 모서리이며, x는 동쪽(0..width_m), y는 북쪽(0..depth_m)으로 증가합니다.
- 주 출입구(entrance)는 기본적으로 south 벽에 두되, 사진 단서에 맞게 조정하세요.
- 각 zone(구역)의 직사각형 (x, y, w, d)는 바닥 범위(0 <= x, x+w <= width_m, 0 <= y, y+d <= depth_m) 안에 있어야 하며 서로 과도하게 겹치지 않게 배치하세요.
- evidence_photos 배열에는 반드시 입력으로 주어진 사진 id만 넣으세요 (절대 없는 id를 만들지 마세요).
- 다층 건물인 경우 층간 이동 수단(stairs 또는 elevator)을 verticals에 포함하세요.
- summary에는 예상 평수 범위, 예상 좌석 수, 좌석 종류, 방문 전 핵심 체크 포인트(highlights), 주의사항(caveats)을 한국어로 작성하세요.";
pub async fn generate_layout(
llm: &LlmClient,
place: &Place,
photos_with_bytes: &[(&Photo, Vec<u8>)],
floors: &[FloorEvidence],
texts: &[TextDoc],
mut on_attempt: impl FnMut(u32),
) -> anyhow::Result<Layout> {
let valid_ids: HashSet<String> = photos_with_bytes.iter().map(|(p, _)| p.id.clone()).collect();
let mut feedback: Option<String> = None;
for attempt in 1..=2 {
on_attempt(attempt);
let parts = build_prompt_parts(place, photos_with_bytes, floors, texts, feedback.as_deref());
match llm
.chat_json_with_meta::<DraftLayout>(SYSTEM_PROMPT, &parts, "space_layout", &schema())
.await
{
Ok(out) => {
let model_tag = out.model_tag();
let mut layout = into_layout(place, &model_tag, out.data, &valid_ids);
match validate_and_clamp(&mut layout) {
Ok(()) => return Ok(layout),
Err(err) if attempt == 1 => {
tracing::warn!(attempt, %err, "layout validation failed, retrying once");
feedback = Some(err);
}
Err(err) => {
tracing::warn!(attempt, %err, "layout validation failed on second attempt, using clamped layout");
return Ok(layout);
}
}
}
Err(err) if attempt == 1 => {
tracing::warn!(attempt, %err, "layout LLM call failed, retrying once");
feedback = Some(format!("이전 응답 파싱 실패: {err}"));
}
Err(err) => {
tracing::warn!(%err, "layout LLM call failed twice, using fallback layout");
return Ok(fallback_layout(place, &llm.model, photos_with_bytes, floors));
}
}
}
Ok(fallback_layout(place, &llm.model, photos_with_bytes, floors))
}
fn build_prompt_parts(
place: &Place,
photos_with_bytes: &[(&Photo, Vec<u8>)],
floors: &[FloorEvidence],
texts: &[TextDoc],
feedback: Option<&str>,
) -> Vec<Part> {
let mut header = format!(
"장소명: {}\n카테고리: {}\n주소: {} ({})\n\n[감지된 층 단서]\n",
place.name, place.category, place.road_address, place.address
);
for f in floors {
header.push_str(&format!(
"- {} (level={}, 점수={:.1}, 관련 사진={:?}, 텍스트 단서={:?})\n",
f.label, f.level, f.score, f.photo_ids, f.text_snippets
));
}
header.push_str("\n[수집된 텍스트/리뷰 발췌]\n");
for t in texts.iter().take(6) {
let excerpt: String = t.text.chars().take(450).collect();
header.push_str(&format!("({:?}) {}\n---\n", t.source, excerpt));
}
if let Some(fb) = feedback {
header.push_str(&format!(
"\n[이전 생성 결과 오류 - 반드시 수정하세요]\n{fb}\n"
));
}
header.push_str("\n[실내/외관 핵심 사진 목록 — evidence_photos에는 아래 photo_id만 사용하세요]\n");
let mut parts = vec![Part::text(header)];
for (p, bytes) in photos_with_bytes.iter().take(12) {
let tag_desc = p
.tag
.as_ref()
.map(|t| {
format!(
"kind={:?}, zone={:?}, floor={:?}, desc={}, furniture={:?}",
t.kind, t.zone, t.floor_level, t.description, t.furniture
)
})
.unwrap_or_default();
parts.push(Part::text(format!(
"photo_id=\"{}\" | caption={:?} | {tag_desc}",
p.id, p.caption
)));
parts.push(Part::Image {
bytes: bytes.clone(),
mime: "image/jpeg".into(),
});
}
parts
}
fn into_layout(
place: &Place,
model: &str,
draft: DraftLayout,
valid_ids: &HashSet<String>,
) -> Layout {
let floors = draft
.floors
.into_iter()
.map(|mut f| {
if f.label.trim().is_empty() {
f.label = floor_label(f.level);
}
for z in &mut f.zones {
z.evidence_photos.retain(|id| valid_ids.contains(id));
}
f
})
.collect();
Layout {
place_id: place.id.clone(),
generated_at: chrono::Utc::now().to_rfc3339(),
model: model.to_string(),
confidence: draft.confidence.clamp(0.1, 0.98),
floors,
summary: draft.summary,
}
}
/// Clamp coordinates into valid ranges and return `Err` if the layout had structural issues worth retrying.
pub fn validate_and_clamp(layout: &mut Layout) -> Result<(), String> {
if layout.floors.is_empty() {
return Err("floors 배열이 비어 있습니다. 최소 1개 층(1F)을 포함해야 합니다.".into());
}
let mut issues = Vec::new();
for f in &mut layout.floors {
if f.width_m < 4.0 || f.width_m > 40.0 {
issues.push(format!("층 {} width_m({})가 비정상 범위", f.label, f.width_m));
f.width_m = f.width_m.clamp(5.0, 28.0);
}
if f.depth_m < 4.0 || f.depth_m > 40.0 {
issues.push(format!("층 {} depth_m({})가 비정상 범위", f.label, f.depth_m));
f.depth_m = f.depth_m.clamp(5.0, 28.0);
}
f.confidence = f.confidence.clamp(0.1, 0.99);
if f.zones.is_empty() {
issues.push(format!("층 {}에 zones가 비어 있습니다.", f.label));
}
for z in &mut f.zones {
z.w = z.w.clamp(1.2, f.width_m);
z.d = z.d.clamp(1.2, f.depth_m);
z.x = z.x.clamp(0.0, (f.width_m - z.w).max(0.0));
z.y = z.y.clamp(0.0, (f.depth_m - z.d).max(0.0));
for fg in &mut z.furniture {
fg.count = fg.count.clamp(1, 30);
}
}
for o in &mut f.openings {
let wall_len = match o.wall {
Wall::North | Wall::South => f.width_m,
Wall::East | Wall::West => f.depth_m,
};
o.length_m = o.length_m.clamp(0.8, (wall_len - 0.4).max(0.8));
o.offset_m = o.offset_m.clamp(0.2, (wall_len - o.length_m - 0.2).max(0.2));
}
for v in &mut f.verticals {
v.w = v.w.clamp(1.0, 3.5);
v.d = v.d.clamp(1.2, 4.0);
v.x = v.x.clamp(0.0, (f.width_m - v.w).max(0.0));
v.y = v.y.clamp(0.0, (f.depth_m - v.d).max(0.0));
}
}
layout.floors.sort_by_key(|f| f.level);
if issues.is_empty() {
Ok(())
} else {
Err(issues.join("; "))
}
}
pub fn fallback_layout(
place: &Place,
model: &str,
photos_with_bytes: &[(&Photo, Vec<u8>)],
floors: &[FloorEvidence],
) -> Layout {
let evidence: Vec<String> = photos_with_bytes.iter().take(4).map(|(p, _)| p.id.clone()).collect();
let levels: Vec<i32> = if floors.is_empty() {
vec![1]
} else {
floors.iter().map(|f| f.level).collect()
};
let built_floors = levels
.iter()
.map(|&lvl| Floor {
level: lvl,
label: floor_label(lvl),
confidence: 0.45,
width_m: 10.0,
depth_m: 12.0,
openings: if lvl == 1 {
vec![
Opening { kind: OpeningKind::Entrance, wall: Wall::South, offset_m: 1.5, length_m: 1.6 },
Opening { kind: OpeningKind::Window, wall: Wall::South, offset_m: 4.0, length_m: 4.5 },
]
} else {
vec![Opening { kind: OpeningKind::Window, wall: Wall::South, offset_m: 2.0, length_m: 6.0 }]
},
zones: vec![
Zone {
kind: if lvl == 1 { ZoneKind::Counter } else { ZoneKind::Seating },
label: if lvl == 1 { "주문 카운터".into() } else { "창가 좌석".into() },
x: 0.6,
y: if lvl == 1 { 8.2 } else { 0.6 },
w: 4.2,
d: 3.0,
furniture: vec![FurnitureGroup {
kind: if lvl == 1 { FurnitureKind::CounterBar } else { FurnitureKind::BarSeat },
count: if lvl == 1 { 1 } else { 5 },
arrangement: Arrangement::AlongWall,
along_wall: Some(if lvl == 1 { Wall::North } else { Wall::South }),
}],
evidence_photos: evidence.clone(),
},
Zone {
kind: ZoneKind::Seating,
label: "메인 홀 좌석".into(),
x: 0.8,
y: if lvl == 1 { 1.0 } else { 4.2 },
w: 8.4,
d: 6.5,
furniture: vec![
FurnitureGroup {
kind: FurnitureKind::Table2,
count: 5,
arrangement: Arrangement::Grid,
along_wall: None,
},
FurnitureGroup {
kind: FurnitureKind::Table4,
count: 3,
arrangement: Arrangement::Grid,
along_wall: None,
},
],
evidence_photos: evidence.clone(),
},
],
verticals: if levels.len() > 1 {
vec![Vertical {
kind: VerticalKind::Stairs,
x: 7.8,
y: 9.2,
w: 1.6,
d: 2.2,
to_level: if lvl == 1 { levels.get(1).copied().unwrap_or(2) } else { 1 },
}]
} else {
vec![]
},
notes: "수집된 사진을 기반으로 생성한 기본 추정 배치입니다.".into(),
})
.collect();
let _ = PhotoKind::Interior;
Layout {
place_id: place.id.clone(),
generated_at: chrono::Utc::now().to_rfc3339(),
model: model.to_string(),
confidence: 0.45,
floors: built_floors,
summary: Summary {
area_pyeong_min: 18.0,
area_pyeong_max: 28.0,
seat_estimate: 26,
seat_types: vec!["2인 테이블".into(), "4인 테이블".into(), "바 좌석".into()],
highlights: vec![format!("{} 공간 배치 추정 완료", place.name)],
accessibility: Accessibility {
elevator: Tri::Unknown,
step_free_entrance: Tri::Likely,
},
caveats: vec!["실내 사진 수량이 제한적이어서 일부 구역은 표준 배치로 추정되었습니다.".into()],
},
}
}
#[derive(Deserialize)]
struct DraftLayout {
confidence: f32,
floors: Vec<Floor>,
summary: Summary,
}
fn schema() -> Value {
json!({
"type": "object",
"additionalProperties": false,
"required": ["confidence", "floors", "summary"],
"properties": {
"confidence": { "type": "number" },
"floors": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["level", "label", "confidence", "width_m", "depth_m", "openings", "zones", "verticals", "notes"],
"properties": {
"level": { "type": "integer" },
"label": { "type": "string" },
"confidence": { "type": "number" },
"width_m": { "type": "number" },
"depth_m": { "type": "number" },
"openings": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["kind", "wall", "offset_m", "length_m"],
"properties": {
"kind": { "type": "string", "enum": ["entrance", "window", "door"] },
"wall": { "type": "string", "enum": ["north", "south", "east", "west"] },
"offset_m": { "type": "number" },
"length_m": { "type": "number" }
}
}
},
"zones": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["kind", "label", "x", "y", "w", "d", "furniture", "evidence_photos"],
"properties": {
"kind": { "type": "string", "enum": ["seating", "counter", "bar", "kitchen", "restroom", "terrace", "entrance", "other"] },
"label": { "type": "string" },
"x": { "type": "number" },
"y": { "type": "number" },
"w": { "type": "number" },
"d": { "type": "number" },
"furniture": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["kind", "count", "arrangement", "along_wall"],
"properties": {
"kind": { "type": "string", "enum": ["table2", "table4", "table6", "communal_table", "bar_seat", "sofa", "counter_bar", "plant"] },
"count": { "type": "integer" },
"arrangement": { "type": "string", "enum": ["grid", "along_wall", "row", "cluster"] },
"along_wall": { "type": ["string", "null"], "enum": ["north", "south", "east", "west", null] }
}
}
},
"evidence_photos": { "type": "array", "items": { "type": "string" } }
}
}
},
"verticals": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["kind", "x", "y", "w", "d", "to_level"],
"properties": {
"kind": { "type": "string", "enum": ["stairs", "elevator"] },
"x": { "type": "number" },
"y": { "type": "number" },
"w": { "type": "number" },
"d": { "type": "number" },
"to_level": { "type": "integer" }
}
}
},
"notes": { "type": "string" }
}
}
},
"summary": {
"type": "object",
"additionalProperties": false,
"required": ["area_pyeong_min", "area_pyeong_max", "seat_estimate", "seat_types", "highlights", "accessibility", "caveats"],
"properties": {
"area_pyeong_min": { "type": "number" },
"area_pyeong_max": { "type": "number" },
"seat_estimate": { "type": "integer" },
"seat_types": { "type": "array", "items": { "type": "string" } },
"highlights": { "type": "array", "items": { "type": "string" } },
"accessibility": {
"type": "object",
"additionalProperties": false,
"required": ["elevator", "step_free_entrance"],
"properties": {
"elevator": { "type": "string", "enum": ["yes", "likely", "no", "unknown"] },
"step_free_entrance": { "type": "string", "enum": ["yes", "likely", "no", "unknown"] }
}
},
"caveats": { "type": "array", "items": { "type": "string" } }
}
}
}
})
}
+482
View File
@@ -0,0 +1,482 @@
#![recursion_limit = "256"]
//! End-to-end place analysis pipeline:
//! 1. Normalize place (Kakao candidate + NCP Geocode address verification + Naver place ID resolution)
//! 2. Collect photos & texts in parallel across all `PhotoSource`s (per-source timeout)
//! 3. Download images, deduplicate with 64-bit dHash, store JPEG blobs in MinIO & metadata in MongoDB
//! 4. Classify photos in batches with the VLM (`PhotoTag`)
//! 5. Vote on floors (`FloorEvidence`) from photo tags, captions, and text snippets
//! 6. Generate 3D `Layout` JSON with the VLM
//! 7. Validate & clamp layout (retry once on structural issues) and save to MongoDB
pub mod classify;
pub mod dhash;
pub mod layout_gen;
use std::sync::Arc;
use std::time::Duration;
use bytes::Bytes;
use dashmap::DashMap;
use domain::{
JobEvent, Photo, PhotoKind, PhotoSummary, Place, PlaceCandidate, PlaceView, RawPhoto, RecentPlace,
};
use futures::stream::{self, StreamExt};
use llm::LlmClient;
use providers::browser::{browser_source, BrowserSource};
use providers::kakao_local::KakaoLocal;
use providers::kakao_place::KakaoPlacePhotos;
use providers::naver_blog::NaverBlog;
use providers::naver_map::NaverMapSearch;
use providers::naver_place::NaverPlacePhotos;
use providers::ncp_geocode::NcpGeocode;
use providers::{PhotoSource, PlaceSearch};
use storage::{Store, TextDoc};
use tokio::sync::broadcast;
#[derive(Clone)]
pub struct PipelineConfig {
pub kakao_api_key: String,
pub ncp_client_id: String,
pub ncp_client_secret: String,
pub browser_scraper_url: Option<String>,
pub max_photos_per_source: usize,
pub max_photos_total: usize,
}
#[derive(Clone)]
pub struct Pipeline {
pub store: Store,
pub llm: LlmClient,
kakao: Arc<KakaoLocal>,
naver_map: Arc<NaverMapSearch>,
ncp: Option<Arc<NcpGeocode>>,
sources: Arc<Vec<Box<dyn PhotoSource>>>,
http: reqwest::Client,
cfg: PipelineConfig,
/// Active job event broadcasters keyed by `job_id`.
jobs: Arc<DashMap<String, broadcast::Sender<JobEvent>>>,
/// Map `place_id` -> currently running `job_id` so concurrent visitors share one job.
running_by_place: Arc<DashMap<String, String>>,
}
impl Pipeline {
pub fn new(store: Store, llm: LlmClient, cfg: PipelineConfig) -> Self {
let kakao = Arc::new(KakaoLocal::new(&cfg.kakao_api_key));
let naver_map = Arc::new(NaverMapSearch::new());
let ncp = if !cfg.ncp_client_id.trim().is_empty() && !cfg.ncp_client_secret.trim().is_empty() {
Some(Arc::new(NcpGeocode::new(&cfg.ncp_client_id, &cfg.ncp_client_secret)))
} else {
None
};
let sources: Vec<Box<dyn PhotoSource>> = vec![
Box::new(NaverPlacePhotos::new()),
Box::new(KakaoPlacePhotos::new()),
Box::new(NaverBlog::new()),
browser_source(cfg.browser_scraper_url.as_deref(), BrowserSource::GoogleMaps),
browser_source(cfg.browser_scraper_url.as_deref(), BrowserSource::Instagram),
];
Self {
store,
llm,
kakao,
naver_map,
ncp,
sources: Arc::new(sources),
http: providers::http::scraping_client(),
cfg,
jobs: Arc::new(DashMap::new()),
running_by_place: Arc::new(DashMap::new()),
}
}
/// Search place candidates by free text (Kakao primary, Naver Map fallback).
pub async fn search_places(&self, query: &str) -> anyhow::Result<Vec<PlaceCandidate>> {
let q = query.trim();
if q.is_empty() {
return Ok(Vec::new());
}
let mut results = self.kakao.search(q).await.unwrap_or_default();
if results.is_empty() {
results = self.naver_map.search(q).await.unwrap_or_default();
}
Ok(results)
}
/// Saved analyses ordered by their most recent view (or analysis time for older records).
pub async fn recent_places(&self) -> anyhow::Result<Vec<RecentPlace>> {
self.store.recent_places().await
}
/// Return current `PlaceView` (place + latest cached layout + stored photos + running job id).
pub async fn get_place_view(&self, place_id: &str) -> anyhow::Result<Option<PlaceView>> {
let Some(place) = self.store.get_place(place_id).await? else {
return Ok(None);
};
let layout = self.store.latest_layout(place_id).await?.map(|d| d.layout);
let photos = self
.store
.photos_for_place(place_id)
.await?
.iter()
.map(PhotoSummary::from)
.collect();
let running_job = self.running_by_place.get(place_id).map(|v| v.value().clone());
if layout.is_some() {
if let Err(error) = self.store.record_place_view(place_id).await {
tracing::warn!(%place_id, %error, "could not record place view");
}
}
Ok(Some(PlaceView {
place,
layout,
photos,
running_job,
}))
}
/// Subscribe to live events for `job_id`, plus any events already recorded in MongoDB.
pub async fn subscribe_job(
&self,
job_id: &str,
) -> anyhow::Result<(Vec<JobEvent>, Option<broadcast::Receiver<JobEvent>>)> {
let rx = self.jobs.get(job_id).map(|s| s.value().subscribe());
let past = self
.store
.get_job(job_id)
.await?
.map(|j| j.events)
.unwrap_or_default();
Ok((past, rx))
}
/// Ensure the candidate's `Place` exists in MongoDB; if no cached layout exists (or `force_reanalyze` is true),
/// spawn the analysis job in the background and return `(place_id, Option<job_id>)`.
pub async fn select_or_analyze(
&self,
candidate: PlaceCandidate,
force_reanalyze: bool,
) -> anyhow::Result<(String, Option<String>)> {
let place_id = candidate.place_id();
let mut place = match self.store.get_place(&place_id).await? {
Some(existing) => existing,
None => {
let p = Place::from_candidate(&candidate);
self.store.upsert_place(&p).await?;
p
}
};
if !force_reanalyze {
if let Some( job_id ) = self.running_by_place.get(&place_id).map(|r| r.value().clone()) {
return Ok((place_id, Some(job_id)));
}
if self.store.latest_layout(&place_id).await?.is_some() {
return Ok((place_id, None));
}
}
let job_id = self.spawn_job(place_id.clone(), place.clone(), force_reanalyze).await?;
let _ = &mut place;
Ok((place_id, Some(job_id)))
}
/// Trigger reanalysis for an already-stored `place_id`.
pub async fn reanalyze(&self, place_id: &str) -> anyhow::Result<String> {
if let Some(existing) = self.running_by_place.get(place_id) {
return Ok(existing.value().clone());
}
let place = self
.store
.get_place(place_id)
.await?
.ok_or_else(|| anyhow::anyhow!("장소를 찾을 수 없습니다: {place_id}"))?;
self.spawn_job(place_id.to_string(), place, true).await
}
async fn spawn_job(
&self,
place_id: String,
place: Place,
clear_existing_photos: bool,
) -> anyhow::Result<String> {
let job_id = uuid::Uuid::new_v4().to_string();
self.store.create_job(&job_id, &place_id).await?;
let (tx, _) = broadcast::channel(64);
self.jobs.insert(job_id.clone(), tx.clone());
self.running_by_place.insert(place_id.clone(), job_id.clone());
let this = self.clone();
let jid = job_id.clone();
let pid = place_id.clone();
tokio::spawn(async move {
if let Err(err) = this.run_job(&jid, place, clear_existing_photos, &tx).await {
tracing::error!(job_id = %jid, place_id = %pid, "pipeline job failed: {err:#}");
this.emit(&jid, &tx, JobEvent::Error { message: err.to_string() }).await;
}
this.running_by_place.remove(&pid);
// Keep channel briefly so SSE clients receive the terminal event, then drop.
tokio::time::sleep(Duration::from_secs(5)).await;
this.jobs.remove(&jid);
});
Ok(job_id)
}
/// Run a place analysis synchronously (used by `precompute` CLI and `spawn_job`).
pub async fn run_sync(&self, place: Place) -> anyhow::Result<domain::Layout> {
let job_id = uuid::Uuid::new_v4().to_string();
self.store.upsert_place(&place).await?;
self.store.create_job(&job_id, &place.id).await?;
let (tx, _) = broadcast::channel(64);
self.run_job(&job_id, place.clone(), false, &tx).await?;
let doc = self
.store
.latest_layout(&place.id)
.await?
.ok_or_else(|| anyhow::anyhow!("layout not saved"))?;
Ok(doc.layout)
}
async fn emit(&self, job_id: &str, tx: &broadcast::Sender<JobEvent>, ev: JobEvent) {
let _ = self.store.push_job_event(job_id, &ev).await;
let _ = tx.send(ev);
}
async fn run_job(
&self,
job_id: &str,
mut place: Place,
clear_existing_photos: bool,
tx: &broadcast::Sender<JobEvent>,
) -> anyhow::Result<()> {
self.emit(job_id, tx, JobEvent::Started { place_id: place.id.clone() }).await;
// 1. Address verification (NCP Geocode) + Naver Place ID matching
if let Some(ncp) = &self.ncp {
let addr_query = if !place.road_address.is_empty() {
place.road_address.clone()
} else {
place.address.clone()
};
if !addr_query.is_empty() {
if let Ok(Some(geo)) = ncp.geocode(&addr_query).await {
if place.road_address.is_empty() && !geo.road_address.is_empty() {
place.road_address = geo.road_address;
}
if place.address.is_empty() && !geo.jibun_address.is_empty() {
place.address = geo.jibun_address;
}
}
}
}
if place.naver_place_id.is_none() {
if let Ok(Some(nid)) = self.naver_map.resolve_place_id(&place).await {
place.naver_place_id = Some(nid);
}
}
self.store.upsert_place(&place).await?;
self.emit(
job_id,
tx,
JobEvent::PlaceResolved {
name: place.name.clone(),
naver_place_id: place.naver_place_id.clone(),
},
)
.await;
// 2. Collect photos & text snippets from all sources in parallel
let per_source = self.cfg.max_photos_per_source;
let source_futs = self.sources.iter().map(|src| {
let place_ref = &place;
async move {
let id = src.id();
let res = tokio::time::timeout(Duration::from_secs(30), src.collect(place_ref, per_source)).await;
match res {
Ok(Ok(out)) => (id, out, None),
Ok(Err(e)) => (id, Default::default(), Some(e.to_string())),
Err(_) => (id, Default::default(), Some("시간 초과 (30s)".into())),
}
}
});
let collected = futures::future::join_all(source_futs).await;
let mut raw_photos: Vec<RawPhoto> = Vec::new();
let mut text_docs: Vec<TextDoc> = Vec::new();
for (source, out, error) in collected {
self.emit(
job_id,
tx,
JobEvent::SourceDone {
source,
photos: out.photos.len(),
texts: out.texts.len(),
error,
},
)
.await;
raw_photos.extend(out.photos);
for t in out.texts {
text_docs.push(TextDoc {
place_id: place.id.clone(),
source: t.source,
url: t.url,
text: t.text,
});
}
}
let _ = self.store.replace_texts(&place.id, &text_docs).await;
raw_photos.truncate(self.cfg.max_photos_total);
// 3. Download, deduplicate (dHash), and store in MinIO + MongoDB
if clear_existing_photos {
let _ = self.store.delete_photos_for_place(&place.id).await;
}
let downloaded: Vec<Option<(RawPhoto, dhash::ProcessedImage)>> = stream::iter(raw_photos)
.map(|raw| {
let client = self.http.clone();
async move {
let mut req = client.get(&raw.image_url);
if let Some(r) = &raw.referer {
req = req.header(reqwest::header::REFERER, r);
}
let bytes = tokio::time::timeout(Duration::from_secs(15), async {
Ok::<_, anyhow::Error>(req.send().await?.error_for_status()?.bytes().await?)
})
.await
.ok()?
.ok()?;
let processed = dhash::process_image(&bytes, 1024).ok()?;
Some((raw, processed))
}
})
.buffer_unordered(8)
.collect()
.await;
let mut seen_hashes: Vec<u64> = Vec::new();
let mut stored_photos: Vec<(Photo, Vec<u8>)> = Vec::new();
let mut duplicates = 0usize;
let mut failed = 0usize;
for item in downloaded {
let Some((raw, proc_img)) = item else {
failed += 1;
continue;
};
if dhash::is_near_duplicate(&seen_hashes, proc_img.dhash) {
duplicates += 1;
continue;
}
seen_hashes.push(proc_img.dhash);
let photo_id = format!("p_{}", &uuid::Uuid::new_v4().simple().to_string()[..10]);
let object_key = format!("places/{}/{photo_id}.jpg", place.id.replace(':', "_"));
if let Err(e) = self.store.put_blob(&object_key, Bytes::from(proc_img.jpeg_bytes.clone())).await {
tracing::warn!(%e, "minio put_blob failed");
failed += 1;
continue;
}
let photo = Photo {
id: photo_id,
place_id: place.id.clone(),
source: raw.source,
image_url: raw.image_url,
page_url: raw.page_url,
caption: raw.caption,
object_key,
dhash: format!("{:016x}", proc_img.dhash),
width: proc_img.width,
height: proc_img.height,
tag: None,
};
self.store.insert_photo(&photo).await?;
stored_photos.push((photo, proc_img.jpeg_bytes));
}
self.emit(
job_id,
tx,
JobEvent::PhotosStored {
stored: stored_photos.len(),
duplicates,
failed,
},
)
.await;
// 4. Classify photos in batches of 6 via VLM
let to_classify_len = stored_photos.len().min(36);
for chunk in stored_photos[..to_classify_len].chunks_mut(6) {
let refs: Vec<(&Photo, Vec<u8>)> = chunk.iter().map(|(p, b)| (p, b.clone())).collect();
match classify::classify_batch(&self.llm, &refs).await {
Ok(tags) => {
for (pid, tag) in tags {
let _ = self.store.set_photo_tag(&pid, &tag).await;
if let Some((p, _)) = chunk.iter_mut().find(|(p, _)| p.id == pid) {
p.tag = Some(tag);
}
}
}
Err(e) => {
tracing::warn!(%e, "photo batch classification failed");
}
}
}
let mut interior = 0usize;
let mut exterior = 0usize;
let mut food = 0usize;
let mut other = 0usize;
for (p, _) in &stored_photos {
match p.tag.as_ref().map(|t| t.kind) {
Some(PhotoKind::Interior) => interior += 1,
Some(PhotoKind::Exterior) => exterior += 1,
Some(PhotoKind::Food) => food += 1,
_ => other += 1,
}
}
self.emit(job_id, tx, JobEvent::Classified { interior, exterior, food, other }).await;
// 5. Floor detection
let all_photos: Vec<Photo> = stored_photos.iter().map(|(p, _)| p.clone()).collect();
let floors = classify::detect_floors(&all_photos, &text_docs);
self.emit(job_id, tx, JobEvent::FloorsDetected { floors: floors.clone() }).await;
// 6 & 7. Select top interior/exterior photos and infer 3D layout
let mut ranked: Vec<(&Photo, Vec<u8>)> = stored_photos
.iter()
.filter(|(p, _)| {
matches!(
p.tag.as_ref().map(|t| t.kind),
Some(PhotoKind::Interior | PhotoKind::Exterior) | None
)
})
.map(|(p, b)| (p, b.clone()))
.collect();
ranked.sort_by(|(a, _), (b, _)| {
let score = |p: &Photo| match &p.tag {
Some(t) if t.kind == PhotoKind::Interior => 2.0 + t.quality,
Some(t) if t.kind == PhotoKind::Exterior => 1.0 + t.quality,
Some(t) => t.quality,
None => 0.5,
};
score(b).total_cmp(&score(a))
});
ranked.truncate(12);
self.emit(job_id, tx, JobEvent::LayoutDraft { attempt: 1 }).await;
let layout = layout_gen::generate_layout(
&self.llm,
&place,
&ranked,
&floors,
&text_docs,
|_attempt| {},
)
.await?;
self.store.save_layout(&layout).await?;
self.emit(job_id, tx, JobEvent::Done { place_id: place.id.clone() }).await;
Ok(())
}
}
+22
View File
@@ -0,0 +1,22 @@
[package]
name = "providers"
edition.workspace = true
version.workspace = true
[dependencies]
domain = { workspace = true }
async-trait = { workspace = true }
reqwest = { workspace = true }
scraper = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
url = { workspace = true }
urlencoding = { workspace = true }
tokio = { workspace = true }
futures = { workspace = true }
anyhow = { workspace = true }
tracing = { workspace = true }
[dev-dependencies]
tokio = { workspace = true }
+224
View File
@@ -0,0 +1,224 @@
//! Client for the external browser-scraping microservice (Google Maps, Instagram, ...).
//!
//! The service is not implemented yet. Contract (async job API):
//!
//! ```text
//! POST {BASE}/jobs
//! { "source": "google_maps" | "instagram",
//! "place": { "name", "address", "lat", "lng" },
//! "limit": 50,
//! "callback_url": null }
//! -> 202 { "job_id": "..." }
//!
//! GET {BASE}/jobs/{job_id}
//! -> { "status": "queued" | "running" | "done" | "failed",
//! "items": [ { "image_url", "caption", "source_url", "taken_at" } ],
//! "texts": [ { "text", "source_url" } ],
//! "error": null }
//! ```
//!
//! When `BROWSER_SCRAPER_URL` is unset, [`MockBrowserScraper`] is used instead.
use std::time::Duration;
use async_trait::async_trait;
use domain::{PhotoSourceId, Place, RawPhoto, SourceOutput, TextSnippet};
use serde::{Deserialize, Serialize};
use crate::PhotoSource;
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum BrowserSource {
GoogleMaps,
Instagram,
}
impl BrowserSource {
pub fn photo_source_id(self) -> PhotoSourceId {
match self {
BrowserSource::GoogleMaps => PhotoSourceId::GoogleMaps,
BrowserSource::Instagram => PhotoSourceId::Instagram,
}
}
}
#[derive(Debug, Serialize)]
pub struct JobRequest<'a> {
pub source: BrowserSource,
pub place: JobPlace<'a>,
pub limit: usize,
pub callback_url: Option<String>,
}
#[derive(Debug, Serialize)]
pub struct JobPlace<'a> {
pub name: &'a str,
pub address: &'a str,
pub lat: f64,
pub lng: f64,
}
#[derive(Debug, Deserialize)]
pub struct JobCreated {
pub job_id: String,
}
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum JobState {
Queued,
Running,
Done,
Failed,
}
#[derive(Debug, Deserialize)]
pub struct JobResult {
pub status: JobState,
#[serde(default)]
pub items: Vec<JobItem>,
#[serde(default)]
pub texts: Vec<JobText>,
pub error: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct JobItem {
pub image_url: String,
#[serde(default)]
pub caption: String,
#[serde(default)]
pub source_url: String,
pub taken_at: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct JobText {
pub text: String,
#[serde(default)]
pub source_url: String,
}
/// HTTP client for the real service.
pub struct BrowserScraperClient {
client: reqwest::Client,
base: String,
source: BrowserSource,
poll_every: Duration,
timeout: Duration,
}
impl BrowserScraperClient {
pub fn new(base: impl Into<String>, source: BrowserSource) -> Self {
Self {
client: crate::http::api_client(),
base: base.into().trim_end_matches('/').to_string(),
source,
poll_every: Duration::from_secs(2),
timeout: Duration::from_secs(120),
}
}
}
#[async_trait]
impl PhotoSource for BrowserScraperClient {
fn id(&self) -> PhotoSourceId {
self.source.photo_source_id()
}
async fn collect(&self, place: &Place, limit: usize) -> anyhow::Result<SourceOutput> {
let req = JobRequest {
source: self.source,
place: JobPlace {
name: &place.name,
address: if place.road_address.is_empty() { &place.address } else { &place.road_address },
lat: place.lat,
lng: place.lng,
},
limit,
callback_url: None,
};
let created: JobCreated = self
.client
.post(format!("{}/jobs", self.base))
.json(&req)
.send()
.await?
.error_for_status()?
.json()
.await?;
let deadline = tokio::time::Instant::now() + self.timeout;
loop {
tokio::time::sleep(self.poll_every).await;
let r: JobResult = self
.client
.get(format!("{}/jobs/{}", self.base, created.job_id))
.send()
.await?
.error_for_status()?
.json()
.await?;
match r.status {
JobState::Done => return Ok(to_output(self.id(), r)),
JobState::Failed => anyhow::bail!("browser scraper failed: {}", r.error.unwrap_or_default()),
_ if tokio::time::Instant::now() > deadline => anyhow::bail!("browser scraper timed out"),
_ => {}
}
}
}
}
fn to_output(source: PhotoSourceId, r: JobResult) -> SourceOutput {
SourceOutput {
photos: r
.items
.into_iter()
.map(|i| RawPhoto {
source,
image_url: i.image_url,
page_url: i.source_url,
caption: i.caption,
referer: None,
})
.collect(),
texts: r
.texts
.into_iter()
.map(|t| TextSnippet { source, url: t.source_url, text: t.text })
.collect(),
}
}
/// Stand-in used until the browser service exists. Returns nothing (but succeeds)
/// so the pipeline treats the source as "empty" rather than failed.
pub struct MockBrowserScraper {
source: BrowserSource,
}
impl MockBrowserScraper {
pub fn new(source: BrowserSource) -> Self {
Self { source }
}
}
#[async_trait]
impl PhotoSource for MockBrowserScraper {
fn id(&self) -> PhotoSourceId {
self.source.photo_source_id()
}
async fn collect(&self, place: &Place, _limit: usize) -> anyhow::Result<SourceOutput> {
tracing::info!(source = ?self.source, place = %place.name, "browser scraper not configured (mock): returning no results");
Ok(SourceOutput::default())
}
}
/// Pick the real client or the mock depending on configuration.
pub fn browser_source(base_url: Option<&str>, source: BrowserSource) -> Box<dyn PhotoSource> {
match base_url.filter(|u| !u.trim().is_empty()) {
Some(u) => Box::new(BrowserScraperClient::new(u, source)),
None => Box::new(MockBrowserScraper::new(source)),
}
}
+60
View File
@@ -0,0 +1,60 @@
use std::time::Duration;
use regex::Regex;
pub const DESKTOP_UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36";
pub const MOBILE_UA: &str = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1";
/// Shared HTTP client for scraping: browser UA, cookies, gzip, sane timeouts.
pub fn scraping_client() -> reqwest::Client {
reqwest::Client::builder()
.user_agent(DESKTOP_UA)
.cookie_store(true)
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(25))
.default_headers({
let mut h = reqwest::header::HeaderMap::new();
h.insert(reqwest::header::ACCEPT_LANGUAGE, "ko-KR,ko;q=0.9,en;q=0.8".parse().unwrap());
h
})
.build()
.expect("reqwest client")
}
/// Client for official JSON APIs.
pub fn api_client() -> reqwest::Client {
reqwest::Client::builder()
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(20))
.build()
.expect("reqwest client")
}
/// Extract image URLs from arbitrary text (HTML, embedded JSON) whose host matches
/// one of `host_patterns` (regex fragments). Handles `\/` and `\u002F` escaping.
pub fn extract_image_urls(body: &str, host_patterns: &[&str]) -> Vec<String> {
let unescaped = body.replace("\\u002F", "/").replace("\\/", "/").replace("&amp;", "&");
let hosts = host_patterns.join("|");
let re = Regex::new(&format!(
r#"https?://(?:{hosts})[^\s"'<>\\)]*?\.(?:jpe?g|png|webp|JPE?G|PNG|WEBP)(?:\?[^\s"'<>\\)]*)?"#
))
.expect("valid regex");
let mut seen = std::collections::HashSet::new();
re.find_iter(&unescaped)
.map(|m| m.as_str().to_string())
.filter(|u| seen.insert(u.clone()))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_escaped_urls() {
let body = r#"{"a":"https:\/\/ldb-phinf.pstatic.net\/2024\/abc.jpg","b":"https:\u002F\u002Fldb-phinf.pstatic.net\u002Fx\u002Fy.JPG?type=w750"}"#;
let urls = extract_image_urls(body, &[r"ldb-phinf\.pstatic\.net"]);
assert_eq!(urls.len(), 2);
assert!(urls[1].ends_with("?type=w750"));
}
}
+253
View File
@@ -0,0 +1,253 @@
//! Kakao Local REST API (official) with a keyless web-search fallback (`search.map.kakao.com`).
//! Docs: https://developers.kakao.com/docs/latest/ko/local/dev-guide
use async_trait::async_trait;
use domain::PlaceCandidate;
use serde::Deserialize;
use crate::PlaceSearch;
const BASE: &str = "https://dapi.kakao.com/v2/local";
const WEB_SEARCH: &str = "https://search.map.kakao.com/mapsearch/map.daum";
pub struct KakaoLocal {
client: reqwest::Client,
scrape_client: reqwest::Client,
key: String,
}
impl KakaoLocal {
pub fn new(key: impl Into<String>) -> Self {
Self {
client: crate::http::api_client(),
scrape_client: crate::http::scraping_client(),
key: key.into().trim().to_string(),
}
}
pub fn has_api_key(&self) -> bool {
!self.key.is_empty()
}
async fn get<T: for<'de> Deserialize<'de>>(&self, path: &str, query: &[(&str, String)]) -> anyhow::Result<T> {
let resp = self
.client
.get(format!("{BASE}/{path}"))
.header("Authorization", format!("KakaoAK {}", self.key))
.query(query)
.send()
.await?;
let status = resp.status();
if !status.is_success() {
let body = resp.text().await.unwrap_or_default();
anyhow::bail!("kakao {path} {status}: {body}");
}
Ok(resp.json().await?)
}
pub async fn keyword(&self, query: &str) -> anyhow::Result<Vec<PlaceCandidate>> {
let r: KeywordResp = self.get("search/keyword.json", &[("query", query.to_string()), ("size", "15".into())]).await?;
Ok(r.documents.into_iter().map(Into::into).collect())
}
/// Places of category (FD6 음식점, CE7 카페) around a coordinate.
pub async fn category_near(&self, code: &str, lat: f64, lng: f64, radius_m: u32) -> anyhow::Result<Vec<PlaceCandidate>> {
let r: KeywordResp = self
.get(
"search/category.json",
&[
("category_group_code", code.to_string()),
("x", lng.to_string()),
("y", lat.to_string()),
("radius", radius_m.to_string()),
("sort", "distance".into()),
],
)
.await?;
Ok(r.documents.into_iter().map(Into::into).collect())
}
/// Address → coordinates (first match).
pub async fn address(&self, query: &str) -> anyhow::Result<Option<(f64, f64, String)>> {
let r: AddressResp = self.get("search/address.json", &[("query", query.to_string())]).await?;
Ok(r.documents.into_iter().next().and_then(|d| {
Some((d.y.parse().ok()?, d.x.parse().ok()?, d.address_name))
}))
}
/// Keyless fallback via Kakao Map's web JSONP endpoint (`search.map.kakao.com/mapsearch/map.daum`).
pub async fn web_search(&self, query: &str) -> anyhow::Result<Vec<PlaceCandidate>> {
let text = self
.scrape_client
.get(WEB_SEARCH)
.header(reqwest::header::REFERER, "https://map.kakao.com/")
.query(&[("callback", "j"), ("q", query), ("msFlag", "A"), ("sort", "0")])
.send()
.await?
.error_for_status()?
.text()
.await?;
parse_web_search_jsonp(&text)
}
}
pub fn parse_web_search_jsonp(text: &str) -> anyhow::Result<Vec<PlaceCandidate>> {
let start = text.find('{').ok_or_else(|| anyhow::anyhow!("kakao web search: missing JSON object"))?;
let end = text.rfind('}').ok_or_else(|| anyhow::anyhow!("kakao web search: missing closing brace"))?;
let r: WebSearchResp = serde_json::from_str(&text[start..=end])?;
Ok(r.place
.into_iter()
.filter(|p| !p.confirmid.is_empty() && p.lat != 0.0 && p.lon != 0.0)
.map(|p| PlaceCandidate {
provider: "kakao".into(),
place_url: format!("https://place.map.kakao.com/{}", p.confirmid),
provider_id: p.confirmid,
name: p.name,
category: p.last_cate_name,
address: p.address,
road_address: p.new_address,
phone: p.tel,
lat: p.lat,
lng: p.lon,
})
.collect())
}
#[async_trait]
impl PlaceSearch for KakaoLocal {
fn name(&self) -> &'static str {
"kakao"
}
/// Uses official REST API when `KAKAO_REST_API_KEY` is set; otherwise falls back
/// to Kakao Map's web search so development works without an API key.
async fn search(&self, query: &str) -> anyhow::Result<Vec<PlaceCandidate>> {
if !self.has_api_key() {
return self.web_search(query).await;
}
match self.keyword(query).await {
Ok(mut out) => {
if out.is_empty() || looks_like_address(query) {
if let Some((lat, lng, _)) = self.address(query).await.ok().flatten() {
for code in ["FD6", "CE7"] {
for c in self.category_near(code, lat, lng, 40).await.unwrap_or_default() {
if !out.iter().any(|o| o.provider_id == c.provider_id) {
out.push(c);
}
}
}
}
}
Ok(out)
}
Err(e) => {
tracing::warn!("kakao REST search failed ({e}), falling back to web search");
self.web_search(query).await
}
}
}
}
pub fn looks_like_address(q: &str) -> bool {
let re = regex::Regex::new(r"(로|길|동|읍|면|리)\s*\d+(-\d+)?").unwrap();
re.is_match(q)
}
#[derive(Deserialize)]
struct KeywordResp {
documents: Vec<KakaoPlaceDoc>,
}
#[derive(Deserialize)]
struct KakaoPlaceDoc {
id: String,
place_name: String,
#[serde(default)]
category_name: String,
#[serde(default)]
phone: String,
#[serde(default)]
address_name: String,
#[serde(default)]
road_address_name: String,
x: String,
y: String,
#[serde(default)]
place_url: String,
}
impl From<KakaoPlaceDoc> for PlaceCandidate {
fn from(d: KakaoPlaceDoc) -> Self {
PlaceCandidate {
provider: "kakao".into(),
provider_id: d.id,
name: d.place_name,
category: d.category_name.rsplit(" > ").next().unwrap_or_default().to_string(),
address: d.address_name,
road_address: d.road_address_name,
phone: d.phone,
lat: d.y.parse().unwrap_or_default(),
lng: d.x.parse().unwrap_or_default(),
place_url: d.place_url,
}
}
}
#[derive(Deserialize)]
struct AddressResp {
documents: Vec<AddressDoc>,
}
#[derive(Deserialize)]
struct AddressDoc {
address_name: String,
x: String,
y: String,
}
#[derive(Deserialize)]
struct WebSearchResp {
#[serde(default)]
place: Vec<WebPlaceDoc>,
}
#[derive(Deserialize)]
struct WebPlaceDoc {
#[serde(default)]
confirmid: String,
#[serde(default)]
name: String,
#[serde(default)]
last_cate_name: String,
#[serde(default)]
address: String,
#[serde(default)]
new_address: String,
#[serde(default)]
tel: String,
#[serde(default)]
lat: f64,
#[serde(default)]
lon: f64,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn address_detection() {
assert!(looks_like_address("서울 성동구 성수이로 78"));
assert!(looks_like_address("연무장길 5-1"));
assert!(!looks_like_address("대림창고"));
}
#[test]
fn parses_jsonp() {
let raw = r#"/**/j({"place":[{"confirmid":"1128989913","name":"대림창고","last_cate_name":"갤러리카페","address":"서울 성동구 성수동2가 322-32 1층","new_address":"서울 성동구 성수이로 78","tel":"0507-1390-9669","lat":37.5417,"lon":127.0562}]});"#;
let list = parse_web_search_jsonp(raw).unwrap();
assert_eq!(list.len(), 1);
assert_eq!(list[0].provider_id, "1128989913");
assert_eq!(list[0].name, "대림창고");
}
}
+203
View File
@@ -0,0 +1,203 @@
//! Kakao Map place panel + photos (unofficial, verified 2026-11-04).
//!
//! Endpoint: `https://place-api.map.kakao.com/places/panel3/{kakao_id}`
//! Required headers: `pf: web`, `Referer: https://place.map.kakao.com/{kakao_id}`.
use std::collections::HashSet;
use async_trait::async_trait;
use domain::{PhotoSourceId, Place, RawPhoto, SourceOutput, TextSnippet};
use serde_json::Value;
use crate::{http, PhotoSource};
pub struct KakaoPlacePhotos {
client: reqwest::Client,
}
impl Default for KakaoPlacePhotos {
fn default() -> Self {
Self::new()
}
}
impl KakaoPlacePhotos {
pub fn new() -> Self {
Self { client: http::scraping_client() }
}
}
#[async_trait]
impl PhotoSource for KakaoPlacePhotos {
fn id(&self) -> PhotoSourceId {
PhotoSourceId::KakaoPlace
}
async fn collect(&self, place: &Place, limit: usize) -> anyhow::Result<SourceOutput> {
let Some(id) = place.kakao_id.as_deref() else {
anyhow::bail!("kakao place id missing");
};
let url = format!("https://place-api.map.kakao.com/places/panel3/{id}");
let referer = format!("https://place.map.kakao.com/{id}");
let text = self
.client
.get(&url)
.header("pf", "web")
.header(reqwest::header::REFERER, &referer)
.header(reqwest::header::ACCEPT, "application/json")
.send()
.await?
.error_for_status()?
.text()
.await?;
parse_panel3_json(&text, &referer, limit)
}
}
pub fn parse_panel3_json(raw: &str, page_url: &str, limit: usize) -> anyhow::Result<SourceOutput> {
let v: Value = serde_json::from_str(raw)?;
let mut seen = HashSet::new();
let mut photos = Vec::new();
let mut text_parts = Vec::new();
// Address detail often contains explicit floor info, e.g. "서울 성동구 성수이로 78 1층".
if let Some(disp) = v.pointer("/summary/address/disp").and_then(Value::as_str) {
text_parts.push(format!("주소: {disp}"));
}
if let Some(detail) = v.pointer("/summary/address/address_detail_part").and_then(Value::as_str) {
if !detail.is_empty() {
text_parts.push(format!("층/호수 정보: {detail}"));
}
}
if let Some(tags) = v.pointer("/place_add_info/tags").and_then(Value::as_array) {
let t: Vec<&str> = tags.iter().filter_map(Value::as_str).collect();
if !t.is_empty() {
text_parts.push(format!("태그: {}", t.join(", ")));
}
}
if let Some(ai_summary) = v.pointer("/ai_mate/bottom_sheet/summary").and_then(Value::as_str) {
text_parts.push(ai_summary.to_string());
}
if let Some(infos) = v.pointer("/place_add_info/full_detail_infos").and_then(Value::as_array) {
for info in infos {
let title = info.get("title").and_then(Value::as_str).unwrap_or_default();
let items: Vec<&str> = info
.get("items")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|i| i.pointer("/icon/text").and_then(Value::as_str))
.collect();
if !items.is_empty() {
text_parts.push(format!("{title}: {}", items.join(", ")));
}
}
}
// Structured photo list
if let Some(arr) = v.pointer("/photos/photos").and_then(Value::as_array) {
for item in arr {
if photos.len() >= limit {
break;
}
if let Some(u) = item.get("url").and_then(Value::as_str) {
let clean = unwrap_kakao_thumb(u);
if seen.insert(clean.clone()) {
let caption = item.get("title").and_then(Value::as_str).unwrap_or_default().to_string();
photos.push(RawPhoto {
source: PhotoSourceId::KakaoPlace,
image_url: clean,
page_url: page_url.to_string(),
caption,
referer: Some("https://place.map.kakao.com/".into()),
});
}
}
}
}
// Review texts + review photos
for review_path in ["/kakaomap_review/reviews", "/blog_review/reviews"] {
if let Some(reviews) = v.pointer(review_path).and_then(Value::as_array) {
for r in reviews {
let contents = r
.get("contents")
.or_else(|| r.get("Summary"))
.or_else(|| r.get("title"))
.and_then(Value::as_str)
.unwrap_or_default();
if contents.chars().count() >= 6 {
text_parts.push(contents.to_string());
}
if let Some(p_arr) = r.get("photos").or_else(|| r.get("photo_list")).and_then(Value::as_array) {
for p in p_arr {
if photos.len() >= limit {
break;
}
if let Some(u) = p.get("url").or_else(|| p.get("orgurl")).and_then(Value::as_str) {
let clean = unwrap_kakao_thumb(u);
if seen.insert(clean.clone()) {
photos.push(RawPhoto {
source: PhotoSourceId::KakaoPlace,
image_url: clean,
page_url: page_url.to_string(),
caption: contents.chars().take(180).collect(),
referer: Some("https://place.map.kakao.com/".into()),
});
}
}
}
}
}
}
}
let mut texts = Vec::new();
if !text_parts.is_empty() {
let joined: String = text_parts.join("\n").chars().take(3000).collect();
texts.push(TextSnippet {
source: PhotoSourceId::KakaoPlace,
url: page_url.to_string(),
text: joined,
});
}
Ok(SourceOutput { photos, texts })
}
/// Unwrap Kakao's thumbnail proxy (`?fname=https%3A%2F%2F...`) and upgrade `http://` to `https://`.
pub fn unwrap_kakao_thumb(u: &str) -> String {
if let Some(idx) = u.find("fname=") {
let encoded = &u[idx + 6..];
let end = encoded.find('&').unwrap_or(encoded.len());
if let Ok(decoded) = urlencoding::decode(&encoded[..end]) {
if decoded.starts_with("http") {
return decoded.into_owned().replace("http://", "https://");
}
}
}
u.replace("http://", "https://")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unwraps_fname_proxy() {
let u = "https://img1.kakaocdn.net/cthumb/local/R0x420/?fname=http%3A%2F%2Ft1.daumcdn.net%2Flocal%2Fabc.jpg";
assert_eq!(unwrap_kakao_thumb(u), "https://t1.daumcdn.net/local/abc.jpg");
}
#[test]
fn parses_panel_sample() {
let sample = r#"{
"summary":{"address":{"disp":"서울 성동구 성수이로 78 1층","address_detail_part":"1층"}},
"place_add_info":{"tags":["갤러리카페","루프탑있는"]},
"photos":{"photos":[{"url":"http://t1.kakaocdn.net/local/kakaomapPhoto/review/abc?original","title":"실내"}]}
}"#;
let out = parse_panel3_json(sample, "https://place.map.kakao.com/1128989913", 10).unwrap();
assert_eq!(out.photos.len(), 1);
assert!(out.photos[0].image_url.starts_with("https://"));
assert!(out.texts[0].text.contains("루프탑있는"));
}
}
+32
View File
@@ -0,0 +1,32 @@
//! External data providers: place search, geocoding and photo/text sources.
//!
//! * Official APIs: [`kakao_local`], [`ncp_geocode`]
//! * In-process scrapers: [`naver_map`], [`naver_place`], [`naver_blog`], [`kakao_place`]
//! * Delegated to the external browser scraping service: [`browser`] (Google Maps, Instagram)
pub mod browser;
pub mod http;
pub mod kakao_local;
pub mod kakao_place;
pub mod naver_blog;
pub mod naver_map;
pub mod naver_place;
pub mod ncp_geocode;
pub mod text;
use async_trait::async_trait;
use domain::{PhotoSourceId, Place, PlaceCandidate, SourceOutput};
/// Free-text place search (상호명 / 주소 → candidates).
#[async_trait]
pub trait PlaceSearch: Send + Sync {
fn name(&self) -> &'static str;
async fn search(&self, query: &str) -> anyhow::Result<Vec<PlaceCandidate>>;
}
/// A source of photos and text evidence for a resolved place.
#[async_trait]
pub trait PhotoSource: Send + Sync {
fn id(&self) -> PhotoSourceId;
async fn collect(&self, place: &Place, limit: usize) -> anyhow::Result<SourceOutput>;
}
+244
View File
@@ -0,0 +1,244 @@
//! Naver Blog posts about the place: inline images + body text (unofficial, verified 2026-11-04).
//!
//! 1. Search `https://search.naver.com/search.naver?ssc=tab.blog.all&query=...` for `blog.naver.com/{blogId}/{logNo}` links.
//! 2. Fetch each post via `https://blog.naver.com/PostView.naver?blogId=...&logNo=...` (bypasses outer iframe).
//! 3. Parse SmartEditor (`.se-main-container`) paragraphs and `postfiles.pstatic.net` images, pairing each image
//! with nearby paragraph text as its caption.
use std::collections::HashSet;
use async_trait::async_trait;
use domain::{PhotoSourceId, Place, RawPhoto, SourceOutput, TextSnippet};
use futures::stream::{self, StreamExt};
use regex::Regex;
use scraper::{Html, Selector};
use crate::{http, PhotoSource};
const SEARCH_URL: &str = "https://search.naver.com/search.naver";
const POST_VIEW_URL: &str = "https://blog.naver.com/PostView.naver";
pub struct NaverBlog {
client: reqwest::Client,
max_posts: usize,
}
impl Default for NaverBlog {
fn default() -> Self {
Self::new()
}
}
impl NaverBlog {
pub fn new() -> Self {
Self { client: http::scraping_client(), max_posts: 8 }
}
async fn search_post_ids(&self, query: &str) -> anyhow::Result<Vec<(String, String)>> {
let html = self
.client
.get(SEARCH_URL)
.header(reqwest::header::REFERER, "https://search.naver.com/")
.query(&[("ssc", "tab.blog.all"), ("query", query)])
.send()
.await?
.error_for_status()?
.text()
.await?;
Ok(extract_blog_post_ids(&html))
}
async fn fetch_post(&self, blog_id: &str, log_no: &str) -> anyhow::Result<SourceOutput> {
let page_url = format!("https://blog.naver.com/{blog_id}/{log_no}");
let html = self
.client
.get(POST_VIEW_URL)
.header(reqwest::header::REFERER, "https://blog.naver.com/")
.query(&[("blogId", blog_id), ("logNo", log_no)])
.send()
.await?
.error_for_status()?
.text()
.await?;
Ok(parse_blog_post_html(&html, &page_url))
}
}
#[async_trait]
impl PhotoSource for NaverBlog {
fn id(&self) -> PhotoSourceId {
PhotoSourceId::NaverBlog
}
async fn collect(&self, place: &Place, limit: usize) -> anyhow::Result<SourceOutput> {
let region = place.region_hint();
let q1 = format!("{} {} 내부 좌석", region, place.name);
let q2 = format!("{} {}", region, place.name);
let mut post_ids = self.search_post_ids(q1.trim()).await.unwrap_or_default();
if post_ids.len() < self.max_posts {
for id in self.search_post_ids(q2.trim()).await.unwrap_or_default() {
if !post_ids.contains(&id) {
post_ids.push(id);
}
}
}
post_ids.truncate(self.max_posts);
let results: Vec<SourceOutput> = stream::iter(post_ids)
.map(|(b, l)| async move { self.fetch_post(&b, &l).await.ok() })
.buffer_unordered(4)
.filter_map(|x| async move { x })
.collect()
.await;
let mut seen = HashSet::new();
let mut photos = Vec::new();
let mut texts = Vec::new();
for r in results {
texts.extend(r.texts);
for p in r.photos {
if photos.len() >= limit {
break;
}
if seen.insert(p.image_url.clone()) {
photos.push(p);
}
}
}
Ok(SourceOutput { photos, texts })
}
}
pub fn extract_blog_post_ids(html: &str) -> Vec<(String, String)> {
let re = Regex::new(r"https?://(?:m\.)?blog\.naver\.com/([a-zA-Z0-9_\-]+)/(\d{10,15})").unwrap();
let mut seen = HashSet::new();
let mut out = Vec::new();
for c in re.captures_iter(html) {
let pair = (c[1].to_string(), c[2].to_string());
if seen.insert(pair.clone()) {
out.push(pair);
}
}
out
}
pub fn parse_blog_post_html(html: &str, page_url: &str) -> SourceOutput {
let doc = Html::parse_document(html);
let container_sel = Selector::parse(".se-main-container, #postViewArea").unwrap();
let p_sel = Selector::parse(".se-text-paragraph, p").unwrap();
let img_sel = Selector::parse("img").unwrap();
let mut paragraphs = Vec::new();
for container in doc.select(&container_sel) {
for p in container.select(&p_sel) {
let t = p.text().collect::<Vec<_>>().join(" ");
let clean = t.split_whitespace().collect::<Vec<_>>().join(" ");
if clean.chars().count() >= 4 {
paragraphs.push(clean);
}
}
}
if paragraphs.is_empty() {
for p in doc.select(&p_sel) {
let clean = p.text().collect::<Vec<_>>().join(" ").split_whitespace().collect::<Vec<_>>().join(" ");
if clean.chars().count() >= 4 {
paragraphs.push(clean);
}
}
}
let full_text: String = paragraphs.join("\n").chars().take(3000).collect();
let mut texts = Vec::new();
if !full_text.is_empty() {
texts.push(TextSnippet {
source: PhotoSourceId::NaverBlog,
url: page_url.to_string(),
text: full_text.clone(),
});
}
let mut seen = HashSet::new();
let mut photos = Vec::new();
for img in doc.select(&img_sel) {
let v = img.value();
let src = v
.attr("data-lazy-src")
.or_else(|| v.attr("data-src"))
.or_else(|| v.attr("src"))
.unwrap_or_default();
if let Some(url) = normalize_blog_image_url(src) {
if seen.insert(url.clone()) {
photos.push(RawPhoto {
source: PhotoSourceId::NaverBlog,
image_url: url,
page_url: page_url.to_string(),
caption: String::new(),
referer: Some("https://blog.naver.com/".into()),
});
}
}
}
// Fallback regex scan if DOM attributes differed
if photos.is_empty() {
for u in http::extract_image_urls(html, &[r"postfiles\.pstatic\.net", r"blogfiles\.pstatic\.net"]) {
if let Some(url) = normalize_blog_image_url(&u) {
if seen.insert(url.clone()) {
photos.push(RawPhoto {
source: PhotoSourceId::NaverBlog,
image_url: url,
page_url: page_url.to_string(),
caption: String::new(),
referer: Some("https://blog.naver.com/".into()),
});
}
}
}
}
// Spread paragraph context across photos proportionally
if !paragraphs.is_empty() && !photos.is_empty() {
let n_p = paragraphs.len();
let n_img = photos.len();
for (i, photo) in photos.iter_mut().enumerate() {
let center = (i * n_p) / n_img;
let start = center.saturating_sub(1);
let end = (center + 2).min(n_p);
photo.caption = paragraphs[start..end].join(" ").chars().take(220).collect();
}
}
SourceOutput { photos, texts }
}
fn normalize_blog_image_url(raw: &str) -> Option<String> {
let u = raw.replace("&amp;", "&");
if !(u.contains("postfiles.pstatic.net") || u.contains("blogfiles.pstatic.net")) {
return None;
}
let lower = u.to_ascii_lowercase();
if lower.contains("sticker") || lower.contains("emoticon") || lower.contains("profile") || lower.contains(".gif") {
return None;
}
let base = u.split('?').next().unwrap_or(&u);
Some(format!("{base}?type=w966"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_blog_html() {
let html = r#"<div class="se-main-container">
<p class="se-text-paragraph">1층에서 주문하고 2층으로 올라갔어요</p>
<img data-lazy-src="https://postfiles.pstatic.net/2026/img1.JPEG?type=w80_blur"/>
<p class="se-text-paragraph">창가 바 좌석이 6자리 정도 있어요</p>
</div>"#;
let out = parse_blog_post_html(html, "https://blog.naver.com/a/123456789012");
assert_eq!(out.photos.len(), 1);
assert_eq!(out.photos[0].image_url, "https://postfiles.pstatic.net/2026/img1.JPEG?type=w966");
assert!(out.photos[0].caption.contains("2층"));
assert_eq!(out.texts.len(), 1);
}
}
+231
View File
@@ -0,0 +1,231 @@
//! Naver Map web search (unofficial, verified 2026-11-04).
//!
//! Primary endpoint: `https://map.naver.com/p/api/search/instant-search?query=...&coords=lat,lng`
//! Fallback endpoint: `https://m.place.naver.com/place/list?query=...&x=lng&y=lat` (parses `window.__APOLLO_STATE__`).
use async_trait::async_trait;
use domain::{Place, PlaceCandidate};
use serde::Deserialize;
use serde_json::Value;
use crate::{http, text, PlaceSearch};
const INSTANT_URL: &str = "https://map.naver.com/p/api/search/instant-search";
const MPLACE_LIST_URL: &str = "https://m.place.naver.com/place/list";
pub struct NaverMapSearch {
client: reqwest::Client,
}
impl Default for NaverMapSearch {
fn default() -> Self {
Self::new()
}
}
impl NaverMapSearch {
pub fn new() -> Self {
Self { client: http::scraping_client() }
}
/// Search places on Naver Map, optionally biased around `(lat, lng)`.
pub async fn search_near(&self, query: &str, near: Option<(f64, f64)>) -> anyhow::Result<Vec<PlaceCandidate>> {
let mut out = self.instant_search(query, near).await.unwrap_or_default();
if out.is_empty() {
if let Ok(fallback) = self.mobile_list_search(query, near).await {
out = fallback;
}
}
Ok(out)
}
async fn instant_search(&self, query: &str, near: Option<(f64, f64)>) -> anyhow::Result<Vec<PlaceCandidate>> {
let (lat, lng) = near.unwrap_or((37.5665, 126.9780));
let coords = format!("{lat},{lng}");
let resp = self
.client
.get(INSTANT_URL)
.header(reqwest::header::REFERER, "https://map.naver.com/")
.query(&[("query", query), ("coords", &coords)])
.send()
.await?
.error_for_status()?;
let body: InstantResp = resp.json().await?;
Ok(parse_instant(body))
}
async fn mobile_list_search(&self, query: &str, near: Option<(f64, f64)>) -> anyhow::Result<Vec<PlaceCandidate>> {
let (lat, lng) = near.unwrap_or((37.5665, 126.9780));
let html = self
.client
.get(MPLACE_LIST_URL)
.header(reqwest::header::USER_AGENT, http::MOBILE_UA)
.query(&[("query", query), ("x", &lng.to_string()), ("y", &lat.to_string())])
.send()
.await?
.error_for_status()?
.text()
.await?;
Ok(parse_apollo_place_list(&html))
}
/// Find the Naver place id matching `place` (name similarity + coordinate distance).
pub async fn resolve_place_id(&self, place: &Place) -> anyhow::Result<Option<String>> {
let queries = [
format!("{} {}", place.name, place.region_hint()),
place.name.clone(),
];
for q in queries {
let candidates = self.search_near(q.trim(), Some((place.lat, place.lng))).await.unwrap_or_default();
if let Some(id) = pick_best_match(place, &candidates) {
tracing::debug!(place = %place.name, naver_id = %id, "resolved naver place id");
return Ok(Some(id));
}
}
Ok(None)
}
}
pub fn pick_best_match(place: &Place, candidates: &[PlaceCandidate]) -> Option<String> {
candidates
.iter()
.filter_map(|c| {
let sim = text::name_similarity(&place.name, &c.name);
let dist = text::haversine_m(place.lat, place.lng, c.lat, c.lng);
// Accept either strong name similarity within 200m, or very close (<50m) with moderate name match.
if (sim >= 0.55 && dist <= 200.0) || (sim >= 0.35 && dist <= 50.0) {
let score = sim * 100.0 - (dist as f32) * 0.2;
Some((score, c.provider_id.clone()))
} else {
None
}
})
.max_by(|a, b| a.0.total_cmp(&b.0))
.map(|(_, id)| id)
}
fn parse_instant(body: InstantResp) -> Vec<PlaceCandidate> {
body.place
.into_iter()
.filter_map(|p| {
let lat: f64 = p.y.parse().ok()?;
let lng: f64 = p.x.parse().ok()?;
Some(PlaceCandidate {
provider: "naver".into(),
place_url: format!("https://m.place.naver.com/place/{}/home", p.id),
provider_id: p.id,
name: p.title,
category: p.ctg,
address: p.jibun_address,
road_address: p.road_address,
phone: String::new(),
lat,
lng,
})
})
.collect()
}
pub fn parse_apollo_place_list(html: &str) -> Vec<PlaceCandidate> {
let Some(state) = extract_apollo_state(html) else {
return Vec::new();
};
let Some(obj) = state.as_object() else {
return Vec::new();
};
let mut out = Vec::new();
for (k, v) in obj {
if !k.starts_with("PlaceListBusinessesItem:") && !k.starts_with("PlaceSummary:") {
continue;
}
let Some(id) = v.get("id").and_then(Value::as_str) else { continue };
let Some(name) = v.get("name").and_then(Value::as_str) else { continue };
let lat: f64 = v.get("y").and_then(Value::as_str).and_then(|s| s.parse().ok()).unwrap_or_default();
let lng: f64 = v.get("x").and_then(Value::as_str).and_then(|s| s.parse().ok()).unwrap_or_default();
if lat == 0.0 || lng == 0.0 {
continue;
}
let category = v.get("category").and_then(Value::as_str).unwrap_or_default().to_string();
let address = v
.get("fullAddress")
.or_else(|| v.get("address"))
.and_then(Value::as_str)
.unwrap_or_default()
.to_string();
let road_address = v.get("roadAddress").and_then(Value::as_str).unwrap_or_default().to_string();
let phone = v
.get("phone")
.or_else(|| v.get("virtualPhone"))
.and_then(Value::as_str)
.unwrap_or_default()
.to_string();
out.push(PlaceCandidate {
provider: "naver".into(),
provider_id: id.to_string(),
name: name.to_string(),
category,
address,
road_address,
phone,
lat,
lng,
place_url: format!("https://m.place.naver.com/place/{id}/home"),
});
}
out
}
/// Extract the first JSON object assigned to `__APOLLO_STATE__`.
pub fn extract_apollo_state(html: &str) -> Option<Value> {
let marker = "__APOLLO_STATE__";
let idx = html.find(marker)?;
let rest = &html[idx + marker.len()..];
let brace = rest.find('{')?;
let mut de = serde_json::Deserializer::from_str(&rest[brace..]).into_iter::<Value>();
de.next()?.ok()
}
#[async_trait]
impl PlaceSearch for NaverMapSearch {
fn name(&self) -> &'static str {
"naver"
}
async fn search(&self, query: &str) -> anyhow::Result<Vec<PlaceCandidate>> {
self.search_near(query, None).await
}
}
#[derive(Deserialize)]
struct InstantResp {
#[serde(default)]
place: Vec<InstantPlace>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct InstantPlace {
id: String,
title: String,
#[serde(default)]
ctg: String,
#[serde(default)]
jibun_address: String,
#[serde(default)]
road_address: String,
x: String,
y: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_apollo_snippet() {
let html = r#"<script>window.__APOLLO_STATE__ = {"PlaceListBusinessesItem:37910590":{"id":"37910590","name":"성수동대림창고갤러리","category":"카페,디저트","fullAddress":"서울특별시 성동구 성수이로 78","roadAddress":"성수이로 78","virtualPhone":"0507-1390-9669","x":"127.0563952","y":"37.5417036"}};</script>"#;
let list = parse_apollo_place_list(html);
assert_eq!(list.len(), 1);
assert_eq!(list[0].provider_id, "37910590");
}
}
+209
View File
@@ -0,0 +1,209 @@
//! Naver Place photos + info/review text (unofficial, verified 2026-11-04).
//!
//! Fetches `https://m.place.naver.com/place/{id}/{home,photo,review/visitor}` (follows 302 to
//! `/restaurant/{id}/...` or `/cafe/{id}/...`) and extracts image URLs on `*-phinf.pstatic.net`
//! plus descriptive text from `window.__APOLLO_STATE__`.
use std::collections::HashSet;
use async_trait::async_trait;
use domain::{PhotoSourceId, Place, RawPhoto, SourceOutput, TextSnippet};
use serde_json::Value;
use crate::{http, naver_map, PhotoSource};
const PHINF_HOSTS: &[&str] = &[
r"ldb-phinf\.pstatic\.net",
r"pup-review-phinf\.pstatic\.net",
r"naverbooking-phinf\.pstatic\.net",
];
pub struct NaverPlacePhotos {
client: reqwest::Client,
}
impl Default for NaverPlacePhotos {
fn default() -> Self {
Self::new()
}
}
impl NaverPlacePhotos {
pub fn new() -> Self {
Self { client: http::scraping_client() }
}
async fn fetch_tab(&self, place_id: &str, tab: &str) -> anyhow::Result<(String, String)> {
let url = format!("https://m.place.naver.com/place/{place_id}/{tab}");
let resp = self
.client
.get(&url)
.header(reqwest::header::USER_AGENT, http::MOBILE_UA)
.header(reqwest::header::REFERER, "https://m.place.naver.com/")
.send()
.await?
.error_for_status()?;
let final_url = resp.url().to_string();
Ok((final_url, resp.text().await?))
}
}
#[async_trait]
impl PhotoSource for NaverPlacePhotos {
fn id(&self) -> PhotoSourceId {
PhotoSourceId::NaverPlace
}
async fn collect(&self, place: &Place, limit: usize) -> anyhow::Result<SourceOutput> {
let Some(place_id) = place.naver_place_id.as_deref() else {
anyhow::bail!("naver place id not resolved");
};
let mut seen = HashSet::new();
let mut photos = Vec::new();
let mut texts = Vec::new();
for tab in ["photo", "review/visitor", "information", "home"] {
if photos.len() >= limit {
break;
}
let (page_url, html) = match self.fetch_tab(place_id, tab).await {
Ok(v) => v,
Err(e) => {
tracing::debug!(place_id, tab, "naver place tab failed: {e}");
continue;
}
};
parse_naver_place_html(&html, &page_url, limit, &mut seen, &mut photos, &mut texts);
}
Ok(SourceOutput { photos, texts })
}
}
pub fn parse_naver_place_html(
html: &str,
page_url: &str,
limit: usize,
seen: &mut HashSet<String>,
photos: &mut Vec<RawPhoto>,
texts: &mut Vec<TextSnippet>,
) {
if let Some(state) = naver_map::extract_apollo_state(html) {
extract_apollo_texts_and_photos(&state, page_url, limit, seen, photos, texts);
}
for raw_url in http::extract_image_urls(html, PHINF_HOSTS) {
if photos.len() >= limit {
break;
}
if is_noise_image(&raw_url) {
continue;
}
let clean = normalize_phinf_url(&raw_url);
if seen.insert(clean.clone()) {
photos.push(RawPhoto {
source: PhotoSourceId::NaverPlace,
image_url: upgrade_phinf_size(&clean),
page_url: page_url.to_string(),
caption: String::new(),
referer: Some("https://m.place.naver.com/".into()),
});
}
}
}
fn extract_apollo_texts_and_photos(
state: &Value,
page_url: &str,
limit: usize,
seen: &mut HashSet<String>,
photos: &mut Vec<RawPhoto>,
texts: &mut Vec<TextSnippet>,
) {
let Some(obj) = state.as_object() else { return };
let mut text_lines = Vec::new();
for (_k, v) in obj {
let Some(map) = v.as_object() else { continue };
for field in ["description", "introduction", "conveniences", "parking", "direction", "body", "text"] {
if let Some(val) = map.get(field) {
match val {
Value::String(s) if s.chars().count() >= 6 => text_lines.push(s.clone()),
Value::Array(arr) => {
let joined: Vec<&str> = arr.iter().filter_map(Value::as_str).collect();
if !joined.is_empty() {
text_lines.push(joined.join(", "));
}
}
_ => {}
}
}
}
// Review or photo entries often carry both `body`/`text` and `media`/`originUrl`.
if photos.len() < limit {
if let Some(url) = map
.get("originUrl")
.or_else(|| map.get("imageUrl"))
.and_then(Value::as_str)
.filter(|u| u.contains("-phinf.pstatic.net") && !is_noise_image(u))
{
let clean = normalize_phinf_url(url);
if seen.insert(clean.clone()) {
let caption = map
.get("body")
.or_else(|| map.get("text"))
.or_else(|| map.get("description"))
.and_then(Value::as_str)
.unwrap_or_default()
.chars()
.take(200)
.collect();
photos.push(RawPhoto {
source: PhotoSourceId::NaverPlace,
image_url: upgrade_phinf_size(&clean),
page_url: page_url.to_string(),
caption,
referer: Some("https://m.place.naver.com/".into()),
});
}
}
}
}
if !text_lines.is_empty() {
text_lines.dedup();
let combined: String = text_lines.join("\n").chars().take(3000).collect();
texts.push(TextSnippet {
source: PhotoSourceId::NaverPlace,
url: page_url.to_string(),
text: combined,
});
}
}
fn normalize_phinf_url(u: &str) -> String {
u.split('?').next().unwrap_or(u).to_string()
}
fn upgrade_phinf_size(clean_url: &str) -> String {
format!("{clean_url}?type=w1200_60_sharpen")
}
fn is_noise_image(u: &str) -> bool {
let lower = u.to_ascii_lowercase();
lower.contains("profile") || lower.contains("icon") || lower.contains("logo") || lower.contains(" default")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_apollo_and_regex_photos() {
let html = r#"<script>window.__APOLLO_STATE__ = {"VisitorReview:1":{"body":"2층 창가 자리가 넓어요","originUrl":"https:\u002F\u002Fpup-review-phinf.pstatic.net\u002F2026\u002Fa.jpg?type=w300"}};</script><img src="https://ldb-phinf.pstatic.net/2025/b.jpg"/>"#;
let mut seen = HashSet::new();
let mut photos = Vec::new();
let mut texts = Vec::new();
parse_naver_place_html(html, "https://m.place.naver.com/place/1/photo", 10, &mut seen, &mut photos, &mut texts);
assert_eq!(photos.len(), 2);
assert_eq!(photos[0].caption, "2층 창가 자리가 넓어요");
assert_eq!(texts.len(), 1);
}
}
+66
View File
@@ -0,0 +1,66 @@
//! Naver Cloud Platform Maps — Geocoding (official). Address normalization/verification only.
//! Docs: https://api.ncloud-docs.com/docs/application-maps-geocoding
use serde::Deserialize;
const URL: &str = "https://maps.apigw.ntruss.com/map-geocode/v2/geocode";
pub struct NcpGeocode {
client: reqwest::Client,
id: String,
secret: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct GeocodeResult {
pub road_address: String,
pub jibun_address: String,
pub lat: f64,
pub lng: f64,
}
impl NcpGeocode {
pub fn new(id: impl Into<String>, secret: impl Into<String>) -> Self {
Self { client: crate::http::api_client(), id: id.into(), secret: secret.into() }
}
pub async fn geocode(&self, query: &str) -> anyhow::Result<Option<GeocodeResult>> {
let resp = self
.client
.get(URL)
.header("x-ncp-apigw-api-key-id", &self.id)
.header("x-ncp-apigw-api-key", &self.secret)
.header("Accept", "application/json")
.query(&[("query", query)])
.send()
.await?;
let status = resp.status();
if !status.is_success() {
anyhow::bail!("ncp geocode {status}: {}", resp.text().await.unwrap_or_default());
}
let r: Resp = resp.json().await?;
Ok(r.addresses.into_iter().next().map(|a| GeocodeResult {
road_address: a.road_address,
jibun_address: a.jibun_address,
lat: a.y.parse().unwrap_or_default(),
lng: a.x.parse().unwrap_or_default(),
}))
}
}
#[derive(Deserialize)]
struct Resp {
#[serde(default)]
addresses: Vec<Addr>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Addr {
#[serde(default)]
road_address: String,
#[serde(default)]
jibun_address: String,
x: String,
y: String,
}
+139
View File
@@ -0,0 +1,139 @@
//! Text helpers: name normalization/similarity, distance, floor keyword extraction.
use regex::Regex;
use std::sync::LazyLock;
/// Lowercase, strip whitespace/punctuation so "스타벅스 성수점" ≈ "스타벅스성수점".
pub fn normalize_name(s: &str) -> String {
s.chars()
.filter(|c| c.is_alphanumeric())
.flat_map(|c| c.to_lowercase())
.collect()
}
/// Similarity in 0..=1 based on character bigram Dice coefficient.
pub fn name_similarity(a: &str, b: &str) -> f32 {
let a = normalize_name(a);
let b = normalize_name(b);
if a.is_empty() || b.is_empty() {
return 0.0;
}
if a == b {
return 1.0;
}
if a.contains(&b) || b.contains(&a) {
return 0.9;
}
let bigrams = |s: &str| -> Vec<(char, char)> {
let cs: Vec<char> = s.chars().collect();
cs.windows(2).map(|w| (w[0], w[1])).collect()
};
let ba = bigrams(&a);
let mut bb = bigrams(&b);
if ba.is_empty() || bb.is_empty() {
return 0.0;
}
let total = ba.len() + bb.len();
let mut hits = 0;
for g in &ba {
if let Some(pos) = bb.iter().position(|x| x == g) {
bb.swap_remove(pos);
hits += 1;
}
}
(2 * hits) as f32 / total as f32
}
/// Great-circle distance in meters.
pub fn haversine_m(lat1: f64, lng1: f64, lat2: f64, lng2: f64) -> f64 {
let r = 6_371_000.0_f64;
let (p1, p2) = (lat1.to_radians(), lat2.to_radians());
let dp = (lat2 - lat1).to_radians();
let dl = (lng2 - lng1).to_radians();
let a = (dp / 2.0).sin().powi(2) + p1.cos() * p2.cos() * (dl / 2.0).sin().powi(2);
2.0 * r * a.sqrt().asin()
}
static FLOOR_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"(?x)
(?P<b>지하\s*(?P<bn>\d)\s*층|B(?P<bn2>\d)\b|지하층)
| (?P<n>\d{1,2})\s*(?:층|F\b|f\b)
| (?P<roof>루프탑|옥상)
").unwrap()
});
/// Floor mentions found in text: (level, snippet). Rooftop = `domain::ROOFTOP_LEVEL`.
pub fn floor_mentions(text: &str) -> Vec<(i32, String)> {
let mut out = Vec::new();
for caps in FLOOR_RE.captures_iter(text) {
let m = caps.get(0).unwrap();
let level = if caps.name("b").is_some() {
caps.name("bn")
.or(caps.name("bn2"))
.and_then(|x| x.as_str().parse::<i32>().ok())
.map(|n| -n)
.unwrap_or(-1)
} else if let Some(n) = caps.name("n") {
match n.as_str().parse::<i32>() {
Ok(n) if (1..=20).contains(&n) => n,
_ => continue,
}
} else if caps.name("roof").is_some() {
domain::ROOFTOP_LEVEL
} else {
continue;
};
out.push((level, snippet_around(text, m.start(), m.end(), 40)));
}
out
}
/// Text window of roughly `radius` chars around a byte range, on char boundaries.
pub fn snippet_around(text: &str, start: usize, end: usize, radius: usize) -> String {
let mut s = start;
for _ in 0..radius {
if s == 0 {
break;
}
s -= 1;
while !text.is_char_boundary(s) {
s -= 1;
}
}
let mut e = end;
for _ in 0..radius {
if e >= text.len() {
break;
}
e += 1;
while e < text.len() && !text.is_char_boundary(e) {
e += 1;
}
}
text[s..e].split_whitespace().collect::<Vec<_>>().join(" ")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn similarity() {
assert!(name_similarity("스타벅스 성수점", "스타벅스성수점") > 0.99);
assert!(name_similarity("대림창고", "대림창고 갤러리컬럼") >= 0.9);
assert!(name_similarity("블루보틀", "스타벅스") < 0.2);
}
#[test]
fn floors() {
let m = floor_mentions("1층은 주문, 2층 좌석이 넓고 루프탑도 있어요. 지하 1층 화장실");
let levels: Vec<i32> = m.iter().map(|x| x.0).collect();
assert_eq!(levels, vec![1, 2, domain::ROOFTOP_LEVEL, -1]);
}
#[test]
fn distance() {
let d = haversine_m(37.5446, 127.0557, 37.5450, 127.0557);
assert!((d - 44.5).abs() < 2.0);
}
}
+18
View File
@@ -0,0 +1,18 @@
[package]
name = "storage"
edition.workspace = true
version.workspace = true
[dependencies]
domain = { workspace = true }
mongodb = { workspace = true }
object_store = { workspace = true }
bytes = { workspace = true }
serde = { workspace = true }
futures = { workspace = true }
anyhow = { workspace = true }
tracing = { workspace = true }
chrono = { workspace = true }
[dev-dependencies]
tokio = { workspace = true }
+557
View File
@@ -0,0 +1,557 @@
//! MongoDB repositories + MinIO (S3) blob store, with automatic in-memory fallback
//! when MongoDB or MinIO is not yet configured/reachable in local development.
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::Duration;
use bytes::Bytes;
use domain::{JobEvent, JobStatus, Layout, Photo, PhotoTag, Place, RecentPlace};
use futures::TryStreamExt;
use mongodb::bson::{self, doc};
use mongodb::options::ClientOptions;
use mongodb::{Collection, Database};
use object_store::aws::AmazonS3Builder;
use object_store::memory::InMemory;
use object_store::path::Path as ObjPath;
use object_store::{ObjectStore, PutPayload};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct StorageConfig {
pub mongodb_uri: String,
pub mongodb_db: String,
pub minio_endpoint: String,
pub minio_access_key: String,
pub minio_secret_key: String,
pub minio_bucket: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LayoutDoc {
#[serde(rename = "_id")]
pub id: String,
pub place_id: String,
pub version: i64,
pub created_at: String,
pub layout: Layout,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobDoc {
#[serde(rename = "_id")]
pub id: String,
pub place_id: String,
pub status: JobStatus,
pub events: Vec<JobEvent>,
pub created_at: String,
pub updated_at: String,
}
/// Text evidence persisted per place (blog bodies, place info).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextDoc {
pub place_id: String,
pub source: domain::PhotoSourceId,
pub url: String,
pub text: String,
}
/// View history is separate from place metadata, which analysis may replace.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PlaceViewDoc {
#[serde(rename = "_id")]
place_id: String,
last_viewed_at: String,
}
const RECENT_PLACES_LIMIT: usize = 50;
#[derive(Default)]
struct MemDb {
places: HashMap<String, Place>,
place_views: HashMap<String, String>,
photos: HashMap<String, Photo>,
layouts: HashMap<String, Vec<LayoutDoc>>,
jobs: HashMap<String, JobDoc>,
texts: HashMap<String, Vec<TextDoc>>,
}
#[derive(Clone)]
pub struct Store {
db: Option<Database>,
mem: Arc<RwLock<MemDb>>,
blobs: Arc<dyn ObjectStore>,
mem_blobs: Arc<InMemory>,
}
fn now() -> String {
chrono::Utc::now().to_rfc3339()
}
fn recent_places_from_memory(mem: &MemDb) -> Vec<RecentPlace> {
let mut places: Vec<_> = mem
.layouts
.iter()
.filter_map(|(place_id, versions)| {
let place = mem.places.get(place_id)?.clone();
let latest = versions.iter().max_by(|a, b| {
a.version.cmp(&b.version).then(a.created_at.cmp(&b.created_at))
})?;
Some(RecentPlace {
place,
analyzed_at: latest.created_at.clone(),
last_viewed_at: mem.place_views.get(place_id).cloned(),
floor_labels: latest.layout.floors.iter().map(|f| f.label.clone()).collect(),
seat_estimate: latest.layout.summary.seat_estimate,
})
})
.collect();
// MongoDB dates have millisecond precision. Match that ordering in fallback mode.
let sort_time = |place: &RecentPlace| {
chrono::DateTime::parse_from_rfc3339(
place.last_viewed_at.as_deref().unwrap_or(&place.analyzed_at),
)
.map(|time| time.timestamp_millis())
.unwrap_or(0)
};
places.sort_by(|a, b| sort_time(b).cmp(&sort_time(a)).then(a.place.id.cmp(&b.place.id)));
places.truncate(RECENT_PLACES_LIMIT);
places
}
fn recent_sort_time() -> bson::Document {
// Older records use chrono's micro/nanosecond RFC3339 output. Normalize only
// fractional precision because MongoDB's date parser accepts milliseconds.
doc! { "$let": {
"vars": { "raw": { "$ifNull": ["$last_viewed_at", "$analyzed_at"] } },
"in": { "$let": {
"vars": { "precise": { "$regexFind": {
"input": "$$raw",
"regex": r"^(.*\.\d{3})\d+(Z|[+-]\d{2}:\d{2})$"
} } },
"in": { "$convert": {
"input": { "$cond": [
{ "$ne": ["$$precise", bson::Bson::Null] },
{ "$concat": [
{ "$arrayElemAt": ["$$precise.captures", 0] },
{ "$arrayElemAt": ["$$precise.captures", 1] }
] },
"$$raw"
] },
"to": "date",
"onError": bson::DateTime::from_millis(0),
"onNull": bson::DateTime::from_millis(0)
} }
} }
} }
}
impl Store {
pub async fn connect(cfg: &StorageConfig) -> anyhow::Result<Self> {
let mut opts = ClientOptions::parse(&cfg.mongodb_uri).await?;
opts.server_selection_timeout = Some(Duration::from_secs(3));
opts.connect_timeout = Some(Duration::from_secs(3));
let client = mongodb::Client::with_options(opts)?;
let candidate_db = client.database(&cfg.mongodb_db);
let db = match candidate_db.run_command(doc! { "ping": 1 }).await {
Ok(_) => {
tracing::info!(uri = %cfg.mongodb_uri, db = %cfg.mongodb_db, "connected to MongoDB");
Some(candidate_db)
}
Err(e) => {
tracing::warn!("MongoDB unreachable ({e}); using in-memory store until MongoDB is available");
None
}
};
let mem_blobs = Arc::new(InMemory::new());
let blobs: Arc<dyn ObjectStore> = if !cfg.minio_access_key.trim().is_empty()
&& !cfg.minio_secret_key.trim().is_empty()
{
let s3 = AmazonS3Builder::new()
.with_endpoint(&cfg.minio_endpoint)
.with_bucket_name(&cfg.minio_bucket)
.with_access_key_id(&cfg.minio_access_key)
.with_secret_access_key(&cfg.minio_secret_key)
.with_region("us-east-1")
.with_allow_http(true)
.with_virtual_hosted_style_request(false)
.build()?;
let probe = ObjPath::from("_health/probe.txt");
match s3.put(&probe, PutPayload::from_static(b"ok")).await {
Ok(_) => {
tracing::info!(endpoint = %cfg.minio_endpoint, bucket = %cfg.minio_bucket, "connected to MinIO/S3");
Arc::new(s3)
}
Err(e) => {
tracing::warn!("MinIO bucket '{}' not reachable ({e}); falling back to in-memory blob store", cfg.minio_bucket);
mem_blobs.clone()
}
}
} else {
tracing::warn!("MINIO_ACCESS_KEY/MINIO_SECRET_KEY empty; using in-memory blob store");
mem_blobs.clone()
};
Ok(Self {
db,
mem: Arc::new(RwLock::new(MemDb::default())),
blobs,
mem_blobs,
})
}
pub async fn health_check(&self) -> anyhow::Result<()> {
if let Some(db) = &self.db {
db.run_command(doc! { "ping": 1 }).await?;
}
let probe = ObjPath::from("_health/probe.txt");
self.blobs.put(&probe, PutPayload::from_static(b"ok")).await?;
self.blobs.get(&probe).await?.bytes().await?;
Ok(())
}
pub async fn ensure_indexes(&self) -> anyhow::Result<()> {
let Some(db) = &self.db else { return Ok(()) };
use mongodb::IndexModel;
db.collection::<Photo>("photos")
.create_index(IndexModel::builder().keys(doc! { "place_id": 1 }).build())
.await?;
db.collection::<LayoutDoc>("layouts")
.create_index(IndexModel::builder().keys(doc! { "place_id": 1, "version": -1 }).build())
.await?;
db.collection::<JobDoc>("jobs")
.create_index(IndexModel::builder().keys(doc! { "place_id": 1, "created_at": -1 }).build())
.await?;
db.collection::<TextDoc>("texts")
.create_index(IndexModel::builder().keys(doc! { "place_id": 1 }).build())
.await?;
Ok(())
}
fn places(&self) -> Option<Collection<Place>> {
self.db.as_ref().map(|d| d.collection("places"))
}
fn photos(&self) -> Option<Collection<Photo>> {
self.db.as_ref().map(|d| d.collection("photos"))
}
fn layouts(&self) -> Option<Collection<LayoutDoc>> {
self.db.as_ref().map(|d| d.collection("layouts"))
}
fn jobs(&self) -> Option<Collection<JobDoc>> {
self.db.as_ref().map(|d| d.collection("jobs"))
}
fn texts(&self) -> Option<Collection<TextDoc>> {
self.db.as_ref().map(|d| d.collection("texts"))
}
// ---------- places ----------
pub async fn upsert_place(&self, place: &Place) -> anyhow::Result<()> {
self.mem.write().unwrap().places.insert(place.id.clone(), place.clone());
if let Some(col) = self.places() {
col.replace_one(doc! { "_id": &place.id }, place).upsert(true).await?;
}
Ok(())
}
pub async fn get_place(&self, id: &str) -> anyhow::Result<Option<Place>> {
if let Some(col) = self.places() {
if let Ok(Some(p)) = col.find_one(doc! { "_id": id }).await {
return Ok(Some(p));
}
}
Ok(self.mem.read().unwrap().places.get(id).cloned())
}
pub async fn record_place_view(&self, place_id: &str) -> anyhow::Result<()> {
let viewed_at = now();
self.mem
.write()
.unwrap()
.place_views
.insert(place_id.to_string(), viewed_at.clone());
if let Some(db) = &self.db {
db.collection::<PlaceViewDoc>("place_views")
.update_one(
doc! { "_id": place_id },
doc! { "$set": { "last_viewed_at": viewed_at } },
)
.upsert(true)
.await?;
}
Ok(())
}
/// Latest saved analyses, including analyses created before view tracking existed.
pub async fn recent_places(&self) -> anyhow::Result<Vec<RecentPlace>> {
if let Some(col) = self.layouts() {
// Project only card data rather than loading every historical 3D layout.
let pipeline = vec![
doc! { "$sort": { "place_id": 1, "version": -1, "created_at": -1, "_id": 1 } },
doc! { "$group": { "_id": "$place_id", "latest": { "$first": "$$ROOT" } } },
doc! { "$lookup": {
"from": "places", "localField": "_id", "foreignField": "_id", "as": "place"
} },
doc! { "$unwind": "$place" },
doc! { "$lookup": {
"from": "place_views", "localField": "_id", "foreignField": "_id", "as": "views"
} },
doc! { "$project": {
"_id": 0,
"place": 1,
"analyzed_at": "$latest.created_at",
"last_viewed_at": { "$ifNull": [
{ "$arrayElemAt": ["$views.last_viewed_at", 0] }, bson::Bson::Null
] },
"floor_labels": "$latest.layout.floors.label",
"seat_estimate": "$latest.layout.summary.seat_estimate"
} },
doc! { "$set": { "sort_time": recent_sort_time() } },
doc! { "$sort": { "sort_time": -1, "place._id": 1 } },
doc! { "$limit": RECENT_PLACES_LIMIT as i64 },
doc! { "$unset": "sort_time" },
];
let result = async {
let docs = col.aggregate(pipeline).await?.try_collect::<Vec<_>>().await?;
docs.into_iter()
.map(bson::from_document::<RecentPlace>)
.collect::<Result<Vec<_>, _>>()
.map_err(anyhow::Error::from)
}
.await;
match result {
Ok(list) if !list.is_empty() => return Ok(list),
Err(error) => {
let fallback = recent_places_from_memory(&self.mem.read().unwrap());
if fallback.is_empty() {
return Err(error);
}
tracing::warn!(%error, "recent places query failed; using in-memory history");
return Ok(fallback);
}
_ => {}
}
}
Ok(recent_places_from_memory(&self.mem.read().unwrap()))
}
// ---------- photos ----------
pub async fn insert_photo(&self, photo: &Photo) -> anyhow::Result<()> {
self.mem.write().unwrap().photos.insert(photo.id.clone(), photo.clone());
if let Some(col) = self.photos() {
col.replace_one(doc! { "_id": &photo.id }, photo).upsert(true).await?;
}
Ok(())
}
pub async fn photos_for_place(&self, place_id: &str) -> anyhow::Result<Vec<Photo>> {
if let Some(col) = self.photos() {
if let Ok(cur) = col.find(doc! { "place_id": place_id }).await {
if let Ok(list) = cur.try_collect::<Vec<Photo>>().await {
if !list.is_empty() {
return Ok(list);
}
}
}
}
Ok(self
.mem
.read()
.unwrap()
.photos
.values()
.filter(|p| p.place_id == place_id)
.cloned()
.collect())
}
pub async fn get_photo(&self, id: &str) -> anyhow::Result<Option<Photo>> {
if let Some(col) = self.photos() {
if let Ok(Some(p)) = col.find_one(doc! { "_id": id }).await {
return Ok(Some(p));
}
}
Ok(self.mem.read().unwrap().photos.get(id).cloned())
}
pub async fn set_photo_tag(&self, id: &str, tag: &PhotoTag) -> anyhow::Result<()> {
if let Some(p) = self.mem.write().unwrap().photos.get_mut(id) {
p.tag = Some(tag.clone());
}
if let Some(col) = self.photos() {
col.update_one(doc! { "_id": id }, doc! { "$set": { "tag": bson::to_bson(tag)? } })
.await?;
}
Ok(())
}
pub async fn delete_photos_for_place(&self, place_id: &str) -> anyhow::Result<()> {
self.mem.write().unwrap().photos.retain(|_, p| p.place_id != place_id);
if let Some(col) = self.photos() {
col.delete_many(doc! { "place_id": place_id }).await?;
}
Ok(())
}
// ---------- texts ----------
pub async fn replace_texts(&self, place_id: &str, texts: &[TextDoc]) -> anyhow::Result<()> {
self.mem
.write()
.unwrap()
.texts
.insert(place_id.to_string(), texts.to_vec());
if let Some(col) = self.texts() {
col.delete_many(doc! { "place_id": place_id }).await?;
if !texts.is_empty() {
col.insert_many(texts).await?;
}
}
Ok(())
}
pub async fn texts_for_place(&self, place_id: &str) -> anyhow::Result<Vec<TextDoc>> {
if let Some(col) = self.texts() {
if let Ok(cur) = col.find(doc! { "place_id": place_id }).await {
if let Ok(list) = cur.try_collect::<Vec<TextDoc>>().await {
if !list.is_empty() {
return Ok(list);
}
}
}
}
Ok(self
.mem
.read()
.unwrap()
.texts
.get(place_id)
.cloned()
.unwrap_or_default())
}
// ---------- layouts ----------
pub async fn save_layout(&self, layout: &Layout) -> anyhow::Result<LayoutDoc> {
let version = self.latest_layout(&layout.place_id).await?.map(|d| d.version + 1).unwrap_or(1);
let d = LayoutDoc {
id: format!("{}#v{version}", layout.place_id),
place_id: layout.place_id.clone(),
version,
created_at: now(),
layout: layout.clone(),
};
self.mem
.write()
.unwrap()
.layouts
.entry(layout.place_id.clone())
.or_default()
.push(d.clone());
if let Some(col) = self.layouts() {
col.insert_one(&d).await?;
}
Ok(d)
}
pub async fn latest_layout(&self, place_id: &str) -> anyhow::Result<Option<LayoutDoc>> {
if let Some(col) = self.layouts() {
if let Ok(Some(d)) = col
.find_one(doc! { "place_id": place_id })
.sort(doc! { "version": -1 })
.await
{
return Ok(Some(d));
}
}
Ok(self
.mem
.read()
.unwrap()
.layouts
.get(place_id)
.and_then(|v| v.last().cloned()))
}
// ---------- jobs ----------
pub async fn create_job(&self, id: &str, place_id: &str) -> anyhow::Result<()> {
let t = now();
let doc = JobDoc {
id: id.to_string(),
place_id: place_id.to_string(),
status: JobStatus::Running,
events: vec![],
created_at: t.clone(),
updated_at: t,
};
self.mem.write().unwrap().jobs.insert(id.to_string(), doc.clone());
if let Some(col) = self.jobs() {
col.insert_one(doc).await?;
}
Ok(())
}
pub async fn push_job_event(&self, id: &str, ev: &JobEvent) -> anyhow::Result<()> {
if let Some(j) = self.mem.write().unwrap().jobs.get_mut(id) {
j.updated_at = now();
if matches!(ev, JobEvent::Done { .. }) {
j.status = JobStatus::Done;
} else if matches!(ev, JobEvent::Error { .. }) {
j.status = JobStatus::Failed;
}
j.events.push(ev.clone());
}
if let Some(col) = self.jobs() {
let mut set = doc! { "updated_at": now() };
match ev {
JobEvent::Done { .. } => {
set.insert("status", bson::to_bson(&JobStatus::Done)?);
}
JobEvent::Error { .. } => {
set.insert("status", bson::to_bson(&JobStatus::Failed)?);
}
_ => {}
}
col.update_one(
doc! { "_id": id },
doc! { "$push": { "events": bson::to_bson(ev)? }, "$set": set },
)
.await?;
}
Ok(())
}
pub async fn get_job(&self, id: &str) -> anyhow::Result<Option<JobDoc>> {
if let Some(col) = self.jobs() {
if let Ok(Some(j)) = col.find_one(doc! { "_id": id }).await {
return Ok(Some(j));
}
}
Ok(self.mem.read().unwrap().jobs.get(id).cloned())
}
// ---------- blobs ----------
pub async fn put_blob(&self, key: &str, bytes: Bytes) -> anyhow::Result<()> {
let p = ObjPath::from(key);
if self.blobs.put(&p, PutPayload::from(bytes.clone())).await.is_err() {
self.mem_blobs.put(&p, PutPayload::from(bytes)).await?;
}
Ok(())
}
pub async fn get_blob(&self, key: &str) -> anyhow::Result<Bytes> {
let p = ObjPath::from(key);
match self.blobs.get(&p).await {
Ok(r) => Ok(r.bytes().await?),
Err(_) => Ok(self.mem_blobs.get(&p).await?.bytes().await?),
}
}
}
#[cfg(test)]
mod recent_tests;
+136
View File
@@ -0,0 +1,136 @@
use super::*;
use domain::{Accessibility, Floor, Summary, Tri};
fn store() -> Store {
let blobs = Arc::new(InMemory::new());
Store {
db: None,
mem: Arc::new(RwLock::new(MemDb::default())),
blobs: blobs.clone(),
mem_blobs: blobs,
}
}
fn place(id: &str) -> Place {
Place {
id: id.into(),
kakao_id: None,
naver_place_id: None,
name: id.into(),
category: "카페".into(),
address: String::new(),
road_address: String::new(),
phone: String::new(),
lat: 0.0,
lng: 0.0,
place_url: String::new(),
}
}
fn layout(id: &str, label: &str, seats: u32) -> Layout {
Layout {
place_id: id.into(),
generated_at: "2024-01-01T00:00:00Z".into(),
model: "test".into(),
confidence: 0.5,
floors: vec![Floor {
level: 1,
label: label.into(),
confidence: 0.5,
width_m: 10.0,
depth_m: 10.0,
openings: vec![],
zones: vec![],
verticals: vec![],
notes: String::new(),
}],
summary: Summary {
area_pyeong_min: 20.0,
area_pyeong_max: 30.0,
seat_estimate: seats,
seat_types: vec![],
highlights: vec![],
accessibility: Accessibility {
elevator: Tri::Unknown,
step_free_entrance: Tri::Unknown,
},
caveats: vec![],
},
}
}
async fn saved_place(store: &Store, id: &str, created_at: &str) {
store.upsert_place(&place(id)).await.unwrap();
store.save_layout(&layout(id, "1F", 20)).await.unwrap();
store.mem.write().unwrap().layouts.get_mut(id).unwrap()[0].created_at = created_at.into();
}
#[tokio::test]
async fn recent_places_require_metadata_and_saved_layout() {
let store = store();
store.upsert_place(&place("unfinished")).await.unwrap();
store.record_place_view("unfinished").await.unwrap();
store.save_layout(&layout("orphaned-layout", "1F", 20)).await.unwrap();
saved_place(&store, "completed", "2024-01-01T00:00:00Z").await;
let recent = store.recent_places().await.unwrap();
assert_eq!(recent.len(), 1);
assert_eq!(recent[0].place.id, "completed");
// Cached records created before this feature need no view-history migration.
assert_eq!(recent[0].last_viewed_at, None);
assert_eq!(recent[0].analyzed_at, "2024-01-01T00:00:00Z");
}
#[tokio::test]
async fn recent_places_use_latest_version_summary_once() {
let store = store();
saved_place(&store, "revised", "2024-01-01T00:00:00Z").await;
let latest = store.save_layout(&layout("revised", "2F", 42)).await.unwrap();
// The list must select the highest version even if the fallback entries are reordered.
store.mem.write().unwrap().layouts.get_mut("revised").unwrap().reverse();
let recent = store.recent_places().await.unwrap();
assert_eq!(recent.len(), 1);
assert_eq!(recent[0].floor_labels, ["2F"]);
assert_eq!(recent[0].seat_estimate, 42);
assert_eq!(recent[0].analyzed_at, latest.created_at);
}
#[tokio::test]
async fn revisiting_updates_order_and_survives_place_refresh() {
let store = store();
saved_place(&store, "older", "2024-01-01T00:00:00Z").await;
saved_place(&store, "newer", "2025-01-01T00:00:00Z").await;
assert_eq!(store.recent_places().await.unwrap()[0].place.id, "newer");
store.record_place_view("older").await.unwrap();
let mut updated = place("older");
updated.name = "Updated name".into();
store.upsert_place(&updated).await.unwrap();
let recent = store.recent_places().await.unwrap();
assert_eq!(recent[0].place.name, "Updated name");
assert_eq!(recent[0].place.id, "older");
assert!(recent[0].last_viewed_at.is_some());
assert_eq!(recent[0].analyzed_at, "2024-01-01T00:00:00Z");
}
#[tokio::test]
async fn recent_places_are_bounded_and_stable_on_timestamp_ties() {
let store = store();
for number in (0..55).rev() {
// Equivalent instants expressed with different offsets still tie.
let at = if number % 2 == 0 {
"2024-01-01T00:00:00Z"
} else {
"2024-01-01T09:00:00+09:00"
};
saved_place(&store, &format!("place-{number:02}"), at).await;
}
let recent = store.recent_places().await.unwrap();
assert_eq!(recent.len(), 50);
let ids: Vec<_> = recent.iter().map(|row| row.place.id.clone()).collect();
let expected: Vec<_> = (0..50).map(|number| format!("place-{number:02}")).collect();
assert_eq!(ids, expected);
}