Added memory system
All checks were successful
Publish Library / Build NPM Project (push) Successful in 30s
Publish Library / Tag Version (push) Successful in 5s

This commit is contained in:
2026-02-08 19:52:02 -05:00
parent d71a6be120
commit cda7db4f45
7 changed files with 163 additions and 57 deletions

View File

@@ -64,13 +64,11 @@ export class OpenAi extends LLMProvider {
}, [] as any[]);
}
ask(message: string, options: LLMRequest = {}): AbortablePromise<LLMMessage[]> {
ask(message: string, options: LLMRequest = {}): AbortablePromise<string> {
const controller = new AbortController();
const response = new Promise<any>(async (res, rej) => {
let history = [...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);
history = this.fromStandard(<any>history);
return Object.assign(new Promise<any>(async (res, rej) => {
if(options.system && options.history?.[0]?.role != 'system') options.history?.splice(0, 0, {role: 'system', content: options.system, timestamp: Date.now()});
const 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,
@@ -124,7 +122,7 @@ export class OpenAi extends LLMProvider {
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);
const result = await tool.fn(args, options.stream, this.ai);
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'})};
@@ -134,10 +132,12 @@ export class OpenAi extends LLMProvider {
requestParams.messages = history;
}
} while (!controller.signal.aborted && resp.choices?.[0]?.message?.tool_calls?.length);
history.push({role: 'assistant', content: resp.choices[0].message.content || ''});
this.toStandard(history);
if(options.stream) options.stream({done: true});
res(this.toStandard([...history, {role: 'assistant', content: resp.choices[0].message.content || ''}]));
});
return Object.assign(response, {abort: () => controller.abort()});
if(options.history) options.history.splice(0, options.history.length, ...history);
res(history.at(-1)?.content);
}), {abort: () => controller.abort()});
}
}