ai: add usage metadata for openai and mistral

This commit is contained in:
end-4 2025-08-07 21:53:37 +07:00
parent f581fd4821
commit 733a792610
2 changed files with 23 additions and 0 deletions

View file

@ -100,6 +100,17 @@ ApiStrategy {
message.content += newContent;
message.rawContent += newContent;
// Usage metadata
if (dataJson.usage) {
return {
tokenUsage: {
input: dataJson.usage.prompt_tokens ?? -1,
output: dataJson.usage.completion_tokens ?? -1,
total: dataJson.usage.total_tokens ?? -1
}
};
}
if (`dataJson`.done) {
return { finished: true };
}

View file

@ -46,6 +46,7 @@ ApiStrategy {
// Real stuff
try {
const dataJson = JSON.parse(cleanData);
console.log(JSON.stringify(dataJson, null, 2));
let newContent = "";
const responseContent = dataJson.choices[0]?.delta?.content || dataJson.message?.content;
@ -72,6 +73,17 @@ ApiStrategy {
message.content += newContent;
message.rawContent += newContent;
// Usage metadata
if (dataJson.usage) {
return {
tokenUsage: {
input: dataJson.usage.prompt_tokens ?? -1,
output: dataJson.usage.completion_tokens ?? -1,
total: dataJson.usage.total_tokens ?? -1
}
};
}
if (dataJson.done) {
return { finished: true };
}