playkit-sdk 1.2.13 → 1.4.0-beta.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/LICENSE +86 -86
- package/README.md +266 -244
- package/dist/playkit-sdk.cjs.js +1948 -1577
- package/dist/playkit-sdk.cjs.js.map +1 -1
- package/dist/playkit-sdk.d.ts +201 -7
- package/dist/playkit-sdk.esm.js +1948 -1578
- package/dist/playkit-sdk.esm.js.map +1 -1
- package/dist/playkit-sdk.umd.js +2042 -1646
- package/dist/playkit-sdk.umd.js.map +1 -1
- package/package.json +72 -70
package/README.md
CHANGED
|
@@ -1,244 +1,266 @@
|
|
|
1
|
-
# PlayKit SDK for JavaScript
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/playkit)
|
|
4
|
-
|
|
5
|
-
JavaScript/TypeScript SDK for integrating AI capabilities into web-based games.
|
|
6
|
-
|
|
7
|
-
## Features
|
|
8
|
-
|
|
9
|
-
- AI-powered text generation using GPT models
|
|
10
|
-
- Image generation using DALL-E models
|
|
11
|
-
- NPC conversation management with automatic history tracking
|
|
12
|
-
- JWT-based authentication and token management
|
|
13
|
-
- Real-time streaming responses
|
|
14
|
-
- Framework-agnostic design (compatible with P5.js, Phaser, PixiJS, etc.)
|
|
15
|
-
- Multiple bundle formats (ESM, CJS, UMD)
|
|
16
|
-
- Encrypted token storage using Web Crypto API
|
|
17
|
-
- Full TypeScript support with type definitions
|
|
18
|
-
- Player balance management and recharge functionality
|
|
19
|
-
|
|
20
|
-
## Installation
|
|
21
|
-
|
|
22
|
-
```bash
|
|
23
|
-
npm install playkit-sdk
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
## Quick Start
|
|
27
|
-
|
|
28
|
-
### Basic Setup
|
|
29
|
-
|
|
30
|
-
```typescript
|
|
31
|
-
import { PlayKitSDK } from 'playkit-sdk';
|
|
32
|
-
|
|
33
|
-
const sdk = new PlayKitSDK({
|
|
34
|
-
gameId: 'your-game-id',
|
|
35
|
-
developerToken: 'your-dev-token', // For development
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
await sdk.initialize();
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
### Text Generation
|
|
42
|
-
|
|
43
|
-
```typescript
|
|
44
|
-
const chat = sdk.createChatClient('gpt-4o-mini');
|
|
45
|
-
|
|
46
|
-
// Simple chat
|
|
47
|
-
const response = await chat.chat('Hello, introduce yourself');
|
|
48
|
-
console.log(response);
|
|
49
|
-
|
|
50
|
-
// With system prompt
|
|
51
|
-
const response = await chat.chat(
|
|
52
|
-
'How should I explore this dungeon?',
|
|
53
|
-
'You are a wise dungeon guide.'
|
|
54
|
-
);
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### Streaming Text
|
|
58
|
-
|
|
59
|
-
```typescript
|
|
60
|
-
await chat.chatStream(
|
|
61
|
-
'Tell a story about a brave knight',
|
|
62
|
-
(chunk) => {
|
|
63
|
-
process.stdout.write(chunk);
|
|
64
|
-
},
|
|
65
|
-
(fullText) => {
|
|
66
|
-
console.log('\nComplete:', fullText);
|
|
67
|
-
}
|
|
68
|
-
);
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### Image Generation
|
|
72
|
-
|
|
73
|
-
```typescript
|
|
74
|
-
const imageClient = sdk.createImageClient('dall-e-3');
|
|
75
|
-
|
|
76
|
-
const image = await imageClient.generate('A futuristic cyberpunk city at night');
|
|
77
|
-
|
|
78
|
-
console.log('Base64:', image.base64);
|
|
79
|
-
console.log('Data URL:', image.toDataURL());
|
|
80
|
-
|
|
81
|
-
// Display in browser
|
|
82
|
-
const imgElement = await image.toHTMLImage();
|
|
83
|
-
document.body.appendChild(imgElement);
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
###
|
|
87
|
-
|
|
88
|
-
```typescript
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
1
|
+
# PlayKit SDK for JavaScript
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/playkit)
|
|
4
|
+
|
|
5
|
+
JavaScript/TypeScript SDK for integrating AI capabilities into web-based games.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- AI-powered text generation using GPT models
|
|
10
|
+
- Image generation using DALL-E models
|
|
11
|
+
- NPC conversation management with automatic history tracking
|
|
12
|
+
- JWT-based authentication and token management
|
|
13
|
+
- Real-time streaming responses
|
|
14
|
+
- Framework-agnostic design (compatible with P5.js, Phaser, PixiJS, etc.)
|
|
15
|
+
- Multiple bundle formats (ESM, CJS, UMD)
|
|
16
|
+
- Encrypted token storage using Web Crypto API
|
|
17
|
+
- Full TypeScript support with type definitions
|
|
18
|
+
- Player balance management and recharge functionality
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install playkit-sdk
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
### Basic Setup
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { PlayKitSDK } from 'playkit-sdk';
|
|
32
|
+
|
|
33
|
+
const sdk = new PlayKitSDK({
|
|
34
|
+
gameId: 'your-game-id',
|
|
35
|
+
developerToken: 'your-dev-token', // For development
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
await sdk.initialize();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Text Generation
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
const chat = sdk.createChatClient('gpt-4o-mini');
|
|
45
|
+
|
|
46
|
+
// Simple chat
|
|
47
|
+
const response = await chat.chat('Hello, introduce yourself');
|
|
48
|
+
console.log(response);
|
|
49
|
+
|
|
50
|
+
// With system prompt
|
|
51
|
+
const response = await chat.chat(
|
|
52
|
+
'How should I explore this dungeon?',
|
|
53
|
+
'You are a wise dungeon guide.'
|
|
54
|
+
);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Streaming Text
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
await chat.chatStream(
|
|
61
|
+
'Tell a story about a brave knight',
|
|
62
|
+
(chunk) => {
|
|
63
|
+
process.stdout.write(chunk);
|
|
64
|
+
},
|
|
65
|
+
(fullText) => {
|
|
66
|
+
console.log('\nComplete:', fullText);
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Image Generation
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
const imageClient = sdk.createImageClient('dall-e-3');
|
|
75
|
+
|
|
76
|
+
const image = await imageClient.generate('A futuristic cyberpunk city at night');
|
|
77
|
+
|
|
78
|
+
console.log('Base64:', image.base64);
|
|
79
|
+
console.log('Data URL:', image.toDataURL());
|
|
80
|
+
|
|
81
|
+
// Display in browser
|
|
82
|
+
const imgElement = await image.toHTMLImage();
|
|
83
|
+
document.body.appendChild(imgElement);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Text-to-Speech (TTS)
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
const tts = sdk.createTTSClient(); // defaults to 'default-tts-model'
|
|
90
|
+
|
|
91
|
+
// Get raw audio bytes plus usage metadata
|
|
92
|
+
const result = await tts.synthesize({
|
|
93
|
+
text: 'Welcome to the game, brave adventurer!',
|
|
94
|
+
voice: 'male-qn-qingse',
|
|
95
|
+
format: 'mp3',
|
|
96
|
+
});
|
|
97
|
+
console.log('Characters billed:', result.usageCharacters);
|
|
98
|
+
console.log('Audio length (ms):', result.audioLengthMs);
|
|
99
|
+
|
|
100
|
+
// Or get a playable object URL directly (browser)
|
|
101
|
+
const url = await tts.synthesizeToObjectURL({ text: 'Hello there!' });
|
|
102
|
+
const audio = new Audio(url);
|
|
103
|
+
audio.play();
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### NPC Conversations
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const npc = sdk.createNPCClient({
|
|
110
|
+
systemPrompt: 'You are a mysterious wizard who speaks in riddles.',
|
|
111
|
+
temperature: 0.8,
|
|
112
|
+
maxHistoryLength: 20,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const reply1 = await npc.talk('Who are you?');
|
|
116
|
+
console.log('Wizard:', reply1);
|
|
117
|
+
|
|
118
|
+
const reply2 = await npc.talk('What is your quest?');
|
|
119
|
+
console.log('Wizard:', reply2);
|
|
120
|
+
|
|
121
|
+
// Save/load history
|
|
122
|
+
const savedHistory = npc.saveHistory();
|
|
123
|
+
localStorage.setItem('npc_history', savedHistory);
|
|
124
|
+
|
|
125
|
+
// Later...
|
|
126
|
+
npc.loadHistory(localStorage.getItem('npc_history'));
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Player Balance Management
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
// Get player info and balance
|
|
133
|
+
const playerInfo = await sdk.getPlayerInfo();
|
|
134
|
+
console.log('Player ID:', playerInfo.userId);
|
|
135
|
+
console.log('Credits:', playerInfo.credits);
|
|
136
|
+
|
|
137
|
+
// Open recharge window
|
|
138
|
+
sdk.openRechargeWindow();
|
|
139
|
+
|
|
140
|
+
// Show insufficient balance modal
|
|
141
|
+
await sdk.showInsufficientBalanceModal();
|
|
142
|
+
|
|
143
|
+
// Enable automatic balance checking
|
|
144
|
+
sdk.enableAutoBalanceCheck(30000); // Check every 30 seconds
|
|
145
|
+
|
|
146
|
+
// Listen to balance events
|
|
147
|
+
sdk.on('balance_updated', (credits) => {
|
|
148
|
+
console.log('New balance:', credits);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
sdk.on('insufficient_credits', (error) => {
|
|
152
|
+
console.log('User needs to recharge');
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Usage with P5.js
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
let sdk, npc, generatedImage;
|
|
160
|
+
|
|
161
|
+
async function setup() {
|
|
162
|
+
createCanvas(800, 600);
|
|
163
|
+
|
|
164
|
+
sdk = new PlayKitSDK({
|
|
165
|
+
gameId: 'your-game-id',
|
|
166
|
+
developerToken: 'your-dev-token'
|
|
167
|
+
});
|
|
168
|
+
await sdk.initialize();
|
|
169
|
+
|
|
170
|
+
npc = sdk.createNPCClient({
|
|
171
|
+
systemPrompt: 'You are a friendly game character.'
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function mousePressed() {
|
|
176
|
+
const reply = await npc.talk('Hello!');
|
|
177
|
+
console.log(reply);
|
|
178
|
+
|
|
179
|
+
const imageClient = sdk.createImageClient();
|
|
180
|
+
const img = await imageClient.generate('A magical forest');
|
|
181
|
+
|
|
182
|
+
const htmlImg = await img.toHTMLImage();
|
|
183
|
+
generatedImage = loadImage(htmlImg.src);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function draw() {
|
|
187
|
+
background(220);
|
|
188
|
+
if (generatedImage) {
|
|
189
|
+
image(generatedImage, 0, 0, 400, 400);
|
|
190
|
+
}
|
|
191
|
+
text('Click to talk to NPC or generate image', 10, height - 20);
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Usage with Vanilla JavaScript
|
|
196
|
+
|
|
197
|
+
```html
|
|
198
|
+
<!DOCTYPE html>
|
|
199
|
+
<html>
|
|
200
|
+
<head>
|
|
201
|
+
<script src="https://unpkg.com/playkit-sdk@latest/dist/playkit-sdk.umd.js"></script>
|
|
202
|
+
</head>
|
|
203
|
+
<body>
|
|
204
|
+
<div id="output"></div>
|
|
205
|
+
<input id="userInput" type="text" placeholder="Type a message...">
|
|
206
|
+
<button onclick="sendMessage()">Send</button>
|
|
207
|
+
|
|
208
|
+
<script>
|
|
209
|
+
let sdk, chat;
|
|
210
|
+
|
|
211
|
+
async function init() {
|
|
212
|
+
// window.PlayKitSDK is the constructor itself.
|
|
213
|
+
// Legacy form `new PlayKitSDK.PlayKitSDK({ ... })` still works for v1.x BC.
|
|
214
|
+
sdk = new PlayKitSDK({
|
|
215
|
+
gameId: 'your-game-id',
|
|
216
|
+
developerToken: 'your-dev-token'
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
await sdk.initialize();
|
|
220
|
+
chat = sdk.createChatClient();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async function sendMessage() {
|
|
224
|
+
const input = document.getElementById('userInput').value;
|
|
225
|
+
const output = document.getElementById('output');
|
|
226
|
+
|
|
227
|
+
output.innerHTML += `<p><strong>You:</strong> ${input}</p>`;
|
|
228
|
+
output.innerHTML += `<p><strong>AI:</strong> <span id="aiReply"></span></p>`;
|
|
229
|
+
|
|
230
|
+
const replyElement = document.getElementById('aiReply');
|
|
231
|
+
await chat.chatStream(
|
|
232
|
+
input,
|
|
233
|
+
(chunk) => { replyElement.innerHTML += chunk; }
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
document.getElementById('userInput').value = '';
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
init();
|
|
240
|
+
</script>
|
|
241
|
+
</body>
|
|
242
|
+
</html>
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
## License
|
|
247
|
+
|
|
248
|
+
Proprietary License - see [LICENSE](LICENSE) file for details.
|
|
249
|
+
|
|
250
|
+
This SDK is proprietary software owned by Agentland Lab. Use of this SDK is subject to the terms and conditions of the license agreement.
|
|
251
|
+
|
|
252
|
+
## Support
|
|
253
|
+
|
|
254
|
+
- Email: support@playkit.ai
|
|
255
|
+
- Issues: [GitHub Issues](https://github.com/cnqdztp/PlayKit-JavascriptSDK/issues)
|
|
256
|
+
|
|
257
|
+
## Changelog
|
|
258
|
+
|
|
259
|
+
### 1.0.0-beta.1
|
|
260
|
+
- Initial public beta release
|
|
261
|
+
- AI chat support (text generation)
|
|
262
|
+
- Image generation support
|
|
263
|
+
- NPC conversation management
|
|
264
|
+
- Authentication and player management
|
|
265
|
+
- Streaming response support
|
|
266
|
+
- Player balance management and recharge functionality
|