mesedi 0.1.0 → 0.6.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/README.md +81 -31
- package/dist/_errorClassesGenerated.d.ts +18 -0
- package/dist/_errorClassesGenerated.d.ts.map +1 -0
- package/dist/_errorClassesGenerated.js +24 -0
- package/dist/_errorClassesGenerated.js.map +1 -0
- package/dist/anthropic_integration.d.ts +9 -9
- package/dist/anthropic_integration.d.ts.map +1 -1
- package/dist/anthropic_integration.js +67 -26
- package/dist/anthropic_integration.js.map +1 -1
- package/dist/client.d.ts +32 -12
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +46 -18
- package/dist/client.js.map +1 -1
- package/dist/cohere_integration.d.ts +42 -0
- package/dist/cohere_integration.d.ts.map +1 -0
- package/dist/cohere_integration.js +456 -0
- package/dist/cohere_integration.js.map +1 -0
- package/dist/compress.d.ts +69 -0
- package/dist/compress.d.ts.map +1 -0
- package/dist/compress.js +75 -0
- package/dist/compress.js.map +1 -0
- package/dist/context.d.ts +13 -4
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +15 -5
- package/dist/context.js.map +1 -1
- package/dist/errors.d.ts +137 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +462 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +19 -6
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +31 -6
- package/dist/events.js.map +1 -1
- package/dist/gemini_integration.d.ts +33 -0
- package/dist/gemini_integration.d.ts.map +1 -0
- package/dist/gemini_integration.js +334 -0
- package/dist/gemini_integration.js.map +1 -0
- package/dist/halt.d.ts +12 -12
- package/dist/halt.js +16 -16
- package/dist/halt.js.map +1 -1
- package/dist/halt_stream.d.ts +6 -6
- package/dist/halt_stream.d.ts.map +1 -1
- package/dist/halt_stream.js +22 -12
- package/dist/halt_stream.js.map +1 -1
- package/dist/index.d.ts +26 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -12
- package/dist/index.js.map +1 -1
- package/dist/integrations/langgraph.d.ts +99 -0
- package/dist/integrations/langgraph.d.ts.map +1 -0
- package/dist/integrations/langgraph.js +572 -0
- package/dist/integrations/langgraph.js.map +1 -0
- package/dist/integrations/mastra.d.ts +114 -0
- package/dist/integrations/mastra.d.ts.map +1 -0
- package/dist/integrations/mastra.js +354 -0
- package/dist/integrations/mastra.js.map +1 -0
- package/dist/integrations/openai_agents.d.ts +66 -0
- package/dist/integrations/openai_agents.d.ts.map +1 -0
- package/dist/integrations/openai_agents.js +243 -0
- package/dist/integrations/openai_agents.js.map +1 -0
- package/dist/integrations/promptfoo.d.ts +62 -0
- package/dist/integrations/promptfoo.d.ts.map +1 -0
- package/dist/integrations/promptfoo.js +76 -0
- package/dist/integrations/promptfoo.js.map +1 -0
- package/dist/integrations/ragas.d.ts +65 -0
- package/dist/integrations/ragas.d.ts.map +1 -0
- package/dist/integrations/ragas.js +78 -0
- package/dist/integrations/ragas.js.map +1 -0
- package/dist/integrations/vectara.d.ts +60 -0
- package/dist/integrations/vectara.d.ts.map +1 -0
- package/dist/integrations/vectara.js +59 -0
- package/dist/integrations/vectara.js.map +1 -0
- package/dist/integrations/vercel_ai.d.ts +6 -6
- package/dist/integrations/vercel_ai.js +10 -10
- package/dist/integrations/vercel_ai.js.map +1 -1
- package/dist/observe.d.ts +358 -3
- package/dist/observe.d.ts.map +1 -1
- package/dist/observe.js +536 -4
- package/dist/observe.js.map +1 -1
- package/dist/ollama_integration.d.ts +126 -0
- package/dist/ollama_integration.d.ts.map +1 -0
- package/dist/ollama_integration.js +404 -0
- package/dist/ollama_integration.js.map +1 -0
- package/dist/openai_integration.d.ts +67 -0
- package/dist/openai_integration.d.ts.map +1 -0
- package/dist/openai_integration.js +574 -0
- package/dist/openai_integration.js.map +1 -0
- package/dist/tool.d.ts +1 -1
- package/dist/tool.d.ts.map +1 -1
- package/dist/tool.js +193 -10
- package/dist/tool.js.map +1 -1
- package/dist/truncate.d.ts +84 -0
- package/dist/truncate.d.ts.map +1 -0
- package/dist/truncate.js +134 -0
- package/dist/truncate.js.map +1 -0
- package/dist/vertex_gemini_integration.d.ts +27 -0
- package/dist/vertex_gemini_integration.d.ts.map +1 -0
- package/dist/vertex_gemini_integration.js +229 -0
- package/dist/vertex_gemini_integration.js.map +1 -0
- package/dist/wrap.d.ts +39 -3
- package/dist/wrap.d.ts.map +1 -1
- package/dist/wrap.js +15 -12
- package/dist/wrap.js.map +1 -1
- package/package.json +28 -4
package/dist/tool.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* tool()
|
|
2
|
+
* tool(), wraps an async function as an instrumented agent tool.
|
|
3
3
|
*
|
|
4
4
|
* Mirrors the Python `@mesedi.tool` decorator, but as a higher-order
|
|
5
5
|
* function (TS doesn't have stable decorators yet). When the
|
|
@@ -22,6 +22,16 @@ import { EventType, utcNowRfc3339 } from "./events.js";
|
|
|
22
22
|
const MAX_ARG_REPR = 200;
|
|
23
23
|
const MAX_RESULT_REPR = 500;
|
|
24
24
|
const MAX_EXC_MSG = 500;
|
|
25
|
+
/** Cap for the JSON-serialized return_value field on the wire.
|
|
26
|
+
* The SDK uses this as a generous upper bound; the backend applies
|
|
27
|
+
* its OWN per-project cap (default 8 KB) at fingerprint time so
|
|
28
|
+
* customers tune storage / fingerprint policy from the dashboard
|
|
29
|
+
* without an SDK redeploy (see #270.a, migration 042).
|
|
30
|
+
*
|
|
31
|
+
* Raised from the v0.5.0 hardcoded 2048 to 16384 (16 KB) so the
|
|
32
|
+
* SDK no longer aggressively truncates returns that the backend
|
|
33
|
+
* would happily fingerprint. */
|
|
34
|
+
const MAX_RETURN_VALUE_JSON = 16384;
|
|
25
35
|
/**
|
|
26
36
|
* Decorate an async function as an observable agent tool.
|
|
27
37
|
*/
|
|
@@ -37,7 +47,7 @@ export function tool(fnOrOpts, maybeFn) {
|
|
|
37
47
|
else {
|
|
38
48
|
opts = fnOrOpts;
|
|
39
49
|
if (!maybeFn) {
|
|
40
|
-
throw new TypeError("tool() requires a function
|
|
50
|
+
throw new TypeError("tool() requires a function, pass either tool(fn) or tool(options, fn).");
|
|
41
51
|
}
|
|
42
52
|
fn = maybeFn;
|
|
43
53
|
}
|
|
@@ -45,13 +55,13 @@ export function tool(fnOrOpts, maybeFn) {
|
|
|
45
55
|
return async function inner(...args) {
|
|
46
56
|
const ctx = currentExecutionContext();
|
|
47
57
|
if (!ctx) {
|
|
48
|
-
// No active execution
|
|
58
|
+
// No active execution, run unobserved.
|
|
49
59
|
return fn(...args);
|
|
50
60
|
}
|
|
51
61
|
// Halt-safe boundary: check the budget BEFORE doing any work.
|
|
52
62
|
// If a budget exists and is exceeded, this throws MesediHalt
|
|
53
63
|
// which propagates up to wrap()'s catch block. The user's tool
|
|
54
|
-
// code never runs
|
|
64
|
+
// code never runs, guarantees halt fires at the boundary, not
|
|
55
65
|
// mid-tool. wrap() also incrementing steps post-check matches
|
|
56
66
|
// the Python pattern (check, then count).
|
|
57
67
|
ctx.checkBudget();
|
|
@@ -66,6 +76,24 @@ export function tool(fnOrOpts, maybeFn) {
|
|
|
66
76
|
try {
|
|
67
77
|
const result = await fn(...args);
|
|
68
78
|
const durationMs = Math.round(performance.now() - start);
|
|
79
|
+
const payload = {
|
|
80
|
+
tool_name: toolName,
|
|
81
|
+
arguments: argsSummary,
|
|
82
|
+
status: "ok",
|
|
83
|
+
// Human-readable repr for dashboard display. Kept
|
|
84
|
+
// alongside structured return_value below so the UI can
|
|
85
|
+
// render whatever shape the customer's tool returns.
|
|
86
|
+
result_summary: truncate(safeRepr(result), MAX_RESULT_REPR),
|
|
87
|
+
};
|
|
88
|
+
// Structured JSON-native form for backend detectors
|
|
89
|
+
// (tool_schema_drift fingerprints the return shape). Only
|
|
90
|
+
// present when the result is JSON-serializable AND under
|
|
91
|
+
// the size cap; absent otherwise so the detector gracefully
|
|
92
|
+
// no-ops on this call instead of mis-fingerprinting.
|
|
93
|
+
const structured = structuredReturnValue(result);
|
|
94
|
+
if (structured !== undefined) {
|
|
95
|
+
payload.return_value = structured;
|
|
96
|
+
}
|
|
69
97
|
const event = {
|
|
70
98
|
event_id: eventId,
|
|
71
99
|
execution_id: ctx.executionId,
|
|
@@ -73,12 +101,7 @@ export function tool(fnOrOpts, maybeFn) {
|
|
|
73
101
|
sequence,
|
|
74
102
|
timestamp: utcNowRfc3339(),
|
|
75
103
|
duration_ms: durationMs,
|
|
76
|
-
payload
|
|
77
|
-
tool_name: toolName,
|
|
78
|
-
arguments: argsSummary,
|
|
79
|
-
status: "ok",
|
|
80
|
-
result_summary: truncate(safeRepr(result), MAX_RESULT_REPR),
|
|
81
|
-
},
|
|
104
|
+
payload,
|
|
82
105
|
};
|
|
83
106
|
client.submitEvent(event);
|
|
84
107
|
return result;
|
|
@@ -112,6 +135,166 @@ function summarizeArgs(args) {
|
|
|
112
135
|
args: args.map((a) => truncate(safeRepr(a), MAX_ARG_REPR)),
|
|
113
136
|
};
|
|
114
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Returns ``result`` as a JSON-native, schema-preserving value
|
|
140
|
+
* suitable for embedding in the ``return_value`` payload field, or
|
|
141
|
+
* ``undefined`` when the result cannot be safely serialized.
|
|
142
|
+
*
|
|
143
|
+
* The backend's tool_schema_drift detector fingerprints the
|
|
144
|
+
* structure (sorted keys + value types) from this field. The
|
|
145
|
+
* pre-#270.b implementation used a JSON.stringify replacer that
|
|
146
|
+
* coerced BigInt / Date / function / etc. to plain strings, which
|
|
147
|
+
* silently collapsed ``{created_at: Date}`` and
|
|
148
|
+
* ``{created_at: "string"}`` to an identical fingerprint and
|
|
149
|
+
* masked real schema drift (#270.b).
|
|
150
|
+
*
|
|
151
|
+
* Fix: type-tagged sentinels matching the Python sibling. A Date
|
|
152
|
+
* becomes ``{"__type__": "datetime", "value": "..."}``; BigInt
|
|
153
|
+
* becomes ``{"__type__": "bigint", ...}``; etc. The structural
|
|
154
|
+
* fingerprint now sees a distinct shape for typed-vs-plain-string
|
|
155
|
+
* fields so the detector catches drift between them.
|
|
156
|
+
*
|
|
157
|
+
* The ``__type__`` tag vocabulary is closed and matches Python's
|
|
158
|
+
* so cross-SDK consumers (e.g. customer running Python tools that
|
|
159
|
+
* call a TypeScript backend) see consistent fingerprints.
|
|
160
|
+
*/
|
|
161
|
+
function structuredReturnValue(result) {
|
|
162
|
+
let walked;
|
|
163
|
+
try {
|
|
164
|
+
walked = walkForShape(result, new WeakSet());
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
// Circular ref or walker failure — omit the field cleanly.
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
let encoded;
|
|
171
|
+
try {
|
|
172
|
+
encoded = JSON.stringify(walked);
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
if (encoded === undefined)
|
|
178
|
+
return undefined;
|
|
179
|
+
if (encoded.length > MAX_RETURN_VALUE_JSON)
|
|
180
|
+
return "<truncated>";
|
|
181
|
+
return walked;
|
|
182
|
+
}
|
|
183
|
+
/** Recursively coerce ``value`` to a JSON-native form that
|
|
184
|
+
* preserves type information via typed sentinels. ``visited``
|
|
185
|
+
* tracks objects on the current path so circular refs throw rather
|
|
186
|
+
* than infinite-loop. */
|
|
187
|
+
function walkForShape(value, visited) {
|
|
188
|
+
// Fast path: JSON-native primitives.
|
|
189
|
+
if (value === null)
|
|
190
|
+
return null;
|
|
191
|
+
const t = typeof value;
|
|
192
|
+
if (t === "string" || t === "boolean")
|
|
193
|
+
return value;
|
|
194
|
+
if (t === "number") {
|
|
195
|
+
const n = value;
|
|
196
|
+
if (!Number.isFinite(n)) {
|
|
197
|
+
return {
|
|
198
|
+
__type__: "object",
|
|
199
|
+
class: "number",
|
|
200
|
+
value: String(n),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
return n;
|
|
204
|
+
}
|
|
205
|
+
// undefined inside a container needs a typed sentinel so the
|
|
206
|
+
// shape preserves "this field is intentionally undefined" vs
|
|
207
|
+
// "this field is a string." Top-level undefined is handled by
|
|
208
|
+
// the caller (returns undefined-the-payload-absence).
|
|
209
|
+
if (t === "undefined") {
|
|
210
|
+
return { __type__: "undefined" };
|
|
211
|
+
}
|
|
212
|
+
if (t === "bigint") {
|
|
213
|
+
return { __type__: "bigint", value: value.toString() };
|
|
214
|
+
}
|
|
215
|
+
if (t === "function") {
|
|
216
|
+
return {
|
|
217
|
+
__type__: "function",
|
|
218
|
+
name: value.name || "anonymous",
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
if (t === "symbol") {
|
|
222
|
+
return { __type__: "symbol", value: value.toString() };
|
|
223
|
+
}
|
|
224
|
+
// Container types: recurse, guarding against cycles.
|
|
225
|
+
if (Array.isArray(value)) {
|
|
226
|
+
if (visited.has(value)) {
|
|
227
|
+
throw new Error("circular array reference");
|
|
228
|
+
}
|
|
229
|
+
visited.add(value);
|
|
230
|
+
try {
|
|
231
|
+
return value.map((v) => walkForShape(v, visited));
|
|
232
|
+
}
|
|
233
|
+
finally {
|
|
234
|
+
visited.delete(value);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (value instanceof Date) {
|
|
238
|
+
return { __type__: "datetime", value: value.toISOString() };
|
|
239
|
+
}
|
|
240
|
+
if (value instanceof Map) {
|
|
241
|
+
return {
|
|
242
|
+
__type__: "map",
|
|
243
|
+
value: Array.from(value.entries()).map(([k, v]) => [
|
|
244
|
+
walkForShape(k, visited),
|
|
245
|
+
walkForShape(v, visited),
|
|
246
|
+
]),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
if (value instanceof Set) {
|
|
250
|
+
// Sets aren't JSON-native; tag and sort so order-insensitive
|
|
251
|
+
// nature is reflected in the shape.
|
|
252
|
+
const members = Array.from(value).map((v) => walkForShape(v, visited));
|
|
253
|
+
members.sort((a, b) => {
|
|
254
|
+
const sa = JSON.stringify(a) ?? "";
|
|
255
|
+
const sb = JSON.stringify(b) ?? "";
|
|
256
|
+
return sa.localeCompare(sb);
|
|
257
|
+
});
|
|
258
|
+
return { __type__: "set", value: members };
|
|
259
|
+
}
|
|
260
|
+
if (value instanceof RegExp) {
|
|
261
|
+
return { __type__: "regexp", value: value.toString() };
|
|
262
|
+
}
|
|
263
|
+
if (value instanceof Error) {
|
|
264
|
+
return {
|
|
265
|
+
__type__: "object",
|
|
266
|
+
class: value.constructor?.name || "Error",
|
|
267
|
+
value: value.message,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
if (typeof value === "object") {
|
|
271
|
+
if (visited.has(value)) {
|
|
272
|
+
throw new Error("circular object reference");
|
|
273
|
+
}
|
|
274
|
+
visited.add(value);
|
|
275
|
+
try {
|
|
276
|
+
const obj = value;
|
|
277
|
+
const out = {};
|
|
278
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
279
|
+
out[k] = walkForShape(v, visited);
|
|
280
|
+
}
|
|
281
|
+
// Tag the class name when it's not a plain Object — preserves
|
|
282
|
+
// "returns User vs returns AdminUser" drift even when both
|
|
283
|
+
// have the same fields.
|
|
284
|
+
const className = value.constructor?.name;
|
|
285
|
+
if (className && className !== "Object") {
|
|
286
|
+
out.__type__ = "object";
|
|
287
|
+
out.class = className;
|
|
288
|
+
}
|
|
289
|
+
return out;
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
visited.delete(value);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
// Should be unreachable; defensive fallback.
|
|
296
|
+
return { __type__: "object", class: typeof value, value: String(value) };
|
|
297
|
+
}
|
|
115
298
|
/**
|
|
116
299
|
* JSON.stringify-with-fallback. Falls back to `String(x)` for values
|
|
117
300
|
* that JSON can't serialize (circular refs, BigInt, functions).
|
package/dist/tool.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool.js","sourceRoot":"","sources":["../src/tool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAA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AK,CAAC,EAAE,CAAC;AAC3E,CAAC;AAED;;;GAGG;AACH,SAAS,QAAQ,CAAC,KAAc;IAC9B,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,WAAW,CAAC;IAC5C,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,MAAM,CAAC;IAClC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC5D,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;AACH,CAAC;AAED,SAAS,QAAQ,CAAC,CAAS,EAAE,GAAW;IACtC,IAAI,CAAC,CAAC,MAAM,IAAI,GAAG;QAAE,OAAO,CAAC,CAAC;IAC9B,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Soft payload-size cap with smart per-field truncation (#243).
|
|
3
|
+
*
|
|
4
|
+
* The client calls {@link maybeTruncate} on every event's payload
|
|
5
|
+
* before the event enters the shipper queue. Payloads under the
|
|
6
|
+
* configured cap pass through unchanged. Payloads over the cap have
|
|
7
|
+
* their longest top-level string field iteratively shortened until
|
|
8
|
+
* the serialized payload fits, preserving structure so downstream
|
|
9
|
+
* readers can still see which fields existed.
|
|
10
|
+
*
|
|
11
|
+
* **Why a soft cap, not a hard reject.** An observability product's
|
|
12
|
+
* worst failure mode is silently losing events. A hard cap (drop the
|
|
13
|
+
* event) means the customer never sees that something went wrong; a
|
|
14
|
+
* soft cap means they see "something happened, here is a useful
|
|
15
|
+
* fingerprint, the payload was bigger than we kept." Truncation is
|
|
16
|
+
* the deliberately less-lossy path.
|
|
17
|
+
*
|
|
18
|
+
* **Why top-level fields only.** Walking arbitrarily nested
|
|
19
|
+
* structures adds complexity for diminishing returns: in practice
|
|
20
|
+
* almost all oversized event payloads stuff one or two big strings at
|
|
21
|
+
* the top level (`response`, `prompt`, `diff`, `stack_trace`). The
|
|
22
|
+
* SDK README documents that deeply nested strings under heavily
|
|
23
|
+
* structured payloads are not automatically truncated.
|
|
24
|
+
*
|
|
25
|
+
* **Markers added on truncation:**
|
|
26
|
+
*
|
|
27
|
+
* - `_truncated` (boolean) — always `true` when this
|
|
28
|
+
* helper altered the payload.
|
|
29
|
+
* - `_original_payload_bytes` (number) — serialized byte count of
|
|
30
|
+
* the original payload, so the
|
|
31
|
+
* customer can see how much got
|
|
32
|
+
* dropped.
|
|
33
|
+
* - `_truncated_fields` (string[]) — names of the top-level
|
|
34
|
+
* fields whose values got shortened.
|
|
35
|
+
*
|
|
36
|
+
* **Fail-open.** Any exception during truncation falls back to the
|
|
37
|
+
* original payload. Compression-style fail-open posture matches the
|
|
38
|
+
* rest of the SDK.
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* Default per-event payload cap. Covers ~99% of real-world event
|
|
42
|
+
* shapes. Customers can override via `configure({ maxPayloadBytes:
|
|
43
|
+
* ... })`.
|
|
44
|
+
*/
|
|
45
|
+
export declare const DEFAULT_MAX_PAYLOAD_BYTES: number;
|
|
46
|
+
/**
|
|
47
|
+
* Minimum characters preserved for any single string before
|
|
48
|
+
* truncation gives up on shrinking it further. Below this floor the
|
|
49
|
+
* surviving prefix is too small to be useful for debugging.
|
|
50
|
+
*/
|
|
51
|
+
export declare const MIN_KEEP_CHARS = 100;
|
|
52
|
+
/**
|
|
53
|
+
* Safety cap on the iterative shrink loop. 50 iterations of halving
|
|
54
|
+
* would take a 1 GB string below MIN_KEEP_CHARS, so anything
|
|
55
|
+
* legitimate terminates well under this.
|
|
56
|
+
*/
|
|
57
|
+
export declare const MAX_ITERATIONS = 50;
|
|
58
|
+
/**
|
|
59
|
+
* Suffix appended to truncated string values so a human reader can
|
|
60
|
+
* tell at a glance that the field is not its full content.
|
|
61
|
+
*/
|
|
62
|
+
export declare const TRUNCATION_SUFFIX = "...[mesedi:truncated]";
|
|
63
|
+
/** Field name stamped onto a truncated payload (boolean flag). */
|
|
64
|
+
export declare const MARKER_TRUNCATED = "_truncated";
|
|
65
|
+
/** Field name stamped onto a truncated payload (original byte count). */
|
|
66
|
+
export declare const MARKER_ORIGINAL_BYTES = "_original_payload_bytes";
|
|
67
|
+
/** Field name stamped onto a truncated payload (which fields shrank). */
|
|
68
|
+
export declare const MARKER_TRUNCATED_FIELDS = "_truncated_fields";
|
|
69
|
+
/**
|
|
70
|
+
* Truncate top-level string fields if the serialized payload exceeds
|
|
71
|
+
* `maxBytes`.
|
|
72
|
+
*
|
|
73
|
+
* Never throws. On any exception, the original payload is returned
|
|
74
|
+
* so an event always ships even if the truncation logic itself
|
|
75
|
+
* breaks.
|
|
76
|
+
*
|
|
77
|
+
* @param payload the event payload object
|
|
78
|
+
* @param maxBytes cap on the serialized JSON byte count
|
|
79
|
+
* @returns the original payload if under cap, otherwise a new object
|
|
80
|
+
* with the longest string fields shortened and marker fields
|
|
81
|
+
* stamped at the top level
|
|
82
|
+
*/
|
|
83
|
+
export declare function maybeTruncate(payload: Record<string, unknown>, maxBytes?: number): Record<string, unknown>;
|
|
84
|
+
//# sourceMappingURL=truncate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"truncate.d.ts","sourceRoot":"","sources":["../src/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,QAAY,CAAC;AAEnD;;;;GAIG;AACH,eAAO,MAAM,cAAc,MAAM,CAAC;AAElC;;;;GAIG;AACH,eAAO,MAAM,cAAc,KAAK,CAAC;AAEjC;;;GAGG;AACH,eAAO,MAAM,iBAAiB,0BAA0B,CAAC;AAEzD,kEAAkE;AAClE,eAAO,MAAM,gBAAgB,eAAe,CAAC;AAC7C,yEAAyE;AACzE,eAAO,MAAM,qBAAqB,4BAA4B,CAAC;AAC/D,yEAAyE;AACzE,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAQ3D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,QAAQ,GAAE,MAAkC,GAC3C,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAgDzB"}
|
package/dist/truncate.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Soft payload-size cap with smart per-field truncation (#243).
|
|
3
|
+
*
|
|
4
|
+
* The client calls {@link maybeTruncate} on every event's payload
|
|
5
|
+
* before the event enters the shipper queue. Payloads under the
|
|
6
|
+
* configured cap pass through unchanged. Payloads over the cap have
|
|
7
|
+
* their longest top-level string field iteratively shortened until
|
|
8
|
+
* the serialized payload fits, preserving structure so downstream
|
|
9
|
+
* readers can still see which fields existed.
|
|
10
|
+
*
|
|
11
|
+
* **Why a soft cap, not a hard reject.** An observability product's
|
|
12
|
+
* worst failure mode is silently losing events. A hard cap (drop the
|
|
13
|
+
* event) means the customer never sees that something went wrong; a
|
|
14
|
+
* soft cap means they see "something happened, here is a useful
|
|
15
|
+
* fingerprint, the payload was bigger than we kept." Truncation is
|
|
16
|
+
* the deliberately less-lossy path.
|
|
17
|
+
*
|
|
18
|
+
* **Why top-level fields only.** Walking arbitrarily nested
|
|
19
|
+
* structures adds complexity for diminishing returns: in practice
|
|
20
|
+
* almost all oversized event payloads stuff one or two big strings at
|
|
21
|
+
* the top level (`response`, `prompt`, `diff`, `stack_trace`). The
|
|
22
|
+
* SDK README documents that deeply nested strings under heavily
|
|
23
|
+
* structured payloads are not automatically truncated.
|
|
24
|
+
*
|
|
25
|
+
* **Markers added on truncation:**
|
|
26
|
+
*
|
|
27
|
+
* - `_truncated` (boolean) — always `true` when this
|
|
28
|
+
* helper altered the payload.
|
|
29
|
+
* - `_original_payload_bytes` (number) — serialized byte count of
|
|
30
|
+
* the original payload, so the
|
|
31
|
+
* customer can see how much got
|
|
32
|
+
* dropped.
|
|
33
|
+
* - `_truncated_fields` (string[]) — names of the top-level
|
|
34
|
+
* fields whose values got shortened.
|
|
35
|
+
*
|
|
36
|
+
* **Fail-open.** Any exception during truncation falls back to the
|
|
37
|
+
* original payload. Compression-style fail-open posture matches the
|
|
38
|
+
* rest of the SDK.
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* Default per-event payload cap. Covers ~99% of real-world event
|
|
42
|
+
* shapes. Customers can override via `configure({ maxPayloadBytes:
|
|
43
|
+
* ... })`.
|
|
44
|
+
*/
|
|
45
|
+
export const DEFAULT_MAX_PAYLOAD_BYTES = 32 * 1024;
|
|
46
|
+
/**
|
|
47
|
+
* Minimum characters preserved for any single string before
|
|
48
|
+
* truncation gives up on shrinking it further. Below this floor the
|
|
49
|
+
* surviving prefix is too small to be useful for debugging.
|
|
50
|
+
*/
|
|
51
|
+
export const MIN_KEEP_CHARS = 100;
|
|
52
|
+
/**
|
|
53
|
+
* Safety cap on the iterative shrink loop. 50 iterations of halving
|
|
54
|
+
* would take a 1 GB string below MIN_KEEP_CHARS, so anything
|
|
55
|
+
* legitimate terminates well under this.
|
|
56
|
+
*/
|
|
57
|
+
export const MAX_ITERATIONS = 50;
|
|
58
|
+
/**
|
|
59
|
+
* Suffix appended to truncated string values so a human reader can
|
|
60
|
+
* tell at a glance that the field is not its full content.
|
|
61
|
+
*/
|
|
62
|
+
export const TRUNCATION_SUFFIX = "...[mesedi:truncated]";
|
|
63
|
+
/** Field name stamped onto a truncated payload (boolean flag). */
|
|
64
|
+
export const MARKER_TRUNCATED = "_truncated";
|
|
65
|
+
/** Field name stamped onto a truncated payload (original byte count). */
|
|
66
|
+
export const MARKER_ORIGINAL_BYTES = "_original_payload_bytes";
|
|
67
|
+
/** Field name stamped onto a truncated payload (which fields shrank). */
|
|
68
|
+
export const MARKER_TRUNCATED_FIELDS = "_truncated_fields";
|
|
69
|
+
const ENCODER = new TextEncoder();
|
|
70
|
+
function serializeBytes(value) {
|
|
71
|
+
return ENCODER.encode(JSON.stringify(value)).byteLength;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Truncate top-level string fields if the serialized payload exceeds
|
|
75
|
+
* `maxBytes`.
|
|
76
|
+
*
|
|
77
|
+
* Never throws. On any exception, the original payload is returned
|
|
78
|
+
* so an event always ships even if the truncation logic itself
|
|
79
|
+
* breaks.
|
|
80
|
+
*
|
|
81
|
+
* @param payload the event payload object
|
|
82
|
+
* @param maxBytes cap on the serialized JSON byte count
|
|
83
|
+
* @returns the original payload if under cap, otherwise a new object
|
|
84
|
+
* with the longest string fields shortened and marker fields
|
|
85
|
+
* stamped at the top level
|
|
86
|
+
*/
|
|
87
|
+
export function maybeTruncate(payload, maxBytes = DEFAULT_MAX_PAYLOAD_BYTES) {
|
|
88
|
+
try {
|
|
89
|
+
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
|
|
90
|
+
return payload;
|
|
91
|
+
}
|
|
92
|
+
const originalBytes = serializeBytes(payload);
|
|
93
|
+
if (originalBytes <= maxBytes) {
|
|
94
|
+
return payload;
|
|
95
|
+
}
|
|
96
|
+
const result = { ...payload };
|
|
97
|
+
const truncatedFields = [];
|
|
98
|
+
for (let i = 0; i < MAX_ITERATIONS; i++) {
|
|
99
|
+
if (serializeBytes(result) <= maxBytes)
|
|
100
|
+
break;
|
|
101
|
+
let longestKey = null;
|
|
102
|
+
let longestLen = MIN_KEEP_CHARS;
|
|
103
|
+
for (const [k, v] of Object.entries(result)) {
|
|
104
|
+
if (k.startsWith("_"))
|
|
105
|
+
continue;
|
|
106
|
+
if (typeof v !== "string")
|
|
107
|
+
continue;
|
|
108
|
+
if (v.length > longestLen) {
|
|
109
|
+
longestLen = v.length;
|
|
110
|
+
longestKey = k;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (longestKey === null)
|
|
114
|
+
break;
|
|
115
|
+
const currentVal = result[longestKey];
|
|
116
|
+
const newLen = Math.max(MIN_KEEP_CHARS, Math.floor(currentVal.length / 2));
|
|
117
|
+
result[longestKey] = currentVal.slice(0, newLen) + TRUNCATION_SUFFIX;
|
|
118
|
+
if (!truncatedFields.includes(longestKey)) {
|
|
119
|
+
truncatedFields.push(longestKey);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
result[MARKER_TRUNCATED] = true;
|
|
123
|
+
result[MARKER_ORIGINAL_BYTES] = originalBytes;
|
|
124
|
+
result[MARKER_TRUNCATED_FIELDS] = truncatedFields;
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
catch (err) {
|
|
128
|
+
// Compression-style fail-open. Compression and truncation both
|
|
129
|
+
// serve performance; neither is allowed to drop an event.
|
|
130
|
+
console.warn(`mesedi: payload truncation failed, shipping original: ${String(err)}`);
|
|
131
|
+
return payload;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=truncate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"truncate.js","sourceRoot":"","sources":["../src/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,EAAE,GAAG,IAAI,CAAC;AAEnD;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,CAAC;AAElC;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,EAAE,CAAC;AAEjC;;;GAGG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,uBAAuB,CAAC;AAEzD,kEAAkE;AAClE,MAAM,CAAC,MAAM,gBAAgB,GAAG,YAAY,CAAC;AAC7C,yEAAyE;AACzE,MAAM,CAAC,MAAM,qB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C,MAAM,GAAG,UAAU,EAAE,CAAC;oBAC1B,UAAU,GAAG,CAAC,CAAC,MAAM,CAAC;oBACtB,UAAU,GAAG,CAAC,CAAC;gBACjB,CAAC;YACH,CAAC;YACD,IAAI,UAAU,KAAK,IAAI;gBAAE,MAAM;YAE/B,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,CAAW,CAAC;YAChD,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,cAAc,EAAE,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;YAC3E,MAAM,CAAC,UAAU,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,GAAG,iBAAiB,CAAC;YACrE,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;gBAC1C,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YACnC,CAAC;QACH,CAAC;QAED,MAAM,CAAC,gBAAgB,CAAC,GAAG,IAAI,CAAC;QAChC,MAAM,CAAC,qBAAqB,CAAC,GAAG,aAAa,CAAC;QAC9C,MAAM,CAAC,uBAAuB,CAAC,GAAG,eAAe,CAAC;QAClD,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,+DAA+D;QAC/D,0DAA0D;QAC1D,OAAO,CAAC,IAAI,CACV,yDAAyD,MAAM,CAAC,GAAG,CAAC,EAAE,CACvE,CAAC;QACF,OAAO,OAAO,CAAC;IACjB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vertex AI Gemini TypeScript SDK monkey-patch — auto-emit llm_call
|
|
3
|
+
* events for every GenerativeModel.generateContent() call inside a
|
|
4
|
+
* wrap()'d execution against the Vertex AI surface
|
|
5
|
+
* (@google-cloud/vertexai package).
|
|
6
|
+
*
|
|
7
|
+
* Same provider tag ("gemini") and same canonical error_class map
|
|
8
|
+
* as instrumentGemini. Customers running both surfaces call both
|
|
9
|
+
* instrumentGemini() and instrumentVertexGemini() — each patches a
|
|
10
|
+
* different package's class. Provider_incident detection sees one
|
|
11
|
+
* signal regardless of which surface was hit.
|
|
12
|
+
*
|
|
13
|
+
* Dependency injection: instrumentVertexGemini accepts an optional
|
|
14
|
+
* class argument so this code is testable without installing
|
|
15
|
+
* @google-cloud/vertexai.
|
|
16
|
+
*/
|
|
17
|
+
export interface VertexGenerativeModelClassLike {
|
|
18
|
+
prototype: {
|
|
19
|
+
generateContent: (...args: any[]) => Promise<any>;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Patch Vertex AI's GenerativeModel.generateContent to emit
|
|
23
|
+
* llm_call events. Returns true if patched OR was a no-op re-patch.
|
|
24
|
+
* Returns false if @google-cloud/vertexai isn't installed AND no
|
|
25
|
+
* class was provided. */
|
|
26
|
+
export declare function instrumentVertexGemini(modelClass?: VertexGenerativeModelClassLike): Promise<boolean>;
|
|
27
|
+
//# sourceMappingURL=vertex_gemini_integration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vertex_gemini_integration.d.ts","sourceRoot":"","sources":["../src/vertex_gemini_integration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAuBH,MAAM,WAAW,8BAA8B;IAE7C,SAAS,EAAE;QAAE,eAAe,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;KAAE,CAAC;CAClE;AAgBD;;;yBAGyB;AACzB,wBAAsB,sBAAsB,CAC1C,UAAU,CAAC,EAAE,8BAA8B,GAC1C,OAAO,CAAC,OAAO,CAAC,CA6HlB"}
|