UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

62 lines (61 loc) 1.97 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { Agent } from "./agent.js"; import { LangMessages } from "../lang/index.js"; class ChatAgent extends Agent { constructor(lang, options) { super(); __publicField(this, "lang"); __publicField(this, "messages"); this.lang = lang; this.messages = new LangMessages([], { tools: options == null ? void 0 : options.tools }); } async runInternal(input, options) { if (input instanceof LangMessages) { this.messages = input; } else { this.messages.push(...new LangMessages(input)); } if (!this.lang) { throw new Error("Language provider not set"); } let streamIdx = 0; while (true) { let lastRoleInRun = null; const response = await this.lang.chat(this.messages, { onResult: (msg) => { if (msg.role != lastRoleInRun) { if (lastRoleInRun !== null) { streamIdx++; } lastRoleInRun = msg.role; } this.emit({ type: "streaming", data: { msg, idx: streamIdx } }); }, signal: options == null ? void 0 : options.signal }); this.messages = response; const lastMessage = this.messages[this.messages.length - 1]; const lastMessageHasToolResults = lastMessage && lastMessage.toolResults.length > 0; if (!lastMessageHasToolResults) { break; } streamIdx++; } this.emit({ type: "finished", output: this.messages }); return this.messages; } getMessages() { return this.messages; } setLanguageProvider(lang) { this.lang = lang; } } export { ChatAgent }; //# sourceMappingURL=ChatAgent.js.map