|
1 | 1 | import { defineConfig } from "@trigger.dev/sdk/v3"; |
2 | 2 | import { rscExtension } from "@trigger.dev/rsc"; |
| 3 | +import { AISDKExporter } from "langsmith/vercel"; |
| 4 | +import { Client } from "langsmith"; |
| 5 | + |
| 6 | +class LangsmithSpanExporterWrapper { |
| 7 | + constructor( |
| 8 | + private underlyingExporter: any, |
| 9 | + private transformSpan: (span: any) => any | undefined = (span) => { |
| 10 | + if (span.attributes["$span.partial"]) { |
| 11 | + // Skip partial spans |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + // Check if this is an attempt span |
| 16 | + if (span.name.startsWith("Attempt ")) { |
| 17 | + // Create a new span that wraps the original but modifies spanContext |
| 18 | + const spanContext = span.spanContext(); |
| 19 | + |
| 20 | + return { |
| 21 | + ...span, |
| 22 | + spanContext: () => spanContext, |
| 23 | + parentSpanId: undefined, |
| 24 | + }; |
| 25 | + } |
| 26 | + return span; |
| 27 | + } |
| 28 | + ) {} |
| 29 | + |
| 30 | + export(spans: any[], resultCallback: (result: any) => void): void { |
| 31 | + const modifiedSpans = spans.map(this.transformSpan); |
| 32 | + this.underlyingExporter.export(modifiedSpans.filter(Boolean), resultCallback); |
| 33 | + } |
| 34 | + |
| 35 | + shutdown(): Promise<void> { |
| 36 | + return this.underlyingExporter.shutdown(); |
| 37 | + } |
| 38 | + |
| 39 | + forceFlush?(): Promise<void> { |
| 40 | + return this.underlyingExporter.forceFlush |
| 41 | + ? this.underlyingExporter.forceFlush() |
| 42 | + : Promise.resolve(); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +const client = new Client(); |
| 47 | + |
| 48 | +const exporter = new AISDKExporter({ |
| 49 | + debug: true, |
| 50 | + client, |
| 51 | +}); |
3 | 52 |
|
4 | 53 | export default defineConfig({ |
5 | 54 | project: "proj_bzhdaqhlymtuhlrcgbqy", |
6 | 55 | dirs: ["./src/trigger"], |
| 56 | + exporters: [new LangsmithSpanExporterWrapper(exporter)], |
7 | 57 | build: { |
8 | 58 | extensions: [rscExtension({ reactDomEnvironment: "worker" })], |
9 | 59 | }, |
|
0 commit comments