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.
@@ -1,9 +1,11 @@
1
1
  import {
2
+ PIIManager,
2
3
  SecurityBlockError
3
- } from "./chunk-FCOSLPMF.mjs";
4
+ } from "./chunk-XFSIAH3F.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;
@@ -369,7 +448,11 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
369
448
  model: modelName,
370
449
  clientEventId: canonicalClientEventId,
371
450
  guardEventId,
372
- transparency
451
+ transparency,
452
+ piiMapping: normalizePiiMapping(prepared.piiMapping),
453
+ shouldDeanonymize: prepared.shouldDeanonymize === true,
454
+ responseStreamed: stream,
455
+ sdkConfigVersion: prepared.capabilityConfig?.version ?? null
373
456
  });
374
457
  logCallbackDebug("handleLLMStart state_set", {
375
458
  runId: id,
@@ -458,7 +541,11 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
458
541
  model: modelName,
459
542
  clientEventId: canonicalClientEventId,
460
543
  guardEventId,
461
- transparency
544
+ transparency,
545
+ piiMapping: normalizePiiMapping(prepared.piiMapping),
546
+ shouldDeanonymize: prepared.shouldDeanonymize === true,
547
+ responseStreamed: stream,
548
+ sdkConfigVersion: prepared.capabilityConfig?.version ?? null
462
549
  });
463
550
  logCallbackDebug("handleChatModelStart state_set", {
464
551
  runId: id,
@@ -480,7 +567,11 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
480
567
  0,
481
568
  Date.now() - (state.pipelineStartedAtMs ?? state.startedAtMs)
482
569
  );
483
- const outText = extractOutputText(output);
570
+ const maskedOutputText = extractOutputText(output);
571
+ if (state.shouldDeanonymize && state.piiMapping) {
572
+ deanonymizeLangChainOutputForClient(output, state.piiMapping);
573
+ }
574
+ const clientOutputText = extractOutputText(output);
484
575
  const usage = extractTokenUsage(output);
485
576
  const metadata = {};
486
577
  if (state.clientEventId) {
@@ -500,13 +591,19 @@ var AgentIDCallbackHandler = class extends BaseCallbackHandler {
500
591
  if (state.transparency) {
501
592
  metadata.transparency = state.transparency;
502
593
  }
594
+ if (typeof state.sdkConfigVersion === "number" && Number.isFinite(state.sdkConfigVersion)) {
595
+ metadata.sdk_config_version = Math.trunc(state.sdkConfigVersion);
596
+ }
597
+ metadata.response_streamed = state.responseStreamed === true;
598
+ metadata.transformed_output = maskedOutputText;
599
+ metadata.output_masked = maskedOutputText !== clientOutputText;
503
600
  metadata.model_latency_ms = modelLatencyMs;
504
601
  metadata.total_pipeline_latency_ms = totalPipelineLatencyMs;
505
602
  const resolvedModel = state.model ?? extractModelFromOutput(output) ?? "unknown";
506
603
  await this.agent.log({
507
604
  system_id: this.systemId,
508
605
  input: state.input,
509
- output: outText,
606
+ output: maskedOutputText,
510
607
  event_id: state.clientEventId,
511
608
  model: resolvedModel,
512
609
  usage,
@@ -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-IonlG0NB.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-IonlG0NB.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.35",
4
4
  "description": "AgentID JavaScript/TypeScript SDK for guard, ingest, tracing, and analytics.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://agentid.ai",