risicare 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/frameworks/instructor.cjs +178 -0
- package/dist/frameworks/instructor.cjs.map +1 -0
- package/dist/frameworks/instructor.d.cts +28 -0
- package/dist/frameworks/instructor.d.ts +28 -0
- package/dist/frameworks/instructor.js +151 -0
- package/dist/frameworks/instructor.js.map +1 -0
- package/dist/frameworks/langchain.cjs +262 -0
- package/dist/frameworks/langchain.cjs.map +1 -0
- package/dist/frameworks/langchain.d.cts +45 -0
- package/dist/frameworks/langchain.d.ts +45 -0
- package/dist/frameworks/langchain.js +235 -0
- package/dist/frameworks/langchain.js.map +1 -0
- package/dist/frameworks/langgraph.cjs +296 -0
- package/dist/frameworks/langgraph.cjs.map +1 -0
- package/dist/frameworks/langgraph.d.cts +28 -0
- package/dist/frameworks/langgraph.d.ts +28 -0
- package/dist/frameworks/langgraph.js +269 -0
- package/dist/frameworks/langgraph.js.map +1 -0
- package/dist/frameworks/llamaindex.cjs +239 -0
- package/dist/frameworks/llamaindex.cjs.map +1 -0
- package/dist/frameworks/llamaindex.d.cts +55 -0
- package/dist/frameworks/llamaindex.d.ts +55 -0
- package/dist/frameworks/llamaindex.js +212 -0
- package/dist/frameworks/llamaindex.js.map +1 -0
- package/dist/index.cjs +468 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +124 -6
- package/dist/index.d.ts +124 -6
- package/dist/index.js +459 -7
- package/dist/index.js.map +1 -1
- package/dist/providers/anthropic/index.cjs +22 -0
- package/dist/providers/anthropic/index.cjs.map +1 -1
- package/dist/providers/anthropic/index.js +22 -0
- package/dist/providers/anthropic/index.js.map +1 -1
- package/dist/providers/bedrock/index.cjs +334 -0
- package/dist/providers/bedrock/index.cjs.map +1 -0
- package/dist/providers/bedrock/index.d.cts +37 -0
- package/dist/providers/bedrock/index.d.ts +37 -0
- package/dist/providers/bedrock/index.js +307 -0
- package/dist/providers/bedrock/index.js.map +1 -0
- package/dist/providers/cerebras/index.cjs +282 -0
- package/dist/providers/cerebras/index.cjs.map +1 -0
- package/dist/providers/cerebras/index.d.cts +24 -0
- package/dist/providers/cerebras/index.d.ts +24 -0
- package/dist/providers/cerebras/index.js +255 -0
- package/dist/providers/cerebras/index.js.map +1 -0
- package/dist/providers/cohere/index.cjs +347 -0
- package/dist/providers/cohere/index.cjs.map +1 -0
- package/dist/providers/cohere/index.d.cts +24 -0
- package/dist/providers/cohere/index.d.ts +24 -0
- package/dist/providers/cohere/index.js +320 -0
- package/dist/providers/cohere/index.js.map +1 -0
- package/dist/providers/google/index.cjs +337 -0
- package/dist/providers/google/index.cjs.map +1 -0
- package/dist/providers/google/index.d.cts +25 -0
- package/dist/providers/google/index.d.ts +25 -0
- package/dist/providers/google/index.js +310 -0
- package/dist/providers/google/index.js.map +1 -0
- package/dist/providers/groq/index.cjs +282 -0
- package/dist/providers/groq/index.cjs.map +1 -0
- package/dist/providers/groq/index.d.cts +23 -0
- package/dist/providers/groq/index.d.ts +23 -0
- package/dist/providers/groq/index.js +255 -0
- package/dist/providers/groq/index.js.map +1 -0
- package/dist/providers/huggingface/index.cjs +289 -0
- package/dist/providers/huggingface/index.cjs.map +1 -0
- package/dist/providers/huggingface/index.d.cts +24 -0
- package/dist/providers/huggingface/index.d.ts +24 -0
- package/dist/providers/huggingface/index.js +262 -0
- package/dist/providers/huggingface/index.js.map +1 -0
- package/dist/providers/mistral/index.cjs +336 -0
- package/dist/providers/mistral/index.cjs.map +1 -0
- package/dist/providers/mistral/index.d.cts +24 -0
- package/dist/providers/mistral/index.d.ts +24 -0
- package/dist/providers/mistral/index.js +309 -0
- package/dist/providers/mistral/index.js.map +1 -0
- package/dist/providers/ollama/index.cjs +376 -0
- package/dist/providers/ollama/index.cjs.map +1 -0
- package/dist/providers/ollama/index.d.cts +28 -0
- package/dist/providers/ollama/index.d.ts +28 -0
- package/dist/providers/ollama/index.js +349 -0
- package/dist/providers/ollama/index.js.map +1 -0
- package/dist/providers/openai/index.cjs +31 -1
- package/dist/providers/openai/index.cjs.map +1 -1
- package/dist/providers/openai/index.js +31 -1
- package/dist/providers/openai/index.js.map +1 -1
- package/dist/providers/together/index.cjs +275 -0
- package/dist/providers/together/index.cjs.map +1 -0
- package/dist/providers/together/index.d.cts +24 -0
- package/dist/providers/together/index.d.ts +24 -0
- package/dist/providers/together/index.js +248 -0
- package/dist/providers/together/index.js.map +1 -0
- package/dist/providers/vercel-ai/index.cjs.map +1 -1
- package/dist/providers/vercel-ai/index.js.map +1 -1
- package/package.json +124 -2
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/providers/huggingface/index.ts
|
|
21
|
+
var huggingface_exports = {};
|
|
22
|
+
__export(huggingface_exports, {
|
|
23
|
+
patchHuggingFace: () => patchHuggingFace
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(huggingface_exports);
|
|
26
|
+
|
|
27
|
+
// src/ids.ts
|
|
28
|
+
var import_node_crypto = require("crypto");
|
|
29
|
+
|
|
30
|
+
// src/noop.ts
|
|
31
|
+
var NOOP_SPAN = Object.freeze({
|
|
32
|
+
traceId: "00000000000000000000000000000000",
|
|
33
|
+
spanId: "0000000000000000",
|
|
34
|
+
parentSpanId: void 0,
|
|
35
|
+
name: "noop",
|
|
36
|
+
kind: "internal" /* INTERNAL */,
|
|
37
|
+
startTime: "",
|
|
38
|
+
startHrtime: 0,
|
|
39
|
+
endTime: void 0,
|
|
40
|
+
status: "unset" /* UNSET */,
|
|
41
|
+
statusMessage: void 0,
|
|
42
|
+
attributes: Object.freeze({}),
|
|
43
|
+
events: Object.freeze([]),
|
|
44
|
+
links: Object.freeze([]),
|
|
45
|
+
sessionId: void 0,
|
|
46
|
+
agentId: void 0,
|
|
47
|
+
agentName: void 0,
|
|
48
|
+
semanticPhase: void 0,
|
|
49
|
+
llmProvider: void 0,
|
|
50
|
+
llmModel: void 0,
|
|
51
|
+
llmPromptTokens: void 0,
|
|
52
|
+
llmCompletionTokens: void 0,
|
|
53
|
+
llmTotalTokens: void 0,
|
|
54
|
+
llmCostUsd: void 0,
|
|
55
|
+
toolName: void 0,
|
|
56
|
+
toolSuccess: void 0,
|
|
57
|
+
isEnded: true,
|
|
58
|
+
durationMs: 0,
|
|
59
|
+
setAttribute() {
|
|
60
|
+
return this;
|
|
61
|
+
},
|
|
62
|
+
setAttributes() {
|
|
63
|
+
return this;
|
|
64
|
+
},
|
|
65
|
+
setStatus() {
|
|
66
|
+
return this;
|
|
67
|
+
},
|
|
68
|
+
addEvent() {
|
|
69
|
+
return this;
|
|
70
|
+
},
|
|
71
|
+
addLink() {
|
|
72
|
+
return this;
|
|
73
|
+
},
|
|
74
|
+
recordException() {
|
|
75
|
+
return this;
|
|
76
|
+
},
|
|
77
|
+
setLlmFields() {
|
|
78
|
+
return this;
|
|
79
|
+
},
|
|
80
|
+
setToolFields() {
|
|
81
|
+
return this;
|
|
82
|
+
},
|
|
83
|
+
end() {
|
|
84
|
+
},
|
|
85
|
+
toPayload() {
|
|
86
|
+
return {
|
|
87
|
+
traceId: this.traceId,
|
|
88
|
+
spanId: this.spanId,
|
|
89
|
+
name: this.name,
|
|
90
|
+
kind: this.kind,
|
|
91
|
+
startTime: this.startTime,
|
|
92
|
+
status: this.status,
|
|
93
|
+
attributes: {},
|
|
94
|
+
events: [],
|
|
95
|
+
links: []
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// src/globals.ts
|
|
101
|
+
var import_node_async_hooks = require("async_hooks");
|
|
102
|
+
var G = globalThis;
|
|
103
|
+
var PREFIX = "__risicare_";
|
|
104
|
+
function getTracer() {
|
|
105
|
+
return G[PREFIX + "tracer"];
|
|
106
|
+
}
|
|
107
|
+
function getContextStorage() {
|
|
108
|
+
if (!G[PREFIX + "ctx"]) {
|
|
109
|
+
G[PREFIX + "ctx"] = new import_node_async_hooks.AsyncLocalStorage();
|
|
110
|
+
}
|
|
111
|
+
return G[PREFIX + "ctx"];
|
|
112
|
+
}
|
|
113
|
+
function getDebug() {
|
|
114
|
+
return G[PREFIX + "debug"] ?? false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/context/storage.ts
|
|
118
|
+
function storage() {
|
|
119
|
+
return getContextStorage();
|
|
120
|
+
}
|
|
121
|
+
function getContext() {
|
|
122
|
+
return storage().getStore() ?? {};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/utils/log.ts
|
|
126
|
+
function debug(msg) {
|
|
127
|
+
if (getDebug()) {
|
|
128
|
+
process.stderr.write(`[risicare] ${msg}
|
|
129
|
+
`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/client.ts
|
|
134
|
+
function requireTracer() {
|
|
135
|
+
const tracer = getTracer();
|
|
136
|
+
if (!tracer) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
"Risicare SDK not initialized. Call init() before using tracing features."
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
return tracer;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// src/utils/pricing.ts
|
|
145
|
+
var PRICING = {
|
|
146
|
+
// OpenAI
|
|
147
|
+
"gpt-4o": { input: 2.5, output: 10 },
|
|
148
|
+
"gpt-4o-mini": { input: 0.15, output: 0.6 },
|
|
149
|
+
"gpt-4-turbo": { input: 10, output: 30 },
|
|
150
|
+
"gpt-4": { input: 30, output: 60 },
|
|
151
|
+
"gpt-3.5-turbo": { input: 0.5, output: 1.5 },
|
|
152
|
+
"o1": { input: 15, output: 60 },
|
|
153
|
+
"o1-mini": { input: 3, output: 12 },
|
|
154
|
+
"o3-mini": { input: 1.1, output: 4.4 },
|
|
155
|
+
// Anthropic
|
|
156
|
+
"claude-opus-4-5-20251101": { input: 15, output: 75 },
|
|
157
|
+
"claude-sonnet-4-5-20250929": { input: 3, output: 15 },
|
|
158
|
+
"claude-haiku-4-5-20251001": { input: 0.8, output: 4 },
|
|
159
|
+
"claude-3-5-sonnet-20241022": { input: 3, output: 15 },
|
|
160
|
+
"claude-3-haiku-20240307": { input: 0.25, output: 1.25 },
|
|
161
|
+
"claude-3-opus-20240229": { input: 15, output: 75 },
|
|
162
|
+
// Google
|
|
163
|
+
"gemini-2.0-flash": { input: 0.1, output: 0.4 },
|
|
164
|
+
"gemini-1.5-pro": { input: 1.25, output: 5 },
|
|
165
|
+
"gemini-1.5-flash": { input: 0.075, output: 0.3 },
|
|
166
|
+
// Groq
|
|
167
|
+
"llama-3.3-70b-versatile": { input: 0.59, output: 0.79 },
|
|
168
|
+
"llama-3.1-8b-instant": { input: 0.05, output: 0.08 },
|
|
169
|
+
"mixtral-8x7b-32768": { input: 0.24, output: 0.24 },
|
|
170
|
+
// DeepSeek
|
|
171
|
+
"deepseek-chat": { input: 0.14, output: 0.28 },
|
|
172
|
+
"deepseek-reasoner": { input: 0.55, output: 2.19 },
|
|
173
|
+
// Together.ai (open-source models)
|
|
174
|
+
"meta-llama/llama-3.3-70b-instruct-turbo": { input: 0.88, output: 0.88 },
|
|
175
|
+
"meta-llama/meta-llama-3.1-8b-instruct-turbo": { input: 0.18, output: 0.18 },
|
|
176
|
+
"meta-llama/llama-3.2-3b-instruct-turbo": { input: 0.06, output: 0.06 },
|
|
177
|
+
"qwen/qwen2.5-7b-instruct-turbo": { input: 0.2, output: 0.2 },
|
|
178
|
+
"mistralai/mistral-small-24b-instruct-2501": { input: 0.2, output: 0.2 },
|
|
179
|
+
"mistralai/mixtral-8x7b-instruct-v0.1": { input: 0.6, output: 0.6 },
|
|
180
|
+
"deepseek-ai/deepseek-v3": { input: 0.27, output: 1.1 }
|
|
181
|
+
};
|
|
182
|
+
function calculateCost(model, promptTokens, completionTokens) {
|
|
183
|
+
const pricing = PRICING[model] ?? PRICING[model.toLowerCase()];
|
|
184
|
+
if (!pricing) return void 0;
|
|
185
|
+
const inputCost = promptTokens / 1e6 * pricing.input;
|
|
186
|
+
const outputCost = completionTokens / 1e6 * pricing.output;
|
|
187
|
+
return inputCost + outputCost;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/context/dedup.ts
|
|
191
|
+
function isProviderInstrumentationSuppressed() {
|
|
192
|
+
return getContext()._suppressProviderInstrumentation === true;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// src/providers/huggingface/patch.ts
|
|
196
|
+
function enrichFromResponse(span, response) {
|
|
197
|
+
try {
|
|
198
|
+
const model = response.model;
|
|
199
|
+
if (model) span.setLlmFields({ model });
|
|
200
|
+
const usage = response.usage;
|
|
201
|
+
if (usage) {
|
|
202
|
+
const promptTokens = usage.prompt_tokens ?? 0;
|
|
203
|
+
const completionTokens = usage.completion_tokens ?? 0;
|
|
204
|
+
const totalTokens = usage.total_tokens ?? promptTokens + completionTokens;
|
|
205
|
+
const cost = model ?? span.llmModel ? calculateCost(model ?? span.llmModel ?? "", promptTokens, completionTokens) : void 0;
|
|
206
|
+
span.setLlmFields({ promptTokens, completionTokens, totalTokens, costUsd: cost });
|
|
207
|
+
}
|
|
208
|
+
} catch {
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function createChatCompletionProxy(original) {
|
|
212
|
+
return function patchedChatCompletion(...args) {
|
|
213
|
+
if (isProviderInstrumentationSuppressed()) {
|
|
214
|
+
return original.apply(this, args);
|
|
215
|
+
}
|
|
216
|
+
let tracer;
|
|
217
|
+
try {
|
|
218
|
+
tracer = requireTracer();
|
|
219
|
+
} catch {
|
|
220
|
+
debug("Tracer not initialized \u2014 call init() before using patchHuggingFace()");
|
|
221
|
+
return original.apply(this, args);
|
|
222
|
+
}
|
|
223
|
+
const params = args[0] ?? {};
|
|
224
|
+
const model = params.model ?? "unknown";
|
|
225
|
+
return tracer.startSpan(
|
|
226
|
+
{ name: "huggingface.chatCompletion", kind: "llm_call" /* LLM_CALL */, attributes: { "llm.request.model": model } },
|
|
227
|
+
(span) => {
|
|
228
|
+
span.setLlmFields({ provider: "huggingface", model });
|
|
229
|
+
const result = original.apply(this, args);
|
|
230
|
+
if (result && typeof result.then === "function") {
|
|
231
|
+
return result.then((response) => {
|
|
232
|
+
if (response) enrichFromResponse(span, response);
|
|
233
|
+
return response;
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
);
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function createTextGenerationProxy(original) {
|
|
242
|
+
return function patchedTextGeneration(...args) {
|
|
243
|
+
if (isProviderInstrumentationSuppressed()) {
|
|
244
|
+
return original.apply(this, args);
|
|
245
|
+
}
|
|
246
|
+
let tracer;
|
|
247
|
+
try {
|
|
248
|
+
tracer = requireTracer();
|
|
249
|
+
} catch {
|
|
250
|
+
debug("Tracer not initialized \u2014 call init() before using patchHuggingFace()");
|
|
251
|
+
return original.apply(this, args);
|
|
252
|
+
}
|
|
253
|
+
const params = args[0] ?? {};
|
|
254
|
+
const model = params.model ?? "unknown";
|
|
255
|
+
return tracer.startSpan(
|
|
256
|
+
{ name: "huggingface.textGeneration", kind: "llm_call" /* LLM_CALL */, attributes: { "llm.request.model": model } },
|
|
257
|
+
(span) => {
|
|
258
|
+
span.setLlmFields({ provider: "huggingface", model });
|
|
259
|
+
const result = original.apply(this, args);
|
|
260
|
+
if (result && typeof result.then === "function") {
|
|
261
|
+
return result.then((response) => {
|
|
262
|
+
if (response) enrichFromResponse(span, response);
|
|
263
|
+
return response;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
268
|
+
);
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
function patchHuggingFace(client) {
|
|
272
|
+
return new Proxy(client, {
|
|
273
|
+
get(target, prop, receiver) {
|
|
274
|
+
const value = Reflect.get(target, prop, receiver);
|
|
275
|
+
if (prop === "chatCompletion" && typeof value === "function") {
|
|
276
|
+
return createChatCompletionProxy(value.bind(target));
|
|
277
|
+
}
|
|
278
|
+
if (prop === "textGeneration" && typeof value === "function") {
|
|
279
|
+
return createTextGenerationProxy(value.bind(target));
|
|
280
|
+
}
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
286
|
+
0 && (module.exports = {
|
|
287
|
+
patchHuggingFace
|
|
288
|
+
});
|
|
289
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/providers/huggingface/index.ts","../../../src/ids.ts","../../../src/noop.ts","../../../src/globals.ts","../../../src/context/storage.ts","../../../src/utils/log.ts","../../../src/client.ts","../../../src/utils/pricing.ts","../../../src/context/dedup.ts","../../../src/providers/huggingface/patch.ts"],"sourcesContent":["export { patchHuggingFace } from './patch.js';\n","/**\n * ID generation for traces and spans.\n *\n * Trace IDs: 32 lowercase hex characters (16 random bytes)\n * Span IDs: 16 lowercase hex characters (8 random bytes)\n *\n * Uses crypto.randomBytes for cryptographically secure randomness.\n */\n\nimport { randomBytes } from 'node:crypto';\n\nconst HEX_REGEX_32 = /^[0-9a-f]{32}$/;\nconst HEX_REGEX_16 = /^[0-9a-f]{16}$/;\n\nexport function generateTraceId(): string {\n return randomBytes(16).toString('hex');\n}\n\nexport function generateSpanId(): string {\n return randomBytes(8).toString('hex');\n}\n\nexport function generateAgentId(prefix?: string): string {\n const suffix = randomBytes(8).toString('hex');\n return prefix ? `${prefix}-${suffix}` : suffix;\n}\n\nexport function validateTraceId(id: string): boolean {\n return HEX_REGEX_32.test(id);\n}\n\nexport function validateSpanId(id: string): boolean {\n return HEX_REGEX_16.test(id);\n}\n","/**\n * No-op implementations for the disabled path.\n *\n * When tracing is disabled, all operations return these no-op objects\n * to maintain zero overhead. No allocations, no side effects.\n */\n\nimport { SpanKind, SpanStatus, type SpanPayload } from './types.js';\n\n/**\n * A frozen no-op span that silently ignores all operations.\n * Used when SDK is disabled to avoid overhead.\n */\nexport const NOOP_SPAN = Object.freeze({\n traceId: '00000000000000000000000000000000',\n spanId: '0000000000000000',\n parentSpanId: undefined,\n name: 'noop',\n kind: SpanKind.INTERNAL,\n startTime: '',\n startHrtime: 0,\n endTime: undefined,\n status: SpanStatus.UNSET,\n statusMessage: undefined,\n attributes: Object.freeze({}) as Record<string, unknown>,\n events: Object.freeze([]) as readonly [],\n links: Object.freeze([]) as readonly [],\n sessionId: undefined,\n agentId: undefined,\n agentName: undefined,\n semanticPhase: undefined,\n llmProvider: undefined,\n llmModel: undefined,\n llmPromptTokens: undefined,\n llmCompletionTokens: undefined,\n llmTotalTokens: undefined,\n llmCostUsd: undefined,\n toolName: undefined,\n toolSuccess: undefined,\n isEnded: true,\n durationMs: 0,\n\n setAttribute() { return this; },\n setAttributes() { return this; },\n setStatus() { return this; },\n addEvent() { return this; },\n addLink() { return this; },\n recordException() { return this; },\n setLlmFields() { return this; },\n setToolFields() { return this; },\n end() {},\n toPayload(): SpanPayload {\n return {\n traceId: this.traceId,\n spanId: this.spanId,\n name: this.name,\n kind: this.kind,\n startTime: this.startTime,\n status: this.status,\n attributes: {},\n events: [],\n links: [],\n };\n },\n});\n\nexport type NoopSpan = typeof NOOP_SPAN;\n","/**\n * Shared state via globalThis — ensures all entry point bundles share\n * the same singleton instances.\n *\n * Problem: tsup with `splitting: false` gives each entry point (index,\n * openai, anthropic, vercel-ai) its own copy of module-level variables.\n * This means `init()` from 'risicare' sets a tracer that 'risicare/openai'\n * can't see — breaking all provider instrumentation silently.\n *\n * Solution: Store all mutable singletons on globalThis with a namespaced\n * prefix. Every bundle reads/writes the same global slots.\n *\n * This pattern is used by React, OpenTelemetry, and other SDKs that must\n * share state across independently bundled entry points.\n *\n * @internal\n */\n\nimport { AsyncLocalStorage } from 'node:async_hooks';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst G = globalThis as any;\nconst PREFIX = '__risicare_';\n\n// ─── Client & Tracer ────────────────────────────────────────────────────────\n\nexport function getClient(): unknown {\n return G[PREFIX + 'client'];\n}\n\nexport function setClient(client: unknown): void {\n G[PREFIX + 'client'] = client;\n}\n\nexport function getTracer(): unknown {\n return G[PREFIX + 'tracer'];\n}\n\nexport function setTracer(tracer: unknown): void {\n G[PREFIX + 'tracer'] = tracer;\n}\n\n// ─── Context Storage ────────────────────────────────────────────────────────\n\nexport function getContextStorage(): AsyncLocalStorage<unknown> {\n if (!G[PREFIX + 'ctx']) {\n G[PREFIX + 'ctx'] = new AsyncLocalStorage();\n }\n return G[PREFIX + 'ctx'];\n}\n\n// ─── Span Registry ──────────────────────────────────────────────────────────\n\nexport function getRegistry(): Map<string, unknown> {\n if (!G[PREFIX + 'registry']) {\n G[PREFIX + 'registry'] = new Map();\n }\n return G[PREFIX + 'registry'];\n}\n\nexport function getOpCount(): number {\n return G[PREFIX + 'opcount'] ?? 0;\n}\n\nexport function setOpCount(n: number): void {\n G[PREFIX + 'opcount'] = n;\n}\n\n// ─── Debug Flag ─────────────────────────────────────────────────────────────\n\nexport function getDebug(): boolean {\n return G[PREFIX + 'debug'] ?? false;\n}\n\nexport function setDebugFlag(enabled: boolean): void {\n G[PREFIX + 'debug'] = enabled;\n}\n","/**\n * AsyncLocalStorage-based context propagation.\n *\n * Uses a single AsyncLocalStorage instance with a composite state object.\n * This is simpler and more performant than multiple separate stores.\n *\n * Node.js AsyncLocalStorage automatically propagates through:\n * - Promise / async-await\n * - setTimeout / setImmediate\n * - EventEmitter callbacks\n * - process.nextTick\n * - async generators (unlike Python's contextvars!)\n */\n\nimport { AsyncLocalStorage } from 'node:async_hooks';\nimport type { Span } from '../span.js';\nimport type { SemanticPhase } from '../types.js';\nimport { getContextStorage } from '../globals.js';\n\n// ─── Context Types ───────────────────────────────────────────────────────────\n\nexport interface SessionContext {\n sessionId: string;\n userId?: string;\n metadata?: Record<string, unknown>;\n parentSessionId?: string;\n turnNumber?: number;\n}\n\nexport interface AgentContext {\n agentId: string;\n agentName?: string;\n agentRole?: string;\n agentType?: string;\n parentAgentId?: string;\n version?: number;\n metadata?: Record<string, unknown>;\n}\n\nexport interface ContextState {\n session?: SessionContext;\n agent?: AgentContext;\n span?: Span;\n phase?: SemanticPhase;\n /** When true, provider instrumentors skip span creation (framework is handling it). */\n _suppressProviderInstrumentation?: boolean;\n}\n\n// ─── Storage Accessor ────────────────────────────────────────────────────────\n\nfunction storage(): AsyncLocalStorage<ContextState> {\n return getContextStorage() as AsyncLocalStorage<ContextState>;\n}\n\n// ─── Core Operations ─────────────────────────────────────────────────────────\n\n/**\n * Get the current context state, or empty object if outside any context.\n */\nexport function getContext(): ContextState {\n return storage().getStore() ?? {};\n}\n\n/**\n * Run a callback within a new context scope.\n * The new scope inherits from the parent, with overrides applied.\n */\nexport function runWithContext<T>(overrides: Partial<ContextState>, fn: () => T): T {\n const parent = getContext();\n const merged: ContextState = { ...parent, ...overrides };\n return storage().run(merged, fn);\n}\n\n/**\n * Run an async callback within a new context scope.\n */\nexport function runWithContextAsync<T>(overrides: Partial<ContextState>, fn: () => Promise<T>): Promise<T> {\n const parent = getContext();\n const merged: ContextState = { ...parent, ...overrides };\n return storage().run(merged, fn);\n}\n\n// ─── Context Accessors ───────────────────────────────────────────────────────\n\nexport function getCurrentSession(): SessionContext | undefined {\n return getContext().session;\n}\n\nexport function getCurrentAgent(): AgentContext | undefined {\n return getContext().agent;\n}\n\nexport function getCurrentSpan(): Span | undefined {\n return getContext().span;\n}\n\nexport function getCurrentPhase(): SemanticPhase | undefined {\n return getContext().phase;\n}\n\nexport function getCurrentSessionId(): string | undefined {\n return getContext().session?.sessionId;\n}\n\nexport function getCurrentAgentId(): string | undefined {\n return getContext().agent?.agentId;\n}\n\nexport function getCurrentTraceId(): string | undefined {\n return getContext().span?.traceId;\n}\n\nexport function getCurrentSpanId(): string | undefined {\n return getContext().span?.spanId;\n}\n\nexport function getCurrentParentSpanId(): string | undefined {\n return getContext().span?.parentSpanId;\n}\n\n/**\n * Get all current context as a plain object (for debugging/serialization).\n */\nexport function getCurrentContext(): Record<string, unknown> {\n const ctx = getContext();\n return {\n session: ctx.session ? {\n sessionId: ctx.session.sessionId,\n userId: ctx.session.userId,\n ...(ctx.session.parentSessionId !== undefined ? { parentSessionId: ctx.session.parentSessionId } : {}),\n ...(ctx.session.turnNumber !== undefined ? { turnNumber: ctx.session.turnNumber } : {}),\n ...(ctx.session.metadata !== undefined ? { metadata: ctx.session.metadata } : {}),\n } : null,\n agent: ctx.agent ? {\n agentId: ctx.agent.agentId,\n agentName: ctx.agent.agentName,\n agentRole: ctx.agent.agentRole,\n agentType: ctx.agent.agentType,\n ...(ctx.agent.parentAgentId !== undefined ? { parentAgentId: ctx.agent.parentAgentId } : {}),\n ...(ctx.agent.version !== undefined ? { version: ctx.agent.version } : {}),\n ...(ctx.agent.metadata !== undefined ? { metadata: ctx.agent.metadata } : {}),\n } : null,\n span: ctx.span ? { spanId: ctx.span.spanId, traceId: ctx.span.traceId } : null,\n phase: ctx.phase ?? null,\n };\n}\n","/**\n * Internal logger for the Risicare SDK.\n *\n * Centralizes all diagnostic output so that:\n * - Debug messages are gated by a single flag (zero-cost when disabled)\n * - Warnings always fire (operational alerts like queue full)\n * - All output goes to stderr with a consistent [risicare] prefix\n * - A future custom logger callback can be added in one place\n */\n\nimport { getDebug, setDebugFlag } from '../globals.js';\n\n/**\n * Enable or disable debug logging. Called once during init().\n * @internal\n */\nexport function setDebug(enabled: boolean): void {\n setDebugFlag(enabled);\n}\n\n/**\n * Log a debug message. Only outputs when debug mode is enabled.\n * @internal\n */\nexport function debug(msg: string): void {\n if (getDebug()) {\n process.stderr.write(`[risicare] ${msg}\\n`);\n }\n}\n\n/**\n * Log a warning. Always outputs regardless of debug mode.\n * Use sparingly — only for operational issues the user should see.\n * @internal\n */\nexport function warn(msg: string): void {\n process.stderr.write(`[risicare] WARNING: ${msg}\\n`);\n}\n","/**\n * RisicareClient — singleton client managing SDK lifecycle.\n *\n * Handles initialization, shutdown, and the connection between\n * the Tracer and the export pipeline (batch processor + HTTP exporter).\n *\n * Usage:\n * import { init, shutdown } from 'risicare';\n * init({ apiKey: 'rsk-...' }); // API key determines project\n * // ... instrument code ...\n * await shutdown(); // flush remaining spans\n */\n\nimport { type RisicareConfig, resolveConfig } from './config.js';\nimport { Tracer } from './tracer.js';\nimport { BatchSpanProcessor } from './exporters/batch.js';\nimport { HttpExporter } from './exporters/http.js';\nimport { ConsoleExporter } from './exporters/console.js';\nimport { SpanKind, SpanStatus } from './types.js';\nimport type { SpanExporter } from './exporters/base.js';\nimport { setDebug, debug } from './utils/log.js';\nimport {\n getClient as getGlobalClient,\n setClient as setGlobalClient,\n getTracer as getGlobalTracer,\n setTracer as setGlobalTracer,\n} from './globals.js';\n\n// ─── Client Class ───────────────────────────────────────────────────────────\n\nclass RisicareClient {\n readonly config: ReturnType<typeof resolveConfig>;\n readonly processor: BatchSpanProcessor;\n readonly tracer: Tracer;\n private _shutdownPromise: Promise<void> | undefined;\n private _shutdownHandlers: { signal: string; handler: () => void }[] = [];\n\n constructor(config?: Partial<RisicareConfig>) {\n this.config = resolveConfig(config);\n\n // API key format validation\n if (this.config.apiKey && !this.config.apiKey.startsWith('rsk-')) {\n debug('Warning: API key should start with \"rsk-\". Got: ' + this.config.apiKey.slice(0, 4) + '...');\n }\n\n // Build exporter chain\n let exporter: SpanExporter;\n if (this.config.debug && !this.config.apiKey) {\n exporter = new ConsoleExporter();\n } else if (this.config.apiKey) {\n exporter = new HttpExporter({\n endpoint: this.config.endpoint,\n apiKey: this.config.apiKey,\n projectId: this.config.projectId || undefined,\n environment: this.config.environment || undefined,\n compress: this.config.compress,\n });\n } else {\n // No API key and not debug — use console as fallback\n exporter = new ConsoleExporter();\n }\n\n this.processor = new BatchSpanProcessor({\n exporters: [exporter],\n batchSize: this.config.batchSize,\n batchTimeoutMs: this.config.batchTimeoutMs,\n maxQueueSize: this.config.maxQueueSize,\n debug: this.config.debug,\n });\n\n this.tracer = new Tracer({\n onSpanEnd: (span) => this.processor.onSpanEnd(span),\n sampleRate: this.config.sampleRate,\n enabled: this.config.enabled,\n traceContent: this.config.traceContent,\n });\n\n // Start the batch processor (enables span queuing and periodic flushing)\n this.processor.start();\n\n // Register shutdown hooks\n this._registerShutdownHooks();\n\n // Enable internal debug logging if configured\n setDebug(this.config.debug);\n debug(`Initialized: enabled=${this.config.enabled}, endpoint=${this.config.endpoint}`);\n }\n\n get enabled(): boolean {\n return this.tracer.enabled;\n }\n\n set enabled(value: boolean) {\n this.tracer.enabled = value;\n }\n\n // Audit #6: Promise-based shutdown dedup (fixes TOCTOU race condition)\n async shutdown(): Promise<void> {\n if (this._shutdownPromise) return this._shutdownPromise;\n this._shutdownPromise = this._doShutdown();\n return this._shutdownPromise;\n }\n\n private async _doShutdown(): Promise<void> {\n debug('Shutting down...');\n\n // Audit #3: Remove process listeners to prevent leak\n for (const { signal, handler } of this._shutdownHandlers) {\n process.removeListener(signal, handler);\n }\n this._shutdownHandlers = [];\n\n await this.processor.shutdown();\n }\n\n async flush(): Promise<void> {\n await this.processor.flush();\n }\n\n private _registerShutdownHooks(): void {\n const onShutdown = () => {\n // Audit #3: Add 5s timeout to prevent hanging on signal\n const timeout = setTimeout(() => process.exit(1), 5000);\n timeout.unref();\n this.shutdown().catch(() => {}).finally(() => clearTimeout(timeout));\n };\n\n const signals = ['beforeExit', 'SIGTERM', 'SIGINT'];\n for (const signal of signals) {\n process.once(signal, onShutdown);\n this._shutdownHandlers.push({ signal, handler: onShutdown });\n }\n }\n}\n\n// ─── Public API ─────────────────────────────────────────────────────────────\n\n/**\n * Initialize the Risicare SDK. Call once at application startup.\n *\n * @example\n * import { init } from 'risicare';\n * init({ apiKey: 'rsk-...', serviceName: 'my-agent', environment: 'production' });\n */\nexport function init(config?: Partial<RisicareConfig>): void {\n if (getGlobalClient()) {\n debug('Already initialized. Call shutdown() first to re-initialize.');\n return;\n }\n\n const client = new RisicareClient(config);\n setGlobalClient(client);\n setGlobalTracer(client.tracer);\n}\n\n/**\n * Gracefully shut down the SDK. Flushes pending spans before resolving.\n */\nexport async function shutdown(): Promise<void> {\n const client = getGlobalClient() as RisicareClient | undefined;\n if (!client) return;\n await client.shutdown();\n setGlobalClient(undefined);\n setGlobalTracer(undefined);\n}\n\n/**\n * Flush all pending spans without shutting down.\n */\nexport async function flush(): Promise<void> {\n const client = getGlobalClient() as RisicareClient | undefined;\n if (!client) return;\n await client.flush();\n}\n\n/**\n * Enable tracing at runtime.\n */\nexport function enable(): void {\n const client = getGlobalClient() as RisicareClient | undefined;\n if (client) client.enabled = true;\n}\n\n/**\n * Disable tracing at runtime. Spans will not be created or exported.\n */\nexport function disable(): void {\n const client = getGlobalClient() as RisicareClient | undefined;\n if (client) client.enabled = false;\n}\n\n/**\n * Check whether tracing is currently enabled.\n */\nexport function isEnabled(): boolean {\n const client = getGlobalClient() as RisicareClient | undefined;\n return client?.enabled ?? false;\n}\n\n/**\n * Get the global tracer instance. Returns undefined if not initialized.\n */\nexport function getTracer(): Tracer | undefined {\n return getGlobalTracer() as Tracer | undefined;\n}\n\n/**\n * Get the global tracer, or throw if not initialized.\n * @internal Used by decorators and providers that require an active tracer.\n */\nexport function requireTracer(): Tracer {\n const tracer = getGlobalTracer() as Tracer | undefined;\n if (!tracer) {\n throw new Error(\n 'Risicare SDK not initialized. Call init() before using tracing features.',\n );\n }\n return tracer;\n}\n\n/**\n * Check whether content tracing (prompt/completion capture) is enabled.\n */\nexport function getTraceContent(): boolean {\n const tracer = getGlobalTracer() as Tracer | undefined;\n return tracer?.traceContent ?? true;\n}\n\n/**\n * Get SDK metrics: exported spans, dropped spans, failed exports, queue stats.\n * Returns zero-valued metrics if SDK is not initialized.\n */\nexport function getMetrics() {\n const client = getGlobalClient() as RisicareClient | undefined;\n return client?.processor.getMetrics() ?? {\n exportedSpans: 0,\n droppedSpans: 0,\n failedExports: 0,\n queueSize: 0,\n queueCapacity: 0,\n queueUtilization: 0,\n };\n}\n\n// ─── reportError ──────────────────────────────────────────────────────────\n\n/**\n * Report a caught exception to the self-healing pipeline.\n *\n * Creates an error span that triggers diagnosis and fix generation.\n * This function never throws and is non-blocking.\n *\n * @param error - The caught exception (Error object or string)\n * @param options - Optional attributes and context overrides\n */\nexport function reportError(\n error: unknown,\n options?: { name?: string; attributes?: Record<string, unknown> },\n): void {\n try {\n const tracer = getTracer();\n if (!tracer) return;\n\n const err = error instanceof Error ? error : new Error(String(error));\n const spanName = options?.name ?? `error:${err.constructor.name}`;\n\n tracer.startSpan({ name: spanName, kind: SpanKind.INTERNAL }, (span) => {\n span.setStatus(SpanStatus.ERROR, err.message);\n span.setAttribute('error', true);\n span.setAttribute('error.type', err.constructor.name);\n span.setAttribute('error.message', err.message.slice(0, 2000));\n if (err.stack) span.setAttribute('error.stack', err.stack.slice(0, 4000));\n span.setAttribute('risicare.reported_error', true);\n if (options?.attributes) {\n for (const [k, v] of Object.entries(options.attributes)) {\n span.setAttribute(k, v);\n }\n }\n });\n } catch {\n // Never crash the host application\n debug('reportError failed');\n }\n}\n\n// ─── score ─────────────────────────────────────────────────────────────────\n\n/**\n * Record a custom evaluation score on a trace.\n *\n * Sends the score to the server in a fire-and-forget fashion.\n * This function never throws and is non-blocking.\n *\n * @param traceId - The trace to score\n * @param name - Score name (e.g., \"accuracy\", \"user_satisfaction\")\n * @param value - Score value between 0.0 and 1.0 inclusive\n * @param options - Optional span_id and comment\n */\nexport function score(\n traceId: string,\n name: string,\n value: number,\n options?: { spanId?: string; comment?: string },\n): void {\n try {\n if (typeof value !== 'number' || value < 0.0 || value > 1.0) {\n debug(`score: value must be in [0.0, 1.0], got ${value}. Score not sent.`);\n return;\n }\n if (!traceId || !name) {\n debug('score: traceId and name are required');\n return;\n }\n\n const client = getGlobalClient() as RisicareClient | undefined;\n if (!client?.enabled || !client.config.apiKey) return;\n\n const endpoint = client.config.endpoint.replace(/\\/$/, '');\n const url = `${endpoint}/api/v1/scores`;\n const body = JSON.stringify({\n trace_id: traceId,\n name,\n score: value,\n source: 'sdk',\n ...(options?.spanId && { span_id: options.spanId }),\n ...(options?.comment && { comment: options.comment }),\n });\n\n // Fire-and-forget — never blocks caller\n fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${client.config.apiKey}`,\n },\n body,\n }).catch((err) => debug(`score: send failed: ${err}`));\n } catch {\n // Never crash the host application\n debug('score failed');\n }\n}\n","/**\n * Token cost calculation table.\n *\n * Prices are per 1M tokens. Update monthly.\n * Source: provider pricing pages.\n */\n\ninterface ModelPricing {\n input: number; // USD per 1M input tokens\n output: number; // USD per 1M output tokens\n}\n\nconst PRICING: Record<string, ModelPricing> = {\n // OpenAI\n 'gpt-4o': { input: 2.50, output: 10.00 },\n 'gpt-4o-mini': { input: 0.15, output: 0.60 },\n 'gpt-4-turbo': { input: 10.00, output: 30.00 },\n 'gpt-4': { input: 30.00, output: 60.00 },\n 'gpt-3.5-turbo': { input: 0.50, output: 1.50 },\n 'o1': { input: 15.00, output: 60.00 },\n 'o1-mini': { input: 3.00, output: 12.00 },\n 'o3-mini': { input: 1.10, output: 4.40 },\n\n // Anthropic\n 'claude-opus-4-5-20251101': { input: 15.00, output: 75.00 },\n 'claude-sonnet-4-5-20250929': { input: 3.00, output: 15.00 },\n 'claude-haiku-4-5-20251001': { input: 0.80, output: 4.00 },\n 'claude-3-5-sonnet-20241022': { input: 3.00, output: 15.00 },\n 'claude-3-haiku-20240307': { input: 0.25, output: 1.25 },\n 'claude-3-opus-20240229': { input: 15.00, output: 75.00 },\n\n // Google\n 'gemini-2.0-flash': { input: 0.10, output: 0.40 },\n 'gemini-1.5-pro': { input: 1.25, output: 5.00 },\n 'gemini-1.5-flash': { input: 0.075, output: 0.30 },\n\n // Groq\n 'llama-3.3-70b-versatile': { input: 0.59, output: 0.79 },\n 'llama-3.1-8b-instant': { input: 0.05, output: 0.08 },\n 'mixtral-8x7b-32768': { input: 0.24, output: 0.24 },\n\n // DeepSeek\n 'deepseek-chat': { input: 0.14, output: 0.28 },\n 'deepseek-reasoner': { input: 0.55, output: 2.19 },\n\n // Together.ai (open-source models)\n 'meta-llama/llama-3.3-70b-instruct-turbo': { input: 0.88, output: 0.88 },\n 'meta-llama/meta-llama-3.1-8b-instruct-turbo': { input: 0.18, output: 0.18 },\n 'meta-llama/llama-3.2-3b-instruct-turbo': { input: 0.06, output: 0.06 },\n 'qwen/qwen2.5-7b-instruct-turbo': { input: 0.20, output: 0.20 },\n 'mistralai/mistral-small-24b-instruct-2501': { input: 0.20, output: 0.20 },\n 'mistralai/mixtral-8x7b-instruct-v0.1': { input: 0.60, output: 0.60 },\n 'deepseek-ai/deepseek-v3': { input: 0.27, output: 1.10 },\n};\n\n/**\n * Calculate cost in USD for a model's token usage.\n * Returns undefined if model is not in pricing table.\n */\nexport function calculateCost(\n model: string,\n promptTokens: number,\n completionTokens: number,\n): number | undefined {\n const pricing = PRICING[model] ?? PRICING[model.toLowerCase()];\n if (!pricing) return undefined;\n\n const inputCost = (promptTokens / 1_000_000) * pricing.input;\n const outputCost = (completionTokens / 1_000_000) * pricing.output;\n return inputCost + outputCost;\n}\n\n/**\n * Check if a model has pricing data.\n */\nexport function hasPricing(model: string): boolean {\n return model in PRICING || model.toLowerCase() in PRICING;\n}\n","/**\n * Double-tracing prevention for framework integrations.\n *\n * When a framework integration (e.g., LlamaIndex handler) creates its own\n * LLM span, the underlying provider proxy (e.g., patchOpenAI) would also\n * create a duplicate span. This module provides suppression:\n *\n * - Framework integrations SET suppression via suppressProviderInstrumentation()\n * - Provider proxies CHECK via isProviderInstrumentationSuppressed() and skip\n *\n * Scoped to AsyncLocalStorage — concurrent calls are independent.\n */\n\nimport { getContext, runWithContext } from './storage.js';\n\n/**\n * Run a callback with provider instrumentation suppressed.\n *\n * During this callback, all provider instrumentors (patchOpenAI, etc.) will\n * skip span creation. The framework is responsible for creating the span.\n *\n * @param fn - The function to run with suppression active\n * @returns The function's return value\n */\nexport function suppressProviderInstrumentation<T>(fn: () => T): T {\n return runWithContext({ _suppressProviderInstrumentation: true }, fn);\n}\n\n/**\n * Check if provider instrumentation should be suppressed.\n *\n * Called by provider instrumentors as an early-exit guard. When true,\n * the provider calls the original method directly without creating a span.\n */\nexport function isProviderInstrumentationSuppressed(): boolean {\n return getContext()._suppressProviderInstrumentation === true;\n}\n","/**\n * HuggingFace Inference (@huggingface/inference) Proxy-based instrumentation.\n *\n * Wraps a HuggingFace InferenceClient instance using ES Proxy to intercept:\n * - chatCompletion({ model, messages }) — chat-style API\n * - textGeneration({ model, inputs }) — raw text generation API\n *\n * Usage:\n * import { InferenceClient } from '@huggingface/inference';\n * import { patchHuggingFace } from 'risicare/huggingface';\n * const hf = patchHuggingFace(new InferenceClient('hf_...'));\n */\n\nimport { requireTracer } from '../../client.js';\nimport { SpanKind } from '../../types.js';\nimport { calculateCost } from '../../utils/pricing.js';\nimport { debug } from '../../utils/log.js';\nimport { isProviderInstrumentationSuppressed } from '../../context/dedup.js';\nimport type { Span } from '../../span.js';\n\nfunction enrichFromResponse(span: Span, response: Record<string, unknown>): void {\n try {\n const model = response.model as string | undefined;\n if (model) span.setLlmFields({ model });\n\n // HuggingFace chat completion may include usage (OpenAI-compatible shape)\n const usage = response.usage as Record<string, number> | undefined;\n if (usage) {\n const promptTokens = usage.prompt_tokens ?? 0;\n const completionTokens = usage.completion_tokens ?? 0;\n const totalTokens = usage.total_tokens ?? (promptTokens + completionTokens);\n const cost = (model ?? span.llmModel)\n ? calculateCost(model ?? span.llmModel ?? '', promptTokens, completionTokens)\n : undefined;\n\n span.setLlmFields({ promptTokens, completionTokens, totalTokens, costUsd: cost });\n }\n } catch {\n // Never fail enrichment\n }\n}\n\nfunction createChatCompletionProxy(original: Function): Function {\n return function patchedChatCompletion(this: unknown, ...args: unknown[]) {\n if (isProviderInstrumentationSuppressed()) {\n return original.apply(this, args);\n }\n\n let tracer;\n try {\n tracer = requireTracer();\n } catch {\n debug('Tracer not initialized — call init() before using patchHuggingFace()');\n return original.apply(this, args);\n }\n\n const params = (args[0] ?? {}) as Record<string, unknown>;\n const model = (params.model as string) ?? 'unknown';\n\n return tracer.startSpan(\n { name: 'huggingface.chatCompletion', kind: SpanKind.LLM_CALL, attributes: { 'llm.request.model': model } },\n (span) => {\n span.setLlmFields({ provider: 'huggingface', model });\n\n const result = original.apply(this, args);\n\n if (result && typeof (result as Promise<unknown>).then === 'function') {\n return (result as Promise<Record<string, unknown>>).then((response) => {\n if (response) enrichFromResponse(span, response);\n return response;\n });\n }\n\n return result;\n },\n );\n };\n}\n\nfunction createTextGenerationProxy(original: Function): Function {\n return function patchedTextGeneration(this: unknown, ...args: unknown[]) {\n if (isProviderInstrumentationSuppressed()) {\n return original.apply(this, args);\n }\n\n let tracer;\n try {\n tracer = requireTracer();\n } catch {\n debug('Tracer not initialized — call init() before using patchHuggingFace()');\n return original.apply(this, args);\n }\n\n const params = (args[0] ?? {}) as Record<string, unknown>;\n // textGeneration uses model param or falls back to client's default model\n const model = (params.model as string) ?? 'unknown';\n\n return tracer.startSpan(\n { name: 'huggingface.textGeneration', kind: SpanKind.LLM_CALL, attributes: { 'llm.request.model': model } },\n (span) => {\n span.setLlmFields({ provider: 'huggingface', model });\n\n const result = original.apply(this, args);\n\n if (result && typeof (result as Promise<unknown>).then === 'function') {\n return (result as Promise<Record<string, unknown>>).then((response) => {\n if (response) enrichFromResponse(span, response);\n return response;\n });\n }\n\n return result;\n },\n );\n };\n}\n\n/**\n * Wrap a HuggingFace InferenceClient instance with tracing instrumentation.\n *\n * Returns a Proxy that intercepts chatCompletion and textGeneration.\n * The original client is NOT modified.\n *\n * @param client - A HuggingFace InferenceClient instance\n * @returns A proxied client with automatic tracing\n */\nexport function patchHuggingFace<T extends object>(client: T): T {\n return new Proxy(client, {\n get(target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver);\n\n if (prop === 'chatCompletion' && typeof value === 'function') {\n return createChatCompletionProxy(value.bind(target));\n }\n\n if (prop === 'textGeneration' && typeof value === 'function') {\n return createTextGenerationProxy(value.bind(target));\n }\n\n return value;\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACSA,yBAA4B;;;ACIrB,IAAM,YAAY,OAAO,OAAO;AAAA,EACrC,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,cAAc;AAAA,EACd,MAAM;AAAA,EACN;AAAA,EACA,WAAW;AAAA,EACX,aAAa;AAAA,EACb,SAAS;AAAA,EACT;AAAA,EACA,eAAe;AAAA,EACf,YAAY,OAAO,OAAO,CAAC,CAAC;AAAA,EAC5B,QAAQ,OAAO,OAAO,CAAC,CAAC;AAAA,EACxB,OAAO,OAAO,OAAO,CAAC,CAAC;AAAA,EACvB,WAAW;AAAA,EACX,SAAS;AAAA,EACT,WAAW;AAAA,EACX,eAAe;AAAA,EACf,aAAa;AAAA,EACb,UAAU;AAAA,EACV,iBAAiB;AAAA,EACjB,qBAAqB;AAAA,EACrB,gBAAgB;AAAA,EAChB,YAAY;AAAA,EACZ,UAAU;AAAA,EACV,aAAa;AAAA,EACb,SAAS;AAAA,E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AAS;AACR,aAAK,aAAa,EAAE,UAAU,eAAe,MAAM,CAAC;AAEpD,cAAM,SAAS,SAAS,MAAM,MAAM,IAAI;AAExC,YAAI,UAAU,OAAQ,OAA4B,SAAS,YAAY;AACrE,iBAAQ,OAA4C,KAAK,CAAC,aAAa;AACrE,gBAAI,SAAU,oBAAmB,MAAM,QAAQ;AAC/C,mBAAO;AAAA,UACT,CAAC;AAAA,QACH;AAEA,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AACF;AAWO,SAAS,iBAAmC,QAAc;AAC/D,SAAO,IAAI,MAAM,QAAQ;AAAA,IACvB,IAAI,QAAQ,MAAM,UAAU;AAC1B,YAAM,QAAQ,QAAQ,IAAI,QAAQ,MAAM,QAAQ;AAEhD,UAAI,SAAS,oBAAoB,OAAO,UAAU,YAAY;AAC5D,eAAO,0BAA0B,MAAM,KAAK,MAAM,CAAC;AAAA,MACrD;AAEA,UAAI,SAAS,oBAAoB,OAAO,UAAU,YAAY;AAC5D,eAAO,0BAA0B,MAAM,KAAK,MAAM,CAAC;AAAA,MACrD;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HuggingFace Inference (@huggingface/inference) Proxy-based instrumentation.
|
|
3
|
+
*
|
|
4
|
+
* Wraps a HuggingFace InferenceClient instance using ES Proxy to intercept:
|
|
5
|
+
* - chatCompletion({ model, messages }) — chat-style API
|
|
6
|
+
* - textGeneration({ model, inputs }) — raw text generation API
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* import { InferenceClient } from '@huggingface/inference';
|
|
10
|
+
* import { patchHuggingFace } from 'risicare/huggingface';
|
|
11
|
+
* const hf = patchHuggingFace(new InferenceClient('hf_...'));
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Wrap a HuggingFace InferenceClient instance with tracing instrumentation.
|
|
15
|
+
*
|
|
16
|
+
* Returns a Proxy that intercepts chatCompletion and textGeneration.
|
|
17
|
+
* The original client is NOT modified.
|
|
18
|
+
*
|
|
19
|
+
* @param client - A HuggingFace InferenceClient instance
|
|
20
|
+
* @returns A proxied client with automatic tracing
|
|
21
|
+
*/
|
|
22
|
+
declare function patchHuggingFace<T extends object>(client: T): T;
|
|
23
|
+
|
|
24
|
+
export { patchHuggingFace };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HuggingFace Inference (@huggingface/inference) Proxy-based instrumentation.
|
|
3
|
+
*
|
|
4
|
+
* Wraps a HuggingFace InferenceClient instance using ES Proxy to intercept:
|
|
5
|
+
* - chatCompletion({ model, messages }) — chat-style API
|
|
6
|
+
* - textGeneration({ model, inputs }) — raw text generation API
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* import { InferenceClient } from '@huggingface/inference';
|
|
10
|
+
* import { patchHuggingFace } from 'risicare/huggingface';
|
|
11
|
+
* const hf = patchHuggingFace(new InferenceClient('hf_...'));
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Wrap a HuggingFace InferenceClient instance with tracing instrumentation.
|
|
15
|
+
*
|
|
16
|
+
* Returns a Proxy that intercepts chatCompletion and textGeneration.
|
|
17
|
+
* The original client is NOT modified.
|
|
18
|
+
*
|
|
19
|
+
* @param client - A HuggingFace InferenceClient instance
|
|
20
|
+
* @returns A proxied client with automatic tracing
|
|
21
|
+
*/
|
|
22
|
+
declare function patchHuggingFace<T extends object>(client: T): T;
|
|
23
|
+
|
|
24
|
+
export { patchHuggingFace };
|