@ai-matrx/api 0.0.0-stage → 0.1.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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +18 -2
- package/dist/account.cjs +670 -0
- package/dist/account.d.cts +3246 -0
- package/dist/account.d.ts +3246 -0
- package/dist/account.js +558 -0
- package/dist/agents.cjs +598 -0
- package/dist/agents.d.cts +3417 -0
- package/dist/agents.d.ts +3417 -0
- package/dist/agents.js +497 -0
- package/dist/ai.cjs +109 -0
- package/dist/ai.d.cts +493 -0
- package/dist/ai.d.ts +493 -0
- package/dist/ai.js +91 -0
- package/dist/apps.cjs +394 -0
- package/dist/apps.d.cts +1094 -0
- package/dist/apps.d.ts +1094 -0
- package/dist/apps.js +328 -0
- package/dist/architecture.cjs +1223 -0
- package/dist/architecture.d.cts +4701 -0
- package/dist/architecture.d.ts +4701 -0
- package/dist/architecture.js +1016 -0
- package/dist/audio.cjs +226 -0
- package/dist/audio.d.cts +723 -0
- package/dist/audio.d.ts +723 -0
- package/dist/audio.js +188 -0
- package/dist/chat.cjs +538 -0
- package/dist/chat.d.cts +2251 -0
- package/dist/chat.d.ts +2251 -0
- package/dist/chat.js +447 -0
- package/dist/chunk-2SXAAKGV.cjs +279 -0
- package/dist/chunk-AY5N6EA4.js +43 -0
- package/dist/chunk-BMQI7UET.cjs +48 -0
- package/dist/chunk-FNIYADWI.cjs +59 -0
- package/dist/chunk-KIEAFMKB.js +264 -0
- package/dist/chunk-UONBYRE4.js +1012 -0
- package/dist/chunk-YTEOFGFC.js +57 -0
- package/dist/chunk-YUDQWDCM.cjs +1047 -0
- package/dist/code.cjs +561 -0
- package/dist/code.d.cts +1976 -0
- package/dist/code.d.ts +1976 -0
- package/dist/code.js +467 -0
- package/dist/commerce.cjs +22 -0
- package/dist/commerce.d.cts +45 -0
- package/dist/commerce.d.ts +45 -0
- package/dist/commerce.js +18 -0
- package/dist/communications.cjs +215 -0
- package/dist/communications.d.cts +948 -0
- package/dist/communications.d.ts +948 -0
- package/dist/communications.js +179 -0
- package/dist/computer.cjs +330 -0
- package/dist/computer.d.cts +733 -0
- package/dist/computer.d.ts +733 -0
- package/dist/computer.js +275 -0
- package/dist/content.cjs +425 -0
- package/dist/content.d.cts +1796 -0
- package/dist/content.d.ts +1796 -0
- package/dist/content.js +354 -0
- package/dist/credentials.cjs +22 -0
- package/dist/credentials.d.cts +82 -0
- package/dist/credentials.d.ts +82 -0
- package/dist/credentials.js +1 -0
- package/dist/crm.cjs +271 -0
- package/dist/crm.d.cts +1731 -0
- package/dist/crm.d.ts +1731 -0
- package/dist/crm.js +226 -0
- package/dist/data.cjs +382 -0
- package/dist/data.d.cts +1860 -0
- package/dist/data.d.ts +1860 -0
- package/dist/data.js +318 -0
- package/dist/education.cjs +28 -0
- package/dist/education.d.cts +73 -0
- package/dist/education.d.ts +73 -0
- package/dist/education.js +23 -0
- package/dist/files.cjs +1330 -0
- package/dist/files.d.cts +4956 -0
- package/dist/files.d.ts +4956 -0
- package/dist/files.js +1104 -0
- package/dist/human-resources.cjs +178 -0
- package/dist/human-resources.d.cts +1208 -0
- package/dist/human-resources.d.ts +1208 -0
- package/dist/human-resources.js +148 -0
- package/dist/index-C2E-Cp19.d.ts +385 -0
- package/dist/index-wC2vtb70.d.cts +385 -0
- package/dist/index.cjs +1934 -0
- package/dist/index.d.cts +2006 -0
- package/dist/index.d.ts +2006 -0
- package/dist/index.js +1668 -0
- package/dist/integrations.cjs +5116 -0
- package/dist/integrations.d.cts +19213 -0
- package/dist/integrations.d.ts +19213 -0
- package/dist/integrations.js +4263 -0
- package/dist/intelligence.cjs +1305 -0
- package/dist/intelligence.d.cts +4658 -0
- package/dist/intelligence.d.ts +4658 -0
- package/dist/intelligence.js +1196 -0
- package/dist/knowledge.cjs +1113 -0
- package/dist/knowledge.d.cts +5427 -0
- package/dist/knowledge.d.ts +5427 -0
- package/dist/knowledge.js +921 -0
- package/dist/legal.cjs +225 -0
- package/dist/legal.d.cts +1038 -0
- package/dist/legal.d.ts +1038 -0
- package/dist/legal.js +187 -0
- package/dist/marketing.cjs +1155 -0
- package/dist/marketing.d.cts +7741 -0
- package/dist/marketing.d.ts +7741 -0
- package/dist/marketing.js +951 -0
- package/dist/masterwork.cjs +283 -0
- package/dist/masterwork.d.cts +1938 -0
- package/dist/masterwork.d.ts +1938 -0
- package/dist/masterwork.js +231 -0
- package/dist/media.cjs +610 -0
- package/dist/media.d.cts +2270 -0
- package/dist/media.d.ts +2270 -0
- package/dist/media.js +507 -0
- package/dist/net.cjs +67 -0
- package/dist/net.d.cts +204 -0
- package/dist/net.d.ts +204 -0
- package/dist/net.js +2 -0
- package/dist/platform.cjs +150 -0
- package/dist/platform.d.cts +572 -0
- package/dist/platform.d.ts +572 -0
- package/dist/platform.js +125 -0
- package/dist/publish.cjs +248 -0
- package/dist/publish.d.cts +1431 -0
- package/dist/publish.d.ts +1431 -0
- package/dist/publish.js +206 -0
- package/dist/schemas.cjs +2 -0
- package/dist/schemas.d.cts +1353 -0
- package/dist/schemas.d.ts +1353 -0
- package/dist/schemas.js +1 -0
- package/dist/stream.cjs +44 -0
- package/dist/stream.d.cts +2 -0
- package/dist/stream.d.ts +2 -0
- package/dist/stream.js +3 -0
- package/dist/web.cjs +1045 -0
- package/dist/web.d.cts +4186 -0
- package/dist/web.d.ts +4186 -0
- package/dist/web.js +867 -0
- package/dist/workflows.cjs +825 -0
- package/dist/workflows.d.cts +4699 -0
- package/dist/workflows.d.ts +4699 -0
- package/dist/workflows.js +686 -0
- package/package.json +418 -4
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/net/errors.ts
|
|
4
|
+
var NetError = class extends Error {
|
|
5
|
+
code;
|
|
6
|
+
cause;
|
|
7
|
+
status;
|
|
8
|
+
retryable;
|
|
9
|
+
constructor(code, message, opts = {}) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = "NetError";
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.cause = opts.cause;
|
|
14
|
+
if (opts.status !== void 0) this.status = opts.status;
|
|
15
|
+
this.retryable = opts.retryable ?? false;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var ConnectTimeoutError = class extends NetError {
|
|
19
|
+
constructor(ms, cause) {
|
|
20
|
+
super("connect-timeout", `Connection timed out after ${ms}ms`, {
|
|
21
|
+
cause,
|
|
22
|
+
retryable: true
|
|
23
|
+
});
|
|
24
|
+
this.name = "ConnectTimeoutError";
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var TotalTimeoutError = class extends NetError {
|
|
28
|
+
constructor(ms, cause) {
|
|
29
|
+
super("total-timeout", `Request exceeded ${ms}ms total budget`, {
|
|
30
|
+
cause,
|
|
31
|
+
retryable: false
|
|
32
|
+
});
|
|
33
|
+
this.name = "TotalTimeoutError";
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var HeartbeatTimeoutError = class extends NetError {
|
|
37
|
+
constructor(ms, cause) {
|
|
38
|
+
super(
|
|
39
|
+
"heartbeat-timeout",
|
|
40
|
+
`No server activity for ${ms}ms \u2014 stream considered dead`,
|
|
41
|
+
{ cause, retryable: false }
|
|
42
|
+
);
|
|
43
|
+
this.name = "HeartbeatTimeoutError";
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var NetworkError = class extends NetError {
|
|
47
|
+
constructor(message, cause) {
|
|
48
|
+
super("network", message, { cause, retryable: true });
|
|
49
|
+
this.name = "NetworkError";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var HttpError = class extends NetError {
|
|
53
|
+
constructor(status, message, cause) {
|
|
54
|
+
const retryable = status === 408 || status === 429 || status >= 500;
|
|
55
|
+
super("http", message, { cause, status, retryable });
|
|
56
|
+
this.name = "HttpError";
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var AbortedError = class extends NetError {
|
|
60
|
+
constructor(cause) {
|
|
61
|
+
super("aborted", "Request was aborted", { cause, retryable: false });
|
|
62
|
+
this.name = "AbortedError";
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var OfflineError = class extends NetError {
|
|
66
|
+
constructor() {
|
|
67
|
+
super("offline", "Browser is offline", { retryable: true });
|
|
68
|
+
this.name = "OfflineError";
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
function isNetError(err) {
|
|
72
|
+
return err instanceof NetError;
|
|
73
|
+
}
|
|
74
|
+
function extractErrorMessage(err, fallback = "An unexpected error occurred") {
|
|
75
|
+
if (!err) return "Unknown error";
|
|
76
|
+
if (typeof err === "string") return err;
|
|
77
|
+
if (err instanceof Error) return err.message;
|
|
78
|
+
if (typeof err === "object") {
|
|
79
|
+
const e = err;
|
|
80
|
+
const parts = [];
|
|
81
|
+
if (typeof e.message === "string" && e.message) parts.push(e.message);
|
|
82
|
+
if (parts.length === 0) {
|
|
83
|
+
if (typeof e.detail === "string" && e.detail) parts.push(e.detail);
|
|
84
|
+
else if (e.detail && typeof e.detail === "object") {
|
|
85
|
+
const d = e.detail;
|
|
86
|
+
if (typeof d.message === "string" && d.message) parts.push(d.message);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (typeof e.details === "string" && e.details) parts.push(e.details);
|
|
90
|
+
if (typeof e.hint === "string" && e.hint) parts.push(`Hint: ${e.hint}`);
|
|
91
|
+
if (typeof e.code === "string" && e.code) parts.push(`Code: ${e.code}`);
|
|
92
|
+
if (parts.length > 0) return parts.join(" \xB7 ");
|
|
93
|
+
const nested = extractNestedErrorMessage(e, 0);
|
|
94
|
+
if (nested !== null) return nested;
|
|
95
|
+
try {
|
|
96
|
+
const s = JSON.stringify(err);
|
|
97
|
+
if (s && s !== "{}") return s;
|
|
98
|
+
} catch {
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return fallback;
|
|
102
|
+
}
|
|
103
|
+
function extractNestedErrorMessage(e, depth) {
|
|
104
|
+
if (depth > 4) return null;
|
|
105
|
+
for (const key of ["error", "exception", "cause"]) {
|
|
106
|
+
const value = e[key];
|
|
107
|
+
if (typeof value === "string" && value) return value;
|
|
108
|
+
if (value && typeof value === "object") {
|
|
109
|
+
const inner = value;
|
|
110
|
+
if (typeof inner.message === "string" && inner.message) return inner.message;
|
|
111
|
+
if (typeof inner.detail === "string" && inner.detail) return inner.detail;
|
|
112
|
+
const deeper = extractNestedErrorMessage(inner, depth + 1);
|
|
113
|
+
if (deeper !== null) return deeper;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
function isTransportFailure(err) {
|
|
119
|
+
if (err instanceof NetworkError || err instanceof OfflineError) return true;
|
|
120
|
+
if (typeof navigator !== "undefined" && navigator.onLine === false) return true;
|
|
121
|
+
if (err instanceof TypeError) return true;
|
|
122
|
+
if (err && typeof err === "object") {
|
|
123
|
+
const e = err;
|
|
124
|
+
if (e.code || e.status) return false;
|
|
125
|
+
const text = `${typeof e.name === "string" ? e.name : ""} ${typeof e.message === "string" ? e.message : ""}`.toLowerCase();
|
|
126
|
+
return text.includes("failed to fetch") || text.includes("networkerror") || text.includes("network request failed") || text.includes("load failed") || // Safari's wording for the same condition
|
|
127
|
+
text.includes("upstream connect error or disconnect/reset before headers");
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
function toNetError(err) {
|
|
132
|
+
if (isNetError(err)) return err;
|
|
133
|
+
if (err instanceof DOMException && err.name === "AbortError") {
|
|
134
|
+
return new AbortedError(err);
|
|
135
|
+
}
|
|
136
|
+
if (err instanceof TypeError) {
|
|
137
|
+
return new NetworkError(err.message || "Network request failed", err);
|
|
138
|
+
}
|
|
139
|
+
return new NetError(
|
|
140
|
+
"unknown",
|
|
141
|
+
extractErrorMessage(err),
|
|
142
|
+
{ cause: err }
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// src/net/resilient-fetch.ts
|
|
147
|
+
var DEFAULTS = {
|
|
148
|
+
connectTimeoutMs: 15e3,
|
|
149
|
+
totalTimeoutMs: 12e4
|
|
150
|
+
};
|
|
151
|
+
function linkAbort(target, signal) {
|
|
152
|
+
if (signal.aborted) {
|
|
153
|
+
target.abort(signal.reason);
|
|
154
|
+
return () => {
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const onAbort = () => target.abort(signal.reason);
|
|
158
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
159
|
+
return () => signal.removeEventListener("abort", onAbort);
|
|
160
|
+
}
|
|
161
|
+
async function resilientFetch(url, init = {}, opts = {}) {
|
|
162
|
+
const connectTimeoutMs = opts.connectTimeoutMs ?? DEFAULTS.connectTimeoutMs;
|
|
163
|
+
const totalTimeoutMs = opts.totalTimeoutMs === void 0 ? DEFAULTS.totalTimeoutMs : opts.totalTimeoutMs;
|
|
164
|
+
const throwOnHttpError = opts.throwOnHttpError ?? true;
|
|
165
|
+
const controller = new AbortController();
|
|
166
|
+
const unlinkCaller = opts.signal ? linkAbort(controller, opts.signal) : () => {
|
|
167
|
+
};
|
|
168
|
+
let connectTimedOut = false;
|
|
169
|
+
let totalTimedOut = false;
|
|
170
|
+
const connectTimer = setTimeout(() => {
|
|
171
|
+
connectTimedOut = true;
|
|
172
|
+
controller.abort("connect-timeout");
|
|
173
|
+
}, connectTimeoutMs);
|
|
174
|
+
const totalTimer = totalTimeoutMs !== null ? setTimeout(() => {
|
|
175
|
+
totalTimedOut = true;
|
|
176
|
+
controller.abort("total-timeout");
|
|
177
|
+
}, totalTimeoutMs) : null;
|
|
178
|
+
try {
|
|
179
|
+
const response = await fetch(url, { ...init, signal: controller.signal });
|
|
180
|
+
clearTimeout(connectTimer);
|
|
181
|
+
if (totalTimer !== null) clearTimeout(totalTimer);
|
|
182
|
+
if (throwOnHttpError && !response.ok) {
|
|
183
|
+
throw new HttpError(
|
|
184
|
+
response.status,
|
|
185
|
+
`HTTP ${response.status} ${response.statusText}`
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
return { response, controller };
|
|
189
|
+
} catch (err) {
|
|
190
|
+
clearTimeout(connectTimer);
|
|
191
|
+
if (totalTimer !== null) clearTimeout(totalTimer);
|
|
192
|
+
if (connectTimedOut) throw new ConnectTimeoutError(connectTimeoutMs, err);
|
|
193
|
+
if (totalTimedOut)
|
|
194
|
+
throw new TotalTimeoutError(totalTimeoutMs, err);
|
|
195
|
+
if (err instanceof DOMException && err.name === "AbortError") {
|
|
196
|
+
throw new AbortedError(err);
|
|
197
|
+
}
|
|
198
|
+
if (opts.signal?.aborted) {
|
|
199
|
+
throw new AbortedError(err);
|
|
200
|
+
}
|
|
201
|
+
if (err instanceof TypeError) {
|
|
202
|
+
if (typeof navigator !== "undefined" && navigator.onLine === false) {
|
|
203
|
+
throw new OfflineError();
|
|
204
|
+
}
|
|
205
|
+
throw new NetworkError(err.message || "Network request failed", err);
|
|
206
|
+
}
|
|
207
|
+
if (err instanceof HttpError) throw err;
|
|
208
|
+
throw new NetworkError(extractErrorMessage(err), err);
|
|
209
|
+
} finally {
|
|
210
|
+
unlinkCaller();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// src/net/retry.ts
|
|
215
|
+
function defaultRetryOn(err) {
|
|
216
|
+
return isNetError(err) && err.retryable;
|
|
217
|
+
}
|
|
218
|
+
function sleep(ms, signal) {
|
|
219
|
+
return new Promise((resolve, reject) => {
|
|
220
|
+
const timer = setTimeout(() => {
|
|
221
|
+
resolve();
|
|
222
|
+
if (signal) signal.removeEventListener("abort", onAbort);
|
|
223
|
+
}, ms);
|
|
224
|
+
const onAbort = () => {
|
|
225
|
+
clearTimeout(timer);
|
|
226
|
+
reject(signal?.reason ?? new Error("Aborted"));
|
|
227
|
+
};
|
|
228
|
+
if (signal) {
|
|
229
|
+
if (signal.aborted) {
|
|
230
|
+
clearTimeout(timer);
|
|
231
|
+
reject(signal.reason);
|
|
232
|
+
} else {
|
|
233
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
async function withRetry(fn, opts = {}) {
|
|
239
|
+
const attempts = Math.max(1, opts.attempts ?? 3);
|
|
240
|
+
const initialDelayMs = opts.initialDelayMs ?? 250;
|
|
241
|
+
const maxDelayMs = opts.maxDelayMs ?? 4e3;
|
|
242
|
+
const factor = opts.backoffFactor ?? 2;
|
|
243
|
+
const jitter = Math.max(0, Math.min(1, opts.jitter ?? 0.25));
|
|
244
|
+
const retryOn = opts.retryOn ?? defaultRetryOn;
|
|
245
|
+
let lastErr;
|
|
246
|
+
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
247
|
+
if (opts.signal?.aborted) throw opts.signal.reason;
|
|
248
|
+
try {
|
|
249
|
+
return await fn(attempt);
|
|
250
|
+
} catch (err) {
|
|
251
|
+
lastErr = err;
|
|
252
|
+
if (attempt === attempts || !retryOn(err)) throw err;
|
|
253
|
+
const base = Math.min(
|
|
254
|
+
maxDelayMs,
|
|
255
|
+
initialDelayMs * Math.pow(factor, attempt - 1)
|
|
256
|
+
);
|
|
257
|
+
const jitterAmount = base * jitter * (Math.random() * 2 - 1);
|
|
258
|
+
const delayMs = Math.max(0, Math.round(base + jitterAmount));
|
|
259
|
+
opts.onRetry?.(attempt, err, delayMs);
|
|
260
|
+
await sleep(delayMs, opts.signal);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
throw lastErr;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
exports.AbortedError = AbortedError;
|
|
267
|
+
exports.ConnectTimeoutError = ConnectTimeoutError;
|
|
268
|
+
exports.HeartbeatTimeoutError = HeartbeatTimeoutError;
|
|
269
|
+
exports.HttpError = HttpError;
|
|
270
|
+
exports.NetError = NetError;
|
|
271
|
+
exports.NetworkError = NetworkError;
|
|
272
|
+
exports.OfflineError = OfflineError;
|
|
273
|
+
exports.TotalTimeoutError = TotalTimeoutError;
|
|
274
|
+
exports.extractErrorMessage = extractErrorMessage;
|
|
275
|
+
exports.isNetError = isNetError;
|
|
276
|
+
exports.isTransportFailure = isTransportFailure;
|
|
277
|
+
exports.resilientFetch = resilientFetch;
|
|
278
|
+
exports.toNetError = toNetError;
|
|
279
|
+
exports.withRetry = withRetry;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/credentials.ts
|
|
2
|
+
var MATRX_HEADERS = {
|
|
3
|
+
authorization: "Authorization",
|
|
4
|
+
fingerprint: "X-Fingerprint-ID",
|
|
5
|
+
organization: "X-Organization-Id",
|
|
6
|
+
actingUser: "X-Matrx-User-Id",
|
|
7
|
+
requestId: "X-Request-Id"
|
|
8
|
+
};
|
|
9
|
+
var API_KEY_PREFIX = "mx_";
|
|
10
|
+
function credentialToHeaders(credential) {
|
|
11
|
+
if (credential === null) return {};
|
|
12
|
+
switch (credential.kind) {
|
|
13
|
+
case "user":
|
|
14
|
+
return { [MATRX_HEADERS.authorization]: `Bearer ${credential.accessToken}` };
|
|
15
|
+
case "guest":
|
|
16
|
+
return { [MATRX_HEADERS.fingerprint]: credential.fingerprintId };
|
|
17
|
+
case "scoped":
|
|
18
|
+
return { [MATRX_HEADERS.authorization]: `Bearer ${credential.token}` };
|
|
19
|
+
case "service": {
|
|
20
|
+
const header = credential.secretHeader ?? MATRX_HEADERS.authorization;
|
|
21
|
+
const headers = header.toLowerCase() === "authorization" ? { [MATRX_HEADERS.authorization]: `Bearer ${credential.secret}` } : { [header]: credential.secret };
|
|
22
|
+
if (credential.actingUserId) headers[MATRX_HEADERS.actingUser] = credential.actingUserId;
|
|
23
|
+
return headers;
|
|
24
|
+
}
|
|
25
|
+
case "api_key": {
|
|
26
|
+
if (!credential.apiKey.startsWith(API_KEY_PREFIX)) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`credentialToHeaders: an api_key credential must start with "${API_KEY_PREFIX}" (AI Matrx API keys look like mx_live_...)`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
return { [MATRX_HEADERS.authorization]: `Bearer ${credential.apiKey}` };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function toCredentialsPort(setting) {
|
|
36
|
+
if (setting === void 0 || setting === null) return { get: async () => null };
|
|
37
|
+
if (typeof setting === "function") return { get: async () => setting() };
|
|
38
|
+
if ("get" in setting && typeof setting.get === "function") return setting;
|
|
39
|
+
const fixed = setting;
|
|
40
|
+
return { get: async () => fixed };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { API_KEY_PREFIX, MATRX_HEADERS, credentialToHeaders, toCredentialsPort };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/credentials.ts
|
|
4
|
+
var MATRX_HEADERS = {
|
|
5
|
+
authorization: "Authorization",
|
|
6
|
+
fingerprint: "X-Fingerprint-ID",
|
|
7
|
+
organization: "X-Organization-Id",
|
|
8
|
+
actingUser: "X-Matrx-User-Id",
|
|
9
|
+
requestId: "X-Request-Id"
|
|
10
|
+
};
|
|
11
|
+
var API_KEY_PREFIX = "mx_";
|
|
12
|
+
function credentialToHeaders(credential) {
|
|
13
|
+
if (credential === null) return {};
|
|
14
|
+
switch (credential.kind) {
|
|
15
|
+
case "user":
|
|
16
|
+
return { [MATRX_HEADERS.authorization]: `Bearer ${credential.accessToken}` };
|
|
17
|
+
case "guest":
|
|
18
|
+
return { [MATRX_HEADERS.fingerprint]: credential.fingerprintId };
|
|
19
|
+
case "scoped":
|
|
20
|
+
return { [MATRX_HEADERS.authorization]: `Bearer ${credential.token}` };
|
|
21
|
+
case "service": {
|
|
22
|
+
const header = credential.secretHeader ?? MATRX_HEADERS.authorization;
|
|
23
|
+
const headers = header.toLowerCase() === "authorization" ? { [MATRX_HEADERS.authorization]: `Bearer ${credential.secret}` } : { [header]: credential.secret };
|
|
24
|
+
if (credential.actingUserId) headers[MATRX_HEADERS.actingUser] = credential.actingUserId;
|
|
25
|
+
return headers;
|
|
26
|
+
}
|
|
27
|
+
case "api_key": {
|
|
28
|
+
if (!credential.apiKey.startsWith(API_KEY_PREFIX)) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`credentialToHeaders: an api_key credential must start with "${API_KEY_PREFIX}" (AI Matrx API keys look like mx_live_...)`
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
return { [MATRX_HEADERS.authorization]: `Bearer ${credential.apiKey}` };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function toCredentialsPort(setting) {
|
|
38
|
+
if (setting === void 0 || setting === null) return { get: async () => null };
|
|
39
|
+
if (typeof setting === "function") return { get: async () => setting() };
|
|
40
|
+
if ("get" in setting && typeof setting.get === "function") return setting;
|
|
41
|
+
const fixed = setting;
|
|
42
|
+
return { get: async () => fixed };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
exports.API_KEY_PREFIX = API_KEY_PREFIX;
|
|
46
|
+
exports.MATRX_HEADERS = MATRX_HEADERS;
|
|
47
|
+
exports.credentialToHeaders = credentialToHeaders;
|
|
48
|
+
exports.toCredentialsPort = toCredentialsPort;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk2SXAAKGV_cjs = require('./chunk-2SXAAKGV.cjs');
|
|
4
|
+
|
|
5
|
+
// src/net/stream-monitor.ts
|
|
6
|
+
var DEFAULTS = {
|
|
7
|
+
heartbeatTimeoutMs: 3e4,
|
|
8
|
+
maxLifetimeMs: 6e5
|
|
9
|
+
};
|
|
10
|
+
async function* monitorStream(source, opts = {}) {
|
|
11
|
+
const heartbeatTimeoutMs = opts.heartbeatTimeoutMs ?? DEFAULTS.heartbeatTimeoutMs;
|
|
12
|
+
const maxLifetimeMs = opts.maxLifetimeMs ?? DEFAULTS.maxLifetimeMs;
|
|
13
|
+
const abort = opts.abortController;
|
|
14
|
+
const startedAt = Date.now();
|
|
15
|
+
const iter = source[Symbol.asyncIterator]();
|
|
16
|
+
while (true) {
|
|
17
|
+
const elapsed = Date.now() - startedAt;
|
|
18
|
+
const remainingLifetime = maxLifetimeMs - elapsed;
|
|
19
|
+
if (remainingLifetime <= 0) {
|
|
20
|
+
abort?.abort("total-timeout");
|
|
21
|
+
throw new chunk2SXAAKGV_cjs.TotalTimeoutError(maxLifetimeMs);
|
|
22
|
+
}
|
|
23
|
+
const deadline = Math.min(heartbeatTimeoutMs, remainingLifetime);
|
|
24
|
+
let timer = null;
|
|
25
|
+
let timedOutKind = null;
|
|
26
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
27
|
+
timer = setTimeout(() => {
|
|
28
|
+
timedOutKind = deadline === remainingLifetime ? "total" : "heartbeat";
|
|
29
|
+
abort?.abort(
|
|
30
|
+
timedOutKind === "total" ? "total-timeout" : "heartbeat-timeout"
|
|
31
|
+
);
|
|
32
|
+
reject(
|
|
33
|
+
timedOutKind === "total" ? new chunk2SXAAKGV_cjs.TotalTimeoutError(maxLifetimeMs) : new chunk2SXAAKGV_cjs.HeartbeatTimeoutError(heartbeatTimeoutMs)
|
|
34
|
+
);
|
|
35
|
+
}, deadline);
|
|
36
|
+
});
|
|
37
|
+
let result;
|
|
38
|
+
try {
|
|
39
|
+
result = await Promise.race([iter.next(), timeoutPromise]);
|
|
40
|
+
} catch (err) {
|
|
41
|
+
if (timer !== null) clearTimeout(timer);
|
|
42
|
+
try {
|
|
43
|
+
const cleanup = iter.return?.(void 0);
|
|
44
|
+
if (cleanup) {
|
|
45
|
+
void Promise.resolve(cleanup).catch(() => {
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
throw err;
|
|
51
|
+
}
|
|
52
|
+
if (timer !== null) clearTimeout(timer);
|
|
53
|
+
if (result.done) return;
|
|
54
|
+
opts.onHeartbeat?.(Date.now());
|
|
55
|
+
yield result.value;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
exports.monitorStream = monitorStream;
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// src/net/errors.ts
|
|
2
|
+
var NetError = class extends Error {
|
|
3
|
+
code;
|
|
4
|
+
cause;
|
|
5
|
+
status;
|
|
6
|
+
retryable;
|
|
7
|
+
constructor(code, message, opts = {}) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "NetError";
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.cause = opts.cause;
|
|
12
|
+
if (opts.status !== void 0) this.status = opts.status;
|
|
13
|
+
this.retryable = opts.retryable ?? false;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var ConnectTimeoutError = class extends NetError {
|
|
17
|
+
constructor(ms, cause) {
|
|
18
|
+
super("connect-timeout", `Connection timed out after ${ms}ms`, {
|
|
19
|
+
cause,
|
|
20
|
+
retryable: true
|
|
21
|
+
});
|
|
22
|
+
this.name = "ConnectTimeoutError";
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var TotalTimeoutError = class extends NetError {
|
|
26
|
+
constructor(ms, cause) {
|
|
27
|
+
super("total-timeout", `Request exceeded ${ms}ms total budget`, {
|
|
28
|
+
cause,
|
|
29
|
+
retryable: false
|
|
30
|
+
});
|
|
31
|
+
this.name = "TotalTimeoutError";
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var HeartbeatTimeoutError = class extends NetError {
|
|
35
|
+
constructor(ms, cause) {
|
|
36
|
+
super(
|
|
37
|
+
"heartbeat-timeout",
|
|
38
|
+
`No server activity for ${ms}ms \u2014 stream considered dead`,
|
|
39
|
+
{ cause, retryable: false }
|
|
40
|
+
);
|
|
41
|
+
this.name = "HeartbeatTimeoutError";
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var NetworkError = class extends NetError {
|
|
45
|
+
constructor(message, cause) {
|
|
46
|
+
super("network", message, { cause, retryable: true });
|
|
47
|
+
this.name = "NetworkError";
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var HttpError = class extends NetError {
|
|
51
|
+
constructor(status, message, cause) {
|
|
52
|
+
const retryable = status === 408 || status === 429 || status >= 500;
|
|
53
|
+
super("http", message, { cause, status, retryable });
|
|
54
|
+
this.name = "HttpError";
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var AbortedError = class extends NetError {
|
|
58
|
+
constructor(cause) {
|
|
59
|
+
super("aborted", "Request was aborted", { cause, retryable: false });
|
|
60
|
+
this.name = "AbortedError";
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var OfflineError = class extends NetError {
|
|
64
|
+
constructor() {
|
|
65
|
+
super("offline", "Browser is offline", { retryable: true });
|
|
66
|
+
this.name = "OfflineError";
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
function isNetError(err) {
|
|
70
|
+
return err instanceof NetError;
|
|
71
|
+
}
|
|
72
|
+
function extractErrorMessage(err, fallback = "An unexpected error occurred") {
|
|
73
|
+
if (!err) return "Unknown error";
|
|
74
|
+
if (typeof err === "string") return err;
|
|
75
|
+
if (err instanceof Error) return err.message;
|
|
76
|
+
if (typeof err === "object") {
|
|
77
|
+
const e = err;
|
|
78
|
+
const parts = [];
|
|
79
|
+
if (typeof e.message === "string" && e.message) parts.push(e.message);
|
|
80
|
+
if (parts.length === 0) {
|
|
81
|
+
if (typeof e.detail === "string" && e.detail) parts.push(e.detail);
|
|
82
|
+
else if (e.detail && typeof e.detail === "object") {
|
|
83
|
+
const d = e.detail;
|
|
84
|
+
if (typeof d.message === "string" && d.message) parts.push(d.message);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (typeof e.details === "string" && e.details) parts.push(e.details);
|
|
88
|
+
if (typeof e.hint === "string" && e.hint) parts.push(`Hint: ${e.hint}`);
|
|
89
|
+
if (typeof e.code === "string" && e.code) parts.push(`Code: ${e.code}`);
|
|
90
|
+
if (parts.length > 0) return parts.join(" \xB7 ");
|
|
91
|
+
const nested = extractNestedErrorMessage(e, 0);
|
|
92
|
+
if (nested !== null) return nested;
|
|
93
|
+
try {
|
|
94
|
+
const s = JSON.stringify(err);
|
|
95
|
+
if (s && s !== "{}") return s;
|
|
96
|
+
} catch {
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return fallback;
|
|
100
|
+
}
|
|
101
|
+
function extractNestedErrorMessage(e, depth) {
|
|
102
|
+
if (depth > 4) return null;
|
|
103
|
+
for (const key of ["error", "exception", "cause"]) {
|
|
104
|
+
const value = e[key];
|
|
105
|
+
if (typeof value === "string" && value) return value;
|
|
106
|
+
if (value && typeof value === "object") {
|
|
107
|
+
const inner = value;
|
|
108
|
+
if (typeof inner.message === "string" && inner.message) return inner.message;
|
|
109
|
+
if (typeof inner.detail === "string" && inner.detail) return inner.detail;
|
|
110
|
+
const deeper = extractNestedErrorMessage(inner, depth + 1);
|
|
111
|
+
if (deeper !== null) return deeper;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
function isTransportFailure(err) {
|
|
117
|
+
if (err instanceof NetworkError || err instanceof OfflineError) return true;
|
|
118
|
+
if (typeof navigator !== "undefined" && navigator.onLine === false) return true;
|
|
119
|
+
if (err instanceof TypeError) return true;
|
|
120
|
+
if (err && typeof err === "object") {
|
|
121
|
+
const e = err;
|
|
122
|
+
if (e.code || e.status) return false;
|
|
123
|
+
const text = `${typeof e.name === "string" ? e.name : ""} ${typeof e.message === "string" ? e.message : ""}`.toLowerCase();
|
|
124
|
+
return text.includes("failed to fetch") || text.includes("networkerror") || text.includes("network request failed") || text.includes("load failed") || // Safari's wording for the same condition
|
|
125
|
+
text.includes("upstream connect error or disconnect/reset before headers");
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
function toNetError(err) {
|
|
130
|
+
if (isNetError(err)) return err;
|
|
131
|
+
if (err instanceof DOMException && err.name === "AbortError") {
|
|
132
|
+
return new AbortedError(err);
|
|
133
|
+
}
|
|
134
|
+
if (err instanceof TypeError) {
|
|
135
|
+
return new NetworkError(err.message || "Network request failed", err);
|
|
136
|
+
}
|
|
137
|
+
return new NetError(
|
|
138
|
+
"unknown",
|
|
139
|
+
extractErrorMessage(err),
|
|
140
|
+
{ cause: err }
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// src/net/resilient-fetch.ts
|
|
145
|
+
var DEFAULTS = {
|
|
146
|
+
connectTimeoutMs: 15e3,
|
|
147
|
+
totalTimeoutMs: 12e4
|
|
148
|
+
};
|
|
149
|
+
function linkAbort(target, signal) {
|
|
150
|
+
if (signal.aborted) {
|
|
151
|
+
target.abort(signal.reason);
|
|
152
|
+
return () => {
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
const onAbort = () => target.abort(signal.reason);
|
|
156
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
157
|
+
return () => signal.removeEventListener("abort", onAbort);
|
|
158
|
+
}
|
|
159
|
+
async function resilientFetch(url, init = {}, opts = {}) {
|
|
160
|
+
const connectTimeoutMs = opts.connectTimeoutMs ?? DEFAULTS.connectTimeoutMs;
|
|
161
|
+
const totalTimeoutMs = opts.totalTimeoutMs === void 0 ? DEFAULTS.totalTimeoutMs : opts.totalTimeoutMs;
|
|
162
|
+
const throwOnHttpError = opts.throwOnHttpError ?? true;
|
|
163
|
+
const controller = new AbortController();
|
|
164
|
+
const unlinkCaller = opts.signal ? linkAbort(controller, opts.signal) : () => {
|
|
165
|
+
};
|
|
166
|
+
let connectTimedOut = false;
|
|
167
|
+
let totalTimedOut = false;
|
|
168
|
+
const connectTimer = setTimeout(() => {
|
|
169
|
+
connectTimedOut = true;
|
|
170
|
+
controller.abort("connect-timeout");
|
|
171
|
+
}, connectTimeoutMs);
|
|
172
|
+
const totalTimer = totalTimeoutMs !== null ? setTimeout(() => {
|
|
173
|
+
totalTimedOut = true;
|
|
174
|
+
controller.abort("total-timeout");
|
|
175
|
+
}, totalTimeoutMs) : null;
|
|
176
|
+
try {
|
|
177
|
+
const response = await fetch(url, { ...init, signal: controller.signal });
|
|
178
|
+
clearTimeout(connectTimer);
|
|
179
|
+
if (totalTimer !== null) clearTimeout(totalTimer);
|
|
180
|
+
if (throwOnHttpError && !response.ok) {
|
|
181
|
+
throw new HttpError(
|
|
182
|
+
response.status,
|
|
183
|
+
`HTTP ${response.status} ${response.statusText}`
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return { response, controller };
|
|
187
|
+
} catch (err) {
|
|
188
|
+
clearTimeout(connectTimer);
|
|
189
|
+
if (totalTimer !== null) clearTimeout(totalTimer);
|
|
190
|
+
if (connectTimedOut) throw new ConnectTimeoutError(connectTimeoutMs, err);
|
|
191
|
+
if (totalTimedOut)
|
|
192
|
+
throw new TotalTimeoutError(totalTimeoutMs, err);
|
|
193
|
+
if (err instanceof DOMException && err.name === "AbortError") {
|
|
194
|
+
throw new AbortedError(err);
|
|
195
|
+
}
|
|
196
|
+
if (opts.signal?.aborted) {
|
|
197
|
+
throw new AbortedError(err);
|
|
198
|
+
}
|
|
199
|
+
if (err instanceof TypeError) {
|
|
200
|
+
if (typeof navigator !== "undefined" && navigator.onLine === false) {
|
|
201
|
+
throw new OfflineError();
|
|
202
|
+
}
|
|
203
|
+
throw new NetworkError(err.message || "Network request failed", err);
|
|
204
|
+
}
|
|
205
|
+
if (err instanceof HttpError) throw err;
|
|
206
|
+
throw new NetworkError(extractErrorMessage(err), err);
|
|
207
|
+
} finally {
|
|
208
|
+
unlinkCaller();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/net/retry.ts
|
|
213
|
+
function defaultRetryOn(err) {
|
|
214
|
+
return isNetError(err) && err.retryable;
|
|
215
|
+
}
|
|
216
|
+
function sleep(ms, signal) {
|
|
217
|
+
return new Promise((resolve, reject) => {
|
|
218
|
+
const timer = setTimeout(() => {
|
|
219
|
+
resolve();
|
|
220
|
+
if (signal) signal.removeEventListener("abort", onAbort);
|
|
221
|
+
}, ms);
|
|
222
|
+
const onAbort = () => {
|
|
223
|
+
clearTimeout(timer);
|
|
224
|
+
reject(signal?.reason ?? new Error("Aborted"));
|
|
225
|
+
};
|
|
226
|
+
if (signal) {
|
|
227
|
+
if (signal.aborted) {
|
|
228
|
+
clearTimeout(timer);
|
|
229
|
+
reject(signal.reason);
|
|
230
|
+
} else {
|
|
231
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
async function withRetry(fn, opts = {}) {
|
|
237
|
+
const attempts = Math.max(1, opts.attempts ?? 3);
|
|
238
|
+
const initialDelayMs = opts.initialDelayMs ?? 250;
|
|
239
|
+
const maxDelayMs = opts.maxDelayMs ?? 4e3;
|
|
240
|
+
const factor = opts.backoffFactor ?? 2;
|
|
241
|
+
const jitter = Math.max(0, Math.min(1, opts.jitter ?? 0.25));
|
|
242
|
+
const retryOn = opts.retryOn ?? defaultRetryOn;
|
|
243
|
+
let lastErr;
|
|
244
|
+
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
245
|
+
if (opts.signal?.aborted) throw opts.signal.reason;
|
|
246
|
+
try {
|
|
247
|
+
return await fn(attempt);
|
|
248
|
+
} catch (err) {
|
|
249
|
+
lastErr = err;
|
|
250
|
+
if (attempt === attempts || !retryOn(err)) throw err;
|
|
251
|
+
const base = Math.min(
|
|
252
|
+
maxDelayMs,
|
|
253
|
+
initialDelayMs * Math.pow(factor, attempt - 1)
|
|
254
|
+
);
|
|
255
|
+
const jitterAmount = base * jitter * (Math.random() * 2 - 1);
|
|
256
|
+
const delayMs = Math.max(0, Math.round(base + jitterAmount));
|
|
257
|
+
opts.onRetry?.(attempt, err, delayMs);
|
|
258
|
+
await sleep(delayMs, opts.signal);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
throw lastErr;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export { AbortedError, ConnectTimeoutError, HeartbeatTimeoutError, HttpError, NetError, NetworkError, OfflineError, TotalTimeoutError, extractErrorMessage, isNetError, isTransportFailure, resilientFetch, toNetError, withRetry };
|