@lark-apaas/aily-web-sdk 0.0.2-beta.1 → 0.0.2-beta.11
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/index.cjs +94 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +44 -13
- package/dist/index.d.ts +44 -13
- package/dist/index.js +92 -54
- package/dist/index.js.map +1 -1
- package/package.json +6 -3
package/dist/index.cjs
CHANGED
|
@@ -3,6 +3,7 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
7
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
8
|
var __export = (target, all) => {
|
|
8
9
|
for (var name in all)
|
|
@@ -17,10 +18,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
18
|
return to;
|
|
18
19
|
};
|
|
19
20
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
20
22
|
|
|
21
23
|
// src/index.ts
|
|
22
24
|
var index_exports = {};
|
|
23
25
|
__export(index_exports, {
|
|
26
|
+
DEFAULT_ANONYMOUS_CHANNEL_TYPE: () => DEFAULT_ANONYMOUS_CHANNEL_TYPE,
|
|
27
|
+
DEFAULT_CHANNEL_TYPE: () => DEFAULT_CHANNEL_TYPE,
|
|
24
28
|
IFRAME_BASE_URL: () => IFRAME_BASE_URL,
|
|
25
29
|
MESSAGE_TIMEOUT: () => MESSAGE_TIMEOUT,
|
|
26
30
|
getIframeBaseURL: () => getIframeBaseURL,
|
|
@@ -44,7 +48,7 @@ function getIframeBaseURL(env) {
|
|
|
44
48
|
if (hostname.includes("aiforce-boe")) {
|
|
45
49
|
return IFRAME_BASE_URL.boe;
|
|
46
50
|
}
|
|
47
|
-
if (hostname.includes("aiforce-pre")) {
|
|
51
|
+
if (hostname.includes("aiforce-pre") || hostname.includes("force-pre.feishuapp.net") || hostname.includes("fsapp.kundou.cn")) {
|
|
48
52
|
return IFRAME_BASE_URL.staging;
|
|
49
53
|
}
|
|
50
54
|
if (hostname.includes("localhost") || hostname.includes("127.0.0.1")) {
|
|
@@ -54,8 +58,8 @@ function getIframeBaseURL(env) {
|
|
|
54
58
|
}
|
|
55
59
|
__name(getIframeBaseURL, "getIframeBaseURL");
|
|
56
60
|
var MESSAGE_TIMEOUT = 30 * 1e3;
|
|
57
|
-
var
|
|
58
|
-
var
|
|
61
|
+
var DEFAULT_CHANNEL_TYPE = "MIAODA_CUI_SDK";
|
|
62
|
+
var DEFAULT_ANONYMOUS_CHANNEL_TYPE = "MIAODA_ANONYMOUS_CUI_SDK";
|
|
59
63
|
var ALLOWED_ORIGINS = [
|
|
60
64
|
"https://aily.feishu.cn",
|
|
61
65
|
"https://aily.feishu-pre.cn",
|
|
@@ -65,17 +69,17 @@ var ALLOWED_ORIGINS = [
|
|
|
65
69
|
"http://localhost:8080"
|
|
66
70
|
];
|
|
67
71
|
|
|
72
|
+
// src/iframe-manager.ts
|
|
73
|
+
var import_internal_slardar2 = require("@lark-apaas/internal-slardar");
|
|
74
|
+
|
|
68
75
|
// src/device-detector.ts
|
|
69
76
|
var MOBILE_BREAKPOINT = 768;
|
|
70
|
-
var
|
|
71
|
-
static {
|
|
72
|
-
__name(this, "DeviceDetector");
|
|
73
|
-
}
|
|
74
|
-
mediaQuery;
|
|
75
|
-
currentDevice;
|
|
76
|
-
callback = null;
|
|
77
|
-
handleChange = null;
|
|
77
|
+
var _DeviceDetector = class _DeviceDetector {
|
|
78
78
|
constructor() {
|
|
79
|
+
__publicField(this, "mediaQuery");
|
|
80
|
+
__publicField(this, "currentDevice");
|
|
81
|
+
__publicField(this, "callback", null);
|
|
82
|
+
__publicField(this, "handleChange", null);
|
|
79
83
|
this.mediaQuery = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
|
|
80
84
|
this.currentDevice = window.innerWidth < MOBILE_BREAKPOINT ? "mobile" : "pc";
|
|
81
85
|
}
|
|
@@ -101,18 +105,18 @@ var DeviceDetector = class {
|
|
|
101
105
|
this.callback = null;
|
|
102
106
|
}
|
|
103
107
|
};
|
|
108
|
+
__name(_DeviceDetector, "DeviceDetector");
|
|
109
|
+
var DeviceDetector = _DeviceDetector;
|
|
104
110
|
|
|
105
111
|
// src/message-bridge.ts
|
|
106
|
-
var
|
|
107
|
-
|
|
108
|
-
__name(this, "MessageBridge");
|
|
109
|
-
}
|
|
110
|
-
iframe = null;
|
|
111
|
-
requestId = 0;
|
|
112
|
-
pendingRequests = /* @__PURE__ */ new Map();
|
|
113
|
-
eventHandlers = /* @__PURE__ */ new Map();
|
|
114
|
-
targetOrigin;
|
|
112
|
+
var import_internal_slardar = require("@lark-apaas/internal-slardar");
|
|
113
|
+
var _MessageBridge = class _MessageBridge {
|
|
115
114
|
constructor(targetOrigin) {
|
|
115
|
+
__publicField(this, "iframe", null);
|
|
116
|
+
__publicField(this, "requestId", 0);
|
|
117
|
+
__publicField(this, "pendingRequests", /* @__PURE__ */ new Map());
|
|
118
|
+
__publicField(this, "eventHandlers", /* @__PURE__ */ new Map());
|
|
119
|
+
__publicField(this, "targetOrigin");
|
|
116
120
|
this.targetOrigin = targetOrigin;
|
|
117
121
|
this.handleMessage = this.handleMessage.bind(this);
|
|
118
122
|
}
|
|
@@ -155,7 +159,13 @@ var MessageBridge = class {
|
|
|
155
159
|
};
|
|
156
160
|
const timer = setTimeout(() => {
|
|
157
161
|
this.pendingRequests.delete(id);
|
|
158
|
-
|
|
162
|
+
const error = new Error(`Message timeout: ${type}`);
|
|
163
|
+
import_internal_slardar.slardar.captureException(error, {
|
|
164
|
+
source: "aily-web-sdk",
|
|
165
|
+
module: "message-bridge",
|
|
166
|
+
messageType: type
|
|
167
|
+
});
|
|
168
|
+
reject(error);
|
|
159
169
|
}, MESSAGE_TIMEOUT);
|
|
160
170
|
this.pendingRequests.set(id, {
|
|
161
171
|
resolve,
|
|
@@ -198,7 +208,13 @@ var MessageBridge = class {
|
|
|
198
208
|
clearTimeout(pending.timer);
|
|
199
209
|
this.pendingRequests.delete(data.id);
|
|
200
210
|
if (data.error) {
|
|
201
|
-
|
|
211
|
+
const error = new Error(data.error.message);
|
|
212
|
+
import_internal_slardar.slardar.captureException(error, {
|
|
213
|
+
source: "aily-web-sdk",
|
|
214
|
+
module: "message-bridge",
|
|
215
|
+
errorCode: data.error.code
|
|
216
|
+
});
|
|
217
|
+
pending.reject(error);
|
|
202
218
|
} else {
|
|
203
219
|
pending.resolve(data.data);
|
|
204
220
|
}
|
|
@@ -221,22 +237,22 @@ var MessageBridge = class {
|
|
|
221
237
|
return data.type === "event";
|
|
222
238
|
}
|
|
223
239
|
};
|
|
240
|
+
__name(_MessageBridge, "MessageBridge");
|
|
241
|
+
var MessageBridge = _MessageBridge;
|
|
224
242
|
|
|
225
243
|
// src/iframe-manager.ts
|
|
226
|
-
var
|
|
227
|
-
static {
|
|
228
|
-
__name(this, "IframeManager");
|
|
229
|
-
}
|
|
230
|
-
iframe = null;
|
|
231
|
-
messageBridge;
|
|
232
|
-
config;
|
|
233
|
-
channelType;
|
|
234
|
-
root;
|
|
235
|
-
deviceDetector = null;
|
|
244
|
+
var _IframeManager = class _IframeManager {
|
|
236
245
|
constructor(root, config) {
|
|
246
|
+
__publicField(this, "iframe", null);
|
|
247
|
+
__publicField(this, "messageBridge");
|
|
248
|
+
__publicField(this, "config");
|
|
249
|
+
__publicField(this, "channelType");
|
|
250
|
+
__publicField(this, "root");
|
|
251
|
+
__publicField(this, "deviceDetector", null);
|
|
237
252
|
this.root = root;
|
|
238
253
|
this.config = config;
|
|
239
|
-
|
|
254
|
+
const defaultChannel = config.anonymous ? DEFAULT_ANONYMOUS_CHANNEL_TYPE : DEFAULT_CHANNEL_TYPE;
|
|
255
|
+
this.channelType = config.common?.channelType || defaultChannel;
|
|
240
256
|
this.messageBridge = new MessageBridge(getIframeBaseURL());
|
|
241
257
|
}
|
|
242
258
|
/**
|
|
@@ -282,7 +298,7 @@ var IframeManager = class {
|
|
|
282
298
|
this.deviceDetector.destroy();
|
|
283
299
|
this.deviceDetector = null;
|
|
284
300
|
}
|
|
285
|
-
if (this.iframe
|
|
301
|
+
if (this.iframe?.parentNode) {
|
|
286
302
|
this.iframe.parentNode.removeChild(this.iframe);
|
|
287
303
|
}
|
|
288
304
|
this.iframe = null;
|
|
@@ -296,16 +312,25 @@ var IframeManager = class {
|
|
|
296
312
|
*/
|
|
297
313
|
buildIframeURL() {
|
|
298
314
|
const baseURL = getIframeBaseURL();
|
|
299
|
-
const { events, anonymous: _anonymous, ...configWithoutEvents } = this.config;
|
|
315
|
+
const { events: _events, anonymous: _anonymous, ...configWithoutEvents } = this.config;
|
|
316
|
+
const editorConfig = configWithoutEvents.editor?.skill ? {
|
|
317
|
+
...configWithoutEvents.editor,
|
|
318
|
+
skill: {
|
|
319
|
+
type: "custom",
|
|
320
|
+
builtinType: "unspecified",
|
|
321
|
+
...configWithoutEvents.editor.skill
|
|
322
|
+
}
|
|
323
|
+
} : configWithoutEvents.editor;
|
|
300
324
|
const iframeConfig = {
|
|
301
325
|
...configWithoutEvents,
|
|
302
326
|
common: {
|
|
303
327
|
...configWithoutEvents.common,
|
|
304
328
|
channelType: this.channelType,
|
|
305
|
-
resourceType: this.channelType
|
|
306
|
-
}
|
|
329
|
+
resourceType: configWithoutEvents.common?.resourceType || this.channelType
|
|
330
|
+
},
|
|
331
|
+
editor: editorConfig
|
|
307
332
|
};
|
|
308
|
-
const configBase64 = btoa(JSON.stringify(iframeConfig));
|
|
333
|
+
const configBase64 = btoa(encodeURIComponent(JSON.stringify(iframeConfig)));
|
|
309
334
|
const params = new URLSearchParams({
|
|
310
335
|
appKey: this.config.appKey,
|
|
311
336
|
config: encodeURIComponent(configBase64)
|
|
@@ -318,7 +343,12 @@ var IframeManager = class {
|
|
|
318
343
|
waitForReady() {
|
|
319
344
|
return new Promise((resolve, reject) => {
|
|
320
345
|
const timeout = setTimeout(() => {
|
|
321
|
-
|
|
346
|
+
const error = new Error("Iframe ready timeout");
|
|
347
|
+
import_internal_slardar2.slardar.captureException(error, {
|
|
348
|
+
source: "aily-web-sdk",
|
|
349
|
+
module: "iframe-ready"
|
|
350
|
+
});
|
|
351
|
+
reject(error);
|
|
322
352
|
}, 3e4);
|
|
323
353
|
const checkMessage = /* @__PURE__ */ __name((event) => {
|
|
324
354
|
if (event.data?.messageBizId === MESSAGE_BIZ_ID && event.data?.type === "event" && event.data?.eventName === "ready") {
|
|
@@ -339,30 +369,36 @@ var IframeManager = class {
|
|
|
339
369
|
if (events.onError) {
|
|
340
370
|
this.messageBridge.onEvent("error", (data) => {
|
|
341
371
|
const msg = data?.message || "Unknown error";
|
|
342
|
-
events.onError(new Error(msg));
|
|
372
|
+
events.onError?.(new Error(msg));
|
|
343
373
|
});
|
|
344
374
|
}
|
|
345
375
|
if (events.onMessage) {
|
|
346
376
|
this.messageBridge.onEvent("onMessage", events.onMessage);
|
|
347
377
|
}
|
|
378
|
+
if (events.onInited) {
|
|
379
|
+
this.messageBridge.onEvent("inited", events.onInited);
|
|
380
|
+
}
|
|
381
|
+
if (events.onInitedWithWelcome) {
|
|
382
|
+
this.messageBridge.onEvent("initedWithWelcome", events.onInitedWithWelcome);
|
|
383
|
+
}
|
|
384
|
+
if (events.onClose) {
|
|
385
|
+
this.messageBridge.onEvent("onClose", events.onClose);
|
|
386
|
+
}
|
|
348
387
|
}
|
|
349
388
|
reportInitMetrics(initStart) {
|
|
350
389
|
try {
|
|
351
390
|
const cost = performance.now() - initStart;
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
}
|
|
364
|
-
});
|
|
365
|
-
}
|
|
391
|
+
import_internal_slardar2.slardar.sendEvent({
|
|
392
|
+
name: "cui_init_total",
|
|
393
|
+
metrics: {
|
|
394
|
+
cost
|
|
395
|
+
},
|
|
396
|
+
categories: {
|
|
397
|
+
arch: "iframe-static",
|
|
398
|
+
appKey: this.config.appKey,
|
|
399
|
+
channelType: this.channelType
|
|
400
|
+
}
|
|
401
|
+
});
|
|
366
402
|
} catch {
|
|
367
403
|
}
|
|
368
404
|
}
|
|
@@ -402,6 +438,8 @@ var IframeManager = class {
|
|
|
402
438
|
};
|
|
403
439
|
}
|
|
404
440
|
};
|
|
441
|
+
__name(_IframeManager, "IframeManager");
|
|
442
|
+
var IframeManager = _IframeManager;
|
|
405
443
|
|
|
406
444
|
// src/index.ts
|
|
407
445
|
async function initAilyChat(root, config) {
|
|
@@ -411,6 +449,8 @@ async function initAilyChat(root, config) {
|
|
|
411
449
|
__name(initAilyChat, "initAilyChat");
|
|
412
450
|
// Annotate the CommonJS export names for ESM import in node:
|
|
413
451
|
0 && (module.exports = {
|
|
452
|
+
DEFAULT_ANONYMOUS_CHANNEL_TYPE,
|
|
453
|
+
DEFAULT_CHANNEL_TYPE,
|
|
414
454
|
IFRAME_BASE_URL,
|
|
415
455
|
MESSAGE_TIMEOUT,
|
|
416
456
|
getIframeBaseURL,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/constants.ts","../src/device-detector.ts","../src/message-bridge.ts","../src/iframe-manager.ts"],"sourcesContent":["/*\n * index.ts\n * Author: perterpon.wang<perterpon.wang@bytedance.com>\n * Create: Tue Feb 10 2026\n */\n\nimport { IframeManager } from './iframe-manager';\n\nimport type { ChatPanel, WebSDKConfig } from './types/config';\n\n/**\n * Initialize Aily Chat via iframe\n * This is the primary method for initializing the chat panel\n * @param root - Container element for the iframe\n * @param config - Configuration options\n * @returns ChatPanel instance for controlling the chat\n */\nexport async function initAilyChat(root: HTMLElement, config: WebSDKConfig): Promise<ChatPanel> {\n const manager = new IframeManager(root, config);\n return manager.init();\n}\n\n// Export types\nexport type {\n ChatPanel,\n WebSDKConfig,\n WebSDKCommonConfig,\n WebSDKConversionConfig,\n WebSDKEditorConfig,\n WebSDKEvents,\n} from './types/config';\n\n// Export constants\nexport { getIframeBaseURL, IFRAME_BASE_URL, MESSAGE_TIMEOUT } from './constants';\n\n// Export message types\nexport type {\n IframeMessageType,\n IframeRequestMessage,\n IframeResponseMessage,\n IframeEventMessage,\n IframeMessage,\n} from './types/message';\n","/**\n * Constants for Aily Web SDK\n */\n\n/** Message business ID for postMessage identification */\nexport const MESSAGE_BIZ_ID = 'cui-sdk-message' as const;\n\n/** Iframe base URLs by environment */\nexport const IFRAME_BASE_URL: Record<string, string> = {\n production: 'https://aily.feishu.cn/cui',\n staging: 'https://aily.feishu-pre.cn/cui',\n boe: 'https://aily.feishu-boe.cn/cui',\n development: 'http://localhost:8080/cui',\n};\n\n/**\n * Get iframe base URL based on environment\n */\nexport function getIframeBaseURL(env?: string): string {\n if (env && IFRAME_BASE_URL[env]) {\n return IFRAME_BASE_URL[env];\n }\n\n // Auto-detect based on hostname\n // MiaoDa 宿主域名: miaoda.feishu[-env].cn, *.aiforce[-env][-preview].bytedance.net, *.aiforce.run, *.aiforce.cloud\n const hostname = typeof window !== 'undefined' ? window.location.hostname : '';\n\n if (hostname.includes('aiforce-boe')) {\n return IFRAME_BASE_URL.boe;\n }\n if (hostname.includes('aiforce-pre')) {\n return IFRAME_BASE_URL.staging;\n }\n if (hostname.includes('localhost') || hostname.includes('127.0.0.1')) {\n return IFRAME_BASE_URL.development;\n }\n\n return IFRAME_BASE_URL.production;\n}\n\n/** Timeout for postMessage requests (30 seconds) */\nexport const MESSAGE_TIMEOUT = 30 * 1000;\n\n/** Miaoda 渠道标识(本 SDK 为 Miaoda 专用包,不允许外部自定义) */\nexport const CHANNEL_TYPE = 'MIAODA_CUI_SDK';\nexport const ANONYMOUS_CHANNEL_TYPE = 'MIAODA_ANONYMOUS_CUI_SDK';\n\n/** Allowed origins for postMessage (cui-iframe 的 origin) */\nexport const ALLOWED_ORIGINS = [\n 'https://aily.feishu.cn',\n 'https://aily.feishu-pre.cn',\n 'https://aily.feishu-boe.cn',\n 'http://localhost:3000',\n 'http://localhost:5173',\n 'http://localhost:8080',\n];\n","const MOBILE_BREAKPOINT = 768;\n\ntype DeviceType = 'pc' | 'mobile';\ntype DeviceChangeCallback = (device: DeviceType) => void;\n\n/**\n * 设备类型自动检测器\n *\n * 使用 matchMedia + 768px 断点检测设备类型,与 miaoda useIsMobile 逻辑一致。\n * 检测在宿主页面(而非 iframe)中执行,结果通过 postMessage 推送给 iframe。\n */\nexport class DeviceDetector {\n private mediaQuery: MediaQueryList;\n private currentDevice: DeviceType;\n private callback: DeviceChangeCallback | null = null;\n private handleChange: (() => void) | null = null;\n\n constructor() {\n this.mediaQuery = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);\n this.currentDevice = window.innerWidth < MOBILE_BREAKPOINT ? 'mobile' : 'pc';\n }\n\n getDevice(): DeviceType {\n return this.currentDevice;\n }\n\n observe(callback: DeviceChangeCallback): void {\n this.callback = callback;\n this.handleChange = () => {\n const newDevice: DeviceType = window.innerWidth < MOBILE_BREAKPOINT ? 'mobile' : 'pc';\n if (newDevice !== this.currentDevice) {\n this.currentDevice = newDevice;\n this.callback?.(newDevice);\n }\n };\n this.mediaQuery.addEventListener('change', this.handleChange);\n }\n\n destroy(): void {\n if (this.handleChange) {\n this.mediaQuery.removeEventListener('change', this.handleChange);\n this.handleChange = null;\n }\n this.callback = null;\n }\n}\n","import { MESSAGE_TIMEOUT, ALLOWED_ORIGINS } from './constants';\nimport {\n MESSAGE_BIZ_ID,\n type IframeMessage,\n type IframeRequestMessage,\n type IframeResponseMessage,\n type IframeEventMessage,\n type IframeMessageType,\n} from './types/message';\n\ninterface PendingRequest {\n resolve: (value: unknown) => void;\n reject: (reason: Error) => void;\n timer: ReturnType<typeof setTimeout>;\n}\n\n/**\n * MessageBridge handles postMessage communication between parent and iframe\n */\nexport class MessageBridge {\n private iframe: HTMLIFrameElement | null = null;\n private requestId = 0;\n private pendingRequests = new Map<number, PendingRequest>();\n private eventHandlers = new Map<string, ((data: unknown) => void)[]>();\n private targetOrigin: string;\n\n constructor(targetOrigin: string) {\n this.targetOrigin = targetOrigin;\n this.handleMessage = this.handleMessage.bind(this);\n }\n\n /**\n * Attach to an iframe and start listening for messages\n */\n attach(iframe: HTMLIFrameElement): void {\n this.iframe = iframe;\n window.addEventListener('message', this.handleMessage);\n }\n\n /**\n * Detach from iframe and clean up\n */\n detach(): void {\n window.removeEventListener('message', this.handleMessage);\n this.iframe = null;\n // Clear all pending requests\n this.pendingRequests.forEach(({ reject }) => {\n reject(new Error('MessageBridge detached'));\n });\n this.pendingRequests.clear();\n }\n\n /**\n * Send a message to the iframe and wait for response\n */\n send<T = unknown>(type: IframeMessageType, data?: unknown): Promise<T> {\n return new Promise((resolve, reject) => {\n if (!this.iframe?.contentWindow) {\n reject(new Error('Iframe not attached'));\n return;\n }\n\n this.requestId += 1;\n const id = this.requestId;\n const message: IframeRequestMessage = {\n messageBizId: MESSAGE_BIZ_ID,\n id,\n type,\n data,\n timestamp: Date.now(),\n isRequest: true,\n };\n\n const timer = setTimeout(() => {\n this.pendingRequests.delete(id);\n reject(new Error(`Message timeout: ${type}`));\n }, MESSAGE_TIMEOUT);\n\n this.pendingRequests.set(id, { resolve: resolve as (value: unknown) => void, reject, timer });\n\n this.iframe.contentWindow.postMessage(message, this.targetOrigin);\n });\n }\n\n /**\n * Register an event handler\n */\n onEvent(eventName: string, handler: (data: unknown) => void): void {\n const handlers = this.eventHandlers.get(eventName) || [];\n handlers.push(handler);\n this.eventHandlers.set(eventName, handlers);\n }\n\n /**\n * Remove an event handler\n */\n offEvent(eventName: string, handler: (data: unknown) => void): void {\n const handlers = this.eventHandlers.get(eventName) || [];\n const index = handlers.indexOf(handler);\n if (index > -1) {\n handlers.splice(index, 1);\n this.eventHandlers.set(eventName, handlers);\n }\n }\n\n private handleMessage(event: MessageEvent<IframeMessage>): void {\n // Validate origin\n if (!ALLOWED_ORIGINS.includes(event.origin)) {\n return;\n }\n\n const { data } = event;\n\n // Validate message format\n if (data?.messageBizId !== MESSAGE_BIZ_ID) {\n return;\n }\n\n // Handle response messages\n if (this.isResponseMessage(data)) {\n const pending = this.pendingRequests.get(data.id);\n if (pending) {\n clearTimeout(pending.timer);\n this.pendingRequests.delete(data.id);\n\n if (data.error) {\n pending.reject(new Error(data.error.message));\n } else {\n pending.resolve(data.data);\n }\n }\n }\n\n // Handle event messages\n if (this.isEventMessage(data)) {\n const handlers = this.eventHandlers.get(data.eventName) || [];\n handlers.forEach((handler) => {\n try {\n handler(data.data);\n } catch {\n // Ignore handler errors\n }\n });\n }\n }\n\n private isResponseMessage(data: IframeMessage): data is IframeResponseMessage {\n return 'isResponse' in data && data.isResponse === true;\n }\n\n private isEventMessage(data: IframeMessage): data is IframeEventMessage {\n return data.type === 'event';\n }\n}\n","import { getIframeBaseURL, MESSAGE_BIZ_ID, CHANNEL_TYPE, ANONYMOUS_CHANNEL_TYPE } from './constants';\nimport { DeviceDetector } from './device-detector';\nimport { MessageBridge } from './message-bridge';\n\nimport type { WebSDKConfig, ChatPanel } from './types/config';\n\n/**\n * IframeManager handles iframe creation and lifecycle\n *\n * iframe 采用自初始化模式:配置通过 URL hash 传入,iframe 加载后自行解析并渲染,\n * 与旧版 copilot iframe 的行为保持一致。宿主通过 postMessage 进行后续操作。\n */\nexport class IframeManager {\n private iframe: HTMLIFrameElement | null = null;\n private messageBridge: MessageBridge;\n private config: WebSDKConfig;\n private channelType: string;\n private root: HTMLElement;\n private deviceDetector: DeviceDetector | null = null;\n\n constructor(root: HTMLElement, config: WebSDKConfig) {\n this.root = root;\n this.config = config;\n this.channelType = config.anonymous ? ANONYMOUS_CHANNEL_TYPE : CHANNEL_TYPE;\n this.messageBridge = new MessageBridge(getIframeBaseURL());\n }\n\n /**\n * Initialize the iframe and establish communication\n * 配置通过 URL hash 编码传入 iframe,iframe 加载后自行初始化\n */\n async init(): Promise<ChatPanel> {\n const initStart = performance.now();\n\n // Auto-detect device if not explicitly provided\n if (!this.config.device) {\n this.deviceDetector = new DeviceDetector();\n this.config.device = this.deviceDetector.getDevice();\n }\n\n // Create iframe element\n this.iframe = document.createElement('iframe');\n this.iframe.src = this.buildIframeURL();\n this.iframe.style.width = '100%';\n this.iframe.style.height = '100%';\n this.iframe.style.border = 'none';\n this.iframe.allow = 'microphone; clipboard-write';\n\n // Attach to DOM\n this.root.appendChild(this.iframe);\n\n // Attach message bridge (开始监听消息)\n this.messageBridge.attach(this.iframe);\n\n // Setup event handlers (在 iframe 初始化完成之前就开始监听,确保不丢失事件)\n this.setupEventHandlers();\n\n // Listen for device changes and push updates to iframe\n if (this.deviceDetector) {\n this.deviceDetector.observe((newDevice) => {\n this.messageBridge.send('updateConfig', { device: newDevice });\n });\n }\n\n // Wait for iframe to finish initialization (iframe 自行读取 URL 配置并渲染,完成后发送 ready 事件)\n await this.waitForReady();\n\n // 上报 cui_init_total 耗时(iframe 创建 → init 完成)\n this.reportInitMetrics(initStart);\n\n // Return ChatPanel interface\n return this.createChatPanel();\n }\n\n /**\n * Destroy the iframe and cleanup\n */\n async destroy(): Promise<void> {\n try {\n await this.messageBridge.send('destroy');\n } catch {\n // Ignore errors during destroy\n }\n\n this.messageBridge.detach();\n\n if (this.deviceDetector) {\n this.deviceDetector.destroy();\n this.deviceDetector = null;\n }\n\n if (this.iframe && this.iframe.parentNode) {\n this.iframe.parentNode.removeChild(this.iframe);\n }\n\n this.iframe = null;\n }\n\n /**\n * 构建 iframe URL,将配置编码到 hash 中\n * 格式: {baseURL}#{params} 其中 config 为 base64(JSON.stringify(configWithoutEvents))\n *\n * 鉴权说明:SDK 不参与 token 获取/传递。iframe 自行处理飞书登录流程,\n * 允许用户在 iframe 中独立登录。匿名渠道跳过登录检查。\n */\n private buildIframeURL(): string {\n const baseURL = getIframeBaseURL();\n\n // 移除 events(函数无法序列化)\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { events, anonymous: _anonymous, ...configWithoutEvents } = this.config;\n // 注入 channelType/resourceType(SDK 内部控制,不暴露给消费者)\n const iframeConfig = {\n ...configWithoutEvents,\n common: {\n ...configWithoutEvents.common,\n channelType: this.channelType,\n resourceType: this.channelType,\n },\n };\n const configBase64 = btoa(JSON.stringify(iframeConfig));\n\n const params = new URLSearchParams({\n appKey: this.config.appKey,\n config: encodeURIComponent(configBase64),\n });\n\n return `${baseURL}#${params.toString()}`;\n }\n\n /**\n * 等待 iframe 自行初始化完成后发送 ready 事件\n */\n private waitForReady(): Promise<void> {\n return new Promise((resolve, reject) => {\n const timeout = setTimeout(() => {\n reject(new Error('Iframe ready timeout'));\n }, 30000);\n\n const checkMessage = (event: MessageEvent) => {\n if (\n event.data?.messageBizId === MESSAGE_BIZ_ID &&\n event.data?.type === 'event' &&\n event.data?.eventName === 'ready'\n ) {\n clearTimeout(timeout);\n window.removeEventListener('message', checkMessage);\n resolve();\n }\n };\n\n window.addEventListener('message', checkMessage);\n });\n }\n\n private setupEventHandlers(): void {\n const { events } = this.config;\n if (!events) return;\n\n if (events.onReady) {\n this.messageBridge.onEvent('ready', events.onReady);\n }\n\n if (events.onError) {\n this.messageBridge.onEvent('error', (data: unknown) => {\n const msg = (data as { message?: string })?.message || 'Unknown error';\n events.onError!(new Error(msg));\n });\n }\n\n if (events.onMessage) {\n this.messageBridge.onEvent('onMessage', events.onMessage);\n }\n }\n\n private reportInitMetrics(initStart: number): void {\n try {\n const cost = performance.now() - initStart;\n const Slardar = (window as unknown as Record<string, unknown>).__Slardar as\n | { sendEvent?: (event: Record<string, unknown>) => void }\n | undefined;\n if (Slardar?.sendEvent) {\n Slardar.sendEvent({\n name: 'cui_init_total',\n metrics: { cost },\n categories: {\n arch: 'iframe-static',\n appKey: this.config.appKey,\n channelType: this.channelType,\n },\n });\n }\n } catch {\n // 性能上报失败不影响功能\n }\n }\n\n private createChatPanel(): ChatPanel {\n return {\n sendMessage: async (data) => {\n await this.messageBridge.send('sendMessage', data);\n },\n clear: async () => {\n await this.messageBridge.send('clear');\n },\n cancelMessage: async (messageItem) => {\n await this.messageBridge.send('cancelMessage', messageItem);\n },\n clearAndStop: async () => {\n await this.messageBridge.send('clearAndStop');\n },\n updateWelcomeMessage: async () => {\n await this.messageBridge.send('updateWelcomeMessage');\n },\n updateConfig: async (config) => {\n await this.messageBridge.send('updateConfig', config);\n },\n observeSkill: async (skillId, name, skillType) => {\n await this.messageBridge.send('observeSkill', { skillId, name, skillType });\n },\n setCurrentSkill: async (skill) => {\n await this.messageBridge.send('setCurrentSkill', skill);\n },\n destroy: async () => {\n await this.destroy();\n },\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;ACKO,IAAMA,iBAAiB;AAGvB,IAAMC,kBAA0C;EACrDC,YAAY;EACZC,SAAS;EACTC,KAAK;EACLC,aAAa;AACf;AAKO,SAASC,iBAAiBC,KAAY;AAC3C,MAAIA,OAAON,gBAAgBM,GAAAA,GAAM;AAC/B,WAAON,gBAAgBM,GAAAA;EACzB;AAIA,QAAMC,WAAW,OAAOC,WAAW,cAAcA,OAAOC,SAASF,WAAW;AAE5E,MAAIA,SAASG,SAAS,aAAA,GAAgB;AACpC,WAAOV,gBAAgBG;EACzB;AACA,MAAII,SAASG,SAAS,aAAA,GAAgB;AACpC,WAAOV,gBAAgBE;EACzB;AACA,MAAIK,SAASG,SAAS,WAAA,KAAgBH,SAASG,SAAS,WAAA,GAAc;AACpE,WAAOV,gBAAgBI;EACzB;AAEA,SAAOJ,gBAAgBC;AACzB;AApBgBI;AAuBT,IAAMM,kBAAkB,KAAK;AAG7B,IAAMC,eAAe;AACrB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","getIframeBaseURL","env","hostname","window","location","includes","MESSAGE_TIMEOUT","CHANNEL_TYPE","ANONYMOUS_CHANNEL_TYPE","ALLOWED_ORIGINS","MOBILE_BREAKPOINT","DeviceDetector","mediaQuery","currentDevice","callback","handleChange","window","matchMedia","innerWidth","getDevice","observe","newDevice","addEventListener","destroy","removeEventListener","MessageBridge","iframe","requestId","pendingRequests","Map","eventHandlers","targetOrigin","handleMessage","bind","attach","window","addEventListener","detach","removeEventListener","forEach","reject","Error","clear","send","type","data","Promise","resolve","contentWindow","id","message","messageBizId","MESSAGE_BIZ_ID","timestamp","Date","now","isRequest","timer","setTimeout","delete","MESSAGE_TIMEOUT","set","postMessage","onEvent","eventName","handler","handlers","get","push","offEvent","index","indexOf","splice","event","ALLOWED_ORIGINS","includes","origin","isResponseMessage","pending","clearTimeout","error","isEventMessage","isResponse","IframeManager","iframe","messageBridge","config","channelType","root","deviceDetector","anonymous","ANONYMOUS_CHANNEL_TYPE","CHANNEL_TYPE","MessageBridge","getIframeBaseURL","init","initStart","performance","now","device","DeviceDetector","getDevice","document","createElement","src","buildIframeURL","style","width","height","border","allow","appendChild","attach","setupEventHandlers","observe","newDevice","send","waitForReady","reportInitMetrics","createChatPanel","destroy","detach","parentNode","removeChild","baseURL","events","_anonymous","configWithoutEvents","iframeConfig","common","resourceType","configBase64","btoa","JSON","stringify","params","URLSearchParams","appKey","encodeURIComponent","toString","Promise","resolve","reject","timeout","setTimeout","Error","checkMessage","event","data","messageBizId","MESSAGE_BIZ_ID","type","eventName","clearTimeout","window","removeEventListener","addEventListener","onReady","onEvent","onError","msg","message","onMessage","cost","Slardar","__Slardar","sendEvent","name","metrics","categories","arch","sendMessage","clear","cancelMessage","messageItem","clearAndStop","updateWelcomeMessage","updateConfig","observeSkill","skillId","skillType","setCurrentSkill","skill","initAilyChat","root","config","manager","IframeManager","init"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/constants.ts","../src/iframe-manager.ts","../src/device-detector.ts","../src/message-bridge.ts"],"sourcesContent":["/*\n * index.ts\n * Author: perterpon.wang<perterpon.wang@bytedance.com>\n * Create: Tue Feb 10 2026\n */\n\nimport { IframeManager } from \"./iframe-manager\";\n\nimport type { ChatPanel, WebSDKConfig } from \"./types/config\";\n\n/**\n * Initialize Aily Chat via iframe\n * This is the primary method for initializing the chat panel\n * @param root - Container element for the iframe\n * @param config - Configuration options\n * @returns ChatPanel instance for controlling the chat\n */\nexport async function initAilyChat(\n\troot: HTMLElement,\n\tconfig: WebSDKConfig,\n): Promise<ChatPanel> {\n\tconst manager = new IframeManager(root, config);\n\treturn manager.init();\n}\n\n// Export constants\nexport {\n\tDEFAULT_ANONYMOUS_CHANNEL_TYPE,\n\tDEFAULT_CHANNEL_TYPE,\n\tgetIframeBaseURL,\n\tIFRAME_BASE_URL,\n\tMESSAGE_TIMEOUT,\n} from \"./constants\";\n// Export types\nexport type {\n\tChatPanel,\n\tWebSDKCommonConfig,\n\tWebSDKConfig,\n\tWebSDKConversionConfig,\n\tWebSDKEditorConfig,\n\tWebSDKEvents,\n\tWebSDKSkillInfo,\n} from \"./types/config\";\n\n// Export message types\nexport type {\n\tIframeEventMessage,\n\tIframeMessage,\n\tIframeMessageType,\n\tIframeRequestMessage,\n\tIframeResponseMessage,\n} from \"./types/message\";\n","/**\n * Constants for Aily Web SDK\n */\n\n/** Message business ID for postMessage identification */\nexport const MESSAGE_BIZ_ID = \"cui-sdk-message\" as const;\n\n/** Iframe base URLs by environment */\nexport const IFRAME_BASE_URL: Record<string, string> = {\n\tproduction: \"https://aily.feishu.cn/cui\",\n\tstaging: \"https://aily.feishu-pre.cn/cui\",\n\tboe: \"https://aily.feishu-boe.cn/cui\",\n\tdevelopment: \"http://localhost:8080/cui\",\n};\n\n/**\n * Get iframe base URL based on environment\n */\nexport function getIframeBaseURL(env?: string): string {\n\tif (env && IFRAME_BASE_URL[env]) {\n\t\treturn IFRAME_BASE_URL[env];\n\t}\n\n\t// Auto-detect based on hostname\n\t// MiaoDa 宿主域名: miaoda.feishu[-env].cn, *.aiforce[-env][-preview].bytedance.net, *.force-pre.feishuapp.net, *.fsapp.kundou.cn, *.aiforce.run, *.aiforce.cloud\n\tconst hostname =\n\t\ttypeof window !== \"undefined\" ? window.location.hostname : \"\";\n\n\tif (hostname.includes(\"aiforce-boe\")) {\n\t\treturn IFRAME_BASE_URL.boe;\n\t}\n\tif (\n\t\thostname.includes(\"aiforce-pre\") ||\n\t\thostname.includes(\"force-pre.feishuapp.net\") ||\n\t\thostname.includes(\"fsapp.kundou.cn\")\n\t) {\n\t\treturn IFRAME_BASE_URL.staging;\n\t}\n\tif (hostname.includes(\"localhost\") || hostname.includes(\"127.0.0.1\")) {\n\t\treturn IFRAME_BASE_URL.development;\n\t}\n\n\treturn IFRAME_BASE_URL.production;\n}\n\n/** Timeout for postMessage requests (30 seconds) */\nexport const MESSAGE_TIMEOUT = 30 * 1000;\n\n/** Miaoda 渠道标识默认值(用户可通过 config.common.channelType 覆盖) */\nexport const DEFAULT_CHANNEL_TYPE = \"MIAODA_CUI_SDK\";\nexport const DEFAULT_ANONYMOUS_CHANNEL_TYPE = \"MIAODA_ANONYMOUS_CUI_SDK\";\n\n/** Allowed origins for postMessage (cui-iframe 的 origin) */\nexport const ALLOWED_ORIGINS = [\n\t\"https://aily.feishu.cn\",\n\t\"https://aily.feishu-pre.cn\",\n\t\"https://aily.feishu-boe.cn\",\n\t\"http://localhost:3000\",\n\t\"http://localhost:5173\",\n\t\"http://localhost:8080\",\n];\n","import {\n\tDEFAULT_ANONYMOUS_CHANNEL_TYPE,\n\tDEFAULT_CHANNEL_TYPE,\n\tgetIframeBaseURL,\n\tMESSAGE_BIZ_ID,\n} from \"./constants\";\nimport { slardar } from \"@lark-apaas/internal-slardar\";\nimport { DeviceDetector } from \"./device-detector\";\nimport { MessageBridge } from \"./message-bridge\";\n\nimport type { ChatPanel, WebSDKConfig } from \"./types/config\";\n\n/**\n * IframeManager handles iframe creation and lifecycle\n *\n * iframe 采用自初始化模式:配置通过 URL hash 传入,iframe 加载后自行解析并渲染,\n * 与旧版 copilot iframe 的行为保持一致。宿主通过 postMessage 进行后续操作。\n */\nexport class IframeManager {\n\tprivate iframe: HTMLIFrameElement | null = null;\n\tprivate messageBridge: MessageBridge;\n\tprivate config: WebSDKConfig;\n\tprivate channelType: string;\n\tprivate root: HTMLElement;\n\tprivate deviceDetector: DeviceDetector | null = null;\n\n\tconstructor(root: HTMLElement, config: WebSDKConfig) {\n\t\tthis.root = root;\n\t\tthis.config = config;\n\t\tconst defaultChannel = config.anonymous\n\t\t\t? DEFAULT_ANONYMOUS_CHANNEL_TYPE\n\t\t\t: DEFAULT_CHANNEL_TYPE;\n\t\tthis.channelType = config.common?.channelType || defaultChannel;\n\t\tthis.messageBridge = new MessageBridge(getIframeBaseURL());\n\t}\n\n\t/**\n\t * Initialize the iframe and establish communication\n\t * 配置通过 URL hash 编码传入 iframe,iframe 加载后自行初始化\n\t */\n\tasync init(): Promise<ChatPanel> {\n\t\tconst initStart = performance.now();\n\n\t\t// Auto-detect device if not explicitly provided\n\t\tif (!this.config.device) {\n\t\t\tthis.deviceDetector = new DeviceDetector();\n\t\t\tthis.config.device = this.deviceDetector.getDevice();\n\t\t}\n\n\t\t// Create iframe element\n\t\tthis.iframe = document.createElement(\"iframe\");\n\t\tthis.iframe.src = this.buildIframeURL();\n\t\tthis.iframe.style.width = \"100%\";\n\t\tthis.iframe.style.height = \"100%\";\n\t\tthis.iframe.style.border = \"none\";\n\t\tthis.iframe.allow = \"microphone; clipboard-write\";\n\n\t\t// Attach to DOM\n\t\tthis.root.appendChild(this.iframe);\n\n\t\t// Attach message bridge (开始监听消息)\n\t\tthis.messageBridge.attach(this.iframe);\n\n\t\t// Setup event handlers (在 iframe 初始化完成之前就开始监听,确保不丢失事件)\n\t\tthis.setupEventHandlers();\n\n\t\t// Listen for device changes and push updates to iframe\n\t\tif (this.deviceDetector) {\n\t\t\tthis.deviceDetector.observe((newDevice) => {\n\t\t\t\tthis.messageBridge.send(\"updateConfig\", { device: newDevice });\n\t\t\t});\n\t\t}\n\n\t\t// Wait for iframe to finish initialization (iframe 自行读取 URL 配置并渲染,完成后发送 ready 事件)\n\t\tawait this.waitForReady();\n\n\t\t// 上报 cui_init_total 耗时(iframe 创建 → init 完成)\n\t\tthis.reportInitMetrics(initStart);\n\n\t\t// Return ChatPanel interface\n\t\treturn this.createChatPanel();\n\t}\n\n\t/**\n\t * Destroy the iframe and cleanup\n\t */\n\tasync destroy(): Promise<void> {\n\t\ttry {\n\t\t\tawait this.messageBridge.send(\"destroy\");\n\t\t} catch {\n\t\t\t// Ignore errors during destroy\n\t\t}\n\n\t\tthis.messageBridge.detach();\n\n\t\tif (this.deviceDetector) {\n\t\t\tthis.deviceDetector.destroy();\n\t\t\tthis.deviceDetector = null;\n\t\t}\n\n\t\tif (this.iframe?.parentNode) {\n\t\t\tthis.iframe.parentNode.removeChild(this.iframe);\n\t\t}\n\n\t\tthis.iframe = null;\n\t}\n\n\t/**\n\t * 构建 iframe URL,将配置编码到 hash 中\n\t * 格式: {baseURL}#{params} 其中 config 为 base64(JSON.stringify(configWithoutEvents))\n\t *\n\t * 鉴权说明:SDK 不参与 token 获取/传递。iframe 自行处理飞书登录流程,\n\t * 允许用户在 iframe 中独立登录。匿名渠道跳过登录检查。\n\t */\n\tprivate buildIframeURL(): string {\n\t\tconst baseURL = getIframeBaseURL();\n\n\t\t// 移除 events(函数无法序列化)\n\t\tconst {\n\t\t\tevents: _events,\n\t\t\tanonymous: _anonymous,\n\t\t\t...configWithoutEvents\n\t\t} = this.config;\n\t\t// 注入 channelType/resourceType(SDK 内部控制,不暴露给消费者)\n\t\t// 补全 editor.skill 硬编码字段\n\t\tconst editorConfig = configWithoutEvents.editor?.skill\n\t\t\t? {\n\t\t\t\t\t...configWithoutEvents.editor,\n\t\t\t\t\tskill: {\n\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\tbuiltinType: \"unspecified\",\n\t\t\t\t\t\t...configWithoutEvents.editor.skill,\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t: configWithoutEvents.editor;\n\n\t\tconst iframeConfig = {\n\t\t\t...configWithoutEvents,\n\t\t\tcommon: {\n\t\t\t\t...configWithoutEvents.common,\n\t\t\t\tchannelType: this.channelType,\n\t\t\t\tresourceType:\n\t\t\t\t\tconfigWithoutEvents.common?.resourceType || this.channelType,\n\t\t\t},\n\t\t\teditor: editorConfig,\n\t\t};\n\t\tconst configBase64 = btoa(encodeURIComponent(JSON.stringify(iframeConfig)));\n\n\t\tconst params = new URLSearchParams({\n\t\t\tappKey: this.config.appKey,\n\t\t\tconfig: encodeURIComponent(configBase64),\n\t\t});\n\n\t\treturn `${baseURL}#${params.toString()}`;\n\t}\n\n\t/**\n\t * 等待 iframe 自行初始化完成后发送 ready 事件\n\t */\n\tprivate waitForReady(): Promise<void> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst timeout = setTimeout(() => {\n\t\t\t\tconst error = new Error(\"Iframe ready timeout\");\n\t\t\t\tslardar.captureException(error, { source: 'aily-web-sdk', module: 'iframe-ready' });\n\t\t\t\treject(error);\n\t\t\t}, 30000);\n\n\t\t\tconst checkMessage = (event: MessageEvent) => {\n\t\t\t\tif (\n\t\t\t\t\tevent.data?.messageBizId === MESSAGE_BIZ_ID &&\n\t\t\t\t\tevent.data?.type === \"event\" &&\n\t\t\t\t\tevent.data?.eventName === \"ready\"\n\t\t\t\t) {\n\t\t\t\t\tclearTimeout(timeout);\n\t\t\t\t\twindow.removeEventListener(\"message\", checkMessage);\n\t\t\t\t\tresolve();\n\t\t\t\t}\n\t\t\t};\n\n\t\t\twindow.addEventListener(\"message\", checkMessage);\n\t\t});\n\t}\n\n\tprivate setupEventHandlers(): void {\n\t\tconst { events } = this.config;\n\t\tif (!events) return;\n\n\t\tif (events.onReady) {\n\t\t\tthis.messageBridge.onEvent(\"ready\", events.onReady);\n\t\t}\n\n\t\tif (events.onError) {\n\t\t\tthis.messageBridge.onEvent(\"error\", (data: unknown) => {\n\t\t\t\tconst msg = (data as { message?: string })?.message || \"Unknown error\";\n\t\t\t\tevents.onError?.(new Error(msg));\n\t\t\t});\n\t\t}\n\n\t\tif (events.onMessage) {\n\t\t\tthis.messageBridge.onEvent(\"onMessage\", events.onMessage);\n\t\t}\n\n\t\tif (events.onInited) {\n\t\t\tthis.messageBridge.onEvent(\n\t\t\t\t\"inited\",\n\t\t\t\tevents.onInited as (data: unknown) => void,\n\t\t\t);\n\t\t}\n\n\t\tif (events.onInitedWithWelcome) {\n\t\t\tthis.messageBridge.onEvent(\n\t\t\t\t\"initedWithWelcome\",\n\t\t\t\tevents.onInitedWithWelcome,\n\t\t\t);\n\t\t}\n\n\t\tif (events.onClose) {\n\t\t\tthis.messageBridge.onEvent(\"onClose\", events.onClose);\n\t\t}\n\t}\n\n\tprivate reportInitMetrics(initStart: number): void {\n\t\ttry {\n\t\t\tconst cost = performance.now() - initStart;\n\t\t\tslardar.sendEvent({\n\t\t\t\tname: \"cui_init_total\",\n\t\t\t\tmetrics: { cost },\n\t\t\t\tcategories: {\n\t\t\t\t\tarch: \"iframe-static\",\n\t\t\t\t\tappKey: this.config.appKey,\n\t\t\t\t\tchannelType: this.channelType,\n\t\t\t\t},\n\t\t\t});\n\t\t} catch {\n\t\t\t// 性能上报失败不影响功能\n\t\t}\n\t}\n\n\tprivate createChatPanel(): ChatPanel {\n\t\treturn {\n\t\t\tsendMessage: async (data) => {\n\t\t\t\tawait this.messageBridge.send(\"sendMessage\", data);\n\t\t\t},\n\t\t\tclear: async () => {\n\t\t\t\tawait this.messageBridge.send(\"clear\");\n\t\t\t},\n\t\t\tcancelMessage: async (messageItem) => {\n\t\t\t\tawait this.messageBridge.send(\"cancelMessage\", messageItem);\n\t\t\t},\n\t\t\tclearAndStop: async () => {\n\t\t\t\tawait this.messageBridge.send(\"clearAndStop\");\n\t\t\t},\n\t\t\tupdateWelcomeMessage: async () => {\n\t\t\t\tawait this.messageBridge.send(\"updateWelcomeMessage\");\n\t\t\t},\n\t\t\tupdateConfig: async (config) => {\n\t\t\t\tawait this.messageBridge.send(\"updateConfig\", config);\n\t\t\t},\n\t\t\tobserveSkill: async (skillId, name, skillType) => {\n\t\t\t\tawait this.messageBridge.send(\"observeSkill\", {\n\t\t\t\t\tskillId,\n\t\t\t\t\tname,\n\t\t\t\t\tskillType,\n\t\t\t\t});\n\t\t\t},\n\t\t\tsetCurrentSkill: async (skill) => {\n\t\t\t\tawait this.messageBridge.send(\"setCurrentSkill\", skill);\n\t\t\t},\n\t\t\tdestroy: async () => {\n\t\t\t\tawait this.destroy();\n\t\t\t},\n\t\t};\n\t}\n}\n","const MOBILE_BREAKPOINT = 768;\n\ntype DeviceType = 'pc' | 'mobile';\ntype DeviceChangeCallback = (device: DeviceType) => void;\n\n/**\n * 设备类型自动检测器\n *\n * 使用 matchMedia + 768px 断点检测设备类型,与 miaoda useIsMobile 逻辑一致。\n * 检测在宿主页面(而非 iframe)中执行,结果通过 postMessage 推送给 iframe。\n */\nexport class DeviceDetector {\n private mediaQuery: MediaQueryList;\n private currentDevice: DeviceType;\n private callback: DeviceChangeCallback | null = null;\n private handleChange: (() => void) | null = null;\n\n constructor() {\n this.mediaQuery = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);\n this.currentDevice = window.innerWidth < MOBILE_BREAKPOINT ? 'mobile' : 'pc';\n }\n\n getDevice(): DeviceType {\n return this.currentDevice;\n }\n\n observe(callback: DeviceChangeCallback): void {\n this.callback = callback;\n this.handleChange = () => {\n const newDevice: DeviceType = window.innerWidth < MOBILE_BREAKPOINT ? 'mobile' : 'pc';\n if (newDevice !== this.currentDevice) {\n this.currentDevice = newDevice;\n this.callback?.(newDevice);\n }\n };\n this.mediaQuery.addEventListener('change', this.handleChange);\n }\n\n destroy(): void {\n if (this.handleChange) {\n this.mediaQuery.removeEventListener('change', this.handleChange);\n this.handleChange = null;\n }\n this.callback = null;\n }\n}\n","import { MESSAGE_TIMEOUT, ALLOWED_ORIGINS } from './constants';\nimport {\n MESSAGE_BIZ_ID,\n type IframeMessage,\n type IframeRequestMessage,\n type IframeResponseMessage,\n type IframeEventMessage,\n type IframeMessageType,\n} from './types/message';\nimport { slardar } from '@lark-apaas/internal-slardar';\n\ninterface PendingRequest {\n resolve: (value: unknown) => void;\n reject: (reason: Error) => void;\n timer: ReturnType<typeof setTimeout>;\n}\n\n/**\n * MessageBridge handles postMessage communication between parent and iframe\n */\nexport class MessageBridge {\n private iframe: HTMLIFrameElement | null = null;\n private requestId = 0;\n private pendingRequests = new Map<number, PendingRequest>();\n private eventHandlers = new Map<string, ((data: unknown) => void)[]>();\n private targetOrigin: string;\n\n constructor(targetOrigin: string) {\n this.targetOrigin = targetOrigin;\n this.handleMessage = this.handleMessage.bind(this);\n }\n\n /**\n * Attach to an iframe and start listening for messages\n */\n attach(iframe: HTMLIFrameElement): void {\n this.iframe = iframe;\n window.addEventListener('message', this.handleMessage);\n }\n\n /**\n * Detach from iframe and clean up\n */\n detach(): void {\n window.removeEventListener('message', this.handleMessage);\n this.iframe = null;\n // Clear all pending requests\n this.pendingRequests.forEach(({ reject }) => {\n reject(new Error('MessageBridge detached'));\n });\n this.pendingRequests.clear();\n }\n\n /**\n * Send a message to the iframe and wait for response\n */\n send<T = unknown>(type: IframeMessageType, data?: unknown): Promise<T> {\n return new Promise((resolve, reject) => {\n if (!this.iframe?.contentWindow) {\n reject(new Error('Iframe not attached'));\n return;\n }\n\n this.requestId += 1;\n const id = this.requestId;\n const message: IframeRequestMessage = {\n messageBizId: MESSAGE_BIZ_ID,\n id,\n type,\n data,\n timestamp: Date.now(),\n isRequest: true,\n };\n\n const timer = setTimeout(() => {\n this.pendingRequests.delete(id);\n const error = new Error(`Message timeout: ${type}`);\n slardar.captureException(error, { source: 'aily-web-sdk', module: 'message-bridge', messageType: type });\n reject(error);\n }, MESSAGE_TIMEOUT);\n\n this.pendingRequests.set(id, { resolve: resolve as (value: unknown) => void, reject, timer });\n\n this.iframe.contentWindow.postMessage(message, this.targetOrigin);\n });\n }\n\n /**\n * Register an event handler\n */\n onEvent(eventName: string, handler: (data: unknown) => void): void {\n const handlers = this.eventHandlers.get(eventName) || [];\n handlers.push(handler);\n this.eventHandlers.set(eventName, handlers);\n }\n\n /**\n * Remove an event handler\n */\n offEvent(eventName: string, handler: (data: unknown) => void): void {\n const handlers = this.eventHandlers.get(eventName) || [];\n const index = handlers.indexOf(handler);\n if (index > -1) {\n handlers.splice(index, 1);\n this.eventHandlers.set(eventName, handlers);\n }\n }\n\n private handleMessage(event: MessageEvent<IframeMessage>): void {\n // Validate origin\n if (!ALLOWED_ORIGINS.includes(event.origin)) {\n return;\n }\n\n const { data } = event;\n\n // Validate message format\n if (data?.messageBizId !== MESSAGE_BIZ_ID) {\n return;\n }\n\n // Handle response messages\n if (this.isResponseMessage(data)) {\n const pending = this.pendingRequests.get(data.id);\n if (pending) {\n clearTimeout(pending.timer);\n this.pendingRequests.delete(data.id);\n\n if (data.error) {\n const error = new Error(data.error.message);\n slardar.captureException(error, { source: 'aily-web-sdk', module: 'message-bridge', errorCode: data.error.code });\n pending.reject(error);\n } else {\n pending.resolve(data.data);\n }\n }\n }\n\n // Handle event messages\n if (this.isEventMessage(data)) {\n const handlers = this.eventHandlers.get(data.eventName) || [];\n handlers.forEach((handler) => {\n try {\n handler(data.data);\n } catch {\n // Ignore handler errors\n }\n });\n }\n }\n\n private isResponseMessage(data: IframeMessage): data is IframeResponseMessage {\n return 'isResponse' in data && data.isResponse === true;\n }\n\n private isEventMessage(data: IframeMessage): data is IframeEventMessage {\n return data.type === 'event';\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;ACKO,IAAMA,iBAAiB;AAGvB,IAAMC,kBAA0C;EACtDC,YAAY;EACZC,SAAS;EACTC,KAAK;EACLC,aAAa;AACd;AAKO,SAASC,iBAAiBC,KAAY;AAC5C,MAAIA,OAAON,gBAAgBM,GAAAA,GAAM;AAChC,WAAON,gBAAgBM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,QAAMC,UAAU,IAAIC,cAAcH,MAAMC,MAAAA;AACxC,SAAOC,QAAQE,KAAI;AACpB;AANsBL;","names":["MESSAGE_BIZ_ID","IFRAME_BASE_URL","production","staging","boe","development","getIframeBaseURL","env","hostname","window","location","includes","MESSAGE_TIMEOUT","DEFAULT_CHANNEL_TYPE","DEFAULT_ANONYMOUS_CHANNEL_TYPE","ALLOWED_ORIGINS","import_internal_slardar","MOBILE_BREAKPOINT","DeviceDetector","mediaQuery","currentDevice","callback","handleChange","window","matchMedia","innerWidth","getDevice","observe","newDevice","addEventListener","destroy","removeEventListener","MessageBridge","targetOrigin","iframe","requestId","pendingRequests","Map","eventHandlers","handleMessage","bind","attach","window","addEventListener","detach","removeEventListener","forEach","reject","Error","clear","send","type","data","Promise","resolve","contentWindow","id","message","messageBizId","MESSAGE_BIZ_ID","timestamp","Date","now","isRequest","timer","setTimeout","delete","error","slardar","captureException","source","module","messageType","MESSAGE_TIMEOUT","set","postMessage","onEvent","eventName","handler","handlers","get","push","offEvent","index","indexOf","splice","event","ALLOWED_ORIGINS","includes","origin","isResponseMessage","pending","clearTimeout","errorCode","code","isEventMessage","isResponse","IframeManager","root","config","iframe","messageBridge","channelType","deviceDetector","defaultChannel","anonymous","DEFAULT_ANONYMOUS_CHANNEL_TYPE","DEFAULT_CHANNEL_TYPE","common","MessageBridge","getIframeBaseURL","init","initStart","performance","now","device","DeviceDetector","getDevice","document","createElement","src","buildIframeURL","style","width","height","border","allow","appendChild","attach","setupEventHandlers","observe","newDevice","send","waitForReady","reportInitMetrics","createChatPanel","destroy","detach","parentNode","removeChild","baseURL","events","_events","_anonymous","configWithoutEvents","editorConfig","editor","skill","type","builtinType","iframeConfig","resourceType","configBase64","btoa","encodeURIComponent","JSON","stringify","params","URLSearchParams","appKey","toString","Promise","resolve","reject","timeout","setTimeout","error","Error","slardar","captureException","source","module","checkMessage","event","data","messageBizId","MESSAGE_BIZ_ID","eventName","clearTimeout","window","removeEventListener","addEventListener","onReady","onEvent","onError","msg","message","onMessage","onInited","onInitedWithWelcome","onClose","cost","sendEvent","name","metrics","categories","arch","sendMessage","clear","cancelMessage","messageItem","clearAndStop","updateWelcomeMessage","updateConfig","observeSkill","skillId","skillType","setCurrentSkill","initAilyChat","root","config","manager","IframeManager","init"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
interface WebSDKCommonConfig {
|
|
8
8
|
/** Base URL for the Aily service */
|
|
9
|
-
baseURL
|
|
9
|
+
baseURL?: string;
|
|
10
10
|
/** Additional headers to send with requests */
|
|
11
11
|
headers?: Record<string, string>;
|
|
12
12
|
/** APAAS application info for API authentication */
|
|
@@ -15,6 +15,14 @@ interface WebSDKCommonConfig {
|
|
|
15
15
|
tenantID?: number;
|
|
16
16
|
tenantType?: number;
|
|
17
17
|
};
|
|
18
|
+
/** 渠道标识,不再硬编码 */
|
|
19
|
+
channelType?: string;
|
|
20
|
+
/** 资源类型,默认等于 channelType */
|
|
21
|
+
resourceType?: string;
|
|
22
|
+
/** 传给后端 API 的额外上下文 */
|
|
23
|
+
channelContext?: Record<string, unknown>;
|
|
24
|
+
/** 最小轮询间隔 (ms),默认 2000 */
|
|
25
|
+
minPollInterval?: number;
|
|
18
26
|
}
|
|
19
27
|
/**
|
|
20
28
|
* Event callbacks
|
|
@@ -26,6 +34,16 @@ interface WebSDKEvents {
|
|
|
26
34
|
onError?: (error: Error) => void;
|
|
27
35
|
/** Callback when a message is received */
|
|
28
36
|
onMessage?: (message: unknown) => void;
|
|
37
|
+
/** 会话创建完成、轮询开始后触发 */
|
|
38
|
+
onInited?: (data: {
|
|
39
|
+
sessionId: string;
|
|
40
|
+
conversationId: string;
|
|
41
|
+
handler: string;
|
|
42
|
+
}) => void;
|
|
43
|
+
/** 欢迎消息创建完成后触发 */
|
|
44
|
+
onInitedWithWelcome?: () => void;
|
|
45
|
+
/** 用户点击关闭按钮时触发 */
|
|
46
|
+
onClose?: () => void;
|
|
29
47
|
}
|
|
30
48
|
/**
|
|
31
49
|
* Conversion (conversation) configuration passed through to cui-sdk
|
|
@@ -41,19 +59,31 @@ interface WebSDKConversionConfig {
|
|
|
41
59
|
needAvatar?: boolean;
|
|
42
60
|
/** Whether to show feedback buttons. Default: true */
|
|
43
61
|
supportFeedback?: boolean;
|
|
44
|
-
/**
|
|
45
|
-
|
|
62
|
+
/** 是否显示欢迎语,默认 true */
|
|
63
|
+
needWelcomeMessage?: boolean;
|
|
64
|
+
/** 系统头像 URL(iframe 序列化限制,只支持 string) */
|
|
65
|
+
systemAvatar?: string;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Skill information for preset skill
|
|
69
|
+
* 字段名与 cui-sdk SkillBaseInfo 对齐,iframe 透传后直接消费
|
|
70
|
+
*/
|
|
71
|
+
interface WebSDKSkillInfo {
|
|
72
|
+
/** API 标识 */
|
|
73
|
+
apiID?: string;
|
|
46
74
|
}
|
|
47
75
|
/**
|
|
48
76
|
* Editor configuration passed through to cui-sdk
|
|
49
77
|
*/
|
|
50
78
|
interface WebSDKEditorConfig {
|
|
51
|
-
/** Interaction mode: 'v1' for classic, 'v2' for new interaction. Default: 'v1' */
|
|
52
|
-
interactionMode?: 'v1' | 'v2';
|
|
53
|
-
/** Auto skill selection. Default: true */
|
|
54
|
-
autoSkill?: boolean;
|
|
55
79
|
/** Whether to show the editor. Default: true */
|
|
56
80
|
display?: boolean;
|
|
81
|
+
/** 预设技能信息 */
|
|
82
|
+
skill?: WebSDKSkillInfo;
|
|
83
|
+
/** 是否显示顶部 header(含标题和新建对话按钮)。仅 V2 生效。默认: true */
|
|
84
|
+
needHeader?: boolean;
|
|
85
|
+
/** 对话标题,显示在 header 中。默认取应用名称 */
|
|
86
|
+
headerTitle?: string;
|
|
57
87
|
}
|
|
58
88
|
/**
|
|
59
89
|
* Main configuration interface for Web SDK
|
|
@@ -64,9 +94,7 @@ interface WebSDKConfig {
|
|
|
64
94
|
/** User identifier */
|
|
65
95
|
uuid?: string;
|
|
66
96
|
/** Device type */
|
|
67
|
-
device?:
|
|
68
|
-
/** Interaction mode: 'v1' for classic, 'v2' for new interaction */
|
|
69
|
-
interactionMode?: 'v1' | 'v2';
|
|
97
|
+
device?: "pc" | "mobile";
|
|
70
98
|
/**
|
|
71
99
|
* Enable anonymous mode (skips login check). Default: false
|
|
72
100
|
* 非匿名模式下,iframe 内自行完成飞书登录(用户在 iframe 中独立登录),
|
|
@@ -74,7 +102,7 @@ interface WebSDKConfig {
|
|
|
74
102
|
*/
|
|
75
103
|
anonymous?: boolean;
|
|
76
104
|
/** Common configuration */
|
|
77
|
-
common
|
|
105
|
+
common?: WebSDKCommonConfig;
|
|
78
106
|
/** Conversation/conversion configuration (avatar, caching, etc.) */
|
|
79
107
|
conversion?: WebSDKConversionConfig;
|
|
80
108
|
/** Editor configuration (interaction mode, auto skill, etc.) */
|
|
@@ -104,7 +132,7 @@ interface ChatPanel {
|
|
|
104
132
|
/** Update panel config */
|
|
105
133
|
updateConfig: (config: unknown) => Promise<void>;
|
|
106
134
|
/** Observe a skill */
|
|
107
|
-
observeSkill: (skillId: string, name: string, skillType:
|
|
135
|
+
observeSkill: (skillId: string, name: string, skillType: "qna" | "bi" | "dataCreate") => Promise<void>;
|
|
108
136
|
/** Set current skill */
|
|
109
137
|
setCurrentSkill: (skill: {
|
|
110
138
|
id: string;
|
|
@@ -122,6 +150,9 @@ declare const IFRAME_BASE_URL: Record<string, string>;
|
|
|
122
150
|
declare function getIframeBaseURL(env?: string): string;
|
|
123
151
|
/** Timeout for postMessage requests (30 seconds) */
|
|
124
152
|
declare const MESSAGE_TIMEOUT: number;
|
|
153
|
+
/** Miaoda 渠道标识默认值(用户可通过 config.common.channelType 覆盖) */
|
|
154
|
+
declare const DEFAULT_CHANNEL_TYPE = "MIAODA_CUI_SDK";
|
|
155
|
+
declare const DEFAULT_ANONYMOUS_CHANNEL_TYPE = "MIAODA_ANONYMOUS_CUI_SDK";
|
|
125
156
|
|
|
126
157
|
/**
|
|
127
158
|
* Message types for postMessage communication between aily-web-sdk and iframe
|
|
@@ -172,4 +203,4 @@ type IframeMessage = IframeRequestMessage | IframeResponseMessage | IframeEventM
|
|
|
172
203
|
*/
|
|
173
204
|
declare function initAilyChat(root: HTMLElement, config: WebSDKConfig): Promise<ChatPanel>;
|
|
174
205
|
|
|
175
|
-
export { type ChatPanel, IFRAME_BASE_URL, type IframeEventMessage, type IframeMessage, type IframeMessageType, type IframeRequestMessage, type IframeResponseMessage, MESSAGE_TIMEOUT, type WebSDKCommonConfig, type WebSDKConfig, type WebSDKConversionConfig, type WebSDKEditorConfig, type WebSDKEvents, getIframeBaseURL, initAilyChat };
|
|
206
|
+
export { type ChatPanel, DEFAULT_ANONYMOUS_CHANNEL_TYPE, DEFAULT_CHANNEL_TYPE, IFRAME_BASE_URL, type IframeEventMessage, type IframeMessage, type IframeMessageType, type IframeRequestMessage, type IframeResponseMessage, MESSAGE_TIMEOUT, type WebSDKCommonConfig, type WebSDKConfig, type WebSDKConversionConfig, type WebSDKEditorConfig, type WebSDKEvents, type WebSDKSkillInfo, getIframeBaseURL, initAilyChat };
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
interface WebSDKCommonConfig {
|
|
8
8
|
/** Base URL for the Aily service */
|
|
9
|
-
baseURL
|
|
9
|
+
baseURL?: string;
|
|
10
10
|
/** Additional headers to send with requests */
|
|
11
11
|
headers?: Record<string, string>;
|
|
12
12
|
/** APAAS application info for API authentication */
|
|
@@ -15,6 +15,14 @@ interface WebSDKCommonConfig {
|
|
|
15
15
|
tenantID?: number;
|
|
16
16
|
tenantType?: number;
|
|
17
17
|
};
|
|
18
|
+
/** 渠道标识,不再硬编码 */
|
|
19
|
+
channelType?: string;
|
|
20
|
+
/** 资源类型,默认等于 channelType */
|
|
21
|
+
resourceType?: string;
|
|
22
|
+
/** 传给后端 API 的额外上下文 */
|
|
23
|
+
channelContext?: Record<string, unknown>;
|
|
24
|
+
/** 最小轮询间隔 (ms),默认 2000 */
|
|
25
|
+
minPollInterval?: number;
|
|
18
26
|
}
|
|
19
27
|
/**
|
|
20
28
|
* Event callbacks
|
|
@@ -26,6 +34,16 @@ interface WebSDKEvents {
|
|
|
26
34
|
onError?: (error: Error) => void;
|
|
27
35
|
/** Callback when a message is received */
|
|
28
36
|
onMessage?: (message: unknown) => void;
|
|
37
|
+
/** 会话创建完成、轮询开始后触发 */
|
|
38
|
+
onInited?: (data: {
|
|
39
|
+
sessionId: string;
|
|
40
|
+
conversationId: string;
|
|
41
|
+
handler: string;
|
|
42
|
+
}) => void;
|
|
43
|
+
/** 欢迎消息创建完成后触发 */
|
|
44
|
+
onInitedWithWelcome?: () => void;
|
|
45
|
+
/** 用户点击关闭按钮时触发 */
|
|
46
|
+
onClose?: () => void;
|
|
29
47
|
}
|
|
30
48
|
/**
|
|
31
49
|
* Conversion (conversation) configuration passed through to cui-sdk
|
|
@@ -41,19 +59,31 @@ interface WebSDKConversionConfig {
|
|
|
41
59
|
needAvatar?: boolean;
|
|
42
60
|
/** Whether to show feedback buttons. Default: true */
|
|
43
61
|
supportFeedback?: boolean;
|
|
44
|
-
/**
|
|
45
|
-
|
|
62
|
+
/** 是否显示欢迎语,默认 true */
|
|
63
|
+
needWelcomeMessage?: boolean;
|
|
64
|
+
/** 系统头像 URL(iframe 序列化限制,只支持 string) */
|
|
65
|
+
systemAvatar?: string;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Skill information for preset skill
|
|
69
|
+
* 字段名与 cui-sdk SkillBaseInfo 对齐,iframe 透传后直接消费
|
|
70
|
+
*/
|
|
71
|
+
interface WebSDKSkillInfo {
|
|
72
|
+
/** API 标识 */
|
|
73
|
+
apiID?: string;
|
|
46
74
|
}
|
|
47
75
|
/**
|
|
48
76
|
* Editor configuration passed through to cui-sdk
|
|
49
77
|
*/
|
|
50
78
|
interface WebSDKEditorConfig {
|
|
51
|
-
/** Interaction mode: 'v1' for classic, 'v2' for new interaction. Default: 'v1' */
|
|
52
|
-
interactionMode?: 'v1' | 'v2';
|
|
53
|
-
/** Auto skill selection. Default: true */
|
|
54
|
-
autoSkill?: boolean;
|
|
55
79
|
/** Whether to show the editor. Default: true */
|
|
56
80
|
display?: boolean;
|
|
81
|
+
/** 预设技能信息 */
|
|
82
|
+
skill?: WebSDKSkillInfo;
|
|
83
|
+
/** 是否显示顶部 header(含标题和新建对话按钮)。仅 V2 生效。默认: true */
|
|
84
|
+
needHeader?: boolean;
|
|
85
|
+
/** 对话标题,显示在 header 中。默认取应用名称 */
|
|
86
|
+
headerTitle?: string;
|
|
57
87
|
}
|
|
58
88
|
/**
|
|
59
89
|
* Main configuration interface for Web SDK
|
|
@@ -64,9 +94,7 @@ interface WebSDKConfig {
|
|
|
64
94
|
/** User identifier */
|
|
65
95
|
uuid?: string;
|
|
66
96
|
/** Device type */
|
|
67
|
-
device?:
|
|
68
|
-
/** Interaction mode: 'v1' for classic, 'v2' for new interaction */
|
|
69
|
-
interactionMode?: 'v1' | 'v2';
|
|
97
|
+
device?: "pc" | "mobile";
|
|
70
98
|
/**
|
|
71
99
|
* Enable anonymous mode (skips login check). Default: false
|
|
72
100
|
* 非匿名模式下,iframe 内自行完成飞书登录(用户在 iframe 中独立登录),
|
|
@@ -74,7 +102,7 @@ interface WebSDKConfig {
|
|
|
74
102
|
*/
|
|
75
103
|
anonymous?: boolean;
|
|
76
104
|
/** Common configuration */
|
|
77
|
-
common
|
|
105
|
+
common?: WebSDKCommonConfig;
|
|
78
106
|
/** Conversation/conversion configuration (avatar, caching, etc.) */
|
|
79
107
|
conversion?: WebSDKConversionConfig;
|
|
80
108
|
/** Editor configuration (interaction mode, auto skill, etc.) */
|
|
@@ -104,7 +132,7 @@ interface ChatPanel {
|
|
|
104
132
|
/** Update panel config */
|
|
105
133
|
updateConfig: (config: unknown) => Promise<void>;
|
|
106
134
|
/** Observe a skill */
|
|
107
|
-
observeSkill: (skillId: string, name: string, skillType:
|
|
135
|
+
observeSkill: (skillId: string, name: string, skillType: "qna" | "bi" | "dataCreate") => Promise<void>;
|
|
108
136
|
/** Set current skill */
|
|
109
137
|
setCurrentSkill: (skill: {
|
|
110
138
|
id: string;
|
|
@@ -122,6 +150,9 @@ declare const IFRAME_BASE_URL: Record<string, string>;
|
|
|
122
150
|
declare function getIframeBaseURL(env?: string): string;
|
|
123
151
|
/** Timeout for postMessage requests (30 seconds) */
|
|
124
152
|
declare const MESSAGE_TIMEOUT: number;
|
|
153
|
+
/** Miaoda 渠道标识默认值(用户可通过 config.common.channelType 覆盖) */
|
|
154
|
+
declare const DEFAULT_CHANNEL_TYPE = "MIAODA_CUI_SDK";
|
|
155
|
+
declare const DEFAULT_ANONYMOUS_CHANNEL_TYPE = "MIAODA_ANONYMOUS_CUI_SDK";
|
|
125
156
|
|
|
126
157
|
/**
|
|
127
158
|
* Message types for postMessage communication between aily-web-sdk and iframe
|
|
@@ -172,4 +203,4 @@ type IframeMessage = IframeRequestMessage | IframeResponseMessage | IframeEventM
|
|
|
172
203
|
*/
|
|
173
204
|
declare function initAilyChat(root: HTMLElement, config: WebSDKConfig): Promise<ChatPanel>;
|
|
174
205
|
|
|
175
|
-
export { type ChatPanel, IFRAME_BASE_URL, type IframeEventMessage, type IframeMessage, type IframeMessageType, type IframeRequestMessage, type IframeResponseMessage, MESSAGE_TIMEOUT, type WebSDKCommonConfig, type WebSDKConfig, type WebSDKConversionConfig, type WebSDKEditorConfig, type WebSDKEvents, getIframeBaseURL, initAilyChat };
|
|
206
|
+
export { type ChatPanel, DEFAULT_ANONYMOUS_CHANNEL_TYPE, DEFAULT_CHANNEL_TYPE, IFRAME_BASE_URL, type IframeEventMessage, type IframeMessage, type IframeMessageType, type IframeRequestMessage, type IframeResponseMessage, MESSAGE_TIMEOUT, type WebSDKCommonConfig, type WebSDKConfig, type WebSDKConversionConfig, type WebSDKEditorConfig, type WebSDKEvents, type WebSDKSkillInfo, getIframeBaseURL, initAilyChat };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2
3
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
3
5
|
|
|
4
6
|
// src/constants.ts
|
|
5
7
|
var MESSAGE_BIZ_ID = "cui-sdk-message";
|
|
@@ -17,7 +19,7 @@ function getIframeBaseURL(env) {
|
|
|
17
19
|
if (hostname.includes("aiforce-boe")) {
|
|
18
20
|
return IFRAME_BASE_URL.boe;
|
|
19
21
|
}
|
|
20
|
-
if (hostname.includes("aiforce-pre")) {
|
|
22
|
+
if (hostname.includes("aiforce-pre") || hostname.includes("force-pre.feishuapp.net") || hostname.includes("fsapp.kundou.cn")) {
|
|
21
23
|
return IFRAME_BASE_URL.staging;
|
|
22
24
|
}
|
|
23
25
|
if (hostname.includes("localhost") || hostname.includes("127.0.0.1")) {
|
|
@@ -27,8 +29,8 @@ function getIframeBaseURL(env) {
|
|
|
27
29
|
}
|
|
28
30
|
__name(getIframeBaseURL, "getIframeBaseURL");
|
|
29
31
|
var MESSAGE_TIMEOUT = 30 * 1e3;
|
|
30
|
-
var
|
|
31
|
-
var
|
|
32
|
+
var DEFAULT_CHANNEL_TYPE = "MIAODA_CUI_SDK";
|
|
33
|
+
var DEFAULT_ANONYMOUS_CHANNEL_TYPE = "MIAODA_ANONYMOUS_CUI_SDK";
|
|
32
34
|
var ALLOWED_ORIGINS = [
|
|
33
35
|
"https://aily.feishu.cn",
|
|
34
36
|
"https://aily.feishu-pre.cn",
|
|
@@ -38,17 +40,17 @@ var ALLOWED_ORIGINS = [
|
|
|
38
40
|
"http://localhost:8080"
|
|
39
41
|
];
|
|
40
42
|
|
|
43
|
+
// src/iframe-manager.ts
|
|
44
|
+
import { slardar as slardar2 } from "@lark-apaas/internal-slardar";
|
|
45
|
+
|
|
41
46
|
// src/device-detector.ts
|
|
42
47
|
var MOBILE_BREAKPOINT = 768;
|
|
43
|
-
var
|
|
44
|
-
static {
|
|
45
|
-
__name(this, "DeviceDetector");
|
|
46
|
-
}
|
|
47
|
-
mediaQuery;
|
|
48
|
-
currentDevice;
|
|
49
|
-
callback = null;
|
|
50
|
-
handleChange = null;
|
|
48
|
+
var _DeviceDetector = class _DeviceDetector {
|
|
51
49
|
constructor() {
|
|
50
|
+
__publicField(this, "mediaQuery");
|
|
51
|
+
__publicField(this, "currentDevice");
|
|
52
|
+
__publicField(this, "callback", null);
|
|
53
|
+
__publicField(this, "handleChange", null);
|
|
52
54
|
this.mediaQuery = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
|
|
53
55
|
this.currentDevice = window.innerWidth < MOBILE_BREAKPOINT ? "mobile" : "pc";
|
|
54
56
|
}
|
|
@@ -74,18 +76,18 @@ var DeviceDetector = class {
|
|
|
74
76
|
this.callback = null;
|
|
75
77
|
}
|
|
76
78
|
};
|
|
79
|
+
__name(_DeviceDetector, "DeviceDetector");
|
|
80
|
+
var DeviceDetector = _DeviceDetector;
|
|
77
81
|
|
|
78
82
|
// src/message-bridge.ts
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
__name(this, "MessageBridge");
|
|
82
|
-
}
|
|
83
|
-
iframe = null;
|
|
84
|
-
requestId = 0;
|
|
85
|
-
pendingRequests = /* @__PURE__ */ new Map();
|
|
86
|
-
eventHandlers = /* @__PURE__ */ new Map();
|
|
87
|
-
targetOrigin;
|
|
83
|
+
import { slardar } from "@lark-apaas/internal-slardar";
|
|
84
|
+
var _MessageBridge = class _MessageBridge {
|
|
88
85
|
constructor(targetOrigin) {
|
|
86
|
+
__publicField(this, "iframe", null);
|
|
87
|
+
__publicField(this, "requestId", 0);
|
|
88
|
+
__publicField(this, "pendingRequests", /* @__PURE__ */ new Map());
|
|
89
|
+
__publicField(this, "eventHandlers", /* @__PURE__ */ new Map());
|
|
90
|
+
__publicField(this, "targetOrigin");
|
|
89
91
|
this.targetOrigin = targetOrigin;
|
|
90
92
|
this.handleMessage = this.handleMessage.bind(this);
|
|
91
93
|
}
|
|
@@ -128,7 +130,13 @@ var MessageBridge = class {
|
|
|
128
130
|
};
|
|
129
131
|
const timer = setTimeout(() => {
|
|
130
132
|
this.pendingRequests.delete(id);
|
|
131
|
-
|
|
133
|
+
const error = new Error(`Message timeout: ${type}`);
|
|
134
|
+
slardar.captureException(error, {
|
|
135
|
+
source: "aily-web-sdk",
|
|
136
|
+
module: "message-bridge",
|
|
137
|
+
messageType: type
|
|
138
|
+
});
|
|
139
|
+
reject(error);
|
|
132
140
|
}, MESSAGE_TIMEOUT);
|
|
133
141
|
this.pendingRequests.set(id, {
|
|
134
142
|
resolve,
|
|
@@ -171,7 +179,13 @@ var MessageBridge = class {
|
|
|
171
179
|
clearTimeout(pending.timer);
|
|
172
180
|
this.pendingRequests.delete(data.id);
|
|
173
181
|
if (data.error) {
|
|
174
|
-
|
|
182
|
+
const error = new Error(data.error.message);
|
|
183
|
+
slardar.captureException(error, {
|
|
184
|
+
source: "aily-web-sdk",
|
|
185
|
+
module: "message-bridge",
|
|
186
|
+
errorCode: data.error.code
|
|
187
|
+
});
|
|
188
|
+
pending.reject(error);
|
|
175
189
|
} else {
|
|
176
190
|
pending.resolve(data.data);
|
|
177
191
|
}
|
|
@@ -194,22 +208,22 @@ var MessageBridge = class {
|
|
|
194
208
|
return data.type === "event";
|
|
195
209
|
}
|
|
196
210
|
};
|
|
211
|
+
__name(_MessageBridge, "MessageBridge");
|
|
212
|
+
var MessageBridge = _MessageBridge;
|
|
197
213
|
|
|
198
214
|
// src/iframe-manager.ts
|
|
199
|
-
var
|
|
200
|
-
static {
|
|
201
|
-
__name(this, "IframeManager");
|
|
202
|
-
}
|
|
203
|
-
iframe = null;
|
|
204
|
-
messageBridge;
|
|
205
|
-
config;
|
|
206
|
-
channelType;
|
|
207
|
-
root;
|
|
208
|
-
deviceDetector = null;
|
|
215
|
+
var _IframeManager = class _IframeManager {
|
|
209
216
|
constructor(root, config) {
|
|
217
|
+
__publicField(this, "iframe", null);
|
|
218
|
+
__publicField(this, "messageBridge");
|
|
219
|
+
__publicField(this, "config");
|
|
220
|
+
__publicField(this, "channelType");
|
|
221
|
+
__publicField(this, "root");
|
|
222
|
+
__publicField(this, "deviceDetector", null);
|
|
210
223
|
this.root = root;
|
|
211
224
|
this.config = config;
|
|
212
|
-
|
|
225
|
+
const defaultChannel = config.anonymous ? DEFAULT_ANONYMOUS_CHANNEL_TYPE : DEFAULT_CHANNEL_TYPE;
|
|
226
|
+
this.channelType = config.common?.channelType || defaultChannel;
|
|
213
227
|
this.messageBridge = new MessageBridge(getIframeBaseURL());
|
|
214
228
|
}
|
|
215
229
|
/**
|
|
@@ -255,7 +269,7 @@ var IframeManager = class {
|
|
|
255
269
|
this.deviceDetector.destroy();
|
|
256
270
|
this.deviceDetector = null;
|
|
257
271
|
}
|
|
258
|
-
if (this.iframe
|
|
272
|
+
if (this.iframe?.parentNode) {
|
|
259
273
|
this.iframe.parentNode.removeChild(this.iframe);
|
|
260
274
|
}
|
|
261
275
|
this.iframe = null;
|
|
@@ -269,16 +283,25 @@ var IframeManager = class {
|
|
|
269
283
|
*/
|
|
270
284
|
buildIframeURL() {
|
|
271
285
|
const baseURL = getIframeBaseURL();
|
|
272
|
-
const { events, anonymous: _anonymous, ...configWithoutEvents } = this.config;
|
|
286
|
+
const { events: _events, anonymous: _anonymous, ...configWithoutEvents } = this.config;
|
|
287
|
+
const editorConfig = configWithoutEvents.editor?.skill ? {
|
|
288
|
+
...configWithoutEvents.editor,
|
|
289
|
+
skill: {
|
|
290
|
+
type: "custom",
|
|
291
|
+
builtinType: "unspecified",
|
|
292
|
+
...configWithoutEvents.editor.skill
|
|
293
|
+
}
|
|
294
|
+
} : configWithoutEvents.editor;
|
|
273
295
|
const iframeConfig = {
|
|
274
296
|
...configWithoutEvents,
|
|
275
297
|
common: {
|
|
276
298
|
...configWithoutEvents.common,
|
|
277
299
|
channelType: this.channelType,
|
|
278
|
-
resourceType: this.channelType
|
|
279
|
-
}
|
|
300
|
+
resourceType: configWithoutEvents.common?.resourceType || this.channelType
|
|
301
|
+
},
|
|
302
|
+
editor: editorConfig
|
|
280
303
|
};
|
|
281
|
-
const configBase64 = btoa(JSON.stringify(iframeConfig));
|
|
304
|
+
const configBase64 = btoa(encodeURIComponent(JSON.stringify(iframeConfig)));
|
|
282
305
|
const params = new URLSearchParams({
|
|
283
306
|
appKey: this.config.appKey,
|
|
284
307
|
config: encodeURIComponent(configBase64)
|
|
@@ -291,7 +314,12 @@ var IframeManager = class {
|
|
|
291
314
|
waitForReady() {
|
|
292
315
|
return new Promise((resolve, reject) => {
|
|
293
316
|
const timeout = setTimeout(() => {
|
|
294
|
-
|
|
317
|
+
const error = new Error("Iframe ready timeout");
|
|
318
|
+
slardar2.captureException(error, {
|
|
319
|
+
source: "aily-web-sdk",
|
|
320
|
+
module: "iframe-ready"
|
|
321
|
+
});
|
|
322
|
+
reject(error);
|
|
295
323
|
}, 3e4);
|
|
296
324
|
const checkMessage = /* @__PURE__ */ __name((event) => {
|
|
297
325
|
if (event.data?.messageBizId === MESSAGE_BIZ_ID && event.data?.type === "event" && event.data?.eventName === "ready") {
|
|
@@ -312,30 +340,36 @@ var IframeManager = class {
|
|
|
312
340
|
if (events.onError) {
|
|
313
341
|
this.messageBridge.onEvent("error", (data) => {
|
|
314
342
|
const msg = data?.message || "Unknown error";
|
|
315
|
-
events.onError(new Error(msg));
|
|
343
|
+
events.onError?.(new Error(msg));
|
|
316
344
|
});
|
|
317
345
|
}
|
|
318
346
|
if (events.onMessage) {
|
|
319
347
|
this.messageBridge.onEvent("onMessage", events.onMessage);
|
|
320
348
|
}
|
|
349
|
+
if (events.onInited) {
|
|
350
|
+
this.messageBridge.onEvent("inited", events.onInited);
|
|
351
|
+
}
|
|
352
|
+
if (events.onInitedWithWelcome) {
|
|
353
|
+
this.messageBridge.onEvent("initedWithWelcome", events.onInitedWithWelcome);
|
|
354
|
+
}
|
|
355
|
+
if (events.onClose) {
|
|
356
|
+
this.messageBridge.onEvent("onClose", events.onClose);
|
|
357
|
+
}
|
|
321
358
|
}
|
|
322
359
|
reportInitMetrics(initStart) {
|
|
323
360
|
try {
|
|
324
361
|
const cost = performance.now() - initStart;
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
}
|
|
362
|
+
slardar2.sendEvent({
|
|
363
|
+
name: "cui_init_total",
|
|
364
|
+
metrics: {
|
|
365
|
+
cost
|
|
366
|
+
},
|
|
367
|
+
categories: {
|
|
368
|
+
arch: "iframe-static",
|
|
369
|
+
appKey: this.config.appKey,
|
|
370
|
+
channelType: this.channelType
|
|
371
|
+
}
|
|
372
|
+
});
|
|
339
373
|
} catch {
|
|
340
374
|
}
|
|
341
375
|
}
|
|
@@ -375,6 +409,8 @@ var IframeManager = class {
|
|
|
375
409
|
};
|
|
376
410
|
}
|
|
377
411
|
};
|
|
412
|
+
__name(_IframeManager, "IframeManager");
|
|
413
|
+
var IframeManager = _IframeManager;
|
|
378
414
|
|
|
379
415
|
// src/index.ts
|
|
380
416
|
async function initAilyChat(root, config) {
|
|
@@ -383,6 +419,8 @@ async function initAilyChat(root, config) {
|
|
|
383
419
|
}
|
|
384
420
|
__name(initAilyChat, "initAilyChat");
|
|
385
421
|
export {
|
|
422
|
+
DEFAULT_ANONYMOUS_CHANNEL_TYPE,
|
|
423
|
+
DEFAULT_CHANNEL_TYPE,
|
|
386
424
|
IFRAME_BASE_URL,
|
|
387
425
|
MESSAGE_TIMEOUT,
|
|
388
426
|
getIframeBaseURL,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/constants.ts","../src/device-detector.ts","../src/message-bridge.ts","../src/iframe-manager.ts","../src/index.ts"],"sourcesContent":["/**\n * Constants for Aily Web SDK\n */\n\n/** Message business ID for postMessage identification */\nexport const MESSAGE_BIZ_ID = 'cui-sdk-message' as const;\n\n/** Iframe base URLs by environment */\nexport const IFRAME_BASE_URL: Record<string, string> = {\n production: 'https://aily.feishu.cn/cui',\n staging: 'https://aily.feishu-pre.cn/cui',\n boe: 'https://aily.feishu-boe.cn/cui',\n development: 'http://localhost:8080/cui',\n};\n\n/**\n * Get iframe base URL based on environment\n */\nexport function getIframeBaseURL(env?: string): string {\n if (env && IFRAME_BASE_URL[env]) {\n return IFRAME_BASE_URL[env];\n }\n\n // Auto-detect based on hostname\n // MiaoDa 宿主域名: miaoda.feishu[-env].cn, *.aiforce[-env][-preview].bytedance.net, *.aiforce.run, *.aiforce.cloud\n const hostname = typeof window !== 'undefined' ? window.location.hostname : '';\n\n if (hostname.includes('aiforce-boe')) {\n return IFRAME_BASE_URL.boe;\n }\n if (hostname.includes('aiforce-pre')) {\n return IFRAME_BASE_URL.staging;\n }\n if (hostname.includes('localhost') || hostname.includes('127.0.0.1')) {\n return IFRAME_BASE_URL.development;\n }\n\n return IFRAME_BASE_URL.production;\n}\n\n/** Timeout for postMessage requests (30 seconds) */\nexport const MESSAGE_TIMEOUT = 30 * 1000;\n\n/** Miaoda 渠道标识(本 SDK 为 Miaoda 专用包,不允许外部自定义) */\nexport const CHANNEL_TYPE = 'MIAODA_CUI_SDK';\nexport const ANONYMOUS_CHANNEL_TYPE = 'MIAODA_ANONYMOUS_CUI_SDK';\n\n/** Allowed origins for postMessage (cui-iframe 的 origin) */\nexport const ALLOWED_ORIGINS = [\n 'https://aily.feishu.cn',\n 'https://aily.feishu-pre.cn',\n 'https://aily.feishu-boe.cn',\n 'http://localhost:3000',\n 'http://localhost:5173',\n 'http://localhost:8080',\n];\n","const MOBILE_BREAKPOINT = 768;\n\ntype DeviceType = 'pc' | 'mobile';\ntype DeviceChangeCallback = (device: DeviceType) => void;\n\n/**\n * 设备类型自动检测器\n *\n * 使用 matchMedia + 768px 断点检测设备类型,与 miaoda useIsMobile 逻辑一致。\n * 检测在宿主页面(而非 iframe)中执行,结果通过 postMessage 推送给 iframe。\n */\nexport class DeviceDetector {\n private mediaQuery: MediaQueryList;\n private currentDevice: DeviceType;\n private callback: DeviceChangeCallback | null = null;\n private handleChange: (() => void) | null = null;\n\n constructor() {\n this.mediaQuery = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);\n this.currentDevice = window.innerWidth < MOBILE_BREAKPOINT ? 'mobile' : 'pc';\n }\n\n getDevice(): DeviceType {\n return this.currentDevice;\n }\n\n observe(callback: DeviceChangeCallback): void {\n this.callback = callback;\n this.handleChange = () => {\n const newDevice: DeviceType = window.innerWidth < MOBILE_BREAKPOINT ? 'mobile' : 'pc';\n if (newDevice !== this.currentDevice) {\n this.currentDevice = newDevice;\n this.callback?.(newDevice);\n }\n };\n this.mediaQuery.addEventListener('change', this.handleChange);\n }\n\n destroy(): void {\n if (this.handleChange) {\n this.mediaQuery.removeEventListener('change', this.handleChange);\n this.handleChange = null;\n }\n this.callback = null;\n }\n}\n","import { MESSAGE_TIMEOUT, ALLOWED_ORIGINS } from './constants';\nimport {\n MESSAGE_BIZ_ID,\n type IframeMessage,\n type IframeRequestMessage,\n type IframeResponseMessage,\n type IframeEventMessage,\n type IframeMessageType,\n} from './types/message';\n\ninterface PendingRequest {\n resolve: (value: unknown) => void;\n reject: (reason: Error) => void;\n timer: ReturnType<typeof setTimeout>;\n}\n\n/**\n * MessageBridge handles postMessage communication between parent and iframe\n */\nexport class MessageBridge {\n private iframe: HTMLIFrameElement | null = null;\n private requestId = 0;\n private pendingRequests = new Map<number, PendingRequest>();\n private eventHandlers = new Map<string, ((data: unknown) => void)[]>();\n private targetOrigin: string;\n\n constructor(targetOrigin: string) {\n this.targetOrigin = targetOrigin;\n this.handleMessage = this.handleMessage.bind(this);\n }\n\n /**\n * Attach to an iframe and start listening for messages\n */\n attach(iframe: HTMLIFrameElement): void {\n this.iframe = iframe;\n window.addEventListener('message', this.handleMessage);\n }\n\n /**\n * Detach from iframe and clean up\n */\n detach(): void {\n window.removeEventListener('message', this.handleMessage);\n this.iframe = null;\n // Clear all pending requests\n this.pendingRequests.forEach(({ reject }) => {\n reject(new Error('MessageBridge detached'));\n });\n this.pendingRequests.clear();\n }\n\n /**\n * Send a message to the iframe and wait for response\n */\n send<T = unknown>(type: IframeMessageType, data?: unknown): Promise<T> {\n return new Promise((resolve, reject) => {\n if (!this.iframe?.contentWindow) {\n reject(new Error('Iframe not attached'));\n return;\n }\n\n this.requestId += 1;\n const id = this.requestId;\n const message: IframeRequestMessage = {\n messageBizId: MESSAGE_BIZ_ID,\n id,\n type,\n data,\n timestamp: Date.now(),\n isRequest: true,\n };\n\n const timer = setTimeout(() => {\n this.pendingRequests.delete(id);\n reject(new Error(`Message timeout: ${type}`));\n }, MESSAGE_TIMEOUT);\n\n this.pendingRequests.set(id, { resolve: resolve as (value: unknown) => void, reject, timer });\n\n this.iframe.contentWindow.postMessage(message, this.targetOrigin);\n });\n }\n\n /**\n * Register an event handler\n */\n onEvent(eventName: string, handler: (data: unknown) => void): void {\n const handlers = this.eventHandlers.get(eventName) || [];\n handlers.push(handler);\n this.eventHandlers.set(eventName, handlers);\n }\n\n /**\n * Remove an event handler\n */\n offEvent(eventName: string, handler: (data: unknown) => void): void {\n const handlers = this.eventHandlers.get(eventName) || [];\n const index = handlers.indexOf(handler);\n if (index > -1) {\n handlers.splice(index, 1);\n this.eventHandlers.set(eventName, handlers);\n }\n }\n\n private handleMessage(event: MessageEvent<IframeMessage>): void {\n // Validate origin\n if (!ALLOWED_ORIGINS.includes(event.origin)) {\n return;\n }\n\n const { data } = event;\n\n // Validate message format\n if (data?.messageBizId !== MESSAGE_BIZ_ID) {\n return;\n }\n\n // Handle response messages\n if (this.isResponseMessage(data)) {\n const pending = this.pendingRequests.get(data.id);\n if (pending) {\n clearTimeout(pending.timer);\n this.pendingRequests.delete(data.id);\n\n if (data.error) {\n pending.reject(new Error(data.error.message));\n } else {\n pending.resolve(data.data);\n }\n }\n }\n\n // Handle event messages\n if (this.isEventMessage(data)) {\n const handlers = this.eventHandlers.get(data.eventName) || [];\n handlers.forEach((handler) => {\n try {\n handler(data.data);\n } catch {\n // Ignore handler errors\n }\n });\n }\n }\n\n private isResponseMessage(data: IframeMessage): data is IframeResponseMessage {\n return 'isResponse' in data && data.isResponse === true;\n }\n\n private isEventMessage(data: IframeMessage): data is IframeEventMessage {\n return data.type === 'event';\n }\n}\n","import { getIframeBaseURL, MESSAGE_BIZ_ID, CHANNEL_TYPE, ANONYMOUS_CHANNEL_TYPE } from './constants';\nimport { DeviceDetector } from './device-detector';\nimport { MessageBridge } from './message-bridge';\n\nimport type { WebSDKConfig, ChatPanel } from './types/config';\n\n/**\n * IframeManager handles iframe creation and lifecycle\n *\n * iframe 采用自初始化模式:配置通过 URL hash 传入,iframe 加载后自行解析并渲染,\n * 与旧版 copilot iframe 的行为保持一致。宿主通过 postMessage 进行后续操作。\n */\nexport class IframeManager {\n private iframe: HTMLIFrameElement | null = null;\n private messageBridge: MessageBridge;\n private config: WebSDKConfig;\n private channelType: string;\n private root: HTMLElement;\n private deviceDetector: DeviceDetector | null = null;\n\n constructor(root: HTMLElement, config: WebSDKConfig) {\n this.root = root;\n this.config = config;\n this.channelType = config.anonymous ? ANONYMOUS_CHANNEL_TYPE : CHANNEL_TYPE;\n this.messageBridge = new MessageBridge(getIframeBaseURL());\n }\n\n /**\n * Initialize the iframe and establish communication\n * 配置通过 URL hash 编码传入 iframe,iframe 加载后自行初始化\n */\n async init(): Promise<ChatPanel> {\n const initStart = performance.now();\n\n // Auto-detect device if not explicitly provided\n if (!this.config.device) {\n this.deviceDetector = new DeviceDetector();\n this.config.device = this.deviceDetector.getDevice();\n }\n\n // Create iframe element\n this.iframe = document.createElement('iframe');\n this.iframe.src = this.buildIframeURL();\n this.iframe.style.width = '100%';\n this.iframe.style.height = '100%';\n this.iframe.style.border = 'none';\n this.iframe.allow = 'microphone; clipboard-write';\n\n // Attach to DOM\n this.root.appendChild(this.iframe);\n\n // Attach message bridge (开始监听消息)\n this.messageBridge.attach(this.iframe);\n\n // Setup event handlers (在 iframe 初始化完成之前就开始监听,确保不丢失事件)\n this.setupEventHandlers();\n\n // Listen for device changes and push updates to iframe\n if (this.deviceDetector) {\n this.deviceDetector.observe((newDevice) => {\n this.messageBridge.send('updateConfig', { device: newDevice });\n });\n }\n\n // Wait for iframe to finish initialization (iframe 自行读取 URL 配置并渲染,完成后发送 ready 事件)\n await this.waitForReady();\n\n // 上报 cui_init_total 耗时(iframe 创建 → init 完成)\n this.reportInitMetrics(initStart);\n\n // Return ChatPanel interface\n return this.createChatPanel();\n }\n\n /**\n * Destroy the iframe and cleanup\n */\n async destroy(): Promise<void> {\n try {\n await this.messageBridge.send('destroy');\n } catch {\n // Ignore errors during destroy\n }\n\n this.messageBridge.detach();\n\n if (this.deviceDetector) {\n this.deviceDetector.destroy();\n this.deviceDetector = null;\n }\n\n if (this.iframe && this.iframe.parentNode) {\n this.iframe.parentNode.removeChild(this.iframe);\n }\n\n this.iframe = null;\n }\n\n /**\n * 构建 iframe URL,将配置编码到 hash 中\n * 格式: {baseURL}#{params} 其中 config 为 base64(JSON.stringify(configWithoutEvents))\n *\n * 鉴权说明:SDK 不参与 token 获取/传递。iframe 自行处理飞书登录流程,\n * 允许用户在 iframe 中独立登录。匿名渠道跳过登录检查。\n */\n private buildIframeURL(): string {\n const baseURL = getIframeBaseURL();\n\n // 移除 events(函数无法序列化)\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { events, anonymous: _anonymous, ...configWithoutEvents } = this.config;\n // 注入 channelType/resourceType(SDK 内部控制,不暴露给消费者)\n const iframeConfig = {\n ...configWithoutEvents,\n common: {\n ...configWithoutEvents.common,\n channelType: this.channelType,\n resourceType: this.channelType,\n },\n };\n const configBase64 = btoa(JSON.stringify(iframeConfig));\n\n const params = new URLSearchParams({\n appKey: this.config.appKey,\n config: encodeURIComponent(configBase64),\n });\n\n return `${baseURL}#${params.toString()}`;\n }\n\n /**\n * 等待 iframe 自行初始化完成后发送 ready 事件\n */\n private waitForReady(): Promise<void> {\n return new Promise((resolve, reject) => {\n const timeout = setTimeout(() => {\n reject(new Error('Iframe ready timeout'));\n }, 30000);\n\n const checkMessage = (event: MessageEvent) => {\n if (\n event.data?.messageBizId === MESSAGE_BIZ_ID &&\n event.data?.type === 'event' &&\n event.data?.eventName === 'ready'\n ) {\n clearTimeout(timeout);\n window.removeEventListener('message', checkMessage);\n resolve();\n }\n };\n\n window.addEventListener('message', checkMessage);\n });\n }\n\n private setupEventHandlers(): void {\n const { events } = this.config;\n if (!events) return;\n\n if (events.onReady) {\n this.messageBridge.onEvent('ready', events.onReady);\n }\n\n if (events.onError) {\n this.messageBridge.onEvent('error', (data: unknown) => {\n const msg = (data as { message?: string })?.message || 'Unknown error';\n events.onError!(new Error(msg));\n });\n }\n\n if (events.onMessage) {\n this.messageBridge.onEvent('onMessage', events.onMessage);\n }\n }\n\n private reportInitMetrics(initStart: number): void {\n try {\n const cost = performance.now() - initStart;\n const Slardar = (window as unknown as Record<string, unknown>).__Slardar as\n | { sendEvent?: (event: Record<string, unknown>) => void }\n | undefined;\n if (Slardar?.sendEvent) {\n Slardar.sendEvent({\n name: 'cui_init_total',\n metrics: { cost },\n categories: {\n arch: 'iframe-static',\n appKey: this.config.appKey,\n channelType: this.channelType,\n },\n });\n }\n } catch {\n // 性能上报失败不影响功能\n }\n }\n\n private createChatPanel(): ChatPanel {\n return {\n sendMessage: async (data) => {\n await this.messageBridge.send('sendMessage', data);\n },\n clear: async () => {\n await this.messageBridge.send('clear');\n },\n cancelMessage: async (messageItem) => {\n await this.messageBridge.send('cancelMessage', messageItem);\n },\n clearAndStop: async () => {\n await this.messageBridge.send('clearAndStop');\n },\n updateWelcomeMessage: async () => {\n await this.messageBridge.send('updateWelcomeMessage');\n },\n updateConfig: async (config) => {\n await this.messageBridge.send('updateConfig', config);\n },\n observeSkill: async (skillId, name, skillType) => {\n await this.messageBridge.send('observeSkill', { skillId, name, skillType });\n },\n setCurrentSkill: async (skill) => {\n await this.messageBridge.send('setCurrentSkill', skill);\n },\n destroy: async () => {\n await this.destroy();\n },\n };\n }\n}\n","/*\n * index.ts\n * Author: perterpon.wang<perterpon.wang@bytedance.com>\n * Create: Tue Feb 10 2026\n */\n\nimport { IframeManager } from './iframe-manager';\n\nimport type { ChatPanel, WebSDKConfig } from './types/config';\n\n/**\n * Initialize Aily Chat via iframe\n * This is the primary method for initializing the chat panel\n * @param root - Container element for the iframe\n * @param config - Configuration options\n * @returns ChatPanel instance for controlling the chat\n */\nexport async function initAilyChat(root: HTMLElement, config: WebSDKConfig): Promise<ChatPanel> {\n const manager = new IframeManager(root, config);\n return manager.init();\n}\n\n// Export types\nexport type {\n ChatPanel,\n WebSDKConfig,\n WebSDKCommonConfig,\n WebSDKConversionConfig,\n WebSDKEditorConfig,\n WebSDKEvents,\n} from './types/config';\n\n// Export constants\nexport { getIframeBaseURL, IFRAME_BASE_URL, MESSAGE_TIMEOUT } from './constants';\n\n// Export message types\nexport type {\n IframeMessageType,\n IframeRequestMessage,\n IframeResponseMessage,\n IframeEventMessage,\n IframeMessage,\n} from './types/message';\n"],"mappings":";;;;AAKO,IAAMA,iBAAiB;AAGvB,IAAMC,kBAA0C;EACrDC,YAAY;EACZC,SAAS;EACTC,KAAK;EACLC,aAAa;AACf;AAKO,SAASC,iBAAiBC,KAAY;AAC3C,MAAIA,OAAON,gBAAgBM,GAAAA,GAAM;AAC/B,WAAON,gBAAgBM,GAAAA;EACzB;AAIA,QAAMC,WAAW,OAAOC,WAAW,cAAcA,OAAOC,SAASF,WAAW;AAE5E,MAAIA,SAASG,SAAS,aAAA,GAAgB;AACpC,WAAOV,gBAAgBG;EACzB;AACA,MAAII,SAASG,SA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P,cAAM,KAAKA,QAAO;MACpB,GAFS;IAGX;EACF;AACF;;;ACnNA,eAAsB+D,aAAaC,MAAmBC,QAAoB;AACxE,QAAMC,UAAU,IAAIC,cAAcH,MAAMC,MAAAA;AACxC,SAAOC,QAAQE,KAAI;AACrB;AAHsBL;","names":["MESSAGE_BIZ_ID","IFRAME_BASE_URL","production","staging","boe","development","getIframeBaseURL","env","hostname","window","location","includes","MESSAGE_TIMEOUT","CHANNEL_TYPE","ANONYMOUS_CHANNEL_TYPE","ALLOWED_ORIGINS","MOBILE_BREAKPOINT","DeviceDetector","mediaQuery","currentDevice","callback","handleChange","window","matchMedia","innerWidth","getDevice","observe","newDevice","addEventListener","destroy","removeEventListener","MessageBridge","iframe","requestId","pendingRequests","Map","eventHandlers","targetOrigin","handleMessage","bind","attach","window","addEventListener","detach","removeEventListener","forEach","reject","Error","clear","send","type","data","Promise","resolve","contentWindow","id","message","messageBizId","MESSAGE_BIZ_ID","timestamp","Date","now","isRequest","timer","setTimeout","delete","MESSAGE_TIMEOUT","set","postMessage","onEvent","eventName","handler","handlers","get","push","offEvent","index","indexOf","splice","event","ALLOWED_ORIGINS","includes","origin","isResponseMessage","pending","clearTimeout","error","isEventMessage","isResponse","IframeManager","iframe","messageBridge","config","channelType","root","deviceDetector","anonymous","ANONYMOUS_CHANNEL_TYPE","CHANNEL_TYPE","MessageBridge","getIframeBaseURL","init","initStart","performance","now","device","DeviceDetector","getDevice","document","createElement","src","buildIframeURL","style","width","height","border","allow","appendChild","attach","setupEventHandlers","observe","newDevice","send","waitForReady","reportInitMetrics","createChatPanel","destroy","detach","parentNode","removeChild","baseURL","events","_anonymous","configWithoutEvents","iframeConfig","common","resourceType","configBase64","btoa","JSON","stringify","params","URLSearchParams","appKey","encodeURIComponent","toString","Promise","resolve","reject","timeout","setTimeout","Error","checkMessage","event","data","messageBizId","MESSAGE_BIZ_ID","type","eventName","clearTimeout","window","removeEventListener","addEventListener","onReady","onEvent","onError","msg","message","onMessage","cost","Slardar","__Slardar","sendEvent","name","metrics","categories","arch","sendMessage","clear","cancelMessage","messageItem","clearAndStop","updateWelcomeMessage","updateConfig","observeSkill","skillId","skillType","setCurrentSkill","skill","initAilyChat","root","config","manager","IframeManager","init"]}
|
|
1
|
+
{"version":3,"sources":["../src/constants.ts","../src/iframe-manager.ts","../src/device-detector.ts","../src/message-bridge.ts","../src/index.ts"],"sourcesContent":["/**\n * Constants for Aily Web SDK\n */\n\n/** Message business ID for postMessage identification */\nexport const MESSAGE_BIZ_ID = \"cui-sdk-message\" as const;\n\n/** Iframe base URLs by environment */\nexport const IFRAME_BASE_URL: Record<string, string> = {\n\tproduction: \"https://aily.feishu.cn/cui\",\n\tstaging: \"https://aily.feishu-pre.cn/cui\",\n\tboe: \"https://aily.feishu-boe.cn/cui\",\n\tdevelopment: \"http://localhost:8080/cui\",\n};\n\n/**\n * Get iframe base URL based on environment\n */\nexport function getIframeBaseURL(env?: string): string {\n\tif (env && IFRAME_BASE_URL[env]) {\n\t\treturn IFRAME_BASE_URL[env];\n\t}\n\n\t// Auto-detect based on hostname\n\t// MiaoDa 宿主域名: miaoda.feishu[-env].cn, *.aiforce[-env][-preview].bytedance.net, *.force-pre.feishuapp.net, *.fsapp.kundou.cn, *.aiforce.run, *.aiforce.cloud\n\tconst hostname =\n\t\ttypeof window !== \"undefined\" ? window.location.hostname : \"\";\n\n\tif (hostname.includes(\"aiforce-boe\")) {\n\t\treturn IFRAME_BASE_URL.boe;\n\t}\n\tif (\n\t\thostname.includes(\"aiforce-pre\") ||\n\t\thostname.includes(\"force-pre.feishuapp.net\") ||\n\t\thostname.includes(\"fsapp.kundou.cn\")\n\t) {\n\t\treturn IFRAME_BASE_URL.staging;\n\t}\n\tif (hostname.includes(\"localhost\") || hostname.includes(\"127.0.0.1\")) {\n\t\treturn IFRAME_BASE_URL.development;\n\t}\n\n\treturn IFRAME_BASE_URL.production;\n}\n\n/** Timeout for postMessage requests (30 seconds) */\nexport const MESSAGE_TIMEOUT = 30 * 1000;\n\n/** Miaoda 渠道标识默认值(用户可通过 config.common.channelType 覆盖) */\nexport const DEFAULT_CHANNEL_TYPE = \"MIAODA_CUI_SDK\";\nexport const DEFAULT_ANONYMOUS_CHANNEL_TYPE = \"MIAODA_ANONYMOUS_CUI_SDK\";\n\n/** Allowed origins for postMessage (cui-iframe 的 origin) */\nexport const ALLOWED_ORIGINS = [\n\t\"https://aily.feishu.cn\",\n\t\"https://aily.feishu-pre.cn\",\n\t\"https://aily.feishu-boe.cn\",\n\t\"http://localhost:3000\",\n\t\"http://localhost:5173\",\n\t\"http://localhost:8080\",\n];\n","import {\n\tDEFAULT_ANONYMOUS_CHANNEL_TYPE,\n\tDEFAULT_CHANNEL_TYPE,\n\tgetIframeBaseURL,\n\tMESSAGE_BIZ_ID,\n} from \"./constants\";\nimport { slardar } from \"@lark-apaas/internal-slardar\";\nimport { DeviceDetector } from \"./device-detector\";\nimport { MessageBridge } from \"./message-bridge\";\n\nimport type { ChatPanel, WebSDKConfig } from \"./types/config\";\n\n/**\n * IframeManager handles iframe creation and lifecycle\n *\n * iframe 采用自初始化模式:配置通过 URL hash 传入,iframe 加载后自行解析并渲染,\n * 与旧版 copilot iframe 的行为保持一致。宿主通过 postMessage 进行后续操作。\n */\nexport class IframeManager {\n\tprivate iframe: HTMLIFrameElement | null = null;\n\tprivate messageBridge: MessageBridge;\n\tprivate config: WebSDKConfig;\n\tprivate channelType: string;\n\tprivate root: HTMLElement;\n\tprivate deviceDetector: DeviceDetector | null = null;\n\n\tconstructor(root: HTMLElement, config: WebSDKConfig) {\n\t\tthis.root = root;\n\t\tthis.config = config;\n\t\tconst defaultChannel = config.anonymous\n\t\t\t? DEFAULT_ANONYMOUS_CHANNEL_TYPE\n\t\t\t: DEFAULT_CHANNEL_TYPE;\n\t\tthis.channelType = config.common?.channelType || defaultChannel;\n\t\tthis.messageBridge = new MessageBridge(getIframeBaseURL());\n\t}\n\n\t/**\n\t * Initialize the iframe and establish communication\n\t * 配置通过 URL hash 编码传入 iframe,iframe 加载后自行初始化\n\t */\n\tasync init(): Promise<ChatPanel> {\n\t\tconst initStart = performance.now();\n\n\t\t// Auto-detect device if not explicitly provided\n\t\tif (!this.config.device) {\n\t\t\tthis.deviceDetector = new DeviceDetector();\n\t\t\tthis.config.device = this.deviceDetector.getDevice();\n\t\t}\n\n\t\t// Create iframe element\n\t\tthis.iframe = document.createElement(\"iframe\");\n\t\tthis.iframe.src = this.buildIframeURL();\n\t\tthis.iframe.style.width = \"100%\";\n\t\tthis.iframe.style.height = \"100%\";\n\t\tthis.iframe.style.border = \"none\";\n\t\tthis.iframe.allow = \"microphone; clipboard-write\";\n\n\t\t// Attach to DOM\n\t\tthis.root.appendChild(this.iframe);\n\n\t\t// Attach message bridge (开始监听消息)\n\t\tthis.messageBridge.attach(this.iframe);\n\n\t\t// Setup event handlers (在 iframe 初始化完成之前就开始监听,确保不丢失事件)\n\t\tthis.setupEventHandlers();\n\n\t\t// Listen for device changes and push updates to iframe\n\t\tif (this.deviceDetector) {\n\t\t\tthis.deviceDetector.observe((newDevice) => {\n\t\t\t\tthis.messageBridge.send(\"updateConfig\", { device: newDevice });\n\t\t\t});\n\t\t}\n\n\t\t// Wait for iframe to finish initialization (iframe 自行读取 URL 配置并渲染,完成后发送 ready 事件)\n\t\tawait this.waitForReady();\n\n\t\t// 上报 cui_init_total 耗时(iframe 创建 → init 完成)\n\t\tthis.reportInitMetrics(initStart);\n\n\t\t// Return ChatPanel interface\n\t\treturn this.createChatPanel();\n\t}\n\n\t/**\n\t * Destroy the iframe and cleanup\n\t */\n\tasync destroy(): Promise<void> {\n\t\ttry {\n\t\t\tawait this.messageBridge.send(\"destroy\");\n\t\t} catch {\n\t\t\t// Ignore errors during destroy\n\t\t}\n\n\t\tthis.messageBridge.detach();\n\n\t\tif (this.deviceDetector) {\n\t\t\tthis.deviceDetector.destroy();\n\t\t\tthis.deviceDetector = null;\n\t\t}\n\n\t\tif (this.iframe?.parentNode) {\n\t\t\tthis.iframe.parentNode.removeChild(this.iframe);\n\t\t}\n\n\t\tthis.iframe = null;\n\t}\n\n\t/**\n\t * 构建 iframe URL,将配置编码到 hash 中\n\t * 格式: {baseURL}#{params} 其中 config 为 base64(JSON.stringify(configWithoutEvents))\n\t *\n\t * 鉴权说明:SDK 不参与 token 获取/传递。iframe 自行处理飞书登录流程,\n\t * 允许用户在 iframe 中独立登录。匿名渠道跳过登录检查。\n\t */\n\tprivate buildIframeURL(): string {\n\t\tconst baseURL = getIframeBaseURL();\n\n\t\t// 移除 events(函数无法序列化)\n\t\tconst {\n\t\t\tevents: _events,\n\t\t\tanonymous: _anonymous,\n\t\t\t...configWithoutEvents\n\t\t} = this.config;\n\t\t// 注入 channelType/resourceType(SDK 内部控制,不暴露给消费者)\n\t\t// 补全 editor.skill 硬编码字段\n\t\tconst editorConfig = configWithoutEvents.editor?.skill\n\t\t\t? {\n\t\t\t\t\t...configWithoutEvents.editor,\n\t\t\t\t\tskill: {\n\t\t\t\t\t\ttype: \"custom\",\n\t\t\t\t\t\tbuiltinType: \"unspecified\",\n\t\t\t\t\t\t...configWithoutEvents.editor.skill,\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t: configWithoutEvents.editor;\n\n\t\tconst iframeConfig = {\n\t\t\t...configWithoutEvents,\n\t\t\tcommon: {\n\t\t\t\t...configWithoutEvents.common,\n\t\t\t\tchannelType: this.channelType,\n\t\t\t\tresourceType:\n\t\t\t\t\tconfigWithoutEvents.common?.resourceType || this.channelType,\n\t\t\t},\n\t\t\teditor: editorConfig,\n\t\t};\n\t\tconst configBase64 = btoa(encodeURIComponent(JSON.stringify(iframeConfig)));\n\n\t\tconst params = new URLSearchParams({\n\t\t\tappKey: this.config.appKey,\n\t\t\tconfig: encodeURIComponent(configBase64),\n\t\t});\n\n\t\treturn `${baseURL}#${params.toString()}`;\n\t}\n\n\t/**\n\t * 等待 iframe 自行初始化完成后发送 ready 事件\n\t */\n\tprivate waitForReady(): Promise<void> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst timeout = setTimeout(() => {\n\t\t\t\tconst error = new Error(\"Iframe ready timeout\");\n\t\t\t\tslardar.captureException(error, { source: 'aily-web-sdk', module: 'iframe-ready' });\n\t\t\t\treject(error);\n\t\t\t}, 30000);\n\n\t\t\tconst checkMessage = (event: MessageEvent) => {\n\t\t\t\tif (\n\t\t\t\t\tevent.data?.messageBizId === MESSAGE_BIZ_ID &&\n\t\t\t\t\tevent.data?.type === \"event\" &&\n\t\t\t\t\tevent.data?.eventName === \"ready\"\n\t\t\t\t) {\n\t\t\t\t\tclearTimeout(timeout);\n\t\t\t\t\twindow.removeEventListener(\"message\", checkMessage);\n\t\t\t\t\tresolve();\n\t\t\t\t}\n\t\t\t};\n\n\t\t\twindow.addEventListener(\"message\", checkMessage);\n\t\t});\n\t}\n\n\tprivate setupEventHandlers(): void {\n\t\tconst { events } = this.config;\n\t\tif (!events) return;\n\n\t\tif (events.onReady) {\n\t\t\tthis.messageBridge.onEvent(\"ready\", events.onReady);\n\t\t}\n\n\t\tif (events.onError) {\n\t\t\tthis.messageBridge.onEvent(\"error\", (data: unknown) => {\n\t\t\t\tconst msg = (data as { message?: string })?.message || \"Unknown error\";\n\t\t\t\tevents.onError?.(new Error(msg));\n\t\t\t});\n\t\t}\n\n\t\tif (events.onMessage) {\n\t\t\tthis.messageBridge.onEvent(\"onMessage\", events.onMessage);\n\t\t}\n\n\t\tif (events.onInited) {\n\t\t\tthis.messageBridge.onEvent(\n\t\t\t\t\"inited\",\n\t\t\t\tevents.onInited as (data: unknown) => void,\n\t\t\t);\n\t\t}\n\n\t\tif (events.onInitedWithWelcome) {\n\t\t\tthis.messageBridge.onEvent(\n\t\t\t\t\"initedWithWelcome\",\n\t\t\t\tevents.onInitedWithWelcome,\n\t\t\t);\n\t\t}\n\n\t\tif (events.onClose) {\n\t\t\tthis.messageBridge.onEvent(\"onClose\", events.onClose);\n\t\t}\n\t}\n\n\tprivate reportInitMetrics(initStart: number): void {\n\t\ttry {\n\t\t\tconst cost = performance.now() - initStart;\n\t\t\tslardar.sendEvent({\n\t\t\t\tname: \"cui_init_total\",\n\t\t\t\tmetrics: { cost },\n\t\t\t\tcategories: {\n\t\t\t\t\tarch: \"iframe-static\",\n\t\t\t\t\tappKey: this.config.appKey,\n\t\t\t\t\tchannelType: this.channelType,\n\t\t\t\t},\n\t\t\t});\n\t\t} catch {\n\t\t\t// 性能上报失败不影响功能\n\t\t}\n\t}\n\n\tprivate createChatPanel(): ChatPanel {\n\t\treturn {\n\t\t\tsendMessage: async (data) => {\n\t\t\t\tawait this.messageBridge.send(\"sendMessage\", data);\n\t\t\t},\n\t\t\tclear: async () => {\n\t\t\t\tawait this.messageBridge.send(\"clear\");\n\t\t\t},\n\t\t\tcancelMessage: async (messageItem) => {\n\t\t\t\tawait this.messageBridge.send(\"cancelMessage\", messageItem);\n\t\t\t},\n\t\t\tclearAndStop: async () => {\n\t\t\t\tawait this.messageBridge.send(\"clearAndStop\");\n\t\t\t},\n\t\t\tupdateWelcomeMessage: async () => {\n\t\t\t\tawait this.messageBridge.send(\"updateWelcomeMessage\");\n\t\t\t},\n\t\t\tupdateConfig: async (config) => {\n\t\t\t\tawait this.messageBridge.send(\"updateConfig\", config);\n\t\t\t},\n\t\t\tobserveSkill: async (skillId, name, skillType) => {\n\t\t\t\tawait this.messageBridge.send(\"observeSkill\", {\n\t\t\t\t\tskillId,\n\t\t\t\t\tname,\n\t\t\t\t\tskillType,\n\t\t\t\t});\n\t\t\t},\n\t\t\tsetCurrentSkill: async (skill) => {\n\t\t\t\tawait this.messageBridge.send(\"setCurrentSkill\", skill);\n\t\t\t},\n\t\t\tdestroy: async () => {\n\t\t\t\tawait this.destroy();\n\t\t\t},\n\t\t};\n\t}\n}\n","const MOBILE_BREAKPOINT = 768;\n\ntype DeviceType = 'pc' | 'mobile';\ntype DeviceChangeCallback = (device: DeviceType) => void;\n\n/**\n * 设备类型自动检测器\n *\n * 使用 matchMedia + 768px 断点检测设备类型,与 miaoda useIsMobile 逻辑一致。\n * 检测在宿主页面(而非 iframe)中执行,结果通过 postMessage 推送给 iframe。\n */\nexport class DeviceDetector {\n private mediaQuery: MediaQueryList;\n private currentDevice: DeviceType;\n private callback: DeviceChangeCallback | null = null;\n private handleChange: (() => void) | null = null;\n\n constructor() {\n this.mediaQuery = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);\n this.currentDevice = window.innerWidth < MOBILE_BREAKPOINT ? 'mobile' : 'pc';\n }\n\n getDevice(): DeviceType {\n return this.currentDevice;\n }\n\n observe(callback: DeviceChangeCallback): void {\n this.callback = callback;\n this.handleChange = () => {\n const newDevice: DeviceType = window.innerWidth < MOBILE_BREAKPOINT ? 'mobile' : 'pc';\n if (newDevice !== this.currentDevice) {\n this.currentDevice = newDevice;\n this.callback?.(newDevice);\n }\n };\n this.mediaQuery.addEventListener('change', this.handleChange);\n }\n\n destroy(): void {\n if (this.handleChange) {\n this.mediaQuery.removeEventListener('change', this.handleChange);\n this.handleChange = null;\n }\n this.callback = null;\n }\n}\n","import { MESSAGE_TIMEOUT, ALLOWED_ORIGINS } from './constants';\nimport {\n MESSAGE_BIZ_ID,\n type IframeMessage,\n type IframeRequestMessage,\n type IframeResponseMessage,\n type IframeEventMessage,\n type IframeMessageType,\n} from './types/message';\nimport { slardar } from '@lark-apaas/internal-slardar';\n\ninterface PendingRequest {\n resolve: (value: unknown) => void;\n reject: (reason: Error) => void;\n timer: ReturnType<typeof setTimeout>;\n}\n\n/**\n * MessageBridge handles postMessage communication between parent and iframe\n */\nexport class MessageBridge {\n private iframe: HTMLIFrameElement | null = null;\n private requestId = 0;\n private pendingRequests = new Map<number, PendingRequest>();\n private eventHandlers = new Map<string, ((data: unknown) => void)[]>();\n private targetOrigin: string;\n\n constructor(targetOrigin: string) {\n this.targetOrigin = targetOrigin;\n this.handleMessage = this.handleMessage.bind(this);\n }\n\n /**\n * Attach to an iframe and start listening for messages\n */\n attach(iframe: HTMLIFrameElement): void {\n this.iframe = iframe;\n window.addEventListener('message', this.handleMessage);\n }\n\n /**\n * Detach from iframe and clean up\n */\n detach(): void {\n window.removeEventListener('message', this.handleMessage);\n this.iframe = null;\n // Clear all pending requests\n this.pendingRequests.forEach(({ reject }) => {\n reject(new Error('MessageBridge detached'));\n });\n this.pendingRequests.clear();\n }\n\n /**\n * Send a message to the iframe and wait for response\n */\n send<T = unknown>(type: IframeMessageType, data?: unknown): Promise<T> {\n return new Promise((resolve, reject) => {\n if (!this.iframe?.contentWindow) {\n reject(new Error('Iframe not attached'));\n return;\n }\n\n this.requestId += 1;\n const id = this.requestId;\n const message: IframeRequestMessage = {\n messageBizId: MESSAGE_BIZ_ID,\n id,\n type,\n data,\n timestamp: Date.now(),\n isRequest: true,\n };\n\n const timer = setTimeout(() => {\n this.pendingRequests.delete(id);\n const error = new Error(`Message timeout: ${type}`);\n slardar.captureException(error, { source: 'aily-web-sdk', module: 'message-bridge', messageType: type });\n reject(error);\n }, MESSAGE_TIMEOUT);\n\n this.pendingRequests.set(id, { resolve: resolve as (value: unknown) => void, reject, timer });\n\n this.iframe.contentWindow.postMessage(message, this.targetOrigin);\n });\n }\n\n /**\n * Register an event handler\n */\n onEvent(eventName: string, handler: (data: unknown) => void): void {\n const handlers = this.eventHandlers.get(eventName) || [];\n handlers.push(handler);\n this.eventHandlers.set(eventName, handlers);\n }\n\n /**\n * Remove an event handler\n */\n offEvent(eventName: string, handler: (data: unknown) => void): void {\n const handlers = this.eventHandlers.get(eventName) || [];\n const index = handlers.indexOf(handler);\n if (index > -1) {\n handlers.splice(index, 1);\n this.eventHandlers.set(eventName, handlers);\n }\n }\n\n private handleMessage(event: MessageEvent<IframeMessage>): void {\n // Validate origin\n if (!ALLOWED_ORIGINS.includes(event.origin)) {\n return;\n }\n\n const { data } = event;\n\n // Validate message format\n if (data?.messageBizId !== MESSAGE_BIZ_ID) {\n return;\n }\n\n // Handle response messages\n if (this.isResponseMessage(data)) {\n const pending = this.pendingRequests.get(data.id);\n if (pending) {\n clearTimeout(pending.timer);\n this.pendingRequests.delete(data.id);\n\n if (data.error) {\n const error = new Error(data.error.message);\n slardar.captureException(error, { source: 'aily-web-sdk', module: 'message-bridge', errorCode: data.error.code });\n pending.reject(error);\n } else {\n pending.resolve(data.data);\n }\n }\n }\n\n // Handle event messages\n if (this.isEventMessage(data)) {\n const handlers = this.eventHandlers.get(data.eventName) || [];\n handlers.forEach((handler) => {\n try {\n handler(data.data);\n } catch {\n // Ignore handler errors\n }\n });\n }\n }\n\n private isResponseMessage(data: IframeMessage): data is IframeResponseMessage {\n return 'isResponse' in data && data.isResponse === true;\n }\n\n private isEventMessage(data: IframeMessage): data is IframeEventMessage {\n return data.type === 'event';\n }\n}\n","/*\n * index.ts\n * Author: perterpon.wang<perterpon.wang@bytedance.com>\n * Create: Tue Feb 10 2026\n */\n\nimport { IframeManager } from \"./iframe-manager\";\n\nimport type { ChatPanel, WebSDKConfig } from \"./types/config\";\n\n/**\n * Initialize Aily Chat via iframe\n * This is the primary method for initializing the chat panel\n * @param root - Container element for the iframe\n * @param config - Configuration options\n * @returns ChatPanel instance for controlling the chat\n */\nexport async function initAilyChat(\n\troot: HTMLElement,\n\tconfig: WebSDKConfig,\n): Promise<ChatPanel> {\n\tconst manager = new IframeManager(root, config);\n\treturn manager.init();\n}\n\n// Export constants\nexport {\n\tDEFAULT_ANONYMOUS_CHANNEL_TYPE,\n\tDEFAULT_CHANNEL_TYPE,\n\tgetIframeBaseURL,\n\tIFRAME_BASE_URL,\n\tMESSAGE_TIMEOUT,\n} from \"./constants\";\n// Export types\nexport type {\n\tChatPanel,\n\tWebSDKCommonConfig,\n\tWebSDKConfig,\n\tWebSDKConversionConfig,\n\tWebSDKEditorConfig,\n\tWebSDKEvents,\n\tWebSDKSkillInfo,\n} from \"./types/config\";\n\n// Export message types\nexport type {\n\tIframeEventMessage,\n\tIframeMessage,\n\tIframeMessageType,\n\tIframeRequestMessage,\n\tIframeResponseMessage,\n} from \"./types/message\";\n"],"mappings":";;;;;;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ACvB,cAAM,KAAK9C,cAAc+B,KAAK,mBAAmBe,KAAAA;MAClD,GAFiB;MAGjBX,SAAS,mCAAA;AACR,cAAM,KAAKA,QAAO;MACnB,GAFS;IAGV;EACD;AACD;AA/PavC;AAAN,IAAMA,gBAAN;;;AGDP,eAAsB+G,aACrBC,MACAC,QAAoB;AAEpB,QAAMC,UAAU,IAAIC,cAAcH,MAAMC,MAAAA;AACxC,SAAOC,QAAQE,KAAI;AACpB;AANsBL;","names":["MESSAGE_BIZ_ID","IFRAME_BASE_URL","production","staging","boe","development","getIframeBaseURL","env","hostname","window","location","includes","MESSAGE_TIMEOUT","DEFAULT_CHANNEL_TYPE","DEFAULT_ANONYMOUS_CHANNEL_TYPE","ALLOWED_ORIGINS","slardar","MOBILE_BREAKPOINT","DeviceDetector","mediaQuery","currentDevice","callback","handleChange","window","matchMedia","innerWidth","getDevice","observe","newDevice","addEventListener","destroy","removeEventListener","slardar","MessageBridge","targetOrigin","iframe","requestId","pendingRequests","Map","eventHandlers","handleMessage","bind","attach","window","addEventListener","detach","removeEventListener","forEach","reject","Error","clear","send","type","data","Promise","resolve","contentWindow","id","message","messageBizId","MESSAGE_BIZ_ID","timestamp","Date","now","isRequest","timer","setTimeout","delete","error","slardar","captureException","source","module","messageType","MESSAGE_TIMEOUT","set","postMessage","onEvent","eventName","handler","handlers","get","push","offEvent","index","indexOf","splice","event","ALLOWED_ORIGINS","includes","origin","isResponseMessage","pending","clearTimeout","errorCode","code","isEventMessage","isResponse","IframeManager","root","config","iframe","messageBridge","channelType","deviceDetector","defaultChannel","anonymous","DEFAULT_ANONYMOUS_CHANNEL_TYPE","DEFAULT_CHANNEL_TYPE","common","MessageBridge","getIframeBaseURL","init","initStart","performance","now","device","DeviceDetector","getDevice","document","createElement","src","buildIframeURL","style","width","height","border","allow","appendChild","attach","setupEventHandlers","observe","newDevice","send","waitForReady","reportInitMetrics","createChatPanel","destroy","detach","parentNode","removeChild","baseURL","events","_events","_anonymous","configWithoutEvents","editorConfig","editor","skill","type","builtinType","iframeConfig","resourceType","configBase64","btoa","encodeURIComponent","JSON","stringify","params","URLSearchParams","appKey","toString","Promise","resolve","reject","timeout","setTimeout","error","Error","slardar","captureException","source","module","checkMessage","event","data","messageBizId","MESSAGE_BIZ_ID","eventName","clearTimeout","window","removeEventListener","addEventListener","onReady","onEvent","onError","msg","message","onMessage","onInited","onInitedWithWelcome","onClose","cost","sendEvent","name","metrics","categories","arch","sendMessage","clear","cancelMessage","messageItem","clearAndStop","updateWelcomeMessage","updateConfig","observeSkill","skillId","skillType","setCurrentSkill","initAilyChat","root","config","manager","IframeManager","init"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/aily-web-sdk",
|
|
3
|
-
"version": "0.0.2-beta.
|
|
3
|
+
"version": "0.0.2-beta.11",
|
|
4
4
|
"description": "Aily Web SDK - iframe-based chat panel integration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
},
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
18
19
|
"import": "./dist/index.js",
|
|
19
|
-
"require": "./dist/index.cjs"
|
|
20
|
-
"types": "./dist/index.d.ts"
|
|
20
|
+
"require": "./dist/index.cjs"
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"scripts": {
|
|
@@ -29,6 +29,9 @@
|
|
|
29
29
|
"build:storybook": "storybook build",
|
|
30
30
|
"prepublishOnly": "npm run build"
|
|
31
31
|
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@lark-apaas/internal-slardar": "^0.0.3"
|
|
34
|
+
},
|
|
32
35
|
"devDependencies": {
|
|
33
36
|
"@rsbuild/core": "~1.4.13",
|
|
34
37
|
"@rsbuild/plugin-react": "^1.3.4",
|