package llm import ( "fmt" "os" ) const ( PromptTypeFormatting = "formatting" ) // LoadDefaultPrompt reads the built-in default prompt for the given type. func LoadDefaultPrompt(promptType string) (string, error) { var filename string switch promptType { case PromptTypeFormatting: filename = "prompts/formatting.txt" default: return "", fmt.Errorf("unknown prompt type: %s", promptType) } data, err := os.ReadFile(filename) if err != nil { return "", fmt.Errorf("reading default prompt %s: %w", filename, err) } return string(data), nil } // BuildFormatMessage builds the user message for the formatting pass. // If userInstructions is non-empty, it wraps the markdown with the prompt instructions tags. func BuildFormatMessage(markdown string, userInstructions string) string { prefix := "Here is the markdown text from a PDF form. Clean up formatting anomalies only — return the ENTIRE document with no sections omitted. Do not summarize, rewrite, or answer the form.\n\n" if userInstructions != "" { prefix += fmt.Sprintf("\n%s\n\n\n", userInstructions) } prefix += "## Markdown to clean:\n" return prefix + markdown } // FormatResponse extracts clean markdown from the LLM response. // Strips code fences if present. func FormatResponse(response string) string { // Strip markdown code fences if present const open = "```" const close = "```" const openMd = "```markdown" const openMdNL = "```md" start := 0 if len(response) > len(openMdNL) && response[:len(openMdNL)] == openMdNL { // Find newline after ```md for i := len(openMdNL); i < len(response); i++ { if response[i] == '\n' { start = i + 1 break } } } else if len(response) > len(openMd) && response[:len(openMd)] == openMd { for i := len(openMd); i < len(response); i++ { if response[i] == '\n' { start = i + 1 break } } } else if len(response) > len(open) && response[:len(open)] == open { for i := len(open); i < len(response); i++ { if response[i] == '\n' { start = i + 1 break } } } if start > 0 { // Find closing fence idx := len(response) - len(close) for i := start; i <= idx; i++ { if response[i:i+len(close)] == close { return response[start:i] } } return response[start:] } return response } // HasOpenAIKey checks if the OpenAI API key is configured. func HasOpenAIKey() bool { return os.Getenv("OPENAI_API_KEY") != "" }