byt-lingxiao-ai 0.3.23 → 0.3.25
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/components/AiMessage.vue +57 -71
- package/components/ChatMessageList.vue +12 -31
- package/components/ChatWindow.vue +37 -29
- package/components/mixins/messageMixin.js +19 -30
- package/components/mixins/webSocketMixin.js +2 -0
- package/components/utils/StreamParser.js +144 -98
- package/dist/index.common.js +64257 -26511
- package/dist/index.common.js.map +1 -1
- package/dist/index.css +1 -1
- package/dist/index.umd.js +64107 -26361
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/package.json +5 -2
|
@@ -1,135 +1,181 @@
|
|
|
1
1
|
export class StreamParser {
|
|
2
2
|
constructor(options = {}) {
|
|
3
3
|
this.options = {
|
|
4
|
-
updateInterval: 16,
|
|
5
|
-
debug: false,
|
|
4
|
+
updateInterval: 16,
|
|
6
5
|
...options
|
|
7
|
-
}
|
|
8
|
-
this.reset()
|
|
6
|
+
}
|
|
7
|
+
this.reset()
|
|
9
8
|
}
|
|
10
9
|
|
|
11
|
-
// 重置解析器状态
|
|
12
10
|
reset() {
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
18
|
-
this.updateTimer = null;
|
|
19
|
-
this.status = 'output'; // thinking | output
|
|
11
|
+
this.inTag = false
|
|
12
|
+
this.tagBuffer = ''
|
|
13
|
+
this.currentType = 'content'
|
|
14
|
+
this.currentEvent = null
|
|
15
|
+
this.updateTimer = null
|
|
20
16
|
}
|
|
21
17
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.parseContent(chunk, callback);
|
|
18
|
+
processChunk(chunk, emit) {
|
|
19
|
+
if (!chunk) return
|
|
20
|
+
this.parse(chunk, emit)
|
|
26
21
|
}
|
|
27
22
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
let i = 0;
|
|
23
|
+
parse(text, emit) {
|
|
24
|
+
let i = 0
|
|
31
25
|
|
|
32
|
-
while (i <
|
|
26
|
+
while (i < text.length) {
|
|
33
27
|
if (this.inTag) {
|
|
34
|
-
const
|
|
35
|
-
if (
|
|
36
|
-
this.tagBuffer +=
|
|
37
|
-
this.handleTag(this.tagBuffer)
|
|
38
|
-
this.tagBuffer = ''
|
|
39
|
-
this.inTag = false
|
|
40
|
-
i =
|
|
28
|
+
const end = text.indexOf('>', i)
|
|
29
|
+
if (end !== -1) {
|
|
30
|
+
this.tagBuffer += text.slice(i, end + 1)
|
|
31
|
+
this.handleTag(this.tagBuffer, emit)
|
|
32
|
+
this.tagBuffer = ''
|
|
33
|
+
this.inTag = false
|
|
34
|
+
i = end + 1
|
|
41
35
|
} else {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (this.tagBuffer.length > 50) {
|
|
45
|
-
this.appendText(this.tagBuffer);
|
|
46
|
-
this.tagBuffer = '';
|
|
47
|
-
this.inTag = false;
|
|
48
|
-
}
|
|
49
|
-
break;
|
|
36
|
+
this.tagBuffer += text.slice(i)
|
|
37
|
+
break
|
|
50
38
|
}
|
|
51
39
|
} else {
|
|
52
|
-
const
|
|
53
|
-
if (
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
i = endIndex + 1;
|
|
40
|
+
const lt = text.indexOf('<', i)
|
|
41
|
+
if (lt !== -1) {
|
|
42
|
+
if (lt > i) this.append(text.slice(i, lt), emit)
|
|
43
|
+
|
|
44
|
+
const gt = text.indexOf('>', lt)
|
|
45
|
+
if (gt !== -1) {
|
|
46
|
+
this.handleTag(text.slice(lt, gt + 1), emit)
|
|
47
|
+
i = gt + 1
|
|
61
48
|
} else {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
this.appendText('<');
|
|
66
|
-
i = startIndex + 1;
|
|
67
|
-
} else {
|
|
68
|
-
this.inTag = true;
|
|
69
|
-
this.tagBuffer = content.substring(startIndex);
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
49
|
+
this.inTag = true
|
|
50
|
+
this.tagBuffer = text.slice(lt)
|
|
51
|
+
break
|
|
72
52
|
}
|
|
73
53
|
} else {
|
|
74
|
-
this.
|
|
75
|
-
break
|
|
54
|
+
this.append(text.slice(i), emit)
|
|
55
|
+
break
|
|
76
56
|
}
|
|
77
57
|
}
|
|
78
58
|
}
|
|
79
59
|
|
|
80
|
-
this.
|
|
60
|
+
this.scheduleFlush(emit)
|
|
81
61
|
}
|
|
82
62
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (t
|
|
87
|
-
|
|
63
|
+
handleTag(tag, emit) {
|
|
64
|
+
const t = tag.toLowerCase().trim()
|
|
65
|
+
|
|
66
|
+
if (t.startsWith('<think')) {
|
|
67
|
+
this.startThinking(emit)
|
|
68
|
+
}
|
|
69
|
+
else if (t.startsWith('</think')) {
|
|
70
|
+
this.closeThinking()
|
|
71
|
+
this.currentType = 'content'
|
|
72
|
+
this.currentEvent = null
|
|
73
|
+
}
|
|
74
|
+
else if (t.startsWith('<tool_call')) {
|
|
75
|
+
const attrs = this.parseTagAttributes(tag)
|
|
76
|
+
this.switchType('tool_call', emit, attrs.name)
|
|
77
|
+
}
|
|
78
|
+
else if (t.startsWith('</tool_call')) {
|
|
79
|
+
this.switchType('content', emit)
|
|
80
|
+
}
|
|
81
|
+
else if (t.startsWith('<tool_result')) {
|
|
82
|
+
this.switchType('tool_result', emit)
|
|
83
|
+
}
|
|
84
|
+
else if (t.startsWith('</tool_result')) {
|
|
85
|
+
this.switchType('content', emit)
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
this.append(tag, emit)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
startThinking(emit) {
|
|
94
|
+
// 如果上一个 thinking 还没结算,先结算
|
|
95
|
+
this.closeThinking()
|
|
96
|
+
|
|
97
|
+
this.currentType = 'thinking'
|
|
98
|
+
this.currentEvent = {
|
|
99
|
+
id: this.uuid(),
|
|
100
|
+
type: 'thinking',
|
|
101
|
+
content: '',
|
|
102
|
+
startTime: Date.now(),
|
|
103
|
+
endTime: null,
|
|
104
|
+
duration: null
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
emit({ type: 'create', event: this.currentEvent })
|
|
88
108
|
}
|
|
89
109
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
110
|
+
closeThinking() {
|
|
111
|
+
if (
|
|
112
|
+
this.currentEvent &&
|
|
113
|
+
this.currentEvent.type === 'thinking' &&
|
|
114
|
+
!this.currentEvent.endTime
|
|
115
|
+
) {
|
|
116
|
+
this.currentEvent.endTime = Date.now()
|
|
117
|
+
this.currentEvent.duration = (
|
|
118
|
+
(this.currentEvent.endTime - this.currentEvent.startTime) / 1000
|
|
119
|
+
).toFixed(2)
|
|
120
|
+
console.log(this.currentEvent.endTime - this.currentEvent.startTime)
|
|
121
|
+
}
|
|
95
122
|
}
|
|
96
123
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (this.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
124
|
+
switchType(type) {
|
|
125
|
+
// 如果是从 thinking 切走,结算时间
|
|
126
|
+
if (this.currentEvent?.type === 'thinking') {
|
|
127
|
+
this.closeThinking()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
this.currentType = type
|
|
131
|
+
this.currentEvent = null
|
|
104
132
|
}
|
|
105
133
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
134
|
+
append(text, emit) {
|
|
135
|
+
if (!text) return
|
|
136
|
+
|
|
137
|
+
if (!this.currentEvent || this.currentEvent.type !== this.currentType) {
|
|
138
|
+
this.currentEvent = {
|
|
139
|
+
id: this.uuid(),
|
|
140
|
+
type: this.currentType,
|
|
141
|
+
content: ''
|
|
142
|
+
}
|
|
143
|
+
emit({ type: 'create', event: this.currentEvent })
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
this.currentEvent.content += text
|
|
147
|
+
emit({ type: 'append', event: this.currentEvent })
|
|
117
148
|
}
|
|
118
149
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
150
|
+
parseTagAttributes(tag) {
|
|
151
|
+
const attrRegex = /(\w+)=['"]([^'"]+)['"]/g
|
|
152
|
+
const attrs = {}
|
|
153
|
+
let match
|
|
154
|
+
|
|
155
|
+
while ((match = attrRegex.exec(tag)) !== null) {
|
|
156
|
+
attrs[match[1]] = match[2]
|
|
125
157
|
}
|
|
126
|
-
|
|
127
|
-
|
|
158
|
+
|
|
159
|
+
return attrs
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
scheduleFlush(emit) {
|
|
163
|
+
if (this.updateTimer) return
|
|
164
|
+
this.updateTimer = setTimeout(() => {
|
|
165
|
+
emit({ type: 'flush' })
|
|
166
|
+
this.updateTimer = null
|
|
167
|
+
}, this.options.updateInterval)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
finish(emit) {
|
|
171
|
+
// 结束时如果还在 thinking,也要结算
|
|
172
|
+
this.closeThinking()
|
|
173
|
+
emit({ type: 'flush' })
|
|
174
|
+
if (this.updateTimer) clearTimeout(this.updateTimer)
|
|
175
|
+
this.reset()
|
|
128
176
|
}
|
|
129
177
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (this.updateTimer) clearTimeout(this.updateTimer);
|
|
133
|
-
this.reset();
|
|
178
|
+
uuid() {
|
|
179
|
+
return Math.random().toString(36).slice(2)
|
|
134
180
|
}
|
|
135
|
-
}
|
|
181
|
+
}
|