tevrocsdk 0.1.72 → 0.1.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/tevroc-sdk.cjs.js +96 -7
- package/tevroc-sdk.cjs.js.map +4 -4
- package/tevroc-sdk.esm.js +96 -7
- package/tevroc-sdk.esm.js.map +4 -4
package/tevroc-sdk.esm.js
CHANGED
|
@@ -40785,6 +40785,15 @@ function createAgentNotificationService(client) {
|
|
|
40785
40785
|
connection.publish({ type: "chat_message", sessionId, session_id: sessionId, message: payload, payload });
|
|
40786
40786
|
setTimeout(() => connection.close(), 1e3);
|
|
40787
40787
|
};
|
|
40788
|
+
const publishStatus = (sessionId, status, session) => publish(sessionId, {
|
|
40789
|
+
id: `status_${status}_${Date.now()}`,
|
|
40790
|
+
type: "chat_status",
|
|
40791
|
+
status,
|
|
40792
|
+
sessionId,
|
|
40793
|
+
session_id: sessionId,
|
|
40794
|
+
visitor_name: session?.visitor_name || "",
|
|
40795
|
+
createdAt: Date.now()
|
|
40796
|
+
});
|
|
40788
40797
|
const accept = async (session) => {
|
|
40789
40798
|
const sessionId = getSessionId(session);
|
|
40790
40799
|
await client.entities.ChatSession.update(session.id, {
|
|
@@ -40792,11 +40801,13 @@ function createAgentNotificationService(client) {
|
|
|
40792
40801
|
agent_email: client.agentIdentity || "agent",
|
|
40793
40802
|
updatedAt: Date.now()
|
|
40794
40803
|
});
|
|
40804
|
+
publishStatus(sessionId, "agent_active", session);
|
|
40795
40805
|
notify({ type: "accepted", sessionId, session });
|
|
40796
40806
|
};
|
|
40797
40807
|
const reject = async (session) => {
|
|
40798
40808
|
const sessionId = getSessionId(session);
|
|
40799
40809
|
await client.entities.ChatSession.update(session.id, { status: "agent_rejected", updatedAt: Date.now() });
|
|
40810
|
+
publishStatus(sessionId, "agent_rejected", session);
|
|
40800
40811
|
notify({ type: "rejected", sessionId, session });
|
|
40801
40812
|
};
|
|
40802
40813
|
const reply = async (session, content) => {
|
|
@@ -40831,15 +40842,19 @@ function createAgentNotificationService(client) {
|
|
|
40831
40842
|
id: `agent-request-${request.sessionId}`,
|
|
40832
40843
|
title: request.title,
|
|
40833
40844
|
body: request.body,
|
|
40834
|
-
data: { sessionId: request.sessionId },
|
|
40845
|
+
data: { sessionId: request.sessionId, kind: request.kind || "agent_request" },
|
|
40835
40846
|
channel: "agent_requests",
|
|
40847
|
+
style: request.style,
|
|
40836
40848
|
actions: [
|
|
40837
|
-
|
|
40838
|
-
|
|
40849
|
+
...request.kind === "agent_message" ? [] : [
|
|
40850
|
+
{ id: "accept", title: "Accept" },
|
|
40851
|
+
{ id: "reject", title: "Reject" }
|
|
40852
|
+
],
|
|
40853
|
+
{ id: "open", title: "Open" },
|
|
40839
40854
|
{ id: "reply", title: "Reply", input: true }
|
|
40840
40855
|
]
|
|
40841
40856
|
});
|
|
40842
|
-
const check = async ({ appId = client.appId } = {}) => {
|
|
40857
|
+
const check = async ({ appId = client.appId, notificationStyle } = {}) => {
|
|
40843
40858
|
const sessions = toList(await client.entities.ChatSession.query({ filter: { appId, status: "escalated" }, limit: 100 }));
|
|
40844
40859
|
for (const session of sessions) {
|
|
40845
40860
|
const sessionId = getSessionId(session);
|
|
@@ -40850,17 +40865,44 @@ function createAgentNotificationService(client) {
|
|
|
40850
40865
|
seen.set(sessionId, fingerprint);
|
|
40851
40866
|
const request = {
|
|
40852
40867
|
sessionId,
|
|
40868
|
+
kind: "agent_request",
|
|
40853
40869
|
title: `Support request from ${session.visitor_name || "a visitor"}`,
|
|
40854
40870
|
body: latest?.content || "A visitor is requesting a human agent.",
|
|
40855
40871
|
latestMessage: latest,
|
|
40856
|
-
session
|
|
40872
|
+
session,
|
|
40873
|
+
style: notificationStyle
|
|
40857
40874
|
};
|
|
40858
40875
|
await showNative(request);
|
|
40859
40876
|
notify({ type: "request", ...request });
|
|
40860
40877
|
}
|
|
40878
|
+
if (client.agentIdentity) {
|
|
40879
|
+
const active = toList(await client.entities.ChatSession.query({ filter: { appId, status: "agent_active", agent_email: client.agentIdentity }, limit: 100 }));
|
|
40880
|
+
for (const session of active) {
|
|
40881
|
+
const sessionId = getSessionId(session);
|
|
40882
|
+
if (!sessionId) continue;
|
|
40883
|
+
const messages2 = await getMessages(sessionId, appId);
|
|
40884
|
+
const latestVisitor = latestByTime(messages2.filter((message2) => message2?.role === "visitor" || message2?.role === "user"));
|
|
40885
|
+
if (!latestVisitor) continue;
|
|
40886
|
+
const fingerprint = `message:${latestVisitor.id || latestVisitor.createdAt || latestVisitor.content}`;
|
|
40887
|
+
if (seen.get(`message:${sessionId}`) === fingerprint) continue;
|
|
40888
|
+
seen.set(`message:${sessionId}`, fingerprint);
|
|
40889
|
+
const request = {
|
|
40890
|
+
sessionId,
|
|
40891
|
+
kind: "agent_message",
|
|
40892
|
+
title: `New message from ${session.visitor_name || "visitor"}`,
|
|
40893
|
+
body: latestVisitor.content || "The visitor sent an attachment.",
|
|
40894
|
+
latestMessage: latestVisitor,
|
|
40895
|
+
session,
|
|
40896
|
+
style: notificationStyle
|
|
40897
|
+
};
|
|
40898
|
+
await showNative(request);
|
|
40899
|
+
notify({ type: "message", ...request });
|
|
40900
|
+
}
|
|
40901
|
+
}
|
|
40861
40902
|
};
|
|
40862
40903
|
const start = async ({ pollMs = 8e3, agentIdentity, ...options } = {}) => {
|
|
40863
40904
|
if (agentIdentity) client.agentIdentity = agentIdentity;
|
|
40905
|
+
if (options.appId) client.appId = options.appId;
|
|
40864
40906
|
stopped = false;
|
|
40865
40907
|
await check(options);
|
|
40866
40908
|
clearInterval(timer);
|
|
@@ -40876,7 +40918,9 @@ function createAgentNotificationService(client) {
|
|
|
40876
40918
|
return () => listeners2.delete(listener);
|
|
40877
40919
|
};
|
|
40878
40920
|
client.notifications.onAction(({ action, data, reply: text2 }) => {
|
|
40879
|
-
if (data?.sessionId
|
|
40921
|
+
if (data?.sessionId && String(data.kind || "").startsWith("agent_")) {
|
|
40922
|
+
dispatchAction({ action, sessionId: data.sessionId, reply: text2 }).catch((error) => notify({ type: "error", error }));
|
|
40923
|
+
}
|
|
40880
40924
|
});
|
|
40881
40925
|
return { start, stop, check, onEvent, accept, reject, reply, dispatchAction };
|
|
40882
40926
|
}
|
|
@@ -40949,7 +40993,8 @@ function createNotificationService() {
|
|
|
40949
40993
|
data: options.data || {},
|
|
40950
40994
|
actions: options.actions || [],
|
|
40951
40995
|
silent: Boolean(options.silent),
|
|
40952
|
-
channel: options.channel || "general"
|
|
40996
|
+
channel: options.channel || "general",
|
|
40997
|
+
style: options.style || {}
|
|
40953
40998
|
};
|
|
40954
40999
|
const plugin = appPlugin();
|
|
40955
41000
|
if (plugin?.show) {
|
|
@@ -41005,6 +41050,49 @@ function createNotificationService() {
|
|
|
41005
41050
|
return { permission, requestPermission, show, schedule, cancel, cancelAll, onAction };
|
|
41006
41051
|
}
|
|
41007
41052
|
|
|
41053
|
+
// src/services/chatNotifications.js
|
|
41054
|
+
function createChatNotificationService(client) {
|
|
41055
|
+
const listeners2 = /* @__PURE__ */ new Set();
|
|
41056
|
+
const emit2 = (event) => listeners2.forEach((listener) => listener(event));
|
|
41057
|
+
const showMessage = async ({ sessionId, title = "New chat message", body = "", data = {}, style } = {}) => {
|
|
41058
|
+
if (!sessionId) return null;
|
|
41059
|
+
return client.notifications.show({
|
|
41060
|
+
id: `chat-message-${sessionId}-${Date.now()}`,
|
|
41061
|
+
title,
|
|
41062
|
+
body,
|
|
41063
|
+
data: { ...data, sessionId, kind: "chat_message" },
|
|
41064
|
+
channel: "chat_messages",
|
|
41065
|
+
style,
|
|
41066
|
+
actions: [
|
|
41067
|
+
{ id: "open", title: "Open chat" },
|
|
41068
|
+
{ id: "reply", title: "Reply", input: true }
|
|
41069
|
+
]
|
|
41070
|
+
});
|
|
41071
|
+
};
|
|
41072
|
+
const showStatus = async ({ sessionId, title = "Chat update", body = "", data = {}, style } = {}) => {
|
|
41073
|
+
if (!sessionId) return null;
|
|
41074
|
+
return client.notifications.show({
|
|
41075
|
+
id: `chat-status-${sessionId}-${Date.now()}`,
|
|
41076
|
+
title,
|
|
41077
|
+
body,
|
|
41078
|
+
data: { ...data, sessionId, kind: "chat_status" },
|
|
41079
|
+
channel: "chat_messages",
|
|
41080
|
+
style,
|
|
41081
|
+
actions: [{ id: "open", title: "Open chat" }]
|
|
41082
|
+
});
|
|
41083
|
+
};
|
|
41084
|
+
const onEvent = (listener) => {
|
|
41085
|
+
listeners2.add(listener);
|
|
41086
|
+
return () => listeners2.delete(listener);
|
|
41087
|
+
};
|
|
41088
|
+
client.notifications.onAction(({ action, data, reply }) => {
|
|
41089
|
+
if (data?.sessionId && ["chat_message", "chat_status"].includes(data.kind)) {
|
|
41090
|
+
emit2({ type: action === "reply" ? "reply" : action, action, sessionId: data.sessionId, reply: reply || "", data });
|
|
41091
|
+
}
|
|
41092
|
+
});
|
|
41093
|
+
return { showMessage, showStatus, onEvent };
|
|
41094
|
+
}
|
|
41095
|
+
|
|
41008
41096
|
// src/index.js
|
|
41009
41097
|
init_usePlatform();
|
|
41010
41098
|
|
|
@@ -42126,6 +42214,7 @@ var TevrocClient = class {
|
|
|
42126
42214
|
this.commerce = createCommerceApi(this);
|
|
42127
42215
|
this.realtime = createRealtimeApi(this);
|
|
42128
42216
|
this.notifications = createNotificationService();
|
|
42217
|
+
this.chatNotifications = createChatNotificationService(this);
|
|
42129
42218
|
this.agentNotifications = createAgentNotificationService(this);
|
|
42130
42219
|
this.shiplogic = createShiplogicApi(this);
|
|
42131
42220
|
this.platform = createPlatformApi(this);
|