feat(suggestions): add vector-based playlist suggestions engine
- Add suggestions.rs with SuggestionsEngine for generating suggestions - Add SuggestionConfig with configurable parameters - Add SuggestedTrack and SuggestionResult types - Integrate with MusicBrainz relationships and Qobuz track search - Add Tauri commands: get_playlist_suggestions_v2, get_vector_store_stats - Add cleanup_vector_store command for maintenance - Add Serialize to StoreStats for IPC - Register ArtistVectorStoreState in app initialization - 23 tests passing
This commit is contained in:
1 parent
f3dcb6ebf2
commit
cadb346e32
6 files changed
+475
-2
No files matched your search
@@ -46,9 +46,13 @@
|
||||
pub mod builder;
|
||||
pub mod sparse_vector;
|
||||
pub mod store;
|
||||
pub mod suggestions;
|
||||
pub mod weights;
|
||||
|
||||
pub use builder::{ArtistVectorBuilder, BuildResult};
|
||||
pub use sparse_vector::SparseVector;
|
||||
pub use store::{ArtistVectorStore, ArtistVectorStoreState, SimilarArtist, StoreStats, VectorEntry};
|
||||
pub use suggestions::{
|
||||
extract_artist_mbids, SuggestionConfig, SuggestionResult, SuggestedTrack, SuggestionsEngine,
|
||||
};
|
||||
pub use weights::RelationshipWeights;
|
||||
@@ -378,7 +378,7 @@ impl ArtistVectorStore {
|
||||
}
|
||||
|
||||
/// Get artist name from index
|
||||
fn get_artist_name(&self, mbid: &str) -> Option<String> {
|
||||
pub fn get_artist_name(&self, mbid: &str) -> Option<String> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT name FROM artist_index WHERE mbid = ?1",
|
||||
@@ -438,7 +438,7 @@ impl ArtistVectorStore {
|
||||
}
|
||||
|
||||
/// Statistics about the vector store
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct StoreStats {
|
||||
pub artist_count: usize,
|
||||
pub vector_count: usize,
|
||||
|
||||
@@ -0,0 +1,376 @@
|
||||
//! Playlist suggestions engine
|
||||
//!
|
||||
//! Uses artist vectors to suggest new tracks for a playlist.
|
||||
//! Algorithm:
|
||||
//! 1. Extract unique artists from playlist tracks
|
||||
//! 2. Compute combined playlist vector (sum + normalize)
|
||||
//! 3. Find nearest artists not already in playlist
|
||||
//! 4. Search Qobuz for top tracks by those artists
|
||||
//! 5. Return suggested tracks with optional reasons
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::api::{QobuzClient, Track};
|
||||
|
||||
use super::builder::ArtistVectorBuilder;
|
||||
use super::sparse_vector::SparseVector;
|
||||
use super::store::ArtistVectorStore;
|
||||
|
||||
/// Configuration for suggestion generation
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SuggestionConfig {
|
||||
/// Maximum number of artists to consider for suggestions
|
||||
pub max_artists: usize,
|
||||
/// Number of tracks to fetch per artist
|
||||
pub tracks_per_artist: usize,
|
||||
/// Maximum total tracks in the suggestion pool
|
||||
pub max_pool_size: usize,
|
||||
/// Maximum age (days) for vector freshness
|
||||
pub vector_max_age_days: i64,
|
||||
/// Minimum similarity score to include artist
|
||||
pub min_similarity: f32,
|
||||
}
|
||||
|
||||
impl Default for SuggestionConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_artists: 20,
|
||||
tracks_per_artist: 5,
|
||||
max_pool_size: 100,
|
||||
vector_max_age_days: 7,
|
||||
min_similarity: 0.1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A suggested track with metadata
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SuggestedTrack {
|
||||
/// Qobuz track ID
|
||||
pub track_id: u64,
|
||||
/// Track title
|
||||
pub title: String,
|
||||
/// Artist name
|
||||
pub artist_name: String,
|
||||
/// Artist MBID (if known)
|
||||
pub artist_mbid: Option<String>,
|
||||
/// Album title
|
||||
pub album_title: String,
|
||||
/// Album ID for cover art
|
||||
pub album_id: String,
|
||||
/// Duration in seconds
|
||||
pub duration: u32,
|
||||
/// Similarity score (higher = more similar to playlist)
|
||||
pub similarity_score: f32,
|
||||
/// Reason for suggestion (for dev mode)
|
||||
pub reason: Option<String>,
|
||||
}
|
||||
|
||||
/// Result of suggestion generation
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SuggestionResult {
|
||||
/// Pool of suggested tracks
|
||||
pub tracks: Vec<SuggestedTrack>,
|
||||
/// Artists that contributed to suggestions
|
||||
pub source_artists: Vec<String>,
|
||||
/// Number of playlist artists analyzed
|
||||
pub playlist_artists_count: usize,
|
||||
/// Number of similar artists found
|
||||
pub similar_artists_count: usize,
|
||||
}
|
||||
|
||||
/// Playlist suggestions engine
|
||||
pub struct SuggestionsEngine {
|
||||
/// Vector store for similarity lookups
|
||||
store: Arc<Mutex<ArtistVectorStore>>,
|
||||
/// Vector builder for lazy construction
|
||||
builder: Arc<ArtistVectorBuilder>,
|
||||
/// Qobuz client for track search
|
||||
qobuz_client: Arc<Mutex<QobuzClient>>,
|
||||
/// Configuration
|
||||
config: SuggestionConfig,
|
||||
}
|
||||
|
||||
impl SuggestionsEngine {
|
||||
/// Create a new suggestions engine
|
||||
pub fn new(
|
||||
store: Arc<Mutex<ArtistVectorStore>>,
|
||||
builder: Arc<ArtistVectorBuilder>,
|
||||
qobuz_client: Arc<Mutex<QobuzClient>>,
|
||||
config: SuggestionConfig,
|
||||
) -> Self {
|
||||
Self {
|
||||
store,
|
||||
builder,
|
||||
qobuz_client,
|
||||
config,
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate suggestions for a playlist
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `playlist_artist_mbids` - MBIDs of artists in the playlist
|
||||
/// * `exclude_track_ids` - Track IDs to exclude (already in playlist)
|
||||
/// * `include_reasons` - Whether to include reason strings (dev mode)
|
||||
pub async fn generate_suggestions(
|
||||
&self,
|
||||
playlist_artist_mbids: &[String],
|
||||
exclude_track_ids: &HashSet<u64>,
|
||||
include_reasons: bool,
|
||||
) -> Result<SuggestionResult, String> {
|
||||
if playlist_artist_mbids.is_empty() {
|
||||
return Ok(SuggestionResult {
|
||||
tracks: Vec::new(),
|
||||
source_artists: Vec::new(),
|
||||
playlist_artists_count: 0,
|
||||
similar_artists_count: 0,
|
||||
});
|
||||
}
|
||||
|
||||
// 1. Ensure vectors exist for playlist artists
|
||||
for mbid in playlist_artist_mbids {
|
||||
let _ = self
|
||||
.builder
|
||||
.ensure_vector(mbid, None, None, self.config.vector_max_age_days)
|
||||
.await;
|
||||
}
|
||||
|
||||
// 2. Compute combined playlist vector
|
||||
let playlist_vector = self.compute_playlist_vector(playlist_artist_mbids).await?;
|
||||
|
||||
if playlist_vector.is_empty() {
|
||||
return Ok(SuggestionResult {
|
||||
tracks: Vec::new(),
|
||||
source_artists: Vec::new(),
|
||||
playlist_artists_count: playlist_artist_mbids.len(),
|
||||
similar_artists_count: 0,
|
||||
});
|
||||
}
|
||||
|
||||
// 3. Find nearest artists (excluding playlist artists)
|
||||
let exclude_vec: Vec<String> = playlist_artist_mbids.to_vec();
|
||||
let similar_artists = {
|
||||
let store = self.store.lock().await;
|
||||
store.find_nearest(&playlist_vector, self.config.max_artists, &exclude_vec)?
|
||||
};
|
||||
|
||||
let similar_artists_count = similar_artists.len();
|
||||
let mut source_artists = Vec::new();
|
||||
let mut all_tracks = Vec::new();
|
||||
|
||||
// 4. Search for tracks by similar artists
|
||||
for artist in &similar_artists {
|
||||
if artist.similarity < self.config.min_similarity {
|
||||
continue;
|
||||
}
|
||||
|
||||
source_artists.push(artist.name.clone().unwrap_or_else(|| artist.mbid.clone()));
|
||||
|
||||
// Search Qobuz for tracks by this artist
|
||||
let tracks = self
|
||||
.search_artist_tracks(&artist.mbid, artist.name.as_deref(), artist.similarity)
|
||||
.await;
|
||||
|
||||
for mut track in tracks {
|
||||
// Skip if already in playlist
|
||||
if exclude_track_ids.contains(&track.track_id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add reason if requested
|
||||
if include_reasons {
|
||||
track.reason = Some(self.generate_reason(
|
||||
&artist.mbid,
|
||||
artist.name.as_deref(),
|
||||
artist.similarity,
|
||||
playlist_artist_mbids,
|
||||
));
|
||||
}
|
||||
|
||||
all_tracks.push(track);
|
||||
}
|
||||
|
||||
// Stop if we have enough tracks
|
||||
if all_tracks.len() >= self.config.max_pool_size {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Sort by similarity score and limit pool size
|
||||
all_tracks.sort_by(|a, b| {
|
||||
b.similarity_score
|
||||
.partial_cmp(&a.similarity_score)
|
||||
.unwrap_or(std::cmp::Ordering::Equal)
|
||||
});
|
||||
all_tracks.truncate(self.config.max_pool_size);
|
||||
|
||||
Ok(SuggestionResult {
|
||||
tracks: all_tracks,
|
||||
source_artists,
|
||||
playlist_artists_count: playlist_artist_mbids.len(),
|
||||
similar_artists_count,
|
||||
})
|
||||
}
|
||||
|
||||
/// Compute combined vector for playlist artists
|
||||
async fn compute_playlist_vector(
|
||||
&self,
|
||||
artist_mbids: &[String],
|
||||
) -> Result<SparseVector, String> {
|
||||
let mut combined = SparseVector::new();
|
||||
let store = self.store.lock().await;
|
||||
|
||||
for mbid in artist_mbids {
|
||||
if let Some(vector) = store.get_vector(mbid) {
|
||||
combined = combined.add(&vector);
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize to unit length
|
||||
Ok(combined.normalize())
|
||||
}
|
||||
|
||||
/// Search Qobuz for tracks by an artist
|
||||
async fn search_artist_tracks(
|
||||
&self,
|
||||
artist_mbid: &str,
|
||||
artist_name: Option<&str>,
|
||||
similarity: f32,
|
||||
) -> Vec<SuggestedTrack> {
|
||||
let search_query = match artist_name {
|
||||
Some(name) => name.to_string(),
|
||||
None => {
|
||||
// Try to get name from store
|
||||
let store = self.store.lock().await;
|
||||
store
|
||||
.get_artist_name(artist_mbid)
|
||||
.unwrap_or_else(|| artist_mbid.to_string())
|
||||
}
|
||||
};
|
||||
|
||||
let client = self.qobuz_client.lock().await;
|
||||
|
||||
// Search for tracks by artist name
|
||||
match client
|
||||
.search_tracks(&search_query, self.config.tracks_per_artist as u32, 0, None)
|
||||
.await
|
||||
{
|
||||
Ok(results) => {
|
||||
let mut tracks = Vec::new();
|
||||
|
||||
for item in results.items {
|
||||
tracks.push(self.track_to_suggested(&item, artist_mbid, similarity));
|
||||
}
|
||||
|
||||
tracks
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Failed to search tracks for {}: {}", search_query, e);
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a Track to a SuggestedTrack
|
||||
fn track_to_suggested(&self, track: &Track, artist_mbid: &str, similarity: f32) -> SuggestedTrack {
|
||||
// Extract album info
|
||||
let (album_title, album_id) = match &track.album {
|
||||
Some(album) => (album.title.clone(), album.id.clone()),
|
||||
None => (String::new(), String::new()),
|
||||
};
|
||||
|
||||
// Extract artist name from track performer
|
||||
let track_artist = track
|
||||
.performer
|
||||
.as_ref()
|
||||
.map(|p| p.name.clone())
|
||||
.unwrap_or_default();
|
||||
|
||||
SuggestedTrack {
|
||||
track_id: track.id,
|
||||
title: track.title.clone(),
|
||||
artist_name: track_artist,
|
||||
artist_mbid: Some(artist_mbid.to_string()),
|
||||
album_title,
|
||||
album_id,
|
||||
duration: track.duration,
|
||||
similarity_score: similarity,
|
||||
reason: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate a human-readable reason for suggestion
|
||||
fn generate_reason(
|
||||
&self,
|
||||
_artist_mbid: &str,
|
||||
artist_name: Option<&str>,
|
||||
similarity: f32,
|
||||
_playlist_artists: &[String],
|
||||
) -> String {
|
||||
let name = artist_name.unwrap_or("Unknown");
|
||||
let score_pct = (similarity * 100.0).round() as u32;
|
||||
|
||||
format!("Similar to your playlist ({score_pct}% match) - {name}")
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract unique artist MBIDs from playlist tracks
|
||||
///
|
||||
/// This is a helper function that should be called from the Tauri command
|
||||
/// to get artist MBIDs from track data.
|
||||
pub fn extract_artist_mbids(
|
||||
tracks: &[(u64, Option<String>)], // (track_id, artist_mbid)
|
||||
) -> Vec<String> {
|
||||
let mut seen = HashSet::new();
|
||||
let mut mbids = Vec::new();
|
||||
|
||||
for (_, mbid) in tracks {
|
||||
if let Some(id) = mbid {
|
||||
if !id.is_empty() && seen.insert(id.clone()) {
|
||||
mbids.push(id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mbids
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_extract_artist_mbids() {
|
||||
let tracks = vec![
|
||||
(1, Some("mbid-1".to_string())),
|
||||
(2, Some("mbid-2".to_string())),
|
||||
(3, Some("mbid-1".to_string())), // Duplicate
|
||||
(4, None), // No MBID
|
||||
(5, Some("".to_string())), // Empty MBID
|
||||
(6, Some("mbid-3".to_string())),
|
||||
];
|
||||
|
||||
let mbids = extract_artist_mbids(&tracks);
|
||||
|
||||
assert_eq!(mbids.len(), 3);
|
||||
assert!(mbids.contains(&"mbid-1".to_string()));
|
||||
assert!(mbids.contains(&"mbid-2".to_string()));
|
||||
assert!(mbids.contains(&"mbid-3".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_suggestion_config_default() {
|
||||
let config = SuggestionConfig::default();
|
||||
|
||||
assert_eq!(config.max_artists, 20);
|
||||
assert_eq!(config.tracks_per_artist, 5);
|
||||
assert_eq!(config.max_pool_size, 100);
|
||||
assert_eq!(config.vector_max_age_days, 7);
|
||||
assert!(config.min_similarity > 0.0);
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ pub mod playback;
|
||||
pub mod playback_context;
|
||||
pub mod playlist;
|
||||
pub mod playlist_import;
|
||||
pub mod playlist_suggestions;
|
||||
pub mod queue;
|
||||
pub mod radio;
|
||||
pub mod search;
|
||||
@@ -39,6 +40,7 @@ pub use playback::*;
|
||||
pub use playback_context::*;
|
||||
pub use playlist::*;
|
||||
pub use playlist_import::*;
|
||||
pub use playlist_suggestions::*;
|
||||
pub use queue::*;
|
||||
pub use radio::*;
|
||||
pub use search::*;
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
//! Tauri commands for vector-based playlist suggestions
|
||||
//!
|
||||
//! These commands expose the playlist suggestions engine to the frontend.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use tauri::State;
|
||||
|
||||
use crate::artist_vectors::{
|
||||
ArtistVectorBuilder, ArtistVectorStoreState, RelationshipWeights, StoreStats,
|
||||
SuggestionConfig, SuggestionResult, SuggestionsEngine,
|
||||
};
|
||||
use crate::musicbrainz::MusicBrainzSharedState;
|
||||
use crate::AppState;
|
||||
|
||||
/// Input for generating suggestions
|
||||
#[derive(Debug, Clone, serde::Deserialize)]
|
||||
pub struct SuggestionsInput {
|
||||
/// Artist MBIDs from the playlist
|
||||
pub artist_mbids: Vec<String>,
|
||||
/// Track IDs already in the playlist (to exclude)
|
||||
pub exclude_track_ids: Vec<u64>,
|
||||
/// Whether to include reason strings (dev mode)
|
||||
#[serde(default)]
|
||||
pub include_reasons: bool,
|
||||
/// Optional custom configuration
|
||||
pub config: Option<SuggestionConfig>,
|
||||
}
|
||||
|
||||
/// Get suggestions for a playlist based on artist similarity vectors
|
||||
#[tauri::command]
|
||||
pub async fn get_playlist_suggestions_v2(
|
||||
input: SuggestionsInput,
|
||||
store_state: State<'_, ArtistVectorStoreState>,
|
||||
mb_state: State<'_, MusicBrainzSharedState>,
|
||||
app_state: State<'_, AppState>,
|
||||
) -> Result<SuggestionResult, String> {
|
||||
// Create the builder (just cloning Arcs, cheap)
|
||||
let builder = Arc::new(ArtistVectorBuilder::new(
|
||||
store_state.store.clone(),
|
||||
mb_state.client.clone(),
|
||||
mb_state.cache.clone(),
|
||||
app_state.client.clone(),
|
||||
RelationshipWeights::default(),
|
||||
));
|
||||
|
||||
// Create the engine with config
|
||||
let config = input.config.unwrap_or_default();
|
||||
let engine = SuggestionsEngine::new(
|
||||
store_state.store.clone(),
|
||||
builder,
|
||||
app_state.client.clone(),
|
||||
config,
|
||||
);
|
||||
|
||||
// Convert exclude list to HashSet
|
||||
let exclude_set: HashSet<u64> = input.exclude_track_ids.into_iter().collect();
|
||||
|
||||
// Generate suggestions
|
||||
engine
|
||||
.generate_suggestions(&input.artist_mbids, &exclude_set, input.include_reasons)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Get store statistics for debugging
|
||||
#[tauri::command]
|
||||
pub async fn get_vector_store_stats(
|
||||
store_state: State<'_, ArtistVectorStoreState>,
|
||||
) -> Result<StoreStats, String> {
|
||||
let store = store_state.store.lock().await;
|
||||
store.get_stats()
|
||||
}
|
||||
|
||||
/// Clean up expired vectors
|
||||
#[tauri::command]
|
||||
pub async fn cleanup_vector_store(
|
||||
max_age_days: Option<i64>,
|
||||
store_state: State<'_, ArtistVectorStoreState>,
|
||||
) -> Result<usize, String> {
|
||||
let max_age_secs = max_age_days.unwrap_or(30) * 24 * 60 * 60;
|
||||
let mut store = store_state.store.lock().await;
|
||||
store.cleanup_expired(max_age_secs)
|
||||
}
|
||||
@@ -202,6 +202,9 @@ pub fn run() {
|
||||
// Initialize MusicBrainz integration state
|
||||
let musicbrainz_state = musicbrainz::MusicBrainzSharedState::new()
|
||||
.expect("Failed to initialize MusicBrainz state");
|
||||
// Initialize artist vector store for playlist suggestions
|
||||
let artist_vectors_state = artist_vectors::ArtistVectorStoreState::new()
|
||||
.expect("Failed to initialize artist vector store");
|
||||
// Initialize ListenBrainz integration state
|
||||
let listenbrainz_state = listenbrainz::ListenBrainzSharedState::new()
|
||||
.expect("Failed to initialize ListenBrainz state");
|
||||
@@ -439,6 +442,7 @@ pub fn run() {
|
||||
.manage(musicbrainz_state)
|
||||
.manage(listenbrainz_state)
|
||||
.manage(remote_metadata_state)
|
||||
.manage(artist_vectors_state)
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
// Auth commands
|
||||
commands::init_client,
|
||||
@@ -528,6 +532,10 @@ pub fn run() {
|
||||
// Playlist import commands
|
||||
commands::playlist_import_preview,
|
||||
commands::playlist_import_execute,
|
||||
// Playlist suggestions commands (v2 vector-based)
|
||||
commands::get_playlist_suggestions_v2,
|
||||
commands::get_vector_store_stats,
|
||||
commands::cleanup_vector_store,
|
||||
// Favorites commands
|
||||
commands::get_favorites,
|
||||
commands::add_favorite,
|
||||
|
||||
Reference in new issue
Block a user