@mujian/js-sdk 0.0.6-beta.7 → 0.0.6-beta.70
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 +34 -2
- package/dist/index.d.ts +16 -39
- package/dist/index.js +345 -16
- package/dist/lite.d.ts +53 -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 +4 -0
- package/dist/modules/ai/index.d.ts +3 -2
- package/dist/modules/ai/openai/chat.d.ts +25 -0
- package/dist/modules/ai/openai/completions.d.ts +19 -0
- package/dist/modules/ai/openai/images.d.ts +19 -0
- package/dist/modules/ai/openai/index.d.ts +4 -0
- package/dist/modules/ai/openai/responses.d.ts +20 -0
- package/dist/modules/ai/text/index.d.ts +9 -2
- package/dist/modules/config.d.ts +12 -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 +2 -1
- package/dist/react/chat/useChat/message.d.ts +25 -13
- package/dist/react/components/MdRenderer/index.d.ts +6 -4
- 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 +780 -143
- package/dist/types/index.d.ts +38 -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 +139 -0
- package/dist/umd/react.js.LICENSE.txt +31 -0
- package/dist/utils/log.d.ts +4 -0
- package/package.json +6 -1
- /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
|
*/
|
|
@@ -67,5 +87,17 @@ export declare enum EVENT {
|
|
|
67
87
|
/**
|
|
68
88
|
* 获取消息提示词
|
|
69
89
|
*/
|
|
70
|
-
MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT = "mujian:ai:chat:message:getPrompt"
|
|
90
|
+
MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT = "mujian:ai:chat:message:getPrompt",
|
|
91
|
+
/**
|
|
92
|
+
* 生成图像
|
|
93
|
+
*/
|
|
94
|
+
MUJIAN_AI_OPENAI_IMAGES_GENERATE = "mujian:ai:openai:images:generate",
|
|
95
|
+
/**
|
|
96
|
+
* 写入剪贴版
|
|
97
|
+
*/
|
|
98
|
+
MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT = "mujian:utils:clipboard:writeText",
|
|
99
|
+
/**
|
|
100
|
+
* 获取配置
|
|
101
|
+
*/
|
|
102
|
+
MUJIAN_GET_CONFIG = "mujian:getConfig"
|
|
71
103
|
}
|
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,8 @@ declare class MujianSdk {
|
|
|
27
34
|
get isReady(): boolean;
|
|
28
35
|
/** @hidden */
|
|
29
36
|
private pendingRequests;
|
|
37
|
+
private _config?;
|
|
38
|
+
get config(): Config | undefined;
|
|
30
39
|
/**
|
|
31
40
|
* 初始化
|
|
32
41
|
*/
|
|
@@ -40,47 +49,15 @@ declare class MujianSdk {
|
|
|
40
49
|
/** @hidden */
|
|
41
50
|
call<Req, Resp>(event: EVENT, data?: Req, controller?: Pick<Call<Resp>, 'onData' | 'onComplete' | 'signal'>): Promise<Resp>;
|
|
42
51
|
/**
|
|
43
|
-
*
|
|
52
|
+
* @namespace ai
|
|
53
|
+
* @description 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";
|
|
@@ -16,19 +21,69 @@ var events_EVENT = /*#__PURE__*/ function(EVENT) {
|
|
|
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
23
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT"] = "mujian:ai:chat:message:getPrompt";
|
|
24
|
+
EVENT["MUJIAN_AI_OPENAI_IMAGES_GENERATE"] = "mujian:ai:openai:images:generate";
|
|
25
|
+
EVENT["MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT"] = "mujian:utils:clipboard:writeText";
|
|
26
|
+
EVENT["MUJIAN_GET_CONFIG"] = "mujian:getConfig";
|
|
19
27
|
return EVENT;
|
|
20
28
|
}({});
|
|
21
|
-
|
|
22
|
-
|
|
29
|
+
function wrapOnData(onData) {
|
|
30
|
+
let fullContent = '';
|
|
31
|
+
let buffer = '';
|
|
32
|
+
let questionId;
|
|
33
|
+
let replyId;
|
|
34
|
+
return function(data) {
|
|
35
|
+
buffer += data;
|
|
36
|
+
const lines = buffer.split('\n');
|
|
37
|
+
buffer = lines.pop() || '';
|
|
38
|
+
for (const line of lines)if (line.startsWith('data: ')) try {
|
|
39
|
+
const parsedData = JSON.parse(line.slice(6));
|
|
40
|
+
if (parsedData.question_id) questionId = parsedData.question_id;
|
|
41
|
+
if (parsedData.reply_id) replyId = parsedData.reply_id;
|
|
42
|
+
if (parsedData.isFinished) return void onData({
|
|
43
|
+
isFinished: true,
|
|
44
|
+
deltaContent: '',
|
|
45
|
+
fullContent,
|
|
46
|
+
questionId,
|
|
47
|
+
replyId
|
|
48
|
+
});
|
|
49
|
+
const deltaContent = parsedData?.choices?.[0]?.delta?.content;
|
|
50
|
+
if (deltaContent?.length > 0) {
|
|
51
|
+
fullContent += deltaContent;
|
|
52
|
+
onData({
|
|
53
|
+
isFinished: false,
|
|
54
|
+
deltaContent: deltaContent,
|
|
55
|
+
fullContent,
|
|
56
|
+
questionId,
|
|
57
|
+
replyId
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
} catch (e) {
|
|
61
|
+
onData({
|
|
62
|
+
isFinished: true,
|
|
63
|
+
error: e,
|
|
64
|
+
deltaContent: '',
|
|
65
|
+
fullContent,
|
|
66
|
+
questionId,
|
|
67
|
+
replyId
|
|
68
|
+
});
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const chat_complete = async function(message, onData, signal, option = {}) {
|
|
74
|
+
await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
|
|
23
75
|
content: message
|
|
24
76
|
}, {
|
|
25
|
-
onData,
|
|
77
|
+
onData: option.parseContent ? wrapOnData(onData) : onData,
|
|
26
78
|
signal
|
|
27
79
|
});
|
|
28
80
|
};
|
|
29
81
|
const applyRegex = async function(props) {
|
|
30
82
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_APPLY_REGEX, props);
|
|
31
83
|
};
|
|
84
|
+
const renderMessage = async function(props) {
|
|
85
|
+
return await this.call(events_EVENT.MUJIAN_AI_CHAT_RENDER_MESSAGE, props);
|
|
86
|
+
};
|
|
32
87
|
const continueComplete = async function(onData, signal) {
|
|
33
88
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
|
|
34
89
|
isContinue: true
|
|
@@ -70,6 +125,12 @@ const getPrompt = async function(messageId) {
|
|
|
70
125
|
messageId
|
|
71
126
|
});
|
|
72
127
|
};
|
|
128
|
+
async function getPage(fromCursor, pageSize) {
|
|
129
|
+
return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_GET_PAGE, {
|
|
130
|
+
fromCursor,
|
|
131
|
+
pageSize
|
|
132
|
+
});
|
|
133
|
+
}
|
|
73
134
|
const getInfo = async function() {
|
|
74
135
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_PROJECT_GET_INFO);
|
|
75
136
|
};
|
|
@@ -92,15 +153,25 @@ const model_setActive = async function(modelId) {
|
|
|
92
153
|
const model_getAll = async function() {
|
|
93
154
|
return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_GET_ALL);
|
|
94
155
|
};
|
|
95
|
-
|
|
96
|
-
return await this.call(events_EVENT.
|
|
97
|
-
|
|
98
|
-
});
|
|
99
|
-
};
|
|
156
|
+
async function getConfig() {
|
|
157
|
+
return await this.call(events_EVENT.MUJIAN_GET_CONFIG);
|
|
158
|
+
}
|
|
100
159
|
const saveGame = async function() {};
|
|
101
160
|
const loadGame = async function() {};
|
|
161
|
+
async function writeText(text) {
|
|
162
|
+
return await this.call(events_EVENT.MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT, text);
|
|
163
|
+
}
|
|
164
|
+
const Log = {
|
|
165
|
+
i (...msg) {
|
|
166
|
+
console.log('[MujianSDK] ', ...msg);
|
|
167
|
+
},
|
|
168
|
+
e (...msg) {
|
|
169
|
+
console.error('[MujianSDK] ', ...msg);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
102
172
|
class MujianSdk {
|
|
103
173
|
constructor(){}
|
|
174
|
+
static EVENT = events_EVENT;
|
|
104
175
|
static getInstance() {
|
|
105
176
|
if (!window.$mujian) window.$mujian = new MujianSdk();
|
|
106
177
|
return window.$mujian;
|
|
@@ -111,11 +182,20 @@ class MujianSdk {
|
|
|
111
182
|
return this.ready;
|
|
112
183
|
}
|
|
113
184
|
pendingRequests = new Map();
|
|
185
|
+
_config;
|
|
186
|
+
get config() {
|
|
187
|
+
return this._config;
|
|
188
|
+
}
|
|
114
189
|
async init() {
|
|
115
190
|
const handshake = new postmate.Model({
|
|
116
|
-
reply: ({ id, complete, data })=>{
|
|
191
|
+
reply: ({ id, complete, data, error })=>{
|
|
117
192
|
const call = this.pendingRequests.get(id);
|
|
118
193
|
if (!call) return;
|
|
194
|
+
if (error) {
|
|
195
|
+
call.reject(error);
|
|
196
|
+
this.pendingRequests.delete(id);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
119
199
|
call.onData?.(data);
|
|
120
200
|
if (complete) {
|
|
121
201
|
call.onComplete?.();
|
|
@@ -128,10 +208,27 @@ class MujianSdk {
|
|
|
128
208
|
const parent = await handshake;
|
|
129
209
|
this.ready = true;
|
|
130
210
|
this.parent = parent;
|
|
131
|
-
|
|
211
|
+
Log.i('mujian sdk client init');
|
|
132
212
|
await this.call(events_EVENT.MUJIAN_INIT);
|
|
213
|
+
const projectInfo = await this.ai.chat.project.getInfo();
|
|
214
|
+
if (projectInfo.config?.customCss) {
|
|
215
|
+
const style = document.createElement('style');
|
|
216
|
+
style.setAttribute('type', 'text/css');
|
|
217
|
+
style.setAttribute('id', 'mujian-custom-css');
|
|
218
|
+
style.textContent = projectInfo.config.customCss;
|
|
219
|
+
document.head.appendChild(style);
|
|
220
|
+
}
|
|
221
|
+
if (projectInfo.config?.customJs) {
|
|
222
|
+
const script = document.createElement("script");
|
|
223
|
+
script.setAttribute('type', "text/javascript");
|
|
224
|
+
script.setAttribute('id', 'mujian-custom-js');
|
|
225
|
+
script.textContent = projectInfo.config.customJs;
|
|
226
|
+
document.head.appendChild(script);
|
|
227
|
+
}
|
|
228
|
+
const config = await getConfig.call(this);
|
|
229
|
+
if (config) this._config = config;
|
|
133
230
|
} catch (error) {
|
|
134
|
-
|
|
231
|
+
Log.e('init error', error);
|
|
135
232
|
}
|
|
136
233
|
}
|
|
137
234
|
emit(event, data) {
|
|
@@ -182,6 +279,7 @@ class MujianSdk {
|
|
|
182
279
|
},
|
|
183
280
|
complete: chat_complete.bind(this),
|
|
184
281
|
applyRegex: applyRegex.bind(this),
|
|
282
|
+
renderMessage: renderMessage.bind(this),
|
|
185
283
|
continue: continueComplete.bind(this),
|
|
186
284
|
regenerate: regenerate.bind(this),
|
|
187
285
|
message: {
|
|
@@ -189,17 +287,248 @@ class MujianSdk {
|
|
|
189
287
|
deleteOne: messageDeleteOne.bind(this),
|
|
190
288
|
editOne: messageEditOne.bind(this),
|
|
191
289
|
swipe: messageSwipe.bind(this),
|
|
192
|
-
getPrompt: getPrompt.bind(this)
|
|
290
|
+
getPrompt: getPrompt.bind(this),
|
|
291
|
+
getPage: getPage.bind(this)
|
|
193
292
|
}
|
|
194
293
|
},
|
|
195
294
|
text: {
|
|
196
295
|
complete: generate.bind(this)
|
|
296
|
+
},
|
|
297
|
+
openai: {
|
|
298
|
+
completions: {
|
|
299
|
+
create: completions.create.bind(this)
|
|
300
|
+
},
|
|
301
|
+
chat: {
|
|
302
|
+
completions: {
|
|
303
|
+
create: chat.completions.create.bind(this)
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
responses: {
|
|
307
|
+
create: responses.create.bind(this)
|
|
308
|
+
},
|
|
309
|
+
images: {
|
|
310
|
+
generate: images_images.generate.bind(this)
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
ui = {};
|
|
315
|
+
utils = {
|
|
316
|
+
clipboard: {
|
|
317
|
+
writeText: writeText.bind(this)
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
hybrid = {};
|
|
321
|
+
player = {};
|
|
322
|
+
game = {
|
|
323
|
+
assets: ()=>{
|
|
324
|
+
console.log('assets');
|
|
325
|
+
},
|
|
326
|
+
saves: {
|
|
327
|
+
saveGame: saveGame.bind(this),
|
|
328
|
+
loadGame: loadGame.bind(this)
|
|
329
|
+
},
|
|
330
|
+
ranking: ()=>{
|
|
331
|
+
console.log('ranking');
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
const generate = async function(content) {
|
|
336
|
+
return await this.call(MujianSdk.EVENT.MUJIAN_AI_TEXT_GENERATE, {
|
|
337
|
+
content
|
|
338
|
+
});
|
|
339
|
+
};
|
|
340
|
+
const chat = {
|
|
341
|
+
completions: {
|
|
342
|
+
create: async function(params, options, onData, signal) {
|
|
343
|
+
return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE, {
|
|
344
|
+
params,
|
|
345
|
+
options
|
|
346
|
+
}, {
|
|
347
|
+
onData,
|
|
348
|
+
signal
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
const completions = {
|
|
354
|
+
create: async function(params, options, onData, signal) {
|
|
355
|
+
return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_COMPLETIONS_CREATE, {
|
|
356
|
+
params,
|
|
357
|
+
options
|
|
358
|
+
}, {
|
|
359
|
+
onData,
|
|
360
|
+
signal
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
const responses = {
|
|
365
|
+
create: async function(params, options, onData, signal) {
|
|
366
|
+
return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_RESPONSES_CREATE, {
|
|
367
|
+
params,
|
|
368
|
+
options
|
|
369
|
+
}, {
|
|
370
|
+
onData,
|
|
371
|
+
signal
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
const images_images = {
|
|
376
|
+
generate: async function(params, options, onData, signal) {
|
|
377
|
+
return await this.call(MujianSdk.EVENT.MUJIAN_AI_OPENAI_IMAGES_GENERATE, {
|
|
378
|
+
params,
|
|
379
|
+
options
|
|
380
|
+
}, {
|
|
381
|
+
onData,
|
|
382
|
+
signal
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
class src_MujianSdk {
|
|
387
|
+
constructor(){}
|
|
388
|
+
static EVENT = events_EVENT;
|
|
389
|
+
static getInstance() {
|
|
390
|
+
if (!window.$mujian) window.$mujian = new src_MujianSdk();
|
|
391
|
+
return window.$mujian;
|
|
392
|
+
}
|
|
393
|
+
parent = null;
|
|
394
|
+
ready = false;
|
|
395
|
+
get isReady() {
|
|
396
|
+
return this.ready;
|
|
397
|
+
}
|
|
398
|
+
pendingRequests = new Map();
|
|
399
|
+
_config;
|
|
400
|
+
get config() {
|
|
401
|
+
return this._config;
|
|
402
|
+
}
|
|
403
|
+
async init() {
|
|
404
|
+
const handshake = new postmate.Model({
|
|
405
|
+
reply: ({ id, complete, data, error })=>{
|
|
406
|
+
const call = this.pendingRequests.get(id);
|
|
407
|
+
if (!call) return;
|
|
408
|
+
if (error) {
|
|
409
|
+
call.reject(error);
|
|
410
|
+
this.pendingRequests.delete(id);
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
call.onData?.(data);
|
|
414
|
+
if (complete) {
|
|
415
|
+
call.onComplete?.();
|
|
416
|
+
call.resolve(data);
|
|
417
|
+
this.pendingRequests.delete(id);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
try {
|
|
422
|
+
const parent = await handshake;
|
|
423
|
+
this.ready = true;
|
|
424
|
+
this.parent = parent;
|
|
425
|
+
Log.i('mujian sdk client init');
|
|
426
|
+
await this.call(events_EVENT.MUJIAN_INIT);
|
|
427
|
+
const projectInfo = await this.ai.chat.project.getInfo();
|
|
428
|
+
if (projectInfo.config?.customCss) {
|
|
429
|
+
const style = document.createElement('style');
|
|
430
|
+
style.setAttribute('type', 'text/css');
|
|
431
|
+
style.setAttribute('id', 'mujian-custom-css');
|
|
432
|
+
style.textContent = projectInfo.config.customCss;
|
|
433
|
+
document.head.appendChild(style);
|
|
434
|
+
}
|
|
435
|
+
if (projectInfo.config?.customJs) {
|
|
436
|
+
const script = document.createElement("script");
|
|
437
|
+
script.setAttribute('type', "text/javascript");
|
|
438
|
+
script.setAttribute('id', 'mujian-custom-js');
|
|
439
|
+
script.textContent = projectInfo.config.customJs;
|
|
440
|
+
document.head.appendChild(script);
|
|
441
|
+
}
|
|
442
|
+
const config = await getConfig.call(this);
|
|
443
|
+
if (config) this._config = config;
|
|
444
|
+
} catch (error) {
|
|
445
|
+
Log.e('init error', error);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
emit(event, data) {
|
|
449
|
+
if (!this.ready) throw new Error('Mujian is not initialized');
|
|
450
|
+
this.parent?.emit(event, data);
|
|
451
|
+
}
|
|
452
|
+
nextCallId = 0;
|
|
453
|
+
getCallId() {
|
|
454
|
+
const id = this.nextCallId;
|
|
455
|
+
this.nextCallId += 1;
|
|
456
|
+
return id;
|
|
457
|
+
}
|
|
458
|
+
async call(event, data, controller) {
|
|
459
|
+
if (!this.ready) throw new Error('Mujian is not initialized');
|
|
460
|
+
const callId = this.getCallId();
|
|
461
|
+
return new Promise((resolve, reject)=>{
|
|
462
|
+
this.pendingRequests.set(callId, {
|
|
463
|
+
resolve,
|
|
464
|
+
reject,
|
|
465
|
+
...controller
|
|
466
|
+
});
|
|
467
|
+
this.emit(event, {
|
|
468
|
+
id: callId,
|
|
469
|
+
data
|
|
470
|
+
});
|
|
471
|
+
controller?.signal?.addEventListener('abort', ()=>{
|
|
472
|
+
this.emit(events_EVENT.MUJIAN_AI_CHAT_STOP, {
|
|
473
|
+
id: callId
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
ai = {
|
|
479
|
+
chat: {
|
|
480
|
+
project: {
|
|
481
|
+
getInfo: getInfo.bind(this)
|
|
482
|
+
},
|
|
483
|
+
settings: {
|
|
484
|
+
model: {
|
|
485
|
+
getAll: model_getAll.bind(this),
|
|
486
|
+
getActive: model_getActive.bind(this),
|
|
487
|
+
setActive: model_setActive.bind(this)
|
|
488
|
+
},
|
|
489
|
+
persona: {
|
|
490
|
+
getActive: getActive.bind(this),
|
|
491
|
+
setActive: setActive.bind(this)
|
|
492
|
+
}
|
|
493
|
+
},
|
|
494
|
+
complete: chat_complete.bind(this),
|
|
495
|
+
applyRegex: applyRegex.bind(this),
|
|
496
|
+
renderMessage: renderMessage.bind(this),
|
|
497
|
+
continue: continueComplete.bind(this),
|
|
498
|
+
regenerate: regenerate.bind(this),
|
|
499
|
+
message: {
|
|
500
|
+
getAll: getAll.bind(this),
|
|
501
|
+
deleteOne: messageDeleteOne.bind(this),
|
|
502
|
+
editOne: messageEditOne.bind(this),
|
|
503
|
+
swipe: messageSwipe.bind(this),
|
|
504
|
+
getPrompt: getPrompt.bind(this),
|
|
505
|
+
getPage: getPage.bind(this)
|
|
506
|
+
}
|
|
507
|
+
},
|
|
508
|
+
text: {
|
|
509
|
+
complete: generate.bind(this)
|
|
510
|
+
},
|
|
511
|
+
openai: {
|
|
512
|
+
completions: {
|
|
513
|
+
create: completions.create.bind(this)
|
|
514
|
+
},
|
|
515
|
+
chat: {
|
|
516
|
+
completions: {
|
|
517
|
+
create: chat.completions.create.bind(this)
|
|
518
|
+
}
|
|
519
|
+
},
|
|
520
|
+
responses: {
|
|
521
|
+
create: responses.create.bind(this)
|
|
522
|
+
},
|
|
523
|
+
images: {
|
|
524
|
+
generate: images_images.generate.bind(this)
|
|
525
|
+
}
|
|
197
526
|
}
|
|
198
527
|
};
|
|
199
528
|
ui = {};
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
529
|
+
utils = {
|
|
530
|
+
clipboard: {
|
|
531
|
+
writeText: writeText.bind(this)
|
|
203
532
|
}
|
|
204
533
|
};
|
|
205
534
|
hybrid = {};
|
|
@@ -217,4 +546,4 @@ class MujianSdk {
|
|
|
217
546
|
}
|
|
218
547
|
};
|
|
219
548
|
}
|
|
220
|
-
export { events_EVENT as EVENT, MujianSdk };
|
|
549
|
+
export { events_EVENT as EVENT, src_MujianSdk as MujianSdk };
|
package/dist/lite.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { EVENT } from './events';
|
|
2
|
+
import { Config } from './modules/config';
|
|
3
|
+
/** @hidden */
|
|
4
|
+
export { EVENT } from './events';
|
|
5
|
+
type Call<T> = {
|
|
6
|
+
resolve: (value: T) => void;
|
|
7
|
+
reject: (reason?: unknown) => void;
|
|
8
|
+
onData?: (data: T) => void;
|
|
9
|
+
onComplete?: () => void;
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
};
|
|
12
|
+
declare global {
|
|
13
|
+
interface Window {
|
|
14
|
+
$mujian_lite: MujianSdkLite;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
declare class MujianSdkLite {
|
|
18
|
+
/** @hidden */
|
|
19
|
+
private constructor();
|
|
20
|
+
/**
|
|
21
|
+
* 事件
|
|
22
|
+
* @enum {string}
|
|
23
|
+
*/
|
|
24
|
+
static EVENT: typeof EVENT;
|
|
25
|
+
static getInstance(): MujianSdkLite;
|
|
26
|
+
/** @hidden */
|
|
27
|
+
private parent;
|
|
28
|
+
/** @hidden */
|
|
29
|
+
private ready;
|
|
30
|
+
/**
|
|
31
|
+
* 是否初始化
|
|
32
|
+
*/
|
|
33
|
+
get isReady(): boolean;
|
|
34
|
+
/** @hidden */
|
|
35
|
+
private pendingRequests;
|
|
36
|
+
private _config?;
|
|
37
|
+
get config(): Config | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* 初始化
|
|
40
|
+
*/
|
|
41
|
+
init(): Promise<void>;
|
|
42
|
+
/** @hidden */
|
|
43
|
+
private ensureReady;
|
|
44
|
+
/** @hidden */
|
|
45
|
+
emit<T>(event: EVENT, data?: T): void;
|
|
46
|
+
/** @hidden */
|
|
47
|
+
private nextCallId;
|
|
48
|
+
/** @hidden */
|
|
49
|
+
private getCallId;
|
|
50
|
+
/** @hidden */
|
|
51
|
+
call<Req, Resp>(event: EVENT, data?: Req, controller?: Pick<Call<Resp>, 'onData' | 'onComplete' | 'signal'>): Promise<Resp>;
|
|
52
|
+
}
|
|
53
|
+
export default MujianSdkLite;
|
|
@@ -1,20 +1,62 @@
|
|
|
1
1
|
import type { MujianSdk } from '../../../index.ts';
|
|
2
2
|
import { Message } from '../../../react';
|
|
3
|
+
/**
|
|
4
|
+
* LLM生成返回的数据
|
|
5
|
+
*/
|
|
6
|
+
export type ParsedData = {
|
|
7
|
+
/** 是否已完成生成,最后一块数据时为true */
|
|
8
|
+
isFinished: boolean;
|
|
9
|
+
/** LLM生成的delta content */
|
|
10
|
+
deltaContent: string;
|
|
11
|
+
/** LLM生成的累积内容 */
|
|
12
|
+
fullContent: string;
|
|
13
|
+
/** 出错时非空 */
|
|
14
|
+
error?: unknown;
|
|
15
|
+
/** 用户发送消息的ID */
|
|
16
|
+
questionId?: string;
|
|
17
|
+
/** AI返回消息的ID */
|
|
18
|
+
replyId?: string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* 对话补全配置
|
|
22
|
+
*/
|
|
23
|
+
export type CompleteOption = {
|
|
24
|
+
/** 给onData传递的数据是否是解析后的,推荐true */
|
|
25
|
+
parseContent?: boolean;
|
|
26
|
+
};
|
|
3
27
|
/**
|
|
4
28
|
* 对话补全
|
|
5
|
-
* @param {string} message 消息
|
|
6
|
-
* @param {Function} onData 数据
|
|
7
|
-
* @param {AbortSignal} signal 信号
|
|
8
29
|
* @returns {Promise<string>} 返回值
|
|
9
30
|
*/
|
|
10
31
|
export declare const complete: (
|
|
11
32
|
/** @hidden */
|
|
12
|
-
this: MujianSdk,
|
|
33
|
+
this: MujianSdk,
|
|
34
|
+
/**
|
|
35
|
+
* 消息
|
|
36
|
+
*/
|
|
37
|
+
message: string,
|
|
38
|
+
/**
|
|
39
|
+
* 数据生成时的回调
|
|
40
|
+
*/
|
|
41
|
+
onData: (data: string | ParsedData) => void,
|
|
42
|
+
/**
|
|
43
|
+
* 停止生成的信号
|
|
44
|
+
*/
|
|
45
|
+
signal?: AbortSignal | undefined,
|
|
46
|
+
/**
|
|
47
|
+
* 对话补全配置
|
|
48
|
+
*/
|
|
49
|
+
option?: CompleteOption | undefined) => Promise<void>;
|
|
13
50
|
export declare const applyRegex: (this: MujianSdk, props: {
|
|
14
51
|
message: Message;
|
|
15
52
|
index: number;
|
|
16
53
|
depth: number;
|
|
17
54
|
}) => Promise<Message>;
|
|
55
|
+
export declare const renderMessage: (this: MujianSdk, props: {
|
|
56
|
+
message: Message;
|
|
57
|
+
index: number;
|
|
58
|
+
depth: number;
|
|
59
|
+
}) => Promise<Message>;
|
|
18
60
|
/**
|
|
19
61
|
* 继续
|
|
20
62
|
* @param onData 数据
|