|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { readFile } from "node:fs/promises"; |
| 3 | +import { initLogger, wrapOpenAI, wrapAnthropic } from "braintrust"; |
| 4 | +import { MINIMAL_PNG_BASE64 } from "./media-fixtures.mjs"; |
| 5 | +import { scopedName } from "./provider-runtime.mjs"; |
| 6 | + |
| 7 | +export const captureCases = [ |
| 8 | + { name: "default", env: "", enabled: false }, |
| 9 | + { name: "local-default", env: "", global: true, local: true, enabled: false }, |
| 10 | + { |
| 11 | + name: "local-enabled", |
| 12 | + env: "false", |
| 13 | + global: false, |
| 14 | + local: true, |
| 15 | + option: true, |
| 16 | + enabled: true, |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "local-disabled", |
| 20 | + env: "true", |
| 21 | + global: true, |
| 22 | + local: true, |
| 23 | + option: false, |
| 24 | + enabled: false, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "local-environment", |
| 28 | + env: "true", |
| 29 | + global: false, |
| 30 | + local: true, |
| 31 | + enabled: true, |
| 32 | + }, |
| 33 | + { name: "global-enabled", env: "false", option: true, enabled: true }, |
| 34 | + { name: "global-disabled", env: "true", option: false, enabled: false }, |
| 35 | +]; |
| 36 | + |
| 37 | +export async function runAttachmentCaptureScenario(provider, scenarioUrl, sdk) { |
| 38 | + const wrapped = process.env.CAPTURE_WRAPPED === "true"; |
| 39 | + let request; |
| 40 | + if (provider === "anthropic") { |
| 41 | + const raw = new sdk.default({ |
| 42 | + apiKey: process.env.ANTHROPIC_API_KEY, |
| 43 | + baseURL: process.env.ANTHROPIC_BASE_URL, |
| 44 | + }); |
| 45 | + const client = wrapped ? wrapAnthropic(raw) : raw; |
| 46 | + const data = ( |
| 47 | + await readFile(new URL("./test-image.png", scenarioUrl)) |
| 48 | + ).toString("base64"); |
| 49 | + request = async () => { |
| 50 | + const result = await client.messages.create({ |
| 51 | + model: "claude-haiku-4-5", |
| 52 | + max_tokens: 32, |
| 53 | + temperature: 0, |
| 54 | + messages: [ |
| 55 | + { |
| 56 | + role: "user", |
| 57 | + content: [ |
| 58 | + { |
| 59 | + type: "text", |
| 60 | + text: "Describe the attached image in one short sentence.", |
| 61 | + }, |
| 62 | + { |
| 63 | + type: "image", |
| 64 | + source: { type: "base64", media_type: "image/png", data }, |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + ], |
| 69 | + }); |
| 70 | + assert.ok(result.content.length > 0); |
| 71 | + }; |
| 72 | + } else if (provider === "openai") { |
| 73 | + const raw = new sdk.default({ |
| 74 | + apiKey: process.env.OPENAI_API_KEY, |
| 75 | + baseURL: process.env.OPENAI_BASE_URL, |
| 76 | + }); |
| 77 | + const client = wrapped ? wrapOpenAI(raw) : raw; |
| 78 | + request = async () => { |
| 79 | + const result = await client.chat.completions.create({ |
| 80 | + model: "gpt-4o-mini-2024-07-18", |
| 81 | + messages: [ |
| 82 | + { |
| 83 | + role: "user", |
| 84 | + content: [ |
| 85 | + { |
| 86 | + type: "text", |
| 87 | + text: "Describe this image in three words or fewer.", |
| 88 | + }, |
| 89 | + { |
| 90 | + type: "image_url", |
| 91 | + image_url: { |
| 92 | + url: `data:image/png;base64,${MINIMAL_PNG_BASE64}`, |
| 93 | + }, |
| 94 | + }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + ], |
| 98 | + max_tokens: 24, |
| 99 | + temperature: 0, |
| 100 | + }); |
| 101 | + assert.ok(result.choices[0].message.content); |
| 102 | + }; |
| 103 | + } |
| 104 | + const projectName = scopedName("tmp-luca-attachment-capture"); |
| 105 | + for (const testCase of captureCases) { |
| 106 | + process.env.BRAINTRUST_CAPTURE_ATTACHMENTS = testCase.env; |
| 107 | + if (testCase.local) |
| 108 | + initLogger({ projectName, captureAttachments: testCase.global }); |
| 109 | + const logger = initLogger({ |
| 110 | + projectName, |
| 111 | + setCurrent: !testCase.local, |
| 112 | + captureAttachments: testCase.option, |
| 113 | + }); |
| 114 | + await logger.traced(request, { |
| 115 | + name: `attachment-capture-${testCase.name}`, |
| 116 | + event: { |
| 117 | + metadata: { |
| 118 | + scenario: `${provider}-instrumentation`, |
| 119 | + testRunId: process.env.BRAINTRUST_E2E_RUN_ID, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }); |
| 123 | + await logger.flush(); |
| 124 | + } |
| 125 | +} |
0 commit comments