import { Tracer, Context } from '@opentelemetry/api'; import { A as AgentEvent } from '../machine-rN7E-yh-.js'; import '../core-sEWVgnpI.js'; import '@standard-schema/spec'; import '../index-tFnc-ElD.js'; import '../index-DUdoElz8.js'; import '../index-CCs-QGGu.js'; import '../types-CtM4tWPp.js'; import '../index-DmoyvP-O.js'; import '../runtime-types-f231H8Gq.js'; import '../index-C1BK3MNo.js'; import '../retry-backoff/index.js'; import '../protocol-CqdURF0g.js'; import '../index-D5RP0MjN.js'; /** * @packageDocumentation * @demlik/tea/otel — an agent run as OpenTelemetry spans. * * `traceAgent(runtime, { tracer })` listens to an agent runtime's semantic * events and writes one span tree per run: a run span, a brain-call span per * turn and a tool span per tool call, the brain and tool spans children of the * run. Nothing here touches the reducer: the spans are a projection of the * `AgentEvent` stream, the way `transcript` is. * * The attributes follow the OpenTelemetry GenAI semantic conventions * (`gen_ai.*`), plus Langfuse's observation attributes (`langfuse.*`), so * Langfuse renders the run as an agent with generations and tool calls without * tea depending on any Langfuse package. Any OTel backend reads the same spans. * * `@opentelemetry/api` is an optional peer. Only this entry imports it. * * ## One trace across a kill * * The trace id is derived from the run's `runId` — the SHA-256 of it, cut to * 32 hex characters, which is exactly Langfuse's `createTraceId(runId)` — and * the run span is parented to a detached span context carrying it. The id * lives on the durable Model, so a process that resumes the run from its * `Store` writes into the same trace as the one that was killed. Spans the * killed process never ended are never exported; OpenTelemetry exports a span * when it ends. What it did end is in the trace beside the resumed leg. * This holds only on the default path: a run started under a host's `parent` * is in the host's trace, and keeping it one trace across a kill is the host's. * * ## Time * * Every span starts and ends at the `at` of the event that opened or closed * it, and `at` is read as **epoch milliseconds** — the runtime's `clock` * contract. It reaches OpenTelemetry as an exact `HrTime`, never a bare * number, so a small clock value (a logical or test clock) stays the instant * it names instead of being rebased onto process start. */ /** * A mask over one exported input or output — the same shape as * `LangfuseSpanProcessor`'s `mask`, so one function serves both. `data` is the * value already serialized to JSON; return what should be exported instead. */ type SpanMask = (params: { readonly data: unknown; }) => unknown; /** How {@link traceAgent} writes spans. */ interface TraceAgentOptions { /** The tracer the spans are started on — `provider.getTracer("…")`. */ readonly tracer: Tracer; /** * Applied to every exported input and output. Omit → {@link maskBase64DataUris}, * which keeps a screenshot a tool returned from being exported as megabytes * of base64. */ readonly mask?: SpanMask; /** * The agent's name, on `gen_ai.agent.name`, the run span's name and * Langfuse's trace name. Omit → `"agent"`. */ readonly name?: string; /** * The trace id a run writes into, from its `runId`. Omit → {@link runTraceId}. * Must return 32 lowercase hex characters, not all zero. Not called when * {@link TraceAgentOptions.parent | parent} is given. */ readonly traceIdOf?: (runId: string) => string; /** * The context every run span starts under — a span the host already has * open, as `trace.setSpan(context.active(), span)`. The run then lands in the * host's trace as that span's child, and its brain and tool spans follow. * * `parent` wins over `traceIdOf`: when both are given, the run is in the * parent's trace and `traceIdOf` is never called. With a `parent`, the run's * trace is the host's, so keeping one trace across a kill is the host's job * too — a resumed run's spans go wherever the resuming host's `parent` * points. Omit → each run hangs off a detached parent in the trace * `traceIdOf(runId)` names, which a resumed process derives again. */ readonly parent?: Context; } /** Anything that delivers an agent's events by type — a runtime built with `events: agentEvents()`. */ interface AgentEventSource { on["_tag"]>(type: K, handler: (event: Extract, { _tag: K; }>) => void): () => void; } /** * Trace an agent runtime into OpenTelemetry. Subscribes to every * `AgentEvent` type on `runtime` and returns a cleanup that detaches the * subscriptions and ends any span still open, at the `at` of the last event * its run folded. * * The runtime must project events — built with * `run(machine, { events: agentEvents() })`. For `defineAgent`, which takes an * `onEvent` listener instead of handing out its runtime, wire * {@link agentSpans}'s `onEvent` there. * * Each span is timed by its events' `at`, read as epoch milliseconds. * * @example * ```ts * import { type AgentEventSource, traceAgent } from "@demlik/tea/otel"; * import type { TracerProvider } from "@opentelemetry/api"; * * declare const provider: TracerProvider; * // Built with `run(machine, { ...wired, ctx, events: agentEvents() }).ready`. * declare const runtime: AgentEventSource; * * const stop = traceAgent(runtime, { tracer: provider.getTracer("audit") }); * ``` */ declare function traceAgent(runtime: AgentEventSource, opts: TraceAgentOptions): () => void; /** The span writer {@link traceAgent} subscribes, for wiring to an `onEvent` listener. */ interface AgentSpans { /** Fold one event into the span tree. */ readonly onEvent: (event: AgentEvent) => void; /** * End every span still open — the leg this process ran, when it stops * before its run does. Each carries `tea.run.detached: true`. * * With `at`, every open span ends at `at`, used as given. Without it, each * run's spans end at the `at` of the last event that run folded — its own * clock, never wall time. */ readonly end: (at?: number) => void; } /** * The span writer behind {@link traceAgent}, as a listener. Use it where the * events arrive through a callback rather than a runtime: * * @example * ```ts * import { defineAgent } from "@demlik/tea/agent"; * import { agentSpans } from "@demlik/tea/otel"; * import { trace } from "@opentelemetry/api"; * * const agent = defineAgent({ * model: async () => ({ content: "done", toolCalls: [] }), * tools: [], * instructions: "Summarise the page.", * }); * * const spans = agentSpans({ tracer: trace.getTracer("audit") }); * await agent.run("https://example.com", { onEvent: spans.onEvent }); * ``` */ declare function agentSpans(opts: TraceAgentOptions): AgentSpans; /** * The default {@link SpanMask}: every base64 `data:` URI in a string becomes * `[base64 data URI stripped before export]`. Anything that is not a string * passes through untouched. */ declare const maskBase64DataUris: SpanMask; /** * The trace id a run's spans are written into: the SHA-256 of `runId` as * lowercase hex, cut to 32 characters. It equals Langfuse's * `createTraceId(runId)`, so a trace can be looked up by the run it traced. */ declare function runTraceId(runId: string): string; export { type AgentEventSource, type AgentSpans, type SpanMask, type TraceAgentOptions, agentSpans, maskBase64DataUris, runTraceId, traceAgent };