@alva-ai/toolkit 0.11.2 → 0.12.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
@@ -23,6 +23,8 @@ __export(src_exports, {
23
23
  AlvaClient: () => AlvaClient,
24
24
  AlvaError: () => AlvaError,
25
25
  PBSV_UPDATE_MESSAGE: () => PBSV_UPDATE_MESSAGE,
26
+ SUBSCRIBE_PROPOSE_MESSAGE: () => SUBSCRIBE_PROPOSE_MESSAGE,
27
+ SUBSCRIBE_RESULT_MESSAGE: () => SUBSCRIBE_RESULT_MESSAGE,
26
28
  UDF_AUTH_REQUIRED_MESSAGE: () => UDF_AUTH_REQUIRED_MESSAGE,
27
29
  UDF_CONSENT_REQUEST_MESSAGE: () => UDF_CONSENT_REQUEST_MESSAGE,
28
30
  UDF_CONSENT_RESPONSE_MESSAGE: () => UDF_CONSENT_RESPONSE_MESSAGE,
@@ -45,6 +47,7 @@ __export(src_exports, {
45
47
  installPlaybookRuntime: () => installPlaybookRuntime,
46
48
  mapUdfErrorResponse: () => mapUdfErrorResponse,
47
49
  playbookIdFromToken: () => playbookIdFromToken,
50
+ subscribe: () => subscribe,
48
51
  udf: () => udf
49
52
  });
50
53
  module.exports = __toCommonJS(src_exports);
@@ -415,6 +418,69 @@ var PlaybooksResource = class {
415
418
  this.client = client;
416
419
  }
417
420
  client;
421
+ /**
422
+ * Resolve playbook ids to named, addressable records (mono-meta#584 A2).
423
+ * Visibility-gated server-side; ids the caller cannot see — and DELETED
424
+ * playbook ids — return no row. Treat a missing id as "deleted or not
425
+ * visible" (the subscriptions list's `target_status` distinguishes ghosts).
426
+ * Ids are strings (snowflake-safe). Max 100 per call.
427
+ *
428
+ * Auth is optional (like `trending`): with an API key, private/paid
429
+ * visibility is evaluated for the caller; without one, only public
430
+ * playbooks resolve. The server decides — no client-side gate.
431
+ */
432
+ async getByIds(ids) {
433
+ return await this.client._request("GET", "/api/v1/playbooks", {
434
+ query: { ids: ids.join(",") }
435
+ });
436
+ }
437
+ /**
438
+ * List a user's playbooks by owner username (mono-meta#584 C10).
439
+ * Visibility-gated: another user's private playbooks are omitted.
440
+ * Auth is optional (like `trending`); without credentials only public
441
+ * playbooks are listed.
442
+ */
443
+ async listByOwner(params) {
444
+ const query = { owner: params.owner };
445
+ if (params.limit !== void 0 && params.limit > 0) {
446
+ query.limit = String(params.limit);
447
+ }
448
+ if (params.cursor) query.cursor = params.cursor;
449
+ return await this.client._request("GET", "/api/v1/playbooks", {
450
+ query
451
+ });
452
+ }
453
+ /**
454
+ * Get one playbook by numeric id or by "owner/name" ref. Returns null when
455
+ * not found / not visible / deleted.
456
+ */
457
+ async get(params) {
458
+ if (params.id) {
459
+ const resp = await this.getByIds([params.id]);
460
+ return resp.items[0] ?? null;
461
+ }
462
+ if (params.ref) {
463
+ const slash = params.ref.indexOf("/");
464
+ if (slash <= 0 || slash === params.ref.length - 1) {
465
+ throw new AlvaError(
466
+ "INVALID_ARGUMENT",
467
+ `Invalid ref "${params.ref}". Expected "owner/name".`,
468
+ 400
469
+ );
470
+ }
471
+ const owner = params.ref.slice(0, slash);
472
+ const name = params.ref.slice(slash + 1);
473
+ let cursor;
474
+ for (; ; ) {
475
+ const page = await this.listByOwner({ owner, cursor, limit: 100 });
476
+ const hit = page.items.find((item) => item.name === name);
477
+ if (hit) return hit;
478
+ if (!page.has_next || !page.next_cursor) return null;
479
+ cursor = page.next_cursor;
480
+ }
481
+ }
482
+ throw new AlvaError("INVALID_ARGUMENT", "id or ref is required", 400);
483
+ }
418
484
  /**
419
485
  * List trending / searchable playbooks. Auth is optional: when the client
420
486
  * has an API key it is forwarded, otherwise this uses the public surface.
@@ -1639,6 +1705,36 @@ var SKILL_ENDPOINT_METADATA = [
1639
1705
  required_subscription_tier: "pro",
1640
1706
  access: "pro_only",
1641
1707
  pro_required: true
1708
+ },
1709
+ {
1710
+ skill: "arrays-data-api-equity-fundamentals",
1711
+ file: "company-detail-non-us",
1712
+ method: "GET",
1713
+ path: "/api/v1/stocks/non-us/company/detail",
1714
+ tier: "public",
1715
+ required_subscription_tier: "free",
1716
+ access: "free_and_pro",
1717
+ pro_required: false
1718
+ },
1719
+ {
1720
+ skill: "arrays-data-api-social-feeds",
1721
+ file: "x-search",
1722
+ method: "GET",
1723
+ path: "/api/v1/social-feeds/x/search",
1724
+ tier: "alternative",
1725
+ required_subscription_tier: "pro",
1726
+ access: "pro_only",
1727
+ pro_required: true
1728
+ },
1729
+ {
1730
+ skill: "arrays-data-api-spot-market-price-and-volume",
1731
+ file: "stocks-non-us-kline",
1732
+ method: "GET",
1733
+ path: "/api/v1/stocks/non-us/kline",
1734
+ tier: "public",
1735
+ required_subscription_tier: "free",
1736
+ access: "free_and_pro",
1737
+ pro_required: false
1642
1738
  }
1643
1739
  ];
1644
1740
  function getSkillEndpointMetadata(skill, file) {
@@ -2170,6 +2266,42 @@ var SubscriptionsResource = class {
2170
2266
  query
2171
2267
  });
2172
2268
  }
2269
+ /**
2270
+ * List the playbooks the caller FOLLOWS (the social relation — the UI's
2271
+ * "Subscribed Playbooks"). Rows carry the playbook identity; follows of
2272
+ * deleted playbooks are filtered server-side. Cursor-paginated.
2273
+ */
2274
+ async follows(params = {}) {
2275
+ this.client._requireAuth();
2276
+ const query = {};
2277
+ if (params.limit !== void 0 && params.limit > 0) {
2278
+ query.limit = String(params.limit);
2279
+ }
2280
+ if (params.cursor) query.cursor = params.cursor;
2281
+ return this.client._request("GET", "/api/v1/me/follows", {
2282
+ query
2283
+ });
2284
+ }
2285
+ /**
2286
+ * Disable alerts for many playbook/feed targets BY TARGET ID in one
2287
+ * idempotent call (max 100 ids). Ids are strings (snowflake-safe). Works
2288
+ * for ghost rows (deleted targets) because no name resolution happens —
2289
+ * the per-id results report `INVALID_ID` for malformed entries while
2290
+ * valid ones proceed. Safe to re-run: already-gone rows are no-ops.
2291
+ */
2292
+ async unsubscribeBatch(params) {
2293
+ this.client._requireAuth();
2294
+ return this.client._request(
2295
+ "POST",
2296
+ "/api/v1/subscriptions/unsubscribe-batch",
2297
+ {
2298
+ body: {
2299
+ playbook_ids: params.playbookIds ?? [],
2300
+ feed_ids: params.feedIds ?? []
2301
+ }
2302
+ }
2303
+ );
2304
+ }
2173
2305
  };
2174
2306
 
2175
2307
  // src/resources/channelGroupSubscriptions.ts
@@ -2249,6 +2381,160 @@ var FeedbackResource = class {
2249
2381
  }
2250
2382
  };
2251
2383
 
2384
+ // src/resources/functions.ts
2385
+ var FunctionsResource = class {
2386
+ constructor(client) {
2387
+ this.client = client;
2388
+ }
2389
+ client;
2390
+ async register(params) {
2391
+ this.client._requireAuth();
2392
+ return this.client._request("POST", "/api/v1/service/functions", {
2393
+ body: {
2394
+ playbook_id: params.playbook_id,
2395
+ function_name: params.function_name,
2396
+ entry_script_path: params.entry_script_path,
2397
+ params_schema: params.params_schema,
2398
+ allow_charges: params.allow_charges
2399
+ }
2400
+ });
2401
+ }
2402
+ async list(params) {
2403
+ this.client._requireAuth();
2404
+ return this.client._request("GET", "/api/v1/service/functions", {
2405
+ query: {
2406
+ playbook_id: params.playbook_id
2407
+ }
2408
+ });
2409
+ }
2410
+ async delete(params) {
2411
+ this.client._requireAuth();
2412
+ await this.client._request("DELETE", "/api/v1/service/functions", {
2413
+ query: {
2414
+ playbook_id: params.playbook_id,
2415
+ function_name: params.function_name
2416
+ }
2417
+ });
2418
+ }
2419
+ async invoke(params) {
2420
+ this.client._requireAuth();
2421
+ const response = await this.client._request(
2422
+ "POST",
2423
+ "/api/v1/service/invoke",
2424
+ {
2425
+ body: {
2426
+ playbook_id: params.playbook_id,
2427
+ function_name: params.function_name,
2428
+ parameters_json: JSON.stringify(params.parameters ?? {})
2429
+ }
2430
+ }
2431
+ );
2432
+ return {
2433
+ ...response,
2434
+ result: parseResultPayload(response.result)
2435
+ };
2436
+ }
2437
+ async getAllowance(params) {
2438
+ this.client._requireAuth();
2439
+ const response = await this.client._request(
2440
+ "GET",
2441
+ "/api/v1/service/allowance",
2442
+ {
2443
+ query: {
2444
+ playbook_id: params.playbook_id
2445
+ }
2446
+ }
2447
+ );
2448
+ return normalizeAllowance(response.allowance);
2449
+ }
2450
+ async listAllowances() {
2451
+ this.client._requireAuth();
2452
+ const response = await this.client._request(
2453
+ "GET",
2454
+ "/api/v1/service/allowances"
2455
+ );
2456
+ return {
2457
+ allowances: (response.allowances ?? []).map((allowance) => normalizeAllowance(allowance)).filter(
2458
+ (allowance) => allowance !== null
2459
+ )
2460
+ };
2461
+ }
2462
+ async createAllowance(params) {
2463
+ this.client._requireAuth();
2464
+ const response = await this.client._request(
2465
+ "POST",
2466
+ "/api/v1/service/allowance",
2467
+ {
2468
+ body: {
2469
+ playbook_id: params.playbook_id,
2470
+ amount: params.amount
2471
+ }
2472
+ }
2473
+ );
2474
+ return { allowance: requireAllowance(response.allowance) };
2475
+ }
2476
+ async revokeAllowance(params) {
2477
+ this.client._requireAuth();
2478
+ await this.client._request("DELETE", "/api/v1/service/allowance", {
2479
+ query: {
2480
+ playbook_id: params.playbook_id
2481
+ }
2482
+ });
2483
+ return { ok: true };
2484
+ }
2485
+ };
2486
+ function normalizeAllowance(raw) {
2487
+ if (!raw) return null;
2488
+ const allowance = {
2489
+ id: raw.id ?? "",
2490
+ consumer_uid: raw.consumer_uid ?? raw.consumerUid ?? "",
2491
+ playbook_id: raw.playbook_id ?? raw.playbookId ?? "",
2492
+ amount: raw.amount ?? 0,
2493
+ used: raw.used ?? 0,
2494
+ remaining: raw.remaining ?? 0
2495
+ };
2496
+ const createdAtMs = timestampMs(
2497
+ raw.created_at_ms ?? raw.createdAtMs ?? raw.created_at ?? raw.createdAt
2498
+ );
2499
+ const updatedAtMs = timestampMs(
2500
+ raw.updated_at_ms ?? raw.updatedAtMs ?? raw.updated_at ?? raw.updatedAt
2501
+ );
2502
+ if (createdAtMs !== void 0) allowance.created_at_ms = createdAtMs;
2503
+ if (updatedAtMs !== void 0) allowance.updated_at_ms = updatedAtMs;
2504
+ return allowance;
2505
+ }
2506
+ function requireAllowance(raw) {
2507
+ const allowance = normalizeAllowance(raw);
2508
+ if (!allowance) {
2509
+ throw new Error("allowance response missing allowance");
2510
+ }
2511
+ return allowance;
2512
+ }
2513
+ function timestampMs(value) {
2514
+ if (value === void 0 || value === null) return void 0;
2515
+ if (typeof value === "number" && Number.isFinite(value)) return value;
2516
+ if (typeof value === "string") {
2517
+ const numeric = Number(value);
2518
+ if (Number.isFinite(numeric)) return numeric;
2519
+ const parsed = Date.parse(value);
2520
+ return Number.isNaN(parsed) ? void 0 : parsed;
2521
+ }
2522
+ if (typeof value !== "object") return void 0;
2523
+ const ts = value;
2524
+ const seconds = typeof ts.seconds === "number" ? ts.seconds : typeof ts.seconds === "string" ? Number(ts.seconds) : void 0;
2525
+ const nanos = typeof ts.nanos === "number" ? ts.nanos : typeof ts.nanos === "string" ? Number(ts.nanos) : 0;
2526
+ if (seconds === void 0 || !Number.isFinite(seconds)) return void 0;
2527
+ return seconds * 1e3 + Math.floor((Number.isFinite(nanos) ? nanos : 0) / 1e6);
2528
+ }
2529
+ function parseResultPayload(result) {
2530
+ if (typeof result !== "string") return result;
2531
+ try {
2532
+ return JSON.parse(result);
2533
+ } catch {
2534
+ return result;
2535
+ }
2536
+ }
2537
+
2252
2538
  // src/client.ts
2253
2539
  var DEFAULT_BASE_URL = "https://api-llm.prd.alva.ai";
2254
2540
  var DEFAULT_ARRAYS_BASE_URL = "https://data-tools.prd.space.id";
@@ -2283,6 +2569,7 @@ var AlvaClient = class {
2283
2569
  _subscriptions;
2284
2570
  _channelGroupSubscriptions;
2285
2571
  _feedback;
2572
+ _functions;
2286
2573
  constructor(config) {
2287
2574
  this.baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
2288
2575
  this.arraysBaseUrl = config.arraysBaseUrl ?? DEFAULT_ARRAYS_BASE_URL;
@@ -2359,6 +2646,9 @@ var AlvaClient = class {
2359
2646
  get feedback() {
2360
2647
  return this._feedback ??= new FeedbackResource(this);
2361
2648
  }
2649
+ get functions() {
2650
+ return this._functions ??= new FunctionsResource(this);
2651
+ }
2362
2652
  _requireAuth() {
2363
2653
  if (!this.pbsvToken && !this.viewer_token && !this.apiKey) {
2364
2654
  throw new AlvaError(
@@ -2518,6 +2808,8 @@ var PBSV_UPDATE_MESSAGE = "alva:pbsv:update";
2518
2808
  var UDF_CONSENT_REQUEST_MESSAGE = "alva:udf:consent-request";
2519
2809
  var UDF_CONSENT_RESPONSE_MESSAGE = "alva:udf:consent-response";
2520
2810
  var UDF_AUTH_REQUIRED_MESSAGE = "alva:udf:auth-required";
2811
+ var SUBSCRIBE_PROPOSE_MESSAGE = "alva:subscribe:propose";
2812
+ var SUBSCRIBE_RESULT_MESSAGE = "alva:subscribe:result";
2521
2813
  var DEFAULT_API_ORIGIN = "https://api-llm.prd.alva.ai";
2522
2814
  var CONSENT_TIMEOUT_MS = 5 * 60 * 1e3;
2523
2815
  var cachedToken = null;
@@ -2527,6 +2819,13 @@ var playbookIdFromBoot = null;
2527
2819
  var runtimeWindow = null;
2528
2820
  var installed = false;
2529
2821
  var pendingConsents = /* @__PURE__ */ new Map();
2822
+ var pendingProposals = /* @__PURE__ */ new Map();
2823
+ var normalizeProposalResult = (value) => {
2824
+ if (value === "subscribed" || value === "declined" || value === "timeout") {
2825
+ return value;
2826
+ }
2827
+ return "error";
2828
+ };
2530
2829
  var decodeBase64Url = (value) => {
2531
2830
  const normalized = value.replace(/-/g, "+").replace(/_/g, "/");
2532
2831
  const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, "=");
@@ -2574,7 +2873,7 @@ var safeJson = async (response) => {
2574
2873
  return {};
2575
2874
  }
2576
2875
  };
2577
- var parseResultPayload = (result) => {
2876
+ var parseResultPayload2 = (result) => {
2578
2877
  if (typeof result !== "string") return result;
2579
2878
  try {
2580
2879
  return JSON.parse(result);
@@ -2586,7 +2885,7 @@ var normalizeInvokeResponse = (body) => {
2586
2885
  const envelope = body;
2587
2886
  return {
2588
2887
  ...envelope,
2589
- result: parseResultPayload(envelope.result)
2888
+ result: parseResultPayload2(envelope.result)
2590
2889
  };
2591
2890
  };
2592
2891
  var errorMetadata = (body) => body.error?.metadata || body.details?.metadata || {};
@@ -2632,6 +2931,15 @@ var onMessage = (event) => {
2632
2931
  }
2633
2932
  return;
2634
2933
  }
2934
+ if (data.type === SUBSCRIBE_RESULT_MESSAGE) {
2935
+ if (typeof data.request_id !== "string") return;
2936
+ const pending2 = pendingProposals.get(data.request_id);
2937
+ if (!pending2) return;
2938
+ clearTimeout(pending2.timer);
2939
+ pendingProposals.delete(data.request_id);
2940
+ pending2.resolve(normalizeProposalResult(data.status));
2941
+ return;
2942
+ }
2635
2943
  if (data.type !== UDF_CONSENT_RESPONSE_MESSAGE) return;
2636
2944
  if (typeof data.request_id !== "string") return;
2637
2945
  const pending = pendingConsents.get(data.request_id);
@@ -2670,6 +2978,33 @@ var requestConsent = async (playbookId, minAllowance) => {
2670
2978
  );
2671
2979
  });
2672
2980
  };
2981
+ var proposeSubscribe = async (input) => {
2982
+ if (!runtimeWindow || !expectedParentOrigin) return "error";
2983
+ const feedOwner = typeof input?.feedOwner === "string" ? input.feedOwner.trim() : "";
2984
+ const feedName = typeof input?.feedName === "string" ? input.feedName.trim() : "";
2985
+ if (!feedOwner || !feedName) return "error";
2986
+ const targetOrigin = expectedParentOrigin;
2987
+ const requestId = randomRequestId();
2988
+ return new Promise((resolve) => {
2989
+ const timer = setTimeout(() => {
2990
+ pendingProposals.delete(requestId);
2991
+ resolve("timeout");
2992
+ }, CONSENT_TIMEOUT_MS);
2993
+ pendingProposals.set(requestId, { resolve, timer });
2994
+ runtimeWindow?.parent.postMessage(
2995
+ {
2996
+ type: SUBSCRIBE_PROPOSE_MESSAGE,
2997
+ request_id: requestId,
2998
+ playbook_id: playbookIdFromBoot,
2999
+ feed: { owner: feedOwner, name: feedName }
3000
+ },
3001
+ targetOrigin
3002
+ );
3003
+ });
3004
+ };
3005
+ var subscribe = {
3006
+ propose: proposeSubscribe
3007
+ };
2673
3008
  var requestAuthentication = (functionName) => {
2674
3009
  if (!runtimeWindow || !expectedParentOrigin) return;
2675
3010
  runtimeWindow.parent.postMessage(
@@ -2838,17 +3173,20 @@ var installPlaybookRuntime = (targetWindow) => {
2838
3173
  const existing = target.alva || {};
2839
3174
  target.alva = {
2840
3175
  ...existing,
2841
- udf
3176
+ udf,
3177
+ subscribe
2842
3178
  };
2843
3179
  };
2844
3180
 
2845
3181
  // src/index.ts
2846
- var VERSION = true ? "0.11.2" : "dev";
3182
+ var VERSION = true ? "0.12.0" : "dev";
2847
3183
  // Annotate the CommonJS export names for ESM import in node:
2848
3184
  0 && (module.exports = {
2849
3185
  AlvaClient,
2850
3186
  AlvaError,
2851
3187
  PBSV_UPDATE_MESSAGE,
3188
+ SUBSCRIBE_PROPOSE_MESSAGE,
3189
+ SUBSCRIBE_RESULT_MESSAGE,
2852
3190
  UDF_AUTH_REQUIRED_MESSAGE,
2853
3191
  UDF_CONSENT_REQUEST_MESSAGE,
2854
3192
  UDF_CONSENT_RESPONSE_MESSAGE,
@@ -2871,6 +3209,7 @@ var VERSION = true ? "0.11.2" : "dev";
2871
3209
  installPlaybookRuntime,
2872
3210
  mapUdfErrorResponse,
2873
3211
  playbookIdFromToken,
3212
+ subscribe,
2874
3213
  udf
2875
3214
  });
2876
3215
  //# sourceMappingURL=index.cjs.map