bstp-agent-widget 0.2.78 → 0.2.80

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
@@ -30,31 +30,88 @@ npm publish --access public
30
30
  bun add bstp-agent-widget
31
31
  ```
32
32
 
33
+ ### 1. Provide runtime configuration via `/environment.json`
34
+
35
+ Host applications must expose a GenAI configuration inside their own `public/environment.json`. The widget loads this file by default (or the URL you pass with `environmentUrl`).
36
+
37
+ ```json
38
+ {
39
+ "url": {
40
+ "api": "https://your-api",
41
+ "apiPaths": {
42
+ "genai": "genai/api/v1"
43
+ }
44
+ },
45
+ "genai": {
46
+ "config": {
47
+ "assistantId": 220,
48
+ "agents": [
49
+ {
50
+ "assistantKey": "salesAgent",
51
+ "assistantId": 220,
52
+ "label": "Sales Assistant",
53
+ "initialMessage": "Hello, my customer id is "
54
+ }
55
+ ]
56
+ }
57
+ }
58
+ }
59
+ ```
60
+
61
+ > GenAI login credentials are bundled inside the widget. Do **not** expose `genai.init` or any client secrets in your app's `environment.json`.
62
+
63
+ ### 2. Mount the widget (React example)
64
+
65
+ Prefer mounting through `mountAgentChatWidgetFromEnvironment`. It automatically hydrates GenAI config from the environment file, while allowing optional overrides through the `config` option.
66
+
33
67
  ```tsx
34
- import { AgentChatWidget } from 'bstp-agent-widget';
68
+ import { useEffect, useRef } from 'react';
69
+ import { mountAgentChatWidgetFromEnvironment } from 'bstp-agent-widget';
35
70
  import 'bstp-agent-widget/style.css';
36
71
 
37
- export function Page() {
38
- return (
39
- <AgentChatWidget
40
- config={{
41
- baseUrl: 'https://your-genai-api-url',
42
- assistantId: 123,
43
- genaiInit: {
44
- email: 'agent@example.com',
45
- clientSecret: 'secret',
72
+ export function AgentWidgetHost() {
73
+ const hostRef = useRef<HTMLDivElement | null>(null);
74
+
75
+ useEffect(() => {
76
+ if (!hostRef.current) return;
77
+
78
+ let cleanup: (() => void) | undefined;
79
+
80
+ (async () => {
81
+ cleanup = await mountAgentChatWidgetFromEnvironment(hostRef.current!, {
82
+ runtime: {
83
+ customerId: '590100010884',
84
+ customerToken: 'jwt-token',
85
+ language: 'tr-TR',
46
86
  },
47
- }}
48
- runtime={{
49
- customerId: '590100010884',
50
- customerToken: 'jwt-token',
51
- language: 'tr-TR',
52
- }}
53
- />
54
- );
87
+ // Optional: override assistant selection coming from environment.json
88
+ config: {
89
+ assistantId: 220,
90
+ assistants: [
91
+ {
92
+ assistantKey: 'serviceAgent',
93
+ assistantId: 94,
94
+ label: 'Get help with your service issues',
95
+ initialMessage: 'Hello, my customer id is ',
96
+ },
97
+ ],
98
+ },
99
+ // Optional UI overrides
100
+ // theme: { withinPortal: true, fabPosition: { bottom: 16, right: 16 } },
101
+ // Optional: supply a different environment file
102
+ // environmentUrl: '/configs/widget-environment.json',
103
+ });
104
+ })();
105
+
106
+ return () => cleanup?.();
107
+ }, []);
108
+
109
+ return <div ref={hostRef} />;
55
110
  }
56
111
  ```
57
112
 
113
+ > Precedence rules: runtime data is always provided by the host. If you pass a `config` option, the widget overrides the `genai.config` block from `environment.json` before booting (so every consumer, including initial automated messages, sees the admin UI values). When no override is provided, it falls back to the values coming from `environment.json`.
114
+
58
115
  ## Development Notes
59
116
 
60
117
  - `react` and `react-dom` are defined as `peerDependencies`.
@@ -101,15 +158,19 @@ export class AgentChatWidgetComponent implements AfterViewInit, OnDestroy {
101
158
  customerToken: 'jwt-token',
102
159
  language: 'en-US',
103
160
  },
104
- // Agent config can be supplied by host app.
105
- // baseUrl and genai.init still come from environment.json.
161
+ // Optional: override what environment.json provides
106
162
  config: {
107
163
  assistantId: 220,
108
164
  assistants: [
109
- { assistantKey: 'salesAgent', assistantId: 220, label: 'Sales Assistant' },
165
+ {
166
+ assistantKey: 'salesAgent',
167
+ assistantId: 220,
168
+ label: 'Sales Assistant',
169
+ initialMessage: 'Hello, my customer id is ',
170
+ },
110
171
  ],
111
172
  },
112
- ui: {
173
+ theme: {
113
174
  zIndex: 12000,
114
175
  withinPortal: true,
115
176
  fabPosition: { bottom: 16, right: 16 },
@@ -137,13 +198,21 @@ export class AgentChatWidgetComponent implements AfterViewInit, OnDestroy {
137
198
  },
138
199
  "genai": {
139
200
  "config": {
140
- "assistantId": 220
201
+ "assistantId": 220,
202
+ "agents": [
203
+ {
204
+ "assistantKey": "serviceAgent",
205
+ "assistantId": 94,
206
+ "label": "Get help with your service issues",
207
+ "initialMessage": "Hello, my customer id is "
208
+ }
209
+ ]
141
210
  }
142
211
  }
143
212
  }
144
213
  ```
145
214
 
146
- Base URL is resolved from `url.api` + `url.apiPaths.genai`.
215
+ Base URL is resolved from `url.api` + `url.apiPaths.genai`. External `config` overrides supplied when mounting the widget take precedence over this environment file. GenAI login credentials are managed internally by the widget, so Angular hosts never provide `genai.init`.
147
216
 
148
217
  # React Starter Kit
149
218
 
@@ -28695,8 +28695,8 @@ function PF() {
28695
28695
  sendMessage: _,
28696
28696
  sendInitialMessage: u(async () => {
28697
28697
  if (m.current || l || r.length > 0 || !e || !t) return;
28698
- let n = nk(e), i = ck(t), a = i ? `${i} ${n}`.trim() : `Hello, my customer id is ${n}`;
28699
- m.current = !0, await _(a, { showUserBubble: !1 });
28698
+ let n = nk(e), i = ck(t)?.trim(), a = null;
28699
+ a = i ? n ? `${i} ${n}`.trim() : i : n ? `Hello, my customer id is ${n}` : "Hello", a.trim() && (m.current = !0, await _(a, { showUserBubble: !1 }));
28700
28700
  }, [
28701
28701
  e,
28702
28702
  t,
@@ -33534,7 +33534,7 @@ var zB = RB(), BB = {
33534
33534
  apiPaths: {
33535
33535
  crm: "/crm",
33536
33536
  uiAuthz: "/ui-authz",
33537
- genai: "genai/api/v1"
33537
+ genai: "/genai/api/v1"
33538
33538
  }
33539
33539
  },
33540
33540
  keycloak: {
@@ -33573,20 +33573,28 @@ var zB = RB(), BB = {
33573
33573
  }
33574
33574
  }
33575
33575
  };
33576
- //#endregion
33577
- //#region src/lib/config.service.ts
33578
- async function VB(e) {
33576
+ function VB(e) {
33577
+ let t = BB.genai?.init;
33578
+ return !t || e.genai?.init ? e : {
33579
+ ...e,
33580
+ genai: {
33581
+ ...e.genai ?? {},
33582
+ init: t
33583
+ }
33584
+ };
33585
+ }
33586
+ async function HB(e) {
33579
33587
  let t = `${(globalThis.__BASE_URL__ ?? "").replace(/\/$/, "")}/environment.json`, n = e ?? t;
33580
33588
  try {
33581
33589
  let e = await fetch(n);
33582
- return e.ok ? await e.json() : BB;
33590
+ return e.ok ? VB(await e.json()) : BB;
33583
33591
  } catch {
33584
33592
  return BB;
33585
33593
  }
33586
33594
  }
33587
33595
  //#endregion
33588
33596
  //#region src/lib/agent-widget.tsx
33589
- function HB({ config: e, runtime: t, theme: n }) {
33597
+ function UB({ config: e, runtime: t, theme: n }) {
33590
33598
  return _A.locale || _A.activate("en"), /* @__PURE__ */ T(fC, {
33591
33599
  i18n: _A,
33592
33600
  children: /* @__PURE__ */ T(Gn, {
@@ -33605,17 +33613,17 @@ function HB({ config: e, runtime: t, theme: n }) {
33605
33613
  })
33606
33614
  });
33607
33615
  }
33608
- var UB = /* @__PURE__ */ new WeakMap();
33609
- function WB(e, t) {
33610
- let n = UB.get(e);
33616
+ var WB = /* @__PURE__ */ new WeakMap();
33617
+ function GB(e, t) {
33618
+ let n = WB.get(e);
33611
33619
  n && n.unmount();
33612
33620
  let r = D(e);
33613
- return r.render(/* @__PURE__ */ T(HB, { ...t })), UB.set(e, r), () => {
33614
- let t = UB.get(e);
33615
- t && (t.unmount(), UB.delete(e));
33621
+ return r.render(/* @__PURE__ */ T(UB, { ...t })), WB.set(e, r), () => {
33622
+ let t = WB.get(e);
33623
+ t && (t.unmount(), WB.delete(e));
33616
33624
  };
33617
33625
  }
33618
- function GB(e) {
33626
+ function KB(e) {
33619
33627
  let t = e.genai?.config;
33620
33628
  if (!t?.assistantId) throw Error("GenAI config is missing in environment.json.");
33621
33629
  return {
@@ -33624,22 +33632,27 @@ function GB(e) {
33624
33632
  genaiInit: e.genai?.init
33625
33633
  };
33626
33634
  }
33627
- function KB(e, t) {
33635
+ function qB(e, t) {
33628
33636
  return t ? {
33629
33637
  assistantId: t.assistantId ?? e.assistantId,
33630
33638
  assistants: t.assistants ?? e.assistants,
33631
33639
  genaiInit: e.genaiInit
33632
33640
  } : e;
33633
33641
  }
33634
- async function qB(e, t) {
33635
- let n = await VB(t.environmentUrl);
33636
- return OF(n), WB(e, {
33637
- config: KB(GB(n), t.config),
33642
+ function JB(e, t) {
33643
+ if (!t) return e;
33644
+ let n = e.genai ??= {}, r = n.config ??= {};
33645
+ return typeof t.assistantId == "number" && (r.assistantId = t.assistantId), t.assistants && (r.agents = t.assistants), e;
33646
+ }
33647
+ async function YB(e, t) {
33648
+ let n = JB(await HB(t.environmentUrl), t.config);
33649
+ return OF(n), GB(e, {
33650
+ config: qB(KB(n), t.config),
33638
33651
  runtime: t.runtime,
33639
33652
  theme: t.theme
33640
33653
  });
33641
33654
  }
33642
33655
  //#endregion
33643
- export { HB as AgentChatWidget, WB as mountAgentChatWidget, qB as mountAgentChatWidgetFromEnvironment };
33656
+ export { UB as AgentChatWidget, GB as mountAgentChatWidget, YB as mountAgentChatWidgetFromEnvironment };
33644
33657
 
33645
33658
  //# sourceMappingURL=agent-widget.js.map