Files
meeting-assistant/MeetingAssistant/MacOs/MacOsWorkflowRulesEditorSessions.cs
T
dh 728e66dd72
PR and Push Build/Test / windows-build-and-test (push) Failing after 38m25s
PR and Push Build/Test / portable-build-and-test (push) Successful in 8m9s
PR and Push Build/Test / macos-native-full (push) Skipped
ci: consolidate platform pipelines and test prerequisites on macOS support
2026-10-06 09:02:19 +02:00

75 lines
2.8 KiB
C#

using System.Collections.Concurrent;
using MeetingAssistant.Workflow;
using Microsoft.Extensions.Options;
namespace MeetingAssistant.MacOs;
public sealed class MacOsWorkflowRulesEditorSessions
{
private readonly MacOsWorkflowRulesEditorSession defaultSession;
private readonly ConcurrentDictionary<string, MacOsWorkflowRulesEditorSession> sessions = new(StringComparer.Ordinal);
private readonly ConcurrentDictionary<string, MacOsAgentWindowRequest> windows = new(StringComparer.Ordinal);
private readonly IWorkflowRulesEditorChatPipeline pipeline;
private readonly string agentUrl;
private readonly CancellationToken applicationStopping;
private readonly ILogger<MacOsWorkflowRulesEditorSessions> logger;
public MacOsWorkflowRulesEditorSessions(
IWorkflowRulesEditorChatPipeline pipeline,
IOptions<MeetingAssistantOptions> options,
IHostApplicationLifetime applicationLifetime,
ILogger<MacOsWorkflowRulesEditorSessions> logger)
{
this.pipeline = pipeline;
this.logger = logger;
applicationStopping = applicationLifetime.ApplicationStopping;
agentUrl = $"{options.Value.Api.PublicBaseUrl.TrimEnd('/')}/diagnostics/settings-and-logs";
defaultSession = CreateSession();
}
public bool TryGetSession(string? id, out MacOsWorkflowRulesEditorSession session)
{
if (string.IsNullOrEmpty(id))
{
session = defaultSession;
return true;
}
return sessions.TryGetValue(id, out session!);
}
public IReadOnlyList<MacOsAgentWindowRequest> GetAgentWindows() =>
applicationStopping.IsCancellationRequested ? [] : windows.Values.ToArray();
public void OpenWithPrompt(string initialPrompt)
{
ArgumentException.ThrowIfNullOrWhiteSpace(initialPrompt);
applicationStopping.ThrowIfCancellationRequested();
var id = Guid.NewGuid().ToString("N");
var session = CreateSession();
sessions[id] = session;
var task = session.SendAsync(initialPrompt, applicationStopping);
windows[id] = new MacOsAgentWindowRequest(id, $"{agentUrl}?session={id}");
_ = ObservePromptAsync(task, id);
}
private MacOsWorkflowRulesEditorSession CreateSession() =>
new(new WorkflowRulesEditorChatViewModel(pipeline), applicationStopping);
private async Task ObservePromptAsync(Task task, string id)
{
try
{
await task;
}
catch (OperationCanceledException) when (applicationStopping.IsCancellationRequested)
{
}
catch (Exception exception)
{
logger.LogError(exception, "Could not start approved workflow conversation {SessionId}", id);
}
}
}
public sealed record MacOsAgentWindowRequest(string Id, string Url);