UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

72 lines (71 loc) 3.03 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 { LangMessages } from "../messages.js"; import { OpenAIChatCompletionsLang } from "../openai/openai-chat-completions-lang.js"; class MockOpenAILikeLang extends OpenAIChatCompletionsLang { constructor(options = {}) { super({ apiKey: "", // not used model: options.model || "gpt-4o-mini", systemPrompt: options.systemPrompt || "", baseURL: "http://mock.local" // not used }); __publicField(this, "mockConfig"); this.mockConfig = options; } async ask(prompt, options) { const messages = new LangMessages(); messages.addUserMessage(prompt); return this.chat(messages, options); } async chat(messages, options) { const messageCollection = messages instanceof LangMessages ? messages : new LangMessages(messages); const result = messageCollection; const onResult = options == null ? void 0 : options.onResult; if (this.mockConfig.mockToolCalls && this.mockConfig.mockToolCalls.length > 0) { let index = 0; for (const call of this.mockConfig.mockToolCalls) { const id = call.id || `call_${index++}`; const nameDelta = { choices: [{ delta: { tool_calls: [{ id, function: { name: call.name } }] } }] }; this.handleStreamData(nameDelta, result, onResult); for (const chunk of call.argumentsChunks) { const delta = { choices: [{ delta: { tool_calls: [{ id, function: { arguments: chunk } }] } }] }; this.handleStreamData(delta, result, onResult); } } this.handleStreamData({ finished: true }, result, onResult); this.mockConfig.mockToolCalls = []; return result; } let fullContent = "Hello from MockOpenAI"; if ((options == null ? void 0 : options.schema) && this.mockConfig.mockResponseObject !== void 0) { fullContent = JSON.stringify(this.mockConfig.mockResponseObject); } else if (typeof this.mockConfig.mockResponseText === "function") { fullContent = this.mockConfig.mockResponseText(); } else if (typeof this.mockConfig.mockResponseText === "string") { fullContent = this.mockConfig.mockResponseText; } const chunkSize = this.mockConfig.chunkSize || 16; const chunks = []; for (let i = 0; i < fullContent.length; i += chunkSize) { chunks.push(fullContent.slice(i, i + chunkSize)); } for (const chunk of chunks) { const deltaPayload = { choices: [ { delta: { content: chunk } } ] }; this.handleStreamData(deltaPayload, result, onResult); } this.handleStreamData({ finished: true }, result, onResult); return result; } } export { MockOpenAILikeLang }; //# sourceMappingURL=mock-openai-like-lang.js.map