playkit-sdk 1.2.3 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/playkit-sdk.cjs.js +113 -12
- package/dist/playkit-sdk.cjs.js.map +1 -1
- package/dist/playkit-sdk.d.ts +102 -8
- package/dist/playkit-sdk.esm.js +112 -13
- package/dist/playkit-sdk.esm.js.map +1 -1
- package/dist/playkit-sdk.umd.js +113 -12
- package/dist/playkit-sdk.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/playkit-sdk.cjs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* playkit-sdk v1.2.
|
|
2
|
+
* playkit-sdk v1.2.5
|
|
3
3
|
* PlayKit SDK for JavaScript
|
|
4
4
|
* @license SEE LICENSE IN LICENSE
|
|
5
5
|
*/
|
|
@@ -12,6 +12,41 @@ var EventEmitter = require('eventemitter3');
|
|
|
12
12
|
/**
|
|
13
13
|
* Common types used across the SDK
|
|
14
14
|
*/
|
|
15
|
+
/**
|
|
16
|
+
* Helper to create a text message
|
|
17
|
+
*/
|
|
18
|
+
function createTextMessage(role, text) {
|
|
19
|
+
return { role, content: text };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Helper to create a multimodal message with text and images
|
|
23
|
+
*/
|
|
24
|
+
function createMultimodalMessage(role, text, images, audios) {
|
|
25
|
+
const content = [];
|
|
26
|
+
// Add text part first
|
|
27
|
+
if (text) {
|
|
28
|
+
content.push({ type: 'text', text });
|
|
29
|
+
}
|
|
30
|
+
// Add image parts
|
|
31
|
+
if (images) {
|
|
32
|
+
for (const img of images) {
|
|
33
|
+
content.push({
|
|
34
|
+
type: 'image_url',
|
|
35
|
+
image_url: { url: img.url, detail: img.detail },
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Add audio parts
|
|
40
|
+
if (audios) {
|
|
41
|
+
for (const audio of audios) {
|
|
42
|
+
content.push({
|
|
43
|
+
type: 'input_audio',
|
|
44
|
+
input_audio: { data: audio.data, format: audio.format },
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return { role, content };
|
|
49
|
+
}
|
|
15
50
|
/**
|
|
16
51
|
* Error types that can be thrown by the SDK
|
|
17
52
|
*/
|
|
@@ -1610,12 +1645,13 @@ const translations = {
|
|
|
1610
1645
|
* RechargeManager handles the recharge modal UI and recharge window opening
|
|
1611
1646
|
*/
|
|
1612
1647
|
class RechargeManager extends EventEmitter {
|
|
1613
|
-
constructor(playerToken, rechargePortalUrl = 'https://playkit.ai/recharge') {
|
|
1648
|
+
constructor(playerToken, rechargePortalUrl = 'https://playkit.ai/recharge', gameId) {
|
|
1614
1649
|
super();
|
|
1615
1650
|
this.modalContainer = null;
|
|
1616
1651
|
this.styleElement = null;
|
|
1617
1652
|
this.playerToken = playerToken;
|
|
1618
1653
|
this.rechargePortalUrl = rechargePortalUrl;
|
|
1654
|
+
this.gameId = gameId;
|
|
1619
1655
|
this.language = this.detectLanguage();
|
|
1620
1656
|
}
|
|
1621
1657
|
/**
|
|
@@ -1644,10 +1680,15 @@ class RechargeManager extends EventEmitter {
|
|
|
1644
1680
|
return translations[this.language][key];
|
|
1645
1681
|
}
|
|
1646
1682
|
/**
|
|
1647
|
-
* Build recharge URL with player token
|
|
1683
|
+
* Build recharge URL with player token and gameId
|
|
1648
1684
|
*/
|
|
1649
1685
|
buildRechargeUrl() {
|
|
1650
|
-
|
|
1686
|
+
let url = `${this.rechargePortalUrl}?playerToken=${encodeURIComponent(this.playerToken)}`;
|
|
1687
|
+
// Add gameId to URL so recharge page can fetch correct owner's wallet
|
|
1688
|
+
if (this.gameId) {
|
|
1689
|
+
url += `&gameId=${encodeURIComponent(this.gameId)}`;
|
|
1690
|
+
}
|
|
1691
|
+
return url;
|
|
1651
1692
|
}
|
|
1652
1693
|
/**
|
|
1653
1694
|
* Open recharge window in a new tab
|
|
@@ -1954,6 +1995,7 @@ class PlayerClient extends EventEmitter {
|
|
|
1954
1995
|
this.balanceCheckInterval = null;
|
|
1955
1996
|
this.authManager = authManager;
|
|
1956
1997
|
this.baseURL = config.baseURL || DEFAULT_BASE_URL$3;
|
|
1998
|
+
this.gameId = config.gameId;
|
|
1957
1999
|
this.rechargeConfig = {
|
|
1958
2000
|
autoShowBalanceModal: (_a = rechargeConfig.autoShowBalanceModal) !== null && _a !== void 0 ? _a : true,
|
|
1959
2001
|
balanceCheckInterval: (_b = rechargeConfig.balanceCheckInterval) !== null && _b !== void 0 ? _b : 30000,
|
|
@@ -1979,11 +2021,16 @@ class PlayerClient extends EventEmitter {
|
|
|
1979
2021
|
};
|
|
1980
2022
|
}
|
|
1981
2023
|
try {
|
|
2024
|
+
// Build headers with X-Game-Id to support Global Developer Token
|
|
2025
|
+
const headers = {
|
|
2026
|
+
Authorization: `Bearer ${token}`,
|
|
2027
|
+
};
|
|
2028
|
+
if (this.gameId) {
|
|
2029
|
+
headers['X-Game-Id'] = this.gameId;
|
|
2030
|
+
}
|
|
1982
2031
|
const response = await fetch(`${this.baseURL}${PLAYER_INFO_ENDPOINT}`, {
|
|
1983
2032
|
method: 'GET',
|
|
1984
|
-
headers
|
|
1985
|
-
Authorization: `Bearer ${token}`,
|
|
1986
|
-
},
|
|
2033
|
+
headers,
|
|
1987
2034
|
});
|
|
1988
2035
|
if (!response.ok) {
|
|
1989
2036
|
const error = await response.json().catch(() => ({ message: 'Failed to get player info' }));
|
|
@@ -2100,7 +2147,7 @@ class PlayerClient extends EventEmitter {
|
|
|
2100
2147
|
initializeRechargeManager() {
|
|
2101
2148
|
const token = this.authManager.getToken();
|
|
2102
2149
|
if (token && !this.rechargeManager) {
|
|
2103
|
-
this.rechargeManager = new RechargeManager(token, this.rechargeConfig.rechargePortalUrl);
|
|
2150
|
+
this.rechargeManager = new RechargeManager(token, this.rechargeConfig.rechargePortalUrl, this.gameId);
|
|
2104
2151
|
// Forward recharge events
|
|
2105
2152
|
this.rechargeManager.on('recharge_opened', () => this.emit('recharge_opened'));
|
|
2106
2153
|
this.rechargeManager.on('recharge_modal_shown', () => this.emit('recharge_modal_shown'));
|
|
@@ -2585,11 +2632,18 @@ class ImageProvider {
|
|
|
2585
2632
|
const endpoint = `/ai/${this.config.gameId}/v2/image`;
|
|
2586
2633
|
const requestBody = {
|
|
2587
2634
|
model,
|
|
2588
|
-
prompt: imageConfig.prompt,
|
|
2589
2635
|
n: imageConfig.n || 1,
|
|
2590
2636
|
size: imageConfig.size || '1024x1024',
|
|
2591
2637
|
seed: imageConfig.seed || null,
|
|
2592
2638
|
};
|
|
2639
|
+
// Add prompt if provided
|
|
2640
|
+
if (imageConfig.prompt) {
|
|
2641
|
+
requestBody.prompt = imageConfig.prompt;
|
|
2642
|
+
}
|
|
2643
|
+
// Add input images for img2img
|
|
2644
|
+
if (imageConfig.images && imageConfig.images.length > 0) {
|
|
2645
|
+
requestBody.images = imageConfig.images.map(img => img.data);
|
|
2646
|
+
}
|
|
2593
2647
|
// Add optional quality and style if provided (for DALL-E models)
|
|
2594
2648
|
if (imageConfig.quality) {
|
|
2595
2649
|
requestBody.quality = imageConfig.quality;
|
|
@@ -2597,6 +2651,10 @@ class ImageProvider {
|
|
|
2597
2651
|
if (imageConfig.style) {
|
|
2598
2652
|
requestBody.style = imageConfig.style;
|
|
2599
2653
|
}
|
|
2654
|
+
// Add transparent option for background removal
|
|
2655
|
+
if (imageConfig.transparent) {
|
|
2656
|
+
requestBody.transparent = true;
|
|
2657
|
+
}
|
|
2600
2658
|
try {
|
|
2601
2659
|
const response = await fetch(`${this.baseURL}${endpoint}`, {
|
|
2602
2660
|
method: 'POST',
|
|
@@ -3200,12 +3258,14 @@ class ChatClient {
|
|
|
3200
3258
|
* Implementation of GeneratedImage interface
|
|
3201
3259
|
*/
|
|
3202
3260
|
class GeneratedImageImpl {
|
|
3203
|
-
constructor(base64, originalPrompt, revisedPrompt, size) {
|
|
3261
|
+
constructor(base64, originalPrompt, revisedPrompt, size, originalBase64, transparentSuccess) {
|
|
3204
3262
|
this.base64 = base64;
|
|
3205
3263
|
this.originalPrompt = originalPrompt;
|
|
3206
3264
|
this.revisedPrompt = revisedPrompt;
|
|
3207
3265
|
this.generatedAt = Date.now();
|
|
3208
3266
|
this.size = size;
|
|
3267
|
+
this.originalBase64 = originalBase64;
|
|
3268
|
+
this.transparentSuccess = transparentSuccess;
|
|
3209
3269
|
}
|
|
3210
3270
|
toDataURL() {
|
|
3211
3271
|
return `data:image/png;base64,${this.base64}`;
|
|
@@ -3234,7 +3294,7 @@ class ImageClient {
|
|
|
3234
3294
|
if (!imageData || !imageData.b64_json) {
|
|
3235
3295
|
throw new Error('No image data in response');
|
|
3236
3296
|
}
|
|
3237
|
-
return new GeneratedImageImpl(imageData.b64_json, config.prompt, imageData.revised_prompt, config.size);
|
|
3297
|
+
return new GeneratedImageImpl(imageData.b64_json, config.prompt, imageData.revised_prompt, config.size, imageData.b64_json_original, imageData.transparent_success);
|
|
3238
3298
|
}
|
|
3239
3299
|
/**
|
|
3240
3300
|
* Generate multiple images
|
|
@@ -3246,7 +3306,7 @@ class ImageClient {
|
|
|
3246
3306
|
if (!imageData.b64_json) {
|
|
3247
3307
|
throw new Error('No image data in response');
|
|
3248
3308
|
}
|
|
3249
|
-
return new GeneratedImageImpl(imageData.b64_json, config.prompt, imageData.revised_prompt, config.size);
|
|
3309
|
+
return new GeneratedImageImpl(imageData.b64_json, config.prompt, imageData.revised_prompt, config.size, imageData.b64_json_original, imageData.transparent_success);
|
|
3250
3310
|
});
|
|
3251
3311
|
}
|
|
3252
3312
|
/**
|
|
@@ -3255,6 +3315,45 @@ class ImageClient {
|
|
|
3255
3315
|
async generate(prompt, size) {
|
|
3256
3316
|
return this.generateImage({ prompt, size });
|
|
3257
3317
|
}
|
|
3318
|
+
/**
|
|
3319
|
+
* Image-to-image generation
|
|
3320
|
+
* @param images - Input images (base64 encoded)
|
|
3321
|
+
* @param prompt - Optional prompt to guide the generation
|
|
3322
|
+
* @param options - Additional generation options
|
|
3323
|
+
*/
|
|
3324
|
+
async img2img(images, prompt, options) {
|
|
3325
|
+
return this.generateImage(Object.assign({ images,
|
|
3326
|
+
prompt }, options));
|
|
3327
|
+
}
|
|
3328
|
+
/**
|
|
3329
|
+
* Convert a data URL to ImageInput format
|
|
3330
|
+
* @param dataUrl - Data URL (e.g., 'data:image/png;base64,...')
|
|
3331
|
+
*/
|
|
3332
|
+
static dataUrlToImageInput(dataUrl) {
|
|
3333
|
+
const match = dataUrl.match(/^data:([^;]+);base64,(.+)$/);
|
|
3334
|
+
if (!match) {
|
|
3335
|
+
throw new Error('Invalid data URL format');
|
|
3336
|
+
}
|
|
3337
|
+
return {
|
|
3338
|
+
mediaType: match[1],
|
|
3339
|
+
data: match[2],
|
|
3340
|
+
};
|
|
3341
|
+
}
|
|
3342
|
+
/**
|
|
3343
|
+
* Convert a File/Blob to ImageInput format (browser only)
|
|
3344
|
+
* @param file - File or Blob object
|
|
3345
|
+
*/
|
|
3346
|
+
static async fileToImageInput(file) {
|
|
3347
|
+
return new Promise((resolve, reject) => {
|
|
3348
|
+
const reader = new FileReader();
|
|
3349
|
+
reader.onload = () => {
|
|
3350
|
+
const result = reader.result;
|
|
3351
|
+
resolve(ImageClient.dataUrlToImageInput(result));
|
|
3352
|
+
};
|
|
3353
|
+
reader.onerror = () => reject(new Error('Failed to read file'));
|
|
3354
|
+
reader.readAsDataURL(file);
|
|
3355
|
+
});
|
|
3356
|
+
}
|
|
3258
3357
|
}
|
|
3259
3358
|
|
|
3260
3359
|
/**
|
|
@@ -4859,6 +4958,8 @@ exports.SchemaLibrary = SchemaLibrary;
|
|
|
4859
4958
|
exports.StreamParser = StreamParser;
|
|
4860
4959
|
exports.TokenStorage = TokenStorage;
|
|
4861
4960
|
exports.TranscriptionClient = TranscriptionClient;
|
|
4961
|
+
exports.createMultimodalMessage = createMultimodalMessage;
|
|
4962
|
+
exports.createTextMessage = createTextMessage;
|
|
4862
4963
|
exports.default = PlayKitSDK;
|
|
4863
4964
|
exports.defaultContextManager = defaultContextManager;
|
|
4864
4965
|
exports.defaultSchemaLibrary = defaultSchemaLibrary;
|