@paid-ai/paid-node 0.0.8-alpha1 → 0.0.8-alpha2
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 +17 -16
- package/dist/cjs/Client.d.ts +4 -2
- package/dist/cjs/Client.js +21 -4
- package/dist/cjs/index.d.ts +3 -1
- package/dist/cjs/index.js +7 -4
- package/dist/cjs/tracing/signal.d.ts +1 -0
- package/dist/cjs/tracing/signal.js +49 -0
- package/dist/cjs/tracing/tracing.d.ts +8 -3
- package/dist/cjs/tracing/tracing.js +100 -28
- package/dist/cjs/tracing/wrappers/anthropicWrapper.d.ts +16 -0
- package/dist/cjs/tracing/wrappers/anthropicWrapper.js +88 -0
- package/dist/cjs/tracing/wrappers/langchainCallback.d.ts +21 -0
- package/dist/cjs/tracing/wrappers/langchainCallback.js +146 -0
- package/dist/cjs/tracing/wrappers/mistralWrapper.d.ts +17 -0
- package/dist/cjs/tracing/wrappers/mistralWrapper.js +85 -0
- package/dist/cjs/tracing/wrappers/openAiWrapper.js +48 -46
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/wrapper/BatchUsage.d.ts +1 -1
- package/dist/cjs/wrapper/BatchUsage.js +2 -2
- package/dist/esm/Client.d.mts +4 -2
- package/dist/esm/Client.mjs +22 -5
- package/dist/esm/index.d.mts +3 -1
- package/dist/esm/index.mjs +3 -1
- package/dist/esm/tracing/signal.d.mts +1 -0
- package/dist/esm/tracing/signal.mjs +46 -0
- package/dist/esm/tracing/tracing.d.mts +8 -3
- package/dist/esm/tracing/tracing.mjs +94 -27
- package/dist/esm/tracing/wrappers/anthropicWrapper.d.mts +16 -0
- package/dist/esm/tracing/wrappers/anthropicWrapper.mjs +84 -0
- package/dist/esm/tracing/wrappers/langchainCallback.d.mts +21 -0
- package/dist/esm/tracing/wrappers/langchainCallback.mjs +142 -0
- package/dist/esm/tracing/wrappers/mistralWrapper.d.mts +17 -0
- package/dist/esm/tracing/wrappers/mistralWrapper.mjs +81 -0
- package/dist/esm/tracing/wrappers/openAiWrapper.mjs +44 -42
- package/dist/esm/version.d.mts +1 -1
- package/dist/esm/version.mjs +1 -1
- package/dist/esm/wrapper/BatchUsage.d.mts +1 -1
- package/dist/esm/wrapper/BatchUsage.mjs +1 -1
- package/package.json +7 -3
- package/dist/cjs/tracing/index.d.ts +0 -1
- package/dist/cjs/tracing/index.js +0 -17
- package/dist/esm/tracing/index.d.mts +0 -1
- package/dist/esm/tracing/index.mjs +0 -1
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PaidLangChainCallback = void 0;
|
|
13
|
+
const base_1 = require("@langchain/core/callbacks/base");
|
|
14
|
+
const api_1 = require("@opentelemetry/api");
|
|
15
|
+
const tracing_js_1 = require("../tracing.js");
|
|
16
|
+
class PaidLangChainCallback extends base_1.BaseCallbackHandler {
|
|
17
|
+
constructor() {
|
|
18
|
+
super();
|
|
19
|
+
this.name = "PaidLangChainCallback";
|
|
20
|
+
this.spans = new Map();
|
|
21
|
+
this.tracer = api_1.trace.getTracer("paid.node");
|
|
22
|
+
}
|
|
23
|
+
extractProvider(serialized) {
|
|
24
|
+
if (!(serialized === null || serialized === void 0 ? void 0 : serialized.id))
|
|
25
|
+
return "unknown";
|
|
26
|
+
const idStr = serialized.id.join(" ").toLowerCase();
|
|
27
|
+
if (idStr.includes("openai"))
|
|
28
|
+
return "openai";
|
|
29
|
+
if (idStr.includes("anthropic"))
|
|
30
|
+
return "anthropic";
|
|
31
|
+
if (idStr.includes("mistral"))
|
|
32
|
+
return "mistral";
|
|
33
|
+
if (idStr.includes("cohere"))
|
|
34
|
+
return "cohere";
|
|
35
|
+
if (idStr.includes("huggingface"))
|
|
36
|
+
return "huggingface";
|
|
37
|
+
if (idStr.includes("azure"))
|
|
38
|
+
return "azure";
|
|
39
|
+
return "unknown";
|
|
40
|
+
}
|
|
41
|
+
handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata) {
|
|
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
|
+
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
48
|
+
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
49
|
+
const token = (0, tracing_js_1.getTokenStorage)();
|
|
50
|
+
if (!token || !externalCustomerId) {
|
|
51
|
+
throw new Error("No token or externalCustomerId: This wrapper should be used inside a callback to paid.trace().");
|
|
52
|
+
}
|
|
53
|
+
const modelType = (metadata === null || metadata === void 0 ? void 0 : metadata.ls_model_type) || "unknown";
|
|
54
|
+
const modelName = (metadata === null || metadata === void 0 ? void 0 : metadata.ls_model_name) || "unknown";
|
|
55
|
+
const spanName = `trace.langchain.${modelType}`;
|
|
56
|
+
const span = this.tracer.startSpan(spanName);
|
|
57
|
+
const attributes = {
|
|
58
|
+
"gen_ai.system": this.extractProvider(llm),
|
|
59
|
+
"gen_ai.operation.name": modelType,
|
|
60
|
+
"gen_ai.request.model": modelName,
|
|
61
|
+
external_customer_id: externalCustomerId,
|
|
62
|
+
token: token,
|
|
63
|
+
};
|
|
64
|
+
if (externalAgentId) {
|
|
65
|
+
attributes["external_agent_id"] = externalAgentId;
|
|
66
|
+
}
|
|
67
|
+
span.setAttributes(attributes);
|
|
68
|
+
this.spans.set(runId, span);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
handleLLMEnd(output, runId, parentRunId) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
74
|
+
const span = this.spans.get(runId);
|
|
75
|
+
if (!span)
|
|
76
|
+
return;
|
|
77
|
+
try {
|
|
78
|
+
const attributes = {};
|
|
79
|
+
// Extract token usage information
|
|
80
|
+
if ((_a = output.llmOutput) === null || _a === void 0 ? void 0 : _a.tokenUsage) {
|
|
81
|
+
const usage = output.llmOutput.tokenUsage;
|
|
82
|
+
if (usage.promptTokens !== undefined) {
|
|
83
|
+
attributes["gen_ai.usage.input_tokens"] = usage.promptTokens;
|
|
84
|
+
}
|
|
85
|
+
if (usage.completionTokens !== undefined) {
|
|
86
|
+
attributes["gen_ai.usage.output_tokens"] = usage.completionTokens;
|
|
87
|
+
}
|
|
88
|
+
if (usage.cachedInputTokens !== undefined) {
|
|
89
|
+
attributes["gen_ai.usage.cached_input_tokens"] = usage.cachedInputTokens;
|
|
90
|
+
}
|
|
91
|
+
if (usage.reasoningOutputTokens !== undefined) {
|
|
92
|
+
attributes["gen_ai.usage.reasoning_output_tokens"] = usage.reasoningOutputTokens;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Alternative token usage format (some providers use different field names)
|
|
96
|
+
if ((_b = output.llmOutput) === null || _b === void 0 ? void 0 : _b.token_usage) {
|
|
97
|
+
const usage = output.llmOutput.token_usage;
|
|
98
|
+
if (usage.prompt_tokens !== undefined) {
|
|
99
|
+
attributes["gen_ai.usage.input_tokens"] = usage.prompt_tokens;
|
|
100
|
+
}
|
|
101
|
+
if (usage.completion_tokens !== undefined) {
|
|
102
|
+
attributes["gen_ai.usage.output_tokens"] = usage.completion_tokens;
|
|
103
|
+
}
|
|
104
|
+
if (usage.cached_input_tokens !== undefined) {
|
|
105
|
+
attributes["gen_ai.usage.cached_input_tokens"] = usage.cached_input_tokens;
|
|
106
|
+
}
|
|
107
|
+
if (usage.reasoning_output_tokens !== undefined) {
|
|
108
|
+
attributes["gen_ai.usage.reasoning_output_tokens"] = usage.reasoning_output_tokens;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Add model from response if available
|
|
112
|
+
if ((_c = output.llmOutput) === null || _c === void 0 ? void 0 : _c.modelName) {
|
|
113
|
+
attributes["gen_ai.response.model"] = output.llmOutput.modelName;
|
|
114
|
+
}
|
|
115
|
+
else if ((_d = output.llmOutput) === null || _d === void 0 ? void 0 : _d.model_name) {
|
|
116
|
+
attributes["gen_ai.response.model"] = output.llmOutput.model_name;
|
|
117
|
+
}
|
|
118
|
+
else if ((_j = (_h = (_g = (_f = (_e = output.generations) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.message) === null || _h === void 0 ? void 0 : _h["response_metadata"]) === null || _j === void 0 ? void 0 : _j.model_name) {
|
|
119
|
+
attributes["gen_ai.response.model"] = output.generations[0][0].message["response_metadata"].model_name;
|
|
120
|
+
}
|
|
121
|
+
span.setAttributes(attributes);
|
|
122
|
+
span.setStatus({ code: api_1.SpanStatusCode.OK });
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
span.end();
|
|
126
|
+
this.spans.delete(runId);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
handleLLMError(err, runId, parentRunId) {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
const span = this.spans.get(runId);
|
|
133
|
+
if (!span)
|
|
134
|
+
return;
|
|
135
|
+
try {
|
|
136
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: err.message });
|
|
137
|
+
span.recordException(err);
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
span.end();
|
|
141
|
+
this.spans.delete(runId);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.PaidLangChainCallback = PaidLangChainCallback;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Mistral } from "@mistralai/mistralai";
|
|
2
|
+
import { Tracer } from "@opentelemetry/api";
|
|
3
|
+
import { OCRRequest, OCRResponse } from "@mistralai/mistralai/models/components";
|
|
4
|
+
import { RequestOptions } from "@mistralai/mistralai/lib/sdks";
|
|
5
|
+
export declare class PaidMistral {
|
|
6
|
+
private readonly mistral;
|
|
7
|
+
private readonly tracer;
|
|
8
|
+
constructor(mistralClient: Mistral);
|
|
9
|
+
get ocr(): OCRWrapper;
|
|
10
|
+
}
|
|
11
|
+
declare class OCRWrapper {
|
|
12
|
+
private mistral;
|
|
13
|
+
private tracer;
|
|
14
|
+
constructor(mistral: Mistral, tracer: Tracer);
|
|
15
|
+
process(request: OCRRequest, options?: RequestOptions): Promise<OCRResponse>;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PaidMistral = void 0;
|
|
13
|
+
const api_1 = require("@opentelemetry/api");
|
|
14
|
+
const tracing_js_1 = require("../tracing.js");
|
|
15
|
+
class PaidMistral {
|
|
16
|
+
constructor(mistralClient) {
|
|
17
|
+
this.mistral = mistralClient;
|
|
18
|
+
this.tracer = api_1.trace.getTracer("paid.node");
|
|
19
|
+
}
|
|
20
|
+
get ocr() {
|
|
21
|
+
return new OCRWrapper(this.mistral, this.tracer);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.PaidMistral = PaidMistral;
|
|
25
|
+
class OCRWrapper {
|
|
26
|
+
constructor(mistral, tracer) {
|
|
27
|
+
this.mistral = mistral;
|
|
28
|
+
this.tracer = tracer;
|
|
29
|
+
}
|
|
30
|
+
process(request, options) {
|
|
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
|
+
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
37
|
+
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
38
|
+
const token = (0, tracing_js_1.getTokenStorage)();
|
|
39
|
+
if (!token || !externalCustomerId) {
|
|
40
|
+
throw new Error("No token or externalCustomerId: This wrapper should be used inside a callback to paid.trace().");
|
|
41
|
+
}
|
|
42
|
+
return this.tracer.startActiveSpan("trace.mistral.ocr", (span) => __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
var _a;
|
|
44
|
+
const attributes = {
|
|
45
|
+
"gen_ai.system": "mistral",
|
|
46
|
+
"gen_ai.operation.name": "ocr",
|
|
47
|
+
external_customer_id: externalCustomerId,
|
|
48
|
+
token: token,
|
|
49
|
+
};
|
|
50
|
+
if (externalAgentId) {
|
|
51
|
+
attributes["external_agent_id"] = externalAgentId;
|
|
52
|
+
}
|
|
53
|
+
// Check if annotations are requested
|
|
54
|
+
if (request.bboxAnnotationFormat || request.documentAnnotationFormat) {
|
|
55
|
+
attributes["gen_ai.ocr.annotated"] = "true";
|
|
56
|
+
}
|
|
57
|
+
// Set request model
|
|
58
|
+
if (request.model) {
|
|
59
|
+
attributes["gen_ai.request.model"] = request.model;
|
|
60
|
+
}
|
|
61
|
+
span.setAttributes(attributes);
|
|
62
|
+
try {
|
|
63
|
+
const response = yield this.mistral.ocr.process(request, options);
|
|
64
|
+
// Add usage information from response
|
|
65
|
+
if (((_a = response.usageInfo) === null || _a === void 0 ? void 0 : _a.pagesProcessed) !== undefined) {
|
|
66
|
+
span.setAttribute("gen_ai.ocr.pages_processed", response.usageInfo.pagesProcessed);
|
|
67
|
+
}
|
|
68
|
+
if (response.model) {
|
|
69
|
+
span.setAttribute("gen_ai.response.model", response.model);
|
|
70
|
+
}
|
|
71
|
+
span.setStatus({ code: api_1.SpanStatusCode.OK });
|
|
72
|
+
return response;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: error.message });
|
|
76
|
+
span.recordException(error);
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
span.end();
|
|
81
|
+
}
|
|
82
|
+
}));
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -11,7 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.PaidOpenAI = void 0;
|
|
13
13
|
const api_1 = require("@opentelemetry/api");
|
|
14
|
-
const
|
|
14
|
+
const tracing_js_1 = require("../tracing.js");
|
|
15
15
|
class PaidOpenAI {
|
|
16
16
|
constructor(openaiClient) {
|
|
17
17
|
this.openai = openaiClient;
|
|
@@ -49,23 +49,23 @@ class ChatCompletionsWrapper {
|
|
|
49
49
|
return __awaiter(this, void 0, void 0, function* () {
|
|
50
50
|
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
51
51
|
if (!currentSpan) {
|
|
52
|
-
|
|
53
|
-
return this.openai.chat.completions.create(params);
|
|
52
|
+
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
54
53
|
}
|
|
55
|
-
const externalCustomerId = (0,
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
55
|
+
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
56
|
+
const token = (0, tracing_js_1.getTokenStorage)();
|
|
57
|
+
if (!token || !externalCustomerId) {
|
|
58
|
+
throw new Error("No token or externalCustomerId: This wrapper should be used inside a callback to paid.trace().");
|
|
59
|
+
}
|
|
60
|
+
return this.tracer.startActiveSpan("trace.openai.chat", (span) => __awaiter(this, void 0, void 0, function* () {
|
|
60
61
|
const attributes = {
|
|
61
62
|
"gen_ai.system": "openai",
|
|
62
63
|
"gen_ai.operation.name": "chat",
|
|
63
64
|
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
attributes["token"] = token;
|
|
65
|
+
attributes["external_customer_id"] = externalCustomerId;
|
|
66
|
+
attributes["token"] = token;
|
|
67
|
+
if (externalAgentId) {
|
|
68
|
+
attributes["external_agent_id"] = externalAgentId;
|
|
69
69
|
}
|
|
70
70
|
span.setAttributes(attributes);
|
|
71
71
|
try {
|
|
@@ -101,22 +101,24 @@ class ResponsesWrapper {
|
|
|
101
101
|
return __awaiter(this, void 0, void 0, function* () {
|
|
102
102
|
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
103
103
|
if (!currentSpan) {
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
105
|
+
}
|
|
106
|
+
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
107
|
+
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
108
|
+
const token = (0, tracing_js_1.getTokenStorage)();
|
|
109
|
+
if (!token || !externalCustomerId) {
|
|
110
|
+
throw new Error("No token or externalCustomerId: This wrapper should be used inside a callback to paid.trace().");
|
|
106
111
|
}
|
|
107
|
-
const externalCustomerId = (0, tracing_1.getCustomerId)();
|
|
108
|
-
const token = (0, tracing_1.getTokenStorage)();
|
|
109
112
|
return this.tracer.startActiveSpan("trace.openai.responses", (span) => __awaiter(this, void 0, void 0, function* () {
|
|
110
113
|
var _a, _b;
|
|
111
114
|
const attributes = {
|
|
112
115
|
"gen_ai.system": "openai",
|
|
113
116
|
"gen_ai.operation.name": "chat", // Equivalent to chat.completions
|
|
114
117
|
};
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
attributes["token"] = token;
|
|
118
|
+
attributes["external_customer_id"] = externalCustomerId;
|
|
119
|
+
attributes["token"] = token;
|
|
120
|
+
if (externalAgentId) {
|
|
121
|
+
attributes["external_agent_id"] = externalAgentId;
|
|
120
122
|
}
|
|
121
123
|
span.setAttributes(attributes);
|
|
122
124
|
try {
|
|
@@ -156,26 +158,25 @@ class EmbeddingsWrapper {
|
|
|
156
158
|
}
|
|
157
159
|
create(params) {
|
|
158
160
|
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
-
var _a;
|
|
160
161
|
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
161
162
|
if (!currentSpan) {
|
|
162
|
-
|
|
163
|
-
return this.openai.embeddings.create(params);
|
|
163
|
+
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
164
164
|
}
|
|
165
|
-
const externalCustomerId = (0,
|
|
166
|
-
const
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
165
|
+
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
166
|
+
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
167
|
+
const token = (0, tracing_js_1.getTokenStorage)();
|
|
168
|
+
if (!token || !externalCustomerId) {
|
|
169
|
+
throw new Error("No token or externalCustomerId: This wrapper should be used inside a callback to paid.trace().");
|
|
170
|
+
}
|
|
171
|
+
return this.tracer.startActiveSpan("trace.openai.embeddings", (span) => __awaiter(this, void 0, void 0, function* () {
|
|
170
172
|
const attributes = {
|
|
171
173
|
"gen_ai.system": "openai",
|
|
172
174
|
"gen_ai.operation.name": "embeddings",
|
|
173
175
|
};
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
attributes["token"] = token;
|
|
176
|
+
attributes["external_customer_id"] = externalCustomerId;
|
|
177
|
+
attributes["token"] = token;
|
|
178
|
+
if (externalAgentId) {
|
|
179
|
+
attributes["external_agent_id"] = externalAgentId;
|
|
179
180
|
}
|
|
180
181
|
span.setAttributes(attributes);
|
|
181
182
|
try {
|
|
@@ -211,25 +212,26 @@ class ImagesWrapper {
|
|
|
211
212
|
var _a;
|
|
212
213
|
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
213
214
|
if (!currentSpan) {
|
|
214
|
-
|
|
215
|
-
return this.openai.images.generate(params);
|
|
215
|
+
throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
|
|
216
216
|
}
|
|
217
|
-
const externalCustomerId = (0,
|
|
218
|
-
const
|
|
217
|
+
const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
|
|
218
|
+
const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
|
|
219
|
+
const token = (0, tracing_js_1.getTokenStorage)();
|
|
219
220
|
const model = (_a = params.model) !== null && _a !== void 0 ? _a : "dall-e-3";
|
|
220
|
-
|
|
221
|
-
|
|
221
|
+
if (!token || !externalCustomerId) {
|
|
222
|
+
throw new Error("No token or externalCustomerId: This wrapper should be used inside a callback to paid.trace().");
|
|
223
|
+
}
|
|
224
|
+
return this.tracer.startActiveSpan("trace.openai.images", (span) => __awaiter(this, void 0, void 0, function* () {
|
|
222
225
|
var _a, _b, _c;
|
|
223
226
|
const attributes = {
|
|
224
227
|
"gen_ai.request.model": model,
|
|
225
228
|
"gen_ai.system": "openai",
|
|
226
229
|
"gen_ai.operation.name": "image_generation",
|
|
227
230
|
};
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
attributes["token"] = token;
|
|
231
|
+
attributes["external_customer_id"] = externalCustomerId;
|
|
232
|
+
attributes["token"] = token;
|
|
233
|
+
if (externalAgentId) {
|
|
234
|
+
attributes["external_agent_id"] = externalAgentId;
|
|
233
235
|
}
|
|
234
236
|
span.setAttributes(attributes);
|
|
235
237
|
try {
|
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-alpha2";
|
package/dist/cjs/version.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as core from "../core/index.js";
|
|
2
|
-
import { Usage as UsageClient } from "../api/resources/usage/client/Client";
|
|
2
|
+
import { Usage as UsageClient } from "../api/resources/usage/client/Client.js";
|
|
3
3
|
import * as Paid from "../api/types/Signal.js";
|
|
4
4
|
export declare class Usage extends UsageClient {
|
|
5
5
|
private signals;
|
|
@@ -35,8 +35,8 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.Usage = void 0;
|
|
37
37
|
const core = __importStar(require("../core/index.js"));
|
|
38
|
-
const
|
|
39
|
-
class Usage extends
|
|
38
|
+
const Client_js_1 = require("../api/resources/usage/client/Client.js");
|
|
39
|
+
class Usage extends Client_js_1.Usage {
|
|
40
40
|
constructor() {
|
|
41
41
|
super(...arguments);
|
|
42
42
|
this.signals = [];
|
package/dist/esm/Client.d.mts
CHANGED
|
@@ -41,6 +41,8 @@ export declare class PaidClient {
|
|
|
41
41
|
get contacts(): Contacts;
|
|
42
42
|
get orders(): Orders;
|
|
43
43
|
get usage(): Usage;
|
|
44
|
-
initializeTracing(): Promise<void>;
|
|
45
|
-
|
|
44
|
+
initializeTracing(collectorEndpoint?: string): Promise<void>;
|
|
45
|
+
trace<T extends (...args: any[]) => any>(externalCustomerId: string, fn: T, externalAgentId?: string, ...args: Parameters<T>): Promise<ReturnType<T>>;
|
|
46
|
+
signal(eventName: string, data?: Record<string, any>): void;
|
|
47
|
+
getSpanProcessorAndInitialize(collectorEndpoint?: string): Promise<import("@opentelemetry/sdk-trace-base").SpanProcessor>;
|
|
46
48
|
}
|
package/dist/esm/Client.mjs
CHANGED
|
@@ -17,7 +17,8 @@ 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 {
|
|
20
|
+
import { _trace, _initializeTracing, _getSpanProcessorAndInitialize } from "./tracing/tracing.mjs";
|
|
21
|
+
import { _signal } from "./tracing/signal.mjs";
|
|
21
22
|
export class PaidClient {
|
|
22
23
|
constructor(_options) {
|
|
23
24
|
this._options = Object.assign(Object.assign({}, _options), { headers: mergeHeaders({
|
|
@@ -49,17 +50,33 @@ export class PaidClient {
|
|
|
49
50
|
var _a;
|
|
50
51
|
return ((_a = this._usage) !== null && _a !== void 0 ? _a : (this._usage = new Usage(this._options)));
|
|
51
52
|
}
|
|
53
|
+
// Use this method if you want paid to initialize tracing automatically.
|
|
52
54
|
initializeTracing() {
|
|
53
|
-
return __awaiter(this,
|
|
55
|
+
return __awaiter(this, arguments, void 0, function* (collectorEndpoint = "https://collector.agentpaid.io:4318/v1/traces") {
|
|
54
56
|
const tokenSupplier = this._options.token;
|
|
55
57
|
const token = typeof tokenSupplier === "function" ? yield tokenSupplier() : tokenSupplier;
|
|
56
58
|
const resolvedToken = yield Promise.resolve(token);
|
|
57
|
-
|
|
59
|
+
_initializeTracing(resolvedToken, collectorEndpoint);
|
|
58
60
|
});
|
|
59
61
|
}
|
|
60
|
-
|
|
62
|
+
// Use this method to track actions like LLM usage and sending signals.
|
|
63
|
+
// The callback to this function is the work that you want to trace.
|
|
64
|
+
trace(externalCustomerId, fn, externalAgentId, ...args) {
|
|
61
65
|
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
-
return yield
|
|
66
|
+
return yield _trace(externalCustomerId, fn, externalAgentId, ...args);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
// sends Paid signal. This needs to called as part of callback to Paid.trace()
|
|
70
|
+
signal(eventName, data) {
|
|
71
|
+
return _signal(eventName, data);
|
|
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);
|
|
63
80
|
});
|
|
64
81
|
}
|
|
65
82
|
}
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -3,4 +3,6 @@ export { PaidClient } from "./Client.mjs";
|
|
|
3
3
|
export { PaidError, PaidTimeoutError } from "./errors/index.mjs";
|
|
4
4
|
export { PaidEnvironment } from "./environments.mjs";
|
|
5
5
|
export { PaidOpenAI } from "./tracing/wrappers/openAiWrapper.mjs";
|
|
6
|
-
export {
|
|
6
|
+
export { PaidAnthropic } from "./tracing/wrappers/anthropicWrapper.mjs";
|
|
7
|
+
export { PaidMistral } from "./tracing/wrappers/mistralWrapper.mjs";
|
|
8
|
+
export { PaidLangChainCallback } from "./tracing/wrappers/langchainCallback.mjs";
|
package/dist/esm/index.mjs
CHANGED
|
@@ -3,4 +3,6 @@ export { PaidClient } from "./Client.mjs";
|
|
|
3
3
|
export { PaidError, PaidTimeoutError } from "./errors/index.mjs";
|
|
4
4
|
export { PaidEnvironment } from "./environments.mjs";
|
|
5
5
|
export { PaidOpenAI } from "./tracing/wrappers/openAiWrapper.mjs";
|
|
6
|
-
export {
|
|
6
|
+
export { PaidAnthropic } from "./tracing/wrappers/anthropicWrapper.mjs";
|
|
7
|
+
export { PaidMistral } from "./tracing/wrappers/mistralWrapper.mjs";
|
|
8
|
+
export { PaidLangChainCallback } from "./tracing/wrappers/langchainCallback.mjs";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function _signal(eventName: string, data?: Record<string, any>): void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { trace, SpanStatusCode } from "@opentelemetry/api";
|
|
2
|
+
import { getCustomerIdStorage, getAgentIdStorage, getTokenStorage } from "./tracing.mjs";
|
|
3
|
+
export function _signal(eventName, data) {
|
|
4
|
+
if (!eventName) {
|
|
5
|
+
throw new Error("Event name is required for signal.");
|
|
6
|
+
}
|
|
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
|
+
const externalCustomerId = getCustomerIdStorage();
|
|
13
|
+
const externalAgentId = getAgentIdStorage();
|
|
14
|
+
const token = getTokenStorage();
|
|
15
|
+
if (!externalCustomerId || !externalAgentId || !token) {
|
|
16
|
+
throw new Error(`Missing some of: external_customer_id: ${externalCustomerId}, external_agent_id: ${externalAgentId}, or token. Make sure to call signal() within trace()`);
|
|
17
|
+
}
|
|
18
|
+
const tracer = trace.getTracer("paid.node");
|
|
19
|
+
tracer.startActiveSpan("trace.signal", (span) => {
|
|
20
|
+
try {
|
|
21
|
+
const attributes = {
|
|
22
|
+
external_customer_id: externalCustomerId,
|
|
23
|
+
external_agent_id: externalAgentId,
|
|
24
|
+
event_name: eventName,
|
|
25
|
+
token: token,
|
|
26
|
+
};
|
|
27
|
+
// Optional data (ex. manual cost tracking)
|
|
28
|
+
if (data) {
|
|
29
|
+
attributes["data"] = JSON.stringify(data);
|
|
30
|
+
}
|
|
31
|
+
span.setAttributes(attributes);
|
|
32
|
+
// Mark span as successful
|
|
33
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
span.setStatus({
|
|
37
|
+
code: SpanStatusCode.ERROR,
|
|
38
|
+
message: error.message,
|
|
39
|
+
});
|
|
40
|
+
span.recordException(error);
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
span.end();
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
2
|
+
import winston from "winston";
|
|
3
|
+
export declare const logger: winston.Logger;
|
|
4
|
+
export declare const getCustomerIdStorage: () => string | null | undefined;
|
|
5
|
+
export declare const getAgentIdStorage: () => string | null | undefined;
|
|
2
6
|
export declare const getTokenStorage: () => string | null | undefined;
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function
|
|
7
|
+
export declare function _initializeTracing(apiKey: string, collectorEndpoint: string): void;
|
|
8
|
+
export declare function _getSpanProcessorAndInitialize(apiKey: string, collectorEndpoint: string): SpanProcessor;
|
|
9
|
+
export declare function _trace<T extends (...args: any[]) => any>(externalCustomerId: string, fn: T, externalAgentId: string | undefined, ...args: Parameters<T>): Promise<ReturnType<T>>;
|