@stigmer/embed 0.1.0-dev.20261017020314.g644422724c27
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/LICENSE +194 -0
- package/define.d.ts +8 -0
- package/define.d.ts.map +1 -0
- package/define.js +9 -0
- package/define.js.map +1 -0
- package/element.d.ts +50 -0
- package/element.d.ts.map +1 -0
- package/element.js +172 -0
- package/element.js.map +1 -0
- package/embed.global.js +1 -0
- package/frame.d.ts +45 -0
- package/frame.d.ts.map +1 -0
- package/frame.js +118 -0
- package/frame.js.map +1 -0
- package/global.d.ts +18 -0
- package/global.d.ts.map +1 -0
- package/global.js +23 -0
- package/global.js.map +1 -0
- package/host.d.ts +26 -0
- package/host.d.ts.map +1 -0
- package/host.js +49 -0
- package/host.js.map +1 -0
- package/index.d.ts +15 -0
- package/index.d.ts.map +1 -0
- package/index.js +15 -0
- package/index.js.map +1 -0
- package/jsx.d.ts +89 -0
- package/jsx.d.ts.map +1 -0
- package/jsx.js +24 -0
- package/jsx.js.map +1 -0
- package/package.json +45 -0
- package/protocol.d.ts +48 -0
- package/protocol.d.ts.map +1 -0
- package/protocol.js +61 -0
- package/protocol.js.map +1 -0
- package/src/define.ts +10 -0
- package/src/element.ts +202 -0
- package/src/frame.ts +129 -0
- package/src/global.ts +30 -0
- package/src/host.ts +67 -0
- package/src/index.ts +39 -0
- package/src/jsx.ts +99 -0
- package/src/protocol.ts +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Stigmer Contributors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
|
191
|
+
|
|
192
|
+
Except for the contents of the ee/ directory, which are licensed under the
|
|
193
|
+
Stigmer Enterprise License (see ee/LICENSE), everything in this repository
|
|
194
|
+
is licensed under the Apache License, Version 2.0, above.
|
package/define.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Side-effect entry for bundler consumers: `import "@stigmer/embed/define"`
|
|
3
|
+
* registers `<stigmer-agent>` on load. Elements still need an app origin —
|
|
4
|
+
* via the `app-origin` attribute or {@link setDefaultAppOrigin} — because a
|
|
5
|
+
* bundled import has no script URL to derive it from.
|
|
6
|
+
*/
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=define.d.ts.map
|
package/define.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["src/define.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
package/define.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Side-effect entry for bundler consumers: `import "@stigmer/embed/define"`
|
|
3
|
+
* registers `<stigmer-agent>` on load. Elements still need an app origin —
|
|
4
|
+
* via the `app-origin` attribute or {@link setDefaultAppOrigin} — because a
|
|
5
|
+
* bundled import has no script URL to derive it from.
|
|
6
|
+
*/
|
|
7
|
+
import { defineStigmerAgent } from "./element.js";
|
|
8
|
+
defineStigmerAgent();
|
|
9
|
+
//# sourceMappingURL=define.js.map
|
package/define.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.js","sourceRoot":"","sources":["src/define.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,kBAAkB,EAAE,CAAC"}
|
package/element.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `<stigmer-agent>` custom element — the widget a site owner pastes next
|
|
3
|
+
* to the one-line loader script:
|
|
4
|
+
*
|
|
5
|
+
* ```html
|
|
6
|
+
* <script src="https://app.stigmer.ai/embed.js" async></script>
|
|
7
|
+
* <stigmer-agent channel="chn_01j9z3k8f2q4m6n7p8r9s0t1v2"></stigmer-agent>
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* `channel` is the web channel's id (a chat link minted before the Channel
|
|
11
|
+
* kind keeps its `ash_` id): the hosted chat link `/chat/<channel id>` names
|
|
12
|
+
* nothing else, so no rename of the channel's organization can break an
|
|
13
|
+
* embed and no later holder of that organization's name can take it over.
|
|
14
|
+
*
|
|
15
|
+
* The attribute was once called `share`. Pages already carrying
|
|
16
|
+
* `share="…"` keep working: the element reads it when `channel` is absent,
|
|
17
|
+
* and `channel` wins when both are present.
|
|
18
|
+
*
|
|
19
|
+
* The element renders a cross-origin iframe onto the hosted chat page — the
|
|
20
|
+
* iframe is the isolation boundary; no Stigmer UI code runs in the host page.
|
|
21
|
+
* It hides itself when the platform refuses the embed context (origin not in
|
|
22
|
+
* the agent's allowed list), re-dispatching lifecycle signals as
|
|
23
|
+
* `stigmer:ready` / `stigmer:refused` CustomEvents for host-page scripting.
|
|
24
|
+
*/
|
|
25
|
+
/** Sets the fallback origin used when an element has no `app-origin` attribute. */
|
|
26
|
+
export declare function setDefaultAppOrigin(origin: string): void;
|
|
27
|
+
/**
|
|
28
|
+
* SSR shim: `extends HTMLElement` is evaluated when the MODULE loads, not
|
|
29
|
+
* when the class is registered — so on a server (Next.js prerenders "use
|
|
30
|
+
* client" components too) a bare `extends HTMLElement` throws
|
|
31
|
+
* `ReferenceError: HTMLElement is not defined` on import, before the
|
|
32
|
+
* `customElements` guard in {@link defineStigmerAgent} can help. Extending a
|
|
33
|
+
* dummy base keeps the module importable everywhere; nothing constructs the
|
|
34
|
+
* element server-side, so the dummy is never observable.
|
|
35
|
+
*/
|
|
36
|
+
declare const BaseElement: typeof HTMLElement;
|
|
37
|
+
export declare class StigmerAgentElement extends BaseElement {
|
|
38
|
+
static get observedAttributes(): readonly string[];
|
|
39
|
+
private iframe;
|
|
40
|
+
private host;
|
|
41
|
+
connectedCallback(): void;
|
|
42
|
+
disconnectedCallback(): void;
|
|
43
|
+
attributeChangedCallback(): void;
|
|
44
|
+
private attach;
|
|
45
|
+
private detach;
|
|
46
|
+
}
|
|
47
|
+
/** Registers `<stigmer-agent>`; safe to call more than once. */
|
|
48
|
+
export declare function defineStigmerAgent(): void;
|
|
49
|
+
export {};
|
|
50
|
+
//# sourceMappingURL=element.d.ts.map
|
package/element.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"element.d.ts","sourceRoot":"","sources":["src/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AA0BH,mFAAmF;AACnF,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAExD;AAED;;;;;;;;GAQG;AACH,QAAA,MAAM,WAAW,EAEZ,OAAO,WAAW,CAAC;AAExB,qBAAa,mBAAoB,SAAQ,WAAW;IAClD,MAAM,KAAK,kBAAkB,IAAI,SAAS,MAAM,EAAE,CAEjD;IAED,OAAO,CAAC,MAAM,CAAkC;IAChD,OAAO,CAAC,IAAI,CAA0B;IAEtC,iBAAiB,IAAI,IAAI;IAIzB,oBAAoB,IAAI,IAAI;IAI5B,wBAAwB,IAAI,IAAI;IAQhC,OAAO,CAAC,MAAM;IAsDd,OAAO,CAAC,MAAM;CAMf;AA4CD,gEAAgE;AAChE,wBAAgB,kBAAkB,IAAI,IAAI,CAKzC"}
|
package/element.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `<stigmer-agent>` custom element — the widget a site owner pastes next
|
|
3
|
+
* to the one-line loader script:
|
|
4
|
+
*
|
|
5
|
+
* ```html
|
|
6
|
+
* <script src="https://app.stigmer.ai/embed.js" async></script>
|
|
7
|
+
* <stigmer-agent channel="chn_01j9z3k8f2q4m6n7p8r9s0t1v2"></stigmer-agent>
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* `channel` is the web channel's id (a chat link minted before the Channel
|
|
11
|
+
* kind keeps its `ash_` id): the hosted chat link `/chat/<channel id>` names
|
|
12
|
+
* nothing else, so no rename of the channel's organization can break an
|
|
13
|
+
* embed and no later holder of that organization's name can take it over.
|
|
14
|
+
*
|
|
15
|
+
* The attribute was once called `share`. Pages already carrying
|
|
16
|
+
* `share="…"` keep working: the element reads it when `channel` is absent,
|
|
17
|
+
* and `channel` wins when both are present.
|
|
18
|
+
*
|
|
19
|
+
* The element renders a cross-origin iframe onto the hosted chat page — the
|
|
20
|
+
* iframe is the isolation boundary; no Stigmer UI code runs in the host page.
|
|
21
|
+
* It hides itself when the platform refuses the embed context (origin not in
|
|
22
|
+
* the agent's allowed list), re-dispatching lifecycle signals as
|
|
23
|
+
* `stigmer:ready` / `stigmer:refused` CustomEvents for host-page scripting.
|
|
24
|
+
*/
|
|
25
|
+
import { createEmbedHost } from "./host.js";
|
|
26
|
+
/** Attribute-driven configuration of `<stigmer-agent>`. */
|
|
27
|
+
const OBSERVED_ATTRIBUTES = [
|
|
28
|
+
"channel",
|
|
29
|
+
"share",
|
|
30
|
+
"token",
|
|
31
|
+
"theme",
|
|
32
|
+
"width",
|
|
33
|
+
"height",
|
|
34
|
+
"app-origin",
|
|
35
|
+
];
|
|
36
|
+
/** Matches the iframe embed snippet's default footprint. */
|
|
37
|
+
const DEFAULT_WIDTH = "400px";
|
|
38
|
+
const DEFAULT_HEIGHT = "600px";
|
|
39
|
+
/**
|
|
40
|
+
* App origin the IIFE loader derives from its own `<script src>` (see
|
|
41
|
+
* `global.ts`). ESM consumers either call {@link setDefaultAppOrigin} or set
|
|
42
|
+
* the `app-origin` attribute per element.
|
|
43
|
+
*/
|
|
44
|
+
let defaultAppOrigin = null;
|
|
45
|
+
/** Sets the fallback origin used when an element has no `app-origin` attribute. */
|
|
46
|
+
export function setDefaultAppOrigin(origin) {
|
|
47
|
+
defaultAppOrigin = origin;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* SSR shim: `extends HTMLElement` is evaluated when the MODULE loads, not
|
|
51
|
+
* when the class is registered — so on a server (Next.js prerenders "use
|
|
52
|
+
* client" components too) a bare `extends HTMLElement` throws
|
|
53
|
+
* `ReferenceError: HTMLElement is not defined` on import, before the
|
|
54
|
+
* `customElements` guard in {@link defineStigmerAgent} can help. Extending a
|
|
55
|
+
* dummy base keeps the module importable everywhere; nothing constructs the
|
|
56
|
+
* element server-side, so the dummy is never observable.
|
|
57
|
+
*/
|
|
58
|
+
const BaseElement = (typeof HTMLElement === "undefined" ? class {
|
|
59
|
+
} : HTMLElement);
|
|
60
|
+
export class StigmerAgentElement extends BaseElement {
|
|
61
|
+
static get observedAttributes() {
|
|
62
|
+
return OBSERVED_ATTRIBUTES;
|
|
63
|
+
}
|
|
64
|
+
iframe = null;
|
|
65
|
+
host = null;
|
|
66
|
+
connectedCallback() {
|
|
67
|
+
this.attach();
|
|
68
|
+
}
|
|
69
|
+
disconnectedCallback() {
|
|
70
|
+
this.detach();
|
|
71
|
+
}
|
|
72
|
+
attributeChangedCallback() {
|
|
73
|
+
// Identity/config changes rebuild the frame; a chat conversation cannot
|
|
74
|
+
// survive a change of agent, theme, or origin anyway.
|
|
75
|
+
if (this.isConnected) {
|
|
76
|
+
this.attach();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
attach() {
|
|
80
|
+
this.detach();
|
|
81
|
+
// `share` is the deprecated name of `channel`; see the header comment.
|
|
82
|
+
const channel = this.getAttribute("channel") || this.getAttribute("share");
|
|
83
|
+
const appOrigin = this.getAttribute("app-origin") ?? defaultAppOrigin;
|
|
84
|
+
if (!channel) {
|
|
85
|
+
console.error("[stigmer-agent] the channel attribute is required");
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (!appOrigin) {
|
|
89
|
+
console.error("[stigmer-agent] no app origin: load embed.js from your Stigmer app " +
|
|
90
|
+
'origin, or set the app-origin attribute (e.g. app-origin="https://app.stigmer.ai")');
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.style.display = "inline-block";
|
|
94
|
+
this.style.width = cssLength(this.getAttribute("width"), DEFAULT_WIDTH);
|
|
95
|
+
this.style.height = cssLength(this.getAttribute("height"), DEFAULT_HEIGHT);
|
|
96
|
+
const iframe = document.createElement("iframe");
|
|
97
|
+
iframe.src = buildChatUrl(appOrigin, channel, this.getAttribute("token"), this.getAttribute("theme"));
|
|
98
|
+
// The element holds only the channel's id, and the frame protocol carries
|
|
99
|
+
// no agent name, so the frame's accessible name is generic unless the
|
|
100
|
+
// host names it with the element's own title attribute. Read once, when
|
|
101
|
+
// the frame is built: title is deliberately not observed, because a
|
|
102
|
+
// rebuild would wipe the visitor's conversation.
|
|
103
|
+
iframe.title = this.getAttribute("title") || "Chat with an agent";
|
|
104
|
+
iframe.setAttribute("loading", "lazy");
|
|
105
|
+
iframe.style.cssText =
|
|
106
|
+
"width:100%;height:100%;border:0;border-radius:12px";
|
|
107
|
+
this.appendChild(iframe);
|
|
108
|
+
this.iframe = iframe;
|
|
109
|
+
this.host = createEmbedHost(iframe, appOrigin, {
|
|
110
|
+
onReady: () => {
|
|
111
|
+
this.dispatchEvent(new CustomEvent("stigmer:ready"));
|
|
112
|
+
},
|
|
113
|
+
onRefused: () => {
|
|
114
|
+
// Hide gracefully rather than erroring — the requirement for embeds
|
|
115
|
+
// on sites the agent owner has not allowed.
|
|
116
|
+
this.style.display = "none";
|
|
117
|
+
this.dispatchEvent(new CustomEvent("stigmer:refused"));
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
detach() {
|
|
122
|
+
this.host?.destroy();
|
|
123
|
+
this.host = null;
|
|
124
|
+
this.iframe?.remove();
|
|
125
|
+
this.iframe = null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function buildChatUrl(appOrigin, channel, token, theme) {
|
|
129
|
+
const url = new URL(`/chat/${encodeURIComponent(channel)}`, appOrigin);
|
|
130
|
+
// `k` is the platform's link-token parameter (see @stigmer/client's
|
|
131
|
+
// LINK_TOKEN_PARAM — not imported: this loader stays dependency-free).
|
|
132
|
+
// Required when the chat link is locked; the hosted page forwards it
|
|
133
|
+
// on its guest mint, where the server validates it.
|
|
134
|
+
if (token) {
|
|
135
|
+
url.searchParams.set("k", token);
|
|
136
|
+
}
|
|
137
|
+
const resolved = resolveTheme(theme);
|
|
138
|
+
if (resolved) {
|
|
139
|
+
url.searchParams.set("theme", resolved);
|
|
140
|
+
}
|
|
141
|
+
return url.toString();
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Resolves the theme attribute once, at mount. Deliberately not live-tracked:
|
|
145
|
+
* a theme flip mid-conversation would reload the iframe and destroy the
|
|
146
|
+
* visitor's chat, which is worse than a stale theme.
|
|
147
|
+
*/
|
|
148
|
+
function resolveTheme(theme) {
|
|
149
|
+
if (theme === "light" || theme === "dark")
|
|
150
|
+
return theme;
|
|
151
|
+
if (theme === "auto") {
|
|
152
|
+
return window.matchMedia?.("(prefers-color-scheme: dark)").matches
|
|
153
|
+
? "dark"
|
|
154
|
+
: "light";
|
|
155
|
+
}
|
|
156
|
+
// No attribute: let the hosted page use its own default.
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
function cssLength(value, fallback) {
|
|
160
|
+
if (!value)
|
|
161
|
+
return fallback;
|
|
162
|
+
return /^\d+$/.test(value) ? `${value}px` : value;
|
|
163
|
+
}
|
|
164
|
+
/** Registers `<stigmer-agent>`; safe to call more than once. */
|
|
165
|
+
export function defineStigmerAgent() {
|
|
166
|
+
if (typeof customElements === "undefined")
|
|
167
|
+
return;
|
|
168
|
+
if (!customElements.get("stigmer-agent")) {
|
|
169
|
+
customElements.define("stigmer-agent", StigmerAgentElement);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=element.js.map
|
package/element.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"element.js","sourceRoot":"","sources":["src/element.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,eAAe,EAAkB,MAAM,WAAW,CAAC;AAE5D,2DAA2D;AAC3D,MAAM,mBAAmB,GAAG;IAC1B,SAAS;IACT,OAAO;IACP,OAAO;IACP,OAAO;IACP,OAAO;IACP,QAAQ;IACR,YAAY;CACJ,CAAC;AAEX,4DAA4D;AA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ED,SAAS,SAAS,CAAC,KAAoB,EAAE,QAAgB;IACvD,IAAI,CAAC,KAAK;QAAE,OAAO,QAAQ,CAAC;IAC5B,OAAO,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;AACpD,CAAC;AAED,gEAAgE;AAChE,MAAM,UAAU,kBAAkB;IAChC,IAAI,OAAO,cAAc,KAAK,WAAW;QAAE,OAAO;IAClD,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,CAAC;QACzC,cAAc,CAAC,MAAM,CAAC,eAAe,EAAE,mBAAmB,CAAC,CAAC;IAC9D,CAAC;AACH,CAAC"}
|
package/embed.global.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var StigmerEmbed=(function(s){"use strict";var k=Object.defineProperty;var M=(s,o,c)=>o in s?k(s,o,{enumerable:!0,configurable:!0,writable:!0,value:c}):s[o]=c;var l=(s,o,c)=>M(s,typeof o!="symbol"?o+"":o,c);const o="stigmer-embed";function y(t){return{source:o,v:1,...t}}function A(t){if(typeof t!="object"||t===null)return null;const e=t;return e.source!==o||e.v!==1||typeof e.type!="string"?null:e}function w(t){const e=A(t);if(!e)return null;switch(e.type){case"hello":case"ready":case"refused":return{type:e.type};default:return null}}function O(t,e,n={}){const r=()=>{var a;(a=t.contentWindow)==null||a.postMessage(y({type:"init"}),e)},i=a=>{var E,b;if(a.source!==t.contentWindow||a.origin!==e)return;const p=w(a.data);if(p)switch(p.type){case"hello":r();break;case"ready":(E=n.onReady)==null||E.call(n);break;case"refused":(b=n.onRefused)==null||b.call(n);break}};return window.addEventListener("message",i),t.addEventListener("load",r),{destroy(){window.removeEventListener("message",i),t.removeEventListener("load",r)}}}const S=["channel","share","token","theme","width","height","app-origin"],R="400px",T="600px";let h=null;function d(t){h=t}const v=typeof HTMLElement>"u"?class{}:HTMLElement;class g extends v{constructor(){super(...arguments);l(this,"iframe",null);l(this,"host",null)}static get observedAttributes(){return S}connectedCallback(){this.attach()}disconnectedCallback(){this.detach()}attributeChangedCallback(){this.isConnected&&this.attach()}attach(){this.detach();const n=this.getAttribute("channel")||this.getAttribute("share"),r=this.getAttribute("app-origin")??h;if(!n){console.error("[stigmer-agent] the channel attribute is required");return}if(!r){console.error('[stigmer-agent] no app origin: load embed.js from your Stigmer app origin, or set the app-origin attribute (e.g. app-origin="https://app.stigmer.ai")');return}this.style.display="inline-block",this.style.width=f(this.getAttribute("width"),R),this.style.height=f(this.getAttribute("height"),T);const i=document.createElement("iframe");i.src=C(r,n,this.getAttribute("token"),this.getAttribute("theme")),i.title=this.getAttribute("title")||"Chat with an agent",i.setAttribute("loading","lazy"),i.style.cssText="width:100%;height:100%;border:0;border-radius:12px",this.appendChild(i),this.iframe=i,this.host=O(i,r,{onReady:()=>{this.dispatchEvent(new CustomEvent("stigmer:ready"))},onRefused:()=>{this.style.display="none",this.dispatchEvent(new CustomEvent("stigmer:refused"))}})}detach(){var n,r;(n=this.host)==null||n.destroy(),this.host=null,(r=this.iframe)==null||r.remove(),this.iframe=null}}function C(t,e,n,r){const i=new URL(`/chat/${encodeURIComponent(e)}`,t);n&&i.searchParams.set("k",n);const a=L(r);return a&&i.searchParams.set("theme",a),i.toString()}function L(t){var e;return t==="light"||t==="dark"?t:t==="auto"?(e=window.matchMedia)!=null&&e.call(window,"(prefers-color-scheme: dark)").matches?"dark":"light":null}function f(t,e){return t?/^\d+$/.test(t)?`${t}px`:t:e}function m(){typeof customElements>"u"||customElements.get("stigmer-agent")||customElements.define("stigmer-agent",g)}const u=document.currentScript;return u instanceof HTMLScriptElement&&u.src&&d(new URL(u.src).origin),m(),s.StigmerAgentElement=g,s.defineStigmerAgent=m,s.setDefaultAppOrigin=d,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"}),s})({});
|
package/frame.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frame-side helpers — the half of the embed protocol that runs inside the
|
|
3
|
+
* hosted chat page (`/chat/<channel id>` in `client-apps/web`).
|
|
4
|
+
*
|
|
5
|
+
* The embedding site cannot alter this code (it is served from the Stigmer app
|
|
6
|
+
* origin), so the parent origin discovered here is honest by construction.
|
|
7
|
+
* Every source in the discovery ladder is browser-controlled:
|
|
8
|
+
*
|
|
9
|
+
* 1. `location.ancestorOrigins[0]` — the direct parent's origin (Chromium and
|
|
10
|
+
* WebKit; not implemented in Firefox).
|
|
11
|
+
* 2. `document.referrer` — the parent page URL at iframe load (all browsers,
|
|
12
|
+
* but suppressible via the embedder's Referrer-Policy).
|
|
13
|
+
* 3. The `hello`/`init` postMessage handshake with the loader — the browser
|
|
14
|
+
* stamps `event.origin` on the parent's reply, which no page can forge.
|
|
15
|
+
*
|
|
16
|
+
* When all three fail (a plain iframe with suppressed referrer and no loader),
|
|
17
|
+
* the ladder resolves to {@link OPAQUE_ORIGIN} — the WHATWG serialization of
|
|
18
|
+
* an opaque origin — which the platform refuses whenever the agent's
|
|
19
|
+
* `allowed_origins` list is non-empty (fail closed in strict mode).
|
|
20
|
+
*/
|
|
21
|
+
import { type FrameToHostMessage } from "./protocol.js";
|
|
22
|
+
/**
|
|
23
|
+
* Serialization of an opaque/undiscoverable origin (WHATWG HTML spec).
|
|
24
|
+
* Mirrors `SharingOriginPolicy.OPAQUE_ORIGIN` on the server.
|
|
25
|
+
*/
|
|
26
|
+
export declare const OPAQUE_ORIGIN = "null";
|
|
27
|
+
/**
|
|
28
|
+
* Whether the page is running inside an iframe. `false` during SSR/prerender
|
|
29
|
+
* (no `window`), so static export never takes the embed path.
|
|
30
|
+
*/
|
|
31
|
+
export declare function isEmbedded(): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Resolves the embedding page's origin via the discovery ladder above.
|
|
34
|
+
* Only meaningful when {@link isEmbedded} is `true`. Never rejects — an
|
|
35
|
+
* undiscoverable parent resolves to {@link OPAQUE_ORIGIN}.
|
|
36
|
+
*/
|
|
37
|
+
export declare function resolveParentOrigin(timeoutMs?: number): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Posts a lifecycle signal to the embedding page's loader.
|
|
40
|
+
*
|
|
41
|
+
* Targets the discovered parent origin when known; `ready`/`refused` carry no
|
|
42
|
+
* payload, so the `"*"` fallback for an opaque parent leaks nothing.
|
|
43
|
+
*/
|
|
44
|
+
export declare function notifyParent(message: FrameToHostMessage, parentOrigin: string): void;
|
|
45
|
+
//# sourceMappingURL=frame.d.ts.map
|
package/frame.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frame.d.ts","sourceRoot":"","sources":["src/frame.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAGL,KAAK,kBAAkB,EACxB,MAAM,eAAe,CAAC;AAEvB;;;GAGG;AACH,eAAO,MAAM,aAAa,SAAS,CAAC;AAKpC;;;GAGG;AACH,wBAAgB,UAAU,IAAI,OAAO,CASpC;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,SAAS,GAAE,MAA6B,GACvC,OAAO,CAAC,MAAM,CAAC,CAQjB;AAiDD;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,kBAAkB,EAC3B,YAAY,EAAE,MAAM,GACnB,IAAI,CAIN"}
|
package/frame.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frame-side helpers — the half of the embed protocol that runs inside the
|
|
3
|
+
* hosted chat page (`/chat/<channel id>` in `client-apps/web`).
|
|
4
|
+
*
|
|
5
|
+
* The embedding site cannot alter this code (it is served from the Stigmer app
|
|
6
|
+
* origin), so the parent origin discovered here is honest by construction.
|
|
7
|
+
* Every source in the discovery ladder is browser-controlled:
|
|
8
|
+
*
|
|
9
|
+
* 1. `location.ancestorOrigins[0]` — the direct parent's origin (Chromium and
|
|
10
|
+
* WebKit; not implemented in Firefox).
|
|
11
|
+
* 2. `document.referrer` — the parent page URL at iframe load (all browsers,
|
|
12
|
+
* but suppressible via the embedder's Referrer-Policy).
|
|
13
|
+
* 3. The `hello`/`init` postMessage handshake with the loader — the browser
|
|
14
|
+
* stamps `event.origin` on the parent's reply, which no page can forge.
|
|
15
|
+
*
|
|
16
|
+
* When all three fail (a plain iframe with suppressed referrer and no loader),
|
|
17
|
+
* the ladder resolves to {@link OPAQUE_ORIGIN} — the WHATWG serialization of
|
|
18
|
+
* an opaque origin — which the platform refuses whenever the agent's
|
|
19
|
+
* `allowed_origins` list is non-empty (fail closed in strict mode).
|
|
20
|
+
*/
|
|
21
|
+
import { parseHostMessage, toWire, } from "./protocol.js";
|
|
22
|
+
/**
|
|
23
|
+
* Serialization of an opaque/undiscoverable origin (WHATWG HTML spec).
|
|
24
|
+
* Mirrors `SharingOriginPolicy.OPAQUE_ORIGIN` on the server.
|
|
25
|
+
*/
|
|
26
|
+
export const OPAQUE_ORIGIN = "null";
|
|
27
|
+
/** How long to wait for the loader's `init` reply before giving up. */
|
|
28
|
+
const HANDSHAKE_TIMEOUT_MS = 1_500;
|
|
29
|
+
/**
|
|
30
|
+
* Whether the page is running inside an iframe. `false` during SSR/prerender
|
|
31
|
+
* (no `window`), so static export never takes the embed path.
|
|
32
|
+
*/
|
|
33
|
+
export function isEmbedded() {
|
|
34
|
+
if (typeof window === "undefined")
|
|
35
|
+
return false;
|
|
36
|
+
try {
|
|
37
|
+
return window.self !== window.top;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// Cross-origin access to window.top throws in some engines — which
|
|
41
|
+
// itself proves the page is framed by a foreign origin.
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Resolves the embedding page's origin via the discovery ladder above.
|
|
47
|
+
* Only meaningful when {@link isEmbedded} is `true`. Never rejects — an
|
|
48
|
+
* undiscoverable parent resolves to {@link OPAQUE_ORIGIN}.
|
|
49
|
+
*/
|
|
50
|
+
export function resolveParentOrigin(timeoutMs = HANDSHAKE_TIMEOUT_MS) {
|
|
51
|
+
const ancestor = ancestorOrigin();
|
|
52
|
+
if (ancestor)
|
|
53
|
+
return Promise.resolve(ancestor);
|
|
54
|
+
const referrer = referrerOrigin();
|
|
55
|
+
if (referrer)
|
|
56
|
+
return Promise.resolve(referrer);
|
|
57
|
+
return handshakeOrigin(timeoutMs);
|
|
58
|
+
}
|
|
59
|
+
function ancestorOrigin() {
|
|
60
|
+
const origins = window.location.ancestorOrigins;
|
|
61
|
+
const direct = origins && origins.length > 0 ? origins[0] : null;
|
|
62
|
+
// An opaque ancestor serializes as "null"; treat it as undiscovered so the
|
|
63
|
+
// remaining rungs get a chance before the opaque fallback.
|
|
64
|
+
return direct && direct !== OPAQUE_ORIGIN ? direct : null;
|
|
65
|
+
}
|
|
66
|
+
function referrerOrigin() {
|
|
67
|
+
const referrer = document.referrer;
|
|
68
|
+
if (!referrer)
|
|
69
|
+
return null;
|
|
70
|
+
try {
|
|
71
|
+
return new URL(referrer).origin;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Asks the loader for `init` and reads the browser-authentic `event.origin`
|
|
79
|
+
* off the reply. The `hello` is posted to `"*"` because the parent origin is
|
|
80
|
+
* exactly what we do not know yet — it carries no payload, so broadcasting
|
|
81
|
+
* it is safe.
|
|
82
|
+
*/
|
|
83
|
+
function handshakeOrigin(timeoutMs) {
|
|
84
|
+
return new Promise((resolveOrigin) => {
|
|
85
|
+
let settled = false;
|
|
86
|
+
const settle = (origin) => {
|
|
87
|
+
if (settled)
|
|
88
|
+
return;
|
|
89
|
+
settled = true;
|
|
90
|
+
window.removeEventListener("message", onMessage);
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
resolveOrigin(origin);
|
|
93
|
+
};
|
|
94
|
+
const onMessage = (event) => {
|
|
95
|
+
if (event.source !== window.parent)
|
|
96
|
+
return;
|
|
97
|
+
if (!parseHostMessage(event.data))
|
|
98
|
+
return;
|
|
99
|
+
settle(event.origin === "" ? OPAQUE_ORIGIN : event.origin);
|
|
100
|
+
};
|
|
101
|
+
const timer = setTimeout(() => settle(OPAQUE_ORIGIN), timeoutMs);
|
|
102
|
+
window.addEventListener("message", onMessage);
|
|
103
|
+
window.parent.postMessage(toWire({ type: "hello" }), "*");
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Posts a lifecycle signal to the embedding page's loader.
|
|
108
|
+
*
|
|
109
|
+
* Targets the discovered parent origin when known; `ready`/`refused` carry no
|
|
110
|
+
* payload, so the `"*"` fallback for an opaque parent leaks nothing.
|
|
111
|
+
*/
|
|
112
|
+
export function notifyParent(message, parentOrigin) {
|
|
113
|
+
if (!isEmbedded())
|
|
114
|
+
return;
|
|
115
|
+
const target = parentOrigin === OPAQUE_ORIGIN ? "*" : parentOrigin;
|
|
116
|
+
window.parent.postMessage(toWire(message), target);
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=frame.js.map
|
package/frame.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frame.js","sourceRoot":"","sources":["src/frame.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EACL,gBAAgB,EAChB,MAAM,GAEP,MAAM,eAAe,CAAC;AAEvB;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC;AAEpC,uEAAuE;AACvE,MAAM,oBAAoB,GAAG,KAAK,CAAC;AAEnC;;;GAGG;AACH,MAAM,UAAU,UAAU;IACxB,IAAI,OAAO,MAAM,KAAK,WAAW;QAAE,OAAO,KAAK,CAAC;IAChD,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,IAAI,KAAK,MAAM,CAAC,GAAG,CAAC;IACpC,CAAC;IAAC,MAAM,CAAC;QACP,mEAAmE;QACnE,w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