@dexto/core 1.19.1 → 1.19.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.
@@ -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.1",
3
+ "version": "1.19.3",
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.1"
177
+ "@dexto/llm": "1.19.3"
178
178
  },
179
179
  "devDependencies": {
180
180
  "@opentelemetry/context-async-hooks": "^2.12.0",