-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
146 lines (129 loc) · 4.14 KB
/
utils.js
File metadata and controls
146 lines (129 loc) · 4.14 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import fs from "fs/promises";
import path from "path";
const CONVERSATIONS_FILE = path.join(
process.cwd(),
"data",
"conversations.json",
);
/**
* Conversation structure:
* {
* id: string,
* language: string, // Customer's detected language code
* customerLocale: string, // Full locale identifier
* messages: [
* {
* role: 'customer' | 'agent',
* original_text: string,
* translated_text: string,
* timestamp: Date
* }
* ],
* status: 'active' | 'resolves' | 'escalated',
* createdAt: Date,
* updatedAt: Date,
* closedAt: Date | null,
* closedBy: 'agent' | 'customer' | null
* }
*/
export const conversations = new Map();
export const customerSessions = new Map(); // Track customer -> conversation mapping
export let conversationIdCounter = 1;
/**
* Load conversations from file on server start
*/
export async function loadConversations() {
try {
// Create data directory if it doesn't exist
await fs.mkdir(path.dirname(CONVERSATIONS_FILE), { recursive: true });
const data = await fs.readFile(CONVERSATIONS_FILE, "utf-8");
const saved = JSON.parse(data);
// Restore conversations to Map
saved.conversations.forEach((conv) => {
// Convert date strings back to Date objects
conv.createdAt = new Date(conv.createdAt);
conv.updatedAt = new Date(conv.updatedAt);
conv.messages.forEach((msg) => {
msg.timestamp = new Date(msg.timestamp);
});
conversations.set(conv.id, conv);
});
// Restore customer sessions
Object.entries(saved.customerSessions).forEach(([customerId, convId]) => {
customerSessions.set(customerId, convId);
});
// Restore counter
if (saved.conversationIdCounter) {
conversationIdCounter = saved.conversationIdCounter;
}
console.log(
`[Persistence] Loaded ${conversations.size} conversations from disk`,
);
} catch (error) {
if (error.code === "ENOENT") {
console.log("[Persistence] No saved conversations found, starting fresh");
} else {
console.error("[Persistence] Error loading conversations:", error);
}
}
}
/**
* Save conversations to file
*/
export async function saveConversations() {
try {
const data = {
conversations: Array.from(conversations.values()),
customerSessions: Object.fromEntries(customerSessions),
conversationIdCounter: conversationIdCounter,
savedAt: new Date().toISOString(),
};
await fs.writeFile(CONVERSATIONS_FILE, JSON.stringify(data, null, 2));
console.log(
`[Persistence] Saved ${conversations.size} conversations to disk`,
);
} catch (error) {
console.error("[Persistence] Error saving conversations:", error);
}
}
// Helper function to find or create conversation for a customer
export function findOrCreateConversation(customerId, detectedLocale) {
// Check if customer already has an active conversation
const existingConvId = customerSessions.get(customerId);
if (existingConvId && conversations.has(existingConvId)) {
// Return existing conversation
return conversations.get(existingConvId);
}
// Create new conversation
const id = `conv_${conversationIdCounter++}`;
const languageCode = detectedLocale.split("-")[0]; // e.g., 'en-US' -> 'en'
const conversation = {
id,
customerId,
language: languageCode,
customerLocale: detectedLocale,
messages: [],
status: "active",
createdAt: new Date(),
updatedAt: new Date(),
closedAt: null,
closedBy: null,
};
conversations.set(id, conversation);
customerSessions.set(customerId, id); // Map customer to this conversation
return conversation;
}
/**
* Get the last message preview for inbox display
*/
export function getLastMessagePreview(conversation) {
if (conversation.messages.length === 0) return "";
const lastMessage = conversation.messages[conversation.messages.length - 1];
return lastMessage.translated_text.substring(0, 80);
}
// Agent's language
export let currentAgentLanguage = "en";
export function setAgentLanguage(language) {
currentAgentLanguage = language;
console.log("[Agent Language Updated]", currentAgentLanguage);
}