Fixed message history and response
All checks were successful
Publish Library / Build NPM Project (push) Successful in 59s
Publish Library / Tag Version (push) Successful in 22s

This commit is contained in:
2026-08-03 19:30:39 -04:00
parent afc6653364
commit 89619e211e
5 changed files with 44 additions and 34 deletions

View File

@@ -86,7 +86,7 @@ export class Anthropic extends LLMProvider {
};
}
let resp: any, hasStreamedText = false, terminal = false;
let resp: any, terminal = false;
do {
requestParams.messages = history.map(({timestamp, ...m}) => m);
resp = await this.client.messages.create(requestParams).catch(err => {
@@ -96,7 +96,6 @@ export class Anthropic extends LLMProvider {
// Streaming mode
if(options.stream) {
if(hasStreamedText) options.stream({text: '\n\n'});
resp.content = [];
for await (const chunk of resp) {
if(controller.signal.aborted) break;
@@ -110,7 +109,7 @@ export class Anthropic extends LLMProvider {
if(chunk.delta.type === 'text_delta') {
const text = chunk.delta.text;
resp.content.at(-1).text += text;
if(text) { hasStreamedText = true; options.stream({text}); }
options.stream({text});
} else if(chunk.delta.type === 'input_json_delta') {
resp.content.at(-1).input += chunk.delta.partial_json;
}
@@ -137,7 +136,7 @@ export class Anthropic extends LLMProvider {
if(chunk.done) { terminal = true; return; }
options.stream!(chunk);
});
const result = await tool.fn(toolCall.input, toolStream, this.ai);
const result = await tool.fn(toolCall.input, toolStream, this.ai, toolCall.id);
return {type: 'tool_result', tool_use_id: toolCall.id, content: typeof result == 'object' ? JSONSanitize(result) : result};
} catch (err: any) {
return {type: 'tool_result', tool_use_id: toolCall.id, is_error: true, content: err?.message || err?.toString() || 'Unknown'};
@@ -150,12 +149,19 @@ 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, timestamp: Date.now()});
history.push({role: 'assistant', content: textContent.trim(), timestamp: Date.now()});
}
history = this.toStandard(history);
if(options.stream) options.stream({done: true});
if(options.history) options.history.splice(0, options.history.length, ...history);
const finalContent = history.at(-1)?.content;
if(options.stream) options.stream({done: true});
const turnStart = history.map(h => h.role).lastIndexOf('user');
const finalContent = history.slice(turnStart + 1).reduce((str, h) => {
if(h.role === 'assistant') return str + (h.content || '');
if(h.role === 'tool') return str + `<tool>${h.name}</tool>\n\n`;
return str;
}, '').trim();
res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent);
}), {abort: () => controller.abort()});
}