@dexto/core 1.19.2 → 1.19.4

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.
@@ -72,6 +72,29 @@ function blockSeparator(previousBlockId, blockId, accumulated, delta) {
72
72
  if (accumulated.endsWith("\n\n")) return "";
73
73
  return accumulated.endsWith("\n") ? "\n" : "\n\n";
74
74
  }
75
+ function isRecord(value) {
76
+ return typeof value === "object" && value !== null && !Array.isArray(value);
77
+ }
78
+ function readOpenRouterReasoningDetails(providerMetadata) {
79
+ if (!isRecord(providerMetadata)) return [];
80
+ const openRouter = providerMetadata["openrouter"];
81
+ if (!isRecord(openRouter)) return [];
82
+ const details = openRouter["reasoning_details"];
83
+ return Array.isArray(details) ? details : [];
84
+ }
85
+ function isEncryptedReasoningPlaceholder(text, providerMetadata) {
86
+ if (text !== "[REDACTED]") return false;
87
+ const details = readOpenRouterReasoningDetails(providerMetadata);
88
+ return details.length > 0 && details.every((detail) => isRecord(detail) && detail["type"] === "reasoning.encrypted");
89
+ }
90
+ function toolCallProviderOptions(provider, providerMetadata) {
91
+ if (provider === "google" || provider === "vertex") {
92
+ return providerMetadata ? { ...providerMetadata } : void 0;
93
+ }
94
+ if (provider !== "dexto-nova") return void 0;
95
+ const details = readOpenRouterReasoningDetails(providerMetadata);
96
+ return details.length === 0 ? void 0 : { openrouter: { reasoning_details: details } };
97
+ }
75
98
  class StreamProcessor {
76
99
  /**
77
100
  * @param contextManager Context manager for message persistence
@@ -258,13 +281,17 @@ class StreamProcessor {
258
281
  lastDeltaReceivedAtMs = lastReasoningDeltaReceivedAtMs;
259
282
  setStreamAttribute("last_delta_received_ms", lastDeltaReceivedAtMs);
260
283
  setStreamAttribute("last_delta_kind", "reasoning");
284
+ const text = isEncryptedReasoningPlaceholder(
285
+ event.text,
286
+ event.providerMetadata
287
+ ) ? "" : event.text;
261
288
  const reasoning = blockSeparator(
262
289
  this.lastReasoningBlockId,
263
290
  event.id,
264
291
  this.reasoningText,
265
- event.text
266
- ) + event.text;
267
- if (event.text !== "") this.lastReasoningBlockId = event.id;
292
+ text
293
+ ) + text;
294
+ if (text !== "") this.lastReasoningBlockId = event.id;
268
295
  this.reasoningText += reasoning;
269
296
  if (event.providerMetadata) {
270
297
  this.mergeReasoningMetadata(event.providerMetadata);
@@ -302,12 +329,11 @@ class StreamProcessor {
302
329
  arguments: JSON.stringify(event.input)
303
330
  }
304
331
  };
305
- const shouldPersistProviderMetadata = this.config.provider === "google" || this.config.provider === "vertex";
306
- if (shouldPersistProviderMetadata && event.providerMetadata) {
307
- toolCall.providerOptions = {
308
- ...event.providerMetadata
309
- };
310
- }
332
+ const providerOptions = toolCallProviderOptions(
333
+ this.config.provider,
334
+ event.providerMetadata
335
+ );
336
+ if (providerOptions) toolCall.providerOptions = providerOptions;
311
337
  await this.contextManager.addToolCall(this.assistantMessageId, toolCall);
312
338
  this.modelToolCalls.push({
313
339
  toolCallId: event.toolCallId,
@@ -673,7 +699,6 @@ class StreamProcessor {
673
699
  this.reasoningMetadata = providerMetadata;
674
700
  return;
675
701
  }
676
- const isRecord = (value) => !!value && typeof value === "object" && !Array.isArray(value);
677
702
  const previous = this.reasoningMetadata;
678
703
  const merged = { ...previous, ...providerMetadata };
679
704
  const previousOpenRouter = previous["openrouter"];
@@ -1 +1 @@
1
- {"version":3,"file":"stream-processor.d.ts","sourceRoot":"","sources":["../../../src/llm/executor/stream-processor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,IAAI,CAAC;AAEhE,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAmB,MAAM,uBAAuB,CAAC;AACzE,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEnD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAKvD,OAAO,KAAK,EAAE,WAAW,EAAoB,gBAAgB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AW,KAAK,gBAAgB,CAAC,aAAa,EAAE,OAAO,CAAC,GACjF,OAAO,CAAC,qBAAqB,CAAC;YASnB,aAAa;IA6jB3B,OAAO,CAAC,kCAAkC;IAoB1C,OAAO,CAAC,cAAc;IA4CtB,OAAO,CAAC,mBAAmB;IAa3B,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,eAAe;YA8BT,gCAAgC;IA+B9C,OAAO,CAAC,sBAAsB;YAqChB,eAAe;YA4Bf,sBAAsB;IAMpC;;;;OAIG;YACW,2BAA2B;IAQzC;;OAEG;YACW,wBAAwB;YAQxB,yBAAyB;CAuC1C"}
@@ -49,6 +49,29 @@ function blockSeparator(previousBlockId, blockId, accumulated, delta) {
49
49
  if (accumulated.endsWith("\n\n")) return "";
50
50
  return accumulated.endsWith("\n") ? "\n" : "\n\n";
51
51
  }
52
+ function isRecord(value) {
53
+ return typeof value === "object" && value !== null && !Array.isArray(value);
54
+ }
55
+ function readOpenRouterReasoningDetails(providerMetadata) {
56
+ if (!isRecord(providerMetadata)) return [];
57
+ const openRouter = providerMetadata["openrouter"];
58
+ if (!isRecord(openRouter)) return [];
59
+ const details = openRouter["reasoning_details"];
60
+ return Array.isArray(details) ? details : [];
61
+ }
62
+ function isEncryptedReasoningPlaceholder(text, providerMetadata) {
63
+ if (text !== "[REDACTED]") return false;
64
+ const details = readOpenRouterReasoningDetails(providerMetadata);
65
+ return details.length > 0 && details.every((detail) => isRecord(detail) && detail["type"] === "reasoning.encrypted");
66
+ }
67
+ function toolCallProviderOptions(provider, providerMetadata) {
68
+ if (provider === "google" || provider === "vertex") {
69
+ return providerMetadata ? { ...providerMetadata } : void 0;
70
+ }
71
+ if (provider !== "dexto-nova") return void 0;
72
+ const details = readOpenRouterReasoningDetails(providerMetadata);
73
+ return details.length === 0 ? void 0 : { openrouter: { reasoning_details: details } };
74
+ }
52
75
  class StreamProcessor {
53
76
  /**
54
77
  * @param contextManager Context manager for message persistence
@@ -235,13 +258,17 @@ class StreamProcessor {
235
258
  lastDeltaReceivedAtMs = lastReasoningDeltaReceivedAtMs;
236
259
  setStreamAttribute("last_delta_received_ms", lastDeltaReceivedAtMs);
237
260
  setStreamAttribute("last_delta_kind", "reasoning");
261
+ const text = isEncryptedReasoningPlaceholder(
262
+ event.text,
263
+ event.providerMetadata
264
+ ) ? "" : event.text;
238
265
  const reasoning = blockSeparator(
239
266
  this.lastReasoningBlockId,
240
267
  event.id,
241
268
  this.reasoningText,
242
- event.text
243
- ) + event.text;
244
- if (event.text !== "") this.lastReasoningBlockId = event.id;
269
+ text
270
+ ) + text;
271
+ if (text !== "") this.lastReasoningBlockId = event.id;
245
272
  this.reasoningText += reasoning;
246
273
  if (event.providerMetadata) {
247
274
  this.mergeReasoningMetadata(event.providerMetadata);
@@ -279,12 +306,11 @@ class StreamProcessor {
279
306
  arguments: JSON.stringify(event.input)
280
307
  }
281
308
  };
282
- const shouldPersistProviderMetadata = this.config.provider === "google" || this.config.provider === "vertex";
283
- if (shouldPersistProviderMetadata && event.providerMetadata) {
284
- toolCall.providerOptions = {
285
- ...event.providerMetadata
286
- };
287
- }
309
+ const providerOptions = toolCallProviderOptions(
310
+ this.config.provider,
311
+ event.providerMetadata
312
+ );
313
+ if (providerOptions) toolCall.providerOptions = providerOptions;
288
314
  await this.contextManager.addToolCall(this.assistantMessageId, toolCall);
289
315
  this.modelToolCalls.push({
290
316
  toolCallId: event.toolCallId,
@@ -650,7 +676,6 @@ class StreamProcessor {
650
676
  this.reasoningMetadata = providerMetadata;
651
677
  return;
652
678
  }
653
- const isRecord = (value) => !!value && typeof value === "object" && !Array.isArray(value);
654
679
  const previous = this.reasoningMetadata;
655
680
  const merged = { ...previous, ...providerMetadata };
656
681
  const previousOpenRouter = previous["openrouter"];
@@ -1364,7 +1364,7 @@ class TurnExecutor {
1364
1364
  } catch (error) {
1365
1365
  const historyLengthAfter2 = await this.contextManager.countModelMessages();
1366
1366
  const historyLengthChanged = historyLengthAfter2 !== historyLengthBefore;
1367
- const withoutMedia = media === "sent" && !historyLengthChanged && !this.stepAbortController.signal.aborted && (0, import_withheld_media.isRejectedModelRequest)(error) ? (0, import_withheld_media.withholdRequestMedia)(attemptRequest.messages) : null;
1367
+ const withoutMedia = media === "sent" && !historyLengthChanged && !this.stepAbortController.signal.aborted && (0, import_withheld_media.isRejectedModelRequest)(error) ? (0, import_withheld_media.withholdRequestMedia)(attemptRequest.messages, error) : null;
1368
1368
  if (withoutMedia !== null) {
1369
1369
  media = "retried";
1370
1370
  attemptRequest = { ...attemptRequest, messages: withoutMedia };
@@ -1351,7 +1351,7 @@ class TurnExecutor {
1351
1351
  } catch (error) {
1352
1352
  const historyLengthAfter2 = await this.contextManager.countModelMessages();
1353
1353
  const historyLengthChanged = historyLengthAfter2 !== historyLengthBefore;
1354
- const withoutMedia = media === "sent" && !historyLengthChanged && !this.stepAbortController.signal.aborted && isRejectedModelRequest(error) ? withholdRequestMedia(attemptRequest.messages) : null;
1354
+ const withoutMedia = media === "sent" && !historyLengthChanged && !this.stepAbortController.signal.aborted && isRejectedModelRequest(error) ? withholdRequestMedia(attemptRequest.messages, error) : null;
1355
1355
  if (withoutMedia !== null) {
1356
1356
  media = "retried";
1357
1357
  attemptRequest = { ...attemptRequest, messages: withoutMedia };
@@ -26,13 +26,20 @@ var import_ai = require("ai");
26
26
  function isRejectedModelRequest(error) {
27
27
  return import_ai.APICallError.isInstance(error) && error.statusCode === 400;
28
28
  }
29
- function withheldMediaText(label) {
30
- return `[${label} withheld: the model provider rejected the request that carried it]`;
29
+ const DAMAGED_MEDIA_PATTERN = /badly formatted|corrupt|malformed (?:file|pdf|image|document)|(?:file|pdf|image|document) (?:is |was )?malformed|damaged|truncated|could not be decoded|invalid pdf/iu;
30
+ function rejectionCause(error, mediaCount) {
31
+ if (mediaCount !== 1 || !import_ai.APICallError.isInstance(error)) return "unknown";
32
+ return DAMAGED_MEDIA_PATTERN.test(`${error.message}
33
+ ${error.responseBody ?? ""}`) ? "damaged" : "unknown";
34
+ }
35
+ function withheldMediaText(label, cause) {
36
+ return cause === "damaged" ? `[${label} not read: the model provider could not open it because it is damaged or not a valid file. Tell the person the ${label.toLowerCase()} looks damaged and could not be read; sending the same ${label.toLowerCase()} again will not help, so ask for a fresh export or its text.]` : `[${label} withheld: the model provider rejected the request that carried it]`;
31
37
  }
32
38
  function mediaLabel(mediaType) {
33
39
  return mediaType.startsWith("image/") ? "Image" : "File";
34
40
  }
35
- function withholdRequestMedia(messages) {
41
+ function withholdRequestMedia(messages, error) {
42
+ const cause = rejectionCause(error, countMedia(messages));
36
43
  let withheld = false;
37
44
  const result = messages.map((message) => {
38
45
  if (message.role === "user" && Array.isArray(message.content)) {
@@ -44,7 +51,8 @@ function withholdRequestMedia(messages) {
44
51
  return {
45
52
  type: "text",
46
53
  text: withheldMediaText(
47
- part.type === "image" ? "Image" : mediaLabel(part.mediaType)
54
+ part.type === "image" ? "Image" : mediaLabel(part.mediaType),
55
+ cause
48
56
  )
49
57
  };
50
58
  })
@@ -64,7 +72,7 @@ function withholdRequestMedia(messages) {
64
72
  withheld = true;
65
73
  return {
66
74
  type: "text",
67
- text: withheldMediaText(mediaLabel(item.mediaType))
75
+ text: withheldMediaText(mediaLabel(item.mediaType), cause)
68
76
  };
69
77
  })
70
78
  }
@@ -76,6 +84,20 @@ function withholdRequestMedia(messages) {
76
84
  });
77
85
  return withheld ? result : null;
78
86
  }
87
+ function countMedia(messages) {
88
+ let count = 0;
89
+ for (const message of messages) {
90
+ if (message.role === "user" && Array.isArray(message.content)) {
91
+ count += message.content.filter((part) => part.type !== "text").length;
92
+ } else if (message.role === "tool") {
93
+ for (const part of message.content) {
94
+ if (part.output.type !== "content") continue;
95
+ count += part.output.value.filter((item) => item.type !== "text").length;
96
+ }
97
+ }
98
+ }
99
+ return count;
100
+ }
79
101
  // Annotate the CommonJS export names for ESM import in node:
80
102
  0 && (module.exports = {
81
103
  isRejectedModelRequest,
@@ -6,7 +6,8 @@ import { type ModelMessage } from 'ai';
6
6
  export declare function isRejectedModelRequest(error: unknown): boolean;
7
7
  /**
8
8
  * The request's messages with every user and tool-result image or file replaced by a text
9
- * placeholder, or null when no message carries media.
9
+ * placeholder that says why, as far as the provider's `error` tells, or null when no message
10
+ * carries media.
10
11
  */
11
- export declare function withholdRequestMedia(messages: ModelMessage[]): ModelMessage[] | null;
12
+ export declare function withholdRequestMedia(messages: ModelMessage[], error: unknown): ModelMessage[] | null;
12
13
  //# sourceMappingURL=withheld-media.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"withheld-media.d.ts","sourceRoot":"","sources":["../../../src/llm/executor/withheld-media.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,YAAY,EAAuB,MAAM,IAAI,CAAC;AAE1E;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE9D;AAUD;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,YAAY,EAAE,GAAG,YAAY,EAAE,GAAG,IAAI,CA2CpF"}
1
+ {"version":3,"file":"withheld-media.d.ts","sourceRoot":"","sources":["../../../src/llm/executor/withheld-media.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,YAAY,EAAuB,MAAM,IAAI,CAAC;AAE1E;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE9D;AAwCD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAChC,QAAQ,EAAE,YAAY,EAAE,EACxB,KAAK,EAAE,OAAO,GACf,YAAY,EAAE,GAAG,IAAI,CA6CvB"}
@@ -3,13 +3,20 @@ import { APICallError } from "ai";
3
3
  function isRejectedModelRequest(error) {
4
4
  return APICallError.isInstance(error) && error.statusCode === 400;
5
5
  }
6
- function withheldMediaText(label) {
7
- return `[${label} withheld: the model provider rejected the request that carried it]`;
6
+ const DAMAGED_MEDIA_PATTERN = /badly formatted|corrupt|malformed (?:file|pdf|image|document)|(?:file|pdf|image|document) (?:is |was )?malformed|damaged|truncated|could not be decoded|invalid pdf/iu;
7
+ function rejectionCause(error, mediaCount) {
8
+ if (mediaCount !== 1 || !APICallError.isInstance(error)) return "unknown";
9
+ return DAMAGED_MEDIA_PATTERN.test(`${error.message}
10
+ ${error.responseBody ?? ""}`) ? "damaged" : "unknown";
11
+ }
12
+ function withheldMediaText(label, cause) {
13
+ return cause === "damaged" ? `[${label} not read: the model provider could not open it because it is damaged or not a valid file. Tell the person the ${label.toLowerCase()} looks damaged and could not be read; sending the same ${label.toLowerCase()} again will not help, so ask for a fresh export or its text.]` : `[${label} withheld: the model provider rejected the request that carried it]`;
8
14
  }
9
15
  function mediaLabel(mediaType) {
10
16
  return mediaType.startsWith("image/") ? "Image" : "File";
11
17
  }
12
- function withholdRequestMedia(messages) {
18
+ function withholdRequestMedia(messages, error) {
19
+ const cause = rejectionCause(error, countMedia(messages));
13
20
  let withheld = false;
14
21
  const result = messages.map((message) => {
15
22
  if (message.role === "user" && Array.isArray(message.content)) {
@@ -21,7 +28,8 @@ function withholdRequestMedia(messages) {
21
28
  return {
22
29
  type: "text",
23
30
  text: withheldMediaText(
24
- part.type === "image" ? "Image" : mediaLabel(part.mediaType)
31
+ part.type === "image" ? "Image" : mediaLabel(part.mediaType),
32
+ cause
25
33
  )
26
34
  };
27
35
  })
@@ -41,7 +49,7 @@ function withholdRequestMedia(messages) {
41
49
  withheld = true;
42
50
  return {
43
51
  type: "text",
44
- text: withheldMediaText(mediaLabel(item.mediaType))
52
+ text: withheldMediaText(mediaLabel(item.mediaType), cause)
45
53
  };
46
54
  })
47
55
  }
@@ -53,6 +61,20 @@ function withholdRequestMedia(messages) {
53
61
  });
54
62
  return withheld ? result : null;
55
63
  }
64
+ function countMedia(messages) {
65
+ let count = 0;
66
+ for (const message of messages) {
67
+ if (message.role === "user" && Array.isArray(message.content)) {
68
+ count += message.content.filter((part) => part.type !== "text").length;
69
+ } else if (message.role === "tool") {
70
+ for (const part of message.content) {
71
+ if (part.output.type !== "content") continue;
72
+ count += part.output.value.filter((item) => item.type !== "text").length;
73
+ }
74
+ }
75
+ }
76
+ return count;
77
+ }
56
78
  export {
57
79
  isRejectedModelRequest,
58
80
  withholdRequestMedia
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dexto/core",
3
- "version": "1.19.2",
3
+ "version": "1.19.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -174,7 +174,7 @@
174
174
  "nanoid": "^5.1.16",
175
175
  "winston": "^3.17.0",
176
176
  "yaml": "^2.8.3",
177
- "@dexto/llm": "1.19.2"
177
+ "@dexto/llm": "1.19.4"
178
178
  },
179
179
  "devDependencies": {
180
180
  "@opentelemetry/context-async-hooks": "^2.12.0",