Files
AX-Copilot-Codex/src/AxCopilot/Services/Agent/EnvTool.cs
lacvet 33c1db4dae
Some checks failed
Release Gate / gate (push) Has been cancelled
에이전트 선택적 탐색 구조 개선과 경고 정리 반영
- claude-code 선택적 탐색 흐름을 참고해 Cowork/Code 시스템 프롬프트에서 folder_map 상시 선행 지시를 완화하고 glob/grep 기반 좁은 탐색을 우선하도록 조정함

- FolderMapTool 기본 depth를 2로, include_files 기본값을 false로 낮추고 MultiReadTool 최대 파일 수를 8개로 줄여 초기 과탐색 폭을 보수적으로 조정함

- AgentLoopExplorationPolicy partial을 추가해 탐색 범위 분류, broad-scan corrective hint, exploration_breadth 성능 로그를 연결함

- AgentLoopService에 탐색 범위 가이드 주입과 실행 중 탐색 폭 추적을 추가하고, 좁은 질문에서 반복적인 folder_map/대량 multi_read를 교정하도록 정리함

- DocxToHtmlConverter nullable 경고를 수정해 Release 빌드 경고 0 / 오류 0 기준을 다시 충족함

- README와 docs/DEVELOPMENT.md에 2026-04-09 10:36 (KST) 기준 개발 이력을 반영함
2026-04-09 14:27:59 +09:00

128 lines
4.6 KiB
C#

using System.Text.Json;
namespace AxCopilot.Services.Agent;
/// <summary>
/// 환경변수 읽기·쓰기 도구.
/// 현재 프로세스 범위에서만 동작하며 시스템 환경변수는 변경하지 않습니다.
/// </summary>
public class EnvTool : IAgentTool
{
public string Name => "env_tool";
public string Description =>
"Read or set environment variables (process scope only). Actions: " +
"'get' — read an environment variable value; " +
"'set' — set an environment variable (process scope, not permanent); " +
"'list' — list all environment variables; " +
"'expand' — expand %VAR% references in a string.";
public ToolParameterSchema Parameters => new()
{
Properties = new()
{
["action"] = new()
{
Type = "string",
Description = "Action to perform",
Enum = ["get", "set", "list", "expand"],
},
["name"] = new()
{
Type = "string",
Description = "Variable name (for get/set)",
},
["value"] = new()
{
Type = "string",
Description = "Variable value (for set) or string to expand (for expand)",
},
["filter"] = new()
{
Type = "string",
Description = "Filter pattern for list action (case-insensitive substring match)",
},
},
Required = ["action"],
};
public Task<ToolResult> ExecuteAsync(JsonElement args, AgentContext context, CancellationToken ct = default)
{
var action = args.GetProperty("action").SafeGetString() ?? "";
try
{
return Task.FromResult(action switch
{
"get" => Get(args),
"set" => Set(args),
"list" => ListVars(args),
"expand" => Expand(args),
_ => ToolResult.Fail($"Unknown action: {action}"),
});
}
catch (Exception ex)
{
return Task.FromResult(ToolResult.Fail($"환경변수 오류: {ex.Message}"));
}
}
private static ToolResult Get(JsonElement args)
{
if (!args.SafeTryGetProperty("name", out var n))
return ToolResult.Fail("'name' parameter is required for get action");
var name = n.SafeGetString() ?? "";
var value = Environment.GetEnvironmentVariable(name);
return value != null
? ToolResult.Ok($"{name}={value}")
: ToolResult.Ok($"{name} is not set");
}
private static ToolResult Set(JsonElement args)
{
if (!args.SafeTryGetProperty("name", out var n))
return ToolResult.Fail("'name' parameter is required for set action");
if (!args.SafeTryGetProperty("value", out var v))
return ToolResult.Fail("'value' parameter is required for set action");
var name = n.SafeGetString() ?? "";
var value = v.SafeGetString() ?? "";
Environment.SetEnvironmentVariable(name, value, EnvironmentVariableTarget.Process);
return ToolResult.Ok($"✓ Set {name}={value} (process scope)");
}
private static ToolResult ListVars(JsonElement args)
{
var filter = args.SafeTryGetProperty("filter", out var f) ? f.SafeGetString() ?? "" : "";
var vars = Environment.GetEnvironmentVariables();
var entries = new List<string>();
foreach (System.Collections.DictionaryEntry entry in vars)
{
var key = entry.Key?.ToString() ?? "";
var val = entry.Value?.ToString() ?? "";
if (!string.IsNullOrEmpty(filter) &&
!key.Contains(filter, StringComparison.OrdinalIgnoreCase) &&
!val.Contains(filter, StringComparison.OrdinalIgnoreCase))
continue;
// 긴 값은 자르기
if (val.Length > 120) val = val[..120] + "...";
entries.Add($"{key}={val}");
}
entries.Sort(StringComparer.OrdinalIgnoreCase);
var result = $"Environment variables ({entries.Count}):\n" + string.Join("\n", entries);
if (result.Length > 8000) result = result[..8000] + "\n... (truncated)";
return ToolResult.Ok(result);
}
private static ToolResult Expand(JsonElement args)
{
if (!args.SafeTryGetProperty("value", out var v))
return ToolResult.Fail("'value' parameter is required for expand action");
var input = v.SafeGetString() ?? "";
var expanded = Environment.ExpandEnvironmentVariables(input);
return ToolResult.Ok(expanded);
}
}