@mujian/js-sdk 0.0.6-beta.70 → 0.0.6-beta.72
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/lite.d.ts +1 -0
- package/dist/umd/lite.js +1 -1
- package/package.json +1 -1
package/dist/lite.d.ts
CHANGED
package/dist/umd/lite.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see lite.js.LICENSE.txt */
|
|
2
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.MujianLite=t():e.MujianLite=t()}(globalThis,()=>(()=>{"use strict";var e,t={};t.d=(e,i)=>{for(var n in i)t.o(i,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:i[n]})},t.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),t.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var i={};t.r(i),t.d(i,{EVENT:()=>
|
|
2
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.MujianLite=t():e.MujianLite=t()}(globalThis,()=>(()=>{"use strict";var e,t={};t.d=(e,i)=>{for(var n in i)t.o(i,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:i[n]})},t.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),t.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var i={};t.r(i),t.d(i,{EVENT:()=>u,default:()=>_});var n="application/x-postmate-v1+json",a=0,r=function(e){var t=document.createElement("a");t.href=e;var i=t.protocol.length>4?t.protocol:window.location.protocol,n=t.host.length?"80"===t.port||"443"===t.port?t.hostname:t.host:window.location.host;return t.origin||i+"//"+n},s={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},o=function(e,t){return("string"!=typeof t||e.origin===t)&&!!e.data&&("object"!=typeof e.data||"postmate"in e.data)&&e.data.type===n&&!!s[e.data.postmate]},d=function(e,t){var i="function"==typeof e[t]?e[t]():e[t];return m.Promise.resolve(i)},l=function(){function e(e){var t=this;this.parent=e.parent,this.frame=e.frame,this.child=e.child,this.childOrigin=e.childOrigin,this.events={},this.listener=function(e){if(!o(e,t.childOrigin))return!1;var i=((e||{}).data||{}).value||{},n=i.data,a=i.name;"emit"===e.data.postmate&&a in t.events&&t.events[a].call(t,n)},this.parent.addEventListener("message",this.listener,!1)}var t=e.prototype;return t.get=function(e){var t=this;return new m.Promise(function(i){var r=++a;t.parent.addEventListener("message",function e(n){n.data.uid===r&&"reply"===n.data.postmate&&(t.parent.removeEventListener("message",e,!1),i(n.data.value))},!1),t.child.postMessage({postmate:"request",type:n,property:e,uid:r},t.childOrigin)})},t.call=function(e,t){this.child.postMessage({postmate:"call",type:n,property:e,data:t},this.childOrigin)},t.on=function(e,t){this.events[e]=t},t.destroy=function(){window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)},e}(),c=function(){function e(e){var t=this;this.model=e.model,this.parent=e.parent,this.parentOrigin=e.parentOrigin,this.child=e.child,this.child.addEventListener("message",function(e){if(o(e,t.parentOrigin)){var i=e.data,a=i.property,r=i.uid,s=i.data;if("call"===e.data.postmate){a in t.model&&"function"==typeof t.model[a]&&t.model[a](s);return}d(t.model,a).then(function(t){return e.source.postMessage({property:a,postmate:"reply",type:n,uid:r,value:t},e.origin)})}})}return e.prototype.emit=function(e,t){this.parent.postMessage({postmate:"emit",type:n,value:{name:e,data:t}},this.parentOrigin)},e}(),m=function(){function e(e){var t=e.container,i=void 0===t?void 0!==i?i:document.body:t,n=e.model,a=e.url,r=e.name,s=e.classListArray;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=r||"",this.frame.classList.add.apply(this.frame.classList,void 0===s?[]:s),i.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=n||{},this.sendHandshake(a)}return e.prototype.sendHandshake=function(t){var i,a=this,s=r(t),d=0;return new e.Promise(function(e,r){a.parent.addEventListener("message",function t(n){return!!o(n,s)&&("handshake-reply"===n.data.postmate?(clearInterval(i),a.parent.removeEventListener("message",t,!1),a.childOrigin=n.origin,e(new l(a))):r("Failed handshake"))},!1);var c=function(){d++,a.child.postMessage({postmate:"handshake",type:n,model:a.model},s),5===d&&clearInterval(i)},m=function(){c(),i=setInterval(c,500)};a.frame.attachEvent?a.frame.attachEvent("onload",m):a.frame.onload=m,a.frame.src=t})},e}();m.debug=!1,m.Promise=function(){try{return window?window.Promise:Promise}catch(e){return null}}(),m.Model=function(){function e(e){return this.child=window,this.model=e,this.parent=this.child.parent,this.sendHandshakeReply()}return e.prototype.sendHandshakeReply=function(){var e=this;return new m.Promise(function(t,i){e.child.addEventListener("message",function a(r){if(r.data.postmate){if("handshake"===r.data.postmate){e.child.removeEventListener("message",a,!1),r.source.postMessage({postmate:"handshake-reply",type:n},r.origin),e.parentOrigin=r.origin;var s=r.data.model;return s&&Object.keys(s).forEach(function(t){e.model[t]=s[t]}),t(new c(e))}return i("Handshake Reply Failed")}},!1)})},e}();var u=((e={}).MUJIAN_INIT="mujian:init",e.MUJIAN_AI_CHAT_STOP="mujian:ai:chat:stop",e.MUJIAN_AI_CHAT_COMPLETE="mujian:ai:chat:complete",e.MUJIAN_AI_CHAT_APPLY_REGEX="mujian:ai:chat:applyRegex",e.MUJIAN_AI_CHAT_RENDER_MESSAGE="mujian:ai:chat:renderMessage",e.MUJIAN_AI_TEXT_GENERATE="mujian:ai:text:generate",e.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE="mujian:ai:openai:completions:create",e.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE="mujian:ai:openai:chat:completions:create",e.MUJIAN_AI_OPENAI_RESPONSES_CREATE="mujian:ai:openai:responses:create",e.MUJIAN_AI_CHAT_MESSAGE_GET_ALL="mujian:ai:chat:message:getAll",e.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE="mujian:ai:chat:message:getPage",e.MUJIAN_AI_CHAT_PROJECT_GET_INFO="mujian:ai:chat:project:getInfo",e.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE="mujian:ai:settings:persona:getActive",e.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE="mujian:ai:settings:persona:setActive",e.MUJIAN_AI_SETTINGS_MODEL_GET_ALL="mujian:ai:settings:model:getAll",e.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE="mujian:ai:settings:model:setActive",e.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE="mujian:ai:settings:model:getActive",e.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE="mujian:ai:chat:message:deleteOne",e.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE="mujian:ai:chat:message:editOne",e.MUJIAN_AI_CHAT_MESSAGE_SWIPE="mujian:ai:chat:message:swipe",e.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT="mujian:ai:chat:message:getPrompt",e.MUJIAN_AI_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",e.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",e.MUJIAN_GET_CONFIG="mujian:getConfig",e);let h={i(...e){console.log("[MujianSDK] ",...e)},e(...e){console.error("[MujianSDK] ",...e)}};class p{constructor(){}static EVENT=u;static getInstance(){return window.$mujian_lite||(window.$mujian_lite=new p),window.$mujian_lite}parent=null;ready=!1;get isReady(){return this.ready}pendingRequests=new Map;_config;get config(){return this._config}initPromise=null;async init(){return this.initPromise||(this.initPromise=(async()=>{let e=new m.Model({reply:({id:e,complete:t,data:i,error:n})=>{let a=this.pendingRequests.get(e);if(a){if(n){a.reject(n),this.pendingRequests.delete(e);return}a.onData?.(i),t&&(a.onComplete?.(),a.resolve(i),this.pendingRequests.delete(e))}}});try{let t=await e;this.parent=t,this.ready=!0,h.i("mujian sdk lite client init"),await this.call(u.MUJIAN_INIT),this._config=await this.call(u.MUJIAN_GET_CONFIG)}catch(e){throw h.e("init error",e),e}})()),this.initPromise}ensureReady(){if(!this.ready)throw Error("MujianLite is not initialized")}emit(e,t){this.ensureReady(),this.parent?.emit(e,t)}nextCallId=0;getCallId(){let e=this.nextCallId;return this.nextCallId+=1,`lite_${e}`}async call(e,t,i){this.ensureReady();let n=this.getCallId();return new Promise((a,r)=>{this.pendingRequests.set(n,{resolve:a,reject:r,...i}),this.emit(e,{id:n,data:t})})}}p.getInstance().init();let _=p;return i})());
|