@kokimoki/app 2.0.0 → 2.0.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/dist/core/index.d.ts +3 -0
- package/dist/core/index.js +3 -0
- package/dist/core/kokimoki-client.d.ts +361 -0
- package/dist/core/kokimoki-client.js +819 -0
- package/dist/core/room-subscription-mode.d.ts +5 -0
- package/dist/core/room-subscription-mode.js +6 -0
- package/dist/core/room-subscription.d.ts +15 -0
- package/dist/core/room-subscription.js +53 -0
- package/dist/index.d.ts +4 -7
- package/dist/index.js +4 -7
- package/dist/kokimoki.min.d.ts +54 -58
- package/dist/kokimoki.min.js +3080 -1794
- package/dist/kokimoki.min.js.map +1 -1
- package/dist/llms.txt +75 -67
- package/dist/protocol/ws-message/index.d.ts +3 -0
- package/dist/protocol/ws-message/index.js +3 -0
- package/dist/protocol/ws-message/reader.d.ts +11 -0
- package/dist/protocol/ws-message/reader.js +36 -0
- package/dist/protocol/ws-message/type.d.ts +11 -0
- package/dist/protocol/ws-message/type.js +12 -0
- package/dist/protocol/ws-message/writer.d.ts +9 -0
- package/dist/protocol/ws-message/writer.js +45 -0
- package/dist/services/index.d.ts +3 -0
- package/dist/services/index.js +3 -0
- package/dist/services/kokimoki-ai.d.ts +153 -0
- package/dist/services/kokimoki-ai.js +164 -0
- package/dist/services/kokimoki-leaderboard.d.ts +175 -0
- package/dist/services/kokimoki-leaderboard.js +203 -0
- package/dist/services/kokimoki-storage.d.ts +155 -0
- package/dist/services/kokimoki-storage.js +208 -0
- package/dist/stores/index.d.ts +3 -0
- package/dist/stores/index.js +3 -0
- package/dist/stores/kokimoki-local-store.d.ts +11 -0
- package/dist/stores/kokimoki-local-store.js +40 -0
- package/dist/stores/kokimoki-store.d.ts +22 -0
- package/dist/stores/kokimoki-store.js +117 -0
- package/dist/stores/kokimoki-transaction.d.ts +18 -0
- package/dist/stores/kokimoki-transaction.js +143 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +3 -0
- package/dist/utils/valtio.d.ts +7 -0
- package/dist/utils/valtio.js +6 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +2 -1
- package/package.json +4 -3
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import type TypedEmitter from "typed-emitter";
|
|
2
|
+
import { KokimokiAiService, KokimokiLeaderboardService, KokimokiStorageService } from "../services";
|
|
3
|
+
import { KokimokiLocalStore, KokimokiStore } from "../stores";
|
|
4
|
+
import type { KokimokiClientEvents } from "../types";
|
|
5
|
+
type Mutable<T> = {
|
|
6
|
+
-readonly [K in keyof T]: T[K] extends object ? Mutable<T[K]> : T[K];
|
|
7
|
+
};
|
|
8
|
+
type StoreValue<S> = S extends KokimokiStore<infer U> ? Mutable<U> : never;
|
|
9
|
+
declare const KokimokiClient_base: {
|
|
10
|
+
new (): TypedEmitter<KokimokiClientEvents>;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Kokimoki Client - Real-time Collaborative Game Development SDK
|
|
14
|
+
*
|
|
15
|
+
* The main entry point for building multiplayer games and collaborative applications.
|
|
16
|
+
* Provides real-time state synchronization, AI integration, cloud storage, leaderboards,
|
|
17
|
+
* and more - all without complex backend setup.
|
|
18
|
+
*
|
|
19
|
+
* **Core Capabilities:**
|
|
20
|
+
* - **Real-time Stores**: Synchronized state with automatic conflict resolution (powered by Valtio + Y.js)
|
|
21
|
+
* - **Atomic Transactions**: Update multiple stores consistently with automatic batching
|
|
22
|
+
* - **AI Integration**: Built-in text generation, structured JSON output, and image modification
|
|
23
|
+
* - **Cloud Storage**: File uploads with CDN delivery and tag-based organization
|
|
24
|
+
* - **Leaderboards**: Efficient player ranking with database indexes and pagination
|
|
25
|
+
* - **Presence Tracking**: Real-time connection status for all players
|
|
26
|
+
* - **Time Sync**: Server-synchronized timestamps across all clients
|
|
27
|
+
* - **Webhooks**: Send data to external services for backend processing
|
|
28
|
+
*
|
|
29
|
+
* **Quick Start:**
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { KokimokiClient } from '@kokimoki/app';
|
|
32
|
+
*
|
|
33
|
+
* // Initialize the client
|
|
34
|
+
* const kmClient = new KokimokiClient(
|
|
35
|
+
* 'your-host.kokimoki.com',
|
|
36
|
+
* 'your-app-id',
|
|
37
|
+
* 'optional-access-code'
|
|
38
|
+
* );
|
|
39
|
+
*
|
|
40
|
+
* // Connect to the server
|
|
41
|
+
* await kmClient.connect();
|
|
42
|
+
*
|
|
43
|
+
* // Create a synchronized store
|
|
44
|
+
* interface GameState {
|
|
45
|
+
* players: Record<string, { name: string; score: number }>;
|
|
46
|
+
* round: number;
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* const gameStore = kmClient.store<GameState>('game', {
|
|
50
|
+
* players: {},
|
|
51
|
+
* round: 1
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* // Update state atomically
|
|
55
|
+
* await kmClient.transact([gameStore], ([game]) => {
|
|
56
|
+
* game.players[kmClient.id] = { name: 'Player 1', score: 0 };
|
|
57
|
+
* game.round = 2;
|
|
58
|
+
* });
|
|
59
|
+
*
|
|
60
|
+
* // Use in React components with Valtio
|
|
61
|
+
* import { useSnapshot } from 'valtio';
|
|
62
|
+
*
|
|
63
|
+
* function GameComponent() {
|
|
64
|
+
* const game = useSnapshot(gameStore.proxy);
|
|
65
|
+
* return <div>Round: {game.round}</div>;
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* **Key Features:**
|
|
70
|
+
*
|
|
71
|
+
* **1. Real-time State Management**
|
|
72
|
+
* - Create global stores shared across all players: `kmClient.store()`
|
|
73
|
+
* - Create local stores for client-side data: `kmClient.localStore()`
|
|
74
|
+
* - Automatic synchronization and conflict resolution
|
|
75
|
+
* - Use `useSnapshot()` from Valtio for reactive React components
|
|
76
|
+
*
|
|
77
|
+
* **2. Atomic Transactions**
|
|
78
|
+
* ```typescript
|
|
79
|
+
* // Update multiple stores atomically
|
|
80
|
+
* await kmClient.transact([playerStore, gameStore], ([player, game]) => {
|
|
81
|
+
* player.score += 10;
|
|
82
|
+
* game.lastUpdate = kmClient.serverTimestamp();
|
|
83
|
+
* });
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* **3. AI Integration (No API keys required)**
|
|
87
|
+
* ```typescript
|
|
88
|
+
* // Generate text
|
|
89
|
+
* const story = await kmClient.ai.chat({
|
|
90
|
+
* model: 'gpt-4o',
|
|
91
|
+
* userPrompt: 'Write a quest description',
|
|
92
|
+
* temperature: 0.8
|
|
93
|
+
* });
|
|
94
|
+
*
|
|
95
|
+
* // Generate structured data
|
|
96
|
+
* interface Quest { title: string; reward: number; }
|
|
97
|
+
* const quest = await kmClient.ai.generateJson<Quest>({
|
|
98
|
+
* userPrompt: 'Create a level 5 quest'
|
|
99
|
+
* });
|
|
100
|
+
*
|
|
101
|
+
* // Modify images
|
|
102
|
+
* const modified = await kmClient.ai.modifyImage(url, 'Make it pixel art');
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* **4. Cloud Storage**
|
|
106
|
+
* ```typescript
|
|
107
|
+
* // Upload files with tags
|
|
108
|
+
* const upload = await kmClient.storage.upload('avatar.jpg', blob, ['profile']);
|
|
109
|
+
*
|
|
110
|
+
* // Query uploads
|
|
111
|
+
* const images = await kmClient.storage.listUploads({
|
|
112
|
+
* clientId: kmClient.id,
|
|
113
|
+
* mimeTypes: ['image/jpeg', 'image/png']
|
|
114
|
+
* });
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* **5. Leaderboards**
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // Submit score (replaces previous entry)
|
|
120
|
+
* await kmClient.leaderboard.upsertEntry(
|
|
121
|
+
* 'high-scores',
|
|
122
|
+
* 'desc',
|
|
123
|
+
* 1500,
|
|
124
|
+
* { playerName: 'Alice' },
|
|
125
|
+
* {}
|
|
126
|
+
* );
|
|
127
|
+
*
|
|
128
|
+
* // Get top 10
|
|
129
|
+
* const top10 = await kmClient.leaderboard.listEntries('high-scores', 'desc', 0, 10);
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* **6. Presence Tracking**
|
|
133
|
+
* ```typescript
|
|
134
|
+
* // Track online players
|
|
135
|
+
* const onlineClientIds = useSnapshot(gameStore.connections).clientIds;
|
|
136
|
+
* const isPlayerOnline = onlineClientIds.has(playerId);
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* **Best Practices:**
|
|
140
|
+
* - Always use `kmClient.serverTimestamp()` for time-sensitive operations
|
|
141
|
+
* - Prefer Records over Arrays: `Record<string, T>` with timestamp keys
|
|
142
|
+
* - Use `kmClient.transact()` for all state updates to ensure atomicity
|
|
143
|
+
* - Tag uploads for easy filtering and organization
|
|
144
|
+
* - Use local stores for client-side settings and preferences
|
|
145
|
+
* - Leverage TypeScript generics for type-safe stores
|
|
146
|
+
*
|
|
147
|
+
* **Events:**
|
|
148
|
+
* - `connected`: Fired when client connects/reconnects to server
|
|
149
|
+
* - `disconnected`: Fired when connection is lost
|
|
150
|
+
*
|
|
151
|
+
* @template ClientContextT The type of client context data (custom user data from your backend)
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```typescript
|
|
155
|
+
* // Listen for connection events
|
|
156
|
+
* kmClient.on('connected', () => {
|
|
157
|
+
* console.log('Connected to Kokimoki!');
|
|
158
|
+
* });
|
|
159
|
+
*
|
|
160
|
+
* kmClient.on('disconnected', () => {
|
|
161
|
+
* console.log('Connection lost, will auto-reconnect...');
|
|
162
|
+
* });
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
export declare class KokimokiClient<ClientContextT = any> extends KokimokiClient_base {
|
|
166
|
+
readonly host: string;
|
|
167
|
+
readonly appId: string;
|
|
168
|
+
readonly code: string;
|
|
169
|
+
private _wsUrl;
|
|
170
|
+
private _apiUrl;
|
|
171
|
+
private _id?;
|
|
172
|
+
private _connectionId?;
|
|
173
|
+
private _token?;
|
|
174
|
+
private _apiHeaders?;
|
|
175
|
+
private _serverTimeOffset;
|
|
176
|
+
private _clientContext?;
|
|
177
|
+
private _ws?;
|
|
178
|
+
private _subscriptionsByName;
|
|
179
|
+
private _subscriptionsByHash;
|
|
180
|
+
private _subscribeReqPromises;
|
|
181
|
+
private _unsubscribeReqPromises;
|
|
182
|
+
private _transactionPromises;
|
|
183
|
+
private _connected;
|
|
184
|
+
private _connectPromise?;
|
|
185
|
+
private _messageId;
|
|
186
|
+
private _autoReconnect;
|
|
187
|
+
private _reconnectTimeout;
|
|
188
|
+
private _pingInterval;
|
|
189
|
+
private _clientTokenKey;
|
|
190
|
+
private _editorContext;
|
|
191
|
+
private _ai?;
|
|
192
|
+
private _storage?;
|
|
193
|
+
private _leaderboard?;
|
|
194
|
+
constructor(host: string, appId: string, code?: string);
|
|
195
|
+
get id(): string;
|
|
196
|
+
get connectionId(): string;
|
|
197
|
+
get token(): string;
|
|
198
|
+
get apiUrl(): string;
|
|
199
|
+
get apiHeaders(): Headers;
|
|
200
|
+
get clientContext(): ClientContextT & ({} | null);
|
|
201
|
+
/**
|
|
202
|
+
* Indicates whether the client is currently connected to the server.
|
|
203
|
+
*/
|
|
204
|
+
get connected(): boolean;
|
|
205
|
+
get ws(): WebSocket;
|
|
206
|
+
/**
|
|
207
|
+
* Indicates whether the client is running in editor/development mode.
|
|
208
|
+
*/
|
|
209
|
+
get isEditor(): boolean;
|
|
210
|
+
/**
|
|
211
|
+
* Establishes a connection to the Kokimoki server.
|
|
212
|
+
*
|
|
213
|
+
* Handles authentication, WebSocket setup, and automatic reconnection.
|
|
214
|
+
* If already connecting, returns the existing connection promise.
|
|
215
|
+
*
|
|
216
|
+
* @returns A promise that resolves when the connection is established.
|
|
217
|
+
* @throws Error if the connection fails.
|
|
218
|
+
*/
|
|
219
|
+
connect(): Promise<void>;
|
|
220
|
+
private handleInitMessage;
|
|
221
|
+
private handleBinaryMessage;
|
|
222
|
+
private handleErrorMessage;
|
|
223
|
+
private handleSubscribeResMessage;
|
|
224
|
+
private handleUnsubscribeResMessage;
|
|
225
|
+
private handleRoomUpdateMessage;
|
|
226
|
+
/**
|
|
227
|
+
* Gets the current server timestamp, accounting for client-server time offset.
|
|
228
|
+
*
|
|
229
|
+
* @returns The current server timestamp in milliseconds.
|
|
230
|
+
*/
|
|
231
|
+
serverTimestamp(): number;
|
|
232
|
+
/**
|
|
233
|
+
* Sends a Y.js update to a specific room.
|
|
234
|
+
*
|
|
235
|
+
* @param room - The name of the room to update.
|
|
236
|
+
* @param update - The Y.js update as a Uint8Array.
|
|
237
|
+
* @returns A promise that resolves with the server response.
|
|
238
|
+
*/
|
|
239
|
+
patchRoomState(room: string, update: Uint8Array): Promise<any>;
|
|
240
|
+
private sendSubscribeReq;
|
|
241
|
+
private sendUnsubscribeReq;
|
|
242
|
+
/**
|
|
243
|
+
* Joins a store by subscribing to its corresponding room.
|
|
244
|
+
*
|
|
245
|
+
* If already joined, this method does nothing. For local stores, initializes
|
|
246
|
+
* the store locally. For remote stores, sends a subscription request to the server.
|
|
247
|
+
*
|
|
248
|
+
* @param store - The KokimokiStore to join.
|
|
249
|
+
* @template T - The type of the store's state object.
|
|
250
|
+
*/
|
|
251
|
+
join<T extends object>(store: KokimokiStore<T>): Promise<void>;
|
|
252
|
+
/**
|
|
253
|
+
* Leaves a store by unsubscribing from its corresponding room.
|
|
254
|
+
*
|
|
255
|
+
* Triggers the store's `onBeforeLeave` and `onLeave` lifecycle hooks.
|
|
256
|
+
*
|
|
257
|
+
* @param store - The KokimokiStore to leave.
|
|
258
|
+
* @template T - The type of the store's state object.
|
|
259
|
+
*/
|
|
260
|
+
leave<T extends object>(store: KokimokiStore<T>): Promise<void>;
|
|
261
|
+
/**
|
|
262
|
+
* Executes a transaction across one or more stores.
|
|
263
|
+
*
|
|
264
|
+
* Provides proxies to the stores that track changes. All changes are batched
|
|
265
|
+
* and sent to the server atomically. The transaction ensures consistency across
|
|
266
|
+
* multiple stores.
|
|
267
|
+
*
|
|
268
|
+
* @param stores - Array of stores to include in the transaction.
|
|
269
|
+
* @param handler - Function that receives store proxies and performs modifications.
|
|
270
|
+
* @returns A promise that resolves with the return value of the handler.
|
|
271
|
+
* @template TStores - Tuple type of the stores array.
|
|
272
|
+
* @template ReturnT - The return type of the handler function.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```ts
|
|
276
|
+
* await client.transact([playerStore, gameStore], ([player, game]) => {
|
|
277
|
+
* player.score += 10;
|
|
278
|
+
* game.lastUpdate = Date.now();
|
|
279
|
+
* });
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
transact<TStores extends readonly KokimokiStore<any>[], ReturnT = void>(stores: [...TStores], handler: (proxies: {
|
|
283
|
+
[K in keyof TStores]: StoreValue<TStores[K]>;
|
|
284
|
+
}) => ReturnT | Promise<ReturnT>): Promise<ReturnT>;
|
|
285
|
+
/**
|
|
286
|
+
* Closes the client connection and cleans up resources.
|
|
287
|
+
*
|
|
288
|
+
* Disables automatic reconnection, closes the WebSocket, and clears all intervals.
|
|
289
|
+
*/
|
|
290
|
+
close(): Promise<void>;
|
|
291
|
+
/**
|
|
292
|
+
* Gets the internal room hash identifier for a store.
|
|
293
|
+
*
|
|
294
|
+
* @param store - The store to get the room hash for.
|
|
295
|
+
* @returns The room hash as a number.
|
|
296
|
+
* @throws Error if the store hasn't been joined.
|
|
297
|
+
* @template T - The type of the store's state object.
|
|
298
|
+
*/
|
|
299
|
+
getRoomHash<T extends object>(store: KokimokiStore<T>): number;
|
|
300
|
+
/**
|
|
301
|
+
* Creates a new remote store synchronized with the server.
|
|
302
|
+
*
|
|
303
|
+
* @param name - The name of the room/store.
|
|
304
|
+
* @param defaultState - The initial state of the store.
|
|
305
|
+
* @param autoJoin - Whether to automatically join the store (default: true).
|
|
306
|
+
* @returns A new KokimokiStore instance.
|
|
307
|
+
* @template T - The type of the store's state object.
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* const gameStore = client.store('game', { players: [], score: 0 });
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
store<T extends object>(name: string, defaultState: T, autoJoin?: boolean): KokimokiStore<T>;
|
|
315
|
+
/**
|
|
316
|
+
* Creates a new local store that persists only in the client's browser.
|
|
317
|
+
*
|
|
318
|
+
* Local stores are automatically joined and are not synchronized with the server.
|
|
319
|
+
* Data is stored locally per client and app.
|
|
320
|
+
*
|
|
321
|
+
* @param name - The name of the local store.
|
|
322
|
+
* @param defaultState - The initial state of the store.
|
|
323
|
+
* @returns A new KokimokiLocalStore instance.
|
|
324
|
+
* @template T - The type of the store's state object.
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* const settingsStore = client.localStore('settings', { volume: 0.5, theme: 'dark' });
|
|
329
|
+
* ```
|
|
330
|
+
*/
|
|
331
|
+
localStore<T extends object>(name: string, defaultState: T): KokimokiLocalStore<T>;
|
|
332
|
+
/**
|
|
333
|
+
* Sends app data to the server via webhook for external processing.
|
|
334
|
+
*
|
|
335
|
+
* @param event - The name of the webhook event.
|
|
336
|
+
* @param data - The data to send with the webhook.
|
|
337
|
+
* @returns A promise that resolves with the job ID.
|
|
338
|
+
* @template T - The type of the data being sent.
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```ts
|
|
342
|
+
* await client.sendWebhook('game-ended', { winner: 'player1', score: 100 });
|
|
343
|
+
* ```
|
|
344
|
+
*/
|
|
345
|
+
sendWebhook<T>(event: string, data: T): Promise<{
|
|
346
|
+
jobId: string;
|
|
347
|
+
}>;
|
|
348
|
+
/**
|
|
349
|
+
* Access AI capabilities including text generation, structured JSON output, and image modification.
|
|
350
|
+
*/
|
|
351
|
+
get ai(): KokimokiAiService;
|
|
352
|
+
/**
|
|
353
|
+
* Access file upload and management for media files, images, and user-generated content.
|
|
354
|
+
*/
|
|
355
|
+
get storage(): KokimokiStorageService;
|
|
356
|
+
/**
|
|
357
|
+
* Access player ranking and score tracking with efficient queries and pagination.
|
|
358
|
+
*/
|
|
359
|
+
get leaderboard(): KokimokiLeaderboardService;
|
|
360
|
+
}
|
|
361
|
+
export {};
|