llmist 0.6.2 → 0.7.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
@@ -30,6 +30,20 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  ));
31
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
32
 
33
+ // src/core/constants.ts
34
+ var GADGET_START_PREFIX, GADGET_END_PREFIX, DEFAULT_GADGET_OUTPUT_LIMIT, DEFAULT_GADGET_OUTPUT_LIMIT_PERCENT, CHARS_PER_TOKEN, FALLBACK_CONTEXT_WINDOW;
35
+ var init_constants = __esm({
36
+ "src/core/constants.ts"() {
37
+ "use strict";
38
+ GADGET_START_PREFIX = "!!!GADGET_START:";
39
+ GADGET_END_PREFIX = "!!!GADGET_END";
40
+ DEFAULT_GADGET_OUTPUT_LIMIT = true;
41
+ DEFAULT_GADGET_OUTPUT_LIMIT_PERCENT = 15;
42
+ CHARS_PER_TOKEN = 4;
43
+ FALLBACK_CONTEXT_WINDOW = 128e3;
44
+ }
45
+ });
46
+
33
47
  // src/core/model-shortcuts.ts
34
48
  function isKnownModelPattern(model) {
35
49
  const normalized = model.toLowerCase();
@@ -344,20 +358,6 @@ var init_registry = __esm({
344
358
  }
345
359
  });
346
360
 
347
- // src/core/constants.ts
348
- var GADGET_START_PREFIX, GADGET_END_PREFIX, DEFAULT_GADGET_OUTPUT_LIMIT, DEFAULT_GADGET_OUTPUT_LIMIT_PERCENT, CHARS_PER_TOKEN, FALLBACK_CONTEXT_WINDOW;
349
- var init_constants = __esm({
350
- "src/core/constants.ts"() {
351
- "use strict";
352
- GADGET_START_PREFIX = "!!!GADGET_START:";
353
- GADGET_END_PREFIX = "!!!GADGET_END";
354
- DEFAULT_GADGET_OUTPUT_LIMIT = true;
355
- DEFAULT_GADGET_OUTPUT_LIMIT_PERCENT = 15;
356
- CHARS_PER_TOKEN = 4;
357
- FALLBACK_CONTEXT_WINDOW = 128e3;
358
- }
359
- });
360
-
361
361
  // src/core/prompt-config.ts
362
362
  function resolvePromptTemplate(template, defaultValue, context) {
363
363
  const resolved = template ?? defaultValue;
@@ -939,6 +939,10 @@ function formatParamsAsYaml(params) {
939
939
  }
940
940
  return lines.join("\n");
941
941
  }
942
+ function formatTomlInlineTable(obj) {
943
+ const entries = Object.entries(obj).map(([k, v]) => `${k} = ${formatTomlValue(v)}`);
944
+ return `{ ${entries.join(", ")} }`;
945
+ }
942
946
  function formatTomlValue(value) {
943
947
  if (typeof value === "string") {
944
948
  if (value.includes("\n")) {
@@ -956,10 +960,17 @@ ${delimiter}`;
956
960
  return '""';
957
961
  }
958
962
  if (Array.isArray(value)) {
959
- return JSON.stringify(value);
963
+ if (value.length === 0) return "[]";
964
+ const items = value.map((item) => {
965
+ if (typeof item === "object" && item !== null && !Array.isArray(item)) {
966
+ return formatTomlInlineTable(item);
967
+ }
968
+ return formatTomlValue(item);
969
+ });
970
+ return `[${items.join(", ")}]`;
960
971
  }
961
972
  if (typeof value === "object") {
962
- return JSON.stringify(value);
973
+ return formatTomlInlineTable(value);
963
974
  }
964
975
  return JSON.stringify(value);
965
976
  }
@@ -2005,6 +2016,14 @@ function preprocessTomlHeredoc(tomlStr) {
2005
2016
  }
2006
2017
  return result.join("\n");
2007
2018
  }
2019
+ function stripMarkdownFences(content) {
2020
+ let cleaned = content.trim();
2021
+ const openingFence = /^```(?:toml|yaml|json)?\s*\n/i;
2022
+ const closingFence = /\n?```\s*$/;
2023
+ cleaned = cleaned.replace(openingFence, "");
2024
+ cleaned = cleaned.replace(closingFence, "");
2025
+ return cleaned.trim();
2026
+ }
2008
2027
  var yaml2, import_js_toml, globalInvocationCounter, StreamParser;
2009
2028
  var init_parser = __esm({
2010
2029
  "src/gadgets/parser.ts"() {
@@ -2060,35 +2079,36 @@ var init_parser = __esm({
2060
2079
  * Parse parameter string according to configured format
2061
2080
  */
2062
2081
  parseParameters(raw) {
2082
+ const cleaned = stripMarkdownFences(raw);
2063
2083
  if (this.parameterFormat === "json") {
2064
2084
  try {
2065
- return { parameters: JSON.parse(raw) };
2085
+ return { parameters: JSON.parse(cleaned) };
2066
2086
  } catch (error) {
2067
2087
  return { parseError: this.truncateParseError(error, "JSON") };
2068
2088
  }
2069
2089
  }
2070
2090
  if (this.parameterFormat === "yaml") {
2071
2091
  try {
2072
- return { parameters: yaml2.load(preprocessYaml(raw)) };
2092
+ return { parameters: yaml2.load(preprocessYaml(cleaned)) };
2073
2093
  } catch (error) {
2074
2094
  return { parseError: this.truncateParseError(error, "YAML") };
2075
2095
  }
2076
2096
  }
2077
2097
  if (this.parameterFormat === "toml") {
2078
2098
  try {
2079
- return { parameters: (0, import_js_toml.load)(preprocessTomlHeredoc(raw)) };
2099
+ return { parameters: (0, import_js_toml.load)(preprocessTomlHeredoc(cleaned)) };
2080
2100
  } catch (error) {
2081
2101
  return { parseError: this.truncateParseError(error, "TOML") };
2082
2102
  }
2083
2103
  }
2084
2104
  try {
2085
- return { parameters: JSON.parse(raw) };
2105
+ return { parameters: JSON.parse(cleaned) };
2086
2106
  } catch {
2087
2107
  try {
2088
- return { parameters: (0, import_js_toml.load)(preprocessTomlHeredoc(raw)) };
2108
+ return { parameters: (0, import_js_toml.load)(preprocessTomlHeredoc(cleaned)) };
2089
2109
  } catch {
2090
2110
  try {
2091
- return { parameters: yaml2.load(preprocessYaml(raw)) };
2111
+ return { parameters: yaml2.load(preprocessYaml(cleaned)) };
2092
2112
  } catch (error) {
2093
2113
  return { parseError: this.truncateParseError(error, "auto") };
2094
2114
  }
@@ -2634,6 +2654,7 @@ var init_agent = __esm({
2634
2654
  gadgetEndPrefix;
2635
2655
  onHumanInputRequired;
2636
2656
  textOnlyHandler;
2657
+ textWithGadgetsHandler;
2637
2658
  stopOnGadgetError;
2638
2659
  shouldContinueAfterError;
2639
2660
  defaultGadgetTimeoutMs;
@@ -2664,6 +2685,7 @@ var init_agent = __esm({
2664
2685
  this.gadgetEndPrefix = options.gadgetEndPrefix;
2665
2686
  this.onHumanInputRequired = options.onHumanInputRequired;
2666
2687
  this.textOnlyHandler = options.textOnlyHandler ?? "terminate";
2688
+ this.textWithGadgetsHandler = options.textWithGadgetsHandler;
2667
2689
  this.stopOnGadgetError = options.stopOnGadgetError ?? true;
2668
2690
  this.shouldContinueAfterError = options.shouldContinueAfterError;
2669
2691
  this.defaultGadgetTimeoutMs = options.defaultGadgetTimeoutMs;
@@ -2851,6 +2873,17 @@ var init_agent = __esm({
2851
2873
  }
2852
2874
  }
2853
2875
  if (result.didExecuteGadgets) {
2876
+ if (this.textWithGadgetsHandler) {
2877
+ const textContent = result.outputs.filter((output) => output.type === "text").map((output) => output.content).join("");
2878
+ if (textContent.trim()) {
2879
+ const { gadgetName, parameterMapping, resultMapping } = this.textWithGadgetsHandler;
2880
+ this.conversation.addGadgetCall(
2881
+ gadgetName,
2882
+ parameterMapping(textContent),
2883
+ resultMapping ? resultMapping(textContent) : textContent
2884
+ );
2885
+ }
2886
+ }
2854
2887
  for (const output of result.outputs) {
2855
2888
  if (output.type === "gadget_result") {
2856
2889
  const gadgetResult = output.result;
@@ -2862,7 +2895,13 @@ var init_agent = __esm({
2862
2895
  }
2863
2896
  }
2864
2897
  } else {
2865
- this.conversation.addAssistantMessage(finalMessage);
2898
+ if (finalMessage.trim()) {
2899
+ this.conversation.addGadgetCall(
2900
+ "TellUser",
2901
+ { message: finalMessage, done: false, type: "info" },
2902
+ `\u2139\uFE0F ${finalMessage}`
2903
+ );
2904
+ }
2866
2905
  const shouldBreak = await this.handleTextOnlyResponse(finalMessage);
2867
2906
  if (shouldBreak) {
2868
2907
  break;
@@ -4736,6 +4775,7 @@ var AgentBuilder;
4736
4775
  var init_builder = __esm({
4737
4776
  "src/agent/builder.ts"() {
4738
4777
  "use strict";
4778
+ init_constants();
4739
4779
  init_model_shortcuts();
4740
4780
  init_registry();
4741
4781
  init_agent();
@@ -4757,6 +4797,7 @@ var init_builder = __esm({
4757
4797
  gadgetStartPrefix;
4758
4798
  gadgetEndPrefix;
4759
4799
  textOnlyHandler;
4800
+ textWithGadgetsHandler;
4760
4801
  stopOnGadgetError;
4761
4802
  shouldContinueAfterError;
4762
4803
  defaultGadgetTimeoutMs;
@@ -5019,6 +5060,30 @@ var init_builder = __esm({
5019
5060
  this.textOnlyHandler = handler;
5020
5061
  return this;
5021
5062
  }
5063
+ /**
5064
+ * Set the handler for text content that appears alongside gadget calls.
5065
+ *
5066
+ * When set, text accompanying gadget responses will be wrapped as a
5067
+ * synthetic gadget call before the actual gadget results in the
5068
+ * conversation history.
5069
+ *
5070
+ * @param handler - Configuration for wrapping text
5071
+ * @returns This builder for chaining
5072
+ *
5073
+ * @example
5074
+ * ```typescript
5075
+ * // Wrap text as TellUser gadget
5076
+ * .withTextWithGadgetsHandler({
5077
+ * gadgetName: "TellUser",
5078
+ * parameterMapping: (text) => ({ message: text, done: false, type: "info" }),
5079
+ * resultMapping: (text) => `ℹ️ ${text}`,
5080
+ * })
5081
+ * ```
5082
+ */
5083
+ withTextWithGadgetsHandler(handler) {
5084
+ this.textWithGadgetsHandler = handler;
5085
+ return this;
5086
+ }
5022
5087
  /**
5023
5088
  * Set whether to stop gadget execution on first error.
5024
5089
  *
@@ -5133,6 +5198,69 @@ var init_builder = __esm({
5133
5198
  this.gadgetOutputLimitPercent = percent;
5134
5199
  return this;
5135
5200
  }
5201
+ /**
5202
+ * Add a synthetic gadget call to the conversation history.
5203
+ *
5204
+ * This is useful for in-context learning - showing the LLM what "past self"
5205
+ * did correctly so it mimics the pattern. The call is formatted with proper
5206
+ * markers and parameter format.
5207
+ *
5208
+ * @param gadgetName - Name of the gadget
5209
+ * @param parameters - Parameters passed to the gadget
5210
+ * @param result - Result returned by the gadget
5211
+ * @returns This builder for chaining
5212
+ *
5213
+ * @example
5214
+ * ```typescript
5215
+ * .withSyntheticGadgetCall(
5216
+ * 'TellUser',
5217
+ * {
5218
+ * message: '👋 Hello!\n\nHere\'s what I can do:\n- Analyze code\n- Run commands',
5219
+ * done: false,
5220
+ * type: 'info'
5221
+ * },
5222
+ * 'ℹ️ 👋 Hello!\n\nHere\'s what I can do:\n- Analyze code\n- Run commands'
5223
+ * )
5224
+ * ```
5225
+ */
5226
+ withSyntheticGadgetCall(gadgetName, parameters, result) {
5227
+ const startPrefix = this.gadgetStartPrefix ?? GADGET_START_PREFIX;
5228
+ const endPrefix = this.gadgetEndPrefix ?? GADGET_END_PREFIX;
5229
+ const format = this.parameterFormat ?? "yaml";
5230
+ const paramStr = this.formatSyntheticParameters(parameters, format);
5231
+ this.initialMessages.push({
5232
+ role: "assistant",
5233
+ content: `${startPrefix}${gadgetName}
5234
+ ${paramStr}
5235
+ ${endPrefix}`
5236
+ });
5237
+ this.initialMessages.push({
5238
+ role: "user",
5239
+ content: `Result: ${result}`
5240
+ });
5241
+ return this;
5242
+ }
5243
+ /**
5244
+ * Format parameters for synthetic gadget calls.
5245
+ * Uses heredoc for multiline string values.
5246
+ */
5247
+ formatSyntheticParameters(parameters, format) {
5248
+ if (format === "json" || format === "auto") {
5249
+ return JSON.stringify(parameters);
5250
+ }
5251
+ return Object.entries(parameters).map(([key, value]) => {
5252
+ if (typeof value === "string" && value.includes("\n")) {
5253
+ const separator = format === "yaml" ? ":" : " =";
5254
+ return `${key}${separator} <<<EOF
5255
+ ${value}
5256
+ EOF`;
5257
+ }
5258
+ if (format === "yaml") {
5259
+ return typeof value === "string" ? `${key}: ${value}` : `${key}: ${JSON.stringify(value)}`;
5260
+ }
5261
+ return `${key} = ${JSON.stringify(value)}`;
5262
+ }).join("\n");
5263
+ }
5136
5264
  /**
5137
5265
  * Build and create the agent with the given user prompt.
5138
5266
  * Returns the Agent instance ready to run.
@@ -5175,6 +5303,7 @@ var init_builder = __esm({
5175
5303
  gadgetStartPrefix: this.gadgetStartPrefix,
5176
5304
  gadgetEndPrefix: this.gadgetEndPrefix,
5177
5305
  textOnlyHandler: this.textOnlyHandler,
5306
+ textWithGadgetsHandler: this.textWithGadgetsHandler,
5178
5307
  stopOnGadgetError: this.stopOnGadgetError,
5179
5308
  shouldContinueAfterError: this.shouldContinueAfterError,
5180
5309
  defaultGadgetTimeoutMs: this.defaultGadgetTimeoutMs,
@@ -5276,6 +5405,7 @@ var init_builder = __esm({
5276
5405
  gadgetStartPrefix: this.gadgetStartPrefix,
5277
5406
  gadgetEndPrefix: this.gadgetEndPrefix,
5278
5407
  textOnlyHandler: this.textOnlyHandler,
5408
+ textWithGadgetsHandler: this.textWithGadgetsHandler,
5279
5409
  stopOnGadgetError: this.stopOnGadgetError,
5280
5410
  shouldContinueAfterError: this.shouldContinueAfterError,
5281
5411
  defaultGadgetTimeoutMs: this.defaultGadgetTimeoutMs,