@dexto/core 1.16.5 → 1.17.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.
- package/dist/agent/schemas.d.ts +2 -2
- package/dist/llm/auth/index.d.ts +1 -1
- package/dist/llm/auth/index.d.ts.map +1 -1
- package/dist/llm/auth/types.d.ts +14 -0
- package/dist/llm/auth/types.d.ts.map +1 -1
- package/dist/llm/error-codes.cjs +2 -0
- package/dist/llm/error-codes.d.ts +2 -0
- package/dist/llm/error-codes.d.ts.map +1 -1
- package/dist/llm/error-codes.js +2 -0
- package/dist/llm/errors.cjs +27 -0
- package/dist/llm/errors.d.ts +9 -0
- package/dist/llm/errors.d.ts.map +1 -1
- package/dist/llm/errors.js +27 -0
- package/dist/llm/executor/tool-output-spill.cjs +46 -14
- package/dist/llm/executor/tool-output-spill.d.ts.map +1 -1
- package/dist/llm/executor/tool-output-spill.js +46 -14
- package/dist/llm/executor/turn-executor.cjs +3 -0
- package/dist/llm/executor/turn-executor.d.ts +5 -5
- package/dist/llm/executor/turn-executor.d.ts.map +1 -1
- package/dist/llm/executor/turn-executor.js +3 -0
- package/dist/llm/index.cjs +6 -0
- package/dist/llm/index.d.ts +3 -1
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +4 -0
- package/dist/llm/private-address.cjs +88 -0
- package/dist/llm/private-address.d.ts +3 -0
- package/dist/llm/private-address.d.ts.map +1 -0
- package/dist/llm/private-address.js +65 -0
- package/dist/llm/safe-download.cjs +159 -0
- package/dist/llm/safe-download.d.ts +21 -0
- package/dist/llm/safe-download.d.ts.map +1 -0
- package/dist/llm/safe-download.js +126 -0
- package/dist/llm/services/factory.cjs +204 -14
- package/dist/llm/services/factory.d.ts +1 -1
- package/dist/llm/services/factory.d.ts.map +1 -1
- package/dist/llm/services/factory.js +204 -14
- package/dist/logger/logger.cjs +8 -9
- package/dist/logger/logger.d.ts.map +1 -1
- package/dist/logger/logger.js +8 -9
- package/dist/logger/v2/dexto-logger.cjs +3 -2
- package/dist/logger/v2/dexto-logger.d.ts.map +1 -1
- package/dist/logger/v2/dexto-logger.js +3 -2
- package/dist/mcp/mcp-client.cjs +17 -8
- package/dist/mcp/mcp-client.d.ts +4 -2
- package/dist/mcp/mcp-client.d.ts.map +1 -1
- package/dist/mcp/mcp-client.js +17 -8
- package/dist/mcp/schemas.cjs +3 -0
- package/dist/mcp/schemas.d.ts +3 -0
- package/dist/mcp/schemas.d.ts.map +1 -1
- package/dist/mcp/schemas.js +3 -0
- package/dist/resources/handlers/filesystem-handler.cjs +7 -6
- package/dist/resources/handlers/filesystem-handler.d.ts.map +1 -1
- package/dist/resources/handlers/filesystem-handler.js +7 -6
- package/dist/systemPrompt/schemas.d.ts +3 -3
- package/dist/telemetry/decorators.cjs +2 -2
- package/dist/telemetry/decorators.js +2 -2
- package/dist/tools/tool-output-read.cjs +4 -3
- package/dist/tools/tool-output-read.d.ts.map +1 -1
- package/dist/tools/tool-output-read.js +4 -3
- package/dist/utils/index.cjs +3 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/metadata-address.cjs +138 -0
- package/dist/utils/metadata-address.d.ts +24 -0
- package/dist/utils/metadata-address.d.ts.map +1 -0
- package/dist/utils/metadata-address.js +113 -0
- package/dist/utils/path.cjs +0 -15
- package/dist/utils/path.d.ts +0 -6
- package/dist/utils/path.d.ts.map +1 -1
- package/dist/utils/path.js +0 -14
- package/dist/utils/redactor.cjs +242 -34
- package/dist/utils/redactor.d.ts +15 -0
- package/dist/utils/redactor.d.ts.map +1 -1
- package/dist/utils/redactor.js +238 -34
- package/dist/utils/text-boundary.cjs +32 -0
- package/dist/utils/text-boundary.d.ts +3 -0
- package/dist/utils/text-boundary.d.ts.map +1 -0
- package/dist/utils/text-boundary.js +9 -0
- package/dist/workspace/index.cjs +7 -2
- package/dist/workspace/index.d.ts +1 -0
- package/dist/workspace/index.d.ts.map +1 -1
- package/dist/workspace/index.js +4 -1
- package/dist/workspace/shell-environment.cjs +96 -0
- package/dist/workspace/shell-environment.d.ts +18 -0
- package/dist/workspace/shell-environment.d.ts.map +1 -0
- package/dist/workspace/shell-environment.js +72 -0
- package/dist/workspace/types.d.ts +9 -0
- package/dist/workspace/types.d.ts.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var private_address_exports = {};
|
|
20
|
+
__export(private_address_exports, {
|
|
21
|
+
isPrivateAddress: () => isPrivateAddress
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(private_address_exports);
|
|
24
|
+
var import_node_net = require("node:net");
|
|
25
|
+
function isPrivateIpv4Octets(a, b, c) {
|
|
26
|
+
if (a === 0) return true;
|
|
27
|
+
if (a === 10) return true;
|
|
28
|
+
if (a === 127) return true;
|
|
29
|
+
if (a === 169 && b === 254) return true;
|
|
30
|
+
if (a === 172 && b >= 16 && b <= 31) return true;
|
|
31
|
+
if (a === 192 && b === 168) return true;
|
|
32
|
+
if (a === 100 && b >= 64 && b <= 127) return true;
|
|
33
|
+
if (a === 192 && b === 0 && c === 0) return true;
|
|
34
|
+
if (a === 192 && b === 0 && c === 2) return true;
|
|
35
|
+
if (a === 198 && b === 51 && c === 100) return true;
|
|
36
|
+
if (a === 203 && b === 0 && c === 113) return true;
|
|
37
|
+
if (a === 198 && (b === 18 || b === 19)) return true;
|
|
38
|
+
if (a >= 224) return true;
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
function isPrivateIpv4(ip) {
|
|
42
|
+
const [a, b, c, d, ...rest] = ip.split(".").map((part) => Number(part));
|
|
43
|
+
if (a === void 0 || b === void 0 || c === void 0 || d === void 0) return false;
|
|
44
|
+
if (rest.length > 0 || [a, b, c, d].some((part) => Number.isNaN(part))) return false;
|
|
45
|
+
return isPrivateIpv4Octets(a, b, c);
|
|
46
|
+
}
|
|
47
|
+
function isPrivateEmbeddedIpv4(high, low) {
|
|
48
|
+
return isPrivateIpv4Octets(high >> 8, high & 255, low >> 8);
|
|
49
|
+
}
|
|
50
|
+
function ipv6Groups(ip) {
|
|
51
|
+
const canonical = new URL(`http://[${ip.split("%")[0]}]/`).hostname.slice(1, -1);
|
|
52
|
+
const [head = "", tail] = canonical.split("::");
|
|
53
|
+
const headGroups = head === "" ? [] : head.split(":");
|
|
54
|
+
const tailGroups = tail === void 0 || tail === "" ? [] : tail.split(":");
|
|
55
|
+
const missing = tail === void 0 ? 0 : 8 - headGroups.length - tailGroups.length;
|
|
56
|
+
return [...headGroups, ...Array(missing).fill("0"), ...tailGroups].map(
|
|
57
|
+
(group) => Number.parseInt(group, 16)
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
function isPrivateIpv6(ip) {
|
|
61
|
+
const [g0 = 0, g1 = 0, g2 = 0, g3 = 0, g4 = 0, g5 = 0, g6 = 0, g7 = 0] = ipv6Groups(ip);
|
|
62
|
+
const upperZero = g0 + g1 + g2 + g3 === 0;
|
|
63
|
+
if (upperZero && g4 + g5 + g6 === 0 && (g7 === 0 || g7 === 1)) return true;
|
|
64
|
+
if (upperZero && g4 === 0 && g5 === 65535) return isPrivateEmbeddedIpv4(g6, g7);
|
|
65
|
+
if (upperZero && g4 === 65535 && g5 === 0) return isPrivateEmbeddedIpv4(g6, g7);
|
|
66
|
+
if (upperZero && g4 === 0 && g5 === 0) return isPrivateEmbeddedIpv4(g6, g7);
|
|
67
|
+
if (g0 === 100 && g1 === 65435) {
|
|
68
|
+
if (g2 + g3 + g4 + g5 === 0) return isPrivateEmbeddedIpv4(g6, g7);
|
|
69
|
+
if (g2 === 1) return true;
|
|
70
|
+
}
|
|
71
|
+
if (g0 === 8194) return isPrivateEmbeddedIpv4(g1, g2);
|
|
72
|
+
if ((g0 & 65472) === 65152) return true;
|
|
73
|
+
if ((g0 & 65472) === 65216) return true;
|
|
74
|
+
if ((g0 & 65024) === 64512) return true;
|
|
75
|
+
if ((g0 & 65280) === 65280) return true;
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
function isPrivateAddress(ip) {
|
|
79
|
+
const address = ip.startsWith("[") && ip.endsWith("]") ? ip.slice(1, -1) : ip;
|
|
80
|
+
const version = (0, import_node_net.isIP)(address.split("%")[0] ?? address);
|
|
81
|
+
if (version === 4) return isPrivateIpv4(address);
|
|
82
|
+
if (version === 6) return isPrivateIpv6(address);
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
isPrivateAddress
|
|
88
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"private-address.d.ts","sourceRoot":"","sources":["../../src/llm/private-address.ts"],"names":[],"mappings":"AA2EA,qFAAqF;AACrF,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAMpD"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import "../chunk-C6A6W6XS.js";
|
|
2
|
+
import { isIP } from "node:net";
|
|
3
|
+
function isPrivateIpv4Octets(a, b, c) {
|
|
4
|
+
if (a === 0) return true;
|
|
5
|
+
if (a === 10) return true;
|
|
6
|
+
if (a === 127) return true;
|
|
7
|
+
if (a === 169 && b === 254) return true;
|
|
8
|
+
if (a === 172 && b >= 16 && b <= 31) return true;
|
|
9
|
+
if (a === 192 && b === 168) return true;
|
|
10
|
+
if (a === 100 && b >= 64 && b <= 127) return true;
|
|
11
|
+
if (a === 192 && b === 0 && c === 0) return true;
|
|
12
|
+
if (a === 192 && b === 0 && c === 2) return true;
|
|
13
|
+
if (a === 198 && b === 51 && c === 100) return true;
|
|
14
|
+
if (a === 203 && b === 0 && c === 113) return true;
|
|
15
|
+
if (a === 198 && (b === 18 || b === 19)) return true;
|
|
16
|
+
if (a >= 224) return true;
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
function isPrivateIpv4(ip) {
|
|
20
|
+
const [a, b, c, d, ...rest] = ip.split(".").map((part) => Number(part));
|
|
21
|
+
if (a === void 0 || b === void 0 || c === void 0 || d === void 0) return false;
|
|
22
|
+
if (rest.length > 0 || [a, b, c, d].some((part) => Number.isNaN(part))) return false;
|
|
23
|
+
return isPrivateIpv4Octets(a, b, c);
|
|
24
|
+
}
|
|
25
|
+
function isPrivateEmbeddedIpv4(high, low) {
|
|
26
|
+
return isPrivateIpv4Octets(high >> 8, high & 255, low >> 8);
|
|
27
|
+
}
|
|
28
|
+
function ipv6Groups(ip) {
|
|
29
|
+
const canonical = new URL(`http://[${ip.split("%")[0]}]/`).hostname.slice(1, -1);
|
|
30
|
+
const [head = "", tail] = canonical.split("::");
|
|
31
|
+
const headGroups = head === "" ? [] : head.split(":");
|
|
32
|
+
const tailGroups = tail === void 0 || tail === "" ? [] : tail.split(":");
|
|
33
|
+
const missing = tail === void 0 ? 0 : 8 - headGroups.length - tailGroups.length;
|
|
34
|
+
return [...headGroups, ...Array(missing).fill("0"), ...tailGroups].map(
|
|
35
|
+
(group) => Number.parseInt(group, 16)
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
function isPrivateIpv6(ip) {
|
|
39
|
+
const [g0 = 0, g1 = 0, g2 = 0, g3 = 0, g4 = 0, g5 = 0, g6 = 0, g7 = 0] = ipv6Groups(ip);
|
|
40
|
+
const upperZero = g0 + g1 + g2 + g3 === 0;
|
|
41
|
+
if (upperZero && g4 + g5 + g6 === 0 && (g7 === 0 || g7 === 1)) return true;
|
|
42
|
+
if (upperZero && g4 === 0 && g5 === 65535) return isPrivateEmbeddedIpv4(g6, g7);
|
|
43
|
+
if (upperZero && g4 === 65535 && g5 === 0) return isPrivateEmbeddedIpv4(g6, g7);
|
|
44
|
+
if (upperZero && g4 === 0 && g5 === 0) return isPrivateEmbeddedIpv4(g6, g7);
|
|
45
|
+
if (g0 === 100 && g1 === 65435) {
|
|
46
|
+
if (g2 + g3 + g4 + g5 === 0) return isPrivateEmbeddedIpv4(g6, g7);
|
|
47
|
+
if (g2 === 1) return true;
|
|
48
|
+
}
|
|
49
|
+
if (g0 === 8194) return isPrivateEmbeddedIpv4(g1, g2);
|
|
50
|
+
if ((g0 & 65472) === 65152) return true;
|
|
51
|
+
if ((g0 & 65472) === 65216) return true;
|
|
52
|
+
if ((g0 & 65024) === 64512) return true;
|
|
53
|
+
if ((g0 & 65280) === 65280) return true;
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
function isPrivateAddress(ip) {
|
|
57
|
+
const address = ip.startsWith("[") && ip.endsWith("]") ? ip.slice(1, -1) : ip;
|
|
58
|
+
const version = isIP(address.split("%")[0] ?? address);
|
|
59
|
+
if (version === 4) return isPrivateIpv4(address);
|
|
60
|
+
if (version === 6) return isPrivateIpv6(address);
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
export {
|
|
64
|
+
isPrivateAddress
|
|
65
|
+
};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var safe_download_exports = {};
|
|
30
|
+
__export(safe_download_exports, {
|
|
31
|
+
createSafeDownload: () => createSafeDownload
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(safe_download_exports);
|
|
34
|
+
var import_node_dns = require("node:dns");
|
|
35
|
+
var import_node_http = __toESM(require("node:http"), 1);
|
|
36
|
+
var import_node_https = __toESM(require("node:https"), 1);
|
|
37
|
+
var import_ai = require("ai");
|
|
38
|
+
var import_metadata_address = require("../utils/metadata-address.js");
|
|
39
|
+
var import_private_address = require("./private-address.js");
|
|
40
|
+
const MAX_DOWNLOAD_BYTES = 100 * 1024 * 1024;
|
|
41
|
+
const MAX_REDIRECTS = 10;
|
|
42
|
+
const DOWNLOAD_TIMEOUT_MS = 6e4;
|
|
43
|
+
const DOWNLOAD_HEADERS = { "user-agent": "@dexto/core", accept: "*/*" };
|
|
44
|
+
function blockedDownloadMessage(hostname) {
|
|
45
|
+
return `Download from ${hostname} is blocked`;
|
|
46
|
+
}
|
|
47
|
+
function createCheckedLookup(dnsLookup, isBlockedAddress) {
|
|
48
|
+
return (hostname, options, callback) => {
|
|
49
|
+
dnsLookup(hostname, { all: true, verbatim: true }).then(
|
|
50
|
+
(records) => {
|
|
51
|
+
const blocked = records.find(
|
|
52
|
+
(record) => (0, import_metadata_address.isMetadataHost)(record.address) || isBlockedAddress(record.address)
|
|
53
|
+
);
|
|
54
|
+
if (blocked) {
|
|
55
|
+
callback(new Error(blockedDownloadMessage(hostname)), []);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const [first] = records;
|
|
59
|
+
if (!first) {
|
|
60
|
+
callback(new Error(`Failed to resolve ${hostname}`), []);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (options.all) {
|
|
64
|
+
callback(null, records);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
callback(null, first.address, first.family);
|
|
68
|
+
},
|
|
69
|
+
(error) => callback(error, [])
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function requestOnce(url, lookup, signal) {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
const request = (url.protocol === "https:" ? import_node_https.default : import_node_http.default).request(
|
|
76
|
+
url,
|
|
77
|
+
{ method: "GET", lookup, signal, headers: DOWNLOAD_HEADERS },
|
|
78
|
+
resolve
|
|
79
|
+
);
|
|
80
|
+
request.on("error", reject);
|
|
81
|
+
request.end();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
async function readLimited(response, url) {
|
|
85
|
+
const chunks = [];
|
|
86
|
+
let total = 0;
|
|
87
|
+
for await (const chunk of response) {
|
|
88
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
89
|
+
total += buffer.length;
|
|
90
|
+
if (total > MAX_DOWNLOAD_BYTES) {
|
|
91
|
+
response.destroy();
|
|
92
|
+
throw new import_ai.DownloadError({
|
|
93
|
+
url,
|
|
94
|
+
message: `Download of ${url} exceeded maximum size of ${MAX_DOWNLOAD_BYTES} bytes.`
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
chunks.push(buffer);
|
|
98
|
+
}
|
|
99
|
+
return new Uint8Array(Buffer.concat(chunks));
|
|
100
|
+
}
|
|
101
|
+
function createSafeDownload(options = {}) {
|
|
102
|
+
const isBlockedAddress = options.isBlockedAddress ?? import_private_address.isPrivateAddress;
|
|
103
|
+
const lookup = createCheckedLookup(
|
|
104
|
+
options.dnsLookup ?? ((hostname, lookupOptions) => import_node_dns.promises.lookup(hostname, lookupOptions)),
|
|
105
|
+
isBlockedAddress
|
|
106
|
+
);
|
|
107
|
+
async function download(first) {
|
|
108
|
+
const signal = AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS);
|
|
109
|
+
let url = first;
|
|
110
|
+
for (let redirects = 0; redirects <= MAX_REDIRECTS; redirects++) {
|
|
111
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
112
|
+
throw new import_ai.DownloadError({
|
|
113
|
+
url: url.toString(),
|
|
114
|
+
message: `URL scheme must be http or https, got ${url.protocol}`
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if ((0, import_metadata_address.isMetadataHost)(url.hostname) || isBlockedAddress(url.hostname)) {
|
|
118
|
+
throw new import_ai.DownloadError({
|
|
119
|
+
url: url.toString(),
|
|
120
|
+
message: blockedDownloadMessage(url.hostname)
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const response = await requestOnce(url, lookup, signal).catch((cause) => {
|
|
124
|
+
throw new import_ai.DownloadError({ url: url.toString(), cause });
|
|
125
|
+
});
|
|
126
|
+
const location = response.headers.location;
|
|
127
|
+
const status = response.statusCode ?? 0;
|
|
128
|
+
if (status >= 300 && status < 400 && location) {
|
|
129
|
+
response.resume();
|
|
130
|
+
url = new URL(location, url);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (status < 200 || status >= 300) {
|
|
134
|
+
response.resume();
|
|
135
|
+
throw new import_ai.DownloadError({
|
|
136
|
+
url: url.toString(),
|
|
137
|
+
statusCode: status,
|
|
138
|
+
statusText: response.statusMessage ?? ""
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
const data = await readLimited(response, url.toString());
|
|
142
|
+
const contentType = response.headers["content-type"]?.split(";")[0]?.trim();
|
|
143
|
+
return { data, mediaType: contentType || void 0 };
|
|
144
|
+
}
|
|
145
|
+
throw new import_ai.DownloadError({
|
|
146
|
+
url: first.toString(),
|
|
147
|
+
message: `Too many redirects (max ${MAX_REDIRECTS})`
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return (requests) => Promise.all(
|
|
151
|
+
requests.map(
|
|
152
|
+
({ url, isUrlSupportedByModel }) => isUrlSupportedByModel ? null : download(url)
|
|
153
|
+
)
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
157
|
+
0 && (module.exports = {
|
|
158
|
+
createSafeDownload
|
|
159
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type LookupAddress } from 'node:dns';
|
|
2
|
+
import { type Experimental_DownloadFunction as DownloadFunction } from 'ai';
|
|
3
|
+
type DnsLookup = (hostname: string, options: {
|
|
4
|
+
all: true;
|
|
5
|
+
verbatim: true;
|
|
6
|
+
}) => Promise<LookupAddress[]>;
|
|
7
|
+
export interface SafeDownloadOptions {
|
|
8
|
+
/** Resolves a host name to every address it has. Replaced in tests. */
|
|
9
|
+
dnsLookup?: DnsLookup;
|
|
10
|
+
/** Decides which resolved addresses are refused. Replaced in tests. */
|
|
11
|
+
isBlockedAddress?: (address: string) => boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The AI SDK downloads the URLs of images and files in a prompt itself, and its own check looks
|
|
15
|
+
* at the host name text only, so a name that resolves to a private address is fetched. This
|
|
16
|
+
* downloader resolves the host, refuses private answers, connects to the address it checked and
|
|
17
|
+
* checks every redirect the same way. URLs the model fetches itself are left to the model.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createSafeDownload(options?: SafeDownloadOptions): DownloadFunction;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=safe-download.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safe-download.d.ts","sourceRoot":"","sources":["../../src/llm/safe-download.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,aAAa,EAAsB,MAAM,UAAU,CAAC;AAGnF,OAAO,EAAiB,KAAK,6BAA6B,IAAI,gBAAgB,EAAE,MAAM,IAAI,CAAC;AAU3F,KAAK,SAAS,GAAG,CACb,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE;IAAE,GAAG,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,IAAI,CAAA;CAAE,KACrC,OAAO,CAAC,aAAa,EAAE,CAAC,CAAC;AAE9B,MAAM,WAAW,mBAAmB;IAChC,uEAAuE;IACvE,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,uEAAuE;IACvE,gBAAgB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC;CACnD;AAuFD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,mBAAwB,GAAG,gBAAgB,CA0DtF"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import "../chunk-C6A6W6XS.js";
|
|
2
|
+
import { promises as dns } from "node:dns";
|
|
3
|
+
import http from "node:http";
|
|
4
|
+
import https from "node:https";
|
|
5
|
+
import { DownloadError } from "ai";
|
|
6
|
+
import { isMetadataHost } from "../utils/metadata-address.js";
|
|
7
|
+
import { isPrivateAddress } from "./private-address.js";
|
|
8
|
+
const MAX_DOWNLOAD_BYTES = 100 * 1024 * 1024;
|
|
9
|
+
const MAX_REDIRECTS = 10;
|
|
10
|
+
const DOWNLOAD_TIMEOUT_MS = 6e4;
|
|
11
|
+
const DOWNLOAD_HEADERS = { "user-agent": "@dexto/core", accept: "*/*" };
|
|
12
|
+
function blockedDownloadMessage(hostname) {
|
|
13
|
+
return `Download from ${hostname} is blocked`;
|
|
14
|
+
}
|
|
15
|
+
function createCheckedLookup(dnsLookup, isBlockedAddress) {
|
|
16
|
+
return (hostname, options, callback) => {
|
|
17
|
+
dnsLookup(hostname, { all: true, verbatim: true }).then(
|
|
18
|
+
(records) => {
|
|
19
|
+
const blocked = records.find(
|
|
20
|
+
(record) => isMetadataHost(record.address) || isBlockedAddress(record.address)
|
|
21
|
+
);
|
|
22
|
+
if (blocked) {
|
|
23
|
+
callback(new Error(blockedDownloadMessage(hostname)), []);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const [first] = records;
|
|
27
|
+
if (!first) {
|
|
28
|
+
callback(new Error(`Failed to resolve ${hostname}`), []);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (options.all) {
|
|
32
|
+
callback(null, records);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
callback(null, first.address, first.family);
|
|
36
|
+
},
|
|
37
|
+
(error) => callback(error, [])
|
|
38
|
+
);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function requestOnce(url, lookup, signal) {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const request = (url.protocol === "https:" ? https : http).request(
|
|
44
|
+
url,
|
|
45
|
+
{ method: "GET", lookup, signal, headers: DOWNLOAD_HEADERS },
|
|
46
|
+
resolve
|
|
47
|
+
);
|
|
48
|
+
request.on("error", reject);
|
|
49
|
+
request.end();
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async function readLimited(response, url) {
|
|
53
|
+
const chunks = [];
|
|
54
|
+
let total = 0;
|
|
55
|
+
for await (const chunk of response) {
|
|
56
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
57
|
+
total += buffer.length;
|
|
58
|
+
if (total > MAX_DOWNLOAD_BYTES) {
|
|
59
|
+
response.destroy();
|
|
60
|
+
throw new DownloadError({
|
|
61
|
+
url,
|
|
62
|
+
message: `Download of ${url} exceeded maximum size of ${MAX_DOWNLOAD_BYTES} bytes.`
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
chunks.push(buffer);
|
|
66
|
+
}
|
|
67
|
+
return new Uint8Array(Buffer.concat(chunks));
|
|
68
|
+
}
|
|
69
|
+
function createSafeDownload(options = {}) {
|
|
70
|
+
const isBlockedAddress = options.isBlockedAddress ?? isPrivateAddress;
|
|
71
|
+
const lookup = createCheckedLookup(
|
|
72
|
+
options.dnsLookup ?? ((hostname, lookupOptions) => dns.lookup(hostname, lookupOptions)),
|
|
73
|
+
isBlockedAddress
|
|
74
|
+
);
|
|
75
|
+
async function download(first) {
|
|
76
|
+
const signal = AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS);
|
|
77
|
+
let url = first;
|
|
78
|
+
for (let redirects = 0; redirects <= MAX_REDIRECTS; redirects++) {
|
|
79
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
80
|
+
throw new DownloadError({
|
|
81
|
+
url: url.toString(),
|
|
82
|
+
message: `URL scheme must be http or https, got ${url.protocol}`
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
if (isMetadataHost(url.hostname) || isBlockedAddress(url.hostname)) {
|
|
86
|
+
throw new DownloadError({
|
|
87
|
+
url: url.toString(),
|
|
88
|
+
message: blockedDownloadMessage(url.hostname)
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const response = await requestOnce(url, lookup, signal).catch((cause) => {
|
|
92
|
+
throw new DownloadError({ url: url.toString(), cause });
|
|
93
|
+
});
|
|
94
|
+
const location = response.headers.location;
|
|
95
|
+
const status = response.statusCode ?? 0;
|
|
96
|
+
if (status >= 300 && status < 400 && location) {
|
|
97
|
+
response.resume();
|
|
98
|
+
url = new URL(location, url);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (status < 200 || status >= 300) {
|
|
102
|
+
response.resume();
|
|
103
|
+
throw new DownloadError({
|
|
104
|
+
url: url.toString(),
|
|
105
|
+
statusCode: status,
|
|
106
|
+
statusText: response.statusMessage ?? ""
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
const data = await readLimited(response, url.toString());
|
|
110
|
+
const contentType = response.headers["content-type"]?.split(";")[0]?.trim();
|
|
111
|
+
return { data, mediaType: contentType || void 0 };
|
|
112
|
+
}
|
|
113
|
+
throw new DownloadError({
|
|
114
|
+
url: first.toString(),
|
|
115
|
+
message: `Too many redirects (max ${MAX_REDIRECTS})`
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return (requests) => Promise.all(
|
|
119
|
+
requests.map(
|
|
120
|
+
({ url, isUrlSupportedByModel }) => isUrlSupportedByModel ? null : download(url)
|
|
121
|
+
)
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
export {
|
|
125
|
+
createSafeDownload
|
|
126
|
+
};
|