@alva-ai/toolkit 0.32.0 → 0.33.1-beta.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/index.cjs CHANGED
@@ -55,6 +55,7 @@ __export(src_exports, {
55
55
  UdfTokenInvalidError: () => UdfTokenInvalidError,
56
56
  UdfTransportError: () => UdfTransportError,
57
57
  VERSION: () => VERSION,
58
+ XResource: () => XResource,
58
59
  getViewerToken: () => getViewerToken,
59
60
  installPlaybookRuntime: () => installPlaybookRuntime,
60
61
  mapUdfErrorResponse: () => mapUdfErrorResponse,
@@ -740,10 +741,134 @@ function requireFeedID(id) {
740
741
  return id;
741
742
  }
742
743
 
743
- // src/resources/thesisPublication.ts
744
+ // src/resources/x.ts
745
+ var GRAPHQL_STATUS_BY_CODE = {
746
+ INVALID_ARGUMENT: 400,
747
+ UNAUTHENTICATED: 401,
748
+ PERMISSION_DENIED: 403,
749
+ NOT_FOUND: 404,
750
+ ALREADY_EXISTS: 409,
751
+ ABORTED: 409,
752
+ FAILED_PRECONDITION: 412,
753
+ RESOURCE_EXHAUSTED: 429,
754
+ INTERNAL: 500,
755
+ UNKNOWN: 500,
756
+ DATA_LOSS: 500,
757
+ UNAVAILABLE: 503,
758
+ DEADLINE_EXCEEDED: 504
759
+ };
744
760
  function record(value) {
745
761
  return typeof value === "object" && value !== null && !Array.isArray(value);
746
762
  }
763
+ function validateXReadParams(params) {
764
+ if (!record(params)) throw new Error("X read parameters must be an object");
765
+ const first = params.first ?? 20;
766
+ if (params.first === null || typeof first !== "number" || !Number.isInteger(first) || first < 5 || first > 100) {
767
+ throw new Error("first must be an integer between 5 and 100");
768
+ }
769
+ if (params.after !== void 0 && (typeof params.after !== "string" || !params.after.trim() || params.after.length > 4096)) {
770
+ throw new Error(
771
+ "after must be a non-empty X cursor of at most 4096 characters"
772
+ );
773
+ }
774
+ }
775
+ function invalidResponse(field) {
776
+ throw new AlvaError(
777
+ "GRAPHQL_INVALID_RESPONSE",
778
+ `GraphQL returned an invalid viewer.${field} response`,
779
+ 502
780
+ );
781
+ }
782
+ function viewerField(response, field) {
783
+ if (!record(response)) return invalidResponse(field);
784
+ if (response.errors !== void 0) {
785
+ if (!Array.isArray(response.errors)) return invalidResponse(field);
786
+ if (response.errors.length > 0) {
787
+ const messages = response.errors.flatMap(
788
+ (error) => record(error) && typeof error.message === "string" ? [error.message] : []
789
+ );
790
+ const codes = response.errors.map(
791
+ (error) => record(error) && record(error.extensions) ? error.extensions.code : void 0
792
+ );
793
+ const code = typeof codes[0] === "string" && Object.hasOwn(GRAPHQL_STATUS_BY_CODE, codes[0]) && codes.every((value) => value === codes[0]) ? codes[0] : "GRAPHQL_ERROR";
794
+ throw new AlvaError(
795
+ code,
796
+ messages.join("; ") || "X read failed",
797
+ GRAPHQL_STATUS_BY_CODE[code] ?? 502,
798
+ { errors: response.errors }
799
+ );
800
+ }
801
+ }
802
+ if (!record(response.data) || !record(response.data.viewer))
803
+ return invalidResponse(field);
804
+ return response.data.viewer[field];
805
+ }
806
+ function readPage(value, field, fields) {
807
+ if (!record(value) || !Array.isArray(value.edges) || !record(value.pageInfo))
808
+ return invalidResponse(field);
809
+ const page = value.pageInfo;
810
+ if (typeof page.startCursor !== "string" || typeof page.endCursor !== "string" || typeof page.hasNextPage !== "boolean" || typeof page.hasPreviousPage !== "boolean" || page.hasNextPage && !page.endCursor)
811
+ return invalidResponse(field);
812
+ for (const edge of value.edges) {
813
+ if (!record(edge) || typeof edge.cursor !== "string" || !edge.cursor || !record(edge.node))
814
+ return invalidResponse(field);
815
+ const node = edge.node;
816
+ if (fields.some((key) => typeof node[key] !== "string"))
817
+ return invalidResponse(field);
818
+ if (!node.id) return invalidResponse(field);
819
+ }
820
+ const first = value.edges[0];
821
+ const last = value.edges[value.edges.length - 1];
822
+ if (page.startCursor !== (first?.cursor ?? "") || last && page.endCursor !== last.cursor)
823
+ return invalidResponse(field);
824
+ return {
825
+ edges: value.edges,
826
+ pageInfo: page,
827
+ retrievedAt: (/* @__PURE__ */ new Date()).toISOString()
828
+ };
829
+ }
830
+ var XResource = class {
831
+ constructor(client) {
832
+ this.client = client;
833
+ }
834
+ client;
835
+ async status() {
836
+ this.client._requireAuth();
837
+ const response = await this.client._request("POST", "/query", {
838
+ body: {
839
+ query: "query ToolkitXStatus { viewer { xConnection { connected canReadFollowing canReadPosts } } }"
840
+ }
841
+ });
842
+ const result = viewerField(response, "xConnection");
843
+ if (!record(result) || ["connected", "canReadFollowing", "canReadPosts"].some(
844
+ (key) => typeof result[key] !== "boolean"
845
+ ))
846
+ return invalidResponse("xConnection");
847
+ return result;
848
+ }
849
+ async following(params = {}) {
850
+ return this.read("xFollowing", "id username name", params);
851
+ }
852
+ async posts(params = {}) {
853
+ return this.read("xPosts", "id text authorId createdAt", params);
854
+ }
855
+ async read(field, fields, params) {
856
+ validateXReadParams(params);
857
+ this.client._requireAuth();
858
+ const response = await this.client._request("POST", "/query", {
859
+ body: {
860
+ query: `query ToolkitXRead($first: Int, $after: String) { viewer { ${field}(first: $first, after: $after) { edges { cursor node { ${fields} } } pageInfo { startCursor endCursor hasNextPage hasPreviousPage } } } }`,
861
+ variables: { first: params.first ?? 20, after: params.after }
862
+ }
863
+ });
864
+ return readPage(viewerField(response, field), field, fields.split(" "));
865
+ }
866
+ };
867
+
868
+ // src/resources/thesisPublication.ts
869
+ function record2(value) {
870
+ return typeof value === "object" && value !== null && !Array.isArray(value);
871
+ }
747
872
  function isPositiveInt64(value) {
748
873
  return typeof value === "string" && /^[1-9]\d{0,18}$/.test(value) && BigInt(value) <= 9223372036854775807n;
749
874
  }
@@ -774,35 +899,35 @@ function positiveInt64List(value) {
774
899
  }
775
900
  function stanceList(value) {
776
901
  return Array.isArray(value) && value.every(
777
- (item) => record(item) && isPositiveInt64(item.entityId) && typeof item.stance === "string" && item.stance.trim() !== ""
902
+ (item) => record2(item) && isPositiveInt64(item.entityId) && typeof item.stance === "string" && item.stance.trim() !== ""
778
903
  );
779
904
  }
780
905
  function entityList(value) {
781
906
  return Array.isArray(value) && value.every(
782
- (item) => record(item) && typeof item.id === "string" && item.id.trim() !== "" && typeof item.ticker === "string" && typeof item.name === "string"
907
+ (item) => record2(item) && typeof item.id === "string" && item.id.trim() !== "" && typeof item.ticker === "string" && typeof item.name === "string"
783
908
  );
784
909
  }
785
910
  function mediaList(value) {
786
911
  return Array.isArray(value) && value.every(
787
- (item) => record(item) && typeof item.type === "string" && item.type.trim() !== "" && typeof item.coverUrl === "string" && (item.url === null || typeof item.url === "string")
912
+ (item) => record2(item) && typeof item.type === "string" && item.type.trim() !== "" && typeof item.coverUrl === "string" && (item.url === null || typeof item.url === "string")
788
913
  );
789
914
  }
790
915
  function sourceRefList(value) {
791
916
  return Array.isArray(value) && value.every(
792
- (item) => record(item) && (item.title === null || typeof item.title === "string") && typeof item.sourceKind === "string" && typeof item.sourceContentId === "string" && typeof item.publicUrl === "string" && nullableMs(item.sourceTimeMs) && typeof item.locator === "string"
917
+ (item) => record2(item) && (item.title === null || typeof item.title === "string") && typeof item.sourceKind === "string" && typeof item.sourceContentId === "string" && typeof item.publicUrl === "string" && nullableMs(item.sourceTimeMs) && typeof item.locator === "string"
793
918
  );
794
919
  }
795
920
  function nullableMs(value) {
796
921
  return value === null || typeof value === "number" && Number.isFinite(value);
797
922
  }
798
923
  function snapshot(value) {
799
- return record(value) && isPositiveInt64(value.versionId) && isPositiveInt64(value.materialVersionId) && typeof value.publishedAtMs === "number" && Number.isFinite(value.publishedAtMs) && typeof value.changeKind === "string" && value.changeKind.trim() !== "" && sourceRefList(value.sourceRefs);
924
+ return record2(value) && isPositiveInt64(value.versionId) && isPositiveInt64(value.materialVersionId) && typeof value.publishedAtMs === "number" && Number.isFinite(value.publishedAtMs) && typeof value.changeKind === "string" && value.changeKind.trim() !== "" && sourceRefList(value.sourceRefs);
800
925
  }
801
926
  function publisher(value) {
802
- return record(value) && (value.__typename === "Person" && typeof value.slug === "string" || value.__typename === "PublicUser" && typeof value.username === "string") && typeof value.id === "string" && value.id.trim() !== "" && typeof value.displayName === "string" && typeof value.avatarUrl === "string" && typeof value.bio === "string" && typeof value.followersCount === "number" && Number.isInteger(value.followersCount) && record(value.viewerState) && (value.viewerState.following === null || typeof value.viewerState.following === "boolean");
927
+ return record2(value) && (value.__typename === "Person" && typeof value.slug === "string" || value.__typename === "PublicUser" && typeof value.username === "string") && typeof value.id === "string" && value.id.trim() !== "" && typeof value.displayName === "string" && typeof value.avatarUrl === "string" && typeof value.bio === "string" && typeof value.followersCount === "number" && Number.isInteger(value.followersCount) && record2(value.viewerState) && (value.viewerState.following === null || typeof value.viewerState.following === "boolean");
803
928
  }
804
929
  function isThesisPublication(value) {
805
- if (!record(value)) return false;
930
+ if (!record2(value)) return false;
806
931
  for (const field of [
807
932
  "thesisId",
808
933
  "playbookId",
@@ -820,7 +945,7 @@ function isThesisPublication(value) {
820
945
  if (!positiveInt64List(value.entityIds) || !positiveInt64List(value.categoryIds) || !stanceList(value.entityStances) || !entityList(value.entities) || !mediaList(value.medias) || !publisher(value.publisher) || !snapshot(value.snapshotRelease)) {
821
946
  return false;
822
947
  }
823
- return value.signalFeed === null || record(value.signalFeed) && isPositiveInt64(value.signalFeed.id);
948
+ return value.signalFeed === null || record2(value.signalFeed) && isPositiveInt64(value.signalFeed.id);
824
949
  }
825
950
 
826
951
  // src/resources/forYou.ts
@@ -853,7 +978,7 @@ query ToolkitForYou($input: FeedEntryConnectionInput) {
853
978
  }
854
979
  `.trim();
855
980
  function validateForYouListParams(params) {
856
- if (!record(params)) throw new Error("For You parameters must be an object");
981
+ if (!record2(params)) throw new Error("For You parameters must be an object");
857
982
  const first = params.first === void 0 ? 20 : params.first;
858
983
  if (typeof first !== "number" || !Number.isInteger(first) || first < 1 || first > 50) {
859
984
  throw new Error("first must be an integer between 1 and 50");
@@ -868,7 +993,7 @@ function validateForYouListParams(params) {
868
993
  throw new Error("feedId must be a positive decimal int64 string");
869
994
  }
870
995
  }
871
- function invalidResponse() {
996
+ function invalidResponse2() {
872
997
  throw new AlvaError(
873
998
  "GRAPHQL_INVALID_RESPONSE",
874
999
  "GraphQL returned an invalid For You connection",
@@ -876,25 +1001,25 @@ function invalidResponse() {
876
1001
  );
877
1002
  }
878
1003
  function connection(value) {
879
- if (!record(value) || !Array.isArray(value.edges) || !record(value.pageInfo)) {
880
- return invalidResponse();
1004
+ if (!record2(value) || !Array.isArray(value.edges) || !record2(value.pageInfo)) {
1005
+ return invalidResponse2();
881
1006
  }
882
1007
  const page = value.pageInfo;
883
1008
  if (typeof page.startCursor !== "string" || typeof page.endCursor !== "string" || typeof page.hasNextPage !== "boolean" || typeof page.hasPreviousPage !== "boolean") {
884
- return invalidResponse();
1009
+ return invalidResponse2();
885
1010
  }
886
1011
  for (const edge of value.edges) {
887
- if (!record(edge) || typeof edge.cursor !== "string" || !edge.cursor.trim() || !record(edge.node))
888
- return invalidResponse();
1012
+ if (!record2(edge) || typeof edge.cursor !== "string" || !edge.cursor.trim() || !record2(edge.node))
1013
+ return invalidResponse2();
889
1014
  const node = edge.node;
890
- if (typeof node.id !== "string" || !node.id.startsWith("FeedEntry:") || !isPositiveInt64(node.id.slice("FeedEntry:".length)) || !record(node.feed) || !isPositiveInt64(node.feed.id) || !record(node.major) || typeof node.major.id !== "string" || !isPositiveInt64(node.major.feedId) || typeof node.title !== "string" || typeof node.body !== "string") {
891
- return invalidResponse();
1015
+ if (typeof node.id !== "string" || !node.id.startsWith("FeedEntry:") || !isPositiveInt64(node.id.slice("FeedEntry:".length)) || !record2(node.feed) || !isPositiveInt64(node.feed.id) || !record2(node.major) || typeof node.major.id !== "string" || !isPositiveInt64(node.major.feedId) || typeof node.title !== "string" || typeof node.body !== "string") {
1016
+ return invalidResponse2();
892
1017
  }
893
1018
  }
894
1019
  const first = value.edges[0];
895
1020
  const last = value.edges[value.edges.length - 1];
896
1021
  if (page.startCursor !== (first?.cursor ?? "") || page.endCursor !== (last?.cursor ?? "") || value.edges.length === 0 && page.hasNextPage)
897
- return invalidResponse();
1022
+ return invalidResponse2();
898
1023
  return value;
899
1024
  }
900
1025
  var THESIS_RECOMMENDATION_QUERY = `
@@ -917,7 +1042,7 @@ query ToolkitForYouTheses($input: ThesisRecommendationInput) {
917
1042
  }
918
1043
  `.trim();
919
1044
  function validateForYouThesesParams(params) {
920
- if (!record(params)) throw new Error("Thesis parameters must be an object");
1045
+ if (!record2(params)) throw new Error("Thesis parameters must be an object");
921
1046
  const first = params.first === void 0 ? 10 : params.first;
922
1047
  if (typeof first !== "number" || !Number.isInteger(first) || first < 1 || first > 10) {
923
1048
  throw new Error("first must be an integer between 1 and 10");
@@ -927,7 +1052,7 @@ function validateForYouThesesParams(params) {
927
1052
  }
928
1053
  }
929
1054
  function graphqlViewerField(response, field) {
930
- if (!record(response)) {
1055
+ if (!record2(response)) {
931
1056
  throw new AlvaError(
932
1057
  "GRAPHQL_INVALID_RESPONSE",
933
1058
  `GraphQL returned an invalid viewer.${field} response`,
@@ -944,7 +1069,7 @@ function graphqlViewerField(response, field) {
944
1069
  }
945
1070
  if (response.errors.length > 0) {
946
1071
  const messages = response.errors.flatMap(
947
- (error) => record(error) && typeof error.message === "string" ? [error.message] : []
1072
+ (error) => record2(error) && typeof error.message === "string" ? [error.message] : []
948
1073
  );
949
1074
  throw new AlvaError(
950
1075
  "GRAPHQL_ERROR",
@@ -954,7 +1079,7 @@ function graphqlViewerField(response, field) {
954
1079
  );
955
1080
  }
956
1081
  }
957
- if (!record(response.data) || !record(response.data.viewer) || response.data.viewer[field] == null) {
1082
+ if (!record2(response.data) || !record2(response.data.viewer) || response.data.viewer[field] == null) {
958
1083
  throw new AlvaError(
959
1084
  "GRAPHQL_EMPTY_RESPONSE",
960
1085
  `GraphQL response did not include viewer.${field}`,
@@ -971,7 +1096,7 @@ function invalidPage() {
971
1096
  );
972
1097
  }
973
1098
  function recommendationPage(value) {
974
- if (!record(value) || !Array.isArray(value.edges) || !record(value.pageInfo) || typeof value.listId !== "string" || typeof value.pageCursor !== "string" || typeof value.exhausted !== "boolean" || typeof value.scanLimited !== "boolean" || value.nextCursor !== null && typeof value.nextCursor !== "string") {
1099
+ if (!record2(value) || !Array.isArray(value.edges) || !record2(value.pageInfo) || typeof value.listId !== "string" || typeof value.pageCursor !== "string" || typeof value.exhausted !== "boolean" || typeof value.scanLimited !== "boolean" || value.nextCursor !== null && typeof value.nextCursor !== "string") {
975
1100
  return invalidPage();
976
1101
  }
977
1102
  const page = value.pageInfo;
@@ -979,7 +1104,7 @@ function recommendationPage(value) {
979
1104
  return invalidPage();
980
1105
  }
981
1106
  for (const edge of value.edges) {
982
- if (!record(edge) || typeof edge.cursor !== "string" || !edge.cursor.trim() || !record(edge.node) || typeof edge.node.itemKey !== "string" || !edge.node.itemKey.trim() || !isThesisPublication(edge.node.publication)) {
1107
+ if (!record2(edge) || typeof edge.cursor !== "string" || !edge.cursor.trim() || !record2(edge.node) || typeof edge.node.itemKey !== "string" || !edge.node.itemKey.trim() || !isThesisPublication(edge.node.publication)) {
983
1108
  return invalidPage();
984
1109
  }
985
1110
  }
@@ -3530,7 +3655,7 @@ var SCHEDULE_NAME = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
3530
3655
  var RFC3339 = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|[+-](\d{2}):(\d{2}))$/i;
3531
3656
  var RFC3339_FRACTIONAL = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(Z|[+-](\d{2}):(\d{2}))$/i;
3532
3657
  var ISO_DURATION = /^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/;
3533
- var GRAPHQL_STATUS_BY_CODE = {
3658
+ var GRAPHQL_STATUS_BY_CODE2 = {
3534
3659
  INVALID_ARGUMENT: 400,
3535
3660
  UNAUTHENTICATED: 401,
3536
3661
  PERMISSION_DENIED: 403,
@@ -3632,7 +3757,7 @@ var SchedulesResource = class {
3632
3757
  const codes = response.errors.map((error) => error.extensions?.code);
3633
3758
  const distinctCodes = new Set(codes);
3634
3759
  const canonicalCode = distinctCodes.size === 1 && typeof codes[0] === "string" ? codes[0] : void 0;
3635
- const canonicalStatus = canonicalCode ? GRAPHQL_STATUS_BY_CODE[canonicalCode] : void 0;
3760
+ const canonicalStatus = canonicalCode ? GRAPHQL_STATUS_BY_CODE2[canonicalCode] : void 0;
3636
3761
  const errorCode = canonicalCode !== void 0 && canonicalStatus !== void 0 ? canonicalCode : "GRAPHQL_ERROR";
3637
3762
  throw new AlvaError(
3638
3763
  errorCode,
@@ -3881,7 +4006,7 @@ function scheduleEmptyResponse() {
3881
4006
  }
3882
4007
 
3883
4008
  // src/resources/steward.ts
3884
- var GRAPHQL_STATUS_BY_CODE2 = {
4009
+ var GRAPHQL_STATUS_BY_CODE3 = {
3885
4010
  INVALID_ARGUMENT: 400,
3886
4011
  UNAUTHENTICATED: 401,
3887
4012
  PERMISSION_DENIED: 403,
@@ -4110,7 +4235,7 @@ var StewardResource = class {
4110
4235
  if (response.errors?.length) {
4111
4236
  const codes = response.errors.map((error) => error.extensions?.code);
4112
4237
  const canonicalCode = new Set(codes).size === 1 && typeof codes[0] === "string" ? codes[0] : void 0;
4113
- const canonicalStatus = canonicalCode ? GRAPHQL_STATUS_BY_CODE2[canonicalCode] : void 0;
4238
+ const canonicalStatus = canonicalCode ? GRAPHQL_STATUS_BY_CODE3[canonicalCode] : void 0;
4114
4239
  throw new AlvaError(
4115
4240
  canonicalCode !== void 0 && canonicalStatus !== void 0 ? canonicalCode : "GRAPHQL_ERROR",
4116
4241
  response.errors.map((error) => error.message).filter((message) => typeof message === "string").join("; ") || "GraphQL request failed",
@@ -4226,12 +4351,12 @@ function dedupe(candidates) {
4226
4351
  });
4227
4352
  }
4228
4353
  function resolveFilters(params) {
4229
- const record2 = isRecord(params) ? params : {};
4354
+ const record3 = isRecord(params) ? params : {};
4230
4355
  return {
4231
- market: typeof record2.market === "string" ? record2.market : void 0,
4232
- instrumentType: typeof record2.instrumentType === "string" ? record2.instrumentType : void 0,
4233
- underlyingType: typeof record2.underlyingType === "string" ? record2.underlyingType : void 0,
4234
- quote: typeof record2.quote === "string" ? record2.quote : void 0
4356
+ market: typeof record3.market === "string" ? record3.market : void 0,
4357
+ instrumentType: typeof record3.instrumentType === "string" ? record3.instrumentType : void 0,
4358
+ underlyingType: typeof record3.underlyingType === "string" ? record3.underlyingType : void 0,
4359
+ quote: typeof record3.quote === "string" ? record3.quote : void 0
4235
4360
  };
4236
4361
  }
4237
4362
  function pairSearchParams(pair, params) {
@@ -4279,11 +4404,11 @@ var TradingPairsResource = class {
4279
4404
  return { query, candidates };
4280
4405
  }
4281
4406
  async resolve(params) {
4282
- const record2 = isRecord(params) ? params : {};
4283
- const pair = typeof record2.pair === "string" ? record2.pair.trim() : void 0;
4407
+ const record3 = isRecord(params) ? params : {};
4408
+ const pair = typeof record3.pair === "string" ? record3.pair.trim() : void 0;
4284
4409
  const filters = resolveFilters(params);
4285
4410
  const searchParams = pair === void 0 ? {
4286
- symbol: typeof record2.symbol === "string" ? record2.symbol : "",
4411
+ symbol: typeof record3.symbol === "string" ? record3.symbol : "",
4287
4412
  ...filters
4288
4413
  } : pairSearchParams(pair, filters);
4289
4414
  const result = await this.search(searchParams);
@@ -4352,7 +4477,7 @@ var SELECTORS = [
4352
4477
  "entityId"
4353
4478
  ];
4354
4479
  function validateThesisPageParams(params) {
4355
- if (!record(params)) throw argument("Thesis parameters must be an object");
4480
+ if (!record2(params)) throw argument("Thesis parameters must be an object");
4356
4481
  const first = params.first === void 0 ? 10 : params.first;
4357
4482
  if (typeof first !== "number" || !Number.isInteger(first) || first < 1 || first > 50) {
4358
4483
  throw argument("first must be an integer between 1 and 50");
@@ -4388,7 +4513,7 @@ async function listTheses(client, params) {
4388
4513
  const entityId = params.ticker === void 0 ? params.entityId : (await resolveUnique(client, ENTITIES_QUERY, params.ticker, "Entity")).id;
4389
4514
  if (entityId !== void 0) {
4390
4515
  const data2 = await graphql(client, ENTITY_QUERY, { id: entityId, input });
4391
- if (!record(data2.market)) throw invalid3();
4516
+ if (!record2(data2.market)) throw invalid3();
4392
4517
  return thesisPage(
4393
4518
  data2.market.scope,
4394
4519
  (node) => isThesisPublication(node) ? { playbookId: node.playbookId, publication: node } : void 0
@@ -4404,7 +4529,7 @@ async function listTheses(client, params) {
4404
4529
  if (!isPositiveInt64(node.id)) return void 0;
4405
4530
  const release = node.latestRelease;
4406
4531
  if (release === null) return { playbookId: node.id, publication: null };
4407
- if (!record(release)) return void 0;
4532
+ if (!record2(release)) return void 0;
4408
4533
  const publication = release.thesisPublication;
4409
4534
  if (publication !== null && !isThesisPublication(publication))
4410
4535
  return void 0;
@@ -4421,9 +4546,9 @@ async function relatedTheses(client, id, params = {}) {
4421
4546
  input: { first: params.first ?? 10, after: params.cursor }
4422
4547
  });
4423
4548
  if (data.node === null) throw notFound();
4424
- if (!record(data.node)) throw invalid3();
4549
+ if (!record2(data.node)) throw invalid3();
4425
4550
  const value = data.node.relatedTheses;
4426
- if (!record(value) || !["GENERATING", "READY", "FAILED"].includes(value.state) || value.nextCursor !== null && !nonempty(value.nextCursor))
4551
+ if (!record2(value) || !["GENERATING", "READY", "FAILED"].includes(value.state) || value.nextCursor !== null && !nonempty(value.nextCursor))
4427
4552
  throw invalid3();
4428
4553
  for (const edge of edges(value)) {
4429
4554
  if (!nonempty(edge.node.itemKey) || typeof edge.node.score !== "number" || !isThesisPublication(edge.node.publication))
@@ -4438,7 +4563,7 @@ async function relatedTheses(client, id, params = {}) {
4438
4563
  function thesisPage(value, readNode) {
4439
4564
  if (value === null) throw notFound();
4440
4565
  const scope = readScope(value);
4441
- if (!record(value) || !record(value.theses)) throw invalid3();
4566
+ if (!record2(value) || !record2(value.theses)) throw invalid3();
4442
4567
  const page = value.theses;
4443
4568
  const items = edges(page).map((edge) => {
4444
4569
  const node = readNode(edge.node);
@@ -4449,11 +4574,11 @@ function thesisPage(value, readNode) {
4449
4574
  }
4450
4575
  async function resolveUnique(client, document, text, kind) {
4451
4576
  const data = await graphql(client, document, { query: text });
4452
- const search = record(data.market) ? data.market.search : data.search;
4453
- if (!record(search) || !Array.isArray(search.edges) || !record(search.pageInfo))
4577
+ const search = record2(data.market) ? data.market.search : data.search;
4578
+ if (!record2(search) || !Array.isArray(search.edges) || !record2(search.pageInfo))
4454
4579
  throw invalid3();
4455
4580
  const candidates = search.edges.map(
4456
- (edge) => readScope(record(edge) ? edge.node : void 0)
4581
+ (edge) => readScope(record2(edge) ? edge.node : void 0)
4457
4582
  );
4458
4583
  if (!candidates.every((c) => c.__typename === kind))
4459
4584
  throw invalid3();
@@ -4470,11 +4595,11 @@ async function resolveUnique(client, document, text, kind) {
4470
4595
  );
4471
4596
  }
4472
4597
  function readScope(value) {
4473
- if (!record(value) || !nonempty(value.id)) throw invalid3();
4598
+ if (!record2(value) || !nonempty(value.id)) throw invalid3();
4474
4599
  if (value.__typename === "Entity") {
4475
4600
  if (!["ticker", "name", "kind", "assetClass"].every(
4476
4601
  (key) => typeof value[key] === "string"
4477
- ) || value.venue !== null && (!record(value.venue) || !nonempty(value.venue.id) || typeof value.venue.name !== "string"))
4602
+ ) || value.venue !== null && (!record2(value.venue) || !nonempty(value.venue.id) || typeof value.venue.name !== "string"))
4478
4603
  throw invalid3();
4479
4604
  } else if (typeof value.displayName !== "string" || typeof value.bio !== "string" || !(value.__typename === "Person" && nonempty(value.slug) || value.__typename === "PublicUser" && nonempty(value.username))) {
4480
4605
  throw invalid3();
@@ -4485,14 +4610,14 @@ function readScope(value) {
4485
4610
  }
4486
4611
  function edges(value) {
4487
4612
  if (!Array.isArray(value.edges) || !value.edges.every(
4488
- (edge) => record(edge) && nonempty(edge.cursor) && record(edge.node)
4613
+ (edge) => record2(edge) && nonempty(edge.cursor) && record2(edge.node)
4489
4614
  ))
4490
4615
  throw invalid3();
4491
4616
  return value.edges;
4492
4617
  }
4493
4618
  function nextCursor(page) {
4494
4619
  const info = page.pageInfo;
4495
- if (!record(info) || typeof info.hasNextPage !== "boolean" || info.hasNextPage && !nonempty(info.endCursor))
4620
+ if (!record2(info) || typeof info.hasNextPage !== "boolean" || info.hasNextPage && !nonempty(info.endCursor))
4496
4621
  throw invalid3();
4497
4622
  return info.hasNextPage ? info.endCursor : null;
4498
4623
  }
@@ -4500,16 +4625,16 @@ async function graphql(client, document, variables) {
4500
4625
  const response = await client._request("POST", "/query", {
4501
4626
  body: { query: document, variables }
4502
4627
  });
4503
- if (!record(response)) throw invalid3();
4628
+ if (!record2(response)) throw invalid3();
4504
4629
  if (response.errors !== void 0) {
4505
4630
  if (!Array.isArray(response.errors)) throw invalid3();
4506
4631
  if (response.errors.length) {
4507
4632
  const messages = response.errors.flatMap(
4508
- (error) => record(error) && typeof error.message === "string" ? [error.message] : []
4633
+ (error) => record2(error) && typeof error.message === "string" ? [error.message] : []
4509
4634
  );
4510
4635
  const codes = new Set(
4511
4636
  response.errors.map(
4512
- (error) => record(error) && record(error.extensions) ? error.extensions.code : void 0
4637
+ (error) => record2(error) && record2(error.extensions) ? error.extensions.code : void 0
4513
4638
  )
4514
4639
  );
4515
4640
  const [code] = codes;
@@ -4522,7 +4647,7 @@ async function graphql(client, document, variables) {
4522
4647
  );
4523
4648
  }
4524
4649
  }
4525
- if (!record(response.data)) throw invalid3();
4650
+ if (!record2(response.data)) throw invalid3();
4526
4651
  return response.data;
4527
4652
  }
4528
4653
  var CANONICAL_STATUS = /* @__PURE__ */ new Map([
@@ -4591,19 +4716,19 @@ var ThesesResource = class {
4591
4716
  const viewer = isRecord2(data) ? data.viewer : void 0;
4592
4717
  const result = isRecord2(viewer) ? viewer.thesisAssetCandidates : void 0;
4593
4718
  if (!isRecord2(result) || !Array.isArray(result.mentions)) {
4594
- throw invalidResponse2("asset candidates response must contain mentions");
4719
+ throw invalidResponse3("asset candidates response must contain mentions");
4595
4720
  }
4596
4721
  const mentions = result.mentions.map((item, index) => {
4597
4722
  if (!isRecord2(item) || typeof item.quote !== "string" || typeof item.ticker !== "string" || !["RESOLVED", "AMBIGUOUS", "UNRESOLVED"].includes(
4598
4723
  item.resolution
4599
4724
  ) || !["UNKNOWN", "BULLISH", "BEARISH"].includes(item.stance) || !Array.isArray(item.candidates)) {
4600
- throw invalidResponse2(`asset mention ${index} is invalid`);
4725
+ throw invalidResponse3(`asset mention ${index} is invalid`);
4601
4726
  }
4602
4727
  const candidates = item.candidates.map((candidate) => {
4603
4728
  if (!isRecord2(candidate) || typeof candidate.ticker !== "string" || typeof candidate.name !== "string" || !["STOCK", "ETF", "CRYPTO", "INDEX"].includes(
4604
4729
  candidate.kind
4605
4730
  )) {
4606
- throw invalidResponse2(`asset mention ${index} has invalid candidate`);
4731
+ throw invalidResponse3(`asset mention ${index} has invalid candidate`);
4607
4732
  }
4608
4733
  return {
4609
4734
  entityId: responseID(
@@ -4643,7 +4768,7 @@ var ThesesResource = class {
4643
4768
  )
4644
4769
  );
4645
4770
  if (response.thesis.id !== thesisID || response.thesis.author_version_id !== versionID) {
4646
- throw invalidResponse2(
4771
+ throw invalidResponse3(
4647
4772
  "version response must match the requested thesis and author version"
4648
4773
  );
4649
4774
  }
@@ -4701,7 +4826,7 @@ var ThesesResource = class {
4701
4826
  )
4702
4827
  );
4703
4828
  if (response.thesis.id !== thesisID || response.thesis.visibility !== visibility) {
4704
- throw invalidResponse2(
4829
+ throw invalidResponse3(
4705
4830
  "visibility response must match the requested thesis and visibility"
4706
4831
  );
4707
4832
  }
@@ -4732,7 +4857,7 @@ var ThesesResource = class {
4732
4857
  );
4733
4858
  if (response === void 0) return {};
4734
4859
  if (!isRecord2(response) || Object.keys(response).length !== 0) {
4735
- throw invalidResponse2("delete response must be {}");
4860
+ throw invalidResponse3("delete response must be {}");
4736
4861
  }
4737
4862
  return {};
4738
4863
  }
@@ -4748,7 +4873,7 @@ var ThesesResource = class {
4748
4873
  }
4749
4874
  );
4750
4875
  if (!isRecord2(response) || typeof response.body !== "string") {
4751
- throw invalidResponse2("rewrite response must contain a string body");
4876
+ throw invalidResponse3("rewrite response must contain a string body");
4752
4877
  }
4753
4878
  return { body: responseBody(response.body) };
4754
4879
  }
@@ -4794,7 +4919,7 @@ function requireEntityStances(value, entityIDs) {
4794
4919
  });
4795
4920
  }
4796
4921
  function updateBody(params) {
4797
- return {
4922
+ const body = {
4798
4923
  request_id: requireRequestID(params.request_id),
4799
4924
  expected_author_version_id: requireID(
4800
4925
  params.expected_author_version_id,
@@ -4806,10 +4931,17 @@ function updateBody(params) {
4806
4931
  visibility: requireVisibility(params.visibility),
4807
4932
  editorial: params.editorial ?? false
4808
4933
  };
4934
+ if (params.entity_stances !== void 0) {
4935
+ body.entity_stances = requireEntityStances(
4936
+ params.entity_stances,
4937
+ body.entity_ids
4938
+ );
4939
+ }
4940
+ return body;
4809
4941
  }
4810
4942
  function thesisResponse(response) {
4811
4943
  if (!isRecord2(response) || !isRecord2(response.thesis)) {
4812
- throw invalidResponse2("response must contain thesis");
4944
+ throw invalidResponse3("response must contain thesis");
4813
4945
  }
4814
4946
  const thesis = response.thesis;
4815
4947
  const value = {
@@ -4833,15 +4965,15 @@ function thesisResponse(response) {
4833
4965
  };
4834
4966
  if (thesis.entity_stances !== void 0) {
4835
4967
  if (!Array.isArray(thesis.entity_stances))
4836
- throw invalidResponse2("thesis.entity_stances must be an array");
4968
+ throw invalidResponse3("thesis.entity_stances must be an array");
4837
4969
  value.entity_stances = thesis.entity_stances.map((item) => {
4838
4970
  if (!isRecord2(item))
4839
- throw invalidResponse2("thesis.entity_stances contains invalid entry");
4971
+ throw invalidResponse3("thesis.entity_stances contains invalid entry");
4840
4972
  const entityID = responseID(item.entity_id, "entity_stances.entity_id");
4841
4973
  if (!value.entity_ids.includes(entityID))
4842
- throw invalidResponse2("entity stance must reference a thesis entity");
4974
+ throw invalidResponse3("entity stance must reference a thesis entity");
4843
4975
  if (!["unknown", "bullish", "bearish"].includes(item.stance))
4844
- throw invalidResponse2("thesis.entity_stances contains invalid stance");
4976
+ throw invalidResponse3("thesis.entity_stances contains invalid stance");
4845
4977
  return { entity_id: entityID, stance: item.stance };
4846
4978
  });
4847
4979
  }
@@ -4850,7 +4982,7 @@ function thesisResponse(response) {
4850
4982
  function thesisGetResponse(response) {
4851
4983
  const base = thesisResponse(response);
4852
4984
  if (!isRecord2(response) || !isRecord2(response.author)) {
4853
- throw invalidResponse2("response must contain author");
4985
+ throw invalidResponse3("response must contain author");
4854
4986
  }
4855
4987
  const authorRecord = response.author;
4856
4988
  const author = {
@@ -4864,14 +4996,14 @@ function thesisGetResponse(response) {
4864
4996
  username: responseText(authorRecord.username, "author.username")
4865
4997
  };
4866
4998
  if (!Array.isArray(response.entities)) {
4867
- throw invalidResponse2("response must contain entities");
4999
+ throw invalidResponse3("response must contain entities");
4868
5000
  }
4869
5001
  if (response.entities.length !== base.thesis.entity_ids.length) {
4870
- throw invalidResponse2("entities must match thesis.entity_ids");
5002
+ throw invalidResponse3("entities must match thesis.entity_ids");
4871
5003
  }
4872
5004
  const entities = response.entities.map((item, index) => {
4873
5005
  if (!isRecord2(item))
4874
- throw invalidResponse2(`entities[${index}] must be an object`);
5006
+ throw invalidResponse3(`entities[${index}] must be an object`);
4875
5007
  const entity = {
4876
5008
  id: responseID(item.id, `entities[${index}].id`),
4877
5009
  ticker: responseText(item.ticker, `entities[${index}].ticker`),
@@ -4880,7 +5012,7 @@ function thesisGetResponse(response) {
4880
5012
  kind: responseText(item.kind, `entities[${index}].kind`)
4881
5013
  };
4882
5014
  if (entity.id !== base.thesis.entity_ids[index]) {
4883
- throw invalidResponse2("entities must preserve thesis.entity_ids order");
5015
+ throw invalidResponse3("entities must preserve thesis.entity_ids order");
4884
5016
  }
4885
5017
  return entity;
4886
5018
  });
@@ -4888,7 +5020,7 @@ function thesisGetResponse(response) {
4888
5020
  }
4889
5021
  function thesisSignalsResponse(response, thesisID, first) {
4890
5022
  if (!isRecord2(response) || !isRecord2(response.research) || !Array.isArray(response.entries) || response.entries.length > first) {
4891
- throw invalidResponse2("invalid Signal history response");
5023
+ throw invalidResponse3("invalid Signal history response");
4892
5024
  }
4893
5025
  const research = response.research;
4894
5026
  const status = {
@@ -4914,13 +5046,13 @@ function thesisSignalsResponse(response, thesisID, first) {
4914
5046
  );
4915
5047
  const entries = response.entries.map((raw) => {
4916
5048
  if (!isRecord2(raw) || !isRecord2(raw.signal) || !isRecord2(raw.signal.source)) {
4917
- throw invalidResponse2("entry must include a Signal and source");
5049
+ throw invalidResponse3("entry must include a Signal and source");
4918
5050
  }
4919
5051
  const signal = raw.signal;
4920
5052
  const source = signal.source;
4921
5053
  const signalID = responseID(signal.thesis_id, "signal.thesis_id");
4922
5054
  if (signalID !== thesisID)
4923
- throw invalidResponse2("Signal belongs to another Thesis");
5055
+ throw invalidResponse3("Signal belongs to another Thesis");
4924
5056
  const cursor = responseNonemptyText(raw.cursor, "entry.cursor");
4925
5057
  const mappedSource = {
4926
5058
  title: responseText(source.title, "source.title")
@@ -4953,7 +5085,7 @@ function thesisSignalsResponse(response, thesisID, first) {
4953
5085
  };
4954
5086
  if (signal.evidence_excerpt !== void 0) {
4955
5087
  if (!isRecord2(signal.evidence_excerpt))
4956
- throw invalidResponse2("invalid evidence excerpt");
5088
+ throw invalidResponse3("invalid evidence excerpt");
4957
5089
  mappedSignal.evidence_excerpt = {
4958
5090
  text: responseNonemptyText(
4959
5091
  signal.evidence_excerpt.text,
@@ -4977,19 +5109,19 @@ function thesisSignalsResponse(response, thesisID, first) {
4977
5109
  });
4978
5110
  const nextCursor2 = responseText(response.next_cursor, "next_cursor");
4979
5111
  if (nextCursor2 && nextCursor2 !== entries[entries.length - 1]?.cursor) {
4980
- throw invalidResponse2("next_cursor must match the final entry");
5112
+ throw invalidResponse3("next_cursor must match the final entry");
4981
5113
  }
4982
5114
  return { research: status, entries, next_cursor: nextCursor2 };
4983
5115
  }
4984
5116
  function responseNonnegativeInteger(value, field) {
4985
5117
  if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
4986
- throw invalidResponse2(field + " must be a nonnegative safe integer");
5118
+ throw invalidResponse3(field + " must be a nonnegative safe integer");
4987
5119
  }
4988
5120
  return value;
4989
5121
  }
4990
5122
  function responseNonemptyText(value, field) {
4991
5123
  const valueText = responseText(value, field);
4992
- if (!valueText.trim()) throw invalidResponse2(field + " must be nonempty");
5124
+ if (!valueText.trim()) throw invalidResponse3(field + " must be nonempty");
4993
5125
  return valueText;
4994
5126
  }
4995
5127
  function requireRequestID(value) {
@@ -5073,20 +5205,20 @@ function requireText2(value, field) {
5073
5205
  }
5074
5206
  function responseID(value, field) {
5075
5207
  if (typeof value === "number") {
5076
- throw invalidResponse2(
5208
+ throw invalidResponse3(
5077
5209
  `${field} must be a decimal string; numeric IDs are unsafe`
5078
5210
  );
5079
5211
  }
5080
5212
  try {
5081
5213
  return requireID(value, field);
5082
5214
  } catch {
5083
- throw invalidResponse2(`${field} must be a positive int64 decimal string`);
5215
+ throw invalidResponse3(`${field} must be a positive int64 decimal string`);
5084
5216
  }
5085
5217
  }
5086
5218
  function responseIDs(value, field) {
5087
- if (!Array.isArray(value)) throw invalidResponse2(`${field} must be an array`);
5219
+ if (!Array.isArray(value)) throw invalidResponse3(`${field} must be an array`);
5088
5220
  if (value.length > MAX_ENTITY_IDS) {
5089
- throw invalidResponse2(
5221
+ throw invalidResponse3(
5090
5222
  `${field} must contain at most ${MAX_ENTITY_IDS} IDs`
5091
5223
  );
5092
5224
  }
@@ -5096,33 +5228,33 @@ function responseText(value, field) {
5096
5228
  try {
5097
5229
  return requireText2(value, field);
5098
5230
  } catch {
5099
- throw invalidResponse2(`${field} must be valid text`);
5231
+ throw invalidResponse3(`${field} must be valid text`);
5100
5232
  }
5101
5233
  }
5102
5234
  function responseClosingNote(value) {
5103
5235
  try {
5104
5236
  return requireText2(value, "note");
5105
5237
  } catch {
5106
- throw invalidResponse2("thesis.closing_note must be valid text");
5238
+ throw invalidResponse3("thesis.closing_note must be valid text");
5107
5239
  }
5108
5240
  }
5109
5241
  function responseBoolean(value, field) {
5110
5242
  if (typeof value !== "boolean")
5111
- throw invalidResponse2(`${field} must be boolean`);
5243
+ throw invalidResponse3(`${field} must be boolean`);
5112
5244
  return value;
5113
5245
  }
5114
5246
  function responseVisibility(value) {
5115
5247
  try {
5116
5248
  return requireVisibility(value);
5117
5249
  } catch {
5118
- throw invalidResponse2("thesis.visibility must be public or private");
5250
+ throw invalidResponse3("thesis.visibility must be public or private");
5119
5251
  }
5120
5252
  }
5121
5253
  function responseBody(value) {
5122
5254
  try {
5123
5255
  return requireBody(value);
5124
5256
  } catch {
5125
- throw invalidResponse2(
5257
+ throw invalidResponse3(
5126
5258
  "response body must be nonblank valid text at most 65536 UTF-8 bytes"
5127
5259
  );
5128
5260
  }
@@ -5131,7 +5263,7 @@ function responseTitle(value) {
5131
5263
  try {
5132
5264
  return requireTitle(value);
5133
5265
  } catch {
5134
- throw invalidResponse2(
5266
+ throw invalidResponse3(
5135
5267
  "response title must be valid text at most 500 UTF-8 bytes"
5136
5268
  );
5137
5269
  }
@@ -5142,7 +5274,7 @@ function isRecord2(value) {
5142
5274
  function invalidArgument(message) {
5143
5275
  return new AlvaError("INVALID_ARGUMENT", message, 400);
5144
5276
  }
5145
- function invalidResponse2(message) {
5277
+ function invalidResponse3(message) {
5146
5278
  return new AlvaError("INVALID_RESPONSE", message, 502);
5147
5279
  }
5148
5280
 
@@ -5152,7 +5284,7 @@ var CREATE_COMPANY_NEWS = `mutation CreateCompanyNews($input: CompanyNewsFeedInp
5152
5284
  createFeed(input: $input) { id producer { id } }
5153
5285
  }
5154
5286
  }`;
5155
- var GRAPHQL_STATUS_BY_CODE3 = {
5287
+ var GRAPHQL_STATUS_BY_CODE4 = {
5156
5288
  INVALID_ARGUMENT: 400,
5157
5289
  UNAUTHENTICATED: 401,
5158
5290
  PERMISSION_DENIED: 403,
@@ -5221,7 +5353,7 @@ var CompanyNewsResource = class {
5221
5353
  throw new AlvaError(
5222
5354
  code,
5223
5355
  graph.errors.map((error) => error?.message).filter(Boolean).join("; ") || "Company News creation failed",
5224
- GRAPHQL_STATUS_BY_CODE3[code] ?? 502,
5356
+ GRAPHQL_STATUS_BY_CODE4[code] ?? 502,
5225
5357
  { errors: graph.errors }
5226
5358
  );
5227
5359
  }
@@ -5329,6 +5461,7 @@ var AlvaClient = class {
5329
5461
  _serviceAccount;
5330
5462
  _release;
5331
5463
  _feed;
5464
+ _x;
5332
5465
  _forYou;
5333
5466
  _automation;
5334
5467
  _playbooks;
@@ -5479,6 +5612,9 @@ var AlvaClient = class {
5479
5612
  );
5480
5613
  }
5481
5614
  }
5615
+ get x() {
5616
+ return this._x ??= new XResource(this);
5617
+ }
5482
5618
  get forYou() {
5483
5619
  return this._forYou ??= new ForYouResource(this);
5484
5620
  }
@@ -6086,7 +6222,7 @@ var installPlaybookRuntime = (targetWindow) => {
6086
6222
  };
6087
6223
 
6088
6224
  // src/index.ts
6089
- var VERSION = true ? "0.32.0" : "dev";
6225
+ var VERSION = true ? "0.33.1-beta.0" : "dev";
6090
6226
  // Annotate the CommonJS export names for ESM import in node:
6091
6227
  0 && (module.exports = {
6092
6228
  AlertsResource,
@@ -6124,6 +6260,7 @@ var VERSION = true ? "0.32.0" : "dev";
6124
6260
  UdfTokenInvalidError,
6125
6261
  UdfTransportError,
6126
6262
  VERSION,
6263
+ XResource,
6127
6264
  getViewerToken,
6128
6265
  installPlaybookRuntime,
6129
6266
  mapUdfErrorResponse,