agentid-sdk 0.1.34 → 0.1.35
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/{agentid-M5I7-YqI.d.mts → agentid-IonlG0NB.d.mts} +23 -2
- package/dist/{agentid-M5I7-YqI.d.ts → agentid-IonlG0NB.d.ts} +23 -2
- package/dist/{chunk-FCOSLPMF.mjs → chunk-XFSIAH3F.mjs} +288 -34
- package/dist/index.d.mts +3 -16
- package/dist/index.d.ts +3 -16
- package/dist/index.js +288 -34
- package/dist/index.mjs +1 -1
- package/dist/langchain.d.mts +1 -1
- package/dist/langchain.d.ts +1 -1
- package/dist/langchain.js +1040 -5
- package/dist/langchain.mjs +102 -5
- package/dist/transparency-badge.d.mts +1 -1
- package/dist/transparency-badge.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -75,7 +75,7 @@ var OpenAIAdapter = class {
|
|
|
75
75
|
|
|
76
76
|
// src/sdk-version.ts
|
|
77
77
|
var FALLBACK_SDK_VERSION = "js-0.0.0-dev";
|
|
78
|
-
var AGENTID_SDK_VERSION_HEADER = "js-0.1.
|
|
78
|
+
var AGENTID_SDK_VERSION_HEADER = "js-0.1.35".trim().length > 0 ? "js-0.1.35" : FALLBACK_SDK_VERSION;
|
|
79
79
|
|
|
80
80
|
// src/pii-national-identifiers.ts
|
|
81
81
|
var MAX_CANDIDATES_PER_RULE = 256;
|
|
@@ -1188,6 +1188,7 @@ var PIIManager = class {
|
|
|
1188
1188
|
|
|
1189
1189
|
// src/local-security-enforcer.ts
|
|
1190
1190
|
var DEFAULT_FAIL_OPEN_CONFIG = {
|
|
1191
|
+
version: null,
|
|
1191
1192
|
shadow_mode: false,
|
|
1192
1193
|
strict_security_mode: false,
|
|
1193
1194
|
failure_mode: "fail_open",
|
|
@@ -1356,6 +1357,25 @@ function readOptionalFailureModeField(body, fallback) {
|
|
|
1356
1357
|
}
|
|
1357
1358
|
return fallback;
|
|
1358
1359
|
}
|
|
1360
|
+
function readOptionalVersionField(body) {
|
|
1361
|
+
if (!("version" in body)) {
|
|
1362
|
+
return null;
|
|
1363
|
+
}
|
|
1364
|
+
const value = body.version;
|
|
1365
|
+
if (value === null || value === void 0 || value === "") {
|
|
1366
|
+
return null;
|
|
1367
|
+
}
|
|
1368
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
1369
|
+
return Math.trunc(value);
|
|
1370
|
+
}
|
|
1371
|
+
if (typeof value === "string") {
|
|
1372
|
+
const parsed = Number.parseInt(value, 10);
|
|
1373
|
+
if (Number.isFinite(parsed)) {
|
|
1374
|
+
return parsed;
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
throw new Error("Invalid config field: version");
|
|
1378
|
+
}
|
|
1359
1379
|
function normalizeCapabilityConfig(payload) {
|
|
1360
1380
|
if (!payload || typeof payload !== "object") {
|
|
1361
1381
|
throw new Error("Invalid config payload");
|
|
@@ -1373,6 +1393,7 @@ function normalizeCapabilityConfig(payload) {
|
|
|
1373
1393
|
false
|
|
1374
1394
|
);
|
|
1375
1395
|
return {
|
|
1396
|
+
version: readOptionalVersionField(body),
|
|
1376
1397
|
shadow_mode: readOptionalBooleanField(body, "shadow_mode", false),
|
|
1377
1398
|
strict_security_mode: effectiveStrictMode,
|
|
1378
1399
|
failure_mode: effectiveStrictMode ? "fail_close" : "fail_open",
|
|
@@ -2314,6 +2335,9 @@ function buildSdkTimingMetadata(params) {
|
|
|
2314
2335
|
setFiniteDurationMetadata(metadata, "sdk_local_scan_ms", params.sdkLocalScanMs);
|
|
2315
2336
|
setFiniteDurationMetadata(metadata, "sdk_guard_ms", params.sdkGuardMs);
|
|
2316
2337
|
setFiniteDurationMetadata(metadata, "sdk_ingest_ms", params.sdkIngestMs);
|
|
2338
|
+
if (typeof params.sdkConfigVersion === "number" && Number.isFinite(params.sdkConfigVersion)) {
|
|
2339
|
+
metadata.sdk_config_version = Math.trunc(params.sdkConfigVersion);
|
|
2340
|
+
}
|
|
2317
2341
|
return metadata;
|
|
2318
2342
|
}
|
|
2319
2343
|
function resolveConfiguredApiKey(value) {
|
|
@@ -2516,6 +2540,57 @@ function createCompletionChunkCollector() {
|
|
|
2516
2540
|
result
|
|
2517
2541
|
};
|
|
2518
2542
|
}
|
|
2543
|
+
function getStreamingPlaceholderCarryLength(buffer, placeholders) {
|
|
2544
|
+
if (!buffer || placeholders.length === 0) {
|
|
2545
|
+
return 0;
|
|
2546
|
+
}
|
|
2547
|
+
let maxPlaceholderLength = 0;
|
|
2548
|
+
for (const placeholder of placeholders) {
|
|
2549
|
+
if (placeholder.length > maxPlaceholderLength) {
|
|
2550
|
+
maxPlaceholderLength = placeholder.length;
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
const maxCarryLength = Math.min(buffer.length, Math.max(0, maxPlaceholderLength - 1));
|
|
2554
|
+
let carryLength = 0;
|
|
2555
|
+
for (let candidateLength = 1; candidateLength <= maxCarryLength; candidateLength += 1) {
|
|
2556
|
+
const suffix = buffer.slice(-candidateLength);
|
|
2557
|
+
if (placeholders.some((placeholder) => placeholder.startsWith(suffix))) {
|
|
2558
|
+
carryLength = candidateLength;
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
return carryLength;
|
|
2562
|
+
}
|
|
2563
|
+
function createStreamingPlaceholderRewriter(piiManager, mapping) {
|
|
2564
|
+
const placeholders = Object.keys(mapping).filter((placeholder) => placeholder.length > 0);
|
|
2565
|
+
if (placeholders.length === 0) {
|
|
2566
|
+
return null;
|
|
2567
|
+
}
|
|
2568
|
+
let pending = "";
|
|
2569
|
+
return {
|
|
2570
|
+
consume(chunk) {
|
|
2571
|
+
if (!chunk) {
|
|
2572
|
+
return "";
|
|
2573
|
+
}
|
|
2574
|
+
pending += chunk;
|
|
2575
|
+
const carryLength = getStreamingPlaceholderCarryLength(pending, placeholders);
|
|
2576
|
+
const flushLength = Math.max(0, pending.length - carryLength);
|
|
2577
|
+
if (flushLength === 0) {
|
|
2578
|
+
return "";
|
|
2579
|
+
}
|
|
2580
|
+
const flushable = pending.slice(0, flushLength);
|
|
2581
|
+
pending = pending.slice(flushLength);
|
|
2582
|
+
return piiManager.deanonymize(flushable, mapping);
|
|
2583
|
+
},
|
|
2584
|
+
flush() {
|
|
2585
|
+
if (!pending) {
|
|
2586
|
+
return "";
|
|
2587
|
+
}
|
|
2588
|
+
const remaining = piiManager.deanonymize(pending, mapping);
|
|
2589
|
+
pending = "";
|
|
2590
|
+
return remaining;
|
|
2591
|
+
}
|
|
2592
|
+
};
|
|
2593
|
+
}
|
|
2519
2594
|
var SecurityBlockError = class extends Error {
|
|
2520
2595
|
constructor(reason = "guard_denied") {
|
|
2521
2596
|
super(`AgentID: Security Blocked (${reason})`);
|
|
@@ -2695,7 +2770,7 @@ var AgentID = class {
|
|
|
2695
2770
|
}
|
|
2696
2771
|
return config.block_on_heuristic;
|
|
2697
2772
|
}
|
|
2698
|
-
async
|
|
2773
|
+
async refreshCapabilityConfigBeforeClientControl(params) {
|
|
2699
2774
|
const refreshed = await this.getCapabilityConfigWithTelemetry(true, params.options);
|
|
2700
2775
|
return {
|
|
2701
2776
|
capabilityConfig: refreshed.capabilityConfig,
|
|
@@ -2717,7 +2792,8 @@ var AgentID = class {
|
|
|
2717
2792
|
eventId: params.clientEventId,
|
|
2718
2793
|
clientEventId: params.clientEventId,
|
|
2719
2794
|
telemetryMetadata: buildSdkTimingMetadata({
|
|
2720
|
-
sdkConfigFetchMs: params.sdkConfigFetchMs
|
|
2795
|
+
sdkConfigFetchMs: params.sdkConfigFetchMs,
|
|
2796
|
+
sdkConfigVersion: params.capabilityConfig.version
|
|
2721
2797
|
})
|
|
2722
2798
|
});
|
|
2723
2799
|
}
|
|
@@ -2762,15 +2838,38 @@ var AgentID = class {
|
|
|
2762
2838
|
false,
|
|
2763
2839
|
options
|
|
2764
2840
|
);
|
|
2841
|
+
let sanitizedInput = params.input;
|
|
2842
|
+
let sdkLocalScanMs = 0;
|
|
2843
|
+
if (this.configuredPiiMasking === null) {
|
|
2844
|
+
const refreshed = await this.refreshCapabilityConfigBeforeClientControl({
|
|
2845
|
+
capabilityConfig,
|
|
2846
|
+
sdkConfigFetchMs,
|
|
2847
|
+
options
|
|
2848
|
+
});
|
|
2849
|
+
capabilityConfig = refreshed.capabilityConfig;
|
|
2850
|
+
sdkConfigFetchMs = refreshed.sdkConfigFetchMs;
|
|
2851
|
+
}
|
|
2765
2852
|
if (!this.clientFastFail) {
|
|
2853
|
+
const effectivePiiMasking2 = this.resolveEffectivePiiMasking(capabilityConfig);
|
|
2854
|
+
if (!capabilityConfig.block_pii_leakage && effectivePiiMasking2) {
|
|
2855
|
+
const masked = this.pii.anonymize(sanitizedInput);
|
|
2856
|
+
return {
|
|
2857
|
+
sanitizedInput: masked.maskedText,
|
|
2858
|
+
capabilityConfig,
|
|
2859
|
+
sdkConfigFetchMs,
|
|
2860
|
+
sdkLocalScanMs,
|
|
2861
|
+
piiMapping: masked.mapping,
|
|
2862
|
+
shouldDeanonymize: Object.keys(masked.mapping).length > 0
|
|
2863
|
+
};
|
|
2864
|
+
}
|
|
2766
2865
|
return {
|
|
2767
|
-
sanitizedInput
|
|
2866
|
+
sanitizedInput,
|
|
2768
2867
|
capabilityConfig,
|
|
2769
2868
|
sdkConfigFetchMs,
|
|
2770
|
-
sdkLocalScanMs
|
|
2869
|
+
sdkLocalScanMs
|
|
2771
2870
|
};
|
|
2772
2871
|
}
|
|
2773
|
-
const refreshedConfig = await this.
|
|
2872
|
+
const refreshedConfig = await this.refreshCapabilityConfigBeforeClientControl({
|
|
2774
2873
|
capabilityConfig,
|
|
2775
2874
|
sdkConfigFetchMs,
|
|
2776
2875
|
options
|
|
@@ -2787,11 +2886,25 @@ var AgentID = class {
|
|
|
2787
2886
|
sdkConfigFetchMs,
|
|
2788
2887
|
runPromptInjectionCheck: !params.skipInjectionScan
|
|
2789
2888
|
});
|
|
2889
|
+
sanitizedInput = enforced.sanitizedInput;
|
|
2890
|
+
sdkLocalScanMs = enforced.sdkLocalScanMs;
|
|
2891
|
+
const effectivePiiMasking = this.resolveEffectivePiiMasking(capabilityConfig);
|
|
2892
|
+
if (!capabilityConfig.block_pii_leakage && effectivePiiMasking) {
|
|
2893
|
+
const masked = this.pii.anonymize(sanitizedInput);
|
|
2894
|
+
return {
|
|
2895
|
+
sanitizedInput: masked.maskedText,
|
|
2896
|
+
capabilityConfig,
|
|
2897
|
+
sdkConfigFetchMs,
|
|
2898
|
+
sdkLocalScanMs,
|
|
2899
|
+
piiMapping: masked.mapping,
|
|
2900
|
+
shouldDeanonymize: Object.keys(masked.mapping).length > 0
|
|
2901
|
+
};
|
|
2902
|
+
}
|
|
2790
2903
|
return {
|
|
2791
|
-
sanitizedInput
|
|
2904
|
+
sanitizedInput,
|
|
2792
2905
|
capabilityConfig,
|
|
2793
2906
|
sdkConfigFetchMs,
|
|
2794
|
-
sdkLocalScanMs
|
|
2907
|
+
sdkLocalScanMs
|
|
2795
2908
|
};
|
|
2796
2909
|
}
|
|
2797
2910
|
async applyLocalFallbackForGuardFailure(params, options) {
|
|
@@ -2800,7 +2913,7 @@ var AgentID = class {
|
|
|
2800
2913
|
capabilityConfig: params.capabilityConfig,
|
|
2801
2914
|
sdkConfigFetchMs: params.sdkConfigFetchMs
|
|
2802
2915
|
} : await this.getCapabilityConfigWithTelemetry(false, options);
|
|
2803
|
-
const refreshedConfig = await this.
|
|
2916
|
+
const refreshedConfig = await this.refreshCapabilityConfigBeforeClientControl({
|
|
2804
2917
|
capabilityConfig: resolvedConfig.capabilityConfig,
|
|
2805
2918
|
sdkConfigFetchMs: resolvedConfig.sdkConfigFetchMs,
|
|
2806
2919
|
options
|
|
@@ -2830,7 +2943,7 @@ var AgentID = class {
|
|
|
2830
2943
|
false,
|
|
2831
2944
|
options
|
|
2832
2945
|
);
|
|
2833
|
-
const refreshedConfig = await this.
|
|
2946
|
+
const refreshedConfig = await this.refreshCapabilityConfigBeforeClientControl({
|
|
2834
2947
|
capabilityConfig: initialConfig.capabilityConfig,
|
|
2835
2948
|
sdkConfigFetchMs: initialConfig.sdkConfigFetchMs,
|
|
2836
2949
|
options
|
|
@@ -2851,7 +2964,8 @@ var AgentID = class {
|
|
|
2851
2964
|
eventId: options?.clientEventId,
|
|
2852
2965
|
clientEventId: options?.clientEventId,
|
|
2853
2966
|
telemetryMetadata: buildSdkTimingMetadata({
|
|
2854
|
-
sdkConfigFetchMs: refreshedConfig.sdkConfigFetchMs
|
|
2967
|
+
sdkConfigFetchMs: refreshedConfig.sdkConfigFetchMs,
|
|
2968
|
+
sdkConfigVersion: refreshedConfig.capabilityConfig.version
|
|
2855
2969
|
})
|
|
2856
2970
|
});
|
|
2857
2971
|
}
|
|
@@ -2904,7 +3018,10 @@ var AgentID = class {
|
|
|
2904
3018
|
action_taken: params.actionTaken,
|
|
2905
3019
|
...buildSdkTimingMetadata({
|
|
2906
3020
|
sdkConfigFetchMs: params.sdkConfigFetchMs,
|
|
2907
|
-
sdkLocalScanMs: params.sdkLocalScanMs
|
|
3021
|
+
sdkLocalScanMs: params.sdkLocalScanMs,
|
|
3022
|
+
sdkConfigVersion: this.getCachedCapabilityConfig({
|
|
3023
|
+
apiKey: params.apiKey
|
|
3024
|
+
}).version
|
|
2908
3025
|
})
|
|
2909
3026
|
}
|
|
2910
3027
|
}, { apiKey: params.apiKey });
|
|
@@ -3243,7 +3360,123 @@ var AgentID = class {
|
|
|
3243
3360
|
const usage = chunk.usage;
|
|
3244
3361
|
return usage && typeof usage === "object" && !Array.isArray(usage) ? usage : void 0;
|
|
3245
3362
|
}
|
|
3246
|
-
|
|
3363
|
+
isOpenAIStreamFinishChunk(chunk) {
|
|
3364
|
+
if (!chunk || typeof chunk !== "object") {
|
|
3365
|
+
return false;
|
|
3366
|
+
}
|
|
3367
|
+
const choices = chunk.choices;
|
|
3368
|
+
if (!Array.isArray(choices)) {
|
|
3369
|
+
return false;
|
|
3370
|
+
}
|
|
3371
|
+
return choices.some(
|
|
3372
|
+
(choice) => choice && typeof choice === "object" && typeof choice.finish_reason === "string" && (choice.finish_reason ?? "").length > 0
|
|
3373
|
+
);
|
|
3374
|
+
}
|
|
3375
|
+
setOpenAIStreamChunkText(chunk, text) {
|
|
3376
|
+
if (!chunk || typeof chunk !== "object") {
|
|
3377
|
+
return false;
|
|
3378
|
+
}
|
|
3379
|
+
const choices = chunk.choices;
|
|
3380
|
+
if (!Array.isArray(choices)) {
|
|
3381
|
+
return false;
|
|
3382
|
+
}
|
|
3383
|
+
let assigned = false;
|
|
3384
|
+
const replaceInContainer = (container) => {
|
|
3385
|
+
if (!container || typeof container !== "object") {
|
|
3386
|
+
return false;
|
|
3387
|
+
}
|
|
3388
|
+
const content = container.content;
|
|
3389
|
+
if (typeof content === "string") {
|
|
3390
|
+
if (!assigned) {
|
|
3391
|
+
container.content = text;
|
|
3392
|
+
assigned = true;
|
|
3393
|
+
} else {
|
|
3394
|
+
container.content = "";
|
|
3395
|
+
}
|
|
3396
|
+
return true;
|
|
3397
|
+
}
|
|
3398
|
+
if (Array.isArray(content)) {
|
|
3399
|
+
let replacedInArray = false;
|
|
3400
|
+
for (const part of content) {
|
|
3401
|
+
if (!part || typeof part !== "object") continue;
|
|
3402
|
+
const typedPart = part;
|
|
3403
|
+
if (typeof typedPart.text !== "string") continue;
|
|
3404
|
+
if (!assigned && !replacedInArray) {
|
|
3405
|
+
typedPart.text = text;
|
|
3406
|
+
assigned = true;
|
|
3407
|
+
replacedInArray = true;
|
|
3408
|
+
} else {
|
|
3409
|
+
typedPart.text = "";
|
|
3410
|
+
}
|
|
3411
|
+
}
|
|
3412
|
+
if (!assigned && text.length > 0) {
|
|
3413
|
+
content.unshift({ type: "text", text });
|
|
3414
|
+
assigned = true;
|
|
3415
|
+
}
|
|
3416
|
+
return replacedInArray || assigned;
|
|
3417
|
+
}
|
|
3418
|
+
if (!assigned && text.length > 0) {
|
|
3419
|
+
container.content = text;
|
|
3420
|
+
assigned = true;
|
|
3421
|
+
return true;
|
|
3422
|
+
}
|
|
3423
|
+
return false;
|
|
3424
|
+
};
|
|
3425
|
+
for (const choice of choices) {
|
|
3426
|
+
if (!choice || typeof choice !== "object") {
|
|
3427
|
+
continue;
|
|
3428
|
+
}
|
|
3429
|
+
const typedChoice = choice;
|
|
3430
|
+
const replacedDelta = replaceInContainer(typedChoice.delta);
|
|
3431
|
+
const replacedMessage = replaceInContainer(typedChoice.message);
|
|
3432
|
+
if (!replacedDelta && !replacedMessage && !assigned && text.length > 0) {
|
|
3433
|
+
if (!typedChoice.delta || typeof typedChoice.delta !== "object") {
|
|
3434
|
+
typedChoice.delta = {};
|
|
3435
|
+
}
|
|
3436
|
+
typedChoice.delta.content = text;
|
|
3437
|
+
assigned = true;
|
|
3438
|
+
}
|
|
3439
|
+
}
|
|
3440
|
+
return assigned;
|
|
3441
|
+
}
|
|
3442
|
+
createSyntheticOpenAIStreamChunk(text, template) {
|
|
3443
|
+
const synthetic = {};
|
|
3444
|
+
if (template && typeof template === "object" && !Array.isArray(template)) {
|
|
3445
|
+
const typedTemplate = template;
|
|
3446
|
+
for (const key of ["id", "object", "created", "model"]) {
|
|
3447
|
+
if (Object.prototype.hasOwnProperty.call(typedTemplate, key)) {
|
|
3448
|
+
synthetic[key] = typedTemplate[key];
|
|
3449
|
+
}
|
|
3450
|
+
}
|
|
3451
|
+
}
|
|
3452
|
+
synthetic.choices = [
|
|
3453
|
+
{
|
|
3454
|
+
index: 0,
|
|
3455
|
+
delta: { content: text },
|
|
3456
|
+
finish_reason: null
|
|
3457
|
+
}
|
|
3458
|
+
];
|
|
3459
|
+
return synthetic;
|
|
3460
|
+
}
|
|
3461
|
+
rewriteOpenAIStreamChunkForClient(chunk, rewriter, isFinishChunk) {
|
|
3462
|
+
const rawText = this.extractStreamChunkText(chunk);
|
|
3463
|
+
const rewrittenText = rawText ? rewriter.consume(rawText) : "";
|
|
3464
|
+
const finalText = isFinishChunk ? `${rewrittenText}${rewriter.flush()}` : rewrittenText;
|
|
3465
|
+
if (typeof chunk === "string") {
|
|
3466
|
+
return finalText ? [finalText] : [];
|
|
3467
|
+
}
|
|
3468
|
+
if (rawText) {
|
|
3469
|
+
if (this.setOpenAIStreamChunkText(chunk, finalText)) {
|
|
3470
|
+
return [chunk];
|
|
3471
|
+
}
|
|
3472
|
+
return finalText.length > 0 ? [this.createSyntheticOpenAIStreamChunk(finalText, chunk), chunk] : [chunk];
|
|
3473
|
+
}
|
|
3474
|
+
if (isFinishChunk && finalText.length > 0) {
|
|
3475
|
+
return [this.createSyntheticOpenAIStreamChunk(finalText, chunk), chunk];
|
|
3476
|
+
}
|
|
3477
|
+
return [chunk];
|
|
3478
|
+
}
|
|
3479
|
+
wrapCompletion(completion, options) {
|
|
3247
3480
|
if (typeof completion === "string") {
|
|
3248
3481
|
const masked = this.pii.anonymize(completion);
|
|
3249
3482
|
return {
|
|
@@ -3267,7 +3500,11 @@ var AgentID = class {
|
|
|
3267
3500
|
const collector = createCompletionChunkCollector();
|
|
3268
3501
|
const extractStreamChunkText = this.extractStreamChunkText.bind(this);
|
|
3269
3502
|
const extractStreamChunkUsage = this.extractStreamChunkUsage.bind(this);
|
|
3503
|
+
const isOpenAIStreamFinishChunk = this.isOpenAIStreamFinishChunk.bind(this);
|
|
3504
|
+
const rewriteOpenAIStreamChunkForClient = this.rewriteOpenAIStreamChunkForClient.bind(this);
|
|
3505
|
+
const createSyntheticOpenAIStreamChunk = this.createSyntheticOpenAIStreamChunk.bind(this);
|
|
3270
3506
|
const piiManager = this.pii;
|
|
3507
|
+
const streamRewriter = options?.deanonymizeForClient === true && options.piiMapping ? createStreamingPlaceholderRewriter(piiManager, options.piiMapping) : null;
|
|
3271
3508
|
let lastUsage;
|
|
3272
3509
|
let resolveDone = null;
|
|
3273
3510
|
let rejectDone = null;
|
|
@@ -3278,17 +3515,44 @@ var AgentID = class {
|
|
|
3278
3515
|
const wrapped = {
|
|
3279
3516
|
[Symbol.asyncIterator]: async function* () {
|
|
3280
3517
|
try {
|
|
3518
|
+
let finishChunkFlushed = false;
|
|
3519
|
+
let lastChunkTemplate;
|
|
3281
3520
|
for await (const chunk of source) {
|
|
3282
3521
|
const chunkText = extractStreamChunkText(chunk);
|
|
3522
|
+
const isFinishChunk = streamRewriter ? isOpenAIStreamFinishChunk(chunk) : false;
|
|
3283
3523
|
if (chunkText) {
|
|
3284
3524
|
await collector.push(chunkText);
|
|
3525
|
+
lastChunkTemplate = chunk;
|
|
3285
3526
|
}
|
|
3286
3527
|
const chunkUsage = extractStreamChunkUsage(chunk);
|
|
3287
3528
|
if (chunkUsage) {
|
|
3288
3529
|
lastUsage = chunkUsage;
|
|
3289
3530
|
}
|
|
3531
|
+
if (streamRewriter) {
|
|
3532
|
+
const rewrittenChunks = rewriteOpenAIStreamChunkForClient(
|
|
3533
|
+
chunk,
|
|
3534
|
+
streamRewriter,
|
|
3535
|
+
isFinishChunk
|
|
3536
|
+
);
|
|
3537
|
+
if (isFinishChunk) {
|
|
3538
|
+
finishChunkFlushed = true;
|
|
3539
|
+
}
|
|
3540
|
+
for (const rewrittenChunk of rewrittenChunks) {
|
|
3541
|
+
yield rewrittenChunk;
|
|
3542
|
+
}
|
|
3543
|
+
continue;
|
|
3544
|
+
}
|
|
3290
3545
|
yield chunk;
|
|
3291
3546
|
}
|
|
3547
|
+
if (streamRewriter && !finishChunkFlushed) {
|
|
3548
|
+
const trailingText = streamRewriter.flush();
|
|
3549
|
+
if (trailingText.length > 0) {
|
|
3550
|
+
yield createSyntheticOpenAIStreamChunk(
|
|
3551
|
+
trailingText,
|
|
3552
|
+
lastChunkTemplate
|
|
3553
|
+
);
|
|
3554
|
+
}
|
|
3555
|
+
}
|
|
3292
3556
|
await collector.close();
|
|
3293
3557
|
const rawOutput = await collector.result;
|
|
3294
3558
|
const masked = piiManager.anonymize(rawOutput);
|
|
@@ -3401,6 +3665,8 @@ var AgentID = class {
|
|
|
3401
3665
|
}, requestOptions);
|
|
3402
3666
|
capabilityConfig = prepared.capabilityConfig;
|
|
3403
3667
|
maskedText = prepared.sanitizedInput;
|
|
3668
|
+
mapping = prepared.piiMapping ?? {};
|
|
3669
|
+
shouldDeanonymize = prepared.shouldDeanonymize === true;
|
|
3404
3670
|
sdkConfigFetchMs = prepared.sdkConfigFetchMs ?? 0;
|
|
3405
3671
|
sdkLocalScanMs = prepared.sdkLocalScanMs ?? 0;
|
|
3406
3672
|
if (maskedText !== userText) {
|
|
@@ -3412,24 +3678,6 @@ var AgentID = class {
|
|
|
3412
3678
|
nextCreateArgs[0] = maskedReq;
|
|
3413
3679
|
createArgs = nextCreateArgs;
|
|
3414
3680
|
}
|
|
3415
|
-
const effectivePiiMasking = this.resolveEffectivePiiMasking(capabilityConfig);
|
|
3416
|
-
if (!capabilityConfig.block_pii_leakage && effectivePiiMasking) {
|
|
3417
|
-
if (stream) {
|
|
3418
|
-
console.warn("AgentID: PII masking is disabled for streaming responses.");
|
|
3419
|
-
} else {
|
|
3420
|
-
const masked = this.pii.anonymize(maskedText);
|
|
3421
|
-
maskedText = masked.maskedText;
|
|
3422
|
-
mapping = masked.mapping;
|
|
3423
|
-
shouldDeanonymize = Object.keys(mapping).length > 0;
|
|
3424
|
-
maskedReq = this.withMaskedOpenAIRequest(
|
|
3425
|
-
req,
|
|
3426
|
-
maskedText
|
|
3427
|
-
);
|
|
3428
|
-
const nextCreateArgs = [...normalizedCreateArgs];
|
|
3429
|
-
nextCreateArgs[0] = maskedReq;
|
|
3430
|
-
createArgs = nextCreateArgs;
|
|
3431
|
-
}
|
|
3432
|
-
}
|
|
3433
3681
|
}
|
|
3434
3682
|
if (!maskedText) {
|
|
3435
3683
|
throw new Error(
|
|
@@ -3508,7 +3756,11 @@ var AgentID = class {
|
|
|
3508
3756
|
if (typeof streamResponse !== "undefined") {
|
|
3509
3757
|
yield streamResponse;
|
|
3510
3758
|
}
|
|
3511
|
-
})()
|
|
3759
|
+
})(),
|
|
3760
|
+
{
|
|
3761
|
+
piiMapping: mapping,
|
|
3762
|
+
deanonymizeForClient: shouldDeanonymize
|
|
3763
|
+
}
|
|
3512
3764
|
);
|
|
3513
3765
|
if (maskedText && wrappedCompletion.mode === "stream") {
|
|
3514
3766
|
void wrappedCompletion.done.then(async (result) => {
|
|
@@ -3545,7 +3797,8 @@ var AgentID = class {
|
|
|
3545
3797
|
...buildSdkTimingMetadata({
|
|
3546
3798
|
sdkConfigFetchMs,
|
|
3547
3799
|
sdkLocalScanMs,
|
|
3548
|
-
sdkGuardMs: guardLatencyMs
|
|
3800
|
+
sdkGuardMs: guardLatencyMs,
|
|
3801
|
+
sdkConfigVersion: capabilityConfig.version
|
|
3549
3802
|
})
|
|
3550
3803
|
},
|
|
3551
3804
|
client_capabilities: this.buildClientCapabilities(
|
|
@@ -3612,7 +3865,8 @@ var AgentID = class {
|
|
|
3612
3865
|
...buildSdkTimingMetadata({
|
|
3613
3866
|
sdkConfigFetchMs,
|
|
3614
3867
|
sdkLocalScanMs,
|
|
3615
|
-
sdkGuardMs: guardLatencyMs
|
|
3868
|
+
sdkGuardMs: guardLatencyMs,
|
|
3869
|
+
sdkConfigVersion: capabilityConfig.version
|
|
3616
3870
|
})
|
|
3617
3871
|
},
|
|
3618
3872
|
client_capabilities: this.buildClientCapabilities(
|
package/dist/index.mjs
CHANGED
package/dist/langchain.d.mts
CHANGED
package/dist/langchain.d.ts
CHANGED