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/README.md CHANGED
@@ -1,244 +1,266 @@
1
- # PlayKit SDK for JavaScript
2
-
3
- [![npm version](https://img.shields.io/npm/v/playkit-sdk.svg)](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
- ### NPC Conversations
87
-
88
- ```typescript
89
- const npc = sdk.createNPCClient({
90
- systemPrompt: 'You are a mysterious wizard who speaks in riddles.',
91
- temperature: 0.8,
92
- maxHistoryLength: 20,
93
- });
94
-
95
- const reply1 = await npc.talk('Who are you?');
96
- console.log('Wizard:', reply1);
97
-
98
- const reply2 = await npc.talk('What is your quest?');
99
- console.log('Wizard:', reply2);
100
-
101
- // Save/load history
102
- const savedHistory = npc.saveHistory();
103
- localStorage.setItem('npc_history', savedHistory);
104
-
105
- // Later...
106
- npc.loadHistory(localStorage.getItem('npc_history'));
107
- ```
108
-
109
- ### Player Balance Management
110
-
111
- ```typescript
112
- // Get player info and balance
113
- const playerInfo = await sdk.getPlayerInfo();
114
- console.log('Player ID:', playerInfo.userId);
115
- console.log('Credits:', playerInfo.credits);
116
-
117
- // Open recharge window
118
- sdk.openRechargeWindow();
119
-
120
- // Show insufficient balance modal
121
- await sdk.showInsufficientBalanceModal();
122
-
123
- // Enable automatic balance checking
124
- sdk.enableAutoBalanceCheck(30000); // Check every 30 seconds
125
-
126
- // Listen to balance events
127
- sdk.on('balance_updated', (credits) => {
128
- console.log('New balance:', credits);
129
- });
130
-
131
- sdk.on('insufficient_credits', (error) => {
132
- console.log('User needs to recharge');
133
- });
134
- ```
135
-
136
- ## Usage with P5.js
137
-
138
- ```javascript
139
- let sdk, npc, generatedImage;
140
-
141
- async function setup() {
142
- createCanvas(800, 600);
143
-
144
- sdk = new PlayKitSDK({
145
- gameId: 'your-game-id',
146
- developerToken: 'your-dev-token'
147
- });
148
- await sdk.initialize();
149
-
150
- npc = sdk.createNPCClient({
151
- systemPrompt: 'You are a friendly game character.'
152
- });
153
- }
154
-
155
- async function mousePressed() {
156
- const reply = await npc.talk('Hello!');
157
- console.log(reply);
158
-
159
- const imageClient = sdk.createImageClient();
160
- const img = await imageClient.generate('A magical forest');
161
-
162
- const htmlImg = await img.toHTMLImage();
163
- generatedImage = loadImage(htmlImg.src);
164
- }
165
-
166
- function draw() {
167
- background(220);
168
- if (generatedImage) {
169
- image(generatedImage, 0, 0, 400, 400);
170
- }
171
- text('Click to talk to NPC or generate image', 10, height - 20);
172
- }
173
- ```
174
-
175
- ## Usage with Vanilla JavaScript
176
-
177
- ```html
178
- <!DOCTYPE html>
179
- <html>
180
- <head>
181
- <script src="https://unpkg.com/playkit-sdk@latest/dist/index.umd.js"></script>
182
- </head>
183
- <body>
184
- <div id="output"></div>
185
- <input id="userInput" type="text" placeholder="Type a message...">
186
- <button onclick="sendMessage()">Send</button>
187
-
188
- <script>
189
- let sdk, chat;
190
-
191
- async function init() {
192
- sdk = new PlayKitSDK.PlayKitSDK({
193
- gameId: 'your-game-id',
194
- developerToken: 'your-dev-token'
195
- });
196
-
197
- await sdk.initialize();
198
- chat = sdk.createChatClient();
199
- }
200
-
201
- async function sendMessage() {
202
- const input = document.getElementById('userInput').value;
203
- const output = document.getElementById('output');
204
-
205
- output.innerHTML += `<p><strong>You:</strong> ${input}</p>`;
206
- output.innerHTML += `<p><strong>AI:</strong> <span id="aiReply"></span></p>`;
207
-
208
- const replyElement = document.getElementById('aiReply');
209
- await chat.chatStream(
210
- input,
211
- (chunk) => { replyElement.innerHTML += chunk; }
212
- );
213
-
214
- document.getElementById('userInput').value = '';
215
- }
216
-
217
- init();
218
- </script>
219
- </body>
220
- </html>
221
- ```
222
-
223
-
224
- ## License
225
-
226
- Proprietary License - see [LICENSE](LICENSE) file for details.
227
-
228
- This SDK is proprietary software owned by Agentland Lab. Use of this SDK is subject to the terms and conditions of the license agreement.
229
-
230
- ## Support
231
-
232
- - Email: support@playkit.ai
233
- - Issues: [GitHub Issues](https://github.com/cnqdztp/PlayKit-JavascriptSDK/issues)
234
-
235
- ## Changelog
236
-
237
- ### 1.0.0-beta.1
238
- - Initial public beta release
239
- - AI chat support (text generation)
240
- - Image generation support
241
- - NPC conversation management
242
- - Authentication and player management
243
- - Streaming response support
244
- - Player balance management and recharge functionality
1
+ # PlayKit SDK for JavaScript
2
+
3
+ [![npm version](https://img.shields.io/npm/v/playkit-sdk.svg)](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