@shashu-ai/sdk 1.1.0 → 1.2.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 +61 -44
- package/NOTICE +1 -1
- package/README.md +7 -4
- package/dist/{_joiner-CmR7qVK8.d.cts → _joiner-D_FGAaoc.d.cts} +1 -11
- package/dist/{_joiner-CmR7qVK8.d.ts → _joiner-D_FGAaoc.d.ts} +1 -11
- package/dist/{chunk-QJQ4MWQ4.js → chunk-CWC6GQTL.js} +2 -2
- package/dist/{chunk-QLK5CQSP.js → chunk-K4ZIXS2U.js} +1 -1
- package/dist/{chunk-WC3BKVXK.js → chunk-MLFWJAEH.js} +2 -2
- package/dist/{chunk-NTIOPTYF.js → chunk-MZGHCDWX.js} +23 -2
- package/dist/{chunk-QHWUNZQS.js → chunk-TI45NK2R.js} +2 -2
- package/dist/{chunk-L6GKR3SC.js → chunk-YSAQRZFP.js} +127 -12
- package/dist/index.cjs +80 -37
- package/dist/index.d.cts +17 -17
- package/dist/index.d.ts +17 -17
- package/dist/index.js +37 -119
- package/dist/{langchain-B9Q9eL7g.d.cts → langchain-CoNoZ_vQ.d.cts} +4 -4
- package/dist/{langchain-CHjjdhDr.d.ts → langchain-D9Xo9IMm.d.ts} +4 -4
- package/dist/middleware/anthropic.cjs +2 -2
- package/dist/middleware/anthropic.d.cts +3 -3
- package/dist/middleware/anthropic.d.ts +3 -3
- package/dist/middleware/anthropic.js +3 -3
- package/dist/middleware/google.cjs +1 -1
- package/dist/middleware/google.d.cts +2 -2
- package/dist/middleware/google.d.ts +2 -2
- package/dist/middleware/google.js +2 -2
- package/dist/middleware/langchain.cjs +159 -43
- package/dist/middleware/langchain.d.cts +3 -3
- package/dist/middleware/langchain.d.ts +3 -3
- package/dist/middleware/langchain.js +3 -3
- package/dist/middleware/vercel-ai.cjs +2 -2
- package/dist/middleware/vercel-ai.d.cts +3 -3
- package/dist/middleware/vercel-ai.d.ts +3 -3
- package/dist/middleware/vercel-ai.js +3 -3
- package/dist/{types-BLs_TMFv.d.ts → types-BQVfxZJP.d.cts} +7 -3
- package/dist/{types-BLs_TMFv.d.cts → types-BQVfxZJP.d.ts} +7 -3
- package/package.json +3 -4
package/dist/index.js
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createShashuAnthropicConfig,
|
|
3
3
|
getAnthropicHeaders
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-MLFWJAEH.js";
|
|
5
5
|
import {
|
|
6
6
|
createShashuGeminiConfig,
|
|
7
7
|
getGeminiHeaders
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-K4ZIXS2U.js";
|
|
9
9
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
DEFAULT_RETRY_POLICY,
|
|
11
|
+
createShashuLangChainConfig,
|
|
12
|
+
executeWithRetry,
|
|
13
|
+
resolveRetryPolicy
|
|
14
|
+
} from "./chunk-YSAQRZFP.js";
|
|
12
15
|
import {
|
|
13
16
|
createShashuOpenAIOptions,
|
|
14
17
|
getShashuHeaders
|
|
15
|
-
} from "./chunk-
|
|
18
|
+
} from "./chunk-TI45NK2R.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-MZGHCDWX.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-CWC6GQTL.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";
|
|
@@ -461,10 +376,10 @@ function blockSource(bodyEnv, headers) {
|
|
|
461
376
|
}
|
|
462
377
|
return headers != null ? { headers } : void 0;
|
|
463
378
|
}
|
|
464
|
-
var SOURCE = /* @__PURE__ */ Symbol("
|
|
465
|
-
var ATTACHMENTS = /* @__PURE__ */ Symbol("
|
|
466
|
-
var MEMORY = /* @__PURE__ */ Symbol("
|
|
467
|
-
var UNREAD = /* @__PURE__ */ Symbol("
|
|
379
|
+
var SOURCE = /* @__PURE__ */ Symbol("shashu.decision.blocks");
|
|
380
|
+
var ATTACHMENTS = /* @__PURE__ */ Symbol("shashu.decision.attachments");
|
|
381
|
+
var MEMORY = /* @__PURE__ */ Symbol("shashu.decision.memory");
|
|
382
|
+
var UNREAD = /* @__PURE__ */ Symbol("shashu.decision.unread");
|
|
468
383
|
var _a, _b;
|
|
469
384
|
SOURCE, _b = ATTACHMENTS, _a = MEMORY;
|
|
470
385
|
var DecisionRecord = class {
|
|
@@ -644,14 +559,14 @@ function isShashuBlock(err) {
|
|
|
644
559
|
}
|
|
645
560
|
|
|
646
561
|
// src/client.ts
|
|
647
|
-
var USER_AGENT = `
|
|
562
|
+
var USER_AGENT = `shashu-node/${SDK_VERSION}`;
|
|
648
563
|
var joiningFetch = withRetrievalJoiner();
|
|
649
564
|
var streamingInertWarningEmitted = false;
|
|
650
565
|
function warnStreamingInert() {
|
|
651
566
|
if (streamingInertWarningEmitted) return;
|
|
652
567
|
streamingInertWarningEmitted = true;
|
|
653
568
|
console.warn(
|
|
654
|
-
"[
|
|
569
|
+
"[shashu] Streaming is not yet supported end-to-end: the gateway buffers /v1/chat/completions and returns full JSON regardless of the `stream` flag, so no incremental tokens are produced. fetchStream() will fall back to the buffered response. (warned once per process)"
|
|
655
570
|
);
|
|
656
571
|
}
|
|
657
572
|
function resolveContext(config, options) {
|
|
@@ -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 {
|
|
@@ -911,7 +829,7 @@ function shashuFetch(options = {}) {
|
|
|
911
829
|
if (options.headers) {
|
|
912
830
|
for (const [k, v] of Object.entries(options.headers)) baked[k.toLowerCase()] = v;
|
|
913
831
|
}
|
|
914
|
-
return async function
|
|
832
|
+
return async function shashuFetching(input, init) {
|
|
915
833
|
const send = async () => {
|
|
916
834
|
const headers = { ...baked };
|
|
917
835
|
const extra = init?.headers;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as ShashuConfig } from './types-
|
|
2
|
-
import { F as FetchLike } from './_joiner-
|
|
1
|
+
import { S as ShashuConfig } from './types-BQVfxZJP.cjs';
|
|
2
|
+
import { F as FetchLike } from './_joiner-D_FGAaoc.cjs';
|
|
3
3
|
|
|
4
4
|
/** One document as `rag_context.v1` spells it. */
|
|
5
5
|
interface BusDoc {
|
|
@@ -32,7 +32,7 @@ interface RetrievalEvent {
|
|
|
32
32
|
* the LLM provider, so the upstream model never sees the control plane.
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
35
|
-
* import { attachRagContext } from "@
|
|
35
|
+
* import { attachRagContext } from "@shashu-ai/sdk";
|
|
36
36
|
* const body = { model: "gpt-4o-mini", messages: [...] };
|
|
37
37
|
* attachRagContext(body, {
|
|
38
38
|
* corpus: "policy_docs_v2",
|
|
@@ -234,7 +234,7 @@ declare function buildRagContext(events: readonly RetrievalEvent[]): RagContext
|
|
|
234
234
|
* Returns `{}` when SHASHU_DISABLE is set: the provider client is then left
|
|
235
235
|
* exactly as it was — its own endpoint, its own credential, no Shashu context
|
|
236
236
|
* headers — which is the same no-op the Python SDK's patchers perform. The
|
|
237
|
-
* caller's
|
|
237
|
+
* caller's shashu_sk_ key is deliberately NOT handed back, since it would be sent to
|
|
238
238
|
* the provider directly.
|
|
239
239
|
*
|
|
240
240
|
* @throws {ShashuConfigError} when no address is configured and the SDK is
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as ShashuConfig } from './types-
|
|
2
|
-
import { F as FetchLike } from './_joiner-
|
|
1
|
+
import { S as ShashuConfig } from './types-BQVfxZJP.js';
|
|
2
|
+
import { F as FetchLike } from './_joiner-D_FGAaoc.js';
|
|
3
3
|
|
|
4
4
|
/** One document as `rag_context.v1` spells it. */
|
|
5
5
|
interface BusDoc {
|
|
@@ -32,7 +32,7 @@ interface RetrievalEvent {
|
|
|
32
32
|
* the LLM provider, so the upstream model never sees the control plane.
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
35
|
-
* import { attachRagContext } from "@
|
|
35
|
+
* import { attachRagContext } from "@shashu-ai/sdk";
|
|
36
36
|
* const body = { model: "gpt-4o-mini", messages: [...] };
|
|
37
37
|
* attachRagContext(body, {
|
|
38
38
|
* corpus: "policy_docs_v2",
|
|
@@ -234,7 +234,7 @@ declare function buildRagContext(events: readonly RetrievalEvent[]): RagContext
|
|
|
234
234
|
* Returns `{}` when SHASHU_DISABLE is set: the provider client is then left
|
|
235
235
|
* exactly as it was — its own endpoint, its own credential, no Shashu context
|
|
236
236
|
* headers — which is the same no-op the Python SDK's patchers perform. The
|
|
237
|
-
* caller's
|
|
237
|
+
* caller's shashu_sk_ key is deliberately NOT handed back, since it would be sent to
|
|
238
238
|
* the provider directly.
|
|
239
239
|
*
|
|
240
240
|
* @throws {ShashuConfigError} when no address is configured and the SDK is
|
|
@@ -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.
|
|
102
|
+
var SDK_VERSION = "1.2.0";
|
|
103
103
|
|
|
104
104
|
// src/tracer.ts
|
|
105
105
|
var import_node_async_hooks = require("async_hooks");
|
|
@@ -744,7 +744,7 @@ function takeContextFor(body) {
|
|
|
744
744
|
return mutated ? body : void 0;
|
|
745
745
|
}
|
|
746
746
|
function withRetrievalJoiner(underlying = globalFetch) {
|
|
747
|
-
return async function
|
|
747
|
+
return async function shashuJoiningFetch(input, init) {
|
|
748
748
|
if (!hasEvents()) return underlying(input, init);
|
|
749
749
|
if (isDisabled()) {
|
|
750
750
|
clear();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as ShashuConfig } from '../types-
|
|
2
|
-
import { F as FetchLike } from '../_joiner-
|
|
1
|
+
import { S as ShashuConfig } from '../types-BQVfxZJP.cjs';
|
|
2
|
+
import { F as FetchLike } from '../_joiner-D_FGAaoc.cjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Anthropic middleware: use Shashu as the base URL for @anthropic-ai/sdk.
|
|
@@ -33,7 +33,7 @@ declare function getAnthropicHeaders(config: ShashuConfig): Record<string, strin
|
|
|
33
33
|
* Returns `{}` when SHASHU_DISABLE is set: the provider client is then left
|
|
34
34
|
* exactly as it was — its own endpoint, its own credential, no Shashu context
|
|
35
35
|
* headers — which is the same no-op the Python SDK's patchers perform. The
|
|
36
|
-
* caller's
|
|
36
|
+
* caller's shashu_sk_ key is deliberately NOT handed back, since it would be sent to
|
|
37
37
|
* the provider directly.
|
|
38
38
|
*
|
|
39
39
|
* @throws {ShashuConfigError} when no address is configured and the SDK is
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as ShashuConfig } from '../types-
|
|
2
|
-
import { F as FetchLike } from '../_joiner-
|
|
1
|
+
import { S as ShashuConfig } from '../types-BQVfxZJP.js';
|
|
2
|
+
import { F as FetchLike } from '../_joiner-D_FGAaoc.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Anthropic middleware: use Shashu as the base URL for @anthropic-ai/sdk.
|
|
@@ -33,7 +33,7 @@ declare function getAnthropicHeaders(config: ShashuConfig): Record<string, strin
|
|
|
33
33
|
* Returns `{}` when SHASHU_DISABLE is set: the provider client is then left
|
|
34
34
|
* exactly as it was — its own endpoint, its own credential, no Shashu context
|
|
35
35
|
* headers — which is the same no-op the Python SDK's patchers perform. The
|
|
36
|
-
* caller's
|
|
36
|
+
* caller's shashu_sk_ key is deliberately NOT handed back, since it would be sent to
|
|
37
37
|
* the provider directly.
|
|
38
38
|
*
|
|
39
39
|
* @throws {ShashuConfigError} when no address is configured and the SDK is
|
|
@@ -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-MLFWJAEH.js";
|
|
5
|
+
import "../chunk-MZGHCDWX.js";
|
|
6
|
+
import "../chunk-CWC6GQTL.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.
|
|
110
|
+
var SDK_VERSION = "1.2.0";
|
|
111
111
|
|
|
112
112
|
// src/tracer.ts
|
|
113
113
|
var import_node_async_hooks = require("async_hooks");
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { S as ShashuConfig } from '../types-
|
|
1
|
+
import { S as ShashuConfig } from '../types-BQVfxZJP.cjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Google Gemini middleware: use Shashu with @google/generative-ai.
|
|
@@ -39,7 +39,7 @@ declare function getGeminiHeaders(config: ShashuConfig): Record<string, string>;
|
|
|
39
39
|
* Returns `{}` when SHASHU_DISABLE is set: the provider client is then left
|
|
40
40
|
* exactly as it was — its own endpoint, its own credential, no Shashu context
|
|
41
41
|
* headers — which is the same no-op the Python SDK's patchers perform. The
|
|
42
|
-
* caller's
|
|
42
|
+
* caller's shashu_sk_ key is deliberately NOT handed back, since it would be sent to
|
|
43
43
|
* the provider directly.
|
|
44
44
|
*
|
|
45
45
|
* @throws {ShashuConfigError} when no address is configured and the SDK is
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { S as ShashuConfig } from '../types-
|
|
1
|
+
import { S as ShashuConfig } from '../types-BQVfxZJP.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Google Gemini middleware: use Shashu with @google/generative-ai.
|
|
@@ -39,7 +39,7 @@ declare function getGeminiHeaders(config: ShashuConfig): Record<string, string>;
|
|
|
39
39
|
* Returns `{}` when SHASHU_DISABLE is set: the provider client is then left
|
|
40
40
|
* exactly as it was — its own endpoint, its own credential, no Shashu context
|
|
41
41
|
* headers — which is the same no-op the Python SDK's patchers perform. The
|
|
42
|
-
* caller's
|
|
42
|
+
* caller's shashu_sk_ key is deliberately NOT handed back, since it would be sent to
|
|
43
43
|
* the provider directly.
|
|
44
44
|
*
|
|
45
45
|
* @throws {ShashuConfigError} when no address is configured and the SDK is
|
|
@@ -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.
|
|
141
|
+
var SDK_VERSION = "1.2.0";
|
|
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,108 @@ 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
|
+
var UNSENT_CODES = /* @__PURE__ */ new Set([
|
|
990
|
+
"ECONNREFUSED",
|
|
991
|
+
"ENOTFOUND",
|
|
992
|
+
"EAI_AGAIN",
|
|
993
|
+
"ENETUNREACH",
|
|
994
|
+
"EHOSTUNREACH",
|
|
995
|
+
"UND_ERR_CONNECT",
|
|
996
|
+
"UND_ERR_CONNECT_TIMEOUT"
|
|
997
|
+
]);
|
|
998
|
+
function requestNeverLeft(err) {
|
|
999
|
+
if (!(err instanceof TypeError)) return false;
|
|
1000
|
+
const cause = err.cause;
|
|
1001
|
+
const code = cause?.code ?? "";
|
|
1002
|
+
if (UNSENT_CODES.has(code)) return true;
|
|
1003
|
+
return cause?.name === "ConnectTimeoutError" || cause?.name === "ConnectError";
|
|
1004
|
+
}
|
|
1005
|
+
var defaultSleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1e3));
|
|
1006
|
+
async function peekJson(res) {
|
|
1007
|
+
try {
|
|
1008
|
+
return await res.clone().json();
|
|
1009
|
+
} catch {
|
|
1010
|
+
return void 0;
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
async function executeWithRetry(send, policy, sleep = defaultSleep) {
|
|
1014
|
+
if (policy === null) return send();
|
|
1015
|
+
let attempt = 0;
|
|
1016
|
+
for (; ; ) {
|
|
1017
|
+
let res;
|
|
1018
|
+
try {
|
|
1019
|
+
res = await send();
|
|
1020
|
+
} catch (err) {
|
|
1021
|
+
if (policy.retryOnConnectionErrors && requestNeverLeft(err) && attempt < policy.maxRetries) {
|
|
1022
|
+
await sleep(delayFor(policy, attempt, null, void 0));
|
|
1023
|
+
attempt += 1;
|
|
1024
|
+
continue;
|
|
1025
|
+
}
|
|
1026
|
+
throw err;
|
|
1027
|
+
}
|
|
1028
|
+
const retryAfter = res.status === 429 || res.status === 503 ? gatewayRetryAfter(res, await peekJson(res)) : void 0;
|
|
1029
|
+
if (attempt < policy.maxRetries && shouldRetryResponse(policy, res, retryAfter)) {
|
|
1030
|
+
await sleep(delayFor(policy, attempt, res, retryAfter));
|
|
1031
|
+
attempt += 1;
|
|
1032
|
+
continue;
|
|
1033
|
+
}
|
|
1034
|
+
return res;
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
|
|
929
1038
|
// src/credential.ts
|
|
930
1039
|
var BOTH_OR_NEITHER = "Give either a SHASHU key or a token provider, not both.";
|
|
931
1040
|
var REFRESH_SKEW_SECONDS = 60;
|
|
@@ -1029,12 +1138,21 @@ function createShashuLangChainConfig(config) {
|
|
|
1029
1138
|
...buildContextHeaders(ctx, { defaultSessionId: config.sessionId ?? (0, import_node_crypto3.randomUUID)() })
|
|
1030
1139
|
};
|
|
1031
1140
|
applyContractVersion(headers, config.contractVersion);
|
|
1032
|
-
const joining = withRetrievalJoiner();
|
|
1033
1141
|
const fetch2 = async (input, init) => {
|
|
1034
|
-
const
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1142
|
+
const baseHeaders = new Headers(init?.headers);
|
|
1143
|
+
baseHeaders.delete("authorization");
|
|
1144
|
+
const headerObj = {};
|
|
1145
|
+
baseHeaders.forEach((v, k) => {
|
|
1146
|
+
headerObj[k] = v;
|
|
1147
|
+
});
|
|
1148
|
+
const pinned = pinOutgoing(input, { ...init, headers: headerObj });
|
|
1149
|
+
const send = async () => {
|
|
1150
|
+
const token = await resolver.get();
|
|
1151
|
+
const headers2 = { ...pinned.init?.headers || {} };
|
|
1152
|
+
if (token) headers2.authorization = `Bearer ${token}`;
|
|
1153
|
+
return globalThis.fetch(pinned.input, { ...pinned.init, headers: headers2 });
|
|
1154
|
+
};
|
|
1155
|
+
const res = await executeWithRetry(send, resolveRetryPolicy(config.retryPolicy));
|
|
1038
1156
|
if (!resolver.usesProvider || res.status !== 401) return res;
|
|
1039
1157
|
const code = passRefusalCodeFromText(await res.clone().text());
|
|
1040
1158
|
if (!shouldRetryPassRefused({
|
|
@@ -1046,9 +1164,7 @@ function createShashuLangChainConfig(config) {
|
|
|
1046
1164
|
return res;
|
|
1047
1165
|
}
|
|
1048
1166
|
resolver.invalidate();
|
|
1049
|
-
|
|
1050
|
-
if (retryToken) next.set("authorization", `Bearer ${retryToken}`);
|
|
1051
|
-
return joining(input, { ...init, headers: next });
|
|
1167
|
+
return send();
|
|
1052
1168
|
};
|
|
1053
1169
|
return {
|
|
1054
1170
|
baseURL: baseUrl,
|
|
@@ -1085,7 +1201,7 @@ var SOURCE = "langchain";
|
|
|
1085
1201
|
var DEFAULT_CORPUS = "langchain-retriever";
|
|
1086
1202
|
var MAX_TRACKED_RUNS = 64;
|
|
1087
1203
|
var captured = /* @__PURE__ */ new Map();
|
|
1088
|
-
var QUERY = /* @__PURE__ */ Symbol.for("
|
|
1204
|
+
var QUERY = /* @__PURE__ */ Symbol.for("shashu.langchain.query");
|
|
1089
1205
|
var enabled = false;
|
|
1090
1206
|
var patched = false;
|
|
1091
1207
|
var corpusName = DEFAULT_CORPUS;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import '../types-
|
|
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-
|
|
1
|
+
import '../types-BQVfxZJP.cjs';
|
|
2
|
+
import '../_joiner-D_FGAaoc.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-CoNoZ_vQ.cjs';
|