File size: 2,175 Bytes
ce72224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**

 * Example Plugin: Custom Logging

 * 

 * Dieses Plugin speichert alle Messages in localStorage

 * und kann sie später abrufen

 */

const STORAGE_KEY = 'chatbot_history'
const MAX_HISTORY = 100

function getHistory() {
  try {
    const data = localStorage.getItem(STORAGE_KEY)
    return data ? JSON.parse(data) : []
  } catch (e) {
    console.error('Failed to read history:', e)
    return []
  }
}

function saveHistory(history) {
  try {
    const limited = history.slice(-MAX_HISTORY)
    localStorage.setItem(STORAGE_KEY, JSON.stringify(limited))
  } catch (e) {
    console.error('Failed to save history:', e)
  }
}

export function onPluginInit(context) {
  const history = getHistory()
  context.log(`💾 Local Storage Plugin initialized with ${history.length} messages`)
  
  // API für Plugins bereitstellen
  window.HistoryAPI = {
    getHistory: getHistory,
    clearHistory: () => {
      localStorage.removeItem(STORAGE_KEY)
      context.log('✓ History cleared')
    },
    exportAsJSON: () => {
      const data = {
        exported: new Date(),
        messages: getHistory(),
      }
      const json = JSON.stringify(data, null, 2)
      const blob = new Blob([json], { type: 'application/json' })
      const url = URL.createObjectURL(blob)
      const a = document.createElement('a')
      a.href = url
      a.download = `chatbot-history-${Date.now()}.json`
      a.click()
      context.log('✓ History exported')
    }
  }
}

export function onMessageSent(context, { message, systemPrompt }) {
  const history = getHistory()
  history.push({
    type: 'user',
    content: message,
    timestamp: new Date(),
    systemPrompt,
  })
  saveHistory(history)
  context.log(`💾 Saved to local storage (${history.length} messages)`)
}

export function onResponseReceived(context, { content, stats }) {
  const history = getHistory()
  history.push({
    type: 'assistant',
    content,
    timestamp: new Date(),
    tokens: stats.tokens,
    time: stats.time,
  })
  saveHistory(history)
  context.log(`💾 Response saved (${history.length} total messages)`)
}