@xsai/text-messages 1.0.0-beta.1 → 1.0.0-beta.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.
Files changed (2) hide show
  1. package/dist/index.js +18 -5
  2. package/package.json +7 -4
package/dist/index.js CHANGED
@@ -221,10 +221,16 @@ const normalizeFormat = (outputFormat) => outputFormat == null ? void 0 : {
221
221
  type: "json_schema"
222
222
  };
223
223
 
224
- const DATA_URL_PATTERN = /^data:([^;,]+);base64,(.*)$/s;
225
- const parseDataUrl = (data) => {
226
- const match = DATA_URL_PATTERN.exec(data);
227
- return match === null ? void 0 : { data: match[2], mediaType: match[1] };
224
+ const parseDataUrl = (str) => {
225
+ const match = /^data:([^;,]+)((?:;[^;,]*)*);base64,(.*)$/s.exec(str);
226
+ if (match == null)
227
+ return;
228
+ const [, mediaType, params, data] = match;
229
+ return {
230
+ charset: /;\s*charset=([^;]*)/i.exec(params)?.[1],
231
+ data,
232
+ mediaType
233
+ };
228
234
  };
229
235
  const normalizeInputTextPart = (part) => ({
230
236
  // TODO: Move citations to a first-class TextPart field.
@@ -245,11 +251,18 @@ const normalizeFilePart = (part) => {
245
251
  const data = part.data.toString();
246
252
  const dataUrl = parseDataUrl(data);
247
253
  if (dataUrl != null) {
254
+ if (dataUrl.mediaType === "text/plain") {
255
+ return { source: {
256
+ data: new TextDecoder(dataUrl.charset ?? "utf-8").decode(Uint8Array.fromBase64(dataUrl.data)),
257
+ media_type: "text/plain",
258
+ type: "text"
259
+ }, type: "document" };
260
+ }
248
261
  if (dataUrl.mediaType !== "application/pdf")
249
262
  throw new XSAIError("invalid-input", `Anthropic documents only support PDF or plain text, got: ${dataUrl.mediaType}`);
250
263
  return { source: { data: dataUrl.data, media_type: "application/pdf", type: "base64" }, type: "document" };
251
264
  }
252
- if (URL.canParse(data))
265
+ if (/^https?:$/.test(URL.parse(data)?.protocol ?? ""))
253
266
  return { source: { type: "url", url: data }, type: "document" };
254
267
  return { source: { data, media_type: "text/plain", type: "text" }, type: "document" };
255
268
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xsai/text-messages",
3
3
  "type": "module",
4
- "version": "1.0.0-beta.1",
4
+ "version": "1.0.0-beta.3",
5
5
  "description": "extra-small AI SDK: Anthropic Messages API model for @xsai/text.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",
@@ -30,14 +30,17 @@
30
30
  "files": [
31
31
  "dist"
32
32
  ],
33
+ "engines": {
34
+ "node": ">=26"
35
+ },
33
36
  "peerDependencies": {
34
- "@xsai/text": "1.0.0-beta.1"
37
+ "@xsai/text": "1.0.0-beta.3"
35
38
  },
36
39
  "dependencies": {
37
- "@xsai/shared": "1.0.0-beta.1"
40
+ "@xsai/shared": "1.0.0-beta.3"
38
41
  },
39
42
  "devDependencies": {
40
- "@xsai/text": "1.0.0-beta.1"
43
+ "@xsai/text": "1.0.0-beta.3"
41
44
  },
42
45
  "scripts": {
43
46
  "build": "pkgroll --clean-dist",