llmist 1.4.0 → 1.6.0

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.cjs CHANGED
@@ -857,38 +857,83 @@ function formatParamsAsBlock(params, prefix = "", argPrefix = GADGET_ARG_PREFIX)
857
857
  }
858
858
  return lines.join("\n");
859
859
  }
860
- function formatSchemaAsPlainText(schema, indent = "") {
860
+ function formatParamLine(key, propObj, isRequired, indent = "") {
861
+ const type = propObj.type;
862
+ const description = propObj.description;
863
+ const enumValues = propObj.enum;
864
+ let line = `${indent}- ${key}`;
865
+ if (type === "array") {
866
+ const items = propObj.items;
867
+ const itemType = items?.type || "any";
868
+ line += ` (array of ${itemType})`;
869
+ } else if (type === "object" && propObj.properties) {
870
+ line += " (object)";
871
+ } else {
872
+ line += ` (${type})`;
873
+ }
874
+ if (isRequired && indent !== "") {
875
+ line += " [required]";
876
+ }
877
+ if (description) {
878
+ line += `: ${description}`;
879
+ }
880
+ if (enumValues) {
881
+ line += ` - one of: ${enumValues.map((v) => `"${v}"`).join(", ")}`;
882
+ }
883
+ return line;
884
+ }
885
+ function formatSchemaAsPlainText(schema, indent = "", atRoot = true) {
861
886
  const lines = [];
862
887
  const properties = schema.properties || {};
863
888
  const required = schema.required || [];
864
- for (const [key, prop] of Object.entries(properties)) {
865
- const propObj = prop;
866
- const type = propObj.type;
867
- const description = propObj.description;
868
- const isRequired = required.includes(key);
869
- const enumValues = propObj.enum;
870
- let line = `${indent}- ${key}`;
871
- if (type === "array") {
872
- const items = propObj.items;
873
- const itemType = items?.type || "any";
874
- line += ` (array of ${itemType})`;
875
- } else if (type === "object" && propObj.properties) {
876
- line += " (object)";
877
- } else {
878
- line += ` (${type})`;
889
+ if (atRoot && indent === "") {
890
+ const requiredProps = [];
891
+ const optionalProps = [];
892
+ for (const [key, prop] of Object.entries(properties)) {
893
+ if (required.includes(key)) {
894
+ requiredProps.push([key, prop]);
895
+ } else {
896
+ optionalProps.push([key, prop]);
897
+ }
879
898
  }
880
- if (isRequired) {
881
- line += " [required]";
899
+ const reqCount = requiredProps.length;
900
+ const optCount = optionalProps.length;
901
+ if (reqCount > 0 || optCount > 0) {
902
+ const parts = [];
903
+ if (reqCount > 0) parts.push(`${reqCount} required`);
904
+ if (optCount > 0) parts.push(`${optCount} optional`);
905
+ lines.push(parts.join(", "));
906
+ lines.push("");
882
907
  }
883
- if (description) {
884
- line += `: ${description}`;
908
+ if (reqCount > 0) {
909
+ lines.push("REQUIRED Parameters:");
910
+ for (const [key, prop] of requiredProps) {
911
+ lines.push(formatParamLine(key, prop, true, ""));
912
+ const propObj = prop;
913
+ if (propObj.type === "object" && propObj.properties) {
914
+ lines.push(formatSchemaAsPlainText(propObj, " ", false));
915
+ }
916
+ }
885
917
  }
886
- if (enumValues) {
887
- line += ` - one of: ${enumValues.map((v) => `"${v}"`).join(", ")}`;
918
+ if (optCount > 0) {
919
+ if (reqCount > 0) lines.push("");
920
+ lines.push("OPTIONAL Parameters:");
921
+ for (const [key, prop] of optionalProps) {
922
+ lines.push(formatParamLine(key, prop, false, ""));
923
+ const propObj = prop;
924
+ if (propObj.type === "object" && propObj.properties) {
925
+ lines.push(formatSchemaAsPlainText(propObj, " ", false));
926
+ }
927
+ }
888
928
  }
889
- lines.push(line);
890
- if (type === "object" && propObj.properties) {
891
- lines.push(formatSchemaAsPlainText(propObj, indent + " "));
929
+ return lines.join("\n");
930
+ }
931
+ for (const [key, prop] of Object.entries(properties)) {
932
+ const isRequired = required.includes(key);
933
+ lines.push(formatParamLine(key, prop, isRequired, indent));
934
+ const propObj = prop;
935
+ if (propObj.type === "object" && propObj.properties) {
936
+ lines.push(formatSchemaAsPlainText(propObj, indent + " ", false));
892
937
  }
893
938
  }
894
939
  return lines.join("\n");
@@ -939,10 +984,11 @@ var init_gadget = __esm({
939
984
  * Generate instruction text for the LLM.
940
985
  * Combines name, description, and parameter schema into a formatted instruction.
941
986
  *
942
- * @param argPrefix - Optional custom argument prefix for block format examples
987
+ * @param optionsOrArgPrefix - Optional custom prefixes for examples, or just argPrefix string for backwards compatibility
943
988
  * @returns Formatted instruction string
944
989
  */
945
- getInstruction(argPrefix) {
990
+ getInstruction(optionsOrArgPrefix) {
991
+ const options = typeof optionsOrArgPrefix === "string" ? { argPrefix: optionsOrArgPrefix } : optionsOrArgPrefix;
946
992
  const parts = [];
947
993
  parts.push(this.description);
948
994
  if (this.parameterSchema) {
@@ -956,18 +1002,25 @@ var init_gadget = __esm({
956
1002
  }
957
1003
  if (this.examples && this.examples.length > 0) {
958
1004
  parts.push("\n\nExamples:");
959
- const effectiveArgPrefix = argPrefix ?? GADGET_ARG_PREFIX;
1005
+ const effectiveArgPrefix = options?.argPrefix ?? GADGET_ARG_PREFIX;
1006
+ const effectiveStartPrefix = options?.startPrefix ?? GADGET_START_PREFIX;
1007
+ const effectiveEndPrefix = options?.endPrefix ?? GADGET_END_PREFIX;
1008
+ const gadgetName = this.name || this.constructor.name;
960
1009
  this.examples.forEach((example, index) => {
961
1010
  if (index > 0) {
962
1011
  parts.push("");
1012
+ parts.push("---");
1013
+ parts.push("");
963
1014
  }
964
1015
  if (example.comment) {
965
1016
  parts.push(`# ${example.comment}`);
966
1017
  }
967
- parts.push("Input:");
1018
+ parts.push(`${effectiveStartPrefix}${gadgetName}`);
968
1019
  parts.push(formatParamsAsBlock(example.params, "", effectiveArgPrefix));
1020
+ parts.push(effectiveEndPrefix);
969
1021
  if (example.output !== void 0) {
970
- parts.push("Output:");
1022
+ parts.push("");
1023
+ parts.push("Expected Output:");
971
1024
  parts.push(example.output);
972
1025
  }
973
1026
  });