[Phase 17-D] 스킬 시스템 고도화 — paths:glob 자동주입 + context:fork 격리 실행
AgentLoopService.Skills.cs (신규, 95줄): - InjectPathBasedSkills(): 파일 도구 성공 후 filePath로 GlobMatcher 매칭 → 매칭 스킬 시스템 프롬프트를 시스템 메시지에 in-place 주입 → SkillActivated JSONL 이벤트 로그 기록 - RunSkillInForkAsync(): context:fork 스킬 격리 LLM 실행 (도구 없음) → SkillCompleted JSONL 이벤트 로그 기록 SkillManagerTool.cs: - SetForkRunner(Func<SkillDefinition, string, CancellationToken, Task<string>>) 추가 - exec 액션 + arguments 파라미터 추가 - ExecSkillAsync(): PrepareSkillBodyAsync 인자 치환 → IsForkContext=true: fork runner 호출 → [Fork 스킬 결과] 반환 → 일반 스킬: 시스템 프롬프트 + 준비된 본문 반환 AgentLoopService.cs: - 생성자: SkillManagerTool.SetForkRunner(RunSkillInForkAsync) 주입 AgentLoopService.Execution.cs: - 도구 성공 직후 InjectPathBasedSkills(result.FilePath, messages) 호출 빌드: 경고 0, 오류 0 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
125
src/AxCopilot/Services/Agent/AgentLoopService.Skills.cs
Normal file
125
src/AxCopilot/Services/Agent/AgentLoopService.Skills.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using AxCopilot.Models;
|
||||
|
||||
namespace AxCopilot.Services.Agent;
|
||||
|
||||
/// <summary>
|
||||
/// Phase 17-D: AgentLoopService — 스킬 시스템 고도화 통합.
|
||||
/// · paths: glob 패턴 매칭 스킬 자동 주입
|
||||
/// · context:fork 스킬 격리 실행
|
||||
/// </summary>
|
||||
public partial class AgentLoopService
|
||||
{
|
||||
// PathBasedSkillActivator 단일 인스턴스 (상태 없음)
|
||||
private static readonly PathBasedSkillActivator _pathActivator = new();
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
// paths: 기반 스킬 자동 주입
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 파일 도구가 반환한 파일 경로에 매칭되는 paths: 스킬을 시스템 메시지에 주입합니다.
|
||||
/// 기존 스킬 섹션은 in-place 교체로 중복 방지.
|
||||
/// </summary>
|
||||
internal void InjectPathBasedSkills(string? filePath, List<ChatMessage> messages)
|
||||
{
|
||||
if (!(_settings.Settings.Llm.EnableSkillSystem)) return;
|
||||
if (string.IsNullOrWhiteSpace(filePath)) return;
|
||||
|
||||
try
|
||||
{
|
||||
// paths: 패턴이 있는 스킬만 필터링
|
||||
var pathSkills = SkillService.Skills
|
||||
.Where(s => s.IsAvailable && s.GetPathPatterns().Count > 0)
|
||||
.ToList();
|
||||
if (pathSkills.Count == 0) return;
|
||||
|
||||
var activeSkills = _pathActivator.GetActiveSkillsForFile(pathSkills, filePath);
|
||||
if (activeSkills.Count == 0) return;
|
||||
|
||||
var injection = _pathActivator.BuildSkillContextInjection(activeSkills);
|
||||
if (string.IsNullOrEmpty(injection)) return;
|
||||
|
||||
const string marker = "## 현재 파일에 자동 적용된 스킬";
|
||||
var sysMsg = messages.FirstOrDefault(m => m.Role == "system");
|
||||
if (sysMsg != null)
|
||||
{
|
||||
// 기존 섹션 교체 (in-place, 중복 방지)
|
||||
var idx = sysMsg.Content.IndexOf(marker, StringComparison.Ordinal);
|
||||
if (idx >= 0)
|
||||
sysMsg.Content = sysMsg.Content[..idx] + injection;
|
||||
else
|
||||
sysMsg.Content += "\n\n" + injection;
|
||||
}
|
||||
else
|
||||
{
|
||||
messages.Insert(0, new ChatMessage { Role = "system", Content = injection });
|
||||
}
|
||||
|
||||
// SkillActivated 이벤트 로그 기록
|
||||
_ = _eventLog?.AppendAsync(AgentEventLogType.SkillActivated,
|
||||
System.Text.Json.JsonSerializer.Serialize(new
|
||||
{
|
||||
filePath,
|
||||
skills = activeSkills.Select(s => s.Name).ToArray(),
|
||||
pathBased = true
|
||||
}));
|
||||
|
||||
EmitEvent(AgentEventType.Thinking, "",
|
||||
$"경로 스킬 자동 활성화: {string.Join(", ", activeSkills.Select(s => s.Label))}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogService.Warn($"[Skills] paths: 스킬 주입 실패: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
// context:fork 격리 실행
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// context:fork 스킬을 격리된 LLM 컨텍스트에서 실행합니다.
|
||||
/// 메인 대화와 독립적이며 도구 호출 없이 텍스트만 생성합니다.
|
||||
/// SkillManagerTool이 PrepareSkillBodyAsync로 인자 치환 완료 후 호출합니다.
|
||||
/// </summary>
|
||||
internal async Task<string> RunSkillInForkAsync(
|
||||
SkillDefinition skill, string preparedBody, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var forkMessages = new List<ChatMessage>();
|
||||
|
||||
// 스킬의 시스템 프롬프트를 격리 컨텍스트에 주입
|
||||
if (!string.IsNullOrWhiteSpace(skill.SystemPrompt))
|
||||
forkMessages.Add(new ChatMessage { Role = "system", Content = skill.SystemPrompt });
|
||||
|
||||
forkMessages.Add(new ChatMessage { Role = "user", Content = preparedBody });
|
||||
|
||||
EmitEvent(AgentEventType.Thinking, "skill_fork",
|
||||
$"[Fork] '{skill.Label}' 스킬을 격리 컨텍스트에서 실행 중...");
|
||||
|
||||
// 도구 없이 텍스트 응답만 생성 (격리 실행)
|
||||
var response = await _llm.SendAsync(forkMessages, ct);
|
||||
|
||||
// SkillCompleted 이벤트 로그 기록
|
||||
_ = _eventLog?.AppendAsync(AgentEventLogType.SkillCompleted,
|
||||
System.Text.Json.JsonSerializer.Serialize(new
|
||||
{
|
||||
skillName = skill.Name,
|
||||
forkContext = true,
|
||||
responseLength = response?.Length ?? 0
|
||||
}));
|
||||
|
||||
return response ?? "";
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return "(취소됨)";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogService.Warn($"[Skills] fork 컨텍스트 실행 실패: {ex.Message}");
|
||||
return $"스킬 실행 오류: {ex.Message}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user