@anuma/sdk 6.15.2 → 6.16.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.
@@ -0,0 +1,255 @@
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/telemetry/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createMetricsHooks: () => createMetricsHooks,
24
+ createRecallDiagnosticsHandler: () => createRecallDiagnosticsHandler,
25
+ noopTelemetrySink: () => noopTelemetrySink
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/telemetry/metrics.ts
30
+ var defaultNowMs = () => typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
31
+ function createMetricsHooks(sink, opts) {
32
+ const now = opts?.now ?? defaultNowMs;
33
+ const includeErrorMessages = opts?.includeErrorMessages ?? false;
34
+ const runs = /* @__PURE__ */ new Map();
35
+ const swallow = (call) => {
36
+ try {
37
+ const result = call();
38
+ if (result && typeof result.then === "function") {
39
+ void Promise.resolve(result).catch(() => {
40
+ });
41
+ }
42
+ } catch {
43
+ }
44
+ };
45
+ const track = (event, properties) => {
46
+ swallow(() => sink.track?.(event, properties));
47
+ };
48
+ const metric = (name, value, tags) => {
49
+ swallow(() => sink.metric?.(name, value, tags));
50
+ };
51
+ const errorProps = (error) => includeErrorMessages && error !== void 0 ? { error } : {};
52
+ const stateFor = (runId) => {
53
+ let state = runs.get(runId);
54
+ if (!state) {
55
+ state = { startedAt: now(), modelCalls: /* @__PURE__ */ new Map(), toolCalls: /* @__PURE__ */ new Map() };
56
+ runs.set(runId, state);
57
+ }
58
+ return state;
59
+ };
60
+ const finishRun = (runId) => {
61
+ runs.delete(runId);
62
+ };
63
+ return {
64
+ onRunStart: (e) => {
65
+ const state = stateFor(e.runId);
66
+ state.model = e.model;
67
+ state.startedAt = now();
68
+ track("run.started", { runId: e.runId, model: e.model });
69
+ },
70
+ onRunEnd: (e) => {
71
+ const state = stateFor(e.runId);
72
+ const durationMs = now() - state.startedAt;
73
+ track("run.completed", {
74
+ runId: e.runId,
75
+ totalSteps: e.totalSteps,
76
+ durationMs
77
+ });
78
+ metric("run.duration", durationMs, {
79
+ ...state.model ? { model: state.model } : {},
80
+ outcome: "completed"
81
+ });
82
+ finishRun(e.runId);
83
+ },
84
+ onRunError: (e) => {
85
+ const state = stateFor(e.runId);
86
+ const durationMs = now() - state.startedAt;
87
+ const errorType = e.errorObject?.name ?? "Error";
88
+ track("run.failed", {
89
+ runId: e.runId,
90
+ durationMs,
91
+ errorType,
92
+ ...errorProps(e.error),
93
+ stage: e.stage
94
+ });
95
+ metric("run.duration", durationMs, {
96
+ ...state.model ? { model: state.model } : {},
97
+ outcome: "failed"
98
+ });
99
+ finishRun(e.runId);
100
+ },
101
+ beforeModelCall: (e) => {
102
+ const state = stateFor(e.runId);
103
+ state.modelCalls.set(e.stepIndex, { startedAt: now(), model: e.model });
104
+ },
105
+ afterModelCall: (e) => {
106
+ const state = runs.get(e.runId);
107
+ const pending = state?.modelCalls.get(e.stepIndex);
108
+ state?.modelCalls.delete(e.stepIndex);
109
+ const latencyMs = pending ? now() - pending.startedAt : void 0;
110
+ const base = {
111
+ runId: e.runId,
112
+ stepIndex: e.stepIndex,
113
+ ...latencyMs !== void 0 ? { latencyMs } : {},
114
+ ...pending?.model ? { model: pending.model } : {}
115
+ };
116
+ if (e.error !== void 0) {
117
+ track("model.call.failed", { ...base, ...errorProps(e.error) });
118
+ if (latencyMs !== void 0) {
119
+ metric("model.call.latency", latencyMs, {
120
+ ...pending?.model ? { model: pending.model } : {},
121
+ outcome: "failed"
122
+ });
123
+ }
124
+ return;
125
+ }
126
+ track("model.call.completed", {
127
+ ...base,
128
+ ...e.usage?.inputTokens !== void 0 ? { inputTokens: e.usage.inputTokens } : {},
129
+ ...e.usage?.outputTokens !== void 0 ? { outputTokens: e.usage.outputTokens } : {},
130
+ ...e.finishReason !== void 0 ? { finishReason: e.finishReason } : {}
131
+ });
132
+ if (latencyMs !== void 0) {
133
+ metric("model.call.latency", latencyMs, {
134
+ ...pending?.model ? { model: pending.model } : {},
135
+ outcome: "completed"
136
+ });
137
+ }
138
+ if (e.usage?.inputTokens !== void 0) {
139
+ metric("model.call.tokens", e.usage.inputTokens, {
140
+ ...pending?.model ? { model: pending.model } : {},
141
+ direction: "input"
142
+ });
143
+ }
144
+ if (e.usage?.outputTokens !== void 0) {
145
+ metric("model.call.tokens", e.usage.outputTokens, {
146
+ ...pending?.model ? { model: pending.model } : {},
147
+ direction: "output"
148
+ });
149
+ }
150
+ },
151
+ beforeToolUse: (e) => {
152
+ const state = stateFor(e.runId);
153
+ state.toolCalls.set(e.toolCallId, { startedAt: now(), toolName: e.name });
154
+ },
155
+ afterToolUse: (e) => {
156
+ const state = runs.get(e.runId);
157
+ const pending = state?.toolCalls.get(e.toolCallId);
158
+ state?.toolCalls.delete(e.toolCallId);
159
+ const durationMs = pending ? now() - pending.startedAt : void 0;
160
+ const base = {
161
+ runId: e.runId,
162
+ stepIndex: e.stepIndex,
163
+ toolCallId: e.toolCallId,
164
+ toolName: e.name,
165
+ ...durationMs !== void 0 ? { durationMs } : {}
166
+ };
167
+ if (e.error !== void 0 || e.errorType !== void 0) {
168
+ track("tool.call.failed", {
169
+ ...base,
170
+ ...errorProps(e.error),
171
+ ...e.errorType !== void 0 ? { errorType: e.errorType } : {}
172
+ });
173
+ if (durationMs !== void 0) {
174
+ metric("tool.call.duration", durationMs, {
175
+ toolName: e.name,
176
+ outcome: "failed",
177
+ ...e.errorType !== void 0 ? { errorType: e.errorType } : {}
178
+ });
179
+ }
180
+ return;
181
+ }
182
+ track("tool.call.completed", base);
183
+ if (durationMs !== void 0) {
184
+ metric("tool.call.duration", durationMs, {
185
+ toolName: e.name,
186
+ outcome: "completed"
187
+ });
188
+ }
189
+ }
190
+ };
191
+ }
192
+
193
+ // src/telemetry/recall.ts
194
+ function createRecallDiagnosticsHandler(sink) {
195
+ const swallow = (call) => {
196
+ try {
197
+ const result = call();
198
+ if (result && typeof result.then === "function") {
199
+ void Promise.resolve(result).catch(() => {
200
+ });
201
+ }
202
+ } catch {
203
+ }
204
+ };
205
+ const track = (event, properties) => {
206
+ swallow(() => sink.track?.(event, properties));
207
+ };
208
+ const metric = (name, value, tags) => {
209
+ swallow(() => sink.metric?.(name, value, tags));
210
+ };
211
+ return (diagnostics) => {
212
+ track("recall.completed", {
213
+ usedBudget: diagnostics.usedBudget,
214
+ reranked: diagnostics.reranked,
215
+ candidateCount: diagnostics.candidateCount,
216
+ factCount: diagnostics.factCount,
217
+ chunkCount: diagnostics.chunkCount,
218
+ totalMs: diagnostics.timings.total,
219
+ ...diagnostics.decryptLast !== void 0 ? { decryptLast: diagnostics.decryptLast } : {},
220
+ degradedCount: diagnostics.degraded.length
221
+ });
222
+ for (const reason of diagnostics.degraded) {
223
+ track("recall.degraded", { reason, usedBudget: diagnostics.usedBudget });
224
+ }
225
+ for (const [lane, ms] of Object.entries(diagnostics.timings)) {
226
+ metric("recall.duration", ms, { lane });
227
+ }
228
+ metric("recall.candidates", diagnostics.candidateCount, {});
229
+ metric("recall.facts", diagnostics.factCount, {});
230
+ metric("recall.chunks", diagnostics.chunkCount, {});
231
+ if (diagnostics.vaultSize !== void 0) {
232
+ metric("recall.vault.size", diagnostics.vaultSize, {});
233
+ }
234
+ if (diagnostics.vaultRowsDecrypted !== void 0) {
235
+ metric("recall.vault.rows_decrypted", diagnostics.vaultRowsDecrypted, {});
236
+ }
237
+ if (diagnostics.vaultRowsEmbedded !== void 0) {
238
+ metric("recall.vault.rows_embedded", diagnostics.vaultRowsEmbedded, {});
239
+ }
240
+ };
241
+ }
242
+
243
+ // src/telemetry/types.ts
244
+ var noopTelemetrySink = Object.freeze({
245
+ track: () => {
246
+ },
247
+ metric: () => {
248
+ }
249
+ });
250
+ // Annotate the CommonJS export names for ESM import in node:
251
+ 0 && (module.exports = {
252
+ createMetricsHooks,
253
+ createRecallDiagnosticsHandler,
254
+ noopTelemetrySink
255
+ });