| | | 1 | | using Microsoft.Extensions.FileProviders; |
| | | 2 | | |
| | | 3 | | namespace OpenAiIntegration; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Default implementation that loads instructions templates from the file system |
| | | 7 | | /// </summary> |
| | | 8 | | public class InstructionsTemplateProvider : IInstructionsTemplateProvider |
| | | 9 | | { |
| | | 10 | | private readonly IFileProvider _fileProvider; |
| | | 11 | | |
| | 1 | 12 | | public InstructionsTemplateProvider(IFileProvider fileProvider) |
| | | 13 | | { |
| | 1 | 14 | | _fileProvider = fileProvider; |
| | 1 | 15 | | } |
| | | 16 | | |
| | | 17 | | public (string template, string path) LoadMatchTemplate(string model, bool includeJustification) |
| | | 18 | | { |
| | 1 | 19 | | var promptModel = GetPromptModelForModel(model); |
| | 1 | 20 | | var fileName = includeJustification ? "match.justification.md" : "match.md"; |
| | 1 | 21 | | var filePath = $"{promptModel}/{fileName}"; |
| | | 22 | | |
| | 1 | 23 | | var fileInfo = _fileProvider.GetFileInfo(filePath); |
| | 1 | 24 | | if (fileInfo.Exists) |
| | | 25 | | { |
| | 1 | 26 | | return ReadFileContent(fileInfo); |
| | | 27 | | } |
| | | 28 | | |
| | 1 | 29 | | if (includeJustification) |
| | | 30 | | { |
| | 1 | 31 | | var fallbackPath = $"{promptModel}/match.md"; |
| | 1 | 32 | | var fallbackFileInfo = _fileProvider.GetFileInfo(fallbackPath); |
| | 1 | 33 | | if (fallbackFileInfo.Exists) |
| | | 34 | | { |
| | 1 | 35 | | return ReadFileContent(fallbackFileInfo); |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | throw new FileNotFoundException($"Match instructions not found at: {filePath}"); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public (string template, string path) LoadBonusTemplate(string model) |
| | | 43 | | { |
| | 1 | 44 | | var promptModel = GetPromptModelForModel(model); |
| | 1 | 45 | | var filePath = $"{promptModel}/bonus.md"; |
| | | 46 | | |
| | 1 | 47 | | var fileInfo = _fileProvider.GetFileInfo(filePath); |
| | 1 | 48 | | if (fileInfo.Exists) |
| | | 49 | | { |
| | 1 | 50 | | return ReadFileContent(fileInfo); |
| | | 51 | | } |
| | | 52 | | |
| | 1 | 53 | | throw new FileNotFoundException($"Bonus instructions not found at: {filePath}"); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Reads the content from a file info and returns it with the physical path |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="fileInfo">The file info to read from</param> |
| | | 60 | | /// <returns>A tuple containing the file content and physical path</returns> |
| | | 61 | | /// <exception cref="InvalidOperationException">Thrown when the physical path is null</exception> |
| | | 62 | | private static (string content, string path) ReadFileContent(IFileInfo fileInfo) |
| | | 63 | | { |
| | 1 | 64 | | if (fileInfo.PhysicalPath == null) |
| | | 65 | | { |
| | 0 | 66 | | throw new InvalidOperationException( |
| | 0 | 67 | | $"File '{fileInfo.Name}' does not have a physical path. " + |
| | 0 | 68 | | "This may indicate the file is from a non-physical file provider."); |
| | | 69 | | } |
| | | 70 | | |
| | 1 | 71 | | using var stream = fileInfo.CreateReadStream(); |
| | 1 | 72 | | using var reader = new StreamReader(stream); |
| | 1 | 73 | | return (reader.ReadToEnd(), fileInfo.PhysicalPath); |
| | 1 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Maps a model name to the appropriate prompt directory, handling cross-model mappings |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="model">The model name to map</param> |
| | | 80 | | /// <returns>The prompt directory name to use</returns> |
| | | 81 | | private static string GetPromptModelForModel(string model) |
| | | 82 | | { |
| | 1 | 83 | | return model switch |
| | 1 | 84 | | { |
| | 1 | 85 | | // Direct mappings |
| | 1 | 86 | | "o3" => "o3", |
| | 1 | 87 | | "gpt-5" => "gpt-5", |
| | 1 | 88 | | |
| | 1 | 89 | | // Cross-model mappings |
| | 1 | 90 | | "o4-mini" => "o3", |
| | 1 | 91 | | "gpt-5-mini" => "gpt-5", |
| | 1 | 92 | | "gpt-5-nano" => "gpt-5", |
| | 1 | 93 | | |
| | 1 | 94 | | // Default to the model name itself for any new models |
| | 1 | 95 | | _ => model |
| | 1 | 96 | | }; |
| | | 97 | | } |
| | | 98 | | } |