@alva-ai/toolkit 0.7.0 → 0.9.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/README.md +87 -3
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/cli.js +10408 -148
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +619 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +277 -18
- package/dist/index.d.ts +277 -18
- package/dist/index.js +596 -37
- package/dist/index.js.map +1 -1
- package/package.json +10 -1
package/dist/index.js
CHANGED
|
@@ -147,7 +147,8 @@ var RunResource = class {
|
|
|
147
147
|
code: params.code,
|
|
148
148
|
entry_path: params.entry_path,
|
|
149
149
|
working_dir: params.working_dir,
|
|
150
|
-
args: params.args
|
|
150
|
+
args: params.args,
|
|
151
|
+
max_heap_size_mb: params.max_heap_size_mb
|
|
151
152
|
}
|
|
152
153
|
});
|
|
153
154
|
}
|
|
@@ -275,7 +276,8 @@ var ReleaseResource = class {
|
|
|
275
276
|
description: params.description,
|
|
276
277
|
feeds: params.feeds,
|
|
277
278
|
trading_symbols: params.trading_symbols,
|
|
278
|
-
|
|
279
|
+
skill_id: params.skill_id,
|
|
280
|
+
tags: params.tags
|
|
279
281
|
}
|
|
280
282
|
});
|
|
281
283
|
}
|
|
@@ -293,6 +295,86 @@ var ReleaseResource = class {
|
|
|
293
295
|
}
|
|
294
296
|
};
|
|
295
297
|
|
|
298
|
+
// src/resources/feed.ts
|
|
299
|
+
var FeedResource = class {
|
|
300
|
+
constructor(client) {
|
|
301
|
+
this.client = client;
|
|
302
|
+
}
|
|
303
|
+
client;
|
|
304
|
+
/**
|
|
305
|
+
* Soft-delete a feed and all its active majors. Cascades:
|
|
306
|
+
* - all active feed_majors are soft-deleted in the same transaction
|
|
307
|
+
* - associated producer cronjobs are removed best-effort (the cronjob
|
|
308
|
+
* scavenger reconciles any leftover rows on its next sweep)
|
|
309
|
+
*
|
|
310
|
+
* Auth: caller must own the feed (uid match), enforced by the backend.
|
|
311
|
+
*/
|
|
312
|
+
async delete(params) {
|
|
313
|
+
this.client._requireAuth();
|
|
314
|
+
if (!Number.isInteger(params.id) || params.id <= 0) {
|
|
315
|
+
throw new Error("feed id must be a positive integer");
|
|
316
|
+
}
|
|
317
|
+
return this.client._request(
|
|
318
|
+
"DELETE",
|
|
319
|
+
`/api/v1/feed/${encodeURIComponent(String(params.id))}`
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
// src/resources/playbooks.ts
|
|
325
|
+
var PlaybooksResource = class {
|
|
326
|
+
constructor(client) {
|
|
327
|
+
this.client = client;
|
|
328
|
+
}
|
|
329
|
+
client;
|
|
330
|
+
/**
|
|
331
|
+
* List trending / searchable playbooks. Auth is optional: when the client
|
|
332
|
+
* has an API key it is forwarded, otherwise this uses the public surface.
|
|
333
|
+
*
|
|
334
|
+
* The gateway returns a frontend-oriented preview payload; this SDK projects
|
|
335
|
+
* it into a smaller, agent-friendly shape.
|
|
336
|
+
*/
|
|
337
|
+
async trending(params = {}) {
|
|
338
|
+
const query = {};
|
|
339
|
+
if (params.cursor) query.cursor = params.cursor;
|
|
340
|
+
if (params.current) query.current = params.current;
|
|
341
|
+
if (params.limit !== void 0) query.limit = params.limit;
|
|
342
|
+
if (params.dir) query.dir = params.dir;
|
|
343
|
+
if (params.sort) query.sort = params.sort;
|
|
344
|
+
if (params.keyword) query.keyword = params.keyword;
|
|
345
|
+
if (params.tags && params.tags.length > 0) {
|
|
346
|
+
query.tags = params.tags.join(",");
|
|
347
|
+
}
|
|
348
|
+
const raw = await this.client._request(
|
|
349
|
+
"GET",
|
|
350
|
+
"/api/v1/playbooks/trending",
|
|
351
|
+
{ query }
|
|
352
|
+
);
|
|
353
|
+
return {
|
|
354
|
+
playbooks: (raw.playbooks ?? []).map(toAgentPlaybook),
|
|
355
|
+
has_next: raw.has_next ?? false
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
function toAgentPlaybook(raw) {
|
|
360
|
+
const username = raw.creator?.name ?? "";
|
|
361
|
+
const name = raw.name ?? "";
|
|
362
|
+
const ref = username && name ? `${username}/${name}` : name;
|
|
363
|
+
return {
|
|
364
|
+
id: raw.id === void 0 ? "" : String(raw.id),
|
|
365
|
+
ref,
|
|
366
|
+
username,
|
|
367
|
+
name,
|
|
368
|
+
display_name: raw.display_name ?? "",
|
|
369
|
+
description: raw.description ?? "",
|
|
370
|
+
tags: raw.tags ?? [],
|
|
371
|
+
follow_count: raw.follow_count ?? 0,
|
|
372
|
+
url_path: username && name ? `/${username}/playbooks/${name}` : "",
|
|
373
|
+
...raw.readme ? { readme: raw.readme } : {},
|
|
374
|
+
cursor: raw.cursor ?? ""
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
296
378
|
// src/resources/secrets.ts
|
|
297
379
|
var SecretsResource = class {
|
|
298
380
|
constructor(client) {
|
|
@@ -362,6 +444,26 @@ var SdkDocsResource = class {
|
|
|
362
444
|
|
|
363
445
|
// src/resources/skillTiers.ts
|
|
364
446
|
var SKILL_ENDPOINT_METADATA = [
|
|
447
|
+
{
|
|
448
|
+
skill: "arrays-data-api-crypto-futures-data",
|
|
449
|
+
file: "binance-perp-usdt-kline",
|
|
450
|
+
method: "GET",
|
|
451
|
+
path: "/api/v1/crypto/binance/perp/usdt/kline",
|
|
452
|
+
tier: "public",
|
|
453
|
+
required_subscription_tier: "free",
|
|
454
|
+
access: "free_and_pro",
|
|
455
|
+
pro_required: false
|
|
456
|
+
},
|
|
457
|
+
{
|
|
458
|
+
skill: "arrays-data-api-spot-market-price-and-volume",
|
|
459
|
+
file: "binance-spot-usdt-kline",
|
|
460
|
+
method: "GET",
|
|
461
|
+
path: "/api/v1/crypto/binance/spot/usdt/kline",
|
|
462
|
+
tier: "public",
|
|
463
|
+
required_subscription_tier: "free",
|
|
464
|
+
access: "free_and_pro",
|
|
465
|
+
pro_required: false
|
|
466
|
+
},
|
|
365
467
|
{
|
|
366
468
|
skill: "arrays-data-api-spot-market-price-and-volume",
|
|
367
469
|
file: "crypto-detail",
|
|
@@ -393,10 +495,10 @@ var SKILL_ENDPOINT_METADATA = [
|
|
|
393
495
|
pro_required: false
|
|
394
496
|
},
|
|
395
497
|
{
|
|
396
|
-
skill: "arrays-data-api-
|
|
397
|
-
file: "
|
|
498
|
+
skill: "arrays-data-api-crypto-futures-data",
|
|
499
|
+
file: "hyperliquid-perp-usdc-kline",
|
|
398
500
|
method: "GET",
|
|
399
|
-
path: "/api/v1/crypto/
|
|
501
|
+
path: "/api/v1/crypto/hyperliquid/perp/usdc/kline",
|
|
400
502
|
tier: "public",
|
|
401
503
|
required_subscription_tier: "free",
|
|
402
504
|
access: "free_and_pro",
|
|
@@ -404,9 +506,9 @@ var SKILL_ENDPOINT_METADATA = [
|
|
|
404
506
|
},
|
|
405
507
|
{
|
|
406
508
|
skill: "arrays-data-api-spot-market-price-and-volume",
|
|
407
|
-
file: "
|
|
509
|
+
file: "hyperliquid-spot-usdc-kline",
|
|
408
510
|
method: "GET",
|
|
409
|
-
path: "/api/v1/crypto/kline",
|
|
511
|
+
path: "/api/v1/crypto/hyperliquid/spot/usdc/kline",
|
|
410
512
|
tier: "public",
|
|
411
513
|
required_subscription_tier: "free",
|
|
412
514
|
access: "free_and_pro",
|
|
@@ -452,16 +554,6 @@ var SKILL_ENDPOINT_METADATA = [
|
|
|
452
554
|
access: "free_and_pro",
|
|
453
555
|
pro_required: false
|
|
454
556
|
},
|
|
455
|
-
{
|
|
456
|
-
skill: "arrays-data-api-spot-market-price-and-volume",
|
|
457
|
-
file: "crypto-ohlcv",
|
|
458
|
-
method: "GET",
|
|
459
|
-
path: "/api/v1/crypto/ohlcv",
|
|
460
|
-
tier: "public",
|
|
461
|
-
required_subscription_tier: "free",
|
|
462
|
-
access: "free_and_pro",
|
|
463
|
-
pro_required: false
|
|
464
|
-
},
|
|
465
557
|
{
|
|
466
558
|
skill: "arrays-data-api-crypto-futures-data",
|
|
467
559
|
file: "open-interest",
|
|
@@ -1152,16 +1244,6 @@ var SKILL_ENDPOINT_METADATA = [
|
|
|
1152
1244
|
access: "pro_only",
|
|
1153
1245
|
pro_required: true
|
|
1154
1246
|
},
|
|
1155
|
-
{
|
|
1156
|
-
skill: "arrays-data-api-equity-ownership-and-flow",
|
|
1157
|
-
file: "senate-trade",
|
|
1158
|
-
method: "GET",
|
|
1159
|
-
path: "/api/v1/stocks/company/senate-trade",
|
|
1160
|
-
tier: "alternative",
|
|
1161
|
-
required_subscription_tier: "pro",
|
|
1162
|
-
access: "pro_only",
|
|
1163
|
-
pro_required: true
|
|
1164
|
-
},
|
|
1165
1247
|
{
|
|
1166
1248
|
skill: "arrays-data-api-stock-metrics",
|
|
1167
1249
|
file: "darkpool",
|
|
@@ -1401,6 +1483,50 @@ var SKILL_ENDPOINT_METADATA = [
|
|
|
1401
1483
|
required_subscription_tier: "pro",
|
|
1402
1484
|
access: "pro_only",
|
|
1403
1485
|
pro_required: true
|
|
1486
|
+
},
|
|
1487
|
+
{
|
|
1488
|
+
// TODO(audit): verify tier — auto-defaulted to pro
|
|
1489
|
+
skill: "arrays-data-api-social-feeds",
|
|
1490
|
+
file: "x-by-handle",
|
|
1491
|
+
method: "GET",
|
|
1492
|
+
path: "/api/v1/social-feeds/x/by-handle",
|
|
1493
|
+
tier: "alternative",
|
|
1494
|
+
required_subscription_tier: "pro",
|
|
1495
|
+
access: "pro_only",
|
|
1496
|
+
pro_required: true
|
|
1497
|
+
},
|
|
1498
|
+
{
|
|
1499
|
+
// TODO(audit): verify tier — auto-defaulted to pro
|
|
1500
|
+
skill: "arrays-data-api-social-feeds",
|
|
1501
|
+
file: "x-by-url",
|
|
1502
|
+
method: "GET",
|
|
1503
|
+
path: "/api/v1/social-feeds/x/by-url",
|
|
1504
|
+
tier: "alternative",
|
|
1505
|
+
required_subscription_tier: "pro",
|
|
1506
|
+
access: "pro_only",
|
|
1507
|
+
pro_required: true
|
|
1508
|
+
},
|
|
1509
|
+
{
|
|
1510
|
+
// TODO(audit): verify tier — auto-defaulted to pro
|
|
1511
|
+
skill: "arrays-data-api-social-feeds",
|
|
1512
|
+
file: "x-entities-handle",
|
|
1513
|
+
method: "GET",
|
|
1514
|
+
path: "/api/v1/social-feeds/x/entities/handle/{twitter_handle}",
|
|
1515
|
+
tier: "alternative",
|
|
1516
|
+
required_subscription_tier: "pro",
|
|
1517
|
+
access: "pro_only",
|
|
1518
|
+
pro_required: true
|
|
1519
|
+
},
|
|
1520
|
+
{
|
|
1521
|
+
// TODO(audit): verify tier — auto-defaulted to pro
|
|
1522
|
+
skill: "arrays-data-api-social-feeds",
|
|
1523
|
+
file: "x-entities-handles",
|
|
1524
|
+
method: "GET",
|
|
1525
|
+
path: "/api/v1/social-feeds/x/entities/handles",
|
|
1526
|
+
tier: "alternative",
|
|
1527
|
+
required_subscription_tier: "pro",
|
|
1528
|
+
access: "pro_only",
|
|
1529
|
+
pro_required: true
|
|
1404
1530
|
}
|
|
1405
1531
|
];
|
|
1406
1532
|
function getSkillEndpointMetadata(skill, file) {
|
|
@@ -1795,6 +1921,38 @@ function buildQuery(params) {
|
|
|
1795
1921
|
return q;
|
|
1796
1922
|
}
|
|
1797
1923
|
|
|
1924
|
+
// src/resources/notificationPreferences.ts
|
|
1925
|
+
var NotificationPreferencesResource = class {
|
|
1926
|
+
constructor(client) {
|
|
1927
|
+
this.client = client;
|
|
1928
|
+
}
|
|
1929
|
+
client;
|
|
1930
|
+
/**
|
|
1931
|
+
* List the caller's notification preference settings. Server defaults
|
|
1932
|
+
* are included even when the caller has not explicitly stored a setting.
|
|
1933
|
+
*/
|
|
1934
|
+
async list() {
|
|
1935
|
+
this.client._requireAuth();
|
|
1936
|
+
return this.client._request(
|
|
1937
|
+
"GET",
|
|
1938
|
+
"/api/v1/me/notifications/preferences"
|
|
1939
|
+
);
|
|
1940
|
+
}
|
|
1941
|
+
/**
|
|
1942
|
+
* Enable or disable one notification preference.
|
|
1943
|
+
*/
|
|
1944
|
+
async update(params) {
|
|
1945
|
+
this.client._requireAuth();
|
|
1946
|
+
return this.client._request(
|
|
1947
|
+
"PATCH",
|
|
1948
|
+
`/api/v1/me/notifications/preferences/${encodeURIComponent(params.key)}`,
|
|
1949
|
+
{
|
|
1950
|
+
body: { enabled: params.enabled }
|
|
1951
|
+
}
|
|
1952
|
+
);
|
|
1953
|
+
}
|
|
1954
|
+
};
|
|
1955
|
+
|
|
1798
1956
|
// src/resources/pushSubscriptions.ts
|
|
1799
1957
|
var PushSubscriptionsResource = class {
|
|
1800
1958
|
constructor(client) {
|
|
@@ -1854,16 +2012,16 @@ var PushSubscriptionsResource = class {
|
|
|
1854
2012
|
);
|
|
1855
2013
|
}
|
|
1856
2014
|
/**
|
|
1857
|
-
* List the caller's personal push subscriptions
|
|
1858
|
-
*
|
|
1859
|
-
* `include_history=true` to also return previously-unsubscribed rows.
|
|
2015
|
+
* List the caller's currently active personal push subscriptions.
|
|
2016
|
+
* Results are cursor-paginated.
|
|
1860
2017
|
*/
|
|
1861
2018
|
async list(params = {}) {
|
|
1862
2019
|
this.client._requireAuth();
|
|
1863
2020
|
const query = {};
|
|
1864
|
-
if (params.
|
|
1865
|
-
query.
|
|
2021
|
+
if (params.first !== void 0 && params.first > 0) {
|
|
2022
|
+
query.first = String(params.first);
|
|
1866
2023
|
}
|
|
2024
|
+
if (params.cursor) query.cursor = params.cursor;
|
|
1867
2025
|
return this.client._request("GET", "/api/v1/me/push-subscriptions", {
|
|
1868
2026
|
query
|
|
1869
2027
|
});
|
|
@@ -1940,11 +2098,17 @@ var AlvaClient = class {
|
|
|
1940
2098
|
baseUrl;
|
|
1941
2099
|
arraysBaseUrl;
|
|
1942
2100
|
viewer_token;
|
|
2101
|
+
pbsvToken;
|
|
1943
2102
|
apiKey;
|
|
2103
|
+
gaClientId;
|
|
2104
|
+
gaSessionId;
|
|
2105
|
+
utmParams;
|
|
1944
2106
|
_fs;
|
|
1945
2107
|
_run;
|
|
1946
2108
|
_deploy;
|
|
1947
2109
|
_release;
|
|
2110
|
+
_feed;
|
|
2111
|
+
_playbooks;
|
|
1948
2112
|
_secrets;
|
|
1949
2113
|
_sdk;
|
|
1950
2114
|
_dataSkills;
|
|
@@ -1956,13 +2120,18 @@ var AlvaClient = class {
|
|
|
1956
2120
|
_trading;
|
|
1957
2121
|
_arraysJwt;
|
|
1958
2122
|
_notifications;
|
|
2123
|
+
_notificationPreferences;
|
|
1959
2124
|
_pushSubscriptions;
|
|
1960
2125
|
_channelGroupSubscriptions;
|
|
1961
2126
|
constructor(config) {
|
|
1962
2127
|
this.baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
|
|
1963
2128
|
this.arraysBaseUrl = config.arraysBaseUrl ?? DEFAULT_ARRAYS_BASE_URL;
|
|
1964
2129
|
this.viewer_token = config.viewer_token;
|
|
2130
|
+
this.pbsvToken = config.pbsvToken;
|
|
1965
2131
|
this.apiKey = config.apiKey;
|
|
2132
|
+
this.gaClientId = config.gaClientId;
|
|
2133
|
+
this.gaSessionId = config.gaSessionId;
|
|
2134
|
+
this.utmParams = config.utmParams;
|
|
1966
2135
|
}
|
|
1967
2136
|
get fs() {
|
|
1968
2137
|
return this._fs ??= new FsResource(this);
|
|
@@ -1976,6 +2145,12 @@ var AlvaClient = class {
|
|
|
1976
2145
|
get release() {
|
|
1977
2146
|
return this._release ??= new ReleaseResource(this);
|
|
1978
2147
|
}
|
|
2148
|
+
get feed() {
|
|
2149
|
+
return this._feed ??= new FeedResource(this);
|
|
2150
|
+
}
|
|
2151
|
+
get playbooks() {
|
|
2152
|
+
return this._playbooks ??= new PlaybooksResource(this);
|
|
2153
|
+
}
|
|
1979
2154
|
get secrets() {
|
|
1980
2155
|
return this._secrets ??= new SecretsResource(this);
|
|
1981
2156
|
}
|
|
@@ -2009,6 +2184,9 @@ var AlvaClient = class {
|
|
|
2009
2184
|
get notifications() {
|
|
2010
2185
|
return this._notifications ??= new NotificationsResource(this);
|
|
2011
2186
|
}
|
|
2187
|
+
get notificationPreferences() {
|
|
2188
|
+
return this._notificationPreferences ??= new NotificationPreferencesResource(this);
|
|
2189
|
+
}
|
|
2012
2190
|
get pushSubscriptions() {
|
|
2013
2191
|
return this._pushSubscriptions ??= new PushSubscriptionsResource(this);
|
|
2014
2192
|
}
|
|
@@ -2016,10 +2194,10 @@ var AlvaClient = class {
|
|
|
2016
2194
|
return this._channelGroupSubscriptions ??= new ChannelGroupSubscriptionsResource(this);
|
|
2017
2195
|
}
|
|
2018
2196
|
_requireAuth() {
|
|
2019
|
-
if (!this.viewer_token && !this.apiKey) {
|
|
2197
|
+
if (!this.pbsvToken && !this.viewer_token && !this.apiKey) {
|
|
2020
2198
|
throw new AlvaError(
|
|
2021
2199
|
"UNAUTHENTICATED",
|
|
2022
|
-
"Authentication is required. Pass viewer_token or apiKey in the constructor.",
|
|
2200
|
+
"Authentication is required. Pass pbsvToken, viewer_token, or apiKey in the constructor.",
|
|
2023
2201
|
401
|
|
2024
2202
|
);
|
|
2025
2203
|
}
|
|
@@ -2041,12 +2219,24 @@ var AlvaClient = class {
|
|
|
2041
2219
|
}
|
|
2042
2220
|
const headers = {};
|
|
2043
2221
|
if (!options?.noAuth) {
|
|
2044
|
-
if (this.
|
|
2222
|
+
if (this.pbsvToken) {
|
|
2223
|
+
headers.Authorization = `Bearer ${this.pbsvToken}`;
|
|
2224
|
+
headers["X-Pbsv"] = "1";
|
|
2225
|
+
} else if (this.viewer_token) {
|
|
2045
2226
|
headers["x-Playbook-Viewer"] = this.viewer_token;
|
|
2046
2227
|
} else if (this.apiKey) {
|
|
2047
2228
|
headers["X-Alva-Api-Key"] = this.apiKey;
|
|
2048
2229
|
}
|
|
2049
2230
|
}
|
|
2231
|
+
if (this.gaClientId) {
|
|
2232
|
+
headers["X-Alva-GA-Client-ID"] = this.gaClientId;
|
|
2233
|
+
}
|
|
2234
|
+
if (this.gaSessionId) {
|
|
2235
|
+
headers["X-Alva-GA-Session-ID"] = this.gaSessionId;
|
|
2236
|
+
}
|
|
2237
|
+
if (this.utmParams) {
|
|
2238
|
+
headers["X-Alva-UTM-Params"] = this.utmParams;
|
|
2239
|
+
}
|
|
2050
2240
|
let fetchBody;
|
|
2051
2241
|
if (options?.rawBody !== void 0) {
|
|
2052
2242
|
headers["Content-Type"] = "application/octet-stream";
|
|
@@ -2106,11 +2296,380 @@ var AlvaClient = class {
|
|
|
2106
2296
|
}
|
|
2107
2297
|
};
|
|
2108
2298
|
|
|
2299
|
+
// src/playbookRuntime.ts
|
|
2300
|
+
var UdfError = class extends Error {
|
|
2301
|
+
code;
|
|
2302
|
+
metadata;
|
|
2303
|
+
status;
|
|
2304
|
+
constructor(message, {
|
|
2305
|
+
code,
|
|
2306
|
+
metadata = {},
|
|
2307
|
+
status
|
|
2308
|
+
}) {
|
|
2309
|
+
super(message);
|
|
2310
|
+
this.name = new.target.name;
|
|
2311
|
+
this.code = code;
|
|
2312
|
+
this.metadata = metadata;
|
|
2313
|
+
this.status = status;
|
|
2314
|
+
}
|
|
2315
|
+
};
|
|
2316
|
+
var UdfAuthRequiredError = class extends UdfError {
|
|
2317
|
+
constructor() {
|
|
2318
|
+
super("Sign in to use this UDF.", { code: "AUTH_REQUIRED" });
|
|
2319
|
+
}
|
|
2320
|
+
};
|
|
2321
|
+
var UdfTokenExpiredError = class extends UdfError {
|
|
2322
|
+
};
|
|
2323
|
+
var UdfTokenInvalidError = class extends UdfError {
|
|
2324
|
+
};
|
|
2325
|
+
var UdfPidMismatchError = class extends UdfError {
|
|
2326
|
+
};
|
|
2327
|
+
var UdfConsentRequiredError = class extends UdfError {
|
|
2328
|
+
};
|
|
2329
|
+
var UdfConsentDeniedError = class extends UdfError {
|
|
2330
|
+
constructor() {
|
|
2331
|
+
super("UDF consent was denied.", { code: "CONSENT_DENIED" });
|
|
2332
|
+
}
|
|
2333
|
+
};
|
|
2334
|
+
var UdfConsentTimeoutError = class extends UdfError {
|
|
2335
|
+
constructor() {
|
|
2336
|
+
super("UDF consent timed out.", { code: "CONSENT_TIMEOUT" });
|
|
2337
|
+
}
|
|
2338
|
+
};
|
|
2339
|
+
var UdfInsufficientCredits = class extends UdfError {
|
|
2340
|
+
};
|
|
2341
|
+
var UdfFunctionNotFoundError = class extends UdfError {
|
|
2342
|
+
};
|
|
2343
|
+
var UdfFunctionDisabledError = class extends UdfError {
|
|
2344
|
+
};
|
|
2345
|
+
var UdfRateLimitedError = class extends UdfError {
|
|
2346
|
+
};
|
|
2347
|
+
var UdfExecutionError = class extends UdfError {
|
|
2348
|
+
};
|
|
2349
|
+
var UdfTransportError = class extends UdfError {
|
|
2350
|
+
};
|
|
2351
|
+
var PBSV_UPDATE_MESSAGE = "alva:pbsv:update";
|
|
2352
|
+
var UDF_CONSENT_REQUEST_MESSAGE = "alva:udf:consent-request";
|
|
2353
|
+
var UDF_CONSENT_RESPONSE_MESSAGE = "alva:udf:consent-response";
|
|
2354
|
+
var DEFAULT_API_ORIGIN = "https://api.alva.ai";
|
|
2355
|
+
var CONSENT_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
2356
|
+
var cachedToken = null;
|
|
2357
|
+
var expectedParentOrigin = null;
|
|
2358
|
+
var apiOrigin = DEFAULT_API_ORIGIN;
|
|
2359
|
+
var playbookIdFromBoot = null;
|
|
2360
|
+
var runtimeWindow = null;
|
|
2361
|
+
var installed = false;
|
|
2362
|
+
var pendingConsents = /* @__PURE__ */ new Map();
|
|
2363
|
+
var decodeBase64Url = (value) => {
|
|
2364
|
+
const normalized = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
2365
|
+
const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, "=");
|
|
2366
|
+
return atob(padded);
|
|
2367
|
+
};
|
|
2368
|
+
var playbookIdFromToken = (token) => {
|
|
2369
|
+
if (!token) return null;
|
|
2370
|
+
const [, payload] = token.split(".");
|
|
2371
|
+
if (!payload) return null;
|
|
2372
|
+
try {
|
|
2373
|
+
const parsed = JSON.parse(decodeBase64Url(payload));
|
|
2374
|
+
if (typeof parsed.pid === "number") return String(parsed.pid);
|
|
2375
|
+
if (typeof parsed.pid === "string") return parsed.pid;
|
|
2376
|
+
} catch {
|
|
2377
|
+
return null;
|
|
2378
|
+
}
|
|
2379
|
+
return null;
|
|
2380
|
+
};
|
|
2381
|
+
var getViewerToken = () => cachedToken;
|
|
2382
|
+
var stripPbsvFromUrl = (targetWindow) => {
|
|
2383
|
+
const url = new URL(targetWindow.location.href);
|
|
2384
|
+
url.searchParams.delete("_pbsv");
|
|
2385
|
+
targetWindow.history.replaceState(
|
|
2386
|
+
null,
|
|
2387
|
+
"",
|
|
2388
|
+
url.pathname + url.search + url.hash
|
|
2389
|
+
);
|
|
2390
|
+
};
|
|
2391
|
+
var parseBootParams = (targetWindow) => {
|
|
2392
|
+
const params = new URLSearchParams(targetWindow.location.search);
|
|
2393
|
+
cachedToken = params.get("_pbsv");
|
|
2394
|
+
expectedParentOrigin = params.get("parent_origin");
|
|
2395
|
+
apiOrigin = (params.get("api_origin") || DEFAULT_API_ORIGIN).replace(
|
|
2396
|
+
/\/$/,
|
|
2397
|
+
""
|
|
2398
|
+
);
|
|
2399
|
+
playbookIdFromBoot = playbookIdFromToken(cachedToken);
|
|
2400
|
+
if (cachedToken) stripPbsvFromUrl(targetWindow);
|
|
2401
|
+
};
|
|
2402
|
+
var safeJson = async (response) => {
|
|
2403
|
+
try {
|
|
2404
|
+
return await response.json();
|
|
2405
|
+
} catch {
|
|
2406
|
+
return {};
|
|
2407
|
+
}
|
|
2408
|
+
};
|
|
2409
|
+
var errorMetadata = (body) => body.error?.metadata || body.details?.metadata || {};
|
|
2410
|
+
var mapUdfErrorResponse = (body, status) => {
|
|
2411
|
+
const code = body.error?.code || "TRANSPORT";
|
|
2412
|
+
const message = body.error?.message || code;
|
|
2413
|
+
const metadata = errorMetadata(body);
|
|
2414
|
+
const props = { code, metadata, status };
|
|
2415
|
+
switch (code) {
|
|
2416
|
+
case "PBSV_EXPIRED":
|
|
2417
|
+
return new UdfTokenExpiredError(message, props);
|
|
2418
|
+
case "PBSV_INVALID":
|
|
2419
|
+
case "UNAUTHENTICATED":
|
|
2420
|
+
return new UdfTokenInvalidError(message, props);
|
|
2421
|
+
case "PBSV_PID_MISMATCH":
|
|
2422
|
+
return new UdfPidMismatchError(message, props);
|
|
2423
|
+
case "CONSENT_REQUIRED":
|
|
2424
|
+
return new UdfConsentRequiredError(message, props);
|
|
2425
|
+
case "INSUFFICIENT_CREDITS":
|
|
2426
|
+
return new UdfInsufficientCredits(message, props);
|
|
2427
|
+
case "FUNCTION_NOT_FOUND":
|
|
2428
|
+
return new UdfFunctionNotFoundError(message, props);
|
|
2429
|
+
case "FUNCTION_DISABLED":
|
|
2430
|
+
return new UdfFunctionDisabledError(message, props);
|
|
2431
|
+
case "RESOURCE_EXHAUSTED":
|
|
2432
|
+
return new UdfRateLimitedError(message, props);
|
|
2433
|
+
case "EXECUTION_FAILED":
|
|
2434
|
+
return new UdfExecutionError(message, props);
|
|
2435
|
+
default:
|
|
2436
|
+
return new UdfTransportError(message, props);
|
|
2437
|
+
}
|
|
2438
|
+
};
|
|
2439
|
+
var onMessage = (event) => {
|
|
2440
|
+
if (!runtimeWindow || !expectedParentOrigin) return;
|
|
2441
|
+
if (event.origin !== expectedParentOrigin) return;
|
|
2442
|
+
if (event.source !== runtimeWindow.parent) return;
|
|
2443
|
+
if (!event.data || typeof event.data !== "object") return;
|
|
2444
|
+
const data = event.data;
|
|
2445
|
+
if (data.type === PBSV_UPDATE_MESSAGE) {
|
|
2446
|
+
if (typeof data.token === "string" && data.token.length > 0) {
|
|
2447
|
+
cachedToken = data.token;
|
|
2448
|
+
playbookIdFromBoot = playbookIdFromToken(data.token);
|
|
2449
|
+
}
|
|
2450
|
+
return;
|
|
2451
|
+
}
|
|
2452
|
+
if (data.type !== UDF_CONSENT_RESPONSE_MESSAGE) return;
|
|
2453
|
+
if (typeof data.request_id !== "string") return;
|
|
2454
|
+
const pending = pendingConsents.get(data.request_id);
|
|
2455
|
+
if (!pending) return;
|
|
2456
|
+
clearTimeout(pending.timer);
|
|
2457
|
+
pendingConsents.delete(data.request_id);
|
|
2458
|
+
pending.resolve(data.granted === true ? "granted" : "denied");
|
|
2459
|
+
};
|
|
2460
|
+
var randomRequestId = () => {
|
|
2461
|
+
if (globalThis.crypto?.randomUUID) return globalThis.crypto.randomUUID();
|
|
2462
|
+
return `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
2463
|
+
};
|
|
2464
|
+
var playbookIdForInvoke = () => {
|
|
2465
|
+
if (!playbookIdFromBoot) return null;
|
|
2466
|
+
const numericId = Number(playbookIdFromBoot);
|
|
2467
|
+
return Number.isSafeInteger(numericId) ? numericId : null;
|
|
2468
|
+
};
|
|
2469
|
+
var requestConsent = async (playbookId, minAllowance) => {
|
|
2470
|
+
if (!runtimeWindow || !expectedParentOrigin) return "denied";
|
|
2471
|
+
const targetOrigin = expectedParentOrigin;
|
|
2472
|
+
const requestId = randomRequestId();
|
|
2473
|
+
return new Promise((resolve) => {
|
|
2474
|
+
const timer = setTimeout(() => {
|
|
2475
|
+
pendingConsents.delete(requestId);
|
|
2476
|
+
resolve("timeout");
|
|
2477
|
+
}, CONSENT_TIMEOUT_MS);
|
|
2478
|
+
pendingConsents.set(requestId, { resolve, timer });
|
|
2479
|
+
runtimeWindow?.parent.postMessage(
|
|
2480
|
+
{
|
|
2481
|
+
type: UDF_CONSENT_REQUEST_MESSAGE,
|
|
2482
|
+
request_id: requestId,
|
|
2483
|
+
playbook_id: playbookId,
|
|
2484
|
+
min_allowance: minAllowance
|
|
2485
|
+
},
|
|
2486
|
+
targetOrigin
|
|
2487
|
+
);
|
|
2488
|
+
});
|
|
2489
|
+
};
|
|
2490
|
+
var call = async (functionName, params, retried = false) => {
|
|
2491
|
+
const token = getViewerToken();
|
|
2492
|
+
if (!token) throw new UdfAuthRequiredError();
|
|
2493
|
+
if (!playbookIdFromBoot) {
|
|
2494
|
+
throw new UdfPidMismatchError("Missing playbook id.", {
|
|
2495
|
+
code: "PBSV_PID_MISMATCH"
|
|
2496
|
+
});
|
|
2497
|
+
}
|
|
2498
|
+
const playbookId = playbookIdForInvoke();
|
|
2499
|
+
if (!playbookId) {
|
|
2500
|
+
throw new UdfPidMismatchError("Invalid playbook id.", {
|
|
2501
|
+
code: "PBSV_PID_MISMATCH"
|
|
2502
|
+
});
|
|
2503
|
+
}
|
|
2504
|
+
let response;
|
|
2505
|
+
try {
|
|
2506
|
+
response = await fetch(`${apiOrigin}/api/v1/service/invoke`, {
|
|
2507
|
+
method: "POST",
|
|
2508
|
+
headers: {
|
|
2509
|
+
Authorization: `Bearer ${token}`,
|
|
2510
|
+
"X-Pbsv": "1",
|
|
2511
|
+
"Content-Type": "application/json"
|
|
2512
|
+
},
|
|
2513
|
+
body: JSON.stringify({
|
|
2514
|
+
playbook_id: playbookId,
|
|
2515
|
+
function_name: functionName,
|
|
2516
|
+
parameters_json: JSON.stringify(params)
|
|
2517
|
+
})
|
|
2518
|
+
});
|
|
2519
|
+
} catch {
|
|
2520
|
+
throw new UdfTransportError("Network error while invoking UDF.", {
|
|
2521
|
+
code: "TRANSPORT"
|
|
2522
|
+
});
|
|
2523
|
+
}
|
|
2524
|
+
const body = await safeJson(response);
|
|
2525
|
+
if (response.ok) {
|
|
2526
|
+
return body.result;
|
|
2527
|
+
}
|
|
2528
|
+
if (body.error?.code === "CONSENT_REQUIRED" && !retried) {
|
|
2529
|
+
const metadata = errorMetadata(body);
|
|
2530
|
+
const playbookId2 = typeof metadata.playbook_id === "string" ? metadata.playbook_id : playbookIdFromBoot;
|
|
2531
|
+
const minAllowance = typeof metadata.min_allowance_suggested === "number" ? metadata.min_allowance_suggested : 1;
|
|
2532
|
+
const consent = await requestConsent(playbookId2, minAllowance);
|
|
2533
|
+
if (consent === "granted") return call(functionName, params, true);
|
|
2534
|
+
if (consent === "timeout") throw new UdfConsentTimeoutError();
|
|
2535
|
+
throw new UdfConsentDeniedError();
|
|
2536
|
+
}
|
|
2537
|
+
throw mapUdfErrorResponse(body, response.status);
|
|
2538
|
+
};
|
|
2539
|
+
var list = async () => {
|
|
2540
|
+
const token = getViewerToken();
|
|
2541
|
+
if (!token) throw new UdfAuthRequiredError();
|
|
2542
|
+
if (!playbookIdFromBoot) {
|
|
2543
|
+
throw new UdfPidMismatchError("Missing playbook id.", {
|
|
2544
|
+
code: "PBSV_PID_MISMATCH"
|
|
2545
|
+
});
|
|
2546
|
+
}
|
|
2547
|
+
const url = new URL(`${apiOrigin}/api/v1/service/functions`);
|
|
2548
|
+
url.searchParams.set("playbook_id", playbookIdFromBoot);
|
|
2549
|
+
const response = await fetch(url.toString(), {
|
|
2550
|
+
headers: {
|
|
2551
|
+
Authorization: `Bearer ${token}`,
|
|
2552
|
+
"X-Pbsv": "1"
|
|
2553
|
+
}
|
|
2554
|
+
});
|
|
2555
|
+
const body = await safeJson(response);
|
|
2556
|
+
if (!response.ok) throw mapUdfErrorResponse(body, response.status);
|
|
2557
|
+
const functions = body.functions;
|
|
2558
|
+
return Array.isArray(functions) ? functions : [];
|
|
2559
|
+
};
|
|
2560
|
+
var dispatchButtonEvent = (button, name, detail) => {
|
|
2561
|
+
button.dispatchEvent(new CustomEvent(name, { bubbles: true, detail }));
|
|
2562
|
+
};
|
|
2563
|
+
var resolveButtonTarget = (target) => {
|
|
2564
|
+
if (typeof target !== "string") return target;
|
|
2565
|
+
const root = runtimeWindow?.document;
|
|
2566
|
+
const element = root?.querySelector(target);
|
|
2567
|
+
if (element && "appendChild" in element) {
|
|
2568
|
+
return element;
|
|
2569
|
+
}
|
|
2570
|
+
throw new Error(`UDF button target not found: ${target}`);
|
|
2571
|
+
};
|
|
2572
|
+
var renderButton = (target, options) => {
|
|
2573
|
+
if (!runtimeWindow) throw new Error("Playbook runtime is not installed.");
|
|
2574
|
+
const container = resolveButtonTarget(target);
|
|
2575
|
+
const button = runtimeWindow.document.createElement("button");
|
|
2576
|
+
const label = options.label || options.functionName;
|
|
2577
|
+
const loadingLabel = options.loadingLabel || "Running...";
|
|
2578
|
+
const disabledLabel = options.disabledLabel || "Sign in to run";
|
|
2579
|
+
button.type = "button";
|
|
2580
|
+
button.className = "alva-udf-button";
|
|
2581
|
+
button.textContent = getViewerToken() ? label : disabledLabel;
|
|
2582
|
+
button.disabled = !getViewerToken();
|
|
2583
|
+
button.setAttribute("data-function-name", options.functionName);
|
|
2584
|
+
if (button.disabled) button.setAttribute("aria-disabled", "true");
|
|
2585
|
+
button.addEventListener("click", async () => {
|
|
2586
|
+
if (button.disabled) return;
|
|
2587
|
+
button.disabled = true;
|
|
2588
|
+
button.textContent = loadingLabel;
|
|
2589
|
+
dispatchButtonEvent(button, "alva:udf-button:loading", {
|
|
2590
|
+
functionName: options.functionName
|
|
2591
|
+
});
|
|
2592
|
+
try {
|
|
2593
|
+
const result = await call(options.functionName, options.params ?? {});
|
|
2594
|
+
dispatchButtonEvent(button, "alva:udf-button:result", {
|
|
2595
|
+
functionName: options.functionName,
|
|
2596
|
+
result
|
|
2597
|
+
});
|
|
2598
|
+
} catch (error) {
|
|
2599
|
+
dispatchButtonEvent(button, "alva:udf-button:error", {
|
|
2600
|
+
functionName: options.functionName,
|
|
2601
|
+
error
|
|
2602
|
+
});
|
|
2603
|
+
} finally {
|
|
2604
|
+
button.disabled = !getViewerToken();
|
|
2605
|
+
button.textContent = button.disabled ? disabledLabel : label;
|
|
2606
|
+
}
|
|
2607
|
+
});
|
|
2608
|
+
container.appendChild(button);
|
|
2609
|
+
return button;
|
|
2610
|
+
};
|
|
2611
|
+
var udf = {
|
|
2612
|
+
call,
|
|
2613
|
+
list,
|
|
2614
|
+
getViewerToken,
|
|
2615
|
+
renderButton,
|
|
2616
|
+
UdfError,
|
|
2617
|
+
UdfAuthRequiredError,
|
|
2618
|
+
UdfTokenExpiredError,
|
|
2619
|
+
UdfTokenInvalidError,
|
|
2620
|
+
UdfPidMismatchError,
|
|
2621
|
+
UdfConsentRequiredError,
|
|
2622
|
+
UdfConsentDeniedError,
|
|
2623
|
+
UdfConsentTimeoutError,
|
|
2624
|
+
UdfInsufficientCredits,
|
|
2625
|
+
UdfFunctionNotFoundError,
|
|
2626
|
+
UdfFunctionDisabledError,
|
|
2627
|
+
UdfRateLimitedError,
|
|
2628
|
+
UdfExecutionError,
|
|
2629
|
+
UdfTransportError
|
|
2630
|
+
};
|
|
2631
|
+
var installPlaybookRuntime = (targetWindow) => {
|
|
2632
|
+
const nextWindow = targetWindow ?? (typeof window === "undefined" ? void 0 : window);
|
|
2633
|
+
if (!nextWindow || installed) return;
|
|
2634
|
+
runtimeWindow = nextWindow;
|
|
2635
|
+
installed = true;
|
|
2636
|
+
parseBootParams(nextWindow);
|
|
2637
|
+
nextWindow.addEventListener("message", onMessage);
|
|
2638
|
+
const target = nextWindow;
|
|
2639
|
+
const existing = target.alva || {};
|
|
2640
|
+
target.alva = {
|
|
2641
|
+
...existing,
|
|
2642
|
+
udf
|
|
2643
|
+
};
|
|
2644
|
+
};
|
|
2645
|
+
|
|
2109
2646
|
// src/index.ts
|
|
2110
|
-
var VERSION = true ? "0.
|
|
2647
|
+
var VERSION = true ? "0.9.0" : "dev";
|
|
2111
2648
|
export {
|
|
2112
2649
|
AlvaClient,
|
|
2113
2650
|
AlvaError,
|
|
2114
|
-
|
|
2651
|
+
PBSV_UPDATE_MESSAGE,
|
|
2652
|
+
UDF_CONSENT_REQUEST_MESSAGE,
|
|
2653
|
+
UDF_CONSENT_RESPONSE_MESSAGE,
|
|
2654
|
+
UdfAuthRequiredError,
|
|
2655
|
+
UdfConsentDeniedError,
|
|
2656
|
+
UdfConsentRequiredError,
|
|
2657
|
+
UdfConsentTimeoutError,
|
|
2658
|
+
UdfError,
|
|
2659
|
+
UdfExecutionError,
|
|
2660
|
+
UdfFunctionDisabledError,
|
|
2661
|
+
UdfFunctionNotFoundError,
|
|
2662
|
+
UdfInsufficientCredits,
|
|
2663
|
+
UdfPidMismatchError,
|
|
2664
|
+
UdfRateLimitedError,
|
|
2665
|
+
UdfTokenExpiredError,
|
|
2666
|
+
UdfTokenInvalidError,
|
|
2667
|
+
UdfTransportError,
|
|
2668
|
+
VERSION,
|
|
2669
|
+
getViewerToken,
|
|
2670
|
+
installPlaybookRuntime,
|
|
2671
|
+
mapUdfErrorResponse,
|
|
2672
|
+
playbookIdFromToken,
|
|
2673
|
+
udf
|
|
2115
2674
|
};
|
|
2116
2675
|
//# sourceMappingURL=index.js.map
|