@shashu-ai/sdk 1.1.0 → 1.1.1
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 +5 -2
- package/dist/{_joiner-CmR7qVK8.d.cts → _joiner-DGi8GsY7.d.cts} +0 -10
- package/dist/{_joiner-CmR7qVK8.d.ts → _joiner-DGi8GsY7.d.ts} +0 -10
- package/dist/{chunk-WC3BKVXK.js → chunk-3GAMCQ6R.js} +2 -2
- package/dist/{chunk-QLK5CQSP.js → chunk-3JZXUKNA.js} +1 -1
- package/dist/{chunk-QHWUNZQS.js → chunk-3X5IUBIA.js} +2 -2
- package/dist/{chunk-L6GKR3SC.js → chunk-F4CXDZQB.js} +113 -11
- package/dist/{chunk-NTIOPTYF.js → chunk-M7EMWSNS.js} +22 -1
- package/dist/{chunk-QJQ4MWQ4.js → chunk-TGD6KDWT.js} +1 -1
- package/dist/index.cjs +55 -25
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +30 -112
- package/dist/{langchain-B9Q9eL7g.d.cts → langchain-DKyrUjk4.d.cts} +1 -1
- package/dist/{langchain-CHjjdhDr.d.ts → langchain-QCATTIR4.d.ts} +1 -1
- package/dist/middleware/anthropic.cjs +1 -1
- package/dist/middleware/anthropic.d.cts +1 -1
- package/dist/middleware/anthropic.d.ts +1 -1
- package/dist/middleware/anthropic.js +3 -3
- package/dist/middleware/google.cjs +1 -1
- package/dist/middleware/google.js +2 -2
- package/dist/middleware/langchain.cjs +145 -42
- package/dist/middleware/langchain.d.cts +2 -2
- package/dist/middleware/langchain.d.ts +2 -2
- package/dist/middleware/langchain.js +3 -3
- package/dist/middleware/vercel-ai.cjs +1 -1
- package/dist/middleware/vercel-ai.d.cts +1 -1
- package/dist/middleware/vercel-ai.d.ts +1 -1
- package/dist/middleware/vercel-ai.js +3 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,10 +5,13 @@ documented here. This project adheres to [Semantic Versioning](https://semver.or
|
|
|
5
5
|
|
|
6
6
|
## [Unreleased]
|
|
7
7
|
|
|
8
|
-
## [1.1.
|
|
8
|
+
## [1.1.1] — 2026-11-25
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Fixed: when the gateway was busy and the SDK retried, the retry could be sent
|
|
11
|
+
without the retrieval declaration. The request (body, headers, and
|
|
12
|
+
`shashu_metadata`) is now built once per logical call and only re-sent.
|
|
11
13
|
|
|
14
|
+
## [1.1.0] — 2026-11-25
|
|
12
15
|
|
|
13
16
|
Wire names moved to SHASHU (requires a SHASHU gateway); key prefix `tg_sk_` unchanged.
|
|
14
17
|
|
|
@@ -33,16 +33,6 @@
|
|
|
33
33
|
*/
|
|
34
34
|
/** The `fetch` shape every provider SDK's option accepts. */
|
|
35
35
|
type FetchLike = (input: any, init?: any) => Promise<Response>;
|
|
36
|
-
/**
|
|
37
|
-
* Wrap a `fetch` so the carried declaration rides the next JSON model call.
|
|
38
|
-
*
|
|
39
|
-
* ```ts
|
|
40
|
-
* const openai = new OpenAI({ baseURL, fetch: withRetrievalJoiner() });
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* Never throws on the SDK's behalf: any failure to understand a request means
|
|
44
|
-
* that request goes out exactly as the caller built it.
|
|
45
|
-
*/
|
|
46
36
|
declare function withRetrievalJoiner(underlying?: FetchLike): FetchLike;
|
|
47
37
|
|
|
48
38
|
export { type FetchLike as F, withRetrievalJoiner as w };
|
|
@@ -33,16 +33,6 @@
|
|
|
33
33
|
*/
|
|
34
34
|
/** The `fetch` shape every provider SDK's option accepts. */
|
|
35
35
|
type FetchLike = (input: any, init?: any) => Promise<Response>;
|
|
36
|
-
/**
|
|
37
|
-
* Wrap a `fetch` so the carried declaration rides the next JSON model call.
|
|
38
|
-
*
|
|
39
|
-
* ```ts
|
|
40
|
-
* const openai = new OpenAI({ baseURL, fetch: withRetrievalJoiner() });
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* Never throws on the SDK's behalf: any failure to understand a request means
|
|
44
|
-
* that request goes out exactly as the caller built it.
|
|
45
|
-
*/
|
|
46
36
|
declare function withRetrievalJoiner(underlying?: FetchLike): FetchLike;
|
|
47
37
|
|
|
48
38
|
export { type FetchLike as F, withRetrievalJoiner as w };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
withRetrievalJoiner
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-M7EMWSNS.js";
|
|
4
4
|
import {
|
|
5
5
|
CredentialResolver,
|
|
6
6
|
applyContractVersion,
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
passRefusalCodeFromText,
|
|
11
11
|
requireBaseUrl,
|
|
12
12
|
shouldRetryPassRefused
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-TGD6KDWT.js";
|
|
14
14
|
|
|
15
15
|
// src/middleware/anthropic.ts
|
|
16
16
|
import { randomUUID } from "crypto";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
withRetrievalJoiner
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-M7EMWSNS.js";
|
|
4
4
|
import {
|
|
5
5
|
CredentialResolver,
|
|
6
6
|
applyContractVersion,
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
passRefusalCodeFromText,
|
|
11
11
|
requireBaseUrl,
|
|
12
12
|
shouldRetryPassRefused
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-TGD6KDWT.js";
|
|
14
14
|
|
|
15
15
|
// src/middleware/vercel-ai.ts
|
|
16
16
|
import { randomUUID } from "crypto";
|
|
@@ -1,21 +1,113 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MAX_EVENTS,
|
|
3
3
|
declareRetrieval,
|
|
4
|
-
|
|
5
|
-
} from "./chunk-
|
|
4
|
+
pinOutgoing
|
|
5
|
+
} from "./chunk-M7EMWSNS.js";
|
|
6
6
|
import {
|
|
7
7
|
CredentialResolver,
|
|
8
8
|
applyContractVersion,
|
|
9
9
|
buildContextHeaders,
|
|
10
|
+
gatewayRetryAfter,
|
|
10
11
|
getWorkflowContextFromEnv,
|
|
11
12
|
isDisabled,
|
|
12
13
|
passRefusalCodeFromText,
|
|
13
14
|
requireBaseUrl,
|
|
14
15
|
shouldRetryPassRefused
|
|
15
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-TGD6KDWT.js";
|
|
16
17
|
|
|
17
18
|
// src/middleware/langchain.ts
|
|
18
19
|
import { randomUUID } from "crypto";
|
|
20
|
+
|
|
21
|
+
// src/retry.ts
|
|
22
|
+
var DEFAULT_RETRY_POLICY = {
|
|
23
|
+
maxRetries: 2,
|
|
24
|
+
backoffBaseSeconds: 0.5,
|
|
25
|
+
backoffMaxSeconds: 30,
|
|
26
|
+
retryOnConnectionErrors: true,
|
|
27
|
+
retryOn429: true,
|
|
28
|
+
retryOn5xx: false,
|
|
29
|
+
neverRetryStatus: [400, 401, 403, 404],
|
|
30
|
+
ignoreRetryAfter: false,
|
|
31
|
+
jitterFraction: 0.2
|
|
32
|
+
};
|
|
33
|
+
function resolveRetryPolicy(policy) {
|
|
34
|
+
if (policy === null) return null;
|
|
35
|
+
if (policy === void 0) return { ...DEFAULT_RETRY_POLICY };
|
|
36
|
+
return { ...DEFAULT_RETRY_POLICY, ...policy };
|
|
37
|
+
}
|
|
38
|
+
function shouldRetryStatus(policy, status) {
|
|
39
|
+
if (policy.neverRetryStatus.includes(status)) return false;
|
|
40
|
+
if (status === 429) return policy.retryOn429;
|
|
41
|
+
if (status === 503) return true;
|
|
42
|
+
if (status >= 500 && status <= 599) return policy.retryOn5xx;
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
function truthyHeader(value) {
|
|
46
|
+
return ["1", "true", "yes", "on"].includes((value ?? "").trim().toLowerCase());
|
|
47
|
+
}
|
|
48
|
+
function gatewaySaysBusyRetry(res, retryAfter) {
|
|
49
|
+
if (res.status !== 429 && res.status !== 503) return false;
|
|
50
|
+
if (truthyHeader(res.headers.get("x-should-retry"))) return true;
|
|
51
|
+
return retryAfter !== void 0;
|
|
52
|
+
}
|
|
53
|
+
function shouldRetryResponse(policy, res, retryAfter) {
|
|
54
|
+
if (policy.neverRetryStatus.includes(res.status)) return false;
|
|
55
|
+
if (res.status === 429 || res.status === 503) {
|
|
56
|
+
if (res.status === 429 && !policy.retryOn429) return false;
|
|
57
|
+
return gatewaySaysBusyRetry(res, retryAfter);
|
|
58
|
+
}
|
|
59
|
+
if (!shouldRetryStatus(policy, res.status)) return false;
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
function delayFor(policy, attempt, res, retryAfter) {
|
|
63
|
+
const jitter = (seconds) => {
|
|
64
|
+
const frac = policy.jitterFraction ?? 0.2;
|
|
65
|
+
const capped = Math.min(policy.backoffMaxSeconds, seconds);
|
|
66
|
+
if (frac <= 0) return capped;
|
|
67
|
+
return Math.min(policy.backoffMaxSeconds, capped * (1 + Math.random() * frac));
|
|
68
|
+
};
|
|
69
|
+
if (res !== null && (res.status === 429 || res.status === 503) && !policy.ignoreRetryAfter && retryAfter !== void 0) {
|
|
70
|
+
return jitter(retryAfter);
|
|
71
|
+
}
|
|
72
|
+
return jitter(policy.backoffBaseSeconds * 2 ** attempt);
|
|
73
|
+
}
|
|
74
|
+
function isConnectionError(err) {
|
|
75
|
+
return err instanceof TypeError;
|
|
76
|
+
}
|
|
77
|
+
var defaultSleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1e3));
|
|
78
|
+
async function peekJson(res) {
|
|
79
|
+
try {
|
|
80
|
+
return await res.clone().json();
|
|
81
|
+
} catch {
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async function executeWithRetry(send, policy, sleep = defaultSleep) {
|
|
86
|
+
if (policy === null) return send();
|
|
87
|
+
let attempt = 0;
|
|
88
|
+
for (; ; ) {
|
|
89
|
+
let res;
|
|
90
|
+
try {
|
|
91
|
+
res = await send();
|
|
92
|
+
} catch (err) {
|
|
93
|
+
if (policy.retryOnConnectionErrors && isConnectionError(err) && attempt < policy.maxRetries) {
|
|
94
|
+
await sleep(delayFor(policy, attempt, null, void 0));
|
|
95
|
+
attempt += 1;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
throw err;
|
|
99
|
+
}
|
|
100
|
+
const retryAfter = res.status === 429 || res.status === 503 ? gatewayRetryAfter(res, await peekJson(res)) : void 0;
|
|
101
|
+
if (attempt < policy.maxRetries && shouldRetryResponse(policy, res, retryAfter)) {
|
|
102
|
+
await sleep(delayFor(policy, attempt, res, retryAfter));
|
|
103
|
+
attempt += 1;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
return res;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/middleware/langchain.ts
|
|
19
111
|
function createShashuLangChainConfig(config) {
|
|
20
112
|
if (isDisabled()) return {};
|
|
21
113
|
const baseUrl = requireBaseUrl(config.baseUrl, {
|
|
@@ -31,12 +123,21 @@ function createShashuLangChainConfig(config) {
|
|
|
31
123
|
...buildContextHeaders(ctx, { defaultSessionId: config.sessionId ?? randomUUID() })
|
|
32
124
|
};
|
|
33
125
|
applyContractVersion(headers, config.contractVersion);
|
|
34
|
-
const joining = withRetrievalJoiner();
|
|
35
126
|
const fetch = async (input, init) => {
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
127
|
+
const baseHeaders = new Headers(init?.headers);
|
|
128
|
+
baseHeaders.delete("authorization");
|
|
129
|
+
const headerObj = {};
|
|
130
|
+
baseHeaders.forEach((v, k) => {
|
|
131
|
+
headerObj[k] = v;
|
|
132
|
+
});
|
|
133
|
+
const pinned = pinOutgoing(input, { ...init, headers: headerObj });
|
|
134
|
+
const send = async () => {
|
|
135
|
+
const token = await resolver.get();
|
|
136
|
+
const headers2 = { ...pinned.init?.headers || {} };
|
|
137
|
+
if (token) headers2.authorization = `Bearer ${token}`;
|
|
138
|
+
return globalThis.fetch(pinned.input, { ...pinned.init, headers: headers2 });
|
|
139
|
+
};
|
|
140
|
+
const res = await executeWithRetry(send, resolveRetryPolicy(config.retryPolicy));
|
|
40
141
|
if (!resolver.usesProvider || res.status !== 401) return res;
|
|
41
142
|
const code = passRefusalCodeFromText(await res.clone().text());
|
|
42
143
|
if (!shouldRetryPassRefused({
|
|
@@ -48,9 +149,7 @@ function createShashuLangChainConfig(config) {
|
|
|
48
149
|
return res;
|
|
49
150
|
}
|
|
50
151
|
resolver.invalidate();
|
|
51
|
-
|
|
52
|
-
if (retryToken) next.set("authorization", `Bearer ${retryToken}`);
|
|
53
|
-
return joining(input, { ...init, headers: next });
|
|
152
|
+
return send();
|
|
54
153
|
};
|
|
55
154
|
return {
|
|
56
155
|
baseURL: baseUrl,
|
|
@@ -217,6 +316,9 @@ function isInstalled() {
|
|
|
217
316
|
}
|
|
218
317
|
|
|
219
318
|
export {
|
|
319
|
+
DEFAULT_RETRY_POLICY,
|
|
320
|
+
resolveRetryPolicy,
|
|
321
|
+
executeWithRetry,
|
|
220
322
|
createShashuLangChainConfig,
|
|
221
323
|
lcDocumentsToRagContext,
|
|
222
324
|
SOURCE,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
isDisabled
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-TGD6KDWT.js";
|
|
4
4
|
|
|
5
5
|
// src/_bus.ts
|
|
6
6
|
import { AsyncLocalStorage } from "async_hooks";
|
|
@@ -456,6 +456,26 @@ function takeContextFor(body) {
|
|
|
456
456
|
}
|
|
457
457
|
return mutated ? body : void 0;
|
|
458
458
|
}
|
|
459
|
+
function pinOutgoing(input, init) {
|
|
460
|
+
if (!hasEvents()) return { input, init };
|
|
461
|
+
if (isDisabled()) {
|
|
462
|
+
clear();
|
|
463
|
+
return { input, init };
|
|
464
|
+
}
|
|
465
|
+
const body = parseJsonObject(init?.body);
|
|
466
|
+
if (body === void 0) return { input, init };
|
|
467
|
+
const merged = takeContextFor(body);
|
|
468
|
+
if (merged === void 0) return { input, init };
|
|
469
|
+
const encoded = JSON.stringify(merged);
|
|
470
|
+
return {
|
|
471
|
+
input,
|
|
472
|
+
init: {
|
|
473
|
+
...init,
|
|
474
|
+
body: encoded,
|
|
475
|
+
headers: fixContentLength(init?.headers, byteLength(encoded))
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
}
|
|
459
479
|
function withRetrievalJoiner(underlying = globalFetch) {
|
|
460
480
|
return async function trustgateJoiningFetch(input, init) {
|
|
461
481
|
if (!hasEvents()) return underlying(input, init);
|
|
@@ -505,5 +525,6 @@ export {
|
|
|
505
525
|
buildRagContext,
|
|
506
526
|
declareMemory,
|
|
507
527
|
memoryScope,
|
|
528
|
+
pinOutgoing,
|
|
508
529
|
withRetrievalJoiner
|
|
509
530
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -360,7 +360,7 @@ function requireBaseUrl(baseUrl, opts) {
|
|
|
360
360
|
var import_node_crypto2 = require("crypto");
|
|
361
361
|
|
|
362
362
|
// src/version.ts
|
|
363
|
-
var SDK_VERSION = "1.1.
|
|
363
|
+
var SDK_VERSION = "1.1.1";
|
|
364
364
|
|
|
365
365
|
// src/tracer.ts
|
|
366
366
|
var import_node_async_hooks = require("async_hooks");
|
|
@@ -1356,6 +1356,26 @@ function takeContextFor(body) {
|
|
|
1356
1356
|
}
|
|
1357
1357
|
return mutated ? body : void 0;
|
|
1358
1358
|
}
|
|
1359
|
+
function pinOutgoing(input, init) {
|
|
1360
|
+
if (!hasEvents()) return { input, init };
|
|
1361
|
+
if (isDisabled()) {
|
|
1362
|
+
clear();
|
|
1363
|
+
return { input, init };
|
|
1364
|
+
}
|
|
1365
|
+
const body = parseJsonObject(init?.body);
|
|
1366
|
+
if (body === void 0) return { input, init };
|
|
1367
|
+
const merged = takeContextFor(body);
|
|
1368
|
+
if (merged === void 0) return { input, init };
|
|
1369
|
+
const encoded = JSON.stringify(merged);
|
|
1370
|
+
return {
|
|
1371
|
+
input,
|
|
1372
|
+
init: {
|
|
1373
|
+
...init,
|
|
1374
|
+
body: encoded,
|
|
1375
|
+
headers: fixContentLength(init?.headers, byteLength(encoded))
|
|
1376
|
+
}
|
|
1377
|
+
};
|
|
1378
|
+
}
|
|
1359
1379
|
function withRetrievalJoiner(underlying = globalFetch) {
|
|
1360
1380
|
return async function trustgateJoiningFetch(input, init) {
|
|
1361
1381
|
if (!hasEvents()) return underlying(input, init);
|
|
@@ -2038,27 +2058,30 @@ var Shashu = class {
|
|
|
2038
2058
|
const context = resolveContext(this.config, options);
|
|
2039
2059
|
const url = path.startsWith("http") ? path : `${this.resolvedBaseUrl}/${path.replace(/^\//, "")}`;
|
|
2040
2060
|
const timeout = options?.timeout ?? 6e4;
|
|
2041
|
-
const
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
this.config,
|
|
2045
|
-
options,
|
|
2046
|
-
context,
|
|
2047
|
-
{
|
|
2061
|
+
const headersOnce = applyContractVersion(
|
|
2062
|
+
{
|
|
2063
|
+
...mergeHeaders(this.config, options, context, {
|
|
2048
2064
|
defaultSessionId: this._sessionId,
|
|
2049
2065
|
requestSessionId: options?.sessionId
|
|
2050
|
-
},
|
|
2051
|
-
|
|
2052
|
-
|
|
2066
|
+
}),
|
|
2067
|
+
...init.headers
|
|
2068
|
+
},
|
|
2069
|
+
this.config.contractVersion
|
|
2070
|
+
);
|
|
2071
|
+
const frozenHeaders = { ...headersOnce };
|
|
2072
|
+
delete frozenHeaders["authorization"];
|
|
2073
|
+
delete frozenHeaders["Authorization"];
|
|
2074
|
+
const pinned = pinOutgoing(url, { ...init, headers: frozenHeaders });
|
|
2075
|
+
const send = async () => {
|
|
2076
|
+
const token = await this.resolver.get();
|
|
2053
2077
|
const controller = new AbortController();
|
|
2054
2078
|
const id = setTimeout(() => controller.abort(), timeout);
|
|
2055
2079
|
try {
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
),
|
|
2080
|
+
const headers = { ...pinned.init?.headers };
|
|
2081
|
+
if (token) headers["authorization"] = `Bearer ${token}`;
|
|
2082
|
+
return await joiningFetch(pinned.input, {
|
|
2083
|
+
...pinned.init,
|
|
2084
|
+
headers,
|
|
2062
2085
|
signal: init.signal ?? controller.signal
|
|
2063
2086
|
});
|
|
2064
2087
|
} finally {
|
|
@@ -2322,12 +2345,21 @@ function createShashuLangChainConfig(config) {
|
|
|
2322
2345
|
...buildContextHeaders(ctx, { defaultSessionId: config.sessionId ?? (0, import_node_crypto6.randomUUID)() })
|
|
2323
2346
|
};
|
|
2324
2347
|
applyContractVersion(headers, config.contractVersion);
|
|
2325
|
-
const joining = withRetrievalJoiner();
|
|
2326
2348
|
const fetch2 = async (input, init) => {
|
|
2327
|
-
const
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2349
|
+
const baseHeaders = new Headers(init?.headers);
|
|
2350
|
+
baseHeaders.delete("authorization");
|
|
2351
|
+
const headerObj = {};
|
|
2352
|
+
baseHeaders.forEach((v, k) => {
|
|
2353
|
+
headerObj[k] = v;
|
|
2354
|
+
});
|
|
2355
|
+
const pinned = pinOutgoing(input, { ...init, headers: headerObj });
|
|
2356
|
+
const send = async () => {
|
|
2357
|
+
const token = await resolver.get();
|
|
2358
|
+
const headers2 = { ...pinned.init?.headers || {} };
|
|
2359
|
+
if (token) headers2.authorization = `Bearer ${token}`;
|
|
2360
|
+
return globalThis.fetch(pinned.input, { ...pinned.init, headers: headers2 });
|
|
2361
|
+
};
|
|
2362
|
+
const res = await executeWithRetry(send, resolveRetryPolicy(config.retryPolicy));
|
|
2331
2363
|
if (!resolver.usesProvider || res.status !== 401) return res;
|
|
2332
2364
|
const code = passRefusalCodeFromText(await res.clone().text());
|
|
2333
2365
|
if (!shouldRetryPassRefused({
|
|
@@ -2339,9 +2371,7 @@ function createShashuLangChainConfig(config) {
|
|
|
2339
2371
|
return res;
|
|
2340
2372
|
}
|
|
2341
2373
|
resolver.invalidate();
|
|
2342
|
-
|
|
2343
|
-
if (retryToken) next.set("authorization", `Bearer ${retryToken}`);
|
|
2344
|
-
return joining(input, { ...init, headers: next });
|
|
2374
|
+
return send();
|
|
2345
2375
|
};
|
|
2346
2376
|
return {
|
|
2347
2377
|
baseURL: baseUrl,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { R as RequestOptions, S as ShashuConfig, W as WorkflowContext, a as ShashuError, D as Decision, M as MemoryActivity, N as N8nHeaderSnippet } from './types-BLs_TMFv.cjs';
|
|
2
2
|
export { A as Attachment, b as AttachmentFinding, c as AttachmentState, d as Attachments, B as BODY_KEY, e as DECISIONS, f as DECISION_ALLOW, g as DECISION_ALLOW_ALTERED, h as DECISION_BLOCK, i as DECISION_FAIL, j as DECISION_NOT_CHECKED, k as DECISION_REFUSE, l as DEFAULT_RETRY_POLICY, m as DELIVERED_DECISIONS, H as HEADER_ATTACHMENTS, n as HEADER_CODE, o as HEADER_DECISION, p as HEADER_INSPECTION, q as HEADER_MEMORY, r as HEADER_REQUEST_ID, s as HEADER_SHASHU_REQUEST_ID, t as HEADER_SHOULD_RETRY, u as HEADER_TRACE_ID, v as MemoryItem, P as Provider, w as RetryPolicy, x as RoutePolicy, y as RuntimeMetadata, z as decisionFrom, C as executeWithRetry, E as isShashuBlock, F as resolveRetryPolicy, G as shashuDecision } from './types-BLs_TMFv.cjs';
|
|
3
|
-
import { F as FetchLike } from './_joiner-
|
|
4
|
-
export { w as withRetrievalJoiner } from './_joiner-
|
|
5
|
-
export { D as DeclareRetrievalOptions, R as RagContext, a as RagDoc, b as RagSource, S as SOURCES, c as attachRagContext, d as buildRagContext, e as createShashuLangChainConfig, f as declareRetrieval, m as makeRagContext, r as retrievalScope } from './langchain-
|
|
3
|
+
import { F as FetchLike } from './_joiner-DGi8GsY7.cjs';
|
|
4
|
+
export { w as withRetrievalJoiner } from './_joiner-DGi8GsY7.cjs';
|
|
5
|
+
export { D as DeclareRetrievalOptions, R as RagContext, a as RagDoc, b as RagSource, S as SOURCES, c as attachRagContext, d as buildRagContext, e as createShashuLangChainConfig, f as declareRetrieval, m as makeRagContext, r as retrievalScope } from './langchain-DKyrUjk4.cjs';
|
|
6
6
|
export { ShashuOpenAIOptions, createShashuOpenAIOptions, getShashuHeaders } from './middleware/vercel-ai.cjs';
|
|
7
7
|
export { createShashuAnthropicConfig, getAnthropicHeaders } from './middleware/anthropic.cjs';
|
|
8
8
|
export { createShashuGeminiConfig, getGeminiHeaders } from './middleware/google.cjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { R as RequestOptions, S as ShashuConfig, W as WorkflowContext, a as ShashuError, D as Decision, M as MemoryActivity, N as N8nHeaderSnippet } from './types-BLs_TMFv.js';
|
|
2
2
|
export { A as Attachment, b as AttachmentFinding, c as AttachmentState, d as Attachments, B as BODY_KEY, e as DECISIONS, f as DECISION_ALLOW, g as DECISION_ALLOW_ALTERED, h as DECISION_BLOCK, i as DECISION_FAIL, j as DECISION_NOT_CHECKED, k as DECISION_REFUSE, l as DEFAULT_RETRY_POLICY, m as DELIVERED_DECISIONS, H as HEADER_ATTACHMENTS, n as HEADER_CODE, o as HEADER_DECISION, p as HEADER_INSPECTION, q as HEADER_MEMORY, r as HEADER_REQUEST_ID, s as HEADER_SHASHU_REQUEST_ID, t as HEADER_SHOULD_RETRY, u as HEADER_TRACE_ID, v as MemoryItem, P as Provider, w as RetryPolicy, x as RoutePolicy, y as RuntimeMetadata, z as decisionFrom, C as executeWithRetry, E as isShashuBlock, F as resolveRetryPolicy, G as shashuDecision } from './types-BLs_TMFv.js';
|
|
3
|
-
import { F as FetchLike } from './_joiner-
|
|
4
|
-
export { w as withRetrievalJoiner } from './_joiner-
|
|
5
|
-
export { D as DeclareRetrievalOptions, R as RagContext, a as RagDoc, b as RagSource, S as SOURCES, c as attachRagContext, d as buildRagContext, e as createShashuLangChainConfig, f as declareRetrieval, m as makeRagContext, r as retrievalScope } from './langchain-
|
|
3
|
+
import { F as FetchLike } from './_joiner-DGi8GsY7.js';
|
|
4
|
+
export { w as withRetrievalJoiner } from './_joiner-DGi8GsY7.js';
|
|
5
|
+
export { D as DeclareRetrievalOptions, R as RagContext, a as RagDoc, b as RagSource, S as SOURCES, c as attachRagContext, d as buildRagContext, e as createShashuLangChainConfig, f as declareRetrieval, m as makeRagContext, r as retrievalScope } from './langchain-QCATTIR4.js';
|
|
6
6
|
export { ShashuOpenAIOptions, createShashuOpenAIOptions, getShashuHeaders } from './middleware/vercel-ai.js';
|
|
7
7
|
export { createShashuAnthropicConfig, getAnthropicHeaders } from './middleware/anthropic.js';
|
|
8
8
|
export { createShashuGeminiConfig, getGeminiHeaders } from './middleware/google.js';
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createShashuAnthropicConfig,
|
|
3
3
|
getAnthropicHeaders
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-3GAMCQ6R.js";
|
|
5
5
|
import {
|
|
6
6
|
createShashuGeminiConfig,
|
|
7
7
|
getGeminiHeaders
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-3JZXUKNA.js";
|
|
9
9
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
DEFAULT_RETRY_POLICY,
|
|
11
|
+
createShashuLangChainConfig,
|
|
12
|
+
executeWithRetry,
|
|
13
|
+
resolveRetryPolicy
|
|
14
|
+
} from "./chunk-F4CXDZQB.js";
|
|
12
15
|
import {
|
|
13
16
|
createShashuOpenAIOptions,
|
|
14
17
|
getShashuHeaders
|
|
15
|
-
} from "./chunk-
|
|
18
|
+
} from "./chunk-3X5IUBIA.js";
|
|
16
19
|
import {
|
|
17
20
|
SOURCES,
|
|
18
21
|
attachRagContext,
|
|
@@ -21,9 +24,10 @@ import {
|
|
|
21
24
|
declareRetrieval,
|
|
22
25
|
makeRagContext,
|
|
23
26
|
memoryScope,
|
|
27
|
+
pinOutgoing,
|
|
24
28
|
retrievalScope,
|
|
25
29
|
withRetrievalJoiner
|
|
26
|
-
} from "./chunk-
|
|
30
|
+
} from "./chunk-M7EMWSNS.js";
|
|
27
31
|
import {
|
|
28
32
|
API_KEY_ENV,
|
|
29
33
|
BASE_URL_ENV,
|
|
@@ -68,7 +72,7 @@ import {
|
|
|
68
72
|
requireBaseUrl,
|
|
69
73
|
shouldRetryPassRefused,
|
|
70
74
|
tracer
|
|
71
|
-
} from "./chunk-
|
|
75
|
+
} from "./chunk-TGD6KDWT.js";
|
|
72
76
|
|
|
73
77
|
// src/client.ts
|
|
74
78
|
import { randomUUID } from "crypto";
|
|
@@ -120,95 +124,6 @@ var Audit = class {
|
|
|
120
124
|
}
|
|
121
125
|
};
|
|
122
126
|
|
|
123
|
-
// src/retry.ts
|
|
124
|
-
var DEFAULT_RETRY_POLICY = {
|
|
125
|
-
maxRetries: 2,
|
|
126
|
-
backoffBaseSeconds: 0.5,
|
|
127
|
-
backoffMaxSeconds: 30,
|
|
128
|
-
retryOnConnectionErrors: true,
|
|
129
|
-
retryOn429: true,
|
|
130
|
-
retryOn5xx: false,
|
|
131
|
-
neverRetryStatus: [400, 401, 403, 404],
|
|
132
|
-
ignoreRetryAfter: false,
|
|
133
|
-
jitterFraction: 0.2
|
|
134
|
-
};
|
|
135
|
-
function resolveRetryPolicy(policy) {
|
|
136
|
-
if (policy === null) return null;
|
|
137
|
-
if (policy === void 0) return { ...DEFAULT_RETRY_POLICY };
|
|
138
|
-
return { ...DEFAULT_RETRY_POLICY, ...policy };
|
|
139
|
-
}
|
|
140
|
-
function shouldRetryStatus(policy, status) {
|
|
141
|
-
if (policy.neverRetryStatus.includes(status)) return false;
|
|
142
|
-
if (status === 429) return policy.retryOn429;
|
|
143
|
-
if (status === 503) return true;
|
|
144
|
-
if (status >= 500 && status <= 599) return policy.retryOn5xx;
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
function truthyHeader(value) {
|
|
148
|
-
return ["1", "true", "yes", "on"].includes((value ?? "").trim().toLowerCase());
|
|
149
|
-
}
|
|
150
|
-
function gatewaySaysBusyRetry(res, retryAfter) {
|
|
151
|
-
if (res.status !== 429 && res.status !== 503) return false;
|
|
152
|
-
if (truthyHeader(res.headers.get("x-should-retry"))) return true;
|
|
153
|
-
return retryAfter !== void 0;
|
|
154
|
-
}
|
|
155
|
-
function shouldRetryResponse(policy, res, retryAfter) {
|
|
156
|
-
if (policy.neverRetryStatus.includes(res.status)) return false;
|
|
157
|
-
if (res.status === 429 || res.status === 503) {
|
|
158
|
-
if (res.status === 429 && !policy.retryOn429) return false;
|
|
159
|
-
return gatewaySaysBusyRetry(res, retryAfter);
|
|
160
|
-
}
|
|
161
|
-
if (!shouldRetryStatus(policy, res.status)) return false;
|
|
162
|
-
return true;
|
|
163
|
-
}
|
|
164
|
-
function delayFor(policy, attempt, res, retryAfter) {
|
|
165
|
-
const jitter = (seconds) => {
|
|
166
|
-
const frac = policy.jitterFraction ?? 0.2;
|
|
167
|
-
const capped = Math.min(policy.backoffMaxSeconds, seconds);
|
|
168
|
-
if (frac <= 0) return capped;
|
|
169
|
-
return Math.min(policy.backoffMaxSeconds, capped * (1 + Math.random() * frac));
|
|
170
|
-
};
|
|
171
|
-
if (res !== null && (res.status === 429 || res.status === 503) && !policy.ignoreRetryAfter && retryAfter !== void 0) {
|
|
172
|
-
return jitter(retryAfter);
|
|
173
|
-
}
|
|
174
|
-
return jitter(policy.backoffBaseSeconds * 2 ** attempt);
|
|
175
|
-
}
|
|
176
|
-
function isConnectionError(err) {
|
|
177
|
-
return err instanceof TypeError;
|
|
178
|
-
}
|
|
179
|
-
var defaultSleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1e3));
|
|
180
|
-
async function peekJson(res) {
|
|
181
|
-
try {
|
|
182
|
-
return await res.clone().json();
|
|
183
|
-
} catch {
|
|
184
|
-
return void 0;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
async function executeWithRetry(send, policy, sleep = defaultSleep) {
|
|
188
|
-
if (policy === null) return send();
|
|
189
|
-
let attempt = 0;
|
|
190
|
-
for (; ; ) {
|
|
191
|
-
let res;
|
|
192
|
-
try {
|
|
193
|
-
res = await send();
|
|
194
|
-
} catch (err) {
|
|
195
|
-
if (policy.retryOnConnectionErrors && isConnectionError(err) && attempt < policy.maxRetries) {
|
|
196
|
-
await sleep(delayFor(policy, attempt, null, void 0));
|
|
197
|
-
attempt += 1;
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
throw err;
|
|
201
|
-
}
|
|
202
|
-
const retryAfter = res.status === 429 || res.status === 503 ? gatewayRetryAfter(res, await peekJson(res)) : void 0;
|
|
203
|
-
if (attempt < policy.maxRetries && shouldRetryResponse(policy, res, retryAfter)) {
|
|
204
|
-
await sleep(delayFor(policy, attempt, res, retryAfter));
|
|
205
|
-
attempt += 1;
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
return res;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
127
|
// src/decision.ts
|
|
213
128
|
var DECISION_ALLOW = "allow";
|
|
214
129
|
var DECISION_ALLOW_ALTERED = "allow_altered";
|
|
@@ -759,27 +674,30 @@ var Shashu = class {
|
|
|
759
674
|
const context = resolveContext(this.config, options);
|
|
760
675
|
const url = path.startsWith("http") ? path : `${this.resolvedBaseUrl}/${path.replace(/^\//, "")}`;
|
|
761
676
|
const timeout = options?.timeout ?? 6e4;
|
|
762
|
-
const
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
this.config,
|
|
766
|
-
options,
|
|
767
|
-
context,
|
|
768
|
-
{
|
|
677
|
+
const headersOnce = applyContractVersion(
|
|
678
|
+
{
|
|
679
|
+
...mergeHeaders(this.config, options, context, {
|
|
769
680
|
defaultSessionId: this._sessionId,
|
|
770
681
|
requestSessionId: options?.sessionId
|
|
771
|
-
},
|
|
772
|
-
|
|
773
|
-
|
|
682
|
+
}),
|
|
683
|
+
...init.headers
|
|
684
|
+
},
|
|
685
|
+
this.config.contractVersion
|
|
686
|
+
);
|
|
687
|
+
const frozenHeaders = { ...headersOnce };
|
|
688
|
+
delete frozenHeaders["authorization"];
|
|
689
|
+
delete frozenHeaders["Authorization"];
|
|
690
|
+
const pinned = pinOutgoing(url, { ...init, headers: frozenHeaders });
|
|
691
|
+
const send = async () => {
|
|
692
|
+
const token = await this.resolver.get();
|
|
774
693
|
const controller = new AbortController();
|
|
775
694
|
const id = setTimeout(() => controller.abort(), timeout);
|
|
776
695
|
try {
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
),
|
|
696
|
+
const headers = { ...pinned.init?.headers };
|
|
697
|
+
if (token) headers["authorization"] = `Bearer ${token}`;
|
|
698
|
+
return await joiningFetch(pinned.input, {
|
|
699
|
+
...pinned.init,
|
|
700
|
+
headers,
|
|
783
701
|
signal: init.signal ?? controller.signal
|
|
784
702
|
});
|
|
785
703
|
} finally {
|
|
@@ -99,7 +99,7 @@ function requireBaseUrl(baseUrl, opts) {
|
|
|
99
99
|
var import_node_crypto2 = require("crypto");
|
|
100
100
|
|
|
101
101
|
// src/version.ts
|
|
102
|
-
var SDK_VERSION = "1.1.
|
|
102
|
+
var SDK_VERSION = "1.1.1";
|
|
103
103
|
|
|
104
104
|
// src/tracer.ts
|
|
105
105
|
var import_node_async_hooks = require("async_hooks");
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createShashuAnthropicConfig,
|
|
3
3
|
getAnthropicHeaders
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-3GAMCQ6R.js";
|
|
5
|
+
import "../chunk-M7EMWSNS.js";
|
|
6
|
+
import "../chunk-TGD6KDWT.js";
|
|
7
7
|
export {
|
|
8
8
|
createShashuAnthropicConfig,
|
|
9
9
|
getAnthropicHeaders
|
|
@@ -107,7 +107,7 @@ function rejectKeyOnly(name, tokenProvider) {
|
|
|
107
107
|
var import_node_crypto2 = require("crypto");
|
|
108
108
|
|
|
109
109
|
// src/version.ts
|
|
110
|
-
var SDK_VERSION = "1.1.
|
|
110
|
+
var SDK_VERSION = "1.1.1";
|
|
111
111
|
|
|
112
112
|
// src/tracer.ts
|
|
113
113
|
var import_node_async_hooks = require("async_hooks");
|
|
@@ -73,6 +73,29 @@ var ShashuConfigError = class _ShashuConfigError extends ShashuError {
|
|
|
73
73
|
Object.setPrototypeOf(this, _ShashuConfigError.prototype);
|
|
74
74
|
}
|
|
75
75
|
};
|
|
76
|
+
function rateLimitDetail(err) {
|
|
77
|
+
const inner = err?.details?.error;
|
|
78
|
+
return inner && typeof inner === "object" ? inner : {};
|
|
79
|
+
}
|
|
80
|
+
function gatewayRetryAfter(res, parsedBody) {
|
|
81
|
+
const fromHeader = parseRetryAfter(res.headers.get("retry-after"));
|
|
82
|
+
if (fromHeader !== void 0) return fromHeader;
|
|
83
|
+
if (!parsedBody || typeof parsedBody !== "object") return void 0;
|
|
84
|
+
const err = parsedBody.error;
|
|
85
|
+
const seconds = rateLimitDetail(err).retry_after_seconds;
|
|
86
|
+
return typeof seconds === "number" ? seconds : void 0;
|
|
87
|
+
}
|
|
88
|
+
function parseRetryAfter(value) {
|
|
89
|
+
if (!value) return void 0;
|
|
90
|
+
const trimmed = value.trim();
|
|
91
|
+
const asInt = Number(trimmed);
|
|
92
|
+
if (Number.isFinite(asInt)) return asInt;
|
|
93
|
+
const asDate = Date.parse(trimmed);
|
|
94
|
+
if (!Number.isNaN(asDate)) {
|
|
95
|
+
return Math.max(0, (asDate - Date.now()) / 1e3);
|
|
96
|
+
}
|
|
97
|
+
return void 0;
|
|
98
|
+
}
|
|
76
99
|
|
|
77
100
|
// src/config.ts
|
|
78
101
|
var BASE_URL_ENV = "SHASHU_BASE_URL";
|
|
@@ -115,7 +138,7 @@ function requireBaseUrl(baseUrl, opts) {
|
|
|
115
138
|
var import_node_crypto2 = require("crypto");
|
|
116
139
|
|
|
117
140
|
// src/version.ts
|
|
118
|
-
var SDK_VERSION = "1.1.
|
|
141
|
+
var SDK_VERSION = "1.1.1";
|
|
119
142
|
|
|
120
143
|
// src/tracer.ts
|
|
121
144
|
var import_node_async_hooks = require("async_hooks");
|
|
@@ -806,7 +829,6 @@ function attachMemoryContext(body, memoryContext) {
|
|
|
806
829
|
}
|
|
807
830
|
|
|
808
831
|
// src/_joiner.ts
|
|
809
|
-
var globalFetch = (input, init) => globalThis.fetch(input, init);
|
|
810
832
|
function parseJsonObject(body) {
|
|
811
833
|
if (typeof body !== "string") return void 0;
|
|
812
834
|
if (!/^[\s]*\{/.test(body)) return void 0;
|
|
@@ -887,38 +909,23 @@ function takeContextFor(body) {
|
|
|
887
909
|
}
|
|
888
910
|
return mutated ? body : void 0;
|
|
889
911
|
}
|
|
890
|
-
function
|
|
891
|
-
return
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
}
|
|
908
|
-
return underlying(replacement, init);
|
|
909
|
-
}
|
|
910
|
-
const body = parseJsonObject(init?.body);
|
|
911
|
-
if (body === void 0) return underlying(input, init);
|
|
912
|
-
const merged = takeContextFor(body);
|
|
913
|
-
if (merged === void 0) return underlying(input, init);
|
|
914
|
-
const encoded = JSON.stringify(merged);
|
|
915
|
-
return underlying(input, {
|
|
916
|
-
...init,
|
|
917
|
-
body: encoded,
|
|
918
|
-
headers: fixContentLength(init?.headers, byteLength(encoded))
|
|
919
|
-
});
|
|
920
|
-
} catch {
|
|
921
|
-
return underlying(input, init);
|
|
912
|
+
function pinOutgoing(input, init) {
|
|
913
|
+
if (!hasEvents()) return { input, init };
|
|
914
|
+
if (isDisabled()) {
|
|
915
|
+
clear();
|
|
916
|
+
return { input, init };
|
|
917
|
+
}
|
|
918
|
+
const body = parseJsonObject(init?.body);
|
|
919
|
+
if (body === void 0) return { input, init };
|
|
920
|
+
const merged = takeContextFor(body);
|
|
921
|
+
if (merged === void 0) return { input, init };
|
|
922
|
+
const encoded = JSON.stringify(merged);
|
|
923
|
+
return {
|
|
924
|
+
input,
|
|
925
|
+
init: {
|
|
926
|
+
...init,
|
|
927
|
+
body: encoded,
|
|
928
|
+
headers: fixContentLength(init?.headers, byteLength(encoded))
|
|
922
929
|
}
|
|
923
930
|
};
|
|
924
931
|
}
|
|
@@ -926,6 +933,95 @@ function byteLength(text) {
|
|
|
926
933
|
return new TextEncoder().encode(text).length;
|
|
927
934
|
}
|
|
928
935
|
|
|
936
|
+
// src/retry.ts
|
|
937
|
+
var DEFAULT_RETRY_POLICY = {
|
|
938
|
+
maxRetries: 2,
|
|
939
|
+
backoffBaseSeconds: 0.5,
|
|
940
|
+
backoffMaxSeconds: 30,
|
|
941
|
+
retryOnConnectionErrors: true,
|
|
942
|
+
retryOn429: true,
|
|
943
|
+
retryOn5xx: false,
|
|
944
|
+
neverRetryStatus: [400, 401, 403, 404],
|
|
945
|
+
ignoreRetryAfter: false,
|
|
946
|
+
jitterFraction: 0.2
|
|
947
|
+
};
|
|
948
|
+
function resolveRetryPolicy(policy) {
|
|
949
|
+
if (policy === null) return null;
|
|
950
|
+
if (policy === void 0) return { ...DEFAULT_RETRY_POLICY };
|
|
951
|
+
return { ...DEFAULT_RETRY_POLICY, ...policy };
|
|
952
|
+
}
|
|
953
|
+
function shouldRetryStatus(policy, status) {
|
|
954
|
+
if (policy.neverRetryStatus.includes(status)) return false;
|
|
955
|
+
if (status === 429) return policy.retryOn429;
|
|
956
|
+
if (status === 503) return true;
|
|
957
|
+
if (status >= 500 && status <= 599) return policy.retryOn5xx;
|
|
958
|
+
return false;
|
|
959
|
+
}
|
|
960
|
+
function truthyHeader(value) {
|
|
961
|
+
return ["1", "true", "yes", "on"].includes((value ?? "").trim().toLowerCase());
|
|
962
|
+
}
|
|
963
|
+
function gatewaySaysBusyRetry(res, retryAfter) {
|
|
964
|
+
if (res.status !== 429 && res.status !== 503) return false;
|
|
965
|
+
if (truthyHeader(res.headers.get("x-should-retry"))) return true;
|
|
966
|
+
return retryAfter !== void 0;
|
|
967
|
+
}
|
|
968
|
+
function shouldRetryResponse(policy, res, retryAfter) {
|
|
969
|
+
if (policy.neverRetryStatus.includes(res.status)) return false;
|
|
970
|
+
if (res.status === 429 || res.status === 503) {
|
|
971
|
+
if (res.status === 429 && !policy.retryOn429) return false;
|
|
972
|
+
return gatewaySaysBusyRetry(res, retryAfter);
|
|
973
|
+
}
|
|
974
|
+
if (!shouldRetryStatus(policy, res.status)) return false;
|
|
975
|
+
return true;
|
|
976
|
+
}
|
|
977
|
+
function delayFor(policy, attempt, res, retryAfter) {
|
|
978
|
+
const jitter = (seconds) => {
|
|
979
|
+
const frac = policy.jitterFraction ?? 0.2;
|
|
980
|
+
const capped = Math.min(policy.backoffMaxSeconds, seconds);
|
|
981
|
+
if (frac <= 0) return capped;
|
|
982
|
+
return Math.min(policy.backoffMaxSeconds, capped * (1 + Math.random() * frac));
|
|
983
|
+
};
|
|
984
|
+
if (res !== null && (res.status === 429 || res.status === 503) && !policy.ignoreRetryAfter && retryAfter !== void 0) {
|
|
985
|
+
return jitter(retryAfter);
|
|
986
|
+
}
|
|
987
|
+
return jitter(policy.backoffBaseSeconds * 2 ** attempt);
|
|
988
|
+
}
|
|
989
|
+
function isConnectionError(err) {
|
|
990
|
+
return err instanceof TypeError;
|
|
991
|
+
}
|
|
992
|
+
var defaultSleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1e3));
|
|
993
|
+
async function peekJson(res) {
|
|
994
|
+
try {
|
|
995
|
+
return await res.clone().json();
|
|
996
|
+
} catch {
|
|
997
|
+
return void 0;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
async function executeWithRetry(send, policy, sleep = defaultSleep) {
|
|
1001
|
+
if (policy === null) return send();
|
|
1002
|
+
let attempt = 0;
|
|
1003
|
+
for (; ; ) {
|
|
1004
|
+
let res;
|
|
1005
|
+
try {
|
|
1006
|
+
res = await send();
|
|
1007
|
+
} catch (err) {
|
|
1008
|
+
if (policy.retryOnConnectionErrors && isConnectionError(err) && attempt < policy.maxRetries) {
|
|
1009
|
+
await sleep(delayFor(policy, attempt, null, void 0));
|
|
1010
|
+
attempt += 1;
|
|
1011
|
+
continue;
|
|
1012
|
+
}
|
|
1013
|
+
throw err;
|
|
1014
|
+
}
|
|
1015
|
+
const retryAfter = res.status === 429 || res.status === 503 ? gatewayRetryAfter(res, await peekJson(res)) : void 0;
|
|
1016
|
+
if (attempt < policy.maxRetries && shouldRetryResponse(policy, res, retryAfter)) {
|
|
1017
|
+
await sleep(delayFor(policy, attempt, res, retryAfter));
|
|
1018
|
+
attempt += 1;
|
|
1019
|
+
continue;
|
|
1020
|
+
}
|
|
1021
|
+
return res;
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
|
|
929
1025
|
// src/credential.ts
|
|
930
1026
|
var BOTH_OR_NEITHER = "Give either a SHASHU key or a token provider, not both.";
|
|
931
1027
|
var REFRESH_SKEW_SECONDS = 60;
|
|
@@ -1029,12 +1125,21 @@ function createShashuLangChainConfig(config) {
|
|
|
1029
1125
|
...buildContextHeaders(ctx, { defaultSessionId: config.sessionId ?? (0, import_node_crypto3.randomUUID)() })
|
|
1030
1126
|
};
|
|
1031
1127
|
applyContractVersion(headers, config.contractVersion);
|
|
1032
|
-
const joining = withRetrievalJoiner();
|
|
1033
1128
|
const fetch2 = async (input, init) => {
|
|
1034
|
-
const
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1129
|
+
const baseHeaders = new Headers(init?.headers);
|
|
1130
|
+
baseHeaders.delete("authorization");
|
|
1131
|
+
const headerObj = {};
|
|
1132
|
+
baseHeaders.forEach((v, k) => {
|
|
1133
|
+
headerObj[k] = v;
|
|
1134
|
+
});
|
|
1135
|
+
const pinned = pinOutgoing(input, { ...init, headers: headerObj });
|
|
1136
|
+
const send = async () => {
|
|
1137
|
+
const token = await resolver.get();
|
|
1138
|
+
const headers2 = { ...pinned.init?.headers || {} };
|
|
1139
|
+
if (token) headers2.authorization = `Bearer ${token}`;
|
|
1140
|
+
return globalThis.fetch(pinned.input, { ...pinned.init, headers: headers2 });
|
|
1141
|
+
};
|
|
1142
|
+
const res = await executeWithRetry(send, resolveRetryPolicy(config.retryPolicy));
|
|
1038
1143
|
if (!resolver.usesProvider || res.status !== 401) return res;
|
|
1039
1144
|
const code = passRefusalCodeFromText(await res.clone().text());
|
|
1040
1145
|
if (!shouldRetryPassRefused({
|
|
@@ -1046,9 +1151,7 @@ function createShashuLangChainConfig(config) {
|
|
|
1046
1151
|
return res;
|
|
1047
1152
|
}
|
|
1048
1153
|
resolver.invalidate();
|
|
1049
|
-
|
|
1050
|
-
if (retryToken) next.set("authorization", `Bearer ${retryToken}`);
|
|
1051
|
-
return joining(input, { ...init, headers: next });
|
|
1154
|
+
return send();
|
|
1052
1155
|
};
|
|
1053
1156
|
return {
|
|
1054
1157
|
baseURL: baseUrl,
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import '../types-BLs_TMFv.cjs';
|
|
2
|
-
import '../_joiner-
|
|
3
|
-
export { g as DEFAULT_CORPUS, h as SOURCE, i as ShashuLangChainCallback, e as createShashuLangChainConfig, j as install, k as isInstalled, l as lcDocumentsToRagContext, u as uninstall } from '../langchain-
|
|
2
|
+
import '../_joiner-DGi8GsY7.cjs';
|
|
3
|
+
export { g as DEFAULT_CORPUS, h as SOURCE, i as ShashuLangChainCallback, e as createShashuLangChainConfig, j as install, k as isInstalled, l as lcDocumentsToRagContext, u as uninstall } from '../langchain-DKyrUjk4.cjs';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import '../types-BLs_TMFv.js';
|
|
2
|
-
import '../_joiner-
|
|
3
|
-
export { g as DEFAULT_CORPUS, h as SOURCE, i as ShashuLangChainCallback, e as createShashuLangChainConfig, j as install, k as isInstalled, l as lcDocumentsToRagContext, u as uninstall } from '../langchain-
|
|
2
|
+
import '../_joiner-DGi8GsY7.js';
|
|
3
|
+
export { g as DEFAULT_CORPUS, h as SOURCE, i as ShashuLangChainCallback, e as createShashuLangChainConfig, j as install, k as isInstalled, l as lcDocumentsToRagContext, u as uninstall } from '../langchain-QCATTIR4.js';
|
|
@@ -7,9 +7,9 @@ import {
|
|
|
7
7
|
isInstalled,
|
|
8
8
|
lcDocumentsToRagContext,
|
|
9
9
|
uninstall
|
|
10
|
-
} from "../chunk-
|
|
11
|
-
import "../chunk-
|
|
12
|
-
import "../chunk-
|
|
10
|
+
} from "../chunk-F4CXDZQB.js";
|
|
11
|
+
import "../chunk-M7EMWSNS.js";
|
|
12
|
+
import "../chunk-TGD6KDWT.js";
|
|
13
13
|
export {
|
|
14
14
|
DEFAULT_CORPUS,
|
|
15
15
|
SOURCE,
|
|
@@ -99,7 +99,7 @@ function requireBaseUrl(baseUrl, opts) {
|
|
|
99
99
|
var import_node_crypto2 = require("crypto");
|
|
100
100
|
|
|
101
101
|
// src/version.ts
|
|
102
|
-
var SDK_VERSION = "1.1.
|
|
102
|
+
var SDK_VERSION = "1.1.1";
|
|
103
103
|
|
|
104
104
|
// src/tracer.ts
|
|
105
105
|
var import_node_async_hooks = require("async_hooks");
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createShashuOpenAIOptions,
|
|
3
3
|
getShashuHeaders
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-3X5IUBIA.js";
|
|
5
|
+
import "../chunk-M7EMWSNS.js";
|
|
6
|
+
import "../chunk-TGD6KDWT.js";
|
|
7
7
|
export {
|
|
8
8
|
createShashuOpenAIOptions,
|
|
9
9
|
getShashuHeaders
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shashu-ai/sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.1",
|
|
5
5
|
"description": "Node.js client for SHASHU by Trustgate AI, a self-hosted AI security gateway. Point it at your own gateway; there is no default endpoint.",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.js",
|