@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.js CHANGED
@@ -364,6 +364,69 @@ var PlaybooksResource = class {
364
364
  this.client = client;
365
365
  }
366
366
  client;
367
+ /**
368
+ * Resolve playbook ids to named, addressable records (mono-meta#584 A2).
369
+ * Visibility-gated server-side; ids the caller cannot see — and DELETED
370
+ * playbook ids — return no row. Treat a missing id as "deleted or not
371
+ * visible" (the subscriptions list's `target_status` distinguishes ghosts).
372
+ * Ids are strings (snowflake-safe). Max 100 per call.
373
+ *
374
+ * Auth is optional (like `trending`): with an API key, private/paid
375
+ * visibility is evaluated for the caller; without one, only public
376
+ * playbooks resolve. The server decides — no client-side gate.
377
+ */
378
+ async getByIds(ids) {
379
+ return await this.client._request("GET", "/api/v1/playbooks", {
380
+ query: { ids: ids.join(",") }
381
+ });
382
+ }
383
+ /**
384
+ * List a user's playbooks by owner username (mono-meta#584 C10).
385
+ * Visibility-gated: another user's private playbooks are omitted.
386
+ * Auth is optional (like `trending`); without credentials only public
387
+ * playbooks are listed.
388
+ */
389
+ async listByOwner(params) {
390
+ const query = { owner: params.owner };
391
+ if (params.limit !== void 0 && params.limit > 0) {
392
+ query.limit = String(params.limit);
393
+ }
394
+ if (params.cursor) query.cursor = params.cursor;
395
+ return await this.client._request("GET", "/api/v1/playbooks", {
396
+ query
397
+ });
398
+ }
399
+ /**
400
+ * Get one playbook by numeric id or by "owner/name" ref. Returns null when
401
+ * not found / not visible / deleted.
402
+ */
403
+ async get(params) {
404
+ if (params.id) {
405
+ const resp = await this.getByIds([params.id]);
406
+ return resp.items[0] ?? null;
407
+ }
408
+ if (params.ref) {
409
+ const slash = params.ref.indexOf("/");
410
+ if (slash <= 0 || slash === params.ref.length - 1) {
411
+ throw new AlvaError(
412
+ "INVALID_ARGUMENT",
413
+ `Invalid ref "${params.ref}". Expected "owner/name".`,
414
+ 400
415
+ );
416
+ }
417
+ const owner = params.ref.slice(0, slash);
418
+ const name = params.ref.slice(slash + 1);
419
+ let cursor;
420
+ for (; ; ) {
421
+ const page = await this.listByOwner({ owner, cursor, limit: 100 });
422
+ const hit = page.items.find((item) => item.name === name);
423
+ if (hit) return hit;
424
+ if (!page.has_next || !page.next_cursor) return null;
425
+ cursor = page.next_cursor;
426
+ }
427
+ }
428
+ throw new AlvaError("INVALID_ARGUMENT", "id or ref is required", 400);
429
+ }
367
430
  /**
368
431
  * List trending / searchable playbooks. Auth is optional: when the client
369
432
  * has an API key it is forwarded, otherwise this uses the public surface.
@@ -1588,6 +1651,36 @@ var SKILL_ENDPOINT_METADATA = [
1588
1651
  required_subscription_tier: "pro",
1589
1652
  access: "pro_only",
1590
1653
  pro_required: true
1654
+ },
1655
+ {
1656
+ skill: "arrays-data-api-equity-fundamentals",
1657
+ file: "company-detail-non-us",
1658
+ method: "GET",
1659
+ path: "/api/v1/stocks/non-us/company/detail",
1660
+ tier: "public",
1661
+ required_subscription_tier: "free",
1662
+ access: "free_and_pro",
1663
+ pro_required: false
1664
+ },
1665
+ {
1666
+ skill: "arrays-data-api-social-feeds",
1667
+ file: "x-search",
1668
+ method: "GET",
1669
+ path: "/api/v1/social-feeds/x/search",
1670
+ tier: "alternative",
1671
+ required_subscription_tier: "pro",
1672
+ access: "pro_only",
1673
+ pro_required: true
1674
+ },
1675
+ {
1676
+ skill: "arrays-data-api-spot-market-price-and-volume",
1677
+ file: "stocks-non-us-kline",
1678
+ method: "GET",
1679
+ path: "/api/v1/stocks/non-us/kline",
1680
+ tier: "public",
1681
+ required_subscription_tier: "free",
1682
+ access: "free_and_pro",
1683
+ pro_required: false
1591
1684
  }
1592
1685
  ];
1593
1686
  function getSkillEndpointMetadata(skill, file) {
@@ -2119,6 +2212,42 @@ var SubscriptionsResource = class {
2119
2212
  query
2120
2213
  });
2121
2214
  }
2215
+ /**
2216
+ * List the playbooks the caller FOLLOWS (the social relation — the UI's
2217
+ * "Subscribed Playbooks"). Rows carry the playbook identity; follows of
2218
+ * deleted playbooks are filtered server-side. Cursor-paginated.
2219
+ */
2220
+ async follows(params = {}) {
2221
+ this.client._requireAuth();
2222
+ const query = {};
2223
+ if (params.limit !== void 0 && params.limit > 0) {
2224
+ query.limit = String(params.limit);
2225
+ }
2226
+ if (params.cursor) query.cursor = params.cursor;
2227
+ return this.client._request("GET", "/api/v1/me/follows", {
2228
+ query
2229
+ });
2230
+ }
2231
+ /**
2232
+ * Disable alerts for many playbook/feed targets BY TARGET ID in one
2233
+ * idempotent call (max 100 ids). Ids are strings (snowflake-safe). Works
2234
+ * for ghost rows (deleted targets) because no name resolution happens —
2235
+ * the per-id results report `INVALID_ID` for malformed entries while
2236
+ * valid ones proceed. Safe to re-run: already-gone rows are no-ops.
2237
+ */
2238
+ async unsubscribeBatch(params) {
2239
+ this.client._requireAuth();
2240
+ return this.client._request(
2241
+ "POST",
2242
+ "/api/v1/subscriptions/unsubscribe-batch",
2243
+ {
2244
+ body: {
2245
+ playbook_ids: params.playbookIds ?? [],
2246
+ feed_ids: params.feedIds ?? []
2247
+ }
2248
+ }
2249
+ );
2250
+ }
2122
2251
  };
2123
2252
 
2124
2253
  // src/resources/channelGroupSubscriptions.ts
@@ -2198,6 +2327,160 @@ var FeedbackResource = class {
2198
2327
  }
2199
2328
  };
2200
2329
 
2330
+ // src/resources/functions.ts
2331
+ var FunctionsResource = class {
2332
+ constructor(client) {
2333
+ this.client = client;
2334
+ }
2335
+ client;
2336
+ async register(params) {
2337
+ this.client._requireAuth();
2338
+ return this.client._request("POST", "/api/v1/service/functions", {
2339
+ body: {
2340
+ playbook_id: params.playbook_id,
2341
+ function_name: params.function_name,
2342
+ entry_script_path: params.entry_script_path,
2343
+ params_schema: params.params_schema,
2344
+ allow_charges: params.allow_charges
2345
+ }
2346
+ });
2347
+ }
2348
+ async list(params) {
2349
+ this.client._requireAuth();
2350
+ return this.client._request("GET", "/api/v1/service/functions", {
2351
+ query: {
2352
+ playbook_id: params.playbook_id
2353
+ }
2354
+ });
2355
+ }
2356
+ async delete(params) {
2357
+ this.client._requireAuth();
2358
+ await this.client._request("DELETE", "/api/v1/service/functions", {
2359
+ query: {
2360
+ playbook_id: params.playbook_id,
2361
+ function_name: params.function_name
2362
+ }
2363
+ });
2364
+ }
2365
+ async invoke(params) {
2366
+ this.client._requireAuth();
2367
+ const response = await this.client._request(
2368
+ "POST",
2369
+ "/api/v1/service/invoke",
2370
+ {
2371
+ body: {
2372
+ playbook_id: params.playbook_id,
2373
+ function_name: params.function_name,
2374
+ parameters_json: JSON.stringify(params.parameters ?? {})
2375
+ }
2376
+ }
2377
+ );
2378
+ return {
2379
+ ...response,
2380
+ result: parseResultPayload(response.result)
2381
+ };
2382
+ }
2383
+ async getAllowance(params) {
2384
+ this.client._requireAuth();
2385
+ const response = await this.client._request(
2386
+ "GET",
2387
+ "/api/v1/service/allowance",
2388
+ {
2389
+ query: {
2390
+ playbook_id: params.playbook_id
2391
+ }
2392
+ }
2393
+ );
2394
+ return normalizeAllowance(response.allowance);
2395
+ }
2396
+ async listAllowances() {
2397
+ this.client._requireAuth();
2398
+ const response = await this.client._request(
2399
+ "GET",
2400
+ "/api/v1/service/allowances"
2401
+ );
2402
+ return {
2403
+ allowances: (response.allowances ?? []).map((allowance) => normalizeAllowance(allowance)).filter(
2404
+ (allowance) => allowance !== null
2405
+ )
2406
+ };
2407
+ }
2408
+ async createAllowance(params) {
2409
+ this.client._requireAuth();
2410
+ const response = await this.client._request(
2411
+ "POST",
2412
+ "/api/v1/service/allowance",
2413
+ {
2414
+ body: {
2415
+ playbook_id: params.playbook_id,
2416
+ amount: params.amount
2417
+ }
2418
+ }
2419
+ );
2420
+ return { allowance: requireAllowance(response.allowance) };
2421
+ }
2422
+ async revokeAllowance(params) {
2423
+ this.client._requireAuth();
2424
+ await this.client._request("DELETE", "/api/v1/service/allowance", {
2425
+ query: {
2426
+ playbook_id: params.playbook_id
2427
+ }
2428
+ });
2429
+ return { ok: true };
2430
+ }
2431
+ };
2432
+ function normalizeAllowance(raw) {
2433
+ if (!raw) return null;
2434
+ const allowance = {
2435
+ id: raw.id ?? "",
2436
+ consumer_uid: raw.consumer_uid ?? raw.consumerUid ?? "",
2437
+ playbook_id: raw.playbook_id ?? raw.playbookId ?? "",
2438
+ amount: raw.amount ?? 0,
2439
+ used: raw.used ?? 0,
2440
+ remaining: raw.remaining ?? 0
2441
+ };
2442
+ const createdAtMs = timestampMs(
2443
+ raw.created_at_ms ?? raw.createdAtMs ?? raw.created_at ?? raw.createdAt
2444
+ );
2445
+ const updatedAtMs = timestampMs(
2446
+ raw.updated_at_ms ?? raw.updatedAtMs ?? raw.updated_at ?? raw.updatedAt
2447
+ );
2448
+ if (createdAtMs !== void 0) allowance.created_at_ms = createdAtMs;
2449
+ if (updatedAtMs !== void 0) allowance.updated_at_ms = updatedAtMs;
2450
+ return allowance;
2451
+ }
2452
+ function requireAllowance(raw) {
2453
+ const allowance = normalizeAllowance(raw);
2454
+ if (!allowance) {
2455
+ throw new Error("allowance response missing allowance");
2456
+ }
2457
+ return allowance;
2458
+ }
2459
+ function timestampMs(value) {
2460
+ if (value === void 0 || value === null) return void 0;
2461
+ if (typeof value === "number" && Number.isFinite(value)) return value;
2462
+ if (typeof value === "string") {
2463
+ const numeric = Number(value);
2464
+ if (Number.isFinite(numeric)) return numeric;
2465
+ const parsed = Date.parse(value);
2466
+ return Number.isNaN(parsed) ? void 0 : parsed;
2467
+ }
2468
+ if (typeof value !== "object") return void 0;
2469
+ const ts = value;
2470
+ const seconds = typeof ts.seconds === "number" ? ts.seconds : typeof ts.seconds === "string" ? Number(ts.seconds) : void 0;
2471
+ const nanos = typeof ts.nanos === "number" ? ts.nanos : typeof ts.nanos === "string" ? Number(ts.nanos) : 0;
2472
+ if (seconds === void 0 || !Number.isFinite(seconds)) return void 0;
2473
+ return seconds * 1e3 + Math.floor((Number.isFinite(nanos) ? nanos : 0) / 1e6);
2474
+ }
2475
+ function parseResultPayload(result) {
2476
+ if (typeof result !== "string") return result;
2477
+ try {
2478
+ return JSON.parse(result);
2479
+ } catch {
2480
+ return result;
2481
+ }
2482
+ }
2483
+
2201
2484
  // src/client.ts
2202
2485
  var DEFAULT_BASE_URL = "https://api-llm.prd.alva.ai";
2203
2486
  var DEFAULT_ARRAYS_BASE_URL = "https://data-tools.prd.space.id";
@@ -2232,6 +2515,7 @@ var AlvaClient = class {
2232
2515
  _subscriptions;
2233
2516
  _channelGroupSubscriptions;
2234
2517
  _feedback;
2518
+ _functions;
2235
2519
  constructor(config) {
2236
2520
  this.baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
2237
2521
  this.arraysBaseUrl = config.arraysBaseUrl ?? DEFAULT_ARRAYS_BASE_URL;
@@ -2308,6 +2592,9 @@ var AlvaClient = class {
2308
2592
  get feedback() {
2309
2593
  return this._feedback ??= new FeedbackResource(this);
2310
2594
  }
2595
+ get functions() {
2596
+ return this._functions ??= new FunctionsResource(this);
2597
+ }
2311
2598
  _requireAuth() {
2312
2599
  if (!this.pbsvToken && !this.viewer_token && !this.apiKey) {
2313
2600
  throw new AlvaError(
@@ -2467,6 +2754,8 @@ var PBSV_UPDATE_MESSAGE = "alva:pbsv:update";
2467
2754
  var UDF_CONSENT_REQUEST_MESSAGE = "alva:udf:consent-request";
2468
2755
  var UDF_CONSENT_RESPONSE_MESSAGE = "alva:udf:consent-response";
2469
2756
  var UDF_AUTH_REQUIRED_MESSAGE = "alva:udf:auth-required";
2757
+ var SUBSCRIBE_PROPOSE_MESSAGE = "alva:subscribe:propose";
2758
+ var SUBSCRIBE_RESULT_MESSAGE = "alva:subscribe:result";
2470
2759
  var DEFAULT_API_ORIGIN = "https://api-llm.prd.alva.ai";
2471
2760
  var CONSENT_TIMEOUT_MS = 5 * 60 * 1e3;
2472
2761
  var cachedToken = null;
@@ -2476,6 +2765,13 @@ var playbookIdFromBoot = null;
2476
2765
  var runtimeWindow = null;
2477
2766
  var installed = false;
2478
2767
  var pendingConsents = /* @__PURE__ */ new Map();
2768
+ var pendingProposals = /* @__PURE__ */ new Map();
2769
+ var normalizeProposalResult = (value) => {
2770
+ if (value === "subscribed" || value === "declined" || value === "timeout") {
2771
+ return value;
2772
+ }
2773
+ return "error";
2774
+ };
2479
2775
  var decodeBase64Url = (value) => {
2480
2776
  const normalized = value.replace(/-/g, "+").replace(/_/g, "/");
2481
2777
  const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, "=");
@@ -2523,7 +2819,7 @@ var safeJson = async (response) => {
2523
2819
  return {};
2524
2820
  }
2525
2821
  };
2526
- var parseResultPayload = (result) => {
2822
+ var parseResultPayload2 = (result) => {
2527
2823
  if (typeof result !== "string") return result;
2528
2824
  try {
2529
2825
  return JSON.parse(result);
@@ -2535,7 +2831,7 @@ var normalizeInvokeResponse = (body) => {
2535
2831
  const envelope = body;
2536
2832
  return {
2537
2833
  ...envelope,
2538
- result: parseResultPayload(envelope.result)
2834
+ result: parseResultPayload2(envelope.result)
2539
2835
  };
2540
2836
  };
2541
2837
  var errorMetadata = (body) => body.error?.metadata || body.details?.metadata || {};
@@ -2581,6 +2877,15 @@ var onMessage = (event) => {
2581
2877
  }
2582
2878
  return;
2583
2879
  }
2880
+ if (data.type === SUBSCRIBE_RESULT_MESSAGE) {
2881
+ if (typeof data.request_id !== "string") return;
2882
+ const pending2 = pendingProposals.get(data.request_id);
2883
+ if (!pending2) return;
2884
+ clearTimeout(pending2.timer);
2885
+ pendingProposals.delete(data.request_id);
2886
+ pending2.resolve(normalizeProposalResult(data.status));
2887
+ return;
2888
+ }
2584
2889
  if (data.type !== UDF_CONSENT_RESPONSE_MESSAGE) return;
2585
2890
  if (typeof data.request_id !== "string") return;
2586
2891
  const pending = pendingConsents.get(data.request_id);
@@ -2619,6 +2924,33 @@ var requestConsent = async (playbookId, minAllowance) => {
2619
2924
  );
2620
2925
  });
2621
2926
  };
2927
+ var proposeSubscribe = async (input) => {
2928
+ if (!runtimeWindow || !expectedParentOrigin) return "error";
2929
+ const feedOwner = typeof input?.feedOwner === "string" ? input.feedOwner.trim() : "";
2930
+ const feedName = typeof input?.feedName === "string" ? input.feedName.trim() : "";
2931
+ if (!feedOwner || !feedName) return "error";
2932
+ const targetOrigin = expectedParentOrigin;
2933
+ const requestId = randomRequestId();
2934
+ return new Promise((resolve) => {
2935
+ const timer = setTimeout(() => {
2936
+ pendingProposals.delete(requestId);
2937
+ resolve("timeout");
2938
+ }, CONSENT_TIMEOUT_MS);
2939
+ pendingProposals.set(requestId, { resolve, timer });
2940
+ runtimeWindow?.parent.postMessage(
2941
+ {
2942
+ type: SUBSCRIBE_PROPOSE_MESSAGE,
2943
+ request_id: requestId,
2944
+ playbook_id: playbookIdFromBoot,
2945
+ feed: { owner: feedOwner, name: feedName }
2946
+ },
2947
+ targetOrigin
2948
+ );
2949
+ });
2950
+ };
2951
+ var subscribe = {
2952
+ propose: proposeSubscribe
2953
+ };
2622
2954
  var requestAuthentication = (functionName) => {
2623
2955
  if (!runtimeWindow || !expectedParentOrigin) return;
2624
2956
  runtimeWindow.parent.postMessage(
@@ -2787,16 +3119,19 @@ var installPlaybookRuntime = (targetWindow) => {
2787
3119
  const existing = target.alva || {};
2788
3120
  target.alva = {
2789
3121
  ...existing,
2790
- udf
3122
+ udf,
3123
+ subscribe
2791
3124
  };
2792
3125
  };
2793
3126
 
2794
3127
  // src/index.ts
2795
- var VERSION = true ? "0.11.2" : "dev";
3128
+ var VERSION = true ? "0.12.0" : "dev";
2796
3129
  export {
2797
3130
  AlvaClient,
2798
3131
  AlvaError,
2799
3132
  PBSV_UPDATE_MESSAGE,
3133
+ SUBSCRIBE_PROPOSE_MESSAGE,
3134
+ SUBSCRIBE_RESULT_MESSAGE,
2800
3135
  UDF_AUTH_REQUIRED_MESSAGE,
2801
3136
  UDF_CONSENT_REQUEST_MESSAGE,
2802
3137
  UDF_CONSENT_RESPONSE_MESSAGE,
@@ -2819,6 +3154,7 @@ export {
2819
3154
  installPlaybookRuntime,
2820
3155
  mapUdfErrorResponse,
2821
3156
  playbookIdFromToken,
3157
+ subscribe,
2822
3158
  udf
2823
3159
  };
2824
3160
  //# sourceMappingURL=index.js.map