@alauda/doom 2.3.0 → 2.3.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.
|
@@ -9,13 +9,12 @@ import { Chat } from "./Chat/index.js";
|
|
|
9
9
|
import { Preamble } from "./Preamble/index.js";
|
|
10
10
|
import { ResizableUserInput } from "./ResizableUserInput/index.js";
|
|
11
11
|
import { Thinking } from "./Thinking.js";
|
|
12
|
-
import { parseStreamContent } from "./utils.js";
|
|
12
|
+
import { consumeSSEEvents, getAnswerDelta, parseStreamContent, } from "./utils.js";
|
|
13
13
|
import CloseIcon from '@alauda/doom/assets/close.svg?react';
|
|
14
14
|
import LogoutIcon from '@alauda/doom/assets/logout.svg?react';
|
|
15
15
|
import NewChatIcon from '@alauda/doom/assets/new-chat.svg?react';
|
|
16
16
|
import { useMemoizedFn, useTranslation } from '@alauda/doom/runtime';
|
|
17
17
|
import classes from '@alauda/doom/styles/ai-assistant.module.scss';
|
|
18
|
-
let textDecoder;
|
|
19
18
|
export const AIAssistant = ({ open, onOpenChange }) => {
|
|
20
19
|
const t = useTranslation();
|
|
21
20
|
const { authInfo, setAuthBasic } = useCloudAuth();
|
|
@@ -92,7 +91,7 @@ export const AIAssistant = ({ open, onOpenChange }) => {
|
|
|
92
91
|
sessionIdRef.current = session_id;
|
|
93
92
|
}
|
|
94
93
|
const sessionId = sessionIdRef.current;
|
|
95
|
-
const res = await xfetch('/smart/api/
|
|
94
|
+
const res = await xfetch('/smart/api/smart_answer_with_search', {
|
|
96
95
|
type: null,
|
|
97
96
|
method: ApiMethod.POST,
|
|
98
97
|
body: {
|
|
@@ -100,30 +99,61 @@ export const AIAssistant = ({ open, onOpenChange }) => {
|
|
|
100
99
|
session_id: sessionId,
|
|
101
100
|
},
|
|
102
101
|
});
|
|
103
|
-
textDecoder
|
|
102
|
+
const textDecoder = new TextDecoder();
|
|
104
103
|
let text = '';
|
|
104
|
+
let sseBuffer = '';
|
|
105
|
+
let sseEvent;
|
|
105
106
|
for await (const chunk_ of res.body) {
|
|
106
107
|
if (sessionId !== sessionIdRef.current) {
|
|
107
108
|
break;
|
|
108
109
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
!
|
|
118
|
-
|
|
110
|
+
sseBuffer += textDecoder.decode(chunk_, { stream: true });
|
|
111
|
+
const result = consumeSSEEvents(sseBuffer, sseEvent);
|
|
112
|
+
const { events, remainder } = result;
|
|
113
|
+
sseBuffer = remainder;
|
|
114
|
+
sseEvent = result.event;
|
|
115
|
+
let updated = false;
|
|
116
|
+
for (const event of events) {
|
|
117
|
+
const delta = getAnswerDelta(event);
|
|
118
|
+
if (!delta) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
text += delta;
|
|
122
|
+
updated = true;
|
|
123
|
+
}
|
|
124
|
+
if (!updated) {
|
|
119
125
|
continue;
|
|
120
126
|
}
|
|
127
|
+
const parsed = parseStreamContent(text);
|
|
121
128
|
flushMessages((messages) => [
|
|
122
129
|
...messages.slice(0, index),
|
|
123
130
|
{ ...messages[index], ...parsed },
|
|
124
131
|
...messages.slice(index + 1),
|
|
125
132
|
]);
|
|
126
133
|
}
|
|
134
|
+
if (sessionId !== sessionIdRef.current) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
sseBuffer += textDecoder.decode();
|
|
138
|
+
const { events } = consumeSSEEvents(`${sseBuffer}\n`, sseEvent);
|
|
139
|
+
let updated = false;
|
|
140
|
+
for (const event of events) {
|
|
141
|
+
const delta = getAnswerDelta(event);
|
|
142
|
+
if (!delta) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
text += delta;
|
|
146
|
+
updated = true;
|
|
147
|
+
}
|
|
148
|
+
if (!updated) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const parsed = parseStreamContent(text);
|
|
152
|
+
flushMessages((messages) => [
|
|
153
|
+
...messages.slice(0, index),
|
|
154
|
+
{ ...messages[index], ...parsed },
|
|
155
|
+
...messages.slice(index + 1),
|
|
156
|
+
]);
|
|
127
157
|
};
|
|
128
158
|
const [loading, setLoading] = useState(false);
|
|
129
159
|
const onSend = useMemoizedFn(async (content) => {
|
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
import type { RefDoc } from './types.js';
|
|
2
|
+
export interface SSEEvent {
|
|
3
|
+
data: string;
|
|
4
|
+
event?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface SSEConsumeResult {
|
|
7
|
+
event?: string;
|
|
8
|
+
events: SSEEvent[];
|
|
9
|
+
remainder: string;
|
|
10
|
+
}
|
|
2
11
|
export declare const unicodeToString: (unicodeStr: string) => string;
|
|
3
12
|
export declare const removePrefix: (prefix: string | null | undefined, text: string) => string;
|
|
13
|
+
export declare const consumeSSEEvents: (buffer: string, event?: string) => SSEConsumeResult;
|
|
14
|
+
export declare const getAnswerDelta: ({ event, data }: SSEEvent) => string;
|
|
4
15
|
export declare function parseStreamContent(text: string): {
|
|
5
16
|
refDocs: RefDoc[];
|
|
6
|
-
thinkingProcess: string |
|
|
17
|
+
thinkingProcess: string | undefined;
|
|
7
18
|
content: string;
|
|
8
19
|
};
|
|
@@ -1,11 +1,109 @@
|
|
|
1
1
|
// based on https://gitlab-ce.alauda.cn/aml/smart-doc/-/blob/master/frontend/src/app/utils/unicode.ts
|
|
2
2
|
import { isObject } from 'es-toolkit/compat';
|
|
3
|
+
const IGNORED_SSE_EVENT_TYPES = /(?:^|[._-])(?:metadata|trace|updates?)(?:$|[._-])/i;
|
|
4
|
+
const STREAM_TEXT_KEYS = ['delta', 'text', 'content', 'answer', 'message'];
|
|
3
5
|
export const unicodeToString = (unicodeStr) => unicodeStr.replace(/\\u([0-9a-fA-F]{4})/g, (_match, p1) => String.fromCharCode(Number.parseInt(p1, 16)));
|
|
6
|
+
const isRecord = (value) => isObject(value) && !Array.isArray(value);
|
|
4
7
|
export const removePrefix = (prefix, text) => (prefix && text.startsWith(prefix) ? text.slice(prefix.length) : text);
|
|
8
|
+
const parseSSEFieldValue = (line, field) => {
|
|
9
|
+
const value = line.slice(field.length + 1);
|
|
10
|
+
return value.startsWith(' ') ? value.slice(1) : value;
|
|
11
|
+
};
|
|
12
|
+
const getStringValue = (value) => {
|
|
13
|
+
if (typeof value === 'string') {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
const text = value.map((item) => getStringValue(item) || '').join('');
|
|
18
|
+
return text || undefined;
|
|
19
|
+
}
|
|
20
|
+
if (!isRecord(value)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
for (const key of STREAM_TEXT_KEYS) {
|
|
24
|
+
const text = getStringValue(value[key]);
|
|
25
|
+
if (text) {
|
|
26
|
+
return text;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const getChoicesDelta = (choices) => {
|
|
31
|
+
if (!Array.isArray(choices)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const text = choices.map((choice) => getStringValue(choice) || '').join('');
|
|
35
|
+
return text || undefined;
|
|
36
|
+
};
|
|
37
|
+
export const consumeSSEEvents = (buffer, event) => {
|
|
38
|
+
const events = [];
|
|
39
|
+
let remaining = buffer;
|
|
40
|
+
let currentEvent = event;
|
|
41
|
+
let lineBoundary = remaining.match(/\r?\n/);
|
|
42
|
+
while (lineBoundary?.index != null) {
|
|
43
|
+
const line = remaining.slice(0, lineBoundary.index);
|
|
44
|
+
remaining = remaining.slice(lineBoundary.index + lineBoundary[0].length);
|
|
45
|
+
if (!line) {
|
|
46
|
+
currentEvent = undefined;
|
|
47
|
+
}
|
|
48
|
+
else if (line.startsWith(':')) {
|
|
49
|
+
//
|
|
50
|
+
}
|
|
51
|
+
else if (line.startsWith('event:')) {
|
|
52
|
+
currentEvent = parseSSEFieldValue(line, 'event');
|
|
53
|
+
}
|
|
54
|
+
else if (line.startsWith('data:')) {
|
|
55
|
+
events.push({
|
|
56
|
+
event: currentEvent,
|
|
57
|
+
data: parseSSEFieldValue(line, 'data'),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
lineBoundary = remaining.match(/\r?\n/);
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
event: currentEvent,
|
|
64
|
+
events,
|
|
65
|
+
remainder: remaining,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
export const getAnswerDelta = ({ event, data }) => {
|
|
69
|
+
if (!data || data === '[DONE]') {
|
|
70
|
+
return '';
|
|
71
|
+
}
|
|
72
|
+
if (event && IGNORED_SSE_EVENT_TYPES.test(event)) {
|
|
73
|
+
return '';
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const payload = JSON.parse(data);
|
|
77
|
+
if (typeof payload === 'string') {
|
|
78
|
+
return payload;
|
|
79
|
+
}
|
|
80
|
+
if (!isRecord(payload)) {
|
|
81
|
+
return '';
|
|
82
|
+
}
|
|
83
|
+
const type = typeof payload.type === 'string' ? payload.type : undefined;
|
|
84
|
+
if (type && IGNORED_SSE_EVENT_TYPES.test(type)) {
|
|
85
|
+
return '';
|
|
86
|
+
}
|
|
87
|
+
const choiceDelta = getChoicesDelta(payload.choices);
|
|
88
|
+
if (choiceDelta) {
|
|
89
|
+
return choiceDelta;
|
|
90
|
+
}
|
|
91
|
+
for (const key of STREAM_TEXT_KEYS) {
|
|
92
|
+
const text = getStringValue(payload[key]);
|
|
93
|
+
if (text) {
|
|
94
|
+
return text;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return '';
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return data;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
5
103
|
export function parseStreamContent(text) {
|
|
6
|
-
const
|
|
7
|
-
const docsReferences =
|
|
8
|
-
? unicodeToString(
|
|
104
|
+
const docsMatches = [...text.matchAll(/<docs>([\s\S]*?)<\/docs>/g)];
|
|
105
|
+
const docsReferences = docsMatches.length
|
|
106
|
+
? unicodeToString(docsMatches.at(-1)[1])
|
|
9
107
|
: '[]';
|
|
10
108
|
let refDocs = [];
|
|
11
109
|
try {
|
|
@@ -14,16 +112,20 @@ export function parseStreamContent(text) {
|
|
|
14
112
|
catch {
|
|
15
113
|
//
|
|
16
114
|
}
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
115
|
+
const thinkMatches = [...text.matchAll(/<think>([\s\S]*?)<\/think>/g)];
|
|
116
|
+
let thinkingProcess = thinkMatches.at(-1)?.[1];
|
|
117
|
+
if (!thinkingProcess) {
|
|
118
|
+
const openThinkMatch = text.match(/<think>([\s\S]*)$/);
|
|
119
|
+
thinkingProcess = openThinkMatch?.[1];
|
|
22
120
|
}
|
|
23
|
-
const
|
|
121
|
+
const content = text
|
|
122
|
+
.replace(/<docs>[\s\S]*?<\/docs>/g, '')
|
|
123
|
+
.replace(/<think>[\s\S]*?<\/think>/g, '')
|
|
124
|
+
.replace(/<docs>[\s\S]*$/g, '')
|
|
125
|
+
.replace(/<think>[\s\S]*$/g, '');
|
|
24
126
|
return {
|
|
25
127
|
refDocs,
|
|
26
|
-
thinkingProcess,
|
|
27
|
-
content: unicodeToString(
|
|
128
|
+
thinkingProcess: thinkingProcess && unicodeToString(thinkingProcess),
|
|
129
|
+
content: unicodeToString(content),
|
|
28
130
|
};
|
|
29
131
|
}
|