UNPKG

@llmbridge/core

Version:

Core package for unified LLM interface

220 lines (215 loc) 7.34 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { LLMBridge: () => LLMBridge }); module.exports = __toCommonJS(index_exports); // src/abort.ts var abort = { value: null }; // src/types.ts function isToolRequest(message) { return "id" in message && "name" in message && "input" in message; } function isToolResponse(message) { return "id" in message && "output" in message; } function isTextMessage(message) { return !isToolRequest(message) && !isToolResponse(message) && typeof message.content === "string"; } function isMediaMessage(message) { return "content" in message && Array.isArray(message.content); } // src/index.ts var import_dotenv = require("dotenv"); var import_path = require("path"); (0, import_dotenv.config)({ path: (0, import_path.resolve)(process.cwd(), ".env") }); var providers = {}; var Plugins = class { constructor() { this.list = []; } add(plugin) { this.list.push(plugin); } async beforeRun(context) { const beforeResponse = []; for (let i = 0; i < this.list.length; i++) { beforeResponse.push(await this.list[i].beforeRun?.(context)); } return beforeResponse; } async afterRun(response, beforeResponse, context) { for (let i = this.list.length - 1; i >= 0; i--) { await this.list[i].afterRun?.({ beforeResponse: beforeResponse[i], response, context }); } } async beforeExec(params, context) { const beforeResponse = []; for (let i = 0; i < this.list.length; i++) { beforeResponse.push(await this.list[i].beforeExec?.(params, context)); } return beforeResponse; } async afterExec(response, beforeResponses, context) { for (let i = this.list.length - 1; i >= 0; i--) { await this.list[i].afterExec?.({ response, beforeResponse: beforeResponses[i], context }); } } async beforeToolExec(tool, counter, input, context) { const beforeResponse = []; for (let i = 0; i < this.list.length; i++) { beforeResponse.push(await this.list[i].beforeToolExec?.(tool, counter, input, context)); } return beforeResponse; } async afterToolExec(response, tool, counter, beforeResponses, context) { for (let i = this.list.length - 1; i >= 0; i--) { await this.list[i].afterToolExec?.({ response, tool, counter, beforeResponse: beforeResponses[i], context }); } } async beforeGetEmbeddings(params, context) { const beforeResponse = []; for (let i = 0; i < this.list.length; i++) { beforeResponse.push(await this.list[i].beforeGetEmbeddings?.(params, context)); } return beforeResponse; } async afterGetEmbeddings(response, beforeResponses, context) { for (let i = this.list.length - 1; i >= 0; i--) { await this.list[i].afterGetEmbeddings?.({ response, beforeResponse: beforeResponses[i], context }); } } }; var plugins = new Plugins(); var LLMBridge; ((LLMBridge2) => { LLMBridge2.isTextMessage = isTextMessage; LLMBridge2.isMediaMessage = isMediaMessage; LLMBridge2.isToolRequest = isToolRequest; LLMBridge2.isToolResponse = isToolResponse; function registerProvider(name, provider) { providers[name] = provider; } LLMBridge2.registerProvider = registerProvider; function use(plugin) { plugins.add(plugin); } LLMBridge2.use = use; async function getTextResponse(model, system_prompt, messages, options) { const result = (await run(model, system_prompt, messages, options)).messages; const lastMessage = result[result.length - 1]; if (isTextMessage(lastMessage)) { return lastMessage.content; } return null; } LLMBridge2.getTextResponse = getTextResponse; async function run(model, systemPrompt, messages, options) { const prevAbort = abort.value; const controller = new AbortController(); abort.value = controller; function checkAbort() { if (controller.signal.aborted) { throw new Error("Operation aborted"); } } try { const [providerName, modelName] = model.split("/"); let result = null; const provider = providers[providerName]; if (!provider) { throw new Error(`Provider ${providerName} not found`); } const context = { model, systemPrompt, messages, options, provider, providerName }; const beforeResponse = await plugins.beforeRun(context); result = await provider.run(providerName, modelName, systemPrompt, messages, checkAbort, plugins, options); await plugins.afterRun(result, beforeResponse, context); return result; } finally { abort.value = prevAbort; } } LLMBridge2.run = run; async function getEmbeddings(model, text) { const [providerName, modelName] = model.split("/"); const provider = providers[providerName]; if (!provider) { throw new Error(`Provider ${providerName} not found`); } const response = await provider.getEmbeddings(modelName, plugins, text); return response; } LLMBridge2.getEmbeddings = getEmbeddings; function cleanJsonSchema(schema) { if (typeof schema !== "object" || schema === null) { return schema; } const cleanedSchema = Array.isArray(schema) ? [] : {}; for (const [key, value] of Object.entries(schema)) { if (key === "additionalProperties" || key === "$schema") { continue; } if (typeof value === "object" && value !== null) { cleanedSchema[key] = cleanJsonSchema(value); } else { cleanedSchema[key] = value; } } return cleanedSchema; } function addPropertyOrdering(schema) { if (typeof schema !== "object" || schema === null || Array.isArray(schema)) { return schema; } if (schema.required && !schema.propertyOrdering) { const requiredArray = Array.isArray(schema.required) ? schema.required : Object.values(schema.required); const allStrings = requiredArray.every((item) => typeof item === "string"); if (allStrings && requiredArray.length > 0) { schema.propertyOrdering = [...requiredArray]; } } for (const [key, value] of Object.entries(schema)) { if (typeof value === "object" && !Array.isArray(value) && value !== null) { schema[key] = addPropertyOrdering(value); } } return schema; } LLMBridge2.utils = { cleanJsonSchema, addPropertyOrdering }; })(LLMBridge || (LLMBridge = {})); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { LLMBridge });