@alva-ai/toolkit 0.8.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 +10273 -128
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +582 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +209 -1
- package/dist/index.d.ts +209 -1
- package/dist/index.js +559 -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) {
|
|
@@ -1822,6 +1921,38 @@ function buildQuery(params) {
|
|
|
1822
1921
|
return q;
|
|
1823
1922
|
}
|
|
1824
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
|
+
|
|
1825
1956
|
// src/resources/pushSubscriptions.ts
|
|
1826
1957
|
var PushSubscriptionsResource = class {
|
|
1827
1958
|
constructor(client) {
|
|
@@ -1967,12 +2098,17 @@ var AlvaClient = class {
|
|
|
1967
2098
|
baseUrl;
|
|
1968
2099
|
arraysBaseUrl;
|
|
1969
2100
|
viewer_token;
|
|
2101
|
+
pbsvToken;
|
|
1970
2102
|
apiKey;
|
|
2103
|
+
gaClientId;
|
|
2104
|
+
gaSessionId;
|
|
2105
|
+
utmParams;
|
|
1971
2106
|
_fs;
|
|
1972
2107
|
_run;
|
|
1973
2108
|
_deploy;
|
|
1974
2109
|
_release;
|
|
1975
2110
|
_feed;
|
|
2111
|
+
_playbooks;
|
|
1976
2112
|
_secrets;
|
|
1977
2113
|
_sdk;
|
|
1978
2114
|
_dataSkills;
|
|
@@ -1984,13 +2120,18 @@ var AlvaClient = class {
|
|
|
1984
2120
|
_trading;
|
|
1985
2121
|
_arraysJwt;
|
|
1986
2122
|
_notifications;
|
|
2123
|
+
_notificationPreferences;
|
|
1987
2124
|
_pushSubscriptions;
|
|
1988
2125
|
_channelGroupSubscriptions;
|
|
1989
2126
|
constructor(config) {
|
|
1990
2127
|
this.baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
|
|
1991
2128
|
this.arraysBaseUrl = config.arraysBaseUrl ?? DEFAULT_ARRAYS_BASE_URL;
|
|
1992
2129
|
this.viewer_token = config.viewer_token;
|
|
2130
|
+
this.pbsvToken = config.pbsvToken;
|
|
1993
2131
|
this.apiKey = config.apiKey;
|
|
2132
|
+
this.gaClientId = config.gaClientId;
|
|
2133
|
+
this.gaSessionId = config.gaSessionId;
|
|
2134
|
+
this.utmParams = config.utmParams;
|
|
1994
2135
|
}
|
|
1995
2136
|
get fs() {
|
|
1996
2137
|
return this._fs ??= new FsResource(this);
|
|
@@ -2007,6 +2148,9 @@ var AlvaClient = class {
|
|
|
2007
2148
|
get feed() {
|
|
2008
2149
|
return this._feed ??= new FeedResource(this);
|
|
2009
2150
|
}
|
|
2151
|
+
get playbooks() {
|
|
2152
|
+
return this._playbooks ??= new PlaybooksResource(this);
|
|
2153
|
+
}
|
|
2010
2154
|
get secrets() {
|
|
2011
2155
|
return this._secrets ??= new SecretsResource(this);
|
|
2012
2156
|
}
|
|
@@ -2040,6 +2184,9 @@ var AlvaClient = class {
|
|
|
2040
2184
|
get notifications() {
|
|
2041
2185
|
return this._notifications ??= new NotificationsResource(this);
|
|
2042
2186
|
}
|
|
2187
|
+
get notificationPreferences() {
|
|
2188
|
+
return this._notificationPreferences ??= new NotificationPreferencesResource(this);
|
|
2189
|
+
}
|
|
2043
2190
|
get pushSubscriptions() {
|
|
2044
2191
|
return this._pushSubscriptions ??= new PushSubscriptionsResource(this);
|
|
2045
2192
|
}
|
|
@@ -2047,10 +2194,10 @@ var AlvaClient = class {
|
|
|
2047
2194
|
return this._channelGroupSubscriptions ??= new ChannelGroupSubscriptionsResource(this);
|
|
2048
2195
|
}
|
|
2049
2196
|
_requireAuth() {
|
|
2050
|
-
if (!this.viewer_token && !this.apiKey) {
|
|
2197
|
+
if (!this.pbsvToken && !this.viewer_token && !this.apiKey) {
|
|
2051
2198
|
throw new AlvaError(
|
|
2052
2199
|
"UNAUTHENTICATED",
|
|
2053
|
-
"Authentication is required. Pass viewer_token or apiKey in the constructor.",
|
|
2200
|
+
"Authentication is required. Pass pbsvToken, viewer_token, or apiKey in the constructor.",
|
|
2054
2201
|
401
|
|
2055
2202
|
);
|
|
2056
2203
|
}
|
|
@@ -2072,12 +2219,24 @@ var AlvaClient = class {
|
|
|
2072
2219
|
}
|
|
2073
2220
|
const headers = {};
|
|
2074
2221
|
if (!options?.noAuth) {
|
|
2075
|
-
if (this.
|
|
2222
|
+
if (this.pbsvToken) {
|
|
2223
|
+
headers.Authorization = `Bearer ${this.pbsvToken}`;
|
|
2224
|
+
headers["X-Pbsv"] = "1";
|
|
2225
|
+
} else if (this.viewer_token) {
|
|
2076
2226
|
headers["x-Playbook-Viewer"] = this.viewer_token;
|
|
2077
2227
|
} else if (this.apiKey) {
|
|
2078
2228
|
headers["X-Alva-Api-Key"] = this.apiKey;
|
|
2079
2229
|
}
|
|
2080
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
|
+
}
|
|
2081
2240
|
let fetchBody;
|
|
2082
2241
|
if (options?.rawBody !== void 0) {
|
|
2083
2242
|
headers["Content-Type"] = "application/octet-stream";
|
|
@@ -2137,11 +2296,380 @@ var AlvaClient = class {
|
|
|
2137
2296
|
}
|
|
2138
2297
|
};
|
|
2139
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
|
+
|
|
2140
2646
|
// src/index.ts
|
|
2141
|
-
var VERSION = true ? "0.
|
|
2647
|
+
var VERSION = true ? "0.9.0" : "dev";
|
|
2142
2648
|
export {
|
|
2143
2649
|
AlvaClient,
|
|
2144
2650
|
AlvaError,
|
|
2145
|
-
|
|
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
|
|
2146
2674
|
};
|
|
2147
2675
|
//# sourceMappingURL=index.js.map
|