langwatch 2.1.0 → 2.3.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.
Files changed (46) hide show
  1. package/dist/agent/index.js +1 -1
  2. package/dist/agent/index.js.map +1 -1
  3. package/dist/agent/index.mjs +1 -1
  4. package/dist/agent/index.mjs.map +1 -1
  5. package/dist/{types-RkiBQZ9j.d.mts → braid-baggage-span-processor-CBNdRYKr.d.mts} +28 -1
  6. package/dist/{types-RkiBQZ9j.d.ts → braid-baggage-span-processor-CBNdRYKr.d.ts} +28 -1
  7. package/dist/{chunk-UHM5RXEW.mjs → chunk-5TNHAT3S.mjs} +101 -4
  8. package/dist/chunk-5TNHAT3S.mjs.map +1 -0
  9. package/dist/{chunk-VJK42Y3W.js → chunk-FQJHCT5Y.js} +111 -14
  10. package/dist/chunk-FQJHCT5Y.js.map +1 -0
  11. package/dist/{chunk-4HRQZXNE.js → chunk-HWKTHOMP.js} +58 -3
  12. package/dist/chunk-HWKTHOMP.js.map +1 -0
  13. package/dist/{chunk-XVXTLQGK.mjs → chunk-IMUFSW7Z.mjs} +58 -3
  14. package/dist/chunk-IMUFSW7Z.mjs.map +1 -0
  15. package/dist/{chunk-XQ4CP2JK.mjs → chunk-MCWJ3AWF.mjs} +1 -1
  16. package/dist/{chunk-WDUUAIG3.js → chunk-PDYPOYVZ.js} +1 -1
  17. package/dist/{chunk-WDUUAIG3.js.map → chunk-PDYPOYVZ.js.map} +1 -1
  18. package/dist/cli/bundle.js +1232 -458
  19. package/dist/{implementation-Atjtldz3.d.ts → implementation-BparHh-q.d.ts} +1 -1
  20. package/dist/{implementation-CNNrmXZu.d.mts → implementation-CrBgn1V6.d.mts} +1 -1
  21. package/dist/index.d.mts +112 -7
  22. package/dist/index.d.ts +112 -7
  23. package/dist/index.js +270 -46
  24. package/dist/index.js.map +1 -1
  25. package/dist/index.mjs +240 -16
  26. package/dist/index.mjs.map +1 -1
  27. package/dist/observability-sdk/index.d.mts +27 -6
  28. package/dist/observability-sdk/index.d.ts +27 -6
  29. package/dist/observability-sdk/index.js +13 -3
  30. package/dist/observability-sdk/index.js.map +1 -1
  31. package/dist/observability-sdk/index.mjs +12 -2
  32. package/dist/observability-sdk/instrumentation/langchain/index.d.mts +1 -1
  33. package/dist/observability-sdk/instrumentation/langchain/index.d.ts +1 -1
  34. package/dist/observability-sdk/setup/node/index.d.mts +39 -3
  35. package/dist/observability-sdk/setup/node/index.d.ts +39 -3
  36. package/dist/observability-sdk/setup/node/index.js +9 -3
  37. package/dist/observability-sdk/setup/node/index.js.map +1 -1
  38. package/dist/observability-sdk/setup/node/index.mjs +10 -4
  39. package/dist/{types--djDPuMb.d.ts → types-BXcB5Uov.d.ts} +809 -23
  40. package/dist/{types-GklcWLfF.d.mts → types-D_MsVA5r.d.mts} +809 -23
  41. package/package.json +1 -1
  42. package/dist/chunk-4HRQZXNE.js.map +0 -1
  43. package/dist/chunk-UHM5RXEW.mjs.map +0 -1
  44. package/dist/chunk-VJK42Y3W.js.map +0 -1
  45. package/dist/chunk-XVXTLQGK.mjs.map +0 -1
  46. /package/dist/{chunk-XQ4CP2JK.mjs.map → chunk-MCWJ3AWF.mjs.map} +0 -0
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  FilterableBatchSpanProcessor
3
- } from "./chunk-XQ4CP2JK.mjs";
3
+ } from "./chunk-MCWJ3AWF.mjs";
4
4
  import {
5
5
  createLangWatchSpan,
6
6
  external_exports,
@@ -9,7 +9,7 @@ import {
9
9
  import {
10
10
  ensureSetup,
11
11
  resolveEndpoint
12
- } from "./chunk-UHM5RXEW.mjs";
12
+ } from "./chunk-5TNHAT3S.mjs";
13
13
  import {
14
14
  API_VERSION_HEADER,
15
15
  LANGWATCH_API_VERSION,
@@ -21,7 +21,7 @@ import {
21
21
  LangWatchTraceExporter,
22
22
  buildAuthHeaders,
23
23
  getLangWatchLogger
24
- } from "./chunk-XVXTLQGK.mjs";
24
+ } from "./chunk-IMUFSW7Z.mjs";
25
25
  import "./chunk-C7IWJ2GJ.mjs";
26
26
  import {
27
27
  ConsoleLogger,
@@ -5985,9 +5985,10 @@ var DatasetApiError = class extends DatasetError {
5985
5985
  }
5986
5986
  };
5987
5987
  var DatasetValidationError = class extends DatasetError {
5988
- constructor(message) {
5988
+ constructor(message, code) {
5989
5989
  super(message);
5990
5990
  this.name = "DatasetValidationError";
5991
+ this.code = code;
5991
5992
  }
5992
5993
  };
5993
5994
  var DatasetPlanLimitError = class extends DatasetError {
@@ -6087,6 +6088,36 @@ var tracer2 = getLangWatchTracer(
6087
6088
  LANGWATCH_SDK_VERSION
6088
6089
  );
6089
6090
 
6091
+ // src/client-sdk/services/datasets/upsert-keys.ts
6092
+ function stableJson(value) {
6093
+ return JSON.stringify(
6094
+ value,
6095
+ (_key, inner) => inner && typeof inner === "object" && !Array.isArray(inner) ? Object.fromEntries(Object.entries(inner).toSorted(([a], [b]) => a.localeCompare(b))) : inner
6096
+ );
6097
+ }
6098
+ function keyOf(value) {
6099
+ return stableJson(value);
6100
+ }
6101
+ function assertUpsertableKeys(entries, key) {
6102
+ const seen = /* @__PURE__ */ new Set();
6103
+ for (const entry of entries) {
6104
+ const value = Object.hasOwn(entry, key) ? keyOf(entry[key]) : void 0;
6105
+ if (value === void 0) {
6106
+ throw new DatasetValidationError(
6107
+ `Every entry must hold a JSON value in the key column "${key}"`,
6108
+ "missing_upsert_key"
6109
+ );
6110
+ }
6111
+ if (seen.has(value)) {
6112
+ throw new DatasetValidationError(
6113
+ `Two entries share the key ${key} = ${value}`,
6114
+ "duplicate_upsert_key"
6115
+ );
6116
+ }
6117
+ seen.add(value);
6118
+ }
6119
+ }
6120
+
6090
6121
  // src/client-sdk/services/datasets/dataset.service.ts
6091
6122
  var API_COLUMN_TYPES = {
6092
6123
  string: "string",
@@ -6115,6 +6146,10 @@ function toApiColumnTypes(columnTypes) {
6115
6146
  return { name, type: parsed.data };
6116
6147
  });
6117
6148
  }
6149
+ var CREATE_BATCH_SIZE = 1e3;
6150
+ function sameEntry(a, b) {
6151
+ return stableJson(a) === stableJson(b);
6152
+ }
6118
6153
  var DatasetService = class {
6119
6154
  constructor(config) {
6120
6155
  this.config = __spreadProps(__spreadValues({}, config), { endpoint: resolveEndpoint(config.endpoint) });
@@ -6333,10 +6368,10 @@ var DatasetService = class {
6333
6368
  this.config.logger.debug(`Creating dataset: ${options.name}`);
6334
6369
  const response = await this.asResponseEnvelope(
6335
6370
  this.config.langwatchApiClient.POST("/api/v1/dataset", {
6336
- body: {
6371
+ body: __spreadValues({
6337
6372
  name: options.name,
6338
6373
  columnTypes: toApiColumnTypes((_a = options.columnTypes) != null ? _a : [])
6339
- }
6374
+ }, options.entries && options.entries.length > 0 ? { entries: options.entries } : {})
6340
6375
  })
6341
6376
  );
6342
6377
  return this.unwrapResponse(response, `create dataset "${options.name}"`);
@@ -6411,6 +6446,54 @@ var DatasetService = class {
6411
6446
  slugOrId
6412
6447
  );
6413
6448
  }
6449
+ /**
6450
+ * Updates the rows whose `key` column matches an entry, adds the entries whose key is new, and
6451
+ * leaves alone the rows an entry would not change. Columns an entry leaves out keep their
6452
+ * values. Refuses, before writing anything, a key that more than one existing row holds.
6453
+ */
6454
+ async upsertRecords(slugOrId, entries, { key }) {
6455
+ var _a, _b;
6456
+ const result = { created: [], updated: [], unchanged: 0 };
6457
+ if (entries.length === 0) {
6458
+ await this.listRecords(slugOrId, { page: 1, limit: 1 });
6459
+ return result;
6460
+ }
6461
+ const existing = /* @__PURE__ */ new Map();
6462
+ for (const row of (await this.getDataset(slugOrId)).entries) {
6463
+ if (!(key in row.entry)) continue;
6464
+ const rowKey = keyOf(row.entry[key]);
6465
+ existing.set(rowKey, [...(_a = existing.get(rowKey)) != null ? _a : [], row]);
6466
+ }
6467
+ const duplicated = entries.filter((entry) => {
6468
+ var _a2, _b2;
6469
+ return ((_b2 = (_a2 = existing.get(keyOf(entry[key]))) == null ? void 0 : _a2.length) != null ? _b2 : 0) > 1;
6470
+ }).map((entry) => keyOf(entry[key]));
6471
+ if (duplicated.length > 0) {
6472
+ throw new DatasetValidationError(
6473
+ `More than one row holds ${key} = ${duplicated.join(", ")}. Delete the extra rows, then upsert again.`,
6474
+ "dataset_has_duplicate_key"
6475
+ );
6476
+ }
6477
+ const toCreate = [];
6478
+ for (const entry of entries) {
6479
+ const row = (_b = existing.get(keyOf(entry[key]))) == null ? void 0 : _b[0];
6480
+ if (!row) {
6481
+ toCreate.push(entry);
6482
+ continue;
6483
+ }
6484
+ const merged = __spreadValues(__spreadValues({}, row.entry), entry);
6485
+ if (sameEntry(merged, row.entry)) {
6486
+ result.unchanged++;
6487
+ continue;
6488
+ }
6489
+ result.updated.push(await this.updateRecord(slugOrId, row.id, merged));
6490
+ }
6491
+ for (let start = 0; start < toCreate.length; start += CREATE_BATCH_SIZE) {
6492
+ const batch = toCreate.slice(start, start + CREATE_BATCH_SIZE);
6493
+ result.created.push(...(await this.createRecords(slugOrId, batch)).data);
6494
+ }
6495
+ return result;
6496
+ }
6414
6497
  /**
6415
6498
  * Deletes records from a dataset by IDs.
6416
6499
  */
@@ -6686,6 +6769,22 @@ var DatasetsFacade = class {
6686
6769
  this.updateRecord = (slugOrId, recordId, entry) => {
6687
6770
  return __privateGet(this, _datasetService).updateRecord(slugOrId, recordId, entry);
6688
6771
  };
6772
+ /**
6773
+ * Upserts rows matched on a key column: a known key updates in place (columns left out are
6774
+ * kept), a new key adds a row, and an unchanged row is not written, so a rerun writes nothing.
6775
+ * @param slugOrId - The slug or ID of the dataset
6776
+ * @param entries - The rows to upsert; each must hold the key column
6777
+ * @param options - The key column that identifies a row, such as "case_id"
6778
+ * @returns The created and updated records and the number of rows left unchanged
6779
+ */
6780
+ this.upsertRecords = async (slugOrId, entries, options) => {
6781
+ const { key } = options;
6782
+ if (!key) {
6783
+ throw new DatasetValidationError("key must name the column that identifies a row");
6784
+ }
6785
+ assertUpsertableKeys(entries, key);
6786
+ return __privateGet(this, _datasetService).upsertRecords(slugOrId, entries, options);
6787
+ };
6689
6788
  /**
6690
6789
  * Deletes records from a dataset by IDs.
6691
6790
  * @param slugOrId - The slug or ID of the dataset
@@ -8158,16 +8257,20 @@ var Experiment = class _Experiment {
8158
8257
  var toRunStartRequest = ({
8159
8258
  data,
8160
8259
  datasetId,
8260
+ datasetVersion,
8161
8261
  parameters,
8162
8262
  rowIndices,
8163
- concurrency
8263
+ concurrency,
8264
+ repeat
8164
8265
  }) => {
8165
8266
  const body = {};
8166
8267
  if (data !== void 0) body.data = data;
8167
8268
  if (datasetId !== void 0) body.dataset_id = datasetId;
8269
+ if (datasetVersion !== void 0) body.dataset_version = datasetVersion;
8168
8270
  if (parameters !== void 0) body.parameters = parameters;
8169
8271
  if (rowIndices !== void 0) body.row_indices = rowIndices;
8170
8272
  if (concurrency !== void 0) body.concurrency = concurrency;
8273
+ if (repeat !== void 0) body.repeat = repeat;
8171
8274
  return Object.keys(body).length > 0 ? body : void 0;
8172
8275
  };
8173
8276
  var ExperimentsApiServiceError = class extends Error {
@@ -8241,7 +8344,9 @@ var ExperimentsApiService = class {
8241
8344
  async startRun(slug, options = {}) {
8242
8345
  const body = toRunStartRequest({
8243
8346
  parameters: options.parameters,
8244
- concurrency: options.concurrency
8347
+ concurrency: options.concurrency,
8348
+ datasetVersion: options.datasetVersion,
8349
+ repeat: options.repeat
8245
8350
  });
8246
8351
  const { data, error, response } = await this.apiClient.POST("/api/v1/experiments/{slug}/run", __spreadValues({
8247
8352
  params: { path: { slug } }
@@ -8715,6 +8820,7 @@ var ExperimentsFacade = class {
8715
8820
  const body = toRunStartRequest({
8716
8821
  data: options.data,
8717
8822
  datasetId: options.datasetId,
8823
+ datasetVersion: options.datasetVersion,
8718
8824
  parameters: options.parameters,
8719
8825
  rowIndices: options.rowIndices,
8720
8826
  concurrency: options.concurrency
@@ -8754,7 +8860,10 @@ var ExperimentsFacade = class {
8754
8860
  var _a, _b, _c, _d, _e, _f, _g, _h;
8755
8861
  const pollInterval = (_a = options.pollInterval) != null ? _a : DEFAULT_POLL_INTERVAL2;
8756
8862
  const timeout = (_b = options.timeout) != null ? _b : DEFAULT_TIMEOUT;
8757
- const startResponse = await this.startRun(slug, options.concurrency);
8863
+ const startResponse = await this.startRun(slug, {
8864
+ concurrency: options.concurrency,
8865
+ datasetVersion: options.datasetVersion
8866
+ });
8758
8867
  const { runId } = startResponse;
8759
8868
  const apiRunUrl = (_c = startResponse.runUrl) != null ? _c : "";
8760
8869
  const runUrl = apiRunUrl ? rebaseUrlToEndpoint(apiRunUrl, this.config.endpoint) : "";
@@ -8813,8 +8922,8 @@ var ExperimentsFacade = class {
8813
8922
  /**
8814
8923
  * Start an experiment run
8815
8924
  */
8816
- async startRun(slug, concurrency) {
8817
- const body = toRunStartRequest({ concurrency });
8925
+ async startRun(slug, { concurrency, datasetVersion } = {}) {
8926
+ const body = toRunStartRequest({ concurrency, datasetVersion });
8818
8927
  let response;
8819
8928
  try {
8820
8929
  response = await this.config.langwatchApiClient.POST("/api/v1/experiments/{slug}/run", __spreadValues({
@@ -9153,11 +9262,12 @@ var EvaluatorsApiService = class {
9153
9262
  });
9154
9263
  }
9155
9264
  /**
9156
- * Fetches a single evaluator by its ID or slug.
9265
+ * Fetches a single evaluator by its ID or slug, at the latest version unless a `label` or a
9266
+ * `version` is asked for. The slug also takes a suffix: `toxicity:production` or `toxicity:2`.
9157
9267
  */
9158
- async get(idOrSlug) {
9268
+ async get(idOrSlug, selector = {}) {
9159
9269
  const { data, error, response } = await this.apiClient.GET("/api/v1/evaluators/{idOrSlug}", {
9160
- params: { path: { idOrSlug } }
9270
+ params: { path: { idOrSlug }, query: selector }
9161
9271
  });
9162
9272
  return unwrapApiResult({
9163
9273
  operation: `fetch evaluator with ID or slug "${idOrSlug}"`,
@@ -9213,6 +9323,48 @@ var EvaluatorsApiService = class {
9213
9323
  onError: this.handleApiError.bind(this)
9214
9324
  });
9215
9325
  }
9326
+ /** Every version of the evaluator, newest first, with the labels on each. */
9327
+ async listVersions(id) {
9328
+ const { data, error, response } = await this.apiClient.GET(
9329
+ "/api/v1/evaluators/{evaluatorId}/versions",
9330
+ { params: { path: { evaluatorId: id } } }
9331
+ );
9332
+ return unwrapApiResult({
9333
+ operation: `list versions of evaluator "${id}"`,
9334
+ data,
9335
+ error,
9336
+ response,
9337
+ onError: this.handleApiError.bind(this)
9338
+ });
9339
+ }
9340
+ /** Points a deploy label, such as `production`, at one of the evaluator's versions. */
9341
+ async assignLabel(id, label, version) {
9342
+ const { data, error, response } = await this.apiClient.PUT(
9343
+ "/api/v1/evaluators/{evaluatorId}/labels/{label}",
9344
+ { params: { path: { evaluatorId: id, label } }, body: { version } }
9345
+ );
9346
+ return unwrapApiResult({
9347
+ operation: `deploy label "${label}" of evaluator "${id}"`,
9348
+ data,
9349
+ error,
9350
+ response,
9351
+ onError: this.handleApiError.bind(this)
9352
+ });
9353
+ }
9354
+ /** Removes a deploy label from the evaluator. */
9355
+ async removeLabel(id, label) {
9356
+ const { data, error, response } = await this.apiClient.DELETE(
9357
+ "/api/v1/evaluators/{evaluatorId}/labels/{label}",
9358
+ { params: { path: { evaluatorId: id, label } } }
9359
+ );
9360
+ return unwrapApiResult({
9361
+ operation: `remove label "${label}" of evaluator "${id}"`,
9362
+ data,
9363
+ error,
9364
+ response,
9365
+ onError: this.handleApiError.bind(this)
9366
+ });
9367
+ }
9216
9368
  };
9217
9369
 
9218
9370
  // src/client-sdk/services/scenarios/errors.ts
@@ -9821,6 +9973,34 @@ var TestSuitesApiService = class {
9821
9973
  onError: this.handleApiError.bind(this)
9822
9974
  });
9823
9975
  }
9976
+ /** Lists the suite templates that ship with LangWatch, scenarios included. */
9977
+ async listTemplates() {
9978
+ const { data, error, response } = await this.apiClient.GET("/api/v1/test-suites/templates");
9979
+ return unwrapApiResult({
9980
+ operation: "list suite templates",
9981
+ data,
9982
+ error,
9983
+ response,
9984
+ onError: this.handleApiError.bind(this)
9985
+ });
9986
+ }
9987
+ /** Creates a test suite from a template, fitted to the agent under test. */
9988
+ async applyTemplate({
9989
+ templateId,
9990
+ body
9991
+ }) {
9992
+ const { data, error, response } = await this.apiClient.POST(
9993
+ "/api/v1/test-suites/templates/{templateId}/apply",
9994
+ { params: { path: { templateId } }, body }
9995
+ );
9996
+ return unwrapApiResult({
9997
+ operation: `apply suite template "${templateId}"`,
9998
+ data,
9999
+ error,
10000
+ response,
10001
+ onError: this.handleApiError.bind(this)
10002
+ });
10003
+ }
9824
10004
  };
9825
10005
 
9826
10006
  // src/client-sdk/services/workflows/workflows-api.service.ts
@@ -9918,6 +10098,7 @@ var WorkflowsApiService = class {
9918
10098
  if (options.versionId !== void 0) body.version_id = options.versionId;
9919
10099
  if (options.data !== void 0) body.data = options.data;
9920
10100
  if (options.datasetId !== void 0) body.dataset_id = options.datasetId;
10101
+ if (options.datasetVersion !== void 0) body.dataset_version = options.datasetVersion;
9921
10102
  if (options.parameters !== void 0) body.parameters = options.parameters;
9922
10103
  if (options.rowIndices !== void 0) body.row_indices = options.rowIndices;
9923
10104
  const startResponse = await this.postUndeclaredEndpoint({
@@ -9992,9 +10173,10 @@ var AgentsApiService = class {
9992
10173
  onError: this.handleApiError.bind(this)
9993
10174
  });
9994
10175
  }
9995
- async get(id) {
10176
+ /** Reads the latest version unless a `label` or a `version` is asked for. */
10177
+ async get(id, selector = {}) {
9996
10178
  const { data, error, response } = await this.apiClient.GET("/api/v1/agents/{id}", {
9997
- params: { path: { id } }
10179
+ params: { path: { id }, query: selector }
9998
10180
  });
9999
10181
  return unwrapApiResult({
10000
10182
  operation: `get agent "${id}"`,
@@ -10075,6 +10257,48 @@ var AgentsApiService = class {
10075
10257
  onError: this.handleApiError.bind(this)
10076
10258
  });
10077
10259
  }
10260
+ /** Every version of the agent, newest first, with the labels on each. */
10261
+ async listVersions(id) {
10262
+ const { data, error, response } = await this.apiClient.GET(
10263
+ "/api/v1/agents/{agentId}/versions",
10264
+ { params: { path: { agentId: id } } }
10265
+ );
10266
+ return unwrapApiResult({
10267
+ operation: `list versions of agent "${id}"`,
10268
+ data,
10269
+ error,
10270
+ response,
10271
+ onError: this.handleApiError.bind(this)
10272
+ });
10273
+ }
10274
+ /** Points a deploy label, such as `production`, at one of the agent's versions. */
10275
+ async assignLabel(id, label, version) {
10276
+ const { data, error, response } = await this.apiClient.PUT(
10277
+ "/api/v1/agents/{agentId}/labels/{label}",
10278
+ { params: { path: { agentId: id, label } }, body: { version } }
10279
+ );
10280
+ return unwrapApiResult({
10281
+ operation: `deploy label "${label}" of agent "${id}"`,
10282
+ data,
10283
+ error,
10284
+ response,
10285
+ onError: this.handleApiError.bind(this)
10286
+ });
10287
+ }
10288
+ /** Removes a deploy label from the agent. */
10289
+ async removeLabel(id, label) {
10290
+ const { data, error, response } = await this.apiClient.DELETE(
10291
+ "/api/v1/agents/{agentId}/labels/{label}",
10292
+ { params: { path: { agentId: id, label } } }
10293
+ );
10294
+ return unwrapApiResult({
10295
+ operation: `remove label "${label}" of agent "${id}"`,
10296
+ data,
10297
+ error,
10298
+ response,
10299
+ onError: this.handleApiError.bind(this)
10300
+ });
10301
+ }
10078
10302
  };
10079
10303
 
10080
10304
  // src/client-sdk/services/annotations/annotations-api.service.ts