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