Spaces:
Sleeping
Sleeping
Commit ·
ab6ad9a
1
Parent(s): e4a761a
Refine strategist prompts for conversational tone, adaptive sizes, and correct chronological chaining
Browse files
FlowAPI.Application/Services/AIService.cs
CHANGED
|
@@ -188,6 +188,7 @@ namespace FlowAPI.Application.Services
|
|
| 188 |
}
|
| 189 |
|
| 190 |
using var httpClient = new HttpClient();
|
|
|
|
| 191 |
var jsonPayload = JsonSerializer.Serialize(requestBody);
|
| 192 |
|
| 193 |
HttpResponseMessage? response = null;
|
|
@@ -240,6 +241,7 @@ namespace FlowAPI.Application.Services
|
|
| 240 |
{
|
| 241 |
throw new Exception("Graph not found.");
|
| 242 |
}
|
|
|
|
| 243 |
|
| 244 |
var failedNode = graph.Nodes.FirstOrDefault(n => n.Id == request.FailedNodeId);
|
| 245 |
if (failedNode == null)
|
|
@@ -304,6 +306,8 @@ namespace FlowAPI.Application.Services
|
|
| 304 |
|
| 305 |
public async Task<FullGraphResponseDto> GenerateGoalGraphAsync(GenerateGoalGraphRequestDto request)
|
| 306 |
{
|
|
|
|
|
|
|
| 307 |
if (string.IsNullOrWhiteSpace(request.Goal))
|
| 308 |
{
|
| 309 |
throw new ArgumentException("Goal description cannot be empty.");
|
|
@@ -312,29 +316,29 @@ namespace FlowAPI.Application.Services
|
|
| 312 |
// 1. Build prompt for Gemini
|
| 313 |
var prompt = $"������������ ����� ������� ��������� ����: \"{request.Goal}\". " +
|
| 314 |
"������� ��������� ������������� �������� �����.\n" +
|
| 315 |
-
"������� ���������� ������ � ��������� (������������� � �����������):\n" +
|
| 316 |
"- � ��� ���� ��� ���� ������:\n" +
|
| 317 |
-
" 1. 'relationship': 'sequential' � ���������������� ���� / ����. ������������ ��� �������� ��������� � ��������� ����� ������� ������ ������ ����������
|
| 318 |
" 2. 'relationship': 'subgoal' � ������������ � �������. ������������ ��� ���������� ������� ���� �� ������ ������ �����.\n" +
|
| 319 |
-
"- ������� ����������� ������������
|
| 320 |
-
" * ����� ����
|
| 321 |
" * �� ������������� ����� ������� ��������. ������� �������� ������ (��������: ������� ���� -> ������� A -> ������� A.1 -> ������� A.1.1) � ��� ��������� � ��������������.\n" +
|
| 322 |
-
"
|
| 323 |
-
"
|
| 324 |
-
" * ������������
|
| 325 |
-
" * �����������
|
| 326 |
-
"
|
| 327 |
-
"
|
| 328 |
-
"
|
| 329 |
"- ������� ��� ����� ����� (����������):\n" +
|
| 330 |
" * 'sketch' � ����������� ���/���� (��������� � 99% �������). ����� ������� �������� ���, ���� ���� �� �������� ��������, ����� �������� (��������, '�������� 30 �����', '��������� 1 ���') ��� �������� ��������, ������ ����� ��� 'sketch'!\n" +
|
| 331 |
" * 'habit' (��������) � ��������� ������ ������ ���� ������������ ����� ������ ���������� ����������, ���������/����������� ������������� �������� ��� ������������ �������� (��������, '������ ���� ���� ����'). ������� �� ��������� ��� ��� ������� ����� ��������!\n" +
|
| 332 |
" * 'condition' (�������) � ��������� ������ ������ ��� ����� ���������� ���������, �������� � ������ (��������, '���� X, �� Y, ����� Z'). ������� �� ��������� ��� ��� ������� ����� � ���������� ����������, ���� ��� ��������� ����!\n" +
|
| 333 |
"������� ���� ��� �������� �������� �������� (label) � ��������� �������� (description).\n" +
|
| 334 |
-
"� ���� 'explanation' ������ �����, ����������� � ������������� ����� �� ���� ����������� ������-��������, ������� ������ ������������ ����� � ��� ������ ������.";
|
| 335 |
|
| 336 |
// 2. Call Gemini API
|
| 337 |
-
var systemInstruction = "You are a friendly, highly professional Roadmap Strategist AI Agent. You design
|
| 338 |
var responseJson = await CallGeminiApiAsync(prompt, systemInstruction, GoalResponseSchema);
|
| 339 |
responseJson = CleanJson(responseJson);
|
| 340 |
|
|
@@ -430,6 +434,7 @@ namespace FlowAPI.Application.Services
|
|
| 430 |
{
|
| 431 |
throw new Exception("Graph not found.");
|
| 432 |
}
|
|
|
|
| 433 |
|
| 434 |
// 1. Serialize existing nodes and edges for Gemini context
|
| 435 |
var existingNodesText = new StringBuilder();
|
|
@@ -454,29 +459,27 @@ namespace FlowAPI.Application.Services
|
|
| 454 |
"���� ������:\n" +
|
| 455 |
"1. ���� ������ ������������ ������� ���������� ����� ���������, ����������� ��� ���������� �����: ���������� ����� ���� (NewNodes) � ����� ����� (NewEdges). ��� ���� ������ ������ �������� ���������� ������:\n" +
|
| 456 |
" - � ��� ���� ��� ���� ������:\n" +
|
| 457 |
-
" * 'relationship': 'sequential' � ���������������� ���� / ����. ������������ ��� �������� ��������� � ��������� ����� ������� ������ ������ ����������
|
| 458 |
" * 'relationship': 'subgoal' � ������������ � �������. ������������ ��� ���������� ������� ���� �� ������ ������ �����.\n" +
|
| 459 |
-
" - ������� ����������� ������������
|
| 460 |
" * ����� ���� �� ����� ������ ����� ���� ���������������! ���� ������� ���� �� ���� �������, �� ������ ������� � �� � ����������� ������� (subgoals) � ���������������� ����.\n" +
|
| 461 |
" * �� ������������� ����� ������� ��������. ������� �������� ������ (��������: ������� ���� -> ������� A -> ������� A.1 -> ������� A.1.1) � ��� ��������� � ��������������.\n" +
|
| 462 |
-
" - ��� ��������� ������� ������������ � �������:\n" +
|
| 463 |
-
" * ������������ �������
|
| 464 |
-
" * ����������������
|
| 465 |
-
" * ��������� ���������: �� ������ �������� ������������� ��� ������� �� ����� ������� �����������.\n" +
|
| 466 |
-
" - ���� ������������ ������ �������� ������� (������������) ��� ����� ��� ������������ ����:\n" +
|
| 467 |
-
" * ���� ��������������� ������������ ���� ��� �� �����, ������� ������ ������������ ���� (��������, '������������' / 'Configuration') � ����� ��� ��������������� � ���������� ������ ����� 'relationship': 'sequential'.\n" +
|
| 468 |
-
" * ����� ������������ ���� � ��������� (��������� ������� ����������� ��� ������������ ����� ��� ���������������� ������� ��� ��������� �����, ��� ������� ����).\n" +
|
| 469 |
" * �� ��������� ������������ �������� ��� ���������������� �����. ������� �� ����������������� ������ / ������. ������� ��������� / ����������� ������ �������� ������������.\n" +
|
| 470 |
-
" - ����������
|
|
|
|
|
|
|
| 471 |
" - ������� ��� ����� ����� (����������):\n" +
|
| 472 |
" * 'sketch' � ����������� ���/���� (��������� � 99% �������). ����� ������� �������� ���, ���� ���� �� �������� ��������, ����� �������� (��������, '�������� 30 �����', '��������� 1 ���') ��� �������� ��������, ������ ����� ��� 'sketch'!\n" +
|
| 473 |
" * 'habit' (��������) � ��������� ������ ������ ���� ������������ ����� ������ ���������� ����������, ���������/����������� ������������� �������� ��� ������������ �������� (��������, '������ ���� ���� ����'). ������� �� ��������� ��� ��� ������� ����� ��������!\n" +
|
| 474 |
" * 'condition' (�������) � ��������� ������ ������ ��� ����� ���������� ���������, �������� � ������ (��������, '���� X, �� Y, ����� Z'). ������� �� ��������� ��� ��� ������� ����� � ���������� ����������, ���� ��� ��������� ����!\n" +
|
| 475 |
" - �������� ����� ���� � ������������� ��� ���� � ������ � ������� ���������� ��������� ID (��������, new_step_1).\n" +
|
| 476 |
"2. ���� ������ ������������ �� ������� ���������� ���������: ������ ������ 'NewNodes' � 'NewEdges' ������� ( [] ).\n" +
|
| 477 |
-
"3. � ���� 'explanation' ������ ������������, ����� � ����������� ����� �� ���� ��������-����������, ������� ��������� ��������� ��� ������� �� �������.";
|
| 478 |
|
| 479 |
-
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. If changes are needed, you return the new nodes and edges, breaking down complex tasks into hierarchical, deeply nested subgoals (recursive subgoals, where subgoals can have their own subgoals). You
|
| 480 |
var contentsList = new List<object>();
|
| 481 |
if (request.History != null)
|
| 482 |
{
|
|
@@ -904,5 +907,34 @@ namespace FlowAPI.Application.Services
|
|
| 904 |
|
| 905 |
return textResponse;
|
| 906 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 907 |
}
|
| 908 |
}
|
|
|
|
| 188 |
}
|
| 189 |
|
| 190 |
using var httpClient = new HttpClient();
|
| 191 |
+
httpClient.Timeout = TimeSpan.FromSeconds(5);
|
| 192 |
var jsonPayload = JsonSerializer.Serialize(requestBody);
|
| 193 |
|
| 194 |
HttpResponseMessage? response = null;
|
|
|
|
| 241 |
{
|
| 242 |
throw new Exception("Graph not found.");
|
| 243 |
}
|
| 244 |
+
await CheckAndIncrementAiGenerationsLimitAsync(graph.UserId);
|
| 245 |
|
| 246 |
var failedNode = graph.Nodes.FirstOrDefault(n => n.Id == request.FailedNodeId);
|
| 247 |
if (failedNode == null)
|
|
|
|
| 306 |
|
| 307 |
public async Task<FullGraphResponseDto> GenerateGoalGraphAsync(GenerateGoalGraphRequestDto request)
|
| 308 |
{
|
| 309 |
+
await CheckAndIncrementAiGenerationsLimitAsync(request.UserId);
|
| 310 |
+
|
| 311 |
if (string.IsNullOrWhiteSpace(request.Goal))
|
| 312 |
{
|
| 313 |
throw new ArgumentException("Goal description cannot be empty.");
|
|
|
|
| 316 |
// 1. Build prompt for Gemini
|
| 317 |
var prompt = $"������������ ����� ������� ��������� ����: \"{request.Goal}\". " +
|
| 318 |
"������� ��������� ������������� �������� �����.\n" +
|
| 319 |
+
"������� ���������� ������ � ��������� (�������������, ����������� � ������������������):\n" +
|
| 320 |
"- � ��� ���� ��� ���� ������:\n" +
|
| 321 |
+
" 1. 'relationship': 'sequential' � ���������������� ���� / ����. ������������ ��� �������� ���������, ��������� ����� ������� ������ ������ ����������, � ����� ��� ���������� ����� ������ ������� ������������ ���� � ������. ��������� ���� ��� ��� �����, ������� ������ �������������� ������� ���� �� ����� (��������: ����������� ���� -> �������� ����� -> ������ ����).\n" +
|
| 322 |
" 2. 'relationship': 'subgoal' � ������������ � �������. ������������ ��� ���������� ������� ���� �� ������ ������ �����.\n" +
|
| 323 |
+
"- ������� ����������� ������������:\n" +
|
| 324 |
+
" * ����� ������� ���� �� ����� ������ ����� ���� ���������������. ���� ������� �������, ������ � �� � ����������� ������� (subgoals) � ���������������� ����.\n" +
|
| 325 |
" * �� ������������� ����� ������� ��������. ������� �������� ������ (��������: ������� ���� -> ������� A -> ������� A.1 -> ������� A.1.1) � ��� ��������� � ��������������.\n" +
|
| 326 |
+
"- ��� ��������� ������� ������������ � ��������-������������ ����� (����������):\n" +
|
| 327 |
+
" * �� ����� ��� ��������� �������������! ���� ���� ������ ������� ������ ����������� ������ ���� �� ������ (���� ��������������� �����������), �� ������ ������� ������������ ���� ������ 'subgoal' ������ � ����� ������ ����� �������. ��� ����������� ���� ���� ������� �������� ��������������� ���� � ������ ����� 'sequential'.\n" +
|
| 328 |
+
" * ��������� ��������� ����� �������� � �������� ����� 'subgoal' (�����������) ������ ���� ��� ������ ���������� ���� �� ����� � �� ����� ������ ������������ ��� � ����� ������� (��������: ���� ingredients � ���������� ������).\n" +
|
| 329 |
+
" * �� ��������� ������������ �������� ��� ���������������� �����. ������� �� ����������������� ������ / ������. ������� ��������� / ����������� ������ �������� ������������.\n" +
|
| 330 |
+
"- ���������� ������ �������� �����:\n" +
|
| 331 |
+
" * ���� ���� ������� (��������, �������� ���, ������ ������), ������� ���������� ����� �� 3-5 �����.\n" +
|
| 332 |
+
" * ���� ���� ������� (������� �����, �������� ����������������), ���������� ����������� ����� �� 8-15+ �����, ������������ ��� ������ ���� � �� ���������.\n" +
|
| 333 |
"- ������� ��� ����� ����� (����������):\n" +
|
| 334 |
" * 'sketch' � ����������� ���/���� (��������� � 99% �������). ����� ������� �������� ���, ���� ���� �� �������� ��������, ����� �������� (��������, '�������� 30 �����', '��������� 1 ���') ��� �������� ��������, ������ ����� ��� 'sketch'!\n" +
|
| 335 |
" * 'habit' (��������) � ��������� ������ ������ ���� ������������ ����� ������ ���������� ����������, ���������/����������� ������������� �������� ��� ������������ �������� (��������, '������ ���� ���� ����'). ������� �� ��������� ��� ��� ������� ����� ��������!\n" +
|
| 336 |
" * 'condition' (�������) � ��������� ������ ������ ��� ����� ���������� ���������, �������� � ������ (��������, '���� X, �� Y, ����� Z'). ������� �� ��������� ��� ��� ������� ����� � ���������� ����������, ���� ��� ��������� ����!\n" +
|
| 337 |
"������� ���� ��� �������� �������� �������� (label) � ��������� �������� (description).\n" +
|
| 338 |
+
"� ���� 'explanation' ������ �����, ������, ����������� � ������������� ����� �� ���� ����������� ������-��������. ������� � ������������� �� ������� ������������ �����, ��� ���������� ���������. ������� ����� ������ ������������ �����, ������ ������������ ����� ����������� �����������, � ������ ���������������, � ��� ������ ������������ ������ �� ���������� ����.";
|
| 339 |
|
| 340 |
// 2. Call Gemini API
|
| 341 |
+
var systemInstruction = "You are a warm, friendly, and highly professional Roadmap Strategist AI Agent. You design roadmaps represented as graphs (nodes and edges). You break down complex goals into hierarchical, deeply nested subgoals (recursive subgoals, where subgoals can have their own subgoals). You connect a parent to multiple parallel subgoals using 'subgoal' relationship ONLY if they can be done in parallel or in any order. If subgoals have chronological dependencies, connect the parent to the first subgoal in the sequence using 'subgoal' and chain the remaining subgoals using 'sequential' relationship. Node types MUST follow these rules: 'sketch' for 99% of regular tasks (including tasks with durations/wait times); 'habit' ONLY for repeating daily/weekly actions meant to form a routine, if explicitly requested; 'condition' ONLY for logical forks/branches with conditions. You adapt the graph size: 3-5 nodes for simple goals, 8-15+ nodes for complex goals. In the 'explanation' field, you write in a warm, direct, and conversational human tone, explaining the plan's logic and giving practical advice.";
|
| 342 |
var responseJson = await CallGeminiApiAsync(prompt, systemInstruction, GoalResponseSchema);
|
| 343 |
responseJson = CleanJson(responseJson);
|
| 344 |
|
|
|
|
| 434 |
{
|
| 435 |
throw new Exception("Graph not found.");
|
| 436 |
}
|
| 437 |
+
await CheckAndIncrementAiGenerationsLimitAsync(graph.UserId);
|
| 438 |
|
| 439 |
// 1. Serialize existing nodes and edges for Gemini context
|
| 440 |
var existingNodesText = new StringBuilder();
|
|
|
|
| 459 |
"���� ������:\n" +
|
| 460 |
"1. ���� ������ ������������ ������� ���������� ����� ���������, ����������� ��� ���������� �����: ���������� ����� ���� (NewNodes) � ����� ����� (NewEdges). ��� ���� ������ ������ �������� ���������� ������:\n" +
|
| 461 |
" - � ��� ���� ��� ���� ������:\n" +
|
| 462 |
+
" * 'relationship': 'sequential' � ���������������� ���� / ����. ������������ ��� �������� ���������, ��������� ����� ������� ������ ������ ����������, � ����� ��� ���������� ����� ������ ������� ������������ ���� � ������. ��������� ���� ��� ��� �����, ������� ������ �������������� ������� ���� �� ����� (��������: ����������� ���� -> �������� ����� -> ������ ����).\n" +
|
| 463 |
" * 'relationship': 'subgoal' � ������������ � �������. ������������ ��� ���������� ������� ���� �� ������ ������ �����.\n" +
|
| 464 |
+
" - ������� ����������� ������������:\n" +
|
| 465 |
" * ����� ���� �� ����� ������ ����� ���� ���������������! ���� ������� ���� �� ���� �������, �� ������ ������� � �� � ����������� ������� (subgoals) � ���������������� ����.\n" +
|
| 466 |
" * �� ������������� ����� ������� ��������. ������� �������� ������ (��������: ������� ���� -> ������� A -> ������� A.1 -> ������� A.1.1) � ��� ��������� � ��������������.\n" +
|
| 467 |
+
" - ��� ��������� ������� ������������ � ��������-������������ ����� (����������):\n" +
|
| 468 |
+
" * �� ����� ��� ��������� �������������! ���� ���� ������ ������� ������ ����������� ������ ���� �� ������ (���� ��������������� �����������), �� ������ ������� ������������ ���� ������ 'subgoal' ������ � ����� ������ ����� �������. ��� ����������� ���� ���� ������� �������� ��������������� ���� � ������ ����� 'sequential'.\n" +
|
| 469 |
+
" * ��������� ��������� ����� �������� � �������� ����� 'subgoal' (�����������) ������ ���� ��� ������ ���������� ���� �� ����� � �� ����� ������ ������������ ��� � ����� �������.\n" +
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
" * �� ��������� ������������ �������� ��� ���������������� �����. ������� �� ����������������� ������ / ������. ������� ��������� / ����������� ������ �������� ������������.\n" +
|
| 471 |
+
" - ���������� ������ ����������:\n" +
|
| 472 |
+
" * ���� ������������ ������ �������� ������� �������� ��� ������ ���������, ������ 1-3 ����.\n" +
|
| 473 |
+
" * ���� ������������ ������ ��������� ��� ��������� ������� ����, ������ �����������, ������� � ��������� ���� �� 5-12 �����.\n" +
|
| 474 |
" - ������� ��� ����� ����� (����������):\n" +
|
| 475 |
" * 'sketch' � ����������� ���/���� (��������� � 99% �������). ����� ������� �������� ���, ���� ���� �� �������� ��������, ����� �������� (��������, '�������� 30 �����', '��������� 1 ���') ��� �������� ��������, ������ ����� ��� 'sketch'!\n" +
|
| 476 |
" * 'habit' (��������) � ��������� ������ ������ ���� ������������ ����� ������ ���������� ����������, ���������/����������� ������������� �������� ��� ������������ �������� (��������, '������ ���� ���� ����'). ������� �� ��������� ��� ��� ������� ����� ��������!\n" +
|
| 477 |
" * 'condition' (�������) � ��������� ������ ������ ��� ����� ���������� ���������, �������� � ������ (��������, '���� X, �� Y, ����� Z'). ������� �� ��������� ��� ��� ������� ����� � ���������� ����������, ���� ��� ��������� ����!\n" +
|
| 478 |
" - �������� ����� ���� � ������������� ��� ���� � ������ � ������� ���������� ��������� ID (��������, new_step_1).\n" +
|
| 479 |
"2. ���� ������ ������������ �� ������� ���������� ���������: ������ ������ 'NewNodes' � 'NewEdges' ������� ( [] ).\n" +
|
| 480 |
+
"3. � ���� 'explanation' ������ ������������, ������, ����� � ����������� ����� �� ���� ��������-����������. ������ �� ������� ������������, ����� ������ ��������� ��������� ��� ������� ������ ������������ ����� �� ������� ������������ �����.";
|
| 481 |
|
| 482 |
+
var systemInstruction = "You are a warm, friendly, and highly professional Roadmap Strategist AI Agent. You interact with the user, answer their questions, and help them design, extend, or analyze their roadmaps. If changes are needed, you return the new nodes and edges, breaking down complex tasks into hierarchical, deeply nested subgoals (recursive subgoals, where subgoals can have their own subgoals). You connect a parent to multiple parallel subgoals using 'subgoal' relationship ONLY if they can be done in parallel or in any order. If subgoals have chronological dependencies, connect the parent to the first subgoal in the sequence using 'subgoal' and chain the remaining subgoals using 'sequential' relationship. Node types MUST follow these rules: 'sketch' for 99% of regular tasks (including tasks with durations/wait times); 'habit' ONLY for repeating daily/weekly actions meant to form a routine, if explicitly requested; 'condition' ONLY for logical forks/branches with conditions. You never connect subgoals directly to unrelated nodes without a parent node. In the 'explanation' field, you write in a warm, direct, and conversational human tone, explaining your changes or answering user questions.";
|
| 483 |
var contentsList = new List<object>();
|
| 484 |
if (request.History != null)
|
| 485 |
{
|
|
|
|
| 907 |
|
| 908 |
return textResponse;
|
| 909 |
}
|
| 910 |
+
|
| 911 |
+
private async Task CheckAndIncrementAiGenerationsLimitAsync(Guid userId)
|
| 912 |
+
{
|
| 913 |
+
var user = await _userRepo.GetByIdAsync(userId);
|
| 914 |
+
if (user == null) throw new Exception("User not found.");
|
| 915 |
+
|
| 916 |
+
var today = DateTime.UtcNow.Date;
|
| 917 |
+
if (user.LastAiGenerationDate.Date != today)
|
| 918 |
+
{
|
| 919 |
+
user.DailyAiGenerationsCount = 0;
|
| 920 |
+
user.LastAiGenerationDate = DateTime.UtcNow;
|
| 921 |
+
}
|
| 922 |
+
|
| 923 |
+
int limit = user.SubscriptionTier switch
|
| 924 |
+
{
|
| 925 |
+
"Free" => 3,
|
| 926 |
+
"Pro" => 200,
|
| 927 |
+
"Ultra" => int.MaxValue,
|
| 928 |
+
_ => 3
|
| 929 |
+
};
|
| 930 |
+
|
| 931 |
+
if (user.DailyAiGenerationsCount >= limit)
|
| 932 |
+
{
|
| 933 |
+
throw new InvalidOperationException($"Daily AI generation limit reached for your tier ({limit} generations per day).");
|
| 934 |
+
}
|
| 935 |
+
|
| 936 |
+
user.DailyAiGenerationsCount++;
|
| 937 |
+
await _userRepo.UpdateAsync(user);
|
| 938 |
+
}
|
| 939 |
}
|
| 940 |
}
|