@alva-ai/toolkit 0.8.0 → 0.9.1
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 +10337 -112
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +614 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +293 -1
- package/dist/index.d.ts +293 -1
- package/dist/index.js +591 -31
- 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
|
}
|
|
@@ -320,6 +321,60 @@ var FeedResource = class {
|
|
|
320
321
|
}
|
|
321
322
|
};
|
|
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
|
+
|
|
323
378
|
// src/resources/secrets.ts
|
|
324
379
|
var SecretsResource = class {
|
|
325
380
|
constructor(client) {
|
|
@@ -389,6 +444,26 @@ var SdkDocsResource = class {
|
|
|
389
444
|
|
|
390
445
|
// src/resources/skillTiers.ts
|
|
391
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
|
+
},
|
|
392
467
|
{
|
|
393
468
|
skill: "arrays-data-api-spot-market-price-and-volume",
|
|
394
469
|
file: "crypto-detail",
|
|
@@ -420,10 +495,10 @@ var SKILL_ENDPOINT_METADATA = [
|
|
|
420
495
|
pro_required: false
|
|
421
496
|
},
|
|
422
497
|
{
|
|
423
|
-
skill: "arrays-data-api-
|
|
424
|
-
file: "
|
|
498
|
+
skill: "arrays-data-api-crypto-futures-data",
|
|
499
|
+
file: "hyperliquid-perp-usdc-kline",
|
|
425
500
|
method: "GET",
|
|
426
|
-
path: "/api/v1/crypto/
|
|
501
|
+
path: "/api/v1/crypto/hyperliquid/perp/usdc/kline",
|
|
427
502
|
tier: "public",
|
|
428
503
|
required_subscription_tier: "free",
|
|
429
504
|
access: "free_and_pro",
|
|
@@ -431,9 +506,9 @@ var SKILL_ENDPOINT_METADATA = [
|
|
|
431
506
|
},
|
|
432
507
|
{
|
|
433
508
|
skill: "arrays-data-api-spot-market-price-and-volume",
|
|
434
|
-
file: "
|
|
509
|
+
file: "hyperliquid-spot-usdc-kline",
|
|
435
510
|
method: "GET",
|
|
436
|
-
path: "/api/v1/crypto/kline",
|
|
511
|
+
path: "/api/v1/crypto/hyperliquid/spot/usdc/kline",
|
|
437
512
|
tier: "public",
|
|
438
513
|
required_subscription_tier: "free",
|
|
439
514
|
access: "free_and_pro",
|
|
@@ -479,16 +554,6 @@ var SKILL_ENDPOINT_METADATA = [
|
|
|
479
554
|
access: "free_and_pro",
|
|
480
555
|
pro_required: false
|
|
481
556
|
},
|
|
482
|
-
{
|
|
483
|
-
skill: "arrays-data-api-spot-market-price-and-volume",
|
|
484
|
-
file: "crypto-ohlcv",
|
|
485
|
-
method: "GET",
|
|
486
|
-
path: "/api/v1/crypto/ohlcv",
|
|
487
|
-
tier: "public",
|
|
488
|
-
required_subscription_tier: "free",
|
|
489
|
-
access: "free_and_pro",
|
|
490
|
-
pro_required: false
|
|
491
|
-
},
|
|
492
557
|
{
|
|
493
558
|
skill: "arrays-data-api-crypto-futures-data",
|
|
494
559
|
file: "open-interest",
|
|
@@ -1179,16 +1244,6 @@ var SKILL_ENDPOINT_METADATA = [
|
|
|
1179
1244
|
access: "pro_only",
|
|
1180
1245
|
pro_required: true
|
|
1181
1246
|
},
|
|
1182
|
-
{
|
|
1183
|
-
skill: "arrays-data-api-equity-ownership-and-flow",
|
|
1184
|
-
file: "senate-trade",
|
|
1185
|
-
method: "GET",
|
|
1186
|
-
path: "/api/v1/stocks/company/senate-trade",
|
|
1187
|
-
tier: "alternative",
|
|
1188
|
-
required_subscription_tier: "pro",
|
|
1189
|
-
access: "pro_only",
|
|
1190
|
-
pro_required: true
|
|
1191
|
-
},
|
|
1192
1247
|
{
|
|
1193
1248
|
skill: "arrays-data-api-stock-metrics",
|
|
1194
1249
|
file: "darkpool",
|
|
@@ -1428,6 +1483,50 @@ var SKILL_ENDPOINT_METADATA = [
|
|
|
1428
1483
|
required_subscription_tier: "pro",
|
|
1429
1484
|
access: "pro_only",
|
|
1430
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
|
|
1431
1530
|
}
|
|
1432
1531
|
];
|
|
1433
1532
|
function getSkillEndpointMetadata(skill, file) {
|
|
@@ -1752,6 +1851,34 @@ var TradingResource = class {
|
|
|
1752
1851
|
}
|
|
1753
1852
|
};
|
|
1754
1853
|
|
|
1854
|
+
// src/resources/portfolio.ts
|
|
1855
|
+
var PortfolioResource = class {
|
|
1856
|
+
constructor(client) {
|
|
1857
|
+
this.client = client;
|
|
1858
|
+
}
|
|
1859
|
+
client;
|
|
1860
|
+
async accounts() {
|
|
1861
|
+
this.client._requireAuth();
|
|
1862
|
+
return this.client._request("GET", "/api/v1/portfolio/accounts");
|
|
1863
|
+
}
|
|
1864
|
+
async summary(accountId) {
|
|
1865
|
+
this.client._requireAuth();
|
|
1866
|
+
return this.client._request("GET", "/api/v1/portfolio/summary", {
|
|
1867
|
+
query: { accountId }
|
|
1868
|
+
});
|
|
1869
|
+
}
|
|
1870
|
+
async activities(params) {
|
|
1871
|
+
this.client._requireAuth();
|
|
1872
|
+
return this.client._request("GET", "/api/v1/portfolio/activities", {
|
|
1873
|
+
query: {
|
|
1874
|
+
accountId: params.accountId,
|
|
1875
|
+
limit: params.limit,
|
|
1876
|
+
pageToken: params.pageToken
|
|
1877
|
+
}
|
|
1878
|
+
});
|
|
1879
|
+
}
|
|
1880
|
+
};
|
|
1881
|
+
|
|
1755
1882
|
// src/resources/arraysJwt.ts
|
|
1756
1883
|
var ArraysJwtResource = class {
|
|
1757
1884
|
constructor(client) {
|
|
@@ -1822,6 +1949,38 @@ function buildQuery(params) {
|
|
|
1822
1949
|
return q;
|
|
1823
1950
|
}
|
|
1824
1951
|
|
|
1952
|
+
// src/resources/notificationPreferences.ts
|
|
1953
|
+
var NotificationPreferencesResource = class {
|
|
1954
|
+
constructor(client) {
|
|
1955
|
+
this.client = client;
|
|
1956
|
+
}
|
|
1957
|
+
client;
|
|
1958
|
+
/**
|
|
1959
|
+
* List the caller's notification preference settings. Server defaults
|
|
1960
|
+
* are included even when the caller has not explicitly stored a setting.
|
|
1961
|
+
*/
|
|
1962
|
+
async list() {
|
|
1963
|
+
this.client._requireAuth();
|
|
1964
|
+
return this.client._request(
|
|
1965
|
+
"GET",
|
|
1966
|
+
"/api/v1/me/notifications/preferences"
|
|
1967
|
+
);
|
|
1968
|
+
}
|
|
1969
|
+
/**
|
|
1970
|
+
* Enable or disable one notification preference.
|
|
1971
|
+
*/
|
|
1972
|
+
async update(params) {
|
|
1973
|
+
this.client._requireAuth();
|
|
1974
|
+
return this.client._request(
|
|
1975
|
+
"PATCH",
|
|
1976
|
+
`/api/v1/me/notifications/preferences/${encodeURIComponent(params.key)}`,
|
|
1977
|
+
{
|
|
1978
|
+
body: { enabled: params.enabled }
|
|
1979
|
+
}
|
|
1980
|
+
);
|
|
1981
|
+
}
|
|
1982
|
+
};
|
|
1983
|
+
|
|
1825
1984
|
// src/resources/pushSubscriptions.ts
|
|
1826
1985
|
var PushSubscriptionsResource = class {
|
|
1827
1986
|
constructor(client) {
|
|
@@ -1967,12 +2126,17 @@ var AlvaClient = class {
|
|
|
1967
2126
|
baseUrl;
|
|
1968
2127
|
arraysBaseUrl;
|
|
1969
2128
|
viewer_token;
|
|
2129
|
+
pbsvToken;
|
|
1970
2130
|
apiKey;
|
|
2131
|
+
gaClientId;
|
|
2132
|
+
gaSessionId;
|
|
2133
|
+
utmParams;
|
|
1971
2134
|
_fs;
|
|
1972
2135
|
_run;
|
|
1973
2136
|
_deploy;
|
|
1974
2137
|
_release;
|
|
1975
2138
|
_feed;
|
|
2139
|
+
_playbooks;
|
|
1976
2140
|
_secrets;
|
|
1977
2141
|
_sdk;
|
|
1978
2142
|
_dataSkills;
|
|
@@ -1982,15 +2146,21 @@ var AlvaClient = class {
|
|
|
1982
2146
|
_screenshot;
|
|
1983
2147
|
_user;
|
|
1984
2148
|
_trading;
|
|
2149
|
+
_portfolio;
|
|
1985
2150
|
_arraysJwt;
|
|
1986
2151
|
_notifications;
|
|
2152
|
+
_notificationPreferences;
|
|
1987
2153
|
_pushSubscriptions;
|
|
1988
2154
|
_channelGroupSubscriptions;
|
|
1989
2155
|
constructor(config) {
|
|
1990
2156
|
this.baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
|
|
1991
2157
|
this.arraysBaseUrl = config.arraysBaseUrl ?? DEFAULT_ARRAYS_BASE_URL;
|
|
1992
2158
|
this.viewer_token = config.viewer_token;
|
|
2159
|
+
this.pbsvToken = config.pbsvToken;
|
|
1993
2160
|
this.apiKey = config.apiKey;
|
|
2161
|
+
this.gaClientId = config.gaClientId;
|
|
2162
|
+
this.gaSessionId = config.gaSessionId;
|
|
2163
|
+
this.utmParams = config.utmParams;
|
|
1994
2164
|
}
|
|
1995
2165
|
get fs() {
|
|
1996
2166
|
return this._fs ??= new FsResource(this);
|
|
@@ -2007,6 +2177,9 @@ var AlvaClient = class {
|
|
|
2007
2177
|
get feed() {
|
|
2008
2178
|
return this._feed ??= new FeedResource(this);
|
|
2009
2179
|
}
|
|
2180
|
+
get playbooks() {
|
|
2181
|
+
return this._playbooks ??= new PlaybooksResource(this);
|
|
2182
|
+
}
|
|
2010
2183
|
get secrets() {
|
|
2011
2184
|
return this._secrets ??= new SecretsResource(this);
|
|
2012
2185
|
}
|
|
@@ -2034,12 +2207,18 @@ var AlvaClient = class {
|
|
|
2034
2207
|
get trading() {
|
|
2035
2208
|
return this._trading ??= new TradingResource(this);
|
|
2036
2209
|
}
|
|
2210
|
+
get portfolio() {
|
|
2211
|
+
return this._portfolio ??= new PortfolioResource(this);
|
|
2212
|
+
}
|
|
2037
2213
|
get arraysJwt() {
|
|
2038
2214
|
return this._arraysJwt ??= new ArraysJwtResource(this);
|
|
2039
2215
|
}
|
|
2040
2216
|
get notifications() {
|
|
2041
2217
|
return this._notifications ??= new NotificationsResource(this);
|
|
2042
2218
|
}
|
|
2219
|
+
get notificationPreferences() {
|
|
2220
|
+
return this._notificationPreferences ??= new NotificationPreferencesResource(this);
|
|
2221
|
+
}
|
|
2043
2222
|
get pushSubscriptions() {
|
|
2044
2223
|
return this._pushSubscriptions ??= new PushSubscriptionsResource(this);
|
|
2045
2224
|
}
|
|
@@ -2047,10 +2226,10 @@ var AlvaClient = class {
|
|
|
2047
2226
|
return this._channelGroupSubscriptions ??= new ChannelGroupSubscriptionsResource(this);
|
|
2048
2227
|
}
|
|
2049
2228
|
_requireAuth() {
|
|
2050
|
-
if (!this.viewer_token && !this.apiKey) {
|
|
2229
|
+
if (!this.pbsvToken && !this.viewer_token && !this.apiKey) {
|
|
2051
2230
|
throw new AlvaError(
|
|
2052
2231
|
"UNAUTHENTICATED",
|
|
2053
|
-
"Authentication is required. Pass viewer_token or apiKey in the constructor.",
|
|
2232
|
+
"Authentication is required. Pass pbsvToken, viewer_token, or apiKey in the constructor.",
|
|
2054
2233
|
401
|
|
2055
2234
|
);
|
|
2056
2235
|
}
|
|
@@ -2072,12 +2251,24 @@ var AlvaClient = class {
|
|
|
2072
2251
|
}
|
|
2073
2252
|
const headers = {};
|
|
2074
2253
|
if (!options?.noAuth) {
|
|
2075
|
-
if (this.
|
|
2254
|
+
if (this.pbsvToken) {
|
|
2255
|
+
headers.Authorization = `Bearer ${this.pbsvToken}`;
|
|
2256
|
+
headers["X-Pbsv"] = "1";
|
|
2257
|
+
} else if (this.viewer_token) {
|
|
2076
2258
|
headers["x-Playbook-Viewer"] = this.viewer_token;
|
|
2077
2259
|
} else if (this.apiKey) {
|
|
2078
2260
|
headers["X-Alva-Api-Key"] = this.apiKey;
|
|
2079
2261
|
}
|
|
2080
2262
|
}
|
|
2263
|
+
if (this.gaClientId) {
|
|
2264
|
+
headers["X-Alva-GA-Client-ID"] = this.gaClientId;
|
|
2265
|
+
}
|
|
2266
|
+
if (this.gaSessionId) {
|
|
2267
|
+
headers["X-Alva-GA-Session-ID"] = this.gaSessionId;
|
|
2268
|
+
}
|
|
2269
|
+
if (this.utmParams) {
|
|
2270
|
+
headers["X-Alva-UTM-Params"] = this.utmParams;
|
|
2271
|
+
}
|
|
2081
2272
|
let fetchBody;
|
|
2082
2273
|
if (options?.rawBody !== void 0) {
|
|
2083
2274
|
headers["Content-Type"] = "application/octet-stream";
|
|
@@ -2137,11 +2328,380 @@ var AlvaClient = class {
|
|
|
2137
2328
|
}
|
|
2138
2329
|
};
|
|
2139
2330
|
|
|
2331
|
+
// src/playbookRuntime.ts
|
|
2332
|
+
var UdfError = class extends Error {
|
|
2333
|
+
code;
|
|
2334
|
+
metadata;
|
|
2335
|
+
status;
|
|
2336
|
+
constructor(message, {
|
|
2337
|
+
code,
|
|
2338
|
+
metadata = {},
|
|
2339
|
+
status
|
|
2340
|
+
}) {
|
|
2341
|
+
super(message);
|
|
2342
|
+
this.name = new.target.name;
|
|
2343
|
+
this.code = code;
|
|
2344
|
+
this.metadata = metadata;
|
|
2345
|
+
this.status = status;
|
|
2346
|
+
}
|
|
2347
|
+
};
|
|
2348
|
+
var UdfAuthRequiredError = class extends UdfError {
|
|
2349
|
+
constructor() {
|
|
2350
|
+
super("Sign in to use this UDF.", { code: "AUTH_REQUIRED" });
|
|
2351
|
+
}
|
|
2352
|
+
};
|
|
2353
|
+
var UdfTokenExpiredError = class extends UdfError {
|
|
2354
|
+
};
|
|
2355
|
+
var UdfTokenInvalidError = class extends UdfError {
|
|
2356
|
+
};
|
|
2357
|
+
var UdfPidMismatchError = class extends UdfError {
|
|
2358
|
+
};
|
|
2359
|
+
var UdfConsentRequiredError = class extends UdfError {
|
|
2360
|
+
};
|
|
2361
|
+
var UdfConsentDeniedError = class extends UdfError {
|
|
2362
|
+
constructor() {
|
|
2363
|
+
super("UDF consent was denied.", { code: "CONSENT_DENIED" });
|
|
2364
|
+
}
|
|
2365
|
+
};
|
|
2366
|
+
var UdfConsentTimeoutError = class extends UdfError {
|
|
2367
|
+
constructor() {
|
|
2368
|
+
super("UDF consent timed out.", { code: "CONSENT_TIMEOUT" });
|
|
2369
|
+
}
|
|
2370
|
+
};
|
|
2371
|
+
var UdfInsufficientCredits = class extends UdfError {
|
|
2372
|
+
};
|
|
2373
|
+
var UdfFunctionNotFoundError = class extends UdfError {
|
|
2374
|
+
};
|
|
2375
|
+
var UdfFunctionDisabledError = class extends UdfError {
|
|
2376
|
+
};
|
|
2377
|
+
var UdfRateLimitedError = class extends UdfError {
|
|
2378
|
+
};
|
|
2379
|
+
var UdfExecutionError = class extends UdfError {
|
|
2380
|
+
};
|
|
2381
|
+
var UdfTransportError = class extends UdfError {
|
|
2382
|
+
};
|
|
2383
|
+
var PBSV_UPDATE_MESSAGE = "alva:pbsv:update";
|
|
2384
|
+
var UDF_CONSENT_REQUEST_MESSAGE = "alva:udf:consent-request";
|
|
2385
|
+
var UDF_CONSENT_RESPONSE_MESSAGE = "alva:udf:consent-response";
|
|
2386
|
+
var DEFAULT_API_ORIGIN = "https://api.alva.ai";
|
|
2387
|
+
var CONSENT_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
2388
|
+
var cachedToken = null;
|
|
2389
|
+
var expectedParentOrigin = null;
|
|
2390
|
+
var apiOrigin = DEFAULT_API_ORIGIN;
|
|
2391
|
+
var playbookIdFromBoot = null;
|
|
2392
|
+
var runtimeWindow = null;
|
|
2393
|
+
var installed = false;
|
|
2394
|
+
var pendingConsents = /* @__PURE__ */ new Map();
|
|
2395
|
+
var decodeBase64Url = (value) => {
|
|
2396
|
+
const normalized = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
2397
|
+
const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, "=");
|
|
2398
|
+
return atob(padded);
|
|
2399
|
+
};
|
|
2400
|
+
var playbookIdFromToken = (token) => {
|
|
2401
|
+
if (!token) return null;
|
|
2402
|
+
const [, payload] = token.split(".");
|
|
2403
|
+
if (!payload) return null;
|
|
2404
|
+
try {
|
|
2405
|
+
const parsed = JSON.parse(decodeBase64Url(payload));
|
|
2406
|
+
if (typeof parsed.pid === "number") return String(parsed.pid);
|
|
2407
|
+
if (typeof parsed.pid === "string") return parsed.pid;
|
|
2408
|
+
} catch {
|
|
2409
|
+
return null;
|
|
2410
|
+
}
|
|
2411
|
+
return null;
|
|
2412
|
+
};
|
|
2413
|
+
var getViewerToken = () => cachedToken;
|
|
2414
|
+
var stripPbsvFromUrl = (targetWindow) => {
|
|
2415
|
+
const url = new URL(targetWindow.location.href);
|
|
2416
|
+
url.searchParams.delete("_pbsv");
|
|
2417
|
+
targetWindow.history.replaceState(
|
|
2418
|
+
null,
|
|
2419
|
+
"",
|
|
2420
|
+
url.pathname + url.search + url.hash
|
|
2421
|
+
);
|
|
2422
|
+
};
|
|
2423
|
+
var parseBootParams = (targetWindow) => {
|
|
2424
|
+
const params = new URLSearchParams(targetWindow.location.search);
|
|
2425
|
+
cachedToken = params.get("_pbsv");
|
|
2426
|
+
expectedParentOrigin = params.get("parent_origin");
|
|
2427
|
+
apiOrigin = (params.get("api_origin") || DEFAULT_API_ORIGIN).replace(
|
|
2428
|
+
/\/$/,
|
|
2429
|
+
""
|
|
2430
|
+
);
|
|
2431
|
+
playbookIdFromBoot = playbookIdFromToken(cachedToken);
|
|
2432
|
+
if (cachedToken) stripPbsvFromUrl(targetWindow);
|
|
2433
|
+
};
|
|
2434
|
+
var safeJson = async (response) => {
|
|
2435
|
+
try {
|
|
2436
|
+
return await response.json();
|
|
2437
|
+
} catch {
|
|
2438
|
+
return {};
|
|
2439
|
+
}
|
|
2440
|
+
};
|
|
2441
|
+
var errorMetadata = (body) => body.error?.metadata || body.details?.metadata || {};
|
|
2442
|
+
var mapUdfErrorResponse = (body, status) => {
|
|
2443
|
+
const code = body.error?.code || "TRANSPORT";
|
|
2444
|
+
const message = body.error?.message || code;
|
|
2445
|
+
const metadata = errorMetadata(body);
|
|
2446
|
+
const props = { code, metadata, status };
|
|
2447
|
+
switch (code) {
|
|
2448
|
+
case "PBSV_EXPIRED":
|
|
2449
|
+
return new UdfTokenExpiredError(message, props);
|
|
2450
|
+
case "PBSV_INVALID":
|
|
2451
|
+
case "UNAUTHENTICATED":
|
|
2452
|
+
return new UdfTokenInvalidError(message, props);
|
|
2453
|
+
case "PBSV_PID_MISMATCH":
|
|
2454
|
+
return new UdfPidMismatchError(message, props);
|
|
2455
|
+
case "CONSENT_REQUIRED":
|
|
2456
|
+
return new UdfConsentRequiredError(message, props);
|
|
2457
|
+
case "INSUFFICIENT_CREDITS":
|
|
2458
|
+
return new UdfInsufficientCredits(message, props);
|
|
2459
|
+
case "FUNCTION_NOT_FOUND":
|
|
2460
|
+
return new UdfFunctionNotFoundError(message, props);
|
|
2461
|
+
case "FUNCTION_DISABLED":
|
|
2462
|
+
return new UdfFunctionDisabledError(message, props);
|
|
2463
|
+
case "RESOURCE_EXHAUSTED":
|
|
2464
|
+
return new UdfRateLimitedError(message, props);
|
|
2465
|
+
case "EXECUTION_FAILED":
|
|
2466
|
+
return new UdfExecutionError(message, props);
|
|
2467
|
+
default:
|
|
2468
|
+
return new UdfTransportError(message, props);
|
|
2469
|
+
}
|
|
2470
|
+
};
|
|
2471
|
+
var onMessage = (event) => {
|
|
2472
|
+
if (!runtimeWindow || !expectedParentOrigin) return;
|
|
2473
|
+
if (event.origin !== expectedParentOrigin) return;
|
|
2474
|
+
if (event.source !== runtimeWindow.parent) return;
|
|
2475
|
+
if (!event.data || typeof event.data !== "object") return;
|
|
2476
|
+
const data = event.data;
|
|
2477
|
+
if (data.type === PBSV_UPDATE_MESSAGE) {
|
|
2478
|
+
if (typeof data.token === "string" && data.token.length > 0) {
|
|
2479
|
+
cachedToken = data.token;
|
|
2480
|
+
playbookIdFromBoot = playbookIdFromToken(data.token);
|
|
2481
|
+
}
|
|
2482
|
+
return;
|
|
2483
|
+
}
|
|
2484
|
+
if (data.type !== UDF_CONSENT_RESPONSE_MESSAGE) return;
|
|
2485
|
+
if (typeof data.request_id !== "string") return;
|
|
2486
|
+
const pending = pendingConsents.get(data.request_id);
|
|
2487
|
+
if (!pending) return;
|
|
2488
|
+
clearTimeout(pending.timer);
|
|
2489
|
+
pendingConsents.delete(data.request_id);
|
|
2490
|
+
pending.resolve(data.granted === true ? "granted" : "denied");
|
|
2491
|
+
};
|
|
2492
|
+
var randomRequestId = () => {
|
|
2493
|
+
if (globalThis.crypto?.randomUUID) return globalThis.crypto.randomUUID();
|
|
2494
|
+
return `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
2495
|
+
};
|
|
2496
|
+
var playbookIdForInvoke = () => {
|
|
2497
|
+
if (!playbookIdFromBoot) return null;
|
|
2498
|
+
const numericId = Number(playbookIdFromBoot);
|
|
2499
|
+
return Number.isSafeInteger(numericId) ? numericId : null;
|
|
2500
|
+
};
|
|
2501
|
+
var requestConsent = async (playbookId, minAllowance) => {
|
|
2502
|
+
if (!runtimeWindow || !expectedParentOrigin) return "denied";
|
|
2503
|
+
const targetOrigin = expectedParentOrigin;
|
|
2504
|
+
const requestId = randomRequestId();
|
|
2505
|
+
return new Promise((resolve) => {
|
|
2506
|
+
const timer = setTimeout(() => {
|
|
2507
|
+
pendingConsents.delete(requestId);
|
|
2508
|
+
resolve("timeout");
|
|
2509
|
+
}, CONSENT_TIMEOUT_MS);
|
|
2510
|
+
pendingConsents.set(requestId, { resolve, timer });
|
|
2511
|
+
runtimeWindow?.parent.postMessage(
|
|
2512
|
+
{
|
|
2513
|
+
type: UDF_CONSENT_REQUEST_MESSAGE,
|
|
2514
|
+
request_id: requestId,
|
|
2515
|
+
playbook_id: playbookId,
|
|
2516
|
+
min_allowance: minAllowance
|
|
2517
|
+
},
|
|
2518
|
+
targetOrigin
|
|
2519
|
+
);
|
|
2520
|
+
});
|
|
2521
|
+
};
|
|
2522
|
+
var call = async (functionName, params, retried = false) => {
|
|
2523
|
+
const token = getViewerToken();
|
|
2524
|
+
if (!token) throw new UdfAuthRequiredError();
|
|
2525
|
+
if (!playbookIdFromBoot) {
|
|
2526
|
+
throw new UdfPidMismatchError("Missing playbook id.", {
|
|
2527
|
+
code: "PBSV_PID_MISMATCH"
|
|
2528
|
+
});
|
|
2529
|
+
}
|
|
2530
|
+
const playbookId = playbookIdForInvoke();
|
|
2531
|
+
if (!playbookId) {
|
|
2532
|
+
throw new UdfPidMismatchError("Invalid playbook id.", {
|
|
2533
|
+
code: "PBSV_PID_MISMATCH"
|
|
2534
|
+
});
|
|
2535
|
+
}
|
|
2536
|
+
let response;
|
|
2537
|
+
try {
|
|
2538
|
+
response = await fetch(`${apiOrigin}/api/v1/service/invoke`, {
|
|
2539
|
+
method: "POST",
|
|
2540
|
+
headers: {
|
|
2541
|
+
Authorization: `Bearer ${token}`,
|
|
2542
|
+
"X-Pbsv": "1",
|
|
2543
|
+
"Content-Type": "application/json"
|
|
2544
|
+
},
|
|
2545
|
+
body: JSON.stringify({
|
|
2546
|
+
playbook_id: playbookId,
|
|
2547
|
+
function_name: functionName,
|
|
2548
|
+
parameters_json: JSON.stringify(params)
|
|
2549
|
+
})
|
|
2550
|
+
});
|
|
2551
|
+
} catch {
|
|
2552
|
+
throw new UdfTransportError("Network error while invoking UDF.", {
|
|
2553
|
+
code: "TRANSPORT"
|
|
2554
|
+
});
|
|
2555
|
+
}
|
|
2556
|
+
const body = await safeJson(response);
|
|
2557
|
+
if (response.ok) {
|
|
2558
|
+
return body.result;
|
|
2559
|
+
}
|
|
2560
|
+
if (body.error?.code === "CONSENT_REQUIRED" && !retried) {
|
|
2561
|
+
const metadata = errorMetadata(body);
|
|
2562
|
+
const playbookId2 = typeof metadata.playbook_id === "string" ? metadata.playbook_id : playbookIdFromBoot;
|
|
2563
|
+
const minAllowance = typeof metadata.min_allowance_suggested === "number" ? metadata.min_allowance_suggested : 1;
|
|
2564
|
+
const consent = await requestConsent(playbookId2, minAllowance);
|
|
2565
|
+
if (consent === "granted") return call(functionName, params, true);
|
|
2566
|
+
if (consent === "timeout") throw new UdfConsentTimeoutError();
|
|
2567
|
+
throw new UdfConsentDeniedError();
|
|
2568
|
+
}
|
|
2569
|
+
throw mapUdfErrorResponse(body, response.status);
|
|
2570
|
+
};
|
|
2571
|
+
var list = async () => {
|
|
2572
|
+
const token = getViewerToken();
|
|
2573
|
+
if (!token) throw new UdfAuthRequiredError();
|
|
2574
|
+
if (!playbookIdFromBoot) {
|
|
2575
|
+
throw new UdfPidMismatchError("Missing playbook id.", {
|
|
2576
|
+
code: "PBSV_PID_MISMATCH"
|
|
2577
|
+
});
|
|
2578
|
+
}
|
|
2579
|
+
const url = new URL(`${apiOrigin}/api/v1/service/functions`);
|
|
2580
|
+
url.searchParams.set("playbook_id", playbookIdFromBoot);
|
|
2581
|
+
const response = await fetch(url.toString(), {
|
|
2582
|
+
headers: {
|
|
2583
|
+
Authorization: `Bearer ${token}`,
|
|
2584
|
+
"X-Pbsv": "1"
|
|
2585
|
+
}
|
|
2586
|
+
});
|
|
2587
|
+
const body = await safeJson(response);
|
|
2588
|
+
if (!response.ok) throw mapUdfErrorResponse(body, response.status);
|
|
2589
|
+
const functions = body.functions;
|
|
2590
|
+
return Array.isArray(functions) ? functions : [];
|
|
2591
|
+
};
|
|
2592
|
+
var dispatchButtonEvent = (button, name, detail) => {
|
|
2593
|
+
button.dispatchEvent(new CustomEvent(name, { bubbles: true, detail }));
|
|
2594
|
+
};
|
|
2595
|
+
var resolveButtonTarget = (target) => {
|
|
2596
|
+
if (typeof target !== "string") return target;
|
|
2597
|
+
const root = runtimeWindow?.document;
|
|
2598
|
+
const element = root?.querySelector(target);
|
|
2599
|
+
if (element && "appendChild" in element) {
|
|
2600
|
+
return element;
|
|
2601
|
+
}
|
|
2602
|
+
throw new Error(`UDF button target not found: ${target}`);
|
|
2603
|
+
};
|
|
2604
|
+
var renderButton = (target, options) => {
|
|
2605
|
+
if (!runtimeWindow) throw new Error("Playbook runtime is not installed.");
|
|
2606
|
+
const container = resolveButtonTarget(target);
|
|
2607
|
+
const button = runtimeWindow.document.createElement("button");
|
|
2608
|
+
const label = options.label || options.functionName;
|
|
2609
|
+
const loadingLabel = options.loadingLabel || "Running...";
|
|
2610
|
+
const disabledLabel = options.disabledLabel || "Sign in to run";
|
|
2611
|
+
button.type = "button";
|
|
2612
|
+
button.className = "alva-udf-button";
|
|
2613
|
+
button.textContent = getViewerToken() ? label : disabledLabel;
|
|
2614
|
+
button.disabled = !getViewerToken();
|
|
2615
|
+
button.setAttribute("data-function-name", options.functionName);
|
|
2616
|
+
if (button.disabled) button.setAttribute("aria-disabled", "true");
|
|
2617
|
+
button.addEventListener("click", async () => {
|
|
2618
|
+
if (button.disabled) return;
|
|
2619
|
+
button.disabled = true;
|
|
2620
|
+
button.textContent = loadingLabel;
|
|
2621
|
+
dispatchButtonEvent(button, "alva:udf-button:loading", {
|
|
2622
|
+
functionName: options.functionName
|
|
2623
|
+
});
|
|
2624
|
+
try {
|
|
2625
|
+
const result = await call(options.functionName, options.params ?? {});
|
|
2626
|
+
dispatchButtonEvent(button, "alva:udf-button:result", {
|
|
2627
|
+
functionName: options.functionName,
|
|
2628
|
+
result
|
|
2629
|
+
});
|
|
2630
|
+
} catch (error) {
|
|
2631
|
+
dispatchButtonEvent(button, "alva:udf-button:error", {
|
|
2632
|
+
functionName: options.functionName,
|
|
2633
|
+
error
|
|
2634
|
+
});
|
|
2635
|
+
} finally {
|
|
2636
|
+
button.disabled = !getViewerToken();
|
|
2637
|
+
button.textContent = button.disabled ? disabledLabel : label;
|
|
2638
|
+
}
|
|
2639
|
+
});
|
|
2640
|
+
container.appendChild(button);
|
|
2641
|
+
return button;
|
|
2642
|
+
};
|
|
2643
|
+
var udf = {
|
|
2644
|
+
call,
|
|
2645
|
+
list,
|
|
2646
|
+
getViewerToken,
|
|
2647
|
+
renderButton,
|
|
2648
|
+
UdfError,
|
|
2649
|
+
UdfAuthRequiredError,
|
|
2650
|
+
UdfTokenExpiredError,
|
|
2651
|
+
UdfTokenInvalidError,
|
|
2652
|
+
UdfPidMismatchError,
|
|
2653
|
+
UdfConsentRequiredError,
|
|
2654
|
+
UdfConsentDeniedError,
|
|
2655
|
+
UdfConsentTimeoutError,
|
|
2656
|
+
UdfInsufficientCredits,
|
|
2657
|
+
UdfFunctionNotFoundError,
|
|
2658
|
+
UdfFunctionDisabledError,
|
|
2659
|
+
UdfRateLimitedError,
|
|
2660
|
+
UdfExecutionError,
|
|
2661
|
+
UdfTransportError
|
|
2662
|
+
};
|
|
2663
|
+
var installPlaybookRuntime = (targetWindow) => {
|
|
2664
|
+
const nextWindow = targetWindow ?? (typeof window === "undefined" ? void 0 : window);
|
|
2665
|
+
if (!nextWindow || installed) return;
|
|
2666
|
+
runtimeWindow = nextWindow;
|
|
2667
|
+
installed = true;
|
|
2668
|
+
parseBootParams(nextWindow);
|
|
2669
|
+
nextWindow.addEventListener("message", onMessage);
|
|
2670
|
+
const target = nextWindow;
|
|
2671
|
+
const existing = target.alva || {};
|
|
2672
|
+
target.alva = {
|
|
2673
|
+
...existing,
|
|
2674
|
+
udf
|
|
2675
|
+
};
|
|
2676
|
+
};
|
|
2677
|
+
|
|
2140
2678
|
// src/index.ts
|
|
2141
|
-
var VERSION = true ? "0.
|
|
2679
|
+
var VERSION = true ? "0.9.1" : "dev";
|
|
2142
2680
|
export {
|
|
2143
2681
|
AlvaClient,
|
|
2144
2682
|
AlvaError,
|
|
2145
|
-
|
|
2683
|
+
PBSV_UPDATE_MESSAGE,
|
|
2684
|
+
UDF_CONSENT_REQUEST_MESSAGE,
|
|
2685
|
+
UDF_CONSENT_RESPONSE_MESSAGE,
|
|
2686
|
+
UdfAuthRequiredError,
|
|
2687
|
+
UdfConsentDeniedError,
|
|
2688
|
+
UdfConsentRequiredError,
|
|
2689
|
+
UdfConsentTimeoutError,
|
|
2690
|
+
UdfError,
|
|
2691
|
+
UdfExecutionError,
|
|
2692
|
+
UdfFunctionDisabledError,
|
|
2693
|
+
UdfFunctionNotFoundError,
|
|
2694
|
+
UdfInsufficientCredits,
|
|
2695
|
+
UdfPidMismatchError,
|
|
2696
|
+
UdfRateLimitedError,
|
|
2697
|
+
UdfTokenExpiredError,
|
|
2698
|
+
UdfTokenInvalidError,
|
|
2699
|
+
UdfTransportError,
|
|
2700
|
+
VERSION,
|
|
2701
|
+
getViewerToken,
|
|
2702
|
+
installPlaybookRuntime,
|
|
2703
|
+
mapUdfErrorResponse,
|
|
2704
|
+
playbookIdFromToken,
|
|
2705
|
+
udf
|
|
2146
2706
|
};
|
|
2147
2707
|
//# sourceMappingURL=index.js.map
|