PluginProbe
ZIP AI – AI Website Builder & AI Agent (Beta) / 0.0.4
ZIP AI – AI Website Builder & AI Agent (Beta) v0.0.4
0.0.10 0.0.9 trunk 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8
zip-ai / src / context / utils.js

utils.js in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.4, at src/context/utils.js

54 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 export const STORAGE_KEY_PREFIX = 'zip_ai_chat_';
2 export const PLAN_STATUS_CACHE_MS = 5 * 60 * 1000;
3 export const INITIAL_MESSAGES_COUNT = 50;
4 export const MESSAGES_PER_PAGE = 50;
5
6 /**
7 * Generate a storage key that includes domain and userId for proper isolation.
8 * This ensures different WordPress sites and users have separate localStorage.
9 */
10 export const getStorageKey = (key, config) => {
11 const domain = config?.domain || 'unknown';
12 const userId = config?.userId || 'unknown';
13 return `${STORAGE_KEY_PREFIX}${domain}_${userId}_${key}`;
14 };
15
16 /**
17 * Format messages from API response to internal format
18 * @param {Array} apiMessages - Messages from API
19 * @returns {Array} Formatted messages
20 */
21 export const formatMessagesFromApi = (apiMessages) => {
22 const formattedMessages = [];
23 apiMessages.forEach((msg, index) => {
24 // Handle user_message/assistant_message format from Laravel (primary format)
25 if (msg.user_message) {
26 formattedMessages.push({
27 id: msg.id || Date.now() + index,
28 role: 'user',
29 content: msg.user_message,
30 timestamp: msg.created_at || new Date().toISOString()
31 });
32 } else if (msg.assistant_message || msg.assistant_content) {
33 const content = Array.isArray(msg.assistant_content) && msg.assistant_content.length > 0
34 ? msg.assistant_content
35 : msg.assistant_message;
36 formattedMessages.push({
37 id: msg.id || Date.now() + index + 0.5,
38 role: 'assistant',
39 content,
40 timestamp: msg.created_at || new Date().toISOString()
41 });
42 } else if (msg.role && msg.content) {
43 // Fallback: support role/content format for backwards compatibility
44 formattedMessages.push({
45 id: msg.id || Date.now() + index,
46 role: msg.role,
47 content: msg.content,
48 timestamp: msg.created_at || new Date().toISOString()
49 });
50 }
51 });
52 return formattedMessages;
53 };
54