@sendbird/ai-agent-messenger-react 1.33.0 → 1.34.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.
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("react"),E=require("styled-components"),w=require("@sendbird/react-uikit-message-template-view"),h=require("@sendbird/uikit-message-template"),n=require("../index.cjs"),M=e=>e&&e.__esModule?e:{default:e};function A(e){if(e&&e.__esModule)return e;const s=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const r in e)if(r!=="default"){const t=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(s,r,t.get?t:{enumerable:!0,get:()=>e[r]})}}return s.default=e,Object.freeze(s)}const y=A(S),I=M(E),v={VARIABLE:e=>new RegExp(`\\{${e}\\}`,"g"),VIEW_VARIABLE:e=>new RegExp(`"{@${e}}"`,"g")};function g({uiTemplateString:e,variables:s={}}){const r=n.flattenObject(s);return n.replaceVariablesSync({template:e,variables:r,variableKeyPattern:t=>v.VARIABLE(t)})}function V({theme:e,uiTemplateString:s,colorVariables:r={}}){return n.replaceVariablesSync({template:s,variables:r,variableKeyPattern:t=>v.VARIABLE(t),variableValueTransformer:t=>n.pickColorByTheme(e,t)})}function O({theme:e,uiTemplateString:s,viewVariables:r={},aiAgentColorVariables:t,internalVariables:i,onRequestTemplate:l}){const u=Object.values(r).flat().map(o=>o.key);return[...new Set(u)].some(o=>!l(o))?null:n.replaceVariablesSync({template:s,variables:r,variableKeyPattern:o=>v.VIEW_VARIABLE(o),variableValueTransformer:o=>{const f=o.map(({key:m,variables:T})=>{const d=l(m),{ui_template:c,color_variables:_}=n.parseToMessageTemplate(d);let p=JSON.stringify(c);return p=g({uiTemplateString:p,variables:i}),p=V({theme:e,uiTemplateString:p,colorVariables:_}),p=V({theme:e,uiTemplateString:p,colorVariables:t}),p=g({uiTemplateString:p,variables:T}),n.parseToUITemplate(p)});return JSON.stringify(f)}})}async function R({theme:e,uiTemplateString:s,viewVariables:r={},aiAgentColorVariables:t,internalVariables:i,onRequestTemplate:l}){return n.replaceVariablesAsync({template:s,variables:r,variableKeyPattern:a=>v.VIEW_VARIABLE(a),variableValueTransformer:async a=>{const u=a.map(async({key:o,variables:f})=>{const m=await l(o),{ui_template:T,color_variables:d}=n.parseToMessageTemplate(m);let c=JSON.stringify(T);return c=g({uiTemplateString:c,variables:i}),c=V({theme:e,uiTemplateString:c,colorVariables:d}),c=V({theme:e,uiTemplateString:c,colorVariables:t}),c=g({uiTemplateString:c,variables:f}),n.parseToUITemplate(c)}),b=await Promise.all(u);return JSON.stringify(b)}})}const C=({rawTemplate:e,aiAgentColorVariables:s,internalVariables:r,...t})=>{const{ui_template:i,color_variables:l}=n.parseToMessageTemplate(e);let a=JSON.stringify(i);if(Object.keys(t.viewVariables??{}).length>0){const b=O({...t,uiTemplateString:a,aiAgentColorVariables:s,internalVariables:r});if(b===null)return null;a=b}return a=g({...t,uiTemplateString:a,variables:r}),a=V({...t,uiTemplateString:a,colorVariables:l}),a=V({...t,uiTemplateString:a,colorVariables:s}),a=g({...t,uiTemplateString:a}),n.parseToUITemplate(a)},j=async({rawTemplate:e,aiAgentColorVariables:s,internalVariables:r,...t})=>{const{ui_template:i,color_variables:l}=n.parseToMessageTemplate(e);let a=JSON.stringify(i);return Object.keys(t.viewVariables??{}).length>0&&(a=await R({...t,uiTemplateString:a,aiAgentColorVariables:s,internalVariables:r})),a=g({...t,uiTemplateString:a,variables:r}),a=V({...t,uiTemplateString:a,colorVariables:l}),a=V({...t,uiTemplateString:a,colorVariables:s}),a=g({...t,uiTemplateString:a}),n.parseToUITemplate(a)},q=e=>{const{aiAgentColorVariables:s={}}=e,{internalVariables:r}=n.useMessageTemplateFetchingContext();return S.useState(()=>{const t=e.onGetCachedTemplate(e.templateKey);return t?C({...e,rawTemplate:t,aiAgentColorVariables:s,internalVariables:r,onRequestTemplate:e.onGetCachedTemplate}):null})},N=e=>{const[s]=q(e);if(s)return s;const{aiAgentColorVariables:r={}}=e,{internalVariables:t,fetchTemplate:i,suspend:l}=n.useMessageTemplateFetchingContext();return l(e.templateKey,async()=>{const u=await i({...e,suspense:!0});return await j({...e,rawTemplate:u,aiAgentColorVariables:r,internalVariables:t})}).value},P=new Set(["default"]),{MessageTemplate:U}=h.createMessageTemplate({parser:w.parser,renderer:w.renderer,Container:e=>y.createElement("div",{...e})}),K=({extendedMessagePayload:e,onGetCachedMessageTemplate:s=()=>null,onRequestMessageTemplate:r,onHandleTemplateInternalAction:t=n.noop,onClickLink:i})=>{const l=e==null?void 0:e.message_template;if(!P.has((l==null?void 0:l.type)??"default"))throw new Error("Unsupported template type");if(!r)throw new Error("onRequestMessageTemplate is required");const u=l==null?void 0:l.key;if(!u)throw new Error("Template key is missing");const{theme:b}=n.useMessengerThemeContext(),o=N({onRequestTemplate:r,onGetCachedTemplate:s,theme:b.selectedTheme,templateKey:u,variables:l==null?void 0:l.variables,viewVariables:l==null?void 0:l.view_variables,aiAgentColorVariables:b.aiAgentColorVariables});let f;return i&&(f=(m,T)=>{m.preventDefault(),m.stopPropagation(),T.data&&i({url:T.data})}),y.createElement(k,{id:`${n.ID_INCOMING}-ext-message-template`},y.createElement(w.MessageProvider,{handleWebAction:f,handleInternalAction:(m,T)=>{m.stopPropagation(),t==null||t(T)}},y.createElement(U,{templateItems:o.body.items,templateVersion:o.version})))},k=I.default.div`box-sizing:border-box;overflow:hidden;width:100%;`;exports.MessageTemplateWrapper=K;
@@ -1,22 +1,22 @@
1
- import * as V from "react";
1
+ import * as f from "react";
2
2
  import { useState as A } from "react";
3
3
  import _ from "styled-components";
4
4
  import { MessageProvider as I, renderer as R, parser as C } from "@sendbird/react-uikit-message-template-view";
5
5
  import { createMessageTemplate as O } from "@sendbird/uikit-message-template";
6
- import { r as M, f as K, a as S, p as N, b as g, d as f, u as h, g as B, I as L, n as k } from "../index.js";
7
- const v = {
6
+ import { r as M, f as K, a as S, p as N, b as v, d as y, u as h, g as B, I as G, n as J } from "../index.js";
7
+ const w = {
8
8
  VARIABLE: (e) => new RegExp(`\\{${e}\\}`, "g"),
9
9
  VIEW_VARIABLE: (e) => new RegExp(`"{@${e}}"`, "g")
10
10
  };
11
- function p({ uiTemplateString: e, variables: l = {} }) {
11
+ function T({ uiTemplateString: e, variables: l = {} }) {
12
12
  const s = K(l);
13
13
  return S({
14
14
  template: e,
15
15
  variables: s,
16
- variableKeyPattern: (t) => v.VARIABLE(t)
16
+ variableKeyPattern: (t) => w.VARIABLE(t)
17
17
  });
18
18
  }
19
- function b({
19
+ function V({
20
20
  theme: e,
21
21
  uiTemplateString: l,
22
22
  colorVariables: s = {}
@@ -24,121 +24,121 @@ function b({
24
24
  return S({
25
25
  template: l,
26
26
  variables: s,
27
- variableKeyPattern: (t) => v.VARIABLE(t),
27
+ variableKeyPattern: (t) => w.VARIABLE(t),
28
28
  variableValueTransformer: (t) => N(e, t)
29
29
  });
30
30
  }
31
- function G({
31
+ function L({
32
32
  theme: e,
33
33
  uiTemplateString: l,
34
34
  viewVariables: s = {},
35
35
  aiAgentColorVariables: t,
36
- internalVariables: a,
37
- onRequestTemplate: i
36
+ internalVariables: i,
37
+ onRequestTemplate: a
38
38
  }) {
39
39
  const c = Object.values(s).flat().map((n) => n.key);
40
- return [...new Set(c)].some((n) => !i(n)) ? null : S({
40
+ return [...new Set(c)].some((n) => !a(n)) ? null : S({
41
41
  template: l,
42
42
  variables: s,
43
- variableKeyPattern: (n) => v.VIEW_VARIABLE(n),
43
+ variableKeyPattern: (n) => w.VIEW_VARIABLE(n),
44
44
  variableValueTransformer: (n) => {
45
- const T = n.map(({ key: y, variables: w }) => {
46
- const E = i(y), { ui_template: o, color_variables: d } = g(E);
45
+ const g = n.map(({ key: m, variables: b }) => {
46
+ const E = a(m), { ui_template: o, color_variables: d } = v(E);
47
47
  let u = JSON.stringify(o);
48
- return u = p({
48
+ return u = T({
49
49
  uiTemplateString: u,
50
- variables: a
51
- }), u = b({
50
+ variables: i
51
+ }), u = V({
52
52
  theme: e,
53
53
  uiTemplateString: u,
54
54
  colorVariables: d
55
- }), u = b({
55
+ }), u = V({
56
56
  theme: e,
57
57
  uiTemplateString: u,
58
58
  colorVariables: t
59
- }), u = p({
59
+ }), u = T({
60
60
  uiTemplateString: u,
61
- variables: w
62
- }), f(u);
61
+ variables: b
62
+ }), y(u);
63
63
  });
64
- return JSON.stringify(T);
64
+ return JSON.stringify(g);
65
65
  }
66
66
  });
67
67
  }
68
- async function J({
68
+ async function P({
69
69
  theme: e,
70
70
  uiTemplateString: l,
71
71
  viewVariables: s = {},
72
72
  aiAgentColorVariables: t,
73
- internalVariables: a,
74
- onRequestTemplate: i
73
+ internalVariables: i,
74
+ onRequestTemplate: a
75
75
  }) {
76
76
  return M({
77
77
  template: l,
78
78
  variables: s,
79
- variableKeyPattern: (r) => v.VIEW_VARIABLE(r),
79
+ variableKeyPattern: (r) => w.VIEW_VARIABLE(r),
80
80
  variableValueTransformer: async (r) => {
81
- const c = r.map(async ({ key: n, variables: T }) => {
82
- const y = await i(n), { ui_template: w, color_variables: E } = g(y);
83
- let o = JSON.stringify(w);
84
- return o = p({
81
+ const c = r.map(async ({ key: n, variables: g }) => {
82
+ const m = await a(n), { ui_template: b, color_variables: E } = v(m);
83
+ let o = JSON.stringify(b);
84
+ return o = T({
85
85
  uiTemplateString: o,
86
- variables: a
87
- }), o = b({
86
+ variables: i
87
+ }), o = V({
88
88
  theme: e,
89
89
  uiTemplateString: o,
90
90
  colorVariables: E
91
- }), o = b({
91
+ }), o = V({
92
92
  theme: e,
93
93
  uiTemplateString: o,
94
94
  colorVariables: t
95
- }), o = p({
95
+ }), o = T({
96
96
  uiTemplateString: o,
97
- variables: T
98
- }), f(o);
99
- }), m = await Promise.all(c);
100
- return JSON.stringify(m);
97
+ variables: g
98
+ }), y(o);
99
+ }), p = await Promise.all(c);
100
+ return JSON.stringify(p);
101
101
  }
102
102
  });
103
103
  }
104
- const P = ({
104
+ const U = ({
105
105
  rawTemplate: e,
106
106
  aiAgentColorVariables: l,
107
107
  internalVariables: s,
108
108
  ...t
109
109
  }) => {
110
- const { ui_template: a, color_variables: i } = g(e);
111
- let r = JSON.stringify(a);
110
+ const { ui_template: i, color_variables: a } = v(e);
111
+ let r = JSON.stringify(i);
112
112
  if (Object.keys(t.viewVariables ?? {}).length > 0) {
113
- const m = G({
113
+ const p = L({
114
114
  ...t,
115
115
  uiTemplateString: r,
116
116
  aiAgentColorVariables: l,
117
117
  internalVariables: s
118
118
  });
119
- if (m === null) return null;
120
- r = m;
119
+ if (p === null) return null;
120
+ r = p;
121
121
  }
122
- return r = p({ ...t, uiTemplateString: r, variables: s }), r = b({ ...t, uiTemplateString: r, colorVariables: i }), r = b({ ...t, uiTemplateString: r, colorVariables: l }), r = p({ ...t, uiTemplateString: r }), f(r);
123
- }, U = async ({
122
+ return r = T({ ...t, uiTemplateString: r, variables: s }), r = V({ ...t, uiTemplateString: r, colorVariables: a }), r = V({ ...t, uiTemplateString: r, colorVariables: l }), r = T({ ...t, uiTemplateString: r }), y(r);
123
+ }, W = async ({
124
124
  rawTemplate: e,
125
125
  aiAgentColorVariables: l,
126
126
  internalVariables: s,
127
127
  ...t
128
128
  }) => {
129
- const { ui_template: a, color_variables: i } = g(e);
130
- let r = JSON.stringify(a);
131
- return Object.keys(t.viewVariables ?? {}).length > 0 && (r = await J({
129
+ const { ui_template: i, color_variables: a } = v(e);
130
+ let r = JSON.stringify(i);
131
+ return Object.keys(t.viewVariables ?? {}).length > 0 && (r = await P({
132
132
  ...t,
133
133
  uiTemplateString: r,
134
134
  aiAgentColorVariables: l,
135
135
  internalVariables: s
136
- })), r = p({ ...t, uiTemplateString: r, variables: s }), r = b({ ...t, uiTemplateString: r, colorVariables: i }), r = b({ ...t, uiTemplateString: r, colorVariables: l }), r = p({ ...t, uiTemplateString: r }), f(r);
136
+ })), r = T({ ...t, uiTemplateString: r, variables: s }), r = V({ ...t, uiTemplateString: r, colorVariables: a }), r = V({ ...t, uiTemplateString: r, colorVariables: l }), r = T({ ...t, uiTemplateString: r }), y(r);
137
137
  }, j = (e) => {
138
138
  const { aiAgentColorVariables: l = {} } = e, { internalVariables: s } = h();
139
139
  return A(() => {
140
140
  const t = e.onGetCachedTemplate(e.templateKey);
141
- return t ? P({
141
+ return t ? U({
142
142
  ...e,
143
143
  rawTemplate: t,
144
144
  aiAgentColorVariables: l,
@@ -146,51 +146,56 @@ const P = ({
146
146
  onRequestTemplate: e.onGetCachedTemplate
147
147
  }) : null;
148
148
  });
149
- }, q = (e) => {
149
+ }, k = (e) => {
150
150
  const [l] = j(e);
151
151
  if (l) return l;
152
- const { aiAgentColorVariables: s = {} } = e, { internalVariables: t, fetchTemplate: a, suspend: i } = h();
153
- return i(e.templateKey, async () => {
154
- const c = await a({ ...e, suspense: !0 });
155
- return await U({
152
+ const { aiAgentColorVariables: s = {} } = e, { internalVariables: t, fetchTemplate: i, suspend: a } = h();
153
+ return a(e.templateKey, async () => {
154
+ const c = await i({ ...e, suspense: !0 });
155
+ return await W({
156
156
  ...e,
157
157
  rawTemplate: c,
158
158
  aiAgentColorVariables: s,
159
159
  internalVariables: t
160
160
  });
161
161
  }).value;
162
- }, x = /* @__PURE__ */ new Set(["default"]), { MessageTemplate: W } = O({
162
+ }, q = /* @__PURE__ */ new Set(["default"]), { MessageTemplate: x } = O({
163
163
  parser: C,
164
164
  renderer: R,
165
- Container: (e) => /* @__PURE__ */ V.createElement("div", { ...e })
165
+ Container: (e) => /* @__PURE__ */ f.createElement("div", { ...e })
166
166
  }), Y = ({
167
167
  extendedMessagePayload: e,
168
168
  onGetCachedMessageTemplate: l = () => null,
169
169
  onRequestMessageTemplate: s,
170
- onHandleTemplateInternalAction: t = k
170
+ onHandleTemplateInternalAction: t = J,
171
+ onClickLink: i
171
172
  }) => {
172
173
  const a = e == null ? void 0 : e.message_template;
173
- if (!x.has((a == null ? void 0 : a.type) ?? "default")) throw new Error("Unsupported template type");
174
+ if (!q.has((a == null ? void 0 : a.type) ?? "default")) throw new Error("Unsupported template type");
174
175
  if (!s) throw new Error("onRequestMessageTemplate is required");
175
- const r = a == null ? void 0 : a.key;
176
- if (!r) throw new Error("Template key is missing");
177
- const { theme: c } = B(), m = q({
176
+ const c = a == null ? void 0 : a.key;
177
+ if (!c) throw new Error("Template key is missing");
178
+ const { theme: p } = B(), n = k({
178
179
  onRequestTemplate: s,
179
180
  onGetCachedTemplate: l,
180
- theme: c.selectedTheme,
181
- templateKey: r,
181
+ theme: p.selectedTheme,
182
+ templateKey: c,
182
183
  variables: a == null ? void 0 : a.variables,
183
184
  viewVariables: a == null ? void 0 : a.view_variables,
184
- aiAgentColorVariables: c.aiAgentColorVariables
185
+ aiAgentColorVariables: p.aiAgentColorVariables
185
186
  });
186
- return /* @__PURE__ */ V.createElement($, { id: `${L}-ext-message-template` }, /* @__PURE__ */ V.createElement(
187
+ let g;
188
+ return i && (g = (m, b) => {
189
+ m.preventDefault(), m.stopPropagation(), b.data && i({ url: b.data });
190
+ }), /* @__PURE__ */ f.createElement($, { id: `${G}-ext-message-template` }, /* @__PURE__ */ f.createElement(
187
191
  I,
188
192
  {
189
- handleInternalAction: (n, T) => {
190
- n.stopPropagation(), t == null || t(T);
193
+ handleWebAction: g,
194
+ handleInternalAction: (m, b) => {
195
+ m.stopPropagation(), t == null || t(b);
191
196
  }
192
197
  },
193
- /* @__PURE__ */ V.createElement(W, { templateItems: m.body.items, templateVersion: m.version })
198
+ /* @__PURE__ */ f.createElement(x, { templateItems: n.body.items, templateVersion: n.version })
194
199
  ));
195
200
  }, $ = _.div`box-sizing:border-box;overflow:hidden;width:100%;`;
196
201
  export {