agentid-sdk 0.1.34 → 0.1.36

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.
@@ -1,9 +1,11 @@
1
1
  import {
2
+ PIIManager,
2
3
  SecurityBlockError
3
- } from "./chunk-FCOSLPMF.mjs";
4
+ } from "./chunk-TY4AXWGS.mjs";
4
5
 
5
6
  // src/langchain.ts
6
7
  import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
8
+ var piiManager = new PIIManager();
7
9
  function safeString(val) {
8
10
  return typeof val === "string" ? val : "";
9
11
  }
@@ -205,6 +207,83 @@ function extractOutputText(output) {
205
207
  const text = first?.text ?? first?.message?.content;
206
208
  return typeof text === "string" ? text : "";
207
209
  }
210
+ function normalizePiiMapping(value) {
211
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
212
+ return void 0;
213
+ }
214
+ const entries = Object.entries(value).filter(
215
+ ([placeholder, replacement]) => placeholder.length > 0 && typeof replacement === "string"
216
+ );
217
+ if (entries.length === 0) {
218
+ return void 0;
219
+ }
220
+ return Object.fromEntries(entries);
221
+ }
222
+ function deanonymizeText(text, mapping) {
223
+ if (!text || !mapping) {
224
+ return text;
225
+ }
226
+ return piiManager.deanonymize(text, mapping);
227
+ }
228
+ function deanonymizeContent(content, mapping) {
229
+ if (!mapping) {
230
+ return content;
231
+ }
232
+ if (typeof content === "string") {
233
+ return deanonymizeText(content, mapping);
234
+ }
235
+ if (!Array.isArray(content)) {
236
+ return content;
237
+ }
238
+ return content.map((item) => {
239
+ if (typeof item === "string") {
240
+ return deanonymizeText(item, mapping);
241
+ }
242
+ if (!item || typeof item !== "object") {
243
+ return item;
244
+ }
245
+ const record = { ...item };
246
+ if (typeof record.text === "string") {
247
+ record.text = deanonymizeText(record.text, mapping);
248
+ }
249
+ if (typeof record.content === "string") {
250
+ record.content = deanonymizeText(record.content, mapping);
251
+ }
252
+ return record;
253
+ });
254
+ }
255
+ function deanonymizeLangChainOutputForClient(output, mapping) {
256
+ if (!mapping) {
257
+ return;
258
+ }
259
+ const generations = output?.generations;
260
+ if (!Array.isArray(generations)) {
261
+ return;
262
+ }
263
+ for (const generationGroup of generations) {
264
+ if (!Array.isArray(generationGroup)) {
265
+ continue;
266
+ }
267
+ for (const generation of generationGroup) {
268
+ if (!generation || typeof generation !== "object") {
269
+ continue;
270
+ }
271
+ if (typeof generation.text === "string") {
272
+ generation.text = deanonymizeText(generation.text, mapping);
273
+ }
274
+ const message = generation.message;
275
+ if (message && typeof message === "object") {
276
+ const typedMessage = message;
277
+ if ("content" in typedMessage) {
278
+ typedMessage.content = deanonymizeContent(typedMessage.content, mapping);
279
+ }
280
+ if (typeof typedMessage.text === "string") {
281
+ typedMessage.text = deanonymizeText(typedMessage.text, mapping);
282
+ }
283
+ }
284
+ }
285
+ }
286
+ }
208
287
  function extractTokenUsage(output) {
209
288
  const llmOutput = output?.llmOutput ?? output?.llm_output;
210
289
  const usage = llmOutput?.tokenUsage ?? llmOutput?.token_usage ?? llmOutput?.usage ?? void 0;
@@ -269,16 +348,23 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
269
348
  get requestOptions() {
270
349
  return this.apiKeyOverride ? { apiKey: this.apiKeyOverride } : void 0;
271
350
  }
272
- getLangchainCapabilities() {
273
- const piiMaskingEnabled = this.agent.getEffectivePiiMasking(this.requestOptions);
351
+ getLangchainCapabilities(piiMaskingEnabled) {
352
+ const resolvedPiiMaskingEnabled = typeof piiMaskingEnabled === "boolean" ? piiMaskingEnabled : this.agent.getEffectivePiiMasking(this.requestOptions);
274
353
  return {
275
354
  capabilities: {
276
355
  has_feedback_handler: true,
277
- pii_masking_enabled: piiMaskingEnabled,
356
+ pii_masking_enabled: resolvedPiiMaskingEnabled,
278
357
  framework: "langchain"
279
358
  }
280
359
  };
281
360
  }
361
+ resolvePreparedPiiMaskingEnabled(prepared) {
362
+ const agentWithResolvedConfig = this.agent;
363
+ if (typeof agentWithResolvedConfig.getEffectivePiiMaskingForConfig === "function") {
364
+ return agentWithResolvedConfig.getEffectivePiiMaskingForConfig(prepared.capabilityConfig);
365
+ }
366
+ return this.agent.getEffectivePiiMasking(this.requestOptions);
367
+ }
282
368
  async preflight(input, stream, clientEventId) {
283
369
  const prepared = await this.agent.prepareInputForDispatch({
284
370
  input,
@@ -299,6 +385,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
299
385
  const stream = extractStreamFlag(serialized, extraParams);
300
386
  const prepared = await this.preflight(input, stream, requestedClientEventId);
301
387
  const sanitizedInput = prepared.sanitizedInput;
388
+ const piiMaskingEnabled = this.resolvePreparedPiiMaskingEnabled(prepared);
302
389
  if (sanitizedInput !== input) {
303
390
  const mutated = setPromptInPrompts(prompts, sanitizedInput);
304
391
  if (!mutated) {
@@ -315,7 +402,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
315
402
  model: modelName,
316
403
  client_event_id: requestedClientEventId,
317
404
  expected_languages: this.expectedLanguages,
318
- client_capabilities: this.getLangchainCapabilities()
405
+ client_capabilities: this.getLangchainCapabilities(piiMaskingEnabled)
319
406
  }, this.requestOptions);
320
407
  let transformedForRun = sanitizedInput;
321
408
  let sdkLocalScanMs = prepared.sdkLocalScanMs;
@@ -369,7 +456,12 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
369
456
  model: modelName,
370
457
  clientEventId: canonicalClientEventId,
371
458
  guardEventId,
372
- transparency
459
+ transparency,
460
+ piiMapping: normalizePiiMapping(prepared.piiMapping),
461
+ shouldDeanonymize: prepared.shouldDeanonymize === true,
462
+ responseStreamed: stream,
463
+ sdkConfigVersion: prepared.capabilityConfig?.version ?? null,
464
+ piiMaskingEnabled
373
465
  });
374
466
  logCallbackDebug("handleLLMStart state_set", {
375
467
  runId: id,
@@ -388,6 +480,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
388
480
  const stream = extractStreamFlag(serialized, extraParams);
389
481
  const prepared = await this.preflight(input, stream, requestedClientEventId);
390
482
  const sanitizedInput = prepared.sanitizedInput;
483
+ const piiMaskingEnabled = this.resolvePreparedPiiMaskingEnabled(prepared);
391
484
  if (sanitizedInput !== input) {
392
485
  const mutated = setPromptInMessages(messages, sanitizedInput);
393
486
  if (!mutated) {
@@ -404,7 +497,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
404
497
  model: modelName,
405
498
  client_event_id: requestedClientEventId,
406
499
  expected_languages: this.expectedLanguages,
407
- client_capabilities: this.getLangchainCapabilities()
500
+ client_capabilities: this.getLangchainCapabilities(piiMaskingEnabled)
408
501
  }, this.requestOptions);
409
502
  let transformedForRun = sanitizedInput;
410
503
  let sdkLocalScanMs = prepared.sdkLocalScanMs;
@@ -458,7 +551,12 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
458
551
  model: modelName,
459
552
  clientEventId: canonicalClientEventId,
460
553
  guardEventId,
461
- transparency
554
+ transparency,
555
+ piiMapping: normalizePiiMapping(prepared.piiMapping),
556
+ shouldDeanonymize: prepared.shouldDeanonymize === true,
557
+ responseStreamed: stream,
558
+ sdkConfigVersion: prepared.capabilityConfig?.version ?? null,
559
+ piiMaskingEnabled
462
560
  });
463
561
  logCallbackDebug("handleChatModelStart state_set", {
464
562
  runId: id,
@@ -480,7 +578,11 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
480
578
  0,
481
579
  Date.now() - (state.pipelineStartedAtMs ?? state.startedAtMs)
482
580
  );
483
- const outText = extractOutputText(output);
581
+ const maskedOutputText = extractOutputText(output);
582
+ if (state.shouldDeanonymize && state.piiMapping) {
583
+ deanonymizeLangChainOutputForClient(output, state.piiMapping);
584
+ }
585
+ const clientOutputText = extractOutputText(output);
484
586
  const usage = extractTokenUsage(output);
485
587
  const metadata = {};
486
588
  if (state.clientEventId) {
@@ -500,19 +602,25 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
500
602
  if (state.transparency) {
501
603
  metadata.transparency = state.transparency;
502
604
  }
605
+ if (typeof state.sdkConfigVersion === "number" && Number.isFinite(state.sdkConfigVersion)) {
606
+ metadata.sdk_config_version = Math.trunc(state.sdkConfigVersion);
607
+ }
608
+ metadata.response_streamed = state.responseStreamed === true;
609
+ metadata.transformed_output = maskedOutputText;
610
+ metadata.output_masked = maskedOutputText !== clientOutputText;
503
611
  metadata.model_latency_ms = modelLatencyMs;
504
612
  metadata.total_pipeline_latency_ms = totalPipelineLatencyMs;
505
613
  const resolvedModel = state.model ?? extractModelFromOutput(output) ?? "unknown";
506
614
  await this.agent.log({
507
615
  system_id: this.systemId,
508
616
  input: state.input,
509
- output: outText,
617
+ output: maskedOutputText,
510
618
  event_id: state.clientEventId,
511
619
  model: resolvedModel,
512
620
  usage,
513
621
  latency: modelLatencyMs,
514
622
  metadata: Object.keys(metadata).length > 0 ? metadata : void 0,
515
- client_capabilities: this.getLangchainCapabilities()
623
+ client_capabilities: this.getLangchainCapabilities(state.piiMaskingEnabled)
516
624
  }, this.requestOptions);
517
625
  logCallbackDebug("handleLLMEnd logged", {
518
626
  runId: id,
@@ -554,7 +662,7 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
554
662
  event_type: "error",
555
663
  severity: "error",
556
664
  metadata,
557
- client_capabilities: this.getLangchainCapabilities()
665
+ client_capabilities: this.getLangchainCapabilities(state?.piiMaskingEnabled)
558
666
  }, this.requestOptions);
559
667
  }
560
668
  };
@@ -1,5 +1,5 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import { T as TransparencyMetadata } from './agentid-M5I7-YqI.mjs';
2
+ import { T as TransparencyMetadata } from './agentid-JQx2Iy7B.mjs';
3
3
 
4
4
  type AgentIDTransparencyBadgeTelemetry = {
5
5
  systemId: string;
@@ -1,5 +1,5 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import { T as TransparencyMetadata } from './agentid-M5I7-YqI.js';
2
+ import { T as TransparencyMetadata } from './agentid-JQx2Iy7B.js';
3
3
 
4
4
  type AgentIDTransparencyBadgeTelemetry = {
5
5
  systemId: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentid-sdk",
3
- "version": "0.1.34",
3
+ "version": "0.1.36",
4
4
  "description": "AgentID JavaScript/TypeScript SDK for guard, ingest, tracing, and analytics.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://agentid.ai",