danylokhodus commited on
Commit
a09c97b
·
1 Parent(s): 206a534

feat: add chat history support to AI strategist assist

Browse files
FlowAPI.Application/DTOs/AI/AIDtos.cs CHANGED
@@ -22,10 +22,17 @@ namespace FlowAPI.Application.DTOs.AI
22
  public string Goal { get; set; } = string.Empty;
23
  }
24
 
 
 
 
 
 
 
25
  public class AICanvasAssistRequestDto
26
  {
27
  public Guid GraphId { get; set; }
28
  public string UserPrompt { get; set; } = string.Empty;
 
29
  }
30
 
31
  public class AICanvasAssistResponseDto
 
22
  public string Goal { get; set; } = string.Empty;
23
  }
24
 
25
+ public class AIChatMessageDto
26
+ {
27
+ public string Role { get; set; } = string.Empty; // "user" or "model"
28
+ public string Text { get; set; } = string.Empty;
29
+ }
30
+
31
  public class AICanvasAssistRequestDto
32
  {
33
  public Guid GraphId { get; set; }
34
  public string UserPrompt { get; set; } = string.Empty;
35
+ public List<AIChatMessageDto> History { get; set; } = new();
36
  }
37
 
38
  public class AICanvasAssistResponseDto
FlowAPI.Application/Services/AIService.cs CHANGED
@@ -114,6 +114,13 @@ namespace FlowAPI.Application.Services
114
  }
115
 
116
  private async Task<string> CallGeminiApiAsync(string prompt, string? systemInstruction = null, string? responseSchemaJson = null)
 
 
 
 
 
 
 
117
  {
118
  var apiKey = _config["Gemini:ApiKey"] ?? Environment.GetEnvironmentVariable("GEMINI_API_KEY");
119
  if (string.IsNullOrEmpty(apiKey))
@@ -137,9 +144,6 @@ namespace FlowAPI.Application.Services
137
  }
138
  var url = $"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={apiKey}";
139
 
140
- var parts = new List<object> { new { text = prompt } };
141
- var contents = new[] { new { parts = parts } };
142
-
143
  object? generationConfig = null;
144
  if (responseSchemaJson != null)
145
  {
@@ -444,7 +448,32 @@ namespace FlowAPI.Application.Services
444
  "3. В поле 'explanation' напиши естественный, живой и дружелюбный ответ пользователю. Общайся напрямую как человек-наставник (без фраз вроде 'Пользовательский запрос...', 'В соответствии со своей ролью я не могу...'). Ответь на его вопрос, прокомментируй граф или дружелюбно объясни, какие изменения ты внес.";
445
 
446
  var systemInstruction = "You are a friendly, highly professional Roadmap Strategist AI Agent. You interact with the user, answer their questions, and help them design, extend, or analyze their roadmaps. You converse naturally, warmly, and directly (in the user's language). If changes are needed, you return the new nodes and edges, breaking down complex tasks into hierarchical subtasks. To model sequential subtasks, you connect the parent to the first subtask using relationship 'subgoal', and then chain the subtasks sequentially using relationship 'sequential', and avoiding habits (relationship: habit) unless specifically requested.";
447
- var responseJson = await CallGeminiApiAsync(prompt, systemInstruction, AssistResponseSchema);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
448
  responseJson = CleanJson(responseJson);
449
 
450
  // 3. Deserialize Gemini response
 
114
  }
115
 
116
  private async Task<string> CallGeminiApiAsync(string prompt, string? systemInstruction = null, string? responseSchemaJson = null)
117
+ {
118
+ var parts = new[] { new { text = prompt } };
119
+ var contents = new[] { new { parts = parts } };
120
+ return await CallGeminiApiAsync(contents, systemInstruction, responseSchemaJson);
121
+ }
122
+
123
+ private async Task<string> CallGeminiApiAsync(object[] contents, string? systemInstruction = null, string? responseSchemaJson = null)
124
  {
125
  var apiKey = _config["Gemini:ApiKey"] ?? Environment.GetEnvironmentVariable("GEMINI_API_KEY");
126
  if (string.IsNullOrEmpty(apiKey))
 
144
  }
145
  var url = $"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={apiKey}";
146
 
 
 
 
147
  object? generationConfig = null;
148
  if (responseSchemaJson != null)
149
  {
 
448
  "3. В поле 'explanation' напиши естественный, живой и дружелюбный ответ пользователю. Общайся напрямую как человек-наставник (без фраз вроде 'Пользовательский запрос...', 'В соответствии со своей ролью я не могу...'). Ответь на его вопрос, прокомментируй граф или дружелюбно объясни, какие изменения ты внес.";
449
 
450
  var systemInstruction = "You are a friendly, highly professional Roadmap Strategist AI Agent. You interact with the user, answer their questions, and help them design, extend, or analyze their roadmaps. You converse naturally, warmly, and directly (in the user's language). If changes are needed, you return the new nodes and edges, breaking down complex tasks into hierarchical subtasks. To model sequential subtasks, you connect the parent to the first subtask using relationship 'subgoal', and then chain the subtasks sequentially using relationship 'sequential', and avoiding habits (relationship: habit) unless specifically requested.";
451
+
452
+ var contentsList = new List<object>();
453
+ if (request.History != null)
454
+ {
455
+ foreach (var msg in request.History)
456
+ {
457
+ var role = msg.Role == "user" ? "user" : "model";
458
+ if (!string.IsNullOrWhiteSpace(msg.Text))
459
+ {
460
+ contentsList.Add(new
461
+ {
462
+ role = role,
463
+ parts = new[] { new { text = msg.Text } }
464
+ });
465
+ }
466
+ }
467
+ }
468
+
469
+ // Add the current user prompt (contains graph context + query)
470
+ contentsList.Add(new
471
+ {
472
+ role = "user",
473
+ parts = new[] { new { text = prompt } }
474
+ });
475
+
476
+ var responseJson = await CallGeminiApiAsync(contentsList.ToArray(), systemInstruction, AssistResponseSchema);
477
  responseJson = CleanJson(responseJson);
478
 
479
  // 3. Deserialize Gemini response