langsmith 0.5.21 → 0.5.23

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.
Files changed (42) hide show
  1. package/dist/client.cjs +327 -10
  2. package/dist/client.d.ts +90 -1
  3. package/dist/client.js +330 -13
  4. package/dist/evaluation/_runner.cjs +1 -4
  5. package/dist/evaluation/_runner.js +1 -4
  6. package/dist/experimental/sandbox/client.cjs +102 -427
  7. package/dist/experimental/sandbox/client.d.ts +68 -159
  8. package/dist/experimental/sandbox/client.js +104 -429
  9. package/dist/experimental/sandbox/errors.cjs +1 -2
  10. package/dist/experimental/sandbox/errors.d.ts +1 -2
  11. package/dist/experimental/sandbox/errors.js +1 -2
  12. package/dist/experimental/sandbox/helpers.cjs +8 -98
  13. package/dist/experimental/sandbox/helpers.d.ts +0 -29
  14. package/dist/experimental/sandbox/helpers.js +9 -95
  15. package/dist/experimental/sandbox/index.cjs +6 -1
  16. package/dist/experimental/sandbox/index.d.ts +7 -2
  17. package/dist/experimental/sandbox/index.js +6 -1
  18. package/dist/experimental/sandbox/sandbox.cjs +3 -11
  19. package/dist/experimental/sandbox/sandbox.d.ts +3 -5
  20. package/dist/experimental/sandbox/sandbox.js +3 -11
  21. package/dist/experimental/sandbox/types.d.ts +32 -149
  22. package/dist/index.cjs +1 -1
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.js +1 -1
  25. package/dist/schemas.d.ts +54 -0
  26. package/dist/utils/error.cjs +7 -0
  27. package/dist/utils/error.d.ts +1 -0
  28. package/dist/utils/error.js +6 -0
  29. package/dist/utils/fast-safe-stringify/index.cjs +228 -0
  30. package/dist/utils/fast-safe-stringify/index.d.ts +33 -0
  31. package/dist/utils/fast-safe-stringify/index.js +227 -0
  32. package/dist/utils/prompts.cjs +7 -2
  33. package/dist/utils/prompts.d.ts +6 -1
  34. package/dist/utils/prompts.js +6 -1
  35. package/dist/wrappers/openai_agents.cjs +849 -0
  36. package/dist/wrappers/openai_agents.d.ts +92 -0
  37. package/dist/wrappers/openai_agents.js +845 -0
  38. package/package.json +22 -6
  39. package/wrappers/openai_agents.cjs +1 -0
  40. package/wrappers/openai_agents.d.cts +1 -0
  41. package/wrappers/openai_agents.d.ts +1 -0
  42. package/wrappers/openai_agents.js +1 -0
@@ -57,6 +57,233 @@ function createDefaultReplacer(userReplacer) {
57
57
  return serializeWellKnownTypes(val);
58
58
  };
59
59
  }
60
+ /**
61
+ * Estimate the serialized JSON byte size of a value without actually
62
+ * serializing it. Used on hot paths (enqueuing runs for batched tracing)
63
+ * where the exact serialized size is not required -- only a reasonable
64
+ * approximation for soft memory accounting.
65
+ *
66
+ * Walks the object graph in O(n) without allocating a JSON string,
67
+ * avoiding the event-loop blocking that JSON.stringify causes on large
68
+ * payloads.
69
+ *
70
+ * Accuracy notes (all estimates are approximate, never exact):
71
+ * - Strings: UTF-8 byte length via Buffer.byteLength when available,
72
+ * falling back to code-unit length for non-Node environments. Does
73
+ * not account for escape expansion (\", \\, control chars, surrogate
74
+ * escapes) which is usually a small fraction of total size.
75
+ * - Binary data (Buffer / typed arrays / ArrayBuffer / DataView): sized
76
+ * from their JSON.stringify representations where practical
77
+ * ({ type: "Buffer", data: [...] } for Buffer, keyed objects for typed
78
+ * arrays). DataView and ArrayBuffer themselves have no enumerable own
79
+ * properties and serialize as "{}". Each byte contributes ~3.5
80
+ * characters on average in Buffer's decimal-array representation
81
+ * (digit(s) + comma).
82
+ * - Other objects with toJSON(): we invoke toJSON() once and estimate
83
+ * the result. This matches JSON.stringify semantics for libraries
84
+ * like Decimal.js, Moment, Luxon, Mongoose documents, etc.
85
+ * - Cycles: detected via an ancestor-path set that is pushed/popped
86
+ * during recursion. This matches JSON.stringify semantics --
87
+ * repeated references that are *not* on the current ancestor chain
88
+ * (shared subobjects) are counted every time they appear, because
89
+ * JSON.stringify will serialize them every time.
90
+ * - No depth limit (JSON.stringify has none either).
91
+ */
92
+ export function estimateSerializedSize(value) {
93
+ try {
94
+ // Ancestor set for cycle detection. An object is only treated as
95
+ // circular if it appears on the current recursion path, not merely
96
+ // if it has been seen before elsewhere in the graph.
97
+ const ancestors = new Set();
98
+ // In Node / Bun, Buffer.byteLength is a fast native way to get UTF-8
99
+ // byte length without allocating an encoded copy. In other runtimes
100
+ // we fall back to code-unit length (a small under-estimate for
101
+ // non-ASCII text, which is acceptable for soft limits).
102
+ const byteLen = typeof Buffer !== "undefined" && typeof Buffer.byteLength === "function"
103
+ ? (s) => Buffer.byteLength(s, "utf8")
104
+ : (s) => s.length;
105
+ function estimateString(s) {
106
+ // +2 for the surrounding quotes. Escape expansion is not counted.
107
+ return byteLen(s) + 2;
108
+ }
109
+ // Size of a byte sequence when rendered as a JSON array of decimal
110
+ // numbers: "[b0,b1,b2,...]". Each byte averages ~3.5 chars (value 0-9
111
+ // => 1 char, 10-99 => 2 chars, 100-255 => 3 chars; weighted mean over
112
+ // a uniform distribution is ~2.81, plus one comma per element except
113
+ // the last). Round up to 4 bytes/element for a small safety margin.
114
+ function estimateByteArrayJson(byteLength) {
115
+ if (byteLength === 0)
116
+ return 2; // "[]"
117
+ return 2 + byteLength * 4;
118
+ }
119
+ // Returns true for values that JSON.stringify drops when they appear
120
+ // as an object property (as opposed to an array element, where they
121
+ // become "null").
122
+ function isDropped(v) {
123
+ return (v === undefined || typeof v === "function" || typeof v === "symbol");
124
+ }
125
+ // In arrays, undefined / function / symbol become "null" (4 bytes).
126
+ function estimateInArray(v) {
127
+ if (v === undefined || typeof v === "function" || typeof v === "symbol") {
128
+ return 4;
129
+ }
130
+ return estimate(v);
131
+ }
132
+ function estimate(val) {
133
+ if (val === null)
134
+ return 4; // "null"
135
+ if (val === undefined)
136
+ return 0; // top-level or property context; array handled separately
137
+ const t = typeof val;
138
+ if (t === "boolean")
139
+ return 5; // "true" / "false" upper bound
140
+ if (t === "number") {
141
+ if (!Number.isFinite(val))
142
+ return 4; // "null"
143
+ // Convert to string to get exact length. This is cheap for numbers
144
+ // (V8 caches small-number strings) and makes the estimate far
145
+ // tighter for common cases like integer arrays.
146
+ return val.toString().length;
147
+ }
148
+ if (t === "bigint") {
149
+ // Our replacer renders BigInt via .toString(), then JSON.stringify
150
+ // quotes it.
151
+ return val.toString().length + 2;
152
+ }
153
+ if (t === "string")
154
+ return estimateString(val);
155
+ if (t === "function" || t === "symbol")
156
+ return 0;
157
+ // Objects from here on.
158
+ const obj = val;
159
+ // Well-known types handled by our replacer.
160
+ if (obj instanceof Date)
161
+ return 26; // "2024-01-01T00:00:00.000Z"
162
+ if (obj instanceof RegExp)
163
+ return byteLen(obj.toString()) + 2;
164
+ if (obj instanceof Error) {
165
+ const name = obj.name ?? "";
166
+ const message = obj.message ?? "";
167
+ // {"name":"...","message":"..."}
168
+ return 22 + byteLen(name) + byteLen(message);
169
+ }
170
+ // Binary data types. These commonly appear in LLM payloads (images,
171
+ // audio) and their JSON representations vary widely.
172
+ if (typeof Buffer !== "undefined" && obj instanceof Buffer) {
173
+ // { "type": "Buffer", "data": [0, 1, 2, ...] }
174
+ return 28 + estimateByteArrayJson(obj.byteLength);
175
+ }
176
+ if (ArrayBuffer.isView(obj)) {
177
+ if (obj instanceof DataView) {
178
+ // DataView has no enumerable own properties; serializes as "{}".
179
+ return 2;
180
+ }
181
+ // Typed arrays serialize as {"0":v0,"1":v1,...} (keyed objects),
182
+ // which is much larger than a plain array would be. Per element
183
+ // cost: digits for index + digits for value + ":" + "," + quotes.
184
+ const len = obj.length ?? 0;
185
+ const isFloat = obj instanceof Float32Array || obj instanceof Float64Array;
186
+ // Index digits grow with len; worst-case per element:
187
+ // "NNN":V, where NNN = log10(len) digits and V depends on type.
188
+ // Loose but safe bounds: integer views ~12 chars/element, float
189
+ // views ~30 chars/element (Float32 ToString can be up to ~17 chars).
190
+ const perElement = isFloat ? 30 : 12;
191
+ return 2 + len * perElement;
192
+ }
193
+ if (obj instanceof ArrayBuffer) {
194
+ // Plain ArrayBuffer has no enumerable properties; serializes as "{}".
195
+ return 2;
196
+ }
197
+ if (ancestors.has(obj)) {
198
+ // Cycle: our decirc fallback replaces with { result: "[Circular]" }.
199
+ return 24;
200
+ }
201
+ // Custom toJSON (Decimal.js, Moment, Luxon, Mongoose docs, etc.).
202
+ // This runs after explicit built-in / binary cases above so known
203
+ // types (for example Buffer) use their dedicated fast-path sizing
204
+ // logic instead of duck-typing through toJSON().
205
+ if (typeof obj.toJSON === "function") {
206
+ let projected;
207
+ try {
208
+ projected = obj.toJSON("");
209
+ }
210
+ catch {
211
+ // If toJSON throws, JSON.stringify would also throw and our
212
+ // serializer would emit "[Unserializable]" (~16 bytes).
213
+ return 16;
214
+ }
215
+ ancestors.add(obj);
216
+ const size = estimate(projected);
217
+ ancestors.delete(obj);
218
+ return size;
219
+ }
220
+ ancestors.add(obj);
221
+ let size;
222
+ if (Array.isArray(obj)) {
223
+ size = 2; // []
224
+ const len = obj.length;
225
+ for (let i = 0; i < len; i++) {
226
+ size += estimateInArray(obj[i]);
227
+ if (i < len - 1)
228
+ size += 1; // comma
229
+ }
230
+ }
231
+ else if (obj instanceof Map) {
232
+ // Rendered as { k: v, ... } via Object.fromEntries.
233
+ size = 2;
234
+ let emitted = 0;
235
+ for (const [k, v] of obj) {
236
+ if (isDropped(v))
237
+ continue;
238
+ if (emitted > 0)
239
+ size += 1; // comma
240
+ const keyStr = typeof k === "string" ? k : String(k);
241
+ size += byteLen(keyStr) + 3; // "key":
242
+ size += estimate(v);
243
+ emitted++;
244
+ }
245
+ }
246
+ else if (obj instanceof Set) {
247
+ // Rendered as [v, ...] via Array.from.
248
+ size = 2;
249
+ let emitted = 0;
250
+ for (const v of obj) {
251
+ if (emitted > 0)
252
+ size += 1; // comma
253
+ size += estimateInArray(v);
254
+ emitted++;
255
+ }
256
+ }
257
+ else {
258
+ size = 2; // {}
259
+ let emitted = 0;
260
+ // Object.keys only returns own enumerable string keys, matching
261
+ // JSON.stringify.
262
+ const keys = Object.keys(obj);
263
+ for (let i = 0; i < keys.length; i++) {
264
+ const key = keys[i];
265
+ const v = obj[key];
266
+ if (isDropped(v))
267
+ continue;
268
+ if (emitted > 0)
269
+ size += 1; // comma
270
+ size += byteLen(key) + 3; // "key":
271
+ size += estimate(v);
272
+ emitted++;
273
+ }
274
+ }
275
+ ancestors.delete(obj);
276
+ return size;
277
+ }
278
+ return estimate(value);
279
+ }
280
+ catch {
281
+ // If the estimator itself hits an unexpected edge case, fall back to the
282
+ // exact serialized size. This preserves correctness of queue-size
283
+ // accounting at the cost of a slower hot path for that one payload.
284
+ return serialize(value).length;
285
+ }
286
+ }
60
287
  // Regular stringify
61
288
  export function serialize(obj, errorContext, replacer, spacer, options) {
62
289
  try {
@@ -1,8 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parsePromptIdentifier = parsePromptIdentifier;
3
+ exports.parseHubIdentifier = parseHubIdentifier;
4
4
  const error_js_1 = require("./error.cjs");
5
- function parsePromptIdentifier(identifier) {
5
+ /**
6
+ * Parse a hub repo identifier (owner/name:hash, name, etc.).
7
+ *
8
+ * Prompts, agents, and skills share the same identifier grammar on Hub.
9
+ */
10
+ function parseHubIdentifier(identifier) {
6
11
  if (!identifier ||
7
12
  identifier.split("/").length > 2 ||
8
13
  identifier.startsWith("/") ||
@@ -1 +1,6 @@
1
- export declare function parsePromptIdentifier(identifier: string): [string, string, string];
1
+ /**
2
+ * Parse a hub repo identifier (owner/name:hash, name, etc.).
3
+ *
4
+ * Prompts, agents, and skills share the same identifier grammar on Hub.
5
+ */
6
+ export declare function parseHubIdentifier(identifier: string): [string, string, string];
@@ -1,5 +1,10 @@
1
1
  import { getInvalidPromptIdentifierMsg } from "./error.js";
2
- export function parsePromptIdentifier(identifier) {
2
+ /**
3
+ * Parse a hub repo identifier (owner/name:hash, name, etc.).
4
+ *
5
+ * Prompts, agents, and skills share the same identifier grammar on Hub.
6
+ */
7
+ export function parseHubIdentifier(identifier) {
3
8
  if (!identifier ||
4
9
  identifier.split("/").length > 2 ||
5
10
  identifier.startsWith("/") ||