Skip to content

Commit 797dc02

Browse files
fix(ai): strip part-level provider options from extraction telemetry (#25396)
## Background Extraction telemetry retained `providerOptions` on text and file parts, exposing private metadata even when file URLs were redacted. ## Summary Strip part-level provider options from telemetry copies before and after state preparation, preserving arbitrary JSON state and provider inputs. ## End-to-End Verification na ## Checklist - [x] All commits are signed (PRs with unsigned commits cannot be merged) - [x] Tests have been added / updated (for bug fixes / features) - [ ] Documentation has been added / updated (for bug fixes / features) - [x] A _patch_ changeset for relevant packages has been added (for bug fixes / features - run `pnpm changeset` in the project root) - [x] I have reviewed this pull request (self-review)
1 parent 25972d7 commit 797dc02

4 files changed

Lines changed: 89 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
Strip provider options from text and file parts in extraction telemetry while preserving arbitrary JSON state and the options sent to the provider.

‎packages/ai/scripts/check-bundle-size.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { join } from 'path';
1515
// Steering interrupts add ~3 KiB.
1616
// Extraction telemetry adds ~2.5 KiB.
1717
// The bundled Gateway Live model and continuous client-secret sessions add ~6 KiB.
18-
const LIMIT = 670 * 1024;
18+
const LIMIT = 675 * 1024;
1919

2020
interface BundleResult {
2121
size: number;

‎packages/ai/src/extract/extract.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,80 @@ describe('telemetry', () => {
10881088
expect(onStart.mock.calls[0][0].state).toBe(fileState);
10891089
});
10901090

1091+
it('strips part provider options from extraction telemetry only', async () => {
1092+
const onTelemetry = vi.fn();
1093+
const url = new URL(
1094+
'https://files.example.com/invoice.pdf?X-Amz-Signature=secret',
1095+
);
1096+
const providerOptions = {
1097+
anthropic: { title: 'private-title', context: 'private-context' },
1098+
};
1099+
const jsonPart = {
1100+
type: 'json' as const,
1101+
value: { providerOptions: { user: { value: 'keep' } } },
1102+
};
1103+
const fileState: Experimental_ExtractionStatePart[] = [
1104+
{ type: 'text', text: 'Invoice', providerOptions },
1105+
{
1106+
type: 'file',
1107+
mediaType: 'application/pdf',
1108+
data: url,
1109+
providerOptions,
1110+
},
1111+
jsonPart,
1112+
];
1113+
const bytes = new Uint8Array([1, 2, 3]);
1114+
const { model, doExtract } = setup({
1115+
answers: { invoiceNumber: { type: 'text', value: 'INV-2041' } },
1116+
warnings: [],
1117+
});
1118+
1119+
await extract({
1120+
model,
1121+
state: fileState,
1122+
fields: { invoiceNumber: fields.invoiceNumber },
1123+
experimental_download: async requests =>
1124+
requests.map(() => ({ data: bytes, mediaType: 'application/pdf' })),
1125+
telemetry: {
1126+
recordInputs: true,
1127+
integrations: {
1128+
experimental_onExtractStart: onTelemetry,
1129+
experimental_onExtractionModelCallStart: onTelemetry,
1130+
experimental_onExtractionModelCallEnd: onTelemetry,
1131+
experimental_onExtractEnd: onTelemetry,
1132+
},
1133+
},
1134+
});
1135+
1136+
expect(onTelemetry).toHaveBeenCalledTimes(4);
1137+
for (const [event] of onTelemetry.mock.calls) {
1138+
expect(event.state).toEqual([
1139+
{ type: 'text', text: 'Invoice' },
1140+
{
1141+
type: 'file',
1142+
mediaType: 'application/pdf',
1143+
data:
1144+
event.operationId === 'ai.extract.doExtract'
1145+
? { type: 'data', data: bytes }
1146+
: '[redacted]',
1147+
},
1148+
jsonPart,
1149+
]);
1150+
}
1151+
expect(fileState[0]).toHaveProperty('providerOptions', providerOptions);
1152+
expect(fileState[1]).toMatchObject({ data: url, providerOptions });
1153+
expect(doExtract.mock.calls[0][0].state).toEqual([
1154+
{ type: 'text', text: 'Invoice', providerOptions },
1155+
{
1156+
type: 'file',
1157+
mediaType: 'application/pdf',
1158+
data: { type: 'data', data: bytes },
1159+
providerOptions,
1160+
},
1161+
jsonPart,
1162+
]);
1163+
});
1164+
10911165
it('redacts the url of a failed download from telemetry errors', async () => {
10921166
const onError = vi.fn();
10931167
const url = 'https://files.example.com/invoice.pdf?X-Amz-Signature=secret';

‎packages/ai/src/extract/restricted-telemetry-dispatcher.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ export function toTelemetryEvent<
2222

2323
function redactFileLocations<STATE>(state: STATE): STATE {
2424
if (!Array.isArray(state)) return state;
25-
return state.map(part =>
26-
part?.type === 'file' && isFileLocation(part.data)
27-
? { ...part, data: '[redacted]' }
28-
: part,
29-
) as STATE;
25+
return state.map(part => {
26+
if (part?.type !== 'text' && part?.type !== 'file') return part;
27+
const copy = { ...part };
28+
delete copy.providerOptions;
29+
if (copy.type === 'file' && isFileLocation(copy.data)) {
30+
copy.data = '[redacted]';
31+
}
32+
return copy;
33+
}) as STATE;
3034
}
3135

3236
function isFileLocation(data: unknown): boolean {

0 commit comments

Comments
 (0)