Repository navigation
Expand file tree
/
Copy pathFileSystemPromptProvider.cs
More file actions
57 lines (49 loc) · 1.69 KB
/
Copy pathFileSystemPromptProvider.cs
File metadata and controls
57 lines (49 loc) · 1.69 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) August 2026, devMobile Software
//
using FoehnAIBuilder.Configuration;
namespace FoehnAIBuilder.Chat;
/// <summary>
/// Reads the system prompt from the path in <see cref="FoehnAIBuilderOptions.SystemMessageFile"/>.
/// Missing / empty / unreadable files log a warning and produce no prompt.
/// </summary>
public sealed class FileSystemPromptProvider : ISystemPromptProvider
{
private readonly FoehnAIBuilderOptions _options;
private readonly ILogger<FileSystemPromptProvider> _logger;
private readonly Lazy<string?> _cached;
public FileSystemPromptProvider(
IOptions<FoehnAIBuilderOptions> options,
ILogger<FileSystemPromptProvider> logger)
{
_options = options.Value;
_logger = logger;
_cached = new Lazy<string?>(Load);
}
public string? GetSystemPrompt() => _cached.Value;
private string? Load()
{
var path = _options.SystemMessageFile;
if (string.IsNullOrWhiteSpace(path))
return null;
if (!File.Exists(path))
{
_logger.LogWarning("System message file '{Path}' was not found; continuing without a system prompt.", path);
return null;
}
try
{
var content = File.ReadAllText(path);
if (string.IsNullOrWhiteSpace(content))
{
_logger.LogWarning("System message file '{Path}' is empty; continuing without a system prompt.", path);
return null;
}
return content;
}
catch (IOException ex)
{
_logger.LogError(ex, "Failed to read system message file '{Path}'.", path);
return null;
}
}
}