|
| 1 | +import { join } from 'node:path'; |
| 2 | +import type { SerializedStreamedSpan } from '@sentry/core'; |
| 3 | +import { |
| 4 | + GEN_AI_INPUT_MESSAGES, |
| 5 | + GEN_AI_OUTPUT_MESSAGES, |
| 6 | + GEN_AI_TOOL_CALL_ARGUMENTS, |
| 7 | + GEN_AI_TOOL_CALL_RESULT, |
| 8 | + SENTRY_ORIGIN, |
| 9 | +} from '@sentry/conventions/attributes'; |
| 10 | +import { afterAll, describe, expect } from 'vitest'; |
| 11 | +import { supports } from '../../../../utils'; |
| 12 | +import { cleanupChildProcesses, createEsmTests } from '../../../../utils/runner'; |
| 13 | + |
| 14 | +function recorded(span: SerializedStreamedSpan): { span: string; inputs: boolean; outputs: boolean } { |
| 15 | + const { attributes } = span; |
| 16 | + return { |
| 17 | + span: span.name, |
| 18 | + inputs: GEN_AI_INPUT_MESSAGES in attributes || GEN_AI_TOOL_CALL_ARGUMENTS in attributes, |
| 19 | + outputs: GEN_AI_OUTPUT_MESSAGES in attributes || GEN_AI_TOOL_CALL_RESULT in attributes, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +// `@flue/runtime` declares `engines.node >= 22.19`. |
| 24 | +describe.runIf(supports({ min: 22 }))('Flue integration', () => { |
| 25 | + afterAll(() => { |
| 26 | + cleanupChildProcesses(); |
| 27 | + }); |
| 28 | + |
| 29 | + createEsmTests( |
| 30 | + join(__dirname, '..'), |
| 31 | + 'scenario-recording-functions.mjs', |
| 32 | + 'instrument-with-pii.mjs', |
| 33 | + (createRunner, test) => { |
| 34 | + test('decides content recording per call when the options are functions', async () => { |
| 35 | + await createRunner() |
| 36 | + .expect({ |
| 37 | + span: container => { |
| 38 | + const spans = container.items; |
| 39 | + const byId = new Map(spans.map(span => [span.span_id, span])); |
| 40 | + const runOf = (span: SerializedStreamedSpan): string | undefined => { |
| 41 | + let current: SerializedStreamedSpan | undefined = span; |
| 42 | + while (current && !current.name.startsWith('run ')) { |
| 43 | + current = current.parent_span_id ? byId.get(current.parent_span_id) : undefined; |
| 44 | + } |
| 45 | + return current?.name; |
| 46 | + }; |
| 47 | + const contentSpans = spans |
| 48 | + .filter(span => |
| 49 | + ['auto.ai.pi_ai', 'auto.ai.flue'].includes(String(span.attributes[SENTRY_ORIGIN]?.value)), |
| 50 | + ) |
| 51 | + .filter(span => span.name !== 'invoke_agent Hello') |
| 52 | + .sort((a, b) => a.start_timestamp - b.start_timestamp); |
| 53 | + |
| 54 | + expect({ |
| 55 | + public: contentSpans.filter(span => runOf(span) === 'run public').map(recorded), |
| 56 | + private: contentSpans.filter(span => runOf(span) === 'run private').map(recorded), |
| 57 | + }).toEqual({ |
| 58 | + public: [ |
| 59 | + { span: 'chat faux-model', inputs: true, outputs: true }, |
| 60 | + { span: 'execute_tool get_weather', inputs: true, outputs: true }, |
| 61 | + { span: 'chat faux-model', inputs: true, outputs: true }, |
| 62 | + ], |
| 63 | + private: [ |
| 64 | + { span: 'chat faux-model', inputs: false, outputs: false }, |
| 65 | + { span: 'execute_tool get_weather', inputs: false, outputs: false }, |
| 66 | + { span: 'chat faux-model', inputs: false, outputs: true }, |
| 67 | + ], |
| 68 | + }); |
| 69 | + }, |
| 70 | + }) |
| 71 | + .start() |
| 72 | + .completed(); |
| 73 | + }); |
| 74 | + }, |
| 75 | + ); |
| 76 | +}); |
0 commit comments