@mujian/js-sdk 0.0.6-beta.35 → 0.0.6-beta.36
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/index.d.ts +2 -1
- package/dist/index.js +29 -2
- package/dist/modules/ai/chat/chat.d.ts +35 -4
- package/dist/modules/ai/chat/index.d.ts +1 -1
- package/dist/react.js +30 -2
- package/dist/umd/index.js +29 -2
- package/dist/umd/react.js +30 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EVENT } from './events';
|
|
2
|
+
import * as ai from './modules/ai';
|
|
2
3
|
/** @hidden */
|
|
3
4
|
export { EVENT } from './events';
|
|
4
5
|
type Call<T> = {
|
|
@@ -58,7 +59,7 @@ declare class MujianSdk {
|
|
|
58
59
|
setActive: (personaId: string) => Promise<void>;
|
|
59
60
|
};
|
|
60
61
|
};
|
|
61
|
-
complete: (message: string, onData: (data: string) => void, signal?: AbortSignal | undefined) => Promise<string>;
|
|
62
|
+
complete: (message: string, onData: (data: string | ai.chat.ParsedData) => void, signal?: AbortSignal | undefined, option?: ai.chat.CompleteOption | undefined) => Promise<string>;
|
|
62
63
|
applyRegex: (props: {
|
|
63
64
|
message: import("./react").Message;
|
|
64
65
|
index: number;
|
package/dist/index.js
CHANGED
|
@@ -18,11 +18,38 @@ var events_EVENT = /*#__PURE__*/ function(EVENT) {
|
|
|
18
18
|
EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT"] = "mujian:ai:chat:message:getPrompt";
|
|
19
19
|
return EVENT;
|
|
20
20
|
}({});
|
|
21
|
-
|
|
21
|
+
function wrapOnData(onData) {
|
|
22
|
+
let buffer = '';
|
|
23
|
+
return function(data) {
|
|
24
|
+
buffer += data;
|
|
25
|
+
const lines = buffer.split('\n');
|
|
26
|
+
buffer = lines.pop() || '';
|
|
27
|
+
for (const line of lines)if (line.startsWith('data: ')) try {
|
|
28
|
+
const parsedData = JSON.parse(line.slice(6));
|
|
29
|
+
if (parsedData.isFinished) return void onData({
|
|
30
|
+
isFinished: true,
|
|
31
|
+
content: ''
|
|
32
|
+
});
|
|
33
|
+
const content = parsedData?.choices?.[0]?.delta?.content;
|
|
34
|
+
if (content?.length > 0) onData({
|
|
35
|
+
isFinished: false,
|
|
36
|
+
content
|
|
37
|
+
});
|
|
38
|
+
} catch (e) {
|
|
39
|
+
onData({
|
|
40
|
+
isFinished: true,
|
|
41
|
+
error: e,
|
|
42
|
+
content: ''
|
|
43
|
+
});
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const chat_complete = async function(message, onData, signal, option = {}) {
|
|
22
49
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
|
|
23
50
|
content: message
|
|
24
51
|
}, {
|
|
25
|
-
onData,
|
|
52
|
+
onData: option.parseContent ? wrapOnData(onData) : onData,
|
|
26
53
|
signal
|
|
27
54
|
});
|
|
28
55
|
};
|
|
@@ -1,15 +1,46 @@
|
|
|
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
|
+
content: string;
|
|
11
|
+
/** 出错时非空 */
|
|
12
|
+
error?: unknown;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* 对话补全配置
|
|
16
|
+
*/
|
|
17
|
+
export type CompleteOption = {
|
|
18
|
+
/** 给onData传递的数据是否是解析后的,推荐true */
|
|
19
|
+
parseContent?: boolean;
|
|
20
|
+
};
|
|
3
21
|
/**
|
|
4
22
|
* 对话补全
|
|
5
|
-
* @param {string} message 消息
|
|
6
|
-
* @param {Function} onData 数据
|
|
7
|
-
* @param {AbortSignal} signal 信号
|
|
8
23
|
* @returns {Promise<string>} 返回值
|
|
9
24
|
*/
|
|
10
25
|
export declare const complete: (
|
|
11
26
|
/** @hidden */
|
|
12
|
-
this: MujianSdk,
|
|
27
|
+
this: MujianSdk,
|
|
28
|
+
/**
|
|
29
|
+
* 消息
|
|
30
|
+
*/
|
|
31
|
+
message: string,
|
|
32
|
+
/**
|
|
33
|
+
* 数据生成时的回调
|
|
34
|
+
*/
|
|
35
|
+
onData: (data: string | ParsedData) => void,
|
|
36
|
+
/**
|
|
37
|
+
* 停止生成的信号
|
|
38
|
+
*/
|
|
39
|
+
signal?: AbortSignal | undefined,
|
|
40
|
+
/**
|
|
41
|
+
* 对话补全配置
|
|
42
|
+
*/
|
|
43
|
+
option?: CompleteOption) => Promise<string>;
|
|
13
44
|
export declare const applyRegex: (this: MujianSdk, props: {
|
|
14
45
|
message: Message;
|
|
15
46
|
index: number;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { applyRegex, complete, continueComplete, regenerate } from './chat';
|
|
1
|
+
export { applyRegex, type CompleteOption, complete, continueComplete, type ParsedData, regenerate, } from './chat';
|
|
2
2
|
export * as message from './message';
|
|
3
3
|
export * as project from './project';
|
|
4
4
|
export * as settings from './settings';
|
package/dist/react.js
CHANGED
|
@@ -432,11 +432,38 @@ const MdRendererBase = ({ content })=>{
|
|
|
432
432
|
const MdRenderer = /*#__PURE__*/ react.memo(MdRendererBase, (prev, next)=>prev.content === next.content);
|
|
433
433
|
MdRendererBase.displayName = "MdRenderer";
|
|
434
434
|
MdRenderer.displayName = "MdRenderer";
|
|
435
|
-
|
|
435
|
+
function wrapOnData(onData) {
|
|
436
|
+
let buffer = '';
|
|
437
|
+
return function(data) {
|
|
438
|
+
buffer += data;
|
|
439
|
+
const lines = buffer.split('\n');
|
|
440
|
+
buffer = lines.pop() || '';
|
|
441
|
+
for (const line of lines)if (line.startsWith('data: ')) try {
|
|
442
|
+
const parsedData = JSON.parse(line.slice(6));
|
|
443
|
+
if (parsedData.isFinished) return void onData({
|
|
444
|
+
isFinished: true,
|
|
445
|
+
content: ''
|
|
446
|
+
});
|
|
447
|
+
const content = parsedData?.choices?.[0]?.delta?.content;
|
|
448
|
+
if (content?.length > 0) onData({
|
|
449
|
+
isFinished: false,
|
|
450
|
+
content
|
|
451
|
+
});
|
|
452
|
+
} catch (e) {
|
|
453
|
+
onData({
|
|
454
|
+
isFinished: true,
|
|
455
|
+
error: e,
|
|
456
|
+
content: ''
|
|
457
|
+
});
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
const chat_complete = async function(message, onData, signal, option = {}) {
|
|
436
463
|
return await this.call("mujian:ai:chat:complete", {
|
|
437
464
|
content: message
|
|
438
465
|
}, {
|
|
439
|
-
onData,
|
|
466
|
+
onData: option.parseContent ? wrapOnData(onData) : onData,
|
|
440
467
|
signal
|
|
441
468
|
});
|
|
442
469
|
};
|
|
@@ -781,6 +808,7 @@ async function* callSdk(mujian, content, type = 'generate', signal) {
|
|
|
781
808
|
try {
|
|
782
809
|
const onData = (data)=>{
|
|
783
810
|
if (error) return;
|
|
811
|
+
if ('string' != typeof data) return;
|
|
784
812
|
queue.push(data);
|
|
785
813
|
if (resolveWait) {
|
|
786
814
|
resolveWait();
|
package/dist/umd/index.js
CHANGED
|
@@ -298,11 +298,38 @@
|
|
|
298
298
|
personaId
|
|
299
299
|
});
|
|
300
300
|
};
|
|
301
|
-
|
|
301
|
+
function wrapOnData(onData) {
|
|
302
|
+
let buffer = '';
|
|
303
|
+
return function(data) {
|
|
304
|
+
buffer += data;
|
|
305
|
+
const lines = buffer.split('\n');
|
|
306
|
+
buffer = lines.pop() || '';
|
|
307
|
+
for (const line of lines)if (line.startsWith('data: ')) try {
|
|
308
|
+
const parsedData = JSON.parse(line.slice(6));
|
|
309
|
+
if (parsedData.isFinished) return void onData({
|
|
310
|
+
isFinished: true,
|
|
311
|
+
content: ''
|
|
312
|
+
});
|
|
313
|
+
const content = parsedData?.choices?.[0]?.delta?.content;
|
|
314
|
+
if (content?.length > 0) onData({
|
|
315
|
+
isFinished: false,
|
|
316
|
+
content
|
|
317
|
+
});
|
|
318
|
+
} catch (e) {
|
|
319
|
+
onData({
|
|
320
|
+
isFinished: true,
|
|
321
|
+
error: e,
|
|
322
|
+
content: ''
|
|
323
|
+
});
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
const chat_complete = async function(message, onData, signal, option = {}) {
|
|
302
329
|
return await this.call(events_EVENT.MUJIAN_AI_CHAT_COMPLETE, {
|
|
303
330
|
content: message
|
|
304
331
|
}, {
|
|
305
|
-
onData,
|
|
332
|
+
onData: option.parseContent ? wrapOnData(onData) : onData,
|
|
306
333
|
signal
|
|
307
334
|
});
|
|
308
335
|
};
|
package/dist/umd/react.js
CHANGED
|
@@ -6290,11 +6290,38 @@ ${content}
|
|
|
6290
6290
|
personaId
|
|
6291
6291
|
});
|
|
6292
6292
|
};
|
|
6293
|
-
|
|
6293
|
+
function wrapOnData(onData) {
|
|
6294
|
+
let buffer = '';
|
|
6295
|
+
return function(data) {
|
|
6296
|
+
buffer += data;
|
|
6297
|
+
const lines = buffer.split('\n');
|
|
6298
|
+
buffer = lines.pop() || '';
|
|
6299
|
+
for (const line of lines)if (line.startsWith('data: ')) try {
|
|
6300
|
+
const parsedData = JSON.parse(line.slice(6));
|
|
6301
|
+
if (parsedData.isFinished) return void onData({
|
|
6302
|
+
isFinished: true,
|
|
6303
|
+
content: ''
|
|
6304
|
+
});
|
|
6305
|
+
const content = parsedData?.choices?.[0]?.delta?.content;
|
|
6306
|
+
if (content?.length > 0) onData({
|
|
6307
|
+
isFinished: false,
|
|
6308
|
+
content
|
|
6309
|
+
});
|
|
6310
|
+
} catch (e) {
|
|
6311
|
+
onData({
|
|
6312
|
+
isFinished: true,
|
|
6313
|
+
error: e,
|
|
6314
|
+
content: ''
|
|
6315
|
+
});
|
|
6316
|
+
return;
|
|
6317
|
+
}
|
|
6318
|
+
};
|
|
6319
|
+
}
|
|
6320
|
+
const chat_complete = async function(message, onData, signal, option = {}) {
|
|
6294
6321
|
return await this.call("mujian:ai:chat:complete", {
|
|
6295
6322
|
content: message
|
|
6296
6323
|
}, {
|
|
6297
|
-
onData,
|
|
6324
|
+
onData: option.parseContent ? wrapOnData(onData) : onData,
|
|
6298
6325
|
signal
|
|
6299
6326
|
});
|
|
6300
6327
|
};
|
|
@@ -8011,6 +8038,7 @@ ${content}
|
|
|
8011
8038
|
try {
|
|
8012
8039
|
const onData = (data)=>{
|
|
8013
8040
|
if (error) return;
|
|
8041
|
+
if ('string' != typeof data) return;
|
|
8014
8042
|
queue.push(data);
|
|
8015
8043
|
if (resolveWait) {
|
|
8016
8044
|
resolveWait();
|