summaryrefslogtreecommitdiff
path: root/src/Agent.Core/Sessions/SessionStore.cs
blob: 6224e80b5ea9ffcddeaa95bbea9132840ee0566b (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
using System.Collections.Concurrent;

namespace Agent.Core.Sessions;

public sealed class SessionStore
{
    private readonly ConcurrentDictionary<Guid, AgentSession> _sessions = new();

    /// <summary>
    /// Enumerates all active sessions. Primarily for TaskGraphToolHandler to resolve graphs by TaskId.
    /// </summary>
    public IEnumerable<AgentSession?> Sessions => _sessions.Values;

    public AgentSession Create()
    {
        while (true)
        {
            var session = new AgentSession(Guid.NewGuid());
            if (_sessions.TryAdd(session.SessionId, session)) return session;
        }
    }

    public bool TryGet(Guid sessionId, out AgentSession? session) => _sessions.TryGetValue(sessionId, out session);
}