summaryrefslogtreecommitdiff
path: root/src/Agent.Mcp/QueuedTaskWorker.cs
blob: edea1ade11650e817fb377b27cc68592a4f23025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using Agent.Core.Tasks;
using Microsoft.Extensions.Hosting;
using Agent.Core.Runtime;

namespace Agent.Mcp;

public sealed class QueuedTaskWorker(TaskRegistry registry, AgentRuntime runtime) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await foreach (Guid taskId in registry.ReadQueuedAsync(stoppingToken).ConfigureAwait(false))
        {
            if (!registry.Transition(taskId, AgentTaskState.Planning, "planning")) continue;
            registry.Transition(taskId, AgentTaskState.Running, "ollama chat");
            using var linked = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken, registry.GetCancellationToken(taskId));
            try
            {
                await runtime.ExecuteAsync(taskId, linked.Token).ConfigureAwait(false);
            }
            catch (OperationCanceledException) when (linked.IsCancellationRequested)
            {
                registry.Cancel(taskId);
            }
        }
    }
}