Skip to content

Commit b9738ed

Browse files
committed
feat: ChatService 리팩토링
1 parent 79297ae commit b9738ed

21 files changed

+204
-309
lines changed

ProjectVG.Api/Controllers/ChatController.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,9 @@ public ChatController(IChatService chatService, IServiceScopeFactory scopeFactor
2727
[HttpPost]
2828
public async Task<IActionResult> ProcessChat([FromBody] ChatRequest request)
2929
{
30-
// 요청 데이터 로깅
31-
_logger.LogInformation("채팅 요청 데이터: SessionId={SessionId}, UserId={UserId}, CharacterId={CharacterId}, Message={Message}",
32-
request.SessionId, request.UserId, request.CharacterId, request.Message);
33-
3430
var command = request.ToProcessChatCommand();
35-
3631
var requestResponse = await _chatService.EnqueueChatRequestAsync(command);
3732

38-
if (!requestResponse.IsAccepted)
39-
{
40-
_logger.LogWarning("채팅 요청 거부: {Message}", requestResponse.Message);
41-
return BadRequest(new {
42-
success = false,
43-
status = requestResponse.Status,
44-
message = requestResponse.Message,
45-
errorCode = requestResponse.ErrorCode,
46-
requestedAt = requestResponse.RequestedAt
47-
});
48-
}
49-
5033
return Ok(new {
5134
success = true,
5235
status = requestResponse.Status,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
global using Microsoft.Extensions.Logging;
2+
13
global using ProjectVG.Common.Constants;
24
global using ProjectVG.Common.Exceptions;
3-
global using ProjectVG.Common.Extensions;
4-
global using ProjectVG.Common.Models;

ProjectVG.Application/Models/Chat/ChatPreprocessContext.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Microsoft.Extensions.Logging;
22
using ProjectVG.Application.Models.Character;
3+
using ProjectVG.Domain.Entities.ConversationHistorys;
4+
using ProjectVG.Domain.Enums;
35

46
namespace ProjectVG.Application.Models.Chat
57
{
@@ -12,7 +14,7 @@ public class ChatPreprocessContext
1214

1315
public string? Action { get; set; }
1416
public List<string> MemoryContext { get; set; } = new();
15-
public List<string> ConversationHistory { get; set; } = new();
17+
public IEnumerable<ConversationHistory> ConversationHistory { get; set; } = new List<ConversationHistory>();
1618

1719
public string MemoryStore { get; set; } = string.Empty;
1820
public string VoiceName { get; set; } = string.Empty;
@@ -21,7 +23,7 @@ public class ChatPreprocessContext
2123
public ChatPreprocessContext(
2224
ProcessChatCommand command,
2325
List<string> memoryContext,
24-
List<string> conversationHistory)
26+
IEnumerable<ConversationHistory> conversationHistory)
2527
{
2628
SessionId = command.SessionId;
2729
UserId = command.UserId;
@@ -30,16 +32,26 @@ public ChatPreprocessContext(
3032
MemoryStore = command.UserId.ToString();
3133
Action = command.Action;
3234
MemoryContext = memoryContext ?? new List<string>();
33-
ConversationHistory = conversationHistory ?? new List<string>();
35+
ConversationHistory = conversationHistory ?? new List<ConversationHistory>();
3436

3537
// Character는 반드시 존재한다고 가정
3638
Character = command.Character!;
3739
VoiceName = command.Character!.VoiceId;
3840
}
3941

42+
public List<string> ParseConversationHistory()
43+
{
44+
return ConversationHistory?.Select(c => $"{c.Role}: {c.Content}").ToList() ?? new List<string>();
45+
}
46+
47+
public List<string> ParseConversationHistory(int takeCount)
48+
{
49+
return ConversationHistory?.Take(takeCount).Select(c => $"{c.Role}: {c.Content}").ToList() ?? new List<string>();
50+
}
51+
4052
public override string ToString()
4153
{
42-
return $"ChatPreprocessContext(SessionId={SessionId}, UserId={UserId}, CharacterId={CharacterId}, UserMessage='{UserMessage}', MemoryContext.Count={MemoryContext.Count}, ConversationHistorys.Count={ConversationHistory.Count})";
54+
return $"ChatPreprocessContext(SessionId={SessionId}, UserId={UserId}, CharacterId={CharacterId}, UserMessage='{UserMessage}', MemoryContext.Count={MemoryContext.Count}, ConversationHistory.Count={ConversationHistory.Count()})";
4355
}
4456

4557
public string GetDetailedInfo()
@@ -61,10 +73,11 @@ public string GetDetailedInfo()
6173
info.AppendLine($" [{i}]: {MemoryContext[i]}");
6274
}
6375

64-
info.AppendLine($"ConversationHistorys ({ConversationHistory.Count} items):");
65-
for (int i = 0; i < ConversationHistory.Count; i++)
76+
info.AppendLine($"ConversationHistory ({ConversationHistory.Count()} items):");
77+
var parsedHistory = ParseConversationHistory();
78+
for (int i = 0; i < parsedHistory.Count; i++)
6679
{
67-
info.AppendLine($" [{i}]: {ConversationHistory[i]}");
80+
info.AppendLine($" [{i}]: {parsedHistory[i]}");
6881
}
6982
info.AppendLine("=== End ChatPreprocessContext ===");
7083

ProjectVG.Application/ProjectVG.Application.csproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<Compile Remove="Services\LLM\**" />
11+
<EmbeddedResource Remove="Services\LLM\**" />
12+
<None Remove="Services\LLM\**" />
13+
</ItemGroup>
14+
915
<ItemGroup>
1016
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
1117
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
@@ -18,8 +24,4 @@
1824
<ProjectReference Include="..\ProjectVG.Infrastructure\ProjectVG.Infrastructure.csproj" />
1925
</ItemGroup>
2026

21-
<ItemGroup>
22-
<Folder Include="Services\LLM\" />
23-
</ItemGroup>
24-
2527
</Project>

ProjectVG.Application/Services/ApplicationServiceCollectionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using ProjectVG.Application.Services.User;
44
using ProjectVG.Application.Services.Chat;
55
using ProjectVG.Application.Services.Chat.Preprocessors;
6+
using ProjectVG.Application.Services.Chat.Processors;
7+
using ProjectVG.Application.Services.Chat.Validators;
68

79
namespace ProjectVG.Application.Services
810
{
@@ -23,10 +25,8 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection
2325
services.AddScoped<ChatTTSProcessor>();
2426
services.AddScoped<ChatResultProcessor>();
2527
services.AddScoped<UserInputAnalysisProcessor>();
26-
27-
// 전처리기들 등록
28+
services.AddScoped<UserInputActionProcessor>();
2829
services.AddScoped<MemoryContextPreprocessor>();
29-
services.AddScoped<ConversationHistoryPreprocessor>();
3030

3131
return services;
3232
}

ProjectVG.Application/Services/Character/TestCharacterService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using ProjectVG.Application.Models.Character;
22
using System.Collections.Concurrent;
3-
using ProjectVG.Common.Constants;
43

54
namespace ProjectVG.Application.Services.Character
65
{

0 commit comments

Comments
 (0)