@mujian/js-sdk 0.0.6-beta.9 → 0.0.6-beta.mjv.67

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.
Files changed (35) hide show
  1. package/dist/events/index.d.ts +30 -2
  2. package/dist/index.d.ts +13 -39
  3. package/dist/index.js +323 -15
  4. package/dist/modules/ai/chat/chat.d.ts +46 -4
  5. package/dist/modules/ai/chat/index.d.ts +1 -1
  6. package/dist/modules/ai/chat/message/index.d.ts +4 -0
  7. package/dist/modules/ai/index.d.ts +3 -2
  8. package/dist/modules/ai/openai/chat.d.ts +25 -0
  9. package/dist/modules/ai/openai/completions.d.ts +19 -0
  10. package/dist/modules/ai/openai/images.d.ts +19 -0
  11. package/dist/modules/ai/openai/index.d.ts +4 -0
  12. package/dist/modules/ai/openai/responses.d.ts +20 -0
  13. package/dist/modules/ai/text/index.d.ts +9 -2
  14. package/dist/modules/utils/clipboard.d.ts +5 -0
  15. package/dist/modules/utils/index.d.ts +4 -0
  16. package/dist/react/chat/useChat/index.d.ts +7 -3
  17. package/dist/react/chat/useChat/inner/chatStreaming.d.ts +1 -1
  18. package/dist/react/chat/useChat/message.d.ts +25 -13
  19. package/dist/react/components/MdRenderer/index.d.ts +4 -3
  20. package/dist/react/components/MdRenderer/utils/height.d.ts +0 -0
  21. package/dist/react/components/MdRenderer/utils/iframe.d.ts +9 -0
  22. package/dist/react/components/MdRenderer/utils/scripts.d.ts +4 -0
  23. package/dist/react/components/MujianSpinner/index.d.ts +7 -0
  24. package/dist/react/components/index.d.ts +1 -0
  25. package/dist/react.css +65 -4
  26. package/dist/react.js +687 -112
  27. package/dist/types/index.d.ts +38 -0
  28. package/dist/umd/index.js +792 -0
  29. package/dist/umd/index.js.LICENSE.txt +7 -0
  30. package/dist/umd/react.css +343 -0
  31. package/dist/umd/react.js +8881 -0
  32. package/dist/umd/react.js.LICENSE.txt +31 -0
  33. package/dist/utils/log.d.ts +4 -0
  34. package/package.json +4 -1
  35. /package/dist/react/components/MdRenderer/{utils.d.ts → utils/md.d.ts} +0 -0
@@ -0,0 +1,792 @@
1
+ /*! For license information please see index.js.LICENSE.txt */
2
+ (function(root, factory) {
3
+ if ('object' == typeof exports && 'object' == typeof module) module.exports = factory();
4
+ else if ('function' == typeof define && define.amd) define([], factory);
5
+ else if ('object' == typeof exports) exports["MujianUMD"] = factory();
6
+ else root["MujianUMD"] = factory();
7
+ })(globalThis, ()=>(()=>{
8
+ "use strict";
9
+ var __webpack_require__ = {};
10
+ (()=>{
11
+ __webpack_require__.d = (exports1, definition)=>{
12
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
13
+ enumerable: true,
14
+ get: definition[key]
15
+ });
16
+ };
17
+ })();
18
+ (()=>{
19
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
20
+ })();
21
+ (()=>{
22
+ __webpack_require__.r = (exports1)=>{
23
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
24
+ value: 'Module'
25
+ });
26
+ Object.defineProperty(exports1, '__esModule', {
27
+ value: true
28
+ });
29
+ };
30
+ })();
31
+ var __webpack_exports__ = {};
32
+ __webpack_require__.r(__webpack_exports__);
33
+ __webpack_require__.d(__webpack_exports__, {
34
+ MujianSdk: ()=>src_MujianSdk,
35
+ EVENT: ()=>events_EVENT
36
+ });
37
+ /**
38
+ postmate - A powerful, simple, promise-based postMessage library
39
+ @version v1.5.2
40
+ @link https://github.com/dollarshaveclub/postmate
41
+ @author Jacob Kelley <jakie8@gmail.com>
42
+ @license MIT
43
+ **/ var messageType = 'application/x-postmate-v1+json';
44
+ var maxHandshakeRequests = 5;
45
+ var _messageId = 0;
46
+ var postmate_es_generateNewMessageId = function() {
47
+ return ++_messageId;
48
+ };
49
+ var postmate_es_resolveOrigin = function(url) {
50
+ var a = document.createElement('a');
51
+ a.href = url;
52
+ var protocol = a.protocol.length > 4 ? a.protocol : window.location.protocol;
53
+ var host = a.host.length ? '80' === a.port || '443' === a.port ? a.hostname : a.host : window.location.host;
54
+ return a.origin || protocol + "//" + host;
55
+ };
56
+ var messageTypes = {
57
+ handshake: 1,
58
+ 'handshake-reply': 1,
59
+ call: 1,
60
+ emit: 1,
61
+ reply: 1,
62
+ request: 1
63
+ };
64
+ var postmate_es_sanitize = function(message, allowedOrigin) {
65
+ if ('string' == typeof allowedOrigin && message.origin !== allowedOrigin) return false;
66
+ if (!message.data) return false;
67
+ if ('object' == typeof message.data && !('postmate' in message.data)) return false;
68
+ if (message.data.type !== messageType) return false;
69
+ if (!messageTypes[message.data.postmate]) return false;
70
+ return true;
71
+ };
72
+ var postmate_es_resolveValue = function(model, property) {
73
+ var unwrappedContext = 'function' == typeof model[property] ? model[property]() : model[property];
74
+ return postmate_es_Postmate.Promise.resolve(unwrappedContext);
75
+ };
76
+ var postmate_es_ParentAPI = /*#__PURE__*/ function() {
77
+ function ParentAPI(info) {
78
+ var _this = this;
79
+ this.parent = info.parent;
80
+ this.frame = info.frame;
81
+ this.child = info.child;
82
+ this.childOrigin = info.childOrigin;
83
+ this.events = {};
84
+ this.listener = function(e) {
85
+ if (!postmate_es_sanitize(e, _this.childOrigin)) return false;
86
+ var _ref = ((e || {}).data || {}).value || {}, data = _ref.data, name = _ref.name;
87
+ if ('emit' === e.data.postmate) {
88
+ if (name in _this.events) _this.events[name].call(_this, data);
89
+ }
90
+ };
91
+ this.parent.addEventListener('message', this.listener, false);
92
+ }
93
+ var _proto = ParentAPI.prototype;
94
+ _proto.get = function(property) {
95
+ var _this2 = this;
96
+ return new postmate_es_Postmate.Promise(function(resolve) {
97
+ var uid = postmate_es_generateNewMessageId();
98
+ var transact = function transact(e) {
99
+ if (e.data.uid === uid && 'reply' === e.data.postmate) {
100
+ _this2.parent.removeEventListener('message', transact, false);
101
+ resolve(e.data.value);
102
+ }
103
+ };
104
+ _this2.parent.addEventListener('message', transact, false);
105
+ _this2.child.postMessage({
106
+ postmate: 'request',
107
+ type: messageType,
108
+ property: property,
109
+ uid: uid
110
+ }, _this2.childOrigin);
111
+ });
112
+ };
113
+ _proto.call = function(property, data) {
114
+ this.child.postMessage({
115
+ postmate: 'call',
116
+ type: messageType,
117
+ property: property,
118
+ data: data
119
+ }, this.childOrigin);
120
+ };
121
+ _proto.on = function(eventName, callback) {
122
+ this.events[eventName] = callback;
123
+ };
124
+ _proto.destroy = function() {
125
+ window.removeEventListener('message', this.listener, false);
126
+ this.frame.parentNode.removeChild(this.frame);
127
+ };
128
+ return ParentAPI;
129
+ }();
130
+ var postmate_es_ChildAPI = /*#__PURE__*/ function() {
131
+ function ChildAPI(info) {
132
+ var _this3 = this;
133
+ this.model = info.model;
134
+ this.parent = info.parent;
135
+ this.parentOrigin = info.parentOrigin;
136
+ this.child = info.child;
137
+ this.child.addEventListener('message', function(e) {
138
+ if (!postmate_es_sanitize(e, _this3.parentOrigin)) return;
139
+ var _e$data = e.data, property = _e$data.property, uid = _e$data.uid, data = _e$data.data;
140
+ if ('call' === e.data.postmate) {
141
+ if (property in _this3.model && 'function' == typeof _this3.model[property]) _this3.model[property](data);
142
+ return;
143
+ }
144
+ postmate_es_resolveValue(_this3.model, property).then(function(value) {
145
+ return e.source.postMessage({
146
+ property: property,
147
+ postmate: 'reply',
148
+ type: messageType,
149
+ uid: uid,
150
+ value: value
151
+ }, e.origin);
152
+ });
153
+ });
154
+ }
155
+ var _proto2 = ChildAPI.prototype;
156
+ _proto2.emit = function(name, data) {
157
+ this.parent.postMessage({
158
+ postmate: 'emit',
159
+ type: messageType,
160
+ value: {
161
+ name: name,
162
+ data: data
163
+ }
164
+ }, this.parentOrigin);
165
+ };
166
+ return ChildAPI;
167
+ }();
168
+ var postmate_es_Postmate = /*#__PURE__*/ function() {
169
+ function Postmate(_ref2) {
170
+ var _ref2$container = _ref2.container, container = void 0 === _ref2$container ? void 0 !== container ? container : document.body : _ref2$container, model = _ref2.model, url = _ref2.url, name = _ref2.name, _ref2$classListArray = _ref2.classListArray, classListArray = void 0 === _ref2$classListArray ? [] : _ref2$classListArray;
171
+ this.parent = window;
172
+ this.frame = document.createElement('iframe');
173
+ this.frame.name = name || '';
174
+ this.frame.classList.add.apply(this.frame.classList, classListArray);
175
+ container.appendChild(this.frame);
176
+ this.child = this.frame.contentWindow || this.frame.contentDocument.parentWindow;
177
+ this.model = model || {};
178
+ return this.sendHandshake(url);
179
+ }
180
+ var _proto3 = Postmate.prototype;
181
+ _proto3.sendHandshake = function(url) {
182
+ var _this4 = this;
183
+ var childOrigin = postmate_es_resolveOrigin(url);
184
+ var attempt = 0;
185
+ var responseInterval;
186
+ return new Postmate.Promise(function(resolve, reject) {
187
+ var reply = function reply(e) {
188
+ if (!postmate_es_sanitize(e, childOrigin)) return false;
189
+ if ('handshake-reply' === e.data.postmate) {
190
+ clearInterval(responseInterval);
191
+ _this4.parent.removeEventListener('message', reply, false);
192
+ _this4.childOrigin = e.origin;
193
+ return resolve(new postmate_es_ParentAPI(_this4));
194
+ }
195
+ return reject('Failed handshake');
196
+ };
197
+ _this4.parent.addEventListener('message', reply, false);
198
+ var doSend = function() {
199
+ attempt++;
200
+ _this4.child.postMessage({
201
+ postmate: 'handshake',
202
+ type: messageType,
203
+ model: _this4.model
204
+ }, childOrigin);
205
+ if (attempt === maxHandshakeRequests) clearInterval(responseInterval);
206
+ };
207
+ var loaded = function() {
208
+ doSend();
209
+ responseInterval = setInterval(doSend, 500);
210
+ };
211
+ if (_this4.frame.attachEvent) _this4.frame.attachEvent('onload', loaded);
212
+ else _this4.frame.onload = loaded;
213
+ _this4.frame.src = url;
214
+ });
215
+ };
216
+ return Postmate;
217
+ }();
218
+ postmate_es_Postmate.debug = false;
219
+ postmate_es_Postmate.Promise = function() {
220
+ try {
221
+ return window ? window.Promise : Promise;
222
+ } catch (e) {
223
+ return null;
224
+ }
225
+ }();
226
+ postmate_es_Postmate.Model = /*#__PURE__*/ function() {
227
+ function Model(model) {
228
+ this.child = window;
229
+ this.model = model;
230
+ this.parent = this.child.parent;
231
+ return this.sendHandshakeReply();
232
+ }
233
+ var _proto4 = Model.prototype;
234
+ _proto4.sendHandshakeReply = function() {
235
+ var _this5 = this;
236
+ return new postmate_es_Postmate.Promise(function(resolve, reject) {
237
+ var shake = function shake(e) {
238
+ if (!e.data.postmate) return;
239
+ if ('handshake' === e.data.postmate) {
240
+ _this5.child.removeEventListener('message', shake, false);
241
+ e.source.postMessage({
242
+ postmate: 'handshake-reply',
243
+ type: messageType
244
+ }, e.origin);
245
+ _this5.parentOrigin = e.origin;
246
+ var defaults = e.data.model;
247
+ if (defaults) Object.keys(defaults).forEach(function(key) {
248
+ _this5.model[key] = defaults[key];
249
+ });
250
+ return resolve(new postmate_es_ChildAPI(_this5));
251
+ }
252
+ return reject('Handshake Reply Failed');
253
+ };
254
+ _this5.child.addEventListener('message', shake, false);
255
+ });
256
+ };
257
+ return Model;
258
+ }();
259
+ const postmate_es = postmate_es_Postmate;
260
+ var events_EVENT = /*#__PURE__*/ function(EVENT) {
261
+ EVENT["MUJIAN_INIT"] = "mujian:init";
262
+ EVENT["MUJIAN_AI_CHAT_STOP"] = "mujian:ai:chat:stop";
263
+ EVENT["MUJIAN_AI_CHAT_COMPLETE"] = "mujian:ai:chat:complete";
264
+ EVENT["MUJIAN_AI_CHAT_APPLY_REGEX"] = "mujian:ai:chat:applyRegex";
265
+ EVENT["MUJIAN_AI_CHAT_RENDER_MESSAGE"] = "mujian:ai:chat:renderMessage";
266
+ EVENT["MUJIAN_AI_TEXT_GENERATE"] = "mujian:ai:text:generate";
267
+ EVENT["MUJIAN_AI_OPENAI_COMPLETIONS_CREATE"] = "mujian:ai:openai:completions:create";
268
+ EVENT["MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE"] = "mujian:ai:openai:chat:completions:create";
269
+ EVENT["MUJIAN_AI_OPENAI_RESPONSES_CREATE"] = "mujian:ai:openai:responses:create";
270
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_ALL"] = "mujian:ai:chat:message:getAll";
271
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_PAGE"] = "mujian:ai:chat:message:getPage";
272
+ EVENT["MUJIAN_AI_CHAT_PROJECT_GET_INFO"] = "mujian:ai:chat:project:getInfo";
273
+ EVENT["MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE"] = "mujian:ai:settings:persona:getActive";
274
+ EVENT["MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE"] = "mujian:ai:settings:persona:setActive";
275
+ EVENT["MUJIAN_AI_SETTINGS_MODEL_GET_ALL"] = "mujian:ai:settings:model:getAll";
276
+ EVENT["MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE"] = "mujian:ai:settings:model:setActive";
277
+ EVENT["MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE"] = "mujian:ai:settings:model:getActive";
278
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE"] = "mujian:ai:chat:message:deleteOne";
279
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE"] = "mujian:ai:chat:message:editOne";
280
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_SWIPE"] = "mujian:ai:chat:message:swipe";
281
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT"] = "mujian:ai:chat:message:getPrompt";
282
+ EVENT["MUJIAN_AI_OPENAI_IMAGES_GENERATE"] = "mujian:ai:openai:images:generate";
283
+ EVENT["MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT"] = "mujian:utils:clipboard:writeText";
284
+ return EVENT;
285
+ }({});
286
+ function wrapOnData(onData) {
287
+ let fullContent = '';
288
+ let buffer = '';
289
+ let questionId;
290
+ let replyId;
291
+ return function(data) {
292
+ buffer += data;
293
+ const lines = buffer.split('\n');
294
+ buffer = lines.pop() || '';
295
+ for (const line of lines)if (line.startsWith('data: ')) try {
296
+ const parsedData = JSON.parse(line.slice(6));
297
+ if (parsedData.question_id) questionId = parsedData.question_id;
298
+ if (parsedData.reply_id) replyId = parsedData.reply_id;
299
+ if (parsedData.isFinished) return void onData({
300
+ isFinished: true,
301
+ deltaContent: '',
302
+ fullContent,
303
+ questionId,
304
+ replyId
305
+ });
306
+ const deltaContent = parsedData?.choices?.[0]?.delta?.content;
307
+ if (deltaContent?.length > 0) {
308
+ fullContent += deltaContent;
309
+ onData({
310
+ isFinished: false,
311
+ deltaContent: deltaContent,
312
+ fullContent,
313
+ questionId,
314
+ replyId
315
+ });
316
+ }
317
+ } catch (e) {
318
+ onData({
319
+ isFinished: true,
320
+ error: e,
321
+ deltaContent: '',
322
+ fullContent,
323
+ questionId,
324
+ replyId
325
+ });
326
+ return;
327
+ }
328
+ };
329
+ }
330
+ const chat_complete = async function(message, onData, signal, option = {}) {
331
+ await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
332
+ content: message
333
+ }, {
334
+ onData: option.parseContent ? wrapOnData(onData) : onData,
335
+ signal
336
+ });
337
+ };
338
+ const applyRegex = async function(props) {
339
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_APPLY_REGEX, props);
340
+ };
341
+ const renderMessage = async function(props) {
342
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_RENDER_MESSAGE, props);
343
+ };
344
+ const continueComplete = async function(onData, signal) {
345
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
346
+ isContinue: true
347
+ }, {
348
+ onData,
349
+ signal
350
+ });
351
+ };
352
+ const regenerate = async function(onData, signal) {
353
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
354
+ isRegenerate: true
355
+ }, {
356
+ onData,
357
+ signal
358
+ });
359
+ };
360
+ const getAll = async function() {
361
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_GET_ALL);
362
+ };
363
+ const messageDeleteOne = async function(messageId) {
364
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE, {
365
+ messageId
366
+ });
367
+ };
368
+ const messageEditOne = async function(messageId, content) {
369
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE, {
370
+ messageId,
371
+ content
372
+ });
373
+ };
374
+ const messageSwipe = async function(messageId, swipeId) {
375
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_SWIPE, {
376
+ messageId,
377
+ swipeId
378
+ });
379
+ };
380
+ const getPrompt = async function(messageId) {
381
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT, {
382
+ messageId
383
+ });
384
+ };
385
+ async function getPage(fromCursor, pageSize) {
386
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE, {
387
+ fromCursor,
388
+ pageSize
389
+ });
390
+ }
391
+ const getInfo = async function() {
392
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_PROJECT_GET_INFO);
393
+ };
394
+ const getActive = async function() {
395
+ return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE);
396
+ };
397
+ const setActive = async function(personaId) {
398
+ return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE, {
399
+ personaId
400
+ });
401
+ };
402
+ const model_getActive = async function() {
403
+ return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE);
404
+ };
405
+ const model_setActive = async function(modelId) {
406
+ return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE, {
407
+ modelId
408
+ });
409
+ };
410
+ const model_getAll = async function() {
411
+ return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_GET_ALL);
412
+ };
413
+ const saveGame = async function() {};
414
+ const loadGame = async function() {};
415
+ async function writeText(text) {
416
+ return await this.call(events_EVENT.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT, text);
417
+ }
418
+ const Log = {
419
+ i (...msg) {
420
+ console.log('[MujianSDK] ', ...msg);
421
+ },
422
+ e (...msg) {
423
+ console.error('[MujianSDK] ', ...msg);
424
+ }
425
+ };
426
+ class MujianSdk {
427
+ constructor(){}
428
+ static EVENT = events_EVENT;
429
+ static getInstance() {
430
+ if (!window.$mujian) window.$mujian = new MujianSdk();
431
+ return window.$mujian;
432
+ }
433
+ parent = null;
434
+ ready = false;
435
+ get isReady() {
436
+ return this.ready;
437
+ }
438
+ pendingRequests = new Map();
439
+ async init() {
440
+ const handshake = new postmate_es.Model({
441
+ reply: ({ id, complete, data, error })=>{
442
+ const call = this.pendingRequests.get(id);
443
+ if (!call) return;
444
+ if (error) {
445
+ call.reject(error);
446
+ this.pendingRequests.delete(id);
447
+ return;
448
+ }
449
+ call.onData?.(data);
450
+ if (complete) {
451
+ call.onComplete?.();
452
+ call.resolve(data);
453
+ this.pendingRequests.delete(id);
454
+ }
455
+ }
456
+ });
457
+ try {
458
+ const parent = await handshake;
459
+ this.ready = true;
460
+ this.parent = parent;
461
+ Log.i('mujian sdk client init');
462
+ await this.call(events_EVENT.MUJIAN_INIT);
463
+ const projectInfo = await this.ai.chat.project.getInfo();
464
+ if (projectInfo.config?.customCss) {
465
+ const style = document.createElement('style');
466
+ style.setAttribute('type', 'text/css');
467
+ style.setAttribute('id', 'mujian-custom-css');
468
+ style.textContent = projectInfo.config.customCss;
469
+ document.head.appendChild(style);
470
+ }
471
+ if (projectInfo.config?.customJs) {
472
+ const script = document.createElement("script");
473
+ script.setAttribute('type', "text/javascript");
474
+ script.setAttribute('id', 'mujian-custom-js');
475
+ script.textContent = projectInfo.config.customJs;
476
+ document.head.appendChild(script);
477
+ }
478
+ } catch (error) {
479
+ Log.e('init error', error);
480
+ }
481
+ }
482
+ emit(event, data) {
483
+ if (!this.ready) throw new Error('Mujian is not initialized');
484
+ this.parent?.emit(event, data);
485
+ }
486
+ nextCallId = 0;
487
+ getCallId() {
488
+ const id = this.nextCallId;
489
+ this.nextCallId += 1;
490
+ return id;
491
+ }
492
+ async call(event, data, controller) {
493
+ if (!this.ready) throw new Error('Mujian is not initialized');
494
+ const callId = this.getCallId();
495
+ return new Promise((resolve, reject)=>{
496
+ this.pendingRequests.set(callId, {
497
+ resolve,
498
+ reject,
499
+ ...controller
500
+ });
501
+ this.emit(event, {
502
+ id: callId,
503
+ data
504
+ });
505
+ controller?.signal?.addEventListener('abort', ()=>{
506
+ this.emit(events_EVENT.MUJIAN_AI_CHAT_STOP, {
507
+ id: callId
508
+ });
509
+ });
510
+ });
511
+ }
512
+ ai = {
513
+ chat: {
514
+ project: {
515
+ getInfo: getInfo.bind(this)
516
+ },
517
+ settings: {
518
+ model: {
519
+ getAll: model_getAll.bind(this),
520
+ getActive: model_getActive.bind(this),
521
+ setActive: model_setActive.bind(this)
522
+ },
523
+ persona: {
524
+ getActive: getActive.bind(this),
525
+ setActive: setActive.bind(this)
526
+ }
527
+ },
528
+ complete: chat_complete.bind(this),
529
+ applyRegex: applyRegex.bind(this),
530
+ renderMessage: renderMessage.bind(this),
531
+ continue: continueComplete.bind(this),
532
+ regenerate: regenerate.bind(this),
533
+ message: {
534
+ getAll: getAll.bind(this),
535
+ deleteOne: messageDeleteOne.bind(this),
536
+ editOne: messageEditOne.bind(this),
537
+ swipe: messageSwipe.bind(this),
538
+ getPrompt: getPrompt.bind(this),
539
+ getPage: getPage.bind(this)
540
+ }
541
+ },
542
+ text: {
543
+ complete: generate.bind(this)
544
+ },
545
+ openai: {
546
+ completions: {
547
+ create: completions.create.bind(this)
548
+ },
549
+ chat: {
550
+ completions: {
551
+ create: chat.completions.create.bind(this)
552
+ }
553
+ },
554
+ responses: {
555
+ create: responses.create.bind(this)
556
+ },
557
+ images: {
558
+ generate: images_images.generate.bind(this)
559
+ }
560
+ }
561
+ };
562
+ ui = {};
563
+ utils = {
564
+ clipboard: {
565
+ writeText: writeText.bind(this)
566
+ }
567
+ };
568
+ hybrid = {};
569
+ player = {};
570
+ game = {
571
+ assets: ()=>{
572
+ console.log('assets');
573
+ },
574
+ saves: {
575
+ saveGame: saveGame.bind(this),
576
+ loadGame: loadGame.bind(this)
577
+ },
578
+ ranking: ()=>{
579
+ console.log('ranking');
580
+ }
581
+ };
582
+ }
583
+ const generate = async function(content) {
584
+ return await this.call(MujianSdk.EVENT.MUJIAN_AI_TEXT_GENERATE, {
585
+ content
586
+ });
587
+ };
588
+ const chat = {
589
+ completions: {
590
+ create: async function(params, options, onData, signal) {
591
+ return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE, {
592
+ params,
593
+ options
594
+ }, {
595
+ onData,
596
+ signal
597
+ });
598
+ }
599
+ }
600
+ };
601
+ const completions = {
602
+ create: async function(params, options, onData, signal) {
603
+ return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE, {
604
+ params,
605
+ options
606
+ }, {
607
+ onData,
608
+ signal
609
+ });
610
+ }
611
+ };
612
+ const responses = {
613
+ create: async function(params, options, onData, signal) {
614
+ return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE, {
615
+ params,
616
+ options
617
+ }, {
618
+ onData,
619
+ signal
620
+ });
621
+ }
622
+ };
623
+ const images_images = {
624
+ generate: async function(params, options, onData, signal) {
625
+ return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE, {
626
+ params,
627
+ options
628
+ }, {
629
+ onData,
630
+ signal
631
+ });
632
+ }
633
+ };
634
+ class src_MujianSdk {
635
+ constructor(){}
636
+ static EVENT = events_EVENT;
637
+ static getInstance() {
638
+ if (!window.$mujian) window.$mujian = new src_MujianSdk();
639
+ return window.$mujian;
640
+ }
641
+ parent = null;
642
+ ready = false;
643
+ get isReady() {
644
+ return this.ready;
645
+ }
646
+ pendingRequests = new Map();
647
+ async init() {
648
+ const handshake = new postmate_es.Model({
649
+ reply: ({ id, complete, data, error })=>{
650
+ const call = this.pendingRequests.get(id);
651
+ if (!call) return;
652
+ if (error) {
653
+ call.reject(error);
654
+ this.pendingRequests.delete(id);
655
+ return;
656
+ }
657
+ call.onData?.(data);
658
+ if (complete) {
659
+ call.onComplete?.();
660
+ call.resolve(data);
661
+ this.pendingRequests.delete(id);
662
+ }
663
+ }
664
+ });
665
+ try {
666
+ const parent = await handshake;
667
+ this.ready = true;
668
+ this.parent = parent;
669
+ Log.i('mujian sdk client init');
670
+ await this.call(events_EVENT.MUJIAN_INIT);
671
+ const projectInfo = await this.ai.chat.project.getInfo();
672
+ if (projectInfo.config?.customCss) {
673
+ const style = document.createElement('style');
674
+ style.setAttribute('type', 'text/css');
675
+ style.setAttribute('id', 'mujian-custom-css');
676
+ style.textContent = projectInfo.config.customCss;
677
+ document.head.appendChild(style);
678
+ }
679
+ if (projectInfo.config?.customJs) {
680
+ const script = document.createElement("script");
681
+ script.setAttribute('type', "text/javascript");
682
+ script.setAttribute('id', 'mujian-custom-js');
683
+ script.textContent = projectInfo.config.customJs;
684
+ document.head.appendChild(script);
685
+ }
686
+ } catch (error) {
687
+ Log.e('init error', error);
688
+ }
689
+ }
690
+ emit(event, data) {
691
+ if (!this.ready) throw new Error('Mujian is not initialized');
692
+ this.parent?.emit(event, data);
693
+ }
694
+ nextCallId = 0;
695
+ getCallId() {
696
+ const id = this.nextCallId;
697
+ this.nextCallId += 1;
698
+ return id;
699
+ }
700
+ async call(event, data, controller) {
701
+ if (!this.ready) throw new Error('Mujian is not initialized');
702
+ const callId = this.getCallId();
703
+ return new Promise((resolve, reject)=>{
704
+ this.pendingRequests.set(callId, {
705
+ resolve,
706
+ reject,
707
+ ...controller
708
+ });
709
+ this.emit(event, {
710
+ id: callId,
711
+ data
712
+ });
713
+ controller?.signal?.addEventListener('abort', ()=>{
714
+ this.emit(events_EVENT.MUJIAN_AI_CHAT_STOP, {
715
+ id: callId
716
+ });
717
+ });
718
+ });
719
+ }
720
+ ai = {
721
+ chat: {
722
+ project: {
723
+ getInfo: getInfo.bind(this)
724
+ },
725
+ settings: {
726
+ model: {
727
+ getAll: model_getAll.bind(this),
728
+ getActive: model_getActive.bind(this),
729
+ setActive: model_setActive.bind(this)
730
+ },
731
+ persona: {
732
+ getActive: getActive.bind(this),
733
+ setActive: setActive.bind(this)
734
+ }
735
+ },
736
+ complete: chat_complete.bind(this),
737
+ applyRegex: applyRegex.bind(this),
738
+ renderMessage: renderMessage.bind(this),
739
+ continue: continueComplete.bind(this),
740
+ regenerate: regenerate.bind(this),
741
+ message: {
742
+ getAll: getAll.bind(this),
743
+ deleteOne: messageDeleteOne.bind(this),
744
+ editOne: messageEditOne.bind(this),
745
+ swipe: messageSwipe.bind(this),
746
+ getPrompt: getPrompt.bind(this),
747
+ getPage: getPage.bind(this)
748
+ }
749
+ },
750
+ text: {
751
+ complete: generate.bind(this)
752
+ },
753
+ openai: {
754
+ completions: {
755
+ create: completions.create.bind(this)
756
+ },
757
+ chat: {
758
+ completions: {
759
+ create: chat.completions.create.bind(this)
760
+ }
761
+ },
762
+ responses: {
763
+ create: responses.create.bind(this)
764
+ },
765
+ images: {
766
+ generate: images_images.generate.bind(this)
767
+ }
768
+ }
769
+ };
770
+ ui = {};
771
+ utils = {
772
+ clipboard: {
773
+ writeText: writeText.bind(this)
774
+ }
775
+ };
776
+ hybrid = {};
777
+ player = {};
778
+ game = {
779
+ assets: ()=>{
780
+ console.log('assets');
781
+ },
782
+ saves: {
783
+ saveGame: saveGame.bind(this),
784
+ loadGame: loadGame.bind(this)
785
+ },
786
+ ranking: ()=>{
787
+ console.log('ranking');
788
+ }
789
+ };
790
+ }
791
+ return __webpack_exports__;
792
+ })());