@tellyouai/plugin 0.1.0 → 0.1.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/README.md CHANGED
@@ -9,7 +9,7 @@ it can be used from React, Vue, Svelte, or a plain browser application.
9
9
  Pin an exact release when application behavior must remain stable:
10
10
 
11
11
  ```sh
12
- npm install --save-exact @tellyouai/plugin@0.1.0
12
+ npm install --save-exact @tellyouai/plugin@0.1.1
13
13
  ```
14
14
 
15
15
  Initialize the plugin after the page has loaded:
@@ -22,6 +22,7 @@ const plugin = await initialize({
22
22
  });
23
23
 
24
24
  plugin.toggle({ open: true });
25
+ // Call plugin.destroy() when the host no longer needs the plugin.
25
26
  ```
26
27
 
27
28
  For an inline or otherwise custom target, create the element before
@@ -48,6 +49,29 @@ Pass an array to initialize multiple instances. The returned controller also
48
49
  provides `load`, `goTo`, `setConsent`, and `update` methods. Importing the
49
50
  package is safe in server-side builds, but `initialize` must run in a browser.
50
51
 
52
+ In React, connect the controller to the component lifecycle:
53
+
54
+ ```tsx
55
+ useEffect(() => {
56
+ let plugin: Awaited<ReturnType<typeof initialize>> | undefined;
57
+ let cancelled = false;
58
+
59
+ void initialize({ entityId: "YOUR_ENTITY_ID" }).then((controller) => {
60
+ if (cancelled) controller.destroy();
61
+ else plugin = controller;
62
+ });
63
+
64
+ return () => {
65
+ cancelled = true;
66
+ plugin?.destroy();
67
+ };
68
+ }, []);
69
+ ```
70
+
71
+ `destroy()` unmounts every instance owned by that controller. Pass a target ID
72
+ to destroy only that instance. Containers created automatically by the plugin
73
+ are removed; host-provided target elements remain in the document.
74
+
51
75
  ## CDN script
52
76
 
53
77
  Existing script integrations remain supported:
@@ -85437,35 +85437,44 @@ var J9, Y9, Wxe, Gxe = o((() => {
85437
85437
  return n;
85438
85438
  }, $9 = () => {
85439
85439
  Qxe();
85440
- let e = window, t = e[Zxe] ||= /* @__PURE__ */ new Map(), n = {
85440
+ let e = window, t = Symbol("tellyouai.plugin.controller"), n = /* @__PURE__ */ new Set(), r = e[Zxe] ||= /* @__PURE__ */ new Map(), i = {
85441
85441
  load: () => {
85442
- let n = e.tellyouai, r = Array.isArray(n) ? n : [n];
85443
- r.length === 0 && r.push(void 0), console.log("Tellyou AI: Initializing plugin instances:", r), r.forEach((e, n) => {
85444
- let r = e?.target;
85442
+ let i = e.tellyouai, a = Array.isArray(i) ? i : [i];
85443
+ a.length === 0 && a.push(void 0), console.log("Tellyou AI: Initializing plugin instances:", a), a.forEach((e, i) => {
85444
+ let a = e?.target;
85445
85445
  try {
85446
- let e = "tellyouai-plugin-" + n, i = !!r && r !== e, a = r || e, o = document.getElementById(a);
85447
- if (!o && !i && (o = document.createElement("div"), o.id = a, document.body.appendChild(o)), !o) throw Error("Tellyou AI: Failed to find target element for instance " + n + ", target: " + a + ", isCustomTarget: " + i);
85448
- let s = t.get(a);
85449
- if (s) {
85450
- if (s.elem === o && o.isConnected) return;
85451
- s.root.unmount(), t.delete(a);
85446
+ let e = "tellyouai-plugin-" + i, o = !!a && a !== e, s = a || e, c = document.getElementById(s), l = !1;
85447
+ if (!c && !o && (c = document.createElement("div"), c.id = s, document.body.appendChild(c), l = !0), !c) throw Error("Tellyou AI: Failed to find target element for instance " + i + ", target: " + s + ", isCustomTarget: " + o);
85448
+ let u = r.get(s);
85449
+ if (u) {
85450
+ if (u.elem === c && c.isConnected) return;
85451
+ u.root.unmount(), r.delete(s);
85452
85452
  }
85453
- o.innerHTML = "";
85454
- let c = (0, Xxe.createRoot)(o);
85455
- t.set(a, {
85456
- root: c,
85457
- elem: o
85458
- }), c.render(/* @__PURE__ */ (0, Z9.jsx)(X9.StrictMode, { children: /* @__PURE__ */ (0, Z9.jsx)(uL, { children: /* @__PURE__ */ (0, Z9.jsxs)(sn, {
85459
- instanceIndex: n,
85460
- target: a,
85461
- isCustomTarget: i,
85462
- children: [/* @__PURE__ */ (0, Z9.jsx)(qxe, { id: a }), /* @__PURE__ */ (0, Z9.jsx)(Hxe, {})]
85453
+ c.innerHTML = "";
85454
+ let d = (0, Xxe.createRoot)(c);
85455
+ r.set(s, {
85456
+ root: d,
85457
+ elem: c,
85458
+ owner: t,
85459
+ createdElement: l
85460
+ }), n.add(s), d.render(/* @__PURE__ */ (0, Z9.jsx)(X9.StrictMode, { children: /* @__PURE__ */ (0, Z9.jsx)(uL, { children: /* @__PURE__ */ (0, Z9.jsxs)(sn, {
85461
+ instanceIndex: i,
85462
+ target: s,
85463
+ isCustomTarget: o,
85464
+ children: [/* @__PURE__ */ (0, Z9.jsx)(qxe, { id: s }), /* @__PURE__ */ (0, Z9.jsx)(Hxe, {})]
85463
85465
  }) }) }));
85464
85466
  } catch (e) {
85465
- console.warn("Tellyou AI: Failed to initialize plugin instance " + n, r, e);
85467
+ console.warn("Tellyou AI: Failed to initialize plugin instance " + i, a, e);
85466
85468
  }
85467
85469
  });
85468
85470
  },
85471
+ destroy: (e) => {
85472
+ let i = e ? [e] : [...n];
85473
+ for (let e of i) {
85474
+ let i = r.get(e);
85475
+ i && i.owner === t && (i.root.unmount(), r.delete(e), n.delete(e), i.createdElement && i.elem.isConnected && i.elem.remove());
85476
+ }
85477
+ },
85469
85478
  toggle: ({ target: e, open: t, userMessage: n, assistantMessage: r }) => {
85470
85479
  window.dispatchEvent(new CustomEvent("tellyouai-plugin", { detail: {
85471
85480
  id: e,
@@ -85491,8 +85500,8 @@ var J9, Y9, Wxe, Gxe = o((() => {
85491
85500
  }
85492
85501
  i || Object.assign(r, a[0] || {}), window.dispatchEvent(new CustomEvent("tellyouai-config-changed", { detail: { target: o } }));
85493
85502
  }
85494
- }, r = e.tellyouai ||= {};
85495
- return Object.assign(r, n), n;
85503
+ }, a = e.tellyouai ||= {};
85504
+ return Object.assign(a, i), i;
85496
85505
  };
85497
85506
  })), eSe = async (e) => {
85498
85507
  if (typeof window > "u" || typeof document > "u") throw Error("Tellyou AI can only be initialized in a browser");
@@ -146,6 +146,7 @@ export type ToggleOptions = {
146
146
  };
147
147
  export type PluginController = {
148
148
  load: () => void;
149
+ destroy: (target?: string) => void;
149
150
  toggle: (options: ToggleOptions) => void;
150
151
  goTo: (mode: Mode, target?: string) => void;
151
152
  setConsent: (consent: Pick<ConsentState, "functional" | "analytics">) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tellyouai/plugin",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Framework-agnostic browser SDK for the Tellyou AI plugin",
5
5
  "repository": {
6
6
  "type": "git",