@t2000/engine 1.25.1 → 1.25.3

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.js CHANGED
@@ -1110,7 +1110,7 @@ function parseNumberOrNull(input) {
1110
1110
  const n = Number(input);
1111
1111
  return Number.isFinite(n) ? n : null;
1112
1112
  }
1113
- var DEFI_PORTFOLIO_TIMEOUT_MS = 4e3;
1113
+ var DEFI_PORTFOLIO_TIMEOUT_MS = 2e3;
1114
1114
  var DEFI_FRESH_TTL_MS_BLOCKVISION = 6e4;
1115
1115
  var DEFI_FRESH_TTL_MS_PARTIAL = 15e3;
1116
1116
  var DEFI_FRESH_TTL_MS_PARTIAL_STALE = 0;
@@ -1334,6 +1334,10 @@ async function fetchOneDefiProtocol(address, protocol, apiKey, retryStats) {
1334
1334
  { signal, retryStats }
1335
1335
  );
1336
1336
  } catch (err) {
1337
+ const errName = err?.name;
1338
+ if (errName === "AbortError" || errName === "TimeoutError") {
1339
+ getTelemetrySink().counter("defi.protocol_timeout_count", { protocol });
1340
+ }
1337
1341
  console.warn(`[defi] ${protocol} fetch threw:`, err);
1338
1342
  return null;
1339
1343
  }
@@ -7668,7 +7672,7 @@ var QueryEngine = class {
7668
7672
  })),
7669
7673
  ...writeResultBlocks
7670
7674
  ];
7671
- const canonicalRouteText = response.approved ? buildCanonicalRouteText(action) : null;
7675
+ const canonicalRouteText = response.approved ? buildCanonicalRouteText(action, response) : null;
7672
7676
  if (canonicalRouteText) {
7673
7677
  allResults.push({ type: "text", text: canonicalRouteText });
7674
7678
  }
@@ -9028,16 +9032,33 @@ ${recipeCtx}`;
9028
9032
  }
9029
9033
  }
9030
9034
  };
9031
- function buildCanonicalRouteText(action) {
9035
+ function buildCanonicalRouteText(action, response) {
9036
+ const failedToolUseIds = /* @__PURE__ */ new Set();
9037
+ if (Array.isArray(action.steps) && action.steps.length > 0) {
9038
+ const stepResultByToolUseId = new Map(
9039
+ (response.stepResults ?? []).map((sr) => [sr.toolUseId, sr])
9040
+ );
9041
+ for (const step of action.steps) {
9042
+ const sr = stepResultByToolUseId.get(step.toolUseId);
9043
+ if (!sr || sr.isError === true) {
9044
+ failedToolUseIds.add(step.toolUseId);
9045
+ }
9046
+ }
9047
+ } else if (isExecutionResultFailure(response.executionResult)) {
9048
+ failedToolUseIds.add(action.toolUseId);
9049
+ }
9032
9050
  const swaps = [];
9033
9051
  if (Array.isArray(action.steps) && action.steps.length > 0) {
9034
9052
  for (const step of action.steps) {
9035
9053
  if (step.toolName === "swap_execute" && step.cetusRoute) {
9054
+ if (failedToolUseIds.has(step.toolUseId)) continue;
9036
9055
  swaps.push({ input: step.input, cetusRoute: step.cetusRoute });
9037
9056
  }
9038
9057
  }
9039
9058
  } else if (action.toolName === "swap_execute" && action.cetusRoute) {
9040
- swaps.push({ input: action.input, cetusRoute: action.cetusRoute });
9059
+ if (!failedToolUseIds.has(action.toolUseId)) {
9060
+ swaps.push({ input: action.input, cetusRoute: action.cetusRoute });
9061
+ }
9041
9062
  }
9042
9063
  if (swaps.length === 0) return null;
9043
9064
  const blocks = swaps.map(({ input, cetusRoute }) => {
@@ -9057,6 +9078,24 @@ function buildCanonicalRouteText(action) {
9057
9078
  });
9058
9079
  return blocks.join("\n\n");
9059
9080
  }
9081
+ function isExecutionResultFailure(executionResult) {
9082
+ if (executionResult === null || executionResult === void 0) return false;
9083
+ if (typeof executionResult !== "object") return false;
9084
+ const er = executionResult;
9085
+ if (er.success === false) return true;
9086
+ if (typeof er.error === "string" && er.error.length > 0) return true;
9087
+ if (er._bundleReverted === true) return true;
9088
+ if (er._sessionExpired === true) return true;
9089
+ if (er._txReverted === true) return true;
9090
+ if (er.data && typeof er.data === "object") {
9091
+ const d = er.data;
9092
+ if (typeof d.error === "string" && d.error.length > 0) return true;
9093
+ if (d._bundleReverted === true) return true;
9094
+ if (d._sessionExpired === true) return true;
9095
+ if (d._txReverted === true) return true;
9096
+ }
9097
+ return false;
9098
+ }
9060
9099
  function isCorruptHistoryError(err) {
9061
9100
  const msg = err instanceof Error ? err.message : String(err);
9062
9101
  return msg.includes("tool_use") && msg.includes("tool_result") || msg.includes("roles must alternate") || msg.includes("400") && msg.includes("invalid_request_error");