llm-exe 2.0.0-beta.9 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -450,64 +450,17 @@ function importHelpers(_helpers) {
450
450
  }
451
451
  __name(importHelpers, "importHelpers");
452
452
 
453
- // src/utils/modules/handlebars/hbs.ts
454
- import Handlebars from "handlebars";
455
-
456
- // src/utils/modules/handlebars/helpers/index.ts
457
- var helpers_exports = {};
458
- __export(helpers_exports, {
459
- eq: () => eq,
460
- getKeyOr: () => getKeyOr,
461
- getOr: () => getOr,
462
- hbsInTemplate: () => hbsInTemplate,
463
- ifCond: () => ifCond,
464
- indentJson: () => indentJson,
465
- join: () => join,
466
- jsonSchemaExample: () => jsonSchemaExample,
467
- neq: () => neq,
468
- objectToList: () => objectToList,
469
- pluralize: () => pluralize
470
- });
471
-
472
- // src/utils/modules/replaceTemplateString.ts
473
- function replaceTemplateString(templateString, substitutions = {}, configuration = {
474
- helpers: [],
475
- partials: []
476
- }) {
477
- if (!templateString) return templateString || "";
478
- const tempHelpers = [];
479
- const tempPartials = [];
480
- if (Array.isArray(configuration.helpers)) {
481
- registerHelpers(configuration.helpers, hbs);
482
- tempHelpers.push(...configuration.helpers.map((a) => a.name));
453
+ // src/utils/modules/toNumber.ts
454
+ function toNumber(value) {
455
+ if (typeof value === "number") {
456
+ return value;
483
457
  }
484
- if (Array.isArray(configuration.partials)) {
485
- registerPartials(configuration.partials, hbs);
486
- tempPartials.push(...configuration.partials.map((a) => a.name));
458
+ if (typeof value === "string" && value.trim() !== "") {
459
+ return Number(value);
487
460
  }
488
- const template = hbs.compile(templateString);
489
- const res = template(substitutions, {
490
- allowedProtoMethods: {
491
- substring: true
492
- }
493
- });
494
- tempHelpers.forEach(function(n) {
495
- hbs.unregisterHelper(n);
496
- });
497
- tempPartials.forEach(function(n) {
498
- hbs.unregisterPartial(n);
499
- });
500
- return res;
501
- }
502
- __name(replaceTemplateString, "replaceTemplateString");
503
-
504
- // src/utils/modules/handlebars/helpers/hbsInTemplate.ts
505
- function hbsInTemplate(arg1) {
506
- const data = this;
507
- const replace = replaceTemplateString(arg1, data);
508
- return replace;
461
+ return NaN;
509
462
  }
510
- __name(hbsInTemplate, "hbsInTemplate");
463
+ __name(toNumber, "toNumber");
511
464
 
512
465
  // src/utils/modules/get.ts
513
466
  function get(obj, path, defaultValue) {
@@ -521,44 +474,6 @@ function get(obj, path, defaultValue) {
521
474
  }
522
475
  __name(get, "get");
523
476
 
524
- // src/utils/modules/handlebars/helpers/getKeyOr.ts
525
- function getKeyOr(key, arg2) {
526
- const res = get(this, key);
527
- return typeof res !== "undefined" && res !== "" ? res : arg2;
528
- }
529
- __name(getKeyOr, "getKeyOr");
530
-
531
- // src/utils/modules/handlebars/helpers/getOr.ts
532
- function getOr(arg1, arg2) {
533
- return typeof arg1 !== "undefined" && arg1 !== "" ? arg1 : arg2;
534
- }
535
- __name(getOr, "getOr");
536
-
537
- // src/utils/modules/handlebars/helpers/indentJson.ts
538
- function indentJson(arg1, collapse = "false") {
539
- if (typeof arg1 !== "object") {
540
- return replaceTemplateString(arg1 || "", this);
541
- }
542
- const replaced = maybeParseJSON(replaceTemplateString(maybeStringifyJSON(arg1), this));
543
- if (collapse == "true") {
544
- return JSON.stringify(replaced);
545
- }
546
- return JSON.stringify(replaced, null, 2);
547
- }
548
- __name(indentJson, "indentJson");
549
-
550
- // src/utils/modules/toNumber.ts
551
- function toNumber(value) {
552
- if (typeof value === "number") {
553
- return value;
554
- }
555
- if (typeof value === "string" && value.trim() !== "") {
556
- return Number(value);
557
- }
558
- return NaN;
559
- }
560
- __name(toNumber, "toNumber");
561
-
562
477
  // src/utils/modules/filterObjectOnSchema.ts
563
478
  function isObject(obj) {
564
479
  return obj === Object(obj);
@@ -623,6 +538,208 @@ function filterObjectOnSchema(schema, doc, detach, property) {
623
538
  }
624
539
  __name(filterObjectOnSchema, "filterObjectOnSchema");
625
540
 
541
+ // src/utils/modules/handlebars/hbs.ts
542
+ import Handlebars from "handlebars";
543
+
544
+ // src/utils/modules/handlebars/utils/registerPartials.ts
545
+ function _registerPartials(partials2, instance) {
546
+ if (partials2 && Array.isArray(partials2)) {
547
+ for (const partial of partials2) {
548
+ if (partial.name && typeof partial.name === "string" && typeof partial.template === "string") {
549
+ if (instance) {
550
+ instance.registerPartial(partial.name, partial.template);
551
+ }
552
+ }
553
+ }
554
+ }
555
+ }
556
+ __name(_registerPartials, "_registerPartials");
557
+
558
+ // src/utils/modules/handlebars/utils/registerHelpers.ts
559
+ function _registerHelpers(helpers, instance) {
560
+ if (helpers && Array.isArray(helpers)) {
561
+ for (const helper of helpers) {
562
+ if (helper.name && typeof helper.name === "string" && typeof helper.handler === "function") {
563
+ if (instance) {
564
+ instance.registerHelper(helper.name, helper.handler);
565
+ }
566
+ }
567
+ }
568
+ }
569
+ }
570
+ __name(_registerHelpers, "_registerHelpers");
571
+
572
+ // src/utils/modules/getEnvironmentVariable.ts
573
+ function getEnvironmentVariable(name) {
574
+ if (typeof process === "object" && process?.env) {
575
+ return process.env[name];
576
+ } else {
577
+ return void 0;
578
+ }
579
+ }
580
+ __name(getEnvironmentVariable, "getEnvironmentVariable");
581
+
582
+ // src/utils/modules/handlebars/templates/index.ts
583
+ var MarkdownCode = `{{#if code}}\`\`\`{{#if language}}{{language}}{{/if}}
584
+ {{{code}}}
585
+ \`\`\`{{/if}}`;
586
+ var ThoughtActionResult = `
587
+ {{#if title}}{{#if attributes.stepsTaken.length}}{{title}}
588
+ {{/if}}{{/if}}
589
+ {{#each attributes.stepsTaken as | step |}}
590
+ {{#if step.thought}}Thought: {{step.result}}{{/if}}
591
+ {{#if step.result}}Action: {{step.action}}{{/if}}
592
+ {{#if step.result}}Result: {{step.result}}{{/if}}
593
+ {{/each}}`;
594
+ var ChatConversationHistory = `{{~#if title}}{{~#if chat_history.length}}{{title}}
595
+ {{~/if}}{{~/if}}
596
+ {{#each chat_history as | item |}}
597
+ {{~#eq item.role 'user'}}{{#if @last}}{{../mostRecentRolePrefix}}{{../userName}}{{../mostRecentRoleSuffix}}{{else}}{{../userName}}{{/if}}: {{#if @last}}{{../mostRecentMessagePrefix}}{{/if}}{{{item.content}}}{{#if @last}}{{../mostRecentMessageSuffix}}{{/if}}
598
+ {{/eq}}
599
+ {{~#eq item.role 'assistant'}}{{#if @last}}{{../mostRecentRolePrefix}}{{../assistantName}}{{../mostRecentRoleSuffix}}{{else}}{{../assistantName}}{{/if}}: {{#if @last}}{{../mostRecentMessagePrefix}}{{/if}}{{{item.content}}}{{#if @last}}{{../mostRecentMessageSuffix}}{{/if}}
600
+ {{/eq}}
601
+ {{~#eq item.role 'system'}}{{../systemName}}: {{{item.content}}}
602
+ {{/eq}}
603
+ {{~/each}}`;
604
+ 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}}`;
605
+ var SingleChatMessage = `{{~#eq role 'user'}}{{getOr name 'User'}}: {{{content}}}{{~/eq}}
606
+ {{~#eq role 'assistant'}}{{getOr assistant 'Assistant'}}: {{{content}}}{{~/eq}}
607
+ {{~#eq role 'system'}}{{getOr system 'System'}}: {{{content}}}{{~/eq}}`;
608
+ var ThoughtsAndObservations = `{{~#each thoughts as | step |}}
609
+ {{~#if step.thought}}Thought: {{{step.thought}}}
610
+ {{/if}}
611
+ {{~#if step.observation}}Observation: {{{step.observation}}}
612
+ {{/if}}
613
+ {{~/each}}`;
614
+ var JsonSchema = `{{#if (getKeyOr key false)}}
615
+ \`\`\`json
616
+ {{{indentJson (getKeyOr key) collapse}}}
617
+ \`\`\`
618
+ {{~/if}}`;
619
+ var JsonSchemaExampleJson = `{{#if (getOr key false)}}
620
+ \`\`\`json
621
+ {{{jsonSchemaExample key (getOr property '') collapse}}}
622
+ \`\`\`
623
+ {{~/if}}`;
624
+ var partials = {
625
+ JsonSchema,
626
+ JsonSchemaExampleJson,
627
+ MarkdownCode,
628
+ DialogueHistory,
629
+ SingleChatMessage,
630
+ ChatConversationHistory,
631
+ ThoughtsAndObservations,
632
+ ThoughtActionResult
633
+ };
634
+
635
+ // src/utils/modules/handlebars/helpers/index.ts
636
+ var helpers_exports = {};
637
+ __export(helpers_exports, {
638
+ cut: () => cutFn,
639
+ eq: () => eq,
640
+ getKeyOr: () => getKeyOr,
641
+ getOr: () => getOr,
642
+ ifCond: () => ifCond,
643
+ indentJson: () => indentJson,
644
+ join: () => join,
645
+ jsonSchemaExample: () => jsonSchemaExample,
646
+ neq: () => neq,
647
+ objectToList: () => objectToList,
648
+ pluralize: () => pluralize,
649
+ substring: () => substringFn,
650
+ with: () => withFn
651
+ });
652
+
653
+ // src/utils/modules/json.ts
654
+ var maybeStringifyJSON = /* @__PURE__ */ __name((objOrMaybeString) => {
655
+ if (!objOrMaybeString || typeof objOrMaybeString !== "object") {
656
+ return objOrMaybeString;
657
+ }
658
+ try {
659
+ const result = JSON.stringify(objOrMaybeString);
660
+ return result;
661
+ } catch (error) {
662
+ }
663
+ return "";
664
+ }, "maybeStringifyJSON");
665
+ var maybeParseJSON = /* @__PURE__ */ __name((objOrMaybeJSON) => {
666
+ if (!objOrMaybeJSON) return {};
667
+ if (typeof objOrMaybeJSON === "string") {
668
+ try {
669
+ const cleanMarkdown = helpJsonMarkup(objOrMaybeJSON);
670
+ const result = JSON.parse(cleanMarkdown);
671
+ if (typeof result === "object" && result !== null) {
672
+ return result;
673
+ }
674
+ } catch (error) {
675
+ }
676
+ }
677
+ if (typeof objOrMaybeJSON === "object" && objOrMaybeJSON !== null) {
678
+ return objOrMaybeJSON;
679
+ }
680
+ return {};
681
+ }, "maybeParseJSON");
682
+ function isObjectStringified(maybeObject) {
683
+ if (typeof maybeObject !== "string") return false;
684
+ const isMaybeObject = maybeObject.substring(0, 1) === "{" && maybeObject.substring(maybeObject.length - 1, maybeObject.length) === "}";
685
+ const isMaybeArray = maybeObject.substring(0, 1) === "[" && maybeObject.substring(maybeObject.length - 1, maybeObject.length) === "]";
686
+ if (!isMaybeObject && !isMaybeArray) {
687
+ return false;
688
+ }
689
+ let canDecode = false;
690
+ try {
691
+ JSON.parse(maybeObject);
692
+ canDecode = true;
693
+ } catch (error) {
694
+ canDecode = false;
695
+ }
696
+ return canDecode;
697
+ }
698
+ __name(isObjectStringified, "isObjectStringified");
699
+ function helpJsonMarkup(str) {
700
+ if (typeof str !== "string") {
701
+ return str;
702
+ }
703
+ const input = str.trim();
704
+ const markdownJsonStartsWith = "```json";
705
+ const markdownJsonEndsWith = "```";
706
+ if (input.substring(0, markdownJsonStartsWith.length) === markdownJsonStartsWith && input.substring(input.length - markdownJsonEndsWith.length, input.length) === markdownJsonEndsWith) {
707
+ return str.substring(markdownJsonStartsWith.length, input.length - markdownJsonEndsWith.length)?.trim();
708
+ }
709
+ return str;
710
+ }
711
+ __name(helpJsonMarkup, "helpJsonMarkup");
712
+
713
+ // src/utils/modules/replaceTemplateStringSimple.ts
714
+ function replaceTemplateStringSimple(template, context) {
715
+ return template.replace(/{{\s*([\w.]+)\s*}}/g, (_match, key) => {
716
+ const keys = key.split(".");
717
+ let value = context;
718
+ for (const k of keys) {
719
+ if (value && typeof value === "object" && k in value) {
720
+ value = value[k];
721
+ } else {
722
+ return "";
723
+ }
724
+ }
725
+ return typeof value === "string" ? value : String(value);
726
+ });
727
+ }
728
+ __name(replaceTemplateStringSimple, "replaceTemplateStringSimple");
729
+
730
+ // src/utils/modules/handlebars/helpers/indentJson.ts
731
+ function indentJson(arg1, collapse = "false") {
732
+ if (typeof arg1 !== "object") {
733
+ return replaceTemplateStringSimple(arg1 || "", this);
734
+ }
735
+ const replaced = maybeParseJSON(replaceTemplateStringSimple(maybeStringifyJSON(arg1), this));
736
+ if (collapse == "true") {
737
+ return JSON.stringify(replaced);
738
+ }
739
+ return JSON.stringify(replaced, null, 2);
740
+ }
741
+ __name(indentJson, "indentJson");
742
+
626
743
  // src/utils/modules/schemaExampleWith.ts
627
744
  function schemaExampleWith(schema, property) {
628
745
  if (schema.type === "array") {
@@ -642,7 +759,7 @@ function jsonSchemaExample(key, prop, collapse) {
642
759
  if (typeof result !== "object") {
643
760
  return "";
644
761
  }
645
- const replaced = maybeParseJSON(replaceTemplateString(maybeStringifyJSON(result), this));
762
+ const replaced = maybeParseJSON(replaceTemplateStringSimple(maybeStringifyJSON(result), this));
646
763
  if (collapse == "true") {
647
764
  return JSON.stringify(replaced);
648
765
  }
@@ -652,6 +769,19 @@ function jsonSchemaExample(key, prop, collapse) {
652
769
  }
653
770
  __name(jsonSchemaExample, "jsonSchemaExample");
654
771
 
772
+ // src/utils/modules/handlebars/helpers/getKeyOr.ts
773
+ function getKeyOr(key, arg2) {
774
+ const res = get(this, key);
775
+ return typeof res !== "undefined" && res !== "" ? res : arg2;
776
+ }
777
+ __name(getKeyOr, "getKeyOr");
778
+
779
+ // src/utils/modules/handlebars/helpers/getOr.ts
780
+ function getOr(arg1, arg2) {
781
+ return typeof arg1 !== "undefined" && arg1 !== "" ? arg1 : arg2;
782
+ }
783
+ __name(getOr, "getOr");
784
+
655
785
  // src/utils/modules/handlebars/helpers/pluralize.ts
656
786
  function pluralize(arg1, arg2) {
657
787
  const [singular, plural] = arg1.split("|");
@@ -714,60 +844,75 @@ function join(array) {
714
844
  if (!Array.isArray(array)) return "";
715
845
  return array.join(", ");
716
846
  }
717
- __name(join, "join");
718
-
719
- // src/utils/modules/handlebars/templates/index.ts
720
- var MarkdownCode = `{{#if code}}\`\`\`{{#if language}}{{language}}{{/if}}
721
- {{{code}}}
722
- \`\`\`{{/if}}`;
723
- var ThoughtActionResult = `
724
- {{#if title}}{{#if attributes.stepsTaken.length}}{{title}}
725
- {{/if}}{{/if}}
726
- {{#each attributes.stepsTaken as | step |}}
727
- {{#if step.thought}}Thought: {{step.result}}{{/if}}
728
- {{#if step.result}}Action: {{step.action}}{{/if}}
729
- {{#if step.result}}Result: {{step.result}}{{/if}}
730
- {{/each}}`;
731
- var ChatConversationHistory = `{{~#if title}}{{~#if chat_history.length}}{{title}}
732
- {{~/if}}{{~/if}}
733
- {{#each chat_history as | item |}}
734
- {{~#eq item.role 'user'}}{{#if @last}}{{../mostRecentRolePrefix}}{{../userName}}{{../mostRecentRoleSuffix}}{{else}}{{../userName}}{{/if}}: {{#if @last}}{{../mostRecentMessagePrefix}}{{/if}}{{{item.content}}}{{#if @last}}{{../mostRecentMessageSuffix}}{{/if}}
735
- {{/eq}}
736
- {{~#eq item.role 'assistant'}}{{#if @last}}{{../mostRecentRolePrefix}}{{../assistantName}}{{../mostRecentRoleSuffix}}{{else}}{{../assistantName}}{{/if}}: {{#if @last}}{{../mostRecentMessagePrefix}}{{/if}}{{{item.content}}}{{#if @last}}{{../mostRecentMessageSuffix}}{{/if}}
737
- {{/eq}}
738
- {{~#eq item.role 'system'}}{{../systemName}}: {{{item.content}}}
739
- {{/eq}}
740
- {{~/each}}`;
741
- 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}}`;
742
- var SingleChatMessage = `{{~#eq role 'user'}}{{getOr name 'User'}}: {{{content}}}{{~/eq}}
743
- {{~#eq role 'assistant'}}{{getOr assistant 'Assistant'}}: {{{content}}}{{~/eq}}
744
- {{~#eq role 'system'}}{{getOr system 'System'}}: {{{content}}}{{~/eq}}`;
745
- var ThoughtsAndObservations = `{{~#each thoughts as | step |}}
746
- {{~#if step.thought}}Thought: {{{step.thought}}}
747
- {{/if}}
748
- {{~#if step.observation}}Observation: {{{step.observation}}}
749
- {{/if}}
750
- {{~/each}}`;
751
- var JsonSchema = `{{#if (getKeyOr key false)}}
752
- \`\`\`json
753
- {{{indentJson (getKeyOr key) collapse}}}
754
- \`\`\`
755
- {{~/if}}`;
756
- var JsonSchemaExampleJson = `{{#if (getOr key false)}}
757
- \`\`\`json
758
- {{{jsonSchemaExample key (getOr property '') collapse}}}
759
- \`\`\`
760
- {{~/if}}`;
761
- var partials = {
762
- JsonSchema,
763
- JsonSchemaExampleJson,
764
- MarkdownCode,
765
- DialogueHistory,
766
- SingleChatMessage,
767
- ChatConversationHistory,
768
- ThoughtsAndObservations,
769
- ThoughtActionResult
770
- };
847
+ __name(join, "join");
848
+
849
+ // src/utils/modules/handlebars/helpers/cut.ts
850
+ function cutFn(str, arg) {
851
+ return str.toString().replace(new RegExp(arg, "g"), "");
852
+ }
853
+ __name(cutFn, "cutFn");
854
+
855
+ // src/utils/modules/handlebars/helpers/substring.ts
856
+ function substringFn(str, start, end) {
857
+ if (start > end) {
858
+ return "";
859
+ }
860
+ if (str.length > end) {
861
+ return str.substring(start, end);
862
+ } else {
863
+ return str;
864
+ }
865
+ }
866
+ __name(substringFn, "substringFn");
867
+
868
+ // src/utils/modules/handlebars/helpers/with.ts
869
+ function withFn(options, context) {
870
+ return options.fn(context);
871
+ }
872
+ __name(withFn, "withFn");
873
+
874
+ // src/utils/modules/handlebars/utils/makeHandlebarsInstance.ts
875
+ function makeHandlebarsInstance(hbs2) {
876
+ const handlebars = hbs2.create();
877
+ const helperKeys = Object.keys(helpers_exports);
878
+ _registerHelpers(helperKeys.map((a) => ({
879
+ handler: helpers_exports[a],
880
+ name: a
881
+ })), handlebars);
882
+ handlebars.registerHelper("hbsInTemplate", function(str, substitutions) {
883
+ const template = handlebars.compile(str);
884
+ return template(substitutions, {
885
+ allowedProtoMethods: {
886
+ substring: true
887
+ }
888
+ });
889
+ });
890
+ const helperPath = getEnvironmentVariable("CUSTOM_PROMPT_TEMPLATE_HELPERS_PATH");
891
+ if (helperPath) {
892
+ const externalHelpers = __require(helperPath);
893
+ _registerHelpers(importHelpers(externalHelpers), handlebars);
894
+ }
895
+ const contextPartialKeys = Object.keys(partials);
896
+ for (const contextPartialKey of contextPartialKeys) {
897
+ handlebars.registerPartial(contextPartialKey, partials[contextPartialKey]);
898
+ }
899
+ const partialsPath = getEnvironmentVariable("CUSTOM_PROMPT_TEMPLATE_PARTIALS_PATH");
900
+ if (typeof process === "object" && partialsPath) {
901
+ const externalPartials = __require(partialsPath);
902
+ _registerPartials(importPartials(externalPartials), handlebars);
903
+ }
904
+ return handlebars;
905
+ }
906
+ __name(makeHandlebarsInstance, "makeHandlebarsInstance");
907
+
908
+ // src/utils/modules/isPromise.ts
909
+ function isPromise(value) {
910
+ if (typeof value === "object" && value !== null && typeof value.then === "function") {
911
+ return true;
912
+ }
913
+ return Object.prototype.toString.call(value) === "[object AsyncFunction]";
914
+ }
915
+ __name(isPromise, "isPromise");
771
916
 
772
917
  // src/utils/modules/handlebars/utils/appendContextPath.ts
773
918
  function appendContextPath(contextPath, id) {
@@ -819,15 +964,6 @@ function isEmpty(value) {
819
964
  }
820
965
  __name(isEmpty, "isEmpty");
821
966
 
822
- // src/utils/modules/isPromise.ts
823
- function isPromise(value) {
824
- if (typeof value === "object" && value !== null && typeof value.then === "function") {
825
- return true;
826
- }
827
- return Object.prototype.toString.call(value) === "[object AsyncFunction]";
828
- }
829
- __name(isPromise, "isPromise");
830
-
831
967
  // src/utils/modules/handlebars/helpers/async/with.ts
832
968
  async function withFnAsync(context, options) {
833
969
  if (arguments.length !== 2) {
@@ -1001,72 +1137,6 @@ var asyncCoreOverrideHelpers = {
1001
1137
  unless: unlessFnAsync
1002
1138
  };
1003
1139
 
1004
- // src/utils/modules/getEnvironmentVariable.ts
1005
- function getEnvironmentVariable(name) {
1006
- if (typeof process === "object" && process?.env) {
1007
- return process.env[name];
1008
- } else {
1009
- return void 0;
1010
- }
1011
- }
1012
- __name(getEnvironmentVariable, "getEnvironmentVariable");
1013
-
1014
- // src/utils/modules/handlebars/useHandlebars.ts
1015
- function useHandlebars(hbsInstance, preferAsync = false) {
1016
- hbsInstance.registerHelper("with", function(context, options) {
1017
- return options.fn(context);
1018
- });
1019
- hbsInstance.registerHelper("cut", function(str, arg2) {
1020
- return str.toString().replace(new RegExp(arg2, "g"), "");
1021
- });
1022
- hbsInstance.registerHelper("substring", function(str, start, end) {
1023
- if (str.length > end) {
1024
- return str.substring(start, end);
1025
- } else {
1026
- return str;
1027
- }
1028
- });
1029
- hbsInstance.registerHelper("unless", function(conditional, options) {
1030
- if (arguments.length !== 2) {
1031
- throw new Error("#unless requires exactly one argument");
1032
- }
1033
- const ifFn = hbsInstance.helpers["if"];
1034
- return ifFn(conditional, {
1035
- fn: options.inverse,
1036
- inverse: options.fn,
1037
- hash: options.hash
1038
- });
1039
- });
1040
- const helperKeys = Object.keys(helpers_exports);
1041
- registerHelpers(helperKeys.map((a) => ({
1042
- handler: helpers_exports[a],
1043
- name: a
1044
- })), hbsInstance);
1045
- if (preferAsync) {
1046
- const asyncHelperKeys = Object.keys(asyncCoreOverrideHelpers);
1047
- registerHelpers(asyncHelperKeys.map((a) => ({
1048
- handler: asyncCoreOverrideHelpers[a],
1049
- name: a
1050
- })), hbsInstance);
1051
- }
1052
- const helperPath = getEnvironmentVariable("CUSTOM_PROMPT_TEMPLATE_HELPERS_PATH");
1053
- if (helperPath) {
1054
- const externalHelpers = __require(helperPath);
1055
- registerHelpers(importHelpers(externalHelpers), hbsInstance);
1056
- }
1057
- const contextPartialKeys = Object.keys(partials);
1058
- for (const contextPartialKey of contextPartialKeys) {
1059
- hbsInstance.registerPartial(contextPartialKey, partials[contextPartialKey]);
1060
- }
1061
- const partialsPath = getEnvironmentVariable("CUSTOM_PROMPT_TEMPLATE_PARTIALS_PATH");
1062
- if (typeof process === "object" && partialsPath) {
1063
- const externalPartials = __require(partialsPath);
1064
- registerPartials(importPartials(externalPartials), hbsInstance);
1065
- }
1066
- return hbsInstance;
1067
- }
1068
- __name(useHandlebars, "useHandlebars");
1069
-
1070
1140
  // src/utils/modules/handlebars/utils/makeHandlebarsInstanceAsync.ts
1071
1141
  function makeHandlebarsInstanceAsync(hbs2) {
1072
1142
  var _a;
@@ -1157,45 +1227,84 @@ function makeHandlebarsInstanceAsync(hbs2) {
1157
1227
  return compiled.call(handlebars, context, execOptions);
1158
1228
  };
1159
1229
  };
1230
+ const helperKeys = Object.keys(helpers_exports);
1231
+ _registerHelpers(helperKeys.map((a) => ({
1232
+ handler: helpers_exports[a],
1233
+ name: a
1234
+ })), handlebars);
1235
+ const asyncHelperKeys = Object.keys(asyncCoreOverrideHelpers);
1236
+ _registerHelpers(asyncHelperKeys.map((a) => ({
1237
+ handler: asyncCoreOverrideHelpers[a],
1238
+ name: a
1239
+ })), handlebars);
1240
+ const contextPartialKeys = Object.keys(partials);
1241
+ for (const contextPartialKey of contextPartialKeys) {
1242
+ handlebars.registerPartial(contextPartialKey, partials[contextPartialKey]);
1243
+ }
1244
+ const partialsPath = getEnvironmentVariable("CUSTOM_PROMPT_TEMPLATE_PARTIALS_PATH");
1245
+ if (typeof process === "object" && partialsPath) {
1246
+ const externalPartials = __require(partialsPath);
1247
+ _registerPartials(importPartials(externalPartials), handlebars);
1248
+ }
1160
1249
  return handlebars;
1161
1250
  }
1162
1251
  __name(makeHandlebarsInstanceAsync, "makeHandlebarsInstanceAsync");
1163
1252
 
1164
1253
  // src/utils/modules/handlebars/hbs.ts
1165
- var __hbsAsync = makeHandlebarsInstanceAsync(Handlebars);
1166
- var __hbs = Handlebars.create();
1167
- var hbs = useHandlebars(__hbs);
1168
- var hbsAsync = useHandlebars(__hbsAsync, true);
1169
- function registerPartials(partials2, instance) {
1170
- if (partials2 && Array.isArray(partials2)) {
1171
- for (const partial of partials2) {
1172
- if (partial.name && typeof partial.name === "string" && typeof partial.template === "string") {
1173
- if (instance) {
1174
- instance.registerPartial(partial.name, partial.template);
1175
- } else {
1176
- hbs.registerPartial(partial.name, partial.template);
1177
- hbsAsync.registerPartial(partial.name, partial.template);
1178
- }
1179
- }
1180
- }
1254
+ var _hbsAsync = makeHandlebarsInstanceAsync(Handlebars);
1255
+ var _hbs = makeHandlebarsInstance(Handlebars);
1256
+
1257
+ // src/utils/modules/handlebars/index.ts
1258
+ var hbs = {
1259
+ handlebars: _hbs,
1260
+ registerHelpers: /* @__PURE__ */ __name((helpers) => {
1261
+ _registerHelpers(helpers, _hbs);
1262
+ }, "registerHelpers"),
1263
+ registerPartials: /* @__PURE__ */ __name((partials2) => {
1264
+ _registerPartials(partials2, _hbs);
1265
+ }, "registerPartials")
1266
+ };
1267
+ var hbsAsync = {
1268
+ handlebars: _hbsAsync,
1269
+ registerHelpers: /* @__PURE__ */ __name((helpers) => {
1270
+ _registerHelpers(helpers, _hbsAsync);
1271
+ }, "registerHelpers"),
1272
+ registerPartials: /* @__PURE__ */ __name((partials2) => {
1273
+ _registerPartials(partials2, _hbsAsync);
1274
+ }, "registerPartials")
1275
+ };
1276
+
1277
+ // src/utils/modules/replaceTemplateString.ts
1278
+ function replaceTemplateString(templateString, substitutions = {}, configuration = {
1279
+ helpers: [],
1280
+ partials: []
1281
+ }) {
1282
+ if (!templateString) return templateString || "";
1283
+ const tempHelpers = [];
1284
+ const tempPartials = [];
1285
+ if (Array.isArray(configuration.helpers)) {
1286
+ hbs.registerHelpers(configuration.helpers);
1287
+ tempHelpers.push(...configuration.helpers.map((a) => a.name));
1181
1288
  }
1182
- }
1183
- __name(registerPartials, "registerPartials");
1184
- function registerHelpers(helpers, instance) {
1185
- if (helpers && Array.isArray(helpers)) {
1186
- for (const helper of helpers) {
1187
- if (helper.name && typeof helper.name === "string" && typeof helper.handler === "function") {
1188
- if (instance) {
1189
- instance.registerHelper(helper.name, helper.handler);
1190
- } else {
1191
- hbs.registerHelper(helper.name, helper.handler);
1192
- hbsAsync.registerHelper(helper.name, helper.handler);
1193
- }
1194
- }
1195
- }
1289
+ if (Array.isArray(configuration.partials)) {
1290
+ hbs.registerPartials(configuration.partials);
1291
+ tempPartials.push(...configuration.partials.map((a) => a.name));
1196
1292
  }
1293
+ const template = hbs.handlebars.compile(templateString);
1294
+ const res = template(substitutions, {
1295
+ allowedProtoMethods: {
1296
+ substring: true
1297
+ }
1298
+ });
1299
+ tempHelpers.forEach(function(n) {
1300
+ hbs.handlebars.unregisterHelper(n);
1301
+ });
1302
+ tempPartials.forEach(function(n) {
1303
+ hbs.handlebars.unregisterPartial(n);
1304
+ });
1305
+ return res;
1197
1306
  }
1198
- __name(registerHelpers, "registerHelpers");
1307
+ __name(replaceTemplateString, "replaceTemplateString");
1199
1308
 
1200
1309
  // src/utils/modules/replaceTemplateStringAsync.ts
1201
1310
  async function replaceTemplateStringAsync(templateString, substitutions = {}, configuration = {
@@ -1206,24 +1315,24 @@ async function replaceTemplateStringAsync(templateString, substitutions = {}, co
1206
1315
  const tempHelpers = [];
1207
1316
  const tempPartials = [];
1208
1317
  if (Array.isArray(configuration.helpers)) {
1209
- registerHelpers(configuration.helpers, hbsAsync);
1318
+ hbsAsync.registerHelpers(configuration.helpers);
1210
1319
  tempHelpers.push(...configuration.helpers.map((a) => a.name));
1211
1320
  }
1212
1321
  if (Array.isArray(configuration.partials)) {
1213
- registerPartials(configuration.partials, hbsAsync);
1322
+ hbsAsync.registerPartials(configuration.partials);
1214
1323
  tempPartials.push(...configuration.partials.map((a) => a.name));
1215
1324
  }
1216
- const template = hbsAsync.compile(templateString);
1325
+ const template = hbsAsync.handlebars.compile(templateString);
1217
1326
  const res = await template(substitutions, {
1218
1327
  allowedProtoMethods: {
1219
1328
  substring: true
1220
1329
  }
1221
1330
  });
1222
1331
  tempHelpers.forEach(function(n) {
1223
- hbsAsync.unregisterHelper(n);
1332
+ hbsAsync.handlebars.unregisterHelper(n);
1224
1333
  });
1225
1334
  tempPartials.forEach(function(n) {
1226
- hbsAsync.unregisterPartial(n);
1335
+ hbsAsync.handlebars.unregisterPartial(n);
1227
1336
  });
1228
1337
  return res;
1229
1338
  }
@@ -1246,65 +1355,17 @@ var asyncCallWithTimeout = /* @__PURE__ */ __name(async (asyncPromise, timeLimit
1246
1355
  });
1247
1356
  }, "asyncCallWithTimeout");
1248
1357
 
1249
- // src/utils/modules/json.ts
1250
- var maybeStringifyJSON = /* @__PURE__ */ __name((objOrMaybeString) => {
1251
- if (!objOrMaybeString || typeof objOrMaybeString !== "object") {
1252
- return objOrMaybeString;
1253
- }
1254
- try {
1255
- const result = JSON.stringify(objOrMaybeString);
1256
- return result;
1257
- } catch (error) {
1258
- }
1259
- return "";
1260
- }, "maybeStringifyJSON");
1261
- var maybeParseJSON = /* @__PURE__ */ __name((objOrMaybeJSON) => {
1262
- if (!objOrMaybeJSON) return {};
1263
- if (typeof objOrMaybeJSON === "string") {
1264
- try {
1265
- const cleanMarkdown = helpJsonMarkup(objOrMaybeJSON);
1266
- const result = JSON.parse(cleanMarkdown);
1267
- if (typeof result === "object" && result !== null) {
1268
- return result;
1269
- }
1270
- } catch (error) {
1271
- }
1272
- }
1273
- if (typeof objOrMaybeJSON === "object" && objOrMaybeJSON !== null) {
1274
- return objOrMaybeJSON;
1275
- }
1276
- return {};
1277
- }, "maybeParseJSON");
1278
- function isObjectStringified(maybeObject) {
1279
- if (typeof maybeObject !== "string") return false;
1280
- const isMaybeObject = maybeObject.substring(0, 1) === "{" && maybeObject.substring(maybeObject.length - 1, maybeObject.length) === "}";
1281
- const isMaybeArray = maybeObject.substring(0, 1) === "[" && maybeObject.substring(maybeObject.length - 1, maybeObject.length) === "]";
1282
- if (!isMaybeObject && !isMaybeArray) {
1283
- return false;
1284
- }
1285
- let canDecode = false;
1286
- try {
1287
- JSON.parse(maybeObject);
1288
- canDecode = true;
1289
- } catch (error) {
1290
- canDecode = false;
1291
- }
1292
- return canDecode;
1358
+ // src/utils/modules/index.ts
1359
+ function registerHelpers(helpers) {
1360
+ hbs.registerHelpers(helpers);
1361
+ hbsAsync.registerHelpers(helpers);
1293
1362
  }
1294
- __name(isObjectStringified, "isObjectStringified");
1295
- function helpJsonMarkup(str) {
1296
- if (typeof str !== "string") {
1297
- return str;
1298
- }
1299
- const input = str.trim();
1300
- const markdownJsonStartsWith = "```json";
1301
- const markdownJsonEndsWith = "```";
1302
- if (input.substring(0, markdownJsonStartsWith.length) === markdownJsonStartsWith && input.substring(input.length - markdownJsonEndsWith.length, input.length) === markdownJsonEndsWith) {
1303
- return str.substring(markdownJsonStartsWith.length, input.length - markdownJsonEndsWith.length)?.trim();
1304
- }
1305
- return str;
1363
+ __name(registerHelpers, "registerHelpers");
1364
+ function registerPartials(partials2) {
1365
+ hbs.registerPartials(partials2);
1366
+ hbsAsync.registerPartials(partials2);
1306
1367
  }
1307
- __name(helpJsonMarkup, "helpJsonMarkup");
1368
+ __name(registerPartials, "registerPartials");
1308
1369
 
1309
1370
  // src/llm/output/_utils/getResultText.ts
1310
1371
  function getResultText(content) {
@@ -1323,7 +1384,7 @@ var _OpenAiFunctionParser = class _OpenAiFunctionParser extends BaseParser {
1323
1384
  this.parser = options.parser;
1324
1385
  }
1325
1386
  parse(text, _options) {
1326
- const functionUse = text.find((a) => a.type === "function_use");
1387
+ const functionUse = text?.find((a) => a.type === "function_use");
1327
1388
  if (functionUse && "name" in functionUse && "input" in functionUse) {
1328
1389
  return {
1329
1390
  name: functionUse.name,
@@ -1689,8 +1750,11 @@ var _LlmExecutor = class _LlmExecutor extends BaseExecutor {
1689
1750
  }
1690
1751
  if (this.promptFn) {
1691
1752
  const prompt = this.promptFn(_input);
1692
- const promptFormatted = prompt.format(_input);
1693
- return promptFormatted;
1753
+ if (isPromise(prompt.formatAsync)) {
1754
+ return await prompt.formatAsync(_input);
1755
+ } else {
1756
+ return prompt.format(_input);
1757
+ }
1694
1758
  }
1695
1759
  throw new Error("Missing prompt");
1696
1760
  }
@@ -2033,7 +2097,16 @@ function anthropicPromptSanitize(_messages, _inputBodyObj, _outputObj) {
2033
2097
  ...a
2034
2098
  }))
2035
2099
  ];
2036
- if (first.role === "system") {
2100
+ if (first.role === "system" && messages.length === 0) {
2101
+ return [
2102
+ {
2103
+ role: "user",
2104
+ content: first.content
2105
+ },
2106
+ ...messages
2107
+ ];
2108
+ }
2109
+ if (first.role === "system" && messages.length > 0) {
2037
2110
  _outputObj.system = first.content;
2038
2111
  return messages;
2039
2112
  }
@@ -2056,7 +2129,8 @@ var anthropicChatV1 = {
2056
2129
  required: [
2057
2130
  true,
2058
2131
  "maxTokens required"
2059
- ]
2132
+ ],
2133
+ default: 4096
2060
2134
  },
2061
2135
  anthropicApiKey: {
2062
2136
  default: getEnvironmentVariable("ANTHROPIC_API_KEY")
@@ -2419,6 +2493,8 @@ function getOutputParser(config, response) {
2419
2493
  return OutputAnthropicClaude3Chat(response, config);
2420
2494
  case "amazon:meta.chat.v1":
2421
2495
  return OutputMetaLlama3Chat(response, config);
2496
+ // case "amazon:nova.chat.v1":
2497
+ // return OutputDefault(response, config);
2422
2498
  default: {
2423
2499
  if (config?.key?.startsWith("custom:")) {
2424
2500
  return OutputDefault(response, config);
@@ -2437,7 +2513,15 @@ async function apiRequest(url, options) {
2437
2513
  try {
2438
2514
  const response = await fetch(url, finalOptions);
2439
2515
  if (!response.ok) {
2440
- throw new Error(`HTTP error! Status: ${response.status}. Error`);
2516
+ let message = `HTTP error. Status: ${response.status}. Error Message: ${response?.statusText || "Unknown error."}`;
2517
+ if (url.startsWith("https://api.openai.com/")) {
2518
+ try {
2519
+ const body = await response.json();
2520
+ message = `HTTP error. Status Code: ${response.status}. Error Message: ${body?.error?.message || "No further details provided."}`;
2521
+ } catch (error) {
2522
+ }
2523
+ }
2524
+ throw new Error(message);
2441
2525
  }
2442
2526
  const responseData = await response.json();
2443
2527
  return responseData;
@@ -2448,23 +2532,6 @@ async function apiRequest(url, options) {
2448
2532
  }
2449
2533
  __name(apiRequest, "apiRequest");
2450
2534
 
2451
- // src/utils/modules/replaceTemplateStringSimple.ts
2452
- function replaceTemplateStringSimple(template, context) {
2453
- return template.replace(/{{\s*([\w.]+)\s*}}/g, (_match, key) => {
2454
- const keys = key.split(".");
2455
- let value = context;
2456
- for (const k of keys) {
2457
- if (value && typeof value === "object" && k in value) {
2458
- value = value[k];
2459
- } else {
2460
- return "";
2461
- }
2462
- }
2463
- return typeof value === "string" ? value : String(value);
2464
- });
2465
- }
2466
- __name(replaceTemplateStringSimple, "replaceTemplateStringSimple");
2467
-
2468
2535
  // src/utils/modules/convertDotNotation.ts
2469
2536
  function convertDotNotation(obj) {
2470
2537
  const result = {};
@@ -2738,23 +2805,21 @@ __name(cleanJsonSchemaFor, "cleanJsonSchemaFor");
2738
2805
  // src/llm/llm.call.ts
2739
2806
  async function useLlm_call(state, messages, _options) {
2740
2807
  const config = getLlmConfig(state.key);
2808
+ const { functionCallStrictInput = false } = _options || {};
2741
2809
  const input = mapBody(config.mapBody, Object.assign({}, state, {
2742
2810
  prompt: messages
2743
2811
  }));
2744
2812
  if (_options && _options?.jsonSchema) {
2745
2813
  if (state.provider === "openai.chat") {
2746
2814
  const curr = input["response_format"] || {};
2747
- const newObj = Object.assign(curr, {
2815
+ input["response_format"] = Object.assign(curr, {
2748
2816
  type: "json_schema",
2749
2817
  json_schema: {
2750
2818
  name: "output",
2819
+ strict: !!functionCallStrictInput,
2751
2820
  schema: cleanJsonSchemaFor(_options?.jsonSchema, "openai.chat")
2752
2821
  }
2753
2822
  });
2754
- if (typeof _options?.functionCallStrictInput === "undefined" || !!_options?.functionCallStrictInput) {
2755
- newObj["json_schema"]["strict"] = true;
2756
- }
2757
- input["response_format"] = newObj;
2758
2823
  }
2759
2824
  }
2760
2825
  if (_options && _options?.functionCall) {
@@ -2791,13 +2856,13 @@ async function useLlm_call(state, messages, _options) {
2791
2856
  function: Object.assign(props, {
2792
2857
  parameters: cleanJsonSchemaFor(props.parameters, "openai.chat")
2793
2858
  }, {
2794
- strict: true
2859
+ strict: functionCallStrictInput
2795
2860
  })
2796
2861
  };
2797
2862
  });
2798
2863
  }
2799
2864
  }
2800
- const body = typeof input === "string" ? input : JSON.stringify(input);
2865
+ const body = JSON.stringify(input);
2801
2866
  const url = replaceTemplateStringSimple(config.endpoint, state);
2802
2867
  const headers = await parseHeaders(config, state, {
2803
2868
  url,
@@ -2969,9 +3034,13 @@ var embeddingConfigs = {
2969
3034
  headers: `{"Authorization":"Bearer {{openAiApiKey}}", "Content-Type": "application/json" }`,
2970
3035
  options: {
2971
3036
  input: {},
2972
- dimensions: {},
3037
+ dimensions: {
3038
+ default: 1536
3039
+ },
2973
3040
  encodingFormat: {},
2974
- openAiApiKey: {}
3041
+ openAiApiKey: {
3042
+ default: getEnvironmentVariable("OPENAI_API_KEY")
3043
+ }
2975
3044
  },
2976
3045
  mapBody: {
2977
3046
  input: {
@@ -2996,7 +3065,9 @@ var embeddingConfigs = {
2996
3065
  headers: `{"Content-Type": "application/json" }`,
2997
3066
  options: {
2998
3067
  input: {},
2999
- dimensions: {},
3068
+ dimensions: {
3069
+ default: 512
3070
+ },
3000
3071
  awsRegion: {
3001
3072
  default: getEnvironmentVariable("AWS_REGION"),
3002
3073
  required: [
@@ -3125,7 +3196,7 @@ async function createEmbedding_call(state, _input, _options) {
3125
3196
  const input = mapBody(config.mapBody, Object.assign({}, state, {
3126
3197
  input: _input
3127
3198
  }));
3128
- const body = typeof input === "string" ? input : JSON.stringify(input);
3199
+ const body = JSON.stringify(input);
3129
3200
  const url = replaceTemplateStringSimple(config.endpoint, state);
3130
3201
  const headers = await parseHeaders(config, state, {
3131
3202
  url,