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,734 lines (1,656 loc) • 142 kB
JavaScript
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 __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;
}, {});
}
// 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 };
}
}
// src/utils/modules/uuid.ts
import { v4 as uuidv4 } from "uuid";
// src/executor/_metadata.ts
var _state;
var ExecutorExecutionMetadataState = class {
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();
function createMetadataState(items) {
return new ExecutorExecutionMetadataState(items);
}
// src/utils/const.ts
var hookOnComplete = `onComplete`;
var hookOnError = `onError`;
var hookOnSuccess = `onSuccess`;
// src/executor/_base.ts
var BaseExecutor = class {
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]);
/**
* @property maxHooksPerEvent - Maximum number of hooks allowed per event
*/
__publicField(this, "maxHooksPerEvent", 100);
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)) {
if (this.hooks[hookKey].length >= this.maxHooksPerEvent) {
throw new Error(
`Maximum number of hooks (${this.maxHooksPerEvent}) reached for event "${String(hookKey)}". Consider removing unused hooks or increasing the limit.`
);
}
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;
if (this.hooks[eventName] && this.hooks[eventName].length >= this.maxHooksPerEvent) {
throw new Error(
`Maximum number of hooks (${this.maxHooksPerEvent}) reached for event "${String(eventName)}". Consider removing unused hooks or increasing the limit.`
);
}
const onceWrapper = (...args) => {
fn(...args);
this.off(eventName, onceWrapper);
};
if (!this.hooks[eventName]) {
this.hooks[eventName] = [];
}
this.hooks[eventName].push(onceWrapper);
return this;
}
withTraceId(traceId) {
this.traceId = traceId;
return this;
}
getTraceId() {
return this.traceId;
}
/**
* Clear all hooks for a specific event or all events
* Useful for preventing memory leaks in long-running processes
* @param eventName - The event name to clear hooks for
*/
clearHooks(eventName) {
if (eventName) {
if (this.hooks[eventName]) {
this.hooks[eventName] = [];
}
} else {
for (const key of this.allowedHooks) {
if (this.hooks[key]) {
this.hooks[key] = [];
}
}
}
return this;
}
/**
* Get the count of hooks for monitoring memory usage
* @param eventName - Optional event name to get count for
* @returns Hook count for specific event or all events
*/
getHookCount(eventName) {
if (eventName) {
return this.hooks[eventName]?.length || 0;
}
const counts = {};
for (const key of this.allowedHooks) {
counts[key] = this.hooks[key]?.length || 0;
}
return counts;
}
};
// src/utils/modules/inferFunctionName.ts
function inferFunctionName(func, defaultName) {
if (typeof func !== "function") return defaultName;
const name = func.name;
if (typeof name === "string" && name.length > 0) {
return name.startsWith("bound ") ? name.slice(6) : name;
}
const match = /^function\s+([\w$]+)\s*\(/.exec(func.toString());
return match?.[1] ?? defaultName;
}
// src/executor/core.ts
var CoreExecutor = class 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);
}
};
// src/parser/_base.ts
var BaseParser = class {
/**
* 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;
}
}
};
var BaseParserWithJson = class 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;
}
}
};
// 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`);
}
}
}
// src/utils/guards.ts
var guards_exports = {};
__export(guards_exports, {
hasFunctionCall: () => hasFunctionCall,
hasToolCall: () => hasToolCall,
isAssistantMessage: () => isAssistantMessage,
isFunctionCall: () => isFunctionCall,
isOutputResult: () => isOutputResult,
isOutputResultContentText: () => isOutputResultContentText,
isSystemMessage: () => isSystemMessage,
isToolCall: () => isToolCall,
isUserMessage: () => isUserMessage
});
function isOutputResult(obj) {
return !!(obj && typeof obj === "object" && "id" in obj && "stopReason" in obj && "content" in obj && Array.isArray(obj.content));
}
function isOutputResultContentText(obj) {
return !!(obj && typeof obj === "object" && "text" in obj && "type" in obj && obj.type === "text" && typeof obj.text === "string");
}
function isFunctionCall(result) {
return !!(result && typeof result === "object" && "functionId" in result && "type" in result && result.type === "function_use");
}
function isToolCall(result) {
return isFunctionCall(result);
}
function hasFunctionCall(results) {
return !!(results && Array.isArray(results) && results.some(isToolCall));
}
function hasToolCall(results) {
return hasFunctionCall(results);
}
function isUserMessage(message) {
return message.role === "user";
}
function isAssistantMessage(message) {
return message.role === "assistant" || message.role === "model";
}
function isSystemMessage(message) {
return message.role === "system";
}
// src/parser/parsers/StringParser.ts
var StringParser = class extends BaseParser {
constructor(options) {
super("string", options);
}
parse(text, _options) {
if (isOutputResult(text)) {
return text.content?.[0]?.text ?? "";
}
assert(
typeof text === "string",
`Invalid input. Expected string. Received ${typeof text}.`
);
const parsed = text.toString();
return parsed;
}
};
// src/parser/parsers/BooleanParser.ts
var BooleanParser = class 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;
}
};
// src/utils/modules/isFinite.ts
function isFinite(value) {
return typeof value === "number" && Number.isFinite(value);
}
// 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;
}
// src/parser/parsers/NumberParser.ts
var NumberParser = class 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;
}
};
// 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/defineSchema.ts
import { asConst } from "json-schema-to-ts";
function defineSchema(obj) {
obj.additionalProperties = false;
return asConst(obj);
}
// 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;
}
// 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;
}
// 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 = (regexp) => pathString.split(regexp).filter(Boolean).reduce(
(res, key) => res !== null && res !== void 0 ? res[key] : res,
obj
);
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
return result === void 0 || result === obj ? defaultValue : result === null ? defaultValue : result;
}
// src/utils/modules/filterObjectOnSchema.ts
function isObject(obj) {
return obj === Object(obj);
}
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;
}
}
}
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;
}
// src/utils/modules/handlebars/hbs.ts
import Handlebars from "handlebars";
// 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);
}
}
}
}
}
// 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 = (objOrMaybeString) => {
if (!objOrMaybeString || typeof objOrMaybeString !== "object") {
return objOrMaybeString;
}
try {
const result = JSON.stringify(objOrMaybeString);
return result;
} catch (error) {
}
return "";
};
var maybeParseJSON = (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 {};
};
function isObjectStringified(maybeObject) {
if (typeof maybeObject !== "string") return false;
const trimmed = maybeObject.trim();
const isWrapped = trimmed.startsWith("{") && trimmed.endsWith("}") || trimmed.startsWith("[") && trimmed.endsWith("]");
if (!isWrapped) return false;
try {
const parsed = JSON.parse(trimmed);
return typeof parsed === "object" && parsed !== null;
} catch {
return false;
}
}
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;
}
// 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);
});
}
// 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);
}
// 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);
}
// 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 "";
}
// src/utils/modules/handlebars/helpers/getKeyOr.ts
function getKeyOr(key, arg2) {
const res = get(this, key);
return typeof res !== "undefined" && res !== "" ? res : arg2;
}
// src/utils/modules/handlebars/helpers/getOr.ts
function getOr(arg1, arg2) {
return typeof arg1 !== "undefined" && arg1 !== "" ? arg1 : arg2;
}
// src/utils/modules/handlebars/helpers/pluralize.ts
function pluralize(arg1, arg2) {
const [singular, plural] = arg1.split("|");
return arg2 > 1 ? plural : singular;
}
// 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);
}
// 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);
}
// 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);
}
}
// src/utils/modules/handlebars/helpers/objectToList.ts
function objectToList(arg = {}) {
return Object.keys(arg).map((key) => `- ${key}: ${arg[key]}`).join("\n");
}
// src/utils/modules/handlebars/helpers/join.ts
function join(array) {
if (typeof array === "string") return array;
if (!Array.isArray(array)) return "";
return array.join(", ");
}
// src/utils/modules/handlebars/helpers/cut.ts
function cutFn(str, arg) {
return str.toString().replace(new RegExp(arg, "g"), "");
}
// 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;
}
}
// src/utils/modules/handlebars/helpers/with.ts
function withFn(options, context) {
return options.fn(context);
}
// 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 contextPartialKeys = Object.keys(
partials
);
for (const contextPartialKey of contextPartialKeys) {
handlebars.registerPartial(
contextPartialKey,
partials[contextPartialKey]
);
}
return handlebars;
}
// 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]";
}
// src/utils/modules/handlebars/utils/appendContextPath.ts
function appendContextPath(contextPath, id) {
return (contextPath ? `${contextPath}.` : "") + id;
}
// src/utils/modules/handlebars/utils/blockParams.ts
function blockParams(params, ids) {
return { ...params, ...{ path: ids } };
}
// 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;
}
// src/utils/modules/handlebars/utils/createFrame.ts
function createFrame(object) {
const frame = extend({}, object);
frame._parent = object;
return frame;
}
// 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;
}
// 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);
}
// 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);
}
}
// src/utils/modules/isReadableStream.ts
function isReadableStream(obj) {
return obj && typeof obj === "object" && typeof obj.pipe === "function" && typeof obj._read === "function";
}
// 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]
)
})
);
}
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("");
}
// 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
});
}
// 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) {
const handlebars = hbs2.create();
const asyncCompiler = 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;
}
};
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);
}
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);
};
}
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]
);
}
return handlebars;
}
// src/utils/modules/handlebars/hbs.ts
var _hbsAsync = makeHandlebarsInstanceAsync(Handlebars);
var _hbs = makeHandlebarsInstance(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);
}
}
}
}
}
// src/utils/modules/handlebars/index.ts
var hbs = {
handlebars: _hbs,
registerHelpers: (helpers) => {
_registerHelpers(helpers, _hbs);
},
registerPartials: (partials2) => {
_registerPartials(partials2, _hbs);
}
};
var hbsAsync = {
handlebars: _hbsAsync,
registerHelpers: (helpers) => {
_registerHelpers(helpers, _hbsAsync);
},
registerPartials: (partials2) => {
_registerPartials(partials2, _hbsAsync);
}
};
// 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;
}
// 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;
}
// src/utils/modules/asyncCallWithTimeout.ts
var asyncCallWithTimeout = 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;
});
};
// 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;
}
function isModelKnownAnthropic(payload) {
const model = payload.model.toLowerCase();
if (model.startsWith("claude-")) {
return true;
}
return false;
}
function isModelKnownXai(payload) {
const model = payload.model.toLowerCase();
if (model.startsWith("grok-")) {
return true;
}
return false;
}
function isModelKnownBedrockAnthropic(payload) {
const model = payload.model.toLowerCase();
if (model.startsWith("anthropic.claude-")) {
return true;
}
return false;
}
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");
}
}
// src/utils/modules/index.ts
function registerHelpers(helpers) {
hbs.registerHelpers(helpers);
hbsAsync.registerHelpers(helpers);
}
function registerPartials(partials2) {
hbs.registerPartials(partials2);
hbsAsync.registerPartials(partials2);
}
// 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);
}
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;
}
// src/utils/modules/errors.ts
var LlmExeError = class 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);
}
}
};
// src/parser/parsers/JsonParser.ts
var JsonParser = class 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;
}
};
// 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("");
}
// src/parser/parsers/ListToJsonParser.ts
var ListToJsonParser = class 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;
}
};
// src/parser/parsers/ListToKeyValueParser.ts
var ListToKeyValueParser = class 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;
}
};
// src/parser/parsers/CustomParser.ts
var CustomParser = class 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);
/**
* Custom parsing function.
* @type {any}
*/
__publicField(this, "parserFn");
this.parserFn = parserFn;
}
/**
* Parses the text using the custom parsing function.
* @param {string} text The text to be parsed.
* @param {any} inputValues Additional input values for the parser function.
* @returns {O} The parsed value.
*/
parse(text, inputValues) {
return this.parserFn.call(this, text, inputValues);
}
};
// src/parser/parsers/ListToArrayParser.ts
var ListToArrayParser = class extends BaseParser {
constructor() {
super("listToArray");
}
parse(text) {
const lines = text.split("\n").map((s) => s.replace(/^- /, "").replace(/'/g, "'").trim());
return lines;
}
};
// src/parser/parsers/ReplaceStringTemplateParser.ts
var ReplaceStringTemplateParser = class extends BaseParser {
constructor(options) {
super("replaceStringTemplate", options);
}
parse(text, attributes) {
return replaceTemplateString(text, attributes);
}
};
// src/parser/singleKeyObjectToString.ts
function singleKeyObjectToString(input) {
try {
const parsed = JSON.parse(input);
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
const keys = Object.keys(parsed);
if (keys.length === 1) {
const value = parsed[keys[0]];
if (typeof value === "string") {
return value;
} else {
return input;
}
}
}
} catch {
}
return input;
}
// src/parser/parsers/MarkdownCodeBlocks.ts
var MarkdownCodeBlocksParser = class extends BaseParser {
constructor(options) {
super("markdownCodeBlocks", options);
}
parse(input) {
const out = [];
if (isObjectStringified(input)) {
input = singleKeyObjectToString(input);
}
const regex = input.matchAll(new RegExp(/```(\w*)\n([\s\S]*?)```/, "g"));
for (const iterator of regex) {
if (iterator) {
const [_input, language, code] = iterator;
out.push({
language,
code
});
}
}
return out;
}
};
// src/parser/parsers/MarkdownCodeBlock.ts
var MarkdownCodeBlockParser = class extends BaseParser {
constructor(options) {
super("markdownCodeBlock", options);
}
parse(input) {
const [block] = new MarkdownCodeBlocksParser().parse(input);
if (!block) {
return {
code: "",
language: ""
};
}
return block;
}
};
// src/parser/parsers/StringExtractParser.ts
var StringExtractParser = class extends BaseParser {
constructor(options) {
super("stringExtract", options);
__publicField(this, "enum", []);
__publicField(this, "ignoreCase");
if (options?.enum) {
this.enum.push(...options.enum);
}
if (options?.ignoreCase) {
this.ignoreCase = true;
}
}
parse(text) {
assert(
typeof text === "string",
`Invalid input. Expected string. Received ${typeof text}.`
);
for (const option of this.enum) {
const regex = this.ignoreCase ? new RegExp(option.toLowerCase(), "i") : new RegExp(option);
if (regex.test(text)) {
return option;
}
}
return "";
}
};
// src/parser/_functions.ts
function createParser(type, options = {}) {
switch (type) {
case "json":
return new JsonParser(options);
case "markdownCodeBlocks":
return new MarkdownCodeBlocksParser();
case "markdownCodeBlo