@mujian/js-sdk 0.13.0-alpha.8 → 0.13.0-alpha.9

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.
@@ -76,6 +76,10 @@ export declare enum EVENT {
76
76
  * 对消息应用正则表达式并渲染模板
77
77
  */
78
78
  MUJIAN_AI_CHAT_V2_RENDER_MESSAGE = "mujian:ai:chat_v2:renderMessage",
79
+ /**
80
+ * 编辑消息V2
81
+ */
82
+ MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE = "mujian:ai:chat_v2:message:editOne",
79
83
  /**
80
84
  * 获取个人设置
81
85
  */
package/dist/index.d.ts CHANGED
@@ -75,7 +75,7 @@ declare class MujianSdk {
75
75
  message: {
76
76
  getAll: () => Promise<import("./react").Message[]>;
77
77
  deleteOne: (messageId: string) => Promise<unknown>;
78
- editOne: (messageId: string, content: string) => Promise<unknown>;
78
+ editOne: (messageId: string, content: import("./react").MessageV2Content) => Promise<unknown>;
79
79
  swipe: (messageId: string, swipeId: number) => Promise<unknown>;
80
80
  getPage: (fromCursor: import("./types").Cursor | undefined, pageSize: number, options?: {
81
81
  compatMode: boolean;
package/dist/index.js CHANGED
@@ -18,6 +18,7 @@ var events_EVENT = /*#__PURE__*/ function(EVENT) {
18
18
  EVENT["MUJIAN_AI_CHAT_V2_STOP"] = "mujian:ai:chat_v2:stop";
19
19
  EVENT["MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE"] = "mujian:ai:chat_v2:message:getPage";
20
20
  EVENT["MUJIAN_AI_CHAT_V2_RENDER_MESSAGE"] = "mujian:ai:chat_v2:renderMessage";
21
+ EVENT["MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE"] = "mujian:ai:chat_v2:message:editOne";
21
22
  EVENT["MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE"] = "mujian:ai:settings:persona:getActive";
22
23
  EVENT["MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE"] = "mujian:ai:settings:persona:setActive";
23
24
  EVENT["MUJIAN_AI_SETTINGS_MODEL_GET_ALL"] = "mujian:ai:settings:model:getAll";
@@ -86,7 +87,7 @@ const messageDeleteOne = async function(messageId) {
86
87
  });
87
88
  };
88
89
  const messageEditOne = async function(messageId, content) {
89
- return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE, {
90
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE, {
90
91
  messageId,
91
92
  content
92
93
  });
package/dist/lite.js CHANGED
@@ -18,6 +18,7 @@ var events_EVENT = /*#__PURE__*/ function(EVENT) {
18
18
  EVENT["MUJIAN_AI_CHAT_V2_STOP"] = "mujian:ai:chat_v2:stop";
19
19
  EVENT["MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE"] = "mujian:ai:chat_v2:message:getPage";
20
20
  EVENT["MUJIAN_AI_CHAT_V2_RENDER_MESSAGE"] = "mujian:ai:chat_v2:renderMessage";
21
+ EVENT["MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE"] = "mujian:ai:chat_v2:message:editOne";
21
22
  EVENT["MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE"] = "mujian:ai:settings:persona:getActive";
22
23
  EVENT["MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE"] = "mujian:ai:settings:persona:setActive";
23
24
  EVENT["MUJIAN_AI_SETTINGS_MODEL_GET_ALL"] = "mujian:ai:settings:model:getAll";
@@ -1,5 +1,5 @@
1
1
  import type { MujianSdk } from '../../../../index.ts';
2
- import type { Message } from '../../../../react/chat/useChat/message';
2
+ import type { Message, MessageV2Content } from '../../../../react/chat/useChat/message';
3
3
  import { Cursor } from '../../../../types';
4
4
  /**
5
5
  * 获取消息列表
@@ -24,7 +24,7 @@ this: MujianSdk, messageId: string) => Promise<unknown>;
24
24
  */
25
25
  export declare const messageEditOne: (
26
26
  /** @hidden */
27
- this: MujianSdk, messageId: string, content: string) => Promise<unknown>;
27
+ this: MujianSdk, messageId: string, content: MessageV2Content) => Promise<unknown>;
28
28
  /**
29
29
  * 滑动消息
30
30
  * @param messageId 消息ID
@@ -1,5 +1,5 @@
1
1
  import { type Dispatch, type SetStateAction } from 'react';
2
- import type { Message } from './message';
2
+ import type { Message, MessageV2Content } from './message';
3
3
  export type { Message };
4
4
  /**
5
5
  * Use chat
@@ -47,7 +47,7 @@ export type UseChatReturn = {
47
47
  setInput: Dispatch<SetStateAction<string>>;
48
48
  handleSubmit: () => void;
49
49
  setSwipe: (messageId: string, swipeId: number) => Promise<void>;
50
- editMessage: (messageId: string, content: string) => Promise<void>;
50
+ editMessage: (messageId: string, content: MessageV2Content) => Promise<void>;
51
51
  deleteMessage: (messageId: string) => Promise<void>;
52
52
  loadMoreMessage: () => Promise<void>;
53
53
  messagesStatus: 'all-loaded' | 'loading' | 'has-more' | 'error';
package/dist/react.js CHANGED
@@ -521,6 +521,7 @@ var events_EVENT = /*#__PURE__*/ function(EVENT) {
521
521
  EVENT["MUJIAN_AI_CHAT_V2_STOP"] = "mujian:ai:chat_v2:stop";
522
522
  EVENT["MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE"] = "mujian:ai:chat_v2:message:getPage";
523
523
  EVENT["MUJIAN_AI_CHAT_V2_RENDER_MESSAGE"] = "mujian:ai:chat_v2:renderMessage";
524
+ EVENT["MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE"] = "mujian:ai:chat_v2:message:editOne";
524
525
  EVENT["MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE"] = "mujian:ai:settings:persona:getActive";
525
526
  EVENT["MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE"] = "mujian:ai:settings:persona:setActive";
526
527
  EVENT["MUJIAN_AI_SETTINGS_MODEL_GET_ALL"] = "mujian:ai:settings:model:getAll";
@@ -589,7 +590,7 @@ const messageDeleteOne = async function(messageId) {
589
590
  });
590
591
  };
591
592
  const messageEditOne = async function(messageId, content) {
592
- return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE, {
593
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE, {
593
594
  messageId,
594
595
  content
595
596
  });
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:()=>V,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_CHAT_SUGGEST_REPLY="mujian:ai:chat:suggest_reply",t.MUJIAN_AI_CHAT_V2_COMPLETE="mujian:ai:chat_v2:complete",t.MUJIAN_AI_CHAT_V2_RECOVER="mujian:ai:chat_v2:recover",t.MUJIAN_AI_CHAT_V2_STOP="mujian:ai:chat_v2:stop",t.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE="mujian:ai:chat_v2:message:getPage",t.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE="mujian:ai:chat_v2:renderMessage",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_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",t.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",t.MUJIAN_HYBRID_SHOW_CHARACTER_DETAIL="mujian:hybrid:showCharacterDetail",t.MUJIAN_HYBRID_GET_CG_URL="mujian:hybrid:getCgUrl",t.MUJIAN_GET_CONFIG="mujian:getConfig",t);let _=async function(t,e,i){await this.call(u.MUJIAN_AI_CHAT_V2_COMPLETE,{content:t,conversationVersion:e},{onData:i})},m=async function(t){return await this.call(u.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE,t)},A=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_V2_COMPLETE,{isContinue:!0,conversationVersion:t},{onData:e})},p=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_V2_COMPLETE,{isRegenerate:!0,conversationVersion:t},{onData:e})},E=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_V2_STOP,{conversationVersion:t,assisMsgId:e})},I=async function(t,e,i){return await this.call(u.MUJIAN_AI_CHAT_V2_RECOVER,{conversationVersion:t,assisMsgId:e},{onData:i})},g=async function(){return await this.call(u.MUJIAN_AI_CHAT_SUGGEST_REPLY)},f=async function(){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_GET_ALL)},T=async function(t){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE,{messageId:t})},M=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE,{messageId:t,content:e})},y=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_MESSAGE_SWIPE,{messageId:t,swipeId:e})};async function N(t,e,i={compatMode:!0}){let n=await this.call(u.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE,{fromCursor:t,pageSize:e});return i.compatMode?{...n,messages:n.messages.map(t=>({...t,swipes:t.swipes.map(t=>"string"==typeof t.content?t.content:t.content.map(t=>{switch(t.type){case"narrative":return t.content;case"character":return`${t.name}: ${t.content}`}}).filter(Boolean).join("\n\n")),content:t.swipes[t.activeSwipeId].content,role:t.isUser?"user":"assistant",sendAt:new Date(1e3*parseInt(t.id.substring(0,8),16))}))}:n}let C=async function(){return await this.call(u.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},w=async function(){return await this.call(u.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)};async function b(){return await this.call(u.MUJIAN_GET_CONFIG)}let S=async function(){},v=async function(){},j=async function(t){return await this.call(u.MUJIAN_HYBRID_SHOW_CHARACTER_DETAIL,{characterId:t})},O=async function(t){return await this.call(u.MUJIAN_HYBRID_GET_CG_URL,{cgId:t})};async function U(t){return await this.call(u.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,t)}let P={i(...t){console.log("[MujianSDK] ",...t)},e(...t){console.error("[MujianSDK] ",...t)}};class J{constructor(){}static EVENT=u;static getInstance(){return window.$mujian||(window.$mujian=new J),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,P.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 b.call(this);n&&(this._config=n)}catch(t){P.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:C.bind(this)},settings:{persona:{getActive:w.bind(this)}},complete:_.bind(this),renderMessage:m.bind(this),continue:A.bind(this),regenerate:p.bind(this),stop:E.bind(this),recover:I.bind(this),message:{getAll:f.bind(this),deleteOne:T.bind(this),editOne:M.bind(this),swipe:y.bind(this),getPage:N.bind(this)},suggestReply:g.bind(this)},text:{complete:R.bind(this)},openai:{completions:{create:H.create.bind(this)},chat:{completions:{create:G.completions.create.bind(this)}},responses:{create:L.create.bind(this)},images:{generate:D.generate.bind(this)}}};ui={};utils={clipboard:{writeText:U.bind(this)}};hybrid={showCharacterDetail:j.bind(this),getCgUrl:O.bind(this)};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:S.bind(this),loadGame:v.bind(this)},ranking:()=>{console.log("ranking")}}}let R=async function(t){return await this.call(J.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:t})},G={completions:{create:async function(t,e,i,n){return await this.call(J.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(J.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:t,options:e},{onData:i,signal:n})}},L={create:async function(t,e,i,n){return await this.call(J.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(J.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:t,options:e},{onData:i,signal:n})}};class V{constructor(){}static EVENT=u;static getInstance(){return window.$mujian||(window.$mujian=new V),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,P.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 b.call(this);n&&(this._config=n)}catch(t){P.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:C.bind(this)},settings:{persona:{getActive:w.bind(this)}},complete:_.bind(this),renderMessage:m.bind(this),continue:A.bind(this),regenerate:p.bind(this),stop:E.bind(this),recover:I.bind(this),message:{getAll:f.bind(this),deleteOne:T.bind(this),editOne:M.bind(this),swipe:y.bind(this),getPage:N.bind(this)},suggestReply:g.bind(this)},text:{complete:R.bind(this)},openai:{completions:{create:H.create.bind(this)},chat:{completions:{create:G.completions.create.bind(this)}},responses:{create:L.create.bind(this)},images:{generate:D.generate.bind(this)}}};ui={};utils={clipboard:{writeText:U.bind(this)}};hybrid={showCharacterDetail:j.bind(this),getCgUrl:O.bind(this)};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:S.bind(this),loadGame:v.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:()=>V,EVENT:()=>_});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 _=((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_CHAT_SUGGEST_REPLY="mujian:ai:chat:suggest_reply",t.MUJIAN_AI_CHAT_V2_COMPLETE="mujian:ai:chat_v2:complete",t.MUJIAN_AI_CHAT_V2_RECOVER="mujian:ai:chat_v2:recover",t.MUJIAN_AI_CHAT_V2_STOP="mujian:ai:chat_v2:stop",t.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE="mujian:ai:chat_v2:message:getPage",t.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE="mujian:ai:chat_v2:renderMessage",t.MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE="mujian:ai:chat_v2:message:editOne",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_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",t.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",t.MUJIAN_HYBRID_SHOW_CHARACTER_DETAIL="mujian:hybrid:showCharacterDetail",t.MUJIAN_HYBRID_GET_CG_URL="mujian:hybrid:getCgUrl",t.MUJIAN_GET_CONFIG="mujian:getConfig",t);let u=async function(t,e,i){await this.call(_.MUJIAN_AI_CHAT_V2_COMPLETE,{content:t,conversationVersion:e},{onData:i})},m=async function(t){return await this.call(_.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE,t)},A=async function(t,e){return await this.call(_.MUJIAN_AI_CHAT_V2_COMPLETE,{isContinue:!0,conversationVersion:t},{onData:e})},p=async function(t,e){return await this.call(_.MUJIAN_AI_CHAT_V2_COMPLETE,{isRegenerate:!0,conversationVersion:t},{onData:e})},E=async function(t,e){return await this.call(_.MUJIAN_AI_CHAT_V2_STOP,{conversationVersion:t,assisMsgId:e})},I=async function(t,e,i){return await this.call(_.MUJIAN_AI_CHAT_V2_RECOVER,{conversationVersion:t,assisMsgId:e},{onData:i})},g=async function(){return await this.call(_.MUJIAN_AI_CHAT_SUGGEST_REPLY)},f=async function(){return await this.call(_.MUJIAN_AI_CHAT_MESSAGE_GET_ALL)},T=async function(t){return await this.call(_.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE,{messageId:t})},M=async function(t,e){return await this.call(_.MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE,{messageId:t,content:e})},y=async function(t,e){return await this.call(_.MUJIAN_AI_CHAT_MESSAGE_SWIPE,{messageId:t,swipeId:e})};async function N(t,e,i={compatMode:!0}){let n=await this.call(_.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE,{fromCursor:t,pageSize:e});return i.compatMode?{...n,messages:n.messages.map(t=>({...t,swipes:t.swipes.map(t=>"string"==typeof t.content?t.content:t.content.map(t=>{switch(t.type){case"narrative":return t.content;case"character":return`${t.name}: ${t.content}`}}).filter(Boolean).join("\n\n")),content:t.swipes[t.activeSwipeId].content,role:t.isUser?"user":"assistant",sendAt:new Date(1e3*parseInt(t.id.substring(0,8),16))}))}:n}let C=async function(){return await this.call(_.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},w=async function(){return await this.call(_.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)};async function S(){return await this.call(_.MUJIAN_GET_CONFIG)}let b=async function(){},v=async function(){},j=async function(t){return await this.call(_.MUJIAN_HYBRID_SHOW_CHARACTER_DETAIL,{characterId:t})},O=async function(t){return await this.call(_.MUJIAN_HYBRID_GET_CG_URL,{cgId:t})};async function U(t){return await this.call(_.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,t)}let J={i(...t){console.log("[MujianSDK] ",...t)},e(...t){console.error("[MujianSDK] ",...t)}};class P{constructor(){}static EVENT=_;static getInstance(){return window.$mujian||(window.$mujian=new P),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,J.i("mujian sdk client init"),await this.call(_.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 S.call(this);n&&(this._config=n)}catch(t){J.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(_.MUJIAN_AI_CHAT_STOP,{id:n})})})}ai={chat:{project:{getInfo:C.bind(this)},settings:{persona:{getActive:w.bind(this)}},complete:u.bind(this),renderMessage:m.bind(this),continue:A.bind(this),regenerate:p.bind(this),stop:E.bind(this),recover:I.bind(this),message:{getAll:f.bind(this),deleteOne:T.bind(this),editOne:M.bind(this),swipe:y.bind(this),getPage:N.bind(this)},suggestReply:g.bind(this)},text:{complete:R.bind(this)},openai:{completions:{create:H.create.bind(this)},chat:{completions:{create:G.completions.create.bind(this)}},responses:{create:L.create.bind(this)},images:{generate:D.generate.bind(this)}}};ui={};utils={clipboard:{writeText:U.bind(this)}};hybrid={showCharacterDetail:j.bind(this),getCgUrl:O.bind(this)};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:b.bind(this),loadGame:v.bind(this)},ranking:()=>{console.log("ranking")}}}let R=async function(t){return await this.call(P.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:t})},G={completions:{create:async function(t,e,i,n){return await this.call(P.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(P.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:t,options:e},{onData:i,signal:n})}},L={create:async function(t,e,i,n){return await this.call(P.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(P.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:t,options:e},{onData:i,signal:n})}};class V{constructor(){}static EVENT=_;static getInstance(){return window.$mujian||(window.$mujian=new V),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,J.i("mujian sdk client init"),await this.call(_.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 S.call(this);n&&(this._config=n)}catch(t){J.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(_.MUJIAN_AI_CHAT_STOP,{id:n})})})}ai={chat:{project:{getInfo:C.bind(this)},settings:{persona:{getActive:w.bind(this)}},complete:u.bind(this),renderMessage:m.bind(this),continue:A.bind(this),regenerate:p.bind(this),stop:E.bind(this),recover:I.bind(this),message:{getAll:f.bind(this),deleteOne:T.bind(this),editOne:M.bind(this),swipe:y.bind(this),getPage:N.bind(this)},suggestReply:g.bind(this)},text:{complete:R.bind(this)},openai:{completions:{create:H.create.bind(this)},chat:{completions:{create:G.completions.create.bind(this)}},responses:{create:L.create.bind(this)},images:{generate:D.generate.bind(this)}}};ui={};utils={clipboard:{writeText:U.bind(this)}};hybrid={showCharacterDetail:j.bind(this),getCgUrl:O.bind(this)};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:b.bind(this),loadGame:v.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:()=>m,default:()=>p});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 _.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 _.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}(),_=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)},_=function(){c(),i=setInterval(c,500)};a.frame.attachEvent?a.frame.attachEvent("onload",_):a.frame.onload=_,a.frame.src=t})},e}();_.debug=!1,_.Promise=function(){try{return window?window.Promise:Promise}catch(e){return null}}(),_.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 _.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 m=((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_CHAT_SUGGEST_REPLY="mujian:ai:chat:suggest_reply",e.MUJIAN_AI_CHAT_V2_COMPLETE="mujian:ai:chat_v2:complete",e.MUJIAN_AI_CHAT_V2_RECOVER="mujian:ai:chat_v2:recover",e.MUJIAN_AI_CHAT_V2_STOP="mujian:ai:chat_v2:stop",e.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE="mujian:ai:chat_v2:message:getPage",e.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE="mujian:ai:chat_v2:renderMessage",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_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",e.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",e.MUJIAN_HYBRID_SHOW_CHARACTER_DETAIL="mujian:hybrid:showCharacterDetail",e.MUJIAN_HYBRID_GET_CG_URL="mujian:hybrid:getCgUrl",e.MUJIAN_GET_CONFIG="mujian:getConfig",e);let u={i(...e){console.log("[MujianSDK] ",...e)},e(...e){console.error("[MujianSDK] ",...e)}};class h{constructor(){}static EVENT=m;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 _.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,u.i("mujian sdk lite client init"),await this.call(m.MUJIAN_INIT),this._config=await this.call(m.MUJIAN_GET_CONFIG)}catch(e){throw u.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 p=h;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 a in i)t.o(i,a)&&!t.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:i[a]})},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:()=>m,default:()=>p});var a="application/x-postmate-v1+json",n=0,r=function(e){var t=document.createElement("a");t.href=e;var i=t.protocol.length>4?t.protocol:window.location.protocol,a=t.host.length?"80"===t.port||"443"===t.port?t.hostname:t.host:window.location.host;return t.origin||i+"//"+a},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===a&&!!s[e.data.postmate]},d=function(e,t){var i="function"==typeof e[t]?e[t]():e[t];return c.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||{},a=i.data,n=i.name;"emit"===e.data.postmate&&n in t.events&&t.events[n].call(t,a)},this.parent.addEventListener("message",this.listener,!1)}var t=e.prototype;return t.get=function(e){var t=this;return new c.Promise(function(i){var r=++n;t.parent.addEventListener("message",function e(a){a.data.uid===r&&"reply"===a.data.postmate&&(t.parent.removeEventListener("message",e,!1),i(a.data.value))},!1),t.child.postMessage({postmate:"request",type:a,property:e,uid:r},t.childOrigin)})},t.call=function(e,t){this.child.postMessage({postmate:"call",type:a,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}(),_=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,n=i.property,r=i.uid,s=i.data;if("call"===e.data.postmate){n in t.model&&"function"==typeof t.model[n]&&t.model[n](s);return}d(t.model,n).then(function(t){return e.source.postMessage({property:n,postmate:"reply",type:a,uid:r,value:t},e.origin)})}})}return e.prototype.emit=function(e,t){this.parent.postMessage({postmate:"emit",type:a,value:{name:e,data:t}},this.parentOrigin)},e}(),c=function(){function e(e){var t=e.container,i=void 0===t?void 0!==i?i:document.body:t,a=e.model,n=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=a||{},this.sendHandshake(n)}return e.prototype.sendHandshake=function(t){var i,n=this,s=r(t),d=0;return new e.Promise(function(e,r){n.parent.addEventListener("message",function t(a){return!!o(a,s)&&("handshake-reply"===a.data.postmate?(clearInterval(i),n.parent.removeEventListener("message",t,!1),n.childOrigin=a.origin,e(new l(n))):r("Failed handshake"))},!1);var _=function(){d++,n.child.postMessage({postmate:"handshake",type:a,model:n.model},s),5===d&&clearInterval(i)},c=function(){_(),i=setInterval(_,500)};n.frame.attachEvent?n.frame.attachEvent("onload",c):n.frame.onload=c,n.frame.src=t})},e}();c.debug=!1,c.Promise=function(){try{return window?window.Promise:Promise}catch(e){return null}}(),c.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 c.Promise(function(t,i){e.child.addEventListener("message",function n(r){if(r.data.postmate){if("handshake"===r.data.postmate){e.child.removeEventListener("message",n,!1),r.source.postMessage({postmate:"handshake-reply",type:a},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 _(e))}return i("Handshake Reply Failed")}},!1)})},e}();var m=((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_CHAT_SUGGEST_REPLY="mujian:ai:chat:suggest_reply",e.MUJIAN_AI_CHAT_V2_COMPLETE="mujian:ai:chat_v2:complete",e.MUJIAN_AI_CHAT_V2_RECOVER="mujian:ai:chat_v2:recover",e.MUJIAN_AI_CHAT_V2_STOP="mujian:ai:chat_v2:stop",e.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE="mujian:ai:chat_v2:message:getPage",e.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE="mujian:ai:chat_v2:renderMessage",e.MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE="mujian:ai:chat_v2:message:editOne",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_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",e.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",e.MUJIAN_HYBRID_SHOW_CHARACTER_DETAIL="mujian:hybrid:showCharacterDetail",e.MUJIAN_HYBRID_GET_CG_URL="mujian:hybrid:getCgUrl",e.MUJIAN_GET_CONFIG="mujian:getConfig",e);let u={i(...e){console.log("[MujianSDK] ",...e)},e(...e){console.error("[MujianSDK] ",...e)}};class h{constructor(){}static EVENT=m;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 c.Model({reply:({id:e,complete:t,data:i,error:a})=>{let n=this.pendingRequests.get(e);if(n){if(a){n.reject(a),this.pendingRequests.delete(e);return}n.onData?.(i),t&&(n.onComplete?.(),n.resolve(i),this.pendingRequests.delete(e))}}});try{let t=await e;this.parent=t,this.ready=!0,u.i("mujian sdk lite client init"),await this.call(m.MUJIAN_INIT),this._config=await this.call(m.MUJIAN_GET_CONFIG)}catch(e){throw u.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 a=this.getCallId();return new Promise((n,r)=>{this.pendingRequests.set(a,{resolve:n,reject:r,...i}),this.emit(e,{id:a,data:t})})}}h.getInstance().init();let p=h;return i})());
package/dist/umd/react.js CHANGED
@@ -161,7 +161,7 @@ ${i}
161
161
  <iframe id="${l}" class="w-full" sandbox="${t?"allow-scripts":"allow-scripts allow-same-origin"}" loading="lazy"
162
162
  referrerpolicy="no-referrer" allowTransparency="true"
163
163
  style="color-scheme: none;background-color: transparent;width:100%;;border:none;" srcdoc="`+f+`"></iframe>
164
- </div>`}),i.current&&(i.current.innerHTML=a)},[e]),(0,N.useEffect)(()=>{window.addEventListener("message",function(e){if(e.data?.type==="MJ_ADJUST_IFRAME_HEIGHT"&&e.data?.iframe_id){let t=i.current?.querySelector("#"+e.data.iframe_id);t&&(t.style.height=`${e.data.height}px`)}}),window.addEventListener("resize",()=>{a.forEach(e=>{let t=i.current?.querySelector("#MJ-iframe-"+e);t&&t.contentWindow?.postMessage({type:"MJ_UPDATE_VIEWPORT_HEIGHT"},"*")})})},[]),(0,N.useEffect)(()=>(a.forEach(e=>{let t=i.current?.querySelector("#MJ-iframe-"+e);if(!t)return;let n=()=>{let n=t.parentElement?.querySelector("#MJ-spin-"+e);n&&n.remove()},r=setTimeout(n,5e3);t.addEventListener("load",()=>{clearTimeout(r),n()})}),()=>{a.forEach(e=>{let t=i.current?.querySelector("#MJ-iframe-"+e);if(!t)return;let n=t.parentElement?.querySelector("#MJ-spin-"+t.id);n&&n.remove(),t.removeEventListener("load",()=>{sY.i("iframe loaded",t.id)})})}),[a]),(0,uX.jsx)("div",{className:"mes_text",ref:i})},s6=R().memo(s2,(e,t)=>e.content===t.content);s2.displayName="MdRenderer",s6.displayName="MdRenderer";var s4="application/x-postmate-v1+json",s9=0,s5=function(e){var t=document.createElement("a");t.href=e;var n=t.protocol.length>4?t.protocol:window.location.protocol,r=t.host.length?"80"===t.port||"443"===t.port?t.hostname:t.host:window.location.host;return t.origin||n+"//"+r},s7={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},de=function(e,t){return("string"!=typeof t||e.origin===t)&&!!e.data&&("object"!=typeof e.data||"postmate"in e.data)&&e.data.type===s4&&!!s7[e.data.postmate]},dt=function(e,t){var n="function"==typeof e[t]?e[t]():e[t];return di.Promise.resolve(n)},dn=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(!de(e,t.childOrigin))return!1;var n=((e||{}).data||{}).value||{},r=n.data,i=n.name;"emit"===e.data.postmate&&i in t.events&&t.events[i].call(t,r)},this.parent.addEventListener("message",this.listener,!1)}var t=e.prototype;return t.get=function(e){var t=this;return new di.Promise(function(n){var r=++s9;t.parent.addEventListener("message",function e(i){i.data.uid===r&&"reply"===i.data.postmate&&(t.parent.removeEventListener("message",e,!1),n(i.data.value))},!1),t.child.postMessage({postmate:"request",type:s4,property:e,uid:r},t.childOrigin)})},t.call=function(e,t){this.child.postMessage({postmate:"call",type:s4,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}(),dr=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(de(e,t.parentOrigin)){var n=e.data,r=n.property,i=n.uid,a=n.data;if("call"===e.data.postmate){r in t.model&&"function"==typeof t.model[r]&&t.model[r](a);return}dt(t.model,r).then(function(t){return e.source.postMessage({property:r,postmate:"reply",type:s4,uid:i,value:t},e.origin)})}})}return e.prototype.emit=function(e,t){this.parent.postMessage({postmate:"emit",type:s4,value:{name:e,data:t}},this.parentOrigin)},e}(),di=function(){function e(e){var t=e.container,n=void 0===t?void 0!==n?n:document.body:t,r=e.model,i=e.url,a=e.name,o=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===o?[]:o),n.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=r||{},this.sendHandshake(i)}return e.prototype.sendHandshake=function(t){var n,r=this,i=s5(t),a=0;return new e.Promise(function(e,o){r.parent.addEventListener("message",function t(a){return!!de(a,i)&&("handshake-reply"===a.data.postmate?(clearInterval(n),r.parent.removeEventListener("message",t,!1),r.childOrigin=a.origin,e(new dn(r))):o("Failed handshake"))},!1);var u=function(){a++,r.child.postMessage({postmate:"handshake",type:s4,model:r.model},i),5===a&&clearInterval(n)},s=function(){u(),n=setInterval(u,500)};r.frame.attachEvent?r.frame.attachEvent("onload",s):r.frame.onload=s,r.frame.src=t})},e}();di.debug=!1,di.Promise=function(){try{return window?window.Promise:Promise}catch(e){return null}}(),di.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 di.Promise(function(t,n){e.child.addEventListener("message",function r(i){if(i.data.postmate){if("handshake"===i.data.postmate){e.child.removeEventListener("message",r,!1),i.source.postMessage({postmate:"handshake-reply",type:s4},i.origin),e.parentOrigin=i.origin;var a=i.data.model;return a&&Object.keys(a).forEach(function(t){e.model[t]=a[t]}),t(new dr(e))}return n("Handshake Reply Failed")}},!1)})},e}();var da=((g={}).MUJIAN_INIT="mujian:init",g.MUJIAN_AI_CHAT_STOP="mujian:ai:chat:stop",g.MUJIAN_AI_CHAT_COMPLETE="mujian:ai:chat:complete",g.MUJIAN_AI_CHAT_APPLY_REGEX="mujian:ai:chat:applyRegex",g.MUJIAN_AI_CHAT_RENDER_MESSAGE="mujian:ai:chat:renderMessage",g.MUJIAN_AI_TEXT_GENERATE="mujian:ai:text:generate",g.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE="mujian:ai:openai:completions:create",g.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE="mujian:ai:openai:chat:completions:create",g.MUJIAN_AI_OPENAI_RESPONSES_CREATE="mujian:ai:openai:responses:create",g.MUJIAN_AI_CHAT_MESSAGE_GET_ALL="mujian:ai:chat:message:getAll",g.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE="mujian:ai:chat:message:getPage",g.MUJIAN_AI_CHAT_PROJECT_GET_INFO="mujian:ai:chat:project:getInfo",g.MUJIAN_AI_CHAT_SUGGEST_REPLY="mujian:ai:chat:suggest_reply",g.MUJIAN_AI_CHAT_V2_COMPLETE="mujian:ai:chat_v2:complete",g.MUJIAN_AI_CHAT_V2_RECOVER="mujian:ai:chat_v2:recover",g.MUJIAN_AI_CHAT_V2_STOP="mujian:ai:chat_v2:stop",g.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE="mujian:ai:chat_v2:message:getPage",g.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE="mujian:ai:chat_v2:renderMessage",g.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE="mujian:ai:settings:persona:getActive",g.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE="mujian:ai:settings:persona:setActive",g.MUJIAN_AI_SETTINGS_MODEL_GET_ALL="mujian:ai:settings:model:getAll",g.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE="mujian:ai:settings:model:setActive",g.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE="mujian:ai:settings:model:getActive",g.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE="mujian:ai:chat:message:deleteOne",g.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE="mujian:ai:chat:message:editOne",g.MUJIAN_AI_CHAT_MESSAGE_SWIPE="mujian:ai:chat:message:swipe",g.MUJIAN_AI_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",g.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",g.MUJIAN_HYBRID_SHOW_CHARACTER_DETAIL="mujian:hybrid:showCharacterDetail",g.MUJIAN_HYBRID_GET_CG_URL="mujian:hybrid:getCgUrl",g.MUJIAN_GET_CONFIG="mujian:getConfig",g);let du=async function(e,t,n){await this.call(da.MUJIAN_AI_CHAT_V2_COMPLETE,{content:e,conversationVersion:t},{onData:n})},ds=async function(e){return await this.call(da.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE,e)},dd=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_V2_COMPLETE,{isContinue:!0,conversationVersion:e},{onData:t})},dc=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_V2_COMPLETE,{isRegenerate:!0,conversationVersion:e},{onData:t})},dl=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_V2_STOP,{conversationVersion:e,assisMsgId:t})},dp=async function(e,t,n){return await this.call(da.MUJIAN_AI_CHAT_V2_RECOVER,{conversationVersion:e,assisMsgId:t},{onData:n})},df=async function(){return await this.call(da.MUJIAN_AI_CHAT_SUGGEST_REPLY)},dh=async function(){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_GET_ALL)},dm=async function(e){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE,{messageId:e})},d_=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE,{messageId:e,content:t})},dg=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_SWIPE,{messageId:e,swipeId:t})};async function db(e,t,n={compatMode:!0}){let r=await this.call(da.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE,{fromCursor:e,pageSize:t});return n.compatMode?{...r,messages:r.messages.map(e=>({...e,swipes:e.swipes.map(e=>"string"==typeof e.content?e.content:e.content.map(e=>{switch(e.type){case"narrative":return e.content;case"character":return`${e.name}: ${e.content}`}}).filter(Boolean).join("\n\n")),content:e.swipes[e.activeSwipeId].content,role:e.isUser?"user":"assistant",sendAt:new Date(1e3*parseInt(e.id.substring(0,8),16))}))}:r}let dy=async function(){return await this.call(da.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},dv=async function(){return await this.call(da.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)},dw=async function(e){return await this.call(dP.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:e})},dk={completions:{create:async function(e,t,n,r){return await this.call(dP.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE,{params:e,options:t},{onData:n,signal:r})}}},dz={create:async function(e,t,n,r){return await this.call(dP.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:e,options:t},{onData:n,signal:r})}},dS={create:async function(e,t,n,r){return await this.call(dP.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE,{params:e,options:t},{onData:n,signal:r})}},dE={generate:async function(e,t,n,r){return await this.call(dP.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:e,options:t},{onData:n,signal:r})}};async function dA(){return await this.call(da.MUJIAN_GET_CONFIG)}let dx=async function(){},dT=async function(){},dj=async function(e){return await this.call(da.MUJIAN_HYBRID_SHOW_CHARACTER_DETAIL,{characterId:e})},dI=async function(e){return await this.call(da.MUJIAN_HYBRID_GET_CG_URL,{cgId:e})};async function dO(e){return await this.call(da.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,e)}class dP{constructor(){}static EVENT=da;static getInstance(){return window.$mujian||(window.$mujian=new dP),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 di.Model({reply:({id:e,complete:t,data:n,error:r})=>{let i=this.pendingRequests.get(e);if(i){if(r){i.reject(r),this.pendingRequests.delete(e);return}i.onData?.(n),t&&(i.onComplete?.(),i.resolve(n),this.pendingRequests.delete(e))}}});try{let t=await e;this.ready=!0,this.parent=t,sY.i("mujian sdk client init"),await this.call(da.MUJIAN_INIT);let n=await this.ai.chat.project.getInfo();if(n.config?.customCss){let e=document.createElement("style");e.setAttribute("type","text/css"),e.setAttribute("id","mujian-custom-css"),e.textContent=n.config.customCss,document.head.appendChild(e)}if(n.config?.customJs){let e=document.createElement("script");e.setAttribute("type","text/javascript"),e.setAttribute("id","mujian-custom-js"),e.textContent=n.config.customJs,document.head.appendChild(e)}let r=await dA.call(this);r&&(this._config=r)}catch(e){sY.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,n){if(!this.ready)throw Error("Mujian is not initialized");let r=this.getCallId();return new Promise((i,a)=>{this.pendingRequests.set(r,{resolve:i,reject:a,...n}),this.emit(e,{id:r,data:t}),n?.signal?.addEventListener("abort",()=>{this.emit(da.MUJIAN_AI_CHAT_STOP,{id:r})})})}ai={chat:{project:{getInfo:dy.bind(this)},settings:{persona:{getActive:dv.bind(this)}},complete:du.bind(this),renderMessage:ds.bind(this),continue:dd.bind(this),regenerate:dc.bind(this),stop:dl.bind(this),recover:dp.bind(this),message:{getAll:dh.bind(this),deleteOne:dm.bind(this),editOne:d_.bind(this),swipe:dg.bind(this),getPage:db.bind(this)},suggestReply:df.bind(this)},text:{complete:dw.bind(this)},openai:{completions:{create:dz.create.bind(this)},chat:{completions:{create:dk.completions.create.bind(this)}},responses:{create:dS.create.bind(this)},images:{generate:dE.generate.bind(this)}}};ui={};utils={clipboard:{writeText:dO.bind(this)}};hybrid={showCharacterDetail:dj.bind(this),getCgUrl:dI.bind(this)};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:dx.bind(this),loadGame:dT.bind(this)},ranking:()=>{console.log("ranking")}}}let d$=`
164
+ </div>`}),i.current&&(i.current.innerHTML=a)},[e]),(0,N.useEffect)(()=>{window.addEventListener("message",function(e){if(e.data?.type==="MJ_ADJUST_IFRAME_HEIGHT"&&e.data?.iframe_id){let t=i.current?.querySelector("#"+e.data.iframe_id);t&&(t.style.height=`${e.data.height}px`)}}),window.addEventListener("resize",()=>{a.forEach(e=>{let t=i.current?.querySelector("#MJ-iframe-"+e);t&&t.contentWindow?.postMessage({type:"MJ_UPDATE_VIEWPORT_HEIGHT"},"*")})})},[]),(0,N.useEffect)(()=>(a.forEach(e=>{let t=i.current?.querySelector("#MJ-iframe-"+e);if(!t)return;let n=()=>{let n=t.parentElement?.querySelector("#MJ-spin-"+e);n&&n.remove()},r=setTimeout(n,5e3);t.addEventListener("load",()=>{clearTimeout(r),n()})}),()=>{a.forEach(e=>{let t=i.current?.querySelector("#MJ-iframe-"+e);if(!t)return;let n=t.parentElement?.querySelector("#MJ-spin-"+t.id);n&&n.remove(),t.removeEventListener("load",()=>{sY.i("iframe loaded",t.id)})})}),[a]),(0,uX.jsx)("div",{className:"mes_text",ref:i})},s6=R().memo(s2,(e,t)=>e.content===t.content);s2.displayName="MdRenderer",s6.displayName="MdRenderer";var s4="application/x-postmate-v1+json",s9=0,s5=function(e){var t=document.createElement("a");t.href=e;var n=t.protocol.length>4?t.protocol:window.location.protocol,r=t.host.length?"80"===t.port||"443"===t.port?t.hostname:t.host:window.location.host;return t.origin||n+"//"+r},s7={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},de=function(e,t){return("string"!=typeof t||e.origin===t)&&!!e.data&&("object"!=typeof e.data||"postmate"in e.data)&&e.data.type===s4&&!!s7[e.data.postmate]},dt=function(e,t){var n="function"==typeof e[t]?e[t]():e[t];return di.Promise.resolve(n)},dn=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(!de(e,t.childOrigin))return!1;var n=((e||{}).data||{}).value||{},r=n.data,i=n.name;"emit"===e.data.postmate&&i in t.events&&t.events[i].call(t,r)},this.parent.addEventListener("message",this.listener,!1)}var t=e.prototype;return t.get=function(e){var t=this;return new di.Promise(function(n){var r=++s9;t.parent.addEventListener("message",function e(i){i.data.uid===r&&"reply"===i.data.postmate&&(t.parent.removeEventListener("message",e,!1),n(i.data.value))},!1),t.child.postMessage({postmate:"request",type:s4,property:e,uid:r},t.childOrigin)})},t.call=function(e,t){this.child.postMessage({postmate:"call",type:s4,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}(),dr=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(de(e,t.parentOrigin)){var n=e.data,r=n.property,i=n.uid,a=n.data;if("call"===e.data.postmate){r in t.model&&"function"==typeof t.model[r]&&t.model[r](a);return}dt(t.model,r).then(function(t){return e.source.postMessage({property:r,postmate:"reply",type:s4,uid:i,value:t},e.origin)})}})}return e.prototype.emit=function(e,t){this.parent.postMessage({postmate:"emit",type:s4,value:{name:e,data:t}},this.parentOrigin)},e}(),di=function(){function e(e){var t=e.container,n=void 0===t?void 0!==n?n:document.body:t,r=e.model,i=e.url,a=e.name,o=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===o?[]:o),n.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=r||{},this.sendHandshake(i)}return e.prototype.sendHandshake=function(t){var n,r=this,i=s5(t),a=0;return new e.Promise(function(e,o){r.parent.addEventListener("message",function t(a){return!!de(a,i)&&("handshake-reply"===a.data.postmate?(clearInterval(n),r.parent.removeEventListener("message",t,!1),r.childOrigin=a.origin,e(new dn(r))):o("Failed handshake"))},!1);var u=function(){a++,r.child.postMessage({postmate:"handshake",type:s4,model:r.model},i),5===a&&clearInterval(n)},s=function(){u(),n=setInterval(u,500)};r.frame.attachEvent?r.frame.attachEvent("onload",s):r.frame.onload=s,r.frame.src=t})},e}();di.debug=!1,di.Promise=function(){try{return window?window.Promise:Promise}catch(e){return null}}(),di.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 di.Promise(function(t,n){e.child.addEventListener("message",function r(i){if(i.data.postmate){if("handshake"===i.data.postmate){e.child.removeEventListener("message",r,!1),i.source.postMessage({postmate:"handshake-reply",type:s4},i.origin),e.parentOrigin=i.origin;var a=i.data.model;return a&&Object.keys(a).forEach(function(t){e.model[t]=a[t]}),t(new dr(e))}return n("Handshake Reply Failed")}},!1)})},e}();var da=((g={}).MUJIAN_INIT="mujian:init",g.MUJIAN_AI_CHAT_STOP="mujian:ai:chat:stop",g.MUJIAN_AI_CHAT_COMPLETE="mujian:ai:chat:complete",g.MUJIAN_AI_CHAT_APPLY_REGEX="mujian:ai:chat:applyRegex",g.MUJIAN_AI_CHAT_RENDER_MESSAGE="mujian:ai:chat:renderMessage",g.MUJIAN_AI_TEXT_GENERATE="mujian:ai:text:generate",g.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE="mujian:ai:openai:completions:create",g.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE="mujian:ai:openai:chat:completions:create",g.MUJIAN_AI_OPENAI_RESPONSES_CREATE="mujian:ai:openai:responses:create",g.MUJIAN_AI_CHAT_MESSAGE_GET_ALL="mujian:ai:chat:message:getAll",g.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE="mujian:ai:chat:message:getPage",g.MUJIAN_AI_CHAT_PROJECT_GET_INFO="mujian:ai:chat:project:getInfo",g.MUJIAN_AI_CHAT_SUGGEST_REPLY="mujian:ai:chat:suggest_reply",g.MUJIAN_AI_CHAT_V2_COMPLETE="mujian:ai:chat_v2:complete",g.MUJIAN_AI_CHAT_V2_RECOVER="mujian:ai:chat_v2:recover",g.MUJIAN_AI_CHAT_V2_STOP="mujian:ai:chat_v2:stop",g.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE="mujian:ai:chat_v2:message:getPage",g.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE="mujian:ai:chat_v2:renderMessage",g.MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE="mujian:ai:chat_v2:message:editOne",g.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE="mujian:ai:settings:persona:getActive",g.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE="mujian:ai:settings:persona:setActive",g.MUJIAN_AI_SETTINGS_MODEL_GET_ALL="mujian:ai:settings:model:getAll",g.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE="mujian:ai:settings:model:setActive",g.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE="mujian:ai:settings:model:getActive",g.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE="mujian:ai:chat:message:deleteOne",g.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE="mujian:ai:chat:message:editOne",g.MUJIAN_AI_CHAT_MESSAGE_SWIPE="mujian:ai:chat:message:swipe",g.MUJIAN_AI_OPENAI_IMAGES_GENERATE="mujian:ai:openai:images:generate",g.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT="mujian:utils:clipboard:writeText",g.MUJIAN_HYBRID_SHOW_CHARACTER_DETAIL="mujian:hybrid:showCharacterDetail",g.MUJIAN_HYBRID_GET_CG_URL="mujian:hybrid:getCgUrl",g.MUJIAN_GET_CONFIG="mujian:getConfig",g);let du=async function(e,t,n){await this.call(da.MUJIAN_AI_CHAT_V2_COMPLETE,{content:e,conversationVersion:t},{onData:n})},ds=async function(e){return await this.call(da.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE,e)},dd=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_V2_COMPLETE,{isContinue:!0,conversationVersion:e},{onData:t})},dc=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_V2_COMPLETE,{isRegenerate:!0,conversationVersion:e},{onData:t})},dl=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_V2_STOP,{conversationVersion:e,assisMsgId:t})},dp=async function(e,t,n){return await this.call(da.MUJIAN_AI_CHAT_V2_RECOVER,{conversationVersion:e,assisMsgId:t},{onData:n})},df=async function(){return await this.call(da.MUJIAN_AI_CHAT_SUGGEST_REPLY)},dh=async function(){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_GET_ALL)},dm=async function(e){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE,{messageId:e})},d_=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_V2_MESSAGE_EDIT_ONE,{messageId:e,content:t})},dg=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_SWIPE,{messageId:e,swipeId:t})};async function db(e,t,n={compatMode:!0}){let r=await this.call(da.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE,{fromCursor:e,pageSize:t});return n.compatMode?{...r,messages:r.messages.map(e=>({...e,swipes:e.swipes.map(e=>"string"==typeof e.content?e.content:e.content.map(e=>{switch(e.type){case"narrative":return e.content;case"character":return`${e.name}: ${e.content}`}}).filter(Boolean).join("\n\n")),content:e.swipes[e.activeSwipeId].content,role:e.isUser?"user":"assistant",sendAt:new Date(1e3*parseInt(e.id.substring(0,8),16))}))}:r}let dy=async function(){return await this.call(da.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},dv=async function(){return await this.call(da.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)},dw=async function(e){return await this.call(dP.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:e})},dk={completions:{create:async function(e,t,n,r){return await this.call(dP.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE,{params:e,options:t},{onData:n,signal:r})}}},dz={create:async function(e,t,n,r){return await this.call(dP.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:e,options:t},{onData:n,signal:r})}},dS={create:async function(e,t,n,r){return await this.call(dP.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE,{params:e,options:t},{onData:n,signal:r})}},dE={generate:async function(e,t,n,r){return await this.call(dP.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:e,options:t},{onData:n,signal:r})}};async function dA(){return await this.call(da.MUJIAN_GET_CONFIG)}let dx=async function(){},dT=async function(){},dj=async function(e){return await this.call(da.MUJIAN_HYBRID_SHOW_CHARACTER_DETAIL,{characterId:e})},dI=async function(e){return await this.call(da.MUJIAN_HYBRID_GET_CG_URL,{cgId:e})};async function dO(e){return await this.call(da.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,e)}class dP{constructor(){}static EVENT=da;static getInstance(){return window.$mujian||(window.$mujian=new dP),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 di.Model({reply:({id:e,complete:t,data:n,error:r})=>{let i=this.pendingRequests.get(e);if(i){if(r){i.reject(r),this.pendingRequests.delete(e);return}i.onData?.(n),t&&(i.onComplete?.(),i.resolve(n),this.pendingRequests.delete(e))}}});try{let t=await e;this.ready=!0,this.parent=t,sY.i("mujian sdk client init"),await this.call(da.MUJIAN_INIT);let n=await this.ai.chat.project.getInfo();if(n.config?.customCss){let e=document.createElement("style");e.setAttribute("type","text/css"),e.setAttribute("id","mujian-custom-css"),e.textContent=n.config.customCss,document.head.appendChild(e)}if(n.config?.customJs){let e=document.createElement("script");e.setAttribute("type","text/javascript"),e.setAttribute("id","mujian-custom-js"),e.textContent=n.config.customJs,document.head.appendChild(e)}let r=await dA.call(this);r&&(this._config=r)}catch(e){sY.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,n){if(!this.ready)throw Error("Mujian is not initialized");let r=this.getCallId();return new Promise((i,a)=>{this.pendingRequests.set(r,{resolve:i,reject:a,...n}),this.emit(e,{id:r,data:t}),n?.signal?.addEventListener("abort",()=>{this.emit(da.MUJIAN_AI_CHAT_STOP,{id:r})})})}ai={chat:{project:{getInfo:dy.bind(this)},settings:{persona:{getActive:dv.bind(this)}},complete:du.bind(this),renderMessage:ds.bind(this),continue:dd.bind(this),regenerate:dc.bind(this),stop:dl.bind(this),recover:dp.bind(this),message:{getAll:dh.bind(this),deleteOne:dm.bind(this),editOne:d_.bind(this),swipe:dg.bind(this),getPage:db.bind(this)},suggestReply:df.bind(this)},text:{complete:dw.bind(this)},openai:{completions:{create:dz.create.bind(this)},chat:{completions:{create:dk.completions.create.bind(this)}},responses:{create:dS.create.bind(this)},images:{generate:dE.generate.bind(this)}}};ui={};utils={clipboard:{writeText:dO.bind(this)}};hybrid={showCharacterDetail:dj.bind(this),getCgUrl:dI.bind(this)};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:dx.bind(this),loadGame:dT.bind(this)},ranking:()=>{console.log("ranking")}}}let d$=`
165
165
  @keyframes spin {
166
166
  0% { transform: rotate(0deg); }
167
167
  100% { transform: rotate(360deg); }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mujian/js-sdk",
3
- "version": "0.13.0-alpha.8",
3
+ "version": "0.13.0-alpha.9",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {