UNPKG

llm-exe

Version:

Simplify building LLM-powered apps with easy-to-use base components, supporting text and chat-based prompts with handlebars template engine, output parsers, and flexible function calling capabilities.

1,653 lines (1,580 loc) 139 kB
var __defProp = Object.defineProperty; var __typeError = (msg) => { throw TypeError(msg); }; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) { if (typeof require !== "undefined") return require.apply(this, arguments); throw Error('Dynamic require of "' + x + '" is not supported'); }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); // src/utils/modules/pick.ts function pick(object, keys) { return keys.reduce((obj, key) => { if (object && object.hasOwnProperty(key)) { obj[key] = object[key]; } return obj; }, {}); } __name(pick, "pick"); // src/utils/modules/ensureInputIsObject.ts function ensureInputIsObject(input) { if (input === null || typeof input === "undefined") { return { input }; } switch (typeof input) { case "object": { if (Array.isArray(input)) { return { input }; } return input; } default: return { input }; } } __name(ensureInputIsObject, "ensureInputIsObject"); // src/utils/modules/uuid.ts import { v4 as uuidv4 } from "uuid"; // src/executor/_metadata.ts var _state; var _ExecutorExecutionMetadataState = class _ExecutorExecutionMetadataState { constructor(items) { __privateAdd(this, _state, { start: null, end: null, input: void 0, handlerInput: void 0, handlerOutput: void 0, output: void 0, errorMessage: void 0, error: void 0, metadata: null }); if (items) { this.setItem(items); } } setItem(items) { if (!items || typeof items !== "object" || Array.isArray(items)) { return this; } const keys = Object.keys(items); for (const key of keys) { const value = items[key]; __privateGet(this, _state)[key] = value; } return this; } asPlainObject() { return Object.freeze({ start: __privateGet(this, _state).start, end: __privateGet(this, _state).end, input: __privateGet(this, _state).input, handlerInput: __privateGet(this, _state).handlerInput, handlerOutput: __privateGet(this, _state).handlerOutput, output: __privateGet(this, _state).output, errorMessage: __privateGet(this, _state).errorMessage, error: __privateGet(this, _state).error, metadata: __privateGet(this, _state).metadata }); } }; _state = new WeakMap(); __name(_ExecutorExecutionMetadataState, "ExecutorExecutionMetadataState"); var ExecutorExecutionMetadataState = _ExecutorExecutionMetadataState; function createMetadataState(items) { return new ExecutorExecutionMetadataState(items); } __name(createMetadataState, "createMetadataState"); // src/utils/const.ts var hookOnComplete = `onComplete`; var hookOnError = `onError`; var hookOnSuccess = `onSuccess`; // src/executor/_base.ts var _BaseExecutor = class _BaseExecutor { constructor(name, type, options) { /** * @property id - internal id of the executor */ __publicField(this, "id"); /** * @property type - type of executor */ __publicField(this, "type"); /** * @property created - timestamp date created */ __publicField(this, "created"); /** * @property name - name of executor */ __publicField(this, "name"); /** * @property executions - */ __publicField(this, "executions"); __publicField(this, "traceId", null); /** * @property hooks - hooks to be ran during execution */ __publicField(this, "hooks"); __publicField(this, "allowedHooks", [ hookOnComplete, hookOnError, hookOnSuccess ]); this.id = uuidv4(); this.type = type; this.name = name; this.created = (/* @__PURE__ */ new Date()).getTime(); this.executions = 0; this.hooks = { onSuccess: [], onError: [], onComplete: [] }; if (options?.hooks) { this.setHooks(options.hooks); } } /** * * Used to filter the input of the handler * @param _input * @returns original input formatted for handler */ async getHandlerInput(_input, _metadata, _options) { return ensureInputIsObject(_input); } /** * * Used to filter the output of the handler * @param _input * @returns output O */ getHandlerOutput(out, _metadata, _options) { return out; } /** * * execute - Runs the executor * @param _input * @returns handler output */ async execute(_input, _options) { this.executions++; const _metadata = createMetadataState({ start: (/* @__PURE__ */ new Date()).getTime(), input: _input }); try { const input = await this.getHandlerInput(_input, _metadata.asPlainObject(), _options); _metadata.setItem({ handlerInput: input }); let result = await this.handler(input, _options); _metadata.setItem({ handlerOutput: result }); const output = this.getHandlerOutput(result, _metadata.asPlainObject(), _options); _metadata.setItem({ output }); this.runHook("onSuccess", _metadata.asPlainObject()); return output; } catch (error) { _metadata.setItem({ error, errorMessage: error.message }); this.runHook("onError", _metadata.asPlainObject()); throw error; } finally { _metadata.setItem({ end: (/* @__PURE__ */ new Date()).getTime() }); this.runHook("onComplete", _metadata.asPlainObject()); } } metadata() { return {}; } getMetadata(metadata) { return Object.assign({}, this.metadata(), { traceId: this.getTraceId(), id: this.id, type: this.type, name: this.name, created: this.created, executions: this.executions, metadata }); } runHook(hook, _metadata) { const { [hook]: hooks = [] } = pick(this.hooks, this.allowedHooks); for (const hookFn of [ ...hooks ]) { if (typeof hookFn === "function") { try { hookFn(_metadata, this.getMetadata()); } catch (error) { } } } } setHooks(hooks = {}) { const hookKeys = Object.keys(hooks); for (const hookKey of hookKeys.filter((k) => this.allowedHooks.includes(k))) { const hookInput = hooks[hookKey]; if (hookInput) { const _hooks = Array.isArray(hookInput) ? hookInput : [ hookInput ]; for (const hook of _hooks) { if (hook && typeof hook === "function" && !this.hooks[hookKey].find((h) => h === hook)) { this.hooks[hookKey].push(hook); } } } } return this; } removeHook(eventName, fn) { if (typeof fn !== "function") return this; const lis = this.hooks[eventName]; if (!lis) return this; for (let i = lis.length; i >= 0; i--) { if (lis[i] === fn) { lis.splice(i, 1); break; } } return this; } on(eventName, fn) { return this.setHooks({ [eventName]: fn }); } off(eventName, fn) { return this.removeHook(eventName, fn); } once(eventName, fn) { if (typeof fn !== "function") return this; const onceWrapper = /* @__PURE__ */ __name((...args) => { fn(...args); this.off(eventName, onceWrapper); }, "onceWrapper"); this.hooks[eventName].push(onceWrapper); return this; } withTraceId(traceId) { this.traceId = traceId; return this; } getTraceId() { return this.traceId; } }; __name(_BaseExecutor, "BaseExecutor"); var BaseExecutor = _BaseExecutor; // src/utils/modules/inferFunctionName.ts function inferFunctionName(func, defaultName) { const name = func?.name; if (name && typeof name === "string") { if (name.substring(0, 6) === "bound ") { return name.replace("bound ", ""); } else { return name; } } var result = /^function\s+([\w\$]+)\s*\(/.exec(func.toString()); return result ? result[1] : defaultName; } __name(inferFunctionName, "inferFunctionName"); // src/executor/core.ts var _CoreExecutor = class _CoreExecutor extends BaseExecutor { constructor(fn, options) { const name = fn?.name ? fn.name : inferFunctionName(fn.handler, "anonymous-core-executor"); super(name, "function-executor", options); __publicField(this, "_handler"); this._handler = fn.handler.bind(null); } async handler(_input) { return this._handler.call(null, _input); } }; __name(_CoreExecutor, "CoreExecutor"); var CoreExecutor = _CoreExecutor; // src/parser/_base.ts var _BaseParser = class _BaseParser { /** * Create a new BaseParser. * @param name - The name of the parser. * @param options - options */ constructor(name, options = {}, target = "text") { __publicField(this, "name"); __publicField(this, "options"); __publicField(this, "target", "text"); this.name = name; this.target = target; if (options) { this.options = options; } } }; __name(_BaseParser, "BaseParser"); var BaseParser = _BaseParser; var _BaseParserWithJson = class _BaseParserWithJson extends BaseParser { /** * Create a new BaseParser. * @param name - The name of the parser. * @param options - options */ constructor(name, options) { super(name); __publicField(this, "schema"); __publicField(this, "validateSchema"); const { schema, validateSchema: validateSchema2 } = options; this.validateSchema = !!validateSchema2; if (schema) { this.schema = schema; } } }; __name(_BaseParserWithJson, "BaseParserWithJson"); var BaseParserWithJson = _BaseParserWithJson; // src/utils/index.ts var utils_exports = {}; __export(utils_exports, { assert: () => assert, asyncCallWithTimeout: () => asyncCallWithTimeout, defineSchema: () => defineSchema, filterObjectOnSchema: () => filterObjectOnSchema, guessProviderFromModel: () => guessProviderFromModel, importHelpers: () => importHelpers, importPartials: () => importPartials, isObjectStringified: () => isObjectStringified, maybeParseJSON: () => maybeParseJSON, maybeStringifyJSON: () => maybeStringifyJSON, registerHelpers: () => registerHelpers, registerPartials: () => registerPartials, replaceTemplateString: () => replaceTemplateString, replaceTemplateStringAsync: () => replaceTemplateStringAsync }); // src/utils/modules/assert.ts function assert(condition, message) { if (condition === void 0 || condition === null || condition === false) { if (typeof message === "string") { throw new Error(message); } else if (message instanceof Error) { throw message; } else { throw new Error(`Assertion error`); } } } __name(assert, "assert"); // src/utils/modules/defineSchema.ts import { asConst } from "json-schema-to-ts"; function defineSchema(obj) { obj.additionalProperties = false; return asConst(obj); } __name(defineSchema, "defineSchema"); // src/utils/modules/handlebars/utils/importPartials.ts function importPartials(_partials) { let partials2 = []; if (_partials) { const externalPartialKeys = Object.keys(_partials); for (const externalPartialKey of externalPartialKeys) { if (typeof externalPartialKey === "string") { partials2.push({ name: externalPartialKey, template: _partials[externalPartialKey] }); } } } return partials2; } __name(importPartials, "importPartials"); // src/utils/modules/handlebars/utils/importHelpers.ts function importHelpers(_helpers) { let helpers = []; if (_helpers) { const externalHelperKeys = Object.keys(_helpers); for (const externalHelperKey of externalHelperKeys) { if (typeof externalHelperKey === "string") { helpers.push({ name: externalHelperKey, handler: _helpers[externalHelperKey] }); } } } return helpers; } __name(importHelpers, "importHelpers"); // src/utils/modules/toNumber.ts function toNumber(value) { if (typeof value === "number") { return value; } if (typeof value === "string" && value.trim() !== "") { return Number(value); } return NaN; } __name(toNumber, "toNumber"); // src/utils/modules/get.ts function get(obj, path, defaultValue) { if (obj == null || path === "" || Array.isArray(path) && path.length === 0) { return defaultValue; } const pathString = Array.isArray(path) ? path.join(".") : path; const travel = /* @__PURE__ */ __name((regexp) => pathString.split(regexp).filter(Boolean).reduce((res, key) => res !== null && res !== void 0 ? res[key] : res, obj), "travel"); const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/); return result === void 0 || result === obj ? defaultValue : result === null ? defaultValue : result; } __name(get, "get"); // src/utils/modules/filterObjectOnSchema.ts function isObject(obj) { return obj === Object(obj); } __name(isObject, "isObject"); function getType(schemaType) { if (!Array.isArray(schemaType)) { return schemaType; } var typeArray = schemaType; for (var i = 0; i < typeArray.length; i++) { var type = typeArray[i]; if (type !== "null") { return type; } } } __name(getType, "getType"); function filterObjectOnSchema(schema, doc, detach, property) { let result; if (doc === null || doc === void 0) { return doc; } var type = getType(schema.type); if (type === "object" && isObject(doc) && schema.properties) { result = {}; Object.keys(schema.properties).forEach(function(key) { var child = doc[key]; var sp = schema.properties[key]; var filteredChild = filterObjectOnSchema(sp, child, detach, property); if (property) { result[key] = get(sp, property, ""); } else { if (filteredChild === void 0) { if (typeof sp?.default !== "undefined") { result[key] = get(sp, "default", ""); } else { return; } } else { if (sp.type === "integer" || sp.type === "number") { result[key] = toNumber(filteredChild); } else if (sp.type === "boolean") { result[key] = !!filteredChild; } else { result[key] = filteredChild; } } } }); } else if (type === "object" && isObject(doc) && detach) { return {}; } else if (type === "array" && Array.isArray(doc) && schema.items) { result = []; doc.forEach(function(item) { result.push(filterObjectOnSchema(schema.items, item, detach, property)); }); } else { result = doc; } return result; } __name(filterObjectOnSchema, "filterObjectOnSchema"); // src/utils/modules/handlebars/hbs.ts import Handlebars from "handlebars"; // src/utils/modules/handlebars/utils/registerPartials.ts function _registerPartials(partials2, instance) { if (partials2 && Array.isArray(partials2)) { for (const partial of partials2) { if (partial.name && typeof partial.name === "string" && typeof partial.template === "string") { if (instance) { instance.registerPartial(partial.name, partial.template); } } } } } __name(_registerPartials, "_registerPartials"); // src/utils/modules/handlebars/utils/registerHelpers.ts function _registerHelpers(helpers, instance) { if (helpers && Array.isArray(helpers)) { for (const helper of helpers) { if (helper.name && typeof helper.name === "string" && typeof helper.handler === "function") { if (instance) { instance.registerHelper(helper.name, helper.handler); } } } } } __name(_registerHelpers, "_registerHelpers"); // src/utils/modules/getEnvironmentVariable.ts function getEnvironmentVariable(name) { if (typeof process === "object" && process?.env) { return process.env[name]; } else { return void 0; } } __name(getEnvironmentVariable, "getEnvironmentVariable"); // src/utils/modules/handlebars/templates/index.ts var MarkdownCode = `{{#if code}}\`\`\`{{#if language}}{{language}}{{/if}} {{{code}}} \`\`\`{{/if}}`; var ThoughtActionResult = ` {{#if title}}{{#if attributes.stepsTaken.length}}{{title}} {{/if}}{{/if}} {{#each attributes.stepsTaken as | step |}} {{#if step.thought}}Thought: {{step.result}}{{/if}} {{#if step.result}}Action: {{step.action}}{{/if}} {{#if step.result}}Result: {{step.result}}{{/if}} {{/each}}`; var ChatConversationHistory = `{{~#if title}}{{~#if chat_history.length}}{{title}} {{~/if}}{{~/if}} {{#each chat_history as | item |}} {{~#eq item.role 'user'}}{{#if @last}}{{../mostRecentRolePrefix}}{{../userName}}{{../mostRecentRoleSuffix}}{{else}}{{../userName}}{{/if}}: {{#if @last}}{{../mostRecentMessagePrefix}}{{/if}}{{{item.content}}}{{#if @last}}{{../mostRecentMessageSuffix}}{{/if}} {{/eq}} {{~#eq item.role 'assistant'}}{{#if @last}}{{../mostRecentRolePrefix}}{{../assistantName}}{{../mostRecentRoleSuffix}}{{else}}{{../assistantName}}{{/if}}: {{#if @last}}{{../mostRecentMessagePrefix}}{{/if}}{{{item.content}}}{{#if @last}}{{../mostRecentMessageSuffix}}{{/if}} {{/eq}} {{~#eq item.role 'system'}}{{../systemName}}: {{{item.content}}} {{/eq}} {{~/each}}`; var DialogueHistory = `{{>ChatConversationHistory title=title chat_history=(getKeyOr key []) assistantName=(getOr assistant 'Assistant') userName=(getOr user 'User') systemName=(getOr system 'System') mostRecentRolePrefix=mostRecentRolePrefix mostRecentRoleSuffix=mostRecentRoleSuffix mostRecentMessagePrefix=mostRecentMessagePrefix mostRecentMessageSuffix=mostRecentMessageSuffix}}`; var SingleChatMessage = `{{~#eq role 'user'}}{{getOr name 'User'}}: {{{content}}}{{~/eq}} {{~#eq role 'assistant'}}{{getOr assistant 'Assistant'}}: {{{content}}}{{~/eq}} {{~#eq role 'system'}}{{getOr system 'System'}}: {{{content}}}{{~/eq}}`; var ThoughtsAndObservations = `{{~#each thoughts as | step |}} {{~#if step.thought}}Thought: {{{step.thought}}} {{/if}} {{~#if step.observation}}Observation: {{{step.observation}}} {{/if}} {{~/each}}`; var JsonSchema = `{{#if (getKeyOr key false)}} \`\`\`json {{{indentJson (getKeyOr key) collapse}}} \`\`\` {{~/if}}`; var JsonSchemaExampleJson = `{{#if (getOr key false)}} \`\`\`json {{{jsonSchemaExample key (getOr property '') collapse}}} \`\`\` {{~/if}}`; var partials = { JsonSchema, JsonSchemaExampleJson, MarkdownCode, DialogueHistory, SingleChatMessage, ChatConversationHistory, ThoughtsAndObservations, ThoughtActionResult }; // src/utils/modules/handlebars/helpers/index.ts var helpers_exports = {}; __export(helpers_exports, { cut: () => cutFn, eq: () => eq, getKeyOr: () => getKeyOr, getOr: () => getOr, ifCond: () => ifCond, indentJson: () => indentJson, join: () => join, jsonSchemaExample: () => jsonSchemaExample, neq: () => neq, objectToList: () => objectToList, pluralize: () => pluralize, substring: () => substringFn, with: () => withFn }); // src/utils/modules/json.ts var maybeStringifyJSON = /* @__PURE__ */ __name((objOrMaybeString) => { if (!objOrMaybeString || typeof objOrMaybeString !== "object") { return objOrMaybeString; } try { const result = JSON.stringify(objOrMaybeString); return result; } catch (error) { } return ""; }, "maybeStringifyJSON"); var maybeParseJSON = /* @__PURE__ */ __name((objOrMaybeJSON) => { if (!objOrMaybeJSON) return {}; if (typeof objOrMaybeJSON === "string") { try { const cleanMarkdown = helpJsonMarkup(objOrMaybeJSON); const result = JSON.parse(cleanMarkdown); if (typeof result === "object" && result !== null) { return result; } } catch (error) { } } if (typeof objOrMaybeJSON === "object" && objOrMaybeJSON !== null) { return objOrMaybeJSON; } return {}; }, "maybeParseJSON"); function isObjectStringified(maybeObject) { if (typeof maybeObject !== "string") return false; const isMaybeObject = maybeObject.substring(0, 1) === "{" && maybeObject.substring(maybeObject.length - 1, maybeObject.length) === "}"; const isMaybeArray = maybeObject.substring(0, 1) === "[" && maybeObject.substring(maybeObject.length - 1, maybeObject.length) === "]"; if (!isMaybeObject && !isMaybeArray) { return false; } let canDecode = false; try { JSON.parse(maybeObject); canDecode = true; } catch (error) { canDecode = false; } return canDecode; } __name(isObjectStringified, "isObjectStringified"); function helpJsonMarkup(str) { if (typeof str !== "string") { return str; } const input = str.trim(); const markdownJsonStartsWith = "```json"; const markdownJsonEndsWith = "```"; if (input.substring(0, markdownJsonStartsWith.length) === markdownJsonStartsWith && input.substring(input.length - markdownJsonEndsWith.length, input.length) === markdownJsonEndsWith) { return str.substring(markdownJsonStartsWith.length, input.length - markdownJsonEndsWith.length)?.trim(); } return str; } __name(helpJsonMarkup, "helpJsonMarkup"); // src/utils/modules/replaceTemplateStringSimple.ts function replaceTemplateStringSimple(template, context) { return template.replace(/{{\s*([\w.]+)\s*}}/g, (_match, key) => { const keys = key.split("."); let value = context; for (const k of keys) { if (value && typeof value === "object" && k in value) { value = value[k]; } else { return ""; } } return typeof value === "string" ? value : String(value); }); } __name(replaceTemplateStringSimple, "replaceTemplateStringSimple"); // src/utils/modules/handlebars/helpers/indentJson.ts function indentJson(arg1, collapse = "false") { if (typeof arg1 !== "object") { return replaceTemplateStringSimple(arg1 || "", this); } const replaced = maybeParseJSON(replaceTemplateStringSimple(maybeStringifyJSON(arg1), this)); if (collapse == "true") { return JSON.stringify(replaced); } return JSON.stringify(replaced, null, 2); } __name(indentJson, "indentJson"); // src/utils/modules/schemaExampleWith.ts function schemaExampleWith(schema, property) { if (schema.type === "array") { return filterObjectOnSchema(schema, [ {} ], void 0, property); } return filterObjectOnSchema(schema, {}, void 0, property); } __name(schemaExampleWith, "schemaExampleWith"); // src/utils/modules/handlebars/helpers/jsonSchemaExample.ts function jsonSchemaExample(key, prop, collapse) { const schema = get(this, key); if (schema && schema.type) { const result = schemaExampleWith(schema, prop); if (typeof result !== "object") { return ""; } const replaced = maybeParseJSON(replaceTemplateStringSimple(maybeStringifyJSON(result), this)); if (collapse == "true") { return JSON.stringify(replaced); } return JSON.stringify(replaced, null, 2); } return ""; } __name(jsonSchemaExample, "jsonSchemaExample"); // src/utils/modules/handlebars/helpers/getKeyOr.ts function getKeyOr(key, arg2) { const res = get(this, key); return typeof res !== "undefined" && res !== "" ? res : arg2; } __name(getKeyOr, "getKeyOr"); // src/utils/modules/handlebars/helpers/getOr.ts function getOr(arg1, arg2) { return typeof arg1 !== "undefined" && arg1 !== "" ? arg1 : arg2; } __name(getOr, "getOr"); // src/utils/modules/handlebars/helpers/pluralize.ts function pluralize(arg1, arg2) { const [singular, plural] = arg1.split("|"); return arg2 > 1 ? plural : singular; } __name(pluralize, "pluralize"); // src/utils/modules/handlebars/helpers/eq.ts function eq(arg1 = "", arg2 = "", options) { const isArr = arg2.toString().split(",").map((a) => a.trim()); return isArr.includes(arg1) ? options.fn(this) : options.inverse(this); } __name(eq, "eq"); // src/utils/modules/handlebars/helpers/neq.ts function neq(arg1 = "", arg2 = "", options) { const isArr = arg2.toString().split(",").map((a) => a.trim()); return !isArr.includes(arg1) ? options.fn(this) : options.inverse(this); } __name(neq, "neq"); // src/utils/modules/handlebars/helpers/ifCond.ts function ifCond(v1, operator, v2, options) { switch (operator) { case "==": return v1 == v2 ? options.fn(this) : options.inverse(this); case "===": return v1 === v2 ? options.fn(this) : options.inverse(this); case "!=": return v1 != v2 ? options.fn(this) : options.inverse(this); case "!==": return v1 !== v2 ? options.fn(this) : options.inverse(this); case "<": return v1 < v2 ? options.fn(this) : options.inverse(this); case "<=": return v1 <= v2 ? options.fn(this) : options.inverse(this); case ">": return v1 > v2 ? options.fn(this) : options.inverse(this); case ">=": return v1 >= v2 ? options.fn(this) : options.inverse(this); case "&&": return v1 && v2 ? options.fn(this) : options.inverse(this); case "||": return v1 || v2 ? options.fn(this) : options.inverse(this); default: return options.inverse(this); } } __name(ifCond, "ifCond"); // src/utils/modules/handlebars/helpers/objectToList.ts function objectToList(arg = {}) { return Object.keys(arg).map((key) => `- ${key}: ${arg[key]}`).join("\n"); } __name(objectToList, "objectToList"); // src/utils/modules/handlebars/helpers/join.ts function join(array) { if (typeof array === "string") return array; if (!Array.isArray(array)) return ""; return array.join(", "); } __name(join, "join"); // src/utils/modules/handlebars/helpers/cut.ts function cutFn(str, arg) { return str.toString().replace(new RegExp(arg, "g"), ""); } __name(cutFn, "cutFn"); // src/utils/modules/handlebars/helpers/substring.ts function substringFn(str, start, end) { if (start > end) { return ""; } if (str.length > end) { return str.substring(start, end); } else { return str; } } __name(substringFn, "substringFn"); // src/utils/modules/handlebars/helpers/with.ts function withFn(options, context) { return options.fn(context); } __name(withFn, "withFn"); // src/utils/modules/handlebars/utils/makeHandlebarsInstance.ts function makeHandlebarsInstance(hbs2) { const handlebars = hbs2.create(); const helperKeys = Object.keys(helpers_exports); _registerHelpers(helperKeys.map((a) => ({ handler: helpers_exports[a], name: a })), handlebars); handlebars.registerHelper("hbsInTemplate", function(str, substitutions) { const template = handlebars.compile(str); return template(substitutions, { allowedProtoMethods: { substring: true } }); }); const helperPath = getEnvironmentVariable("CUSTOM_PROMPT_TEMPLATE_HELPERS_PATH"); if (helperPath) { const externalHelpers = __require(helperPath); _registerHelpers(importHelpers(externalHelpers), handlebars); } const contextPartialKeys = Object.keys(partials); for (const contextPartialKey of contextPartialKeys) { handlebars.registerPartial(contextPartialKey, partials[contextPartialKey]); } const partialsPath = getEnvironmentVariable("CUSTOM_PROMPT_TEMPLATE_PARTIALS_PATH"); if (typeof process === "object" && partialsPath) { const externalPartials = __require(partialsPath); _registerPartials(importPartials(externalPartials), handlebars); } return handlebars; } __name(makeHandlebarsInstance, "makeHandlebarsInstance"); // src/utils/modules/isPromise.ts function isPromise(value) { if (typeof value === "object" && value !== null && typeof value.then === "function") { return true; } return Object.prototype.toString.call(value) === "[object AsyncFunction]"; } __name(isPromise, "isPromise"); // src/utils/modules/handlebars/utils/appendContextPath.ts function appendContextPath(contextPath, id) { return (contextPath ? `${contextPath}.` : "") + id; } __name(appendContextPath, "appendContextPath"); // src/utils/modules/handlebars/utils/blockParams.ts function blockParams(params, ids) { return { ...params, ...{ path: ids } }; } __name(blockParams, "blockParams"); // src/utils/modules/extend.ts function extend(obj, _source) { for (let i = 1; i < arguments.length; i++) { for (const key in arguments[i]) { if (Object.prototype.hasOwnProperty.call(arguments[i], key)) { obj[key] = arguments[i][key]; } } } return obj; } __name(extend, "extend"); // src/utils/modules/handlebars/utils/createFrame.ts function createFrame(object) { const frame = extend({}, object); frame._parent = object; return frame; } __name(createFrame, "createFrame"); // src/utils/modules/isEmpty.ts function isEmpty(value) { if (!value && value !== 0) { return true; } if (Array.isArray(value) && value.length === 0) { return true; } return false; } __name(isEmpty, "isEmpty"); // src/utils/modules/handlebars/helpers/async/with.ts async function withFnAsync(context, options) { if (arguments.length !== 2) { throw new Error("#with requires exactly one argument"); } if (isPromise(context)) { context = await context; } else if (typeof context === "function") { context = context.call(this); } const { fn } = options; if (!isEmpty(context)) { let { data } = options; if (options.data && options.ids) { data = createFrame(options.data); data.contextPath = appendContextPath(options.data.contextPath, options.ids[0]); } return fn(context, { data, blockParams: blockParams([ context ], [ data && data.contextPath ]) }); } return options.inverse(this); } __name(withFnAsync, "withFnAsync"); // src/utils/modules/handlebars/helpers/async/if.ts async function ifFnAsync(conditional, options) { if (arguments.length !== 2) { throw new Error("#if requires exactly one argument"); } if (isPromise(conditional)) { conditional = await conditional; } else if (typeof conditional === "function") { conditional = conditional.call(this); } if (!options.hash.includeZero && !conditional || isEmpty(conditional)) { return options.inverse(this); } else { return options.fn(this); } } __name(ifFnAsync, "ifFnAsync"); // src/utils/modules/isReadableStream.ts function isReadableStream(obj) { return obj && typeof obj === "object" && typeof obj.pipe === "function" && typeof obj._read === "function"; } __name(isReadableStream, "isReadableStream"); // src/utils/modules/handlebars/helpers/async/each.ts async function eachFnAsync(arg1, options) { if (!options) { throw new Error("Must pass iterator to #each"); } const { fn } = options; const { inverse } = options; let i = 0; let data = {}; let ret = []; let contextPath = ""; if (options.data && options.ids) { contextPath = `${appendContextPath(options.data.contextPath, options.ids[0])}.`; } if (typeof arg1 === "function") { arg1 = arg1.call(this); } if (options.data) { data = createFrame(options.data); } async function execIteration(field, index, last) { if (data) { data.key = field; data.index = index; data.first = index === 0; data.last = !!last; if (contextPath) { data.contextPath = contextPath + field; } } ret.push(await fn(arg1[field], { data, blockParams: blockParams([ arg1[field], field ], [ contextPath + field, null ]) })); } __name(execIteration, "execIteration"); if (isPromise(arg1)) { arg1 = await arg1; } if (arg1 && typeof arg1 === "object") { if (Array.isArray(arg1)) { for (let j = arg1.length; i < j; i++) { if (i in arg1) { await execIteration(i, i, i === arg1.length - 1); } } } else if (global.Symbol && arg1[global.Symbol.iterator]) { const newContext = [], iterator = arg1[global.Symbol.iterator](); for (let it = iterator.next(); !it.done; it = iterator.next()) { newContext.push(it.value); } arg1 = newContext; for (let j = arg1.length; i < j; i++) { await execIteration(i, i, i === arg1.length - 1); } } else if (isReadableStream(arg1)) { const newContext = []; await new Promise((resolve, reject) => { arg1.on("data", (item) => { newContext.push(item); }).on("end", async () => { arg1 = newContext; for (let j = arg1.length; i < j; i++) { await execIteration(i, i, i === arg1.length - 1); } resolve(true); }).once("error", (e) => reject(e)); }); } else { let priorKey; for (const key of Object.keys(arg1)) { if (priorKey !== void 0) { await execIteration(priorKey, i - 1); } priorKey = key; i++; } if (priorKey !== void 0) { await execIteration(priorKey, i - 1, true); } } } if (i === 0) { ret = inverse(this); ret = [ inverse(this) ]; } return ret.join(""); } __name(eachFnAsync, "eachFnAsync"); // src/utils/modules/handlebars/helpers/async/unless.ts async function unlessFnAsync(conditional, options) { if (arguments.length !== 2) { throw new Error("#unless requires exactly one argument"); } return ifFnAsync.call(this, conditional, { fn: options.inverse, inverse: options.fn, hash: options.hash }); } __name(unlessFnAsync, "unlessFnAsync"); // src/utils/modules/handlebars/helpers/async/async-helpers.ts var asyncCoreOverrideHelpers = { if: ifFnAsync, with: withFnAsync, each: eachFnAsync, unless: unlessFnAsync }; // src/utils/modules/handlebars/utils/makeHandlebarsInstanceAsync.ts function makeHandlebarsInstanceAsync(hbs2) { var _a; const handlebars = hbs2.create(); const asyncCompiler = (_a = class extends hbs2.JavaScriptCompiler { constructor() { super(); this.compiler = asyncCompiler; } mergeSource(varDeclarations) { const sources = super.mergeSource(varDeclarations); sources.prepend("return (async () => {"); sources.add(" })()"); return sources; } appendToBuffer(source, location, explicit) { if (!Array.isArray(source)) { source = [ source ]; } source = this.source.wrap(source, location); if (this.environment.isSimple) { return [ "return await ", source, ";" ]; } if (explicit) { return [ "buffer += await ", source, ";" ]; } source.appendToBuffer = true; source.prepend("await "); return source; } }, __name(_a, "asyncCompiler"), _a); handlebars.JavaScriptCompiler = asyncCompiler; const _compile = handlebars.compile; const _template = handlebars.VM.template; const _escapeExpression = handlebars.escapeExpression; function escapeExpression(value) { if (isPromise(value)) { return value.then((v) => _escapeExpression(v)); } return _escapeExpression(value); } __name(escapeExpression, "escapeExpression"); function lookupProperty(containerLookupProperty) { return function(parent, propertyName) { if (isPromise(parent)) { if (typeof parent?.then === "function") { return parent.then((p) => containerLookupProperty(p, propertyName)); } return parent().then((p) => containerLookupProperty(p, propertyName)); } return containerLookupProperty(parent, propertyName); }; } __name(lookupProperty, "lookupProperty"); handlebars.template = function(spec) { spec.main_d = (_prog, _props, container, _depth, data, blockParams2, depths) => async (context) => { container.escapeExpression = escapeExpression; container.lookupProperty = lookupProperty(container.lookupProperty); if (depths.length == 0) { depths = [ data.root ]; } const v = spec.main(container, context, container.helpers, container.partials, data, blockParams2, depths); return v; }; return _template(spec, handlebars); }; handlebars.compile = function(template, options) { const compiled = _compile.apply(handlebars, [ template, { ...options } ]); return function(context, execOptions) { context = context || {}; return compiled.call(handlebars, context, execOptions); }; }; const helperKeys = Object.keys(helpers_exports); _registerHelpers(helperKeys.map((a) => ({ handler: helpers_exports[a], name: a })), handlebars); const asyncHelperKeys = Object.keys(asyncCoreOverrideHelpers); _registerHelpers(asyncHelperKeys.map((a) => ({ handler: asyncCoreOverrideHelpers[a], name: a })), handlebars); const contextPartialKeys = Object.keys(partials); for (const contextPartialKey of contextPartialKeys) { handlebars.registerPartial(contextPartialKey, partials[contextPartialKey]); } const partialsPath = getEnvironmentVariable("CUSTOM_PROMPT_TEMPLATE_PARTIALS_PATH"); if (typeof process === "object" && partialsPath) { const externalPartials = __require(partialsPath); _registerPartials(importPartials(externalPartials), handlebars); } return handlebars; } __name(makeHandlebarsInstanceAsync, "makeHandlebarsInstanceAsync"); // src/utils/modules/handlebars/hbs.ts var _hbsAsync = makeHandlebarsInstanceAsync(Handlebars); var _hbs = makeHandlebarsInstance(Handlebars); // src/utils/modules/handlebars/index.ts var hbs = { handlebars: _hbs, registerHelpers: /* @__PURE__ */ __name((helpers) => { _registerHelpers(helpers, _hbs); }, "registerHelpers"), registerPartials: /* @__PURE__ */ __name((partials2) => { _registerPartials(partials2, _hbs); }, "registerPartials") }; var hbsAsync = { handlebars: _hbsAsync, registerHelpers: /* @__PURE__ */ __name((helpers) => { _registerHelpers(helpers, _hbsAsync); }, "registerHelpers"), registerPartials: /* @__PURE__ */ __name((partials2) => { _registerPartials(partials2, _hbsAsync); }, "registerPartials") }; // src/utils/modules/replaceTemplateString.ts function replaceTemplateString(templateString, substitutions = {}, configuration = { helpers: [], partials: [] }) { if (!templateString) return templateString || ""; const tempHelpers = []; const tempPartials = []; if (Array.isArray(configuration.helpers)) { hbs.registerHelpers(configuration.helpers); tempHelpers.push(...configuration.helpers.map((a) => a.name)); } if (Array.isArray(configuration.partials)) { hbs.registerPartials(configuration.partials); tempPartials.push(...configuration.partials.map((a) => a.name)); } const template = hbs.handlebars.compile(templateString); const res = template(substitutions, { allowedProtoMethods: { substring: true } }); tempHelpers.forEach(function(n) { hbs.handlebars.unregisterHelper(n); }); tempPartials.forEach(function(n) { hbs.handlebars.unregisterPartial(n); }); return res; } __name(replaceTemplateString, "replaceTemplateString"); // src/utils/modules/replaceTemplateStringAsync.ts async function replaceTemplateStringAsync(templateString, substitutions = {}, configuration = { helpers: [], partials: [] }) { if (!templateString) return Promise.resolve(templateString || ""); const tempHelpers = []; const tempPartials = []; if (Array.isArray(configuration.helpers)) { hbsAsync.registerHelpers(configuration.helpers); tempHelpers.push(...configuration.helpers.map((a) => a.name)); } if (Array.isArray(configuration.partials)) { hbsAsync.registerPartials(configuration.partials); tempPartials.push(...configuration.partials.map((a) => a.name)); } const template = hbsAsync.handlebars.compile(templateString); const res = await template(substitutions, { allowedProtoMethods: { substring: true } }); tempHelpers.forEach(function(n) { hbsAsync.handlebars.unregisterHelper(n); }); tempPartials.forEach(function(n) { hbsAsync.handlebars.unregisterPartial(n); }); return res; } __name(replaceTemplateStringAsync, "replaceTemplateStringAsync"); // src/utils/modules/asyncCallWithTimeout.ts var asyncCallWithTimeout = /* @__PURE__ */ __name(async (asyncPromise, timeLimit = 1e4) => { let timeoutHandle; const timeoutPromise = new Promise((_resolve, reject) => { timeoutHandle = setTimeout(() => { return reject(new Error("Unable to perform action. Try again, or use another action.")); }, timeLimit); }); return Promise.race([ asyncPromise, timeoutPromise ]).then((result) => { clearTimeout(timeoutHandle); return result; }); }, "asyncCallWithTimeout"); // src/utils/modules/guessProviderFromModel.ts function isModelKnownOpenAi(payload) { const model = payload.model.toLowerCase(); if (model.startsWith("gpt-") || model.startsWith("chatgpt-")) { return true; } if (model === "o1" || model.startsWith("o1-")) { return true; } if (model === "o3-mini") { return true; } return false; } __name(isModelKnownOpenAi, "isModelKnownOpenAi"); function isModelKnownAnthropic(payload) { const model = payload.model.toLowerCase(); if (model.startsWith("claude-")) { return true; } return false; } __name(isModelKnownAnthropic, "isModelKnownAnthropic"); function isModelKnownXai(payload) { const model = payload.model.toLowerCase(); if (model.startsWith("grok-")) { return true; } return false; } __name(isModelKnownXai, "isModelKnownXai"); function isModelKnownBedrockAnthropic(payload) { const model = payload.model.toLowerCase(); if (model.startsWith("anthropic.claude-")) { return true; } return false; } __name(isModelKnownBedrockAnthropic, "isModelKnownBedrockAnthropic"); function guessProviderFromModel(payload) { switch (true) { case isModelKnownOpenAi(payload): return "openai"; case isModelKnownXai(payload): return "xai"; case isModelKnownBedrockAnthropic(payload): return "bedrock:anthropic"; case isModelKnownAnthropic(payload): return "anthropic"; default: throw new Error("Unsupported model"); } } __name(guessProviderFromModel, "guessProviderFromModel"); // src/utils/modules/index.ts function registerHelpers(helpers) { hbs.registerHelpers(helpers); hbsAsync.registerHelpers(helpers); } __name(registerHelpers, "registerHelpers"); function registerPartials(partials2) { hbs.registerPartials(partials2); hbsAsync.registerPartials(partials2); } __name(registerPartials, "registerPartials"); // src/llm/output/_utils/getResultText.ts function getResultText(content) { if (content.length === 1 && content.every((a) => a.type === "text")) { return content[0]?.text || ""; } return ""; } __name(getResultText, "getResultText"); // src/parser/parsers/OpenAiFunctionParser.ts var _OpenAiFunctionParser = class _OpenAiFunctionParser extends BaseParser { constructor(options) { super("openAiFunction", options, "function_call"); __publicField(this, "parser"); this.parser = options.parser; } parse(text, _options) { const functionUse = text?.find((a) => a.type === "function_use"); if (functionUse && "name" in functionUse && "input" in functionUse) { return { name: functionUse.name, arguments: maybeParseJSON(functionUse.input) }; } return this.parser.parse(getResultText(text)); } }; __name(_OpenAiFunctionParser, "OpenAiFunctionParser"); var OpenAiFunctionParser = _OpenAiFunctionParser; // src/parser/parsers/StringParser.ts var _StringParser = class _StringParser extends BaseParser { constructor(options) { super("string", options); } parse(text, _options) { assert(typeof text === "string", `Invalid input. Expected string. Received ${typeof text}.`); const parsed = text.toString(); return parsed; } }; __name(_StringParser, "StringParser"); var StringParser = _StringParser; // src/parser/parsers/BooleanParser.ts var _BooleanParser = class _BooleanParser extends BaseParser { constructor(options) { super("boolean", options); } parse(text) { assert(typeof text === "string", `Invalid input. Expected string. Received ${typeof text}.`); const clean = text.toLowerCase().trim(); if (clean === "true") { return true; } return false; } }; __name(_BooleanParser, "BooleanParser"); var BooleanParser = _BooleanParser; // src/utils/modules/isFinite.ts function isFinite(value) { return typeof value === "number" && Number.isFinite(value); } __name(isFinite, "isFinite"); // src/parser/parsers/NumberParser.ts var _NumberParser = class _NumberParser extends BaseParser { constructor(options) { super("number", options); } parse(text) { const match = text.match(/\d/g); return match && isFinite(toNumber(match[0])) ? toNumber(match[0]) : -1; } }; __name(_NumberParser, "NumberParser"); var NumberParser = _NumberParser; // src/parser/_utils.ts import { validate as validateSchema } from "jsonschema"; function enforceParserSchema(schema, parsed) { if (!schema || !parsed || typeof parsed !== "object") { return parsed; } return filterObjectOnSchema(schema, parsed); } __name(enforceParserSchema, "enforceParserSchema"); function validateParserSchema(schema, parsed) { if (!schema || !parsed || typeof parsed !== "object") { return null; } const validate = validateSchema(parsed, schema); if (validate.errors.length) { return validate.errors; } return null; } __name(validateParserSchema, "validateParserSchema"); // src/utils/modules/errors.ts var _LlmExeError = class _LlmExeError extends Error { constructor(message, code, context) { super(message ?? ""); __publicField(this, "code"); __publicField(this, "context"); this.name = this.constructor.name; this.code = code ?? "unknown"; this.context = context; if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } } }; __name(_LlmExeError, "LlmExeError"); var LlmExeError = _LlmExeError; // src/parser/parsers/JsonParser.ts var _JsonParser = class _JsonParser extends BaseParserWithJson { constructor(options = {}) { super("json", options); } parse(text, _attributes) { const parsed = maybeParseJSON(helpJsonMarkup(text)); if (this.schema) { const enforce = enforceParserSchema(this.schema, parsed); if (this.validateSchema) { const valid = validateParserSchema(this.schema, enforce); if (valid && valid.length) { throw new LlmExeError(valid[0].message, "parser", { parser: "json", output: parsed, error: valid[0].message }); } } return enforce; } return parsed; } }; __name(_JsonParser, "JsonParser"); var JsonParser = _JsonParser; // src/utils/modules/camelCase.ts function camelCase(input) { if (!input) return input; input = input.replace(/[^a-zA-Z0-9_]+/g, " ").trim(); const words = input.split(/\s+|_/); return words.map((word, index) => { if (index === 0) { return word.toLowerCase(); } else { return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); } }).join(""); } __name(camelCase, "camelCase"); // src/parser/parsers/ListToJsonParser.ts var _ListToJsonParser = class _ListToJsonParser extends BaseParserWithJson { constructor(options = {}) { super("listToJson", options); } parse(text) { const lines = text.split("\n"); const output = {}; lines.forEach((line) => { const [key, value] = line.split(":"); if (value) { output[camelCase(key)] = value.trim(); } }); if (this.schema) { const parsed = enforceParserSchema(this.schema, output); if (this?.validateSchema) { const valid = validateParserSchema(this.schema, parsed); if (valid && valid.length) { throw new LlmExeError(valid[0].message, "parser", { parser: "json", output: parsed, error: valid[0].message }); } } return parsed; } return output; } }; __name(_ListToJsonParser, "ListToJsonParser"); var ListToJsonParser = _ListToJsonParser; // src/parser/parsers/ListToKeyValueParser.ts var _ListToKeyValueParser = class _ListToKeyValueParser extends BaseParser { constructor(options) { super("listToKeyValue", options); } parse(text) { const lines = text.split("\n").map((s) => s.replace("- ", "").replace(/\'/g, "'")); let res = []; for (const line of lines) { const [key, value] = line.split(":"); if (key && value) { res.push({ key: key?.trim(), value: value?.trim() }); } } return res; } }; __name(_ListToKeyValueParser, "ListToKeyValueParser"); var ListToKeyValueParser = _ListToKeyValueParser; // src/parser/parsers/CustomParser.ts var _CustomParser = class _CustomParser extends BaseParser { /** * Creates a new CustomParser instance. * @param {string} name The name of the parser. * @param {any} parserFn The custom parsing function. */ constructor(name, parserFn) { super(name); /** * Cust