llmist 2.0.0 → 2.2.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.
@@ -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.
@@ -3785,6 +3879,23 @@ var init_agent = __esm({
3785
3879
  maxIterations: this.maxIterations
3786
3880
  });
3787
3881
  while (currentIteration < this.maxIterations) {
3882
+ if (this.signal?.aborted) {
3883
+ this.logger.info("Agent loop terminated by abort signal", {
3884
+ iteration: currentIteration,
3885
+ reason: this.signal.reason
3886
+ });
3887
+ await this.safeObserve(async () => {
3888
+ if (this.hooks.observers?.onAbort) {
3889
+ const context = {
3890
+ iteration: currentIteration,
3891
+ reason: this.signal?.reason,
3892
+ logger: this.logger
3893
+ };
3894
+ await this.hooks.observers.onAbort(context);
3895
+ }
3896
+ });
3897
+ return;
3898
+ }
3788
3899
  this.logger.debug("Starting iteration", { iteration: currentIteration });
3789
3900
  try {
3790
3901
  if (this.compactionManager) {
@@ -3846,6 +3957,17 @@ var init_agent = __esm({
3846
3957
  llmOptions = { ...llmOptions, ...action.modifiedOptions };
3847
3958
  }
3848
3959
  }
3960
+ await this.safeObserve(async () => {
3961
+ if (this.hooks.observers?.onLLMCallReady) {
3962
+ const context = {
3963
+ iteration: currentIteration,
3964
+ maxIterations: this.maxIterations,
3965
+ options: llmOptions,
3966
+ logger: this.logger
3967
+ };
3968
+ await this.hooks.observers.onLLMCallReady(context);
3969
+ }
3970
+ });
3849
3971
  this.logger.info("Calling LLM", { model: this.model });
3850
3972
  this.logger.silly("LLM request details", {
3851
3973
  model: llmOptions.model,
@@ -8379,4 +8501,4 @@ export {
8379
8501
  MockPromptRecorder,
8380
8502
  waitFor
8381
8503
  };
8382
- //# sourceMappingURL=chunk-LBHWVCZ2.js.map
8504
+ //# sourceMappingURL=chunk-GANXNBIZ.js.map