@mujian/js-sdk 0.1.0-beta.6 → 0.1.0-beta.8
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 +9 -1
- package/dist/index.js +11 -1
- package/dist/modules/ai/chat/message/index.d.ts +9 -2
- package/dist/react.js +12 -2
- package/dist/umd/index.js +1 -1
- package/dist/umd/react.js +2 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -77,8 +77,16 @@ declare class MujianSdk {
|
|
|
77
77
|
editOne: (messageId: string, content: string) => Promise<unknown>;
|
|
78
78
|
swipe: (messageId: string, swipeId: number) => Promise<unknown>;
|
|
79
79
|
getPage: (fromCursor: import("./types").Cursor | undefined, pageSize: number) => Promise<{
|
|
80
|
+
messages: {
|
|
81
|
+
swipes: (string & import("./react/chat/useChat/message").MessageV2Swipe)[];
|
|
82
|
+
id: string;
|
|
83
|
+
isUser: boolean;
|
|
84
|
+
activeSwipeId: number;
|
|
85
|
+
isGenerating?: boolean;
|
|
86
|
+
finishReason?: string;
|
|
87
|
+
error?: import("./react/chat/useChat/message").ChatError;
|
|
88
|
+
}[];
|
|
80
89
|
nextCursor?: import("./types").Cursor;
|
|
81
|
-
messages: Array<import("./react").Message>;
|
|
82
90
|
total?: number;
|
|
83
91
|
conversationVersion: number;
|
|
84
92
|
}>;
|
package/dist/index.js
CHANGED
|
@@ -89,10 +89,20 @@ const messageSwipe = async function(messageId, swipeId) {
|
|
|
89
89
|
});
|
|
90
90
|
};
|
|
91
91
|
async function getPage(fromCursor, pageSize) {
|
|
92
|
-
|
|
92
|
+
const resp = await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE, {
|
|
93
93
|
fromCursor,
|
|
94
94
|
pageSize
|
|
95
95
|
});
|
|
96
|
+
return {
|
|
97
|
+
...resp,
|
|
98
|
+
messages: resp.messages.map((m)=>({
|
|
99
|
+
...m,
|
|
100
|
+
swipes: m.swipes.map((s)=>{
|
|
101
|
+
const ret = Object.assign(s.content, s);
|
|
102
|
+
return ret;
|
|
103
|
+
})
|
|
104
|
+
}))
|
|
105
|
+
};
|
|
96
106
|
}
|
|
97
107
|
const getInfo = async function() {
|
|
98
108
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_PROJECT_GET_INFO);
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { MujianSdk } from '../../../../index.ts';
|
|
2
|
-
import type { Message } from '../../../../react/chat/useChat/message';
|
|
3
2
|
import { Cursor } from '../../../../types';
|
|
4
3
|
/**
|
|
5
4
|
* 删除消息
|
|
@@ -30,9 +29,17 @@ this: MujianSdk, messageId: string, swipeId: number) => Promise<unknown>;
|
|
|
30
29
|
export declare function getPage(
|
|
31
30
|
/** @hidden */
|
|
32
31
|
this: MujianSdk, fromCursor: Cursor | undefined, pageSize: number): Promise<{
|
|
32
|
+
messages: {
|
|
33
|
+
swipes: (string & import("../../../../react/chat/useChat/message").MessageV2Swipe)[];
|
|
34
|
+
id: string;
|
|
35
|
+
isUser: boolean;
|
|
36
|
+
activeSwipeId: number;
|
|
37
|
+
isGenerating?: boolean;
|
|
38
|
+
finishReason?: string;
|
|
39
|
+
error?: import("../../../../react/chat/useChat/message").ChatError;
|
|
40
|
+
}[];
|
|
33
41
|
/** 分页游标,若为空则表示无下一页 */
|
|
34
42
|
nextCursor?: Cursor;
|
|
35
|
-
messages: Array<Message>;
|
|
36
43
|
total?: number;
|
|
37
44
|
conversationVersion: number;
|
|
38
45
|
}>;
|
package/dist/react.js
CHANGED
|
@@ -592,10 +592,20 @@ const messageSwipe = async function(messageId, swipeId) {
|
|
|
592
592
|
});
|
|
593
593
|
};
|
|
594
594
|
async function getPage(fromCursor, pageSize) {
|
|
595
|
-
|
|
595
|
+
const resp = await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE, {
|
|
596
596
|
fromCursor,
|
|
597
597
|
pageSize
|
|
598
598
|
});
|
|
599
|
+
return {
|
|
600
|
+
...resp,
|
|
601
|
+
messages: resp.messages.map((m)=>({
|
|
602
|
+
...m,
|
|
603
|
+
swipes: m.swipes.map((s)=>{
|
|
604
|
+
const ret = Object.assign(s.content, s);
|
|
605
|
+
return ret;
|
|
606
|
+
})
|
|
607
|
+
}))
|
|
608
|
+
};
|
|
599
609
|
}
|
|
600
610
|
const getInfo = async function() {
|
|
601
611
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_PROJECT_GET_INFO);
|
|
@@ -1463,7 +1473,7 @@ const useChat = ({ body, onFinish, pageSize = 10 })=>{
|
|
|
1463
1473
|
mujian.ai.chat.stop(conversationVersion.current, assisMsgId).then(()=>{
|
|
1464
1474
|
setMessages(produce((draft)=>{
|
|
1465
1475
|
const lastMessage = draft.at(-1);
|
|
1466
|
-
if (lastMessage) lastMessage.finishReason = 'user_stopped';
|
|
1476
|
+
if (lastMessage?.finishReason) lastMessage.finishReason = 'user_stopped';
|
|
1467
1477
|
}));
|
|
1468
1478
|
});
|
|
1469
1479
|
};
|
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:()=>G,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_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_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)},p=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_V2_COMPLETE,{isContinue:!0,conversationVersion:t},{onData:e})},A=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(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})};async function M(t,e){return await this.call(u.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE,{fromCursor:t,pageSize:e})}let y=async function(){return await this.call(u.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},N=async function(){return await this.call(u.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)};async function C(){return await this.call(u.MUJIAN_GET_CONFIG)}let w=async function(){},v=async function(){};async function S(t){return await this.call(u.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,t)}let b={i(...t){console.log("[MujianSDK] ",...t)},e(...t){console.error("[MujianSDK] ",...t)}};class O{constructor(){}static EVENT=u;static getInstance(){return window.$mujian||(window.$mujian=new O),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,b.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 C.call(this);n&&(this._config=n)}catch(t){b.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:y.bind(this)},settings:{persona:{getActive:N.bind(this)}},complete:_.bind(this),renderMessage:m.bind(this),continue:p.bind(this),regenerate:A.bind(this),stop:E.bind(this),recover:I.bind(this),message:{deleteOne:g.bind(this),editOne:f.bind(this),swipe:T.bind(this),getPage:M.bind(this)}},text:{complete:j.bind(this)},openai:{completions:{create:J.create.bind(this)},chat:{completions:{create:P.completions.create.bind(this)}},responses:{create:U.create.bind(this)},images:{generate:R.generate.bind(this)}}};ui={};utils={clipboard:{writeText:S.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:w.bind(this),loadGame:v.bind(this)},ranking:()=>{console.log("ranking")}}}let j=async function(t){return await this.call(O.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:t})},P={completions:{create:async function(t,e,i,n){return await this.call(O.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE,{params:t,options:e},{onData:i,signal:n})}}},J={create:async function(t,e,i,n){return await this.call(O.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:t,options:e},{onData:i,signal:n})}},U={create:async function(t,e,i,n){return await this.call(O.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE,{params:t,options:e},{onData:i,signal:n})}},R={generate:async function(t,e,i,n){return await this.call(O.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:t,options:e},{onData:i,signal:n})}};class G{constructor(){}static EVENT=u;static getInstance(){return window.$mujian||(window.$mujian=new G),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,b.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 C.call(this);n&&(this._config=n)}catch(t){b.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:y.bind(this)},settings:{persona:{getActive:N.bind(this)}},complete:_.bind(this),renderMessage:m.bind(this),continue:p.bind(this),regenerate:A.bind(this),stop:E.bind(this),recover:I.bind(this),message:{deleteOne:g.bind(this),editOne:f.bind(this),swipe:T.bind(this),getPage:M.bind(this)}},text:{complete:j.bind(this)},openai:{completions:{create:J.create.bind(this)},chat:{completions:{create:P.completions.create.bind(this)}},responses:{create:U.create.bind(this)},images:{generate:R.generate.bind(this)}}};ui={};utils={clipboard:{writeText:S.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:w.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:()=>G,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_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_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)},p=async function(t,e){return await this.call(u.MUJIAN_AI_CHAT_V2_COMPLETE,{isContinue:!0,conversationVersion:t},{onData:e})},A=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(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})};async function M(t,e){let i=await this.call(u.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE,{fromCursor:t,pageSize:e});return{...i,messages:i.messages.map(t=>({...t,swipes:t.swipes.map(t=>Object.assign(t.content,t))}))}}let y=async function(){return await this.call(u.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},N=async function(){return await this.call(u.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)};async function C(){return await this.call(u.MUJIAN_GET_CONFIG)}let w=async function(){},v=async function(){};async function b(t){return await this.call(u.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,t)}let O={i(...t){console.log("[MujianSDK] ",...t)},e(...t){console.error("[MujianSDK] ",...t)}};class S{constructor(){}static EVENT=u;static getInstance(){return window.$mujian||(window.$mujian=new S),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,O.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 C.call(this);n&&(this._config=n)}catch(t){O.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:y.bind(this)},settings:{persona:{getActive:N.bind(this)}},complete:_.bind(this),renderMessage:m.bind(this),continue:p.bind(this),regenerate:A.bind(this),stop:E.bind(this),recover:I.bind(this),message:{deleteOne:g.bind(this),editOne:f.bind(this),swipe:T.bind(this),getPage:M.bind(this)}},text:{complete:j.bind(this)},openai:{completions:{create:J.create.bind(this)},chat:{completions:{create:P.completions.create.bind(this)}},responses:{create:U.create.bind(this)},images:{generate:R.generate.bind(this)}}};ui={};utils={clipboard:{writeText:b.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:w.bind(this),loadGame:v.bind(this)},ranking:()=>{console.log("ranking")}}}let j=async function(t){return await this.call(S.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:t})},P={completions:{create:async function(t,e,i,n){return await this.call(S.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE,{params:t,options:e},{onData:i,signal:n})}}},J={create:async function(t,e,i,n){return await this.call(S.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:t,options:e},{onData:i,signal:n})}},U={create:async function(t,e,i,n){return await this.call(S.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE,{params:t,options:e},{onData:i,signal:n})}},R={generate:async function(t,e,i,n){return await this.call(S.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:t,options:e},{onData:i,signal:n})}};class G{constructor(){}static EVENT=u;static getInstance(){return window.$mujian||(window.$mujian=new G),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,O.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 C.call(this);n&&(this._config=n)}catch(t){O.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:y.bind(this)},settings:{persona:{getActive:N.bind(this)}},complete:_.bind(this),renderMessage:m.bind(this),continue:p.bind(this),regenerate:A.bind(this),stop:E.bind(this),recover:I.bind(this),message:{deleteOne:g.bind(this),editOne:f.bind(this),swipe:T.bind(this),getPage:M.bind(this)}},text:{complete:j.bind(this)},openai:{completions:{create:J.create.bind(this)},chat:{completions:{create:P.completions.create.bind(this)}},responses:{create:U.create.bind(this)},images:{generate:R.generate.bind(this)}}};ui={};utils={clipboard:{writeText:b.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:w.bind(this),loadGame:v.bind(this)},ranking:()=>{console.log("ranking")}}}return i})());
|
package/dist/umd/react.js
CHANGED
|
@@ -161,9 +161,9 @@ ${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=o)},[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",()=>{o.forEach(e=>{let t=i.current?.querySelector("#MJ-iframe-"+e);t&&t.contentWindow?.postMessage({type:"MJ_UPDATE_VIEWPORT_HEIGHT"},"*")})})},[]),(0,N.useEffect)(()=>(o.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()})}),()=>{o.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",()=>{sX.i("iframe loaded",t.id)})})}),[o]),(0,uY.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,o=n.data;if("call"===e.data.postmate){r in t.model&&"function"==typeof t.model[r]&&t.model[r](o);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,o=e.name,a=e.classListArray;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=o||"",this.frame.classList.add.apply(this.frame.classList,void 0===a?[]:a),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),o=0;return new e.Promise(function(e,a){r.parent.addEventListener("message",function t(o){return!!de(o,i)&&("handshake-reply"===o.data.postmate?(clearInterval(n),r.parent.removeEventListener("message",t,!1),r.childOrigin=o.origin,e(new dn(r))):a("Failed handshake"))},!1);var u=function(){o++,r.child.postMessage({postmate:"handshake",type:s4,model:r.model},i),5===o&&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 o=i.data.model;return o&&Object.keys(o).forEach(function(t){e.model[t]=o[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_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_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(e){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE,{messageId:e})},dh=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE,{messageId:e,content:t})},dm=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_SWIPE,{messageId:e,swipeId:t})};async function d_(e,t){return await this.call(da.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE,{fromCursor:e,pageSize:t})}let dg=async function(){return await this.call(da.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},db=async function(){return await this.call(da.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)},dy=async function(e){return await this.call(dT.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:e})},dv={completions:{create:async function(e,t,n,r){return await this.call(dT.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE,{params:e,options:t},{onData:n,signal:r})}}},dw={create:async function(e,t,n,r){return await this.call(dT.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:e,options:t},{onData:n,signal:r})}},dk={create:async function(e,t,n,r){return await this.call(dT.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE,{params:e,options:t},{onData:n,signal:r})}},dz={generate:async function(e,t,n,r){return await this.call(dT.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:e,options:t},{onData:n,signal:r})}};async function dS(){return await this.call(da.MUJIAN_GET_CONFIG)}let dE=async function(){},dA=async function(){};async function dx(e){return await this.call(da.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,e)}class dT{constructor(){}static EVENT=da;static getInstance(){return window.$mujian||(window.$mujian=new dT),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,sX.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 dS.call(this);r&&(this._config=r)}catch(e){sX.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,o)=>{this.pendingRequests.set(r,{resolve:i,reject:o,...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:dg.bind(this)},settings:{persona:{getActive:db.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:{deleteOne:df.bind(this),editOne:dh.bind(this),swipe:dm.bind(this),getPage:d_.bind(this)}},text:{complete:dy.bind(this)},openai:{completions:{create:dw.create.bind(this)},chat:{completions:{create:dv.completions.create.bind(this)}},responses:{create:dk.create.bind(this)},images:{generate:dz.generate.bind(this)}}};ui={};utils={clipboard:{writeText:dx.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:dE.bind(this),loadGame:dA.bind(this)},ranking:()=>{console.log("ranking")}}}let dj=`
|
|
164
|
+
</div>`}),i.current&&(i.current.innerHTML=o)},[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",()=>{o.forEach(e=>{let t=i.current?.querySelector("#MJ-iframe-"+e);t&&t.contentWindow?.postMessage({type:"MJ_UPDATE_VIEWPORT_HEIGHT"},"*")})})},[]),(0,N.useEffect)(()=>(o.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()})}),()=>{o.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",()=>{sX.i("iframe loaded",t.id)})})}),[o]),(0,uY.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,o=n.data;if("call"===e.data.postmate){r in t.model&&"function"==typeof t.model[r]&&t.model[r](o);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,o=e.name,a=e.classListArray;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=o||"",this.frame.classList.add.apply(this.frame.classList,void 0===a?[]:a),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),o=0;return new e.Promise(function(e,a){r.parent.addEventListener("message",function t(o){return!!de(o,i)&&("handshake-reply"===o.data.postmate?(clearInterval(n),r.parent.removeEventListener("message",t,!1),r.childOrigin=o.origin,e(new dn(r))):a("Failed handshake"))},!1);var u=function(){o++,r.child.postMessage({postmate:"handshake",type:s4,model:r.model},i),5===o&&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 o=i.data.model;return o&&Object.keys(o).forEach(function(t){e.model[t]=o[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_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_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(e){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE,{messageId:e})},dh=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE,{messageId:e,content:t})},dm=async function(e,t){return await this.call(da.MUJIAN_AI_CHAT_MESSAGE_SWIPE,{messageId:e,swipeId:t})};async function d_(e,t){let n=await this.call(da.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE,{fromCursor:e,pageSize:t});return{...n,messages:n.messages.map(e=>({...e,swipes:e.swipes.map(e=>Object.assign(e.content,e))}))}}let dg=async function(){return await this.call(da.MUJIAN_AI_CHAT_PROJECT_GET_INFO)},db=async function(){return await this.call(da.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE)},dy=async function(e){return await this.call(dT.EVENT.MUJIAN_AI_TEXT_GENERATE,{content:e})},dv={completions:{create:async function(e,t,n,r){return await this.call(dT.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE,{params:e,options:t},{onData:n,signal:r})}}},dw={create:async function(e,t,n,r){return await this.call(dT.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE,{params:e,options:t},{onData:n,signal:r})}},dk={create:async function(e,t,n,r){return await this.call(dT.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE,{params:e,options:t},{onData:n,signal:r})}},dz={generate:async function(e,t,n,r){return await this.call(dT.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE,{params:e,options:t},{onData:n,signal:r})}};async function dS(){return await this.call(da.MUJIAN_GET_CONFIG)}let dE=async function(){},dA=async function(){};async function dx(e){return await this.call(da.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT,e)}class dT{constructor(){}static EVENT=da;static getInstance(){return window.$mujian||(window.$mujian=new dT),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,sX.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 dS.call(this);r&&(this._config=r)}catch(e){sX.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,o)=>{this.pendingRequests.set(r,{resolve:i,reject:o,...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:dg.bind(this)},settings:{persona:{getActive:db.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:{deleteOne:df.bind(this),editOne:dh.bind(this),swipe:dm.bind(this),getPage:d_.bind(this)}},text:{complete:dy.bind(this)},openai:{completions:{create:dw.create.bind(this)},chat:{completions:{create:dv.completions.create.bind(this)}},responses:{create:dk.create.bind(this)},images:{generate:dz.generate.bind(this)}}};ui={};utils={clipboard:{writeText:dx.bind(this)}};hybrid={};player={};game={assets:()=>{console.log("assets")},saves:{saveGame:dE.bind(this),loadGame:dA.bind(this)},ranking:()=>{console.log("ranking")}}}let dj=`
|
|
165
165
|
@keyframes spin {
|
|
166
166
|
0% { transform: rotate(0deg); }
|
|
167
167
|
100% { transform: rotate(360deg); }
|
|
168
168
|
}
|
|
169
|
-
`,dO={width:48,height:48,animation:"spin 1s linear infinite"},dP=({className:e,style:t})=>(0,uY.jsxs)(uY.Fragment,{children:[(0,uY.jsx)("style",{children:dj}),(0,uY.jsx)("svg",{xmlns:"http://www.w3.org/2000/svg",width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",className:e,style:{...dO,...t},children:(0,uY.jsx)("path",{d:"M21 12a9 9 0 1 1-6.219-8.56"})})]}),dI=(0,N.createContext)(null),d$=(0,N.createContext)(null),dM=({children:e,loadingComponent:t})=>{let[n,r]=(0,N.useState)(null),[i,o]=(0,N.useState)(0);return(0,N.useEffect)(()=>{(async()=>{let e=dT.getInstance();await e.init(),r(e)})()},[]),(0,uY.jsx)(dI.Provider,{value:n,children:(0,uY.jsx)(d$.Provider,{value:{messageTypeWritingCount:i,updateMessageTypeWritingCount(e){o(t=>Math.max(t+("add"===e?1:-1),0))}},children:n?e:t??(0,uY.jsx)("div",{style:{height:"100%",width:"100%",display:"flex",justifyContent:"center",alignItems:"center"},children:(0,uY.jsx)(dP,{style:{color:"#EC4342"}})})})})},dC=()=>{let e=(0,N.useContext)(dI);if(!e){let e="Mujian is not initialized. Please check that useMujian is called inside MujianProvider";throw console.error(e),Error(e)}return e},dN=()=>{let e=(0,N.useContext)(d$);if(!e){let e="Context is not initialized. Please check that useChatContext is called inside MujianProvider";throw console.error(e),Error(e)}return e};var dR=r("../../common/temp/node_modules/.pnpm/react-dom@19.1.5_react@19.1.5/node_modules/react-dom/index.js"),{min:dL,max:dZ,abs:dD,floor:dU}=Math,dF=(e,t,n)=>dL(n,dZ(t,e)),dH=e=>[...e].sort((e,t)=>e-t),dJ=setTimeout,dB=clearTimeout,dV="function"==typeof queueMicrotask?queueMicrotask:e=>{Promise.resolve().then(e)},dG=()=>{let e;return[new Promise(t=>{e=t}),e]},dW="undefined"!=typeof window,dq=(b=()=>!!/iP(hone|od|ad)/.test(navigator.userAgent)||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>0,()=>(b&&(d=b(),b=void 0),d)),dK=(e,t,n,r=0,i=t-1)=>{let o=r;for(;r<=i;){let t=dU((r+i)/2);e(t)<=n?(o=t,r=t+1):i=t-1}return dF(o,0,t-1)},dX=(e,t,n)=>{let r=n?"unshift":"push";for(let n=0;n<t;n++)e[r](-1);return e},dY=(e,t)=>t?-e:e,dQ=(e,t)=>{e.V(()=>t)},d0=dW?N.useLayoutEffect:N.useEffect,d3="current",d8=(e,t)=>{if(Array.isArray(e))for(let n of e)d8(n,t);else null==e||"boolean"==typeof e||t.push(e)},d1=e=>{let t=(0,N.useRef)(e);return d0(()=>{t[d3]=e},[e]),t},d2=(0,N.memo)(({N:e,Z:t,ee:n,te:r,ne:i,oe:o,re:a,ie:u})=>{let s=(0,N.useRef)(null);d0(()=>t(s[d3],n),[n]);let d=(0,N.useMemo)(()=>{let e={contain:"layout style",position:i&&u?void 0:"absolute",[a?"height":"width"]:"100%",[a?"top":"left"]:0,[a?"insetInlineStart":"top"]:r,visibility:!i||u?void 0:"hidden"};return a&&(e.display="inline-flex"),e},[r,i,u,a]);return(0,uY.jsx)(o,"string"==typeof o?{ref:s,style:d,children:e}:{ref:s,style:d,index:n,children:e})}),d6=(0,N.forwardRef)(({children:e,data:t,bufferSize:n,itemSize:r,shift:i,horizontal:o,keepMounted:a,cache:u,startMargin:s=0,ssrCount:d,as:c="div",item:l="div",scrollRef:p,onScroll:f,onScrollEnd:h},m)=>{var _;let g,[b,y]=(0,N.useMemo)(()=>{let n;if("function"==typeof e)return[n=>e(t[n],n),t.length];let r=(d8(e,n=[]),n);return[e=>r[e],r.length]},[e,t]),v=(0,N.useRef)(null),w=(0,N.useRef)(!!d),k=d1(f),z=d1(h),[S,E,A,x]=(_=()=>{var e;let t,n,i,a,s,c,l,p,f=!!o,h=(e=y,t=u&&u[1]||r||40,n=-1,i=0,s=(a=u&&u[0])?dX(a.slice(0,dL(e,a.length)),dZ(0,e-a.length)):dX([],e),c=dX([],e+1),l=e=>{let n=s[e];return -1===n?t:n},{i:(t,n)=>{let r,o;return p(i=dL(i,e-1))<=t?(o=dK(p,e,n,i),r=dK(p,e,t,i,o)):(r=dK(p,e,t,void 0,i),o=dK(p,e,n,r)),i=r,[r,o]},l:t=>dK(p,e,t),u:p=t=>{if(!e)return 0;if(n>=t)return c[t];n<0&&(c[0]=0,n=0);let r=n,i=c[r];for(;r<t;)i+=l(r),c[++r]=i;return n=t,i},h:l,v:(e,t)=>{let r=-1===s[e];return s[e]=t,n=dL(e,n),r},p:(e,t=-1)=>s[e]===t,t:()=>p(e),S:()=>e,m:(r,i)=>{let o=r-e;return n=i?-1:dL(r-1,n),e=r,o>0?(dX(c,o),dX(s,o,i),t*o):(c.splice(o),(i?s.splice(0,-o):s.splice(o)).reduce((e,n)=>e-(-1===n?t:n),0))},$:r?void 0:e=>{let r=0,i=[];s.forEach((t,n)=>{-1!==t&&(t&&i.push(t),n<e&&r++)}),n=-1;let o=dH(i),a=o.length,u=a/2|0,d=a%2==0?(o[u-1]+o[u])/2:o[u],c=t;return((t=d)-c)*dZ(e-r,0)},q:()=>[s.slice(),t]}),m=(({i:e,l:t,u:n,h:r,v:i,p:o,t:a,S:u,m:s,$:d},c=0)=>{let l=!!d,p=!!c,f=1,h=0,m=0,_=0,g=0,b=0,y=0,v=0,w=0,k=null,z=[0,p?dZ(c-1,0):-1],S=0,E=!1,A=new Set,x=()=>_-m+b+g,T=e=>n(e)-b,j=e=>{e&&(dq()&&0!==v||k&&1===w?b+=e:g+=e)};return{_:()=>{A.clear()},I:()=>f,i:(t=200)=>{let n,r;if(!E||p)return z;if(y)[n,r]=z;else{let i=dZ(0,x()),o=i+h;l||(t=dZ(0,t),1!==v&&(i-=t),2!==v&&(o+=t)),[n,r]=z=e(dZ(0,i),dZ(0,o)),k&&(n=dL(n,k[0]),r=dZ(r,k[1]))}return[dZ(n,0),dL(r,u()-1)]},k:e=>t(e-m),R:o,u:T,h:r,C:u,T:()=>_,M:()=>0!==v,o:()=>h,O:()=>m,t:a,L:()=>(y=g,g=0,[y,2===w]),H:(e,t)=>{let n=[e,t];return A.add(n),()=>{A.delete(n)}},W:(n,u)=>{let c,z,O=0;switch(n){case 1:{if(u===_&&0===w)break;let e=y;y=0;let t=u-_,n=dD(t);e&&n<dD(e)+1||0!==w||(v=t<0?2:1),p&&(p=!1),_=u,O=4;let r=_-m;r>=-h&&r<=a()&&(O+=1,z=n>h);break}case 2:O=8,0!==v&&(c=!0,O+=1),v=0,w=0,k=null;break;case 3:{let e=u.filter(([e,t])=>!o(e,t));if(!e.length)break;for(let[t,n]of(j(e.reduce((e,[t,n])=>{let i;if(2===w)i=!0;else if(k&&1===w)i=t<k[0];else{let e=_-m,n=T(t),o=r(t);i=1!==v&&0===w?n+o<=e:n<e&&n+o<e+h}return i&&(e+=n-r(t)),e},0)),e)){let e=r(t),o=i(t,n);l&&(S+=o?n:n-e)}l&&h&&S>h&&(j(d(t(x()))),l=!1),O=3,z=!0;break}case 4:h!==u&&(h||(E=z=!0),h=u,O=3);break;case 5:u[1]?(j(s(u[0],!0)),w=2):s(u[0]),O=1;break;case 6:m=u;break;case 7:w=1;break;case 8:k=e(u,u+h),O=1}O&&(f=1+(0x7fffffff&f),c&&b&&(g+=b,b=0),A.forEach(([e,t])=>{O&e&&t(z)}))}}})(h,d);return[m,h,((e,t)=>{var n,r,i;let o,a,u,s,d=dG(),c=!1,l=t?"scrollLeft":"scrollTop",p=t?"left":"top",f=t?"overflowX":"overflowY",[h,m]=(n=()=>d[0],r=(e,t)=>{u.scrollTo({[p]:dY(e,c),behavior:t?"smooth":"instant"})},[async(t,i)=>{let a,u,s;if(!await n())return;o&&o();let d=o=()=>{a=!0,dB(u),s&&s()},c=()=>{if(!a){if(e.o()&&(dB(u),u=dJ(d,150)),i){for(let[t,n]=e.i(0);t<=n;t++)if(e.R(t))return;d()}e.W(7),r(t(),i)}},l=()=>{let t;a||(s=e.H(2,()=>{t||(t=!0,dV(()=>{t=!1,c()}))}),c())};i?(e.W(8,t()),dV(l)):l()},()=>{o&&o()}]),_=t?"width":"height",g=new WeakMap,b=(i=t=>{let n=[];for(let{target:r,contentRect:i}of t)if(r.offsetParent)if(r===u)e.W(4,i[_]);else{let e=g.get(r);null!=e&&n.push([e,i[_]])}n.length&&e.W(3,n)},{A(e){(a||(a=new e.ownerDocument.defaultView.ResizeObserver(i))).observe(e)},B(e){a.unobserve(e)},X(){a&&a.disconnect()}});return{D(n,r=n.parentElement){var i,o;let a,h,_,g,y,v,w,k,z,S,E,A,x;b.A(u=r),t&&(c="rtl"===getComputedStyle(r).direction),i=()=>dY(r[l],c),h=0,_=!1,g=!1,y=!1,v=!1,w=Date.now,k=()=>{_||g?(_=!1,z()):(y=!1,e.W(2))},z=()=>{dB(a),a=dJ(k,150)},S=()=>{h=w(),y&&(v=!0),o&&e.W(6,o()),e.W(1,i()),z()},E=n=>{if(_||!e.M()||n.ctrlKey)return;let r=w()-h;150>r&&50<r&&(t?n.deltaX:n.deltaY)&&(_=!0)},A=()=>{g=!0,y=v=!1},x=()=>{g=!1,dq()&&(y=!0)},r.addEventListener("scroll",S),r.addEventListener("wheel",E,{passive:!0}),r.addEventListener("touchstart",A,{passive:!0}),r.addEventListener("touchend",x,{passive:!0}),s={X:()=>{r.removeEventListener("scroll",S),r.removeEventListener("wheel",E),r.removeEventListener("touchstart",A),r.removeEventListener("touchend",x),dB(a)},Y:()=>{let[t,n]=e.L();t&&(((t,n,i)=>{if(i){let e=r.style,t=e[f];e[f]="hidden",dJ(()=>{e[f]=t})}let o=e.T()+t;o<=0||o>=e.O()+e.t()-e.o()?r.scrollTo({[p]:dY(o,c),behavior:"instant"}):r.scrollBy({[p]:dY(t,c),behavior:"instant"}),n&&m()})(t,n,v),v=!1,n&&e.o()>e.t()&&e.W(1,i()))}},d[1](!0)},_(){b.X(),s&&s.X(),d[1](!1),d=dG()},P:(e,t)=>(g.set(e,t),b.A(e),()=>{g.delete(e),b.B(e)}),V:h,J(){s&&s.Y()},j:e.O}})(m,f),f]},(g=(0,N.useRef)(null)).current||(g.current=_()));y!==S.C()&&S.W(5,[y,i]),s!==S.O()&&S.W(6,s);let[T,j]=(0,N.useReducer)(S.I,void 0,S.I),O=S.M(),P=S.t(),I=[],$=e=>{let t,n=b(e);return(0,uY.jsx)(d2,{Z:A.P,ee:e,te:S.u(e),ne:S.R(e),oe:l,N:n,re:x,ie:w[d3]},null!=(t=n.key)?t:"_"+e)};if(d0(()=>{w[d3]=!1,S.H(1,e=>{e?(0,dR.flushSync)(j):j()}),S.H(4,()=>{k.current&&k.current(S.T())}),S.H(8,()=>{z.current&&z.current()});let e=v[d3];return p?dV(()=>{p.current&&A.D(e,p[d3])}):A.D(e),()=>{S._(),A._()}},[]),d0(()=>{A.J()},[T]),(0,N.useImperativeHandle)(m,()=>({get cache(){return E.q()},get scrollOffset(){return S.T()},get scrollSize(){return dZ(S.t(),S.o())},get viewportSize(){return S.o()},findItemIndex:S.k,getItemOffset:S.u,getItemSize:S.h,scrollToIndex:(e,t)=>((e,t,n,{align:r,smooth:i,offset:o=0}={})=>{if(n=dF(n,0,t.C()-1),"nearest"===r){let e=t.u(n),i=t.T();if(e<i)r="start";else{if(!(e+t.h(n)>i+t.o()))return;r="end"}}e.V(()=>o+e.j()+t.u(n)+("end"===r?t.h(n)-t.o():"center"===r?(t.h(n)-t.o())/2:0),i)})(A,S,e,t),scrollTo:e=>dQ(A,e),scrollBy:e=>{dQ(A,e+S.T())}}),[]),a){let e=new Set(a);for(let[t,r]=S.i(n);t<=r;t++)e.add(t);dH([...e]).forEach(e=>{I.push($(e))})}else for(let[e,t]=S.i(n);e<=t;e++)I.push($(e));return(0,uY.jsx)(c,{ref:v,style:{contain:"size style",overflowAnchor:"none",flex:"none",position:"relative",width:x?P:"100%",height:x?"100%":P,pointerEvents:O?"none":void 0},children:I})});let d4={MessageItem:e=>{let{message:t,children:n,depth:r,index:i,typingOpts:o={chunkSize:2,delay:40}}=e,a=dC(),{updateMessageTypeWritingCount:u}=dN(),s=L(t),[d,c]=(0,N.useState)(null),l=(0,N.useRef)(null);function p(){l.current&&(clearTimeout(l.current),l.current=null,setTimeout(()=>{u("sub")}))}F(()=>{p()}),(0,N.useEffect)(()=>{if(t.isGenerating){let e;c(t.swipes[t.activeSwipeId].content.length),u("add"),p(),e=Date.now(),l.current=setInterval(()=>{c(t=>{let n,r,i=(r=(n=Date.now())-e,e=n,Math.round(r/o.delay)*o.chunkSize),a=s.current,u=a.swipes[a.activeSwipeId].content.length,d=Math.min((t||0)+i,u);return a.isGenerating||u!==d?d:(p(),null)})},o.delay)}},[t.isGenerating]);let f=(0,N.useRef)(null);(0,N.useEffect)(()=>{let e=t.swipes[t.activeSwipeId].content.length;if(f.current){let{length:t}=f.current;0===t&&c(e)}f.current={length:e}},[t]),(0,N.useEffect)(()=>{"user_stopped"===t.finishReason&&c(t.swipes[t.activeSwipeId].content.length)},[t.finishReason]);let{data:h}=ek(async()=>null!==d?{...t,swipes:t.swipes.map((e,n)=>n!==t.activeSwipeId?e:{...e,content:e.content.slice(0,d)})}:await a.ai.chat.renderMessage({message:t,depth:r,index:i}),{refreshDeps:[t,d]});return n(h||t,t)},MessageList:(0,N.forwardRef)((e,t)=>{let{children:n,messages:r,vListProps:i}=e;return(0,uY.jsx)(d6,{...i,data:r,ref:t,children:(e,t)=>n({message:e,index:t,depth:r.length-1-t})})})};var d9=Array.isArray,d5="object"==typeof global&&global&&global.Object===Object&&global,d7="object"==typeof self&&self&&self.Object===Object&&self,ce=d5||d7||Function("return this")(),ct=ce.Symbol,cn=Object.prototype,cr=cn.hasOwnProperty,ci=cn.toString,co=ct?ct.toStringTag:void 0;let ca=function(e){var t=cr.call(e,co),n=e[co];try{e[co]=void 0;var r=!0}catch(e){}var i=ci.call(e);return r&&(t?e[co]=n:delete e[co]),i};var cu=Object.prototype.toString,cs=ct?ct.toStringTag:void 0;let cd=function(e){return null==e?void 0===e?"[object Undefined]":"[object Null]":cs&&cs in Object(e)?ca(e):cu.call(e)},cc=function(e){return"symbol"==typeof e||null!=e&&"object"==typeof e&&"[object Symbol]"==cd(e)};var cl=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,cp=/^\w*$/;let cf=function(e,t){if(d9(e))return!1;var n=typeof e;return!!("number"==n||"symbol"==n||"boolean"==n||null==e||cc(e))||cp.test(e)||!cl.test(e)||null!=t&&e in Object(t)},ch=function(e){var t=typeof e;return null!=e&&("object"==t||"function"==t)},cm=function(e){if(!ch(e))return!1;var t=cd(e);return"[object Function]"==t||"[object GeneratorFunction]"==t||"[object AsyncFunction]"==t||"[object Proxy]"==t};var c_=ce["__core-js_shared__"],cg=(y=/[^.]+$/.exec(c_&&c_.keys&&c_.keys.IE_PROTO||""))?"Symbol(src)_1."+y:"",cb=Function.prototype.toString;let cy=function(e){if(null!=e){try{return cb.call(e)}catch(e){}try{return e+""}catch(e){}}return""};var cv=/^\[object .+?Constructor\]$/,cw=Object.prototype,ck=Function.prototype.toString,cz=cw.hasOwnProperty,cS=RegExp("^"+ck.call(cz).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");let cE=function(e){return!!ch(e)&&(!cg||!(cg in e))&&(cm(e)?cS:cv).test(cy(e))},cA=function(e,t){var n=null==e?void 0:e[t];return cE(n)?n:void 0};var cx=cA(Object,"create"),cT=Object.prototype.hasOwnProperty,cj=Object.prototype.hasOwnProperty;function cO(e){var t=-1,n=null==e?0:e.length;for(this.clear();++t<n;){var r=e[t];this.set(r[0],r[1])}}cO.prototype.clear=function(){this.__data__=cx?cx(null):{},this.size=0},cO.prototype.delete=function(e){var t=this.has(e)&&delete this.__data__[e];return this.size-=!!t,t},cO.prototype.get=function(e){var t=this.__data__;if(cx){var n=t[e];return"__lodash_hash_undefined__"===n?void 0:n}return cT.call(t,e)?t[e]:void 0},cO.prototype.has=function(e){var t=this.__data__;return cx?void 0!==t[e]:cj.call(t,e)},cO.prototype.set=function(e,t){var n=this.__data__;return this.size+=+!this.has(e),n[e]=cx&&void 0===t?"__lodash_hash_undefined__":t,this};let cP=function(e,t){for(var n,r=e.length;r--;)if((n=e[r][0])===t||n!=n&&t!=t)return r;return -1};var cI=Array.prototype.splice;function c$(e){var t=-1,n=null==e?0:e.length;for(this.clear();++t<n;){var r=e[t];this.set(r[0],r[1])}}c$.prototype.clear=function(){this.__data__=[],this.size=0},c$.prototype.delete=function(e){var t=this.__data__,n=cP(t,e);return!(n<0)&&(n==t.length-1?t.pop():cI.call(t,n,1),--this.size,!0)},c$.prototype.get=function(e){var t=this.__data__,n=cP(t,e);return n<0?void 0:t[n][1]},c$.prototype.has=function(e){return cP(this.__data__,e)>-1},c$.prototype.set=function(e,t){var n=this.__data__,r=cP(n,e);return r<0?(++this.size,n.push([e,t])):n[r][1]=t,this};var cM=cA(ce,"Map");let cC=function(e){var t=typeof e;return"string"==t||"number"==t||"symbol"==t||"boolean"==t?"__proto__"!==e:null===e},cN=function(e,t){var n=e.__data__;return cC(t)?n["string"==typeof t?"string":"hash"]:n.map};function cR(e){var t=-1,n=null==e?0:e.length;for(this.clear();++t<n;){var r=e[t];this.set(r[0],r[1])}}function cL(e,t){if("function"!=typeof e||null!=t&&"function"!=typeof t)throw TypeError("Expected a function");var n=function(){var r=arguments,i=t?t.apply(this,r):r[0],o=n.cache;if(o.has(i))return o.get(i);var a=e.apply(this,r);return n.cache=o.set(i,a)||o,a};return n.cache=new(cL.Cache||cR),n}cR.prototype.clear=function(){this.size=0,this.__data__={hash:new cO,map:new(cM||c$),string:new cO}},cR.prototype.delete=function(e){var t=cN(this,e).delete(e);return this.size-=!!t,t},cR.prototype.get=function(e){return cN(this,e).get(e)},cR.prototype.has=function(e){return cN(this,e).has(e)},cR.prototype.set=function(e,t){var n=cN(this,e),r=n.size;return n.set(e,t),this.size+=+(n.size!=r),this},cL.Cache=cR;var cZ=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,cD=/\\(\\)?/g,cU=(w=(v=cL(function(e){var t=[];return 46===e.charCodeAt(0)&&t.push(""),e.replace(cZ,function(e,n,r,i){t.push(r?i.replace(cD,"$1"):n||e)}),t},function(e){return 500===w.size&&w.clear(),e})).cache,v);let cF=function(e,t){for(var n=-1,r=null==e?0:e.length,i=Array(r);++n<r;)i[n]=t(e[n],n,e);return i};var cH=1/0,cJ=ct?ct.prototype:void 0,cB=cJ?cJ.toString:void 0;let cV=function e(t){if("string"==typeof t)return t;if(d9(t))return cF(t,e)+"";if(cc(t))return cB?cB.call(t):"";var n=t+"";return"0"==n&&1/t==-cH?"-0":n},cG=function(e,t){return d9(e)?e:cf(e,t)?[e]:cU(null==e?"":cV(e))};var cW=1/0;let cq=function(e){if("string"==typeof e||cc(e))return e;var t=e+"";return"0"==t&&1/e==-cW?"-0":t},cK=function(e,t){t=cG(t,e);for(var n=0,r=t.length;null!=e&&n<r;)e=e[cq(t[n++])];return n&&n==r?e:void 0},cX=function(e,t,n){var r=null==e?void 0:cK(e,t);return void 0===r?n:r};function cY({projectInfo:e,activePersona:r,onSend:i,messages:d,setSwipe:c}){(0,N.useEffect)(()=>{n=e??void 0,o=r??void 0},[e,r]),(0,N.useEffect)(()=>{t=async e=>{await i(e)},a=async e=>{if(d.length>1)throw Error("已有新消息,不允许切换开场白");if(0===d.length)throw Error("断言失败:没有开场消息,无法切换开场白");let t=Math.floor(e)-1,n=d[0].swipes.length;t<0||t>=n||c(d[0].id,t)},s=(e,t)=>d.find(t=>t.id===e)?.swipes[t]?.mjv?.value,u=(e,t,n)=>{let r=s?.(e,t);if(r)return cX(r,n)}},[i,d,c])}window.$mj_engine={ai:{chat:{get complete(){return t},get project(){return n},settings:{get persona(){return o}},get setFirstMesIndex(){return a}}},bind:({messageId:e,swipeId:r})=>({ai:{chat:{mjv:{get:t=>u?.(e,r,t),getAll:()=>s?.(e,r)??{}},get complete(){return t},get project(){return n},settings:{get persona(){return o}},get setFirstMesIndex(){return a}}}})};var cQ=Symbol.for("immer-nothing"),c0=Symbol.for("immer-draftable"),c3=Symbol.for("immer-state");function c8(e){throw Error(`[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`)}var c1=Object,c2=c1.getPrototypeOf,c6="constructor",c4="prototype",c9="configurable",c5="enumerable",c7="writable",le="value",lt=e=>!!e&&!!e[c3];function ln(e){return!!e&&(lo(e)||ll(e)||!!e[c0]||!!e[c6]?.[c0]||lp(e)||lf(e))}var lr=c1[c4][c6].toString(),li=new WeakMap;function lo(e){if(!e||!lh(e))return!1;let t=c2(e);if(null===t||t===c1[c4])return!0;let n=c1.hasOwnProperty.call(t,c6)&&t[c6];if(n===Object)return!0;if(!lm(n))return!1;let r=li.get(n);return void 0===r&&(r=Function.toString.call(n),li.set(n,r)),r===lr}function la(e,t,n=!0){0===lu(e)?(n?Reflect.ownKeys(e):c1.keys(e)).forEach(n=>{t(n,e[n],e)}):e.forEach((n,r)=>t(r,n,e))}function lu(e){let t=e[c3];return t?t.type_:ll(e)?1:lp(e)?2:3*!!lf(e)}var ls=(e,t,n=lu(e))=>2===n?e.has(t):c1[c4].hasOwnProperty.call(e,t),ld=(e,t,n=lu(e))=>2===n?e.get(t):e[t],lc=(e,t,n,r=lu(e))=>{2===r?e.set(t,n):3===r?e.add(n):e[t]=n},ll=Array.isArray,lp=e=>e instanceof Map,lf=e=>e instanceof Set,lh=e=>"object"==typeof e,lm=e=>"function"==typeof e,l_=e=>e.modified_?e.copy_:e.base_;function lg(e,t){if(lp(e))return new Map(e);if(lf(e))return new Set(e);if(ll(e))return Array[c4].slice.call(e);let n=lo(e);if(!0!==t&&("class_only"!==t||n)){let t=c2(e);if(null!==t&&n)return{...e};let r=c1.create(t);return c1.assign(r,e)}{let t=c1.getOwnPropertyDescriptors(e);delete t[c3];let n=Reflect.ownKeys(t);for(let r=0;r<n.length;r++){let i=n[r],o=t[i];!1===o[c7]&&(o[c7]=!0,o[c9]=!0),(o.get||o.set)&&(t[i]={[c9]:!0,[c7]:!0,[c5]:o[c5],[le]:e[i]})}return c1.create(c2(e),t)}}function lb(e,t=!1){return lv(e)||lt(e)||!ln(e)||(lu(e)>1&&c1.defineProperties(e,{set:ly,add:ly,clear:ly,delete:ly}),c1.freeze(e),t&&la(e,(e,t)=>{lb(t,!0)},!1)),e}var ly={[le]:function(){c8(2)}};function lv(e){return!(null!==e&&lh(e))||c1.isFrozen(e)}var lw="MapSet",lk="Patches",lz="ArrayMethods",lS={};function lE(e){let t=lS[e];return t||c8(0,e),t}var lA=e=>!!lS[e];function lx(e,t){t&&(e.patchPlugin_=lE(lk),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function lT(e){lj(e),e.drafts_.forEach(lP),e.drafts_=null}function lj(e){e===E&&(E=e.parent_)}var lO=e=>E={drafts_:[],parent_:E,immer_:e,canAutoFreeze_:!0,unfinalizedDrafts_:0,handledSet_:new Set,processedForPatches_:new Set,mapSetPlugin_:lA(lw)?lE(lw):void 0,arrayMethodsPlugin_:lA(lz)?lE(lz):void 0};function lP(e){let t=e[c3];0===t.type_||1===t.type_?t.revoke_():t.revoked_=!0}function lI(e,t){t.unfinalizedDrafts_=t.drafts_.length;let n=t.drafts_[0];if(void 0!==e&&e!==n){n[c3].modified_&&(lT(t),c8(4)),ln(e)&&(e=l$(t,e));let{patchPlugin_:r}=t;r&&r.generateReplacementPatches_(n[c3].base_,e,t)}else e=l$(t,n);return function(e,t,n=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&lb(t,n)}(t,e,!0),lT(t),t.patches_&&t.patchListener_(t.patches_,t.inversePatches_),e!==cQ?e:void 0}function l$(e,t){if(lv(t))return t;let n=t[c3];if(!n)return lZ(t,e.handledSet_,e);if(!lC(n,e))return t;if(!n.modified_)return n.base_;if(!n.finalized_){let{callbacks_:t}=n;if(t)for(;t.length>0;)t.pop()(e);lL(n,e)}return n.copy_}function lM(e){e.finalized_=!0,e.scope_.unfinalizedDrafts_--}var lC=(e,t)=>e.scope_===t,lN=[];function lR(e,t,n,r){let i=e.copy_||e.base_,o=e.type_;if(void 0!==r&&ld(i,r,o)===t)return void lc(i,r,n,o);if(!e.draftLocations_){let t=e.draftLocations_=new Map;la(i,(e,n)=>{if(lt(n)){let r=t.get(n)||[];r.push(e),t.set(n,r)}})}for(let r of e.draftLocations_.get(t)??lN)lc(i,r,n,o)}function lL(e,t){if(e.modified_&&!e.finalized_&&(3===e.type_||1===e.type_&&e.allIndicesReassigned_||(e.assigned_?.size??0)>0)){let{patchPlugin_:n}=t;if(n){let r=n.getPath(e);r&&n.generatePatches_(e,r,t)}lM(e)}}function lZ(e,t,n){return!n.immer_.autoFreeze_&&n.unfinalizedDrafts_<1||lt(e)||t.has(e)||!ln(e)||lv(e)||(t.add(e),la(e,(r,i)=>{if(lt(i)){let t=i[c3];lC(t,n)&&(lc(e,r,l_(t),e.type_),lM(t))}else ln(i)&&lZ(i,t,n)})),e}var lD={get(e,t){let n;if(t===c3)return e;let r=e.scope_.arrayMethodsPlugin_,i=1===e.type_&&"string"==typeof t;if(i&&r?.isArrayOperationMethod(t))return r.createMethodInterceptor(e,t);let o=e.copy_||e.base_;if(!ls(o,t,e.type_)){var a;let n;return a=e,(n=lH(o,t))?le in n?n[le]:n.get?.call(a.draft_):void 0}let u=o[t];if(e.finalized_||!ln(u)||i&&e.operationMethod&&r?.isMutatingArrayMethod(e.operationMethod)&&Number.isInteger(n=+t)&&String(n)===t)return u;if(u===lF(e.base_,t)){lB(e);let n=1===e.type_?+t:t,r=lV(e.scope_,u,e,n);return e.copy_[n]=r}return u},has:(e,t)=>t in(e.copy_||e.base_),ownKeys:e=>Reflect.ownKeys(e.copy_||e.base_),set(e,t,n){let r=lH(e.copy_||e.base_,t);if(r?.set)return r.set.call(e.draft_,n),!0;if(!e.modified_){let r=lF(e.copy_||e.base_,t),i=r?.[c3];if(i&&i.base_===n)return e.copy_[t]=n,e.assigned_.set(t,!1),!0;if((n===r?0!==n||1/n==1/r:n!=n&&r!=r)&&(void 0!==n||ls(e.base_,t,e.type_)))return!0;lB(e),lJ(e)}return!!(e.copy_[t]===n&&(void 0!==n||t in e.copy_)||Number.isNaN(n)&&Number.isNaN(e.copy_[t]))||(e.copy_[t]=n,e.assigned_.set(t,!0),!function(e,t,n){let{scope_:r}=e;if(lt(n)){let i=n[c3];lC(i,r)&&i.callbacks_.push(function(){lB(e),lR(e,n,l_(i),t)})}else ln(n)&&e.callbacks_.push(function(){let i=e.copy_||e.base_;3===e.type_?i.has(n)&&lZ(n,r.handledSet_,r):ld(i,t,e.type_)===n&&r.drafts_.length>1&&(e.assigned_.get(t)??!1)===!0&&e.copy_&&lZ(ld(e.copy_,t,e.type_),r.handledSet_,r)})}(e,t,n),!0)},deleteProperty:(e,t)=>(lB(e),void 0!==lF(e.base_,t)||t in e.base_?(e.assigned_.set(t,!1),lJ(e)):e.assigned_.delete(t),e.copy_&&delete e.copy_[t],!0),getOwnPropertyDescriptor(e,t){let n=e.copy_||e.base_,r=Reflect.getOwnPropertyDescriptor(n,t);return r?{[c7]:!0,[c9]:1!==e.type_||"length"!==t,[c5]:r[c5],[le]:n[t]}:r},defineProperty(){c8(11)},getPrototypeOf:e=>c2(e.base_),setPrototypeOf(){c8(12)}},lU={};for(let e in lD){let t=lD[e];lU[e]=function(){let e=arguments;return e[0]=e[0][0],t.apply(this,e)}}function lF(e,t){let n=e[c3];return(n?n.copy_||n.base_:e)[t]}function lH(e,t){if(!(t in e))return;let n=c2(e);for(;n;){let e=Object.getOwnPropertyDescriptor(n,t);if(e)return e;n=c2(n)}}function lJ(e){!e.modified_&&(e.modified_=!0,e.parent_&&lJ(e.parent_))}function lB(e){e.copy_||(e.assigned_=new Map,e.copy_=lg(e.base_,e.scope_.immer_.useStrictShallowCopy_))}function lV(e,t,n,r){let[i,o]=lp(t)?lE(lw).proxyMap_(t,n):lf(t)?lE(lw).proxySet_(t,n):function(e,t){let n=ll(e),r={type_:+!!n,scope_:t?t.scope_:E,modified_:!1,finalized_:!1,assigned_:void 0,parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1,callbacks_:void 0},i=r,o=lD;n&&(i=[r],o=lU);let{revoke:a,proxy:u}=Proxy.revocable(i,o);return r.draft_=u,r.revoke_=a,[u,r]}(t,n);if((n?.scope_??E).drafts_.push(i),o.callbacks_=n?.callbacks_??[],o.key_=r,n&&void 0!==r)n.callbacks_.push(function(e){if(!o||!lC(o,e))return;e.mapSetPlugin_?.fixSetContents(o);let t=l_(o);lR(n,o.draft_??o,t,r),lL(o,e)});else o.callbacks_.push(function(e){e.mapSetPlugin_?.fixSetContents(o);let{patchPlugin_:t}=e;o.modified_&&t&&t.generatePatches_(o,[],e)});return i}lU.deleteProperty=function(e,t){return lU.set.call(this,e,t,void 0)},lU.set=function(e,t,n){return lD.set.call(this,e[0],t,n,e[0])};var lG=new class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.useStrictIteration_=!1,this.produce=(e,t,n)=>{let r;if(lm(e)&&!lm(t)){let n=t;t=e;let r=this;return function(e=n,...i){return r.produce(e,e=>t.call(this,e,...i))}}if(lm(t)||c8(6),void 0===n||lm(n)||c8(7),ln(e)){let i=lO(this),o=lV(i,e,void 0),a=!0;try{r=t(o),a=!1}finally{a?lT(i):lj(i)}return lx(i,n),lI(r,i)}if(e&&lh(e))c8(1,e);else{if(void 0===(r=t(e))&&(r=e),r===cQ&&(r=void 0),this.autoFreeze_&&lb(r,!0),n){let t=[],i=[];lE(lk).generateReplacementPatches_(e,r,{patches_:t,inversePatches_:i}),n(t,i)}return r}},this.produceWithPatches=(e,t)=>{let n,r;return lm(e)?(t,...n)=>this.produceWithPatches(t,t=>e(t,...n)):[this.produce(e,t,(e,t)=>{n=e,r=t}),n,r]},"boolean"==typeof e?.autoFreeze&&this.setAutoFreeze(e.autoFreeze),"boolean"==typeof e?.useStrictShallowCopy&&this.setUseStrictShallowCopy(e.useStrictShallowCopy),"boolean"==typeof e?.useStrictIteration&&this.setUseStrictIteration(e.useStrictIteration)}createDraft(e){var t;ln(e)||c8(8),lt(e)&&(lt(t=e)||c8(10,t),e=function e(t){let n;if(!ln(t)||lv(t))return t;let r=t[c3],i=!0;if(r){if(!r.modified_)return r.base_;r.finalized_=!0,n=lg(t,r.scope_.immer_.useStrictShallowCopy_),i=r.scope_.immer_.shouldUseStrictIteration()}else n=lg(t,!0);return la(n,(t,r)=>{lc(n,t,e(r))},i),r&&(r.finalized_=!1),n}(t));let n=lO(this),r=lV(n,e,void 0);return r[c3].isManual_=!0,lj(n),r}finishDraft(e,t){let n=e&&e[c3];n&&n.isManual_||c8(9);let{scope_:r}=n;return lx(r,t),lI(void 0,r)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}setUseStrictIteration(e){this.useStrictIteration_=e}shouldUseStrictIteration(){return this.useStrictIteration_}applyPatches(e,t){let n;for(n=t.length-1;n>=0;n--){let r=t[n];if(0===r.path.length&&"replace"===r.op){e=r.value;break}}n>-1&&(t=t.slice(n+1));let r=lE(lk).applyPatches_;return lt(e)?r(e,t):this.produce(e,e=>r(e,t))}}().produce;let lW=["network error","NetworkError when attempting to fetch resource.","The Internet connection appears to be offline.","Network request failed","Load failed","Failed to fetch"];function lq(e){return lW.some(t=>e.includes(t))}async function*lK(e,t,n="generate",r,i){let o=[],a=null,u=!1,s=null;try{let d=e=>{!s&&(o.push(e),a&&(a(),a=null))},c=()=>{u=!0,a&&(a(),a=null)},l=e=>{s=e,a&&(a(),a=null)};if("generate"===n)e.ai.chat.complete(t,r,d).then(c).catch(l);else if("regenerate"===n)e.ai.chat.regenerate(r,d).then(c).catch(l);else if("continue"===n)e.ai.chat.continue(r,d).then(c).catch(l);else{if(!i)throw Error("call recover without assisMsgId");e.ai.chat.recover(r,i,d).then(c).catch(l)}}catch(e){s=e,u=!0}for(;;)if(o.length>0)yield o.shift();else if(u)break;else if(s)throw s;else await new Promise(e=>{a=e})}let lX=aF({message:ov()});function lY(e){let{success:t}=lX.safeParse(e);return t}let lQ="not_saved",l0=({body:e,onFinish:t,pageSize:n=10})=>{let[r,i]=(0,N.useState)(""),o=(0,N.useRef)(0),a=dC(),u=(0,N.useRef)(!1),{data:s,loadMoreAsync:d,loadingMore:c,loading:l,mutate:p,error:f,reloadAsync:h}=eS(async e=>{if(u.current=!0,e?.noMore)return{noMore:!0,list:[]};try{let t=await a.ai.chat.message.getPage(e?.cursor,n);o.current=t.conversationVersion;let r=t.messages.at(-1);return r?.isGenerating&&z(r.id,o.current),{cursor:t.nextCursor,list:t.messages,noMore:!t.nextCursor}}finally{u.current=!1}},{direction:"top"}),m=async()=>{await h()},_=async()=>{if(!u.current)try{u.current=!0,await d()}finally{u.current=!1}},g=s?.list??[],b=L(s),y=(0,N.useRef)(g);y.current=g;let v=e=>{let t;t="function"==typeof e?e(y.current):e,y.current=t,p({...b.current,list:t})},{chatStreaming:w,isStreaming:k,recoverStreaming:z}=(({common:e,setMessages:t,getMessages:n,setInput:r,mujian:i,conversationVersion:o})=>{let{body:a}=e,[u,s]=(0,N.useState)(!1);return{chatStreaming:(0,N.useCallback)(async({query:e,regenerate:a,is_continue:u,conversationVersion:d})=>{let c=n();try{s(!0);let n={id:`${lQ}_user_${Date.now()}`,isUser:!0,swipes:[{content:e??""}],activeSwipeId:0,isGenerating:!1},r={id:`${lQ}_assistant_${Date.now()}`,isUser:!1,swipes:[{content:""}],activeSwipeId:0,isGenerating:!0};for await(let s of(a?t(lG(e=>{let t=e.at(-1);t.activeSwipeId=t.swipes.length,t.swipes.push({content:""}),t.isGenerating=!0,t.finishReason=void 0,t.error=void 0})):u?t(e=>[...e,r]):t(e=>[...e,n,r]),lK(i,e||"",u?"continue":a?"regenerate":"generate",d))){if(!s)return;"meta"===s.type?(a||t(lG(e=>{u||(e[e.length-2].id=s.userMsgId),e[e.length-1].id=s.assisMsgId})),o.current=s.newConversationVersion):"data"===s.type&&(t(lG(e=>{let t=e.at(-1);t.swipes[t.activeSwipeId]={content:s.content,mjv:s.mjv},s.finishReason&&(t.finishReason=s.finishReason),s.error&&(t.error=s.error)})),await new Promise(e=>setTimeout(e,10)))}}catch(e){sX.e("Stream error",e),t(lG(t=>{let n=t.at(-1);lY(e)&&lq(e.message)?n.error={code:-1e4,message:"E_NETWORK_ERROR"}:n.error={code:0x3b9aca05,message:String(e)}}))}finally{t(lG(n=>{let i=n[n.length-1];if(i.isGenerating=!1,i.error?.code===0x77dde1f9){let e=c.find(e=>e.id===i.id);e?(i.swipes=e.swipes,i.activeSwipeId=e.activeSwipeId):(i.activeSwipeId-=1,i.swipes.length=i.swipes.length-1)}else i.error?.code===0x77dde1f1&&(t(c),e&&r(e))})),s(!1)}},[a,i]),isStreaming:u,recoverStreaming:(0,N.useCallback)(async(e,n)=>{let r=lK(i,"","recover",n,e);try{for await(let n of r){if(!n)return;"data"===n.type&&t(lG(t=>{let r=t.find(t=>t.id===e);r&&(r.swipes[r.activeSwipeId]={content:n.content,mjv:n.mjv},n.finishReason&&(r.finishReason=n.finishReason),n.error&&(r.error=n.error))}))}}catch(e){sX.e("Stream error",e),t(lG(t=>{let n=t.at(-1);lY(e)&&lq(e.message)?n.error={code:-1e4,message:"E_NETWORK_ERROR"}:n.error={code:0x3b9aca05,message:String(e)}}))}finally{t(lG(t=>{let n=t.find(t=>t.id===e);n&&(n.isGenerating=!1)}))}},[i])}})({common:{body:e},setMessages:v,getMessages:(0,N.useCallback)(()=>y.current,[y]),setInput:i,mujian:a,conversationVersion:o});G(()=>{if(!k){let e=g[g.length-1];t?.(e)}},[k]);let S=(0,N.useCallback)(async e=>{if(k)throw Error("useChat is streaming already.");await w({query:e,conversationVersion:o.current})},[k,w]),E=(0,N.useCallback)(async()=>{if(k)throw Error("useChat is streaming already.");let e=g.findLast(e=>e.isUser),t=e?e.swipes[e.activeSwipeId].content:"";await w({query:t,regenerate:!0,conversationVersion:o.current})},[k,g,w]),A=(0,N.useCallback)(async()=>{if(k)throw Error("useChat is streaming already.");await w({query:"",is_continue:!0,conversationVersion:o.current})},[k,w]),x=async(e,t)=>{if(t<0)return;v(lG(n=>{let r=n.find(t=>t.id===e);r&&(r.activeSwipeId=t)}));let n=await a.ai.chat.message.swipe(e,t),{data:r,success:i}=aF({newConversationVersion:as()}).safeParse(n);i&&r.newConversationVersion&&(o.current=r.newConversationVersion)},T=async(e,t)=>{let n=await a.ai.chat.message.editOne(e,t),{data:r,success:i}=aF({newConversationVersion:as()}).safeParse(n);i&&r.newConversationVersion&&(o.current=r.newConversationVersion),v(lG(n=>{let r=n.find(t=>t.id===e);r&&(r.swipes[r.activeSwipeId].content=t)}))},j=async e=>{if(!e.startsWith(lQ))try{let t=await a.ai.chat.message.deleteOne(e),{data:n,success:r}=aF({newConversationVersion:as()}).safeParse(t);r&&n.newConversationVersion&&(o.current=n.newConversationVersion)}catch(e){if(e?.code!==0x77dde1f4)throw e}v(t=>t.filter(t=>t.id!==e))};return{append:S,regenerate:E,continueGenerate:A,stop:()=>{let e=g.at(-1)?.id;e&&a.ai.chat.stop(o.current,e).then(()=>{v(lG(e=>{let t=e.at(-1);t&&(t.finishReason="user_stopped")}))})},messages:g,status:k?"streaming":"ready",setMessages:v,input:r,setInput:i,handleSubmit:()=>{S(r)},setSwipe:x,editMessage:T,deleteMessage:j,loadMoreMessage:_,messagesStatus:f?"error":c||l?"loading":s?.noMore?"all-loaded":"has-more",refresh:m}}})(),i})());
|
|
169
|
+
`,dO={width:48,height:48,animation:"spin 1s linear infinite"},dP=({className:e,style:t})=>(0,uY.jsxs)(uY.Fragment,{children:[(0,uY.jsx)("style",{children:dj}),(0,uY.jsx)("svg",{xmlns:"http://www.w3.org/2000/svg",width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",className:e,style:{...dO,...t},children:(0,uY.jsx)("path",{d:"M21 12a9 9 0 1 1-6.219-8.56"})})]}),dI=(0,N.createContext)(null),d$=(0,N.createContext)(null),dM=({children:e,loadingComponent:t})=>{let[n,r]=(0,N.useState)(null),[i,o]=(0,N.useState)(0);return(0,N.useEffect)(()=>{(async()=>{let e=dT.getInstance();await e.init(),r(e)})()},[]),(0,uY.jsx)(dI.Provider,{value:n,children:(0,uY.jsx)(d$.Provider,{value:{messageTypeWritingCount:i,updateMessageTypeWritingCount(e){o(t=>Math.max(t+("add"===e?1:-1),0))}},children:n?e:t??(0,uY.jsx)("div",{style:{height:"100%",width:"100%",display:"flex",justifyContent:"center",alignItems:"center"},children:(0,uY.jsx)(dP,{style:{color:"#EC4342"}})})})})},dC=()=>{let e=(0,N.useContext)(dI);if(!e){let e="Mujian is not initialized. Please check that useMujian is called inside MujianProvider";throw console.error(e),Error(e)}return e},dN=()=>{let e=(0,N.useContext)(d$);if(!e){let e="Context is not initialized. Please check that useChatContext is called inside MujianProvider";throw console.error(e),Error(e)}return e};var dR=r("../../common/temp/node_modules/.pnpm/react-dom@19.1.5_react@19.1.5/node_modules/react-dom/index.js"),{min:dL,max:dZ,abs:dD,floor:dU}=Math,dF=(e,t,n)=>dL(n,dZ(t,e)),dH=e=>[...e].sort((e,t)=>e-t),dJ=setTimeout,dB=clearTimeout,dV="function"==typeof queueMicrotask?queueMicrotask:e=>{Promise.resolve().then(e)},dG=()=>{let e;return[new Promise(t=>{e=t}),e]},dW="undefined"!=typeof window,dq=(b=()=>!!/iP(hone|od|ad)/.test(navigator.userAgent)||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>0,()=>(b&&(d=b(),b=void 0),d)),dK=(e,t,n,r=0,i=t-1)=>{let o=r;for(;r<=i;){let t=dU((r+i)/2);e(t)<=n?(o=t,r=t+1):i=t-1}return dF(o,0,t-1)},dX=(e,t,n)=>{let r=n?"unshift":"push";for(let n=0;n<t;n++)e[r](-1);return e},dY=(e,t)=>t?-e:e,dQ=(e,t)=>{e.V(()=>t)},d0=dW?N.useLayoutEffect:N.useEffect,d3="current",d8=(e,t)=>{if(Array.isArray(e))for(let n of e)d8(n,t);else null==e||"boolean"==typeof e||t.push(e)},d1=e=>{let t=(0,N.useRef)(e);return d0(()=>{t[d3]=e},[e]),t},d2=(0,N.memo)(({N:e,Z:t,ee:n,te:r,ne:i,oe:o,re:a,ie:u})=>{let s=(0,N.useRef)(null);d0(()=>t(s[d3],n),[n]);let d=(0,N.useMemo)(()=>{let e={contain:"layout style",position:i&&u?void 0:"absolute",[a?"height":"width"]:"100%",[a?"top":"left"]:0,[a?"insetInlineStart":"top"]:r,visibility:!i||u?void 0:"hidden"};return a&&(e.display="inline-flex"),e},[r,i,u,a]);return(0,uY.jsx)(o,"string"==typeof o?{ref:s,style:d,children:e}:{ref:s,style:d,index:n,children:e})}),d6=(0,N.forwardRef)(({children:e,data:t,bufferSize:n,itemSize:r,shift:i,horizontal:o,keepMounted:a,cache:u,startMargin:s=0,ssrCount:d,as:c="div",item:l="div",scrollRef:p,onScroll:f,onScrollEnd:h},m)=>{var _;let g,[b,y]=(0,N.useMemo)(()=>{let n;if("function"==typeof e)return[n=>e(t[n],n),t.length];let r=(d8(e,n=[]),n);return[e=>r[e],r.length]},[e,t]),v=(0,N.useRef)(null),w=(0,N.useRef)(!!d),k=d1(f),z=d1(h),[S,E,A,x]=(_=()=>{var e;let t,n,i,a,s,c,l,p,f=!!o,h=(e=y,t=u&&u[1]||r||40,n=-1,i=0,s=(a=u&&u[0])?dX(a.slice(0,dL(e,a.length)),dZ(0,e-a.length)):dX([],e),c=dX([],e+1),l=e=>{let n=s[e];return -1===n?t:n},{i:(t,n)=>{let r,o;return p(i=dL(i,e-1))<=t?(o=dK(p,e,n,i),r=dK(p,e,t,i,o)):(r=dK(p,e,t,void 0,i),o=dK(p,e,n,r)),i=r,[r,o]},l:t=>dK(p,e,t),u:p=t=>{if(!e)return 0;if(n>=t)return c[t];n<0&&(c[0]=0,n=0);let r=n,i=c[r];for(;r<t;)i+=l(r),c[++r]=i;return n=t,i},h:l,v:(e,t)=>{let r=-1===s[e];return s[e]=t,n=dL(e,n),r},p:(e,t=-1)=>s[e]===t,t:()=>p(e),S:()=>e,m:(r,i)=>{let o=r-e;return n=i?-1:dL(r-1,n),e=r,o>0?(dX(c,o),dX(s,o,i),t*o):(c.splice(o),(i?s.splice(0,-o):s.splice(o)).reduce((e,n)=>e-(-1===n?t:n),0))},$:r?void 0:e=>{let r=0,i=[];s.forEach((t,n)=>{-1!==t&&(t&&i.push(t),n<e&&r++)}),n=-1;let o=dH(i),a=o.length,u=a/2|0,d=a%2==0?(o[u-1]+o[u])/2:o[u],c=t;return((t=d)-c)*dZ(e-r,0)},q:()=>[s.slice(),t]}),m=(({i:e,l:t,u:n,h:r,v:i,p:o,t:a,S:u,m:s,$:d},c=0)=>{let l=!!d,p=!!c,f=1,h=0,m=0,_=0,g=0,b=0,y=0,v=0,w=0,k=null,z=[0,p?dZ(c-1,0):-1],S=0,E=!1,A=new Set,x=()=>_-m+b+g,T=e=>n(e)-b,j=e=>{e&&(dq()&&0!==v||k&&1===w?b+=e:g+=e)};return{_:()=>{A.clear()},I:()=>f,i:(t=200)=>{let n,r;if(!E||p)return z;if(y)[n,r]=z;else{let i=dZ(0,x()),o=i+h;l||(t=dZ(0,t),1!==v&&(i-=t),2!==v&&(o+=t)),[n,r]=z=e(dZ(0,i),dZ(0,o)),k&&(n=dL(n,k[0]),r=dZ(r,k[1]))}return[dZ(n,0),dL(r,u()-1)]},k:e=>t(e-m),R:o,u:T,h:r,C:u,T:()=>_,M:()=>0!==v,o:()=>h,O:()=>m,t:a,L:()=>(y=g,g=0,[y,2===w]),H:(e,t)=>{let n=[e,t];return A.add(n),()=>{A.delete(n)}},W:(n,u)=>{let c,z,O=0;switch(n){case 1:{if(u===_&&0===w)break;let e=y;y=0;let t=u-_,n=dD(t);e&&n<dD(e)+1||0!==w||(v=t<0?2:1),p&&(p=!1),_=u,O=4;let r=_-m;r>=-h&&r<=a()&&(O+=1,z=n>h);break}case 2:O=8,0!==v&&(c=!0,O+=1),v=0,w=0,k=null;break;case 3:{let e=u.filter(([e,t])=>!o(e,t));if(!e.length)break;for(let[t,n]of(j(e.reduce((e,[t,n])=>{let i;if(2===w)i=!0;else if(k&&1===w)i=t<k[0];else{let e=_-m,n=T(t),o=r(t);i=1!==v&&0===w?n+o<=e:n<e&&n+o<e+h}return i&&(e+=n-r(t)),e},0)),e)){let e=r(t),o=i(t,n);l&&(S+=o?n:n-e)}l&&h&&S>h&&(j(d(t(x()))),l=!1),O=3,z=!0;break}case 4:h!==u&&(h||(E=z=!0),h=u,O=3);break;case 5:u[1]?(j(s(u[0],!0)),w=2):s(u[0]),O=1;break;case 6:m=u;break;case 7:w=1;break;case 8:k=e(u,u+h),O=1}O&&(f=1+(0x7fffffff&f),c&&b&&(g+=b,b=0),A.forEach(([e,t])=>{O&e&&t(z)}))}}})(h,d);return[m,h,((e,t)=>{var n,r,i;let o,a,u,s,d=dG(),c=!1,l=t?"scrollLeft":"scrollTop",p=t?"left":"top",f=t?"overflowX":"overflowY",[h,m]=(n=()=>d[0],r=(e,t)=>{u.scrollTo({[p]:dY(e,c),behavior:t?"smooth":"instant"})},[async(t,i)=>{let a,u,s;if(!await n())return;o&&o();let d=o=()=>{a=!0,dB(u),s&&s()},c=()=>{if(!a){if(e.o()&&(dB(u),u=dJ(d,150)),i){for(let[t,n]=e.i(0);t<=n;t++)if(e.R(t))return;d()}e.W(7),r(t(),i)}},l=()=>{let t;a||(s=e.H(2,()=>{t||(t=!0,dV(()=>{t=!1,c()}))}),c())};i?(e.W(8,t()),dV(l)):l()},()=>{o&&o()}]),_=t?"width":"height",g=new WeakMap,b=(i=t=>{let n=[];for(let{target:r,contentRect:i}of t)if(r.offsetParent)if(r===u)e.W(4,i[_]);else{let e=g.get(r);null!=e&&n.push([e,i[_]])}n.length&&e.W(3,n)},{A(e){(a||(a=new e.ownerDocument.defaultView.ResizeObserver(i))).observe(e)},B(e){a.unobserve(e)},X(){a&&a.disconnect()}});return{D(n,r=n.parentElement){var i,o;let a,h,_,g,y,v,w,k,z,S,E,A,x;b.A(u=r),t&&(c="rtl"===getComputedStyle(r).direction),i=()=>dY(r[l],c),h=0,_=!1,g=!1,y=!1,v=!1,w=Date.now,k=()=>{_||g?(_=!1,z()):(y=!1,e.W(2))},z=()=>{dB(a),a=dJ(k,150)},S=()=>{h=w(),y&&(v=!0),o&&e.W(6,o()),e.W(1,i()),z()},E=n=>{if(_||!e.M()||n.ctrlKey)return;let r=w()-h;150>r&&50<r&&(t?n.deltaX:n.deltaY)&&(_=!0)},A=()=>{g=!0,y=v=!1},x=()=>{g=!1,dq()&&(y=!0)},r.addEventListener("scroll",S),r.addEventListener("wheel",E,{passive:!0}),r.addEventListener("touchstart",A,{passive:!0}),r.addEventListener("touchend",x,{passive:!0}),s={X:()=>{r.removeEventListener("scroll",S),r.removeEventListener("wheel",E),r.removeEventListener("touchstart",A),r.removeEventListener("touchend",x),dB(a)},Y:()=>{let[t,n]=e.L();t&&(((t,n,i)=>{if(i){let e=r.style,t=e[f];e[f]="hidden",dJ(()=>{e[f]=t})}let o=e.T()+t;o<=0||o>=e.O()+e.t()-e.o()?r.scrollTo({[p]:dY(o,c),behavior:"instant"}):r.scrollBy({[p]:dY(t,c),behavior:"instant"}),n&&m()})(t,n,v),v=!1,n&&e.o()>e.t()&&e.W(1,i()))}},d[1](!0)},_(){b.X(),s&&s.X(),d[1](!1),d=dG()},P:(e,t)=>(g.set(e,t),b.A(e),()=>{g.delete(e),b.B(e)}),V:h,J(){s&&s.Y()},j:e.O}})(m,f),f]},(g=(0,N.useRef)(null)).current||(g.current=_()));y!==S.C()&&S.W(5,[y,i]),s!==S.O()&&S.W(6,s);let[T,j]=(0,N.useReducer)(S.I,void 0,S.I),O=S.M(),P=S.t(),I=[],$=e=>{let t,n=b(e);return(0,uY.jsx)(d2,{Z:A.P,ee:e,te:S.u(e),ne:S.R(e),oe:l,N:n,re:x,ie:w[d3]},null!=(t=n.key)?t:"_"+e)};if(d0(()=>{w[d3]=!1,S.H(1,e=>{e?(0,dR.flushSync)(j):j()}),S.H(4,()=>{k.current&&k.current(S.T())}),S.H(8,()=>{z.current&&z.current()});let e=v[d3];return p?dV(()=>{p.current&&A.D(e,p[d3])}):A.D(e),()=>{S._(),A._()}},[]),d0(()=>{A.J()},[T]),(0,N.useImperativeHandle)(m,()=>({get cache(){return E.q()},get scrollOffset(){return S.T()},get scrollSize(){return dZ(S.t(),S.o())},get viewportSize(){return S.o()},findItemIndex:S.k,getItemOffset:S.u,getItemSize:S.h,scrollToIndex:(e,t)=>((e,t,n,{align:r,smooth:i,offset:o=0}={})=>{if(n=dF(n,0,t.C()-1),"nearest"===r){let e=t.u(n),i=t.T();if(e<i)r="start";else{if(!(e+t.h(n)>i+t.o()))return;r="end"}}e.V(()=>o+e.j()+t.u(n)+("end"===r?t.h(n)-t.o():"center"===r?(t.h(n)-t.o())/2:0),i)})(A,S,e,t),scrollTo:e=>dQ(A,e),scrollBy:e=>{dQ(A,e+S.T())}}),[]),a){let e=new Set(a);for(let[t,r]=S.i(n);t<=r;t++)e.add(t);dH([...e]).forEach(e=>{I.push($(e))})}else for(let[e,t]=S.i(n);e<=t;e++)I.push($(e));return(0,uY.jsx)(c,{ref:v,style:{contain:"size style",overflowAnchor:"none",flex:"none",position:"relative",width:x?P:"100%",height:x?"100%":P,pointerEvents:O?"none":void 0},children:I})});let d4={MessageItem:e=>{let{message:t,children:n,depth:r,index:i,typingOpts:o={chunkSize:2,delay:40}}=e,a=dC(),{updateMessageTypeWritingCount:u}=dN(),s=L(t),[d,c]=(0,N.useState)(null),l=(0,N.useRef)(null);function p(){l.current&&(clearTimeout(l.current),l.current=null,setTimeout(()=>{u("sub")}))}F(()=>{p()}),(0,N.useEffect)(()=>{if(t.isGenerating){let e;c(t.swipes[t.activeSwipeId].content.length),u("add"),p(),e=Date.now(),l.current=setInterval(()=>{c(t=>{let n,r,i=(r=(n=Date.now())-e,e=n,Math.round(r/o.delay)*o.chunkSize),a=s.current,u=a.swipes[a.activeSwipeId].content.length,d=Math.min((t||0)+i,u);return a.isGenerating||u!==d?d:(p(),null)})},o.delay)}},[t.isGenerating]);let f=(0,N.useRef)(null);(0,N.useEffect)(()=>{let e=t.swipes[t.activeSwipeId].content.length;if(f.current){let{length:t}=f.current;0===t&&c(e)}f.current={length:e}},[t]),(0,N.useEffect)(()=>{"user_stopped"===t.finishReason&&c(t.swipes[t.activeSwipeId].content.length)},[t.finishReason]);let{data:h}=ek(async()=>null!==d?{...t,swipes:t.swipes.map((e,n)=>n!==t.activeSwipeId?e:{...e,content:e.content.slice(0,d)})}:await a.ai.chat.renderMessage({message:t,depth:r,index:i}),{refreshDeps:[t,d]});return n(h||t,t)},MessageList:(0,N.forwardRef)((e,t)=>{let{children:n,messages:r,vListProps:i}=e;return(0,uY.jsx)(d6,{...i,data:r,ref:t,children:(e,t)=>n({message:e,index:t,depth:r.length-1-t})})})};var d9=Array.isArray,d5="object"==typeof global&&global&&global.Object===Object&&global,d7="object"==typeof self&&self&&self.Object===Object&&self,ce=d5||d7||Function("return this")(),ct=ce.Symbol,cn=Object.prototype,cr=cn.hasOwnProperty,ci=cn.toString,co=ct?ct.toStringTag:void 0;let ca=function(e){var t=cr.call(e,co),n=e[co];try{e[co]=void 0;var r=!0}catch(e){}var i=ci.call(e);return r&&(t?e[co]=n:delete e[co]),i};var cu=Object.prototype.toString,cs=ct?ct.toStringTag:void 0;let cd=function(e){return null==e?void 0===e?"[object Undefined]":"[object Null]":cs&&cs in Object(e)?ca(e):cu.call(e)},cc=function(e){return"symbol"==typeof e||null!=e&&"object"==typeof e&&"[object Symbol]"==cd(e)};var cl=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,cp=/^\w*$/;let cf=function(e,t){if(d9(e))return!1;var n=typeof e;return!!("number"==n||"symbol"==n||"boolean"==n||null==e||cc(e))||cp.test(e)||!cl.test(e)||null!=t&&e in Object(t)},ch=function(e){var t=typeof e;return null!=e&&("object"==t||"function"==t)},cm=function(e){if(!ch(e))return!1;var t=cd(e);return"[object Function]"==t||"[object GeneratorFunction]"==t||"[object AsyncFunction]"==t||"[object Proxy]"==t};var c_=ce["__core-js_shared__"],cg=(y=/[^.]+$/.exec(c_&&c_.keys&&c_.keys.IE_PROTO||""))?"Symbol(src)_1."+y:"",cb=Function.prototype.toString;let cy=function(e){if(null!=e){try{return cb.call(e)}catch(e){}try{return e+""}catch(e){}}return""};var cv=/^\[object .+?Constructor\]$/,cw=Object.prototype,ck=Function.prototype.toString,cz=cw.hasOwnProperty,cS=RegExp("^"+ck.call(cz).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");let cE=function(e){return!!ch(e)&&(!cg||!(cg in e))&&(cm(e)?cS:cv).test(cy(e))},cA=function(e,t){var n=null==e?void 0:e[t];return cE(n)?n:void 0};var cx=cA(Object,"create"),cT=Object.prototype.hasOwnProperty,cj=Object.prototype.hasOwnProperty;function cO(e){var t=-1,n=null==e?0:e.length;for(this.clear();++t<n;){var r=e[t];this.set(r[0],r[1])}}cO.prototype.clear=function(){this.__data__=cx?cx(null):{},this.size=0},cO.prototype.delete=function(e){var t=this.has(e)&&delete this.__data__[e];return this.size-=!!t,t},cO.prototype.get=function(e){var t=this.__data__;if(cx){var n=t[e];return"__lodash_hash_undefined__"===n?void 0:n}return cT.call(t,e)?t[e]:void 0},cO.prototype.has=function(e){var t=this.__data__;return cx?void 0!==t[e]:cj.call(t,e)},cO.prototype.set=function(e,t){var n=this.__data__;return this.size+=+!this.has(e),n[e]=cx&&void 0===t?"__lodash_hash_undefined__":t,this};let cP=function(e,t){for(var n,r=e.length;r--;)if((n=e[r][0])===t||n!=n&&t!=t)return r;return -1};var cI=Array.prototype.splice;function c$(e){var t=-1,n=null==e?0:e.length;for(this.clear();++t<n;){var r=e[t];this.set(r[0],r[1])}}c$.prototype.clear=function(){this.__data__=[],this.size=0},c$.prototype.delete=function(e){var t=this.__data__,n=cP(t,e);return!(n<0)&&(n==t.length-1?t.pop():cI.call(t,n,1),--this.size,!0)},c$.prototype.get=function(e){var t=this.__data__,n=cP(t,e);return n<0?void 0:t[n][1]},c$.prototype.has=function(e){return cP(this.__data__,e)>-1},c$.prototype.set=function(e,t){var n=this.__data__,r=cP(n,e);return r<0?(++this.size,n.push([e,t])):n[r][1]=t,this};var cM=cA(ce,"Map");let cC=function(e){var t=typeof e;return"string"==t||"number"==t||"symbol"==t||"boolean"==t?"__proto__"!==e:null===e},cN=function(e,t){var n=e.__data__;return cC(t)?n["string"==typeof t?"string":"hash"]:n.map};function cR(e){var t=-1,n=null==e?0:e.length;for(this.clear();++t<n;){var r=e[t];this.set(r[0],r[1])}}function cL(e,t){if("function"!=typeof e||null!=t&&"function"!=typeof t)throw TypeError("Expected a function");var n=function(){var r=arguments,i=t?t.apply(this,r):r[0],o=n.cache;if(o.has(i))return o.get(i);var a=e.apply(this,r);return n.cache=o.set(i,a)||o,a};return n.cache=new(cL.Cache||cR),n}cR.prototype.clear=function(){this.size=0,this.__data__={hash:new cO,map:new(cM||c$),string:new cO}},cR.prototype.delete=function(e){var t=cN(this,e).delete(e);return this.size-=!!t,t},cR.prototype.get=function(e){return cN(this,e).get(e)},cR.prototype.has=function(e){return cN(this,e).has(e)},cR.prototype.set=function(e,t){var n=cN(this,e),r=n.size;return n.set(e,t),this.size+=+(n.size!=r),this},cL.Cache=cR;var cZ=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,cD=/\\(\\)?/g,cU=(w=(v=cL(function(e){var t=[];return 46===e.charCodeAt(0)&&t.push(""),e.replace(cZ,function(e,n,r,i){t.push(r?i.replace(cD,"$1"):n||e)}),t},function(e){return 500===w.size&&w.clear(),e})).cache,v);let cF=function(e,t){for(var n=-1,r=null==e?0:e.length,i=Array(r);++n<r;)i[n]=t(e[n],n,e);return i};var cH=1/0,cJ=ct?ct.prototype:void 0,cB=cJ?cJ.toString:void 0;let cV=function e(t){if("string"==typeof t)return t;if(d9(t))return cF(t,e)+"";if(cc(t))return cB?cB.call(t):"";var n=t+"";return"0"==n&&1/t==-cH?"-0":n},cG=function(e,t){return d9(e)?e:cf(e,t)?[e]:cU(null==e?"":cV(e))};var cW=1/0;let cq=function(e){if("string"==typeof e||cc(e))return e;var t=e+"";return"0"==t&&1/e==-cW?"-0":t},cK=function(e,t){t=cG(t,e);for(var n=0,r=t.length;null!=e&&n<r;)e=e[cq(t[n++])];return n&&n==r?e:void 0},cX=function(e,t,n){var r=null==e?void 0:cK(e,t);return void 0===r?n:r};function cY({projectInfo:e,activePersona:r,onSend:i,messages:d,setSwipe:c}){(0,N.useEffect)(()=>{n=e??void 0,o=r??void 0},[e,r]),(0,N.useEffect)(()=>{t=async e=>{await i(e)},a=async e=>{if(d.length>1)throw Error("已有新消息,不允许切换开场白");if(0===d.length)throw Error("断言失败:没有开场消息,无法切换开场白");let t=Math.floor(e)-1,n=d[0].swipes.length;t<0||t>=n||c(d[0].id,t)},s=(e,t)=>d.find(t=>t.id===e)?.swipes[t]?.mjv?.value,u=(e,t,n)=>{let r=s?.(e,t);if(r)return cX(r,n)}},[i,d,c])}window.$mj_engine={ai:{chat:{get complete(){return t},get project(){return n},settings:{get persona(){return o}},get setFirstMesIndex(){return a}}},bind:({messageId:e,swipeId:r})=>({ai:{chat:{mjv:{get:t=>u?.(e,r,t),getAll:()=>s?.(e,r)??{}},get complete(){return t},get project(){return n},settings:{get persona(){return o}},get setFirstMesIndex(){return a}}}})};var cQ=Symbol.for("immer-nothing"),c0=Symbol.for("immer-draftable"),c3=Symbol.for("immer-state");function c8(e){throw Error(`[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`)}var c1=Object,c2=c1.getPrototypeOf,c6="constructor",c4="prototype",c9="configurable",c5="enumerable",c7="writable",le="value",lt=e=>!!e&&!!e[c3];function ln(e){return!!e&&(lo(e)||ll(e)||!!e[c0]||!!e[c6]?.[c0]||lp(e)||lf(e))}var lr=c1[c4][c6].toString(),li=new WeakMap;function lo(e){if(!e||!lh(e))return!1;let t=c2(e);if(null===t||t===c1[c4])return!0;let n=c1.hasOwnProperty.call(t,c6)&&t[c6];if(n===Object)return!0;if(!lm(n))return!1;let r=li.get(n);return void 0===r&&(r=Function.toString.call(n),li.set(n,r)),r===lr}function la(e,t,n=!0){0===lu(e)?(n?Reflect.ownKeys(e):c1.keys(e)).forEach(n=>{t(n,e[n],e)}):e.forEach((n,r)=>t(r,n,e))}function lu(e){let t=e[c3];return t?t.type_:ll(e)?1:lp(e)?2:3*!!lf(e)}var ls=(e,t,n=lu(e))=>2===n?e.has(t):c1[c4].hasOwnProperty.call(e,t),ld=(e,t,n=lu(e))=>2===n?e.get(t):e[t],lc=(e,t,n,r=lu(e))=>{2===r?e.set(t,n):3===r?e.add(n):e[t]=n},ll=Array.isArray,lp=e=>e instanceof Map,lf=e=>e instanceof Set,lh=e=>"object"==typeof e,lm=e=>"function"==typeof e,l_=e=>e.modified_?e.copy_:e.base_;function lg(e,t){if(lp(e))return new Map(e);if(lf(e))return new Set(e);if(ll(e))return Array[c4].slice.call(e);let n=lo(e);if(!0!==t&&("class_only"!==t||n)){let t=c2(e);if(null!==t&&n)return{...e};let r=c1.create(t);return c1.assign(r,e)}{let t=c1.getOwnPropertyDescriptors(e);delete t[c3];let n=Reflect.ownKeys(t);for(let r=0;r<n.length;r++){let i=n[r],o=t[i];!1===o[c7]&&(o[c7]=!0,o[c9]=!0),(o.get||o.set)&&(t[i]={[c9]:!0,[c7]:!0,[c5]:o[c5],[le]:e[i]})}return c1.create(c2(e),t)}}function lb(e,t=!1){return lv(e)||lt(e)||!ln(e)||(lu(e)>1&&c1.defineProperties(e,{set:ly,add:ly,clear:ly,delete:ly}),c1.freeze(e),t&&la(e,(e,t)=>{lb(t,!0)},!1)),e}var ly={[le]:function(){c8(2)}};function lv(e){return!(null!==e&&lh(e))||c1.isFrozen(e)}var lw="MapSet",lk="Patches",lz="ArrayMethods",lS={};function lE(e){let t=lS[e];return t||c8(0,e),t}var lA=e=>!!lS[e];function lx(e,t){t&&(e.patchPlugin_=lE(lk),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function lT(e){lj(e),e.drafts_.forEach(lP),e.drafts_=null}function lj(e){e===E&&(E=e.parent_)}var lO=e=>E={drafts_:[],parent_:E,immer_:e,canAutoFreeze_:!0,unfinalizedDrafts_:0,handledSet_:new Set,processedForPatches_:new Set,mapSetPlugin_:lA(lw)?lE(lw):void 0,arrayMethodsPlugin_:lA(lz)?lE(lz):void 0};function lP(e){let t=e[c3];0===t.type_||1===t.type_?t.revoke_():t.revoked_=!0}function lI(e,t){t.unfinalizedDrafts_=t.drafts_.length;let n=t.drafts_[0];if(void 0!==e&&e!==n){n[c3].modified_&&(lT(t),c8(4)),ln(e)&&(e=l$(t,e));let{patchPlugin_:r}=t;r&&r.generateReplacementPatches_(n[c3].base_,e,t)}else e=l$(t,n);return function(e,t,n=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&lb(t,n)}(t,e,!0),lT(t),t.patches_&&t.patchListener_(t.patches_,t.inversePatches_),e!==cQ?e:void 0}function l$(e,t){if(lv(t))return t;let n=t[c3];if(!n)return lZ(t,e.handledSet_,e);if(!lC(n,e))return t;if(!n.modified_)return n.base_;if(!n.finalized_){let{callbacks_:t}=n;if(t)for(;t.length>0;)t.pop()(e);lL(n,e)}return n.copy_}function lM(e){e.finalized_=!0,e.scope_.unfinalizedDrafts_--}var lC=(e,t)=>e.scope_===t,lN=[];function lR(e,t,n,r){let i=e.copy_||e.base_,o=e.type_;if(void 0!==r&&ld(i,r,o)===t)return void lc(i,r,n,o);if(!e.draftLocations_){let t=e.draftLocations_=new Map;la(i,(e,n)=>{if(lt(n)){let r=t.get(n)||[];r.push(e),t.set(n,r)}})}for(let r of e.draftLocations_.get(t)??lN)lc(i,r,n,o)}function lL(e,t){if(e.modified_&&!e.finalized_&&(3===e.type_||1===e.type_&&e.allIndicesReassigned_||(e.assigned_?.size??0)>0)){let{patchPlugin_:n}=t;if(n){let r=n.getPath(e);r&&n.generatePatches_(e,r,t)}lM(e)}}function lZ(e,t,n){return!n.immer_.autoFreeze_&&n.unfinalizedDrafts_<1||lt(e)||t.has(e)||!ln(e)||lv(e)||(t.add(e),la(e,(r,i)=>{if(lt(i)){let t=i[c3];lC(t,n)&&(lc(e,r,l_(t),e.type_),lM(t))}else ln(i)&&lZ(i,t,n)})),e}var lD={get(e,t){let n;if(t===c3)return e;let r=e.scope_.arrayMethodsPlugin_,i=1===e.type_&&"string"==typeof t;if(i&&r?.isArrayOperationMethod(t))return r.createMethodInterceptor(e,t);let o=e.copy_||e.base_;if(!ls(o,t,e.type_)){var a;let n;return a=e,(n=lH(o,t))?le in n?n[le]:n.get?.call(a.draft_):void 0}let u=o[t];if(e.finalized_||!ln(u)||i&&e.operationMethod&&r?.isMutatingArrayMethod(e.operationMethod)&&Number.isInteger(n=+t)&&String(n)===t)return u;if(u===lF(e.base_,t)){lB(e);let n=1===e.type_?+t:t,r=lV(e.scope_,u,e,n);return e.copy_[n]=r}return u},has:(e,t)=>t in(e.copy_||e.base_),ownKeys:e=>Reflect.ownKeys(e.copy_||e.base_),set(e,t,n){let r=lH(e.copy_||e.base_,t);if(r?.set)return r.set.call(e.draft_,n),!0;if(!e.modified_){let r=lF(e.copy_||e.base_,t),i=r?.[c3];if(i&&i.base_===n)return e.copy_[t]=n,e.assigned_.set(t,!1),!0;if((n===r?0!==n||1/n==1/r:n!=n&&r!=r)&&(void 0!==n||ls(e.base_,t,e.type_)))return!0;lB(e),lJ(e)}return!!(e.copy_[t]===n&&(void 0!==n||t in e.copy_)||Number.isNaN(n)&&Number.isNaN(e.copy_[t]))||(e.copy_[t]=n,e.assigned_.set(t,!0),!function(e,t,n){let{scope_:r}=e;if(lt(n)){let i=n[c3];lC(i,r)&&i.callbacks_.push(function(){lB(e),lR(e,n,l_(i),t)})}else ln(n)&&e.callbacks_.push(function(){let i=e.copy_||e.base_;3===e.type_?i.has(n)&&lZ(n,r.handledSet_,r):ld(i,t,e.type_)===n&&r.drafts_.length>1&&(e.assigned_.get(t)??!1)===!0&&e.copy_&&lZ(ld(e.copy_,t,e.type_),r.handledSet_,r)})}(e,t,n),!0)},deleteProperty:(e,t)=>(lB(e),void 0!==lF(e.base_,t)||t in e.base_?(e.assigned_.set(t,!1),lJ(e)):e.assigned_.delete(t),e.copy_&&delete e.copy_[t],!0),getOwnPropertyDescriptor(e,t){let n=e.copy_||e.base_,r=Reflect.getOwnPropertyDescriptor(n,t);return r?{[c7]:!0,[c9]:1!==e.type_||"length"!==t,[c5]:r[c5],[le]:n[t]}:r},defineProperty(){c8(11)},getPrototypeOf:e=>c2(e.base_),setPrototypeOf(){c8(12)}},lU={};for(let e in lD){let t=lD[e];lU[e]=function(){let e=arguments;return e[0]=e[0][0],t.apply(this,e)}}function lF(e,t){let n=e[c3];return(n?n.copy_||n.base_:e)[t]}function lH(e,t){if(!(t in e))return;let n=c2(e);for(;n;){let e=Object.getOwnPropertyDescriptor(n,t);if(e)return e;n=c2(n)}}function lJ(e){!e.modified_&&(e.modified_=!0,e.parent_&&lJ(e.parent_))}function lB(e){e.copy_||(e.assigned_=new Map,e.copy_=lg(e.base_,e.scope_.immer_.useStrictShallowCopy_))}function lV(e,t,n,r){let[i,o]=lp(t)?lE(lw).proxyMap_(t,n):lf(t)?lE(lw).proxySet_(t,n):function(e,t){let n=ll(e),r={type_:+!!n,scope_:t?t.scope_:E,modified_:!1,finalized_:!1,assigned_:void 0,parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1,callbacks_:void 0},i=r,o=lD;n&&(i=[r],o=lU);let{revoke:a,proxy:u}=Proxy.revocable(i,o);return r.draft_=u,r.revoke_=a,[u,r]}(t,n);if((n?.scope_??E).drafts_.push(i),o.callbacks_=n?.callbacks_??[],o.key_=r,n&&void 0!==r)n.callbacks_.push(function(e){if(!o||!lC(o,e))return;e.mapSetPlugin_?.fixSetContents(o);let t=l_(o);lR(n,o.draft_??o,t,r),lL(o,e)});else o.callbacks_.push(function(e){e.mapSetPlugin_?.fixSetContents(o);let{patchPlugin_:t}=e;o.modified_&&t&&t.generatePatches_(o,[],e)});return i}lU.deleteProperty=function(e,t){return lU.set.call(this,e,t,void 0)},lU.set=function(e,t,n){return lD.set.call(this,e[0],t,n,e[0])};var lG=new class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.useStrictIteration_=!1,this.produce=(e,t,n)=>{let r;if(lm(e)&&!lm(t)){let n=t;t=e;let r=this;return function(e=n,...i){return r.produce(e,e=>t.call(this,e,...i))}}if(lm(t)||c8(6),void 0===n||lm(n)||c8(7),ln(e)){let i=lO(this),o=lV(i,e,void 0),a=!0;try{r=t(o),a=!1}finally{a?lT(i):lj(i)}return lx(i,n),lI(r,i)}if(e&&lh(e))c8(1,e);else{if(void 0===(r=t(e))&&(r=e),r===cQ&&(r=void 0),this.autoFreeze_&&lb(r,!0),n){let t=[],i=[];lE(lk).generateReplacementPatches_(e,r,{patches_:t,inversePatches_:i}),n(t,i)}return r}},this.produceWithPatches=(e,t)=>{let n,r;return lm(e)?(t,...n)=>this.produceWithPatches(t,t=>e(t,...n)):[this.produce(e,t,(e,t)=>{n=e,r=t}),n,r]},"boolean"==typeof e?.autoFreeze&&this.setAutoFreeze(e.autoFreeze),"boolean"==typeof e?.useStrictShallowCopy&&this.setUseStrictShallowCopy(e.useStrictShallowCopy),"boolean"==typeof e?.useStrictIteration&&this.setUseStrictIteration(e.useStrictIteration)}createDraft(e){var t;ln(e)||c8(8),lt(e)&&(lt(t=e)||c8(10,t),e=function e(t){let n;if(!ln(t)||lv(t))return t;let r=t[c3],i=!0;if(r){if(!r.modified_)return r.base_;r.finalized_=!0,n=lg(t,r.scope_.immer_.useStrictShallowCopy_),i=r.scope_.immer_.shouldUseStrictIteration()}else n=lg(t,!0);return la(n,(t,r)=>{lc(n,t,e(r))},i),r&&(r.finalized_=!1),n}(t));let n=lO(this),r=lV(n,e,void 0);return r[c3].isManual_=!0,lj(n),r}finishDraft(e,t){let n=e&&e[c3];n&&n.isManual_||c8(9);let{scope_:r}=n;return lx(r,t),lI(void 0,r)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}setUseStrictIteration(e){this.useStrictIteration_=e}shouldUseStrictIteration(){return this.useStrictIteration_}applyPatches(e,t){let n;for(n=t.length-1;n>=0;n--){let r=t[n];if(0===r.path.length&&"replace"===r.op){e=r.value;break}}n>-1&&(t=t.slice(n+1));let r=lE(lk).applyPatches_;return lt(e)?r(e,t):this.produce(e,e=>r(e,t))}}().produce;let lW=["network error","NetworkError when attempting to fetch resource.","The Internet connection appears to be offline.","Network request failed","Load failed","Failed to fetch"];function lq(e){return lW.some(t=>e.includes(t))}async function*lK(e,t,n="generate",r,i){let o=[],a=null,u=!1,s=null;try{let d=e=>{!s&&(o.push(e),a&&(a(),a=null))},c=()=>{u=!0,a&&(a(),a=null)},l=e=>{s=e,a&&(a(),a=null)};if("generate"===n)e.ai.chat.complete(t,r,d).then(c).catch(l);else if("regenerate"===n)e.ai.chat.regenerate(r,d).then(c).catch(l);else if("continue"===n)e.ai.chat.continue(r,d).then(c).catch(l);else{if(!i)throw Error("call recover without assisMsgId");e.ai.chat.recover(r,i,d).then(c).catch(l)}}catch(e){s=e,u=!0}for(;;)if(o.length>0)yield o.shift();else if(u)break;else if(s)throw s;else await new Promise(e=>{a=e})}let lX=aF({message:ov()});function lY(e){let{success:t}=lX.safeParse(e);return t}let lQ="not_saved",l0=({body:e,onFinish:t,pageSize:n=10})=>{let[r,i]=(0,N.useState)(""),o=(0,N.useRef)(0),a=dC(),u=(0,N.useRef)(!1),{data:s,loadMoreAsync:d,loadingMore:c,loading:l,mutate:p,error:f,reloadAsync:h}=eS(async e=>{if(u.current=!0,e?.noMore)return{noMore:!0,list:[]};try{let t=await a.ai.chat.message.getPage(e?.cursor,n);o.current=t.conversationVersion;let r=t.messages.at(-1);return r?.isGenerating&&z(r.id,o.current),{cursor:t.nextCursor,list:t.messages,noMore:!t.nextCursor}}finally{u.current=!1}},{direction:"top"}),m=async()=>{await h()},_=async()=>{if(!u.current)try{u.current=!0,await d()}finally{u.current=!1}},g=s?.list??[],b=L(s),y=(0,N.useRef)(g);y.current=g;let v=e=>{let t;t="function"==typeof e?e(y.current):e,y.current=t,p({...b.current,list:t})},{chatStreaming:w,isStreaming:k,recoverStreaming:z}=(({common:e,setMessages:t,getMessages:n,setInput:r,mujian:i,conversationVersion:o})=>{let{body:a}=e,[u,s]=(0,N.useState)(!1);return{chatStreaming:(0,N.useCallback)(async({query:e,regenerate:a,is_continue:u,conversationVersion:d})=>{let c=n();try{s(!0);let n={id:`${lQ}_user_${Date.now()}`,isUser:!0,swipes:[{content:e??""}],activeSwipeId:0,isGenerating:!1},r={id:`${lQ}_assistant_${Date.now()}`,isUser:!1,swipes:[{content:""}],activeSwipeId:0,isGenerating:!0};for await(let s of(a?t(lG(e=>{let t=e.at(-1);t.activeSwipeId=t.swipes.length,t.swipes.push({content:""}),t.isGenerating=!0,t.finishReason=void 0,t.error=void 0})):u?t(e=>[...e,r]):t(e=>[...e,n,r]),lK(i,e||"",u?"continue":a?"regenerate":"generate",d))){if(!s)return;"meta"===s.type?(a||t(lG(e=>{u||(e[e.length-2].id=s.userMsgId),e[e.length-1].id=s.assisMsgId})),o.current=s.newConversationVersion):"data"===s.type&&(t(lG(e=>{let t=e.at(-1);t.swipes[t.activeSwipeId]={content:s.content,mjv:s.mjv},s.finishReason&&(t.finishReason=s.finishReason),s.error&&(t.error=s.error)})),await new Promise(e=>setTimeout(e,10)))}}catch(e){sX.e("Stream error",e),t(lG(t=>{let n=t.at(-1);lY(e)&&lq(e.message)?n.error={code:-1e4,message:"E_NETWORK_ERROR"}:n.error={code:0x3b9aca05,message:String(e)}}))}finally{t(lG(n=>{let i=n[n.length-1];if(i.isGenerating=!1,i.error?.code===0x77dde1f9){let e=c.find(e=>e.id===i.id);e?(i.swipes=e.swipes,i.activeSwipeId=e.activeSwipeId):(i.activeSwipeId-=1,i.swipes.length=i.swipes.length-1)}else i.error?.code===0x77dde1f1&&(t(c),e&&r(e))})),s(!1)}},[a,i]),isStreaming:u,recoverStreaming:(0,N.useCallback)(async(e,n)=>{let r=lK(i,"","recover",n,e);try{for await(let n of r){if(!n)return;"data"===n.type&&t(lG(t=>{let r=t.find(t=>t.id===e);r&&(r.swipes[r.activeSwipeId]={content:n.content,mjv:n.mjv},n.finishReason&&(r.finishReason=n.finishReason),n.error&&(r.error=n.error))}))}}catch(e){sX.e("Stream error",e),t(lG(t=>{let n=t.at(-1);lY(e)&&lq(e.message)?n.error={code:-1e4,message:"E_NETWORK_ERROR"}:n.error={code:0x3b9aca05,message:String(e)}}))}finally{t(lG(t=>{let n=t.find(t=>t.id===e);n&&(n.isGenerating=!1)}))}},[i])}})({common:{body:e},setMessages:v,getMessages:(0,N.useCallback)(()=>y.current,[y]),setInput:i,mujian:a,conversationVersion:o});G(()=>{if(!k){let e=g[g.length-1];t?.(e)}},[k]);let S=(0,N.useCallback)(async e=>{if(k)throw Error("useChat is streaming already.");await w({query:e,conversationVersion:o.current})},[k,w]),E=(0,N.useCallback)(async()=>{if(k)throw Error("useChat is streaming already.");let e=g.findLast(e=>e.isUser),t=e?e.swipes[e.activeSwipeId].content:"";await w({query:t,regenerate:!0,conversationVersion:o.current})},[k,g,w]),A=(0,N.useCallback)(async()=>{if(k)throw Error("useChat is streaming already.");await w({query:"",is_continue:!0,conversationVersion:o.current})},[k,w]),x=async(e,t)=>{if(t<0)return;v(lG(n=>{let r=n.find(t=>t.id===e);r&&(r.activeSwipeId=t)}));let n=await a.ai.chat.message.swipe(e,t),{data:r,success:i}=aF({newConversationVersion:as()}).safeParse(n);i&&r.newConversationVersion&&(o.current=r.newConversationVersion)},T=async(e,t)=>{let n=await a.ai.chat.message.editOne(e,t),{data:r,success:i}=aF({newConversationVersion:as()}).safeParse(n);i&&r.newConversationVersion&&(o.current=r.newConversationVersion),v(lG(n=>{let r=n.find(t=>t.id===e);r&&(r.swipes[r.activeSwipeId].content=t)}))},j=async e=>{if(!e.startsWith(lQ))try{let t=await a.ai.chat.message.deleteOne(e),{data:n,success:r}=aF({newConversationVersion:as()}).safeParse(t);r&&n.newConversationVersion&&(o.current=n.newConversationVersion)}catch(e){if(e?.code!==0x77dde1f4)throw e}v(t=>t.filter(t=>t.id!==e))};return{append:S,regenerate:E,continueGenerate:A,stop:()=>{let e=g.at(-1)?.id;e&&a.ai.chat.stop(o.current,e).then(()=>{v(lG(e=>{let t=e.at(-1);t?.finishReason&&(t.finishReason="user_stopped")}))})},messages:g,status:k?"streaming":"ready",setMessages:v,input:r,setInput:i,handleSubmit:()=>{S(r)},setSwipe:x,editMessage:T,deleteMessage:j,loadMoreMessage:_,messagesStatus:f?"error":c||l?"loading":s?.noMore?"all-loaded":"has-more",refresh:m}}})(),i})());
|