@kieksme/csp-plugin-chat 0.9.0 → 0.10.0

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/browser.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  cspPlugin
3
- } from "./chunk-R6G6BYF4.js";
3
+ } from "./chunk-MGUWXMOX.js";
4
4
 
5
5
  // src/tools.ts
6
6
  import {
@@ -182,6 +182,7 @@ async function supportSession(ctx) {
182
182
  },
183
183
  name: "Portal-Nutzer Demo"
184
184
  };
185
+ if (c.auth === "anonymous") return anonymousSession(ctx);
185
186
  if (c.auth === "session") return cookieSession(ctx);
186
187
  const { PublicClientApplication } = await import("@azure/msal-browser");
187
188
  const client = new PublicClientApplication({
@@ -234,12 +235,74 @@ function cookieSession(ctx) {
234
235
  };
235
236
  return current;
236
237
  }
238
+ function anonymousSession(ctx) {
239
+ const key = `csp-support:${ctx.config.apiUrl}`;
240
+ let saved;
241
+ try {
242
+ const value = JSON.parse(window.sessionStorage.getItem(key) ?? "null");
243
+ if (typeof value?.token === "string" && typeof value?.expiresAt === "number" && value.expiresAt > Date.now() && typeof value?.name === "string")
244
+ saved = value;
245
+ } catch {
246
+ }
247
+ let pending;
248
+ const current = {
249
+ name: saved?.name ?? "Besucher",
250
+ async token() {
251
+ if (saved) {
252
+ if (saved.expiresAt <= Date.now())
253
+ throw new Error(
254
+ "Chat-Sitzung abgelaufen. Bitte verbinden Sie den Chat neu."
255
+ );
256
+ return saved.token;
257
+ }
258
+ if (pending) return pending;
259
+ pending = (async () => {
260
+ const response = await (ctx.request ?? fetch)(
261
+ ctx.config.apiUrl + "/api/v1/conversations/session",
262
+ {
263
+ method: "POST",
264
+ credentials: "omit",
265
+ cache: "no-store"
266
+ }
267
+ );
268
+ if (!response.ok)
269
+ throw new Error(
270
+ "Chat derzeit nicht verf\xFCgbar. Bitte versuchen Sie es sp\xE4ter erneut."
271
+ );
272
+ const value = await response.json();
273
+ if (typeof value.token !== "string" || typeof value.expiresAt !== "number" || value.expiresAt <= Date.now() || typeof value.name !== "string")
274
+ throw new Error("Chat derzeit nicht verf\xFCgbar.");
275
+ saved = value;
276
+ current.name = value.name;
277
+ try {
278
+ window.sessionStorage.setItem(key, JSON.stringify(value));
279
+ } catch {
280
+ }
281
+ return value.token;
282
+ })();
283
+ try {
284
+ return await pending;
285
+ } finally {
286
+ pending = void 0;
287
+ }
288
+ },
289
+ async login() {
290
+ try {
291
+ window.sessionStorage.removeItem(key);
292
+ } catch {
293
+ }
294
+ window.location.reload();
295
+ }
296
+ };
297
+ return current;
298
+ }
237
299
 
238
300
  // src/support-chat.tsx
239
301
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
240
302
  function SupportChat(ctx) {
241
303
  const [current, setCurrent] = useState(null), [input, setInput] = useState(""), [error, setError] = useState(""), [busy, setBusy] = useState(false), [ready, setReady] = useState(false), [auth, setAuth] = useState(null);
242
304
  const seq = useRef(0), requestRef = useRef(null);
305
+ const anonymous = ctx.config.chatSupport?.auth === "anonymous";
243
306
  const demo = ctx.config.chatSupport?.mode === "demo";
244
307
  useEffect(() => {
245
308
  let alive = true;
@@ -400,7 +463,7 @@ function SupportChat(ctx) {
400
463
  /* @__PURE__ */ jsx("p", { className: "text-[0.75rem] text-ink mb-3", children: demo ? "Synthetische Support-Demo. Keine \xDCbertragung an einen Support-Channel." : `Ihre Nachrichten und die Antworten werden an den Support in ${ctx.config.chatSupport?.mode === "slack" ? "Slack" : "Microsoft Teams"} weitergegeben.` }),
401
464
  /* @__PURE__ */ jsxs("p", { role: "status", className: "text-[0.8125rem] mb-3", children: [
402
465
  current?.status === "support" ? `${current.support?.name} \xB7 Support` : current?.status === "closed" ? "Gespr\xE4ch abgeschlossen" : "Digitaler Assistent",
403
- auth && ready && !demo ? ` \xB7 Angemeldet als ${auth.name}` : ""
466
+ auth && ready && !demo ? anonymous ? ` \xB7 ${auth.name}` : ` \xB7 Angemeldet als ${auth.name}` : ""
404
467
  ] }),
405
468
  /* @__PURE__ */ jsx(
406
469
  "div",
@@ -441,9 +504,11 @@ function SupportChat(ctx) {
441
504
  className: "bg-accent text-accent-ink rounded-card px-4 py-2",
442
505
  disabled: !auth,
443
506
  onClick: () => void auth?.login().catch(
444
- () => setError("Microsoft-Anmeldung konnte nicht gestartet werden.")
507
+ () => setError(
508
+ anonymous ? "Chat konnte nicht neu verbunden werden." : "Microsoft-Anmeldung konnte nicht gestartet werden."
509
+ )
445
510
  ),
446
- children: ctx.config.chatSupport?.auth === "session" ? "Portal-Anmeldung erneuern" : "Mit Microsoft anmelden"
511
+ children: anonymous ? "Chat neu verbinden" : ctx.config.chatSupport?.auth === "session" ? "Portal-Anmeldung erneuern" : "Mit Microsoft anmelden"
447
512
  }
448
513
  ),
449
514
  ready && /* @__PURE__ */ jsxs("form", { onSubmit: submit, className: "chat-form flex gap-2.5 items-end", children: [
@@ -530,9 +595,11 @@ function SupportChat(ctx) {
530
595
  type: "button",
531
596
  className: "underline mt-3",
532
597
  onClick: () => void auth.login().catch(
533
- () => setError("Microsoft-Anmeldung konnte nicht gestartet werden.")
598
+ () => setError(
599
+ anonymous ? "Chat konnte nicht neu verbunden werden." : "Microsoft-Anmeldung konnte nicht gestartet werden."
600
+ )
534
601
  ),
535
- children: ctx.config.chatSupport?.auth === "session" ? "Portal-Anmeldung erneuern" : "Microsoft-Anmeldung erneuern"
602
+ children: anonymous ? "Chat neu verbinden" : ctx.config.chatSupport?.auth === "session" ? "Portal-Anmeldung erneuern" : "Microsoft-Anmeldung erneuern"
536
603
  }
537
604
  ),
538
605
  error && /* @__PURE__ */ jsx("p", { role: "alert", className: "text-danger text-[0.75rem] mt-3", children: error })
@@ -1,7 +1,7 @@
1
1
  // package.json
2
2
  var cspPlugin = {
3
3
  id: "chat",
4
- sdkVersion: "^0.9.0",
4
+ sdkVersion: "^0.10.0",
5
5
  browser: "./browser",
6
6
  server: "./server"
7
7
  };
package/dist/server.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  cspPlugin
3
- } from "./chunk-R6G6BYF4.js";
3
+ } from "./chunk-MGUWXMOX.js";
4
4
 
5
5
  // src/server.ts
6
6
  import { z as z2 } from "zod";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kieksme/csp-plugin-chat",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "dependencies": {
25
25
  "@azure/msal-browser": "^5.25.0",
26
- "@kieksme/csp-sdk": "^0.9.0",
26
+ "@kieksme/csp-sdk": "^0.10.0",
27
27
  "openai": "^6.3.0",
28
28
  "zod": "^3.25.76"
29
29
  },
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "cspPlugin": {
37
37
  "id": "chat",
38
- "sdkVersion": "^0.9.0",
38
+ "sdkVersion": "^0.10.0",
39
39
  "browser": "./browser",
40
40
  "server": "./server"
41
41
  },