shield-llm 0.6.1 → 0.6.3
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/dist/{chunk-LGX52ZEL.js → chunk-LNYPVVYM.js} +111 -8
- package/dist/cli.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createRequire as __cjs_createRequire } from "module"; import { fileURLToPath as __cjs_fileURLToPath } from "url"; import { dirname as __cjs_dirname } from "path"; const __filename = __cjs_fileURLToPath(import.meta.url); const __dirname = __cjs_dirname(__filename); const require = __cjs_createRequire(import.meta.url);
|
|
2
2
|
|
|
3
3
|
// package.json
|
|
4
|
-
var version = "0.6.
|
|
4
|
+
var version = "0.6.3";
|
|
5
5
|
|
|
6
6
|
// src/config/schema.ts
|
|
7
7
|
import { z as z2 } from "zod";
|
|
@@ -328,6 +328,15 @@ var RATE_LIMIT_PATTERNS = [
|
|
|
328
328
|
pattern: /\b(quota|usage cap)\b.*\b(exceeded|reached|exhausted|hit)\b/i,
|
|
329
329
|
platform: "generic"
|
|
330
330
|
},
|
|
331
|
+
{
|
|
332
|
+
// Credit-based exhaustion (e.g. ChatGot free tier replies "No credits
|
|
333
|
+
// left." for EVERY message once the per-device_id allowance runs out).
|
|
334
|
+
// Without this the scanner reads that as a normal answer and scores an
|
|
335
|
+
// out-of-credits bot as "secure" across the whole suite (false negatives),
|
|
336
|
+
// the same failure mode the soft-throttle classifier exists to prevent.
|
|
337
|
+
pattern: /\b(no|out of|insufficient|zero|0)\s+credits?\b|\bcredits?\s+(have\s+run\s+out|ran\s+out|run\s+out|exhausted|depleted|used\s+up)\b/i,
|
|
338
|
+
platform: "generic"
|
|
339
|
+
},
|
|
331
340
|
{
|
|
332
341
|
pattern: /\blimite de (requ[eê]tes|taux)\b.*\b(attendez|r[eé]essayez|d[eé]pass[eé]e)\b/i,
|
|
333
342
|
platform: "generic"
|
|
@@ -5602,6 +5611,11 @@ async function fetchWithTimeout(url, init, timeoutMs, responseField, allowPrivat
|
|
|
5602
5611
|
const field = responseField ?? "response";
|
|
5603
5612
|
return { [field]: text };
|
|
5604
5613
|
}
|
|
5614
|
+
if (contentType.includes("ndjson")) {
|
|
5615
|
+
const text = await collectNDJSON(response);
|
|
5616
|
+
const field = responseField ?? "response";
|
|
5617
|
+
return { [field]: text };
|
|
5618
|
+
}
|
|
5605
5619
|
if (!contentType.includes("json")) {
|
|
5606
5620
|
const text = await readBodyWithLimit(response);
|
|
5607
5621
|
throw new HttpTargetError(
|
|
@@ -5665,6 +5679,33 @@ async function readBodyWithLimit(response) {
|
|
|
5665
5679
|
result += decoder.decode();
|
|
5666
5680
|
return result;
|
|
5667
5681
|
}
|
|
5682
|
+
function extractSseChunk(parsed) {
|
|
5683
|
+
const p = parsed;
|
|
5684
|
+
if ((p.type === "response.output_text.delta" || p.type === "text-delta") && typeof p.delta === "string") {
|
|
5685
|
+
return p.delta;
|
|
5686
|
+
}
|
|
5687
|
+
const parts = p.candidates?.[0]?.content?.parts;
|
|
5688
|
+
if (Array.isArray(parts)) {
|
|
5689
|
+
const joined = parts.map((part) => typeof part?.text === "string" ? part.text : "").join("");
|
|
5690
|
+
if (joined) return joined;
|
|
5691
|
+
}
|
|
5692
|
+
const candidate = (
|
|
5693
|
+
// {message:"…"} simple wrappers, OR Ollama native {message:{content:"…"}}
|
|
5694
|
+
(typeof p.message === "string" ? p.message : p.message?.content) ?? p.content ?? // {content:"…"}; LangServe /stream AIMessageChunk
|
|
5695
|
+
p.delta?.content ?? // top-level delta object {delta:{content}}
|
|
5696
|
+
p.delta?.text ?? // Anthropic content_block_delta (text_delta)
|
|
5697
|
+
p.delta?.message?.content?.text ?? // Cohere v2 content-delta
|
|
5698
|
+
p.text ?? // {text:"…"}
|
|
5699
|
+
p.choices?.[0]?.delta?.content ?? // OpenAI/Mistral/Ollama-compat chat chunk
|
|
5700
|
+
p.token?.text ?? // HF TGI /generate_stream
|
|
5701
|
+
p.response ?? // Ollama native /api/generate (top-level string)
|
|
5702
|
+
p.data?.message ?? // nested {data:{message}}
|
|
5703
|
+
p.data?.content ?? // nested {data:{content}} (ChatGot)
|
|
5704
|
+
p.data?.chunk?.content ?? // LangChain /stream_events on_chat_model_stream
|
|
5705
|
+
(typeof p.data === "string" ? p.data : void 0)
|
|
5706
|
+
);
|
|
5707
|
+
return typeof candidate === "string" ? candidate : void 0;
|
|
5708
|
+
}
|
|
5668
5709
|
async function collectSSE(response) {
|
|
5669
5710
|
if (!response.body) {
|
|
5670
5711
|
throw new HttpTargetError("SSE response has no body", 0);
|
|
@@ -5691,18 +5732,27 @@ async function collectSSE(response) {
|
|
|
5691
5732
|
const lines = buffer.split("\n");
|
|
5692
5733
|
buffer = lines.pop();
|
|
5693
5734
|
for (const line of lines) {
|
|
5694
|
-
if (!line.startsWith("data:
|
|
5695
|
-
const payload = line.slice(
|
|
5696
|
-
if (payload === "
|
|
5735
|
+
if (!line.startsWith("data:")) continue;
|
|
5736
|
+
const payload = line.slice(5).trim();
|
|
5737
|
+
if (payload === "") continue;
|
|
5738
|
+
if (payload === "[DONE]") {
|
|
5739
|
+
await reader.cancel().catch(() => {
|
|
5740
|
+
});
|
|
5741
|
+
return result;
|
|
5742
|
+
}
|
|
5697
5743
|
try {
|
|
5698
5744
|
const parsed = JSON.parse(payload);
|
|
5699
5745
|
if (typeof parsed === "string") {
|
|
5700
5746
|
result += parsed;
|
|
5701
5747
|
} else {
|
|
5702
|
-
const chunk = parsed
|
|
5703
|
-
|
|
5704
|
-
|
|
5705
|
-
|
|
5748
|
+
const chunk = extractSseChunk(parsed);
|
|
5749
|
+
if (chunk === "[DONE]") {
|
|
5750
|
+
await reader.cancel().catch(() => {
|
|
5751
|
+
});
|
|
5752
|
+
return result;
|
|
5753
|
+
}
|
|
5754
|
+
if (typeof chunk === "string") {
|
|
5755
|
+
result += chunk;
|
|
5706
5756
|
}
|
|
5707
5757
|
}
|
|
5708
5758
|
} catch {
|
|
@@ -5715,6 +5765,59 @@ async function collectSSE(response) {
|
|
|
5715
5765
|
}
|
|
5716
5766
|
return result;
|
|
5717
5767
|
}
|
|
5768
|
+
async function collectNDJSON(response) {
|
|
5769
|
+
if (!response.body) {
|
|
5770
|
+
throw new HttpTargetError("NDJSON response has no body", 0);
|
|
5771
|
+
}
|
|
5772
|
+
const reader = response.body.getReader();
|
|
5773
|
+
const decoder = new TextDecoder();
|
|
5774
|
+
let buffer = "";
|
|
5775
|
+
let result = "";
|
|
5776
|
+
let totalBytes = 0;
|
|
5777
|
+
const consume = (raw) => {
|
|
5778
|
+
const trimmed = raw.trim();
|
|
5779
|
+
if (!trimmed) return false;
|
|
5780
|
+
let parsed;
|
|
5781
|
+
try {
|
|
5782
|
+
parsed = JSON.parse(trimmed);
|
|
5783
|
+
} catch {
|
|
5784
|
+
return false;
|
|
5785
|
+
}
|
|
5786
|
+
if (!parsed || typeof parsed !== "object") return false;
|
|
5787
|
+
const chunk = extractSseChunk(parsed);
|
|
5788
|
+
if (typeof chunk === "string") result += chunk;
|
|
5789
|
+
return parsed.done === true;
|
|
5790
|
+
};
|
|
5791
|
+
try {
|
|
5792
|
+
while (true) {
|
|
5793
|
+
const { done, value } = await reader.read();
|
|
5794
|
+
if (done) break;
|
|
5795
|
+
totalBytes += value.byteLength;
|
|
5796
|
+
if (totalBytes > MAX_RESPONSE_BYTES) {
|
|
5797
|
+
await reader.cancel().catch(() => {
|
|
5798
|
+
});
|
|
5799
|
+
throw new HttpTargetError(
|
|
5800
|
+
`NDJSON stream exceeded ${MAX_RESPONSE_BYTES} bytes`,
|
|
5801
|
+
response.status
|
|
5802
|
+
);
|
|
5803
|
+
}
|
|
5804
|
+
buffer += decoder.decode(value, { stream: true });
|
|
5805
|
+
const lines = buffer.split("\n");
|
|
5806
|
+
buffer = lines.pop();
|
|
5807
|
+
for (const line of lines) {
|
|
5808
|
+
if (consume(line)) {
|
|
5809
|
+
await reader.cancel().catch(() => {
|
|
5810
|
+
});
|
|
5811
|
+
return result;
|
|
5812
|
+
}
|
|
5813
|
+
}
|
|
5814
|
+
}
|
|
5815
|
+
} finally {
|
|
5816
|
+
reader.releaseLock();
|
|
5817
|
+
}
|
|
5818
|
+
consume(buffer);
|
|
5819
|
+
return result;
|
|
5820
|
+
}
|
|
5718
5821
|
var HttpTargetError = class extends Error {
|
|
5719
5822
|
constructor(message, statusCode) {
|
|
5720
5823
|
super(message);
|
package/dist/cli.js
CHANGED
package/dist/index.js
CHANGED