@webmcp-agents/telemetry 0.0.0-stage → 0.5.0
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/README.md +38 -2
- package/dist/bridge-n3EZZQrm.js +76 -0
- package/dist/engine-K_Cwwt9r.js +420 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.js +19 -0
- package/dist/observe-Bei_BEpM.js +225 -0
- package/dist/react.d.ts +19 -0
- package/dist/react.js +29 -0
- package/dist/semconv-74esS-9v.js +222 -0
- package/dist/semconv-BXKrNtuf.d.ts +221 -0
- package/dist/server.d.ts +244 -0
- package/dist/server.js +1431 -0
- package/dist/targets-vd2wpXrO.js +22 -0
- package/dist/types-8Tdrt7C_.d.ts +96 -0
- package/dist/types-Bqk6T-HL.d.ts +169 -0
- package/dist/util-4und6m9s.js +123 -0
- package/package.json +94 -3
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every span name, attribute key and event name this package writes, in one place (design decision 8). The GenAI ones
|
|
3
|
+
* follow the OpenTelemetry GenAI semantic conventions as they stood in November 2026 (status: Development, so they may
|
|
4
|
+
* still be renamed upstream); the `webmcp.*` ones are this library's own.
|
|
5
|
+
*
|
|
6
|
+
* Use them in a `spanFilter`, a `metadataMapping` or a test instead of retyping the strings.
|
|
7
|
+
*
|
|
8
|
+
* @experimental The GenAI conventions are not stable yet: names may change in a minor release when they do.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* import { ATTR_CONVERSATION_ID } from "@webmcp-agents/telemetry/server";
|
|
12
|
+
* defineObservability({ metadataMapping: { [ATTR_CONVERSATION_ID]: "session.id" } });
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* The instrumentation scope of every span this package creates in the browser, and the only one the relay accepts.
|
|
16
|
+
*
|
|
17
|
+
* @experimental
|
|
18
|
+
*/
|
|
19
|
+
declare const SCOPE_NAME = "@webmcp-agents/telemetry";
|
|
20
|
+
/**
|
|
21
|
+
* The scope of the server's model spans in the `ai.*` style (`conventions: "legacy"` or `"both"`).
|
|
22
|
+
*
|
|
23
|
+
* @experimental
|
|
24
|
+
*/
|
|
25
|
+
declare const SCOPE_NAME_LEGACY = "@webmcp-agents/telemetry/legacy";
|
|
26
|
+
/**
|
|
27
|
+
* One agent run: `invoke_agent <agent id>` (`invoke_agent web-agent` for a run without an agent).
|
|
28
|
+
*
|
|
29
|
+
* @experimental
|
|
30
|
+
*/
|
|
31
|
+
declare const SPAN_INVOKE_AGENT = "invoke_agent";
|
|
32
|
+
/**
|
|
33
|
+
* One tool call on the page: `execute_tool <tool name>`.
|
|
34
|
+
*
|
|
35
|
+
* @experimental
|
|
36
|
+
*/
|
|
37
|
+
declare const SPAN_EXECUTE_TOOL = "execute_tool";
|
|
38
|
+
/**
|
|
39
|
+
* One model step: `chat <model>` (the server's, from the AI SDK; the browser's for an on-device run).
|
|
40
|
+
*
|
|
41
|
+
* @experimental
|
|
42
|
+
*/
|
|
43
|
+
declare const SPAN_CHAT = "chat";
|
|
44
|
+
/**
|
|
45
|
+
* The subject of a run span when the run has no agent.
|
|
46
|
+
*
|
|
47
|
+
* @experimental
|
|
48
|
+
*/
|
|
49
|
+
declare const DEFAULT_AGENT_NAME = "web-agent";
|
|
50
|
+
/**
|
|
51
|
+
* `invoke_agent`, `execute_tool` or `chat`.
|
|
52
|
+
*
|
|
53
|
+
* @experimental
|
|
54
|
+
*/
|
|
55
|
+
declare const ATTR_OPERATION_NAME = "gen_ai.operation.name";
|
|
56
|
+
/**
|
|
57
|
+
* The agent's id (`defineWebAgent({ id })`).
|
|
58
|
+
*
|
|
59
|
+
* @experimental
|
|
60
|
+
*/
|
|
61
|
+
declare const ATTR_AGENT_ID = "gen_ai.agent.id";
|
|
62
|
+
/**
|
|
63
|
+
* The tool's name.
|
|
64
|
+
*
|
|
65
|
+
* @experimental
|
|
66
|
+
*/
|
|
67
|
+
declare const ATTR_TOOL_NAME = "gen_ai.tool.name";
|
|
68
|
+
/**
|
|
69
|
+
* The tool call's id, as the model gave it.
|
|
70
|
+
*
|
|
71
|
+
* @experimental
|
|
72
|
+
*/
|
|
73
|
+
declare const ATTR_TOOL_CALL_ID = "gen_ai.tool.call.id";
|
|
74
|
+
/**
|
|
75
|
+
* The tool's input as JSON. Only with `recordInputs`, always redacted.
|
|
76
|
+
*
|
|
77
|
+
* @experimental
|
|
78
|
+
*/
|
|
79
|
+
declare const ATTR_TOOL_CALL_ARGUMENTS = "gen_ai.tool.call.arguments";
|
|
80
|
+
/**
|
|
81
|
+
* The tool's result as JSON. Only with `recordOutputs`, always redacted.
|
|
82
|
+
*
|
|
83
|
+
* @experimental
|
|
84
|
+
*/
|
|
85
|
+
declare const ATTR_TOOL_CALL_RESULT = "gen_ai.tool.call.result";
|
|
86
|
+
/**
|
|
87
|
+
* The model a step asked for.
|
|
88
|
+
*
|
|
89
|
+
* @experimental
|
|
90
|
+
*/
|
|
91
|
+
declare const ATTR_REQUEST_MODEL = "gen_ai.request.model";
|
|
92
|
+
/**
|
|
93
|
+
* Prompt tokens of a step.
|
|
94
|
+
*
|
|
95
|
+
* @experimental
|
|
96
|
+
*/
|
|
97
|
+
declare const ATTR_USAGE_INPUT_TOKENS = "gen_ai.usage.input_tokens";
|
|
98
|
+
/**
|
|
99
|
+
* Generated tokens of a step.
|
|
100
|
+
*
|
|
101
|
+
* @experimental
|
|
102
|
+
*/
|
|
103
|
+
declare const ATTR_USAGE_OUTPUT_TOKENS = "gen_ai.usage.output_tokens";
|
|
104
|
+
/**
|
|
105
|
+
* The error code of a failed run or call (OpenTelemetry's general `error.type`).
|
|
106
|
+
*
|
|
107
|
+
* @experimental
|
|
108
|
+
*/
|
|
109
|
+
declare const ATTR_ERROR_TYPE = "error.type";
|
|
110
|
+
/**
|
|
111
|
+
* The conversation: the same for every turn of one chat.
|
|
112
|
+
*
|
|
113
|
+
* @experimental
|
|
114
|
+
*/
|
|
115
|
+
declare const ATTR_CONVERSATION_ID = "webmcp.conversation.id";
|
|
116
|
+
/**
|
|
117
|
+
* The run: one user turn, one `agent.invoke`, or one sub-agent run.
|
|
118
|
+
*
|
|
119
|
+
* @experimental
|
|
120
|
+
*/
|
|
121
|
+
declare const ATTR_RUN_ID = "webmcp.run.id";
|
|
122
|
+
/**
|
|
123
|
+
* The model step within the run, from 1.
|
|
124
|
+
*
|
|
125
|
+
* @experimental
|
|
126
|
+
*/
|
|
127
|
+
declare const ATTR_STEP = "webmcp.step";
|
|
128
|
+
/**
|
|
129
|
+
* For a sub-agent's run: the id of the `delegate_to_<id>` call that started it.
|
|
130
|
+
*
|
|
131
|
+
* @experimental
|
|
132
|
+
*/
|
|
133
|
+
declare const ATTR_PARENT_CALL_ID = "webmcp.parent.call.id";
|
|
134
|
+
/**
|
|
135
|
+
* The agent's id, on the server's spans.
|
|
136
|
+
*
|
|
137
|
+
* @experimental
|
|
138
|
+
*/
|
|
139
|
+
declare const ATTR_WEBMCP_AGENT_ID = "webmcp.agent.id";
|
|
140
|
+
/**
|
|
141
|
+
* The chat session's id, on the server's spans (`createChatHandler({ session })`).
|
|
142
|
+
*
|
|
143
|
+
* @experimental
|
|
144
|
+
*/
|
|
145
|
+
declare const ATTR_SESSION_ID = "webmcp.session.id";
|
|
146
|
+
/**
|
|
147
|
+
* The adapter that ran the model: `cloud`, `local`…
|
|
148
|
+
*
|
|
149
|
+
* @experimental
|
|
150
|
+
*/
|
|
151
|
+
declare const ATTR_ADAPTER = "webmcp.adapter";
|
|
152
|
+
/**
|
|
153
|
+
* How the run ended: `stop`, `max-steps`, `aborted` or `error`.
|
|
154
|
+
*
|
|
155
|
+
* @experimental
|
|
156
|
+
*/
|
|
157
|
+
declare const ATTR_RUN_REASON = "webmcp.run.reason";
|
|
158
|
+
/**
|
|
159
|
+
* Model steps the run took.
|
|
160
|
+
*
|
|
161
|
+
* @experimental
|
|
162
|
+
*/
|
|
163
|
+
declare const ATTR_RUN_STEPS = "webmcp.run.steps";
|
|
164
|
+
/**
|
|
165
|
+
* The page the run started on, redacted (origin and path; query values withheld).
|
|
166
|
+
*
|
|
167
|
+
* @experimental
|
|
168
|
+
*/
|
|
169
|
+
declare const ATTR_PAGE = "webmcp.page";
|
|
170
|
+
/**
|
|
171
|
+
* How the call ended: `done`, `error` or `cancelled`.
|
|
172
|
+
*
|
|
173
|
+
* @experimental
|
|
174
|
+
*/
|
|
175
|
+
declare const ATTR_TOOL_STATE = "webmcp.tool.state";
|
|
176
|
+
/**
|
|
177
|
+
* The error code of a failed or cancelled call (`not_found`, `cancelled`, `needs_user`…).
|
|
178
|
+
*
|
|
179
|
+
* @experimental
|
|
180
|
+
*/
|
|
181
|
+
declare const ATTR_TOOL_ERROR_CODE = "webmcp.tool.error.code";
|
|
182
|
+
/**
|
|
183
|
+
* Of the call's time, the milliseconds spent waiting for the person.
|
|
184
|
+
*
|
|
185
|
+
* @experimental
|
|
186
|
+
*/
|
|
187
|
+
declare const ATTR_TOOL_WAITED_MS = "webmcp.tool.waited_ms";
|
|
188
|
+
/**
|
|
189
|
+
* For a call that waited for the person: `accepted`, `cancelled` or `timeout`.
|
|
190
|
+
*
|
|
191
|
+
* @experimental
|
|
192
|
+
*/
|
|
193
|
+
declare const ATTR_TOOL_CONFIRM = "webmcp.tool.confirm";
|
|
194
|
+
/**
|
|
195
|
+
* The call started waiting for the person. Attribute `prompt`: what they were asked (redacted).
|
|
196
|
+
*
|
|
197
|
+
* @experimental
|
|
198
|
+
*/
|
|
199
|
+
declare const EVENT_PARK = "webmcp.park";
|
|
200
|
+
/**
|
|
201
|
+
* The person interacted and the cursor got out of the way. Attribute `reason`.
|
|
202
|
+
*
|
|
203
|
+
* @experimental
|
|
204
|
+
*/
|
|
205
|
+
declare const EVENT_YIELD = "webmcp.yield";
|
|
206
|
+
/**
|
|
207
|
+
* The cursor came back after a yield.
|
|
208
|
+
*
|
|
209
|
+
* @experimental
|
|
210
|
+
*/
|
|
211
|
+
declare const EVENT_RESUME = "webmcp.resume";
|
|
212
|
+
/**
|
|
213
|
+
* `true` on every span the app created through this package (`withAgentSpan` in the browser, `observability.span` /
|
|
214
|
+
* `observability.event` on the server), so a `spanFilter` or a backend query can tell them from the library's own.
|
|
215
|
+
* Their names are the app's, as given; their attribute keys are free except `webmcp.*`, which this library reserves
|
|
216
|
+
* (an app's `webmcp.*` keys are dropped).
|
|
217
|
+
*
|
|
218
|
+
* @experimental
|
|
219
|
+
*/
|
|
220
|
+
declare const ATTR_CUSTOM = "webmcp.custom";
|
|
221
|
+
export { SCOPE_NAME as A, ATTR_USAGE_INPUT_TOKENS as C, EVENT_PARK as D, DEFAULT_AGENT_NAME as E, SPAN_CHAT as M, SPAN_EXECUTE_TOOL as N, EVENT_RESUME as O, SPAN_INVOKE_AGENT as P, ATTR_TOOL_WAITED_MS as S, ATTR_WEBMCP_AGENT_ID as T, ATTR_TOOL_CALL_RESULT as _, ATTR_ERROR_TYPE as a, ATTR_TOOL_NAME as b, ATTR_PARENT_CALL_ID as c, ATTR_RUN_REASON as d, ATTR_RUN_STEPS as f, ATTR_TOOL_CALL_ID as g, ATTR_TOOL_CALL_ARGUMENTS as h, ATTR_CUSTOM as i, SCOPE_NAME_LEGACY as j, EVENT_YIELD as k, ATTR_REQUEST_MODEL as l, ATTR_STEP as m, ATTR_AGENT_ID as n, ATTR_OPERATION_NAME as o, ATTR_SESSION_ID as p, ATTR_CONVERSATION_ID as r, ATTR_PAGE as s, ATTR_ADAPTER as t, ATTR_RUN_ID as u, ATTR_TOOL_CONFIRM as v, ATTR_USAGE_OUTPUT_TOKENS as w, ATTR_TOOL_STATE as x, ATTR_TOOL_ERROR_CODE as y };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @webmcp-agents/telemetry/server: the chat handler's observability layer and the relay for the browser's spans.
|
|
3
|
+
*
|
|
4
|
+
* import { defineObservability } from "@webmcp-agents/telemetry/server";
|
|
5
|
+
* export const observability = defineObservability(); // OTEL_* env decides where spans go
|
|
6
|
+
* export const POST = createChatHandler({ agents, observability });
|
|
7
|
+
* // app/api/telemetry/route.ts
|
|
8
|
+
* export const POST = observability.relay();
|
|
9
|
+
*
|
|
10
|
+
* Everything here is experimental in 0.5.0.
|
|
11
|
+
*/
|
|
12
|
+
import { a as ReadableSpanLike, c as SpanExporter, d as TracerProviderOption, i as OtlpTarget, l as SpanProcessor, n as Attributes, o as Sampling, r as CustomAttributes, s as SamplingContext, t as AttributeValue, u as TracerProvider } from "./types-Bqk6T-HL.js";
|
|
13
|
+
import { A as SCOPE_NAME, C as ATTR_USAGE_INPUT_TOKENS, D as EVENT_PARK, E as DEFAULT_AGENT_NAME, M as SPAN_CHAT, N as SPAN_EXECUTE_TOOL, O as EVENT_RESUME, P as SPAN_INVOKE_AGENT, S as ATTR_TOOL_WAITED_MS, T as ATTR_WEBMCP_AGENT_ID, _ as ATTR_TOOL_CALL_RESULT, a as ATTR_ERROR_TYPE, b as ATTR_TOOL_NAME, c as ATTR_PARENT_CALL_ID, d as ATTR_RUN_REASON, f as ATTR_RUN_STEPS, g as ATTR_TOOL_CALL_ID, h as ATTR_TOOL_CALL_ARGUMENTS, i as ATTR_CUSTOM, j as SCOPE_NAME_LEGACY, k as EVENT_YIELD, l as ATTR_REQUEST_MODEL, m as ATTR_STEP, n as ATTR_AGENT_ID, o as ATTR_OPERATION_NAME, p as ATTR_SESSION_ID, r as ATTR_CONVERSATION_ID, s as ATTR_PAGE, t as ATTR_ADAPTER, u as ATTR_RUN_ID, v as ATTR_TOOL_CONFIRM, w as ATTR_USAGE_OUTPUT_TOKENS, x as ATTR_TOOL_STATE, y as ATTR_TOOL_ERROR_CODE } from "./semconv-BXKrNtuf.js";
|
|
14
|
+
import { ChatObservability } from "@webmcp-agents/chat/server";
|
|
15
|
+
import { RedactOptions, Redactor } from "@webmcp-agents/core/redact";
|
|
16
|
+
import "@opentelemetry/api";
|
|
17
|
+
/** Environment variables, as `process.env` holds them. */
|
|
18
|
+
type Env = Record<string, string | undefined>;
|
|
19
|
+
/**
|
|
20
|
+
* Where the env alone sends spans: the URL, or `"off"` (no endpoint variable, a kill switch, or a target that cannot
|
|
21
|
+
* be used). The design's table (§7.2) as a function.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* resolveTarget({}, { OTEL_EXPORTER_OTLP_ENDPOINT: "https://c.example" }) // "https://c.example/v1/traces"
|
|
25
|
+
*/
|
|
26
|
+
export declare function resolveTarget(target: OtlpTarget, env: Env): string;
|
|
27
|
+
/**
|
|
28
|
+
* Options for `defineObservability()`. Every field is optional: with none, the standard `OTEL_*` variables (or a
|
|
29
|
+
* tracer provider the app registered) decide.
|
|
30
|
+
*
|
|
31
|
+
* @experimental
|
|
32
|
+
*/
|
|
33
|
+
interface ObservabilityConfig {
|
|
34
|
+
/** `service.name` of every span. Default `OTEL_SERVICE_NAME`, else `"web-agents"`. */
|
|
35
|
+
serviceName?: string;
|
|
36
|
+
/** Resource attributes (release, region), merged over `OTEL_RESOURCE_ATTRIBUTES`; an option wins over the env. */
|
|
37
|
+
resourceAttributes?: Record<string, string>;
|
|
38
|
+
/**
|
|
39
|
+
* Where spans go: OTLP targets (`otlp()`) and/or any OpenTelemetry `SpanExporter` instance, as is. Default: the
|
|
40
|
+
* app's registered tracer provider, else one OTLP target from `OTEL_EXPORTER_OTLP_*`, else off.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* exporters: [otlp({ endpoint: "https://otel.example.com", headers: { "x-api-key": process.env.OTEL_KEY! } })]
|
|
44
|
+
*/
|
|
45
|
+
exporters?: readonly (OtlpTarget | SpanExporter)[];
|
|
46
|
+
/**
|
|
47
|
+
* Any OpenTelemetry `SpanProcessor` (a vendor's own, or yours), as is. They run first, before `metadataMapping`,
|
|
48
|
+
* redaction and `spanFilter`. A processor that exports by itself sends what the AI SDK set, which this package
|
|
49
|
+
* redacted when the span was created; what the processor adds is its own to handle.
|
|
50
|
+
*/
|
|
51
|
+
spanProcessors?: readonly SpanProcessor[];
|
|
52
|
+
/**
|
|
53
|
+
* A provider to use as is (its pipeline is the app's), `"global"` to require the app's registered one (off, with one
|
|
54
|
+
* error, when there is none), or `"own"` never to use it. Default: the registered one when there is one.
|
|
55
|
+
*/
|
|
56
|
+
tracerProvider?: TracerProviderOption;
|
|
57
|
+
/**
|
|
58
|
+
* Where the relay sends the browser's spans when the server's go through an app's provider (a `tracerProvider`, or
|
|
59
|
+
* the registered one). `"exporter"`: straight to the OTLP target (`exporters`, else `OTEL_EXPORTER_OTLP_*`) with
|
|
60
|
+
* the browser's own span ids, so a run and its tool calls need no reordering on the server and line up with the
|
|
61
|
+
* server's step spans. `"provider"`: re-created through the app's provider (new span ids, a child that comes a batch
|
|
62
|
+
* before its parent waits for it up to 30 s), for an app whose provider must see every span. With no OTLP target to
|
|
63
|
+
* send to, it is `"provider"`. With a provider this package builds, spans always go through it. Default `"exporter"`.
|
|
64
|
+
*/
|
|
65
|
+
relayThrough?: "exporter" | "provider";
|
|
66
|
+
/**
|
|
67
|
+
* What to do with the app's own `@ai-sdk/otel` integrations (`registerTelemetry(new OpenTelemetry())`) on a sampled
|
|
68
|
+
* request whose spans go to the same provider as the app's (the registered one): `"dedupe"` leaves them out of that
|
|
69
|
+
* call, logged once, so each model step is one `chat` span, not two; `"keep"` passes them along (both record). Other
|
|
70
|
+
* integrations, and every one when the request is not sampled or the layer builds its own provider, are always
|
|
71
|
+
* kept, never recording content. An `@ai-sdk/otel` integration the app gave a tracer of another provider is left out
|
|
72
|
+
* as well: pass `"keep"` then. Default `"dedupe"`.
|
|
73
|
+
*/
|
|
74
|
+
appIntegrations?: "dedupe" | "keep";
|
|
75
|
+
/** Which requests are exported. Default `OTEL_TRACES_SAMPLER` (+ `_ARG`), else `{ type: "always" }`. */
|
|
76
|
+
sampling?: Sampling;
|
|
77
|
+
/**
|
|
78
|
+
* Attribute style of the model spans: `"genai"` (`gen_ai.*`, default), `"legacy"` (`ai.*`, what LangWatch, PostHog
|
|
79
|
+
* and Braintrust read), or `"both"`, each from its own tracer.
|
|
80
|
+
*/
|
|
81
|
+
conventions?: "genai" | "legacy" | "both";
|
|
82
|
+
/** Record prompts and tool definitions on model spans, redacted. The handler's own `telemetry.recordInputs` wins. Default false. */
|
|
83
|
+
recordInputs?: boolean;
|
|
84
|
+
/** Record the model's output on model spans, redacted. The handler's own `telemetry.recordOutputs` wins. Default false. */
|
|
85
|
+
recordOutputs?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Redaction of everything exported (server spans and relayed browser spans). Default: the process's redactor
|
|
88
|
+
* (`configureRedaction`, `createChatHandler({ redact })`), read at each use. `false` only for local debugging.
|
|
89
|
+
*/
|
|
90
|
+
redact?: false | RedactOptions | Redactor;
|
|
91
|
+
/**
|
|
92
|
+
* Copy one of the span's attributes to the name a backend groups by. Runs before redaction.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* metadataMapping: { "webmcp.conversation.id": "session.id", "user.id": "enduser.id" }
|
|
96
|
+
*/
|
|
97
|
+
metadataMapping?: Record<string, string>;
|
|
98
|
+
/**
|
|
99
|
+
* Keep (true) or drop (false) a span; a throw drops it. With a provider this package builds it runs last, on the
|
|
100
|
+
* redacted span; through an app's provider it runs when the span starts (name and attributes only).
|
|
101
|
+
*/
|
|
102
|
+
spanFilter?: (span: ReadableSpanLike) => boolean;
|
|
103
|
+
/** Spans per export batch. Default 512. */
|
|
104
|
+
batchSize?: number;
|
|
105
|
+
/** Also print each exported span to the console, redacted. Default `WEB_AGENTS_OTEL_DEBUG === "true"`. */
|
|
106
|
+
debug?: boolean;
|
|
107
|
+
/** Where the `OTEL_*` variables are read. Default `process.env`. */
|
|
108
|
+
env?: Record<string, string | undefined>;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Options for `observability.relay()`, the endpoint that accepts the browser's spans.
|
|
112
|
+
*
|
|
113
|
+
* @experimental
|
|
114
|
+
*/
|
|
115
|
+
interface TelemetryRelayOptions {
|
|
116
|
+
/**
|
|
117
|
+
* Runs first (after the cross-site check): return a `Response` to refuse the request (a rate limit, a session check),
|
|
118
|
+
* or nothing to let it through. A throw refuses with 500 and is logged once.
|
|
119
|
+
*/
|
|
120
|
+
guard?: (req: Request) => Promise<Response | undefined> | Response | undefined;
|
|
121
|
+
/** The largest body accepted, in bytes; larger is refused with 413. Default 262144 (256 KB). */
|
|
122
|
+
maxBytes?: number;
|
|
123
|
+
/**
|
|
124
|
+
* Accept a `POST` with no `Origin` header. Default `false`: a browser always sends one (`fetch`, `keepalive`,
|
|
125
|
+
* `sendBeacon`), so a request without it is a script (`curl`, a bot) and is refused with 403. Set it only behind a
|
|
126
|
+
* proxy that strips `Origin`, and give the relay a `guard` then: anything that reaches it is forwarded to the backend.
|
|
127
|
+
*/
|
|
128
|
+
allowMissingOrigin?: boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Resource attributes added on the server (a release, a region), never trusted from the browser. Through an app's
|
|
131
|
+
* own provider, where the resource is the app's, they are set on each span instead.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* attributes: () => ({ "service.version": process.env.VERCEL_GIT_COMMIT_SHA ?? "dev" })
|
|
135
|
+
*/
|
|
136
|
+
attributes?: (req: Request) => Record<string, string>;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Options for `observability.event()` / `observability.span()`.
|
|
140
|
+
*
|
|
141
|
+
* @experimental
|
|
142
|
+
*/
|
|
143
|
+
interface CustomSpanOptions {
|
|
144
|
+
/**
|
|
145
|
+
* The chat request this belongs to, for code that runs before the handler enters it: a `guard` or `beforeModel`
|
|
146
|
+
* (both get the request). Code inside the request (a server-side tool, `prepareAgent`, `telemetry.metadata`, the
|
|
147
|
+
* app's own code awaited there) needs nothing: the request is found by `AsyncLocalStorage`. On the very first
|
|
148
|
+
* request the layer is not built yet when its `guard` runs: that one span is not recorded.
|
|
149
|
+
*/
|
|
150
|
+
request?: Request;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* What `defineObservability()` returns: the chat handler's `observability` layer, plus the relay and shutdown.
|
|
154
|
+
* Nothing loads until the first request.
|
|
155
|
+
*
|
|
156
|
+
* @experimental
|
|
157
|
+
*/
|
|
158
|
+
interface Observability extends ChatObservability {
|
|
159
|
+
/** Whether spans will be exported: false when the configuration resolves to off, or once building it failed. */
|
|
160
|
+
readonly enabled: boolean;
|
|
161
|
+
/**
|
|
162
|
+
* A `POST` handler that accepts the browser's spans and forwards them through this layer (mount it at
|
|
163
|
+
* `/api/telemetry`). Arguments after the request are the host's (a Worker's `ctx`): one with `waitUntil` keeps the
|
|
164
|
+
* function alive until the batch is sent, as Vercel's request context does.
|
|
165
|
+
*/
|
|
166
|
+
relay(options?: TelemetryRelayOptions): (req: Request, ...host: unknown[]) => Promise<Response>;
|
|
167
|
+
/** Send what is buffered and shut a provider this package built down. An app's own provider is only flushed. Never rejects. */
|
|
168
|
+
shutdown(): Promise<void>;
|
|
169
|
+
/**
|
|
170
|
+
* Record that something happened during the current chat request: a zero-length span named `name` with
|
|
171
|
+
* `attributes` and `webmcp.custom: true`, in the request's trace (see {@link Observability.span} for the parent,
|
|
172
|
+
* the pipeline and the naming rule). A span, not a span event: the run span the request belongs to is the browser's,
|
|
173
|
+
* and a span event can only go on a span still open in this process. A no-op outside a chat request and when the
|
|
174
|
+
* request is not sampled.
|
|
175
|
+
*
|
|
176
|
+
* @experimental
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* observability.event("inventory.checked", { "inventory.sku": sku, "inventory.left": left });
|
|
180
|
+
*/
|
|
181
|
+
event(name: string, attributes?: CustomAttributes, options?: CustomSpanOptions): void;
|
|
182
|
+
/**
|
|
183
|
+
* Run `fn` inside a span of the current chat request's trace, timed around it (sync, or until its promise settles).
|
|
184
|
+
* A throw or rejection sets the span's status to error (`error.type`: the error's name) and is passed on unchanged;
|
|
185
|
+
* returns what `fn` returns. Works in any code running during a chat request (a server-side tool, `prepareAgent`,
|
|
186
|
+
* `telemetry.metadata`, the app's own code awaited inside the handler; a `guard` / `beforeModel` with
|
|
187
|
+
* `{ request }`). Its parent, first match: the custom span it runs in; the app's active span (with a context manager
|
|
188
|
+
* registered); the browser's run span when the page records it (the `traceparent`'s sampled flag); the request's
|
|
189
|
+
* first model span. Before a request's first model step on a page that does not record its run, it starts a trace
|
|
190
|
+
* of its own.
|
|
191
|
+
*
|
|
192
|
+
* It is sampled with the request, created by this layer's tracer (a provider it builds, or the app's), and goes
|
|
193
|
+
* through the same pipeline as the model spans: `metadataMapping`, redaction, `spanFilter`. The name is the app's,
|
|
194
|
+
* as given (redacted like any name); attribute keys are free except `webmcp.*` (dropped: reserved for this library,
|
|
195
|
+
* which sets the request's ids and `webmcp.custom: true` itself). A no-op outside a chat request (no agent run to
|
|
196
|
+
* join, no sampling decision to follow) and when the request is not sampled: `fn` just runs. Telemetry never breaks
|
|
197
|
+
* `fn`: a failure in it is logged once.
|
|
198
|
+
*
|
|
199
|
+
* An app that already runs OpenTelemetry can also use `@opentelemetry/api` directly inside the request: with a
|
|
200
|
+
* global provider and context manager registered, its spans join the same trace (and nest under this span).
|
|
201
|
+
*
|
|
202
|
+
* @experimental
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* const price = await observability.span("pricing.quote", () => quote(cart), { "cart.items": cart.length });
|
|
206
|
+
*/
|
|
207
|
+
span<T>(name: string, fn: () => T, attributes?: CustomAttributes, options?: CustomSpanOptions): T;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* The observability layer for `createChatHandler({ observability })`: every model step as an OpenTelemetry span,
|
|
211
|
+
* with the run's ids, joined to the browser's run through its `traceparent`. Where spans go, first match wins: a
|
|
212
|
+
* `tracerProvider` passed in › `exporters` / `spanProcessors` › a tracer provider the app registered (Sentry,
|
|
213
|
+
* `@vercel/otel`, the OpenTelemetry SDK) › `OTEL_EXPORTER_OTLP_ENDPOINT` › off. It never registers anything globally.
|
|
214
|
+
* `relay()` is the endpoint for the browser's spans.
|
|
215
|
+
*
|
|
216
|
+
* Needs `@opentelemetry/api` and `@ai-sdk/otel` installed (and `@opentelemetry/sdk-trace` unless the app has its own
|
|
217
|
+
* provider); without them it is off, with one line saying which to install.
|
|
218
|
+
*
|
|
219
|
+
* @experimental
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* // app/api/chat/route.ts: everything else is OTEL_* env
|
|
223
|
+
* export const POST = createChatHandler({ agents, observability: defineObservability() });
|
|
224
|
+
*/
|
|
225
|
+
export declare function defineObservability(config?: ObservabilityConfig): Observability;
|
|
226
|
+
/**
|
|
227
|
+
* An OTLP target. Fields left out fall back to the `OTEL_EXPORTER_OTLP_*` variables when the layer is built, so
|
|
228
|
+
* `otlp()` alone means "whatever the env says". `endpoint` and `endpoints` together is a type error (and, at run time,
|
|
229
|
+
* that target is off with an error).
|
|
230
|
+
*
|
|
231
|
+
* @experimental
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* defineObservability({
|
|
235
|
+
* exporters: [
|
|
236
|
+
* otlp({ endpoint: "https://otel.example.com", protocol: "http/protobuf", headers: { "x-api-key": process.env.OTEL_KEY! } }),
|
|
237
|
+
* otlp({ endpoints: { traces: "https://traces.example.com/api/ingest/?tenant=acme" } }),
|
|
238
|
+
* ],
|
|
239
|
+
* });
|
|
240
|
+
*/
|
|
241
|
+
export declare function otlp(target?: OtlpTarget): OtlpTarget;
|
|
242
|
+
/** The relay's default body cap: 256 KB. */
|
|
243
|
+
export declare const RELAY_MAX_BYTES: number;
|
|
244
|
+
export { ATTR_ADAPTER, ATTR_AGENT_ID, ATTR_CONVERSATION_ID, ATTR_CUSTOM, ATTR_ERROR_TYPE, ATTR_OPERATION_NAME, ATTR_PAGE, ATTR_PARENT_CALL_ID, ATTR_REQUEST_MODEL, ATTR_RUN_ID, ATTR_RUN_REASON, ATTR_RUN_STEPS, ATTR_SESSION_ID, ATTR_STEP, ATTR_TOOL_CALL_ARGUMENTS, ATTR_TOOL_CALL_ID, ATTR_TOOL_CALL_RESULT, ATTR_TOOL_CONFIRM, ATTR_TOOL_ERROR_CODE, ATTR_TOOL_NAME, ATTR_TOOL_STATE, ATTR_TOOL_WAITED_MS, ATTR_USAGE_INPUT_TOKENS, ATTR_USAGE_OUTPUT_TOKENS, ATTR_WEBMCP_AGENT_ID, type AttributeValue, type Attributes, type CustomAttributes, type CustomSpanOptions, DEFAULT_AGENT_NAME, EVENT_PARK, EVENT_RESUME, EVENT_YIELD, type Observability, type ObservabilityConfig, type OtlpTarget, type ReadableSpanLike, SCOPE_NAME, SCOPE_NAME_LEGACY, SPAN_CHAT, SPAN_EXECUTE_TOOL, SPAN_INVOKE_AGENT, type Sampling, type SamplingContext, type SpanExporter, type SpanProcessor, type TelemetryRelayOptions, type TracerProvider, type TracerProviderOption };
|