Spaces:
Running
Running
| /** | |
| * 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)`) | |
| } | |