@uselemma/tracing 7.15.2 → 7.16.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 +1 -1
- package/dist/client.d.ts +129 -56
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +599 -205
- package/dist/client.js.map +1 -1
- package/dist/coding-agent.d.ts +4 -1
- package/dist/coding-agent.d.ts.map +1 -1
- package/dist/coding-agent.js +73 -31
- package/dist/coding-agent.js.map +1 -1
- package/dist/delivery.d.ts +29 -0
- package/dist/delivery.d.ts.map +1 -1
- package/dist/delivery.js +96 -10
- package/dist/delivery.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -1
- package/dist/index.js.map +1 -1
- package/dist/integration-client.d.ts +1 -1
- package/dist/integration-client.d.ts.map +1 -1
- package/dist/integration-client.js.map +1 -1
- package/dist/langchain.d.ts +15 -17
- package/dist/langchain.d.ts.map +1 -1
- package/dist/langchain.js +327 -87
- package/dist/langchain.js.map +1 -1
- package/dist/mastra.d.ts.map +1 -1
- package/dist/mastra.js +225 -24
- package/dist/mastra.js.map +1 -1
- package/dist/messages.d.ts +84 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +1102 -0
- package/dist/messages.js.map +1 -0
- package/dist/openai-agents-containers.d.ts +1 -0
- package/dist/openai-agents-containers.d.ts.map +1 -1
- package/dist/openai-agents-containers.js.map +1 -1
- package/dist/openai-agents-generation.d.ts +8 -5
- package/dist/openai-agents-generation.d.ts.map +1 -1
- package/dist/openai-agents-generation.js +92 -44
- package/dist/openai-agents-generation.js.map +1 -1
- package/dist/openai-agents-tool-calls.d.ts +24 -0
- package/dist/openai-agents-tool-calls.d.ts.map +1 -0
- package/dist/openai-agents-tool-calls.js +207 -0
- package/dist/openai-agents-tool-calls.js.map +1 -0
- package/dist/openai-agents.d.ts +2 -7
- package/dist/openai-agents.d.ts.map +1 -1
- package/dist/openai-agents.js +259 -20
- package/dist/openai-agents.js.map +1 -1
- package/dist/payload.d.ts +30 -0
- package/dist/payload.d.ts.map +1 -0
- package/dist/payload.js +413 -0
- package/dist/payload.js.map +1 -0
- package/dist/release.d.ts +12 -0
- package/dist/release.d.ts.map +1 -1
- package/dist/release.js +41 -7
- package/dist/release.js.map +1 -1
- package/dist/tool-call-id.d.ts +14 -0
- package/dist/tool-call-id.d.ts.map +1 -0
- package/dist/tool-call-id.js +56 -0
- package/dist/tool-call-id.js.map +1 -0
- package/dist/turn.d.ts +25 -1
- package/dist/turn.d.ts.map +1 -1
- package/dist/turn.js +97 -14
- package/dist/turn.js.map +1 -1
- package/dist/usage.d.ts +5 -3
- package/dist/usage.d.ts.map +1 -1
- package/dist/usage.js +38 -35
- package/dist/usage.js.map +1 -1
- package/dist/vercel-ai-events.d.ts +46 -9
- package/dist/vercel-ai-events.d.ts.map +1 -1
- package/dist/vercel-ai-events.js +81 -7
- package/dist/vercel-ai-events.js.map +1 -1
- package/dist/vercel-ai-run.d.ts +7 -0
- package/dist/vercel-ai-run.d.ts.map +1 -1
- package/dist/vercel-ai-run.js +191 -47
- package/dist/vercel-ai-run.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
package/dist/client.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Lemma = exports.TraceHandle = exports.TraceContext = exports.NoopSpanHandle = exports.SpanHandle = exports.toWireTokenUsage = exports.normalizeTokenUsage = exports.attachResultUsage = void 0;
|
|
3
|
+
exports.Lemma = exports.TraceHandle = exports.TraceContext = exports.NoopSpanHandle = exports.SpanHandle = exports.TRACE_CONTRACT_SCHEMA_VERSION = exports.TRUNCATION_MARKER_KEY = exports.STRUCTURAL_PAYLOAD_KEYS = exports.DEDUP_MARKER_KEY = exports.normalizeToolDefinitions = exports.normalizeMessages = exports.normalizeFinishReason = exports.normalizeDocuments = exports.toWireTokenUsage = exports.normalizeTokenUsage = exports.attachResultUsage = exports.normalizeToolCallId = void 0;
|
|
4
4
|
exports.startBackgroundTrace = startBackgroundTrace;
|
|
5
5
|
const debug_delivery_1 = require("./debug-delivery");
|
|
6
6
|
const debug_mode_1 = require("./debug-mode");
|
|
@@ -8,27 +8,119 @@ const delivery_1 = require("./delivery");
|
|
|
8
8
|
const dropped_traces_1 = require("./dropped-traces");
|
|
9
9
|
const env_1 = require("./env");
|
|
10
10
|
const error_message_1 = require("./error-message");
|
|
11
|
+
const messages_1 = require("./messages");
|
|
12
|
+
const payload_1 = require("./payload");
|
|
11
13
|
const release_1 = require("./release");
|
|
12
14
|
const serialize_1 = require("./serialize");
|
|
13
15
|
const stream_result_1 = require("./stream-result");
|
|
16
|
+
const tool_call_id_1 = require("./tool-call-id");
|
|
14
17
|
const usage_1 = require("./usage");
|
|
15
18
|
const version_1 = require("./version");
|
|
19
|
+
var tool_call_id_2 = require("./tool-call-id");
|
|
20
|
+
Object.defineProperty(exports, "normalizeToolCallId", { enumerable: true, get: function () { return tool_call_id_2.normalizeToolCallId; } });
|
|
16
21
|
var usage_2 = require("./usage");
|
|
17
22
|
Object.defineProperty(exports, "attachResultUsage", { enumerable: true, get: function () { return usage_2.attachResultUsage; } });
|
|
18
23
|
Object.defineProperty(exports, "normalizeTokenUsage", { enumerable: true, get: function () { return usage_2.normalizeTokenUsage; } });
|
|
19
24
|
Object.defineProperty(exports, "toWireTokenUsage", { enumerable: true, get: function () { return usage_2.toWireTokenUsage; } });
|
|
25
|
+
var messages_2 = require("./messages");
|
|
26
|
+
Object.defineProperty(exports, "normalizeDocuments", { enumerable: true, get: function () { return messages_2.normalizeDocuments; } });
|
|
27
|
+
Object.defineProperty(exports, "normalizeFinishReason", { enumerable: true, get: function () { return messages_2.normalizeFinishReason; } });
|
|
28
|
+
Object.defineProperty(exports, "normalizeMessages", { enumerable: true, get: function () { return messages_2.normalizeMessages; } });
|
|
29
|
+
Object.defineProperty(exports, "normalizeToolDefinitions", { enumerable: true, get: function () { return messages_2.normalizeToolDefinitions; } });
|
|
30
|
+
var payload_2 = require("./payload");
|
|
31
|
+
Object.defineProperty(exports, "DEDUP_MARKER_KEY", { enumerable: true, get: function () { return payload_2.DEDUP_MARKER_KEY; } });
|
|
32
|
+
Object.defineProperty(exports, "STRUCTURAL_PAYLOAD_KEYS", { enumerable: true, get: function () { return payload_2.STRUCTURAL_PAYLOAD_KEYS; } });
|
|
33
|
+
Object.defineProperty(exports, "TRUNCATION_MARKER_KEY", { enumerable: true, get: function () { return payload_2.TRUNCATION_MARKER_KEY; } });
|
|
34
|
+
exports.TRACE_CONTRACT_SCHEMA_VERSION = 1;
|
|
20
35
|
const SDK_LANGUAGE = "typescript";
|
|
21
36
|
const SDK_INTEGRATION_ATTR = "lemma.sdk.integration";
|
|
22
37
|
const SDK_LANGUAGE_ATTR = "lemma.sdk.language";
|
|
38
|
+
const FINISH_REASON_ERROR_MESSAGE = 'Model call ended with finishReason "error"';
|
|
23
39
|
function required(value, envName) {
|
|
24
40
|
if (value?.trim())
|
|
25
41
|
return value.trim();
|
|
26
42
|
throw new Error(`@uselemma/tracing: Missing ${envName}`);
|
|
27
43
|
}
|
|
44
|
+
function nonBlankString(value) {
|
|
45
|
+
if (typeof value !== "string")
|
|
46
|
+
return undefined;
|
|
47
|
+
return value.trim().length > 0 ? value : undefined;
|
|
48
|
+
}
|
|
49
|
+
function nonNegativeNumber(value) {
|
|
50
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
function asPlainObject(value) {
|
|
56
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
const RFC3339_TIMESTAMP_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/i;
|
|
28
62
|
function iso(value) {
|
|
29
63
|
if (value == null)
|
|
30
64
|
return value;
|
|
31
|
-
|
|
65
|
+
if (value instanceof Date) {
|
|
66
|
+
return Number.isNaN(value.getTime()) ? undefined : value.toISOString();
|
|
67
|
+
}
|
|
68
|
+
const trimmed = value.trim();
|
|
69
|
+
if (!trimmed)
|
|
70
|
+
return undefined;
|
|
71
|
+
const parsed = new Date(trimmed);
|
|
72
|
+
if (Number.isNaN(parsed.getTime()))
|
|
73
|
+
return undefined;
|
|
74
|
+
return RFC3339_TIMESTAMP_RE.test(trimmed) ? trimmed : parsed.toISOString();
|
|
75
|
+
}
|
|
76
|
+
function firstTrimmed(...values) {
|
|
77
|
+
for (const value of values) {
|
|
78
|
+
const normalized = (0, tool_call_id_1.normalizeSingleLineTrimmed)(value);
|
|
79
|
+
if (normalized !== undefined)
|
|
80
|
+
return normalized;
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
function firstNonBlank(...values) {
|
|
85
|
+
for (const value of values) {
|
|
86
|
+
const normalized = nonBlankString(value);
|
|
87
|
+
if (normalized !== undefined)
|
|
88
|
+
return normalized;
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
function cloneForTransform(value, seen = new WeakMap()) {
|
|
93
|
+
if (value === null || typeof value !== "object")
|
|
94
|
+
return value;
|
|
95
|
+
if (value instanceof Date ||
|
|
96
|
+
value instanceof RegExp ||
|
|
97
|
+
value instanceof URL ||
|
|
98
|
+
value instanceof Error ||
|
|
99
|
+
ArrayBuffer.isView(value)) {
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
const tag = Object.prototype.toString.call(value);
|
|
103
|
+
if (tag === "[object ArrayBuffer]" || tag === "[object SharedArrayBuffer]") {
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
if (typeof Blob !== "undefined" && value instanceof Blob)
|
|
107
|
+
return value;
|
|
108
|
+
if (seen.has(value))
|
|
109
|
+
return seen.get(value);
|
|
110
|
+
if (Array.isArray(value)) {
|
|
111
|
+
const copy = [];
|
|
112
|
+
seen.set(value, copy);
|
|
113
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
114
|
+
copy[i] = cloneForTransform(value[i], seen);
|
|
115
|
+
}
|
|
116
|
+
return copy;
|
|
117
|
+
}
|
|
118
|
+
const copy = {};
|
|
119
|
+
seen.set(value, copy);
|
|
120
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
121
|
+
copy[key] = cloneForTransform(entry, seen);
|
|
122
|
+
}
|
|
123
|
+
return copy;
|
|
32
124
|
}
|
|
33
125
|
function timestampMs(value) {
|
|
34
126
|
if (value == null)
|
|
@@ -53,123 +145,255 @@ function summarizeSpanForDebug(span, index) {
|
|
|
53
145
|
status: span.status,
|
|
54
146
|
durationMs: span.duration_ms,
|
|
55
147
|
model: span.model,
|
|
56
|
-
hasInput: span.input !== undefined,
|
|
57
|
-
hasOutput: span.output !== undefined,
|
|
148
|
+
hasInput: span.input !== undefined || span.llm_input_messages !== undefined,
|
|
149
|
+
hasOutput: span.output !== undefined || span.llm_output_messages !== undefined,
|
|
58
150
|
hasError: Boolean(span.error),
|
|
59
151
|
}).filter(([, value]) => value !== undefined));
|
|
60
152
|
}
|
|
61
|
-
function serializeAttribute(value) {
|
|
62
|
-
if (value == null)
|
|
63
|
-
return value;
|
|
64
|
-
if (typeof value === "string" ||
|
|
65
|
-
typeof value === "number" ||
|
|
66
|
-
typeof value === "boolean")
|
|
67
|
-
return value;
|
|
68
|
-
return (0, serialize_1.stringifyTraceJson)(value);
|
|
69
|
-
}
|
|
70
|
-
function addDefined(attributes, key, value) {
|
|
71
|
-
if (value !== undefined)
|
|
72
|
-
attributes[key] = value;
|
|
73
|
-
}
|
|
74
|
-
function flattenMessage(attributes, prefix, message) {
|
|
75
|
-
if (message && typeof message === "object" && !Array.isArray(message)) {
|
|
76
|
-
for (const [key, value] of Object.entries(message)) {
|
|
77
|
-
addDefined(attributes, `${prefix}.message.${key}`, serializeAttribute(value));
|
|
78
|
-
}
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
addDefined(attributes, `${prefix}.message.content`, serializeAttribute(message));
|
|
82
|
-
}
|
|
83
|
-
function flattenDocument(attributes, prefix, document) {
|
|
84
|
-
if (document && typeof document === "object" && !Array.isArray(document)) {
|
|
85
|
-
for (const [key, value] of Object.entries(document)) {
|
|
86
|
-
addDefined(attributes, `${prefix}.document.${key}`, serializeAttribute(value));
|
|
87
|
-
}
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
addDefined(attributes, `${prefix}.document.content`, serializeAttribute(document));
|
|
91
|
-
}
|
|
92
153
|
function resolveUsage(options) {
|
|
93
154
|
return (0, usage_1.normalizeTokenUsage)(options.usage);
|
|
94
155
|
}
|
|
95
|
-
function contractAttributes(options) {
|
|
96
|
-
const attributes = {};
|
|
97
|
-
addDefined(attributes, "input.mime_type", options.inputMimeType);
|
|
98
|
-
addDefined(attributes, "output.mime_type", options.outputMimeType);
|
|
99
|
-
const modelIdentity = options.llmModelName ?? options.model;
|
|
100
|
-
addDefined(attributes, "llm.model_name", modelIdentity);
|
|
101
|
-
addDefined(attributes, "gen_ai.request.model", modelIdentity);
|
|
102
|
-
addDefined(attributes, "ai.model.id", modelIdentity);
|
|
103
|
-
addDefined(attributes, "llm.provider", options.llmProvider);
|
|
104
|
-
// llm.system from explicit llmSystem; gen_ai.system from llmSystem or llmProvider.
|
|
105
|
-
addDefined(attributes, "llm.system", options.llmSystem);
|
|
106
|
-
addDefined(attributes, "gen_ai.system", options.llmSystem ?? options.llmProvider);
|
|
107
|
-
Object.assign(attributes, (0, usage_1.tokenUsageAttributes)(resolveUsage(options)));
|
|
108
|
-
addDefined(attributes, "llm.invocation_parameters", serializeAttribute(options.llmInvocationParameters));
|
|
109
|
-
addDefined(attributes, "llm.tools", serializeAttribute(options.llmTools));
|
|
110
|
-
addDefined(attributes, "llm.prompt_template.template", options.llmPromptTemplate);
|
|
111
|
-
addDefined(attributes, "llm.prompt_template.variables", serializeAttribute(options.llmPromptTemplateVariables));
|
|
112
|
-
addDefined(attributes, "llm.prompt_template.version", options.llmPromptTemplateVersion);
|
|
113
|
-
addDefined(attributes, "tool.description", options.toolDescription);
|
|
114
|
-
addDefined(attributes, "tool.parameters", serializeAttribute(options.toolParameters));
|
|
115
|
-
if (options.userFacingMessage !== undefined) {
|
|
116
|
-
attributes["lemma.tool.kind"] = "user_message";
|
|
117
|
-
attributes["lemma.tool.message"] = options.userFacingMessage;
|
|
118
|
-
}
|
|
119
|
-
addDefined(attributes, "embedding.model_name", options.embeddingModelName);
|
|
120
|
-
addDefined(attributes, "embedding.invocation_parameters", serializeAttribute(options.embeddingInvocationParameters));
|
|
121
|
-
addDefined(attributes, "embedding.embeddings", serializeAttribute(options.embeddingEmbeddings));
|
|
122
|
-
addDefined(attributes, "reranker.model_name", options.rerankerModelName);
|
|
123
|
-
options.llmInputMessages?.forEach((message, index) => {
|
|
124
|
-
flattenMessage(attributes, `llm.input_messages.${index}`, message);
|
|
125
|
-
});
|
|
126
|
-
options.llmOutputMessages?.forEach((message, index) => {
|
|
127
|
-
flattenMessage(attributes, `llm.output_messages.${index}`, message);
|
|
128
|
-
});
|
|
129
|
-
options.rerankerInputDocuments?.forEach((document, index) => {
|
|
130
|
-
flattenDocument(attributes, `reranker.input_documents.${index}`, document);
|
|
131
|
-
});
|
|
132
|
-
options.rerankerOutputDocuments?.forEach((document, index) => {
|
|
133
|
-
flattenDocument(attributes, `reranker.output_documents.${index}`, document);
|
|
134
|
-
});
|
|
135
|
-
return attributes;
|
|
136
|
-
}
|
|
137
156
|
function spanAttributes(options) {
|
|
138
157
|
const callerAttributes = options.attributes ?? {};
|
|
139
|
-
const integration = typeof callerAttributes[SDK_INTEGRATION_ATTR] === "string"
|
|
158
|
+
const integration = typeof callerAttributes[SDK_INTEGRATION_ATTR] === "string" &&
|
|
159
|
+
callerAttributes[SDK_INTEGRATION_ATTR].trim()
|
|
140
160
|
? callerAttributes[SDK_INTEGRATION_ATTR]
|
|
141
161
|
: "manual";
|
|
142
|
-
|
|
162
|
+
return {
|
|
143
163
|
...callerAttributes,
|
|
144
|
-
...contractAttributes(options),
|
|
145
164
|
[SDK_LANGUAGE_ATTR]: SDK_LANGUAGE,
|
|
146
165
|
[SDK_INTEGRATION_ATTR]: integration,
|
|
147
166
|
};
|
|
148
|
-
|
|
167
|
+
}
|
|
168
|
+
function isSameJson(a, b) {
|
|
169
|
+
if (a === b)
|
|
170
|
+
return true;
|
|
171
|
+
if (a === undefined || b === undefined)
|
|
172
|
+
return false;
|
|
173
|
+
return (0, serialize_1.stringifyTraceJson)(a) === (0, serialize_1.stringifyTraceJson)(b);
|
|
174
|
+
}
|
|
175
|
+
function isRedundantGenerationInput(rawInput, rawMessages, canonicalMessages) {
|
|
176
|
+
if (rawInput === undefined)
|
|
177
|
+
return true;
|
|
178
|
+
if (!canonicalMessages)
|
|
179
|
+
return false;
|
|
180
|
+
if (isSameJson(rawInput, rawMessages) || isSameJson(rawInput, canonicalMessages)) {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
if (canonicalMessages.length === 1 &&
|
|
184
|
+
canonicalMessages[0].role === "user" &&
|
|
185
|
+
typeof canonicalMessages[0].content === "string" &&
|
|
186
|
+
rawInput === canonicalMessages[0].content) {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
function isRedundantGenerationOutput(rawOutput, rawMessages, canonicalMessages) {
|
|
192
|
+
if (rawOutput === undefined)
|
|
193
|
+
return true;
|
|
194
|
+
if (!canonicalMessages)
|
|
195
|
+
return false;
|
|
196
|
+
if (isSameJson(rawOutput, rawMessages) ||
|
|
197
|
+
isSameJson(rawOutput, canonicalMessages)) {
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
if (canonicalMessages.length === 1 &&
|
|
201
|
+
canonicalMessages[0].role === "assistant" &&
|
|
202
|
+
!canonicalMessages[0].tool_calls?.length &&
|
|
203
|
+
typeof canonicalMessages[0].content === "string" &&
|
|
204
|
+
rawOutput === canonicalMessages[0].content) {
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
return false;
|
|
149
208
|
}
|
|
150
209
|
function normalizeSpan(options, fallbackType) {
|
|
210
|
+
const type = options.type ?? fallbackType;
|
|
151
211
|
const startedAt = options.startedAt === undefined ? new Date() : options.startedAt;
|
|
152
212
|
const endedAt = options.endedAt === undefined ? new Date() : options.endedAt;
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
213
|
+
const finishReason = type === "generation"
|
|
214
|
+
? (0, messages_1.normalizeFinishReason)(options.finishReason)
|
|
215
|
+
: undefined;
|
|
216
|
+
const rawError = (0, error_message_1.failureMessage)(options.error) ?? undefined;
|
|
217
|
+
const error = rawError ??
|
|
218
|
+
(finishReason === "error" ? FINISH_REASON_ERROR_MESSAGE : undefined);
|
|
219
|
+
const status = error
|
|
220
|
+
? "ERROR"
|
|
221
|
+
: options.status === "OK" || options.status === "ERROR"
|
|
222
|
+
? options.status
|
|
223
|
+
: undefined;
|
|
224
|
+
const isError = status === "ERROR" || error !== undefined;
|
|
225
|
+
const span = {
|
|
226
|
+
name: nonBlankString(options.name) ?? type,
|
|
227
|
+
type,
|
|
163
228
|
attributes: spanAttributes(options),
|
|
164
|
-
...(startedAt === null ? {} : { started_at: iso(startedAt) }),
|
|
165
|
-
...(endedAt === null ? {} : { ended_at: iso(endedAt) }),
|
|
166
|
-
duration_ms: options.durationMs,
|
|
167
|
-
status: options.status ?? (error ? "ERROR" : undefined),
|
|
168
|
-
error,
|
|
169
|
-
model: options.model,
|
|
170
|
-
tool_name: options.toolName,
|
|
171
|
-
...(usage ? { usage } : {}),
|
|
172
229
|
};
|
|
230
|
+
const id = nonBlankString(options.id);
|
|
231
|
+
if (id !== undefined)
|
|
232
|
+
span.id = id;
|
|
233
|
+
const rawParentId = options.parentId !== undefined ? options.parentId : options.parentSpanId;
|
|
234
|
+
if (rawParentId === null) {
|
|
235
|
+
span.parent_id = null;
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
const parentId = nonBlankString(rawParentId);
|
|
239
|
+
if (parentId !== undefined)
|
|
240
|
+
span.parent_id = parentId;
|
|
241
|
+
}
|
|
242
|
+
if (startedAt !== null && startedAt !== undefined) {
|
|
243
|
+
const startedIso = iso(startedAt);
|
|
244
|
+
if (startedIso)
|
|
245
|
+
span.started_at = startedIso;
|
|
246
|
+
}
|
|
247
|
+
if (endedAt !== null && endedAt !== undefined) {
|
|
248
|
+
const endedIso = iso(endedAt);
|
|
249
|
+
if (endedIso)
|
|
250
|
+
span.ended_at = endedIso;
|
|
251
|
+
}
|
|
252
|
+
const durationMs = nonNegativeNumber(options.durationMs);
|
|
253
|
+
if (durationMs !== undefined)
|
|
254
|
+
span.duration_ms = durationMs;
|
|
255
|
+
if (status !== undefined)
|
|
256
|
+
span.status = status;
|
|
257
|
+
if (error !== undefined)
|
|
258
|
+
span.error = error;
|
|
259
|
+
const inputMimeType = (0, tool_call_id_1.normalizeSingleLineTrimmed)(options.inputMimeType);
|
|
260
|
+
if (inputMimeType !== undefined)
|
|
261
|
+
span.input_mime_type = inputMimeType;
|
|
262
|
+
const outputMimeType = (0, tool_call_id_1.normalizeSingleLineTrimmed)(options.outputMimeType);
|
|
263
|
+
if (outputMimeType !== undefined)
|
|
264
|
+
span.output_mime_type = outputMimeType;
|
|
265
|
+
const metadata = asPlainObject(options.metadata);
|
|
266
|
+
if (metadata !== undefined)
|
|
267
|
+
span.metadata = metadata;
|
|
268
|
+
const provider = firstTrimmed(options.provider, options.llmProvider, options.llmSystem);
|
|
269
|
+
const usage = (0, usage_1.toWireTokenUsage)(resolveUsage(options));
|
|
270
|
+
if (type === "generation") {
|
|
271
|
+
const model = firstNonBlank(options.model, options.llmModelName);
|
|
272
|
+
if (model !== undefined)
|
|
273
|
+
span.model = model;
|
|
274
|
+
if (provider !== undefined)
|
|
275
|
+
span.provider = provider;
|
|
276
|
+
const invocationParameters = asPlainObject(options.invocationParameters ?? options.llmInvocationParameters);
|
|
277
|
+
if (invocationParameters !== undefined) {
|
|
278
|
+
span.invocation_parameters = invocationParameters;
|
|
279
|
+
}
|
|
280
|
+
const tools = (0, messages_1.normalizeToolDefinitions)(options.tools ?? options.llmTools);
|
|
281
|
+
if (tools !== undefined)
|
|
282
|
+
span.tools = tools;
|
|
283
|
+
const llmInputMessages = Array.isArray(options.llmInputMessages)
|
|
284
|
+
? (0, messages_1.normalizeMessages)(options.llmInputMessages)
|
|
285
|
+
: undefined;
|
|
286
|
+
if (llmInputMessages !== undefined) {
|
|
287
|
+
span.llm_input_messages = llmInputMessages;
|
|
288
|
+
}
|
|
289
|
+
const llmOutputMessages = Array.isArray(options.llmOutputMessages)
|
|
290
|
+
? (0, messages_1.normalizeMessages)(options.llmOutputMessages, {
|
|
291
|
+
defaultRole: "assistant",
|
|
292
|
+
})
|
|
293
|
+
: undefined;
|
|
294
|
+
if (llmOutputMessages !== undefined) {
|
|
295
|
+
span.llm_output_messages = llmOutputMessages;
|
|
296
|
+
}
|
|
297
|
+
if (usage !== undefined)
|
|
298
|
+
span.usage = usage;
|
|
299
|
+
if (finishReason !== undefined)
|
|
300
|
+
span.finish_reason = finishReason;
|
|
301
|
+
const ttft = nonNegativeNumber(options.timeToFirstTokenMs);
|
|
302
|
+
if (ttft !== undefined) {
|
|
303
|
+
const bound = durationMs ??
|
|
304
|
+
(span.started_at !== undefined && span.ended_at !== undefined
|
|
305
|
+
? elapsedMs(span.started_at, span.ended_at)
|
|
306
|
+
: undefined);
|
|
307
|
+
span.time_to_first_token_ms =
|
|
308
|
+
bound !== undefined ? Math.min(ttft, bound) : ttft;
|
|
309
|
+
}
|
|
310
|
+
const promptTemplate = options.promptTemplate ?? options.llmPromptTemplate;
|
|
311
|
+
if (typeof promptTemplate === "string") {
|
|
312
|
+
span.prompt_template = promptTemplate;
|
|
313
|
+
}
|
|
314
|
+
const promptTemplateVariables = asPlainObject(options.promptTemplateVariables ?? options.llmPromptTemplateVariables);
|
|
315
|
+
if (promptTemplateVariables !== undefined) {
|
|
316
|
+
span.prompt_template_variables = promptTemplateVariables;
|
|
317
|
+
}
|
|
318
|
+
const promptTemplateVersion = (0, tool_call_id_1.normalizeSingleLineTrimmed)(options.promptTemplateVersion ?? options.llmPromptTemplateVersion);
|
|
319
|
+
if (promptTemplateVersion !== undefined) {
|
|
320
|
+
span.prompt_template_version = promptTemplateVersion;
|
|
321
|
+
}
|
|
322
|
+
if (!isRedundantGenerationInput(options.input, options.llmInputMessages, llmInputMessages)) {
|
|
323
|
+
span.input = options.input;
|
|
324
|
+
}
|
|
325
|
+
if (!isError &&
|
|
326
|
+
!isRedundantGenerationOutput(options.output, options.llmOutputMessages, llmOutputMessages)) {
|
|
327
|
+
span.output = options.output;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
if (options.input !== undefined)
|
|
332
|
+
span.input = options.input;
|
|
333
|
+
if (!isError && options.output !== undefined)
|
|
334
|
+
span.output = options.output;
|
|
335
|
+
if (type === "tool") {
|
|
336
|
+
const toolName = (0, tool_call_id_1.normalizeSingleLineTrimmed)(options.toolName);
|
|
337
|
+
if (toolName !== undefined)
|
|
338
|
+
span.tool_name = toolName;
|
|
339
|
+
const toolCallId = (0, tool_call_id_1.normalizeToolCallId)(options.toolCallId);
|
|
340
|
+
if (toolCallId !== undefined)
|
|
341
|
+
span.tool_call_id = toolCallId;
|
|
342
|
+
if (typeof options.toolDescription === "string") {
|
|
343
|
+
span.tool_description = options.toolDescription;
|
|
344
|
+
}
|
|
345
|
+
const toolParameters = asPlainObject(options.toolParameters);
|
|
346
|
+
if (toolParameters !== undefined)
|
|
347
|
+
span.tool_parameters = toolParameters;
|
|
348
|
+
if (typeof options.userFacingMessage === "string") {
|
|
349
|
+
span.user_facing_message = options.userFacingMessage;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
else if (type === "retrieval") {
|
|
353
|
+
const docs = (0, messages_1.normalizeDocuments)(options.retrievalDocuments);
|
|
354
|
+
if (docs !== undefined)
|
|
355
|
+
span.retrieval_documents = docs;
|
|
356
|
+
}
|
|
357
|
+
else if (type === "embedding") {
|
|
358
|
+
const model = nonBlankString(options.model ?? options.embeddingModelName);
|
|
359
|
+
if (model !== undefined)
|
|
360
|
+
span.model = model;
|
|
361
|
+
if (provider !== undefined)
|
|
362
|
+
span.provider = provider;
|
|
363
|
+
const invocationParameters = asPlainObject(options.invocationParameters ?? options.embeddingInvocationParameters);
|
|
364
|
+
if (invocationParameters !== undefined) {
|
|
365
|
+
span.invocation_parameters = invocationParameters;
|
|
366
|
+
}
|
|
367
|
+
if (usage !== undefined)
|
|
368
|
+
span.usage = usage;
|
|
369
|
+
}
|
|
370
|
+
else if (type === "reranker") {
|
|
371
|
+
const model = nonBlankString(options.model ?? options.rerankerModelName);
|
|
372
|
+
if (model !== undefined)
|
|
373
|
+
span.model = model;
|
|
374
|
+
if (provider !== undefined)
|
|
375
|
+
span.provider = provider;
|
|
376
|
+
const inputDocs = (0, messages_1.normalizeDocuments)(options.rerankerInputDocuments);
|
|
377
|
+
if (inputDocs !== undefined)
|
|
378
|
+
span.reranker_input_documents = inputDocs;
|
|
379
|
+
const outputDocs = (0, messages_1.normalizeDocuments)(options.rerankerOutputDocuments);
|
|
380
|
+
if (outputDocs !== undefined)
|
|
381
|
+
span.reranker_output_documents = outputDocs;
|
|
382
|
+
}
|
|
383
|
+
else if (type === "guardrail") {
|
|
384
|
+
if (typeof options.guardrailTriggered === "boolean") {
|
|
385
|
+
span.guardrail_triggered = options.guardrailTriggered;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return span;
|
|
390
|
+
}
|
|
391
|
+
/** Replace all own enumerable properties of `target` with `source`. */
|
|
392
|
+
function replaceSpanPayload(target, source) {
|
|
393
|
+
for (const key of Object.keys(target)) {
|
|
394
|
+
delete target[key];
|
|
395
|
+
}
|
|
396
|
+
Object.assign(target, source);
|
|
173
397
|
}
|
|
174
398
|
/** JSON for a built payload, which is a plain object and always serializes. */
|
|
175
399
|
function payloadBody(payload) {
|
|
@@ -178,6 +402,16 @@ function payloadBody(payload) {
|
|
|
178
402
|
throw new Error("trace payload is empty");
|
|
179
403
|
return body;
|
|
180
404
|
}
|
|
405
|
+
async function gzipString(body) {
|
|
406
|
+
const bytes = new TextEncoder().encode(body);
|
|
407
|
+
if (typeof CompressionStream === "undefined") {
|
|
408
|
+
throw new Error("@uselemma/tracing: gzip compression requires CompressionStream support in this runtime");
|
|
409
|
+
}
|
|
410
|
+
const stream = new Blob([bytes])
|
|
411
|
+
.stream()
|
|
412
|
+
.pipeThrough(new CompressionStream("gzip"));
|
|
413
|
+
return new Uint8Array(await new Response(stream).arrayBuffer());
|
|
414
|
+
}
|
|
181
415
|
function warnNoop(message) {
|
|
182
416
|
console.warn(`@uselemma/tracing: ${message}`);
|
|
183
417
|
}
|
|
@@ -223,16 +457,24 @@ class SpanHandle {
|
|
|
223
457
|
if (this.ended)
|
|
224
458
|
return;
|
|
225
459
|
this.ended = true;
|
|
226
|
-
|
|
460
|
+
const next = normalizeSpan({
|
|
227
461
|
...this.options,
|
|
228
462
|
...options,
|
|
229
463
|
id: this.id,
|
|
230
464
|
startedAt: this.options.startedAt,
|
|
231
465
|
endedAt: options.endedAt ?? new Date(),
|
|
232
|
-
//
|
|
233
|
-
//
|
|
234
|
-
|
|
235
|
-
|
|
466
|
+
// Prefer a resolved response model passed to end() over start-time model,
|
|
467
|
+
// without letting undefined/blank erase start-time model or letting
|
|
468
|
+
// deprecated llmModelName override an explicit model.
|
|
469
|
+
model: firstNonBlank(options.model, this.options.model, options.llmModelName, this.options.llmModelName),
|
|
470
|
+
// Prefer end-time provider when non-blank, otherwise keep start-time provider.
|
|
471
|
+
provider: firstTrimmed(options.provider, options.llmProvider, options.llmSystem, this.options.provider, this.options.llmProvider, this.options.llmSystem),
|
|
472
|
+
// Keep a start-time toolCallId when end() omits or blanks it; fill in
|
|
473
|
+
// at end when unset (e.g. OpenAI Agents recovering call_id on span end).
|
|
474
|
+
toolCallId: (0, tool_call_id_1.normalizeToolCallId)(this.options.toolCallId) ??
|
|
475
|
+
(0, tool_call_id_1.normalizeToolCallId)(options.toolCallId),
|
|
476
|
+
}, this.options.type ?? "span");
|
|
477
|
+
replaceSpanPayload(this.payload, next);
|
|
236
478
|
this.trace.spanEnded(this.payload);
|
|
237
479
|
this.trace.changed();
|
|
238
480
|
}
|
|
@@ -254,7 +496,7 @@ class SpanHandle {
|
|
|
254
496
|
/**
|
|
255
497
|
* Fill input or output after end() when a child model call finishes later.
|
|
256
498
|
* Input is written only while this span still has none. Output is replaced
|
|
257
|
-
* when the span did not carry its own output.
|
|
499
|
+
* when the span did not carry its own output and did not fail.
|
|
258
500
|
*/
|
|
259
501
|
fillContainerIo(input, output, ownOutput) {
|
|
260
502
|
if (!this.ended)
|
|
@@ -262,7 +504,8 @@ class SpanHandle {
|
|
|
262
504
|
if (this.payload.input === undefined && input !== undefined) {
|
|
263
505
|
this.payload.input = input;
|
|
264
506
|
}
|
|
265
|
-
|
|
507
|
+
const isError = this.payload.status === "ERROR" || this.payload.error !== undefined;
|
|
508
|
+
if (!ownOutput && !isError && output !== undefined) {
|
|
266
509
|
this.payload.output = output;
|
|
267
510
|
}
|
|
268
511
|
this.trace.changed();
|
|
@@ -307,6 +550,34 @@ class SpanHandle {
|
|
|
307
550
|
parentId: toolOptions.parentId ?? this.id,
|
|
308
551
|
});
|
|
309
552
|
}
|
|
553
|
+
startRetrieval(options) {
|
|
554
|
+
const retrievalOptions = typeof options === "string" ? { name: options } : options;
|
|
555
|
+
return this.trace.startRetrieval({
|
|
556
|
+
...retrievalOptions,
|
|
557
|
+
parentId: retrievalOptions.parentId ?? this.id,
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
startEmbedding(options) {
|
|
561
|
+
const embeddingOptions = typeof options === "string" ? { name: options } : options;
|
|
562
|
+
return this.trace.startEmbedding({
|
|
563
|
+
...embeddingOptions,
|
|
564
|
+
parentId: embeddingOptions.parentId ?? this.id,
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
startReranker(options) {
|
|
568
|
+
const rerankerOptions = typeof options === "string" ? { name: options } : options;
|
|
569
|
+
return this.trace.startReranker({
|
|
570
|
+
...rerankerOptions,
|
|
571
|
+
parentId: rerankerOptions.parentId ?? this.id,
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
startGuardrail(options) {
|
|
575
|
+
const guardrailOptions = typeof options === "string" ? { name: options } : options;
|
|
576
|
+
return this.trace.startGuardrail({
|
|
577
|
+
...guardrailOptions,
|
|
578
|
+
parentId: guardrailOptions.parentId ?? this.id,
|
|
579
|
+
});
|
|
580
|
+
}
|
|
310
581
|
recordSpan(options) {
|
|
311
582
|
const spanOptions = typeof options === "string" ? { name: options } : options;
|
|
312
583
|
return this.trace.recordSpan({
|
|
@@ -328,6 +599,34 @@ class SpanHandle {
|
|
|
328
599
|
parentId: toolOptions.parentId ?? this.id,
|
|
329
600
|
});
|
|
330
601
|
}
|
|
602
|
+
recordRetrieval(options) {
|
|
603
|
+
const retrievalOptions = typeof options === "string" ? { name: options } : options;
|
|
604
|
+
this.trace.recordRetrieval({
|
|
605
|
+
...retrievalOptions,
|
|
606
|
+
parentId: retrievalOptions.parentId ?? this.id,
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
recordEmbedding(options) {
|
|
610
|
+
const embeddingOptions = typeof options === "string" ? { name: options } : options;
|
|
611
|
+
this.trace.recordEmbedding({
|
|
612
|
+
...embeddingOptions,
|
|
613
|
+
parentId: embeddingOptions.parentId ?? this.id,
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
recordReranker(options) {
|
|
617
|
+
const rerankerOptions = typeof options === "string" ? { name: options } : options;
|
|
618
|
+
this.trace.recordReranker({
|
|
619
|
+
...rerankerOptions,
|
|
620
|
+
parentId: rerankerOptions.parentId ?? this.id,
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
recordGuardrail(options) {
|
|
624
|
+
const guardrailOptions = typeof options === "string" ? { name: options } : options;
|
|
625
|
+
this.trace.recordGuardrail({
|
|
626
|
+
...guardrailOptions,
|
|
627
|
+
parentId: guardrailOptions.parentId ?? this.id,
|
|
628
|
+
});
|
|
629
|
+
}
|
|
331
630
|
span(options) {
|
|
332
631
|
return typeof options === "string"
|
|
333
632
|
? this.startSpan(options)
|
|
@@ -358,11 +657,27 @@ class NoopSpanHandle {
|
|
|
358
657
|
startTool() {
|
|
359
658
|
return this;
|
|
360
659
|
}
|
|
660
|
+
startRetrieval() {
|
|
661
|
+
return this;
|
|
662
|
+
}
|
|
663
|
+
startEmbedding() {
|
|
664
|
+
return this;
|
|
665
|
+
}
|
|
666
|
+
startReranker() {
|
|
667
|
+
return this;
|
|
668
|
+
}
|
|
669
|
+
startGuardrail() {
|
|
670
|
+
return this;
|
|
671
|
+
}
|
|
361
672
|
recordSpan() {
|
|
362
673
|
return this;
|
|
363
674
|
}
|
|
364
675
|
recordGeneration() { }
|
|
365
676
|
recordTool() { }
|
|
677
|
+
recordRetrieval() { }
|
|
678
|
+
recordEmbedding() { }
|
|
679
|
+
recordReranker() { }
|
|
680
|
+
recordGuardrail() { }
|
|
366
681
|
span() {
|
|
367
682
|
return this;
|
|
368
683
|
}
|
|
@@ -377,7 +692,8 @@ class TraceContext {
|
|
|
377
692
|
spanHandles = new Map();
|
|
378
693
|
/** Boxed, so an output set to undefined still counts as set. */
|
|
379
694
|
rootOutput;
|
|
380
|
-
traceError
|
|
695
|
+
traceError;
|
|
696
|
+
traceStatus;
|
|
381
697
|
/** Set by {@link markPayloadFinal}; cleared by the first change after it. */
|
|
382
698
|
sent = false;
|
|
383
699
|
id;
|
|
@@ -389,6 +705,8 @@ class TraceContext {
|
|
|
389
705
|
this.id = this.options.id;
|
|
390
706
|
this.rootOutput =
|
|
391
707
|
options.output === undefined ? undefined : { value: options.output };
|
|
708
|
+
this.traceError = (0, error_message_1.failureMessage)(options.error) ?? undefined;
|
|
709
|
+
this.traceStatus = options.status;
|
|
392
710
|
}
|
|
393
711
|
input(value) {
|
|
394
712
|
this.options.input = value;
|
|
@@ -414,12 +732,20 @@ class TraceContext {
|
|
|
414
732
|
this.options.userId = value;
|
|
415
733
|
this.changed();
|
|
416
734
|
}
|
|
735
|
+
environment(value) {
|
|
736
|
+
this.options.environment = value;
|
|
737
|
+
this.changed();
|
|
738
|
+
}
|
|
739
|
+
tags(value) {
|
|
740
|
+
this.options.tags = value;
|
|
741
|
+
this.changed();
|
|
742
|
+
}
|
|
417
743
|
duration(durationMs) {
|
|
418
744
|
this.options.durationMs = durationMs;
|
|
419
745
|
this.changed();
|
|
420
746
|
}
|
|
421
747
|
fail(error) {
|
|
422
|
-
this.traceError = (0, error_message_1.failureMessage)(error);
|
|
748
|
+
this.traceError = (0, error_message_1.failureMessage)(error) ?? undefined;
|
|
423
749
|
this.changed();
|
|
424
750
|
}
|
|
425
751
|
/** Root identity for a versioned turn context token. */
|
|
@@ -505,6 +831,34 @@ class TraceContext {
|
|
|
505
831
|
this.debugSpan("span recorded", span);
|
|
506
832
|
this.changed();
|
|
507
833
|
}
|
|
834
|
+
recordRetrieval(options) {
|
|
835
|
+
const retrievalOptions = typeof options === "string" ? { name: options } : options;
|
|
836
|
+
const span = normalizeSpan({ ...retrievalOptions, type: "retrieval" }, "retrieval");
|
|
837
|
+
this.spans.push(span);
|
|
838
|
+
this.debugSpan("span recorded", span);
|
|
839
|
+
this.changed();
|
|
840
|
+
}
|
|
841
|
+
recordEmbedding(options) {
|
|
842
|
+
const embeddingOptions = typeof options === "string" ? { name: options } : options;
|
|
843
|
+
const span = normalizeSpan({ ...embeddingOptions, type: "embedding" }, "embedding");
|
|
844
|
+
this.spans.push(span);
|
|
845
|
+
this.debugSpan("span recorded", span);
|
|
846
|
+
this.changed();
|
|
847
|
+
}
|
|
848
|
+
recordReranker(options) {
|
|
849
|
+
const rerankerOptions = typeof options === "string" ? { name: options } : options;
|
|
850
|
+
const span = normalizeSpan({ ...rerankerOptions, type: "reranker" }, "reranker");
|
|
851
|
+
this.spans.push(span);
|
|
852
|
+
this.debugSpan("span recorded", span);
|
|
853
|
+
this.changed();
|
|
854
|
+
}
|
|
855
|
+
recordGuardrail(options) {
|
|
856
|
+
const guardrailOptions = typeof options === "string" ? { name: options } : options;
|
|
857
|
+
const span = normalizeSpan({ ...guardrailOptions, type: "guardrail" }, "guardrail");
|
|
858
|
+
this.spans.push(span);
|
|
859
|
+
this.debugSpan("span recorded", span);
|
|
860
|
+
this.changed();
|
|
861
|
+
}
|
|
508
862
|
startSpan(options) {
|
|
509
863
|
const spanOptions = typeof options === "string" ? { name: options } : options;
|
|
510
864
|
return new SpanHandle(this, {
|
|
@@ -524,6 +878,18 @@ class TraceContext {
|
|
|
524
878
|
startedAt: options.startedAt ?? new Date(),
|
|
525
879
|
});
|
|
526
880
|
}
|
|
881
|
+
startRetrieval(options) {
|
|
882
|
+
return this.startSpan({ ...options, type: "retrieval" });
|
|
883
|
+
}
|
|
884
|
+
startEmbedding(options) {
|
|
885
|
+
return this.startSpan({ ...options, type: "embedding" });
|
|
886
|
+
}
|
|
887
|
+
startReranker(options) {
|
|
888
|
+
return this.startSpan({ ...options, type: "reranker" });
|
|
889
|
+
}
|
|
890
|
+
startGuardrail(options) {
|
|
891
|
+
return this.startSpan({ ...options, type: "guardrail" });
|
|
892
|
+
}
|
|
527
893
|
span(options) {
|
|
528
894
|
return typeof options === "string"
|
|
529
895
|
? this.startSpan(options)
|
|
@@ -549,23 +915,50 @@ class TraceContext {
|
|
|
549
915
|
return stamped;
|
|
550
916
|
}
|
|
551
917
|
toPayload(projectId, startedAt, endedAt) {
|
|
918
|
+
const error = this.traceError;
|
|
919
|
+
const status = error ? "ERROR" : this.traceStatus;
|
|
920
|
+
const isError = status === "ERROR" || error !== undefined;
|
|
921
|
+
const threadId = nonBlankString(this.options.threadId);
|
|
922
|
+
const userId = nonBlankString(this.options.userId);
|
|
923
|
+
const environment = (0, release_1.normalizeEnvironment)(this.options.environment);
|
|
924
|
+
const tags = (0, release_1.normalizeTags)(this.options.tags);
|
|
925
|
+
const durationMs = nonNegativeNumber(this.options.durationMs) ??
|
|
926
|
+
elapsedMs(startedAt, endedAt);
|
|
927
|
+
const metadata = asPlainObject(this.options.metadata);
|
|
928
|
+
const trace = {
|
|
929
|
+
name: nonBlankString(this.options.name) ?? "trace",
|
|
930
|
+
started_at: startedAt.toISOString(),
|
|
931
|
+
ended_at: endedAt.toISOString(),
|
|
932
|
+
spans: this.spans,
|
|
933
|
+
};
|
|
934
|
+
const id = nonBlankString(this.options.id);
|
|
935
|
+
if (id !== undefined)
|
|
936
|
+
trace.id = id;
|
|
937
|
+
if (this.options.input !== undefined)
|
|
938
|
+
trace.input = this.options.input;
|
|
939
|
+
if (!isError && this.rootOutput?.value !== undefined) {
|
|
940
|
+
trace.output = this.rootOutput.value;
|
|
941
|
+
}
|
|
942
|
+
if (status !== undefined)
|
|
943
|
+
trace.status = status;
|
|
944
|
+
if (error !== undefined)
|
|
945
|
+
trace.error = error;
|
|
946
|
+
if (threadId !== undefined)
|
|
947
|
+
trace.thread_id = threadId;
|
|
948
|
+
if (userId !== undefined)
|
|
949
|
+
trace.user_id = userId;
|
|
950
|
+
if (environment !== undefined)
|
|
951
|
+
trace.environment = environment;
|
|
952
|
+
if (tags !== undefined)
|
|
953
|
+
trace.tags = tags;
|
|
954
|
+
if (durationMs !== undefined)
|
|
955
|
+
trace.duration_ms = durationMs;
|
|
956
|
+
if (metadata !== undefined)
|
|
957
|
+
trace.metadata = metadata;
|
|
552
958
|
return {
|
|
959
|
+
schema_version: exports.TRACE_CONTRACT_SCHEMA_VERSION,
|
|
553
960
|
project_id: projectId,
|
|
554
|
-
trace
|
|
555
|
-
id: this.options.id,
|
|
556
|
-
name: this.options.name ?? "trace",
|
|
557
|
-
input: this.options.input,
|
|
558
|
-
output: this.rootOutput?.value,
|
|
559
|
-
metadata: this.options.metadata,
|
|
560
|
-
thread_id: this.options.threadId,
|
|
561
|
-
user_id: this.options.userId,
|
|
562
|
-
started_at: startedAt.toISOString(),
|
|
563
|
-
ended_at: endedAt.toISOString(),
|
|
564
|
-
duration_ms: this.options.durationMs ?? elapsedMs(startedAt, endedAt),
|
|
565
|
-
status: this.traceError ? "ERROR" : undefined,
|
|
566
|
-
error: this.traceError,
|
|
567
|
-
spans: this.spans,
|
|
568
|
-
},
|
|
961
|
+
trace,
|
|
569
962
|
};
|
|
570
963
|
}
|
|
571
964
|
}
|
|
@@ -581,27 +974,14 @@ class TraceHandle extends TraceContext {
|
|
|
581
974
|
super(options);
|
|
582
975
|
this.sendFn = sendFn;
|
|
583
976
|
this.startedAt = startedAt ?? coerceDate(options.startedAt) ?? new Date();
|
|
584
|
-
// A trace is delivered only when it is explicitly ended (`end()`). We
|
|
585
|
-
// deliberately do NOT auto-send on construction or on every recorded span:
|
|
586
|
-
// the ingest API is append-only and treats each submission's spans as new,
|
|
587
|
-
// so streaming partial snapshots re-sends every prior span and accumulates
|
|
588
|
-
// duplicates. Recording mutates the in-memory trace only; the single
|
|
589
|
-
// terminal send from `end()` carries the complete trace.
|
|
590
977
|
}
|
|
591
978
|
end(outputOrOptions) {
|
|
592
|
-
// A second end() returns the first send, so a delivered trace's spans
|
|
593
|
-
// are never sent twice. Only a failed strict send lets end() send again.
|
|
594
979
|
if (this.sending)
|
|
595
980
|
return this.sending;
|
|
596
981
|
this.rootTiming ??= this.applyEnd(outputOrOptions, arguments.length > 0);
|
|
597
982
|
const { startedAt, endedAt } = this.rootTiming;
|
|
598
|
-
// Started now, not on a later tick, so a flush() called right after
|
|
599
|
-
// end() already waits for this send.
|
|
600
983
|
const sending = this.startSend(startedAt, endedAt);
|
|
601
984
|
this.sending = sending;
|
|
602
|
-
// Registered before the caller's own handler, so a caller that catches
|
|
603
|
-
// the failure can call end() again at once. It also observes the
|
|
604
|
-
// rejection, so an end() nobody awaits is not an unhandled rejection.
|
|
605
985
|
sending.catch(() => {
|
|
606
986
|
this.sending = undefined;
|
|
607
987
|
});
|
|
@@ -656,6 +1036,12 @@ class Lemma {
|
|
|
656
1036
|
/** Identity and credentials on every request; spread last so nothing overrides them. */
|
|
657
1037
|
baseHeaders;
|
|
658
1038
|
release;
|
|
1039
|
+
environment;
|
|
1040
|
+
tags;
|
|
1041
|
+
gzip;
|
|
1042
|
+
beforeSend;
|
|
1043
|
+
maxPayloadBytes;
|
|
1044
|
+
deduplicateSpanInputs;
|
|
659
1045
|
traces = new Map();
|
|
660
1046
|
/** Sends in flight, and callback traces held open by a returned stream. */
|
|
661
1047
|
pending = new Set();
|
|
@@ -665,11 +1051,6 @@ class Lemma {
|
|
|
665
1051
|
this.projectId = required(options.projectId ?? (0, env_1.readEnv)(() => process.env.LEMMA_PROJECT_ID), "LEMMA_PROJECT_ID");
|
|
666
1052
|
this.baseUrl = (options.baseUrl ??
|
|
667
1053
|
((0, env_1.readEnv)(() => process.env.LEMMA_BASE_URL)?.trim() || debug_delivery_1.PRODUCTION_BASE_URL)).replace(/\/+$/, "");
|
|
668
|
-
// Workers' fetch throws "Illegal invocation" when it is called with a
|
|
669
|
-
// receiver other than the global object, so neither the global nor a
|
|
670
|
-
// passed-in fetch is called as a method of this client. The global is
|
|
671
|
-
// read per call, so a fetch that instrumentation installs after the
|
|
672
|
-
// client is built is the one used.
|
|
673
1054
|
const customFetch = options.fetch;
|
|
674
1055
|
this.fetchImpl = (input, init) => (customFetch ?? fetch)(input, init);
|
|
675
1056
|
this.baseHeaders = {
|
|
@@ -677,6 +1058,18 @@ class Lemma {
|
|
|
677
1058
|
Authorization: `Bearer ${this.apiKey}`,
|
|
678
1059
|
};
|
|
679
1060
|
this.release = (0, release_1.normalizeRelease)(options.release ?? (0, env_1.readEnv)(() => process.env.LEMMA_RELEASE));
|
|
1061
|
+
this.environment = (0, release_1.normalizeEnvironment)(options.environment ?? (0, env_1.readEnv)(() => process.env.LEMMA_ENVIRONMENT));
|
|
1062
|
+
this.tags = (0, release_1.normalizeTags)(options.tags);
|
|
1063
|
+
this.gzip = options.gzip === true;
|
|
1064
|
+
this.beforeSend = options.beforeSend;
|
|
1065
|
+
if (options.maxPayloadBytes !== undefined) {
|
|
1066
|
+
if (!Number.isFinite(options.maxPayloadBytes) ||
|
|
1067
|
+
options.maxPayloadBytes <= 0) {
|
|
1068
|
+
throw new Error("@uselemma/tracing: maxPayloadBytes must be > 0");
|
|
1069
|
+
}
|
|
1070
|
+
this.maxPayloadBytes = options.maxPayloadBytes;
|
|
1071
|
+
}
|
|
1072
|
+
this.deduplicateSpanInputs = options.deduplicateSpanInputs === true;
|
|
680
1073
|
this.logInitConfigOnce();
|
|
681
1074
|
}
|
|
682
1075
|
/**
|
|
@@ -703,15 +1096,16 @@ class Lemma {
|
|
|
703
1096
|
});
|
|
704
1097
|
context.output("ok");
|
|
705
1098
|
const startedAt = new Date();
|
|
706
|
-
const payload = this.
|
|
1099
|
+
const payload = this.stampClientDefaults(context.toPayload(this.projectId, startedAt, new Date()));
|
|
707
1100
|
const body = payloadBody(payload);
|
|
1101
|
+
const wireBody = this.gzip ? await gzipString(body) : body;
|
|
708
1102
|
const url = `${this.baseUrl}${debug_delivery_1.INGEST_PATH}`;
|
|
709
1103
|
let response;
|
|
710
1104
|
try {
|
|
711
|
-
response = await (0, delivery_1.withIngestTimeout)((signal) => this.postIngest(
|
|
1105
|
+
response = await (0, delivery_1.withIngestTimeout)((signal) => this.postIngest(wireBody, signal));
|
|
712
1106
|
}
|
|
713
|
-
catch {
|
|
714
|
-
hints.push(
|
|
1107
|
+
catch (error) {
|
|
1108
|
+
hints.push(`ingest request failed (network error): ${(0, delivery_1.describeSendError)(error)}`);
|
|
715
1109
|
return {
|
|
716
1110
|
ok: false,
|
|
717
1111
|
config,
|
|
@@ -722,12 +1116,8 @@ class Lemma {
|
|
|
722
1116
|
const ingestWarnings = [];
|
|
723
1117
|
if (response.status !== debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS) {
|
|
724
1118
|
ingestWarnings.push(`expected status ${debug_delivery_1.EXPECTED_INGEST_SUCCESS_STATUS}, got ${response.status}`);
|
|
725
|
-
// Best effort under its own timeout: a stalled body must neither hang
|
|
726
|
-
// the smoke test nor hide the status Lemma returned.
|
|
727
1119
|
const failureBody = await (0, delivery_1.withIngestTimeout)(() => response.text()).catch(() => "");
|
|
728
|
-
|
|
729
|
-
if (hint)
|
|
730
|
-
hints.push(hint);
|
|
1120
|
+
hints.push(...(0, delivery_1.ingestFailureHints)(response.status, failureBody));
|
|
731
1121
|
}
|
|
732
1122
|
if (!responseHeaders["cf-ray"]) {
|
|
733
1123
|
hints.push("missing cf-ray response header (may not be Lemma production)");
|
|
@@ -775,24 +1165,18 @@ class Lemma {
|
|
|
775
1165
|
result = await fn(context);
|
|
776
1166
|
}
|
|
777
1167
|
catch (error) {
|
|
778
|
-
// Only the agent's own failure belongs in this branch.
|
|
779
1168
|
context.fail(error);
|
|
780
1169
|
await this.flushTraceAutomatic(context, startedAt, new Date(), delivery_1.AWAITED_DELIVERY);
|
|
781
1170
|
throw error;
|
|
782
1171
|
}
|
|
783
1172
|
const held = (0, stream_result_1.holdTraceForStream)(result, context, () => this.flushTraceAutomatic(context, startedAt, new Date(), delivery_1.BACKGROUND_DELIVERY));
|
|
784
1173
|
if (held) {
|
|
785
|
-
// Tracked from now, so a flush() called while the stream is still
|
|
786
|
-
// being read waits for its trace.
|
|
787
1174
|
this.track(held.sent);
|
|
788
1175
|
return held.stream;
|
|
789
1176
|
}
|
|
790
1177
|
if (!context.hasOutput) {
|
|
791
1178
|
context.output(result);
|
|
792
1179
|
}
|
|
793
|
-
// Automatic delivery is outside the agent's try: a transport failure
|
|
794
|
-
// must not be recorded as an agent failure. Fail-open so Lemma being
|
|
795
|
-
// down cannot fail the caller's app; ingest() stays strict for retries.
|
|
796
1180
|
await this.flushTraceAutomatic(context, startedAt, new Date(), delivery_1.AWAITED_DELIVERY);
|
|
797
1181
|
return result;
|
|
798
1182
|
})();
|
|
@@ -802,11 +1186,6 @@ class Lemma {
|
|
|
802
1186
|
* dropped, including each callback trace whose returned stream is still
|
|
803
1187
|
* being read. Never rejects. A trace handle that hasn't been ended yet is
|
|
804
1188
|
* not waited for.
|
|
805
|
-
*
|
|
806
|
-
* Pass it to the runtime so delivery outlives the response:
|
|
807
|
-
* `ctx.waitUntil(lemma.flush())` on Cloudflare Workers,
|
|
808
|
-
* `after(() => lemma.flush())` in Next.js. On AWS Lambda, await it before
|
|
809
|
-
* the handler returns.
|
|
810
1189
|
*/
|
|
811
1190
|
async flush() {
|
|
812
1191
|
await Promise.allSettled(Array.from(this.pending));
|
|
@@ -830,8 +1209,6 @@ class Lemma {
|
|
|
830
1209
|
await this.flushTraceAutomatic(trace, startedAt, endedAt, budget);
|
|
831
1210
|
}
|
|
832
1211
|
finally {
|
|
833
|
-
// Drop the handle once ended so long-lived clients don't retain
|
|
834
|
-
// every historical TraceHandle (and its spans/payloads) forever.
|
|
835
1212
|
this.traces.delete(trace.id);
|
|
836
1213
|
}
|
|
837
1214
|
});
|
|
@@ -844,22 +1221,6 @@ class Lemma {
|
|
|
844
1221
|
}
|
|
845
1222
|
/**
|
|
846
1223
|
* Deliver a trace you assembled yourself, in a single request.
|
|
847
|
-
*
|
|
848
|
-
* This is the manual counterpart to {@link Lemma.trace}: instead of the client
|
|
849
|
-
* owning the lifecycle, you build a {@link TraceContext} — recording spans,
|
|
850
|
-
* output, and status on it — and hand it back to be sent. Use it for producers
|
|
851
|
-
* that live outside a single process (cross-process buffers, queues, batch
|
|
852
|
-
* backfills) where a long-lived handle can't be held.
|
|
853
|
-
*
|
|
854
|
-
* Deliver **one complete trace** when the execution (agent turn) finishes:
|
|
855
|
-
* root input/output, thread/user, and all child spans in a single call. This
|
|
856
|
-
* is required — patching a trace over time is not supported. Omitted root
|
|
857
|
-
* fields do not preserve prior values, and after Lemma processes the trace
|
|
858
|
-
* once, a later re-delivery does not re-run issue extraction (late new span
|
|
859
|
-
* IDs may still append for display). Retries of the same complete payload are
|
|
860
|
-
* safe: already-stored span IDs are skipped. Throws an `IngestError` on
|
|
861
|
-
* a non-2xx response, and never mutates the trace's status, so a failed send
|
|
862
|
-
* can be retried as-is.
|
|
863
1224
|
*/
|
|
864
1225
|
async ingest(context, options) {
|
|
865
1226
|
const failure = await this.sendTrace(context, options.startedAt, options.endedAt ?? new Date(), delivery_1.STRICT_DELIVERY);
|
|
@@ -969,13 +1330,6 @@ class Lemma {
|
|
|
969
1330
|
tool(options) {
|
|
970
1331
|
this.recordTool(options);
|
|
971
1332
|
}
|
|
972
|
-
traceFor(traceId) {
|
|
973
|
-
const trace = this.traces.get(traceId);
|
|
974
|
-
if (!trace) {
|
|
975
|
-
throw new Error(`@uselemma/tracing: unknown trace id "${traceId}"`);
|
|
976
|
-
}
|
|
977
|
-
return trace;
|
|
978
|
-
}
|
|
979
1333
|
detachedTraceFor(traceId, kind) {
|
|
980
1334
|
if (!traceId) {
|
|
981
1335
|
warnNoop(`${kind} handle requires traceId; skipping ${kind}`);
|
|
@@ -1002,20 +1356,28 @@ class Lemma {
|
|
|
1002
1356
|
...(warnings.length ? { warnings } : {}),
|
|
1003
1357
|
});
|
|
1004
1358
|
}
|
|
1005
|
-
|
|
1359
|
+
stampClientDefaults(payload) {
|
|
1006
1360
|
if (this.release)
|
|
1007
1361
|
payload.trace.release = this.release;
|
|
1362
|
+
if (this.environment && !payload.trace.environment) {
|
|
1363
|
+
payload.trace.environment = this.environment;
|
|
1364
|
+
}
|
|
1365
|
+
if (this.tags) {
|
|
1366
|
+
const merged = (0, release_1.normalizeTags)([
|
|
1367
|
+
...this.tags,
|
|
1368
|
+
...(payload.trace.tags ?? []),
|
|
1369
|
+
]);
|
|
1370
|
+
if (merged)
|
|
1371
|
+
payload.trace.tags = merged;
|
|
1372
|
+
}
|
|
1008
1373
|
return payload;
|
|
1009
1374
|
}
|
|
1010
1375
|
/**
|
|
1011
1376
|
* Automatic SDK delivery. Retries transient failures, then fails open with
|
|
1012
1377
|
* a rate-limited warning so Lemma being down cannot fail the caller's app.
|
|
1013
|
-
* {@link Lemma.ingest} sends once and throws, because queue/plugin producers
|
|
1014
|
-
* keep their own retry backlog.
|
|
1015
1378
|
*/
|
|
1016
1379
|
async flushTraceAutomatic(context, startedAt, endedAt, budget) {
|
|
1017
1380
|
const sending = this.sendTrace(context, startedAt, endedAt, budget);
|
|
1018
|
-
// Built by now, and never resent, so a later change isn't delivered.
|
|
1019
1381
|
context.markPayloadFinal();
|
|
1020
1382
|
const failure = await sending;
|
|
1021
1383
|
if (failure)
|
|
@@ -1026,19 +1388,18 @@ class Lemma {
|
|
|
1026
1388
|
return this.track(this.buildAndSend(context, startedAt, endedAt, budget));
|
|
1027
1389
|
}
|
|
1028
1390
|
/**
|
|
1029
|
-
* Build, send, and check one trace; undefined when it was delivered
|
|
1030
|
-
*
|
|
1031
|
-
* automatic delivery handle it once.
|
|
1391
|
+
* Build, send, and check one trace; undefined when it was delivered or
|
|
1392
|
+
* intentionally dropped by `beforeSend`.
|
|
1032
1393
|
*/
|
|
1033
1394
|
async buildAndSend(context, startedAt, endedAt, budget) {
|
|
1034
|
-
const built = this.buildIngestBody(context, startedAt, endedAt);
|
|
1395
|
+
const built = await this.buildIngestBody(context, startedAt, endedAt);
|
|
1035
1396
|
if (!built.ok)
|
|
1036
1397
|
return built;
|
|
1037
|
-
|
|
1398
|
+
if (built.dropped)
|
|
1399
|
+
return undefined;
|
|
1400
|
+
const result = await (0, delivery_1.deliver)((signal) => this.postIngest(built.wireBody, signal), { traceId: context.id, projectId: this.projectId }, budget);
|
|
1038
1401
|
if (!result.ok)
|
|
1039
1402
|
return result;
|
|
1040
|
-
// Debug-only checks; the trace is already stored, so a failure here
|
|
1041
|
-
// must neither fail the caller nor count as a dropped trace.
|
|
1042
1403
|
try {
|
|
1043
1404
|
await this.afterTraceSent(built.payload, result.response);
|
|
1044
1405
|
}
|
|
@@ -1048,23 +1409,51 @@ class Lemma {
|
|
|
1048
1409
|
return undefined;
|
|
1049
1410
|
}
|
|
1050
1411
|
/**
|
|
1051
|
-
*
|
|
1052
|
-
*
|
|
1412
|
+
* Build, shape (`deduplicateSpanInputs`, `beforeSend`, `maxPayloadBytes`),
|
|
1413
|
+
* and serialize once so retries send identical bytes.
|
|
1053
1414
|
*/
|
|
1054
|
-
buildIngestBody(context, startedAt, endedAt) {
|
|
1415
|
+
async buildIngestBody(context, startedAt, endedAt) {
|
|
1055
1416
|
try {
|
|
1056
|
-
const
|
|
1417
|
+
const rawPayload = this.stampClientDefaults(context.toPayload(this.projectId, startedAt, endedAt));
|
|
1418
|
+
const needsTransform = this.deduplicateSpanInputs ||
|
|
1419
|
+
this.beforeSend !== undefined ||
|
|
1420
|
+
this.maxPayloadBytes !== undefined;
|
|
1421
|
+
// Clone container objects before dedup/beforeSend/cap so live TraceContext
|
|
1422
|
+
// spans and caller-owned metadata are never mutated, without double-running
|
|
1423
|
+
// stringifyTraceJson's string cap.
|
|
1424
|
+
let payload = needsTransform ? cloneForTransform(rawPayload) : rawPayload;
|
|
1425
|
+
if (this.deduplicateSpanInputs) {
|
|
1426
|
+
payload = (0, payload_1.applySpanContentDedup)(payload);
|
|
1427
|
+
}
|
|
1428
|
+
if (this.beforeSend) {
|
|
1429
|
+
const next = (0, payload_1.applyBeforeSend)(payload, this.beforeSend);
|
|
1430
|
+
if (next === null) {
|
|
1431
|
+
(0, debug_mode_1.lemmaDebug)("client", "trace dropped by beforeSend", {
|
|
1432
|
+
traceId: context.id,
|
|
1433
|
+
projectId: this.projectId,
|
|
1434
|
+
});
|
|
1435
|
+
return { ok: true, dropped: true };
|
|
1436
|
+
}
|
|
1437
|
+
payload = next;
|
|
1438
|
+
}
|
|
1439
|
+
if (this.maxPayloadBytes !== undefined) {
|
|
1440
|
+
payload = (0, payload_1.applyPayloadCap)(payload, this.maxPayloadBytes);
|
|
1441
|
+
}
|
|
1057
1442
|
const body = payloadBody(payload);
|
|
1443
|
+
const wireBody = this.gzip ? await gzipString(body) : body;
|
|
1058
1444
|
(0, debug_mode_1.lemmaDebug)("client", "sending trace", {
|
|
1059
1445
|
traceId: payload.trace.id,
|
|
1060
1446
|
name: payload.trace.name,
|
|
1061
1447
|
spanCount: payload.trace.spans.length,
|
|
1062
1448
|
projectId: payload.project_id,
|
|
1063
|
-
bodyBytes:
|
|
1449
|
+
bodyBytes: typeof wireBody === "string"
|
|
1450
|
+
? new TextEncoder().encode(wireBody).length
|
|
1451
|
+
: wireBody.byteLength,
|
|
1452
|
+
...(this.gzip ? { contentEncoding: "gzip" } : {}),
|
|
1064
1453
|
requestedAt: new Date().toISOString(),
|
|
1065
1454
|
url: `${this.baseUrl}${debug_delivery_1.INGEST_PATH}`,
|
|
1066
1455
|
});
|
|
1067
|
-
return { ok: true, payload,
|
|
1456
|
+
return { ok: true, dropped: false, payload, wireBody };
|
|
1068
1457
|
}
|
|
1069
1458
|
catch (error) {
|
|
1070
1459
|
(0, debug_mode_1.lemmaDebug)("client", "trace ingest failed", {
|
|
@@ -1076,10 +1465,15 @@ class Lemma {
|
|
|
1076
1465
|
}
|
|
1077
1466
|
}
|
|
1078
1467
|
postIngest(body, signal) {
|
|
1468
|
+
const headers = {
|
|
1469
|
+
"Content-Type": "application/json",
|
|
1470
|
+
...(this.gzip ? { "Content-Encoding": "gzip" } : {}),
|
|
1471
|
+
...this.baseHeaders,
|
|
1472
|
+
};
|
|
1079
1473
|
return this.fetchImpl(`${this.baseUrl}${debug_delivery_1.INGEST_PATH}`, {
|
|
1080
1474
|
method: "POST",
|
|
1081
|
-
headers
|
|
1082
|
-
body,
|
|
1475
|
+
headers,
|
|
1476
|
+
body: body,
|
|
1083
1477
|
signal,
|
|
1084
1478
|
});
|
|
1085
1479
|
}
|