@mastra/langfuse 0.0.0-vector-query-tool-provider-options-20250828222356 → 0.0.0-vnext-20251104230439

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ai-tracing.ts DELETED
@@ -1,198 +0,0 @@
1
- /**
2
- * Langfuse Exporter for Mastra AI Tracing
3
- *
4
- * This exporter sends tracing data to Langfuse for AI observability.
5
- * Root spans start traces in Langfuse.
6
- * LLM_GENERATION spans become Langfuse generations, all others become spans.
7
- */
8
-
9
- import type { AITracingExporter, AITracingEvent, AnyAISpan, LLMGenerationAttributes } from '@mastra/core/ai-tracing';
10
- import { AISpanType, sanitizeMetadata, omitKeys } from '@mastra/core/ai-tracing';
11
- import { Langfuse } from 'langfuse';
12
- import type { LangfuseTraceClient, LangfuseSpanClient, LangfuseGenerationClient } from 'langfuse';
13
-
14
- export interface LangfuseExporterConfig {
15
- /** Langfuse API key */
16
- publicKey: string;
17
- /** Langfuse secret key */
18
- secretKey: string;
19
- /** Langfuse host URL */
20
- baseUrl: string;
21
- /** Enable realtime mode - flushes after each event for immediate visibility */
22
- realtime?: boolean;
23
- /** Additional options to pass to the Langfuse client */
24
- options?: any;
25
- }
26
-
27
- type TraceData = {
28
- trace: LangfuseTraceClient; // Langfuse trace object
29
- spans: Map<string, LangfuseSpanClient | LangfuseGenerationClient>; // Maps span.id to Langfuse span/generation
30
- };
31
-
32
- type LangfuseSpan = LangfuseTraceClient | LangfuseSpanClient | LangfuseGenerationClient;
33
-
34
- export class LangfuseExporter implements AITracingExporter {
35
- name = 'langfuse';
36
- private client: Langfuse;
37
- private realtime: boolean;
38
- private traceMap = new Map<string, TraceData>();
39
-
40
- constructor(config: LangfuseExporterConfig) {
41
- this.realtime = config.realtime ?? false;
42
- this.client = new Langfuse({
43
- publicKey: config.publicKey,
44
- secretKey: config.secretKey,
45
- baseUrl: config.baseUrl,
46
- ...config.options,
47
- });
48
- }
49
-
50
- async exportEvent(event: AITracingEvent): Promise<void> {
51
- switch (event.type) {
52
- case 'span_started':
53
- await this.handleSpanStarted(event.span);
54
- break;
55
- case 'span_updated':
56
- await this.handleSpanUpdateOrEnd(event.span, true);
57
- break;
58
- case 'span_ended':
59
- await this.handleSpanUpdateOrEnd(event.span, false);
60
- break;
61
- }
62
-
63
- // Flush immediately in realtime mode for instant visibility
64
- if (this.realtime) {
65
- await this.client.flushAsync();
66
- }
67
- }
68
-
69
- private async handleSpanStarted(span: AnyAISpan): Promise<void> {
70
- if (span.isRootSpan) {
71
- const trace = this.client.trace(this.buildTracePayload(span));
72
- this.traceMap.set(span.trace.id, { trace, spans: new Map() });
73
- }
74
-
75
- const traceData = this.traceMap.get(span.trace.id);
76
- if (!traceData) {
77
- console.log('NO TRACE');
78
- // TODO: log warning
79
- return;
80
- }
81
-
82
- const langfuseParent =
83
- span.parent && traceData.spans.has(span.parent.id)
84
- ? (traceData.spans.get(span.parent.id) as LangfuseSpan)
85
- : traceData.trace;
86
-
87
- const payload = this.buildSpanPayload(span, true);
88
-
89
- const langfuseSpan =
90
- span.type === AISpanType.LLM_GENERATION ? langfuseParent.generation(payload) : langfuseParent.span(payload);
91
-
92
- traceData.spans.set(span.id, langfuseSpan);
93
- }
94
-
95
- private async handleSpanUpdateOrEnd(span: AnyAISpan, isUpdate: boolean): Promise<void> {
96
- const traceData = this.traceMap.get(span.trace.id);
97
- if (!traceData) {
98
- console.log('NO TRACE');
99
- // TODO: log warning
100
- return;
101
- }
102
-
103
- const langfuseSpan = traceData.spans.get(span.id);
104
- if (!langfuseSpan) {
105
- console.log('NO SPAN');
106
- // TODO: log warning
107
- return;
108
- }
109
-
110
- if (isUpdate) {
111
- langfuseSpan.update(this.buildSpanPayload(span, false));
112
- } else {
113
- langfuseSpan.end(this.buildSpanPayload(span, false));
114
-
115
- if (span.isRootSpan) {
116
- traceData.trace.update({ output: span.output });
117
- this.traceMap.delete(span.trace.id);
118
- }
119
- }
120
- }
121
-
122
- private buildTracePayload(span: AnyAISpan): Record<string, any> {
123
- const payload: Record<string, any> = {
124
- id: span.trace.id,
125
- name: span.name,
126
- };
127
-
128
- const { userId, sessionId, ...remainingMetadata } = span.metadata ?? {};
129
-
130
- if (userId) payload.userId = userId;
131
- if (sessionId) payload.sessionId = sessionId;
132
- if (span.input) payload.input = span.input;
133
-
134
- payload.metadata = {
135
- spanType: span.type,
136
- ...sanitizeMetadata(span.attributes),
137
- ...sanitizeMetadata(remainingMetadata),
138
- };
139
-
140
- return payload;
141
- }
142
-
143
- private buildSpanPayload(span: AnyAISpan, isCreate: boolean): Record<string, any> {
144
- const payload: Record<string, any> = {};
145
-
146
- if (isCreate) {
147
- payload.id = span.id;
148
- payload.name = span.name;
149
- payload.startTime = span.startTime;
150
- if (span.input !== undefined) payload.input = span.input;
151
- }
152
-
153
- if (span.output !== undefined) payload.output = span.output;
154
- if (span.endTime !== undefined) payload.endTime = span.endTime;
155
-
156
- const attributes = (span.attributes ?? {}) as Record<string, any>;
157
-
158
- // Strip special fields from metadata if used in top-level keys
159
- const attributesToOmit: string[] = [];
160
-
161
- if (span.type === AISpanType.LLM_GENERATION) {
162
- const llmAttr = attributes as LLMGenerationAttributes;
163
-
164
- if (llmAttr.model !== undefined) {
165
- payload.model = llmAttr.model;
166
- attributesToOmit.push('model');
167
- }
168
-
169
- if (llmAttr.usage !== undefined) {
170
- payload.usage = llmAttr.usage;
171
- attributesToOmit.push('usage');
172
- }
173
-
174
- if (llmAttr.parameters !== undefined) {
175
- payload.modelParameters = llmAttr.parameters;
176
- attributesToOmit.push('parameters');
177
- }
178
- }
179
-
180
- payload.metadata = {
181
- spanType: span.type,
182
- ...sanitizeMetadata(omitKeys(attributes, attributesToOmit)),
183
- ...sanitizeMetadata(span.metadata),
184
- };
185
-
186
- if (span.errorInfo) {
187
- payload.level = 'ERROR';
188
- payload.statusMessage = span.errorInfo.message;
189
- }
190
-
191
- return payload;
192
- }
193
-
194
- async shutdown(): Promise<void> {
195
- await this.client.shutdownAsync();
196
- this.traceMap.clear();
197
- }
198
- }
package/src/index.ts DELETED
@@ -1,9 +0,0 @@
1
- /**
2
- * Langfuse Observability Provider for Mastra
3
- *
4
- * This package provides Langfuse-specific observability features for Mastra applications.
5
- * Currently includes AI tracing support with plans for additional observability features.
6
- */
7
-
8
- // AI Tracing
9
- export * from './ai-tracing';
@@ -1,9 +0,0 @@
1
- {
2
- "extends": ["./tsconfig.json", "../../tsconfig.build.json"],
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src"
6
- },
7
- "include": ["src/**/*"],
8
- "exclude": ["node_modules", "**/*.test.ts", "src/**/*.mock.ts"]
9
- }
package/tsconfig.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.node.json",
3
- "include": ["src/**/*", "tsup.config.ts"],
4
- "exclude": ["node_modules", "**/*.test.ts"]
5
- }
package/tsup.config.ts DELETED
@@ -1,17 +0,0 @@
1
- import { generateTypes } from '@internal/types-builder';
2
- import { defineConfig } from 'tsup';
3
-
4
- export default defineConfig({
5
- entry: ['src/index.ts'],
6
- format: ['esm', 'cjs'],
7
- clean: true,
8
- dts: false,
9
- splitting: true,
10
- treeshake: {
11
- preset: 'smallest',
12
- },
13
- sourcemap: true,
14
- onSuccess: async () => {
15
- await generateTypes(process.cwd());
16
- },
17
- });
package/vitest.config.ts DELETED
@@ -1,11 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
-
3
- export default defineConfig({
4
- test: {
5
- environment: 'node',
6
- include: ['src/**/*.test.ts'],
7
- coverage: {
8
- reporter: ['text', 'json', 'html'],
9
- },
10
- },
11
- });