diff options
Diffstat (limited to 'src/Agent.Core/Tools/InternalToolDispatcher.cs')
| -rw-r--r-- | src/Agent.Core/Tools/InternalToolDispatcher.cs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/Agent.Core/Tools/InternalToolDispatcher.cs b/src/Agent.Core/Tools/InternalToolDispatcher.cs new file mode 100644 index 0000000..7302c1f --- /dev/null +++ b/src/Agent.Core/Tools/InternalToolDispatcher.cs @@ -0,0 +1,29 @@ +using System.Text.Json; + +namespace Agent.Core.Tools; + +public sealed class InternalToolDispatcher(IToolPolicyEngine policy, IInternalToolBackend backend) +{ + public Task BeginTaskAsync(Guid taskId, CancellationToken cancellationToken) => backend.BeginTaskAsync(taskId, cancellationToken); + public Task EndTaskAsync(Guid taskId, bool success, CancellationToken cancellationToken) => backend.EndTaskAsync(taskId, success, cancellationToken); + + public async Task<ToolExecutionResult> DispatchAsync(Guid taskId, string toolName, JsonElement arguments, CancellationToken cancellationToken) + { + InternalToolValidator.Validate(toolName, arguments); + ToolPolicyDecision decision = policy.Evaluate(toolName, arguments); + if (!decision.Allowed) return new(false, decision.Reason, false, $"policy:{toolName}:{decision.Reason}"); + return await backend.ExecuteAsync(taskId, toolName, arguments, cancellationToken).ConfigureAwait(false); + } +} + +public sealed class FakeInternalToolBackend : IInternalToolBackend +{ + private readonly List<(string Name, JsonElement Arguments)> _calls = []; + public IReadOnlyList<(string Name, JsonElement Arguments)> Calls => _calls; + public Task<ToolExecutionResult> ExecuteAsync(Guid taskId, string toolName, JsonElement arguments, CancellationToken cancellationToken) + { + _calls.Add((toolName, arguments.Clone())); + bool effect = toolName is "file_edit" or "process_run"; + return Task.FromResult(new ToolExecutionResult(true, "fake-ok", effect)); + } +} |
