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