@mujian/js-sdk 0.0.6-beta.mjv.68 → 0.1.0-beta.1
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 +25 -5
- package/dist/index.d.ts +41 -2
- package/dist/index.js +143 -184
- package/dist/lite.d.ts +73 -0
- package/dist/lite.js +124 -0
- package/dist/modules/ai/chat/chat.d.ts +18 -26
- package/dist/modules/ai/chat/index.d.ts +1 -1
- package/dist/modules/ai/chat/message/index.d.ts +7 -16
- 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/openai/chat.d.ts +0 -3
- package/dist/modules/ai/openai/completions.d.ts +0 -2
- package/dist/modules/ai/openai/images.d.ts +0 -2
- package/dist/modules/ai/openai/responses.d.ts +0 -2
- package/dist/modules/config.d.ts +17 -0
- package/dist/react/chat/useChat/index.d.ts +27 -5
- package/dist/react/chat/useChat/inner/chatStreaming.d.ts +8 -4
- package/dist/react/chat/useChat/inner/stream.d.ts +25 -2
- package/dist/react/chat/useChat/message.d.ts +27 -38
- package/dist/react/components/MdRenderer/index.d.ts +3 -2
- package/dist/react/components/MujianProvider/index.d.ts +4 -0
- package/dist/react/components/Thread/MessageItem.d.ts +6 -0
- package/dist/react/components/index.d.ts +1 -1
- package/dist/react/index.d.ts +1 -0
- package/dist/react/mjEngine/index.d.ts +48 -0
- package/dist/react.js +443 -343
- package/dist/types/index.d.ts +1 -0
- package/dist/umd/index.js +1 -791
- package/dist/umd/lite.js +2 -0
- package/dist/umd/lite.js.LICENSE.txt +7 -0
- package/dist/umd/react.css +1 -343
- package/dist/umd/react.js +95 -8807
- package/dist/umd/react.js.LICENSE.txt +1 -1
- package/dist/utils/is-network-error.d.ts +1 -0
- package/package.json +14 -6
- package/dist/modules/ai/chat/settings/model.d.ts +0 -24
package/dist/events/index.d.ts
CHANGED
|
@@ -52,6 +52,26 @@ export declare enum EVENT {
|
|
|
52
52
|
* 获取项目信息
|
|
53
53
|
*/
|
|
54
54
|
MUJIAN_AI_CHAT_PROJECT_GET_INFO = "mujian:ai:chat:project:getInfo",
|
|
55
|
+
/**
|
|
56
|
+
* 发送消息V2
|
|
57
|
+
*/
|
|
58
|
+
MUJIAN_AI_CHAT_V2_COMPLETE = "mujian:ai:chat_v2:complete",
|
|
59
|
+
/**
|
|
60
|
+
* 恢复生成消息V2
|
|
61
|
+
*/
|
|
62
|
+
MUJIAN_AI_CHAT_V2_RECOVER = "mujian:ai:chat_v2:recover",
|
|
63
|
+
/**
|
|
64
|
+
* 停止生成消息V2
|
|
65
|
+
*/
|
|
66
|
+
MUJIAN_AI_CHAT_V2_STOP = "mujian:ai:chat_v2:stop",
|
|
67
|
+
/**
|
|
68
|
+
* 获取对话分页
|
|
69
|
+
*/
|
|
70
|
+
MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE = "mujian:ai:chat_v2:message:getPage",
|
|
71
|
+
/**
|
|
72
|
+
* 对消息应用正则表达式并渲染模板
|
|
73
|
+
*/
|
|
74
|
+
MUJIAN_AI_CHAT_V2_RENDER_MESSAGE = "mujian:ai:chat_v2:renderMessage",
|
|
55
75
|
/**
|
|
56
76
|
* 获取个人设置
|
|
57
77
|
*/
|
|
@@ -84,10 +104,6 @@ export declare enum EVENT {
|
|
|
84
104
|
* 滑动消息
|
|
85
105
|
*/
|
|
86
106
|
MUJIAN_AI_CHAT_MESSAGE_SWIPE = "mujian:ai:chat:message:swipe",
|
|
87
|
-
/**
|
|
88
|
-
* 获取消息提示词
|
|
89
|
-
*/
|
|
90
|
-
MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT = "mujian:ai:chat:message:getPrompt",
|
|
91
107
|
/**
|
|
92
108
|
* 生成图像
|
|
93
109
|
*/
|
|
@@ -95,5 +111,9 @@ export declare enum EVENT {
|
|
|
95
111
|
/**
|
|
96
112
|
* 写入剪贴版
|
|
97
113
|
*/
|
|
98
|
-
MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT = "mujian:utils:clipboard:writeText"
|
|
114
|
+
MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT = "mujian:utils:clipboard:writeText",
|
|
115
|
+
/**
|
|
116
|
+
* 获取配置
|
|
117
|
+
*/
|
|
118
|
+
MUJIAN_GET_CONFIG = "mujian:getConfig"
|
|
99
119
|
}
|
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> = {
|
|
@@ -33,6 +34,9 @@ declare class MujianSdk {
|
|
|
33
34
|
get isReady(): boolean;
|
|
34
35
|
/** @hidden */
|
|
35
36
|
private pendingRequests;
|
|
37
|
+
private _config?;
|
|
38
|
+
get openapi(): Config['openapi'] | undefined;
|
|
39
|
+
private initPromise;
|
|
36
40
|
/**
|
|
37
41
|
* 初始化
|
|
38
42
|
*/
|
|
@@ -47,9 +51,44 @@ declare class MujianSdk {
|
|
|
47
51
|
call<Req, Resp>(event: EVENT, data?: Req, controller?: Pick<Call<Resp>, 'onData' | 'onComplete' | 'signal'>): Promise<Resp>;
|
|
48
52
|
/**
|
|
49
53
|
* @namespace ai
|
|
50
|
-
* @description AI
|
|
51
54
|
*/
|
|
52
|
-
ai:
|
|
55
|
+
ai: {
|
|
56
|
+
chat: {
|
|
57
|
+
project: {
|
|
58
|
+
getInfo: () => Promise<import("./types").ProjectInfo>;
|
|
59
|
+
};
|
|
60
|
+
settings: {
|
|
61
|
+
persona: {
|
|
62
|
+
getActive: () => Promise<import("./types").PersonaInfo>;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
complete: (message: string, conversationVersion: number, onData: (data: string) => void) => Promise<void>;
|
|
66
|
+
renderMessage: (props: {
|
|
67
|
+
message: import("./react").Message;
|
|
68
|
+
index: number;
|
|
69
|
+
depth: number;
|
|
70
|
+
}) => Promise<import("./react").Message>;
|
|
71
|
+
continue: (conversationVersion: number, onData: (data: string) => void) => Promise<string>;
|
|
72
|
+
regenerate: (conversationVersion: number, onData: (data: string) => void) => Promise<string>;
|
|
73
|
+
stop: (conversationVersion: number, assisMsgId: string) => Promise<string>;
|
|
74
|
+
recover: (conversationVersion: number, assisMsgId: string, onData: (data: string) => void) => Promise<string>;
|
|
75
|
+
message: {
|
|
76
|
+
deleteOne: (messageId: string) => Promise<unknown>;
|
|
77
|
+
editOne: (messageId: string, content: string) => Promise<unknown>;
|
|
78
|
+
swipe: (messageId: string, swipeId: number) => Promise<unknown>;
|
|
79
|
+
getPage: (fromCursor: import("./types").Cursor | undefined, pageSize: number) => Promise<{
|
|
80
|
+
nextCursor?: import("./types").Cursor;
|
|
81
|
+
messages: Array<import("./react").Message>;
|
|
82
|
+
total?: number;
|
|
83
|
+
conversationVersion: number;
|
|
84
|
+
}>;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
text: {
|
|
88
|
+
complete: (content: string) => Promise<void>;
|
|
89
|
+
};
|
|
90
|
+
openai: any;
|
|
91
|
+
};
|
|
53
92
|
ui: {};
|
|
54
93
|
utils: {
|
|
55
94
|
clipboard: {
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,11 @@ var events_EVENT = /*#__PURE__*/ function(EVENT) {
|
|
|
12
12
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_ALL"] = "mujian:ai:chat:message:getAll";
|
|
13
13
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_PAGE"] = "mujian:ai:chat:message:getPage";
|
|
14
14
|
EVENT["MUJIAN_AI_CHAT_PROJECT_GET_INFO"] = "mujian:ai:chat:project:getInfo";
|
|
15
|
+
EVENT["MUJIAN_AI_CHAT_V2_COMPLETE"] = "mujian:ai:chat_v2:complete";
|
|
16
|
+
EVENT["MUJIAN_AI_CHAT_V2_RECOVER"] = "mujian:ai:chat_v2:recover";
|
|
17
|
+
EVENT["MUJIAN_AI_CHAT_V2_STOP"] = "mujian:ai:chat_v2:stop";
|
|
18
|
+
EVENT["MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE"] = "mujian:ai:chat_v2:message:getPage";
|
|
19
|
+
EVENT["MUJIAN_AI_CHAT_V2_RENDER_MESSAGE"] = "mujian:ai:chat_v2:renderMessage";
|
|
15
20
|
EVENT["MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE"] = "mujian:ai:settings:persona:getActive";
|
|
16
21
|
EVENT["MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE"] = "mujian:ai:settings:persona:setActive";
|
|
17
22
|
EVENT["MUJIAN_AI_SETTINGS_MODEL_GET_ALL"] = "mujian:ai:settings:model:getAll";
|
|
@@ -20,87 +25,51 @@ var events_EVENT = /*#__PURE__*/ function(EVENT) {
|
|
|
20
25
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE"] = "mujian:ai:chat:message:deleteOne";
|
|
21
26
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE"] = "mujian:ai:chat:message:editOne";
|
|
22
27
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_SWIPE"] = "mujian:ai:chat:message:swipe";
|
|
23
|
-
EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT"] = "mujian:ai:chat:message:getPrompt";
|
|
24
28
|
EVENT["MUJIAN_AI_OPENAI_IMAGES_GENERATE"] = "mujian:ai:openai:images:generate";
|
|
25
29
|
EVENT["MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT"] = "mujian:utils:clipboard:writeText";
|
|
30
|
+
EVENT["MUJIAN_GET_CONFIG"] = "mujian:getConfig";
|
|
26
31
|
return EVENT;
|
|
27
32
|
}({});
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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, {
|
|
74
|
-
content: message
|
|
33
|
+
const chat_complete = async function(message, conversationVersion, onData) {
|
|
34
|
+
await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_COMPLETE, {
|
|
35
|
+
content: message,
|
|
36
|
+
conversationVersion
|
|
75
37
|
}, {
|
|
76
|
-
onData
|
|
77
|
-
signal
|
|
38
|
+
onData
|
|
78
39
|
});
|
|
79
40
|
};
|
|
80
|
-
const applyRegex = async function(props) {
|
|
81
|
-
return await this.call(events_EVENT.MUJIAN_AI_CHAT_APPLY_REGEX, props);
|
|
82
|
-
};
|
|
83
41
|
const renderMessage = async function(props) {
|
|
84
|
-
return await this.call(events_EVENT.
|
|
42
|
+
return await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_RENDER_MESSAGE, props);
|
|
85
43
|
};
|
|
86
|
-
const continueComplete = async function(
|
|
87
|
-
return await this.call(events_EVENT.
|
|
88
|
-
isContinue: true
|
|
44
|
+
const continueComplete = async function(conversationVersion, onData) {
|
|
45
|
+
return await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_COMPLETE, {
|
|
46
|
+
isContinue: true,
|
|
47
|
+
conversationVersion
|
|
89
48
|
}, {
|
|
90
|
-
onData
|
|
91
|
-
signal
|
|
49
|
+
onData
|
|
92
50
|
});
|
|
93
51
|
};
|
|
94
|
-
const regenerate = async function(
|
|
95
|
-
return await this.call(events_EVENT.
|
|
96
|
-
isRegenerate: true
|
|
52
|
+
const regenerate = async function(conversationVersion, onData) {
|
|
53
|
+
return await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_COMPLETE, {
|
|
54
|
+
isRegenerate: true,
|
|
55
|
+
conversationVersion
|
|
97
56
|
}, {
|
|
98
|
-
onData
|
|
99
|
-
|
|
57
|
+
onData
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
const stop = async function(conversationVersion, assisMsgId) {
|
|
61
|
+
return await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_STOP, {
|
|
62
|
+
conversationVersion,
|
|
63
|
+
assisMsgId
|
|
100
64
|
});
|
|
101
65
|
};
|
|
102
|
-
const
|
|
103
|
-
return await this.call(events_EVENT.
|
|
66
|
+
const recover = async function(conversationVersion, assisMsgId, onData) {
|
|
67
|
+
return await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_RECOVER, {
|
|
68
|
+
conversationVersion,
|
|
69
|
+
assisMsgId
|
|
70
|
+
}, {
|
|
71
|
+
onData
|
|
72
|
+
});
|
|
104
73
|
};
|
|
105
74
|
const messageDeleteOne = async function(messageId) {
|
|
106
75
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE, {
|
|
@@ -119,13 +88,8 @@ const messageSwipe = async function(messageId, swipeId) {
|
|
|
119
88
|
swipeId
|
|
120
89
|
});
|
|
121
90
|
};
|
|
122
|
-
const getPrompt = async function(messageId) {
|
|
123
|
-
return await this.call(events_EVENT.MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT, {
|
|
124
|
-
messageId
|
|
125
|
-
});
|
|
126
|
-
};
|
|
127
91
|
async function getPage(fromCursor, pageSize) {
|
|
128
|
-
return await this.call(events_EVENT.
|
|
92
|
+
return await this.call(events_EVENT.MUJIAN_AI_CHAT_V2_MESSAGE_GET_PAGE, {
|
|
129
93
|
fromCursor,
|
|
130
94
|
pageSize
|
|
131
95
|
});
|
|
@@ -136,22 +100,9 @@ const getInfo = async function() {
|
|
|
136
100
|
const getActive = async function() {
|
|
137
101
|
return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE);
|
|
138
102
|
};
|
|
139
|
-
|
|
140
|
-
return await this.call(events_EVENT.
|
|
141
|
-
|
|
142
|
-
});
|
|
143
|
-
};
|
|
144
|
-
const model_getActive = async function() {
|
|
145
|
-
return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE);
|
|
146
|
-
};
|
|
147
|
-
const model_setActive = async function(modelId) {
|
|
148
|
-
return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE, {
|
|
149
|
-
modelId
|
|
150
|
-
});
|
|
151
|
-
};
|
|
152
|
-
const model_getAll = async function() {
|
|
153
|
-
return await this.call(events_EVENT.MUJIAN_AI_SETTINGS_MODEL_GET_ALL);
|
|
154
|
-
};
|
|
103
|
+
async function getConfig() {
|
|
104
|
+
return await this.call(events_EVENT.MUJIAN_GET_CONFIG);
|
|
105
|
+
}
|
|
155
106
|
const saveGame = async function() {};
|
|
156
107
|
const loadGame = async function() {};
|
|
157
108
|
async function writeText(text) {
|
|
@@ -178,48 +129,59 @@ class MujianSdk {
|
|
|
178
129
|
return this.ready;
|
|
179
130
|
}
|
|
180
131
|
pendingRequests = new Map();
|
|
132
|
+
_config;
|
|
133
|
+
get openapi() {
|
|
134
|
+
return this._config?.openapi;
|
|
135
|
+
}
|
|
136
|
+
initPromise = null;
|
|
181
137
|
async init() {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
call
|
|
188
|
-
|
|
189
|
-
|
|
138
|
+
if (this.initPromise) return this.initPromise;
|
|
139
|
+
this.initPromise = (async ()=>{
|
|
140
|
+
const handshake = new postmate.Model({
|
|
141
|
+
reply: ({ id, complete, data, error })=>{
|
|
142
|
+
const call = this.pendingRequests.get(id);
|
|
143
|
+
if (!call) return;
|
|
144
|
+
if (error) {
|
|
145
|
+
call.reject(error);
|
|
146
|
+
this.pendingRequests.delete(id);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
call.onData?.(data);
|
|
150
|
+
if (complete) {
|
|
151
|
+
call.onComplete?.();
|
|
152
|
+
call.resolve(data);
|
|
153
|
+
this.pendingRequests.delete(id);
|
|
154
|
+
}
|
|
190
155
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
156
|
+
});
|
|
157
|
+
try {
|
|
158
|
+
const parent = await handshake;
|
|
159
|
+
this.ready = true;
|
|
160
|
+
this.parent = parent;
|
|
161
|
+
Log.i('mujian sdk client init');
|
|
162
|
+
await this.call(events_EVENT.MUJIAN_INIT);
|
|
163
|
+
const projectInfo = await this.ai.chat.project.getInfo();
|
|
164
|
+
if (projectInfo.config?.customCss) {
|
|
165
|
+
const style = document.createElement('style');
|
|
166
|
+
style.setAttribute('type', 'text/css');
|
|
167
|
+
style.setAttribute('id', 'mujian-custom-css');
|
|
168
|
+
style.textContent = projectInfo.config.customCss;
|
|
169
|
+
document.head.appendChild(style);
|
|
196
170
|
}
|
|
171
|
+
if (projectInfo.config?.customJs) {
|
|
172
|
+
const script = document.createElement("script");
|
|
173
|
+
script.setAttribute('type', "text/javascript");
|
|
174
|
+
script.setAttribute('id', 'mujian-custom-js');
|
|
175
|
+
script.textContent = projectInfo.config.customJs;
|
|
176
|
+
document.head.appendChild(script);
|
|
177
|
+
}
|
|
178
|
+
const config = await getConfig.call(this);
|
|
179
|
+
if (config) this._config = config;
|
|
180
|
+
} catch (error) {
|
|
181
|
+
Log.e('init error', error);
|
|
197
182
|
}
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
const parent = await handshake;
|
|
201
|
-
this.ready = true;
|
|
202
|
-
this.parent = parent;
|
|
203
|
-
Log.i('mujian sdk client init');
|
|
204
|
-
await this.call(events_EVENT.MUJIAN_INIT);
|
|
205
|
-
const projectInfo = await this.ai.chat.project.getInfo();
|
|
206
|
-
if (projectInfo.config?.customCss) {
|
|
207
|
-
const style = document.createElement('style');
|
|
208
|
-
style.setAttribute('type', 'text/css');
|
|
209
|
-
style.setAttribute('id', 'mujian-custom-css');
|
|
210
|
-
style.textContent = projectInfo.config.customCss;
|
|
211
|
-
document.head.appendChild(style);
|
|
212
|
-
}
|
|
213
|
-
if (projectInfo.config?.customJs) {
|
|
214
|
-
const script = document.createElement("script");
|
|
215
|
-
script.setAttribute('type', "text/javascript");
|
|
216
|
-
script.setAttribute('id', 'mujian-custom-js');
|
|
217
|
-
script.textContent = projectInfo.config.customJs;
|
|
218
|
-
document.head.appendChild(script);
|
|
219
|
-
}
|
|
220
|
-
} catch (error) {
|
|
221
|
-
Log.e('init error', error);
|
|
222
|
-
}
|
|
183
|
+
})();
|
|
184
|
+
return this.initPromise;
|
|
223
185
|
}
|
|
224
186
|
emit(event, data) {
|
|
225
187
|
if (!this.ready) throw new Error('Mujian is not initialized');
|
|
@@ -257,27 +219,20 @@ class MujianSdk {
|
|
|
257
219
|
getInfo: getInfo.bind(this)
|
|
258
220
|
},
|
|
259
221
|
settings: {
|
|
260
|
-
model: {
|
|
261
|
-
getAll: model_getAll.bind(this),
|
|
262
|
-
getActive: model_getActive.bind(this),
|
|
263
|
-
setActive: model_setActive.bind(this)
|
|
264
|
-
},
|
|
265
222
|
persona: {
|
|
266
|
-
getActive: getActive.bind(this)
|
|
267
|
-
setActive: setActive.bind(this)
|
|
223
|
+
getActive: getActive.bind(this)
|
|
268
224
|
}
|
|
269
225
|
},
|
|
270
226
|
complete: chat_complete.bind(this),
|
|
271
|
-
applyRegex: applyRegex.bind(this),
|
|
272
227
|
renderMessage: renderMessage.bind(this),
|
|
273
228
|
continue: continueComplete.bind(this),
|
|
274
229
|
regenerate: regenerate.bind(this),
|
|
230
|
+
stop: stop.bind(this),
|
|
231
|
+
recover: recover.bind(this),
|
|
275
232
|
message: {
|
|
276
|
-
getAll: getAll.bind(this),
|
|
277
233
|
deleteOne: messageDeleteOne.bind(this),
|
|
278
234
|
editOne: messageEditOne.bind(this),
|
|
279
235
|
swipe: messageSwipe.bind(this),
|
|
280
|
-
getPrompt: getPrompt.bind(this),
|
|
281
236
|
getPage: getPage.bind(this)
|
|
282
237
|
}
|
|
283
238
|
},
|
|
@@ -386,48 +341,59 @@ class src_MujianSdk {
|
|
|
386
341
|
return this.ready;
|
|
387
342
|
}
|
|
388
343
|
pendingRequests = new Map();
|
|
344
|
+
_config;
|
|
345
|
+
get openapi() {
|
|
346
|
+
return this._config?.openapi;
|
|
347
|
+
}
|
|
348
|
+
initPromise = null;
|
|
389
349
|
async init() {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
call
|
|
396
|
-
|
|
397
|
-
|
|
350
|
+
if (this.initPromise) return this.initPromise;
|
|
351
|
+
this.initPromise = (async ()=>{
|
|
352
|
+
const handshake = new postmate.Model({
|
|
353
|
+
reply: ({ id, complete, data, error })=>{
|
|
354
|
+
const call = this.pendingRequests.get(id);
|
|
355
|
+
if (!call) return;
|
|
356
|
+
if (error) {
|
|
357
|
+
call.reject(error);
|
|
358
|
+
this.pendingRequests.delete(id);
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
call.onData?.(data);
|
|
362
|
+
if (complete) {
|
|
363
|
+
call.onComplete?.();
|
|
364
|
+
call.resolve(data);
|
|
365
|
+
this.pendingRequests.delete(id);
|
|
366
|
+
}
|
|
398
367
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
368
|
+
});
|
|
369
|
+
try {
|
|
370
|
+
const parent = await handshake;
|
|
371
|
+
this.ready = true;
|
|
372
|
+
this.parent = parent;
|
|
373
|
+
Log.i('mujian sdk client init');
|
|
374
|
+
await this.call(events_EVENT.MUJIAN_INIT);
|
|
375
|
+
const projectInfo = await this.ai.chat.project.getInfo();
|
|
376
|
+
if (projectInfo.config?.customCss) {
|
|
377
|
+
const style = document.createElement('style');
|
|
378
|
+
style.setAttribute('type', 'text/css');
|
|
379
|
+
style.setAttribute('id', 'mujian-custom-css');
|
|
380
|
+
style.textContent = projectInfo.config.customCss;
|
|
381
|
+
document.head.appendChild(style);
|
|
404
382
|
}
|
|
383
|
+
if (projectInfo.config?.customJs) {
|
|
384
|
+
const script = document.createElement("script");
|
|
385
|
+
script.setAttribute('type', "text/javascript");
|
|
386
|
+
script.setAttribute('id', 'mujian-custom-js');
|
|
387
|
+
script.textContent = projectInfo.config.customJs;
|
|
388
|
+
document.head.appendChild(script);
|
|
389
|
+
}
|
|
390
|
+
const config = await getConfig.call(this);
|
|
391
|
+
if (config) this._config = config;
|
|
392
|
+
} catch (error) {
|
|
393
|
+
Log.e('init error', error);
|
|
405
394
|
}
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
const parent = await handshake;
|
|
409
|
-
this.ready = true;
|
|
410
|
-
this.parent = parent;
|
|
411
|
-
Log.i('mujian sdk client init');
|
|
412
|
-
await this.call(events_EVENT.MUJIAN_INIT);
|
|
413
|
-
const projectInfo = await this.ai.chat.project.getInfo();
|
|
414
|
-
if (projectInfo.config?.customCss) {
|
|
415
|
-
const style = document.createElement('style');
|
|
416
|
-
style.setAttribute('type', 'text/css');
|
|
417
|
-
style.setAttribute('id', 'mujian-custom-css');
|
|
418
|
-
style.textContent = projectInfo.config.customCss;
|
|
419
|
-
document.head.appendChild(style);
|
|
420
|
-
}
|
|
421
|
-
if (projectInfo.config?.customJs) {
|
|
422
|
-
const script = document.createElement("script");
|
|
423
|
-
script.setAttribute('type', "text/javascript");
|
|
424
|
-
script.setAttribute('id', 'mujian-custom-js');
|
|
425
|
-
script.textContent = projectInfo.config.customJs;
|
|
426
|
-
document.head.appendChild(script);
|
|
427
|
-
}
|
|
428
|
-
} catch (error) {
|
|
429
|
-
Log.e('init error', error);
|
|
430
|
-
}
|
|
395
|
+
})();
|
|
396
|
+
return this.initPromise;
|
|
431
397
|
}
|
|
432
398
|
emit(event, data) {
|
|
433
399
|
if (!this.ready) throw new Error('Mujian is not initialized');
|
|
@@ -465,27 +431,20 @@ class src_MujianSdk {
|
|
|
465
431
|
getInfo: getInfo.bind(this)
|
|
466
432
|
},
|
|
467
433
|
settings: {
|
|
468
|
-
model: {
|
|
469
|
-
getAll: model_getAll.bind(this),
|
|
470
|
-
getActive: model_getActive.bind(this),
|
|
471
|
-
setActive: model_setActive.bind(this)
|
|
472
|
-
},
|
|
473
434
|
persona: {
|
|
474
|
-
getActive: getActive.bind(this)
|
|
475
|
-
setActive: setActive.bind(this)
|
|
435
|
+
getActive: getActive.bind(this)
|
|
476
436
|
}
|
|
477
437
|
},
|
|
478
438
|
complete: chat_complete.bind(this),
|
|
479
|
-
applyRegex: applyRegex.bind(this),
|
|
480
439
|
renderMessage: renderMessage.bind(this),
|
|
481
440
|
continue: continueComplete.bind(this),
|
|
482
441
|
regenerate: regenerate.bind(this),
|
|
442
|
+
stop: stop.bind(this),
|
|
443
|
+
recover: recover.bind(this),
|
|
483
444
|
message: {
|
|
484
|
-
getAll: getAll.bind(this),
|
|
485
445
|
deleteOne: messageDeleteOne.bind(this),
|
|
486
446
|
editOne: messageEditOne.bind(this),
|
|
487
447
|
swipe: messageSwipe.bind(this),
|
|
488
|
-
getPrompt: getPrompt.bind(this),
|
|
489
448
|
getPage: getPage.bind(this)
|
|
490
449
|
}
|
|
491
450
|
},
|
package/dist/lite.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
/**
|
|
18
|
+
* @class MujianSdkLite
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* (window as any).$mujian_lite.init().then(() => {
|
|
22
|
+
* console.log((window as any).$mujian_lite.openapi.baseURL); // API 的 URL
|
|
23
|
+
* console.log((window as any).$mujian_lite.openapi.apiKey); // API 的 KEY (当前用户唯一)
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare class MujianSdkLite {
|
|
28
|
+
/** @hidden */
|
|
29
|
+
private constructor();
|
|
30
|
+
/**
|
|
31
|
+
* 事件
|
|
32
|
+
* @enum {string}
|
|
33
|
+
*/
|
|
34
|
+
/** @hidden */
|
|
35
|
+
static EVENT: typeof EVENT;
|
|
36
|
+
/** @hidden */
|
|
37
|
+
static getInstance(): MujianSdkLite;
|
|
38
|
+
/** @hidden */
|
|
39
|
+
private parent;
|
|
40
|
+
/** @hidden */
|
|
41
|
+
private ready;
|
|
42
|
+
/**
|
|
43
|
+
* 是否初始化
|
|
44
|
+
*/
|
|
45
|
+
/** @hidden */
|
|
46
|
+
get isReady(): boolean;
|
|
47
|
+
/** @hidden */
|
|
48
|
+
private pendingRequests;
|
|
49
|
+
/** @hidden */
|
|
50
|
+
private _config?;
|
|
51
|
+
/**
|
|
52
|
+
* 获取 API 配置
|
|
53
|
+
* @returns {{ baseURL: string, apiKey: string }} { baseURL: API 的 URL, apiKey: API 的 KEY }
|
|
54
|
+
*/
|
|
55
|
+
get openapi(): Config['openapi'] | undefined;
|
|
56
|
+
/** @hidden */
|
|
57
|
+
private initPromise;
|
|
58
|
+
/**
|
|
59
|
+
* 初始化
|
|
60
|
+
*/
|
|
61
|
+
init(): Promise<void>;
|
|
62
|
+
/** @hidden */
|
|
63
|
+
private ensureReady;
|
|
64
|
+
/** @hidden */
|
|
65
|
+
emit<T>(event: EVENT, data?: T): void;
|
|
66
|
+
/** @hidden */
|
|
67
|
+
private nextCallId;
|
|
68
|
+
/** @hidden */
|
|
69
|
+
private getCallId;
|
|
70
|
+
/** @hidden */
|
|
71
|
+
call<Req, Resp>(event: EVENT, data?: Req, controller?: Pick<Call<Resp>, 'onData' | 'onComplete' | 'signal'>): Promise<Resp>;
|
|
72
|
+
}
|
|
73
|
+
export default MujianSdkLite;
|