UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

48 lines (47 loc) • 1.79 kB
import { CONTENT_KEYS, FULLY_MASKED, METADATA_PREFIXES, } from "../constants/langfuse.js"; function parseContent(value) { const text = typeof value === "string" ? value : JSON.stringify(value); try { return JSON.parse(text); } catch { return text ?? null; } } function maskValue(value, mask) { try { const masked = mask?.({ data: parseContent(value) }); return typeof masked === "string" ? masked : JSON.stringify(masked); } catch { return FULLY_MASKED; } } /** * @description Applies the exporter's policy to a span's attributes: fills environment and release when the span has none, removes input and output when `redactIO` is set, and masks input, output and metadata when a mask is configured. * @param attributes {SpanAttributes} - Attributes of the span being exported. * @param policy {LangfuseAttributePolicy} - Environment, release, redaction and mask settings. * @returns {SpanAttributes} A new attribute record; the input is not modified. */ export function protectAttributes(attributes, policy) { const result = { ...(policy.environment && { "langfuse.environment": policy.environment }), ...(policy.release && { "langfuse.release": policy.release }), ...attributes, }; for (const key of Object.keys(result)) { if (CONTENT_KEYS.has(key)) { if (policy.redactIO) { delete result[key]; } else if (policy.mask) { result[key] = maskValue(result[key], policy.mask); } } else if (policy.mask && METADATA_PREFIXES.some((prefix) => key.startsWith(prefix))) { result[key] = maskValue(result[key], policy.mask); } } return result; }