@lobehub/chat-plugin-sdk
Version:
Lobe Chat Plugin SDK, help you to build an amazing chat plugin for Lobe Chat
123 lines (121 loc) • 4.82 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/client/lobeChat.ts
var lobeChat_exports = {};
__export(lobeChat_exports, {
lobeChat: () => lobeChat
});
module.exports = __toCommonJS(lobeChat_exports);
var import_const = require("./const");
var LobeChat = class {
constructor() {
this.getPluginPayload = () => new Promise((resolve) => {
if (typeof window === "undefined") {
resolve(void 0);
return;
}
const timer = setTimeout(() => {
resolve(void 0);
window.removeEventListener("message", receiverData);
}, 1e3);
const receiverData = (e) => {
if (e.data.type === import_const.PluginChannel.initStandalonePlugin) {
const payload = e.data.payload || e.data.props;
const func = payload.apiName;
const args = JSON.parse(payload.arguments || "{}");
clearTimeout(timer);
resolve({
arguments: args,
name: func,
settings: e.data.settings,
state: e.data.state
});
window.removeEventListener("message", receiverData);
}
};
window.addEventListener("message", receiverData);
top == null ? void 0 : top.postMessage({ type: import_const.PluginChannel.pluginReadyForRender }, "*");
});
this.getPluginSettings = () => new Promise((resolve) => {
if (typeof window === "undefined") {
resolve(void 0);
return;
}
const receiverData = (e) => {
if (e.data.type === import_const.PluginChannel.renderPluginSettings) {
resolve(e.data.value);
window.removeEventListener("message", receiverData);
}
};
window.addEventListener("message", receiverData);
top == null ? void 0 : top.postMessage({ type: import_const.PluginChannel.fetchPluginSettings }, "*");
});
this.setPluginSettings = (settings) => {
top == null ? void 0 : top.postMessage({ type: import_const.PluginChannel.updatePluginSettings, value: settings }, "*");
};
this.getPluginMessage = () => new Promise((resolve) => {
if (typeof window === "undefined") {
resolve(void 0);
return;
}
const receiverData = (e) => {
if (e.data.type === import_const.PluginChannel.renderPlugin) {
const props = e.data.props;
resolve(props.content);
window.removeEventListener("message", receiverData);
}
};
window.addEventListener("message", receiverData);
top == null ? void 0 : top.postMessage({ type: import_const.PluginChannel.fetchPluginMessage }, "*");
});
this.setPluginMessage = (content, triggerAiMessage) => {
top == null ? void 0 : top.postMessage(
{ content, triggerAiMessage, type: import_const.PluginChannel.fillStandalonePluginContent },
"*"
);
};
this.getPluginState = (key) => new Promise((resolve) => {
if (typeof window === "undefined") {
resolve(void 0);
return;
}
const receiverData = (e) => {
if (e.data.type === import_const.PluginChannel.renderPluginState && e.data.key === key) {
resolve(e.data.value);
window.removeEventListener("message", receiverData);
}
};
window.addEventListener("message", receiverData);
top == null ? void 0 : top.postMessage({ key, type: import_const.PluginChannel.fetchPluginState }, "*");
});
this.setPluginState = (key, value) => {
top == null ? void 0 : top.postMessage({ key, type: import_const.PluginChannel.updatePluginState, value }, "*");
};
this.triggerAIMessage = (id) => {
top == null ? void 0 : top.postMessage({ id, type: import_const.PluginChannel.triggerAIMessage }, "*");
};
this.createAssistantMessage = (content) => {
top == null ? void 0 : top.postMessage({ content, type: import_const.PluginChannel.createAssistantMessage }, "*");
};
}
};
var lobeChat = new LobeChat();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
lobeChat
});