feat(assist): сбор usage-токенов и отдача в meta (время этапов уже было)

This commit is contained in:
Repinoid
2026-10-07 18:53:11 +03:00
parent 5018acf150
commit f42017f012
4 changed files with 22 additions and 0 deletions
+10
View File
@@ -272,6 +272,7 @@ function createSemaphore(max) {
* model?:string,
* temperature?:number,
* responseFormat?:{type:string} // напр. {type:'json_object'}
* usageRef?:object // мутабельный объект для сбора токенов {prompt,completion,total}
* }} params
* @returns {Promise<string>} — содержимое choices[0].message.content
*/
@@ -284,6 +285,7 @@ async function llmChat({
model = ENV.LLM_MODEL,
temperature,
responseFormat,
usageRef,
}) {
const base = String(baseUrl).replace(/\/+$/, '');
const url = `${base}/v1/chat/completions`;
@@ -349,6 +351,14 @@ async function llmChat({
}
const data = await response.json();
// Собираем токены (usage) для отображения пользователю.
if (usageRef && data && data.usage) {
usageRef.prompt = (usageRef.prompt || 0) + (data.usage.prompt_tokens || 0);
usageRef.completion = (usageRef.completion || 0) + (data.usage.completion_tokens || 0);
usageRef.total = (usageRef.total || 0) + (data.usage.total_tokens || 0);
}
const content = data && data.choices && data.choices[0] &&
data.choices[0].message && data.choices[0].message.content;