@alva-ai/toolkit 0.35.0 → 0.36.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
@@ -5249,6 +5249,91 @@ function nextCursor(page) {
5249
5249
  throw invalid3();
5250
5250
  return info.hasNextPage ? info.endCursor : null;
5251
5251
  }
5252
+ var RETURN_QUERY = `query ToolkitThesisReturn($id: ID!, $detail: Boolean!) {
5253
+ node(id: $id) { ... on Playbook { latestRelease { thesisPublication {
5254
+ returnTracking
5255
+ thesisReturn(detail: $detail) {
5256
+ live value firstFillAtMs pricedAtMs stale computedAtMs
5257
+ updates { authorVersionId publishedAtMs startValue updateReturn
5258
+ positions { entityId symbol direction startPrice startAtMs endPrice endAtMs end positionReturn currency } }
5259
+ }
5260
+ } } } }
5261
+ }`;
5262
+ async function thesisReturn(client, id, params = {}) {
5263
+ if (!isPositiveInt64(id))
5264
+ throw argument("id must be an internal positive int64 Playbook ID");
5265
+ if (params.detail !== void 0 && typeof params.detail !== "boolean")
5266
+ throw argument("detail must be a boolean");
5267
+ client._requireAuth();
5268
+ const data = await graphql(client, RETURN_QUERY, {
5269
+ id: `Playbook:${id}`,
5270
+ detail: params.detail ?? false
5271
+ });
5272
+ if (data.node === null) throw notFound();
5273
+ if (!record2(data.node)) throw invalid3();
5274
+ const release = data.node.latestRelease;
5275
+ if (release === null || release === void 0)
5276
+ return { playbookId: id, returnTracking: null, thesisReturn: null };
5277
+ if (!record2(release)) throw invalid3();
5278
+ const publication = release.thesisPublication;
5279
+ if (publication === null)
5280
+ return { playbookId: id, returnTracking: null, thesisReturn: null };
5281
+ if (!record2(publication)) throw invalid3();
5282
+ const tracking = publication.returnTracking;
5283
+ if (tracking !== null && !["UNSET", "ON", "OFF"].includes(tracking))
5284
+ throw invalid3();
5285
+ const value = publication.thesisReturn;
5286
+ if (value !== null && !validThesisReturn(value)) throw invalid3();
5287
+ return {
5288
+ playbookId: id,
5289
+ returnTracking: tracking,
5290
+ thesisReturn: value
5291
+ };
5292
+ }
5293
+ var WATCHPOINTS_QUERY = `query ToolkitThesisWatchpoints($id: ID!) {
5294
+ node(id: $id) { ... on Playbook { latestRelease { thesisPublication {
5295
+ watchpoints { label text origin }
5296
+ } } } }
5297
+ }`;
5298
+ async function thesisWatchpoints(client, id) {
5299
+ if (!isPositiveInt64(id))
5300
+ throw argument("id must be an internal positive int64 Playbook ID");
5301
+ client._requireAuth();
5302
+ const data = await graphql(client, WATCHPOINTS_QUERY, {
5303
+ id: `Playbook:${id}`
5304
+ });
5305
+ if (data.node === null) throw notFound();
5306
+ if (!record2(data.node)) throw invalid3();
5307
+ const release = data.node.latestRelease;
5308
+ if (release === null || release === void 0)
5309
+ return { playbookId: id, watchpoints: [] };
5310
+ if (!record2(release)) throw invalid3();
5311
+ const publication = release.thesisPublication;
5312
+ if (publication === null) return { playbookId: id, watchpoints: [] };
5313
+ if (!record2(publication) || !Array.isArray(publication.watchpoints))
5314
+ throw invalid3();
5315
+ const watchpoints = publication.watchpoints.map((item) => {
5316
+ if (!record2(item) || !nonempty(item.label) || !nonempty(item.text) || !["stated", "inferred", "agent"].includes(item.origin))
5317
+ throw invalid3();
5318
+ return {
5319
+ label: item.label,
5320
+ text: item.text,
5321
+ origin: item.origin
5322
+ };
5323
+ });
5324
+ return { playbookId: id, watchpoints };
5325
+ }
5326
+ function validThesisReturn(value) {
5327
+ if (!record2(value) || typeof value.live !== "boolean" || typeof value.value !== "number" || typeof value.stale !== "boolean" || typeof value.computedAtMs !== "number" || !Array.isArray(value.updates))
5328
+ return false;
5329
+ return value.updates.every(
5330
+ (update) => record2(update) && nonempty(update.authorVersionId) && typeof update.publishedAtMs === "number" && typeof update.startValue === "number" && Array.isArray(update.positions) && update.positions.every(
5331
+ (position) => record2(position) && nonempty(position.symbol) && ["BULLISH", "BEARISH"].includes(position.direction) && ["PENDING", "NEXT_UPDATE", "LATEST"].includes(
5332
+ position.end
5333
+ ) && typeof position.positionReturn === "number"
5334
+ )
5335
+ );
5336
+ }
5252
5337
  async function graphql(client, document, variables) {
5253
5338
  const response = await client._request("POST", "/query", {
5254
5339
  body: { query: document, variables }
@@ -5334,17 +5419,29 @@ var ThesesResource = class {
5334
5419
  related(id, params = {}) {
5335
5420
  return relatedTheses(this.client, id, params);
5336
5421
  }
5337
- async assetCandidates(text) {
5422
+ async assetCandidates(text, params = {}) {
5338
5423
  this.client._requireAuth();
5339
5424
  const source = requireBody(text);
5425
+ const input = {
5426
+ text: source,
5427
+ selectedEntityIds: requireIDs(params.entityIds ?? [], "entityIds")
5428
+ };
5429
+ if (params.thesisId !== void 0)
5430
+ input.thesisId = requireID(params.thesisId, "thesisId");
5431
+ if (params.discover !== void 0) {
5432
+ if (typeof params.discover !== "boolean")
5433
+ throw invalidArgument("discover must be a boolean");
5434
+ input.discover = params.discover;
5435
+ }
5340
5436
  const response = await this.client._request("POST", "/query", {
5341
5437
  body: {
5342
5438
  query: `query ThesisAssetCandidates($input: ThesisAssetCandidatesInput!) {
5343
5439
  viewer { thesisAssetCandidates(input: $input) {
5344
5440
  mentions { quote ticker resolution stance candidates { entityId ticker name kind } }
5441
+ returnPreview { tracking askSwitch directions { entityId direction } keptEntityIds lastLegRemoved }
5345
5442
  } }
5346
5443
  }`,
5347
- variables: { input: { text: source } }
5444
+ variables: { input }
5348
5445
  }
5349
5446
  });
5350
5447
  if (Array.isArray(response?.errors) && response.errors.length > 0) {
@@ -5382,7 +5479,18 @@ var ThesesResource = class {
5382
5479
  });
5383
5480
  return { ...item, candidates };
5384
5481
  });
5385
- return { mentions };
5482
+ return { mentions, returnPreview: returnPreview(result.returnPreview) };
5483
+ }
5484
+ /** The Thesis return of a Playbook's current publication; detail revalues it. */
5485
+ thesisReturn(id, params = {}) {
5486
+ return thesisReturn(this.client, id, params);
5487
+ }
5488
+ /**
5489
+ * List every watchpoint of a Thesis (its current set, in number order): the
5490
+ * things the Thesis depends on, or that would show it right or wrong.
5491
+ */
5492
+ watchpoints(id) {
5493
+ return thesisWatchpoints(this.client, id);
5386
5494
  }
5387
5495
  async create(params) {
5388
5496
  this.client._requireAuth();
@@ -5444,30 +5552,6 @@ var ThesesResource = class {
5444
5552
  throw invalidResponse4("Update list must match requested Thesis");
5445
5553
  return { versions, next_cursor: response.next_cursor };
5446
5554
  }
5447
- /** Deletes only the selected Update; never falls back to whole-Thesis deletion. */
5448
- async deleteUpdate(id, authorVersionID, expectedAuthorVersionID) {
5449
- this.client._requireAuth();
5450
- const thesisID = requireID(id, "id");
5451
- const versionID = requireID(authorVersionID, "author_version_id");
5452
- const expected = requireID(
5453
- expectedAuthorVersionID,
5454
- "expected_author_version_id"
5455
- );
5456
- const response = thesisResponse(
5457
- await this.client._request(
5458
- "DELETE",
5459
- `/api/v1/theses/${thesisID}/versions/${versionID}`,
5460
- {
5461
- query: { expected_author_version_id: expected }
5462
- }
5463
- )
5464
- );
5465
- if (response.thesis.id !== thesisID)
5466
- throw invalidResponse4(
5467
- "delete Update response must match requested Thesis"
5468
- );
5469
- return response;
5470
- }
5471
5555
  async signals(id, params = {}) {
5472
5556
  this.client._requireAuth();
5473
5557
  const first = params.first ?? 20;
@@ -5643,35 +5727,6 @@ var ThesesResource = class {
5643
5727
  }
5644
5728
  return response;
5645
5729
  }
5646
- async close(id, params) {
5647
- this.client._requireAuth();
5648
- const body = {
5649
- expected_author_version_id: requireID(
5650
- params.expected_author_version_id,
5651
- "expected_author_version_id"
5652
- )
5653
- };
5654
- if (params.note !== void 0) body.note = requireText2(params.note, "note");
5655
- return thesisResponse(
5656
- await this.client._request(
5657
- "POST",
5658
- `/api/v1/theses/${requireID(id, "id")}/close`,
5659
- { body }
5660
- )
5661
- );
5662
- }
5663
- async delete(id) {
5664
- this.client._requireAuth();
5665
- const response = await this.client._request(
5666
- "DELETE",
5667
- `/api/v1/theses/${requireID(id, "id")}`
5668
- );
5669
- if (response === void 0) return {};
5670
- if (!isRecord2(response) || Object.keys(response).length !== 0) {
5671
- throw invalidResponse4("delete response must be {}");
5672
- }
5673
- return {};
5674
- }
5675
5730
  async rewrite(params) {
5676
5731
  this.client._requireAuth();
5677
5732
  const body = requireBody(params.body);
@@ -5714,6 +5769,8 @@ function createBody(params) {
5714
5769
  }
5715
5770
  if (params.tickers !== void 0)
5716
5771
  body.tickers = requireTickers(params.tickers);
5772
+ if (params.track_return !== void 0)
5773
+ body.track_return = requireTrackReturn(params.track_return);
5717
5774
  if (params.entity_stances !== void 0) {
5718
5775
  body.entity_stances = requireEntityStances(
5719
5776
  params.entity_stances,
@@ -5722,6 +5779,52 @@ function createBody(params) {
5722
5779
  }
5723
5780
  return body;
5724
5781
  }
5782
+ var ENTITY_STANCES = [
5783
+ "unknown",
5784
+ "bullish",
5785
+ "bearish",
5786
+ "none"
5787
+ ];
5788
+ var THESIS_DIRECTIONS = [
5789
+ "UNKNOWN",
5790
+ "BULLISH",
5791
+ "BEARISH",
5792
+ "NONE"
5793
+ ];
5794
+ var RETURN_TRACKING = ["UNSET", "ON", "OFF"];
5795
+ function requireTrackReturn(value) {
5796
+ if (typeof value !== "boolean")
5797
+ throw invalidArgument("track_return must be a boolean");
5798
+ return value;
5799
+ }
5800
+ function selectedDirections(value, field) {
5801
+ if (!Array.isArray(value)) throw invalidResponse4(`${field} must be an array`);
5802
+ return value.map((item, index) => {
5803
+ if (!isRecord2(item) || !THESIS_DIRECTIONS.includes(item.direction))
5804
+ throw invalidResponse4(`${field}[${index}] is invalid`);
5805
+ return {
5806
+ entityId: responseID(item.entityId, `${field}[${index}].entityId`),
5807
+ direction: item.direction
5808
+ };
5809
+ });
5810
+ }
5811
+ function returnPreview(value) {
5812
+ if (value === null || value === void 0) return null;
5813
+ if (!isRecord2(value) || !RETURN_TRACKING.includes(value.tracking) || typeof value.askSwitch !== "boolean" || typeof value.lastLegRemoved !== "boolean" || !Array.isArray(value.keptEntityIds))
5814
+ throw invalidResponse4("returnPreview is invalid");
5815
+ return {
5816
+ tracking: value.tracking,
5817
+ askSwitch: value.askSwitch,
5818
+ directions: selectedDirections(
5819
+ value.directions,
5820
+ "returnPreview.directions"
5821
+ ),
5822
+ keptEntityIds: value.keptEntityIds.map(
5823
+ (id, index) => responseID(id, `returnPreview.keptEntityIds[${index}]`)
5824
+ ),
5825
+ lastLegRemoved: value.lastLegRemoved
5826
+ };
5827
+ }
5725
5828
  function requireEntityStances(value, entityIDs) {
5726
5829
  if (!Array.isArray(value) || value.length > MAX_ENTITY_IDS)
5727
5830
  throw invalidArgument("entity_stances must contain at most 100 values");
@@ -5739,7 +5842,7 @@ function requireEntityStances(value, entityIDs) {
5739
5842
  "entity stance must reference one unique entity_id"
5740
5843
  );
5741
5844
  seen.add(entityID);
5742
- if (!["unknown", "bullish", "bearish"].includes(item.stance))
5845
+ if (!ENTITY_STANCES.includes(item.stance))
5743
5846
  throw invalidArgument(`entity_stances[${index}].stance is invalid`);
5744
5847
  return { entity_id: entityID, stance: item.stance };
5745
5848
  });
@@ -5754,8 +5857,7 @@ function updateBody(params) {
5754
5857
  body: requireBody(params.body),
5755
5858
  title: requireTitle(params.title ?? ""),
5756
5859
  entity_ids: requireIDs(params.entity_ids ?? [], "entity_ids"),
5757
- visibility: requireVisibility(params.visibility),
5758
- editorial: params.editorial ?? false
5860
+ visibility: requireVisibility(params.visibility)
5759
5861
  };
5760
5862
  if (params.medias !== void 0) {
5761
5863
  if (!Array.isArray(params.medias) || params.medias.length > 20)
@@ -5770,6 +5872,8 @@ function updateBody(params) {
5770
5872
  body.archived = params.archived;
5771
5873
  }
5772
5874
  if (params.note !== void 0) body.note = requireText2(params.note, "note");
5875
+ if (params.track_return !== void 0)
5876
+ body.track_return = requireTrackReturn(params.track_return);
5773
5877
  if (params.entity_stances !== void 0) {
5774
5878
  body.entity_stances = requireEntityStances(
5775
5879
  params.entity_stances,
@@ -5832,9 +5936,20 @@ function thesisResponse(response) {
5832
5936
  const entityID = responseID(item.entity_id, "entity_stances.entity_id");
5833
5937
  if (!value.entity_ids.includes(entityID))
5834
5938
  throw invalidResponse4("entity stance must reference a thesis entity");
5835
- if (!["unknown", "bullish", "bearish"].includes(item.stance))
5939
+ if (!ENTITY_STANCES.includes(item.stance))
5836
5940
  throw invalidResponse4("thesis.entity_stances contains invalid stance");
5837
- return { entity_id: entityID, stance: item.stance };
5941
+ const stance = {
5942
+ entity_id: entityID,
5943
+ stance: item.stance
5944
+ };
5945
+ if (item.direction !== void 0) {
5946
+ if (!ENTITY_STANCES.includes(item.direction))
5947
+ throw invalidResponse4(
5948
+ "thesis.entity_stances contains invalid direction"
5949
+ );
5950
+ stance.direction = item.direction;
5951
+ }
5952
+ return stance;
5838
5953
  });
5839
5954
  }
5840
5955
  return { thesis: value };
@@ -6685,7 +6800,8 @@ var AlvaClient = class {
6685
6800
  throw new AlvaError(
6686
6801
  data.error.code ?? "UNKNOWN",
6687
6802
  data.error.message ?? `HTTP ${response.status}`,
6688
- response.status
6803
+ response.status,
6804
+ data.details
6689
6805
  );
6690
6806
  }
6691
6807
  } catch (e) {
@@ -6815,14 +6931,16 @@ var stripPbsvFromUrl = (targetWindow) => {
6815
6931
  };
6816
6932
  var parseBootParams = (targetWindow) => {
6817
6933
  const params = new URLSearchParams(targetWindow.location.search);
6818
- cachedToken = params.get("_pbsv");
6934
+ const boot = targetWindow;
6935
+ cachedToken = boot.__ALVA_PBSV_BOOTSTRAP__ || null;
6936
+ delete boot.__ALVA_PBSV_BOOTSTRAP__;
6937
+ if (params.has("_pbsv")) stripPbsvFromUrl(targetWindow);
6819
6938
  expectedParentOrigin = params.get("parent_origin");
6820
6939
  apiOrigin = (params.get("api_origin") || DEFAULT_API_ORIGIN).replace(
6821
6940
  /\/$/,
6822
6941
  ""
6823
6942
  );
6824
6943
  playbookIdFromBoot = playbookIdFromToken(cachedToken);
6825
- if (cachedToken) stripPbsvFromUrl(targetWindow);
6826
6944
  };
6827
6945
  var safeJson = async (response) => {
6828
6946
  try {
@@ -7148,7 +7266,7 @@ var installPlaybookRuntime = (targetWindow) => {
7148
7266
  };
7149
7267
 
7150
7268
  // src/index.ts
7151
- var VERSION = true ? "0.35.0" : "dev";
7269
+ var VERSION = true ? "0.36.0" : "dev";
7152
7270
  // Annotate the CommonJS export names for ESM import in node:
7153
7271
  0 && (module.exports = {
7154
7272
  AlertsResource,