Fixed tool calls
All checks were successful
Publish Library / Build NPM Project (push) Successful in 39s
Publish Library / Tag Version (push) Successful in 8s

This commit is contained in:
2025-12-27 17:27:53 -05:00
parent 98dd0bb323
commit 6dce0e8954
6 changed files with 29 additions and 15 deletions

View File

@@ -67,13 +67,14 @@ export class OpenAi extends LLMProvider {
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
if(options.compress) history = await this.ai.language.compressHistory(<any>history, options.compress.max, options.compress.min, options);
const tools = options.tools || this.ai.options.tools || [];
const requestParams: any = {
model: options.model || this.model,
messages: history,
stream: !!options.stream,
max_tokens: options.max_tokens || this.ai.options.max_tokens || 4096,
temperature: options.temperature || this.ai.options.temperature || 0.7,
tools: (options.tools || this.ai.options.tools || []).map(t => ({
tools: tools.map(t => ({
type: 'function',
function: {
name: t.name,
@@ -89,7 +90,11 @@ export class OpenAi extends LLMProvider {
let resp: any, isFirstMessage = true;
do {
resp = await this.client.chat.completions.create(requestParams);
resp = await this.client.chat.completions.create(requestParams).catch(err => {
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`;
throw err;
});
if(options.stream) {
if(!isFirstMessage) options.stream({text: '\n\n'});
else isFirstMessage = false;
@@ -110,7 +115,7 @@ export class OpenAi extends LLMProvider {
if(toolCalls.length && !controller.signal.aborted) {
history.push(resp.choices[0].message);
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
const tool = options.tools?.find(findByProp('name', toolCall.function.name));
const tool = tools?.find(findByProp('name', toolCall.function.name));
if(!tool) return {role: 'tool', tool_call_id: toolCall.id, content: '{"error": "Tool not found"}'};
try {
const args = JSONAttemptParse(toolCall.function.arguments, {});