-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromptFileReader.cs
More file actions
111 lines (95 loc) · 3.56 KB
/
PromptFileReader.cs
File metadata and controls
111 lines (95 loc) · 3.56 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
namespace MultiCliLauncher;
/// <summary>
/// Result of reading a prompts file, including parsed prompts and optional model override
/// </summary>
public class PromptsFileResult
{
public List<string> Prompts { get; set; } = new();
public string? ModelOverride { get; set; }
}
/// <summary>
/// Reads prompts from a file with support for multi-line prompts and directives
/// </summary>
public static class PromptFileReader
{
private const string PromptDelimiter = "---";
private const string ModelDirectivePrefix = "#model:";
/// <summary>
/// Reads prompts from a file asynchronously
/// </summary>
/// <param name="filePath">Path to the prompts file</param>
/// <returns>PromptsFileResult containing prompts and optional model override</returns>
public static async Task<PromptsFileResult> ReadPromptsAsync(string filePath)
{
var content = await File.ReadAllTextAsync(filePath);
return ParseContent(content);
}
/// <summary>
/// Reads prompts from a file synchronously
/// </summary>
/// <param name="filePath">Path to the prompts file</param>
/// <returns>PromptsFileResult containing prompts and optional model override</returns>
public static PromptsFileResult ReadPrompts(string filePath)
{
var content = File.ReadAllText(filePath);
return ParseContent(content);
}
/// <summary>
/// Parses content string into prompts (useful for testing)
/// </summary>
/// <param name="content">Raw file content</param>
/// <returns>PromptsFileResult containing prompts and optional model override</returns>
public static PromptsFileResult ParseContent(string content)
{
var result = new PromptsFileResult();
if (string.IsNullOrWhiteSpace(content))
{
return result;
}
// Split by delimiter
var sections = content.Split(PromptDelimiter, StringSplitOptions.None);
foreach (var section in sections)
{
var processedSection = ProcessSection(section, result);
if (!string.IsNullOrWhiteSpace(processedSection))
{
result.Prompts.Add(processedSection);
}
}
return result;
}
/// <summary>
/// Processes a section of the file, extracting directives and filtering comments
/// </summary>
private static string ProcessSection(string section, PromptsFileResult result)
{
var lines = section.Split('\n');
var promptLines = new List<string>();
foreach (var rawLine in lines)
{
var line = rawLine.TrimEnd('\r'); // Handle CRLF
var trimmedLine = line.TrimStart();
// Check for model directive (only if not already set)
if (result.ModelOverride == null &&
trimmedLine.StartsWith(ModelDirectivePrefix, StringComparison.OrdinalIgnoreCase))
{
var modelValue = trimmedLine[ModelDirectivePrefix.Length..].Trim();
if (!string.IsNullOrWhiteSpace(modelValue))
{
result.ModelOverride = modelValue;
}
continue;
}
// Skip comment lines (lines starting with #)
if (trimmedLine.StartsWith('#'))
{
continue;
}
// Add non-comment lines to prompt
promptLines.Add(line);
}
// Join lines and trim the result
var prompt = string.Join('\n', promptLines).Trim();
return prompt;
}
}