@mujian/js-sdk 0.0.6-beta.9 → 0.0.6-beta.90
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/events/index.d.ts +31 -3
- package/dist/index.d.ts +16 -39
- package/dist/index.js +360 -62
- package/dist/lite.d.ts +73 -0
- package/dist/lite.js +119 -0
- package/dist/modules/ai/chat/chat.d.ts +46 -4
- package/dist/modules/ai/chat/index.d.ts +1 -1
- package/dist/modules/ai/chat/message/index.d.ts +3 -7
- package/dist/modules/ai/chat/settings/index.d.ts +1 -2
- package/dist/modules/ai/chat/settings/persona.d.ts +0 -8
- package/dist/modules/ai/index.d.ts +3 -2
- package/dist/modules/ai/openai/chat.d.ts +22 -0
- package/dist/modules/ai/openai/completions.d.ts +17 -0
- package/dist/modules/ai/openai/images.d.ts +17 -0
- package/dist/modules/ai/openai/index.d.ts +4 -0
- package/dist/modules/ai/openai/responses.d.ts +18 -0
- package/dist/modules/ai/text/index.d.ts +9 -2
- package/dist/modules/config.d.ts +17 -0
- package/dist/modules/utils/clipboard.d.ts +5 -0
- package/dist/modules/utils/index.d.ts +4 -0
- package/dist/react/chat/useChat/index.d.ts +34 -8
- package/dist/react/chat/useChat/inner/chatStreaming.d.ts +4 -2
- package/dist/react/chat/useChat/message.d.ts +31 -13
- package/dist/react/components/MdRenderer/index.d.ts +5 -3
- package/dist/react/components/MdRenderer/utils/height.d.ts +0 -0
- package/dist/react/components/MdRenderer/utils/iframe.d.ts +9 -0
- package/dist/react/components/MdRenderer/utils/scripts.d.ts +4 -0
- package/dist/react/components/MujianSpinner/index.d.ts +7 -0
- package/dist/react/components/index.d.ts +1 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/mjEngine/index.d.ts +48 -0
- package/dist/react.css +65 -4
- package/dist/react.js +834 -188
- package/dist/types/index.d.ts +39 -0
- package/dist/umd/index.js +2 -0
- package/dist/umd/index.js.LICENSE.txt +7 -0
- package/dist/umd/lite.js +2 -0
- package/dist/umd/lite.js.LICENSE.txt +7 -0
- package/dist/umd/react.css +1 -0
- package/dist/umd/react.js +133 -0
- package/dist/umd/react.js.LICENSE.txt +31 -0
- package/dist/utils/is-network-error.d.ts +1 -0
- package/dist/utils/log.d.ts +4 -0
- package/package.json +16 -6
- package/dist/modules/ai/chat/settings/model.d.ts +0 -24
- /package/dist/react/components/MdRenderer/{utils.d.ts → utils/md.d.ts} +0 -0
package/dist/events/index.d.ts
CHANGED
|
@@ -21,13 +21,33 @@ export declare enum EVENT {
|
|
|
21
21
|
*/
|
|
22
22
|
MUJIAN_AI_CHAT_APPLY_REGEX = "mujian:ai:chat:applyRegex",
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
24
|
+
* 对消息应用正则表达式并渲染模板
|
|
25
|
+
*/
|
|
26
|
+
MUJIAN_AI_CHAT_RENDER_MESSAGE = "mujian:ai:chat:renderMessage",
|
|
27
|
+
/**
|
|
28
|
+
* 文本补全
|
|
25
29
|
*/
|
|
26
30
|
MUJIAN_AI_TEXT_GENERATE = "mujian:ai:text:generate",
|
|
31
|
+
/**
|
|
32
|
+
* OpenAI 文本补全
|
|
33
|
+
*/
|
|
34
|
+
MUJIAN_AI_OPENAI_COMPLETIONS_CREATE = "mujian:ai:openai:completions:create",
|
|
35
|
+
/**
|
|
36
|
+
* OpenAI 聊天补全
|
|
37
|
+
*/
|
|
38
|
+
MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE = "mujian:ai:openai:chat:completions:create",
|
|
39
|
+
/**
|
|
40
|
+
* OpenAI 响应创建
|
|
41
|
+
*/
|
|
42
|
+
MUJIAN_AI_OPENAI_RESPONSES_CREATE = "mujian:ai:openai:responses:create",
|
|
27
43
|
/**
|
|
28
44
|
* 获取对话
|
|
29
45
|
*/
|
|
30
46
|
MUJIAN_AI_CHAT_MESSAGE_GET_ALL = "mujian:ai:chat:message:getAll",
|
|
47
|
+
/**
|
|
48
|
+
* 获取对话分页
|
|
49
|
+
*/
|
|
50
|
+
MUJIAN_AI_CHAT_MESSAGE_GET_PAGE = "mujian:ai:chat:message:getPage",
|
|
31
51
|
/**
|
|
32
52
|
* 获取项目信息
|
|
33
53
|
*/
|
|
@@ -65,7 +85,15 @@ export declare enum EVENT {
|
|
|
65
85
|
*/
|
|
66
86
|
MUJIAN_AI_CHAT_MESSAGE_SWIPE = "mujian:ai:chat:message:swipe",
|
|
67
87
|
/**
|
|
68
|
-
*
|
|
88
|
+
* 生成图像
|
|
89
|
+
*/
|
|
90
|
+
MUJIAN_AI_OPENAI_IMAGES_GENERATE = "mujian:ai:openai:images:generate",
|
|
91
|
+
/**
|
|
92
|
+
* 写入剪贴版
|
|
93
|
+
*/
|
|
94
|
+
MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT = "mujian:utils:clipboard:writeText",
|
|
95
|
+
/**
|
|
96
|
+
* 获取配置
|
|
69
97
|
*/
|
|
70
|
-
|
|
98
|
+
MUJIAN_GET_CONFIG = "mujian:getConfig"
|
|
71
99
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EVENT } from './events';
|
|
2
|
+
import { Config } from './modules/config';
|
|
2
3
|
/** @hidden */
|
|
3
4
|
export { EVENT } from './events';
|
|
4
5
|
type Call<T> = {
|
|
@@ -14,7 +15,13 @@ declare global {
|
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
17
|
declare class MujianSdk {
|
|
18
|
+
/** @hidden */
|
|
17
19
|
private constructor();
|
|
20
|
+
/**
|
|
21
|
+
* 事件
|
|
22
|
+
* @enum {string}
|
|
23
|
+
*/
|
|
24
|
+
static EVENT: typeof EVENT;
|
|
18
25
|
/** @hidden */
|
|
19
26
|
static getInstance(): MujianSdk;
|
|
20
27
|
/** @hidden */
|
|
@@ -27,6 +34,9 @@ declare class MujianSdk {
|
|
|
27
34
|
get isReady(): boolean;
|
|
28
35
|
/** @hidden */
|
|
29
36
|
private pendingRequests;
|
|
37
|
+
private _config?;
|
|
38
|
+
get openapi(): Config['openapi'] | undefined;
|
|
39
|
+
private initPromise;
|
|
30
40
|
/**
|
|
31
41
|
* 初始化
|
|
32
42
|
*/
|
|
@@ -40,47 +50,14 @@ declare class MujianSdk {
|
|
|
40
50
|
/** @hidden */
|
|
41
51
|
call<Req, Resp>(event: EVENT, data?: Req, controller?: Pick<Call<Resp>, 'onData' | 'onComplete' | 'signal'>): Promise<Resp>;
|
|
42
52
|
/**
|
|
43
|
-
*
|
|
53
|
+
* @namespace ai
|
|
44
54
|
*/
|
|
45
|
-
ai:
|
|
46
|
-
chat: {
|
|
47
|
-
project: {
|
|
48
|
-
getInfo: () => Promise<import("./types").ProjectInfo>;
|
|
49
|
-
};
|
|
50
|
-
settings: {
|
|
51
|
-
model: {
|
|
52
|
-
getAll: () => Promise<import("./types").ModelInfo[]>;
|
|
53
|
-
getActive: () => Promise<string>;
|
|
54
|
-
setActive: (modelId: string) => Promise<string>;
|
|
55
|
-
};
|
|
56
|
-
persona: {
|
|
57
|
-
getActive: () => Promise<import("./types").PersonaInfo>;
|
|
58
|
-
setActive: (personaId: string) => Promise<void>;
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
complete: (message: string, onData: (data: string) => void, signal?: AbortSignal | undefined) => Promise<string>;
|
|
62
|
-
applyRegex: (props: {
|
|
63
|
-
message: import("./react").Message;
|
|
64
|
-
index: number;
|
|
65
|
-
depth: number;
|
|
66
|
-
}) => Promise<import("./react").Message>;
|
|
67
|
-
continue: (onData: (data: string) => void, signal?: AbortSignal | undefined) => Promise<string>;
|
|
68
|
-
regenerate: (onData: (data: string) => void, signal?: AbortSignal | undefined) => Promise<string>;
|
|
69
|
-
message: {
|
|
70
|
-
getAll: () => Promise<import("./react").Message[]>;
|
|
71
|
-
deleteOne: (messageId: string) => Promise<unknown>;
|
|
72
|
-
editOne: (messageId: string, content: string) => Promise<unknown>;
|
|
73
|
-
swipe: (messageId: string, swipeId: number) => Promise<unknown>;
|
|
74
|
-
getPrompt: (messageId: string) => Promise<unknown>;
|
|
75
|
-
};
|
|
76
|
-
};
|
|
77
|
-
text: {
|
|
78
|
-
complete: () => Promise<void>;
|
|
79
|
-
};
|
|
80
|
-
};
|
|
55
|
+
ai: any;
|
|
81
56
|
ui: {};
|
|
82
|
-
|
|
83
|
-
|
|
57
|
+
utils: {
|
|
58
|
+
clipboard: {
|
|
59
|
+
writeText: (text: string) => Promise<unknown>;
|
|
60
|
+
};
|
|
84
61
|
};
|
|
85
62
|
hybrid: {};
|
|
86
63
|
player: {};
|
package/dist/index.js
CHANGED
|
@@ -4,8 +4,13 @@ var events_EVENT = /*#__PURE__*/ function(EVENT) {
|
|
|
4
4
|
EVENT["MUJIAN_AI_CHAT_STOP"] = "mujian:ai:chat:stop";
|
|
5
5
|
EVENT["MUJIAN_AI_CHAT_COMPLETE"] = "mujian:ai:chat:complete";
|
|
6
6
|
EVENT["MUJIAN_AI_CHAT_APPLY_REGEX"] = "mujian:ai:chat:applyRegex";
|
|
7
|
+
EVENT["MUJIAN_AI_CHAT_RENDER_MESSAGE"] = "mujian:ai:chat:renderMessage";
|
|
7
8
|
EVENT["MUJIAN_AI_TEXT_GENERATE"] = "mujian:ai:text:generate";
|
|
9
|
+
EVENT["MUJIAN_AI_OPENAI_COMPLETIONS_CREATE"] = "mujian:ai:openai:completions:create";
|
|
10
|
+
EVENT["MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE"] = "mujian:ai:openai:chat:completions:create";
|
|
11
|
+
EVENT["MUJIAN_AI_OPENAI_RESPONSES_CREATE"] = "mujian:ai:openai:responses:create";
|
|
8
12
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_ALL"] = "mujian:ai:chat:message:getAll";
|
|
13
|
+
EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_PAGE"] = "mujian:ai:chat:message:getPage";
|
|
9
14
|
EVENT["MUJIAN_AI_CHAT_PROJECT_GET_INFO"] = "mujian:ai:chat:project:getInfo";
|
|
10
15
|
EVENT["MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE"] = "mujian:ai:settings:persona:getActive";
|
|
11
16
|
EVENT["MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE"] = "mujian:ai:settings:persona:setActive";
|
|
@@ -15,20 +20,69 @@ var events_EVENT = /*#__PURE__*/ function(EVENT) {
|
|
|
15
20
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE"] = "mujian:ai:chat:message:deleteOne";
|
|
16
21
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE"] = "mujian:ai:chat:message:editOne";
|
|
17
22
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_SWIPE"] = "mujian:ai:chat:message:swipe";
|
|
18
|
-
EVENT["
|
|
23
|
+
EVENT["MUJIAN_AI_OPENAI_IMAGES_GENERATE"] = "mujian:ai:openai:images:generate";
|
|
24
|
+
EVENT["MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT"] = "mujian:utils:clipboard:writeText";
|
|
25
|
+
EVENT["MUJIAN_GET_CONFIG"] = "mujian:getConfig";
|
|
19
26
|
return EVENT;
|
|
20
27
|
}({});
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
function wrapOnData(onData) {
|
|
29
|
+
let fullContent = '';
|
|
30
|
+
let buffer = '';
|
|
31
|
+
let questionId;
|
|
32
|
+
let replyId;
|
|
33
|
+
return function(data) {
|
|
34
|
+
buffer += data;
|
|
35
|
+
const lines = buffer.split('\n');
|
|
36
|
+
buffer = lines.pop() || '';
|
|
37
|
+
for (const line of lines)if (line.startsWith('data: ')) try {
|
|
38
|
+
const parsedData = JSON.parse(line.slice(6));
|
|
39
|
+
if (parsedData.question_id) questionId = parsedData.question_id;
|
|
40
|
+
if (parsedData.reply_id) replyId = parsedData.reply_id;
|
|
41
|
+
if (parsedData.isFinished) return void onData({
|
|
42
|
+
isFinished: true,
|
|
43
|
+
deltaContent: '',
|
|
44
|
+
fullContent,
|
|
45
|
+
questionId,
|
|
46
|
+
replyId
|
|
47
|
+
});
|
|
48
|
+
const deltaContent = parsedData?.choices?.[0]?.delta?.content;
|
|
49
|
+
if (deltaContent?.length > 0) {
|
|
50
|
+
fullContent += deltaContent;
|
|
51
|
+
onData({
|
|
52
|
+
isFinished: false,
|
|
53
|
+
deltaContent: deltaContent,
|
|
54
|
+
fullContent,
|
|
55
|
+
questionId,
|
|
56
|
+
replyId
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
} catch (e) {
|
|
60
|
+
onData({
|
|
61
|
+
isFinished: true,
|
|
62
|
+
error: e,
|
|
63
|
+
deltaContent: '',
|
|
64
|
+
fullContent,
|
|
65
|
+
questionId,
|
|
66
|
+
replyId
|
|
67
|
+
});
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const chat_complete = async function(message, onData, signal, option = {}) {
|
|
73
|
+
await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
|
|
23
74
|
content: message
|
|
24
75
|
}, {
|
|
25
|
-
onData,
|
|
76
|
+
onData: option.parseContent ? wrapOnData(onData) : onData,
|
|
26
77
|
signal
|
|
27
78
|
});
|
|
28
79
|
};
|
|
29
80
|
const applyRegex = async function(props) {
|
|
30
81
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_APPLY_REGEX, props);
|
|
31
82
|
};
|
|
83
|
+
const renderMessage = async function(props) {
|
|
84
|
+
return await this.call(events_EVENT.MUJIAN_AI_CHAT_RENDER_MESSAGE, props);
|
|
85
|
+
};
|
|
32
86
|
const continueComplete = async function(onData, signal) {
|
|
33
87
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
|
|
34
88
|
isContinue: true
|
|
@@ -65,42 +119,37 @@ const messageSwipe = async function(messageId, swipeId) {
|
|
|
65
119
|
swipeId
|
|
66
120
|
});
|
|
67
121
|
};
|
|
68
|
-
|
|
69
|
-
return await this.call(events_EVENT.
|
|
70
|
-
|
|
122
|
+
async function getPage(fromCursor, pageSize) {
|
|
123
|
+
return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE, {
|
|
124
|
+
fromCursor,
|
|
125
|
+
pageSize
|
|
71
126
|
});
|
|
72
|
-
}
|
|
127
|
+
}
|
|
73
128
|
const getInfo = async function() {
|
|
74
129
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_PROJECT_GET_INFO);
|
|
75
130
|
};
|
|
76
131
|
const getActive = async function() {
|
|
77
132
|
return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE);
|
|
78
133
|
};
|
|
79
|
-
|
|
80
|
-
return await this.call(events_EVENT.
|
|
81
|
-
|
|
82
|
-
});
|
|
83
|
-
};
|
|
84
|
-
const model_getActive = async function() {
|
|
85
|
-
return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE);
|
|
86
|
-
};
|
|
87
|
-
const model_setActive = async function(modelId) {
|
|
88
|
-
return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE, {
|
|
89
|
-
modelId
|
|
90
|
-
});
|
|
91
|
-
};
|
|
92
|
-
const model_getAll = async function() {
|
|
93
|
-
return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_GET_ALL);
|
|
94
|
-
};
|
|
95
|
-
const generate = async function() {
|
|
96
|
-
return await this.call(events_EVENT.MUJIAN_AI_TEXT_GENERATE, {
|
|
97
|
-
content: 'q'
|
|
98
|
-
});
|
|
99
|
-
};
|
|
134
|
+
async function getConfig() {
|
|
135
|
+
return await this.call(events_EVENT.MUJIAN_GET_CONFIG);
|
|
136
|
+
}
|
|
100
137
|
const saveGame = async function() {};
|
|
101
138
|
const loadGame = async function() {};
|
|
139
|
+
async function writeText(text) {
|
|
140
|
+
return await this.call(events_EVENT.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT, text);
|
|
141
|
+
}
|
|
142
|
+
const Log = {
|
|
143
|
+
i (...msg) {
|
|
144
|
+
console.log('[MujianSDK] ', ...msg);
|
|
145
|
+
},
|
|
146
|
+
e (...msg) {
|
|
147
|
+
console.error('[MujianSDK] ', ...msg);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
102
150
|
class MujianSdk {
|
|
103
151
|
constructor(){}
|
|
152
|
+
static EVENT = events_EVENT;
|
|
104
153
|
static getInstance() {
|
|
105
154
|
if (!window.$mujian) window.$mujian = new MujianSdk();
|
|
106
155
|
return window.$mujian;
|
|
@@ -111,34 +160,272 @@ class MujianSdk {
|
|
|
111
160
|
return this.ready;
|
|
112
161
|
}
|
|
113
162
|
pendingRequests = new Map();
|
|
163
|
+
_config;
|
|
164
|
+
get openapi() {
|
|
165
|
+
return this._config?.openapi;
|
|
166
|
+
}
|
|
167
|
+
initPromise = null;
|
|
114
168
|
async init() {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
call
|
|
121
|
-
|
|
122
|
-
|
|
169
|
+
if (this.initPromise) return this.initPromise;
|
|
170
|
+
this.initPromise = (async ()=>{
|
|
171
|
+
const handshake = new postmate.Model({
|
|
172
|
+
reply: ({ id, complete, data, error })=>{
|
|
173
|
+
const call = this.pendingRequests.get(id);
|
|
174
|
+
if (!call) return;
|
|
175
|
+
if (error) {
|
|
176
|
+
call.reject(error);
|
|
177
|
+
this.pendingRequests.delete(id);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
call.onData?.(data);
|
|
181
|
+
if (complete) {
|
|
182
|
+
call.onComplete?.();
|
|
183
|
+
call.resolve(data);
|
|
184
|
+
this.pendingRequests.delete(id);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
try {
|
|
189
|
+
const parent = await handshake;
|
|
190
|
+
this.ready = true;
|
|
191
|
+
this.parent = parent;
|
|
192
|
+
Log.i('mujian sdk client init');
|
|
193
|
+
await this.call(events_EVENT.MUJIAN_INIT);
|
|
194
|
+
const projectInfo = await this.ai.chat.project.getInfo();
|
|
195
|
+
if (projectInfo.config?.customCss) {
|
|
196
|
+
const style = document.createElement('style');
|
|
197
|
+
style.setAttribute('type', 'text/css');
|
|
198
|
+
style.setAttribute('id', 'mujian-custom-css');
|
|
199
|
+
style.textContent = projectInfo.config.customCss;
|
|
200
|
+
document.head.appendChild(style);
|
|
123
201
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
202
|
+
if (projectInfo.config?.customJs) {
|
|
203
|
+
const script = document.createElement("script");
|
|
204
|
+
script.setAttribute('type', "text/javascript");
|
|
205
|
+
script.setAttribute('id', 'mujian-custom-js');
|
|
206
|
+
script.textContent = projectInfo.config.customJs;
|
|
207
|
+
document.head.appendChild(script);
|
|
129
208
|
}
|
|
209
|
+
const config = await getConfig.call(this);
|
|
210
|
+
if (config) this._config = config;
|
|
211
|
+
} catch (error) {
|
|
212
|
+
Log.e('init error', error);
|
|
130
213
|
}
|
|
214
|
+
})();
|
|
215
|
+
return this.initPromise;
|
|
216
|
+
}
|
|
217
|
+
emit(event, data) {
|
|
218
|
+
if (!this.ready) throw new Error('Mujian is not initialized');
|
|
219
|
+
this.parent?.emit(event, data);
|
|
220
|
+
}
|
|
221
|
+
nextCallId = 0;
|
|
222
|
+
getCallId() {
|
|
223
|
+
const id = this.nextCallId;
|
|
224
|
+
this.nextCallId += 1;
|
|
225
|
+
return id;
|
|
226
|
+
}
|
|
227
|
+
async call(event, data, controller) {
|
|
228
|
+
if (!this.ready) throw new Error('Mujian is not initialized');
|
|
229
|
+
const callId = this.getCallId();
|
|
230
|
+
return new Promise((resolve, reject)=>{
|
|
231
|
+
this.pendingRequests.set(callId, {
|
|
232
|
+
resolve,
|
|
233
|
+
reject,
|
|
234
|
+
...controller
|
|
235
|
+
});
|
|
236
|
+
this.emit(event, {
|
|
237
|
+
id: callId,
|
|
238
|
+
data
|
|
239
|
+
});
|
|
240
|
+
controller?.signal?.addEventListener('abort', ()=>{
|
|
241
|
+
this.emit(events_EVENT.MUJIAN_AI_CHAT_STOP, {
|
|
242
|
+
id: callId
|
|
243
|
+
});
|
|
244
|
+
});
|
|
131
245
|
});
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
246
|
+
}
|
|
247
|
+
ai = {
|
|
248
|
+
chat: {
|
|
249
|
+
project: {
|
|
250
|
+
getInfo: getInfo.bind(this)
|
|
251
|
+
},
|
|
252
|
+
settings: {
|
|
253
|
+
persona: {
|
|
254
|
+
getActive: getActive.bind(this)
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
complete: chat_complete.bind(this),
|
|
258
|
+
applyRegex: applyRegex.bind(this),
|
|
259
|
+
renderMessage: renderMessage.bind(this),
|
|
260
|
+
continue: continueComplete.bind(this),
|
|
261
|
+
regenerate: regenerate.bind(this),
|
|
262
|
+
message: {
|
|
263
|
+
getAll: getAll.bind(this),
|
|
264
|
+
deleteOne: messageDeleteOne.bind(this),
|
|
265
|
+
editOne: messageEditOne.bind(this),
|
|
266
|
+
swipe: messageSwipe.bind(this),
|
|
267
|
+
getPage: getPage.bind(this)
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
text: {
|
|
271
|
+
complete: generate.bind(this)
|
|
272
|
+
},
|
|
273
|
+
openai: {
|
|
274
|
+
completions: {
|
|
275
|
+
create: completions.create.bind(this)
|
|
276
|
+
},
|
|
277
|
+
chat: {
|
|
278
|
+
completions: {
|
|
279
|
+
create: chat.completions.create.bind(this)
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
responses: {
|
|
283
|
+
create: responses.create.bind(this)
|
|
284
|
+
},
|
|
285
|
+
images: {
|
|
286
|
+
generate: images_images.generate.bind(this)
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
ui = {};
|
|
291
|
+
utils = {
|
|
292
|
+
clipboard: {
|
|
293
|
+
writeText: writeText.bind(this)
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
hybrid = {};
|
|
297
|
+
player = {};
|
|
298
|
+
game = {
|
|
299
|
+
assets: ()=>{
|
|
300
|
+
console.log('assets');
|
|
301
|
+
},
|
|
302
|
+
saves: {
|
|
303
|
+
saveGame: saveGame.bind(this),
|
|
304
|
+
loadGame: loadGame.bind(this)
|
|
305
|
+
},
|
|
306
|
+
ranking: ()=>{
|
|
307
|
+
console.log('ranking');
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
const generate = async function(content) {
|
|
312
|
+
return await this.call(MujianSdk.EVENT.MUJIAN_AI_TEXT_GENERATE, {
|
|
313
|
+
content
|
|
314
|
+
});
|
|
315
|
+
};
|
|
316
|
+
const chat = {
|
|
317
|
+
completions: {
|
|
318
|
+
create: async function(params, options, onData, signal) {
|
|
319
|
+
return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE, {
|
|
320
|
+
params,
|
|
321
|
+
options
|
|
322
|
+
}, {
|
|
323
|
+
onData,
|
|
324
|
+
signal
|
|
325
|
+
});
|
|
140
326
|
}
|
|
141
327
|
}
|
|
328
|
+
};
|
|
329
|
+
const completions = {
|
|
330
|
+
create: async function(params, options, onData, signal) {
|
|
331
|
+
return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE, {
|
|
332
|
+
params,
|
|
333
|
+
options
|
|
334
|
+
}, {
|
|
335
|
+
onData,
|
|
336
|
+
signal
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
const responses = {
|
|
341
|
+
create: async function(params, options, onData, signal) {
|
|
342
|
+
return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE, {
|
|
343
|
+
params,
|
|
344
|
+
options
|
|
345
|
+
}, {
|
|
346
|
+
onData,
|
|
347
|
+
signal
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
const images_images = {
|
|
352
|
+
generate: async function(params, options, onData, signal) {
|
|
353
|
+
return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE, {
|
|
354
|
+
params,
|
|
355
|
+
options
|
|
356
|
+
}, {
|
|
357
|
+
onData,
|
|
358
|
+
signal
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
class src_MujianSdk {
|
|
363
|
+
constructor(){}
|
|
364
|
+
static EVENT = events_EVENT;
|
|
365
|
+
static getInstance() {
|
|
366
|
+
if (!window.$mujian) window.$mujian = new src_MujianSdk();
|
|
367
|
+
return window.$mujian;
|
|
368
|
+
}
|
|
369
|
+
parent = null;
|
|
370
|
+
ready = false;
|
|
371
|
+
get isReady() {
|
|
372
|
+
return this.ready;
|
|
373
|
+
}
|
|
374
|
+
pendingRequests = new Map();
|
|
375
|
+
_config;
|
|
376
|
+
get openapi() {
|
|
377
|
+
return this._config?.openapi;
|
|
378
|
+
}
|
|
379
|
+
initPromise = null;
|
|
380
|
+
async init() {
|
|
381
|
+
if (this.initPromise) return this.initPromise;
|
|
382
|
+
this.initPromise = (async ()=>{
|
|
383
|
+
const handshake = new postmate.Model({
|
|
384
|
+
reply: ({ id, complete, data, error })=>{
|
|
385
|
+
const call = this.pendingRequests.get(id);
|
|
386
|
+
if (!call) return;
|
|
387
|
+
if (error) {
|
|
388
|
+
call.reject(error);
|
|
389
|
+
this.pendingRequests.delete(id);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
call.onData?.(data);
|
|
393
|
+
if (complete) {
|
|
394
|
+
call.onComplete?.();
|
|
395
|
+
call.resolve(data);
|
|
396
|
+
this.pendingRequests.delete(id);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
try {
|
|
401
|
+
const parent = await handshake;
|
|
402
|
+
this.ready = true;
|
|
403
|
+
this.parent = parent;
|
|
404
|
+
Log.i('mujian sdk client init');
|
|
405
|
+
await this.call(events_EVENT.MUJIAN_INIT);
|
|
406
|
+
const projectInfo = await this.ai.chat.project.getInfo();
|
|
407
|
+
if (projectInfo.config?.customCss) {
|
|
408
|
+
const style = document.createElement('style');
|
|
409
|
+
style.setAttribute('type', 'text/css');
|
|
410
|
+
style.setAttribute('id', 'mujian-custom-css');
|
|
411
|
+
style.textContent = projectInfo.config.customCss;
|
|
412
|
+
document.head.appendChild(style);
|
|
413
|
+
}
|
|
414
|
+
if (projectInfo.config?.customJs) {
|
|
415
|
+
const script = document.createElement("script");
|
|
416
|
+
script.setAttribute('type', "text/javascript");
|
|
417
|
+
script.setAttribute('id', 'mujian-custom-js');
|
|
418
|
+
script.textContent = projectInfo.config.customJs;
|
|
419
|
+
document.head.appendChild(script);
|
|
420
|
+
}
|
|
421
|
+
const config = await getConfig.call(this);
|
|
422
|
+
if (config) this._config = config;
|
|
423
|
+
} catch (error) {
|
|
424
|
+
Log.e('init error', error);
|
|
425
|
+
}
|
|
426
|
+
})();
|
|
427
|
+
return this.initPromise;
|
|
428
|
+
}
|
|
142
429
|
emit(event, data) {
|
|
143
430
|
if (!this.ready) throw new Error('Mujian is not initialized');
|
|
144
431
|
this.parent?.emit(event, data);
|
|
@@ -175,18 +462,13 @@ class MujianSdk {
|
|
|
175
462
|
getInfo: getInfo.bind(this)
|
|
176
463
|
},
|
|
177
464
|
settings: {
|
|
178
|
-
model: {
|
|
179
|
-
getAll: model_getAll.bind(this),
|
|
180
|
-
getActive: model_getActive.bind(this),
|
|
181
|
-
setActive: model_setActive.bind(this)
|
|
182
|
-
},
|
|
183
465
|
persona: {
|
|
184
|
-
getActive: getActive.bind(this)
|
|
185
|
-
setActive: setActive.bind(this)
|
|
466
|
+
getActive: getActive.bind(this)
|
|
186
467
|
}
|
|
187
468
|
},
|
|
188
469
|
complete: chat_complete.bind(this),
|
|
189
470
|
applyRegex: applyRegex.bind(this),
|
|
471
|
+
renderMessage: renderMessage.bind(this),
|
|
190
472
|
continue: continueComplete.bind(this),
|
|
191
473
|
regenerate: regenerate.bind(this),
|
|
192
474
|
message: {
|
|
@@ -194,17 +476,33 @@ class MujianSdk {
|
|
|
194
476
|
deleteOne: messageDeleteOne.bind(this),
|
|
195
477
|
editOne: messageEditOne.bind(this),
|
|
196
478
|
swipe: messageSwipe.bind(this),
|
|
197
|
-
|
|
479
|
+
getPage: getPage.bind(this)
|
|
198
480
|
}
|
|
199
481
|
},
|
|
200
482
|
text: {
|
|
201
483
|
complete: generate.bind(this)
|
|
484
|
+
},
|
|
485
|
+
openai: {
|
|
486
|
+
completions: {
|
|
487
|
+
create: completions.create.bind(this)
|
|
488
|
+
},
|
|
489
|
+
chat: {
|
|
490
|
+
completions: {
|
|
491
|
+
create: chat.completions.create.bind(this)
|
|
492
|
+
}
|
|
493
|
+
},
|
|
494
|
+
responses: {
|
|
495
|
+
create: responses.create.bind(this)
|
|
496
|
+
},
|
|
497
|
+
images: {
|
|
498
|
+
generate: images_images.generate.bind(this)
|
|
499
|
+
}
|
|
202
500
|
}
|
|
203
501
|
};
|
|
204
502
|
ui = {};
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
503
|
+
utils = {
|
|
504
|
+
clipboard: {
|
|
505
|
+
writeText: writeText.bind(this)
|
|
208
506
|
}
|
|
209
507
|
};
|
|
210
508
|
hybrid = {};
|
|
@@ -222,4 +520,4 @@ class MujianSdk {
|
|
|
222
520
|
}
|
|
223
521
|
};
|
|
224
522
|
}
|
|
225
|
-
export { events_EVENT as EVENT, MujianSdk };
|
|
523
|
+
export { events_EVENT as EVENT, src_MujianSdk as MujianSdk };
|