File size: 1,980 Bytes
78d0e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * Utility functions for the Iko Ikang assistant
 */

/**
 * Enhances Flameborn references in text
 */
export const enhanceFlambornReferences = (text: string): string => {
  return text.replace(/Flameborn/gi, "🔥 Flameborn")
}

/**
 * Detects repetition in AI responses
 */
export const detectRepetition = (prevMessages: { role: string; content: string }[], newResponse: string): boolean => {
  const lastAssistantMessage = [...prevMessages].reverse().find((msg) => msg.role === "assistant")

  if (!lastAssistantMessage) return false

  // Check for substantial overlap
  const similarity = getSimilarity(lastAssistantMessage.content, newResponse)
  return similarity > 0.7 // 70% similarity threshold
}

/**
 * Calculate text similarity (simple version)
 */
const getSimilarity = (str1: string, str2: string): number => {
  const words1 = new Set(str1.toLowerCase().split(/\s+/))
  const words2 = new Set(str2.toLowerCase().split(/\s+/))

  // Calculate Jaccard similarity
  const intersection = new Set([...words1].filter((x) => words2.has(x)))
  const union = new Set([...words1, ...words2])

  return intersection.size / union.size
}

/**
 * Inject African wisdom quotes randomly
 */
export const injectAfricanWisdom = (text: string): string => {
  // Only inject wisdom occasionally
  if (Math.random() > 0.25) return text

  const wisdomQuotes = [
    "As our ancestors say: 'If you want to go fast, go alone. If you want to go far, go together.'",
    "In the spirit of Ubuntu: 'I am because we are.'",
    "Remember the wisdom: 'Until the lion tells his side of the story, the tale of the hunt will always glorify the hunter.'",
    "As we say in Africa: 'When spider webs unite, they can tie up a lion.'",
    "The elders teach us: 'Cross the river in a crowd and the crocodile won't eat you.'",
  ]

  const selectedQuote = wisdomQuotes[Math.floor(Math.random() * wisdomQuotes.length)]

  // Add the quote as a postscript
  return `${text}\n\n${selectedQuote}`
}