@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.js CHANGED
@@ -5181,6 +5181,91 @@ function nextCursor(page) {
5181
5181
  throw invalid3();
5182
5182
  return info.hasNextPage ? info.endCursor : null;
5183
5183
  }
5184
+ var RETURN_QUERY = `query ToolkitThesisReturn($id: ID!, $detail: Boolean!) {
5185
+ node(id: $id) { ... on Playbook { latestRelease { thesisPublication {
5186
+ returnTracking
5187
+ thesisReturn(detail: $detail) {
5188
+ live value firstFillAtMs pricedAtMs stale computedAtMs
5189
+ updates { authorVersionId publishedAtMs startValue updateReturn
5190
+ positions { entityId symbol direction startPrice startAtMs endPrice endAtMs end positionReturn currency } }
5191
+ }
5192
+ } } } }
5193
+ }`;
5194
+ async function thesisReturn(client, id, params = {}) {
5195
+ if (!isPositiveInt64(id))
5196
+ throw argument("id must be an internal positive int64 Playbook ID");
5197
+ if (params.detail !== void 0 && typeof params.detail !== "boolean")
5198
+ throw argument("detail must be a boolean");
5199
+ client._requireAuth();
5200
+ const data = await graphql(client, RETURN_QUERY, {
5201
+ id: `Playbook:${id}`,
5202
+ detail: params.detail ?? false
5203
+ });
5204
+ if (data.node === null) throw notFound();
5205
+ if (!record2(data.node)) throw invalid3();
5206
+ const release = data.node.latestRelease;
5207
+ if (release === null || release === void 0)
5208
+ return { playbookId: id, returnTracking: null, thesisReturn: null };
5209
+ if (!record2(release)) throw invalid3();
5210
+ const publication = release.thesisPublication;
5211
+ if (publication === null)
5212
+ return { playbookId: id, returnTracking: null, thesisReturn: null };
5213
+ if (!record2(publication)) throw invalid3();
5214
+ const tracking = publication.returnTracking;
5215
+ if (tracking !== null && !["UNSET", "ON", "OFF"].includes(tracking))
5216
+ throw invalid3();
5217
+ const value = publication.thesisReturn;
5218
+ if (value !== null && !validThesisReturn(value)) throw invalid3();
5219
+ return {
5220
+ playbookId: id,
5221
+ returnTracking: tracking,
5222
+ thesisReturn: value
5223
+ };
5224
+ }
5225
+ var WATCHPOINTS_QUERY = `query ToolkitThesisWatchpoints($id: ID!) {
5226
+ node(id: $id) { ... on Playbook { latestRelease { thesisPublication {
5227
+ watchpoints { label text origin }
5228
+ } } } }
5229
+ }`;
5230
+ async function thesisWatchpoints(client, id) {
5231
+ if (!isPositiveInt64(id))
5232
+ throw argument("id must be an internal positive int64 Playbook ID");
5233
+ client._requireAuth();
5234
+ const data = await graphql(client, WATCHPOINTS_QUERY, {
5235
+ id: `Playbook:${id}`
5236
+ });
5237
+ if (data.node === null) throw notFound();
5238
+ if (!record2(data.node)) throw invalid3();
5239
+ const release = data.node.latestRelease;
5240
+ if (release === null || release === void 0)
5241
+ return { playbookId: id, watchpoints: [] };
5242
+ if (!record2(release)) throw invalid3();
5243
+ const publication = release.thesisPublication;
5244
+ if (publication === null) return { playbookId: id, watchpoints: [] };
5245
+ if (!record2(publication) || !Array.isArray(publication.watchpoints))
5246
+ throw invalid3();
5247
+ const watchpoints = publication.watchpoints.map((item) => {
5248
+ if (!record2(item) || !nonempty(item.label) || !nonempty(item.text) || !["stated", "inferred", "agent"].includes(item.origin))
5249
+ throw invalid3();
5250
+ return {
5251
+ label: item.label,
5252
+ text: item.text,
5253
+ origin: item.origin
5254
+ };
5255
+ });
5256
+ return { playbookId: id, watchpoints };
5257
+ }
5258
+ function validThesisReturn(value) {
5259
+ if (!record2(value) || typeof value.live !== "boolean" || typeof value.value !== "number" || typeof value.stale !== "boolean" || typeof value.computedAtMs !== "number" || !Array.isArray(value.updates))
5260
+ return false;
5261
+ return value.updates.every(
5262
+ (update) => record2(update) && nonempty(update.authorVersionId) && typeof update.publishedAtMs === "number" && typeof update.startValue === "number" && Array.isArray(update.positions) && update.positions.every(
5263
+ (position) => record2(position) && nonempty(position.symbol) && ["BULLISH", "BEARISH"].includes(position.direction) && ["PENDING", "NEXT_UPDATE", "LATEST"].includes(
5264
+ position.end
5265
+ ) && typeof position.positionReturn === "number"
5266
+ )
5267
+ );
5268
+ }
5184
5269
  async function graphql(client, document, variables) {
5185
5270
  const response = await client._request("POST", "/query", {
5186
5271
  body: { query: document, variables }
@@ -5266,17 +5351,29 @@ var ThesesResource = class {
5266
5351
  related(id, params = {}) {
5267
5352
  return relatedTheses(this.client, id, params);
5268
5353
  }
5269
- async assetCandidates(text) {
5354
+ async assetCandidates(text, params = {}) {
5270
5355
  this.client._requireAuth();
5271
5356
  const source = requireBody(text);
5357
+ const input = {
5358
+ text: source,
5359
+ selectedEntityIds: requireIDs(params.entityIds ?? [], "entityIds")
5360
+ };
5361
+ if (params.thesisId !== void 0)
5362
+ input.thesisId = requireID(params.thesisId, "thesisId");
5363
+ if (params.discover !== void 0) {
5364
+ if (typeof params.discover !== "boolean")
5365
+ throw invalidArgument("discover must be a boolean");
5366
+ input.discover = params.discover;
5367
+ }
5272
5368
  const response = await this.client._request("POST", "/query", {
5273
5369
  body: {
5274
5370
  query: `query ThesisAssetCandidates($input: ThesisAssetCandidatesInput!) {
5275
5371
  viewer { thesisAssetCandidates(input: $input) {
5276
5372
  mentions { quote ticker resolution stance candidates { entityId ticker name kind } }
5373
+ returnPreview { tracking askSwitch directions { entityId direction } keptEntityIds lastLegRemoved }
5277
5374
  } }
5278
5375
  }`,
5279
- variables: { input: { text: source } }
5376
+ variables: { input }
5280
5377
  }
5281
5378
  });
5282
5379
  if (Array.isArray(response?.errors) && response.errors.length > 0) {
@@ -5314,7 +5411,18 @@ var ThesesResource = class {
5314
5411
  });
5315
5412
  return { ...item, candidates };
5316
5413
  });
5317
- return { mentions };
5414
+ return { mentions, returnPreview: returnPreview(result.returnPreview) };
5415
+ }
5416
+ /** The Thesis return of a Playbook's current publication; detail revalues it. */
5417
+ thesisReturn(id, params = {}) {
5418
+ return thesisReturn(this.client, id, params);
5419
+ }
5420
+ /**
5421
+ * List every watchpoint of a Thesis (its current set, in number order): the
5422
+ * things the Thesis depends on, or that would show it right or wrong.
5423
+ */
5424
+ watchpoints(id) {
5425
+ return thesisWatchpoints(this.client, id);
5318
5426
  }
5319
5427
  async create(params) {
5320
5428
  this.client._requireAuth();
@@ -5376,30 +5484,6 @@ var ThesesResource = class {
5376
5484
  throw invalidResponse4("Update list must match requested Thesis");
5377
5485
  return { versions, next_cursor: response.next_cursor };
5378
5486
  }
5379
- /** Deletes only the selected Update; never falls back to whole-Thesis deletion. */
5380
- async deleteUpdate(id, authorVersionID, expectedAuthorVersionID) {
5381
- this.client._requireAuth();
5382
- const thesisID = requireID(id, "id");
5383
- const versionID = requireID(authorVersionID, "author_version_id");
5384
- const expected = requireID(
5385
- expectedAuthorVersionID,
5386
- "expected_author_version_id"
5387
- );
5388
- const response = thesisResponse(
5389
- await this.client._request(
5390
- "DELETE",
5391
- `/api/v1/theses/${thesisID}/versions/${versionID}`,
5392
- {
5393
- query: { expected_author_version_id: expected }
5394
- }
5395
- )
5396
- );
5397
- if (response.thesis.id !== thesisID)
5398
- throw invalidResponse4(
5399
- "delete Update response must match requested Thesis"
5400
- );
5401
- return response;
5402
- }
5403
5487
  async signals(id, params = {}) {
5404
5488
  this.client._requireAuth();
5405
5489
  const first = params.first ?? 20;
@@ -5575,35 +5659,6 @@ var ThesesResource = class {
5575
5659
  }
5576
5660
  return response;
5577
5661
  }
5578
- async close(id, params) {
5579
- this.client._requireAuth();
5580
- const body = {
5581
- expected_author_version_id: requireID(
5582
- params.expected_author_version_id,
5583
- "expected_author_version_id"
5584
- )
5585
- };
5586
- if (params.note !== void 0) body.note = requireText2(params.note, "note");
5587
- return thesisResponse(
5588
- await this.client._request(
5589
- "POST",
5590
- `/api/v1/theses/${requireID(id, "id")}/close`,
5591
- { body }
5592
- )
5593
- );
5594
- }
5595
- async delete(id) {
5596
- this.client._requireAuth();
5597
- const response = await this.client._request(
5598
- "DELETE",
5599
- `/api/v1/theses/${requireID(id, "id")}`
5600
- );
5601
- if (response === void 0) return {};
5602
- if (!isRecord2(response) || Object.keys(response).length !== 0) {
5603
- throw invalidResponse4("delete response must be {}");
5604
- }
5605
- return {};
5606
- }
5607
5662
  async rewrite(params) {
5608
5663
  this.client._requireAuth();
5609
5664
  const body = requireBody(params.body);
@@ -5646,6 +5701,8 @@ function createBody(params) {
5646
5701
  }
5647
5702
  if (params.tickers !== void 0)
5648
5703
  body.tickers = requireTickers(params.tickers);
5704
+ if (params.track_return !== void 0)
5705
+ body.track_return = requireTrackReturn(params.track_return);
5649
5706
  if (params.entity_stances !== void 0) {
5650
5707
  body.entity_stances = requireEntityStances(
5651
5708
  params.entity_stances,
@@ -5654,6 +5711,52 @@ function createBody(params) {
5654
5711
  }
5655
5712
  return body;
5656
5713
  }
5714
+ var ENTITY_STANCES = [
5715
+ "unknown",
5716
+ "bullish",
5717
+ "bearish",
5718
+ "none"
5719
+ ];
5720
+ var THESIS_DIRECTIONS = [
5721
+ "UNKNOWN",
5722
+ "BULLISH",
5723
+ "BEARISH",
5724
+ "NONE"
5725
+ ];
5726
+ var RETURN_TRACKING = ["UNSET", "ON", "OFF"];
5727
+ function requireTrackReturn(value) {
5728
+ if (typeof value !== "boolean")
5729
+ throw invalidArgument("track_return must be a boolean");
5730
+ return value;
5731
+ }
5732
+ function selectedDirections(value, field) {
5733
+ if (!Array.isArray(value)) throw invalidResponse4(`${field} must be an array`);
5734
+ return value.map((item, index) => {
5735
+ if (!isRecord2(item) || !THESIS_DIRECTIONS.includes(item.direction))
5736
+ throw invalidResponse4(`${field}[${index}] is invalid`);
5737
+ return {
5738
+ entityId: responseID(item.entityId, `${field}[${index}].entityId`),
5739
+ direction: item.direction
5740
+ };
5741
+ });
5742
+ }
5743
+ function returnPreview(value) {
5744
+ if (value === null || value === void 0) return null;
5745
+ if (!isRecord2(value) || !RETURN_TRACKING.includes(value.tracking) || typeof value.askSwitch !== "boolean" || typeof value.lastLegRemoved !== "boolean" || !Array.isArray(value.keptEntityIds))
5746
+ throw invalidResponse4("returnPreview is invalid");
5747
+ return {
5748
+ tracking: value.tracking,
5749
+ askSwitch: value.askSwitch,
5750
+ directions: selectedDirections(
5751
+ value.directions,
5752
+ "returnPreview.directions"
5753
+ ),
5754
+ keptEntityIds: value.keptEntityIds.map(
5755
+ (id, index) => responseID(id, `returnPreview.keptEntityIds[${index}]`)
5756
+ ),
5757
+ lastLegRemoved: value.lastLegRemoved
5758
+ };
5759
+ }
5657
5760
  function requireEntityStances(value, entityIDs) {
5658
5761
  if (!Array.isArray(value) || value.length > MAX_ENTITY_IDS)
5659
5762
  throw invalidArgument("entity_stances must contain at most 100 values");
@@ -5671,7 +5774,7 @@ function requireEntityStances(value, entityIDs) {
5671
5774
  "entity stance must reference one unique entity_id"
5672
5775
  );
5673
5776
  seen.add(entityID);
5674
- if (!["unknown", "bullish", "bearish"].includes(item.stance))
5777
+ if (!ENTITY_STANCES.includes(item.stance))
5675
5778
  throw invalidArgument(`entity_stances[${index}].stance is invalid`);
5676
5779
  return { entity_id: entityID, stance: item.stance };
5677
5780
  });
@@ -5686,8 +5789,7 @@ function updateBody(params) {
5686
5789
  body: requireBody(params.body),
5687
5790
  title: requireTitle(params.title ?? ""),
5688
5791
  entity_ids: requireIDs(params.entity_ids ?? [], "entity_ids"),
5689
- visibility: requireVisibility(params.visibility),
5690
- editorial: params.editorial ?? false
5792
+ visibility: requireVisibility(params.visibility)
5691
5793
  };
5692
5794
  if (params.medias !== void 0) {
5693
5795
  if (!Array.isArray(params.medias) || params.medias.length > 20)
@@ -5702,6 +5804,8 @@ function updateBody(params) {
5702
5804
  body.archived = params.archived;
5703
5805
  }
5704
5806
  if (params.note !== void 0) body.note = requireText2(params.note, "note");
5807
+ if (params.track_return !== void 0)
5808
+ body.track_return = requireTrackReturn(params.track_return);
5705
5809
  if (params.entity_stances !== void 0) {
5706
5810
  body.entity_stances = requireEntityStances(
5707
5811
  params.entity_stances,
@@ -5764,9 +5868,20 @@ function thesisResponse(response) {
5764
5868
  const entityID = responseID(item.entity_id, "entity_stances.entity_id");
5765
5869
  if (!value.entity_ids.includes(entityID))
5766
5870
  throw invalidResponse4("entity stance must reference a thesis entity");
5767
- if (!["unknown", "bullish", "bearish"].includes(item.stance))
5871
+ if (!ENTITY_STANCES.includes(item.stance))
5768
5872
  throw invalidResponse4("thesis.entity_stances contains invalid stance");
5769
- return { entity_id: entityID, stance: item.stance };
5873
+ const stance = {
5874
+ entity_id: entityID,
5875
+ stance: item.stance
5876
+ };
5877
+ if (item.direction !== void 0) {
5878
+ if (!ENTITY_STANCES.includes(item.direction))
5879
+ throw invalidResponse4(
5880
+ "thesis.entity_stances contains invalid direction"
5881
+ );
5882
+ stance.direction = item.direction;
5883
+ }
5884
+ return stance;
5770
5885
  });
5771
5886
  }
5772
5887
  return { thesis: value };
@@ -6617,7 +6732,8 @@ var AlvaClient = class {
6617
6732
  throw new AlvaError(
6618
6733
  data.error.code ?? "UNKNOWN",
6619
6734
  data.error.message ?? `HTTP ${response.status}`,
6620
- response.status
6735
+ response.status,
6736
+ data.details
6621
6737
  );
6622
6738
  }
6623
6739
  } catch (e) {
@@ -6747,14 +6863,16 @@ var stripPbsvFromUrl = (targetWindow) => {
6747
6863
  };
6748
6864
  var parseBootParams = (targetWindow) => {
6749
6865
  const params = new URLSearchParams(targetWindow.location.search);
6750
- cachedToken = params.get("_pbsv");
6866
+ const boot = targetWindow;
6867
+ cachedToken = boot.__ALVA_PBSV_BOOTSTRAP__ || null;
6868
+ delete boot.__ALVA_PBSV_BOOTSTRAP__;
6869
+ if (params.has("_pbsv")) stripPbsvFromUrl(targetWindow);
6751
6870
  expectedParentOrigin = params.get("parent_origin");
6752
6871
  apiOrigin = (params.get("api_origin") || DEFAULT_API_ORIGIN).replace(
6753
6872
  /\/$/,
6754
6873
  ""
6755
6874
  );
6756
6875
  playbookIdFromBoot = playbookIdFromToken(cachedToken);
6757
- if (cachedToken) stripPbsvFromUrl(targetWindow);
6758
6876
  };
6759
6877
  var safeJson = async (response) => {
6760
6878
  try {
@@ -7080,7 +7198,7 @@ var installPlaybookRuntime = (targetWindow) => {
7080
7198
  };
7081
7199
 
7082
7200
  // src/index.ts
7083
- var VERSION = true ? "0.35.0" : "dev";
7201
+ var VERSION = true ? "0.36.0" : "dev";
7084
7202
  export {
7085
7203
  AlertsResource,
7086
7204
  AlvaClient,