Added tps + duration to AI history
All checks were successful
Publish Library / Build NPM Project (push) Successful in 52s
Publish Library / Tag Version (push) Successful in 11s

This commit is contained in:
2026-08-04 09:26:57 -04:00
parent d53b1c6328
commit 62fbe73b22
4 changed files with 69 additions and 92 deletions

View File

@@ -21,10 +21,10 @@ export class Anthropic extends LLMProvider {
messages.push(<any>{timestamp, ...h});
} else {
const textContent = h.content?.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n');
if(textContent) messages.push({role: h.role, content: textContent, timestamp: timestamp});
if(textContent) messages.push({role: h.role, content: textContent, timestamp: timestamp, duration: h.duration, tps: h.tps});
h.content.forEach((c: any) => {
if(c.type == 'tool_use') {
messages.push({role: 'tool', id: c.id, name: c.name, args: c.input, timestamp: c.timestamp, content: undefined});
messages.push({role: 'tool', id: c.id, name: c.name, args: c.input, timestamp: h.timestamp, content: undefined, duration: h.duration, tps: h.tps});
} else if(c.type == 'tool_result') {
const m: any = messages.findLast(m => (<any>m).id == c.tool_use_id);
if(m) m[c.is_error ? 'error' : 'content'] = c.content;
@@ -86,15 +86,16 @@ export class Anthropic extends LLMProvider {
};
}
let resp: any, terminal = false;
let resp: any, terminal = false, duration = 0, tps = 0;
do {
requestParams.messages = history.map(({timestamp, ...m}) => m);
const callStart = Date.now();
resp = await this.client.messages.create(requestParams).catch(err => {
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`;
throw err;
});
// Streaming mode
let usage: any;
if(options.stream) {
resp.content = [];
for await (const chunk of resp) {
@@ -116,22 +117,26 @@ export class Anthropic extends LLMProvider {
} else if(chunk.type === 'content_block_stop') {
const last = resp.content.at(-1);
if(last?.input != null) last.input = last.input ? JSONAttemptParse(last.input, {}) : {};
} else if(chunk.type === 'message_delta') {
if(chunk.usage) usage = chunk.usage;
} else if(chunk.type === 'message_stop') {
break;
}
}
} else {
usage = resp.usage;
}
duration = Date.now() - callStart;
tps = usage?.output_tokens && duration > 0 ? usage.output_tokens / (duration / 1000) : 0;
// Run tools
const toolCalls = resp.content.filter((c: any) => c.type === 'tool_use');
if(toolCalls.length && !controller.signal.aborted) {
history.push({role: 'assistant', content: resp.content, timestamp: Date.now()});
history.push({role: 'assistant', content: resp.content, timestamp: Date.now(), duration, tps});
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
const tool = tools.find(findByProp('name', toolCall.name));
if(options.stream) options.stream({tool: toolCall.name});
if(!tool) return {tool_use_id: toolCall.id, is_error: true, content: 'Tool not found'};
try {
// Wrap stream so a tool's `done` ends turn gracefully
const toolStream = options.stream && ((chunk: any) => {
if(chunk.done) { terminal = true; return; }
options.stream!(chunk);
@@ -149,8 +154,9 @@ export class Anthropic extends LLMProvider {
if(!terminal) {
const textContent = resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n');
history.push({role: 'assistant', content: textContent.trim(), timestamp: Date.now()});
history.push({role: 'assistant', content: textContent.trim(), timestamp: Date.now(), duration, tps});
}
history = this.toStandard(history);
if(options.history) options.history.splice(0, options.history.length, ...history);
if(options.stream) options.stream({done: true});