cencori 1.12.0 → 1.12.1
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/dist/react/index.d.mts +7 -1
- package/dist/react/index.d.ts +7 -1
- package/dist/react/index.js +47 -3
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +47 -3
- package/dist/react/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/react/index.d.mts
CHANGED
|
@@ -92,7 +92,13 @@ interface UseAgentChatOptions {
|
|
|
92
92
|
agent?: string;
|
|
93
93
|
/** A publishable key (cpk_) whose allowed domains include this site. Used with `agent`. */
|
|
94
94
|
publishableKey?: string;
|
|
95
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* An agent built in the Cencori console: its website key (cwk_...), from the
|
|
97
|
+
* agent's Website page. Works on the sites allowed there; conversations show
|
|
98
|
+
* in the agent's inbox like website chats.
|
|
99
|
+
*/
|
|
100
|
+
widgetKey?: string;
|
|
101
|
+
/** Cencori's base URL for `agent` and `widgetKey` modes. Default https://cencori.com. */
|
|
96
102
|
baseUrl?: string;
|
|
97
103
|
/** Extra headers on every request (auth, CSRF...). */
|
|
98
104
|
headers?: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
package/dist/react/index.d.ts
CHANGED
|
@@ -92,7 +92,13 @@ interface UseAgentChatOptions {
|
|
|
92
92
|
agent?: string;
|
|
93
93
|
/** A publishable key (cpk_) whose allowed domains include this site. Used with `agent`. */
|
|
94
94
|
publishableKey?: string;
|
|
95
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* An agent built in the Cencori console: its website key (cwk_...), from the
|
|
97
|
+
* agent's Website page. Works on the sites allowed there; conversations show
|
|
98
|
+
* in the agent's inbox like website chats.
|
|
99
|
+
*/
|
|
100
|
+
widgetKey?: string;
|
|
101
|
+
/** Cencori's base URL for `agent` and `widgetKey` modes. Default https://cencori.com. */
|
|
96
102
|
baseUrl?: string;
|
|
97
103
|
/** Extra headers on every request (auth, CSRF...). */
|
|
98
104
|
headers?: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
package/dist/react/index.js
CHANGED
|
@@ -892,7 +892,19 @@ function useAgentChat(options) {
|
|
|
892
892
|
const extra = typeof o.headers === "function" ? await o.headers() : o.headers ?? {};
|
|
893
893
|
const direct = !o.api;
|
|
894
894
|
let res;
|
|
895
|
-
if (direct) {
|
|
895
|
+
if (direct && o.widgetKey) {
|
|
896
|
+
if (payload.approval) throw new Error("This agent needs approvals: serve it from your backend with `api`.");
|
|
897
|
+
const input = payload.message ?? (payload.action ? formatWidgetAction(payload.action) : "");
|
|
898
|
+
const base = (o.baseUrl ?? "https://cencori.com").replace(/\/$/, "");
|
|
899
|
+
const grant = await widgetGrant(o.fetch ?? fetch, base, o.widgetKey, sessionRef.current, controller.signal);
|
|
900
|
+
if (grant.sessionId !== sessionRef.current) setSessionId(grant.sessionId);
|
|
901
|
+
res = await (o.fetch ?? fetch)(`${base}/v1/sessions/${encodeURIComponent(grant.sessionId)}/turns`, {
|
|
902
|
+
method: "POST",
|
|
903
|
+
headers: { "Content-Type": "application/json", Accept: "text/event-stream", Authorization: `Bearer ${grant.token}` },
|
|
904
|
+
body: JSON.stringify({ input, stream: true, widgets: true }),
|
|
905
|
+
signal: controller.signal
|
|
906
|
+
});
|
|
907
|
+
} else if (direct) {
|
|
896
908
|
if (!o.agent || !o.publishableKey) throw new Error("Pass `api`, or `agent` with a `publishableKey`.");
|
|
897
909
|
if (payload.approval) throw new Error("This agent needs approvals: serve it from your backend with `api`.");
|
|
898
910
|
const input = payload.message ?? (payload.action ? formatWidgetAction(payload.action) : "");
|
|
@@ -1084,6 +1096,8 @@ function sessionEventsToChat() {
|
|
|
1084
1096
|
}
|
|
1085
1097
|
case "turn.completed":
|
|
1086
1098
|
return [{ event: "done", data: { sessionId } }];
|
|
1099
|
+
case "owner.handling":
|
|
1100
|
+
return [{ event: "text", data: { delta: "Someone from the team will reply here." } }, { event: "done", data: { sessionId } }];
|
|
1087
1101
|
case "turn.paused":
|
|
1088
1102
|
return [{ event: "error", data: { message: "This agent needs approvals: serve it from your backend with `api`." } }];
|
|
1089
1103
|
case "turn.failed": {
|
|
@@ -1099,6 +1113,36 @@ function sessionEventsToChat() {
|
|
|
1099
1113
|
}
|
|
1100
1114
|
};
|
|
1101
1115
|
}
|
|
1116
|
+
var grants = /* @__PURE__ */ new Map();
|
|
1117
|
+
function visitorId(widgetKey) {
|
|
1118
|
+
const storageKey = `cencori-visitor:${widgetKey}`;
|
|
1119
|
+
try {
|
|
1120
|
+
const saved = window.localStorage.getItem(storageKey);
|
|
1121
|
+
if (saved && /^v_[A-Za-z0-9-]{8,64}$/.test(saved)) return saved;
|
|
1122
|
+
} catch {
|
|
1123
|
+
}
|
|
1124
|
+
const id = `v_${typeof crypto !== "undefined" && "randomUUID" in crypto ? crypto.randomUUID() : Math.random().toString(36).slice(2) + Date.now().toString(36)}`;
|
|
1125
|
+
try {
|
|
1126
|
+
window.localStorage.setItem(storageKey, id);
|
|
1127
|
+
} catch {
|
|
1128
|
+
}
|
|
1129
|
+
return id;
|
|
1130
|
+
}
|
|
1131
|
+
async function widgetGrant(fetchImpl, base, widgetKey, sessionId, signal) {
|
|
1132
|
+
const cached = sessionId ? grants.get(sessionId) : void 0;
|
|
1133
|
+
if (cached && cached.expiresAt - Date.now() > 6e4) return cached;
|
|
1134
|
+
const res = await fetchImpl(`${base}/api/widget/session`, {
|
|
1135
|
+
method: "POST",
|
|
1136
|
+
headers: { "Content-Type": "application/json" },
|
|
1137
|
+
body: JSON.stringify({ key: widgetKey, visitor_id: visitorId(widgetKey), ...sessionId ? { session_id: sessionId } : {} }),
|
|
1138
|
+
signal
|
|
1139
|
+
});
|
|
1140
|
+
const data = await res.json().catch(() => ({}));
|
|
1141
|
+
if (!res.ok || !data.token || !data.session_id) throw new Error(data.error?.message || `Couldn't start the chat (${res.status}).`);
|
|
1142
|
+
const grant = { token: data.token, sessionId: data.session_id, expiresAt: data.expires_at ? Date.parse(data.expires_at) : Date.now() + 14 * 6e4 };
|
|
1143
|
+
grants.set(grant.sessionId, grant);
|
|
1144
|
+
return grant;
|
|
1145
|
+
}
|
|
1102
1146
|
|
|
1103
1147
|
// src/react/agent-chat/widget.tsx
|
|
1104
1148
|
var import_react3 = require("react");
|
|
@@ -1539,8 +1583,8 @@ function WidgetForm({ node }) {
|
|
|
1539
1583
|
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
1540
1584
|
function AgentChat(props) {
|
|
1541
1585
|
if (props.chat) return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AgentChatView, { ...props, chat: props.chat });
|
|
1542
|
-
if (!props.api && !(props.agent && props.publishableKey)) {
|
|
1543
|
-
throw new Error("<AgentChat> needs an `api` route, or a stored `agent` with a `publishableKey` (or a `chat` from useAgentChat).");
|
|
1586
|
+
if (!props.api && !props.widgetKey && !(props.agent && props.publishableKey)) {
|
|
1587
|
+
throw new Error("<AgentChat> needs an `api` route, a console agent's `widgetKey`, or a stored `agent` with a `publishableKey` (or a `chat` from useAgentChat).");
|
|
1544
1588
|
}
|
|
1545
1589
|
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(OwnedAgentChat, { ...props });
|
|
1546
1590
|
}
|