@mujian/js-sdk 0.0.6-beta.71 → 0.0.6-beta.73

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.d.ts CHANGED
@@ -35,7 +35,8 @@ declare class MujianSdk {
35
35
  /** @hidden */
36
36
  private pendingRequests;
37
37
  private _config?;
38
- get config(): Config | undefined;
38
+ get openapi(): Config['openapi'] | undefined;
39
+ private initPromise;
39
40
  /**
40
41
  * 初始化
41
42
  */
package/dist/index.js CHANGED
@@ -183,53 +183,58 @@ class MujianSdk {
183
183
  }
184
184
  pendingRequests = new Map();
185
185
  _config;
186
- get config() {
187
- return this._config;
186
+ get openapi() {
187
+ return this._config?.openapi;
188
188
  }
189
+ initPromise = null;
189
190
  async init() {
190
- const handshake = new postmate.Model({
191
- reply: ({ id, complete, data, error })=>{
192
- const call = this.pendingRequests.get(id);
193
- if (!call) return;
194
- if (error) {
195
- call.reject(error);
196
- this.pendingRequests.delete(id);
197
- return;
191
+ if (this.initPromise) return this.initPromise;
192
+ this.initPromise = (async ()=>{
193
+ const handshake = new postmate.Model({
194
+ reply: ({ id, complete, data, error })=>{
195
+ const call = this.pendingRequests.get(id);
196
+ if (!call) return;
197
+ if (error) {
198
+ call.reject(error);
199
+ this.pendingRequests.delete(id);
200
+ return;
201
+ }
202
+ call.onData?.(data);
203
+ if (complete) {
204
+ call.onComplete?.();
205
+ call.resolve(data);
206
+ this.pendingRequests.delete(id);
207
+ }
198
208
  }
199
- call.onData?.(data);
200
- if (complete) {
201
- call.onComplete?.();
202
- call.resolve(data);
203
- this.pendingRequests.delete(id);
209
+ });
210
+ try {
211
+ const parent = await handshake;
212
+ this.ready = true;
213
+ this.parent = parent;
214
+ Log.i('mujian sdk client init');
215
+ await this.call(events_EVENT.MUJIAN_INIT);
216
+ const projectInfo = await this.ai.chat.project.getInfo();
217
+ if (projectInfo.config?.customCss) {
218
+ const style = document.createElement('style');
219
+ style.setAttribute('type', 'text/css');
220
+ style.setAttribute('id', 'mujian-custom-css');
221
+ style.textContent = projectInfo.config.customCss;
222
+ document.head.appendChild(style);
204
223
  }
224
+ if (projectInfo.config?.customJs) {
225
+ const script = document.createElement("script");
226
+ script.setAttribute('type', "text/javascript");
227
+ script.setAttribute('id', 'mujian-custom-js');
228
+ script.textContent = projectInfo.config.customJs;
229
+ document.head.appendChild(script);
230
+ }
231
+ const config = await getConfig.call(this);
232
+ if (config) this._config = config;
233
+ } catch (error) {
234
+ Log.e('init error', error);
205
235
  }
206
- });
207
- try {
208
- const parent = await handshake;
209
- this.ready = true;
210
- this.parent = parent;
211
- Log.i('mujian sdk client init');
212
- await this.call(events_EVENT.MUJIAN_INIT);
213
- const projectInfo = await this.ai.chat.project.getInfo();
214
- if (projectInfo.config?.customCss) {
215
- const style = document.createElement('style');
216
- style.setAttribute('type', 'text/css');
217
- style.setAttribute('id', 'mujian-custom-css');
218
- style.textContent = projectInfo.config.customCss;
219
- document.head.appendChild(style);
220
- }
221
- if (projectInfo.config?.customJs) {
222
- const script = document.createElement("script");
223
- script.setAttribute('type', "text/javascript");
224
- script.setAttribute('id', 'mujian-custom-js');
225
- script.textContent = projectInfo.config.customJs;
226
- document.head.appendChild(script);
227
- }
228
- const config = await getConfig.call(this);
229
- if (config) this._config = config;
230
- } catch (error) {
231
- Log.e('init error', error);
232
- }
236
+ })();
237
+ return this.initPromise;
233
238
  }
234
239
  emit(event, data) {
235
240
  if (!this.ready) throw new Error('Mujian is not initialized');
@@ -397,53 +402,58 @@ class src_MujianSdk {
397
402
  }
398
403
  pendingRequests = new Map();
399
404
  _config;
400
- get config() {
401
- return this._config;
405
+ get openapi() {
406
+ return this._config?.openapi;
402
407
  }
408
+ initPromise = null;
403
409
  async init() {
404
- const handshake = new postmate.Model({
405
- reply: ({ id, complete, data, error })=>{
406
- const call = this.pendingRequests.get(id);
407
- if (!call) return;
408
- if (error) {
409
- call.reject(error);
410
- this.pendingRequests.delete(id);
411
- return;
410
+ if (this.initPromise) return this.initPromise;
411
+ this.initPromise = (async ()=>{
412
+ const handshake = new postmate.Model({
413
+ reply: ({ id, complete, data, error })=>{
414
+ const call = this.pendingRequests.get(id);
415
+ if (!call) return;
416
+ if (error) {
417
+ call.reject(error);
418
+ this.pendingRequests.delete(id);
419
+ return;
420
+ }
421
+ call.onData?.(data);
422
+ if (complete) {
423
+ call.onComplete?.();
424
+ call.resolve(data);
425
+ this.pendingRequests.delete(id);
426
+ }
412
427
  }
413
- call.onData?.(data);
414
- if (complete) {
415
- call.onComplete?.();
416
- call.resolve(data);
417
- this.pendingRequests.delete(id);
428
+ });
429
+ try {
430
+ const parent = await handshake;
431
+ this.ready = true;
432
+ this.parent = parent;
433
+ Log.i('mujian sdk client init');
434
+ await this.call(events_EVENT.MUJIAN_INIT);
435
+ const projectInfo = await this.ai.chat.project.getInfo();
436
+ if (projectInfo.config?.customCss) {
437
+ const style = document.createElement('style');
438
+ style.setAttribute('type', 'text/css');
439
+ style.setAttribute('id', 'mujian-custom-css');
440
+ style.textContent = projectInfo.config.customCss;
441
+ document.head.appendChild(style);
418
442
  }
443
+ if (projectInfo.config?.customJs) {
444
+ const script = document.createElement("script");
445
+ script.setAttribute('type', "text/javascript");
446
+ script.setAttribute('id', 'mujian-custom-js');
447
+ script.textContent = projectInfo.config.customJs;
448
+ document.head.appendChild(script);
449
+ }
450
+ const config = await getConfig.call(this);
451
+ if (config) this._config = config;
452
+ } catch (error) {
453
+ Log.e('init error', error);
419
454
  }
420
- });
421
- try {
422
- const parent = await handshake;
423
- this.ready = true;
424
- this.parent = parent;
425
- Log.i('mujian sdk client init');
426
- await this.call(events_EVENT.MUJIAN_INIT);
427
- const projectInfo = await this.ai.chat.project.getInfo();
428
- if (projectInfo.config?.customCss) {
429
- const style = document.createElement('style');
430
- style.setAttribute('type', 'text/css');
431
- style.setAttribute('id', 'mujian-custom-css');
432
- style.textContent = projectInfo.config.customCss;
433
- document.head.appendChild(style);
434
- }
435
- if (projectInfo.config?.customJs) {
436
- const script = document.createElement("script");
437
- script.setAttribute('type', "text/javascript");
438
- script.setAttribute('id', 'mujian-custom-js');
439
- script.textContent = projectInfo.config.customJs;
440
- document.head.appendChild(script);
441
- }
442
- const config = await getConfig.call(this);
443
- if (config) this._config = config;
444
- } catch (error) {
445
- Log.e('init error', error);
446
- }
455
+ })();
456
+ return this.initPromise;
447
457
  }
448
458
  emit(event, data) {
449
459
  if (!this.ready) throw new Error('Mujian is not initialized');
package/dist/lite.d.ts CHANGED
@@ -34,7 +34,7 @@ declare class MujianSdkLite {
34
34
  /** @hidden */
35
35
  private pendingRequests;
36
36
  private _config?;
37
- get config(): Config | undefined;
37
+ get openapi(): Config['openapi'] | undefined;
38
38
  private initPromise;
39
39
  /**
40
40
  * 初始化
package/dist/react.js CHANGED
@@ -724,53 +724,58 @@ class MujianSdk {
724
724
  }
725
725
  pendingRequests = new Map();
726
726
  _config;
727
- get config() {
728
- return this._config;
727
+ get openapi() {
728
+ return this._config?.openapi;
729
729
  }
730
+ initPromise = null;
730
731
  async init() {
731
- const handshake = new postmate.Model({
732
- reply: ({ id, complete, data, error })=>{
733
- const call = this.pendingRequests.get(id);
734
- if (!call) return;
735
- if (error) {
736
- call.reject(error);
737
- this.pendingRequests.delete(id);
738
- return;
732
+ if (this.initPromise) return this.initPromise;
733
+ this.initPromise = (async ()=>{
734
+ const handshake = new postmate.Model({
735
+ reply: ({ id, complete, data, error })=>{
736
+ const call = this.pendingRequests.get(id);
737
+ if (!call) return;
738
+ if (error) {
739
+ call.reject(error);
740
+ this.pendingRequests.delete(id);
741
+ return;
742
+ }
743
+ call.onData?.(data);
744
+ if (complete) {
745
+ call.onComplete?.();
746
+ call.resolve(data);
747
+ this.pendingRequests.delete(id);
748
+ }
739
749
  }
740
- call.onData?.(data);
741
- if (complete) {
742
- call.onComplete?.();
743
- call.resolve(data);
744
- this.pendingRequests.delete(id);
750
+ });
751
+ try {
752
+ const parent = await handshake;
753
+ this.ready = true;
754
+ this.parent = parent;
755
+ Log.i('mujian sdk client init');
756
+ await this.call(events_EVENT.MUJIAN_INIT);
757
+ const projectInfo = await this.ai.chat.project.getInfo();
758
+ if (projectInfo.config?.customCss) {
759
+ const style = document.createElement('style');
760
+ style.setAttribute('type', 'text/css');
761
+ style.setAttribute('id', 'mujian-custom-css');
762
+ style.textContent = projectInfo.config.customCss;
763
+ document.head.appendChild(style);
745
764
  }
765
+ if (projectInfo.config?.customJs) {
766
+ const script = document.createElement("script");
767
+ script.setAttribute('type', "text/javascript");
768
+ script.setAttribute('id', 'mujian-custom-js');
769
+ script.textContent = projectInfo.config.customJs;
770
+ document.head.appendChild(script);
771
+ }
772
+ const config = await getConfig.call(this);
773
+ if (config) this._config = config;
774
+ } catch (error) {
775
+ Log.e('init error', error);
746
776
  }
747
- });
748
- try {
749
- const parent = await handshake;
750
- this.ready = true;
751
- this.parent = parent;
752
- Log.i('mujian sdk client init');
753
- await this.call(events_EVENT.MUJIAN_INIT);
754
- const projectInfo = await this.ai.chat.project.getInfo();
755
- if (projectInfo.config?.customCss) {
756
- const style = document.createElement('style');
757
- style.setAttribute('type', 'text/css');
758
- style.setAttribute('id', 'mujian-custom-css');
759
- style.textContent = projectInfo.config.customCss;
760
- document.head.appendChild(style);
761
- }
762
- if (projectInfo.config?.customJs) {
763
- const script = document.createElement("script");
764
- script.setAttribute('type', "text/javascript");
765
- script.setAttribute('id', 'mujian-custom-js');
766
- script.textContent = projectInfo.config.customJs;
767
- document.head.appendChild(script);
768
- }
769
- const config = await getConfig.call(this);
770
- if (config) this._config = config;
771
- } catch (error) {
772
- Log.e('init error', error);
773
- }
777
+ })();
778
+ return this.initPromise;
774
779
  }
775
780
  emit(event, data) {
776
781
  if (!this.ready) throw new Error('Mujian is not initialized');
package/dist/umd/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  /*! For license information please see index.js.LICENSE.txt */
2
- !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.MujianUMD=e():t.MujianUMD=e()}(globalThis,()=>(()=>{"use strict";var t,e={};e.d=(t,i)=>{for(var n in i)e.o(i,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:i[n]})},e.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),e.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var i={};e.r(i),e.d(i,{MujianSdk:()=>k,EVENT:()=>u});var n="application/x-postmate-v1+json",a=0,s=function(t){var e=document.createElement("a");e.href=t;var i=e.protocol.length>4?e.protocol:window.location.protocol,n=e.host.length?"80"===e.port||"443"===e.port?e.hostname:e.host:window.location.host;return e.origin||i+"//"+n},r={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},o=function(t,e){return("string"!=typeof e||t.origin===e)&&!!t.data&&("object"!=typeof t.data||"postmate"in t.data)&&t.data.type===n&&!!r[t.data.postmate]},c=function(t,e){var i="function"==typeof t[e]?t[e]():t[e];return h.Promise.resolve(i)},l=function(){function t(t){var e=this;this.parent=t.parent,this.frame=t.frame,this.child=t.child,this.childOrigin=t.childOrigin,this.events={},this.listener=function(t){if(!o(t,e.childOrigin))return!1;var i=((t||{}).data||{}).value||{},n=i.data,a=i.name;"emit"===t.data.postmate&&a in e.events&&e.events[a].call(e,n)},this.parent.addEventListener("message",this.listener,!1)}var e=t.prototype;return e.get=function(t){var e=this;return new h.Promise(function(i){var s=++a;e.parent.addEventListener("message",function t(n){n.data.uid===s&&"reply"===n.data.postmate&&(e.parent.removeEventListener("message",t,!1),i(n.data.value))},!1),e.child.postMessage({postmate:"request",type:n,property:t,uid:s},e.childOrigin)})},e.call=function(t,e){this.child.postMessage({postmate:"call",type:n,property:t,data:e},this.childOrigin)},e.on=function(t,e){this.events[t]=e},e.destroy=function(){window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)},t}(),d=function(){function t(t){var e=this;this.model=t.model,this.parent=t.parent,this.parentOrigin=t.parentOrigin,this.child=t.child,this.child.addEventListener("message",function(t){if(o(t,e.parentOrigin)){var i=t.data,a=i.property,s=i.uid,r=i.data;if("call"===t.data.postmate){a in e.model&&"function"==typeof e.model[a]&&e.model[a](r);return}c(e.model,a).then(function(e){return t.source.postMessage({property:a,postmate:"reply",type:n,uid:s,value:e},t.origin)})}})}return t.prototype.emit=function(t,e){this.parent.postMessage({postmate:"emit",type:n,value:{name:t,data:e}},this.parentOrigin)},t}(),h=function(){function t(t){var e=t.container,i=void 0===e?void 0!==i?i:document.body:e,n=t.model,a=t.url,s=t.name,r=t.classListArray;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=s||"",this.frame.classList.add.apply(this.frame.classList,void 0===r?[]:r),i.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=n||{},this.sendHandshake(a)}return t.prototype.sendHandshake=function(e){var i,a=this,r=s(e),c=0;return new t.Promise(function(t,s){a.parent.addEventListener("message",function e(n){return!!o(n,r)&&("handshake-reply"===n.data.postmate?(clearInterval(i),a.parent.removeEventListener("message",e,!1),a.childOrigin=n.origin,t(new l(a))):s("Failed handshake"))},!1);var d=function(){c++,a.child.postMessage({postmate:"handshake",type:n,model:a.model},r),5===c&&clearInterval(i)},h=function(){d(),i=setInterval(d,500)};a.frame.attachEvent?a.frame.attachEvent("onload",h):a.frame.onload=h,a.frame.src=e})},t}();h.debug=!1,h.Promise=function(){try{return window?window.Promise:Promise}catch(t){return null}}(),h.Model=function(){function t(t){return this.child=window,this.model=t,this.parent=this.child.parent,this.sendHandshakeReply()}return t.prototype.sendHandshakeReply=function(){var t=this;return new h.Promise(function(e,i){t.child.addEventListener("message",function a(s){if(s.data.postmate){if("handshake"===s.data.postmate){t.child.removeEventListener("message",a,!1),s.source.postMessage({postmate:"handshake-reply",type:n},s.origin),t.parentOrigin=s.origin;var r=s.data.model;return r&&Object.keys(r).forEach(function(e){t.model[e]=r[e]}),e(new d(t))}return i("Handshake Reply Failed")}},!1)})},t}();var u=((t={}).MUJIAN_INIT="mujian:init",t.MUJIAN_AI_CHAT_STOP="mujian:ai:chat:stop",t.MUJIAN_AI_CHAT_COMPLETE="mujian:ai:chat:complete",t.MUJIAN_AI_CHAT_APPLY_REGEX="mujian:ai:chat:applyRegex",t.MUJIAN_AI_CHAT_RENDER_MESSAGE="mujian:ai:chat:renderMessage",t.MUJIAN_AI_TEXT_GENERATE="mujian:ai:text:generate",t.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE="mujian:ai:openai:completions:create",t.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE="mujian:ai:openai:chat:completions:create",t.MUJIAN_AI_OPENAI_RESPONSES_CREATE="mujian:ai:openai:responses:create",t.MUJIAN_AI_CHAT_MESSAGE_GET_ALL="mujian:ai:chat:message:getAll",t.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE="mujian:ai:chat:message:getPage",t.MUJIAN_AI_CHAT_PROJECT_GET_INFO="mujian:ai:chat:project:getInfo",t.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE="mujian:ai:settings:persona:getActive",t.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE="mujian:ai:settings:persona:setActive",t.MUJIAN_AI_SETTINGS_MODEL_GET_ALL="mujian:ai:settings:model:getAll",t.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE="mujian:ai:settings:model:setActive",t.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE="mujian:ai:settings:model:getActive",t.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE="mujian:ai:chat:message:deleteOne",t.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE="mujian:ai:chat:message:editOne",t.MUJIAN_AI_CHAT_MESSAGE_SWIPE="mujian:ai:chat:message:swipe",t.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT="mujian:ai:chat:message:getPrompt",t.MUJIAN_AI_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",t.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",t.MUJIAN_GET_CONFIG="mujian:getConfig",t);let _=async function(t,e,i,n={}){let a,s,r,o;await this.call(u.MUJIAN_AI_CHAT_COMPLETE,{content:t},{onData:n.parseContent?(a="",s="",function(t){let i=(s+=t).split("\n");for(let t of(s=i.pop()||"",i))if(t.startsWith("data: "))try{let i=JSON.parse(t.slice(6));if(i.question_id&&(r=i.question_id),i.reply_id&&(o=i.reply_id),i.isFinished)return void e({isFinished:!0,deltaContent:"",fullContent:a,questionId:r,replyId:o});let n=i?.choices?.[0]?.delta?.content;n?.length>0&&(a+=n,e({isFinished:!1,deltaContent:n,fullContent:a,questionId:r,replyId:o}))}catch(t){e({isFinished:!0,error:t,deltaContent:"",fullContent:a,questionId:r,replyId:o});return}}):e,signal:i})},A=async function(t){return await this.call(u.MUJIAN_AI_CHAT_APPLY_REGEX,t)},m=async function(t){return await this.call(u.MUJIAN_AI_CHAT_RENDER_MESSAGE,t)},p=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_COMPLETE,{isContinue:!0},{onData:t,signal:e})},E=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_COMPLETE,{isRegenerate:!0},{onData:t,signal:e})},I=async function(){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_GET_ALL)},g=async function(t){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE,{messageId:t})},f=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE,{messageId:t,content:e})},T=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_SWIPE,{messageId:t,swipeId:e})},M=async function(t){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT,{messageId:t})};async function y(t,e){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE,{fromCursor:t,pageSize:e})}let N=async function(){return await this.call(u.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},C=async function(){return await this.call(u.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)},w=async function(t){return await this.call(u.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE,{personaId:t})},S=async function(){return await this.call(u.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE)},b=async function(t){return await this.call(u.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE,{modelId:t})},v=async function(){return await this.call(u.MUJIAN_AI_SETTINGS_MODEL_GET_ALL)};async function O(){return await this.call(u.MUJIAN_GET_CONFIG)}let j=async function(){},P=async function(){};async function J(t){return await this.call(u.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,t)}let G={i(...t){console.log("[MujianSDK] ",...t)},e(...t){console.error("[MujianSDK] ",...t)}};class U{constructor(){}static EVENT=u;static getInstance(){return window.$mujian||(window.$mujian=new U),window.$mujian}parent=null;ready=!1;get isReady(){return this.ready}pendingRequests=new Map;_config;get config(){return this._config}async init(){let t=new h.Model({reply:({id:t,complete:e,data:i,error:n})=>{let a=this.pendingRequests.get(t);if(a){if(n){a.reject(n),this.pendingRequests.delete(t);return}a.onData?.(i),e&&(a.onComplete?.(),a.resolve(i),this.pendingRequests.delete(t))}}});try{let e=await t;this.ready=!0,this.parent=e,G.i("mujian sdk client init"),await this.call(u.MUJIAN_INIT);let i=await this.ai.chat.project.getInfo();if(i.config?.customCss){let t=document.createElement("style");t.setAttribute("type","text/css"),t.setAttribute("id","mujian-custom-css"),t.textContent=i.config.customCss,document.head.appendChild(t)}if(i.config?.customJs){let t=document.createElement("script");t.setAttribute("type","text/javascript"),t.setAttribute("id","mujian-custom-js"),t.textContent=i.config.customJs,document.head.appendChild(t)}let n=await O.call(this);n&&(this._config=n)}catch(t){G.e("init error",t)}}emit(t,e){if(!this.ready)throw Error("Mujian is not initialized");this.parent?.emit(t,e)}nextCallId=0;getCallId(){let t=this.nextCallId;return this.nextCallId+=1,t}async call(t,e,i){if(!this.ready)throw Error("Mujian is not initialized");let n=this.getCallId();return new Promise((a,s)=>{this.pendingRequests.set(n,{resolve:a,reject:s,...i}),this.emit(t,{id:n,data:e}),i?.signal?.addEventListener("abort",()=>{this.emit(u.MUJIAN_AI_CHAT_STOP,{id:n})})})}ai={chat:{project:{getInfo:N.bind(this)},settings:{model:{getAll:v.bind(this),getActive:S.bind(this),setActive:b.bind(this)},persona:{getActive:C.bind(this),setActive:w.bind(this)}},complete:_.bind(this),applyRegex:A.bind(this),renderMessage:m.bind(this),continue:p.bind(this),regenerate:E.bind(this),message:{getAll:I.bind(this),deleteOne:g.bind(this),editOne:f.bind(this),swipe:T.bind(this),getPrompt:M.bind(this),getPage:y.bind(this)}},text:{complete:R.bind(this)},openai:{completions:{create:H.create.bind(this)},chat:{completions:{create:L.completions.create.bind(this)}},responses:{create:x.create.bind(this)},images:{generate:D.generate.bind(this)}}};ui={};utils={clipboard:{writeText:J.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:j.bind(this),loadGame:P.bind(this)},ranking:()=>{console.log("ranking")}}}let R=async function(t){return await this.call(U.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:t})},L={completions:{create:async function(t,e,i,n){return await this.call(U.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE,{params:t,options:e},{onData:i,signal:n})}}},H={create:async function(t,e,i,n){return await this.call(U.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:t,options:e},{onData:i,signal:n})}},x={create:async function(t,e,i,n){return await this.call(U.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE,{params:t,options:e},{onData:i,signal:n})}},D={generate:async function(t,e,i,n){return await this.call(U.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:t,options:e},{onData:i,signal:n})}};class k{constructor(){}static EVENT=u;static getInstance(){return window.$mujian||(window.$mujian=new k),window.$mujian}parent=null;ready=!1;get isReady(){return this.ready}pendingRequests=new Map;_config;get config(){return this._config}async init(){let t=new h.Model({reply:({id:t,complete:e,data:i,error:n})=>{let a=this.pendingRequests.get(t);if(a){if(n){a.reject(n),this.pendingRequests.delete(t);return}a.onData?.(i),e&&(a.onComplete?.(),a.resolve(i),this.pendingRequests.delete(t))}}});try{let e=await t;this.ready=!0,this.parent=e,G.i("mujian sdk client init"),await this.call(u.MUJIAN_INIT);let i=await this.ai.chat.project.getInfo();if(i.config?.customCss){let t=document.createElement("style");t.setAttribute("type","text/css"),t.setAttribute("id","mujian-custom-css"),t.textContent=i.config.customCss,document.head.appendChild(t)}if(i.config?.customJs){let t=document.createElement("script");t.setAttribute("type","text/javascript"),t.setAttribute("id","mujian-custom-js"),t.textContent=i.config.customJs,document.head.appendChild(t)}let n=await O.call(this);n&&(this._config=n)}catch(t){G.e("init error",t)}}emit(t,e){if(!this.ready)throw Error("Mujian is not initialized");this.parent?.emit(t,e)}nextCallId=0;getCallId(){let t=this.nextCallId;return this.nextCallId+=1,t}async call(t,e,i){if(!this.ready)throw Error("Mujian is not initialized");let n=this.getCallId();return new Promise((a,s)=>{this.pendingRequests.set(n,{resolve:a,reject:s,...i}),this.emit(t,{id:n,data:e}),i?.signal?.addEventListener("abort",()=>{this.emit(u.MUJIAN_AI_CHAT_STOP,{id:n})})})}ai={chat:{project:{getInfo:N.bind(this)},settings:{model:{getAll:v.bind(this),getActive:S.bind(this),setActive:b.bind(this)},persona:{getActive:C.bind(this),setActive:w.bind(this)}},complete:_.bind(this),applyRegex:A.bind(this),renderMessage:m.bind(this),continue:p.bind(this),regenerate:E.bind(this),message:{getAll:I.bind(this),deleteOne:g.bind(this),editOne:f.bind(this),swipe:T.bind(this),getPrompt:M.bind(this),getPage:y.bind(this)}},text:{complete:R.bind(this)},openai:{completions:{create:H.create.bind(this)},chat:{completions:{create:L.completions.create.bind(this)}},responses:{create:x.create.bind(this)},images:{generate:D.generate.bind(this)}}};ui={};utils={clipboard:{writeText:J.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:j.bind(this),loadGame:P.bind(this)},ranking:()=>{console.log("ranking")}}}return i})());
2
+ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.MujianUMD=e():t.MujianUMD=e()}(globalThis,()=>(()=>{"use strict";var t,e={};e.d=(t,i)=>{for(var n in i)e.o(i,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:i[n]})},e.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),e.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var i={};e.r(i),e.d(i,{MujianSdk:()=>k,EVENT:()=>u});var n="application/x-postmate-v1+json",a=0,s=function(t){var e=document.createElement("a");e.href=t;var i=e.protocol.length>4?e.protocol:window.location.protocol,n=e.host.length?"80"===e.port||"443"===e.port?e.hostname:e.host:window.location.host;return e.origin||i+"//"+n},r={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},o=function(t,e){return("string"!=typeof e||t.origin===e)&&!!t.data&&("object"!=typeof t.data||"postmate"in t.data)&&t.data.type===n&&!!r[t.data.postmate]},c=function(t,e){var i="function"==typeof t[e]?t[e]():t[e];return h.Promise.resolve(i)},l=function(){function t(t){var e=this;this.parent=t.parent,this.frame=t.frame,this.child=t.child,this.childOrigin=t.childOrigin,this.events={},this.listener=function(t){if(!o(t,e.childOrigin))return!1;var i=((t||{}).data||{}).value||{},n=i.data,a=i.name;"emit"===t.data.postmate&&a in e.events&&e.events[a].call(e,n)},this.parent.addEventListener("message",this.listener,!1)}var e=t.prototype;return e.get=function(t){var e=this;return new h.Promise(function(i){var s=++a;e.parent.addEventListener("message",function t(n){n.data.uid===s&&"reply"===n.data.postmate&&(e.parent.removeEventListener("message",t,!1),i(n.data.value))},!1),e.child.postMessage({postmate:"request",type:n,property:t,uid:s},e.childOrigin)})},e.call=function(t,e){this.child.postMessage({postmate:"call",type:n,property:t,data:e},this.childOrigin)},e.on=function(t,e){this.events[t]=e},e.destroy=function(){window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)},t}(),d=function(){function t(t){var e=this;this.model=t.model,this.parent=t.parent,this.parentOrigin=t.parentOrigin,this.child=t.child,this.child.addEventListener("message",function(t){if(o(t,e.parentOrigin)){var i=t.data,a=i.property,s=i.uid,r=i.data;if("call"===t.data.postmate){a in e.model&&"function"==typeof e.model[a]&&e.model[a](r);return}c(e.model,a).then(function(e){return t.source.postMessage({property:a,postmate:"reply",type:n,uid:s,value:e},t.origin)})}})}return t.prototype.emit=function(t,e){this.parent.postMessage({postmate:"emit",type:n,value:{name:t,data:e}},this.parentOrigin)},t}(),h=function(){function t(t){var e=t.container,i=void 0===e?void 0!==i?i:document.body:e,n=t.model,a=t.url,s=t.name,r=t.classListArray;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=s||"",this.frame.classList.add.apply(this.frame.classList,void 0===r?[]:r),i.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=n||{},this.sendHandshake(a)}return t.prototype.sendHandshake=function(e){var i,a=this,r=s(e),c=0;return new t.Promise(function(t,s){a.parent.addEventListener("message",function e(n){return!!o(n,r)&&("handshake-reply"===n.data.postmate?(clearInterval(i),a.parent.removeEventListener("message",e,!1),a.childOrigin=n.origin,t(new l(a))):s("Failed handshake"))},!1);var d=function(){c++,a.child.postMessage({postmate:"handshake",type:n,model:a.model},r),5===c&&clearInterval(i)},h=function(){d(),i=setInterval(d,500)};a.frame.attachEvent?a.frame.attachEvent("onload",h):a.frame.onload=h,a.frame.src=e})},t}();h.debug=!1,h.Promise=function(){try{return window?window.Promise:Promise}catch(t){return null}}(),h.Model=function(){function t(t){return this.child=window,this.model=t,this.parent=this.child.parent,this.sendHandshakeReply()}return t.prototype.sendHandshakeReply=function(){var t=this;return new h.Promise(function(e,i){t.child.addEventListener("message",function a(s){if(s.data.postmate){if("handshake"===s.data.postmate){t.child.removeEventListener("message",a,!1),s.source.postMessage({postmate:"handshake-reply",type:n},s.origin),t.parentOrigin=s.origin;var r=s.data.model;return r&&Object.keys(r).forEach(function(e){t.model[e]=r[e]}),e(new d(t))}return i("Handshake Reply Failed")}},!1)})},t}();var u=((t={}).MUJIAN_INIT="mujian:init",t.MUJIAN_AI_CHAT_STOP="mujian:ai:chat:stop",t.MUJIAN_AI_CHAT_COMPLETE="mujian:ai:chat:complete",t.MUJIAN_AI_CHAT_APPLY_REGEX="mujian:ai:chat:applyRegex",t.MUJIAN_AI_CHAT_RENDER_MESSAGE="mujian:ai:chat:renderMessage",t.MUJIAN_AI_TEXT_GENERATE="mujian:ai:text:generate",t.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE="mujian:ai:openai:completions:create",t.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE="mujian:ai:openai:chat:completions:create",t.MUJIAN_AI_OPENAI_RESPONSES_CREATE="mujian:ai:openai:responses:create",t.MUJIAN_AI_CHAT_MESSAGE_GET_ALL="mujian:ai:chat:message:getAll",t.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE="mujian:ai:chat:message:getPage",t.MUJIAN_AI_CHAT_PROJECT_GET_INFO="mujian:ai:chat:project:getInfo",t.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE="mujian:ai:settings:persona:getActive",t.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE="mujian:ai:settings:persona:setActive",t.MUJIAN_AI_SETTINGS_MODEL_GET_ALL="mujian:ai:settings:model:getAll",t.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE="mujian:ai:settings:model:setActive",t.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE="mujian:ai:settings:model:getActive",t.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE="mujian:ai:chat:message:deleteOne",t.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE="mujian:ai:chat:message:editOne",t.MUJIAN_AI_CHAT_MESSAGE_SWIPE="mujian:ai:chat:message:swipe",t.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT="mujian:ai:chat:message:getPrompt",t.MUJIAN_AI_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",t.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",t.MUJIAN_GET_CONFIG="mujian:getConfig",t);let _=async function(t,e,i,n={}){let a,s,r,o;await this.call(u.MUJIAN_AI_CHAT_COMPLETE,{content:t},{onData:n.parseContent?(a="",s="",function(t){let i=(s+=t).split("\n");for(let t of(s=i.pop()||"",i))if(t.startsWith("data: "))try{let i=JSON.parse(t.slice(6));if(i.question_id&&(r=i.question_id),i.reply_id&&(o=i.reply_id),i.isFinished)return void e({isFinished:!0,deltaContent:"",fullContent:a,questionId:r,replyId:o});let n=i?.choices?.[0]?.delta?.content;n?.length>0&&(a+=n,e({isFinished:!1,deltaContent:n,fullContent:a,questionId:r,replyId:o}))}catch(t){e({isFinished:!0,error:t,deltaContent:"",fullContent:a,questionId:r,replyId:o});return}}):e,signal:i})},A=async function(t){return await this.call(u.MUJIAN_AI_CHAT_APPLY_REGEX,t)},m=async function(t){return await this.call(u.MUJIAN_AI_CHAT_RENDER_MESSAGE,t)},p=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_COMPLETE,{isContinue:!0},{onData:t,signal:e})},E=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_COMPLETE,{isRegenerate:!0},{onData:t,signal:e})},I=async function(){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_GET_ALL)},g=async function(t){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE,{messageId:t})},f=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE,{messageId:t,content:e})},T=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_SWIPE,{messageId:t,swipeId:e})},y=async function(t){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT,{messageId:t})};async function M(t,e){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE,{fromCursor:t,pageSize:e})}let N=async function(){return await this.call(u.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},C=async function(){return await this.call(u.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)},w=async function(t){return await this.call(u.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE,{personaId:t})},S=async function(){return await this.call(u.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE)},b=async function(t){return await this.call(u.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE,{modelId:t})},v=async function(){return await this.call(u.MUJIAN_AI_SETTINGS_MODEL_GET_ALL)};async function O(){return await this.call(u.MUJIAN_GET_CONFIG)}let P=async function(){},j=async function(){};async function J(t){return await this.call(u.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,t)}let G={i(...t){console.log("[MujianSDK] ",...t)},e(...t){console.error("[MujianSDK] ",...t)}};class U{constructor(){}static EVENT=u;static getInstance(){return window.$mujian||(window.$mujian=new U),window.$mujian}parent=null;ready=!1;get isReady(){return this.ready}pendingRequests=new Map;_config;get openapi(){return this._config?.openapi}initPromise=null;async init(){return this.initPromise||(this.initPromise=(async()=>{let t=new h.Model({reply:({id:t,complete:e,data:i,error:n})=>{let a=this.pendingRequests.get(t);if(a){if(n){a.reject(n),this.pendingRequests.delete(t);return}a.onData?.(i),e&&(a.onComplete?.(),a.resolve(i),this.pendingRequests.delete(t))}}});try{let e=await t;this.ready=!0,this.parent=e,G.i("mujian sdk client init"),await this.call(u.MUJIAN_INIT);let i=await this.ai.chat.project.getInfo();if(i.config?.customCss){let t=document.createElement("style");t.setAttribute("type","text/css"),t.setAttribute("id","mujian-custom-css"),t.textContent=i.config.customCss,document.head.appendChild(t)}if(i.config?.customJs){let t=document.createElement("script");t.setAttribute("type","text/javascript"),t.setAttribute("id","mujian-custom-js"),t.textContent=i.config.customJs,document.head.appendChild(t)}let n=await O.call(this);n&&(this._config=n)}catch(t){G.e("init error",t)}})()),this.initPromise}emit(t,e){if(!this.ready)throw Error("Mujian is not initialized");this.parent?.emit(t,e)}nextCallId=0;getCallId(){let t=this.nextCallId;return this.nextCallId+=1,t}async call(t,e,i){if(!this.ready)throw Error("Mujian is not initialized");let n=this.getCallId();return new Promise((a,s)=>{this.pendingRequests.set(n,{resolve:a,reject:s,...i}),this.emit(t,{id:n,data:e}),i?.signal?.addEventListener("abort",()=>{this.emit(u.MUJIAN_AI_CHAT_STOP,{id:n})})})}ai={chat:{project:{getInfo:N.bind(this)},settings:{model:{getAll:v.bind(this),getActive:S.bind(this),setActive:b.bind(this)},persona:{getActive:C.bind(this),setActive:w.bind(this)}},complete:_.bind(this),applyRegex:A.bind(this),renderMessage:m.bind(this),continue:p.bind(this),regenerate:E.bind(this),message:{getAll:I.bind(this),deleteOne:g.bind(this),editOne:f.bind(this),swipe:T.bind(this),getPrompt:y.bind(this),getPage:M.bind(this)}},text:{complete:R.bind(this)},openai:{completions:{create:H.create.bind(this)},chat:{completions:{create:L.completions.create.bind(this)}},responses:{create:x.create.bind(this)},images:{generate:D.generate.bind(this)}}};ui={};utils={clipboard:{writeText:J.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:P.bind(this),loadGame:j.bind(this)},ranking:()=>{console.log("ranking")}}}let R=async function(t){return await this.call(U.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:t})},L={completions:{create:async function(t,e,i,n){return await this.call(U.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE,{params:t,options:e},{onData:i,signal:n})}}},H={create:async function(t,e,i,n){return await this.call(U.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:t,options:e},{onData:i,signal:n})}},x={create:async function(t,e,i,n){return await this.call(U.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE,{params:t,options:e},{onData:i,signal:n})}},D={generate:async function(t,e,i,n){return await this.call(U.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:t,options:e},{onData:i,signal:n})}};class k{constructor(){}static EVENT=u;static getInstance(){return window.$mujian||(window.$mujian=new k),window.$mujian}parent=null;ready=!1;get isReady(){return this.ready}pendingRequests=new Map;_config;get openapi(){return this._config?.openapi}initPromise=null;async init(){return this.initPromise||(this.initPromise=(async()=>{let t=new h.Model({reply:({id:t,complete:e,data:i,error:n})=>{let a=this.pendingRequests.get(t);if(a){if(n){a.reject(n),this.pendingRequests.delete(t);return}a.onData?.(i),e&&(a.onComplete?.(),a.resolve(i),this.pendingRequests.delete(t))}}});try{let e=await t;this.ready=!0,this.parent=e,G.i("mujian sdk client init"),await this.call(u.MUJIAN_INIT);let i=await this.ai.chat.project.getInfo();if(i.config?.customCss){let t=document.createElement("style");t.setAttribute("type","text/css"),t.setAttribute("id","mujian-custom-css"),t.textContent=i.config.customCss,document.head.appendChild(t)}if(i.config?.customJs){let t=document.createElement("script");t.setAttribute("type","text/javascript"),t.setAttribute("id","mujian-custom-js"),t.textContent=i.config.customJs,document.head.appendChild(t)}let n=await O.call(this);n&&(this._config=n)}catch(t){G.e("init error",t)}})()),this.initPromise}emit(t,e){if(!this.ready)throw Error("Mujian is not initialized");this.parent?.emit(t,e)}nextCallId=0;getCallId(){let t=this.nextCallId;return this.nextCallId+=1,t}async call(t,e,i){if(!this.ready)throw Error("Mujian is not initialized");let n=this.getCallId();return new Promise((a,s)=>{this.pendingRequests.set(n,{resolve:a,reject:s,...i}),this.emit(t,{id:n,data:e}),i?.signal?.addEventListener("abort",()=>{this.emit(u.MUJIAN_AI_CHAT_STOP,{id:n})})})}ai={chat:{project:{getInfo:N.bind(this)},settings:{model:{getAll:v.bind(this),getActive:S.bind(this),setActive:b.bind(this)},persona:{getActive:C.bind(this),setActive:w.bind(this)}},complete:_.bind(this),applyRegex:A.bind(this),renderMessage:m.bind(this),continue:p.bind(this),regenerate:E.bind(this),message:{getAll:I.bind(this),deleteOne:g.bind(this),editOne:f.bind(this),swipe:T.bind(this),getPrompt:y.bind(this),getPage:M.bind(this)}},text:{complete:R.bind(this)},openai:{completions:{create:H.create.bind(this)},chat:{completions:{create:L.completions.create.bind(this)}},responses:{create:x.create.bind(this)},images:{generate:D.generate.bind(this)}}};ui={};utils={clipboard:{writeText:J.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:P.bind(this),loadGame:j.bind(this)},ranking:()=>{console.log("ranking")}}}return i})());
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:()=>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]},l=function(e,t){var i="function"==typeof e[t]?e[t]():e[t];return m.Promise.resolve(i)},d=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}l(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),l=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 d(a))):r("Failed handshake"))},!1);var c=function(){l++,a.child.postMessage({postmate:"handshake",type:n,model:a.model},s),5===l&&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(console.log("call init"),this.ready)?Promise.resolve():(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})());
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 p={i(...e){console.log("[MujianSDK] ",...e)},e(...e){console.error("[MujianSDK] ",...e)}};class h{constructor(){}static EVENT=u;static getInstance(){return window.$mujian_lite||(window.$mujian_lite=new h),window.$mujian_lite}parent=null;ready=!1;get isReady(){return this.ready}pendingRequests=new Map;_config;get openapi(){return this._config?.openapi}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,p.i("mujian sdk lite client init"),await this.call(u.MUJIAN_INIT),this._config=await this.call(u.MUJIAN_GET_CONFIG)}catch(e){throw p.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})})}}h.getInstance().init();let _=h;return i})());
package/dist/umd/react.js CHANGED
@@ -130,7 +130,7 @@ ${n}
130
130
  <iframe id="${c}" class="w-full" sandbox="${t?"allow-scripts":"allow-scripts allow-same-origin"}" loading="lazy"
131
131
  referrerpolicy="no-referrer" allowTransparency="true"
132
132
  style="color-scheme: none;background-color: transparent;width:100%;;border:none;" srcdoc="`+l.replace(/"/g,"&quot;").replace(/'/g,"&#39;")+`"></iframe>
133
- </div>`}),n.current&&(n.current.innerHTML=d)},[e]),(0,A.useEffect)(()=>{window.addEventListener("message",function(e){if(e.data?.type==="MJ_ADJUST_IFRAME_HEIGHT"&&e.data?.iframe_id){let t=n.current?.querySelector("#"+e.data.iframe_id);t&&(t.style.height=`${e.data.height}px`)}}),window.addEventListener("resize",()=>{d.forEach(e=>{let t=n.current?.querySelector("#MJ-iframe-"+e);t&&t.contentWindow?.postMessage({type:"MJ_UPDATE_VIEWPORT_HEIGHT"},"*")})})},[]),(0,A.useEffect)(()=>(d.forEach(e=>{let t=n.current?.querySelector("#MJ-iframe-"+e);if(!t)return;let r=()=>{let r=t.parentElement?.querySelector("#MJ-spin-"+e);r&&r.remove()},d=setTimeout(r,5e3);t.addEventListener("load",()=>{clearTimeout(d),r()})}),()=>{d.forEach(e=>{let t=n.current?.querySelector("#MJ-iframe-"+e);if(!t)return;let r=t.parentElement?.querySelector("#MJ-spin-"+t.id);r&&r.remove(),t.removeEventListener("load",()=>{tf.i("iframe loaded",t.id)})})}),[d]),(0,ep.jsx)("div",{className:"mes_text",ref:n})},tw=S().memo(tb,(e,t)=>e.content===t.content);tb.displayName="MdRenderer",tw.displayName="MdRenderer";var ty="application/x-postmate-v1+json",tv=0,tk=function(e){var t=document.createElement("a");t.href=e;var r=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||r+"//"+n},tE={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},tA=function(e,t){return("string"!=typeof t||e.origin===t)&&!!e.data&&("object"!=typeof e.data||"postmate"in e.data)&&e.data.type===ty&&!!tE[e.data.postmate]},tS=function(e,t){var r="function"==typeof e[t]?e[t]():e[t];return tj.Promise.resolve(r)},tT=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(!tA(e,t.childOrigin))return!1;var r=((e||{}).data||{}).value||{},n=r.data,d=r.name;"emit"===e.data.postmate&&d in t.events&&t.events[d].call(t,n)},this.parent.addEventListener("message",this.listener,!1)}var t=e.prototype;return t.get=function(e){var t=this;return new tj.Promise(function(r){var n=++tv;t.parent.addEventListener("message",function e(d){d.data.uid===n&&"reply"===d.data.postmate&&(t.parent.removeEventListener("message",e,!1),r(d.data.value))},!1),t.child.postMessage({postmate:"request",type:ty,property:e,uid:n},t.childOrigin)})},t.call=function(e,t){this.child.postMessage({postmate:"call",type:ty,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}(),tx=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(tA(e,t.parentOrigin)){var r=e.data,n=r.property,d=r.uid,a=r.data;if("call"===e.data.postmate){n in t.model&&"function"==typeof t.model[n]&&t.model[n](a);return}tS(t.model,n).then(function(t){return e.source.postMessage({property:n,postmate:"reply",type:ty,uid:d,value:t},e.origin)})}})}return e.prototype.emit=function(e,t){this.parent.postMessage({postmate:"emit",type:ty,value:{name:e,data:t}},this.parentOrigin)},e}(),tj=function(){function e(e){var t=e.container,r=void 0===t?void 0!==r?r:document.body:t,n=e.model,d=e.url,a=e.name,i=e.classListArray;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=a||"",this.frame.classList.add.apply(this.frame.classList,void 0===i?[]:i),r.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=n||{},this.sendHandshake(d)}return e.prototype.sendHandshake=function(t){var r,n=this,d=tk(t),a=0;return new e.Promise(function(e,i){n.parent.addEventListener("message",function t(a){return!!tA(a,d)&&("handshake-reply"===a.data.postmate?(clearInterval(r),n.parent.removeEventListener("message",t,!1),n.childOrigin=a.origin,e(new tT(n))):i("Failed handshake"))},!1);var u=function(){a++,n.child.postMessage({postmate:"handshake",type:ty,model:n.model},d),5===a&&clearInterval(r)},o=function(){u(),r=setInterval(u,500)};n.frame.attachEvent?n.frame.attachEvent("onload",o):n.frame.onload=o,n.frame.src=t})},e}();tj.debug=!1,tj.Promise=function(){try{return window?window.Promise:Promise}catch(e){return null}}(),tj.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 tj.Promise(function(t,r){e.child.addEventListener("message",function n(d){if(d.data.postmate){if("handshake"===d.data.postmate){e.child.removeEventListener("message",n,!1),d.source.postMessage({postmate:"handshake-reply",type:ty},d.origin),e.parentOrigin=d.origin;var a=d.data.model;return a&&Object.keys(a).forEach(function(t){e.model[t]=a[t]}),t(new tx(e))}return r("Handshake Reply Failed")}},!1)})},e}();var tM=((p={}).MUJIAN_INIT="mujian:init",p.MUJIAN_AI_CHAT_STOP="mujian:ai:chat:stop",p.MUJIAN_AI_CHAT_COMPLETE="mujian:ai:chat:complete",p.MUJIAN_AI_CHAT_APPLY_REGEX="mujian:ai:chat:applyRegex",p.MUJIAN_AI_CHAT_RENDER_MESSAGE="mujian:ai:chat:renderMessage",p.MUJIAN_AI_TEXT_GENERATE="mujian:ai:text:generate",p.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE="mujian:ai:openai:completions:create",p.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE="mujian:ai:openai:chat:completions:create",p.MUJIAN_AI_OPENAI_RESPONSES_CREATE="mujian:ai:openai:responses:create",p.MUJIAN_AI_CHAT_MESSAGE_GET_ALL="mujian:ai:chat:message:getAll",p.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE="mujian:ai:chat:message:getPage",p.MUJIAN_AI_CHAT_PROJECT_GET_INFO="mujian:ai:chat:project:getInfo",p.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE="mujian:ai:settings:persona:getActive",p.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE="mujian:ai:settings:persona:setActive",p.MUJIAN_AI_SETTINGS_MODEL_GET_ALL="mujian:ai:settings:model:getAll",p.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE="mujian:ai:settings:model:setActive",p.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE="mujian:ai:settings:model:getActive",p.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE="mujian:ai:chat:message:deleteOne",p.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE="mujian:ai:chat:message:editOne",p.MUJIAN_AI_CHAT_MESSAGE_SWIPE="mujian:ai:chat:message:swipe",p.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT="mujian:ai:chat:message:getPrompt",p.MUJIAN_AI_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",p.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",p.MUJIAN_GET_CONFIG="mujian:getConfig",p);let tI=async function(e,t,r,n={}){let d,a,i,u;await this.call(tM.MUJIAN_AI_CHAT_COMPLETE,{content:e},{onData:n.parseContent?(d="",a="",function(e){let r=(a+=e).split("\n");for(let e of(a=r.pop()||"",r))if(e.startsWith("data: "))try{let r=JSON.parse(e.slice(6));if(r.question_id&&(i=r.question_id),r.reply_id&&(u=r.reply_id),r.isFinished)return void t({isFinished:!0,deltaContent:"",fullContent:d,questionId:i,replyId:u});let n=r?.choices?.[0]?.delta?.content;n?.length>0&&(d+=n,t({isFinished:!1,deltaContent:n,fullContent:d,questionId:i,replyId:u}))}catch(e){t({isFinished:!0,error:e,deltaContent:"",fullContent:d,questionId:i,replyId:u});return}}):t,signal:r})},tP=async function(e){return await this.call(tM.MUJIAN_AI_CHAT_APPLY_REGEX,e)},tC=async function(e){return await this.call(tM.MUJIAN_AI_CHAT_RENDER_MESSAGE,e)},tO=async function(e,t){return await this.call(tM.MUJIAN_AI_CHAT_COMPLETE,{isContinue:!0},{onData:e,signal:t})},tN=async function(e,t){return await this.call(tM.MUJIAN_AI_CHAT_COMPLETE,{isRegenerate:!0},{onData:e,signal:t})},tL=async function(){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_GET_ALL)},tR=async function(e){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE,{messageId:e})},tz=async function(e,t){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE,{messageId:e,content:t})},t$=async function(e,t){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_SWIPE,{messageId:e,swipeId:t})},tH=async function(e){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT,{messageId:e})};async function tD(e,t){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE,{fromCursor:e,pageSize:t})}let tU=async function(){return await this.call(tM.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},tB=async function(){return await this.call(tM.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)},tG=async function(e){return await this.call(tM.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE,{personaId:e})},tF=async function(){return await this.call(tM.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE)},tq=async function(e){return await this.call(tM.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE,{modelId:e})},tV=async function(){return await this.call(tM.MUJIAN_AI_SETTINGS_MODEL_GET_ALL)},tJ=async function(e){return await this.call(t0.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:e})},tW={completions:{create:async function(e,t,r,n){return await this.call(t0.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE,{params:e,options:t},{onData:r,signal:n})}}},tY={create:async function(e,t,r,n){return await this.call(t0.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:e,options:t},{onData:r,signal:n})}},tK={create:async function(e,t,r,n){return await this.call(t0.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE,{params:e,options:t},{onData:r,signal:n})}},tX={generate:async function(e,t,r,n){return await this.call(t0.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:e,options:t},{onData:r,signal:n})}};async function tZ(){return await this.call(tM.MUJIAN_GET_CONFIG)}let tQ=async function(){},t3=async function(){};async function t8(e){return await this.call(tM.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,e)}class t0{constructor(){}static EVENT=tM;static getInstance(){return window.$mujian||(window.$mujian=new t0),window.$mujian}parent=null;ready=!1;get isReady(){return this.ready}pendingRequests=new Map;_config;get config(){return this._config}async init(){let e=new tj.Model({reply:({id:e,complete:t,data:r,error:n})=>{let d=this.pendingRequests.get(e);if(d){if(n){d.reject(n),this.pendingRequests.delete(e);return}d.onData?.(r),t&&(d.onComplete?.(),d.resolve(r),this.pendingRequests.delete(e))}}});try{let t=await e;this.ready=!0,this.parent=t,tf.i("mujian sdk client init"),await this.call(tM.MUJIAN_INIT);let r=await this.ai.chat.project.getInfo();if(r.config?.customCss){let e=document.createElement("style");e.setAttribute("type","text/css"),e.setAttribute("id","mujian-custom-css"),e.textContent=r.config.customCss,document.head.appendChild(e)}if(r.config?.customJs){let e=document.createElement("script");e.setAttribute("type","text/javascript"),e.setAttribute("id","mujian-custom-js"),e.textContent=r.config.customJs,document.head.appendChild(e)}let n=await tZ.call(this);n&&(this._config=n)}catch(e){tf.e("init error",e)}}emit(e,t){if(!this.ready)throw Error("Mujian is not initialized");this.parent?.emit(e,t)}nextCallId=0;getCallId(){let e=this.nextCallId;return this.nextCallId+=1,e}async call(e,t,r){if(!this.ready)throw Error("Mujian is not initialized");let n=this.getCallId();return new Promise((d,a)=>{this.pendingRequests.set(n,{resolve:d,reject:a,...r}),this.emit(e,{id:n,data:t}),r?.signal?.addEventListener("abort",()=>{this.emit(tM.MUJIAN_AI_CHAT_STOP,{id:n})})})}ai={chat:{project:{getInfo:tU.bind(this)},settings:{model:{getAll:tV.bind(this),getActive:tF.bind(this),setActive:tq.bind(this)},persona:{getActive:tB.bind(this),setActive:tG.bind(this)}},complete:tI.bind(this),applyRegex:tP.bind(this),renderMessage:tC.bind(this),continue:tO.bind(this),regenerate:tN.bind(this),message:{getAll:tL.bind(this),deleteOne:tR.bind(this),editOne:tz.bind(this),swipe:t$.bind(this),getPrompt:tH.bind(this),getPage:tD.bind(this)}},text:{complete:tJ.bind(this)},openai:{completions:{create:tY.create.bind(this)},chat:{completions:{create:tW.completions.create.bind(this)}},responses:{create:tK.create.bind(this)},images:{generate:tX.generate.bind(this)}}};ui={};utils={clipboard:{writeText:t8.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:tQ.bind(this),loadGame:t3.bind(this)},ranking:()=>{console.log("ranking")}}}let t1=`
133
+ </div>`}),n.current&&(n.current.innerHTML=d)},[e]),(0,A.useEffect)(()=>{window.addEventListener("message",function(e){if(e.data?.type==="MJ_ADJUST_IFRAME_HEIGHT"&&e.data?.iframe_id){let t=n.current?.querySelector("#"+e.data.iframe_id);t&&(t.style.height=`${e.data.height}px`)}}),window.addEventListener("resize",()=>{d.forEach(e=>{let t=n.current?.querySelector("#MJ-iframe-"+e);t&&t.contentWindow?.postMessage({type:"MJ_UPDATE_VIEWPORT_HEIGHT"},"*")})})},[]),(0,A.useEffect)(()=>(d.forEach(e=>{let t=n.current?.querySelector("#MJ-iframe-"+e);if(!t)return;let r=()=>{let r=t.parentElement?.querySelector("#MJ-spin-"+e);r&&r.remove()},d=setTimeout(r,5e3);t.addEventListener("load",()=>{clearTimeout(d),r()})}),()=>{d.forEach(e=>{let t=n.current?.querySelector("#MJ-iframe-"+e);if(!t)return;let r=t.parentElement?.querySelector("#MJ-spin-"+t.id);r&&r.remove(),t.removeEventListener("load",()=>{tf.i("iframe loaded",t.id)})})}),[d]),(0,ep.jsx)("div",{className:"mes_text",ref:n})},tw=S().memo(tb,(e,t)=>e.content===t.content);tb.displayName="MdRenderer",tw.displayName="MdRenderer";var ty="application/x-postmate-v1+json",tv=0,tk=function(e){var t=document.createElement("a");t.href=e;var r=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||r+"//"+n},tE={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},tA=function(e,t){return("string"!=typeof t||e.origin===t)&&!!e.data&&("object"!=typeof e.data||"postmate"in e.data)&&e.data.type===ty&&!!tE[e.data.postmate]},tS=function(e,t){var r="function"==typeof e[t]?e[t]():e[t];return tj.Promise.resolve(r)},tT=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(!tA(e,t.childOrigin))return!1;var r=((e||{}).data||{}).value||{},n=r.data,d=r.name;"emit"===e.data.postmate&&d in t.events&&t.events[d].call(t,n)},this.parent.addEventListener("message",this.listener,!1)}var t=e.prototype;return t.get=function(e){var t=this;return new tj.Promise(function(r){var n=++tv;t.parent.addEventListener("message",function e(d){d.data.uid===n&&"reply"===d.data.postmate&&(t.parent.removeEventListener("message",e,!1),r(d.data.value))},!1),t.child.postMessage({postmate:"request",type:ty,property:e,uid:n},t.childOrigin)})},t.call=function(e,t){this.child.postMessage({postmate:"call",type:ty,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}(),tx=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(tA(e,t.parentOrigin)){var r=e.data,n=r.property,d=r.uid,a=r.data;if("call"===e.data.postmate){n in t.model&&"function"==typeof t.model[n]&&t.model[n](a);return}tS(t.model,n).then(function(t){return e.source.postMessage({property:n,postmate:"reply",type:ty,uid:d,value:t},e.origin)})}})}return e.prototype.emit=function(e,t){this.parent.postMessage({postmate:"emit",type:ty,value:{name:e,data:t}},this.parentOrigin)},e}(),tj=function(){function e(e){var t=e.container,r=void 0===t?void 0!==r?r:document.body:t,n=e.model,d=e.url,a=e.name,i=e.classListArray;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=a||"",this.frame.classList.add.apply(this.frame.classList,void 0===i?[]:i),r.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=n||{},this.sendHandshake(d)}return e.prototype.sendHandshake=function(t){var r,n=this,d=tk(t),a=0;return new e.Promise(function(e,i){n.parent.addEventListener("message",function t(a){return!!tA(a,d)&&("handshake-reply"===a.data.postmate?(clearInterval(r),n.parent.removeEventListener("message",t,!1),n.childOrigin=a.origin,e(new tT(n))):i("Failed handshake"))},!1);var u=function(){a++,n.child.postMessage({postmate:"handshake",type:ty,model:n.model},d),5===a&&clearInterval(r)},o=function(){u(),r=setInterval(u,500)};n.frame.attachEvent?n.frame.attachEvent("onload",o):n.frame.onload=o,n.frame.src=t})},e}();tj.debug=!1,tj.Promise=function(){try{return window?window.Promise:Promise}catch(e){return null}}(),tj.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 tj.Promise(function(t,r){e.child.addEventListener("message",function n(d){if(d.data.postmate){if("handshake"===d.data.postmate){e.child.removeEventListener("message",n,!1),d.source.postMessage({postmate:"handshake-reply",type:ty},d.origin),e.parentOrigin=d.origin;var a=d.data.model;return a&&Object.keys(a).forEach(function(t){e.model[t]=a[t]}),t(new tx(e))}return r("Handshake Reply Failed")}},!1)})},e}();var tM=((p={}).MUJIAN_INIT="mujian:init",p.MUJIAN_AI_CHAT_STOP="mujian:ai:chat:stop",p.MUJIAN_AI_CHAT_COMPLETE="mujian:ai:chat:complete",p.MUJIAN_AI_CHAT_APPLY_REGEX="mujian:ai:chat:applyRegex",p.MUJIAN_AI_CHAT_RENDER_MESSAGE="mujian:ai:chat:renderMessage",p.MUJIAN_AI_TEXT_GENERATE="mujian:ai:text:generate",p.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE="mujian:ai:openai:completions:create",p.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE="mujian:ai:openai:chat:completions:create",p.MUJIAN_AI_OPENAI_RESPONSES_CREATE="mujian:ai:openai:responses:create",p.MUJIAN_AI_CHAT_MESSAGE_GET_ALL="mujian:ai:chat:message:getAll",p.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE="mujian:ai:chat:message:getPage",p.MUJIAN_AI_CHAT_PROJECT_GET_INFO="mujian:ai:chat:project:getInfo",p.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE="mujian:ai:settings:persona:getActive",p.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE="mujian:ai:settings:persona:setActive",p.MUJIAN_AI_SETTINGS_MODEL_GET_ALL="mujian:ai:settings:model:getAll",p.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE="mujian:ai:settings:model:setActive",p.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE="mujian:ai:settings:model:getActive",p.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE="mujian:ai:chat:message:deleteOne",p.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE="mujian:ai:chat:message:editOne",p.MUJIAN_AI_CHAT_MESSAGE_SWIPE="mujian:ai:chat:message:swipe",p.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT="mujian:ai:chat:message:getPrompt",p.MUJIAN_AI_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",p.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",p.MUJIAN_GET_CONFIG="mujian:getConfig",p);let tI=async function(e,t,r,n={}){let d,a,i,u;await this.call(tM.MUJIAN_AI_CHAT_COMPLETE,{content:e},{onData:n.parseContent?(d="",a="",function(e){let r=(a+=e).split("\n");for(let e of(a=r.pop()||"",r))if(e.startsWith("data: "))try{let r=JSON.parse(e.slice(6));if(r.question_id&&(i=r.question_id),r.reply_id&&(u=r.reply_id),r.isFinished)return void t({isFinished:!0,deltaContent:"",fullContent:d,questionId:i,replyId:u});let n=r?.choices?.[0]?.delta?.content;n?.length>0&&(d+=n,t({isFinished:!1,deltaContent:n,fullContent:d,questionId:i,replyId:u}))}catch(e){t({isFinished:!0,error:e,deltaContent:"",fullContent:d,questionId:i,replyId:u});return}}):t,signal:r})},tP=async function(e){return await this.call(tM.MUJIAN_AI_CHAT_APPLY_REGEX,e)},tC=async function(e){return await this.call(tM.MUJIAN_AI_CHAT_RENDER_MESSAGE,e)},tO=async function(e,t){return await this.call(tM.MUJIAN_AI_CHAT_COMPLETE,{isContinue:!0},{onData:e,signal:t})},tN=async function(e,t){return await this.call(tM.MUJIAN_AI_CHAT_COMPLETE,{isRegenerate:!0},{onData:e,signal:t})},tL=async function(){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_GET_ALL)},tR=async function(e){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE,{messageId:e})},tz=async function(e,t){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE,{messageId:e,content:t})},t$=async function(e,t){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_SWIPE,{messageId:e,swipeId:t})},tH=async function(e){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT,{messageId:e})};async function tD(e,t){return await this.call(tM.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE,{fromCursor:e,pageSize:t})}let tU=async function(){return await this.call(tM.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},tB=async function(){return await this.call(tM.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)},tG=async function(e){return await this.call(tM.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE,{personaId:e})},tF=async function(){return await this.call(tM.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE)},tq=async function(e){return await this.call(tM.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE,{modelId:e})},tV=async function(){return await this.call(tM.MUJIAN_AI_SETTINGS_MODEL_GET_ALL)},tJ=async function(e){return await this.call(t0.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:e})},tW={completions:{create:async function(e,t,r,n){return await this.call(t0.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE,{params:e,options:t},{onData:r,signal:n})}}},tY={create:async function(e,t,r,n){return await this.call(t0.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:e,options:t},{onData:r,signal:n})}},tK={create:async function(e,t,r,n){return await this.call(t0.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE,{params:e,options:t},{onData:r,signal:n})}},tX={generate:async function(e,t,r,n){return await this.call(t0.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:e,options:t},{onData:r,signal:n})}};async function tZ(){return await this.call(tM.MUJIAN_GET_CONFIG)}let tQ=async function(){},t3=async function(){};async function t8(e){return await this.call(tM.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,e)}class t0{constructor(){}static EVENT=tM;static getInstance(){return window.$mujian||(window.$mujian=new t0),window.$mujian}parent=null;ready=!1;get isReady(){return this.ready}pendingRequests=new Map;_config;get openapi(){return this._config?.openapi}initPromise=null;async init(){return this.initPromise||(this.initPromise=(async()=>{let e=new tj.Model({reply:({id:e,complete:t,data:r,error:n})=>{let d=this.pendingRequests.get(e);if(d){if(n){d.reject(n),this.pendingRequests.delete(e);return}d.onData?.(r),t&&(d.onComplete?.(),d.resolve(r),this.pendingRequests.delete(e))}}});try{let t=await e;this.ready=!0,this.parent=t,tf.i("mujian sdk client init"),await this.call(tM.MUJIAN_INIT);let r=await this.ai.chat.project.getInfo();if(r.config?.customCss){let e=document.createElement("style");e.setAttribute("type","text/css"),e.setAttribute("id","mujian-custom-css"),e.textContent=r.config.customCss,document.head.appendChild(e)}if(r.config?.customJs){let e=document.createElement("script");e.setAttribute("type","text/javascript"),e.setAttribute("id","mujian-custom-js"),e.textContent=r.config.customJs,document.head.appendChild(e)}let n=await tZ.call(this);n&&(this._config=n)}catch(e){tf.e("init error",e)}})()),this.initPromise}emit(e,t){if(!this.ready)throw Error("Mujian is not initialized");this.parent?.emit(e,t)}nextCallId=0;getCallId(){let e=this.nextCallId;return this.nextCallId+=1,e}async call(e,t,r){if(!this.ready)throw Error("Mujian is not initialized");let n=this.getCallId();return new Promise((d,a)=>{this.pendingRequests.set(n,{resolve:d,reject:a,...r}),this.emit(e,{id:n,data:t}),r?.signal?.addEventListener("abort",()=>{this.emit(tM.MUJIAN_AI_CHAT_STOP,{id:n})})})}ai={chat:{project:{getInfo:tU.bind(this)},settings:{model:{getAll:tV.bind(this),getActive:tF.bind(this),setActive:tq.bind(this)},persona:{getActive:tB.bind(this),setActive:tG.bind(this)}},complete:tI.bind(this),applyRegex:tP.bind(this),renderMessage:tC.bind(this),continue:tO.bind(this),regenerate:tN.bind(this),message:{getAll:tL.bind(this),deleteOne:tR.bind(this),editOne:tz.bind(this),swipe:t$.bind(this),getPrompt:tH.bind(this),getPage:tD.bind(this)}},text:{complete:tJ.bind(this)},openai:{completions:{create:tY.create.bind(this)},chat:{completions:{create:tW.completions.create.bind(this)}},responses:{create:tK.create.bind(this)},images:{generate:tX.generate.bind(this)}}};ui={};utils={clipboard:{writeText:t8.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:tQ.bind(this),loadGame:t3.bind(this)},ranking:()=>{console.log("ranking")}}}let t1=`
134
134
  @keyframes spin {
135
135
  0% { transform: rotate(0deg); }
136
136
  100% { transform: rotate(360deg); }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mujian/js-sdk",
3
- "version": "0.0.6-beta.71",
3
+ "version": "0.0.6-beta.73",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {