@mujian/js-sdk 0.0.6-beta.33 → 0.0.6-beta.34

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.
@@ -0,0 +1,484 @@
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["RslibUmdExample"] = factory();
6
+ else root["RslibUmdExample"] = 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: ()=>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_TEXT_GENERATE"] = "mujian:ai:text:generate";
266
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_ALL"] = "mujian:ai:chat:message:getAll";
267
+ EVENT["MUJIAN_AI_CHAT_PROJECT_GET_INFO"] = "mujian:ai:chat:project:getInfo";
268
+ EVENT["MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE"] = "mujian:ai:settings:persona:getActive";
269
+ EVENT["MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE"] = "mujian:ai:settings:persona:setActive";
270
+ EVENT["MUJIAN_AI_SETTINGS_MODEL_GET_ALL"] = "mujian:ai:settings:model:getAll";
271
+ EVENT["MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE"] = "mujian:ai:settings:model:setActive";
272
+ EVENT["MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE"] = "mujian:ai:settings:model:getActive";
273
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE"] = "mujian:ai:chat:message:deleteOne";
274
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE"] = "mujian:ai:chat:message:editOne";
275
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_SWIPE"] = "mujian:ai:chat:message:swipe";
276
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT"] = "mujian:ai:chat:message:getPrompt";
277
+ return EVENT;
278
+ }({});
279
+ const getInfo = async function() {
280
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_PROJECT_GET_INFO);
281
+ };
282
+ const getActive = async function() {
283
+ return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE);
284
+ };
285
+ const setActive = async function(modelId) {
286
+ return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE, {
287
+ modelId
288
+ });
289
+ };
290
+ const getAll = async function() {
291
+ return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_GET_ALL);
292
+ };
293
+ const persona_getActive = async function() {
294
+ return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE);
295
+ };
296
+ const persona_setActive = async function(personaId) {
297
+ return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE, {
298
+ personaId
299
+ });
300
+ };
301
+ const chat_complete = async function(message, onData, signal) {
302
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
303
+ content: message
304
+ }, {
305
+ onData,
306
+ signal
307
+ });
308
+ };
309
+ const applyRegex = async function(props) {
310
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_APPLY_REGEX, props);
311
+ };
312
+ const continueComplete = async function(onData, signal) {
313
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
314
+ isContinue: true
315
+ }, {
316
+ onData,
317
+ signal
318
+ });
319
+ };
320
+ const regenerate = async function(onData, signal) {
321
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
322
+ isRegenerate: true
323
+ }, {
324
+ onData,
325
+ signal
326
+ });
327
+ };
328
+ const message_getAll = async function() {
329
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_GET_ALL);
330
+ };
331
+ const messageDeleteOne = async function(messageId) {
332
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE, {
333
+ messageId
334
+ });
335
+ };
336
+ const messageEditOne = async function(messageId, content) {
337
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE, {
338
+ messageId,
339
+ content
340
+ });
341
+ };
342
+ const messageSwipe = async function(messageId, swipeId) {
343
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_SWIPE, {
344
+ messageId,
345
+ swipeId
346
+ });
347
+ };
348
+ const getPrompt = async function(messageId) {
349
+ return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT, {
350
+ messageId
351
+ });
352
+ };
353
+ const generate = async function() {
354
+ return await this.call(events_EVENT.MUJIAN_AI_TEXT_GENERATE, {
355
+ content: 'q'
356
+ });
357
+ };
358
+ const saveGame = async function() {};
359
+ const loadGame = async function() {};
360
+ class MujianSdk {
361
+ constructor(){}
362
+ static getInstance() {
363
+ if (!window.$mujian) window.$mujian = new MujianSdk();
364
+ return window.$mujian;
365
+ }
366
+ parent = null;
367
+ ready = false;
368
+ get isReady() {
369
+ return this.ready;
370
+ }
371
+ pendingRequests = new Map();
372
+ async init() {
373
+ const handshake = new postmate_es.Model({
374
+ reply: ({ id, complete, data, error })=>{
375
+ const call = this.pendingRequests.get(id);
376
+ if (!call) return;
377
+ if (error) {
378
+ call.reject(error);
379
+ this.pendingRequests.delete(id);
380
+ return;
381
+ }
382
+ call.onData?.(data);
383
+ if (complete) {
384
+ call.onComplete?.();
385
+ call.resolve(data);
386
+ this.pendingRequests.delete(id);
387
+ }
388
+ }
389
+ });
390
+ try {
391
+ const parent = await handshake;
392
+ this.ready = true;
393
+ this.parent = parent;
394
+ console.log('mujian sdk client init');
395
+ await this.call(events_EVENT.MUJIAN_INIT);
396
+ } catch (error) {
397
+ console.log('init error', error);
398
+ }
399
+ }
400
+ emit(event, data) {
401
+ if (!this.ready) throw new Error('Mujian is not initialized');
402
+ this.parent?.emit(event, data);
403
+ }
404
+ nextCallId = 0;
405
+ getCallId() {
406
+ const id = this.nextCallId;
407
+ this.nextCallId += 1;
408
+ return id;
409
+ }
410
+ async call(event, data, controller) {
411
+ if (!this.ready) throw new Error('Mujian is not initialized');
412
+ const callId = this.getCallId();
413
+ return new Promise((resolve, reject)=>{
414
+ this.pendingRequests.set(callId, {
415
+ resolve,
416
+ reject,
417
+ ...controller
418
+ });
419
+ this.emit(event, {
420
+ id: callId,
421
+ data
422
+ });
423
+ controller?.signal?.addEventListener('abort', ()=>{
424
+ this.emit(events_EVENT.MUJIAN_AI_CHAT_STOP, {
425
+ id: callId
426
+ });
427
+ });
428
+ });
429
+ }
430
+ ai = {
431
+ chat: {
432
+ project: {
433
+ getInfo: getInfo.bind(this)
434
+ },
435
+ settings: {
436
+ model: {
437
+ getAll: getAll.bind(this),
438
+ getActive: getActive.bind(this),
439
+ setActive: setActive.bind(this)
440
+ },
441
+ persona: {
442
+ getActive: persona_getActive.bind(this),
443
+ setActive: persona_setActive.bind(this)
444
+ }
445
+ },
446
+ complete: chat_complete.bind(this),
447
+ applyRegex: applyRegex.bind(this),
448
+ continue: continueComplete.bind(this),
449
+ regenerate: regenerate.bind(this),
450
+ message: {
451
+ getAll: message_getAll.bind(this),
452
+ deleteOne: messageDeleteOne.bind(this),
453
+ editOne: messageEditOne.bind(this),
454
+ swipe: messageSwipe.bind(this),
455
+ getPrompt: getPrompt.bind(this)
456
+ }
457
+ },
458
+ text: {
459
+ complete: generate.bind(this)
460
+ }
461
+ };
462
+ ui = {};
463
+ util = {
464
+ cn: ()=>{
465
+ console.log('cn');
466
+ }
467
+ };
468
+ hybrid = {};
469
+ player = {};
470
+ game = {
471
+ assets: ()=>{
472
+ console.log('assets');
473
+ },
474
+ saves: {
475
+ saveGame: saveGame.bind(this),
476
+ loadGame: loadGame.bind(this)
477
+ },
478
+ ranking: ()=>{
479
+ console.log('ranking');
480
+ }
481
+ };
482
+ }
483
+ return __webpack_exports__;
484
+ })());
@@ -0,0 +1,7 @@
1
+ /**
2
+ postmate - A powerful, simple, promise-based postMessage library
3
+ @version v1.5.2
4
+ @link https://github.com/dollarshaveclub/postmate
5
+ @author Jacob Kelley <jakie8@gmail.com>
6
+ @license MIT
7
+ **/