tevrocsdk 0.1.72 → 0.1.73

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/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,11 +40842,14 @@ 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",
40836
40847
  actions: [
40837
- { id: "accept", title: "Accept" },
40838
- { id: "reject", title: "Reject" },
40848
+ ...request.kind === "agent_message" ? [] : [
40849
+ { id: "accept", title: "Accept" },
40850
+ { id: "reject", title: "Reject" }
40851
+ ],
40852
+ { id: "open", title: "Open" },
40839
40853
  { id: "reply", title: "Reply", input: true }
40840
40854
  ]
40841
40855
  });
@@ -40850,6 +40864,7 @@ function createAgentNotificationService(client) {
40850
40864
  seen.set(sessionId, fingerprint);
40851
40865
  const request = {
40852
40866
  sessionId,
40867
+ kind: "agent_request",
40853
40868
  title: `Support request from ${session.visitor_name || "a visitor"}`,
40854
40869
  body: latest?.content || "A visitor is requesting a human agent.",
40855
40870
  latestMessage: latest,
@@ -40858,9 +40873,33 @@ function createAgentNotificationService(client) {
40858
40873
  await showNative(request);
40859
40874
  notify({ type: "request", ...request });
40860
40875
  }
40876
+ if (client.agentIdentity) {
40877
+ const active = toList(await client.entities.ChatSession.query({ filter: { appId, status: "agent_active", agent_email: client.agentIdentity }, limit: 100 }));
40878
+ for (const session of active) {
40879
+ const sessionId = getSessionId(session);
40880
+ if (!sessionId) continue;
40881
+ const messages2 = await getMessages(sessionId, appId);
40882
+ const latestVisitor = latestByTime(messages2.filter((message2) => message2?.role === "visitor" || message2?.role === "user"));
40883
+ if (!latestVisitor) continue;
40884
+ const fingerprint = `message:${latestVisitor.id || latestVisitor.createdAt || latestVisitor.content}`;
40885
+ if (seen.get(`message:${sessionId}`) === fingerprint) continue;
40886
+ seen.set(`message:${sessionId}`, fingerprint);
40887
+ const request = {
40888
+ sessionId,
40889
+ kind: "agent_message",
40890
+ title: `New message from ${session.visitor_name || "visitor"}`,
40891
+ body: latestVisitor.content || "The visitor sent an attachment.",
40892
+ latestMessage: latestVisitor,
40893
+ session
40894
+ };
40895
+ await showNative(request);
40896
+ notify({ type: "message", ...request });
40897
+ }
40898
+ }
40861
40899
  };
40862
40900
  const start = async ({ pollMs = 8e3, agentIdentity, ...options } = {}) => {
40863
40901
  if (agentIdentity) client.agentIdentity = agentIdentity;
40902
+ if (options.appId) client.appId = options.appId;
40864
40903
  stopped = false;
40865
40904
  await check(options);
40866
40905
  clearInterval(timer);
@@ -40876,7 +40915,9 @@ function createAgentNotificationService(client) {
40876
40915
  return () => listeners2.delete(listener);
40877
40916
  };
40878
40917
  client.notifications.onAction(({ action, data, reply: text2 }) => {
40879
- if (data?.sessionId) dispatchAction({ action, sessionId: data.sessionId, reply: text2 }).catch((error) => notify({ type: "error", error }));
40918
+ if (data?.sessionId && String(data.kind || "").startsWith("agent_")) {
40919
+ dispatchAction({ action, sessionId: data.sessionId, reply: text2 }).catch((error) => notify({ type: "error", error }));
40920
+ }
40880
40921
  });
40881
40922
  return { start, stop, check, onEvent, accept, reject, reply, dispatchAction };
40882
40923
  }
@@ -41005,6 +41046,47 @@ function createNotificationService() {
41005
41046
  return { permission, requestPermission, show, schedule, cancel, cancelAll, onAction };
41006
41047
  }
41007
41048
 
41049
+ // src/services/chatNotifications.js
41050
+ function createChatNotificationService(client) {
41051
+ const listeners2 = /* @__PURE__ */ new Set();
41052
+ const emit2 = (event) => listeners2.forEach((listener) => listener(event));
41053
+ const showMessage = async ({ sessionId, title = "New chat message", body = "", data = {} } = {}) => {
41054
+ if (!sessionId) return null;
41055
+ return client.notifications.show({
41056
+ id: `chat-message-${sessionId}-${Date.now()}`,
41057
+ title,
41058
+ body,
41059
+ data: { ...data, sessionId, kind: "chat_message" },
41060
+ channel: "chat_messages",
41061
+ actions: [
41062
+ { id: "open", title: "Open chat" },
41063
+ { id: "reply", title: "Reply", input: true }
41064
+ ]
41065
+ });
41066
+ };
41067
+ const showStatus = async ({ sessionId, title = "Chat update", body = "", data = {} } = {}) => {
41068
+ if (!sessionId) return null;
41069
+ return client.notifications.show({
41070
+ id: `chat-status-${sessionId}-${Date.now()}`,
41071
+ title,
41072
+ body,
41073
+ data: { ...data, sessionId, kind: "chat_status" },
41074
+ channel: "chat_messages",
41075
+ actions: [{ id: "open", title: "Open chat" }]
41076
+ });
41077
+ };
41078
+ const onEvent = (listener) => {
41079
+ listeners2.add(listener);
41080
+ return () => listeners2.delete(listener);
41081
+ };
41082
+ client.notifications.onAction(({ action, data, reply }) => {
41083
+ if (data?.sessionId && ["chat_message", "chat_status"].includes(data.kind)) {
41084
+ emit2({ type: action === "reply" ? "reply" : action, action, sessionId: data.sessionId, reply: reply || "", data });
41085
+ }
41086
+ });
41087
+ return { showMessage, showStatus, onEvent };
41088
+ }
41089
+
41008
41090
  // src/index.js
41009
41091
  init_usePlatform();
41010
41092
 
@@ -42126,6 +42208,7 @@ var TevrocClient = class {
42126
42208
  this.commerce = createCommerceApi(this);
42127
42209
  this.realtime = createRealtimeApi(this);
42128
42210
  this.notifications = createNotificationService();
42211
+ this.chatNotifications = createChatNotificationService(this);
42129
42212
  this.agentNotifications = createAgentNotificationService(this);
42130
42213
  this.shiplogic = createShiplogicApi(this);
42131
42214
  this.platform = createPlatformApi(this);