The Open Framework for Building Agents
import { describe, expect, it } from "vitest";
import { withoutDeclinedContent } from "./content-policy.js";
const ATTRIBUTES = {
"agent.channel.delivery.input": '{"message":"private"}',
"agent.session.title": "Private title",
"agent.trace.content.input": true,
"agent.trace.content.output": true,
"gen_ai.input.messages": "what the user said",
"gen_ai.system_instructions": '[{"content":"private instructions","type":"text"}]',
"ai.response.finish_reason": "stop",
"ai.response.text": "what the model said",
"gen_ai.request.model": "test-model",
"gen_ai.tool.call.arguments": "{}",
"gen_ai.tool.call.result": "42",
"gen_ai.tool.name": "weather",
};
describe("withoutDeclinedContent", () => {
it("keeps everything when the destination declined nothing", () => {
expect(
withoutDeclinedContent(ATTRIBUTES, { recordInputs: true, recordOutputs: true }),
).toBeUndefined();
});
it("keeps everything when the span carries none of what was declined", () => {
expect(
withoutDeclinedContent(
{ "gen_ai.request.model": "test-model" },
{ recordInputs: false, recordOutputs: false },
),
).toBeUndefined();
});
it("drops inputs alone", () => {
expect(
withoutDeclinedContent(ATTRIBUTES, { recordInputs: false, recordOutputs: true }),
).toEqual({
"agent.trace.content.input": false,
"agent.trace.content.output": true,
"ai.response.finish_reason": "stop",
"ai.response.text": "what the model said",
"gen_ai.request.model": "test-model",
"gen_ai.tool.call.result": "42",
"gen_ai.tool.name": "weather",
});
});
it("drops outputs alone", () => {
expect(
withoutDeclinedContent(ATTRIBUTES, { recordInputs: true, recordOutputs: false }),
).toEqual({
"agent.channel.delivery.input": '{"message":"private"}',
"agent.session.title": "Private title",
"agent.trace.content.input": true,
"agent.trace.content.output": false,
"gen_ai.input.messages": "what the user said",
"gen_ai.system_instructions": '[{"content":"private instructions","type":"text"}]',
"ai.response.finish_reason": "stop",
"gen_ai.request.model": "test-model",
"gen_ai.tool.call.arguments": "{}",
"gen_ai.tool.name": "weather",
});
});
it("drops system instructions when the destination declined inputs", () => {
expect(
withoutDeclinedContent(
{ "gen_ai.system_instructions": '[{"content":"private","type":"text"}]' },
{ recordInputs: false, recordOutputs: true },
),
).toEqual({});
});
// The prefixes are shared: `ai.response.finish_reason` and `gen_ai.tool.name`
// say what happened rather than what was said, so declining content cannot
// cost a destination the ability to read its own traces.
it("keeps metadata that shares a prefix with content", () => {
expect(
withoutDeclinedContent(ATTRIBUTES, { recordInputs: false, recordOutputs: false }),
).toEqual({
"agent.trace.content.input": false,
"agent.trace.content.output": false,
"ai.response.finish_reason": "stop",
"gen_ai.request.model": "test-model",
"gen_ai.tool.name": "weather",
});
});
it("leaves the attributes it was handed alone", () => {
const attributes = { ...ATTRIBUTES };
withoutDeclinedContent(attributes, { recordInputs: false, recordOutputs: false });
expect(attributes).toEqual(ATTRIBUTES);
});
it("narrows content policy attributes even when no content is present", () => {
expect(
withoutDeclinedContent(
{
"agent.trace.content.input": true,
"agent.trace.content.output": true,
},
{ recordInputs: false, recordOutputs: true },
),
).toEqual({
"agent.trace.content.input": false,
"agent.trace.content.output": true,
});
});
it("redacts recalled memory records as input content", () => {
const searched = {
"gen_ai.memory.records": '[{"content":"Private preference"}]',
"gen_ai.operation.name": "search_memory",
};
expect(withoutDeclinedContent(searched, { recordInputs: false, recordOutputs: true })).toEqual({
"gen_ai.operation.name": "search_memory",
});
expect(
withoutDeclinedContent(searched, { recordInputs: true, recordOutputs: false }),
).toBeUndefined();
});
});