thebird 1.2.13 → 1.2.15
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/docs/app.js +29 -16
- package/package.json +1 -1
package/docs/app.js
CHANGED
|
@@ -84,7 +84,14 @@ class BirdChat extends HTMLElement {
|
|
|
84
84
|
if (this.state.apiKey) this.loadModels(this.state.apiKey);
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
setState(patch) {
|
|
87
|
+
setState(patch) {
|
|
88
|
+
Object.assign(this.state, patch);
|
|
89
|
+
this.render();
|
|
90
|
+
if (this.streamWrap && !this.querySelector('#msg-list')?.contains(this.streamWrap)) {
|
|
91
|
+
const list = this.querySelector('#msg-list');
|
|
92
|
+
if (list) list.appendChild(this.streamWrap);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
88
95
|
|
|
89
96
|
async loadModels(apiKey) {
|
|
90
97
|
this.setState({ modelsLoading: true, status: '' });
|
|
@@ -127,11 +134,7 @@ class BirdChat extends HTMLElement {
|
|
|
127
134
|
<div key=${i} class=${'flex ' + (m.role === 'user' ? 'justify-end' : 'justify-start')}>
|
|
128
135
|
<div class=${'msg-bubble card px-4 py-3 text-sm leading-relaxed ' + (m.role === 'user' ? 'bg-primary text-primary-content' : 'bg-base-200 text-base-content')}>${m.content}</div>
|
|
129
136
|
</div>`)}
|
|
130
|
-
${streamingText && html
|
|
131
|
-
<div class="flex justify-start">
|
|
132
|
-
<div class="msg-bubble card bg-base-200 text-base-content px-4 py-3 text-sm leading-relaxed">${streamingText}<span class="animate-pulse ml-1">▋</span></div>
|
|
133
|
-
</div>`}
|
|
134
|
-
${!streamingText && streaming && html`<div class="flex justify-start"><div class="card bg-base-200 px-4 py-3"><span class="loading loading-dots loading-sm"></span></div></div>`}
|
|
137
|
+
${streaming && !streamingText && html`<div class="flex justify-start"><div class="card bg-base-200 px-4 py-3"><span class="loading loading-dots loading-sm"></span></div></div>`}
|
|
135
138
|
</div>
|
|
136
139
|
|
|
137
140
|
${status && html`<div class="text-xs text-error px-4 pb-1">${status}</div>`}
|
|
@@ -160,19 +163,29 @@ class BirdChat extends HTMLElement {
|
|
|
160
163
|
this.setState({ messages, streaming: true, status: '', streamingText: '' });
|
|
161
164
|
try {
|
|
162
165
|
let full = '';
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
+
this.streamEl = document.createElement('div');
|
|
167
|
+
this.streamEl.className = 'msg-bubble card bg-base-200 text-base-content px-4 py-3 text-sm leading-relaxed';
|
|
168
|
+
const cursor = document.createElement('span');
|
|
169
|
+
cursor.className = 'animate-pulse ml-1';
|
|
170
|
+
cursor.textContent = '▋';
|
|
171
|
+
this.streamWrap = document.createElement('div');
|
|
172
|
+
this.streamWrap.className = 'flex justify-start';
|
|
173
|
+
this.streamWrap.appendChild(this.streamEl);
|
|
174
|
+
this.streamWrap.appendChild(cursor);
|
|
175
|
+
const list = this.querySelector('#msg-list');
|
|
176
|
+
if (list) list.appendChild(this.streamWrap);
|
|
177
|
+
for await (const chunk of streamGenerate(apiKey, model, convertMessages(messages))) {
|
|
166
178
|
full += chunk;
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
requestAnimationFrame(() => { this.setState({ streamingText: snap }); rafPending = false; });
|
|
171
|
-
}
|
|
179
|
+
this.streamEl.textContent = full;
|
|
180
|
+
const l = this.querySelector('#msg-list');
|
|
181
|
+
if (l) l.scrollTop = l.scrollHeight;
|
|
172
182
|
}
|
|
183
|
+
this.streamWrap.remove();
|
|
184
|
+
this.streamEl = null;
|
|
185
|
+
this.streamWrap = null;
|
|
173
186
|
this.setState({ messages: [...messages, { role: 'assistant', content: full || '(empty)' }], streaming: false, streamingText: '' });
|
|
174
|
-
const
|
|
175
|
-
if (
|
|
187
|
+
const l2 = this.querySelector('#msg-list');
|
|
188
|
+
if (l2) l2.scrollTop = l2.scrollHeight;
|
|
176
189
|
} catch (err) {
|
|
177
190
|
this.setState({ streaming: false, streamingText: '', status: 'Error: ' + (err?.message || String(err)) });
|
|
178
191
|
}
|
package/package.json
CHANGED