Fixed timestamp breaking api calls
This commit is contained in:
@@ -47,16 +47,15 @@ export class OpenAi extends LLMProvider {
|
||||
content: null,
|
||||
tool_calls: [{ id: h.id, type: 'function', function: { name: h.name, arguments: JSON.stringify(h.args) } }],
|
||||
refusal: null,
|
||||
annotations: [],
|
||||
timestamp: h.timestamp
|
||||
annotations: []
|
||||
}, {
|
||||
role: 'tool',
|
||||
tool_call_id: h.id,
|
||||
content: h.error || h.content,
|
||||
timestamp: Date.now()
|
||||
content: h.error || h.content
|
||||
});
|
||||
} else {
|
||||
result.push(h);
|
||||
const {timestamp, ...rest} = h;
|
||||
result.push(rest);
|
||||
}
|
||||
return result;
|
||||
}, [] as any[]);
|
||||
@@ -111,20 +110,20 @@ export class OpenAi extends LLMProvider {
|
||||
|
||||
const toolCalls = resp.choices[0].message.tool_calls || [];
|
||||
if(toolCalls.length && !controller.signal.aborted) {
|
||||
history.push({...resp.choices[0].message, timestamp: Date.now()});
|
||||
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));
|
||||
if(!tool) return {role: 'tool', tool_call_id: toolCall.id, content: '{"error": "Tool not found"}', timestamp: Date.now()};
|
||||
if(!tool) return {role: 'tool', tool_call_id: toolCall.id, content: '{"error": "Tool not found"}'};
|
||||
try {
|
||||
const args = JSONAttemptParse(toolCall.function.arguments, {});
|
||||
const result = await tool.fn(args, this.ai);
|
||||
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize(result), timestamp: Date.now()};
|
||||
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize(result)};
|
||||
} catch (err: any) {
|
||||
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'}), timestamp: Date.now()};
|
||||
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'})};
|
||||
}
|
||||
}));
|
||||
history.push(...results);
|
||||
loopMessages.push(...results);
|
||||
loopMessages.push(...results.map(r => ({...r, timestamp: Date.now()})));
|
||||
requestParams.messages = history;
|
||||
}
|
||||
} while (!controller.signal.aborted && resp.choices?.[0]?.message?.tool_calls?.length);
|
||||
@@ -134,7 +133,6 @@ export class OpenAi extends LLMProvider {
|
||||
if(options.stream) options.stream({done: true});
|
||||
res(this.toStandard([...history, {role: 'assistant', content: combinedContent, timestamp: Date.now()}]));
|
||||
});
|
||||
|
||||
return Object.assign(response, {abort: () => controller.abort()});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user