blun-king-cli 7.0.0 → 7.0.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/lib/chat.js +27 -77
- package/package.json +1 -1
package/lib/chat.js
CHANGED
|
@@ -271,91 +271,41 @@ function startChat() {
|
|
|
271
271
|
return;
|
|
272
272
|
}
|
|
273
273
|
|
|
274
|
-
//
|
|
274
|
+
// Send message
|
|
275
275
|
var spinner = ui.renderSpinner("Thinking...");
|
|
276
276
|
client
|
|
277
|
-
.
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
277
|
+
.sendMessage(input, { model: settings.model || "auto" })
|
|
278
|
+
.then(function (res) {
|
|
279
|
+
ui.stopSpinner(true, "Response received");
|
|
280
|
+
if (res.status === 200 && res.data) {
|
|
281
|
+
var text =
|
|
282
|
+
res.data.answer ||
|
|
283
|
+
res.data.response ||
|
|
284
|
+
res.data.text ||
|
|
285
|
+
res.data.message ||
|
|
286
|
+
JSON.stringify(res.data);
|
|
287
|
+
ui.renderResponse(text, "agent");
|
|
288
|
+
var tok = res.data.tokens || res.data.usage || {};
|
|
289
|
+
var tokTotal = tok.total || tok.total_tokens || 0;
|
|
290
|
+
if (!tokTotal && text) {
|
|
291
|
+
tokTotal = Math.ceil(text.length / 4) + Math.ceil(input.length / 4);
|
|
284
292
|
}
|
|
285
|
-
ui.
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if (spinner) {
|
|
290
|
-
// No streaming happened, fallback to non-stream
|
|
291
|
-
ui.stopSpinner(true, "Response received");
|
|
292
|
-
ui.renderResponse(result.text || "(empty response)", "agent");
|
|
293
|
+
if (tokTotal) ui.addTokens(tokTotal);
|
|
294
|
+
ui.renderMiniStatus(settings.model || "chat", tokTotal);
|
|
295
|
+
} else if (res.status === 401 || res.status === 403) {
|
|
296
|
+
ui.renderResponse("Auth error. Use /login to re-authenticate.", "error");
|
|
293
297
|
} else {
|
|
294
|
-
ui.
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// Token tracking
|
|
298
|
-
var tokTotal = 0;
|
|
299
|
-
if (result.usage) {
|
|
300
|
-
tokTotal =
|
|
301
|
-
result.usage.total ||
|
|
302
|
-
result.usage.total_tokens ||
|
|
303
|
-
(result.usage.input || 0) + (result.usage.output || 0) ||
|
|
304
|
-
0;
|
|
305
|
-
}
|
|
306
|
-
// Estimate tokens from text length if no usage data
|
|
307
|
-
if (!tokTotal && result.text) {
|
|
308
|
-
tokTotal = Math.ceil(result.text.length / 4) + Math.ceil(input.length / 4);
|
|
298
|
+
ui.renderResponse("Error: " + JSON.stringify(res.data), "error");
|
|
309
299
|
}
|
|
310
|
-
if (tokTotal) ui.addTokens(tokTotal);
|
|
311
|
-
|
|
312
|
-
var usedModel = settings.model || "chat";
|
|
313
|
-
ui.renderMiniStatus(usedModel, tokTotal);
|
|
314
300
|
showPrompt(rl, username, model);
|
|
315
301
|
})
|
|
316
302
|
.catch(function (e) {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
ui.renderResponse("Stream failed, trying non-stream...", "system");
|
|
324
|
-
return client
|
|
325
|
-
.sendMessage(input, { model: settings.model || "auto" })
|
|
326
|
-
.then(function (res) {
|
|
327
|
-
if (res.status === 200 && res.data) {
|
|
328
|
-
var text =
|
|
329
|
-
res.data.answer ||
|
|
330
|
-
res.data.response ||
|
|
331
|
-
res.data.text ||
|
|
332
|
-
res.data.message ||
|
|
333
|
-
JSON.stringify(res.data);
|
|
334
|
-
ui.renderResponse(text, "agent");
|
|
335
|
-
var tok = res.data.tokens || res.data.usage || {};
|
|
336
|
-
var tokTotal = tok.total || tok.total_tokens || 0;
|
|
337
|
-
if (tokTotal) ui.addTokens(tokTotal);
|
|
338
|
-
ui.renderMiniStatus(settings.model || "chat", tokTotal);
|
|
339
|
-
} else if (res.status === 401 || res.status === 403) {
|
|
340
|
-
ui.renderResponse(
|
|
341
|
-
"Auth error. Use /login to re-authenticate.",
|
|
342
|
-
"error"
|
|
343
|
-
);
|
|
344
|
-
} else {
|
|
345
|
-
ui.renderResponse(
|
|
346
|
-
"Error: " + JSON.stringify(res.data),
|
|
347
|
-
"error"
|
|
348
|
-
);
|
|
349
|
-
}
|
|
350
|
-
showPrompt(rl, username, model);
|
|
351
|
-
})
|
|
352
|
-
.catch(function (e2) {
|
|
353
|
-
ui.renderResponse(
|
|
354
|
-
"Request failed: " + e2.message + "\n Try /login if auth expired.",
|
|
355
|
-
"error"
|
|
356
|
-
);
|
|
357
|
-
showPrompt(rl, username, model);
|
|
358
|
-
});
|
|
303
|
+
ui.stopSpinner(false, e.message);
|
|
304
|
+
ui.renderResponse(
|
|
305
|
+
"Request failed: " + e.message + "\n Try /login if auth expired.",
|
|
306
|
+
"error"
|
|
307
|
+
);
|
|
308
|
+
showPrompt(rl, username, model);
|
|
359
309
|
});
|
|
360
310
|
});
|
|
361
311
|
|