@paid-ai/paid-node 0.0.8-alpha2 → 0.0.8-alpha3
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/dist/cjs/Client.d.ts +0 -1
- package/dist/cjs/Client.js +3 -12
- package/dist/cjs/tracing/signal.js +2 -6
- package/dist/cjs/tracing/tracing.d.ts +2 -3
- package/dist/cjs/tracing/tracing.js +18 -43
- package/dist/cjs/tracing/wrappers/anthropicWrapper.js +1 -5
- package/dist/cjs/tracing/wrappers/langchainCallback.js +1 -5
- package/dist/cjs/tracing/wrappers/mistralWrapper.js +1 -5
- package/dist/cjs/tracing/wrappers/openAiWrapper.js +1 -17
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/Client.d.mts +0 -1
- package/dist/esm/Client.mjs +4 -13
- package/dist/esm/tracing/signal.mjs +3 -7
- package/dist/esm/tracing/tracing.d.mts +2 -3
- package/dist/esm/tracing/tracing.mjs +18 -42
- package/dist/esm/tracing/wrappers/anthropicWrapper.mjs +3 -7
- package/dist/esm/tracing/wrappers/langchainCallback.mjs +3 -7
- package/dist/esm/tracing/wrappers/mistralWrapper.mjs +3 -7
- package/dist/esm/tracing/wrappers/openAiWrapper.mjs +3 -19
- package/dist/esm/version.d.mts +1 -1
- package/dist/esm/version.mjs +1 -1
- package/package.json +1 -1
package/dist/cjs/Client.d.ts
CHANGED
|
@@ -44,5 +44,4 @@ export declare class PaidClient {
|
|
|
44
44
|
initializeTracing(collectorEndpoint?: string): Promise<void>;
|
|
45
45
|
trace<T extends (...args: any[]) => any>(externalCustomerId: string, fn: T, externalAgentId?: string, ...args: Parameters<T>): Promise<ReturnType<T>>;
|
|
46
46
|
signal(eventName: string, data?: Record<string, any>): void;
|
|
47
|
-
getSpanProcessorAndInitialize(collectorEndpoint?: string): Promise<import("@opentelemetry/sdk-trace-base").SpanProcessor>;
|
|
48
47
|
}
|
package/dist/cjs/Client.js
CHANGED
|
@@ -86,9 +86,9 @@ class PaidClient {
|
|
|
86
86
|
var _a;
|
|
87
87
|
return ((_a = this._usage) !== null && _a !== void 0 ? _a : (this._usage = new BatchUsage_js_1.Usage(this._options)));
|
|
88
88
|
}
|
|
89
|
-
//
|
|
90
|
-
initializeTracing() {
|
|
91
|
-
return __awaiter(this,
|
|
89
|
+
// Need to call this method before using tracing or creating wrappers.
|
|
90
|
+
initializeTracing(collectorEndpoint) {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
92
|
const tokenSupplier = this._options.token;
|
|
93
93
|
const token = typeof tokenSupplier === "function" ? yield tokenSupplier() : tokenSupplier;
|
|
94
94
|
const resolvedToken = yield Promise.resolve(token);
|
|
@@ -106,14 +106,5 @@ class PaidClient {
|
|
|
106
106
|
signal(eventName, data) {
|
|
107
107
|
return (0, signal_js_1._signal)(eventName, data);
|
|
108
108
|
}
|
|
109
|
-
// Use this method if you're setting up OTEL SDK in your code and want to add Paid's span processor.
|
|
110
|
-
getSpanProcessorAndInitialize() {
|
|
111
|
-
return __awaiter(this, arguments, void 0, function* (collectorEndpoint = "https://collector.agentpaid.io:4318/v1/traces") {
|
|
112
|
-
const tokenSupplier = this._options.token;
|
|
113
|
-
const token = typeof tokenSupplier === "function" ? yield tokenSupplier() : tokenSupplier;
|
|
114
|
-
const resolvedToken = yield Promise.resolve(token);
|
|
115
|
-
return (0, tracing_js_1._getSpanProcessorAndInitialize)(resolvedToken, collectorEndpoint);
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
109
|
}
|
|
119
110
|
exports.PaidClient = PaidClient;
|
|
@@ -3,22 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports._signal = _signal;
|
|
4
4
|
const api_1 = require("@opentelemetry/api");
|
|
5
5
|
const tracing_js_1 = require("./tracing.js");
|
|
6
|
+
const tracing_js_2 = require("./tracing.js");
|
|
6
7
|
function _signal(eventName, data) {
|
|
7
8
|
if (!eventName) {
|
|
8
9
|
throw new Error("Event name is required for signal.");
|
|
9
10
|
}
|
|
10
|
-
// Check if there's an active span (from _trace())
|
|
11
|
-
const currentSpan = api_1.trace.getActiveSpan();
|
|
12
|
-
if (!currentSpan) {
|
|
13
|
-
throw new Error("Cannot send signal: you should call signal() within trace()");
|
|
14
|
-
}
|
|
15
11
|
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
16
12
|
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
17
13
|
const token = (0, tracing_js_1.getTokenStorage)();
|
|
18
14
|
if (!externalCustomerId || !externalAgentId || !token) {
|
|
19
15
|
throw new Error(`Missing some of: external_customer_id: ${externalCustomerId}, external_agent_id: ${externalAgentId}, or token. Make sure to call signal() within trace()`);
|
|
20
16
|
}
|
|
21
|
-
const tracer =
|
|
17
|
+
const tracer = tracing_js_2.paidTracer;
|
|
22
18
|
tracer.startActiveSpan("trace.signal", (span) => {
|
|
23
19
|
try {
|
|
24
20
|
const attributes = {
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
2
1
|
import winston from "winston";
|
|
3
2
|
export declare const logger: winston.Logger;
|
|
3
|
+
export declare let paidTracer: import("@opentelemetry/api").Tracer;
|
|
4
4
|
export declare const getCustomerIdStorage: () => string | null | undefined;
|
|
5
5
|
export declare const getAgentIdStorage: () => string | null | undefined;
|
|
6
6
|
export declare const getTokenStorage: () => string | null | undefined;
|
|
7
|
-
export declare function _initializeTracing(apiKey: string, collectorEndpoint
|
|
8
|
-
export declare function _getSpanProcessorAndInitialize(apiKey: string, collectorEndpoint: string): SpanProcessor;
|
|
7
|
+
export declare function _initializeTracing(apiKey: string, collectorEndpoint?: string): void;
|
|
9
8
|
export declare function _trace<T extends (...args: any[]) => any>(externalCustomerId: string, fn: T, externalAgentId: string | undefined, ...args: Parameters<T>): Promise<ReturnType<T>>;
|
|
@@ -12,11 +12,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.getTokenStorage = exports.getAgentIdStorage = exports.getCustomerIdStorage = exports.logger = void 0;
|
|
15
|
+
exports.getTokenStorage = exports.getAgentIdStorage = exports.getCustomerIdStorage = exports.paidTracer = exports.logger = void 0;
|
|
16
16
|
exports._initializeTracing = _initializeTracing;
|
|
17
|
-
exports._getSpanProcessorAndInitialize = _getSpanProcessorAndInitialize;
|
|
18
17
|
exports._trace = _trace;
|
|
19
|
-
const
|
|
18
|
+
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
20
19
|
const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http");
|
|
21
20
|
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
22
21
|
const api_1 = require("@opentelemetry/api");
|
|
@@ -31,6 +30,12 @@ const logLevel = process.env.PAID_LOG_LEVEL;
|
|
|
31
30
|
if (logLevel) {
|
|
32
31
|
exports.logger.level = logLevel;
|
|
33
32
|
}
|
|
33
|
+
const COLLECTOR_ENDPOINT = process.env.PAID_COLLECTOR_ENDPOINT || "https://collector.agentpaid.io:4318/v1/traces";
|
|
34
|
+
// set up default tracing provider
|
|
35
|
+
let paidExporter = new exporter_trace_otlp_http_1.OTLPTraceExporter({ url: COLLECTOR_ENDPOINT });
|
|
36
|
+
let spanProcessor = new sdk_trace_base_1.BatchSpanProcessor(paidExporter);
|
|
37
|
+
let paidTracerProvider = new sdk_trace_node_1.NodeTracerProvider({ spanProcessors: [spanProcessor] });
|
|
38
|
+
exports.paidTracer = paidTracerProvider.getTracer("paid.node");
|
|
34
39
|
// storage for passing info to child spans
|
|
35
40
|
const customerIdStorage = new async_hooks_1.AsyncLocalStorage();
|
|
36
41
|
const agentIdStorage = new async_hooks_1.AsyncLocalStorage();
|
|
@@ -63,19 +68,6 @@ function setupGracefulShutdown(shuttable) {
|
|
|
63
68
|
});
|
|
64
69
|
});
|
|
65
70
|
}
|
|
66
|
-
function isOTELInitialized() {
|
|
67
|
-
const provider = api_1.trace.getTracerProvider();
|
|
68
|
-
if (!provider) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
if (provider.constructor.name !== "ProxyTracerProvider" && provider.constructor.name !== "NodeTracerProvider") {
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
if (provider._delegate) {
|
|
75
|
-
return provider._delegate.constructor.name !== "NoopTracerProvider";
|
|
76
|
-
}
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
71
|
function _initializeTracing(apiKey, collectorEndpoint) {
|
|
80
72
|
if (getToken()) {
|
|
81
73
|
throw new Error("Tracing SDK is already initialized.");
|
|
@@ -83,40 +75,23 @@ function _initializeTracing(apiKey, collectorEndpoint) {
|
|
|
83
75
|
if (!apiKey) {
|
|
84
76
|
throw new Error("Cannot initialize tracing SDK - first initialize the PaidClient with an API key");
|
|
85
77
|
}
|
|
86
|
-
|
|
87
|
-
new
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
if (isOTELInitialized()) {
|
|
93
|
-
throw new Error("OTEL SDK is already initialized with a different provider.\n" +
|
|
94
|
-
"If you're already using OTEL in your code - " +
|
|
95
|
-
"please use function Paid.getSpanProcessorAndInitialize() " +
|
|
96
|
-
"and add the span processor to your OTEL SDK initialization");
|
|
78
|
+
if (collectorEndpoint) {
|
|
79
|
+
paidExporter = new exporter_trace_otlp_http_1.OTLPTraceExporter({ url: collectorEndpoint });
|
|
80
|
+
spanProcessor = new sdk_trace_base_1.BatchSpanProcessor(paidExporter);
|
|
81
|
+
paidTracerProvider = new sdk_trace_node_1.NodeTracerProvider({ spanProcessors: [spanProcessor] });
|
|
82
|
+
exports.paidTracer = paidTracerProvider.getTracer("paid.node");
|
|
97
83
|
}
|
|
98
|
-
const sdk = new sdk_node_1.NodeSDK({
|
|
99
|
-
traceExporter: new exporter_trace_otlp_http_1.OTLPTraceExporter({
|
|
100
|
-
url: collectorEndpoint,
|
|
101
|
-
}),
|
|
102
|
-
});
|
|
103
|
-
sdk.start();
|
|
104
|
-
setupGracefulShutdown(sdk);
|
|
105
|
-
setToken(apiKey);
|
|
106
|
-
exports.logger.info(`Paid tracing SDK initialized with collector endpoint: ${collectorEndpoint}`);
|
|
107
|
-
}
|
|
108
|
-
function _getSpanProcessorAndInitialize(apiKey, collectorEndpoint) {
|
|
109
|
-
setToken(apiKey);
|
|
110
|
-
const spanProcessor = new sdk_trace_base_1.BatchSpanProcessor(new exporter_trace_otlp_http_1.OTLPTraceExporter({ url: collectorEndpoint }));
|
|
111
84
|
setupGracefulShutdown(spanProcessor);
|
|
112
|
-
|
|
85
|
+
setToken(apiKey);
|
|
86
|
+
const collectorAddress = collectorEndpoint || COLLECTOR_ENDPOINT;
|
|
87
|
+
exports.logger.info(`Paid tracing SDK initialized with collector endpoint: ${collectorAddress}`);
|
|
113
88
|
}
|
|
114
89
|
function _trace(externalCustomerId, fn, externalAgentId, ...args) {
|
|
115
90
|
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
-
const tracer =
|
|
91
|
+
const tracer = exports.paidTracer;
|
|
117
92
|
const token = getToken();
|
|
118
93
|
if (!token || !externalCustomerId) {
|
|
119
|
-
throw new Error(`
|
|
94
|
+
throw new Error(`Paid tracing is not initialized. Make sure to call initializeTracing() first.`);
|
|
120
95
|
}
|
|
121
96
|
return tracer.startActiveSpan("paid.node", (span) => __awaiter(this, void 0, void 0, function* () {
|
|
122
97
|
span.setAttribute("external_customer_id", externalCustomerId);
|
|
@@ -15,7 +15,7 @@ const tracing_js_1 = require("../tracing.js");
|
|
|
15
15
|
class PaidAnthropic {
|
|
16
16
|
constructor(anthropicClient) {
|
|
17
17
|
this.anthropic = anthropicClient;
|
|
18
|
-
this.tracer =
|
|
18
|
+
this.tracer = tracing_js_1.paidTracer;
|
|
19
19
|
}
|
|
20
20
|
get messages() {
|
|
21
21
|
return new MessagesWrapper(this.anthropic, this.tracer);
|
|
@@ -29,10 +29,6 @@ class MessagesWrapper {
|
|
|
29
29
|
}
|
|
30
30
|
create(params) {
|
|
31
31
|
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
-
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
33
|
-
if (!currentSpan) {
|
|
34
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
35
|
-
}
|
|
36
32
|
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
37
33
|
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
38
34
|
const token = (0, tracing_js_1.getTokenStorage)();
|
|
@@ -18,7 +18,7 @@ class PaidLangChainCallback extends base_1.BaseCallbackHandler {
|
|
|
18
18
|
super();
|
|
19
19
|
this.name = "PaidLangChainCallback";
|
|
20
20
|
this.spans = new Map();
|
|
21
|
-
this.tracer =
|
|
21
|
+
this.tracer = tracing_js_1.paidTracer;
|
|
22
22
|
}
|
|
23
23
|
extractProvider(serialized) {
|
|
24
24
|
if (!(serialized === null || serialized === void 0 ? void 0 : serialized.id))
|
|
@@ -40,10 +40,6 @@ class PaidLangChainCallback extends base_1.BaseCallbackHandler {
|
|
|
40
40
|
}
|
|
41
41
|
handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata) {
|
|
42
42
|
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
-
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
44
|
-
if (!currentSpan) {
|
|
45
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
46
|
-
}
|
|
47
43
|
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
48
44
|
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
49
45
|
const token = (0, tracing_js_1.getTokenStorage)();
|
|
@@ -15,7 +15,7 @@ const tracing_js_1 = require("../tracing.js");
|
|
|
15
15
|
class PaidMistral {
|
|
16
16
|
constructor(mistralClient) {
|
|
17
17
|
this.mistral = mistralClient;
|
|
18
|
-
this.tracer =
|
|
18
|
+
this.tracer = tracing_js_1.paidTracer;
|
|
19
19
|
}
|
|
20
20
|
get ocr() {
|
|
21
21
|
return new OCRWrapper(this.mistral, this.tracer);
|
|
@@ -29,10 +29,6 @@ class OCRWrapper {
|
|
|
29
29
|
}
|
|
30
30
|
process(request, options) {
|
|
31
31
|
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
-
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
33
|
-
if (!currentSpan) {
|
|
34
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
35
|
-
}
|
|
36
32
|
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
37
33
|
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
38
34
|
const token = (0, tracing_js_1.getTokenStorage)();
|
|
@@ -15,7 +15,7 @@ const tracing_js_1 = require("../tracing.js");
|
|
|
15
15
|
class PaidOpenAI {
|
|
16
16
|
constructor(openaiClient) {
|
|
17
17
|
this.openai = openaiClient;
|
|
18
|
-
this.tracer =
|
|
18
|
+
this.tracer = tracing_js_1.paidTracer;
|
|
19
19
|
}
|
|
20
20
|
get chat() {
|
|
21
21
|
return new ChatWrapper(this.openai, this.tracer);
|
|
@@ -47,10 +47,6 @@ class ChatCompletionsWrapper {
|
|
|
47
47
|
}
|
|
48
48
|
create(params) {
|
|
49
49
|
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
-
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
51
|
-
if (!currentSpan) {
|
|
52
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
53
|
-
}
|
|
54
50
|
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
55
51
|
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
56
52
|
const token = (0, tracing_js_1.getTokenStorage)();
|
|
@@ -99,10 +95,6 @@ class ResponsesWrapper {
|
|
|
99
95
|
}
|
|
100
96
|
create(params) {
|
|
101
97
|
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
-
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
103
|
-
if (!currentSpan) {
|
|
104
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
105
|
-
}
|
|
106
98
|
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
107
99
|
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
108
100
|
const token = (0, tracing_js_1.getTokenStorage)();
|
|
@@ -158,10 +150,6 @@ class EmbeddingsWrapper {
|
|
|
158
150
|
}
|
|
159
151
|
create(params) {
|
|
160
152
|
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
-
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
162
|
-
if (!currentSpan) {
|
|
163
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
164
|
-
}
|
|
165
153
|
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
166
154
|
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
167
155
|
const token = (0, tracing_js_1.getTokenStorage)();
|
|
@@ -210,10 +198,6 @@ class ImagesWrapper {
|
|
|
210
198
|
generate(params) {
|
|
211
199
|
return __awaiter(this, void 0, void 0, function* () {
|
|
212
200
|
var _a;
|
|
213
|
-
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
214
|
-
if (!currentSpan) {
|
|
215
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
216
|
-
}
|
|
217
201
|
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
218
202
|
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
219
203
|
const token = (0, tracing_js_1.getTokenStorage)();
|
package/dist/cjs/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.0.8-
|
|
1
|
+
export declare const SDK_VERSION = "0.0.8-alpha3";
|
package/dist/cjs/version.js
CHANGED
package/dist/esm/Client.d.mts
CHANGED
|
@@ -44,5 +44,4 @@ export declare class PaidClient {
|
|
|
44
44
|
initializeTracing(collectorEndpoint?: string): Promise<void>;
|
|
45
45
|
trace<T extends (...args: any[]) => any>(externalCustomerId: string, fn: T, externalAgentId?: string, ...args: Parameters<T>): Promise<ReturnType<T>>;
|
|
46
46
|
signal(eventName: string, data?: Record<string, any>): void;
|
|
47
|
-
getSpanProcessorAndInitialize(collectorEndpoint?: string): Promise<import("@opentelemetry/sdk-trace-base").SpanProcessor>;
|
|
48
47
|
}
|
package/dist/esm/Client.mjs
CHANGED
|
@@ -17,7 +17,7 @@ import { Agents } from "./api/resources/agents/client/Client.mjs";
|
|
|
17
17
|
import { Contacts } from "./api/resources/contacts/client/Client.mjs";
|
|
18
18
|
import { Orders } from "./api/resources/orders/client/Client.mjs";
|
|
19
19
|
import { Usage } from "./wrapper/BatchUsage.mjs";
|
|
20
|
-
import { _trace, _initializeTracing
|
|
20
|
+
import { _trace, _initializeTracing } from "./tracing/tracing.mjs";
|
|
21
21
|
import { _signal } from "./tracing/signal.mjs";
|
|
22
22
|
export class PaidClient {
|
|
23
23
|
constructor(_options) {
|
|
@@ -50,9 +50,9 @@ export class PaidClient {
|
|
|
50
50
|
var _a;
|
|
51
51
|
return ((_a = this._usage) !== null && _a !== void 0 ? _a : (this._usage = new Usage(this._options)));
|
|
52
52
|
}
|
|
53
|
-
//
|
|
54
|
-
initializeTracing() {
|
|
55
|
-
return __awaiter(this,
|
|
53
|
+
// Need to call this method before using tracing or creating wrappers.
|
|
54
|
+
initializeTracing(collectorEndpoint) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
56
|
const tokenSupplier = this._options.token;
|
|
57
57
|
const token = typeof tokenSupplier === "function" ? yield tokenSupplier() : tokenSupplier;
|
|
58
58
|
const resolvedToken = yield Promise.resolve(token);
|
|
@@ -70,13 +70,4 @@ export class PaidClient {
|
|
|
70
70
|
signal(eventName, data) {
|
|
71
71
|
return _signal(eventName, data);
|
|
72
72
|
}
|
|
73
|
-
// Use this method if you're setting up OTEL SDK in your code and want to add Paid's span processor.
|
|
74
|
-
getSpanProcessorAndInitialize() {
|
|
75
|
-
return __awaiter(this, arguments, void 0, function* (collectorEndpoint = "https://collector.agentpaid.io:4318/v1/traces") {
|
|
76
|
-
const tokenSupplier = this._options.token;
|
|
77
|
-
const token = typeof tokenSupplier === "function" ? yield tokenSupplier() : tokenSupplier;
|
|
78
|
-
const resolvedToken = yield Promise.resolve(token);
|
|
79
|
-
return _getSpanProcessorAndInitialize(resolvedToken, collectorEndpoint);
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
73
|
}
|
|
@@ -1,21 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
2
2
|
import { getCustomerIdStorage, getAgentIdStorage, getTokenStorage } from "./tracing.mjs";
|
|
3
|
+
import { paidTracer } from "./tracing.mjs";
|
|
3
4
|
export function _signal(eventName, data) {
|
|
4
5
|
if (!eventName) {
|
|
5
6
|
throw new Error("Event name is required for signal.");
|
|
6
7
|
}
|
|
7
|
-
// Check if there's an active span (from _trace())
|
|
8
|
-
const currentSpan = trace.getActiveSpan();
|
|
9
|
-
if (!currentSpan) {
|
|
10
|
-
throw new Error("Cannot send signal: you should call signal() within trace()");
|
|
11
|
-
}
|
|
12
8
|
const externalCustomerId = getCustomerIdStorage();
|
|
13
9
|
const externalAgentId = getAgentIdStorage();
|
|
14
10
|
const token = getTokenStorage();
|
|
15
11
|
if (!externalCustomerId || !externalAgentId || !token) {
|
|
16
12
|
throw new Error(`Missing some of: external_customer_id: ${externalCustomerId}, external_agent_id: ${externalAgentId}, or token. Make sure to call signal() within trace()`);
|
|
17
13
|
}
|
|
18
|
-
const tracer =
|
|
14
|
+
const tracer = paidTracer;
|
|
19
15
|
tracer.startActiveSpan("trace.signal", (span) => {
|
|
20
16
|
try {
|
|
21
17
|
const attributes = {
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
2
1
|
import winston from "winston";
|
|
3
2
|
export declare const logger: winston.Logger;
|
|
3
|
+
export declare let paidTracer: import("@opentelemetry/api").Tracer;
|
|
4
4
|
export declare const getCustomerIdStorage: () => string | null | undefined;
|
|
5
5
|
export declare const getAgentIdStorage: () => string | null | undefined;
|
|
6
6
|
export declare const getTokenStorage: () => string | null | undefined;
|
|
7
|
-
export declare function _initializeTracing(apiKey: string, collectorEndpoint
|
|
8
|
-
export declare function _getSpanProcessorAndInitialize(apiKey: string, collectorEndpoint: string): SpanProcessor;
|
|
7
|
+
export declare function _initializeTracing(apiKey: string, collectorEndpoint?: string): void;
|
|
9
8
|
export declare function _trace<T extends (...args: any[]) => any>(externalCustomerId: string, fn: T, externalAgentId: string | undefined, ...args: Parameters<T>): Promise<ReturnType<T>>;
|
|
@@ -7,10 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
10
|
+
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
|
|
11
11
|
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
12
12
|
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
13
|
-
import {
|
|
13
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
14
14
|
import { AsyncLocalStorage } from "async_hooks";
|
|
15
15
|
import winston from "winston";
|
|
16
16
|
export const logger = winston.createLogger({
|
|
@@ -22,6 +22,12 @@ const logLevel = process.env.PAID_LOG_LEVEL;
|
|
|
22
22
|
if (logLevel) {
|
|
23
23
|
logger.level = logLevel;
|
|
24
24
|
}
|
|
25
|
+
const COLLECTOR_ENDPOINT = process.env.PAID_COLLECTOR_ENDPOINT || "https://collector.agentpaid.io:4318/v1/traces";
|
|
26
|
+
// set up default tracing provider
|
|
27
|
+
let paidExporter = new OTLPTraceExporter({ url: COLLECTOR_ENDPOINT });
|
|
28
|
+
let spanProcessor = new BatchSpanProcessor(paidExporter);
|
|
29
|
+
let paidTracerProvider = new NodeTracerProvider({ spanProcessors: [spanProcessor] });
|
|
30
|
+
export let paidTracer = paidTracerProvider.getTracer("paid.node");
|
|
25
31
|
// storage for passing info to child spans
|
|
26
32
|
const customerIdStorage = new AsyncLocalStorage();
|
|
27
33
|
const agentIdStorage = new AsyncLocalStorage();
|
|
@@ -51,19 +57,6 @@ function setupGracefulShutdown(shuttable) {
|
|
|
51
57
|
});
|
|
52
58
|
});
|
|
53
59
|
}
|
|
54
|
-
function isOTELInitialized() {
|
|
55
|
-
const provider = trace.getTracerProvider();
|
|
56
|
-
if (!provider) {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
if (provider.constructor.name !== "ProxyTracerProvider" && provider.constructor.name !== "NodeTracerProvider") {
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
if (provider._delegate) {
|
|
63
|
-
return provider._delegate.constructor.name !== "NoopTracerProvider";
|
|
64
|
-
}
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
60
|
export function _initializeTracing(apiKey, collectorEndpoint) {
|
|
68
61
|
if (getToken()) {
|
|
69
62
|
throw new Error("Tracing SDK is already initialized.");
|
|
@@ -71,40 +64,23 @@ export function _initializeTracing(apiKey, collectorEndpoint) {
|
|
|
71
64
|
if (!apiKey) {
|
|
72
65
|
throw new Error("Cannot initialize tracing SDK - first initialize the PaidClient with an API key");
|
|
73
66
|
}
|
|
74
|
-
|
|
75
|
-
new
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
if (isOTELInitialized()) {
|
|
81
|
-
throw new Error("OTEL SDK is already initialized with a different provider.\n" +
|
|
82
|
-
"If you're already using OTEL in your code - " +
|
|
83
|
-
"please use function Paid.getSpanProcessorAndInitialize() " +
|
|
84
|
-
"and add the span processor to your OTEL SDK initialization");
|
|
67
|
+
if (collectorEndpoint) {
|
|
68
|
+
paidExporter = new OTLPTraceExporter({ url: collectorEndpoint });
|
|
69
|
+
spanProcessor = new BatchSpanProcessor(paidExporter);
|
|
70
|
+
paidTracerProvider = new NodeTracerProvider({ spanProcessors: [spanProcessor] });
|
|
71
|
+
paidTracer = paidTracerProvider.getTracer("paid.node");
|
|
85
72
|
}
|
|
86
|
-
const sdk = new NodeSDK({
|
|
87
|
-
traceExporter: new OTLPTraceExporter({
|
|
88
|
-
url: collectorEndpoint,
|
|
89
|
-
}),
|
|
90
|
-
});
|
|
91
|
-
sdk.start();
|
|
92
|
-
setupGracefulShutdown(sdk);
|
|
93
|
-
setToken(apiKey);
|
|
94
|
-
logger.info(`Paid tracing SDK initialized with collector endpoint: ${collectorEndpoint}`);
|
|
95
|
-
}
|
|
96
|
-
export function _getSpanProcessorAndInitialize(apiKey, collectorEndpoint) {
|
|
97
|
-
setToken(apiKey);
|
|
98
|
-
const spanProcessor = new BatchSpanProcessor(new OTLPTraceExporter({ url: collectorEndpoint }));
|
|
99
73
|
setupGracefulShutdown(spanProcessor);
|
|
100
|
-
|
|
74
|
+
setToken(apiKey);
|
|
75
|
+
const collectorAddress = collectorEndpoint || COLLECTOR_ENDPOINT;
|
|
76
|
+
logger.info(`Paid tracing SDK initialized with collector endpoint: ${collectorAddress}`);
|
|
101
77
|
}
|
|
102
78
|
export function _trace(externalCustomerId, fn, externalAgentId, ...args) {
|
|
103
79
|
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
-
const tracer =
|
|
80
|
+
const tracer = paidTracer;
|
|
105
81
|
const token = getToken();
|
|
106
82
|
if (!token || !externalCustomerId) {
|
|
107
|
-
throw new Error(`
|
|
83
|
+
throw new Error(`Paid tracing is not initialized. Make sure to call initializeTracing() first.`);
|
|
108
84
|
}
|
|
109
85
|
return tracer.startActiveSpan("paid.node", (span) => __awaiter(this, void 0, void 0, function* () {
|
|
110
86
|
span.setAttribute("external_customer_id", externalCustomerId);
|
|
@@ -7,12 +7,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
11
|
-
import { getCustomerIdStorage, getAgentIdStorage, getTokenStorage } from "../tracing.mjs";
|
|
10
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
11
|
+
import { getCustomerIdStorage, getAgentIdStorage, getTokenStorage, paidTracer } from "../tracing.mjs";
|
|
12
12
|
export class PaidAnthropic {
|
|
13
13
|
constructor(anthropicClient) {
|
|
14
14
|
this.anthropic = anthropicClient;
|
|
15
|
-
this.tracer =
|
|
15
|
+
this.tracer = paidTracer;
|
|
16
16
|
}
|
|
17
17
|
get messages() {
|
|
18
18
|
return new MessagesWrapper(this.anthropic, this.tracer);
|
|
@@ -25,10 +25,6 @@ class MessagesWrapper {
|
|
|
25
25
|
}
|
|
26
26
|
create(params) {
|
|
27
27
|
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
-
const currentSpan = trace.getSpan(context.active());
|
|
29
|
-
if (!currentSpan) {
|
|
30
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
31
|
-
}
|
|
32
28
|
const externalCustomerId = getCustomerIdStorage();
|
|
33
29
|
const externalAgentId = getAgentIdStorage();
|
|
34
30
|
const token = getTokenStorage();
|
|
@@ -8,14 +8,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
|
|
11
|
-
import {
|
|
12
|
-
import { getCustomerIdStorage, getAgentIdStorage, getTokenStorage } from "../tracing.mjs";
|
|
11
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
12
|
+
import { getCustomerIdStorage, getAgentIdStorage, getTokenStorage, paidTracer } from "../tracing.mjs";
|
|
13
13
|
export class PaidLangChainCallback extends BaseCallbackHandler {
|
|
14
14
|
constructor() {
|
|
15
15
|
super();
|
|
16
16
|
this.name = "PaidLangChainCallback";
|
|
17
17
|
this.spans = new Map();
|
|
18
|
-
this.tracer =
|
|
18
|
+
this.tracer = paidTracer;
|
|
19
19
|
}
|
|
20
20
|
extractProvider(serialized) {
|
|
21
21
|
if (!(serialized === null || serialized === void 0 ? void 0 : serialized.id))
|
|
@@ -37,10 +37,6 @@ export class PaidLangChainCallback extends BaseCallbackHandler {
|
|
|
37
37
|
}
|
|
38
38
|
handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata) {
|
|
39
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
-
const currentSpan = trace.getSpan(context.active());
|
|
41
|
-
if (!currentSpan) {
|
|
42
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
43
|
-
}
|
|
44
40
|
const externalCustomerId = getCustomerIdStorage();
|
|
45
41
|
const externalAgentId = getAgentIdStorage();
|
|
46
42
|
const token = getTokenStorage();
|
|
@@ -7,12 +7,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
11
|
-
import { getCustomerIdStorage, getAgentIdStorage, getTokenStorage } from "../tracing.mjs";
|
|
10
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
11
|
+
import { getCustomerIdStorage, getAgentIdStorage, getTokenStorage, paidTracer } from "../tracing.mjs";
|
|
12
12
|
export class PaidMistral {
|
|
13
13
|
constructor(mistralClient) {
|
|
14
14
|
this.mistral = mistralClient;
|
|
15
|
-
this.tracer =
|
|
15
|
+
this.tracer = paidTracer;
|
|
16
16
|
}
|
|
17
17
|
get ocr() {
|
|
18
18
|
return new OCRWrapper(this.mistral, this.tracer);
|
|
@@ -25,10 +25,6 @@ class OCRWrapper {
|
|
|
25
25
|
}
|
|
26
26
|
process(request, options) {
|
|
27
27
|
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
-
const currentSpan = trace.getSpan(context.active());
|
|
29
|
-
if (!currentSpan) {
|
|
30
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
31
|
-
}
|
|
32
28
|
const externalCustomerId = getCustomerIdStorage();
|
|
33
29
|
const externalAgentId = getAgentIdStorage();
|
|
34
30
|
const token = getTokenStorage();
|
|
@@ -7,12 +7,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
11
|
-
import { getCustomerIdStorage, getAgentIdStorage, getTokenStorage } from "../tracing.mjs";
|
|
10
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
11
|
+
import { getCustomerIdStorage, getAgentIdStorage, getTokenStorage, paidTracer } from "../tracing.mjs";
|
|
12
12
|
export class PaidOpenAI {
|
|
13
13
|
constructor(openaiClient) {
|
|
14
14
|
this.openai = openaiClient;
|
|
15
|
-
this.tracer =
|
|
15
|
+
this.tracer = paidTracer;
|
|
16
16
|
}
|
|
17
17
|
get chat() {
|
|
18
18
|
return new ChatWrapper(this.openai, this.tracer);
|
|
@@ -43,10 +43,6 @@ class ChatCompletionsWrapper {
|
|
|
43
43
|
}
|
|
44
44
|
create(params) {
|
|
45
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
const currentSpan = trace.getSpan(context.active());
|
|
47
|
-
if (!currentSpan) {
|
|
48
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
49
|
-
}
|
|
50
46
|
const externalCustomerId = getCustomerIdStorage();
|
|
51
47
|
const externalAgentId = getAgentIdStorage();
|
|
52
48
|
const token = getTokenStorage();
|
|
@@ -95,10 +91,6 @@ class ResponsesWrapper {
|
|
|
95
91
|
}
|
|
96
92
|
create(params) {
|
|
97
93
|
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
-
const currentSpan = trace.getSpan(context.active());
|
|
99
|
-
if (!currentSpan) {
|
|
100
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
101
|
-
}
|
|
102
94
|
const externalCustomerId = getCustomerIdStorage();
|
|
103
95
|
const externalAgentId = getAgentIdStorage();
|
|
104
96
|
const token = getTokenStorage();
|
|
@@ -154,10 +146,6 @@ class EmbeddingsWrapper {
|
|
|
154
146
|
}
|
|
155
147
|
create(params) {
|
|
156
148
|
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
-
const currentSpan = trace.getSpan(context.active());
|
|
158
|
-
if (!currentSpan) {
|
|
159
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
160
|
-
}
|
|
161
149
|
const externalCustomerId = getCustomerIdStorage();
|
|
162
150
|
const externalAgentId = getAgentIdStorage();
|
|
163
151
|
const token = getTokenStorage();
|
|
@@ -206,10 +194,6 @@ class ImagesWrapper {
|
|
|
206
194
|
generate(params) {
|
|
207
195
|
return __awaiter(this, void 0, void 0, function* () {
|
|
208
196
|
var _a;
|
|
209
|
-
const currentSpan = trace.getSpan(context.active());
|
|
210
|
-
if (!currentSpan) {
|
|
211
|
-
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
212
|
-
}
|
|
213
197
|
const externalCustomerId = getCustomerIdStorage();
|
|
214
198
|
const externalAgentId = getAgentIdStorage();
|
|
215
199
|
const token = getTokenStorage();
|
package/dist/esm/version.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.0.8-
|
|
1
|
+
export declare const SDK_VERSION = "0.0.8-alpha3";
|
package/dist/esm/version.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "0.0.8-
|
|
1
|
+
export const SDK_VERSION = "0.0.8-alpha3";
|