@shashu-ai/sdk 1.0.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 +20 -0
- package/README.md +18 -18
- package/dist/{_joiner-p5LFxJcu.d.cts → _joiner-DGi8GsY7.d.cts} +1 -11
- package/dist/{_joiner-p5LFxJcu.d.ts → _joiner-DGi8GsY7.d.ts} +1 -11
- package/dist/{chunk-Z6DROYQA.js → chunk-3GAMCQ6R.js} +2 -2
- package/dist/{chunk-BEU2UWW3.js → chunk-3JZXUKNA.js} +1 -1
- package/dist/{chunk-M4JDSAUC.js → chunk-3X5IUBIA.js} +2 -2
- package/dist/{chunk-4CDQ7JMK.js → chunk-F4CXDZQB.js} +113 -11
- package/dist/{chunk-5TW3FISD.js → chunk-M7EMWSNS.js} +27 -6
- package/dist/{chunk-IQYB5OZ6.js → chunk-TGD6KDWT.js} +33 -53
- package/dist/index.cjs +133 -101
- package/dist/index.d.cts +30 -30
- package/dist/index.d.ts +30 -30
- package/dist/index.js +45 -109
- package/dist/{langchain-DNOsiRna.d.ts → langchain-DKyrUjk4.d.cts} +11 -11
- package/dist/{langchain-xCWwa1Ud.d.cts → langchain-QCATTIR4.d.ts} +11 -11
- package/dist/middleware/anthropic.cjs +34 -52
- package/dist/middleware/anthropic.d.cts +5 -5
- package/dist/middleware/anthropic.d.ts +5 -5
- package/dist/middleware/anthropic.js +3 -3
- package/dist/middleware/google.cjs +28 -46
- package/dist/middleware/google.d.cts +5 -5
- package/dist/middleware/google.d.ts +5 -5
- package/dist/middleware/google.js +2 -2
- package/dist/middleware/langchain.cjs +178 -93
- 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 +34 -52
- package/dist/middleware/vercel-ai.d.cts +8 -8
- package/dist/middleware/vercel-ai.d.ts +8 -8
- package/dist/middleware/vercel-ai.js +3 -3
- package/dist/{types-Rs49lJyD.d.ts → types-BLs_TMFv.d.cts} +18 -16
- package/dist/{types-Rs49lJyD.d.cts → types-BLs_TMFv.d.ts} +18 -16
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,26 @@ documented here. This project adheres to [Semantic Versioning](https://semver.or
|
|
|
5
5
|
|
|
6
6
|
## [Unreleased]
|
|
7
7
|
|
|
8
|
+
## [1.1.1] — 2026-11-25
|
|
9
|
+
|
|
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.
|
|
13
|
+
|
|
14
|
+
## [1.1.0] — 2026-11-25
|
|
15
|
+
|
|
16
|
+
Wire names moved to SHASHU (requires a SHASHU gateway); key prefix `tg_sk_` unchanged.
|
|
17
|
+
|
|
18
|
+
Retries when the gateway says busy (Retry-After + jitter, default 2 retries, configurable).
|
|
19
|
+
|
|
20
|
+
The one-shot `pass_expired` retry is unchanged and does not stack with busy-retry.
|
|
21
|
+
`x-shashu-hop-ticket` is not sent (never sent in 1.0.0). Planned later.
|
|
22
|
+
|
|
23
|
+
### Breaking
|
|
24
|
+
|
|
25
|
+
- Environment variables are `SHASHU_API_KEY` / `SHASHU_BASE_URL` / `SHASHU_DISABLE` only. The `TRUSTGATE_*` fallbacks are removed.
|
|
26
|
+
- Wire names are SHASHU only (`x-shashu-*`, `Shashu-Version`, `shashu_metadata`, body `shashu` / `error.shashu`). Old TrustGate names are neither sent nor read.
|
|
27
|
+
|
|
8
28
|
## [1.0.0] — 2026-11-14
|
|
9
29
|
|
|
10
30
|
Rename and cloud-pass sign-in. Keys (`tg_sk_…`) keep working exactly as before.
|
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ regulated industries. There is no default gateway hostname — you point the
|
|
|
5
5
|
client at your own address, and it raises a configuration error rather than
|
|
6
6
|
guessing. After the first mention, this document says **SHASHU**.
|
|
7
7
|
|
|
8
|
-
Wire names `x-
|
|
8
|
+
Wire names `x-shashu-*`, `shashu_metadata`, and the `tg_sk_` key prefix
|
|
9
9
|
are protocol names and are unchanged.
|
|
10
10
|
|
|
11
11
|
Requires Node.js 18 or later.
|
|
@@ -101,19 +101,19 @@ is on your network, under your name, and the SDK will not guess a host for you.
|
|
|
101
101
|
Every entry point resolves the address the same way:
|
|
102
102
|
|
|
103
103
|
1. the explicit `baseUrl` option, then
|
|
104
|
-
2. the `
|
|
104
|
+
2. the `SHASHU_BASE_URL` environment variable, then
|
|
105
105
|
3. a `ShashuConfigError`.
|
|
106
106
|
|
|
107
107
|
```ts
|
|
108
108
|
import { Shashu, ShashuConfigError } from '@shashu-ai/sdk';
|
|
109
109
|
|
|
110
110
|
try {
|
|
111
|
-
const client = new Shashu({ apiKey: process.env.
|
|
111
|
+
const client = new Shashu({ apiKey: process.env.SHASHU_API_KEY });
|
|
112
112
|
} catch (e) {
|
|
113
113
|
if (e instanceof ShashuConfigError) {
|
|
114
114
|
// "SHASHU gateway address is not configured, so new Shashu() has
|
|
115
115
|
// nothing to route to. Pass baseUrl to new Shashu(), or set the
|
|
116
|
-
//
|
|
116
|
+
// SHASHU_BASE_URL environment variable. ..."
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
```
|
|
@@ -124,14 +124,14 @@ rule 3: it is constructed when the module is imported, so an unset address
|
|
|
124
124
|
leaves it with no gateway and it simply sends no span telemetry rather than
|
|
125
125
|
breaking `import`.
|
|
126
126
|
|
|
127
|
-
### Turning the SDK off: `
|
|
127
|
+
### Turning the SDK off: `SHASHU_DISABLE`
|
|
128
128
|
|
|
129
|
-
Set `
|
|
129
|
+
Set `SHASHU_DISABLE=1` to make the SDK a no-op without a code change, so a
|
|
130
130
|
platform team can stop a deployed service's instrumentation from its
|
|
131
131
|
environment. `1`, `true`, `yes` and `on` are all accepted (any case).
|
|
132
132
|
|
|
133
133
|
```bash
|
|
134
|
-
|
|
134
|
+
SHASHU_DISABLE=1 node ./my-service.js
|
|
135
135
|
```
|
|
136
136
|
|
|
137
137
|
When it is set:
|
|
@@ -177,10 +177,10 @@ Six values, and the split that matters is whether you got the model's content:
|
|
|
177
177
|
gateway did not send a decision — that is not `allow`. `isShashuBlock()`
|
|
178
178
|
inspects; it patches nothing. An unrecognised decision fails closed (`true`).
|
|
179
179
|
|
|
180
|
-
**This SDK sends `
|
|
180
|
+
**This SDK sends `shashu-version: 2026-09-01` by default** (since 4.0.0), the
|
|
181
181
|
same value as the Trustgate AI Python SDK. The `Shashu` client,
|
|
182
182
|
`shashuFetch()` and the middleware configs all send it. With it, the body of
|
|
183
|
-
every reply carries the full `
|
|
183
|
+
every reply carries the full `shashu` block, so `decision.memory.items` lists
|
|
184
184
|
each memory save and recall and you get the inspection detail, not just header
|
|
185
185
|
counts. A refusal arrives as an error instead of a 200 reply: a content block is
|
|
186
186
|
an HTTP **400** (`ShashuContentFilterError`), a policy refusal a **403**
|
|
@@ -220,8 +220,8 @@ import { createShashuOpenAIOptions } from '@shashu-ai/sdk/middleware/vercel-ai';
|
|
|
220
220
|
|
|
221
221
|
const openai = createOpenAI(
|
|
222
222
|
createShashuOpenAIOptions({
|
|
223
|
-
baseUrl: process.env.
|
|
224
|
-
apiKey: process.env.
|
|
223
|
+
baseUrl: process.env.SHASHU_BASE_URL!,
|
|
224
|
+
apiKey: process.env.SHASHU_API_KEY,
|
|
225
225
|
})
|
|
226
226
|
);
|
|
227
227
|
|
|
@@ -242,8 +242,8 @@ import { ChatOpenAI } from '@langchain/openai';
|
|
|
242
242
|
import { createShashuLangChainConfig } from '@shashu-ai/sdk/middleware/langchain';
|
|
243
243
|
|
|
244
244
|
const config = createShashuLangChainConfig({
|
|
245
|
-
baseUrl: process.env.
|
|
246
|
-
apiKey: process.env.
|
|
245
|
+
baseUrl: process.env.SHASHU_BASE_URL!,
|
|
246
|
+
apiKey: process.env.SHASHU_API_KEY,
|
|
247
247
|
});
|
|
248
248
|
|
|
249
249
|
const llm = new ChatOpenAI({
|
|
@@ -259,7 +259,7 @@ retrieval to the outgoing body — see [Declaring retrieval](#declaring-retrieva
|
|
|
259
259
|
## Declaring retrieval (RAG)
|
|
260
260
|
|
|
261
261
|
The gateway scans retrieved documents on a dedicated `ingress-rag` surface when
|
|
262
|
-
the request body carries `
|
|
262
|
+
the request body carries `shashu_metadata.rag_context` — with the corpus, the
|
|
263
263
|
per-document previews and the scores, rather than as an undifferentiated wall of
|
|
264
264
|
user text. Declare what you retrieved **where you retrieved it**; the SDK carries
|
|
265
265
|
the declaration to the gateway on the next model call.
|
|
@@ -280,7 +280,7 @@ await openai.chat.completions.create({ model: 'gpt-4o-mini', messages });
|
|
|
280
280
|
`metadata`), LangChain `Document`s or LlamaIndex nodes. The payload is capped at
|
|
281
281
|
the contract's limits: 64 documents, 2048-character previews, 8 declarations,
|
|
282
282
|
newest kept. An explicit `attachRagContext()` on the body always wins, and
|
|
283
|
-
`
|
|
283
|
+
`SHASHU_DISABLE=1` makes the whole path inert.
|
|
284
284
|
|
|
285
285
|
### Where the declaration reaches
|
|
286
286
|
|
|
@@ -370,11 +370,11 @@ Get a pre-formatted JSON object of headers for n8n “HTTP Request” nodes so e
|
|
|
370
370
|
import { getN8nHeaderSnippet } from '@shashu-ai/sdk';
|
|
371
371
|
|
|
372
372
|
const { headers, json } = getN8nHeaderSnippet();
|
|
373
|
-
// headers: [ { name: 'X-
|
|
374
|
-
// json: '{"X-
|
|
373
|
+
// headers: [ { name: 'X-Shashu-Source', value: 'n8n' }, ... ]
|
|
374
|
+
// json: '{"X-Shashu-Source":"n8n","X-Shashu-Workflow-Id":"...", ...}'
|
|
375
375
|
```
|
|
376
376
|
|
|
377
|
-
In n8n, set **Send Headers** (or equivalent) to the `headers` array or paste the `json` into your node config. You can pass `apiKey` and `baseUrl` explicitly or rely on `
|
|
377
|
+
In n8n, set **Send Headers** (or equivalent) to the `headers` array or paste the `json` into your node config. You can pass `apiKey` and `baseUrl` explicitly or rely on `SHASHU_API_KEY` and `SHASHU_BASE_URL` in the environment.
|
|
378
378
|
|
|
379
379
|
## Streaming (SSE)
|
|
380
380
|
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* on the wire are the bytes the unwrapped client produced -- asserted by
|
|
20
20
|
* reference identity in `test/joiner.test.ts`.
|
|
21
21
|
* 2. **Explicit always wins.** A body already carrying
|
|
22
|
-
* `
|
|
22
|
+
* `shashu_metadata.rag_context` is left exactly as the caller built it.
|
|
23
23
|
* The ambient declaration is consumed and discarded, not deferred: a
|
|
24
24
|
* declaration that lost this call must not turn up on the next one.
|
|
25
25
|
* 3. **Snapshot and clear.** The bus is emptied the moment it is written to a
|
|
@@ -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 };
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* on the wire are the bytes the unwrapped client produced -- asserted by
|
|
20
20
|
* reference identity in `test/joiner.test.ts`.
|
|
21
21
|
* 2. **Explicit always wins.** A body already carrying
|
|
22
|
-
* `
|
|
22
|
+
* `shashu_metadata.rag_context` is left exactly as the caller built it.
|
|
23
23
|
* The ambient declaration is consumed and discarded, not deferred: a
|
|
24
24
|
* declaration that lost this call must not turn up on the next one.
|
|
25
25
|
* 3. **Snapshot and clear.** The bus is emptied the moment it is written to a
|
|
@@ -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";
|
|
@@ -75,10 +75,10 @@ function attachRagContext(body, ragContext) {
|
|
|
75
75
|
if (ragContext === null || typeof ragContext !== "object") {
|
|
76
76
|
throw new TypeError("ragContext must be an object");
|
|
77
77
|
}
|
|
78
|
-
const existing = body.
|
|
78
|
+
const existing = body.shashu_metadata;
|
|
79
79
|
const meta = existing !== null && typeof existing === "object" ? existing : {};
|
|
80
80
|
meta.rag_context = ragContext;
|
|
81
|
-
body.
|
|
81
|
+
body.shashu_metadata = meta;
|
|
82
82
|
return body;
|
|
83
83
|
}
|
|
84
84
|
var SOURCES = [
|
|
@@ -368,9 +368,9 @@ function buildMemoryContext(events) {
|
|
|
368
368
|
}
|
|
369
369
|
function attachMemoryContext(body, memoryContext) {
|
|
370
370
|
const bag = body;
|
|
371
|
-
const existing = bag.
|
|
371
|
+
const existing = bag.shashu_metadata;
|
|
372
372
|
const metadata = existing !== null && typeof existing === "object" ? existing : {};
|
|
373
|
-
bag.
|
|
373
|
+
bag.shashu_metadata = { ...metadata, memory_context: memoryContext };
|
|
374
374
|
return body;
|
|
375
375
|
}
|
|
376
376
|
|
|
@@ -387,7 +387,7 @@ function parseJsonObject(body) {
|
|
|
387
387
|
}
|
|
388
388
|
}
|
|
389
389
|
function metadataBag(body) {
|
|
390
|
-
const metadata = body.
|
|
390
|
+
const metadata = body.shashu_metadata;
|
|
391
391
|
if (metadata !== null && typeof metadata === "object") {
|
|
392
392
|
return metadata;
|
|
393
393
|
}
|
|
@@ -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
|
};
|
|
@@ -213,50 +213,32 @@ function raiseIfContentFiltered(json, decision, headers) {
|
|
|
213
213
|
|
|
214
214
|
// src/config.ts
|
|
215
215
|
var BASE_URL_ENV = "SHASHU_BASE_URL";
|
|
216
|
-
var BASE_URL_ENV_LEGACY = "TRUSTGATE_BASE_URL";
|
|
217
216
|
var API_KEY_ENV = "SHASHU_API_KEY";
|
|
218
|
-
var API_KEY_ENV_LEGACY = "TRUSTGATE_API_KEY";
|
|
219
217
|
var DISABLE_ENV = "SHASHU_DISABLE";
|
|
220
|
-
var DISABLE_ENV_LEGACY = "TRUSTGATE_DISABLE";
|
|
221
|
-
var fallbackWarned = /* @__PURE__ */ new Set();
|
|
222
218
|
var TRUTHY = /* @__PURE__ */ new Set(["1", "true", "yes", "on"]);
|
|
223
219
|
function env() {
|
|
224
220
|
return typeof process !== "undefined" && process.env ? process.env : {};
|
|
225
221
|
}
|
|
226
|
-
function envGet(current,
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
if (now) return now;
|
|
230
|
-
const old = (e[legacy] ?? "").trim();
|
|
231
|
-
if (old) {
|
|
232
|
-
if (!fallbackWarned.has(legacy)) {
|
|
233
|
-
fallbackWarned.add(legacy);
|
|
234
|
-
console.warn(
|
|
235
|
-
`${legacy} is deprecated; set ${current} instead. The TRUSTGATE_* name still works for this release.`
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
return old;
|
|
239
|
-
}
|
|
240
|
-
return void 0;
|
|
222
|
+
function envGet(current, _legacy) {
|
|
223
|
+
const now = (env()[current] ?? "").trim();
|
|
224
|
+
return now || void 0;
|
|
241
225
|
}
|
|
242
226
|
function isDisabled() {
|
|
243
|
-
return TRUTHY.has((envGet(DISABLE_ENV
|
|
227
|
+
return TRUTHY.has((envGet(DISABLE_ENV) ?? "").trim().toLowerCase());
|
|
244
228
|
}
|
|
245
229
|
function firstConfigured(baseUrl, envVars) {
|
|
246
230
|
if (baseUrl && baseUrl.trim()) return baseUrl.trim().replace(/\/+$/, "");
|
|
247
231
|
for (const name of envVars) {
|
|
248
|
-
const
|
|
249
|
-
const legacy = name.startsWith("SHASHU_") ? name.replace("SHASHU_", "TRUSTGATE_") : name.startsWith("TRUSTGATE_") ? name : "";
|
|
250
|
-
const value = legacy && legacy !== current ? envGet(current, legacy) : env()[name]?.trim();
|
|
232
|
+
const value = env()[name]?.trim();
|
|
251
233
|
if (value) return value.replace(/\/+$/, "");
|
|
252
234
|
}
|
|
253
235
|
return void 0;
|
|
254
236
|
}
|
|
255
|
-
function optionalBaseUrl(baseUrl, envVars = [BASE_URL_ENV
|
|
237
|
+
function optionalBaseUrl(baseUrl, envVars = [BASE_URL_ENV]) {
|
|
256
238
|
return firstConfigured(baseUrl, envVars);
|
|
257
239
|
}
|
|
258
240
|
function requireBaseUrl(baseUrl, opts) {
|
|
259
|
-
const envVars = opts.envVars ?? [BASE_URL_ENV
|
|
241
|
+
const envVars = opts.envVars ?? [BASE_URL_ENV];
|
|
260
242
|
const resolved = firstConfigured(baseUrl, envVars);
|
|
261
243
|
if (resolved) return resolved;
|
|
262
244
|
const option = opts.option ?? "baseUrl";
|
|
@@ -308,14 +290,14 @@ function parseHopDepth(raw) {
|
|
|
308
290
|
return Number.isFinite(n) && n > 0 ? n : 0;
|
|
309
291
|
}
|
|
310
292
|
function captureFromHeaders(h) {
|
|
311
|
-
const trace = (h["x-
|
|
293
|
+
const trace = (h["x-shashu-trace-id"] ?? "").trim();
|
|
312
294
|
if (!trace) return void 0;
|
|
313
|
-
const session = (h["x-
|
|
314
|
-
const caller = (h["x-
|
|
295
|
+
const session = (h["x-shashu-session-id"] ?? "").trim();
|
|
296
|
+
const caller = (h["x-shashu-caller-agent-id"] ?? "").trim();
|
|
315
297
|
return {
|
|
316
298
|
traceId: trace,
|
|
317
299
|
sessionId: session || void 0,
|
|
318
|
-
inboundHopDepth: parseHopDepth(h["x-
|
|
300
|
+
inboundHopDepth: parseHopDepth(h["x-shashu-hop-depth"]),
|
|
319
301
|
inboundCallerAgentId: caller || void 0
|
|
320
302
|
};
|
|
321
303
|
}
|
|
@@ -335,7 +317,7 @@ var Tracer = class {
|
|
|
335
317
|
this.buffer = [];
|
|
336
318
|
this.flushTimer = null;
|
|
337
319
|
this._gatewayUrl = optionalBaseUrl();
|
|
338
|
-
this._apiKey = envGet(API_KEY_ENV
|
|
320
|
+
this._apiKey = envGet(API_KEY_ENV);
|
|
339
321
|
}
|
|
340
322
|
/**
|
|
341
323
|
* Current gateway base URL (respects configure()), or undefined when none is
|
|
@@ -388,7 +370,7 @@ var Tracer = class {
|
|
|
388
370
|
get sessionId() {
|
|
389
371
|
return a2aStorage.getStore()?.sessionId || void 0;
|
|
390
372
|
}
|
|
391
|
-
/** Advisory inbound hop depth captured from x-
|
|
373
|
+
/** Advisory inbound hop depth captured from x-shashu-hop-depth (0 if absent). */
|
|
392
374
|
get inboundHopDepth() {
|
|
393
375
|
return a2aStorage.getStore()?.inboundHopDepth ?? 0;
|
|
394
376
|
}
|
|
@@ -397,7 +379,7 @@ var Tracer = class {
|
|
|
397
379
|
get outboundHopDepth() {
|
|
398
380
|
return this.inboundHopDepth + 1;
|
|
399
381
|
}
|
|
400
|
-
/** The captured inbound x-
|
|
382
|
+
/** The captured inbound x-shashu-caller-agent-id — the upstream agent that INVOKED
|
|
401
383
|
* this hop. The SDK FORWARDS this on the outbound gateway call so the gateway records
|
|
402
384
|
* the real (invoker -> this agent) edge, NEVER this agent's own id (§4.1). undefined at
|
|
403
385
|
* origin / caller-stripped. */
|
|
@@ -407,7 +389,7 @@ var Tracer = class {
|
|
|
407
389
|
/**
|
|
408
390
|
* Capture an inbound flow from the headers of a request this agent is SERVING into the
|
|
409
391
|
* AsyncLocalStorage store, so a downstream call made through the SDK propagates the SAME
|
|
410
|
-
* flow (WP-A2A-4 / ADR-007 §0). Reads (case-insensitively) x-
|
|
392
|
+
* flow (WP-A2A-4 / ADR-007 §0). Reads (case-insensitively) x-shashu-trace-id (the flow
|
|
411
393
|
* id), -session-id, -hop-depth, and -caller-agent-id (the upstream INVOKER). The captured
|
|
412
394
|
* trace is PROPAGATED on the next outbound call (never re-minted); the session, inbound hop
|
|
413
395
|
* depth, and inbound caller ride along — the caller is FORWARDED on the gateway call (§4.1),
|
|
@@ -510,13 +492,13 @@ function captureIncoming(headers) {
|
|
|
510
492
|
import { randomUUID as randomUUID2 } from "crypto";
|
|
511
493
|
|
|
512
494
|
// src/version.ts
|
|
513
|
-
var SDK_VERSION = "1.
|
|
495
|
+
var SDK_VERSION = "1.1.1";
|
|
514
496
|
|
|
515
497
|
// src/context.ts
|
|
516
|
-
var HEADER_PREFIX = "x-
|
|
517
|
-
var SOURCE_TOOL_HEADER = "x-
|
|
498
|
+
var HEADER_PREFIX = "x-shashu";
|
|
499
|
+
var SOURCE_TOOL_HEADER = "x-shashu-source-tool";
|
|
518
500
|
var DEFAULT_CONTRACT_VERSION = "2026-09-01";
|
|
519
|
-
var CONTRACT_VERSION_HEADER = "
|
|
501
|
+
var CONTRACT_VERSION_HEADER = "shashu-version";
|
|
520
502
|
function applyContractVersion(headers, contractVersion) {
|
|
521
503
|
if (contractVersion === null) return headers;
|
|
522
504
|
const named = Object.keys(headers).some((k) => k.toLowerCase() === CONTRACT_VERSION_HEADER);
|
|
@@ -635,20 +617,20 @@ function buildContextHeaders(ctx, session) {
|
|
|
635
617
|
if (v) h[`${TRUSTLOG_HEADER_PREFIX}${k.replace(/_/g, "-")}`] = String(v);
|
|
636
618
|
}
|
|
637
619
|
if (tracer.isActive) {
|
|
638
|
-
if (tracer.traceId) h["x-
|
|
639
|
-
h["x-
|
|
640
|
-
h["x-
|
|
620
|
+
if (tracer.traceId) h["x-shashu-trace-id"] = tracer.traceId;
|
|
621
|
+
h["x-shashu-span-id"] = tracer.currentSpanId ?? "";
|
|
622
|
+
h["x-shashu-parent-id"] = tracer.parentSpanId ?? "";
|
|
641
623
|
} else if (tracer.isA2aHop) {
|
|
642
|
-
if (tracer.traceId) h["x-
|
|
643
|
-
} else if (!h["x-
|
|
644
|
-
h["x-
|
|
624
|
+
if (tracer.traceId) h["x-shashu-trace-id"] = tracer.traceId;
|
|
625
|
+
} else if (!h["x-shashu-trace-id"]) {
|
|
626
|
+
h["x-shashu-trace-id"] = resolveOriginTraceId();
|
|
645
627
|
}
|
|
646
628
|
const sessionId = resolveSessionId(session);
|
|
647
|
-
if (sessionId) h["x-
|
|
629
|
+
if (sessionId) h["x-shashu-session-id"] = sessionId;
|
|
648
630
|
if (tracer.isA2aHop) {
|
|
649
631
|
const inboundCaller = tracer.inboundCallerAgentId;
|
|
650
|
-
if (inboundCaller) h["x-
|
|
651
|
-
h["x-
|
|
632
|
+
if (inboundCaller) h["x-shashu-caller-agent-id"] = inboundCaller;
|
|
633
|
+
h["x-shashu-hop-depth"] = String(tracer.outboundHopDepth);
|
|
652
634
|
}
|
|
653
635
|
return lowercaseHeaderKeys(h);
|
|
654
636
|
}
|
|
@@ -668,11 +650,11 @@ function buildInvocationHeaders(opts) {
|
|
|
668
650
|
const hop = opts.hopDepth ?? tracer.outboundHopDepth;
|
|
669
651
|
const session = resolveSessionId({ requestSessionId: opts.sessionId });
|
|
670
652
|
const h = {
|
|
671
|
-
"x-
|
|
672
|
-
"x-
|
|
673
|
-
"x-
|
|
653
|
+
"x-shashu-trace-id": String(trace),
|
|
654
|
+
"x-shashu-caller-agent-id": String(opts.callerAgentId),
|
|
655
|
+
"x-shashu-hop-depth": String(hop)
|
|
674
656
|
};
|
|
675
|
-
if (session) h["x-
|
|
657
|
+
if (session) h["x-shashu-session-id"] = String(session);
|
|
676
658
|
return lowercaseHeaderKeys(h);
|
|
677
659
|
}
|
|
678
660
|
|
|
@@ -730,7 +712,7 @@ function shouldRetryPassRefused(opts) {
|
|
|
730
712
|
}
|
|
731
713
|
var CredentialResolver = class {
|
|
732
714
|
constructor(opts) {
|
|
733
|
-
const envKey = envGet(API_KEY_ENV
|
|
715
|
+
const envKey = envGet(API_KEY_ENV) ?? void 0;
|
|
734
716
|
const hasKeyArg = opts.apiKey !== void 0;
|
|
735
717
|
const hasProvider = opts.tokenProvider !== void 0;
|
|
736
718
|
if (hasKeyArg && hasProvider) throw new ShashuConfigError(BOTH_OR_NEITHER);
|
|
@@ -787,9 +769,7 @@ export {
|
|
|
787
769
|
raiseForResponse,
|
|
788
770
|
raiseIfContentFiltered,
|
|
789
771
|
BASE_URL_ENV,
|
|
790
|
-
BASE_URL_ENV_LEGACY,
|
|
791
772
|
API_KEY_ENV,
|
|
792
|
-
API_KEY_ENV_LEGACY,
|
|
793
773
|
envGet,
|
|
794
774
|
isDisabled,
|
|
795
775
|
requireBaseUrl,
|