@agenticmail/enterprise 0.5.481 → 0.5.483

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.
@@ -0,0 +1,209 @@
1
+ // src/agent-tools/common.ts
2
+ var ToolInputError = class extends Error {
3
+ status = 400;
4
+ constructor(message) {
5
+ super(message);
6
+ this.name = "ToolInputError";
7
+ }
8
+ };
9
+ function readStringParam(params, key, options = {}) {
10
+ var { required = false, trim = true, label = key, allowEmpty = false } = options;
11
+ var raw = params[key];
12
+ if (typeof raw !== "string") {
13
+ if (required) throw new ToolInputError(label + " required");
14
+ return void 0;
15
+ }
16
+ var value = trim ? raw.trim() : raw;
17
+ if (!value && !allowEmpty) {
18
+ if (required) throw new ToolInputError(label + " required");
19
+ return void 0;
20
+ }
21
+ return value;
22
+ }
23
+ function readNumberParam(params, key, options = {}) {
24
+ var { required = false, label = key, integer = false } = options;
25
+ var raw = params[key];
26
+ var value;
27
+ if (typeof raw === "number" && Number.isFinite(raw)) {
28
+ value = raw;
29
+ } else if (typeof raw === "string") {
30
+ var trimmed = raw.trim();
31
+ if (trimmed) {
32
+ var parsed = Number.parseFloat(trimmed);
33
+ if (Number.isFinite(parsed)) value = parsed;
34
+ }
35
+ }
36
+ if (value === void 0) {
37
+ if (required) throw new ToolInputError(label + " required");
38
+ return void 0;
39
+ }
40
+ return integer ? Math.trunc(value) : value;
41
+ }
42
+ function readBooleanParam(params, key, defaultValue = false) {
43
+ var raw = params[key];
44
+ if (typeof raw === "boolean") return raw;
45
+ if (raw === "true") return true;
46
+ if (raw === "false") return false;
47
+ return defaultValue;
48
+ }
49
+ function readStringArrayParam(params, key, options = {}) {
50
+ var { required = false, label = key } = options;
51
+ var raw = params[key];
52
+ if (Array.isArray(raw)) {
53
+ var values = raw.filter(function(entry) {
54
+ return typeof entry === "string";
55
+ }).map(function(entry) {
56
+ return entry.trim();
57
+ }).filter(Boolean);
58
+ if (values.length === 0) {
59
+ if (required) throw new ToolInputError(label + " required");
60
+ return void 0;
61
+ }
62
+ return values;
63
+ }
64
+ if (typeof raw === "string") {
65
+ var value = raw.trim();
66
+ if (!value) {
67
+ if (required) throw new ToolInputError(label + " required");
68
+ return void 0;
69
+ }
70
+ return [value];
71
+ }
72
+ if (required) throw new ToolInputError(label + " required");
73
+ return void 0;
74
+ }
75
+ function jsonResult(payload) {
76
+ return {
77
+ content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
78
+ details: payload
79
+ };
80
+ }
81
+ function textResult(text) {
82
+ return {
83
+ content: [{ type: "text", text }]
84
+ };
85
+ }
86
+ function errorResult(message) {
87
+ return {
88
+ content: [{ type: "text", text: "Error: " + message }],
89
+ details: { error: message }
90
+ };
91
+ }
92
+ function imageResult(params) {
93
+ return {
94
+ content: [
95
+ { type: "text", text: params.extraText ?? params.label },
96
+ { type: "image", data: params.base64, mimeType: params.mimeType }
97
+ ]
98
+ };
99
+ }
100
+ function wrapExternalContent(content, source) {
101
+ return '<external-content source="' + source + '">\n' + content + "\n</external-content>";
102
+ }
103
+ function wrapWebContent(content, source = "web") {
104
+ return '<web-content source="' + source + '" untrusted="true">\n' + content + "\n</web-content>";
105
+ }
106
+ function createActionGate(actions) {
107
+ return function(key, defaultValue) {
108
+ if (defaultValue === void 0) defaultValue = true;
109
+ var value = actions?.[key];
110
+ if (value === void 0) return defaultValue;
111
+ return value !== false;
112
+ };
113
+ }
114
+ function normalizeSecretInput(value) {
115
+ if (typeof value !== "string") return "";
116
+ var trimmed = value.trim();
117
+ if (!trimmed || trimmed === "undefined" || trimmed === "null") return "";
118
+ return trimmed;
119
+ }
120
+ var DEFAULT_REDACT_PATTERNS = [
121
+ "apikey",
122
+ "api_key",
123
+ "secret",
124
+ "password",
125
+ "passwd",
126
+ "token",
127
+ "credential",
128
+ "authorization",
129
+ "auth_token",
130
+ "access_key",
131
+ "private_key",
132
+ "client_secret"
133
+ ];
134
+ function redactSecrets(params, additionalKeys) {
135
+ var patterns = DEFAULT_REDACT_PATTERNS;
136
+ if (additionalKeys && additionalKeys.length > 0) {
137
+ patterns = patterns.concat(additionalKeys.map(function(k) {
138
+ return k.toLowerCase();
139
+ }));
140
+ }
141
+ var result = {};
142
+ for (var key of Object.keys(params)) {
143
+ var keyLower = key.toLowerCase();
144
+ var shouldRedact = patterns.some(function(p) {
145
+ return keyLower.includes(p);
146
+ });
147
+ if (shouldRedact && params[key] !== void 0 && params[key] !== null) {
148
+ result[key] = "[REDACTED]";
149
+ } else {
150
+ result[key] = params[key];
151
+ }
152
+ }
153
+ return result;
154
+ }
155
+ var MAX_IMAGE_BYTES = 15e4;
156
+ var MAX_IMAGE_SIDE = 1024;
157
+ async function imageResultFromFile(params) {
158
+ const fs = await import("fs/promises");
159
+ var buf = await fs.readFile(params.path);
160
+ const ext = params.path.split(".").pop()?.toLowerCase();
161
+ const mimeMap = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp", svg: "image/svg+xml" };
162
+ var mimeType = mimeMap[ext || ""] || "image/png";
163
+ if (buf.byteLength > MAX_IMAGE_BYTES && mimeType !== "image/svg+xml") {
164
+ try {
165
+ const sharp = (await import("sharp")).default;
166
+ var qualities = [75, 60, 45, 30];
167
+ var lastCompressed = null;
168
+ for (var q of qualities) {
169
+ lastCompressed = await sharp(buf).resize({ width: MAX_IMAGE_SIDE, height: MAX_IMAGE_SIDE, fit: "inside", withoutEnlargement: true }).jpeg({ quality: q, mozjpeg: true }).toBuffer();
170
+ if (lastCompressed.byteLength <= MAX_IMAGE_BYTES) {
171
+ buf = lastCompressed;
172
+ mimeType = "image/jpeg";
173
+ break;
174
+ }
175
+ }
176
+ if (buf.byteLength > MAX_IMAGE_BYTES && lastCompressed && lastCompressed.byteLength < buf.byteLength) {
177
+ buf = lastCompressed;
178
+ mimeType = "image/jpeg";
179
+ }
180
+ } catch {
181
+ }
182
+ }
183
+ return imageResult({
184
+ label: params.label,
185
+ path: params.path,
186
+ base64: buf.toString("base64"),
187
+ mimeType,
188
+ extraText: params.extraText,
189
+ details: params.details ? JSON.stringify(params.details) : void 0
190
+ });
191
+ }
192
+
193
+ export {
194
+ ToolInputError,
195
+ readStringParam,
196
+ readNumberParam,
197
+ readBooleanParam,
198
+ readStringArrayParam,
199
+ jsonResult,
200
+ textResult,
201
+ errorResult,
202
+ imageResult,
203
+ wrapExternalContent,
204
+ wrapWebContent,
205
+ createActionGate,
206
+ normalizeSecretInput,
207
+ redactSecrets,
208
+ imageResultFromFile
209
+ };