Public Access
121 lines
4.5 KiB
C#
121 lines
4.5 KiB
C#
using MeetingAssistant.Transcription;
|
|
using MeetingAssistant.MeetingNotes;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace MeetingAssistant.Recording;
|
|
|
|
public sealed class VaultTranscriptStore : ITranscriptStore
|
|
{
|
|
private readonly MeetingAssistantOptions options;
|
|
private readonly ILogger<VaultTranscriptStore> logger;
|
|
|
|
public VaultTranscriptStore(IOptions<MeetingAssistantOptions> options, ILogger<VaultTranscriptStore> logger)
|
|
{
|
|
this.options = options.Value;
|
|
this.logger = logger;
|
|
}
|
|
|
|
public async Task<TranscriptSession> CreateSessionAsync(CancellationToken cancellationToken)
|
|
{
|
|
return await CreateSessionAsync(options, cancellationToken);
|
|
}
|
|
|
|
public async Task<TranscriptSession> CreateSessionAsync(
|
|
MeetingAssistantOptions options,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var folder = VaultPath.Resolve(options.Vault, options.Vault.TranscriptsFolder);
|
|
Directory.CreateDirectory(folder);
|
|
|
|
var fileName = $"{DateTimeOffset.Now:yyyyMMdd-HHmmss-fffffff}-transcript.md";
|
|
var path = Path.Combine(folder, fileName);
|
|
await File.WriteAllTextAsync(path, $"# Meeting Transcript{Environment.NewLine}{Environment.NewLine}", cancellationToken);
|
|
logger.LogInformation("Created meeting transcript file {TranscriptPath}", path);
|
|
|
|
return new TranscriptSession(path);
|
|
}
|
|
|
|
public Task AppendAsync(TranscriptSession session, TranscriptionSegment segment, CancellationToken cancellationToken)
|
|
{
|
|
return AppendToBodyAsync(session.TranscriptPath, FormatSegment(segment), cancellationToken);
|
|
}
|
|
|
|
public Task ReplaceAsync(
|
|
TranscriptSession session,
|
|
IReadOnlyList<TranscriptionSegment> replacementSegments,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var existing = File.Exists(session.TranscriptPath)
|
|
? File.ReadAllText(session.TranscriptPath)
|
|
: "";
|
|
var (frontmatter, _) = MeetingArtifactFrontmatterRenderer.Split(existing);
|
|
var body = "# Meeting Transcript"
|
|
+ Environment.NewLine
|
|
+ Environment.NewLine
|
|
+ string.Concat(replacementSegments.Select(FormatSegment));
|
|
var content = string.IsNullOrWhiteSpace(frontmatter)
|
|
? body
|
|
: "---" + Environment.NewLine + frontmatter + Environment.NewLine + "---" + Environment.NewLine + Environment.NewLine + body;
|
|
return File.WriteAllTextAsync(session.TranscriptPath, content, cancellationToken);
|
|
}
|
|
|
|
public async Task UpdateMetadataAsync(
|
|
TranscriptSession session,
|
|
MeetingSessionArtifacts artifacts,
|
|
MeetingNote meetingNote,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var body = File.Exists(session.TranscriptPath)
|
|
? MeetingArtifactFrontmatterRenderer.Split(await File.ReadAllTextAsync(session.TranscriptPath, cancellationToken)).Body
|
|
: "# Meeting Transcript" + Environment.NewLine + Environment.NewLine;
|
|
var frontmatter = MeetingArtifactFrontmatterRenderer.Create(
|
|
artifacts,
|
|
meetingNote,
|
|
MeetingArtifactFrontmatterRenderer.DefaultTitle(meetingNote, "Meeting Transcript"),
|
|
session.TranscriptPath);
|
|
|
|
await File.WriteAllTextAsync(
|
|
session.TranscriptPath,
|
|
MeetingArtifactFrontmatterRenderer.Render(frontmatter, body),
|
|
cancellationToken);
|
|
}
|
|
|
|
private static string FormatSegment(TranscriptionSegment segment)
|
|
{
|
|
var speaker = string.IsNullOrWhiteSpace(segment.Speaker) ? "Unknown" : segment.Speaker;
|
|
return $"[{segment.Start:hh\\:mm\\:ss}] {speaker}: {segment.Text}{Environment.NewLine}";
|
|
}
|
|
|
|
private static async Task AppendToBodyAsync(
|
|
string path,
|
|
string text,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (!File.Exists(path))
|
|
{
|
|
await File.AppendAllTextAsync(path, text, cancellationToken);
|
|
return;
|
|
}
|
|
|
|
var content = await File.ReadAllTextAsync(path, cancellationToken);
|
|
var (frontmatter, body) = MeetingArtifactFrontmatterRenderer.Split(content);
|
|
if (string.IsNullOrWhiteSpace(frontmatter))
|
|
{
|
|
await File.AppendAllTextAsync(path, text, cancellationToken);
|
|
return;
|
|
}
|
|
|
|
var updated = "---"
|
|
+ Environment.NewLine
|
|
+ frontmatter
|
|
+ Environment.NewLine
|
|
+ "---"
|
|
+ Environment.NewLine
|
|
+ Environment.NewLine
|
|
+ body
|
|
+ text;
|
|
await File.WriteAllTextAsync(path, updated, cancellationToken);
|
|
}
|
|
|
|
}
|