feat(worker): add ClaudeArgsBuilder for dynamic CLI argument construction
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
65
src/ClaudeDo.Worker/Runner/ClaudeArgsBuilder.cs
Normal file
65
src/ClaudeDo.Worker/Runner/ClaudeArgsBuilder.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ClaudeDo.Worker.Runner;
|
||||
|
||||
public sealed record ClaudeRunConfig(
|
||||
string? Model,
|
||||
string? SystemPrompt,
|
||||
string? AgentPath,
|
||||
string? ResumeSessionId
|
||||
);
|
||||
|
||||
public sealed class ClaudeArgsBuilder
|
||||
{
|
||||
private static readonly string ResultSchema = JsonSerializer.Serialize(new
|
||||
{
|
||||
type = "object",
|
||||
properties = new
|
||||
{
|
||||
summary = new { type = "string" },
|
||||
files_changed = new { type = "array", items = new { type = "string" } },
|
||||
commit_type = new { type = "string" },
|
||||
},
|
||||
required = new[] { "summary" },
|
||||
});
|
||||
|
||||
public string Build(ClaudeRunConfig config)
|
||||
{
|
||||
var args = new List<string>
|
||||
{
|
||||
"-p",
|
||||
"--output-format stream-json",
|
||||
"--verbose",
|
||||
"--dangerously-skip-permissions",
|
||||
};
|
||||
|
||||
if (config.Model is not null)
|
||||
args.Add($"--model {config.Model}");
|
||||
|
||||
if (config.SystemPrompt is not null)
|
||||
args.Add($"--append-system-prompt {Escape(config.SystemPrompt)}");
|
||||
|
||||
if (config.AgentPath is not null)
|
||||
{
|
||||
var agentJson = JsonSerializer.Serialize(new[] { new { file = config.AgentPath } });
|
||||
args.Add($"--agents {Escape(agentJson)}");
|
||||
}
|
||||
|
||||
args.Add($"--json-schema {Escape(ResultSchema)}");
|
||||
|
||||
if (config.ResumeSessionId is not null)
|
||||
args.Add($"--resume {config.ResumeSessionId}");
|
||||
|
||||
return string.Join(" ", args);
|
||||
}
|
||||
|
||||
private static string Escape(string value)
|
||||
{
|
||||
if (value.Contains(' ') || value.Contains('"') || value.Contains('\''))
|
||||
{
|
||||
var escaped = value.Replace("\\", "\\\\").Replace("\"", "\\\"");
|
||||
return $"\"{escaped}\"";
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user