agentid-sdk 0.1.41 → 0.1.42
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/README.md +83 -2
- package/dist/{agentid-Mjh8rXn0.d.mts → agentid-DbTWrLnN.d.mts} +29 -0
- package/dist/{agentid-Mjh8rXn0.d.ts → agentid-DbTWrLnN.d.ts} +29 -0
- package/dist/{chunk-L2WVWRAC.mjs → chunk-C5U4L4JY.mjs} +566 -418
- package/dist/index.d.mts +27 -3
- package/dist/index.d.ts +27 -3
- package/dist/index.js +598 -422
- package/dist/index.mjs +32 -5
- package/dist/langchain.d.mts +8 -1
- package/dist/langchain.d.ts +8 -1
- package/dist/langchain.js +301 -71
- package/dist/langchain.mjs +129 -44
- package/dist/transparency-badge.d.mts +1 -1
- package/dist/transparency-badge.d.ts +1 -1
- package/package.json +2 -1
package/dist/langchain.mjs
CHANGED
|
@@ -2,12 +2,24 @@ import {
|
|
|
2
2
|
PIIManager,
|
|
3
3
|
SecurityBlockError,
|
|
4
4
|
createAgentIdTelemetryContext
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-C5U4L4JY.mjs";
|
|
6
6
|
|
|
7
7
|
// src/langchain.ts
|
|
8
8
|
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
|
|
9
9
|
var piiManager = new PIIManager();
|
|
10
10
|
var LANGCHAIN_TELEMETRY_FIELD = "agentid_telemetry";
|
|
11
|
+
var MAX_PROMPT_CONTEXT_CHARS = 64e3;
|
|
12
|
+
function getZeroRetentionInput(rawInput, candidateInput) {
|
|
13
|
+
const raw = typeof rawInput === "string" ? rawInput : "";
|
|
14
|
+
const candidate = typeof candidateInput === "string" ? candidateInput : "";
|
|
15
|
+
if (candidate.length > 0 && candidate !== raw) {
|
|
16
|
+
return candidate;
|
|
17
|
+
}
|
|
18
|
+
return "[REDACTED]";
|
|
19
|
+
}
|
|
20
|
+
function isFailOpenGuardBypassReason(reason) {
|
|
21
|
+
return reason === "timeout_fallback" || reason === "guard_unreachable" || reason === "system_failure_fail_open";
|
|
22
|
+
}
|
|
11
23
|
function safeString(val) {
|
|
12
24
|
return typeof val === "string" ? val : "";
|
|
13
25
|
}
|
|
@@ -178,9 +190,6 @@ function setFiniteDurationMetadata(metadata, key, value) {
|
|
|
178
190
|
metadata[key] = Math.max(0, Math.trunc(value));
|
|
179
191
|
}
|
|
180
192
|
}
|
|
181
|
-
function isGuardFailureEligibleForLocalFallback(reason) {
|
|
182
|
-
return reason === "network_error_strict_mode" || reason === "server_error" || reason === "system_failure" || reason === "system_failure_db_unavailable" || reason === "logging_failed" || reason === "guard_unreachable" || reason === "api_key_pepper_missing" || reason === "encryption_key_missing";
|
|
183
|
-
}
|
|
184
193
|
function extractTextFromContent(content) {
|
|
185
194
|
if (typeof content === "string") {
|
|
186
195
|
return content;
|
|
@@ -230,6 +239,26 @@ function extractPromptFromPrompts(prompts) {
|
|
|
230
239
|
}
|
|
231
240
|
return "";
|
|
232
241
|
}
|
|
242
|
+
function truncatePromptContext(value) {
|
|
243
|
+
if (value.length <= MAX_PROMPT_CONTEXT_CHARS) {
|
|
244
|
+
return value;
|
|
245
|
+
}
|
|
246
|
+
const headChars = Math.floor((MAX_PROMPT_CONTEXT_CHARS - 32) / 2);
|
|
247
|
+
const tailChars = MAX_PROMPT_CONTEXT_CHARS - headChars - 32;
|
|
248
|
+
return `${value.slice(0, headChars)}
|
|
249
|
+
[...TRUNCATED CONTEXT...]
|
|
250
|
+
${value.slice(-tailChars)}`;
|
|
251
|
+
}
|
|
252
|
+
function extractPromptContextFromPrompts(prompts) {
|
|
253
|
+
if (!Array.isArray(prompts)) {
|
|
254
|
+
return "";
|
|
255
|
+
}
|
|
256
|
+
const sections = prompts.map((prompt, index) => {
|
|
257
|
+
const text = safeString(prompt);
|
|
258
|
+
return text ? `[prompt_${index + 1}] ${text}` : null;
|
|
259
|
+
}).filter((section) => section !== null);
|
|
260
|
+
return sections.length > 0 ? truncatePromptContext(sections.join("\n\n")) : "";
|
|
261
|
+
}
|
|
233
262
|
function extractPromptFromMessages(messages) {
|
|
234
263
|
const flat = [];
|
|
235
264
|
if (Array.isArray(messages)) {
|
|
@@ -255,6 +284,28 @@ function extractPromptFromMessages(messages) {
|
|
|
255
284
|
const typedLast = last;
|
|
256
285
|
return extractTextFromContent(typedLast.content ?? typedLast.text);
|
|
257
286
|
}
|
|
287
|
+
function extractPromptContextFromMessages(messages) {
|
|
288
|
+
const flat = [];
|
|
289
|
+
if (Array.isArray(messages)) {
|
|
290
|
+
for (const item of messages) {
|
|
291
|
+
if (Array.isArray(item)) {
|
|
292
|
+
flat.push(...item);
|
|
293
|
+
} else {
|
|
294
|
+
flat.push(item);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const sections = flat.map((message) => {
|
|
299
|
+
const role = getMessageRole(message) ?? "message";
|
|
300
|
+
const typed = message;
|
|
301
|
+
const text = extractTextFromContent(typed?.content ?? typed?.text);
|
|
302
|
+
return text ? `[${role}] ${text}` : null;
|
|
303
|
+
}).filter((section) => section !== null);
|
|
304
|
+
return sections.length > 0 ? truncatePromptContext(sections.join("\n\n")) : "";
|
|
305
|
+
}
|
|
306
|
+
function maskPromptContext(promptContext, options) {
|
|
307
|
+
return promptContext;
|
|
308
|
+
}
|
|
258
309
|
function setPromptInPrompts(prompts, sanitizedInput) {
|
|
259
310
|
if (!Array.isArray(prompts) || prompts.length === 0) {
|
|
260
311
|
return false;
|
|
@@ -384,6 +435,12 @@ function deanonymizeText(text, mapping) {
|
|
|
384
435
|
}
|
|
385
436
|
return piiManager.deanonymize(text, mapping);
|
|
386
437
|
}
|
|
438
|
+
function textContainsMappingPlaceholder(text, mapping) {
|
|
439
|
+
if (!text || !mapping) {
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
return Object.keys(mapping).some((placeholder) => placeholder.length > 0 && text.includes(placeholder));
|
|
443
|
+
}
|
|
387
444
|
function deanonymizeContent(content, mapping) {
|
|
388
445
|
if (!mapping) {
|
|
389
446
|
return content;
|
|
@@ -506,6 +563,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
506
563
|
);
|
|
507
564
|
this.apiKeyOverride = options.apiKey?.trim() || options.api_key?.trim() || void 0;
|
|
508
565
|
this.telemetry = createAgentIdTelemetryContext(options.telemetry);
|
|
566
|
+
this.deanonymizeOutputForClient = options.deanonymizeOutputForClient === true || options.deanonymize_output_for_client === true;
|
|
509
567
|
}
|
|
510
568
|
get requestOptions() {
|
|
511
569
|
return this.apiKeyOverride ? { apiKey: this.apiKeyOverride } : void 0;
|
|
@@ -539,9 +597,10 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
539
597
|
}
|
|
540
598
|
return false;
|
|
541
599
|
}
|
|
542
|
-
async preflight(input, stream, clientEventId, telemetryMetadata) {
|
|
600
|
+
async preflight(input, stream, clientEventId, telemetryMetadata, promptContext) {
|
|
543
601
|
const prepared = await this.agent.prepareInputForDispatch({
|
|
544
602
|
input,
|
|
603
|
+
promptContext,
|
|
545
604
|
systemId: this.systemId,
|
|
546
605
|
stream,
|
|
547
606
|
clientEventId,
|
|
@@ -602,6 +661,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
602
661
|
}
|
|
603
662
|
async handleLLMStart(serialized, prompts, runId, _parentRunId, extraParams) {
|
|
604
663
|
const input = extractPromptFromPrompts(prompts);
|
|
664
|
+
const rawPromptContext = extractPromptContextFromPrompts(prompts);
|
|
605
665
|
const id = String(runId ?? "");
|
|
606
666
|
logCallbackDebug("handleLLMStart", { runId: id, hasInput: input.length > 0 });
|
|
607
667
|
if (!input) {
|
|
@@ -614,7 +674,8 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
614
674
|
input,
|
|
615
675
|
stream,
|
|
616
676
|
requestedClientEventId,
|
|
617
|
-
telemetryMetadata
|
|
677
|
+
telemetryMetadata,
|
|
678
|
+
rawPromptContext
|
|
618
679
|
);
|
|
619
680
|
const sanitizedInput = prepared.sanitizedInput;
|
|
620
681
|
const piiMaskingEnabled = this.resolvePreparedPiiMaskingEnabled(prepared);
|
|
@@ -627,10 +688,18 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
627
688
|
);
|
|
628
689
|
}
|
|
629
690
|
}
|
|
691
|
+
const promptContextForGuard = maskPromptContext(
|
|
692
|
+
extractPromptContextFromPrompts(prompts),
|
|
693
|
+
{
|
|
694
|
+
pii: piiMaskingEnabled,
|
|
695
|
+
secrets: secretMaskingEnabled
|
|
696
|
+
}
|
|
697
|
+
);
|
|
630
698
|
const modelName = extractModel(serialized, extraParams);
|
|
631
699
|
const pipelineStartedAtMs = Date.now();
|
|
632
700
|
const verdict = await this.agent.guard({
|
|
633
701
|
input: sanitizedInput,
|
|
702
|
+
prompt_context: promptContextForGuard || void 0,
|
|
634
703
|
system_id: this.systemId,
|
|
635
704
|
model: modelName,
|
|
636
705
|
client_event_id: requestedClientEventId,
|
|
@@ -642,28 +711,30 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
642
711
|
)
|
|
643
712
|
}, this.requestOptions);
|
|
644
713
|
let transformedForRun = sanitizedInput;
|
|
645
|
-
let
|
|
714
|
+
let sdkConfigFetchMs = prepared.sdkConfigFetchMs ?? 0;
|
|
715
|
+
let sdkLocalScanMs = prepared.sdkLocalScanMs ?? 0;
|
|
646
716
|
let localFallbackApplied = false;
|
|
647
717
|
let localFallbackReason = null;
|
|
648
718
|
if (!verdict.allowed) {
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
719
|
+
throw new SecurityBlockError(verdict.reason ?? "guard_denied");
|
|
720
|
+
}
|
|
721
|
+
if (isFailOpenGuardBypassReason(verdict.reason)) {
|
|
722
|
+
const fallback = await this.agent.runLocalPromptInjectionFallback(
|
|
723
|
+
{
|
|
724
|
+
input,
|
|
725
|
+
promptContext: rawPromptContext,
|
|
653
726
|
systemId: this.systemId,
|
|
654
|
-
stream,
|
|
655
727
|
clientEventId: requestedClientEventId,
|
|
656
728
|
capabilityConfig: prepared.capabilityConfig,
|
|
657
|
-
sdkConfigFetchMs
|
|
729
|
+
sdkConfigFetchMs,
|
|
658
730
|
telemetryMetadata
|
|
659
|
-
},
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
}
|
|
731
|
+
},
|
|
732
|
+
this.requestOptions
|
|
733
|
+
);
|
|
734
|
+
sdkConfigFetchMs = fallback.sdkConfigFetchMs;
|
|
735
|
+
sdkLocalScanMs += fallback.sdkLocalScanMs;
|
|
736
|
+
localFallbackApplied = true;
|
|
737
|
+
localFallbackReason = verdict.reason ?? null;
|
|
667
738
|
}
|
|
668
739
|
if (transformedForRun !== sanitizedInput) {
|
|
669
740
|
const mutated = setPromptInPrompts(prompts, transformedForRun);
|
|
@@ -682,12 +753,13 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
682
753
|
transformedInput = transformedForRun;
|
|
683
754
|
}
|
|
684
755
|
}
|
|
756
|
+
const retainedInput = getZeroRetentionInput(input, transformedInput);
|
|
685
757
|
this.runs.set(id, {
|
|
686
|
-
input:
|
|
758
|
+
input: retainedInput,
|
|
687
759
|
startedAtMs: Date.now(),
|
|
688
760
|
pipelineStartedAtMs,
|
|
689
761
|
guardLatencyMs,
|
|
690
|
-
sdkConfigFetchMs
|
|
762
|
+
sdkConfigFetchMs,
|
|
691
763
|
sdkLocalScanMs,
|
|
692
764
|
localFallbackApplied,
|
|
693
765
|
localFallbackReason,
|
|
@@ -697,7 +769,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
697
769
|
telemetryMetadata,
|
|
698
770
|
transparency,
|
|
699
771
|
piiMapping: normalizePiiMapping(prepared.piiMapping),
|
|
700
|
-
shouldDeanonymize: prepared.shouldDeanonymize === true,
|
|
772
|
+
shouldDeanonymize: this.deanonymizeOutputForClient && prepared.shouldDeanonymize === true,
|
|
701
773
|
responseStreamed: stream,
|
|
702
774
|
sdkConfigVersion: prepared.capabilityConfig?.version ?? null,
|
|
703
775
|
piiMaskingEnabled,
|
|
@@ -711,6 +783,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
711
783
|
}
|
|
712
784
|
async handleChatModelStart(serialized, messages, runId, _parentRunId, extraParams) {
|
|
713
785
|
const input = extractPromptFromMessages(messages);
|
|
786
|
+
const rawPromptContext = extractPromptContextFromMessages(messages);
|
|
714
787
|
const id = String(runId ?? "");
|
|
715
788
|
logCallbackDebug("handleChatModelStart", { runId: id, hasInput: input.length > 0 });
|
|
716
789
|
if (!input) {
|
|
@@ -723,7 +796,8 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
723
796
|
input,
|
|
724
797
|
stream,
|
|
725
798
|
requestedClientEventId,
|
|
726
|
-
telemetryMetadata
|
|
799
|
+
telemetryMetadata,
|
|
800
|
+
rawPromptContext
|
|
727
801
|
);
|
|
728
802
|
const sanitizedInput = prepared.sanitizedInput;
|
|
729
803
|
const piiMaskingEnabled = this.resolvePreparedPiiMaskingEnabled(prepared);
|
|
@@ -736,10 +810,18 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
736
810
|
);
|
|
737
811
|
}
|
|
738
812
|
}
|
|
813
|
+
const promptContextForGuard = maskPromptContext(
|
|
814
|
+
extractPromptContextFromMessages(messages),
|
|
815
|
+
{
|
|
816
|
+
pii: piiMaskingEnabled,
|
|
817
|
+
secrets: secretMaskingEnabled
|
|
818
|
+
}
|
|
819
|
+
);
|
|
739
820
|
const modelName = extractModel(serialized, extraParams);
|
|
740
821
|
const pipelineStartedAtMs = Date.now();
|
|
741
822
|
const verdict = await this.agent.guard({
|
|
742
823
|
input: sanitizedInput,
|
|
824
|
+
prompt_context: promptContextForGuard || void 0,
|
|
743
825
|
system_id: this.systemId,
|
|
744
826
|
model: modelName,
|
|
745
827
|
client_event_id: requestedClientEventId,
|
|
@@ -751,28 +833,30 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
751
833
|
)
|
|
752
834
|
}, this.requestOptions);
|
|
753
835
|
let transformedForRun = sanitizedInput;
|
|
754
|
-
let
|
|
836
|
+
let sdkConfigFetchMs = prepared.sdkConfigFetchMs ?? 0;
|
|
837
|
+
let sdkLocalScanMs = prepared.sdkLocalScanMs ?? 0;
|
|
755
838
|
let localFallbackApplied = false;
|
|
756
839
|
let localFallbackReason = null;
|
|
757
840
|
if (!verdict.allowed) {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
841
|
+
throw new SecurityBlockError(verdict.reason ?? "guard_denied");
|
|
842
|
+
}
|
|
843
|
+
if (isFailOpenGuardBypassReason(verdict.reason)) {
|
|
844
|
+
const fallback = await this.agent.runLocalPromptInjectionFallback(
|
|
845
|
+
{
|
|
846
|
+
input,
|
|
847
|
+
promptContext: rawPromptContext,
|
|
762
848
|
systemId: this.systemId,
|
|
763
|
-
stream,
|
|
764
849
|
clientEventId: requestedClientEventId,
|
|
765
850
|
capabilityConfig: prepared.capabilityConfig,
|
|
766
|
-
sdkConfigFetchMs
|
|
851
|
+
sdkConfigFetchMs,
|
|
767
852
|
telemetryMetadata
|
|
768
|
-
},
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
}
|
|
853
|
+
},
|
|
854
|
+
this.requestOptions
|
|
855
|
+
);
|
|
856
|
+
sdkConfigFetchMs = fallback.sdkConfigFetchMs;
|
|
857
|
+
sdkLocalScanMs += fallback.sdkLocalScanMs;
|
|
858
|
+
localFallbackApplied = true;
|
|
859
|
+
localFallbackReason = verdict.reason ?? null;
|
|
776
860
|
}
|
|
777
861
|
if (transformedForRun !== sanitizedInput) {
|
|
778
862
|
const mutated = setPromptInMessages(messages, transformedForRun);
|
|
@@ -791,12 +875,13 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
791
875
|
transformedInput = transformedForRun;
|
|
792
876
|
}
|
|
793
877
|
}
|
|
878
|
+
const retainedInput = getZeroRetentionInput(input, transformedInput);
|
|
794
879
|
this.runs.set(id, {
|
|
795
|
-
input:
|
|
880
|
+
input: retainedInput,
|
|
796
881
|
startedAtMs: Date.now(),
|
|
797
882
|
pipelineStartedAtMs,
|
|
798
883
|
guardLatencyMs,
|
|
799
|
-
sdkConfigFetchMs
|
|
884
|
+
sdkConfigFetchMs,
|
|
800
885
|
sdkLocalScanMs,
|
|
801
886
|
localFallbackApplied,
|
|
802
887
|
localFallbackReason,
|
|
@@ -806,7 +891,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
806
891
|
telemetryMetadata,
|
|
807
892
|
transparency,
|
|
808
893
|
piiMapping: normalizePiiMapping(prepared.piiMapping),
|
|
809
|
-
shouldDeanonymize: prepared.shouldDeanonymize === true,
|
|
894
|
+
shouldDeanonymize: this.deanonymizeOutputForClient && prepared.shouldDeanonymize === true,
|
|
810
895
|
responseStreamed: stream,
|
|
811
896
|
sdkConfigVersion: prepared.capabilityConfig?.version ?? null,
|
|
812
897
|
piiMaskingEnabled,
|
|
@@ -863,7 +948,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
|
|
|
863
948
|
}
|
|
864
949
|
metadata.response_streamed = state.responseStreamed === true;
|
|
865
950
|
metadata.transformed_output = maskedOutputText;
|
|
866
|
-
metadata.output_masked = maskedOutputText !== clientOutputText;
|
|
951
|
+
metadata.output_masked = maskedOutputText !== clientOutputText || textContainsMappingPlaceholder(maskedOutputText, state.piiMapping);
|
|
867
952
|
metadata.model_latency_ms = modelLatencyMs;
|
|
868
953
|
metadata.total_pipeline_latency_ms = totalPipelineLatencyMs;
|
|
869
954
|
const resolvedModel = state.model ?? extractModelFromOutput(output) ?? "unknown";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentid-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.42",
|
|
4
4
|
"description": "AgentID JavaScript/TypeScript SDK for guardrails, masking, workflow telemetry, and audit logging.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://agentid.ai",
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"@langchain/core": "^1.1.42",
|
|
68
68
|
"@types/react": "^19.2.2",
|
|
69
|
+
"langsmith": "^0.7.1",
|
|
69
70
|
"tsup": "^8.3.5",
|
|
70
71
|
"typescript": "^5.0.0",
|
|
71
72
|
"vitest": "^4.0.16"
|