|
|
|
|
@@ -13,25 +13,25 @@ export class Anthropic extends LLMProvider {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private toStandard(history: any[]): LLMMessage[] {
|
|
|
|
|
for(let i = 0; i < history.length; i++) {
|
|
|
|
|
const orgI = i;
|
|
|
|
|
if(typeof history[orgI].content != 'string') {
|
|
|
|
|
if(history[orgI].role == 'assistant') {
|
|
|
|
|
history[orgI].content.filter((c: any) => c.type =='tool_use').forEach((c: any) => {
|
|
|
|
|
history.splice(i + 1, 0, {role: 'tool', id: c.id, name: c.name, args: c.input, timestamp: Date.now()});
|
|
|
|
|
});
|
|
|
|
|
} else if(history[orgI].role == 'user') {
|
|
|
|
|
history[orgI].content.filter((c: any) => c.type =='tool_result').forEach((c: any) => {
|
|
|
|
|
const h = history.find((h: any) => h.id == c.tool_use_id);
|
|
|
|
|
h[c.is_error ? 'error' : 'content'] = c.content;
|
|
|
|
|
const timestamp = Date.now();
|
|
|
|
|
const messages: LLMMessage[] = [];
|
|
|
|
|
for(let h of history) {
|
|
|
|
|
if(typeof h.content == 'string') {
|
|
|
|
|
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({timestamp, role: h.role, content: textContent});
|
|
|
|
|
h.content.forEach((c: any) => {
|
|
|
|
|
if(c.type == 'tool_use') {
|
|
|
|
|
messages.push({timestamp, role: 'tool', id: c.id, name: c.name, args: c.input, content: undefined});
|
|
|
|
|
} 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;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
history[orgI].content = history[orgI].content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n');
|
|
|
|
|
if(!history[orgI].content) history.splice(orgI, 1);
|
|
|
|
|
}
|
|
|
|
|
if(!history[orgI].timestamp) history[orgI].timestamp = Date.now();
|
|
|
|
|
}
|
|
|
|
|
return history.filter(h => !!h.content);
|
|
|
|
|
return messages;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fromStandard(history: LLMMessage[]): any[] {
|
|
|
|
|
@@ -50,8 +50,8 @@ export class Anthropic extends LLMProvider {
|
|
|
|
|
|
|
|
|
|
ask(message: string, options: LLMRequest = {}): AbortablePromise<string> {
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
return Object.assign(new Promise<any>(async (res, rej) => {
|
|
|
|
|
const history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
|
|
|
|
|
return Object.assign(new Promise<any>(async (res) => {
|
|
|
|
|
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
|
|
|
|
|
const tools = options.tools || this.ai.options.llm?.tools || [];
|
|
|
|
|
const requestParams: any = {
|
|
|
|
|
model: options.model || this.model,
|
|
|
|
|
@@ -73,7 +73,6 @@ export class Anthropic extends LLMProvider {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let resp: any, isFirstMessage = true;
|
|
|
|
|
const assistantMessages: string[] = [];
|
|
|
|
|
do {
|
|
|
|
|
resp = await this.client.messages.create(requestParams).catch(err => {
|
|
|
|
|
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`;
|
|
|
|
|
@@ -119,7 +118,6 @@ export class Anthropic extends LLMProvider {
|
|
|
|
|
if(options.stream) options.stream({tool: toolCall.name});
|
|
|
|
|
if(!tool) return {tool_use_id: toolCall.id, is_error: true, content: 'Tool not found'};
|
|
|
|
|
try {
|
|
|
|
|
console.log(typeof tool.fn);
|
|
|
|
|
const result = await tool.fn(toolCall.input, options?.stream, this.ai);
|
|
|
|
|
return {type: 'tool_result', tool_use_id: toolCall.id, content: JSONSanitize(result)};
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
@@ -131,7 +129,7 @@ export class Anthropic extends LLMProvider {
|
|
|
|
|
}
|
|
|
|
|
} while (!controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use'));
|
|
|
|
|
history.push({role: 'assistant', content: resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n')});
|
|
|
|
|
this.toStandard(history);
|
|
|
|
|
history = this.toStandard(history);
|
|
|
|
|
|
|
|
|
|
if(options.stream) options.stream({done: true});
|
|
|
|
|
if(options.history) options.history.splice(0, options.history.length, ...history);
|
|
|
|
|
|