@alva-ai/toolkit 0.38.0-beta.0 → 0.38.0-beta.2

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
@@ -1365,6 +1365,20 @@ function graphQLErrorMessage(errors) {
1365
1365
  return messages.length > 0 ? messages.join("; ") : "GraphQL request failed";
1366
1366
  }
1367
1367
 
1368
+ // src/resources/webOrigin.ts
1369
+ function webOriginFromApiBase(apiBaseUrl) {
1370
+ try {
1371
+ const host = new URL(apiBaseUrl).hostname;
1372
+ const match = /^api-llm\.([a-z0-9-]+)\.alva\.ai$/i.exec(host);
1373
+ if (match) {
1374
+ const env = match[1].toLowerCase();
1375
+ return env === "prd" ? "https://alva.ai" : `https://${env}.alva.ai`;
1376
+ }
1377
+ } catch {
1378
+ }
1379
+ return "https://alva.ai";
1380
+ }
1381
+
1368
1382
  // src/resources/playbooks.ts
1369
1383
  var PLAYBOOK_VISIBILITIES = [
1370
1384
  "public",
@@ -1515,18 +1529,6 @@ var PlaybooksResource = class {
1515
1529
  );
1516
1530
  }
1517
1531
  };
1518
- function webOriginFromApiBase(apiBaseUrl) {
1519
- try {
1520
- const host = new URL(apiBaseUrl).hostname;
1521
- const match = /^api-llm\.([a-z0-9-]+)\.alva\.ai$/i.exec(host);
1522
- if (match) {
1523
- const env = match[1].toLowerCase();
1524
- return env === "prd" ? "https://alva.ai" : `https://${env}.alva.ai`;
1525
- }
1526
- } catch {
1527
- }
1528
- return "https://alva.ai";
1529
- }
1530
1532
  function toAgentPlaybook(raw, webOrigin) {
1531
1533
  const username = raw.creator?.name ?? "";
1532
1534
  const name = raw.name ?? "";
@@ -3083,7 +3085,8 @@ var RETURN_QUERY = `query ToolkitThesisReturn($id: ID!, $detail: Boolean!) {
3083
3085
  thesisReturn(detail: $detail) {
3084
3086
  live value firstFillAtMs pricedAtMs stale computedAtMs
3085
3087
  updates { authorVersionId publishedAtMs startValue updateReturn
3086
- positions { entityId symbol direction startPrice startAtMs endPrice endAtMs end positionReturn currency } }
3088
+ positions { entityId symbol direction startPrice startAtMs endPrice endAtMs end positionReturn currency weight } }
3089
+ allocation { holdings { entityId symbol direction weight } cash }
3087
3090
  }
3088
3091
  } } } }
3089
3092
  }`;
@@ -3152,16 +3155,21 @@ async function thesisWatchpoints(client, id) {
3152
3155
  return { playbookId: id, watchpoints };
3153
3156
  }
3154
3157
  function validThesisReturn(value) {
3155
- if (!record2(value) || typeof value.live !== "boolean" || typeof value.value !== "number" || typeof value.stale !== "boolean" || typeof value.computedAtMs !== "number" || !Array.isArray(value.updates))
3158
+ if (!record2(value) || typeof value.live !== "boolean" || typeof value.value !== "number" || typeof value.stale !== "boolean" || typeof value.computedAtMs !== "number" || !Array.isArray(value.updates) || value.allocation !== null && !validAllocation(value.allocation))
3156
3159
  return false;
3157
3160
  return value.updates.every(
3158
3161
  (update) => record2(update) && nonempty(update.authorVersionId) && typeof update.publishedAtMs === "number" && typeof update.startValue === "number" && Array.isArray(update.positions) && update.positions.every(
3159
3162
  (position) => record2(position) && nonempty(position.symbol) && ["BULLISH", "BEARISH"].includes(position.direction) && ["PENDING", "NEXT_UPDATE", "LATEST"].includes(
3160
3163
  position.end
3161
- ) && typeof position.positionReturn === "number"
3164
+ ) && typeof position.positionReturn === "number" && typeof position.weight === "number"
3162
3165
  )
3163
3166
  );
3164
3167
  }
3168
+ function validAllocation(value) {
3169
+ return record2(value) && typeof value.cash === "number" && Array.isArray(value.holdings) && value.holdings.every(
3170
+ (holding) => record2(holding) && nonempty(holding.entityId) && nonempty(holding.symbol) && ["BULLISH", "BEARISH"].includes(holding.direction) && typeof holding.weight === "number"
3171
+ );
3172
+ }
3165
3173
  async function graphql(client, document, variables) {
3166
3174
  const response = await client._request("POST", "/query", {
3167
3175
  body: { query: document, variables }
@@ -3218,6 +3226,13 @@ function notFound() {
3218
3226
  }
3219
3227
 
3220
3228
  // src/resources/theses.ts
3229
+ var THESIS_SIGNAL_KINDS = [
3230
+ "fundamentals",
3231
+ "macro",
3232
+ "technicals",
3233
+ "commentary"
3234
+ ];
3235
+ var THESIS_SIGNAL_MIN_IMPORTANCES = ["all", "notable", "major"];
3221
3236
  var SET_THESIS_STARRED = `mutation SetThesisStarred($input: SetThesisStarredInput!) {
3222
3237
  setThesisStarred(input: $input) {
3223
3238
  thesisId
@@ -3323,9 +3338,12 @@ var ThesesResource = class {
3323
3338
  async create(params) {
3324
3339
  this.client._requireAuth();
3325
3340
  const body = createBody(params);
3326
- return thesisResponse(
3341
+ const response = thesisResponse(
3327
3342
  await this.client._request("POST", "/api/v1/theses", { body })
3328
3343
  );
3344
+ requireKeptWeights(body.entity_stances, response.thesis.entity_stances);
3345
+ const webOrigin = webOriginFromApiBase(this.client.baseUrl);
3346
+ return { ...response, url: `${webOrigin}/thesis/${response.thesis.id}` };
3329
3347
  }
3330
3348
  async get(id) {
3331
3349
  this.client._requireAuth();
@@ -3414,6 +3432,7 @@ var ThesesResource = class {
3414
3432
  if (response.thesis.id !== thesisID) {
3415
3433
  throw invalidResponse3("update response must match the requested thesis");
3416
3434
  }
3435
+ requireKeptWeights(body.entity_stances, response.thesis.entity_stances);
3417
3436
  return response;
3418
3437
  }
3419
3438
  /**
@@ -3448,7 +3467,11 @@ var ThesesResource = class {
3448
3467
  body: current.thesis.body,
3449
3468
  title: current.thesis.title,
3450
3469
  entity_ids: current.thesis.entity_ids,
3451
- entity_stances: current.thesis.entity_stances,
3470
+ // Directions only: archiving keeps the current Update and unarchiving
3471
+ // keeps its shares without restating them.
3472
+ entity_stances: current.thesis.entity_stances?.map(
3473
+ ({ entity_id, stance }) => ({ entity_id, stance })
3474
+ ),
3452
3475
  visibility: current.thesis.visibility,
3453
3476
  archived: params.archived,
3454
3477
  medias: current.thesis.medias,
@@ -3495,6 +3518,45 @@ var ThesesResource = class {
3495
3518
  is_starred: returnedStarred
3496
3519
  };
3497
3520
  }
3521
+ /**
3522
+ * Reads which Signals one of the caller's Theses shows: its setting, or every
3523
+ * kind and `all` when it has none. Without an id, reads the setting the caller
3524
+ * last saved, which prefills a new Thesis. Only the Thesis's author can read it.
3525
+ */
3526
+ async getSignalPreferences(id) {
3527
+ this.client._requireAuth();
3528
+ if (id === void 0) {
3529
+ return signalPreferencesResponse(
3530
+ await this.client._request("GET", "/api/v1/theses/signal-preferences"),
3531
+ null
3532
+ );
3533
+ }
3534
+ const thesisID = requireID(id, "id");
3535
+ return signalPreferencesResponse(
3536
+ await this.client._request(
3537
+ "GET",
3538
+ `/api/v1/theses/${thesisID}/signal-preferences`
3539
+ ),
3540
+ thesisID
3541
+ );
3542
+ }
3543
+ /**
3544
+ * Replaces which Signals one of the caller's Theses shows, for every reader,
3545
+ * and records it as the caller's last choice. Author only.
3546
+ */
3547
+ async setSignalPreferences(id, preferences) {
3548
+ this.client._requireAuth();
3549
+ const thesisID = requireID(id, "id");
3550
+ const input = requireSignalPreferences(preferences);
3551
+ return signalPreferencesResponse(
3552
+ await this.client._request(
3553
+ "PUT",
3554
+ `/api/v1/theses/${thesisID}/signal-preferences`,
3555
+ { body: { signal_preferences: input } }
3556
+ ),
3557
+ thesisID
3558
+ );
3559
+ }
3498
3560
  async setSignalEnabled(id, enabled) {
3499
3561
  this.client._requireAuth();
3500
3562
  const thesisID = requireID(id, "id");
@@ -3601,6 +3663,10 @@ function createBody(params) {
3601
3663
  body.tickers = requireTickers(params.tickers);
3602
3664
  if (params.track_return !== void 0)
3603
3665
  body.track_return = requireTrackReturn(params.track_return);
3666
+ if (params.signal_preferences !== void 0)
3667
+ body.signal_preferences = requireSignalPreferences(
3668
+ params.signal_preferences
3669
+ );
3604
3670
  if (params.entity_stances !== void 0) {
3605
3671
  body.entity_stances = requireEntityStances(
3606
3672
  params.entity_stances,
@@ -3609,6 +3675,43 @@ function createBody(params) {
3609
3675
  }
3610
3676
  return body;
3611
3677
  }
3678
+ function requireSignalPreferences(value) {
3679
+ if (!isRecord(value) || !Array.isArray(value.kinds))
3680
+ throw invalidArgument(
3681
+ "signal_preferences must have kinds and min_importance"
3682
+ );
3683
+ const kinds = [...new Set(value.kinds)];
3684
+ if (kinds.length === 0 || !kinds.every(
3685
+ (kind) => THESIS_SIGNAL_KINDS.includes(kind)
3686
+ ))
3687
+ throw invalidArgument(
3688
+ `signal kinds must be one or more of ${THESIS_SIGNAL_KINDS.join(", ")}`
3689
+ );
3690
+ if (!THESIS_SIGNAL_MIN_IMPORTANCES.includes(
3691
+ value.min_importance
3692
+ ))
3693
+ throw invalidArgument(
3694
+ `min_importance must be ${THESIS_SIGNAL_MIN_IMPORTANCES.join(", ")}`
3695
+ );
3696
+ return {
3697
+ kinds,
3698
+ min_importance: value.min_importance
3699
+ };
3700
+ }
3701
+ function signalPreferencesResponse(value, thesisID) {
3702
+ if (!isRecord(value) || value.thesis_id !== thesisID)
3703
+ throw invalidResponse3(
3704
+ "Signal setting response must name the requested thesis"
3705
+ );
3706
+ try {
3707
+ return {
3708
+ thesis_id: thesisID,
3709
+ preferences: requireSignalPreferences(value.signal_preferences)
3710
+ };
3711
+ } catch {
3712
+ throw invalidResponse3("response contains an unknown Signal setting");
3713
+ }
3714
+ }
3612
3715
  var ENTITY_STANCES = [
3613
3716
  "unknown",
3614
3717
  "bullish",
@@ -3674,9 +3777,44 @@ function requireEntityStances(value, entityIDs) {
3674
3777
  seen.add(entityID);
3675
3778
  if (!ENTITY_STANCES.includes(item.stance))
3676
3779
  throw invalidArgument(`entity_stances[${index}].stance is invalid`);
3677
- return { entity_id: entityID, stance: item.stance };
3780
+ const stance = {
3781
+ entity_id: entityID,
3782
+ stance: item.stance
3783
+ };
3784
+ if (item.weight !== void 0) {
3785
+ if (!validWeight(item.weight))
3786
+ throw invalidArgument(
3787
+ `entity_stances[${index}].weight must be a fraction in (0, 1] with at most four decimals`
3788
+ );
3789
+ if (stance.stance !== "bullish" && stance.stance !== "bearish")
3790
+ throw invalidArgument(
3791
+ `entity_stances[${index}].weight needs a bullish or bearish stance`
3792
+ );
3793
+ stance.weight = item.weight;
3794
+ }
3795
+ return stance;
3678
3796
  });
3679
3797
  }
3798
+ function validWeight(value) {
3799
+ if (typeof value !== "number" || !Number.isFinite(value)) return false;
3800
+ const bps = Math.round(value * 1e4);
3801
+ return bps >= 1 && bps <= 1e4 && Math.abs(value * 1e4 - bps) <= 1e-6;
3802
+ }
3803
+ function requireKeptWeights(sent, returned) {
3804
+ if (!Array.isArray(sent)) return;
3805
+ const weights = /* @__PURE__ */ new Map();
3806
+ for (const item of returned ?? []) {
3807
+ if (item.weight !== void 0) weights.set(item.entity_id, item.weight);
3808
+ }
3809
+ for (const item of sent) {
3810
+ if (item.weight === void 0) continue;
3811
+ const kept = weights.get(item.entity_id);
3812
+ if (kept === void 0 || Math.abs(kept - item.weight) > 1e-9)
3813
+ throw invalidResponse3(
3814
+ "the Update was published without the weights sent: the server predates weights"
3815
+ );
3816
+ }
3817
+ }
3680
3818
  function updateBody(params) {
3681
3819
  const body = {
3682
3820
  request_id: requireRequestID(params.request_id),
@@ -3784,6 +3922,13 @@ function thesisResponse(response) {
3784
3922
  );
3785
3923
  stance.direction = item.direction;
3786
3924
  }
3925
+ if (item.weight !== void 0) {
3926
+ if (!validWeight(item.weight))
3927
+ throw invalidResponse3(
3928
+ "thesis.entity_stances contains invalid weight"
3929
+ );
3930
+ stance.weight = item.weight;
3931
+ }
3787
3932
  return stance;
3788
3933
  });
3789
3934
  }
@@ -4648,13 +4793,12 @@ function followingThesesPage(value) {
4648
4793
  }
4649
4794
  function followingThesisAuthor(value) {
4650
4795
  if (value === null) return null;
4651
- if (!record3(value) || value.kind !== "user" && value.kind !== "person" || typeof value.display_name !== "string" || value.username !== void 0 && typeof value.username !== "string" || value.slug !== void 0 && typeof value.slug !== "string") {
4796
+ if (!record3(value) || value.kind !== "user" || typeof value.username !== "string" || typeof value.display_name !== "string") {
4652
4797
  throw invalidFollowingTheses("invalid following theses author");
4653
4798
  }
4654
4799
  return {
4655
4800
  kind: value.kind,
4656
- ...value.username === void 0 ? {} : { username: value.username },
4657
- ...value.slug === void 0 ? {} : { slug: value.slug },
4801
+ username: value.username,
4658
4802
  display_name: value.display_name
4659
4803
  };
4660
4804
  }
@@ -7330,7 +7474,7 @@ var installPlaybookRuntime = (targetWindow) => {
7330
7474
  };
7331
7475
 
7332
7476
  // src/index.ts
7333
- var VERSION = true ? "0.38.0-beta.0" : "dev";
7477
+ var VERSION = true ? "0.38.0-beta.2" : "dev";
7334
7478
  // Annotate the CommonJS export names for ESM import in node:
7335
7479
  0 && (module.exports = {
7336
7480
  AlertsResource,