neatlogs 1.0.4 → 1.0.6

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.
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/mastra-wrap.ts"],"sourcesContent":["/**\n * Neatlogs Mastra wrapper — full nested tracing via method-wrapping.\n *\n * Usage:\n * import { wrapMastra } from 'neatlogs/mastra';\n * import { Agent } from '@mastra/core/agent';\n * const agent = wrapMastra(new Agent({ ... }));\n * const result = await agent.generate('Hello');\n *\n * Philosophy (same as wrapAISDK): we own the capturing layer. Instead of\n * depending on Mastra's internal observability bus or @mastra/observability,\n * we wrap Mastra's own methods and emit OpenTelemetry spans ourselves. Every\n * parent span is opened as the ACTIVE span, so child operations (LLM calls,\n * tool executions, workflow steps) nest automatically.\n *\n * Coverage (duck-typed by the methods present on the entity):\n * - Agent.generate()/stream() → AGENT (parent)\n * ↳ resolved model doGenerate/doStream → LLM (child, per model step)\n * ↳ each tool's execute() → TOOL (child, per tool call)\n * - Workflow.createRun().start()/resume() → WORKFLOW\n * - MastraVector.query() → RETRIEVER\n * - MastraVector.upsert/update/delete() → VECTOR_STORE\n * - MastraMemory.recall/saveMessages/... → CHAIN (memory ops)\n * - MDocument.chunk() → CHAIN\n * - rerank() (standalone fn) → RERANKER\n * - root Mastra (getAgent + getWorkflow) → proxy that wraps what it returns\n */\n\nimport { trace, context as otelContext, SpanStatusCode, type Span } from '@opentelemetry/api';\n\nconst TRACER_NAME = 'neatlogs.mastra';\nconst PATCH_FLAG = '_neatlogs_patched';\n\n// ---------------------------------------------------------------------------\n// Public API\n// ---------------------------------------------------------------------------\n\nexport function wrapMastra<T extends object>(entity: T): T {\n if (!entity || (entity as any)[PATCH_FLAG]) return entity;\n\n const e = entity as any;\n const className = entity.constructor?.name ?? '';\n\n // Root Mastra: wrap the agents/workflows it hands out (it itself has neither\n // generate nor createRun). Detect by the getter pair.\n if (isRootMastra(e)) {\n const proxied = wrapRootMastra(e);\n markPatched(proxied);\n return proxied as T;\n }\n\n if (isAgent(e, className)) {\n patchAgent(e);\n } else if (isWorkflow(e, className)) {\n patchWorkflow(e);\n } else if (isVector(e, className)) {\n patchVector(e);\n } else if (isMemory(e, className)) {\n patchMemory(e);\n } else if (isDocument(e, className)) {\n patchDocument(e);\n }\n\n markPatched(e);\n return entity;\n}\n\n/**\n * Wrap a standalone `rerank()` function (from `@mastra/rag`) so each call emits\n * a RERANKER span. Returns a wrapped function; the original is unchanged.\n *\n * import { rerank } from '@mastra/rag';\n * const tracedRerank = wrapMastraRerank(rerank);\n */\nexport function wrapMastraRerank<F extends (...args: any[]) => any>(rerankFn: F): F {\n return (async function tracedRerank(...args: any[]) {\n return withSpan('mastra.rerank', { 'neatlogs.span.kind': 'RERANKER' }, (span) => {\n // rerank(results, query, model, options)\n const results = args?.[0];\n const query = args?.[1];\n const options = args?.[3];\n if (typeof query === 'string') {\n span.setAttribute('neatlogs.reranker.query', query.slice(0, 10000));\n span.setAttribute('input.value', query.slice(0, 10000));\n }\n if (Array.isArray(results)) span.setAttribute('neatlogs.reranker.input_documents', safeStringify(results).slice(0, 10000));\n if (options?.topK != null) span.setAttribute('neatlogs.reranker.top_k', options.topK);\n return Promise.resolve(rerankFn(...args)).then((result) => {\n span.setAttribute('neatlogs.reranker.output_documents', safeStringify(result).slice(0, 10000));\n span.setAttribute('output.value', safeStringify(result).slice(0, 10000));\n return result;\n });\n });\n }) as unknown as F;\n}\n\n// ---------------------------------------------------------------------------\n// Detection (duck-typed — robust across Mastra versions / build shapes)\n// ---------------------------------------------------------------------------\n\nfunction isRootMastra(e: any): boolean {\n return typeof e.getAgent === 'function' && typeof e.getWorkflow === 'function';\n}\n\nfunction isAgent(e: any, className: string): boolean {\n // An agent exposes generate()/stream(). getLLM()/listTools() are wrapped\n // opportunistically when present (installAgentLlmHook/installAgentToolHooks\n // no-op otherwise), so they are not required for detection.\n return className === 'Agent' || typeof e.generate === 'function' || typeof e.stream === 'function';\n}\n\nfunction isWorkflow(e: any, className: string): boolean {\n return className === 'Workflow' || typeof e.createRun === 'function';\n}\n\nfunction isVector(e: any, className: string): boolean {\n return /Vector/.test(className) || (typeof e.query === 'function' && typeof e.upsert === 'function');\n}\n\nfunction isMemory(e: any, className: string): boolean {\n return /Memory/.test(className) || (typeof e.recall === 'function' && typeof e.saveMessages === 'function');\n}\n\nfunction isDocument(e: any, className: string): boolean {\n return className === 'MDocument' || (typeof e.chunk === 'function' && typeof e.getDocs === 'function');\n}\n\n// ---------------------------------------------------------------------------\n// Agent\n// ---------------------------------------------------------------------------\n\nfunction patchAgent(agent: any): void {\n patchAgentMethod(agent, 'generate', false);\n patchAgentMethod(agent, 'stream', true);\n}\n\n/**\n * Wrap generate()/stream() with an AGENT parent span. Before delegating, also\n * install child-span hooks on this agent's resolved model (LLM) and tools\n * (TOOL) so they nest under the AGENT span. Hooks are installed lazily/once.\n */\nfunction patchAgentMethod(agent: any, method: 'generate' | 'stream', streaming: boolean): void {\n if (typeof agent[method] !== 'function') return;\n const orig = agent[method].bind(agent);\n\n agent[method] = async function tracedAgentMethod(input: any, opts?: any): Promise<any> {\n const agentName = agent.name ?? agent.id ?? 'mastra_agent';\n const model = extractModelId(agent.model);\n\n // Ensure LLM + TOOL child hooks are present (idempotent).\n installAgentLlmHook(agent);\n installAgentToolHooks(agent);\n\n const attrs: Record<string, any> = {\n 'neatlogs.span.kind': 'AGENT',\n 'neatlogs.agent.name': agentName,\n 'input.value': toInputValue(input),\n };\n if (model) attrs['neatlogs.llm.model_name'] = model;\n if (agent.instructions && typeof agent.instructions === 'string') {\n attrs['neatlogs.llm.system_prompt'] = agent.instructions.slice(0, 5000);\n }\n if (streaming) attrs['neatlogs.llm.is_streaming'] = true;\n\n if (streaming) {\n // stream() returns immediately; the model only runs when the caller drains\n // the stream. Keep the AGENT span open + active across the whole stream so\n // doStream (LLM) children nest, and finalize when the output completes.\n const tracer = trace.getTracer(TRACER_NAME);\n const span = tracer.startSpan(`mastra.agent.${agentName}`, { attributes: attrs }, otelContext.active());\n const ctx = trace.setSpan(otelContext.active(), span);\n try {\n const result = await otelContext.with(ctx, () => orig(input, opts));\n return wrapStreamingOutput(result, span, ctx);\n } catch (err) {\n recordError(span, err);\n span.end();\n throw err;\n }\n }\n\n return withActiveSpan(`mastra.agent.${agentName}`, attrs, async (span) => {\n const result = await orig(input, opts);\n finalizeAgentResult(span, result);\n return result;\n });\n };\n}\n\n/**\n * Patch agent.getLLM so the model it returns has doGenerate/doStream wrapped\n * (emitting LLM child spans). Mastra calls this.getLLM() inside generate/stream,\n * and the resolved model is the single chokepoint for the real provider call.\n */\nfunction installAgentLlmHook(agent: any): void {\n if (agent.__neatlogs_llm_hook || typeof agent.getLLM !== 'function') return;\n agent.__neatlogs_llm_hook = true;\n\n const origGetLLM = agent.getLLM.bind(agent);\n agent.getLLM = function patchedGetLLM(...args: any[]): any {\n const out = origGetLLM(...args);\n return Promise.resolve(out).then((llm: any) => {\n try {\n const model = typeof llm?.getModel === 'function' ? llm.getModel() : llm;\n patchModelInPlace(model);\n } catch {\n /* best-effort */\n }\n return llm;\n });\n };\n}\n\n/** Wrap a resolved language model's doGenerate/doStream in place (idempotent). */\nfunction patchModelInPlace(model: any): void {\n if (!model || model.__neatlogs_model_patched) return;\n model.__neatlogs_model_patched = true;\n const modelId = model.modelId ?? model.modelName ?? '';\n const provider = model.provider ?? '';\n\n for (const fn of ['doGenerate', 'doStream'] as const) {\n if (typeof model[fn] !== 'function') continue;\n const orig = model[fn].bind(model);\n const isStream = fn === 'doStream';\n\n model[fn] = function tracedModelCall(callOpts: any): any {\n const attrs: Record<string, any> = { 'neatlogs.span.kind': 'LLM' };\n if (modelId) attrs['neatlogs.llm.model_name'] = modelId;\n if (provider) attrs['neatlogs.llm.provider'] = provider;\n if (isStream) attrs['neatlogs.llm.is_streaming'] = true;\n const promptInput = callOpts?.prompt ?? callOpts?.messages;\n if (promptInput !== undefined) attrs['input.value'] = safeStringify(promptInput).slice(0, 10000);\n captureInvocationParams(attrs, callOpts);\n\n return withActiveSpan(`mastra.llm.${modelId || 'model'}.${fn}`, attrs, async (span) => {\n const result = await orig(callOpts);\n if (!isStream) finalizeModelResult(span, result);\n // Streaming doGenerate-style results serialize lazily; record what we can.\n else if (result?.usage) recordUsage(span, result.usage);\n return result;\n });\n };\n }\n}\n\n/** Patch each tool's execute() so tool calls become TOOL child spans. */\nfunction installAgentToolHooks(agent: any): void {\n if (agent.__neatlogs_tool_hook) return;\n let tools: Record<string, any> | undefined;\n try {\n tools = typeof agent.listTools === 'function' ? agent.listTools() : undefined;\n } catch {\n tools = undefined;\n }\n if (!tools || typeof tools !== 'object') return;\n agent.__neatlogs_tool_hook = true;\n\n for (const [key, tool] of Object.entries<any>(tools)) {\n patchToolExecute(tool, key);\n }\n}\n\nfunction patchToolExecute(tool: any, key: string): void {\n if (!tool || typeof tool.execute !== 'function' || tool.__neatlogs_tool_patched) return;\n tool.__neatlogs_tool_patched = true;\n const toolName = tool.id ?? key;\n const orig = tool.execute.bind(tool);\n\n tool.execute = function tracedToolExecute(params: any, options?: any): any {\n const attrs: Record<string, any> = {\n 'neatlogs.span.kind': 'TOOL',\n 'neatlogs.tool.name': toolName,\n 'input.value': safeStringify(params).slice(0, 10000),\n };\n if (tool.description) attrs['neatlogs.tool.description'] = String(tool.description).slice(0, 2000);\n\n return withActiveSpan(`mastra.tool.${toolName}`, attrs, async (span) => {\n const result = await orig(params, options);\n span.setAttribute('output.value', safeStringify(result).slice(0, 10000));\n return result;\n });\n };\n}\n\n// ---------------------------------------------------------------------------\n// Workflow\n// ---------------------------------------------------------------------------\n\nfunction patchWorkflow(workflow: any): void {\n if (typeof workflow.createRun !== 'function') return;\n const origCreateRun = workflow.createRun.bind(workflow);\n const workflowName = workflow.name ?? workflow.id ?? 'mastra_workflow';\n\n workflow.createRun = async function tracedCreateRun(...args: any[]): Promise<any> {\n const run = await origCreateRun(...args);\n if (!run) return run;\n patchRunMethod(run, 'start', workflowName);\n patchRunMethod(run, 'resume', workflowName);\n return run;\n };\n}\n\nfunction patchRunMethod(run: any, method: 'start' | 'resume', workflowName: string): void {\n if (typeof run[method] !== 'function') return;\n const orig = run[method].bind(run);\n\n run[method] = async function tracedRunMethod(startOpts: any): Promise<any> {\n const attrs: Record<string, any> = {\n 'neatlogs.span.kind': 'WORKFLOW',\n 'neatlogs.workflow.name': workflowName,\n };\n if (startOpts?.inputData !== undefined) {\n attrs['input.value'] = safeStringify(startOpts.inputData).slice(0, 10000);\n }\n\n return withActiveSpan(`mastra.workflow.${workflowName}`, attrs, async (span) => {\n const result = await orig(startOpts);\n if (result?.status) span.setAttribute('neatlogs.metadata', safeStringify({ status: result.status }));\n if (result?.result !== undefined) {\n span.setAttribute('output.value', safeStringify(result.result).slice(0, 10000));\n }\n return result;\n });\n };\n}\n\n// ---------------------------------------------------------------------------\n// Vector store\n// ---------------------------------------------------------------------------\n\nconst VECTOR_READ_OPS = ['query'];\nconst VECTOR_WRITE_OPS = ['upsert', 'updateVector', 'deleteVector', 'createIndex', 'deleteIndex'];\n\nfunction patchVector(vector: any): void {\n for (const op of VECTOR_READ_OPS) patchVectorOp(vector, op, 'RETRIEVER');\n for (const op of VECTOR_WRITE_OPS) patchVectorOp(vector, op, 'VECTOR_STORE');\n}\n\nfunction patchVectorOp(vector: any, op: string, kind: 'RETRIEVER' | 'VECTOR_STORE'): void {\n if (typeof vector[op] !== 'function') return;\n const orig = vector[op].bind(vector);\n const dbName = vector.constructor?.name ?? 'vector';\n\n vector[op] = async function tracedVectorOp(params: any): Promise<any> {\n const attrs: Record<string, any> = {\n 'neatlogs.span.kind': kind,\n 'neatlogs.db.system': dbName,\n 'neatlogs.db.operation': op,\n 'input.value': safeStringify(params).slice(0, 10000),\n };\n const indexName = params?.indexName;\n if (indexName) attrs['neatlogs.vectordb.index_name'] = String(indexName);\n if (kind === 'RETRIEVER' && params?.topK != null) attrs['neatlogs.retriever.top_k'] = params.topK;\n return withActiveSpan(`mastra.vector.${op}`, attrs, async (span) => {\n const result = await orig(params);\n span.setAttribute('output.value', safeStringify(result).slice(0, 10000));\n return result;\n });\n };\n}\n\n// ---------------------------------------------------------------------------\n// Memory\n// ---------------------------------------------------------------------------\n\nconst MEMORY_OPS = ['recall', 'saveMessages', 'updateWorkingMemory', 'deleteMessages'];\n\nfunction patchMemory(memory: any): void {\n for (const op of MEMORY_OPS) {\n if (typeof memory[op] !== 'function') continue;\n const orig = memory[op].bind(memory);\n memory[op] = async function tracedMemoryOp(...args: any[]): Promise<any> {\n const attrs: Record<string, any> = {\n 'neatlogs.span.kind': 'CHAIN',\n 'neatlogs.db.operation': op,\n 'input.value': safeStringify(args?.[0]).slice(0, 10000),\n };\n return withActiveSpan(`mastra.memory.${op}`, attrs, async (span) => {\n const result = await orig(...args);\n span.setAttribute('output.value', safeStringify(result).slice(0, 8000));\n return result;\n });\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// MDocument\n// ---------------------------------------------------------------------------\n\nfunction patchDocument(doc: any): void {\n if (typeof doc.chunk !== 'function') return;\n const orig = doc.chunk.bind(doc);\n doc.chunk = async function tracedChunk(...args: any[]): Promise<any> {\n const attrs: Record<string, any> = {\n 'neatlogs.span.kind': 'CHAIN',\n 'neatlogs.db.operation': 'chunk',\n };\n return withActiveSpan('mastra.document.chunk', attrs, async (span) => {\n const result = await orig(...args);\n if (Array.isArray(result)) span.setAttribute('neatlogs.db.documents_count', result.length);\n return result;\n });\n };\n}\n\n// ---------------------------------------------------------------------------\n// Root Mastra proxy\n// ---------------------------------------------------------------------------\n\nfunction wrapRootMastra(mastra: any): any {\n const AGENT_GETTERS = new Set(['getAgent', 'getAgentById']);\n const WORKFLOW_GETTERS = new Set(['getWorkflow', 'getWorkflowById']);\n\n return new Proxy(mastra, {\n get(target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver);\n if (typeof value !== 'function') return value;\n const name = String(prop);\n\n if (AGENT_GETTERS.has(name) || WORKFLOW_GETTERS.has(name)) {\n return (...args: any[]) => {\n const entity = value.apply(target, args);\n return entity && typeof entity === 'object' ? wrapMastra(entity) : entity;\n };\n }\n return value.bind(target);\n },\n });\n}\n\n// ---------------------------------------------------------------------------\n// Streaming output wrapping\n// ---------------------------------------------------------------------------\n\n/**\n * Finalize the AGENT span for a streaming result.\n *\n * Mastra's `stream()` returns a `MastraModelOutput` (NOT a plain async-iterable):\n * the model only runs when the caller drains `.textStream`/`.fullStream` or\n * awaits `.text`. We therefore:\n * 1. Re-establish the AGENT span's context around stream consumption so the\n * model's `doStream` (LLM) child nests under it.\n * 2. Tap the `.text`/`.usage`/`.finishReason` promises — which resolve when the\n * stream completes regardless of how the caller consumes — to record output\n * and end the span exactly once.\n * Falls back to legacy async-iterable / thenable shapes for older Mastra.\n */\nfunction wrapStreamingOutput(output: any, span: Span, ctx: ReturnType<typeof otelContext.active>): any {\n if (!output) {\n span.setStatus({ code: SpanStatusCode.OK });\n span.end();\n return output;\n }\n\n let ended = false;\n const endOnce = (err?: unknown) => {\n if (ended) return;\n ended = true;\n if (err) recordError(span, err);\n else span.setStatus({ code: SpanStatusCode.OK });\n span.end();\n };\n\n // MastraModelOutput: has awaitable .text (thenable). Tap completion promises.\n if (output.text && typeof output.text.then === 'function') {\n const finalize = async () => {\n try {\n const [text, usage, finishReason] = await Promise.all([\n Promise.resolve(output.text).catch(() => undefined),\n Promise.resolve(output.usage).catch(() => undefined),\n Promise.resolve(output.finishReason).catch(() => undefined),\n ]);\n if (typeof text === 'string' && text) {\n span.setAttribute('neatlogs.llm.output_messages.0.role', 'assistant');\n span.setAttribute('neatlogs.llm.output_messages.0.content', text.slice(0, 10000));\n }\n if (usage) recordUsage(span, usage);\n if (finishReason) span.setAttribute('neatlogs.llm.stop_reason', normalizeFinishReason(finishReason));\n endOnce();\n } catch (err) {\n endOnce(err);\n }\n };\n // Drive finalization without requiring the caller to consume; the promises\n // resolve when Mastra finishes the stream internally.\n void finalize();\n\n // Re-establish span context around stream getters so doStream children nest.\n return rebindStreamContext(output, ctx);\n }\n\n // Legacy async-iterable\n if (output[Symbol.asyncIterator]) {\n const origIterator = output[Symbol.asyncIterator].bind(output);\n const textParts: string[] = [];\n const wrapped = Object.create(Object.getPrototypeOf(output));\n Object.assign(wrapped, output);\n wrapped[Symbol.asyncIterator] = function () {\n const iterator = origIterator();\n const finish = () => {\n if (textParts.length) {\n span.setAttribute('neatlogs.llm.output_messages.0.role', 'assistant');\n span.setAttribute('neatlogs.llm.output_messages.0.content', textParts.join('').slice(0, 10000));\n }\n endOnce();\n };\n return {\n async next(): Promise<IteratorResult<any>> {\n try {\n const r = await iterator.next();\n if (r.done) { finish(); return r; }\n const chunk = r.value;\n if (typeof chunk === 'string') textParts.push(chunk);\n else if (chunk?.text) textParts.push(chunk.text);\n else if (chunk?.delta) textParts.push(chunk.delta);\n return r;\n } catch (err) { endOnce(err); throw err; }\n },\n async return(value?: any): Promise<IteratorResult<any>> {\n finish();\n return iterator.return?.(value) ?? { done: true, value: undefined };\n },\n async throw(err?: any): Promise<IteratorResult<any>> {\n endOnce(err);\n return iterator.throw?.(err) ?? { done: true, value: undefined };\n },\n };\n };\n return wrapped;\n }\n\n // Thenable (awaited result)\n if (typeof output.then === 'function') {\n return output\n .then((resolved: any) => { finalizeAgentResult(span, resolved); endOnce(); return resolved; })\n .catch((err: any) => { endOnce(err); throw err; });\n }\n\n endOnce();\n return output;\n}\n\n/**\n * Wrap the async-iterable getters on a MastraModelOutput so that draining the\n * stream runs inside the AGENT span's context (lets the model's doStream child\n * span attach to the right parent). Returns a proxy; non-stream props pass through.\n */\nfunction rebindStreamContext(output: any, ctx: ReturnType<typeof otelContext.active>): any {\n const STREAM_PROPS = new Set(['textStream', 'fullStream', 'objectStream', 'elementStream']);\n return new Proxy(output, {\n get(target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver);\n if (typeof prop === 'string' && STREAM_PROPS.has(prop) && value && value[Symbol.asyncIterator]) {\n const origIterator = value[Symbol.asyncIterator].bind(value);\n return {\n [Symbol.asyncIterator]() {\n return otelContext.with(ctx, () => origIterator());\n },\n };\n }\n if (typeof value === 'function') return value.bind(target);\n return value;\n },\n });\n}\n\n// ---------------------------------------------------------------------------\n// Result finalization\n// ---------------------------------------------------------------------------\n\nfunction finalizeAgentResult(span: Span, result: any): void {\n if (!result) return;\n\n if (result.text) {\n span.setAttribute('neatlogs.llm.output_messages.0.role', 'assistant');\n span.setAttribute('neatlogs.llm.output_messages.0.content', String(result.text).slice(0, 10000));\n }\n\n if (Array.isArray(result.toolCalls)) {\n for (let i = 0; i < result.toolCalls.length; i++) {\n const tc = result.toolCalls[i];\n const p = tc.payload ?? tc;\n setToolCall(span, i, p.toolName ?? p.name, p.args ?? p.arguments, p.toolCallId ?? p.id);\n }\n }\n\n if (result.usage) recordUsage(span, result.usage);\n if (result.finishReason) span.setAttribute('neatlogs.llm.stop_reason', normalizeFinishReason(result.finishReason));\n if (result.model) span.setAttribute('neatlogs.llm.model_name', result.model);\n}\n\nfunction finalizeModelResult(span: Span, result: any): void {\n if (!result) return;\n // AI SDK v5 doGenerate result: { content[], finishReason, usage, ... }.\n // `content` is an array of typed parts (text / tool-call); `text` may be absent.\n if (typeof result.text === 'string' && result.text) {\n span.setAttribute('neatlogs.llm.output_messages.0.role', 'assistant');\n span.setAttribute('neatlogs.llm.output_messages.0.content', result.text.slice(0, 10000));\n } else if (Array.isArray(result.content)) {\n const text = result.content\n .filter((p: any) => p?.type === 'text' && typeof p.text === 'string')\n .map((p: any) => p.text)\n .join('');\n if (text) {\n span.setAttribute('neatlogs.llm.output_messages.0.role', 'assistant');\n span.setAttribute('neatlogs.llm.output_messages.0.content', text.slice(0, 10000));\n }\n // Tool-call parts → indexed tool_calls\n const toolCalls = result.content.filter((p: any) => p?.type === 'tool-call');\n for (let i = 0; i < toolCalls.length; i++) {\n const tc = toolCalls[i];\n setToolCall(span, i, tc.toolName, tc.input ?? tc.args, tc.toolCallId);\n }\n span.setAttribute('output.value', safeStringify(result.content).slice(0, 10000));\n }\n if (result.usage) recordUsage(span, result.usage);\n span.setAttribute('neatlogs.llm.stop_reason', normalizeFinishReason(result.finishReason));\n}\n\nfunction normalizeFinishReason(fr: any): string {\n if (fr == null) return '';\n if (typeof fr === 'string') return fr;\n // AI SDK v5 raw model returns { unified: 'tool-calls', ... }\n return String(fr.unified ?? fr.reason ?? fr.type ?? safeStringify(fr));\n}\n\n/** Read a possibly-nested token count: 42, or { total: 42 }. */\nfunction tokenValue(v: any): number | undefined {\n if (v == null) return undefined;\n if (typeof v === 'number') return v;\n if (typeof v === 'object' && typeof v.total === 'number') return v.total;\n const n = Number(v);\n return Number.isFinite(n) ? n : undefined;\n}\n\n/**\n * Capture LLM sampling/invocation parameters from the model call options onto\n * the span, using only canonical neatlogs.* keys. Present-only — absent params\n * are skipped. Mirrors AI SDK LanguageModelV2CallOptions field names.\n */\nfunction captureInvocationParams(attrs: Record<string, any>, callOpts: any): void {\n if (!callOpts) return;\n const map: Array<[string, string]> = [\n ['temperature', 'neatlogs.llm.temperature'],\n ['maxOutputTokens', 'neatlogs.llm.max_tokens'],\n ['maxTokens', 'neatlogs.llm.max_tokens'],\n ['topP', 'neatlogs.llm.top_p'],\n ['topK', 'neatlogs.llm.top_k'],\n ['frequencyPenalty', 'neatlogs.llm.frequency_penalty'],\n ['presencePenalty', 'neatlogs.llm.presence_penalty'],\n ];\n const invocation: Record<string, any> = {};\n for (const [src, target] of map) {\n const v = callOpts[src];\n if (v != null) {\n attrs[target] = v;\n invocation[src] = v;\n }\n }\n if (Array.isArray(callOpts.stopSequences) && callOpts.stopSequences.length) {\n attrs['neatlogs.llm.stop_sequences'] = safeStringify(callOpts.stopSequences);\n invocation.stopSequences = callOpts.stopSequences;\n }\n // Tool definitions advertised to the model → neatlogs.llm.tools.{i}\n if (Array.isArray(callOpts.tools)) {\n for (let i = 0; i < callOpts.tools.length; i++) {\n attrs[`neatlogs.llm.tools.${i}`] = safeStringify(callOpts.tools[i]).slice(0, 4000);\n }\n invocation.toolChoice = callOpts.toolChoice;\n }\n if (Object.keys(invocation).length) {\n attrs['neatlogs.llm.invocation_parameters'] = safeStringify(invocation).slice(0, 4000);\n }\n}\n\n/**\n * Emit an indexed tool call directly in the neatlogs target namespace\n * `neatlogs.llm.tool_calls.{i}.{id,name,arguments}` — matching how the validated\n * openai/anthropic wrappers (src/openai.ts) emit them. The backend's\n * tool_calls.{i} target is an indexed object with these sub-fields.\n */\nfunction setToolCall(span: Span, i: number, name: any, args: any, id: any): void {\n span.setAttribute(`neatlogs.llm.tool_calls.${i}.name`, name ?? '');\n span.setAttribute(`neatlogs.llm.tool_calls.${i}.arguments`, safeStringify(args ?? {}));\n if (id) span.setAttribute(`neatlogs.llm.tool_calls.${i}.id`, id);\n}\n\nfunction recordUsage(span: Span, usage: any): void {\n if (!usage) return;\n // Shapes seen:\n // - AISDK v5 raw model: { inputTokens: {total}, outputTokens: {total} }\n // - Mastra result: { inputTokens, outputTokens, totalTokens }\n // - AISDK v3: { promptTokens, completionTokens, totalTokens }\n const prompt = tokenValue(usage.promptTokens ?? usage.inputTokens ?? usage.input_tokens);\n const completion = tokenValue(usage.completionTokens ?? usage.outputTokens ?? usage.output_tokens);\n const total = tokenValue(usage.totalTokens);\n if (prompt != null) span.setAttribute('neatlogs.llm.token_count.prompt', prompt);\n if (completion != null) span.setAttribute('neatlogs.llm.token_count.completion', completion);\n if (total != null) span.setAttribute('neatlogs.llm.token_count.total', total);\n else if (prompt != null && completion != null) {\n span.setAttribute('neatlogs.llm.token_count.total', prompt + completion);\n }\n}\n\n// ---------------------------------------------------------------------------\n// Span helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Open a span as the ACTIVE span for the duration of `fn`, so any spans created\n * inside `fn` (model calls, tool executions, workflow steps) nest under it.\n */\nfunction withActiveSpan<T>(name: string, attrs: Record<string, any>, fn: (span: Span) => Promise<T>): Promise<T> {\n const tracer = trace.getTracer(TRACER_NAME);\n const span = tracer.startSpan(name, { attributes: attrs }, otelContext.active());\n const ctx = trace.setSpan(otelContext.active(), span);\n return otelContext.with(ctx, async () => {\n try {\n const result = await fn(span);\n span.setStatus({ code: SpanStatusCode.OK });\n return result;\n } catch (err) {\n recordError(span, err);\n throw err;\n } finally {\n // Stream wrappers end the span themselves once the stream drains; guard\n // against double-end via the OTel SDK (ending twice is a no-op there).\n span.end();\n }\n });\n}\n\n/** Like withActiveSpan but for non-async-context-sensitive leaf operations. */\nfunction withSpan<T>(name: string, attrs: Record<string, any>, fn: (span: Span) => Promise<T> | T): Promise<T> {\n const tracer = trace.getTracer(TRACER_NAME);\n const span = tracer.startSpan(name, { attributes: attrs }, otelContext.active());\n return Promise.resolve()\n .then(() => fn(span))\n .then((result) => {\n span.setStatus({ code: SpanStatusCode.OK });\n span.end();\n return result;\n })\n .catch((err) => {\n recordError(span, err);\n span.end();\n throw err;\n });\n}\n\n// ---------------------------------------------------------------------------\n// Utilities\n// ---------------------------------------------------------------------------\n\nfunction markPatched(e: any): void {\n try {\n Object.defineProperty(e, PATCH_FLAG, { value: true, enumerable: false, configurable: true });\n } catch {\n e[PATCH_FLAG] = true;\n }\n}\n\nfunction extractModelId(model: any): string {\n if (!model) return '';\n if (typeof model === 'string') return model;\n return model.modelId ?? model.name ?? '';\n}\n\nfunction toInputValue(input: any): string {\n return (typeof input === 'string' ? input : safeStringify(input)).slice(0, 10000);\n}\n\nfunction safeStringify(value: unknown): string {\n if (typeof value === 'string') return value;\n try {\n return JSON.stringify(value) ?? '';\n } catch {\n return '';\n }\n}\n\nfunction recordError(span: Span, err: unknown): void {\n if (err instanceof Error) {\n span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });\n span.recordException(err);\n } else {\n span.setStatus({ code: SpanStatusCode.ERROR, message: String(err) });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA4BA,iBAAyE;AAEzE,IAAM,cAAc;AACpB,IAAM,aAAa;AAMZ,SAAS,WAA6B,QAAc;AACzD,MAAI,CAAC,UAAW,OAAe,UAAU,EAAG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AQ;AACN,MAAE,UAAU,IAAI;AAAA,EAClB;AACF;AAEA,SAAS,eAAe,OAAoB;AAC1C,MAAI,CAAC,MAAO,QAAO;AACnB,MAAI,OAAO,UAAU,SAAU,QAAO;AACtC,SAAO,MAAM,WAAW,MAAM,QAAQ;AACxC;AAEA,SAAS,aAAa,OAAoB;AACxC,UAAQ,OAAO,UAAU,WAAW,QAAQ,cAAc,KAAK,GAAG,MAAM,GAAG,GAAK;AAClF;AAEA,SAAS,cAAc,OAAwB;AAC7C,MAAI,OAAO,UAAU,SAAU,QAAO;AACtC,MAAI;AACF,WAAO,KAAK,UAAU,KAAK,KAAK;AAAA,EAClC,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,SAAS,YAAY,MAAY,KAAoB;AACnD,MAAI,eAAe,OAAO;AACxB,SAAK,UAAU,EAAE,MAAM,0BAAe,OAAO,SAAS,IAAI,QAAQ,CAAC;AACnE,SAAK,gBAAgB,GAAG;AAAA,EAC1B,OAAO;AACL,SAAK,UAAU,EAAE,MAAM,0BAAe,OAAO,SAAS,OAAO,GAAG,EAAE,CAAC;AAAA,EACrE;AACF;","names":["otelContext"]}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Neatlogs Mastra wrapper — full nested tracing via method-wrapping.
3
+ *
4
+ * Usage:
5
+ * import { wrapMastra } from 'neatlogs/mastra';
6
+ * import { Agent } from '@mastra/core/agent';
7
+ * const agent = wrapMastra(new Agent({ ... }));
8
+ * const result = await agent.generate('Hello');
9
+ *
10
+ * Philosophy (same as wrapAISDK): we own the capturing layer. Instead of
11
+ * depending on Mastra's internal observability bus or @mastra/observability,
12
+ * we wrap Mastra's own methods and emit OpenTelemetry spans ourselves. Every
13
+ * parent span is opened as the ACTIVE span, so child operations (LLM calls,
14
+ * tool executions, workflow steps) nest automatically.
15
+ *
16
+ * Coverage (duck-typed by the methods present on the entity):
17
+ * - Agent.generate()/stream() → AGENT (parent)
18
+ * ↳ resolved model doGenerate/doStream → LLM (child, per model step)
19
+ * ↳ each tool's execute() → TOOL (child, per tool call)
20
+ * - Workflow.createRun().start()/resume() → WORKFLOW
21
+ * - MastraVector.query() → RETRIEVER
22
+ * - MastraVector.upsert/update/delete() → VECTOR_STORE
23
+ * - MastraMemory.recall/saveMessages/... → CHAIN (memory ops)
24
+ * - MDocument.chunk() → CHAIN
25
+ * - rerank() (standalone fn) → RERANKER
26
+ * - root Mastra (getAgent + getWorkflow) → proxy that wraps what it returns
27
+ */
28
+ declare function wrapMastra<T extends object>(entity: T): T;
29
+ /**
30
+ * Wrap a standalone `rerank()` function (from `@mastra/rag`) so each call emits
31
+ * a RERANKER span. Returns a wrapped function; the original is unchanged.
32
+ *
33
+ * import { rerank } from '@mastra/rag';
34
+ * const tracedRerank = wrapMastraRerank(rerank);
35
+ */
36
+ declare function wrapMastraRerank<F extends (...args: any[]) => any>(rerankFn: F): F;
37
+
38
+ export { wrapMastra, wrapMastraRerank };