Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/Core/Models/PromptTemplate.swift3// Description: `{{variable}}` placeholders of the prompt library: extraction in order of appearance and substitution, and the parsing of the composer's slash / mention triggers.4import Foundation56public enum PromptTemplate {7 private static let pattern = try? NSRegularExpression(pattern: #"\{\{\s*([^{}\n]{1,60}?)\s*\}\}"#)89 /// Distinct variable names, in order of first appearance (`{{ sujet }}` and `{{sujet}}` are the same variable).10 public static func variables(in body: String) -> [String] {11 guard let pattern else { return [] }12 var seen = Set<String>(); var out: [String] = []13 let ns = body as NSString14 for m in pattern.matches(in: body, range: NSRange(location: 0, length: ns.length)) {15 let name = ns.substring(with: m.range(at: 1)).trimmingCharacters(in: .whitespaces)16 if !name.isEmpty, seen.insert(name).inserted { out.append(name) }17 }18 return out19 }2021 /// Replaces every placeholder that has a value; a variable left without value keeps its placeholder (nothing is silently dropped).22 public static func fill(_ body: String, values: [String: String]) -> String {23 guard let pattern else { return body }24 let ns = body as NSString; var out = ""; var cursor = 025 for m in pattern.matches(in: body, range: NSRange(location: 0, length: ns.length)) {26 out += ns.substring(with: NSRange(location: cursor, length: m.range.location - cursor))27 let name = ns.substring(with: m.range(at: 1)).trimmingCharacters(in: .whitespaces)28 if let v = values[name], !v.isEmpty { out += v } else { out += ns.substring(with: m.range) }29 cursor = m.range.location + m.range.length30 }31 out += ns.substring(from: cursor)32 return out33 }34}3536/// What the composer text asks for besides being a message: `/…` opens the commands (prompts, presets, model, tools),37/// a trailing `@…` word picks a model.38public enum ComposerTrigger: Sendable, Equatable {39 case none40 /// The whole draft is a command line: `/` followed by the filter text.41 case command(query: String)42 /// The draft ends with `@query` (start of text or after a space); `range` is the UTF-16 range of the `@word` to remove.43 case mention(query: String, location: Int, length: Int)4445 public init(draft: String) {46 if draft.hasPrefix("/"), !draft.contains("\n"), draft.count <= 80 { self = .command(query: String(draft.dropFirst())); return }47 let ns = draft as NSString48 var i = ns.length - 1; var word = 049 while i >= 0 {50 let ch = ns.character(at: i)51 if ch == 0x40 /* @ */ {52 let before: unichar? = i > 0 ? ns.character(at: i - 1) : nil53 if before == nil || before == 0x20 || before == 0x0a {54 self = .mention(query: ns.substring(with: NSRange(location: i + 1, length: word)), location: i, length: word + 1); return55 }56 break57 }58 if ch == 0x20 || ch == 0x0a || word >= 40 { break }59 word += 1; i -= 160 }61 self = .none62 }63 public var isActive: Bool { self != .none }64}65