neatlogs 1.0.6 → 1.0.8
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/azure-openai.cjs +344 -0
- package/dist/azure-openai.cjs.map +1 -0
- package/dist/azure-openai.d.ts +30 -0
- package/dist/azure-openai.mjs +318 -0
- package/dist/azure-openai.mjs.map +1 -0
- package/dist/bedrock.cjs +540 -0
- package/dist/bedrock.cjs.map +1 -0
- package/dist/bedrock.d.ts +29 -0
- package/dist/bedrock.mjs +514 -0
- package/dist/bedrock.mjs.map +1 -0
- package/dist/browser.cjs +121 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.ts +143 -0
- package/dist/browser.mjs +96 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/claude-agent-sdk.cjs +406 -0
- package/dist/claude-agent-sdk.cjs.map +1 -0
- package/dist/claude-agent-sdk.d.ts +49 -0
- package/dist/claude-agent-sdk.mjs +381 -0
- package/dist/claude-agent-sdk.mjs.map +1 -0
- package/dist/index.cjs +3219 -1129
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.mjs +3210 -1131
- package/dist/index.mjs.map +1 -1
- package/dist/langchain.cjs +9 -9
- package/dist/langchain.cjs.map +1 -1
- package/dist/langchain.mjs +9 -9
- package/dist/langchain.mjs.map +1 -1
- package/dist/mastra-wrap.cjs +25 -25
- package/dist/mastra-wrap.cjs.map +1 -1
- package/dist/mastra-wrap.mjs +25 -25
- package/dist/mastra-wrap.mjs.map +1 -1
- package/dist/openai-agents.cjs +6 -6
- package/dist/openai-agents.cjs.map +1 -1
- package/dist/openai-agents.mjs +6 -6
- package/dist/openai-agents.mjs.map +1 -1
- package/dist/opencode-plugin.cjs +5879 -0
- package/dist/opencode-plugin.cjs.map +1 -0
- package/dist/opencode-plugin.d.ts +38 -0
- package/dist/opencode-plugin.mjs +5854 -0
- package/dist/opencode-plugin.mjs.map +1 -0
- package/dist/openrouter-agent.cjs +273 -0
- package/dist/openrouter-agent.cjs.map +1 -0
- package/dist/openrouter-agent.d.ts +34 -0
- package/dist/openrouter-agent.mjs +247 -0
- package/dist/openrouter-agent.mjs.map +1 -0
- package/dist/pi-agent.cjs +10 -10
- package/dist/pi-agent.cjs.map +1 -1
- package/dist/pi-agent.mjs +10 -10
- package/dist/pi-agent.mjs.map +1 -1
- package/dist/vertex-ai.cjs +424 -0
- package/dist/vertex-ai.cjs.map +1 -0
- package/dist/vertex-ai.d.ts +39 -0
- package/dist/vertex-ai.mjs +397 -0
- package/dist/vertex-ai.mjs.map +1 -0
- package/package.json +76 -1
|
@@ -0,0 +1,344 @@
|
|
|
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/azure-openai.ts
|
|
21
|
+
var azure_openai_exports = {};
|
|
22
|
+
__export(azure_openai_exports, {
|
|
23
|
+
traceTool: () => traceTool,
|
|
24
|
+
wrapAzureOpenAI: () => wrapAzureOpenAI
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(azure_openai_exports);
|
|
27
|
+
var import_api = require("@opentelemetry/api");
|
|
28
|
+
var TRACER_NAME = "neatlogs.azure_openai";
|
|
29
|
+
var PROVIDER = "azure";
|
|
30
|
+
var SYSTEM = "azure";
|
|
31
|
+
function wrapAzureOpenAI(client) {
|
|
32
|
+
return wrapNamespace(client, []);
|
|
33
|
+
}
|
|
34
|
+
function traceTool(name, fn) {
|
|
35
|
+
return async function tracedTool(args) {
|
|
36
|
+
const tracer = import_api.trace.getTracer(TRACER_NAME);
|
|
37
|
+
return tracer.startActiveSpan(
|
|
38
|
+
`tool.${name}`,
|
|
39
|
+
{
|
|
40
|
+
attributes: {
|
|
41
|
+
"neatlogs.span.kind": "TOOL",
|
|
42
|
+
"neatlogs.tool.name": name,
|
|
43
|
+
"input.value": safeStringify(args)
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
import_api.context.active(),
|
|
47
|
+
async (span) => {
|
|
48
|
+
try {
|
|
49
|
+
const result = await fn(args);
|
|
50
|
+
span.setAttribute("output.value", safeStringify(result));
|
|
51
|
+
span.setStatus({ code: import_api.SpanStatusCode.OK });
|
|
52
|
+
return result;
|
|
53
|
+
} catch (err) {
|
|
54
|
+
recordError(span, err);
|
|
55
|
+
throw err;
|
|
56
|
+
} finally {
|
|
57
|
+
span.end();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function wrapNamespace(target, path) {
|
|
64
|
+
return new Proxy(target, {
|
|
65
|
+
get(obj, prop, receiver) {
|
|
66
|
+
const value = Reflect.get(obj, prop, receiver);
|
|
67
|
+
if (typeof prop === "symbol" || String(prop).startsWith("_")) return value;
|
|
68
|
+
const currentPath = [...path, String(prop)];
|
|
69
|
+
const pathStr = currentPath.join(".");
|
|
70
|
+
if (pathStr === "chat.completions.create" && typeof value === "function") {
|
|
71
|
+
return tracedChatCompletionsCreate(value.bind(obj));
|
|
72
|
+
}
|
|
73
|
+
if (pathStr === "responses.create" && typeof value === "function") {
|
|
74
|
+
return tracedResponsesCreate(value.bind(obj));
|
|
75
|
+
}
|
|
76
|
+
if (value && typeof value === "object" && !Array.isArray(value) && isNamespace(currentPath)) {
|
|
77
|
+
return wrapNamespace(value, currentPath);
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
function isNamespace(path) {
|
|
84
|
+
if (path.length > 3) return false;
|
|
85
|
+
const key = path[path.length - 1];
|
|
86
|
+
return ["chat", "completions", "responses", "beta"].includes(key);
|
|
87
|
+
}
|
|
88
|
+
function tracedChatCompletionsCreate(original) {
|
|
89
|
+
return function(opts, ...rest) {
|
|
90
|
+
const tracer = import_api.trace.getTracer(TRACER_NAME);
|
|
91
|
+
const model = opts?.model ?? "";
|
|
92
|
+
const messages = opts?.messages ?? [];
|
|
93
|
+
const isStream = opts?.stream === true;
|
|
94
|
+
const span = tracer.startSpan("azure_openai.chat.completions.create", {
|
|
95
|
+
attributes: {
|
|
96
|
+
"neatlogs.span.kind": "LLM",
|
|
97
|
+
"neatlogs.llm.provider": PROVIDER,
|
|
98
|
+
"neatlogs.llm.system": SYSTEM,
|
|
99
|
+
"neatlogs.llm.model_name": model,
|
|
100
|
+
"neatlogs.llm.is_streaming": isStream
|
|
101
|
+
}
|
|
102
|
+
}, import_api.context.active());
|
|
103
|
+
for (let i = 0; i < messages.length; i++) {
|
|
104
|
+
const msg = messages[i];
|
|
105
|
+
span.setAttribute(`neatlogs.llm.input_messages.${i}.role`, msg.role ?? "");
|
|
106
|
+
if (typeof msg.content === "string") {
|
|
107
|
+
span.setAttribute(`neatlogs.llm.input_messages.${i}.content`, msg.content);
|
|
108
|
+
} else if (msg.content) {
|
|
109
|
+
span.setAttribute(`neatlogs.llm.input_messages.${i}.content`, safeStringify(msg.content));
|
|
110
|
+
}
|
|
111
|
+
if (msg.tool_call_id) {
|
|
112
|
+
span.setAttribute(`neatlogs.llm.input_messages.${i}.tool_call_id`, msg.tool_call_id);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (opts?.tools) {
|
|
116
|
+
for (let i = 0; i < opts.tools.length; i++) {
|
|
117
|
+
const fn = opts.tools[i]?.function ?? {};
|
|
118
|
+
span.setAttribute(`neatlogs.llm.tools.${i}.name`, fn.name ?? "");
|
|
119
|
+
if (fn.description) span.setAttribute(`neatlogs.llm.tools.${i}.description`, fn.description);
|
|
120
|
+
if (fn.parameters) span.setAttribute(`neatlogs.llm.tools.${i}.input_schema`, safeStringify(fn.parameters));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
setInvocationParams(span, opts);
|
|
124
|
+
if (isStream) {
|
|
125
|
+
opts = { ...opts };
|
|
126
|
+
const streamOpts = opts.stream_options ?? {};
|
|
127
|
+
if (!streamOpts.include_usage) {
|
|
128
|
+
opts.stream_options = { ...streamOpts, include_usage: true };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const ctx = import_api.trace.setSpan(import_api.context.active(), span);
|
|
132
|
+
const promise = import_api.context.with(ctx, () => original(opts, ...rest));
|
|
133
|
+
return promise.then(
|
|
134
|
+
(response) => {
|
|
135
|
+
if (isStream) {
|
|
136
|
+
return wrapAsyncIterableStream(response, span);
|
|
137
|
+
}
|
|
138
|
+
finalizeChatResponse(span, response);
|
|
139
|
+
return response;
|
|
140
|
+
},
|
|
141
|
+
(err) => {
|
|
142
|
+
recordError(span, err);
|
|
143
|
+
throw err;
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function tracedResponsesCreate(original) {
|
|
149
|
+
return function(opts, ...rest) {
|
|
150
|
+
const tracer = import_api.trace.getTracer(TRACER_NAME);
|
|
151
|
+
const model = opts?.model ?? "";
|
|
152
|
+
const span = tracer.startSpan("azure_openai.responses.create", {
|
|
153
|
+
attributes: {
|
|
154
|
+
"neatlogs.span.kind": "LLM",
|
|
155
|
+
"neatlogs.llm.provider": PROVIDER,
|
|
156
|
+
"neatlogs.llm.system": SYSTEM,
|
|
157
|
+
"neatlogs.llm.model_name": model,
|
|
158
|
+
"input.value": safeStringify(opts?.input ?? "")
|
|
159
|
+
}
|
|
160
|
+
}, import_api.context.active());
|
|
161
|
+
const ctx = import_api.trace.setSpan(import_api.context.active(), span);
|
|
162
|
+
const promise = import_api.context.with(ctx, () => original(opts, ...rest));
|
|
163
|
+
return promise.then(
|
|
164
|
+
(response) => {
|
|
165
|
+
if (response?.output_text) {
|
|
166
|
+
span.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
|
|
167
|
+
span.setAttribute("neatlogs.llm.output_messages.0.content", response.output_text);
|
|
168
|
+
}
|
|
169
|
+
if (response?.model) span.setAttribute("neatlogs.llm.model_name", response.model);
|
|
170
|
+
if (response?.usage) {
|
|
171
|
+
if (response.usage.input_tokens != null) span.setAttribute("neatlogs.llm.token_count.prompt", response.usage.input_tokens);
|
|
172
|
+
if (response.usage.output_tokens != null) span.setAttribute("neatlogs.llm.token_count.completion", response.usage.output_tokens);
|
|
173
|
+
}
|
|
174
|
+
span.setStatus({ code: import_api.SpanStatusCode.OK });
|
|
175
|
+
span.end();
|
|
176
|
+
return response;
|
|
177
|
+
},
|
|
178
|
+
(err) => {
|
|
179
|
+
recordError(span, err);
|
|
180
|
+
throw err;
|
|
181
|
+
}
|
|
182
|
+
);
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function wrapAsyncIterableStream(stream, span) {
|
|
186
|
+
const chunks = [];
|
|
187
|
+
const originalAsyncIterator = stream?.[Symbol.asyncIterator]?.bind(stream);
|
|
188
|
+
if (!originalAsyncIterator) {
|
|
189
|
+
span.setStatus({ code: import_api.SpanStatusCode.OK });
|
|
190
|
+
span.end();
|
|
191
|
+
return stream;
|
|
192
|
+
}
|
|
193
|
+
const wrapped = Object.create(Object.getPrototypeOf(stream));
|
|
194
|
+
Object.assign(wrapped, stream);
|
|
195
|
+
wrapped[Symbol.asyncIterator] = function() {
|
|
196
|
+
const iterator = originalAsyncIterator();
|
|
197
|
+
return {
|
|
198
|
+
async next() {
|
|
199
|
+
try {
|
|
200
|
+
const result = await iterator.next();
|
|
201
|
+
if (result.done) {
|
|
202
|
+
finalizeStreamChunks(span, chunks);
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
chunks.push(result.value);
|
|
206
|
+
return result;
|
|
207
|
+
} catch (err) {
|
|
208
|
+
recordError(span, err);
|
|
209
|
+
throw err;
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
async return(value) {
|
|
213
|
+
finalizeStreamChunks(span, chunks);
|
|
214
|
+
return iterator.return?.(value) ?? { done: true, value: void 0 };
|
|
215
|
+
},
|
|
216
|
+
async throw(err) {
|
|
217
|
+
recordError(span, err);
|
|
218
|
+
return iterator.throw?.(err) ?? { done: true, value: void 0 };
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
return wrapped;
|
|
223
|
+
}
|
|
224
|
+
function finalizeStreamChunks(span, chunks) {
|
|
225
|
+
const textParts = [];
|
|
226
|
+
const toolCallsAcc = {};
|
|
227
|
+
let finishReason = "";
|
|
228
|
+
let model = "";
|
|
229
|
+
let usage = null;
|
|
230
|
+
for (const chunk of chunks) {
|
|
231
|
+
if (!chunk?.choices?.length) {
|
|
232
|
+
if (chunk?.usage) usage = chunk.usage;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
const choice = chunk.choices[0];
|
|
236
|
+
const delta = choice?.delta;
|
|
237
|
+
if (delta?.content) textParts.push(delta.content);
|
|
238
|
+
if (delta?.tool_calls) {
|
|
239
|
+
for (const tc of delta.tool_calls) {
|
|
240
|
+
const idx = tc.index ?? 0;
|
|
241
|
+
if (!toolCallsAcc[idx]) toolCallsAcc[idx] = { id: "", name: "", arguments: "" };
|
|
242
|
+
if (tc.id) toolCallsAcc[idx].id = tc.id;
|
|
243
|
+
if (tc.function?.name) toolCallsAcc[idx].name = tc.function.name;
|
|
244
|
+
if (tc.function?.arguments) toolCallsAcc[idx].arguments += tc.function.arguments;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (choice?.finish_reason) finishReason = choice.finish_reason;
|
|
248
|
+
if (chunk?.model) model = chunk.model;
|
|
249
|
+
}
|
|
250
|
+
const fullText = textParts.join("");
|
|
251
|
+
if (fullText) {
|
|
252
|
+
span.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
|
|
253
|
+
span.setAttribute("neatlogs.llm.output_messages.0.content", fullText);
|
|
254
|
+
}
|
|
255
|
+
let j = 0;
|
|
256
|
+
for (const tc of Object.values(toolCallsAcc)) {
|
|
257
|
+
span.setAttribute(`neatlogs.llm.tool_calls.${j}.id`, tc.id);
|
|
258
|
+
span.setAttribute(`neatlogs.llm.tool_calls.${j}.name`, tc.name);
|
|
259
|
+
span.setAttribute(`neatlogs.llm.tool_calls.${j}.arguments`, tc.arguments);
|
|
260
|
+
j++;
|
|
261
|
+
}
|
|
262
|
+
if (model) span.setAttribute("neatlogs.llm.model_name", model);
|
|
263
|
+
if (finishReason) span.setAttribute("neatlogs.llm.finish_reason", finishReason);
|
|
264
|
+
if (usage) {
|
|
265
|
+
if (usage.prompt_tokens != null) span.setAttribute("neatlogs.llm.token_count.prompt", usage.prompt_tokens);
|
|
266
|
+
if (usage.completion_tokens != null) span.setAttribute("neatlogs.llm.token_count.completion", usage.completion_tokens);
|
|
267
|
+
if (usage.total_tokens != null) span.setAttribute("neatlogs.llm.token_count.total", usage.total_tokens);
|
|
268
|
+
if (usage.prompt_tokens_details?.cached_tokens != null) {
|
|
269
|
+
span.setAttribute("neatlogs.llm.token_count.cache_read", usage.prompt_tokens_details.cached_tokens);
|
|
270
|
+
}
|
|
271
|
+
if (usage.completion_tokens_details?.reasoning_tokens != null) {
|
|
272
|
+
span.setAttribute("neatlogs.llm.token_count.reasoning", usage.completion_tokens_details.reasoning_tokens);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
span.setStatus({ code: import_api.SpanStatusCode.OK });
|
|
276
|
+
span.end();
|
|
277
|
+
}
|
|
278
|
+
function finalizeChatResponse(span, response) {
|
|
279
|
+
const choices = response?.choices ?? [];
|
|
280
|
+
for (let i = 0; i < choices.length; i++) {
|
|
281
|
+
const message = choices[i]?.message;
|
|
282
|
+
if (!message) continue;
|
|
283
|
+
span.setAttribute(`neatlogs.llm.output_messages.${i}.role`, "assistant");
|
|
284
|
+
if (message.content) {
|
|
285
|
+
span.setAttribute(`neatlogs.llm.output_messages.${i}.content`, message.content);
|
|
286
|
+
}
|
|
287
|
+
if (message.tool_calls) {
|
|
288
|
+
for (let j = 0; j < message.tool_calls.length; j++) {
|
|
289
|
+
const tc = message.tool_calls[j];
|
|
290
|
+
span.setAttribute(`neatlogs.llm.tool_calls.${j}.id`, tc.id ?? "");
|
|
291
|
+
span.setAttribute(`neatlogs.llm.tool_calls.${j}.name`, tc.function?.name ?? "");
|
|
292
|
+
span.setAttribute(`neatlogs.llm.tool_calls.${j}.arguments`, tc.function?.arguments ?? "");
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (choices[i].finish_reason) {
|
|
296
|
+
span.setAttribute("neatlogs.llm.finish_reason", choices[i].finish_reason);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const usage = response?.usage;
|
|
300
|
+
if (usage) {
|
|
301
|
+
if (usage.prompt_tokens != null) span.setAttribute("neatlogs.llm.token_count.prompt", usage.prompt_tokens);
|
|
302
|
+
if (usage.completion_tokens != null) span.setAttribute("neatlogs.llm.token_count.completion", usage.completion_tokens);
|
|
303
|
+
if (usage.total_tokens != null) span.setAttribute("neatlogs.llm.token_count.total", usage.total_tokens);
|
|
304
|
+
if (usage.prompt_tokens_details?.cached_tokens != null) {
|
|
305
|
+
span.setAttribute("neatlogs.llm.token_count.cache_read", usage.prompt_tokens_details.cached_tokens);
|
|
306
|
+
}
|
|
307
|
+
if (usage.completion_tokens_details?.reasoning_tokens != null) {
|
|
308
|
+
span.setAttribute("neatlogs.llm.token_count.reasoning", usage.completion_tokens_details.reasoning_tokens);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (response?.model) span.setAttribute("neatlogs.llm.model_name", response.model);
|
|
312
|
+
span.setStatus({ code: import_api.SpanStatusCode.OK });
|
|
313
|
+
span.end();
|
|
314
|
+
}
|
|
315
|
+
function setInvocationParams(span, opts) {
|
|
316
|
+
if (opts?.temperature != null) span.setAttribute("neatlogs.llm.temperature", opts.temperature);
|
|
317
|
+
if (opts?.top_p != null) span.setAttribute("neatlogs.llm.top_p", opts.top_p);
|
|
318
|
+
if (opts?.max_tokens != null) span.setAttribute("neatlogs.llm.max_tokens", opts.max_tokens);
|
|
319
|
+
if (opts?.frequency_penalty != null) span.setAttribute("neatlogs.llm.frequency_penalty", opts.frequency_penalty);
|
|
320
|
+
if (opts?.presence_penalty != null) span.setAttribute("neatlogs.llm.presence_penalty", opts.presence_penalty);
|
|
321
|
+
if (opts?.stop) span.setAttribute("neatlogs.llm.stop_sequences", safeStringify(opts.stop));
|
|
322
|
+
}
|
|
323
|
+
function safeStringify(value) {
|
|
324
|
+
try {
|
|
325
|
+
return typeof value === "string" ? value : JSON.stringify(value);
|
|
326
|
+
} catch {
|
|
327
|
+
return "";
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function recordError(span, err) {
|
|
331
|
+
if (err instanceof Error) {
|
|
332
|
+
span.setStatus({ code: import_api.SpanStatusCode.ERROR, message: err.message });
|
|
333
|
+
span.recordException(err);
|
|
334
|
+
} else {
|
|
335
|
+
span.setStatus({ code: import_api.SpanStatusCode.ERROR, message: String(err) });
|
|
336
|
+
}
|
|
337
|
+
span.end();
|
|
338
|
+
}
|
|
339
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
340
|
+
0 && (module.exports = {
|
|
341
|
+
traceTool,
|
|
342
|
+
wrapAzureOpenAI
|
|
343
|
+
});
|
|
344
|
+
//# sourceMappingURL=azure-openai.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/azure-openai.ts"],"sourcesContent":["/**\n * Neatlogs Azure OpenAI wrapper — ES6 Proxy-based.\n *\n * Azure OpenAI is accessed through the `openai` SDK's `AzureOpenAI` client,\n * which exposes the same `chat.completions.create()` shape as `OpenAI`. This\n * wrapper traces those calls with provider `azure` so Azure traffic is\n * distinguished from direct OpenAI traffic.\n *\n * Usage:\n * import { wrapAzureOpenAI } from 'neatlogs/azure-openai';\n * import { AzureOpenAI } from 'openai';\n * const client = wrapAzureOpenAI(new AzureOpenAI({ endpoint, apiKey, apiVersion }));\n * await client.chat.completions.create({ model: 'gpt-4o', messages: [...] });\n *\n * Intercepts:\n * - client.chat.completions.create() — non-streaming and streaming\n * - client.responses.create() — Responses API\n */\n\nimport { trace, context as otelContext, SpanStatusCode, type Span } from '@opentelemetry/api';\n\nconst TRACER_NAME = 'neatlogs.azure_openai';\nconst PROVIDER = 'azure';\nconst SYSTEM = 'azure';\n\nexport function wrapAzureOpenAI<T extends object>(client: T): T {\n return wrapNamespace(client, []) as T;\n}\n\n/**\n * Wrap a tool/function implementation to emit TOOL spans when executed.\n *\n * Usage:\n * const getWeather = traceTool('get_weather', async (args: { city: string }) => {\n * return `Weather in ${args.city}: sunny`;\n * });\n */\nexport function traceTool<TArgs = any, TResult = any>(\n name: string,\n fn: (args: TArgs) => TResult | Promise<TResult>,\n): (args: TArgs) => Promise<TResult> {\n return async function tracedTool(args: TArgs): Promise<TResult> {\n const tracer = trace.getTracer(TRACER_NAME);\n return tracer.startActiveSpan(\n `tool.${name}`,\n {\n attributes: {\n 'neatlogs.span.kind': 'TOOL',\n 'neatlogs.tool.name': name,\n 'input.value': safeStringify(args),\n },\n },\n otelContext.active(),\n async (span) => {\n try {\n const result = await fn(args);\n span.setAttribute('output.value', safeStringify(result));\n span.setStatus({ code: SpanStatusCode.OK });\n return result;\n } catch (err) {\n recordError(span, err);\n throw err;\n } finally {\n span.end();\n }\n },\n );\n };\n}\n\nfunction wrapNamespace(target: any, path: string[]): any {\n return new Proxy(target, {\n get(obj, prop, receiver) {\n const value = Reflect.get(obj, prop, receiver);\n if (typeof prop === 'symbol' || String(prop).startsWith('_')) return value;\n\n const currentPath = [...path, String(prop)];\n const pathStr = currentPath.join('.');\n\n if (pathStr === 'chat.completions.create' && typeof value === 'function') {\n return tracedChatCompletionsCreate(value.bind(obj));\n }\n if (pathStr === 'responses.create' && typeof value === 'function') {\n return tracedResponsesCreate(value.bind(obj));\n }\n\n if (value && typeof value === 'object' && !Array.isArray(value) && isNamespace(currentPath)) {\n return wrapNamespace(value, currentPath);\n }\n\n return value;\n },\n });\n}\n\nfunction isNamespace(path: string[]): boolean {\n if (path.length > 3) return false;\n const key = path[path.length - 1];\n return ['chat', 'completions', 'responses', 'beta'].includes(key);\n}\n\n// ---------------------------------------------------------------------------\n// chat.completions.create — non-streaming + streaming\n// ---------------------------------------------------------------------------\n\nfunction tracedChatCompletionsCreate(original: (...args: any[]) => any) {\n return function (opts: any, ...rest: any[]): any {\n const tracer = trace.getTracer(TRACER_NAME);\n const model = opts?.model ?? '';\n const messages: any[] = opts?.messages ?? [];\n const isStream = opts?.stream === true;\n\n const span = tracer.startSpan('azure_openai.chat.completions.create', {\n attributes: {\n 'neatlogs.span.kind': 'LLM',\n 'neatlogs.llm.provider': PROVIDER,\n 'neatlogs.llm.system': SYSTEM,\n 'neatlogs.llm.model_name': model,\n 'neatlogs.llm.is_streaming': isStream,\n },\n }, otelContext.active());\n\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n span.setAttribute(`neatlogs.llm.input_messages.${i}.role`, msg.role ?? '');\n if (typeof msg.content === 'string') {\n span.setAttribute(`neatlogs.llm.input_messages.${i}.content`, msg.content);\n } else if (msg.content) {\n span.setAttribute(`neatlogs.llm.input_messages.${i}.content`, safeStringify(msg.content));\n }\n if (msg.tool_call_id) {\n span.setAttribute(`neatlogs.llm.input_messages.${i}.tool_call_id`, msg.tool_call_id);\n }\n }\n\n if (opts?.tools) {\n for (let i = 0; i < opts.tools.length; i++) {\n const fn = opts.tools[i]?.function ?? {};\n span.setAttribute(`neatlogs.llm.tools.${i}.name`, fn.name ?? '');\n if (fn.description) span.setAttribute(`neatlogs.llm.tools.${i}.description`, fn.description);\n if (fn.parameters) span.setAttribute(`neatlogs.llm.tools.${i}.input_schema`, safeStringify(fn.parameters));\n }\n }\n\n setInvocationParams(span, opts);\n\n if (isStream) {\n opts = { ...opts };\n const streamOpts = opts.stream_options ?? {};\n if (!streamOpts.include_usage) {\n opts.stream_options = { ...streamOpts, include_usage: true };\n }\n }\n\n const ctx = trace.setSpan(otelContext.active(), span);\n const promise = otelContext.with(ctx, () => original(opts, ...rest));\n\n return promise.then(\n (response: any) => {\n if (isStream) {\n return wrapAsyncIterableStream(response, span);\n }\n finalizeChatResponse(span, response);\n return response;\n },\n (err: any) => {\n recordError(span, err);\n throw err;\n },\n );\n };\n}\n\n// ---------------------------------------------------------------------------\n// responses.create\n// ---------------------------------------------------------------------------\n\nfunction tracedResponsesCreate(original: (...args: any[]) => any) {\n return function (opts: any, ...rest: any[]): any {\n const tracer = trace.getTracer(TRACER_NAME);\n const model = opts?.model ?? '';\n\n const span = tracer.startSpan('azure_openai.responses.create', {\n attributes: {\n 'neatlogs.span.kind': 'LLM',\n 'neatlogs.llm.provider': PROVIDER,\n 'neatlogs.llm.system': SYSTEM,\n 'neatlogs.llm.model_name': model,\n 'input.value': safeStringify(opts?.input ?? ''),\n },\n }, otelContext.active());\n\n const ctx = trace.setSpan(otelContext.active(), span);\n const promise = otelContext.with(ctx, () => original(opts, ...rest));\n\n return promise.then(\n (response: any) => {\n if (response?.output_text) {\n span.setAttribute('neatlogs.llm.output_messages.0.role', 'assistant');\n span.setAttribute('neatlogs.llm.output_messages.0.content', response.output_text);\n }\n if (response?.model) span.setAttribute('neatlogs.llm.model_name', response.model);\n if (response?.usage) {\n if (response.usage.input_tokens != null) span.setAttribute('neatlogs.llm.token_count.prompt', response.usage.input_tokens);\n if (response.usage.output_tokens != null) span.setAttribute('neatlogs.llm.token_count.completion', response.usage.output_tokens);\n }\n span.setStatus({ code: SpanStatusCode.OK });\n span.end();\n return response;\n },\n (err: any) => {\n recordError(span, err);\n throw err;\n },\n );\n };\n}\n\n// ---------------------------------------------------------------------------\n// Streaming support\n// ---------------------------------------------------------------------------\n\nfunction wrapAsyncIterableStream(stream: any, span: Span): any {\n const chunks: any[] = [];\n const originalAsyncIterator = stream?.[Symbol.asyncIterator]?.bind(stream);\n\n if (!originalAsyncIterator) {\n span.setStatus({ code: SpanStatusCode.OK });\n span.end();\n return stream;\n }\n\n const wrapped = Object.create(Object.getPrototypeOf(stream));\n Object.assign(wrapped, stream);\n\n wrapped[Symbol.asyncIterator] = function () {\n const iterator = originalAsyncIterator();\n return {\n async next(): Promise<IteratorResult<any>> {\n try {\n const result = await iterator.next();\n if (result.done) {\n finalizeStreamChunks(span, chunks);\n return result;\n }\n chunks.push(result.value);\n return result;\n } catch (err) {\n recordError(span, err);\n throw err;\n }\n },\n async return(value?: any): Promise<IteratorResult<any>> {\n finalizeStreamChunks(span, chunks);\n return iterator.return?.(value) ?? { done: true, value: undefined };\n },\n async throw(err?: any): Promise<IteratorResult<any>> {\n recordError(span, err);\n return iterator.throw?.(err) ?? { done: true, value: undefined };\n },\n };\n };\n\n return wrapped;\n}\n\nfunction finalizeStreamChunks(span: Span, chunks: any[]): void {\n const textParts: string[] = [];\n const toolCallsAcc: Record<number, { id: string; name: string; arguments: string }> = {};\n let finishReason = '';\n let model = '';\n let usage: any = null;\n\n for (const chunk of chunks) {\n if (!chunk?.choices?.length) {\n if (chunk?.usage) usage = chunk.usage;\n continue;\n }\n const choice = chunk.choices[0];\n const delta = choice?.delta;\n if (delta?.content) textParts.push(delta.content);\n if (delta?.tool_calls) {\n for (const tc of delta.tool_calls) {\n const idx = tc.index ?? 0;\n if (!toolCallsAcc[idx]) toolCallsAcc[idx] = { id: '', name: '', arguments: '' };\n if (tc.id) toolCallsAcc[idx].id = tc.id;\n if (tc.function?.name) toolCallsAcc[idx].name = tc.function.name;\n if (tc.function?.arguments) toolCallsAcc[idx].arguments += tc.function.arguments;\n }\n }\n if (choice?.finish_reason) finishReason = choice.finish_reason;\n if (chunk?.model) model = chunk.model;\n }\n\n const fullText = textParts.join('');\n if (fullText) {\n span.setAttribute('neatlogs.llm.output_messages.0.role', 'assistant');\n span.setAttribute('neatlogs.llm.output_messages.0.content', fullText);\n }\n\n let j = 0;\n for (const tc of Object.values(toolCallsAcc)) {\n span.setAttribute(`neatlogs.llm.tool_calls.${j}.id`, tc.id);\n span.setAttribute(`neatlogs.llm.tool_calls.${j}.name`, tc.name);\n span.setAttribute(`neatlogs.llm.tool_calls.${j}.arguments`, tc.arguments);\n j++;\n }\n\n if (model) span.setAttribute('neatlogs.llm.model_name', model);\n if (finishReason) span.setAttribute('neatlogs.llm.finish_reason', finishReason);\n\n if (usage) {\n if (usage.prompt_tokens != null) span.setAttribute('neatlogs.llm.token_count.prompt', usage.prompt_tokens);\n if (usage.completion_tokens != null) span.setAttribute('neatlogs.llm.token_count.completion', usage.completion_tokens);\n if (usage.total_tokens != null) span.setAttribute('neatlogs.llm.token_count.total', usage.total_tokens);\n if (usage.prompt_tokens_details?.cached_tokens != null) {\n span.setAttribute('neatlogs.llm.token_count.cache_read', usage.prompt_tokens_details.cached_tokens);\n }\n if (usage.completion_tokens_details?.reasoning_tokens != null) {\n span.setAttribute('neatlogs.llm.token_count.reasoning', usage.completion_tokens_details.reasoning_tokens);\n }\n }\n\n span.setStatus({ code: SpanStatusCode.OK });\n span.end();\n}\n\n// ---------------------------------------------------------------------------\n// Non-streaming response finalization\n// ---------------------------------------------------------------------------\n\nfunction finalizeChatResponse(span: Span, response: any): void {\n const choices = response?.choices ?? [];\n for (let i = 0; i < choices.length; i++) {\n const message = choices[i]?.message;\n if (!message) continue;\n\n span.setAttribute(`neatlogs.llm.output_messages.${i}.role`, 'assistant');\n if (message.content) {\n span.setAttribute(`neatlogs.llm.output_messages.${i}.content`, message.content);\n }\n if (message.tool_calls) {\n for (let j = 0; j < message.tool_calls.length; j++) {\n const tc = message.tool_calls[j];\n span.setAttribute(`neatlogs.llm.tool_calls.${j}.id`, tc.id ?? '');\n span.setAttribute(`neatlogs.llm.tool_calls.${j}.name`, tc.function?.name ?? '');\n span.setAttribute(`neatlogs.llm.tool_calls.${j}.arguments`, tc.function?.arguments ?? '');\n }\n }\n if (choices[i].finish_reason) {\n span.setAttribute('neatlogs.llm.finish_reason', choices[i].finish_reason);\n }\n }\n\n const usage = response?.usage;\n if (usage) {\n if (usage.prompt_tokens != null) span.setAttribute('neatlogs.llm.token_count.prompt', usage.prompt_tokens);\n if (usage.completion_tokens != null) span.setAttribute('neatlogs.llm.token_count.completion', usage.completion_tokens);\n if (usage.total_tokens != null) span.setAttribute('neatlogs.llm.token_count.total', usage.total_tokens);\n if (usage.prompt_tokens_details?.cached_tokens != null) {\n span.setAttribute('neatlogs.llm.token_count.cache_read', usage.prompt_tokens_details.cached_tokens);\n }\n if (usage.completion_tokens_details?.reasoning_tokens != null) {\n span.setAttribute('neatlogs.llm.token_count.reasoning', usage.completion_tokens_details.reasoning_tokens);\n }\n }\n\n if (response?.model) span.setAttribute('neatlogs.llm.model_name', response.model);\n\n span.setStatus({ code: SpanStatusCode.OK });\n span.end();\n}\n\n// ---------------------------------------------------------------------------\n// Utilities\n// ---------------------------------------------------------------------------\n\nfunction setInvocationParams(span: Span, opts: any): void {\n if (opts?.temperature != null) span.setAttribute('neatlogs.llm.temperature', opts.temperature);\n if (opts?.top_p != null) span.setAttribute('neatlogs.llm.top_p', opts.top_p);\n if (opts?.max_tokens != null) span.setAttribute('neatlogs.llm.max_tokens', opts.max_tokens);\n if (opts?.frequency_penalty != null) span.setAttribute('neatlogs.llm.frequency_penalty', opts.frequency_penalty);\n if (opts?.presence_penalty != null) span.setAttribute('neatlogs.llm.presence_penalty', opts.presence_penalty);\n if (opts?.stop) span.setAttribute('neatlogs.llm.stop_sequences', safeStringify(opts.stop));\n}\n\nfunction safeStringify(value: unknown): string {\n try {\n return typeof value === 'string' ? value : JSON.stringify(value);\n } catch {\n return '';\n }\n}\n\nfunction recordError(span: Span, err: unknown): void {\n if (err instanceof Error) {\n span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });\n span.recordException(err);\n } else {\n span.setStatus({ code: SpanStatusCode.ERROR, message: String(err) });\n }\n span.end();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBA,iBAAyE;AAEzE,IAAM,cAAc;AACpB,IAAM,WAAW;AACjB,IAAM,SAAS;AAER,SAAS,gBAAkC,QAAc;AAC9D,SAAO,cAAc,QAAQ,CAAC,CAAC;AACjC;AAUO,SAAS,UACd,MACA,IACmC;AACnC,SAAO,eAAe,WAAW,MAA+B;AAC9D,UAAM,SAAS,iBAAM,UAAU,WAAW;AAC1C,WAAO,OAAO;AAAA,MACZ,QAAQ,IAAI;AAAA,MACZ;AAAA,QACE,YAAY;AAAA,UACV,sBAAsB;AAAA,UACtB,sBAAsB;AAAA,UACtB,eAAe,cAAc,IAAI;AAAA,QACnC;AAAA,MACF;AAAA,MACA,WAAAA,QAAY,OAAO;AAAA,MACnB,OAAO,SAAS;AACd,YAAI;AACF,gBAAM,SAAS,MAAM,GAAG,IAAI;AAC5B,eAAK,aAAa,gBAAgB,cAAc,MAAM,CAAC;AACvD,eAAK,UAAU,EAAE,MAAM,0BAAe,GAAG,CAAC;AAC1C,iBAAO;AAAA,QACT,SAAS,KAAK;AACZ,sBAAY,MAAM,GAAG;AACrB,gBAAM;AAAA,QACR,UAAE;AACA,eAAK,IAAI;AAAA,QACX;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,cAAc,QAAa,MAAqB;AACvD,SAAO,IAAI,MAAM,QAAQ;AAAA,IACvB,IAAI,KAAK,MAAM,UAAU;AACvB,YAAM,QAAQ,QAAQ,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AAA,EACrE;AACA,OAAK,IAAI;AACX;","names":["otelContext"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Neatlogs Azure OpenAI wrapper — ES6 Proxy-based.
|
|
3
|
+
*
|
|
4
|
+
* Azure OpenAI is accessed through the `openai` SDK's `AzureOpenAI` client,
|
|
5
|
+
* which exposes the same `chat.completions.create()` shape as `OpenAI`. This
|
|
6
|
+
* wrapper traces those calls with provider `azure` so Azure traffic is
|
|
7
|
+
* distinguished from direct OpenAI traffic.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import { wrapAzureOpenAI } from 'neatlogs/azure-openai';
|
|
11
|
+
* import { AzureOpenAI } from 'openai';
|
|
12
|
+
* const client = wrapAzureOpenAI(new AzureOpenAI({ endpoint, apiKey, apiVersion }));
|
|
13
|
+
* await client.chat.completions.create({ model: 'gpt-4o', messages: [...] });
|
|
14
|
+
*
|
|
15
|
+
* Intercepts:
|
|
16
|
+
* - client.chat.completions.create() — non-streaming and streaming
|
|
17
|
+
* - client.responses.create() — Responses API
|
|
18
|
+
*/
|
|
19
|
+
declare function wrapAzureOpenAI<T extends object>(client: T): T;
|
|
20
|
+
/**
|
|
21
|
+
* Wrap a tool/function implementation to emit TOOL spans when executed.
|
|
22
|
+
*
|
|
23
|
+
* Usage:
|
|
24
|
+
* const getWeather = traceTool('get_weather', async (args: { city: string }) => {
|
|
25
|
+
* return `Weather in ${args.city}: sunny`;
|
|
26
|
+
* });
|
|
27
|
+
*/
|
|
28
|
+
declare function traceTool<TArgs = any, TResult = any>(name: string, fn: (args: TArgs) => TResult | Promise<TResult>): (args: TArgs) => Promise<TResult>;
|
|
29
|
+
|
|
30
|
+
export { traceTool, wrapAzureOpenAI };
|