206 lines
6.8 KiB
Swift
206 lines
6.8 KiB
Swift
import Foundation
|
|
|
|
enum SybilInlineMention {
|
|
private struct Fence {
|
|
var marker: Character
|
|
var length: Int
|
|
}
|
|
|
|
static func hasExplicitSybilMention(in text: String) -> Bool {
|
|
let lines = text.components(separatedBy: .newlines)
|
|
var fence: Fence?
|
|
var inBlockquote = false
|
|
var paragraph: [String] = []
|
|
|
|
func paragraphHasMention() -> Bool {
|
|
hasMentionOutsideInlineCode(in: paragraph.joined(separator: "\n"))
|
|
}
|
|
|
|
func isBlank(_ line: String) -> Bool {
|
|
line.allSatisfy(\.isWhitespace)
|
|
}
|
|
|
|
for line in lines {
|
|
if let currentFence = fence {
|
|
if let closingFence = fenceMarker(in: line, opening: false),
|
|
closingFence.marker == currentFence.marker,
|
|
closingFence.length >= currentFence.length {
|
|
fence = nil
|
|
}
|
|
continue
|
|
}
|
|
|
|
if let openingFence = fenceMarker(in: line, opening: true) {
|
|
if paragraphHasMention() { return true }
|
|
paragraph.removeAll(keepingCapacity: true)
|
|
fence = openingFence
|
|
continue
|
|
}
|
|
|
|
if isBlank(line) {
|
|
if paragraphHasMention() { return true }
|
|
paragraph.removeAll(keepingCapacity: true)
|
|
inBlockquote = false
|
|
continue
|
|
}
|
|
|
|
if isBlockquoteLine(line) {
|
|
if paragraphHasMention() { return true }
|
|
paragraph.removeAll(keepingCapacity: true)
|
|
inBlockquote = true
|
|
continue
|
|
}
|
|
|
|
if inBlockquote || isIndentedCodeLine(line) {
|
|
if !paragraph.isEmpty {
|
|
if paragraphHasMention() { return true }
|
|
paragraph.removeAll(keepingCapacity: true)
|
|
}
|
|
continue
|
|
}
|
|
|
|
paragraph.append(line)
|
|
}
|
|
|
|
return paragraphHasMention()
|
|
}
|
|
|
|
private static func fenceMarker(in line: String, opening: Bool) -> Fence? {
|
|
let characters = Array(line)
|
|
var index = 0
|
|
while index < characters.count, characters[index] == " ", index < 4 {
|
|
index += 1
|
|
}
|
|
guard index <= 3, index < characters.count else { return nil }
|
|
|
|
let marker = characters[index]
|
|
guard marker == "`" || marker == "~" else { return nil }
|
|
let start = index
|
|
while index < characters.count, characters[index] == marker {
|
|
index += 1
|
|
}
|
|
let length = index - start
|
|
guard length >= 3 else { return nil }
|
|
|
|
if opening {
|
|
if marker == "`", characters[index...].contains("`") { return nil }
|
|
} else if !characters[index...].allSatisfy(\.isWhitespace) {
|
|
return nil
|
|
}
|
|
|
|
return Fence(marker: marker, length: length)
|
|
}
|
|
|
|
private static func isBlockquoteLine(_ line: String) -> Bool {
|
|
let characters = Array(line)
|
|
var index = 0
|
|
while index < characters.count, characters[index] == " ", index < 4 {
|
|
index += 1
|
|
}
|
|
return index <= 3 && index < characters.count && characters[index] == ">"
|
|
}
|
|
|
|
private static func isIndentedCodeLine(_ line: String) -> Bool {
|
|
let characters = Array(line)
|
|
var index = 0
|
|
while index < characters.count, characters[index] == " ", index < 4 {
|
|
index += 1
|
|
}
|
|
return index == 4 || (index < characters.count && characters[index] == "\t")
|
|
}
|
|
|
|
private static func hasMentionOutsideInlineCode(in text: String) -> Bool {
|
|
let visible = strippingInlineCode(from: text)
|
|
let characters = Array(visible)
|
|
guard characters.count >= 6 else { return false }
|
|
|
|
for index in 0...(characters.count - 6) where characters[index] == "@" {
|
|
let token = String(characters[index..<(index + 6)])
|
|
guard token.caseInsensitiveCompare("@sybil") == .orderedSame else { continue }
|
|
if index > 0, isEmailLocalPartCharacter(characters[index - 1]) { continue }
|
|
if index + 6 < characters.count, isWordCharacter(characters[index + 6]) { continue }
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
private static func strippingInlineCode(from text: String) -> String {
|
|
let characters = Array(text)
|
|
var visible: [Character] = []
|
|
var index = 0
|
|
|
|
while index < characters.count {
|
|
guard characters[index] == "`" else {
|
|
visible.append(characters[index])
|
|
index += 1
|
|
continue
|
|
}
|
|
|
|
let start = index
|
|
while index < characters.count, characters[index] == "`" {
|
|
index += 1
|
|
}
|
|
let runLength = index - start
|
|
if hasEscapedBacktick(before: start, in: characters) {
|
|
visible.append(contentsOf: characters[start..<index])
|
|
continue
|
|
}
|
|
|
|
if let closingStart = matchingBacktickRun(after: index, length: runLength, in: characters) {
|
|
index = closingStart + runLength
|
|
} else {
|
|
visible.append(contentsOf: characters[start..<index])
|
|
}
|
|
}
|
|
|
|
return String(visible)
|
|
}
|
|
|
|
private static func matchingBacktickRun(after start: Int, length: Int, in characters: [Character]) -> Int? {
|
|
var index = start
|
|
while index < characters.count {
|
|
guard characters[index] == "`" else {
|
|
index += 1
|
|
continue
|
|
}
|
|
|
|
let runStart = index
|
|
while index < characters.count, characters[index] == "`" {
|
|
index += 1
|
|
}
|
|
if index - runStart == length { return runStart }
|
|
}
|
|
return nil
|
|
}
|
|
|
|
private static func hasEscapedBacktick(before index: Int, in characters: [Character]) -> Bool {
|
|
guard index > 0 else { return false }
|
|
var cursor = index
|
|
while cursor > 0, characters[cursor - 1] == "\\" {
|
|
cursor -= 1
|
|
}
|
|
return (index - cursor).isMultiple(of: 2) == false
|
|
}
|
|
|
|
private static func isEmailLocalPartCharacter(_ character: Character) -> Bool {
|
|
character.isLetter || character.isNumber || "!#$%&'*+-/=?^_`{|}~.".contains(character)
|
|
}
|
|
|
|
private static func isWordCharacter(_ character: Character) -> Bool {
|
|
character.isLetter || character.isNumber || character == "_"
|
|
}
|
|
}
|
|
|
|
extension Message {
|
|
var isAgentAside: Bool {
|
|
role == .assistant && metadata?.objectValue?["agentAside"]?.boolValue == true
|
|
}
|
|
|
|
mutating func setAgentAside(_ value: Bool) {
|
|
var object = metadata?.objectValue ?? [:]
|
|
object["agentAside"] = .bool(value)
|
|
metadata = .object(object)
|
|
}
|
|
}
|