| | | 1 | | using Chronicis.Shared.DTOs; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Api.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Service for generating contextual prompts based on user state. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class PromptService : IPromptService |
| | | 9 | | { |
| | | 10 | | private readonly ILogger<PromptService> _logger; |
| | | 11 | | |
| | | 12 | | // Configuration constants |
| | | 13 | | private const int StaleCharacterDays = 14; |
| | | 14 | | private const int SessionFollowUpDays = 7; |
| | | 15 | | private const int MaxPrompts = 3; |
| | | 16 | | |
| | | 17 | | public PromptService(ILogger<PromptService> logger) |
| | | 18 | | { |
| | 17 | 19 | | _logger = logger; |
| | 17 | 20 | | } |
| | | 21 | | |
| | | 22 | | public List<PromptDto> GeneratePrompts(DashboardDto dashboard) |
| | | 23 | | { |
| | 16 | 24 | | var prompts = new List<PromptDto>(); |
| | | 25 | | |
| | | 26 | | // Priority 1: Missing fundamentals |
| | 16 | 27 | | AddMissingFundamentalPrompts(dashboard, prompts); |
| | | 28 | | |
| | | 29 | | // Priority 2: Stale content needing attention |
| | 16 | 30 | | AddStaleContentPrompts(dashboard, prompts); |
| | | 31 | | |
| | | 32 | | // Priority 3: Session follow-up |
| | 16 | 33 | | AddSessionFollowUpPrompts(dashboard, prompts); |
| | | 34 | | |
| | | 35 | | // Priority 4: Engagement suggestions |
| | 16 | 36 | | AddEngagementPrompts(dashboard, prompts); |
| | | 37 | | |
| | | 38 | | // Priority 5: General tips (fallback) |
| | 16 | 39 | | AddGeneralTips(dashboard, prompts); |
| | | 40 | | |
| | | 41 | | // Return top N prompts by priority |
| | 16 | 42 | | return prompts |
| | 16 | 43 | | .OrderBy(p => p.Priority) |
| | 16 | 44 | | .Take(MaxPrompts) |
| | 16 | 45 | | .ToList(); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | private void AddMissingFundamentalPrompts(DashboardDto dashboard, List<PromptDto> prompts) |
| | | 49 | | { |
| | | 50 | | // No worlds at all |
| | 16 | 51 | | if (!dashboard.Worlds.Any()) |
| | | 52 | | { |
| | 1 | 53 | | prompts.Add(new PromptDto |
| | 1 | 54 | | { |
| | 1 | 55 | | Key = "no-worlds", |
| | 1 | 56 | | Title = "Create Your First World", |
| | 1 | 57 | | Message = "Every great campaign starts with a world. Create one to begin organizing your adventures.", |
| | 1 | 58 | | Icon = "🌍", |
| | 1 | 59 | | ActionText = "Create World", |
| | 1 | 60 | | ActionUrl = "/dashboard", // Dashboard has the create button |
| | 1 | 61 | | Priority = 10, |
| | 1 | 62 | | Category = PromptCategory.MissingFundamental |
| | 1 | 63 | | }); |
| | 1 | 64 | | return; // Don't add more prompts if no worlds |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | // Has world but no campaigns |
| | 15 | 68 | | var worldsWithoutCampaigns = dashboard.Worlds.Where(w => !w.Campaigns.Any()).ToList(); |
| | 15 | 69 | | if (worldsWithoutCampaigns.Any()) |
| | | 70 | | { |
| | 2 | 71 | | var world = worldsWithoutCampaigns.First(); |
| | 2 | 72 | | prompts.Add(new PromptDto |
| | 2 | 73 | | { |
| | 2 | 74 | | Key = $"no-campaign-{world.Id}", |
| | 2 | 75 | | Title = "Start a Campaign", |
| | 2 | 76 | | Message = $"Your world \"{world.Name}\" doesn't have any campaigns yet. Create one to track your adventu |
| | 2 | 77 | | Icon = "📜", |
| | 2 | 78 | | ActionText = "View World", |
| | 2 | 79 | | ActionUrl = $"/world/{world.Slug}", |
| | 2 | 80 | | Priority = 20, |
| | 2 | 81 | | Category = PromptCategory.MissingFundamental |
| | 2 | 82 | | }); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | // Has campaigns but no claimed characters |
| | 15 | 86 | | if (!dashboard.ClaimedCharacters.Any() && dashboard.Worlds.Any(w => w.Campaigns.Any())) |
| | | 87 | | { |
| | 3 | 88 | | prompts.Add(new PromptDto |
| | 3 | 89 | | { |
| | 3 | 90 | | Key = "no-characters", |
| | 3 | 91 | | Title = "Claim Your Character", |
| | 3 | 92 | | Message = "You haven't claimed any characters yet. Find your character in the tree and click 'Claim as M |
| | 3 | 93 | | Icon = "👤", |
| | 3 | 94 | | Priority = 30, |
| | 3 | 95 | | Category = PromptCategory.MissingFundamental |
| | 3 | 96 | | }); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | // Has campaign but no sessions |
| | 15 | 100 | | var campaignsWithoutSessions = dashboard.Worlds |
| | 15 | 101 | | .SelectMany(w => w.Campaigns) |
| | 15 | 102 | | .Where(c => c.SessionCount == 0) |
| | 15 | 103 | | .ToList(); |
| | | 104 | | |
| | 15 | 105 | | if (campaignsWithoutSessions.Any()) |
| | | 106 | | { |
| | 2 | 107 | | var campaign = campaignsWithoutSessions.First(); |
| | 2 | 108 | | var world = dashboard.Worlds.First(w => w.Campaigns.Contains(campaign)); |
| | 2 | 109 | | prompts.Add(new PromptDto |
| | 2 | 110 | | { |
| | 2 | 111 | | Key = $"no-sessions-{campaign.Id}", |
| | 2 | 112 | | Title = "Record Your First Session", |
| | 2 | 113 | | Message = $"Campaign \"{campaign.Name}\" has no session notes yet. Start documenting your adventures!", |
| | 2 | 114 | | Icon = "📝", |
| | 2 | 115 | | ActionText = "Add Session", |
| | 2 | 116 | | ActionUrl = $"/world/{world.Slug}", |
| | 2 | 117 | | Priority = 40, |
| | 2 | 118 | | Category = PromptCategory.MissingFundamental |
| | 2 | 119 | | }); |
| | | 120 | | } |
| | 15 | 121 | | } |
| | | 122 | | |
| | | 123 | | private void AddStaleContentPrompts(DashboardDto dashboard, List<PromptDto> prompts) |
| | | 124 | | { |
| | 16 | 125 | | var staleThreshold = DateTime.UtcNow.AddDays(-StaleCharacterDays); |
| | | 126 | | |
| | | 127 | | // Stale characters (not updated in 14+ days) |
| | 16 | 128 | | var staleCharacters = dashboard.ClaimedCharacters |
| | 16 | 129 | | .Where(c => (c.ModifiedAt ?? c.CreatedAt) < staleThreshold) |
| | 16 | 130 | | .OrderBy(c => c.ModifiedAt ?? c.CreatedAt) |
| | 16 | 131 | | .ToList(); |
| | | 132 | | |
| | 16 | 133 | | if (staleCharacters.Any()) |
| | | 134 | | { |
| | 2 | 135 | | var stalest = staleCharacters.First(); |
| | 2 | 136 | | var daysSinceUpdate = (int)(DateTime.UtcNow - (stalest.ModifiedAt ?? stalest.CreatedAt)).TotalDays; |
| | | 137 | | |
| | 2 | 138 | | prompts.Add(new PromptDto |
| | 2 | 139 | | { |
| | 2 | 140 | | Key = $"stale-character-{stalest.Id}", |
| | 2 | 141 | | Title = "Update Your Character", |
| | 2 | 142 | | Message = $"\"{stalest.Title}\" hasn't been updated in {daysSinceUpdate} days. How have they grown since |
| | 2 | 143 | | Icon = "✨", |
| | 2 | 144 | | ActionText = "View Character", |
| | 2 | 145 | | ActionUrl = $"/article/{stalest.Id}", |
| | 2 | 146 | | Priority = 50, |
| | 2 | 147 | | Category = PromptCategory.NeedsAttention |
| | 2 | 148 | | }); |
| | | 149 | | } |
| | 16 | 150 | | } |
| | | 151 | | |
| | | 152 | | private void AddSessionFollowUpPrompts(DashboardDto dashboard, List<PromptDto> prompts) |
| | | 153 | | { |
| | 16 | 154 | | var followUpThreshold = DateTime.UtcNow.AddDays(-SessionFollowUpDays); |
| | | 155 | | |
| | | 156 | | // Find campaigns with recent sessions that might need follow-up notes |
| | 62 | 157 | | foreach (var world in dashboard.Worlds) |
| | | 158 | | { |
| | 39 | 159 | | foreach (var campaign in world.Campaigns.Where(c => c.IsActive)) |
| | | 160 | | { |
| | 5 | 161 | | var latestSessionDate = campaign.CurrentArc?.LatestSessionDate; |
| | 5 | 162 | | if (latestSessionDate.HasValue) |
| | | 163 | | { |
| | 3 | 164 | | var lastSession = latestSessionDate.Value; |
| | 3 | 165 | | var daysSinceSession = (int)(DateTime.UtcNow - lastSession).TotalDays; |
| | | 166 | | |
| | | 167 | | // Session was 2-7 days ago - good time to add notes while fresh |
| | 3 | 168 | | if (daysSinceSession >= 2 && daysSinceSession <= SessionFollowUpDays) |
| | | 169 | | { |
| | 1 | 170 | | prompts.Add(new PromptDto |
| | 1 | 171 | | { |
| | 1 | 172 | | Key = $"session-followup-{campaign.Id}", |
| | 1 | 173 | | Title = "Add Session Notes", |
| | 1 | 174 | | Message = $"Your last session in \"{campaign.Name}\" was {daysSinceSession} days ago. Add no |
| | 1 | 175 | | Icon = "📖", |
| | 1 | 176 | | ActionText = "View Campaign", |
| | 1 | 177 | | ActionUrl = $"/world/{world.Slug}", |
| | 1 | 178 | | Priority = 60, |
| | 1 | 179 | | Category = PromptCategory.NeedsAttention |
| | 1 | 180 | | }); |
| | 1 | 181 | | break; // Only one session follow-up prompt |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | } |
| | 16 | 186 | | } |
| | | 187 | | |
| | | 188 | | private void AddEngagementPrompts(DashboardDto dashboard, List<PromptDto> prompts) |
| | | 189 | | { |
| | | 190 | | // Only add engagement prompts if user has some content |
| | 16 | 191 | | if (!dashboard.Worlds.Any()) |
| | 1 | 192 | | return; |
| | | 193 | | |
| | 15 | 194 | | var totalArticles = dashboard.Worlds.Sum(w => w.ArticleCount); |
| | | 195 | | |
| | | 196 | | // Suggest wiki links if they have multiple articles |
| | 15 | 197 | | if (totalArticles >= 5) |
| | | 198 | | { |
| | 2 | 199 | | prompts.Add(new PromptDto |
| | 2 | 200 | | { |
| | 2 | 201 | | Key = "try-wiki-links", |
| | 2 | 202 | | Title = "Connect Your Content", |
| | 2 | 203 | | Message = "Use [[Article Name]] in your notes to create links between articles. It's a great way to buil |
| | 2 | 204 | | Icon = "🔗", |
| | 2 | 205 | | Priority = 70, |
| | 2 | 206 | | Category = PromptCategory.Suggestion |
| | 2 | 207 | | }); |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | // Suggest AI summaries if they have backlinks |
| | 15 | 211 | | if (totalArticles >= 10) |
| | | 212 | | { |
| | 1 | 213 | | prompts.Add(new PromptDto |
| | 1 | 214 | | { |
| | 1 | 215 | | Key = "try-ai-summaries", |
| | 1 | 216 | | Title = "Try AI Summaries", |
| | 1 | 217 | | Message = "Articles with backlinks can generate AI summaries. Open an article and click 'Generate Summar |
| | 1 | 218 | | Icon = "🤖", |
| | 1 | 219 | | Priority = 75, |
| | 1 | 220 | | Category = PromptCategory.Suggestion |
| | 1 | 221 | | }); |
| | | 222 | | } |
| | 15 | 223 | | } |
| | | 224 | | |
| | | 225 | | private void AddGeneralTips(DashboardDto dashboard, List<PromptDto> prompts) |
| | | 226 | | { |
| | | 227 | | // Only add tips if we don't have enough higher-priority prompts |
| | 17 | 228 | | if (prompts.Count >= MaxPrompts) |
| | 1 | 229 | | return; |
| | | 230 | | |
| | 16 | 231 | | var tips = new List<PromptDto> |
| | 16 | 232 | | { |
| | 16 | 233 | | new() |
| | 16 | 234 | | { |
| | 16 | 235 | | Key = "tip-keyboard-shortcuts", |
| | 16 | 236 | | Title = "Keyboard Shortcuts", |
| | 16 | 237 | | Message = "Press Ctrl+S to save, Ctrl+N to create a new article. Your work auto-saves as you type!", |
| | 16 | 238 | | Icon = "⌨️", |
| | 16 | 239 | | Priority = 100, |
| | 16 | 240 | | Category = PromptCategory.Tip |
| | 16 | 241 | | }, |
| | 16 | 242 | | new() |
| | 16 | 243 | | { |
| | 16 | 244 | | Key = "tip-tree-search", |
| | 16 | 245 | | Title = "Quick Navigation", |
| | 16 | 246 | | Message = "Use the search box in the sidebar to quickly filter articles by title.", |
| | 16 | 247 | | Icon = "🔍", |
| | 16 | 248 | | Priority = 101, |
| | 16 | 249 | | Category = PromptCategory.Tip |
| | 16 | 250 | | }, |
| | 16 | 251 | | new() |
| | 16 | 252 | | { |
| | 16 | 253 | | Key = "tip-drag-drop", |
| | 16 | 254 | | Title = "Organize with Drag & Drop", |
| | 16 | 255 | | Message = "Drag articles in the tree to reorganize your content hierarchy.", |
| | 16 | 256 | | Icon = "📂", |
| | 16 | 257 | | Priority = 102, |
| | 16 | 258 | | Category = PromptCategory.Tip |
| | 16 | 259 | | }, |
| | 16 | 260 | | new() |
| | 16 | 261 | | { |
| | 16 | 262 | | Key = "tip-backlinks", |
| | 16 | 263 | | Title = "Discover Connections", |
| | 16 | 264 | | Message = "Click the metadata button on any article to see what other articles link to it.", |
| | 16 | 265 | | Icon = "🔙", |
| | 16 | 266 | | Priority = 103, |
| | 16 | 267 | | Category = PromptCategory.Tip |
| | 16 | 268 | | } |
| | 16 | 269 | | }; |
| | | 270 | | |
| | | 271 | | // Add a random tip if we need more prompts |
| | 16 | 272 | | var random = new Random(); |
| | 16 | 273 | | var shuffledTips = tips.OrderBy(_ => random.Next()).ToList(); |
| | | 274 | | |
| | 160 | 275 | | foreach (var tip in shuffledTips) |
| | | 276 | | { |
| | 64 | 277 | | if (prompts.Count < MaxPrompts) |
| | | 278 | | { |
| | 34 | 279 | | prompts.Add(tip); |
| | | 280 | | } |
| | | 281 | | } |
| | 16 | 282 | | } |
| | | 283 | | } |