llmist 2.0.0 → 2.1.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.
@@ -30,7 +30,7 @@ import {
30
30
  init_strategy,
31
31
  init_stream_processor,
32
32
  resolveHintTemplate
33
- } from "./chunk-LBHWVCZ2.js";
33
+ } from "./chunk-PDYVT3FI.js";
34
34
 
35
35
  // src/index.ts
36
36
  init_builder();
@@ -978,4 +978,4 @@ export {
978
978
  Gadget,
979
979
  z
980
980
  };
981
- //# sourceMappingURL=chunk-LFSIEPAE.js.map
981
+ //# sourceMappingURL=chunk-LSCCBXS7.js.map
@@ -1034,6 +1034,100 @@ var init_gadget = __esm({
1034
1034
  throw new AbortError();
1035
1035
  }
1036
1036
  }
1037
+ /**
1038
+ * Register a cleanup function to run when execution is aborted (timeout or cancellation).
1039
+ * The cleanup function is called immediately if the signal is already aborted.
1040
+ * Errors thrown by the cleanup function are silently ignored.
1041
+ *
1042
+ * Use this to clean up resources like browser instances, database connections,
1043
+ * or child processes when the gadget is cancelled due to timeout.
1044
+ *
1045
+ * @param ctx - The execution context containing the abort signal
1046
+ * @param cleanup - Function to run on abort (can be sync or async)
1047
+ *
1048
+ * @example
1049
+ * ```typescript
1050
+ * class BrowserGadget extends Gadget({
1051
+ * description: 'Fetches web page content',
1052
+ * schema: z.object({ url: z.string() }),
1053
+ * }) {
1054
+ * async execute(params: this['params'], ctx?: ExecutionContext): Promise<string> {
1055
+ * const browser = await chromium.launch();
1056
+ * this.onAbort(ctx, () => browser.close());
1057
+ *
1058
+ * const page = await browser.newPage();
1059
+ * this.onAbort(ctx, () => page.close());
1060
+ *
1061
+ * await page.goto(params.url);
1062
+ * const content = await page.content();
1063
+ *
1064
+ * await browser.close();
1065
+ * return content;
1066
+ * }
1067
+ * }
1068
+ * ```
1069
+ */
1070
+ onAbort(ctx, cleanup) {
1071
+ if (!ctx?.signal) return;
1072
+ const safeCleanup = () => {
1073
+ try {
1074
+ const result = cleanup();
1075
+ if (result && typeof result === "object" && "catch" in result) {
1076
+ result.catch(() => {
1077
+ });
1078
+ }
1079
+ } catch {
1080
+ }
1081
+ };
1082
+ if (ctx.signal.aborted) {
1083
+ safeCleanup();
1084
+ return;
1085
+ }
1086
+ ctx.signal.addEventListener("abort", safeCleanup, { once: true });
1087
+ }
1088
+ /**
1089
+ * Create an AbortController linked to the execution context's signal.
1090
+ * When the parent signal aborts, the returned controller also aborts with the same reason.
1091
+ *
1092
+ * Useful for passing abort signals to child operations like fetch() while still
1093
+ * being able to abort them independently if needed.
1094
+ *
1095
+ * @param ctx - The execution context containing the parent abort signal
1096
+ * @returns A new AbortController linked to the parent signal
1097
+ *
1098
+ * @example
1099
+ * ```typescript
1100
+ * class FetchGadget extends Gadget({
1101
+ * description: 'Fetches data from URL',
1102
+ * schema: z.object({ url: z.string() }),
1103
+ * }) {
1104
+ * async execute(params: this['params'], ctx?: ExecutionContext): Promise<string> {
1105
+ * const controller = this.createLinkedAbortController(ctx);
1106
+ *
1107
+ * // fetch() will automatically abort when parent times out
1108
+ * const response = await fetch(params.url, { signal: controller.signal });
1109
+ * return response.text();
1110
+ * }
1111
+ * }
1112
+ * ```
1113
+ */
1114
+ createLinkedAbortController(ctx) {
1115
+ const controller = new AbortController();
1116
+ if (ctx?.signal) {
1117
+ if (ctx.signal.aborted) {
1118
+ controller.abort(ctx.signal.reason);
1119
+ } else {
1120
+ ctx.signal.addEventListener(
1121
+ "abort",
1122
+ () => {
1123
+ controller.abort(ctx.signal.reason);
1124
+ },
1125
+ { once: true }
1126
+ );
1127
+ }
1128
+ }
1129
+ return controller;
1130
+ }
1037
1131
  /**
1038
1132
  * Auto-generated instruction text for the LLM.
1039
1133
  * Combines name, description, and parameter schema into a formatted instruction.
@@ -3846,6 +3940,17 @@ var init_agent = __esm({
3846
3940
  llmOptions = { ...llmOptions, ...action.modifiedOptions };
3847
3941
  }
3848
3942
  }
3943
+ await this.safeObserve(async () => {
3944
+ if (this.hooks.observers?.onLLMCallReady) {
3945
+ const context = {
3946
+ iteration: currentIteration,
3947
+ maxIterations: this.maxIterations,
3948
+ options: llmOptions,
3949
+ logger: this.logger
3950
+ };
3951
+ await this.hooks.observers.onLLMCallReady(context);
3952
+ }
3953
+ });
3849
3954
  this.logger.info("Calling LLM", { model: this.model });
3850
3955
  this.logger.silly("LLM request details", {
3851
3956
  model: llmOptions.model,
@@ -8379,4 +8484,4 @@ export {
8379
8484
  MockPromptRecorder,
8380
8485
  waitFor
8381
8486
  };
8382
- //# sourceMappingURL=chunk-LBHWVCZ2.js.map
8487
+ //# sourceMappingURL=chunk-PDYVT3FI.js.map