weflayr 0.23.0 → 0.26.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 +2 -2
- package/dist/{api → src/api}/generated/facade.gen.d.ts +163 -12
- package/dist/{api → src/api}/generated/facade.gen.js +10 -2
- package/dist/src/api/generated/index.d.ts +2 -0
- package/dist/{api → src/api}/generated/index.js +9 -1
- package/dist/{api → src/api}/generated/sdk.gen.d.ts +35 -3
- package/dist/{api → src/api}/generated/sdk.gen.js +88 -4
- package/{src/api/generated/types.gen.ts → dist/src/api/generated/types.gen.d.ts} +692 -116
- package/dist/{api → src/api}/makeEndpoint.js +1 -3
- package/dist/src/auto-instrument.d.ts +42 -0
- package/dist/src/auto-instrument.js +156 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.js +49 -0
- package/dist/src/instrumentation/anthropic/get_cache_and_reasoning_telemetry.d.ts +20 -0
- package/dist/src/instrumentation/anthropic/get_cache_and_reasoning_telemetry.js +114 -0
- package/dist/src/instrumentation/anthropic/get_cache_config_telemetry.d.ts +28 -0
- package/dist/src/instrumentation/anthropic/get_cache_config_telemetry.js +92 -0
- package/dist/src/instrumentation/anthropic/get_streaming_accumulation_telemetry.d.ts +25 -0
- package/dist/src/instrumentation/anthropic/get_streaming_accumulation_telemetry.js +141 -0
- package/dist/src/instrumentation/anthropic/get_structured_output_schema_telemetry.d.ts +9 -0
- package/dist/src/instrumentation/anthropic/get_structured_output_schema_telemetry.js +17 -0
- package/dist/src/instrumentation/anthropic/get_tool_definitions_telemetry.d.ts +15 -0
- package/dist/src/instrumentation/anthropic/get_tool_definitions_telemetry.js +50 -0
- package/dist/src/instrumentation/anthropic/set_telemetry_attributes.d.ts +15 -0
- package/dist/src/instrumentation/anthropic/set_telemetry_attributes.js +39 -0
- package/dist/src/instrumentation/bedrock/get_cache_config_telemetry.d.ts +22 -0
- package/dist/src/instrumentation/bedrock/get_cache_config_telemetry.js +97 -0
- package/dist/src/instrumentation/bedrock/get_cache_telemetry.d.ts +22 -0
- package/dist/src/instrumentation/bedrock/get_cache_telemetry.js +102 -0
- package/dist/src/instrumentation/bedrock/get_input_message_content_telemetry.d.ts +15 -0
- package/dist/src/instrumentation/bedrock/get_input_message_content_telemetry.js +91 -0
- package/dist/src/instrumentation/bedrock/get_model_id_telemetry.d.ts +21 -0
- package/dist/src/instrumentation/bedrock/get_model_id_telemetry.js +54 -0
- package/dist/src/instrumentation/bedrock/get_reasoning_output_telemetry.d.ts +19 -0
- package/dist/src/instrumentation/bedrock/get_reasoning_output_telemetry.js +81 -0
- package/dist/src/instrumentation/bedrock/get_streaming_output_message_telemetry.d.ts +27 -0
- package/dist/src/instrumentation/bedrock/get_streaming_output_message_telemetry.js +126 -0
- package/dist/src/instrumentation/bedrock/get_tool_call_output_telemetry.d.ts +17 -0
- package/dist/src/instrumentation/bedrock/get_tool_call_output_telemetry.js +85 -0
- package/dist/src/instrumentation/bedrock/get_tool_definitions_telemetry.d.ts +15 -0
- package/dist/src/instrumentation/bedrock/get_tool_definitions_telemetry.js +68 -0
- package/dist/src/instrumentation/bedrock/set_telemetry_attributes.d.ts +18 -0
- package/dist/src/instrumentation/bedrock/set_telemetry_attributes.js +47 -0
- package/dist/src/instrumentation/cohere/get_chat_telemetry.d.ts +23 -0
- package/dist/src/instrumentation/cohere/get_chat_telemetry.js +77 -0
- package/dist/src/instrumentation/cohere/get_embeddings_telemetry.d.ts +18 -0
- package/dist/src/instrumentation/cohere/get_embeddings_telemetry.js +88 -0
- package/dist/src/instrumentation/cohere/get_structured_output_schema_telemetry.d.ts +12 -0
- package/dist/src/instrumentation/cohere/get_structured_output_schema_telemetry.js +18 -0
- package/dist/src/instrumentation/cohere/get_tool_call_output_telemetry.d.ts +31 -0
- package/dist/src/instrumentation/cohere/get_tool_call_output_telemetry.js +106 -0
- package/dist/src/instrumentation/cohere/get_tool_definitions_telemetry.d.ts +27 -0
- package/dist/src/instrumentation/cohere/get_tool_definitions_telemetry.js +75 -0
- package/dist/src/instrumentation/cohere/set_telemetry_attributes.d.ts +17 -0
- package/dist/src/instrumentation/cohere/set_telemetry_attributes.js +44 -0
- package/dist/src/instrumentation/elevenlabs/full_instrumentation.d.ts +28 -0
- package/dist/src/instrumentation/elevenlabs/full_instrumentation.js +209 -0
- package/dist/src/instrumentation/google/get_audio_telemetry.d.ts +9 -0
- package/dist/src/instrumentation/google/get_audio_telemetry.js +28 -0
- package/dist/src/instrumentation/google/get_cache_operations_telemetry.d.ts +23 -0
- package/dist/src/instrumentation/google/get_cache_operations_telemetry.js +154 -0
- package/dist/src/instrumentation/google/get_cache_telemetry.d.ts +16 -0
- package/dist/src/instrumentation/google/get_cache_telemetry.js +38 -0
- package/dist/src/instrumentation/google/get_embeddings_telemetry.d.ts +12 -0
- package/dist/src/instrumentation/google/get_embeddings_telemetry.js +85 -0
- package/dist/src/instrumentation/google/get_provider_name.d.ts +11 -0
- package/{src → dist/src}/instrumentation/google/get_provider_name.js +11 -12
- package/{src/instrumentation/google/get_reasoning_telemetry.js → dist/src/instrumentation/google/get_reasoning_telemetry.d.ts} +1 -11
- package/dist/src/instrumentation/google/get_reasoning_telemetry.js +21 -0
- package/dist/src/instrumentation/google/get_structured_output_schema_telemetry.d.ts +12 -0
- package/dist/src/instrumentation/google/get_structured_output_schema_telemetry.js +19 -0
- package/dist/src/instrumentation/google/set_telemetry_attributes.d.ts +18 -0
- package/dist/src/instrumentation/google/set_telemetry_attributes.js +98 -0
- package/dist/src/instrumentation/openai/get_audio_telemetry.d.ts +25 -0
- package/dist/src/instrumentation/openai/get_audio_telemetry.js +123 -0
- package/dist/src/instrumentation/openai/get_bedrock_mantle_telemetry.d.ts +35 -0
- package/dist/src/instrumentation/openai/get_bedrock_mantle_telemetry.js +148 -0
- package/dist/src/instrumentation/openai/get_cache_and_reasoning_telemetry.d.ts +27 -0
- package/dist/src/instrumentation/openai/get_cache_and_reasoning_telemetry.js +72 -0
- package/dist/src/instrumentation/openai/get_cache_config_telemetry.d.ts +28 -0
- package/dist/src/instrumentation/openai/get_cache_config_telemetry.js +91 -0
- package/dist/src/instrumentation/openai/get_embeddings_telemetry.d.ts +18 -0
- package/dist/src/instrumentation/openai/get_embeddings_telemetry.js +86 -0
- package/dist/src/instrumentation/openai/get_streaming_usage.d.ts +21 -0
- package/dist/src/instrumentation/openai/get_streaming_usage.js +74 -0
- package/dist/src/instrumentation/openai/get_structured_output_schema_telemetry.d.ts +12 -0
- package/dist/src/instrumentation/openai/get_structured_output_schema_telemetry.js +20 -0
- package/dist/src/instrumentation/openai/set_telemetry_attributes.d.ts +17 -0
- package/dist/src/instrumentation/openai/set_telemetry_attributes.js +47 -0
- package/dist/src/instrumentation/shared/get_structured_output_schema_telemetry.d.ts +35 -0
- package/dist/src/instrumentation/shared/get_structured_output_schema_telemetry.js +76 -0
- package/dist/src/instrumentation/vercel/full_instrumentation.d.ts +3 -0
- package/dist/src/instrumentation/vercel/full_instrumentation.js +54 -0
- package/dist/src/instrumentation/vercel/get_cache_creation_telemetry.d.ts +22 -0
- package/dist/src/instrumentation/vercel/get_cache_creation_telemetry.js +66 -0
- package/dist/src/instrumentation/vercel/get_structured_output_schema_telemetry.d.ts +24 -0
- package/dist/src/instrumentation/vercel/get_structured_output_schema_telemetry.js +84 -0
- package/dist/src/logger.d.ts +1 -0
- package/dist/src/logger.js +6 -0
- package/dist/src/otel/exporter.d.ts +19 -0
- package/dist/src/otel/exporter.js +94 -0
- package/dist/src/otel/propagation.d.ts +46 -0
- package/dist/src/otel/propagation.js +110 -0
- package/dist/src/otel/span-filter.d.ts +3 -0
- package/dist/src/otel/span-filter.js +61 -0
- package/dist/src/otel/span-processor.d.ts +55 -0
- package/dist/src/otel/span-processor.js +169 -0
- package/dist/src/otel/vercel-ai-sdk-span-filter.d.ts +12 -0
- package/dist/src/otel/vercel-ai-sdk-span-filter.js +88 -0
- package/dist/src/setup.d.ts +50 -0
- package/dist/src/setup.js +155 -0
- package/package.json +11 -5
- package/.prettierignore +0 -2
- package/.prettierrc.json +0 -7
- package/LICENSE-MIT +0 -21
- package/NOTICE +0 -12
- package/dist/api/generated/index.d.ts +0 -2
- package/dist/api/generated/types.gen.d.ts +0 -749
- package/eslint.config.mjs +0 -36
- package/index.d.ts +0 -167
- package/openapi-ts.config.mjs +0 -9
- package/scripts/generate_facade.mjs +0 -86
- package/src/api/caseConversion.ts +0 -58
- package/src/api/generated/client/client.gen.ts +0 -277
- package/src/api/generated/client/index.ts +0 -27
- package/src/api/generated/client/types.gen.ts +0 -218
- package/src/api/generated/client/utils.gen.ts +0 -316
- package/src/api/generated/client.gen.ts +0 -16
- package/src/api/generated/core/auth.gen.ts +0 -48
- package/src/api/generated/core/bodySerializer.gen.ts +0 -82
- package/src/api/generated/core/params.gen.ts +0 -178
- package/src/api/generated/core/pathSerializer.gen.ts +0 -171
- package/src/api/generated/core/queryKeySerializer.gen.ts +0 -117
- package/src/api/generated/core/serverSentEvents.gen.ts +0 -242
- package/src/api/generated/core/types.gen.ts +0 -110
- package/src/api/generated/core/utils.gen.ts +0 -140
- package/src/api/generated/facade.gen.ts +0 -68
- package/src/api/generated/index.ts +0 -4
- package/src/api/generated/sdk.gen.ts +0 -121
- package/src/api/index.ts +0 -45
- package/src/api/makeEndpoint.ts +0 -54
- package/src/auto-instrument.js +0 -171
- package/src/index.js +0 -19
- package/src/instrumentation/anthropic/get_cache_and_reasoning_telemetry.js +0 -107
- package/src/instrumentation/anthropic/get_streaming_stop_reason_telemetry.js +0 -121
- package/src/instrumentation/anthropic/get_tool_definitions_telemetry.js +0 -50
- package/src/instrumentation/anthropic/set_telemetry_attributes.js +0 -37
- package/src/instrumentation/bedrock/get_cache_telemetry.js +0 -104
- package/src/instrumentation/bedrock/get_input_message_content_telemetry.js +0 -94
- package/src/instrumentation/bedrock/get_streaming_output_message_telemetry.js +0 -87
- package/src/instrumentation/bedrock/set_telemetry_attributes.js +0 -36
- package/src/instrumentation/cohere/get_chat_telemetry.js +0 -88
- package/src/instrumentation/cohere/get_embeddings_telemetry.js +0 -107
- package/src/instrumentation/cohere/set_telemetry_attributes.js +0 -32
- package/src/instrumentation/elevenlabs/full_instrumentation.js +0 -236
- package/src/instrumentation/google/get_audio_telemetry.js +0 -30
- package/src/instrumentation/google/get_cache_telemetry.js +0 -25
- package/src/instrumentation/google/get_embeddings_telemetry.js +0 -99
- package/src/instrumentation/google/set_telemetry_attributes.js +0 -98
- package/src/instrumentation/openai/get_audio_telemetry.js +0 -149
- package/src/instrumentation/openai/get_cache_and_reasoning_telemetry.js +0 -71
- package/src/instrumentation/openai/get_embeddings_telemetry.js +0 -107
- package/src/instrumentation/openai/get_streaming_usage.js +0 -81
- package/src/instrumentation/openai/set_telemetry_attributes.js +0 -39
- package/src/instrumentation/vercel/full_instrumentation.js +0 -44
- package/src/logger.js +0 -8
- package/src/otel/exporter.js +0 -108
- package/src/otel/propagation.js +0 -133
- package/src/otel/span-filter.js +0 -69
- package/src/otel/span-processor.js +0 -162
- package/src/otel/vercel-ai-sdk-span-filter.js +0 -79
- package/src/setup.js +0 -167
- package/tests/api/index.test.js +0 -437
- package/tests/index.test.js +0 -824
- package/tests/instrumentation/anthropic/get_cache_and_reasoning_telemetry.test.js +0 -158
- package/tests/instrumentation/anthropic/get_streaming_stop_reason_telemetry.test.js +0 -117
- package/tests/instrumentation/anthropic/get_tool_definitions_telemetry.test.js +0 -82
- package/tests/instrumentation/bedrock/get_cache_telemetry.test.js +0 -102
- package/tests/instrumentation/bedrock/get_input_message_content_telemetry.test.js +0 -89
- package/tests/instrumentation/bedrock/get_streaming_output_message_telemetry.test.js +0 -87
- package/tests/instrumentation/cohere/get_chat_telemetry.test.js +0 -148
- package/tests/instrumentation/cohere/get_embeddings_telemetry.test.js +0 -132
- package/tests/instrumentation/elevenlabs/full_instrumentation.test.js +0 -258
- package/tests/instrumentation/google/get_embeddings_telemetry.test.js +0 -128
- package/tests/instrumentation/google/get_reasoning_telemetry.test.js +0 -41
- package/tests/instrumentation/google/set_telemetry_attributes.test.js +0 -219
- package/tests/instrumentation/openai/get_audio_telemetry.test.js +0 -170
- package/tests/instrumentation/openai/get_cache_and_reasoning_telemetry.test.js +0 -83
- package/tests/instrumentation/openai/get_embeddings_telemetry.test.js +0 -145
- package/tests/instrumentation/openai/get_streaming_usage.test.js +0 -105
- package/tests/instrumentation/vercel/full_instrumentation.test.js +0 -16
- package/tests/otel/vercel-ai-sdk-span-filter.test.js +0 -159
- package/tsconfig.json +0 -18
- /package/dist/{api → src/api}/caseConversion.d.ts +0 -0
- /package/dist/{api → src/api}/caseConversion.js +0 -0
- /package/dist/{api → src/api}/generated/client/client.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/client/client.gen.js +0 -0
- /package/dist/{api → src/api}/generated/client/index.d.ts +0 -0
- /package/dist/{api → src/api}/generated/client/index.js +0 -0
- /package/dist/{api → src/api}/generated/client/types.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/client/types.gen.js +0 -0
- /package/dist/{api → src/api}/generated/client/utils.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/client/utils.gen.js +0 -0
- /package/dist/{api → src/api}/generated/client.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/client.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/auth.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/auth.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/bodySerializer.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/bodySerializer.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/params.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/params.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/pathSerializer.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/pathSerializer.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/queryKeySerializer.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/queryKeySerializer.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/serverSentEvents.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/serverSentEvents.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/types.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/types.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/utils.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/utils.gen.js +0 -0
- /package/dist/{api → src/api}/generated/types.gen.js +0 -0
- /package/dist/{api → src/api}/index.d.ts +0 -0
- /package/dist/{api → src/api}/index.js +0 -0
- /package/dist/{api → src/api}/makeEndpoint.d.ts +0 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Chat token-usage enrichment complement for the Cohere v1 chat path.
|
|
4
|
+
*
|
|
5
|
+
* `@traceloop/instrumentation-cohere` creates the `cohere.chat` span but reads
|
|
6
|
+
* token usage from the obsolete `result.token_count.{prompt,response}_tokens`
|
|
7
|
+
* shape; modern cohere-ai returns usage under `meta.billedUnits` instead, so the
|
|
8
|
+
* traceloop span ends up with zero chat tokens. This thin complement backfills
|
|
9
|
+
* `gen_ai.usage.{input,output}_tokens` from `meta.billedUnits`, the same source
|
|
10
|
+
* traceloop already uses for the `generate` endpoint — bringing Node chat tokens
|
|
11
|
+
* to parity with the Python SDK (OpenLLMetry captures them natively, so there is
|
|
12
|
+
* no Python counterpart).
|
|
13
|
+
*
|
|
14
|
+
* It hooks the internal `CohereClient.prototype.__chat` (which the public `chat`
|
|
15
|
+
* delegates to synchronously) rather than the public `chat`: traceloop wraps
|
|
16
|
+
* `chat` and opens the span around it, so `__chat` runs while that span is the
|
|
17
|
+
* active span — letting this complement enrich it regardless of patch order.
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.CohereChatTokenUsageInstrumentor = void 0;
|
|
21
|
+
const api_1 = require("@opentelemetry/api");
|
|
22
|
+
function _billedUnits(result) {
|
|
23
|
+
// Public `chat` resolves to the response body, but `__chat` resolves to the
|
|
24
|
+
// Fern wrapper `{ data, rawResponse }`; read billedUnits off either shape.
|
|
25
|
+
const response = result && result.data ? result.data : result;
|
|
26
|
+
return response && response.meta ? response.meta.billedUnits : undefined;
|
|
27
|
+
}
|
|
28
|
+
function _setChatTokenUsage(span, result) {
|
|
29
|
+
const billedUnits = _billedUnits(result);
|
|
30
|
+
if (!billedUnits)
|
|
31
|
+
return;
|
|
32
|
+
if (typeof billedUnits.inputTokens === 'number') {
|
|
33
|
+
span.setAttribute('gen_ai.usage.input_tokens', billedUnits.inputTokens);
|
|
34
|
+
}
|
|
35
|
+
if (typeof billedUnits.outputTokens === 'number') {
|
|
36
|
+
span.setAttribute('gen_ai.usage.output_tokens', billedUnits.outputTokens);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
class CohereChatTokenUsageInstrumentor {
|
|
40
|
+
// [ownerPrototype, methodName, originalFunction] for uninstrument().
|
|
41
|
+
_patched = [];
|
|
42
|
+
instrument() {
|
|
43
|
+
let CohereClient;
|
|
44
|
+
try {
|
|
45
|
+
({ CohereClient } = require('cohere-ai/Client'));
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
throw new Error('weflayr.autoInstrument(COHERE): the cohere-ai client is not installed. ' +
|
|
49
|
+
'Install it with: npm install cohere-ai\n' +
|
|
50
|
+
`Underlying error: ${err.message}`);
|
|
51
|
+
}
|
|
52
|
+
this._wrap(CohereClient.prototype, '__chat');
|
|
53
|
+
}
|
|
54
|
+
uninstrument() {
|
|
55
|
+
for (const [owner, methodName, original] of this._patched) {
|
|
56
|
+
owner[methodName] = original;
|
|
57
|
+
}
|
|
58
|
+
this._patched = [];
|
|
59
|
+
}
|
|
60
|
+
_wrap(owner, methodName) {
|
|
61
|
+
const original = owner[methodName];
|
|
62
|
+
function wrapper(...args) {
|
|
63
|
+
// Captured synchronously, while traceloop's chat span is still active.
|
|
64
|
+
const span = api_1.trace.getActiveSpan();
|
|
65
|
+
const result = original.apply(this, args);
|
|
66
|
+
if (!span || !span.isRecording || !span.isRecording())
|
|
67
|
+
return result;
|
|
68
|
+
return Promise.resolve(result).then((response) => {
|
|
69
|
+
_setChatTokenUsage(span, response);
|
|
70
|
+
return response;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
owner[methodName] = wrapper;
|
|
74
|
+
this._patched.push([owner, methodName, original]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.CohereChatTokenUsageInstrumentor = CohereChatTokenUsageInstrumentor;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hand-written OTel instrumentor for the Cohere embeddings endpoint.
|
|
3
|
+
*
|
|
4
|
+
* `@traceloop/instrumentation-cohere` (the package weflayr uses for Cohere on
|
|
5
|
+
* Node) wraps chat/generate/rerank but not `embed`, so this ships inside weflayr
|
|
6
|
+
* to cover it. OpenLLMetry's cohere instrumentor doesn't cover `embed` either, so
|
|
7
|
+
* the Python SDK has a mirror complement (`cohere_embeddings.py`) shaped to
|
|
8
|
+
* normalize identically. It is registered alongside the traceloop instrumentor
|
|
9
|
+
* whenever `autoInstrument({ aiSdks: 'cohere' })` runs.
|
|
10
|
+
*
|
|
11
|
+
* Embeddings are billed per input token, so the span records the input tokens
|
|
12
|
+
* Cohere returns under `gen_ai.usage.input_tokens`; there is no completion.
|
|
13
|
+
*/
|
|
14
|
+
export declare class CohereEmbeddingsInstrumentor {
|
|
15
|
+
private _patched;
|
|
16
|
+
instrument(): void;
|
|
17
|
+
uninstrument(): void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Hand-written OTel instrumentor for the Cohere embeddings endpoint.
|
|
4
|
+
*
|
|
5
|
+
* `@traceloop/instrumentation-cohere` (the package weflayr uses for Cohere on
|
|
6
|
+
* Node) wraps chat/generate/rerank but not `embed`, so this ships inside weflayr
|
|
7
|
+
* to cover it. OpenLLMetry's cohere instrumentor doesn't cover `embed` either, so
|
|
8
|
+
* the Python SDK has a mirror complement (`cohere_embeddings.py`) shaped to
|
|
9
|
+
* normalize identically. It is registered alongside the traceloop instrumentor
|
|
10
|
+
* whenever `autoInstrument({ aiSdks: 'cohere' })` runs.
|
|
11
|
+
*
|
|
12
|
+
* Embeddings are billed per input token, so the span records the input tokens
|
|
13
|
+
* Cohere returns under `gen_ai.usage.input_tokens`; there is no completion.
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.CohereEmbeddingsInstrumentor = void 0;
|
|
17
|
+
const api_1 = require("@opentelemetry/api");
|
|
18
|
+
const TRACER_NAME = 'weflayr.instrumentation.cohere_embeddings';
|
|
19
|
+
// Matches the gen_ai.system the cohere chat instrumentors set (Python + Node).
|
|
20
|
+
const SYSTEM = 'Cohere';
|
|
21
|
+
const EMBEDDINGS_OPERATION = 'embeddings';
|
|
22
|
+
function _recordError(span, error) {
|
|
23
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: error?.message ?? String(error) });
|
|
24
|
+
span.recordException(error);
|
|
25
|
+
}
|
|
26
|
+
function _wrapEmbed(owner, patched) {
|
|
27
|
+
const original = owner.embed;
|
|
28
|
+
function wrapper(request, ...rest) {
|
|
29
|
+
const span = api_1.trace.getTracer(TRACER_NAME).startSpan(EMBEDDINGS_OPERATION);
|
|
30
|
+
span.setAttribute('gen_ai.system', SYSTEM);
|
|
31
|
+
span.setAttribute('gen_ai.operation.name', EMBEDDINGS_OPERATION);
|
|
32
|
+
if (request && request.model) {
|
|
33
|
+
span.setAttribute('gen_ai.request.model', request.model);
|
|
34
|
+
}
|
|
35
|
+
// The embedded texts are the prompt; stamp one user message per text, like
|
|
36
|
+
// the chat path. The span processor strips it when content capture is off.
|
|
37
|
+
if (request && Array.isArray(request.texts) && request.texts.length) {
|
|
38
|
+
span.setAttribute('gen_ai.input.messages', JSON.stringify(request.texts.map((text) => ({
|
|
39
|
+
role: 'user',
|
|
40
|
+
parts: [{ type: 'text', content: text }],
|
|
41
|
+
}))));
|
|
42
|
+
}
|
|
43
|
+
return Promise.resolve(original.call(this, request, ...rest)).then((response) => {
|
|
44
|
+
// Node SDK reports the billed input tokens under camelCase keys.
|
|
45
|
+
const inputTokens = response?.meta?.billedUnits?.inputTokens ?? response?.meta?.tokens?.inputTokens;
|
|
46
|
+
if (inputTokens != null) {
|
|
47
|
+
span.setAttribute('gen_ai.usage.input_tokens', inputTokens);
|
|
48
|
+
}
|
|
49
|
+
span.end();
|
|
50
|
+
return response;
|
|
51
|
+
}, (error) => {
|
|
52
|
+
_recordError(span, error);
|
|
53
|
+
span.end();
|
|
54
|
+
throw error;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
owner.embed = wrapper;
|
|
58
|
+
patched.push([owner, 'embed', original]);
|
|
59
|
+
}
|
|
60
|
+
class CohereEmbeddingsInstrumentor {
|
|
61
|
+
// [ownerPrototype, methodName, originalFunction] for uninstrument().
|
|
62
|
+
_patched = [];
|
|
63
|
+
instrument() {
|
|
64
|
+
let CohereClient;
|
|
65
|
+
let V2Client;
|
|
66
|
+
try {
|
|
67
|
+
// v1 `embed` is a prototype method on CohereClient; v2 `embed` lives on
|
|
68
|
+
// V2Client.prototype (CohereClientV2 binds it as an instance property, so
|
|
69
|
+
// it must be patched on the underlying V2Client class instead).
|
|
70
|
+
({ CohereClient } = require('cohere-ai/Client'));
|
|
71
|
+
({ V2Client } = require('cohere-ai/api/resources/v2/client/Client'));
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
throw new Error('weflayr.autoInstrument(COHERE): the cohere-ai client is not installed. ' +
|
|
75
|
+
'Install it with: npm install cohere-ai\n' +
|
|
76
|
+
`Underlying error: ${err.message}`);
|
|
77
|
+
}
|
|
78
|
+
_wrapEmbed(CohereClient.prototype, this._patched);
|
|
79
|
+
_wrapEmbed(V2Client.prototype, this._patched);
|
|
80
|
+
}
|
|
81
|
+
uninstrument() {
|
|
82
|
+
for (const [owner, methodName, original] of this._patched) {
|
|
83
|
+
owner[methodName] = original;
|
|
84
|
+
}
|
|
85
|
+
this._patched = [];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.CohereEmbeddingsInstrumentor = CohereEmbeddingsInstrumentor;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structured-output schema location for Cohere requests.
|
|
3
|
+
*
|
|
4
|
+
* Cohere pins an answer to a JSON schema with
|
|
5
|
+
* `responseFormat: { type: 'json_object', schema }`. The traceloop instrumentor
|
|
6
|
+
* records the request's model and sampling params but never the schema.
|
|
7
|
+
*
|
|
8
|
+
* The JS client sends camelCase (`responseFormat`) where the Python one sends
|
|
9
|
+
* snake_case, so both spellings are read: the instrumentor hands over whatever
|
|
10
|
+
* the caller passed.
|
|
11
|
+
*/
|
|
12
|
+
export declare function readStructuredOutputSchema(params: any): unknown;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Structured-output schema location for Cohere requests.
|
|
4
|
+
*
|
|
5
|
+
* Cohere pins an answer to a JSON schema with
|
|
6
|
+
* `responseFormat: { type: 'json_object', schema }`. The traceloop instrumentor
|
|
7
|
+
* records the request's model and sampling params but never the schema.
|
|
8
|
+
*
|
|
9
|
+
* The JS client sends camelCase (`responseFormat`) where the Python one sends
|
|
10
|
+
* snake_case, so both spellings are read: the instrumentor hands over whatever
|
|
11
|
+
* the caller passed.
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.readStructuredOutputSchema = readStructuredOutputSchema;
|
|
15
|
+
function readStructuredOutputSchema(params) {
|
|
16
|
+
const responseFormat = params && (params.responseFormat || params.response_format);
|
|
17
|
+
return responseFormat && responseFormat.schema;
|
|
18
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool-call output parts for Cohere chat spans.
|
|
3
|
+
*
|
|
4
|
+
* `@traceloop/instrumentation-cohere` records the response as legacy indexed
|
|
5
|
+
* `gen_ai.completion.0.*` attributes, which carry a role and flat text and have
|
|
6
|
+
* no way to express a tool call, so the v1 client's `response.toolCalls` (bare
|
|
7
|
+
* `{name, parameters}` entries) reach the span nowhere.
|
|
8
|
+
*
|
|
9
|
+
* When the response carries tool calls this complement stamps the whole
|
|
10
|
+
* `gen_ai.output.messages` instead, in the standard role+parts shape, so the
|
|
11
|
+
* tool call normalizes to the same `tool_call` part every other provider
|
|
12
|
+
* produces. Calls without tool calls are left to the instrumentor's own
|
|
13
|
+
* attributes: there is nothing it fails to express about them.
|
|
14
|
+
*
|
|
15
|
+
* Like the chat token-usage complement it hooks the internal
|
|
16
|
+
* `CohereClient.prototype.__chat`, which runs while traceloop's `cohere.chat`
|
|
17
|
+
* span is active — so the stamp lands on that span regardless of patch order.
|
|
18
|
+
*/
|
|
19
|
+
export declare const OUTPUT_MESSAGES_ATTRIBUTE = "gen_ai.output.messages";
|
|
20
|
+
/**
|
|
21
|
+
* The response as one assistant message, or [] when it carries no tool call
|
|
22
|
+
* (nothing the instrumentor's own attributes cannot express). The text the
|
|
23
|
+
* model emits alongside a tool call — its plan for using it — is kept as a
|
|
24
|
+
* leading text part so stamping this attribute loses nothing.
|
|
25
|
+
*/
|
|
26
|
+
export declare function buildOutputMessages(result: any): any[];
|
|
27
|
+
export declare class CohereToolCallOutputInstrumentor {
|
|
28
|
+
private _patched;
|
|
29
|
+
instrument(): void;
|
|
30
|
+
uninstrument(): void;
|
|
31
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Tool-call output parts for Cohere chat spans.
|
|
4
|
+
*
|
|
5
|
+
* `@traceloop/instrumentation-cohere` records the response as legacy indexed
|
|
6
|
+
* `gen_ai.completion.0.*` attributes, which carry a role and flat text and have
|
|
7
|
+
* no way to express a tool call, so the v1 client's `response.toolCalls` (bare
|
|
8
|
+
* `{name, parameters}` entries) reach the span nowhere.
|
|
9
|
+
*
|
|
10
|
+
* When the response carries tool calls this complement stamps the whole
|
|
11
|
+
* `gen_ai.output.messages` instead, in the standard role+parts shape, so the
|
|
12
|
+
* tool call normalizes to the same `tool_call` part every other provider
|
|
13
|
+
* produces. Calls without tool calls are left to the instrumentor's own
|
|
14
|
+
* attributes: there is nothing it fails to express about them.
|
|
15
|
+
*
|
|
16
|
+
* Like the chat token-usage complement it hooks the internal
|
|
17
|
+
* `CohereClient.prototype.__chat`, which runs while traceloop's `cohere.chat`
|
|
18
|
+
* span is active — so the stamp lands on that span regardless of patch order.
|
|
19
|
+
*/
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.CohereToolCallOutputInstrumentor = exports.OUTPUT_MESSAGES_ATTRIBUTE = void 0;
|
|
22
|
+
exports.buildOutputMessages = buildOutputMessages;
|
|
23
|
+
const api_1 = require("@opentelemetry/api");
|
|
24
|
+
exports.OUTPUT_MESSAGES_ATTRIBUTE = 'gen_ai.output.messages';
|
|
25
|
+
// Public `chat` resolves to the response body, but `__chat` resolves to the
|
|
26
|
+
// Fern wrapper `{ data, rawResponse }`; read the response off either shape.
|
|
27
|
+
function _responseBody(result) {
|
|
28
|
+
return result && result.data ? result.data : result;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The response as one assistant message, or [] when it carries no tool call
|
|
32
|
+
* (nothing the instrumentor's own attributes cannot express). The text the
|
|
33
|
+
* model emits alongside a tool call — its plan for using it — is kept as a
|
|
34
|
+
* leading text part so stamping this attribute loses nothing.
|
|
35
|
+
*/
|
|
36
|
+
function buildOutputMessages(result) {
|
|
37
|
+
const response = _responseBody(result);
|
|
38
|
+
const toolCalls = response && response.toolCalls;
|
|
39
|
+
if (!Array.isArray(toolCalls) || !toolCalls.length)
|
|
40
|
+
return [];
|
|
41
|
+
const parts = [];
|
|
42
|
+
if (response.text)
|
|
43
|
+
parts.push({ type: 'text', content: String(response.text) });
|
|
44
|
+
for (const toolCall of toolCalls) {
|
|
45
|
+
// v1 assigns no call id, so the part carries none — unlike the providers
|
|
46
|
+
// whose API returns one.
|
|
47
|
+
if (toolCall && toolCall.name) {
|
|
48
|
+
parts.push({
|
|
49
|
+
type: 'tool_call',
|
|
50
|
+
name: String(toolCall.name),
|
|
51
|
+
arguments: toolCall.parameters || {},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!parts.length)
|
|
56
|
+
return [];
|
|
57
|
+
const message = { role: 'assistant', parts };
|
|
58
|
+
if (response.finishReason)
|
|
59
|
+
message.finish_reason = String(response.finishReason);
|
|
60
|
+
return [message];
|
|
61
|
+
}
|
|
62
|
+
class CohereToolCallOutputInstrumentor {
|
|
63
|
+
// [ownerPrototype, methodName, originalFunction] for uninstrument().
|
|
64
|
+
_patched = [];
|
|
65
|
+
instrument() {
|
|
66
|
+
let CohereClient;
|
|
67
|
+
try {
|
|
68
|
+
({ CohereClient } = require('cohere-ai/Client'));
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
throw new Error('weflayr.autoInstrument(COHERE): the cohere-ai client is not installed. ' +
|
|
72
|
+
'Install it with: npm install cohere-ai\n' +
|
|
73
|
+
`Underlying error: ${err.message}`);
|
|
74
|
+
}
|
|
75
|
+
const owner = CohereClient.prototype;
|
|
76
|
+
const original = owner.__chat;
|
|
77
|
+
function wrapper(...args) {
|
|
78
|
+
// Captured synchronously, while traceloop's chat span is still active.
|
|
79
|
+
const span = api_1.trace.getActiveSpan();
|
|
80
|
+
const result = original.apply(this, args);
|
|
81
|
+
if (!span || !span.isRecording || !span.isRecording())
|
|
82
|
+
return result;
|
|
83
|
+
return Promise.resolve(result).then((response) => {
|
|
84
|
+
try {
|
|
85
|
+
const messages = buildOutputMessages(response);
|
|
86
|
+
if (messages.length) {
|
|
87
|
+
span.setAttribute(exports.OUTPUT_MESSAGES_ATTRIBUTE, JSON.stringify(messages));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// Never break the caller's Cohere call over an enrichment failure.
|
|
92
|
+
}
|
|
93
|
+
return response;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
owner.__chat = wrapper;
|
|
97
|
+
this._patched.push([owner, '__chat', original]);
|
|
98
|
+
}
|
|
99
|
+
uninstrument() {
|
|
100
|
+
for (const [owner, methodName, original] of this._patched) {
|
|
101
|
+
owner[methodName] = original;
|
|
102
|
+
}
|
|
103
|
+
this._patched = [];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.CohereToolCallOutputInstrumentor = CohereToolCallOutputInstrumentor;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool definitions for Cohere chat spans.
|
|
3
|
+
*
|
|
4
|
+
* `@traceloop/instrumentation-cohere` records the request's model and message
|
|
5
|
+
* but never its `tools`, so a tool-using call carries no
|
|
6
|
+
* `gen_ai.tool.definitions`.
|
|
7
|
+
*
|
|
8
|
+
* Like the chat token-usage complement it hooks the internal
|
|
9
|
+
* `CohereClient.prototype.__chat`, which runs while traceloop's `cohere.chat`
|
|
10
|
+
* span is active — so the stamp lands on that span regardless of patch order.
|
|
11
|
+
*/
|
|
12
|
+
export declare const TOOL_DEFINITIONS_ATTRIBUTE = "gen_ai.tool.definitions";
|
|
13
|
+
/**
|
|
14
|
+
* The request's tool definitions in the shape they go on the wire.
|
|
15
|
+
*
|
|
16
|
+
* The complement sees the request before the JS client serializes it, where the
|
|
17
|
+
* parameters still sit under the camelCase `parameterDefinitions`; the client
|
|
18
|
+
* renames it to `parameter_definitions` on the way out, which is what the
|
|
19
|
+
* Python client passes in already. Renaming it here is what lets both SDKs
|
|
20
|
+
* normalize a Cohere tool definition identically.
|
|
21
|
+
*/
|
|
22
|
+
export declare function extractToolDefinitions(request: any): any[];
|
|
23
|
+
export declare class CohereToolDefinitionsInstrumentor {
|
|
24
|
+
private _patched;
|
|
25
|
+
instrument(): void;
|
|
26
|
+
uninstrument(): void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Tool definitions for Cohere chat spans.
|
|
4
|
+
*
|
|
5
|
+
* `@traceloop/instrumentation-cohere` records the request's model and message
|
|
6
|
+
* but never its `tools`, so a tool-using call carries no
|
|
7
|
+
* `gen_ai.tool.definitions`.
|
|
8
|
+
*
|
|
9
|
+
* Like the chat token-usage complement it hooks the internal
|
|
10
|
+
* `CohereClient.prototype.__chat`, which runs while traceloop's `cohere.chat`
|
|
11
|
+
* span is active — so the stamp lands on that span regardless of patch order.
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.CohereToolDefinitionsInstrumentor = exports.TOOL_DEFINITIONS_ATTRIBUTE = void 0;
|
|
15
|
+
exports.extractToolDefinitions = extractToolDefinitions;
|
|
16
|
+
const api_1 = require("@opentelemetry/api");
|
|
17
|
+
exports.TOOL_DEFINITIONS_ATTRIBUTE = 'gen_ai.tool.definitions';
|
|
18
|
+
/**
|
|
19
|
+
* The request's tool definitions in the shape they go on the wire.
|
|
20
|
+
*
|
|
21
|
+
* The complement sees the request before the JS client serializes it, where the
|
|
22
|
+
* parameters still sit under the camelCase `parameterDefinitions`; the client
|
|
23
|
+
* renames it to `parameter_definitions` on the way out, which is what the
|
|
24
|
+
* Python client passes in already. Renaming it here is what lets both SDKs
|
|
25
|
+
* normalize a Cohere tool definition identically.
|
|
26
|
+
*/
|
|
27
|
+
function extractToolDefinitions(request) {
|
|
28
|
+
const tools = request && request.tools;
|
|
29
|
+
if (!Array.isArray(tools))
|
|
30
|
+
return [];
|
|
31
|
+
return tools
|
|
32
|
+
.filter((tool) => tool && tool.name)
|
|
33
|
+
.map(({ parameterDefinitions, ...tool }) => parameterDefinitions === undefined
|
|
34
|
+
? tool
|
|
35
|
+
: { ...tool, parameter_definitions: parameterDefinitions });
|
|
36
|
+
}
|
|
37
|
+
class CohereToolDefinitionsInstrumentor {
|
|
38
|
+
// [ownerPrototype, methodName, originalFunction] for uninstrument().
|
|
39
|
+
_patched = [];
|
|
40
|
+
instrument() {
|
|
41
|
+
let CohereClient;
|
|
42
|
+
try {
|
|
43
|
+
({ CohereClient } = require('cohere-ai/Client'));
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
throw new Error('weflayr.autoInstrument(COHERE): the cohere-ai client is not installed. ' +
|
|
47
|
+
'Install it with: npm install cohere-ai\n' +
|
|
48
|
+
`Underlying error: ${err.message}`);
|
|
49
|
+
}
|
|
50
|
+
const owner = CohereClient.prototype;
|
|
51
|
+
const original = owner.__chat;
|
|
52
|
+
function wrapper(...args) {
|
|
53
|
+
const span = api_1.trace.getActiveSpan();
|
|
54
|
+
try {
|
|
55
|
+
const definitions = extractToolDefinitions(args[0]);
|
|
56
|
+
if (definitions.length && span && span.isRecording && span.isRecording()) {
|
|
57
|
+
span.setAttribute(exports.TOOL_DEFINITIONS_ATTRIBUTE, JSON.stringify(definitions));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Never break the caller's Cohere call over an enrichment failure.
|
|
62
|
+
}
|
|
63
|
+
return original.apply(this, args);
|
|
64
|
+
}
|
|
65
|
+
owner.__chat = wrapper;
|
|
66
|
+
this._patched.push([owner, '__chat', original]);
|
|
67
|
+
}
|
|
68
|
+
uninstrument() {
|
|
69
|
+
for (const [owner, methodName, original] of this._patched) {
|
|
70
|
+
owner[methodName] = original;
|
|
71
|
+
}
|
|
72
|
+
this._patched = [];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.CohereToolDefinitionsInstrumentor = CohereToolDefinitionsInstrumentor;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cohere telemetry complement — the registered orchestrator.
|
|
3
|
+
*
|
|
4
|
+
* Wires the weflayr-supplied patches the traceloop Cohere instrumentor misses:
|
|
5
|
+
* - chat token usage (traceloop reads the obsolete usage shape and records
|
|
6
|
+
* zero chat tokens),
|
|
7
|
+
* - the embeddings endpoint it doesn't cover,
|
|
8
|
+
* - the request's structured-output schema, and
|
|
9
|
+
* - function calling: it records neither the request's tools nor the
|
|
10
|
+
* response's tool calls.
|
|
11
|
+
*/
|
|
12
|
+
export declare class CohereTelemetryInstrumentor {
|
|
13
|
+
private _subInstrumentors;
|
|
14
|
+
constructor();
|
|
15
|
+
instrument(instrumentation: any): void;
|
|
16
|
+
uninstrument(): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Cohere telemetry complement — the registered orchestrator.
|
|
4
|
+
*
|
|
5
|
+
* Wires the weflayr-supplied patches the traceloop Cohere instrumentor misses:
|
|
6
|
+
* - chat token usage (traceloop reads the obsolete usage shape and records
|
|
7
|
+
* zero chat tokens),
|
|
8
|
+
* - the embeddings endpoint it doesn't cover,
|
|
9
|
+
* - the request's structured-output schema, and
|
|
10
|
+
* - function calling: it records neither the request's tools nor the
|
|
11
|
+
* response's tool calls.
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.CohereTelemetryInstrumentor = void 0;
|
|
15
|
+
const get_structured_output_schema_telemetry_1 = require("../shared/get_structured_output_schema_telemetry");
|
|
16
|
+
const get_chat_telemetry_1 = require("./get_chat_telemetry");
|
|
17
|
+
const get_embeddings_telemetry_1 = require("./get_embeddings_telemetry");
|
|
18
|
+
const get_structured_output_schema_telemetry_2 = require("./get_structured_output_schema_telemetry");
|
|
19
|
+
const get_tool_call_output_telemetry_1 = require("./get_tool_call_output_telemetry");
|
|
20
|
+
const get_tool_definitions_telemetry_1 = require("./get_tool_definitions_telemetry");
|
|
21
|
+
class CohereTelemetryInstrumentor {
|
|
22
|
+
_subInstrumentors;
|
|
23
|
+
constructor() {
|
|
24
|
+
this._subInstrumentors = [
|
|
25
|
+
new get_chat_telemetry_1.CohereChatTokenUsageInstrumentor(),
|
|
26
|
+
new get_embeddings_telemetry_1.CohereEmbeddingsInstrumentor(),
|
|
27
|
+
new get_structured_output_schema_telemetry_1.TraceloopStructuredOutputSchemaInstrumentor(get_structured_output_schema_telemetry_2.readStructuredOutputSchema),
|
|
28
|
+
new get_tool_definitions_telemetry_1.CohereToolDefinitionsInstrumentor(),
|
|
29
|
+
new get_tool_call_output_telemetry_1.CohereToolCallOutputInstrumentor(),
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
instrument(instrumentation) {
|
|
33
|
+
for (const sub of this._subInstrumentors)
|
|
34
|
+
sub.instrument(instrumentation);
|
|
35
|
+
}
|
|
36
|
+
uninstrument() {
|
|
37
|
+
// Newest first: the token-usage, tool-definition and tool-call complements
|
|
38
|
+
// all wrap `CohereClient.prototype.__chat`, so unwinding in instrument
|
|
39
|
+
// order would reinstall an earlier wrapper instead of the true original.
|
|
40
|
+
for (const sub of [...this._subInstrumentors].reverse())
|
|
41
|
+
sub.uninstrument();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.CohereTelemetryInstrumentor = CohereTelemetryInstrumentor;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hand-written OTel instrumentor for the ElevenLabs audio clients.
|
|
3
|
+
*
|
|
4
|
+
* No official OpenTelemetry instrumentor exists for the ElevenLabs SDK (it's
|
|
5
|
+
* audio, not an LLM), so this ships inside weflayr. It wraps the Text-to-Speech
|
|
6
|
+
* and Speech-to-Text entrypoints to emit one `gen_ai.*` span per call, which
|
|
7
|
+
* flows through the standard span processor -> exporter with no new transport code.
|
|
8
|
+
*
|
|
9
|
+
* The billable unit differs by API, so each span records the one ElevenLabs bills:
|
|
10
|
+
* - Text-to-Speech is billed per input character -> `gen_ai.usage.input_characters`.
|
|
11
|
+
* - Speech-to-Text is billed per second of audio -> `gen_ai.usage.input_seconds`.
|
|
12
|
+
*
|
|
13
|
+
* Entrypoints come in three result shapes, independent of which API they belong to:
|
|
14
|
+
* - *byte stream* methods resolve to a `ReadableStream` of audio chunks;
|
|
15
|
+
* - *object stream* methods resolve to an async-iterable of chunk objects;
|
|
16
|
+
* - *unary* methods resolve to the full response.
|
|
17
|
+
* The HTTP request -- plus any error -- only happens while a stream is awaited
|
|
18
|
+
* and consumed, so the span is started eagerly (to capture the
|
|
19
|
+
* `propagateMetadata` context active at the call site) and ended once the result
|
|
20
|
+
* is exhausted (streams) or returned (unary), giving an `elapsed_ms` that covers
|
|
21
|
+
* the whole call. The per-second billable unit is read off the STT response.
|
|
22
|
+
*/
|
|
23
|
+
export declare class ElevenLabsInstrumentor {
|
|
24
|
+
private _patched;
|
|
25
|
+
instrument(): void;
|
|
26
|
+
uninstrument(): void;
|
|
27
|
+
private _wrap;
|
|
28
|
+
}
|