langwatch 1.12.1 → 1.13.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 (41) hide show
  1. package/dist/agent/index.d.mts +5 -1
  2. package/dist/agent/index.d.ts +5 -1
  3. package/dist/agent/index.js +239 -14
  4. package/dist/agent/index.js.map +1 -1
  5. package/dist/agent/index.mjs +239 -14
  6. package/dist/agent/index.mjs.map +1 -1
  7. package/dist/{chunk-OSVJ65OO.js → chunk-7R54TMZZ.js} +2 -2
  8. package/dist/{chunk-OSVJ65OO.js.map → chunk-7R54TMZZ.js.map} +1 -1
  9. package/dist/{chunk-H2DJGGYQ.js → chunk-G64XZMIU.js} +12 -12
  10. package/dist/{chunk-H2DJGGYQ.js.map → chunk-G64XZMIU.js.map} +1 -1
  11. package/dist/{chunk-53NOLJEX.mjs → chunk-N3755EHD.mjs} +2 -2
  12. package/dist/{chunk-2HA4SJSD.mjs → chunk-NPEK4RSD.mjs} +5 -2
  13. package/dist/{chunk-2HA4SJSD.mjs.map → chunk-NPEK4RSD.mjs.map} +1 -1
  14. package/dist/{chunk-4ERRCKOH.js → chunk-O4J3ZPZY.js} +5 -2
  15. package/dist/chunk-O4J3ZPZY.js.map +1 -0
  16. package/dist/{chunk-IVRETMQH.mjs → chunk-UHRSTPZ5.mjs} +2 -2
  17. package/dist/{chunk-IVRETMQH.mjs.map → chunk-UHRSTPZ5.mjs.map} +1 -1
  18. package/dist/cli/bundle.js +362 -358
  19. package/dist/{implementation-BNaydX6f.d.ts → implementation-BDK4X-CS.d.ts} +1 -1
  20. package/dist/{implementation-DLMEZQgA.d.mts → implementation-BOP-3GoX.d.mts} +1 -1
  21. package/dist/index.d.mts +75 -5
  22. package/dist/index.d.ts +75 -5
  23. package/dist/index.js +324 -99
  24. package/dist/index.js.map +1 -1
  25. package/dist/index.mjs +242 -17
  26. package/dist/index.mjs.map +1 -1
  27. package/dist/observability-sdk/index.d.mts +3 -3
  28. package/dist/observability-sdk/index.d.ts +3 -3
  29. package/dist/observability-sdk/index.js +3 -3
  30. package/dist/observability-sdk/index.mjs +2 -2
  31. package/dist/observability-sdk/instrumentation/langchain/index.d.mts +1 -1
  32. package/dist/observability-sdk/instrumentation/langchain/index.d.ts +1 -1
  33. package/dist/observability-sdk/instrumentation/langchain/index.js +3 -3
  34. package/dist/observability-sdk/instrumentation/langchain/index.mjs +1 -1
  35. package/dist/observability-sdk/setup/node/index.js +3 -3
  36. package/dist/observability-sdk/setup/node/index.mjs +2 -2
  37. package/dist/{types-CLDQMYpV.d.ts → types-C3SDhizh.d.ts} +1138 -111
  38. package/dist/{types-DWNdXOWw.d.mts → types-C9abONOS.d.mts} +1138 -111
  39. package/package.json +1 -1
  40. package/dist/chunk-4ERRCKOH.js.map +0 -1
  41. /package/dist/{chunk-53NOLJEX.mjs.map → chunk-N3755EHD.mjs.map} +0 -0
package/dist/index.mjs CHANGED
@@ -5,11 +5,11 @@ import {
5
5
  createLangWatchSpan,
6
6
  external_exports,
7
7
  getLangWatchTracer
8
- } from "./chunk-2HA4SJSD.mjs";
8
+ } from "./chunk-NPEK4RSD.mjs";
9
9
  import {
10
10
  ensureSetup,
11
11
  resolveEndpoint
12
- } from "./chunk-53NOLJEX.mjs";
12
+ } from "./chunk-N3755EHD.mjs";
13
13
  import {
14
14
  LANGWATCH_SDK_LANGUAGE,
15
15
  LANGWATCH_SDK_NAME_CLIENT,
@@ -20,7 +20,7 @@ import {
20
20
  buildAuthHeaders,
21
21
  getLangWatchLogger,
22
22
  version
23
- } from "./chunk-IVRETMQH.mjs";
23
+ } from "./chunk-UHRSTPZ5.mjs";
24
24
  import {
25
25
  ConsoleLogger,
26
26
  NoOpLogger,
@@ -650,6 +650,214 @@ var LangWatchApiError = class extends Error {
650
650
  }
651
651
  };
652
652
 
653
+ // src/internal/http/langwatchFetch.ts
654
+ var REDIRECT_STATUSES = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
655
+ var FOLLOWING_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD"]);
656
+ var MAX_FOLLOW_HOPS = 5;
657
+ var CREDENTIAL_HEADERS = [
658
+ "authorization",
659
+ "cookie",
660
+ "proxy-authorization",
661
+ "x-api-key",
662
+ "x-auth-token",
663
+ "x-project-id"
664
+ ];
665
+ var LangWatchRedirectError = class extends Error {
666
+ constructor({
667
+ url,
668
+ location,
669
+ status
670
+ }) {
671
+ super(
672
+ `LangWatch refused to follow a redirect from ${url} to ${location != null ? location : "an unknown location"} (HTTP ${status}). Set the endpoint to the final URL.`
673
+ );
674
+ this.name = "LangWatchRedirectError";
675
+ this.url = url;
676
+ this.location = location;
677
+ this.status = status;
678
+ }
679
+ };
680
+ var warnedOrigins = /* @__PURE__ */ new Set();
681
+ var isRedirect = (response) => response.type === "opaqueredirect" || REDIRECT_STATUSES.has(response.status);
682
+ var isStream = (body) => typeof ReadableStream !== "undefined" && body instanceof ReadableStream;
683
+ var abortError = (signal) => {
684
+ const reason = signal.reason;
685
+ if (reason instanceof Error) return reason;
686
+ if (typeof DOMException !== "undefined") {
687
+ return new DOMException("This operation was aborted", "AbortError");
688
+ }
689
+ return Object.assign(new Error("This operation was aborted"), { name: "AbortError" });
690
+ };
691
+ var discard = (spare) => {
692
+ var _a;
693
+ void ((_a = spare == null ? void 0 : spare.body) == null ? void 0 : _a.cancel().catch(() => void 0));
694
+ };
695
+ var replayBody = async ({
696
+ spare,
697
+ signal
698
+ }) => {
699
+ if (!signal) return spare.arrayBuffer();
700
+ if (signal.aborted) throw abortError(signal);
701
+ const aborted = new Promise((_, reject) => {
702
+ signal.addEventListener("abort", () => reject(abortError(signal)), { once: true });
703
+ });
704
+ void aborted.catch(() => void 0);
705
+ try {
706
+ return await Promise.race([spare.arrayBuffer(), aborted]);
707
+ } catch (error) {
708
+ discard(spare);
709
+ throw error;
710
+ }
711
+ };
712
+ var requestUrl = (input) => {
713
+ if (typeof input === "string") return input;
714
+ if (input instanceof URL) return input.href;
715
+ return input.url;
716
+ };
717
+ var parseHop = ({
718
+ url,
719
+ location
720
+ }) => {
721
+ try {
722
+ const from = new URL(url);
723
+ return { from, to: new URL(location, from) };
724
+ } catch (e) {
725
+ return null;
726
+ }
727
+ };
728
+ var isSchemeUpgrade = ({ from, to }) => from.protocol === "http:" && to.protocol === "https:" && from.hostname === to.hostname && from.port === to.port;
729
+ var schemeUpgradeTarget = ({
730
+ url,
731
+ location
732
+ }) => {
733
+ const hop = parseHop({ url, location });
734
+ if (hop === null || !isSchemeUpgrade(hop)) return null;
735
+ const { from, to } = hop;
736
+ if (from.pathname !== to.pathname) return null;
737
+ if (from.search !== to.search) return null;
738
+ to.hash = "";
739
+ return to.href;
740
+ };
741
+ var followTarget = ({
742
+ url,
743
+ location
744
+ }) => {
745
+ const hop = parseHop({ url, location });
746
+ if (hop === null) return null;
747
+ const { from, to } = hop;
748
+ if (to.protocol !== "http:" && to.protocol !== "https:") return null;
749
+ if (from.protocol === "https:" && to.protocol === "http:") return null;
750
+ to.hash = "";
751
+ return to.href;
752
+ };
753
+ var keepsCredentials = ({ from, to }) => from.origin === to.origin || isSchemeUpgrade({ from, to });
754
+ var withoutCredentials = (headers) => {
755
+ const stripped = new Headers(headers);
756
+ for (const name of CREDENTIAL_HEADERS) stripped.delete(name);
757
+ return stripped;
758
+ };
759
+ var warnOnce = ({ url, logger: logger2 }) => {
760
+ const { origin, host } = new URL(url);
761
+ if (warnedOrigins.has(origin)) return;
762
+ warnedOrigins.add(origin);
763
+ logger2.warn(
764
+ `LangWatch endpoint ${origin} redirected to https. Set the endpoint to https://${host} to skip the extra round trip.`
765
+ );
766
+ };
767
+ var refusalOf = ({
768
+ url,
769
+ response
770
+ }) => new LangWatchRedirectError({
771
+ url,
772
+ location: response.headers.get("location"),
773
+ status: response.status
774
+ });
775
+ var effectiveRequest = ({
776
+ input,
777
+ init
778
+ }) => typeof Request !== "undefined" && input instanceof Request ? new Request(input, __spreadProps(__spreadValues({}, init), { redirect: "manual" })) : null;
779
+ var follow = async ({
780
+ send,
781
+ log,
782
+ effective,
783
+ init,
784
+ url,
785
+ method,
786
+ first
787
+ }) => {
788
+ var _a, _b;
789
+ let headers = new Headers((_a = effective == null ? void 0 : effective.headers) != null ? _a : init == null ? void 0 : init.headers);
790
+ const signal = (_b = effective == null ? void 0 : effective.signal) != null ? _b : init == null ? void 0 : init.signal;
791
+ let current = url;
792
+ let response = first;
793
+ for (let hop = 0; hop < MAX_FOLLOW_HOPS; hop++) {
794
+ const location = response.headers.get("location");
795
+ const target = location === null ? null : followTarget({ url: current, location });
796
+ if (target === null) throw refusalOf({ url: current, response });
797
+ const from = new URL(current);
798
+ const to = new URL(target);
799
+ if (!keepsCredentials({ from, to })) headers = withoutCredentials(headers);
800
+ if (isSchemeUpgrade({ from, to })) warnOnce({ url: current, logger: log });
801
+ current = target;
802
+ response = effective ? await send(new Request(target, { method, headers, signal, redirect: "manual" })) : await send(target, __spreadProps(__spreadValues({}, init), { method, headers, body: void 0, redirect: "manual" }));
803
+ if (!isRedirect(response)) return response;
804
+ }
805
+ throw refusalOf({ url: current, response });
806
+ };
807
+ var upgrade = async ({
808
+ send,
809
+ log,
810
+ effective,
811
+ init,
812
+ url,
813
+ first,
814
+ spare
815
+ }) => {
816
+ const location = first.headers.get("location");
817
+ const refused = refusalOf({ url, response: first });
818
+ const target = location === null ? null : schemeUpgradeTarget({ url, location });
819
+ if (location === null || first.status === 303 || target === null || isStream(init == null ? void 0 : init.body)) {
820
+ discard(spare);
821
+ throw refused;
822
+ }
823
+ warnOnce({ url, logger: log });
824
+ const second = effective ? await send(
825
+ new Request(target, {
826
+ method: effective.method,
827
+ headers: effective.headers,
828
+ body: spare ? await replayBody({ spare, signal: effective.signal }) : null,
829
+ signal: effective.signal,
830
+ redirect: "manual"
831
+ })
832
+ ) : await send(target, __spreadProps(__spreadValues({}, init), { redirect: "manual" }));
833
+ if (!isRedirect(second)) return second;
834
+ throw refusalOf({ url: target, response: second });
835
+ };
836
+ var createLangWatchFetch = ({
837
+ fetch: fetchImpl,
838
+ logger: logger2
839
+ } = {}) => {
840
+ const send = fetchImpl != null ? fetchImpl : ((input, init) => globalThis.fetch(input, init));
841
+ const log = logger2 != null ? logger2 : new ConsoleLogger({ level: "warn", prefix: "LangWatch" });
842
+ return async (input, init) => {
843
+ var _a, _b;
844
+ const url = requestUrl(input);
845
+ const effective = effectiveRequest({ input, init });
846
+ const method = ((_b = (_a = effective == null ? void 0 : effective.method) != null ? _a : init == null ? void 0 : init.method) != null ? _b : "GET").toUpperCase();
847
+ const spare = effective !== null && effective.body !== null ? effective.clone() : null;
848
+ const first = effective ? await send(effective) : await send(input, __spreadProps(__spreadValues({}, init), { redirect: "manual" }));
849
+ if (!isRedirect(first)) {
850
+ discard(spare);
851
+ return first;
852
+ }
853
+ const hop = { send, log, effective, init, url, first };
854
+ if (!FOLLOWING_METHODS.has(method)) return upgrade(__spreadProps(__spreadValues({}, hop), { spare }));
855
+ discard(spare);
856
+ return follow(__spreadProps(__spreadValues({}, hop), { method }));
857
+ };
858
+ };
859
+ var langwatchFetch = createLangWatchFetch();
860
+
653
861
  // src/internal/api/client.ts
654
862
  var handledErrorMiddleware = {
655
863
  async onResponse({ request, response }) {
@@ -677,6 +885,7 @@ var handledErrorMiddleware = {
677
885
  var createLangWatchApiClient = (apiKey = ((_b) => (_b = ((_a) => (_a = scopedApiKey()) != null ? _a : process.env.LANGWATCH_API_KEY)()) != null ? _b : "")(), endpoint, projectId = ((_c) => (_c = scopedProjectId()) != null ? _c : process.env.LANGWATCH_PROJECT_ID)()) => {
678
886
  const client = openApiCreateClient({
679
887
  baseUrl: resolveEndpoint(endpoint),
888
+ fetch: langwatchFetch,
680
889
  headers: __spreadProps(__spreadValues({}, buildAuthHeaders({ apiKey, projectId })), {
681
890
  "content-type": "application/json",
682
891
  "user-agent": `langwatch-sdk-node/${version}`,
@@ -2244,7 +2453,7 @@ var DatasetService = class {
2244
2453
  async fetchMultipart(path2, formData, operation, slugOrId) {
2245
2454
  const { endpoint, apiKey } = this.config;
2246
2455
  const url = `${endpoint}${path2}`;
2247
- const response = await fetch(url, {
2456
+ const response = await langwatchFetch(url, {
2248
2457
  method: "POST",
2249
2458
  headers: buildAuthHeaders({ apiKey }),
2250
2459
  body: formData
@@ -2970,7 +3179,7 @@ var Experiment = class _Experiment {
2970
3179
  );
2971
3180
  }
2972
3181
  try {
2973
- const response = await fetch(`${this.endpoint}/api/experiment/init`, {
3182
+ const response = await langwatchFetch(`${this.endpoint}/api/experiment/init`, {
2974
3183
  method: "POST",
2975
3184
  headers: __spreadValues({
2976
3185
  "Content-Type": "application/json"
@@ -3324,7 +3533,7 @@ var Experiment = class _Experiment {
3324
3533
  spanId,
3325
3534
  asGuardrail = false
3326
3535
  }) {
3327
- const response = await fetch(
3536
+ const response = await langwatchFetch(
3328
3537
  `${this.endpoint}/api/evaluations/${evaluatorSlug}/evaluate`,
3329
3538
  {
3330
3539
  method: "POST",
@@ -3863,7 +4072,7 @@ var Experiment = class _Experiment {
3863
4072
  finished_at: finished ? Date.now() : null
3864
4073
  }
3865
4074
  };
3866
- this.pendingFlush = fetch(`${this.endpoint}/api/evaluations/batch/log_results`, {
4075
+ this.pendingFlush = langwatchFetch(`${this.endpoint}/api/evaluations/batch/log_results`, {
3867
4076
  method: "POST",
3868
4077
  headers: __spreadValues({
3869
4078
  "Content-Type": "application/json"
@@ -4851,7 +5060,7 @@ var EvaluationsFacade = class {
4851
5060
  };
4852
5061
  const url = `${__privateGet(this, _endpoint)}/api/evaluations/${slug}/evaluate`;
4853
5062
  __privateGet(this, _logger).debug(`Calling evaluation API: ${url}`);
4854
- const response = await fetch(url, {
5063
+ const response = await langwatchFetch(url, {
4855
5064
  method: "POST",
4856
5065
  headers: __spreadValues({
4857
5066
  "Content-Type": "application/json"
@@ -5338,11 +5547,24 @@ var TestSuitesApiService = class {
5338
5547
  if (error) this.handleApiError(`get test suite "${id}"`, error, response);
5339
5548
  return data;
5340
5549
  }
5550
+ /**
5551
+ * Edits a suite: any of its name, its fields and its evaluators. A field
5552
+ * list or an evaluator list replaces the one the suite holds; a key left
5553
+ * out keeps what the suite has.
5554
+ */
5555
+ async update(id, params) {
5556
+ const { data, error, response } = await this.apiClient.PATCH(
5557
+ "/api/v1/test-suites/{id}",
5558
+ { params: { path: { id } }, body: params }
5559
+ );
5560
+ if (error) this.handleApiError(`update test suite "${id}"`, error, response);
5561
+ return data;
5562
+ }
5341
5563
  /** Renames a suite. The slug is kept, so links and run history stay put. */
5342
5564
  async rename(id, params) {
5343
5565
  const { data, error, response } = await this.apiClient.PATCH(
5344
5566
  "/api/v1/test-suites/{id}",
5345
- { params: { path: { id } }, body: params }
5567
+ { params: { path: { id } }, body: { name: params.name } }
5346
5568
  );
5347
5569
  if (error) this.handleApiError(`rename test suite "${id}"`, error, response);
5348
5570
  return data;
@@ -6014,7 +6236,7 @@ var MonitorsApiService = class {
6014
6236
  }
6015
6237
  async request(path2, options) {
6016
6238
  var _a, _b;
6017
- const response = await fetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, options), {
6239
+ const response = await langwatchFetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, options), {
6018
6240
  headers: __spreadValues(__spreadProps(__spreadValues({}, buildAuthHeaders({ apiKey: this.apiKey })), {
6019
6241
  "Content-Type": "application/json"
6020
6242
  }), options == null ? void 0 : options.headers)
@@ -6089,7 +6311,7 @@ var SecretsApiService = class {
6089
6311
  }
6090
6312
  async request(path2, options) {
6091
6313
  var _a, _b;
6092
- const response = await fetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, options), {
6314
+ const response = await langwatchFetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, options), {
6093
6315
  headers: __spreadValues(__spreadProps(__spreadValues({}, buildAuthHeaders({ apiKey: this.apiKey })), {
6094
6316
  "Content-Type": "application/json"
6095
6317
  }), options == null ? void 0 : options.headers)
@@ -6232,7 +6454,7 @@ var VirtualKeysApiService = class {
6232
6454
  }
6233
6455
  async request(operation, path2, init) {
6234
6456
  var _a, _b, _c;
6235
- const response = await fetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, init), {
6457
+ const response = await langwatchFetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, init), {
6236
6458
  // A hung control plane must fail the command, not freeze it.
6237
6459
  signal: (_a = init == null ? void 0 : init.signal) != null ? _a : AbortSignal.timeout(3e4),
6238
6460
  headers: __spreadValues(__spreadValues({}, this.headers()), (_b = init == null ? void 0 : init.headers) != null ? _b : {})
@@ -6460,7 +6682,7 @@ var GatewayBudgetsApiService = class {
6460
6682
  }
6461
6683
  async request(operation, path2, init) {
6462
6684
  var _a, _b, _c;
6463
- const response = await fetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, init), {
6685
+ const response = await langwatchFetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, init), {
6464
6686
  // A hung control plane must fail the command, not freeze it.
6465
6687
  signal: (_a = init == null ? void 0 : init.signal) != null ? _a : AbortSignal.timeout(3e4),
6466
6688
  headers: __spreadValues(__spreadValues({}, this.headers()), (_b = init == null ? void 0 : init.headers) != null ? _b : {})
@@ -6712,7 +6934,7 @@ var SpendEventsApiService = class {
6712
6934
  }
6713
6935
  async request(operation, path2, init) {
6714
6936
  var _a, _b;
6715
- const response = await fetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, init), {
6937
+ const response = await langwatchFetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, init), {
6716
6938
  // A hung control plane must fail the command, not freeze it.
6717
6939
  signal: (_a = init == null ? void 0 : init.signal) != null ? _a : AbortSignal.timeout(3e4),
6718
6940
  headers: __spreadValues({
@@ -6929,7 +7151,7 @@ var WebhooksApiService = class {
6929
7151
  }
6930
7152
  async request(operation, path2, init) {
6931
7153
  var _a, _b, _c;
6932
- const response = await fetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, init), {
7154
+ const response = await langwatchFetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, init), {
6933
7155
  // A hung control plane must fail the command, not freeze it.
6934
7156
  signal: (_a = init == null ? void 0 : init.signal) != null ? _a : AbortSignal.timeout(3e4),
6935
7157
  headers: __spreadValues({
@@ -7210,7 +7432,7 @@ var createManagementRequest = ({
7210
7432
  query,
7211
7433
  signal
7212
7434
  }) => {
7213
- const response = await fetch(
7435
+ const response = await langwatchFetch(
7214
7436
  `${endpoint}${path2}${buildQueryString(query)}`,
7215
7437
  __spreadProps(__spreadValues(__spreadProps(__spreadValues({}, method ? { method } : {}), {
7216
7438
  headers: {
@@ -7366,7 +7588,7 @@ var ProjectsApiService = class {
7366
7588
  }
7367
7589
  async request(operation, path2, init) {
7368
7590
  var _a;
7369
- const response = await fetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, init), {
7591
+ const response = await langwatchFetch(`${this.endpoint}${path2}`, __spreadProps(__spreadValues({}, init), {
7370
7592
  headers: __spreadValues(__spreadValues({}, this.headers()), (_a = init == null ? void 0 : init.headers) != null ? _a : {})
7371
7593
  }));
7372
7594
  if (!response.ok) {
@@ -7739,6 +7961,7 @@ export {
7739
7961
  LangWatchApiError,
7740
7962
  LangWatchTraceExporter as LangWatchExporter,
7741
7963
  LangWatchHandledError,
7964
+ LangWatchRedirectError,
7742
7965
  ProjectsApiError,
7743
7966
  ProjectsApiService,
7744
7967
  SpendEventsApiError,
@@ -7756,9 +7979,11 @@ export {
7756
7979
  WebhooksApiError,
7757
7980
  WebhooksApiService,
7758
7981
  attributes_exports as attributes,
7982
+ createLangWatchFetch,
7759
7983
  getLangWatchLogger,
7760
7984
  getLangWatchTracer,
7761
7985
  isLangWatchHandledError,
7986
+ langwatchFetch,
7762
7987
  logger,
7763
7988
  verifyWebhookSignature
7764
7989
  };