@kokimoki/app 2.1.0 → 3.0.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.
Files changed (38) hide show
  1. package/README.md +72 -0
  2. package/dist/core/kokimoki-client.d.ts +75 -15
  3. package/dist/core/kokimoki-client.js +137 -22
  4. package/dist/index.d.ts +6 -1
  5. package/dist/index.js +4 -0
  6. package/dist/kokimoki.min.d.ts +322 -2
  7. package/dist/kokimoki.min.js +1884 -72
  8. package/dist/kokimoki.min.js.map +1 -1
  9. package/dist/llms.txt +6 -0
  10. package/dist/protocol/ws-message/reader.d.ts +1 -1
  11. package/dist/services/index.d.ts +1 -0
  12. package/dist/services/index.js +1 -0
  13. package/dist/services/kokimoki-ai.d.ts +185 -122
  14. package/dist/services/kokimoki-ai.js +201 -109
  15. package/dist/services/kokimoki-i18n.d.ts +259 -0
  16. package/dist/services/kokimoki-i18n.js +325 -0
  17. package/dist/stores/kokimoki-local-store.d.ts +1 -1
  18. package/dist/types/common.d.ts +9 -0
  19. package/dist/types/env.d.ts +36 -0
  20. package/dist/types/env.js +1 -0
  21. package/dist/types/index.d.ts +1 -0
  22. package/dist/types/index.js +1 -0
  23. package/dist/utils/kokimoki-client.d.ts +31 -0
  24. package/dist/utils/kokimoki-client.js +38 -0
  25. package/dist/utils/kokimoki-dev.d.ts +30 -0
  26. package/dist/utils/kokimoki-dev.js +75 -0
  27. package/dist/utils/kokimoki-env.d.ts +20 -0
  28. package/dist/utils/kokimoki-env.js +30 -0
  29. package/dist/version.d.ts +1 -1
  30. package/dist/version.js +1 -1
  31. package/docs/kokimoki-ai.instructions.md +316 -0
  32. package/docs/kokimoki-dynamic-stores.instructions.md +439 -0
  33. package/docs/kokimoki-i18n.instructions.md +285 -0
  34. package/docs/kokimoki-leaderboard.instructions.md +189 -0
  35. package/docs/kokimoki-sdk.instructions.md +221 -0
  36. package/docs/kokimoki-storage.instructions.md +162 -0
  37. package/llms.txt +43 -0
  38. package/package.json +9 -13
package/dist/llms.txt CHANGED
@@ -308,10 +308,13 @@ Used to generate text response with AI
308
308
  - `gpt-5-nano`: Smallest GPT-5 variant for lightweight tasks
309
309
  - `gemini-2.5-flash-lite`: Google Gemini lite variant
310
310
  - `gemini-2.5-flash`: Google Gemini fast variant
311
+ - `gemini-3-flash-preview`: Google Gemini 3 Flash preview
312
+ - `gemini-3-pro-preview`: Google Gemini 3 Pro preview
311
313
  - **req.systemPrompt**: `string` AI role/behavior (optional)
312
314
  - **req.userPrompt**: `string` User message (optional)
313
315
  - **req.temperature**: `number` Creativity level from 0.0 = factual to 2.0 = creative (optional)
314
316
  - **req.maxTokens**: `number` Response length limit (optional)
317
+ - **req.imageUrls**: `string[]` Image URLs to include with the user prompt (Gemini models only) (optional)
315
318
 
316
319
  **Examples:**
317
320
 
@@ -342,10 +345,13 @@ Used to generate structured JSON output with AI. Automatically ensures the respo
342
345
  - `gpt-5-nano`: Smallest GPT-5 variant for lightweight tasks
343
346
  - `gemini-2.5-flash-lite`: Google Gemini lite variant
344
347
  - `gemini-2.5-flash`: Google Gemini fast variant
348
+ - `gemini-3-flash-preview`: Google Gemini 3 Flash preview
349
+ - `gemini-3-pro-preview`: Google Gemini 3 Pro preview
345
350
  - **req.systemPrompt**: `string` AI role/behavior (optional)
346
351
  - **req.userPrompt**: `string` User message (optional)
347
352
  - **req.temperature**: `number` Creativity level from 0.0 = factual to 2.0 = creative (optional)
348
353
  - **req.maxTokens**: `number` Response length limit (optional)
354
+ - **req.imageUrls**: `string[]` Image URLs to include with the user prompt (Gemini models only) (optional)
349
355
 
350
356
  **Returns:** Promise resolving to parsed JSON object of type T
351
357
 
@@ -6,6 +6,6 @@ export declare class WsMessageReader {
6
6
  readInt32(): number;
7
7
  readUint32(): number;
8
8
  readString(): string;
9
- readUint8Array(): Uint8Array<ArrayBuffer>;
9
+ readUint8Array(): Uint8Array;
10
10
  get end(): boolean;
11
11
  }
@@ -1,3 +1,4 @@
1
1
  export * from "./kokimoki-ai";
2
+ export * from "./kokimoki-i18n";
2
3
  export * from "./kokimoki-leaderboard";
3
4
  export * from "./kokimoki-storage";
@@ -1,3 +1,4 @@
1
1
  export * from "./kokimoki-ai";
2
+ export * from "./kokimoki-i18n";
2
3
  export * from "./kokimoki-leaderboard";
3
4
  export * from "./kokimoki-storage";
@@ -1,17 +1,74 @@
1
+ import { z, ZodType } from "zod/v4";
1
2
  import { KokimokiClient } from "../core";
2
- import type { Upload } from "../types";
3
+ import type { PollOptions, Upload } from "../types";
4
+ /** AI job status */
5
+ export type AiJobStatus = "processing" | "queued" | "completed" | "failed";
6
+ /** AI job type */
7
+ export type AiJobType = "text" | "json" | "image";
8
+ /** AI job information */
9
+ export interface AiJob {
10
+ jobId: string;
11
+ type: AiJobType;
12
+ status: AiJobStatus;
13
+ result?: unknown;
14
+ error?: {
15
+ message: string;
16
+ code?: string;
17
+ };
18
+ createdAt: number;
19
+ }
20
+ /** AI model options */
21
+ type AiModel = "gpt-4o" | "gpt-4o-mini" | "gpt-5" | "gpt-5-mini" | "gpt-5-nano" | "gemini-2.5-flash-lite" | "gemini-2.5-flash" | "gemini-3-flash-preview" | "gemini-3-pro-preview";
22
+ /** Image generation model options */
23
+ type ImageModel = "gemini-2.5-flash-image" | "gemini-3-pro-image-preview";
24
+ /** Common request options for text generation */
25
+ interface TextGenerateRequest {
26
+ model?: AiModel;
27
+ /** System prompt that sets the behavior and context for the AI */
28
+ systemPrompt?: string;
29
+ /** The user's message or question to send to the AI */
30
+ prompt: string;
31
+ /** Controls randomness in the response (0.0 to 1.0) */
32
+ temperature?: number;
33
+ /** Image URLs to include with the prompt (Gemini models only) */
34
+ imageUrls?: string[];
35
+ }
36
+ /** Request options for JSON generation */
37
+ interface JsonGenerateRequest<T extends ZodType> extends Omit<TextGenerateRequest, "prompt"> {
38
+ /** Zod schema that defines the expected JSON structure */
39
+ schema: T;
40
+ /** The user's message or question to send to the AI */
41
+ prompt: string;
42
+ }
43
+ /** Request options for image generation */
44
+ interface ImageGenerateRequest {
45
+ model?: ImageModel;
46
+ /** The prompt describing the image modification */
47
+ prompt: string;
48
+ /** Image URLs to include in the generation */
49
+ imageUrls?: string[];
50
+ /** Tags to associate with the generated image */
51
+ tags?: string[];
52
+ }
53
+ /** Job submission response */
54
+ interface JobSubmitResponse {
55
+ jobId: string;
56
+ status: AiJobStatus;
57
+ }
3
58
  /**
4
59
  * Kokimoki AI Integration Service
5
60
  *
6
61
  * Provides built-in AI capabilities for game applications without requiring API keys or setup.
7
- * Includes text generation, structured JSON output, and image modification.
62
+ * All generation methods are async job-based - they submit a job and return immediately with a jobId.
63
+ * Use the corresponding poll method to wait for the result.
8
64
  *
9
65
  * **Key Features:**
10
66
  * - Multiple AI models (GPT-4, GPT-5, Gemini variants)
11
67
  * - Text generation with configurable creativity (temperature)
12
- * - Structured JSON output for game data
13
- * - AI-powered image modifications
14
- * - No API keys or configuration required
68
+ * - Structured JSON output with Zod schema validation
69
+ * - AI-powered image generation and modification
70
+ * - Job-based async processing with polling support
71
+ * - Resumable after page reload (persist jobId)
15
72
  *
16
73
  * **Common Use Cases:**
17
74
  * - Generate dynamic game content (quests, dialogues, stories)
@@ -25,143 +82,149 @@ import type { Upload } from "../types";
25
82
  *
26
83
  * @example
27
84
  * ```typescript
28
- * // Generate story text
29
- * const story = await kmClient.ai.chat({
30
- * systemPrompt: 'You are a fantasy story writer',
31
- * userPrompt: 'Write a short quest description',
32
- * temperature: 0.8
85
+ * // Generate structured game data
86
+ * const enemySchema = z.object({
87
+ * name: z.string(),
88
+ * health: z.number(),
89
+ * attack: z.number()
33
90
  * });
34
91
  *
35
- * // Generate structured game data
36
- * interface Enemy {
37
- * name: string;
38
- * health: number;
39
- * attack: number;
40
- * }
41
- * const enemy = await kmClient.ai.generateJson<Enemy>({
42
- * userPrompt: 'Create a level 5 goblin warrior'
92
+ * // Submit job
93
+ * const { jobId } = await kmClient.ai.generateJson({
94
+ * schema: enemySchema,
95
+ * prompt: 'Create a level 5 goblin warrior'
43
96
  * });
44
97
  *
45
- * // Transform image
46
- * const modified = await kmClient.ai.modifyImage(
47
- * imageUrl,
48
- * 'Make it look like pixel art'
49
- * );
98
+ * // Save jobId to store for persistence across reloads
99
+ * await saveJobId(jobId);
100
+ *
101
+ * // Poll for result (can resume after reload)
102
+ * const enemy = await kmClient.ai.pollJson(jobId, { schema: enemySchema });
50
103
  * ```
51
104
  */
52
105
  export declare class KokimokiAiService {
53
106
  private readonly client;
54
107
  constructor(client: KokimokiClient);
55
108
  /**
56
- * Generate a chat response from the AI model.
57
- *
58
- * Sends a chat request to the AI service and returns the generated response.
59
- * Supports multiple AI models including GPT and Gemini variants with configurable
60
- * parameters for fine-tuning the response behavior.
61
- *
62
- * @param req The chat request parameters.
63
- * @param req.model Optional. The AI model to use. Defaults to server-side default if not specified.
64
- * Available models:
65
- * - `gpt-4o`: OpenAI GPT-4 Optimized
66
- * - `gpt-4o-mini`: Smaller, faster GPT-4 variant
67
- * - `gpt-5`: OpenAI GPT-5 (latest)
68
- * - `gpt-5-mini`: Smaller GPT-5 variant
69
- * - `gpt-5-nano`: Smallest GPT-5 variant for lightweight tasks
70
- * - `gemini-2.5-flash-lite`: Google Gemini lite variant
71
- * - `gemini-2.5-flash`: Google Gemini fast variant
72
- * - `gemini-3-flash-preview`: Google Gemini 3 Flash preview
73
- * - `gemini-3-pro-preview`: Google Gemini 3 Pro preview
74
- * @param req.systemPrompt Optional. The system message that sets the behavior and context for the AI.
75
- * This helps define the AI's role, personality, and constraints.
76
- * @param req.userPrompt The user's message or question to send to the AI.
77
- * @param req.temperature Optional. Controls randomness in the response (0.0 to 1.0).
78
- * Lower values make output more focused and deterministic,
79
- * higher values make it more creative and varied.
80
- * @param req.maxTokens Optional. The maximum number of tokens to generate in the response.
81
- * Controls the length of the AI's output.
82
- *
83
- * @returns A promise that resolves to an object containing the AI-generated response.
84
- * @returns {string} content The text content of the AI's response.
85
- *
86
- * @throws An error object if the API request fails.
109
+ * Generate text content from the AI model.
110
+ *
111
+ * Submits a text generation job and returns immediately with a jobId.
112
+ * Use `pollText()` to wait for the result.
113
+ *
114
+ * @param req The generation request parameters.
115
+ * @returns A promise that resolves to an object containing the jobId.
87
116
  *
88
117
  * @example
89
118
  * ```typescript
90
- * const response = await client.ai.chat({
91
- * model: "gpt-4o",
92
- * systemPrompt: "You are a helpful coding assistant.",
93
- * userPrompt: "Explain what TypeScript is in one sentence.",
94
- * temperature: 0.7,
95
- * maxTokens: 100
119
+ * const { jobId } = await kmClient.ai.generateText({
120
+ * systemPrompt: 'You are a fantasy story writer',
121
+ * prompt: 'Write a short quest description',
122
+ * temperature: 0.8
96
123
  * });
97
- * console.log(response.content);
124
+ *
125
+ * const text = await kmClient.ai.pollText(jobId);
98
126
  * ```
99
127
  */
100
- chat(req: {
101
- model?: "gpt-4o" | "gpt-4o-mini" | "gpt-5" | "gpt-5-mini" | "gpt-5-nano" | "gemini-2.5-flash-lite" | "gemini-2.5-flash" | "gemini-3-flash-preview" | "gemini-3-pro-preview";
102
- systemPrompt?: string;
103
- userPrompt: string;
104
- temperature?: number;
105
- maxTokens?: number;
106
- responseMimeType?: string;
107
- /** Image URLs to include with the user prompt (Gemini models only) */
108
- imageUrls?: string[];
109
- }): Promise<{
110
- content: string;
111
- }>;
128
+ generateText(req: TextGenerateRequest): Promise<JobSubmitResponse>;
112
129
  /**
113
130
  * Generate structured JSON output from the AI model.
114
131
  *
115
- * Sends a chat request to the AI service with the expectation of receiving
116
- * a JSON-formatted response. This is useful for scenarios where the output
117
- * needs to be parsed or processed programmatically.
118
- *
119
- * **Important:** Your prompt must include the word "json" and should describe
120
- * the expected output schema (field names, types, and structure) to ensure
121
- * the AI generates properly formatted responses.
122
- *
123
- * @param req The chat request parameters.
124
- * @param req.model Optional. The AI model to use. Defaults to server-side default if not specified.
125
- * Available models:
126
- * - `gpt-4o`: OpenAI GPT-4 Optimized
127
- * - `gpt-4o-mini`: Smaller, faster GPT-4 variant
128
- * - `gpt-5`: OpenAI GPT-5 (latest)
129
- * - `gpt-5-mini`: Smaller GPT-5 variant
130
- * - `gpt-5-nano`: Smallest GPT-5 variant for lightweight tasks
131
- * - `gemini-2.5-flash-lite`: Google Gemini lite variant
132
- * - `gemini-2.5-flash`: Google Gemini fast variant
133
- * - `gemini-3-flash-preview`: Google Gemini 3 Flash preview
134
- * - `gemini-3-pro-preview`: Google Gemini 3 Pro preview
135
- * @param req.systemPrompt Optional. The system message that sets the behavior and context for the AI.
136
- * This helps define the AI's role, personality, and constraints.
137
- * @param req.userPrompt The user's message or question to send to the AI.
138
- * @param req.temperature Optional. Controls randomness in the response (0.0 to 1.0).
139
- * Lower values make output more focused and deterministic,
140
- * higher values make it more creative and varied.
141
- * @param req.maxTokens Optional. The maximum number of tokens to generate in the response.
142
- * Controls the length of the AI's output.
143
- * @param req.imageUrls Optional. Image URLs to include with the user prompt (Gemini models only).
144
- * Allows the AI to analyze images and generate structured JSON based on them.
145
- *
146
- * @returns A promise that resolves to the parsed JSON object generated by the AI.
147
- *
148
- * @throws An error object if the API request fails or if the response is not valid JSON.
132
+ * Submits a JSON generation job and returns immediately with a jobId.
133
+ * The Zod schema is converted to JSON Schema and sent to the AI.
134
+ * Use `pollJson()` to wait for the result with type inference.
135
+ *
136
+ * @param req The generation request parameters including Zod schema.
137
+ * @returns A promise that resolves to an object containing the jobId.
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * const enemySchema = z.object({
142
+ * name: z.string(),
143
+ * health: z.number(),
144
+ * attack: z.number()
145
+ * });
146
+ *
147
+ * const { jobId } = await kmClient.ai.generateJson({
148
+ * schema: enemySchema,
149
+ * prompt: 'Create a level 5 goblin warrior'
150
+ * });
151
+ *
152
+ * // Poll with schema for type inference and validation
153
+ * const enemy = await kmClient.ai.pollJson(jobId, { schema: enemySchema });
154
+ * ```
155
+ */
156
+ generateJson<T extends ZodType>(req: JsonGenerateRequest<T>): Promise<JobSubmitResponse>;
157
+ /**
158
+ * Generate or modify an image using the AI model.
159
+ *
160
+ * Submits an image generation job and returns immediately with a jobId.
161
+ * Use `pollImage()` to wait for the result.
162
+ *
163
+ * @param req The generation request parameters.
164
+ * @returns A promise that resolves to an object containing the jobId.
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * // Modify existing image
169
+ * const { jobId } = await kmClient.ai.generateImage({
170
+ * imageUrls: [imageUrl],
171
+ * prompt: 'Make it look like pixel art'
172
+ * });
173
+ *
174
+ * const image = await kmClient.ai.pollImage(jobId);
175
+ * ```
176
+ */
177
+ generateImage(req: ImageGenerateRequest): Promise<JobSubmitResponse>;
178
+ /**
179
+ * Get the current status of an AI job.
180
+ *
181
+ * One-shot status check without polling.
182
+ *
183
+ * @param jobId The job ID to check.
184
+ * @returns A promise that resolves to the job information.
185
+ */
186
+ getJob(jobId: string): Promise<AiJob>;
187
+ /**
188
+ * Poll a text generation job until completion.
189
+ *
190
+ * @param jobId The job ID to poll.
191
+ * @param options Polling options (interval, timeout, progress callback).
192
+ * @returns A promise that resolves to the generated text.
193
+ * @throws If the job fails or times out.
194
+ */
195
+ pollText(jobId: string, options?: PollOptions<AiJobStatus>): Promise<string>;
196
+ /**
197
+ * Poll a JSON generation job until completion.
198
+ *
199
+ * @param jobId The job ID to poll.
200
+ * @param options Polling options including the Zod schema for validation.
201
+ * @returns A promise that resolves to the parsed and validated JSON.
202
+ * @throws If the job fails, times out, or validation fails.
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * const enemy = await kmClient.ai.pollJson(savedJobId, {
207
+ * schema: enemySchema,
208
+ * timeout: 60000,
209
+ * onProgress: (status) => console.log('Status:', status)
210
+ * });
211
+ * ```
212
+ */
213
+ pollJson<T extends ZodType>(jobId: string, options: PollOptions<AiJobStatus> & {
214
+ schema: T;
215
+ }): Promise<z.infer<T>>;
216
+ /**
217
+ * Poll an image generation job until completion.
218
+ *
219
+ * @param jobId The job ID to poll.
220
+ * @param options Polling options (interval, timeout, progress callback).
221
+ * @returns A promise that resolves to the upload information.
222
+ * @throws If the job fails or times out.
149
223
  */
150
- generateJson<T extends object>(req: {
151
- model?: "gpt-4o" | "gpt-4o-mini" | "gpt-5" | "gpt-5-mini" | "gpt-5-nano" | "gemini-2.5-flash-lite" | "gemini-2.5-flash" | "gemini-3-flash-preview" | "gemini-3-pro-preview";
152
- systemPrompt?: string;
153
- userPrompt: string;
154
- temperature?: number;
155
- maxTokens?: number;
156
- /** Image URLs to include with the user prompt (Gemini models only) */
157
- imageUrls?: string[];
158
- }): Promise<T>;
224
+ pollImage(jobId: string, options?: PollOptions<AiJobStatus>): Promise<Upload>;
159
225
  /**
160
- * Modify an image using the AI service.
161
- * @param baseImageUrl The URL of the base image to modify.
162
- * @param prompt The modification prompt to apply to the image.
163
- * @param tags Optional. Tags to associate with the image.
164
- * @returns A promise that resolves to the modified image upload information.
226
+ * Internal polling implementation.
165
227
  */
166
- modifyImage(baseImageUrl: string, prompt: string, tags?: string[], model?: 'gemini-2.5-flash-image' | 'gemini-3-pro-image-preview'): Promise<Upload>;
228
+ private pollJob;
167
229
  }
230
+ export {};