/** * Morph Tracing — public types. * * Morph Tracing instruments the top AI SDKs (OpenAI, Anthropic, Vercel AI SDK, * Bedrock, Vertex, Cohere, Together, LangChain, LlamaIndex, …) and ships * OpenTelemetry spans to Morph. It is a thin, Morph-branded layer over * OpenLLMetry / Traceloop, exactly like `raindrop-ai` is. */ /** * Modules to explicitly instrument. Pass the imported SDK module/constructor so * Traceloop patches the exact instance your app uses. If omitted, all supported * libraries are auto-instrumented. */ /** * Reflexes (evals) to run automatically on a trace. You pick which role each model classifies: * `user` (the user's message) and/or `assistant` (the agent's output). Always explicit — there is * no implicit default, since a model run on the wrong role gives a meaningless label. * * @example evals: { user: ["jailbreak", "guardrail"] } * @example evals: { user: ["jailbreak"], assistant: ["leaked-thinking"] } */ interface EvalSelection { /** Reflexes to run on the user's message, e.g. ["jailbreak", "guardrail"]. */ user?: string[]; /** Reflexes to run on the agent's output, e.g. ["leaked-thinking", "stuck-in-a-loop"]. */ assistant?: string[]; } interface InstrumentModules { openAI?: unknown; anthropic?: unknown; cohere?: unknown; bedrock?: unknown; google_vertexai?: unknown; google_aiplatform?: unknown; pinecone?: unknown; together?: unknown; langchain?: boolean; llamaIndex?: unknown; chromadb?: unknown; qdrant?: unknown; mcp?: unknown; [key: string]: unknown; } interface MorphTracingConfig { /** * Morph API key. Defaults to `process.env.MORPH_API_KEY`. Sent as * `Authorization: Bearer ` to the trace ingest endpoint. */ apiKey?: string; /** * Base URL for the Morph ingest API. Traces are POSTed to `${baseUrl}/v1/traces`. * Defaults to `process.env.MORPH_TRACES_URL` then `https://api.morphllm.com`. */ baseUrl?: string; /** Service/app name attached to every span. Defaults to the host package name. */ appName?: string; /** * When true, the SDK initializes nothing and ships nothing. Useful for tests * and local/dev. Defaults to false. */ disabled?: boolean; /** * Send spans immediately instead of batching. Defaults to true outside * production (`NODE_ENV !== 'production'`) so local runs flush instantly. */ disableBatching?: boolean; /** * Capture prompt/response content on spans. Set false for zero-data-retention. * Defaults to true. */ traceContent?: boolean; /** Explicit modules to instrument. Omit to auto-instrument everything. */ instrumentModules?: InstrumentModules; /** * Set true when you run your own OpenTelemetry NodeSDK. Morph will not start * its own SDK; instead use `getInstrumentations()` and `createSpanProcessor()`. */ useExternalOtel?: boolean; /** Extra headers merged onto the OTLP exporter request. */ headers?: Record; /** Verbose `[morph-tracing]` logging. Defaults to `MORPH_TRACING_DEBUG=1`. */ debug?: boolean; /** * Default evals to run on every interaction's trace. Overridable per-interaction via * `begin({ evals })`. Requires `begin()` — evals only run on turns wrapped in an interaction * (so the trace carries an event id). Omit for none. */ evals?: EvalSelection; } /** Identity + content for a single traced AI interaction. */ interface TraceContext { /** Stable identifier for the interaction; auto-generated if omitted. */ eventId?: string; /** End-user identifier. */ userId?: string; /** Conversation/session identifier grouping related interactions. */ convoId?: string; /** Event name (defaults to "ai_generation" on the backend). */ event?: string; /** User input for this interaction. */ input?: string; /** Arbitrary string metadata propagated onto spans. */ properties?: Record; /** * Evals to run automatically on this interaction's trace. Results appear in the traces * dashboard already labeled. Overrides any default set in `morphTracing({ evals })`. * Omit to inherit the default (or run none). */ evals?: EvalSelection; } interface SpanParams { name: string; properties?: Record; } interface ToolParams { name: string; version?: number; properties?: Record; } /** Record a tool invocation that has already completed. */ interface TrackToolParams { name: string; input?: unknown; output?: unknown; durationMs?: number; error?: Error | string; properties?: Record; } interface FinishOptions { output: string; properties?: Record; } /** A manual tool span the caller drives explicitly. */ interface ToolSpan { setInput(input: unknown): void; setOutput(output: unknown): void; setError(error: Error | string): void; end(): void; } /** Metadata payload for the Vercel AI SDK `experimental_telemetry.metadata`. */ interface MetadataOptions { userId: string; convoId?: string; eventName?: string; eventId?: string; /** Custom tags, e.g. { source: "support-bot" } → association.properties.source */ properties?: Record; } export type { EvalSelection, FinishOptions, InstrumentModules, MetadataOptions, MorphTracingConfig, SpanParams, ToolParams, ToolSpan, TraceContext, TrackToolParams };