@paid-ai/paid-node 0.0.8-alpha0 → 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.
Files changed (44) hide show
  1. package/README.md +17 -16
  2. package/dist/cjs/Client.d.ts +4 -2
  3. package/dist/cjs/Client.js +21 -4
  4. package/dist/cjs/index.d.ts +3 -1
  5. package/dist/cjs/index.js +7 -4
  6. package/dist/cjs/tracing/signal.d.ts +1 -0
  7. package/dist/cjs/tracing/signal.js +49 -0
  8. package/dist/cjs/tracing/tracing.d.ts +8 -3
  9. package/dist/cjs/tracing/tracing.js +100 -28
  10. package/dist/cjs/tracing/wrappers/anthropicWrapper.d.ts +16 -0
  11. package/dist/cjs/tracing/wrappers/anthropicWrapper.js +88 -0
  12. package/dist/cjs/tracing/wrappers/langchainCallback.d.ts +21 -0
  13. package/dist/cjs/tracing/wrappers/langchainCallback.js +146 -0
  14. package/dist/cjs/tracing/wrappers/mistralWrapper.d.ts +17 -0
  15. package/dist/cjs/tracing/wrappers/mistralWrapper.js +85 -0
  16. package/dist/cjs/tracing/wrappers/openAiWrapper.js +58 -52
  17. package/dist/cjs/version.d.ts +1 -1
  18. package/dist/cjs/version.js +1 -1
  19. package/dist/cjs/wrapper/BatchUsage.d.ts +1 -1
  20. package/dist/cjs/wrapper/BatchUsage.js +2 -2
  21. package/dist/esm/Client.d.mts +4 -2
  22. package/dist/esm/Client.mjs +22 -5
  23. package/dist/esm/index.d.mts +3 -1
  24. package/dist/esm/index.mjs +3 -1
  25. package/dist/esm/tracing/signal.d.mts +1 -0
  26. package/dist/esm/tracing/signal.mjs +46 -0
  27. package/dist/esm/tracing/tracing.d.mts +8 -3
  28. package/dist/esm/tracing/tracing.mjs +94 -27
  29. package/dist/esm/tracing/wrappers/anthropicWrapper.d.mts +16 -0
  30. package/dist/esm/tracing/wrappers/anthropicWrapper.mjs +84 -0
  31. package/dist/esm/tracing/wrappers/langchainCallback.d.mts +21 -0
  32. package/dist/esm/tracing/wrappers/langchainCallback.mjs +142 -0
  33. package/dist/esm/tracing/wrappers/mistralWrapper.d.mts +17 -0
  34. package/dist/esm/tracing/wrappers/mistralWrapper.mjs +81 -0
  35. package/dist/esm/tracing/wrappers/openAiWrapper.mjs +54 -48
  36. package/dist/esm/version.d.mts +1 -1
  37. package/dist/esm/version.mjs +1 -1
  38. package/dist/esm/wrapper/BatchUsage.d.mts +1 -1
  39. package/dist/esm/wrapper/BatchUsage.mjs +1 -1
  40. package/package.json +7 -3
  41. package/dist/cjs/tracing/index.d.ts +0 -1
  42. package/dist/cjs/tracing/index.js +0 -17
  43. package/dist/esm/tracing/index.d.mts +0 -1
  44. 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 tracing_1 = require("../tracing");
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
- console.warn("No active span found, calling OpenAI directly without tracing.");
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, tracing_1.getCustomerId)();
56
- const token = (0, tracing_1.getTokenStorage)();
57
- const model = params.model;
58
- const spanName = `trace.chat ${model}`;
59
- return this.tracer.startActiveSpan(spanName, (span) => __awaiter(this, void 0, void 0, function* () {
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
- if (externalCustomerId) {
65
- attributes["external_customer_id"] = externalCustomerId;
66
- }
67
- if (token) {
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 {
@@ -99,36 +99,42 @@ class ResponsesWrapper {
99
99
  }
100
100
  create(params) {
101
101
  return __awaiter(this, void 0, void 0, function* () {
102
- var _a;
103
102
  const currentSpan = api_1.trace.getSpan(api_1.context.active());
104
103
  if (!currentSpan) {
105
- console.warn("No active span found, calling OpenAI directly without tracing.");
106
- return this.openai.responses.create(params);
104
+ throw new Error("No active span found, make sure to call this inside of a callback to paid.trace().");
107
105
  }
108
- const externalCustomerId = (0, tracing_1.getCustomerId)();
109
- const token = (0, tracing_1.getTokenStorage)();
110
- const model = (_a = params.model) !== null && _a !== void 0 ? _a : "unknown";
111
- const spanName = `trace.responses ${model}`;
112
- return this.tracer.startActiveSpan(spanName, (span) => __awaiter(this, void 0, void 0, function* () {
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().");
111
+ }
112
+ return this.tracer.startActiveSpan("trace.openai.responses", (span) => __awaiter(this, void 0, void 0, function* () {
113
+ var _a, _b;
113
114
  const attributes = {
114
115
  "gen_ai.system": "openai",
115
116
  "gen_ai.operation.name": "chat", // Equivalent to chat.completions
116
117
  };
117
- if (externalCustomerId) {
118
- attributes["external_customer_id"] = externalCustomerId;
119
- }
120
- if (token) {
121
- attributes["token"] = token;
118
+ attributes["external_customer_id"] = externalCustomerId;
119
+ attributes["token"] = token;
120
+ if (externalAgentId) {
121
+ attributes["external_agent_id"] = externalAgentId;
122
122
  }
123
123
  span.setAttributes(attributes);
124
124
  try {
125
125
  const response = yield this.openai.responses.create(params);
126
126
  if (response.usage) {
127
127
  span.setAttributes({
128
- "gen_ai.usage.input_tokens": response.usage.prompt_tokens,
129
- "gen_ai.usage.output_tokens": response.usage.completion_tokens,
128
+ "gen_ai.usage.input_tokens": response.usage.input_tokens,
129
+ "gen_ai.usage.output_tokens": response.usage.output_tokens,
130
130
  "gen_ai.response.model": response.model,
131
131
  });
132
+ if ((_a = response.usage.input_tokens_details) === null || _a === void 0 ? void 0 : _a.cached_tokens) {
133
+ span.setAttribute("gen_ai.usage.cached_input_tokens", response.usage.input_tokens_details.cached_tokens);
134
+ }
135
+ if ((_b = response.usage.output_tokens_details) === null || _b === void 0 ? void 0 : _b.reasoning_tokens) {
136
+ span.setAttribute("gen_ai.usage.reasoning_output_tokens", response.usage.output_tokens_details.reasoning_tokens);
137
+ }
132
138
  }
133
139
  span.setStatus({ code: api_1.SpanStatusCode.OK });
134
140
  return response;
@@ -152,26 +158,25 @@ class EmbeddingsWrapper {
152
158
  }
153
159
  create(params) {
154
160
  return __awaiter(this, void 0, void 0, function* () {
155
- var _a;
156
161
  const currentSpan = api_1.trace.getSpan(api_1.context.active());
157
162
  if (!currentSpan) {
158
- console.warn("No active span found, calling OpenAI directly without tracing.");
159
- 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
+ }
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().");
160
170
  }
161
- const externalCustomerId = (0, tracing_1.getCustomerId)();
162
- const token = (0, tracing_1.getTokenStorage)();
163
- const model = (_a = params.model) !== null && _a !== void 0 ? _a : "unknown";
164
- const spanName = `trace.embeddings ${model}`;
165
- return this.tracer.startActiveSpan(spanName, (span) => __awaiter(this, void 0, void 0, function* () {
171
+ return this.tracer.startActiveSpan("trace.openai.embeddings", (span) => __awaiter(this, void 0, void 0, function* () {
166
172
  const attributes = {
167
173
  "gen_ai.system": "openai",
168
174
  "gen_ai.operation.name": "embeddings",
169
175
  };
170
- if (externalCustomerId) {
171
- attributes["external_customer_id"] = externalCustomerId;
172
- }
173
- if (token) {
174
- attributes["token"] = token;
176
+ attributes["external_customer_id"] = externalCustomerId;
177
+ attributes["token"] = token;
178
+ if (externalAgentId) {
179
+ attributes["external_agent_id"] = externalAgentId;
175
180
  }
176
181
  span.setAttributes(attributes);
177
182
  try {
@@ -207,25 +212,26 @@ class ImagesWrapper {
207
212
  var _a;
208
213
  const currentSpan = api_1.trace.getSpan(api_1.context.active());
209
214
  if (!currentSpan) {
210
- console.warn("No active span found, calling OpenAI directly without tracing.");
211
- 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().");
212
216
  }
213
- const externalCustomerId = (0, tracing_1.getCustomerId)();
214
- const token = (0, tracing_1.getTokenStorage)();
217
+ const externalCustomerId = (0, tracing_js_1.getCustomerIdStorage)();
218
+ const externalAgentId = (0, tracing_js_1.getAgentIdStorage)();
219
+ const token = (0, tracing_js_1.getTokenStorage)();
215
220
  const model = (_a = params.model) !== null && _a !== void 0 ? _a : "dall-e-3";
216
- const spanName = `trace.images ${model}`;
217
- return this.tracer.startActiveSpan(spanName, (span) => __awaiter(this, void 0, void 0, function* () {
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* () {
218
225
  var _a, _b, _c;
219
226
  const attributes = {
220
227
  "gen_ai.request.model": model,
221
228
  "gen_ai.system": "openai",
222
229
  "gen_ai.operation.name": "image_generation",
223
230
  };
224
- if (externalCustomerId) {
225
- attributes["external_customer_id"] = externalCustomerId;
226
- }
227
- if (token) {
228
- attributes["token"] = token;
231
+ attributes["external_customer_id"] = externalCustomerId;
232
+ attributes["token"] = token;
233
+ if (externalAgentId) {
234
+ attributes["external_agent_id"] = externalAgentId;
229
235
  }
230
236
  span.setAttributes(attributes);
231
237
  try {
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.0.8-alpha0";
1
+ export declare const SDK_VERSION = "0.0.8-alpha2";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = "0.0.8-alpha0";
4
+ exports.SDK_VERSION = "0.0.8-alpha2";
@@ -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 Client_1 = require("../api/resources/usage/client/Client");
39
- class Usage extends Client_1.Usage {
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 = [];
@@ -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
- capture<T extends (...args: any[]) => any>(externalCustomerId: string, fn: T, ...args: Parameters<T>): Promise<ReturnType<T>>;
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
  }
@@ -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 { capture, initializeTracing } from "./tracing/tracing.mjs";
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, void 0, void 0, function* () {
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
- initializeTracing(resolvedToken);
59
+ _initializeTracing(resolvedToken, collectorEndpoint);
58
60
  });
59
61
  }
60
- capture(externalCustomerId, fn, ...args) {
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 capture(externalCustomerId, fn, ...args);
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
  }
@@ -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 { initializeTracing, capture } from "./tracing/tracing.mjs";
6
+ export { PaidAnthropic } from "./tracing/wrappers/anthropicWrapper.mjs";
7
+ export { PaidMistral } from "./tracing/wrappers/mistralWrapper.mjs";
8
+ export { PaidLangChainCallback } from "./tracing/wrappers/langchainCallback.mjs";
@@ -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 { initializeTracing, capture } from "./tracing/tracing.mjs";
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
- export declare const getCustomerId: () => string | null | undefined;
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 initializeTracing(apiKey: string): void;
4
- export declare function capture<T extends (...args: any[]) => any>(externalCustomerId: string, fn: T, ...args: Parameters<T>): Promise<ReturnType<T>>;
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>>;