// // PromptLibrary.swift // Zyquo Local // // Author: Simon-Pierre Boucher // Mail: contact@spboucher.ai // import Foundation import Observation /// A reusable prompt template with `{{input}}` variables. struct PromptTemplate: Identifiable, Codable, Hashable, Sendable { var id: UUID var name: String var category: String var template: String var isBuiltIn: Bool init(id: UUID = UUID(), name: String, category: String, template: String, isBuiltIn: Bool = false) { self.id = id self.name = name self.category = category self.template = template self.isBuiltIn = isBuiltIn } /// Variable names in order of appearance ({{input}}, {{language}}, …). var variables: [String] { var seen = Set() var result: [String] = [] var search = template[template.startIndex...] while let open = search.range(of: "{{"), let close = search[open.upperBound...].range(of: "}}") { let name = String(search[open.upperBound.. String { var output = template for (key, value) in values { output = output.replacingOccurrences(of: "{{\(key)}}", with: value) output = output.replacingOccurrences(of: "{{ \(key) }}", with: value) } return output } } /// Ships ≥50 quality templates; user templates persist alongside. @MainActor @Observable final class PromptLibrary { var userTemplates: [PromptTemplate] { didSet { PersistenceService.saveDocument(userTemplates, named: "prompt-templates") } } var all: [PromptTemplate] { Self.builtIns + userTemplates } var categories: [String] { var seen = Set() return all.compactMap { seen.insert($0.category).inserted ? $0.category : nil } } init() { userTemplates = PersistenceService.loadDocument([PromptTemplate].self, named: "prompt-templates") ?? [] } func add(_ template: PromptTemplate) { userTemplates.append(template) } func remove(id: UUID) { userTemplates.removeAll { $0.id == id } } private static func t(_ name: String, _ category: String, _ template: String) -> PromptTemplate { PromptTemplate(name: name, category: category, template: template, isBuiltIn: true) } /// 56 built-in templates across 8 categories. static let builtIns: [PromptTemplate] = [ // ── Writing ───────────────────────────────────────────────────────── t("Improve writing", "Writing", "Improve the clarity, flow and concision of this text while keeping its meaning and tone:\n\n{{input}}"), t("Fix grammar", "Writing", "Fix all grammar, spelling and punctuation mistakes in this text. Return only the corrected text:\n\n{{input}}"), t("Make it shorter", "Writing", "Rewrite this text at half its length without losing the key information:\n\n{{input}}"), t("Make it longer", "Writing", "Expand this text with more detail, examples and nuance, keeping its voice:\n\n{{input}}"), t("Change tone", "Writing", "Rewrite this text in a {{tone}} tone:\n\n{{input}}"), t("Draft an email", "Writing", "Write a professional email about the following, with a clear subject line. Keep it under 150 words:\n\n{{input}}"), t("Blog post outline", "Writing", "Create a detailed outline for a blog post about: {{input}}. Include a hook, 4–6 sections with bullet points, and a conclusion."), t("Title ideas", "Writing", "Suggest 10 compelling titles for: {{input}}. Mix styles: direct, curiosity-driven, how-to, and listicle."), // ── Coding ────────────────────────────────────────────────────────── t("Explain code", "Coding", "Explain what this code does, step by step, then summarize its purpose in one sentence:\n\n```\n{{input}}\n```"), t("Review code", "Coding", "Review this code for bugs, edge cases, performance and readability. Give concrete fixes:\n\n```\n{{input}}\n```"), t("Refactor code", "Coding", "Refactor this code for clarity and maintainability, preserving exact behavior. Explain each change briefly:\n\n```\n{{input}}\n```"), t("Write tests", "Coding", "Write thorough unit tests for this code, covering happy paths and edge cases:\n\n```\n{{input}}\n```"), t("Add documentation", "Coding", "Add clear documentation comments to this code. Return the documented code:\n\n```\n{{input}}\n```"), t("Translate to language", "Coding", "Translate this code to {{language}}, keeping behavior identical and using idiomatic style:\n\n```\n{{input}}\n```"), t("Debug an error", "Coding", "Here is code and the error it produces. Diagnose the root cause and give a fix.\n\nCode:\n```\n{{input}}\n```\n\nError:\n{{error}}"), t("Regex builder", "Coding", "Write a regular expression that {{input}}. Explain each part and give 3 matching and 3 non-matching examples."), t("SQL query", "Coding", "Write a SQL query that {{input}}. Assume sensible table/column names, state your assumptions, and explain the query."), t("Shell one-liner", "Coding", "Write a macOS shell one-liner that {{input}}. Explain what each part does and note any pitfalls."), // ── Analysis ──────────────────────────────────────────────────────── t("Summarize", "Analysis", "Summarize this text in 5 bullet points, then one sentence:\n\n{{input}}"), t("Key takeaways", "Analysis", "Extract the key takeaways from this text as a prioritized list, most important first:\n\n{{input}}"), t("Pros and cons", "Analysis", "List the pros and cons of: {{input}}. End with a balanced recommendation."), t("Compare options", "Analysis", "Compare these options across the criteria that matter most; use a table, then recommend one:\n\n{{input}}"), t("Find weaknesses", "Analysis", "Steelman the strongest objections to this argument, then assess which objections actually hold:\n\n{{input}}"), t("Fact-check reasoning", "Analysis", "Check this reasoning for logical fallacies and unsupported claims, quoting each problem:\n\n{{input}}"), t("SWOT analysis", "Analysis", "Produce a SWOT analysis (strengths, weaknesses, opportunities, threats) for: {{input}}"), t("Data interpretation", "Analysis", "Interpret this data: what patterns, anomalies and conclusions stand out? What further data would help?\n\n{{input}}"), // ── Learning ──────────────────────────────────────────────────────── t("Explain like I'm five", "Learning", "Explain {{input}} so a curious 5-year-old would get it, using one everyday analogy."), t("Explain in depth", "Learning", "Give an expert-level explanation of {{input}}: precise definitions, how it works, trade-offs, and common misconceptions."), t("Study plan", "Learning", "Design a 4-week study plan to learn {{input}} from scratch: weekly goals, resources, exercises and checkpoints."), t("Quiz me", "Learning", "Create a 10-question quiz on {{input}} with increasing difficulty. Show answers with explanations at the end."), t("Analogy maker", "Learning", "Explain {{input}} through 3 different analogies from unrelated domains, and note where each analogy breaks down."), t("Flashcards", "Learning", "Turn this material into 15 question→answer flashcards, hardest concepts first:\n\n{{input}}"), t("Historical context", "Learning", "Explain the historical context of {{input}}: what led to it, why it mattered, and its lasting consequences."), // ── Productivity ──────────────────────────────────────────────────── t("Meeting agenda", "Productivity", "Create a focused agenda for a {{duration}} meeting about {{input}}: timed sections, owners, and desired outcomes."), t("Meeting minutes", "Productivity", "Turn these raw notes into clean meeting minutes with decisions, action items (owner + due date), and open questions:\n\n{{input}}"), t("Prioritize tasks", "Productivity", "Prioritize these tasks with an effort/impact matrix and propose what to do today, this week, and to drop:\n\n{{input}}"), t("Project plan", "Productivity", "Break this project into phases with milestones, dependencies and risks:\n\n{{input}}"), t("Decision matrix", "Productivity", "Build a weighted decision matrix for this decision; propose criteria and weights, score the options, and conclude:\n\n{{input}}"), t("Brainstorm ideas", "Productivity", "Brainstorm 20 ideas for {{input}} — first 10 sensible, next 10 wild. Then mark the 3 most promising."), t("Weekly review", "Productivity", "Structure a weekly review from these notes: wins, misses, lessons, and next week's top 3 priorities:\n\n{{input}}"), // ── Communication ─────────────────────────────────────────────────── t("Difficult message", "Communication", "Help me write a kind but clear message about this difficult situation. Offer two versions — softer and more direct:\n\n{{input}}"), t("Negotiation prep", "Communication", "Prepare me for this negotiation: my leverage, their likely position, anchors, concessions and walk-away point:\n\n{{input}}"), t("Feedback for a colleague", "Communication", "Turn these observations into constructive, specific, actionable feedback using the SBI (situation-behavior-impact) format:\n\n{{input}}"), t("Announcement", "Communication", "Write a clear announcement about {{input}} for {{audience}}: what changes, why, when, and what they need to do."), t("Apology note", "Communication", "Write a sincere apology for this situation — own the mistake, no excuses, concrete repair step:\n\n{{input}}"), t("Elevator pitch", "Communication", "Craft a 30-second elevator pitch for {{input}}, plus a one-line hook and a follow-up question to keep the conversation going."), // ── Translation & language ────────────────────────────────────────── t("Translate", "Language", "Translate this text to {{language}}, preserving tone and idioms naturally:\n\n{{input}}"), t("Translate + explain", "Language", "Translate this to {{language}}, then explain any idioms or cultural references that required adaptation:\n\n{{input}}"), t("Proofread (non-native)", "Language", "I'm not a native speaker. Correct this text and briefly explain the 3 most instructive mistakes:\n\n{{input}}"), t("Vocabulary builder", "Language", "Give me 10 advanced ways to express “{{input}}”, from formal to casual, with an example sentence each."), // ── Creative ──────────────────────────────────────────────────────── t("Short story", "Creative", "Write a 500-word short story about {{input}} with a strong opening line and an unexpected ending."), t("Character builder", "Creative", "Create a rich character based on: {{input}}. Include appearance, voice, motivation, flaw, secret, and a sample line of dialogue."), t("World-building", "Creative", "Develop a setting from this seed: {{input}}. Cover geography, society, conflict, and 3 story hooks."), t("Poem", "Creative", "Write a poem about {{input}} in the style of {{style}}."), t("Naming ideas", "Creative", "Suggest 15 names for {{input}}: 5 descriptive, 5 evocative, 5 invented words. Note availability concerns for the top 3."), t("Dialogue scene", "Creative", "Write a dialogue-only scene (no narration) where {{input}}. Make each voice distinct."), ] }