@kokimoki/app 2.1.1 → 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 (37) 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 +320 -2
  7. package/dist/kokimoki.min.js +1916 -115
  8. package/dist/kokimoki.min.js.map +1 -1
  9. package/dist/protocol/ws-message/reader.d.ts +1 -1
  10. package/dist/services/index.d.ts +1 -0
  11. package/dist/services/index.js +1 -0
  12. package/dist/services/kokimoki-ai.d.ts +185 -124
  13. package/dist/services/kokimoki-ai.js +201 -111
  14. package/dist/services/kokimoki-i18n.d.ts +259 -0
  15. package/dist/services/kokimoki-i18n.js +325 -0
  16. package/dist/stores/kokimoki-local-store.d.ts +1 -1
  17. package/dist/types/common.d.ts +9 -0
  18. package/dist/types/env.d.ts +36 -0
  19. package/dist/types/env.js +1 -0
  20. package/dist/types/index.d.ts +1 -0
  21. package/dist/types/index.js +1 -0
  22. package/dist/utils/kokimoki-client.d.ts +31 -0
  23. package/dist/utils/kokimoki-client.js +38 -0
  24. package/dist/utils/kokimoki-dev.d.ts +30 -0
  25. package/dist/utils/kokimoki-dev.js +75 -0
  26. package/dist/utils/kokimoki-env.d.ts +20 -0
  27. package/dist/utils/kokimoki-env.js +30 -0
  28. package/dist/version.d.ts +1 -1
  29. package/dist/version.js +1 -1
  30. package/docs/kokimoki-ai.instructions.md +316 -0
  31. package/docs/kokimoki-dynamic-stores.instructions.md +439 -0
  32. package/docs/kokimoki-i18n.instructions.md +285 -0
  33. package/docs/kokimoki-leaderboard.instructions.md +189 -0
  34. package/docs/kokimoki-sdk.instructions.md +221 -0
  35. package/docs/kokimoki-storage.instructions.md +162 -0
  36. package/llms.txt +43 -0
  37. package/package.json +9 -13
@@ -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,145 +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
- * @param req.imageUrls Optional. Image URLs to include with the user prompt (Gemini models only).
83
- * Allows the AI to analyze and respond based on the provided images.
84
- *
85
- * @returns A promise that resolves to an object containing the AI-generated response.
86
- * @returns {string} content The text content of the AI's response.
87
- *
88
- * @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.
89
116
  *
90
117
  * @example
91
118
  * ```typescript
92
- * const response = await client.ai.chat({
93
- * model: "gpt-4o",
94
- * systemPrompt: "You are a helpful coding assistant.",
95
- * userPrompt: "Explain what TypeScript is in one sentence.",
96
- * temperature: 0.7,
97
- * 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
98
123
  * });
99
- * console.log(response.content);
124
+ *
125
+ * const text = await kmClient.ai.pollText(jobId);
100
126
  * ```
101
127
  */
102
- chat(req: {
103
- 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";
104
- systemPrompt?: string;
105
- userPrompt: string;
106
- temperature?: number;
107
- maxTokens?: number;
108
- responseMimeType?: string;
109
- /** Image URLs to include with the user prompt (Gemini models only) */
110
- imageUrls?: string[];
111
- }): Promise<{
112
- content: string;
113
- }>;
128
+ generateText(req: TextGenerateRequest): Promise<JobSubmitResponse>;
114
129
  /**
115
130
  * Generate structured JSON output from the AI model.
116
131
  *
117
- * Sends a chat request to the AI service with the expectation of receiving
118
- * a JSON-formatted response. This is useful for scenarios where the output
119
- * needs to be parsed or processed programmatically.
120
- *
121
- * **Important:** Your prompt must include the word "json" and should describe
122
- * the expected output schema (field names, types, and structure) to ensure
123
- * the AI generates properly formatted responses.
124
- *
125
- * @param req The chat request parameters.
126
- * @param req.model Optional. The AI model to use. Defaults to server-side default if not specified.
127
- * Available models:
128
- * - `gpt-4o`: OpenAI GPT-4 Optimized
129
- * - `gpt-4o-mini`: Smaller, faster GPT-4 variant
130
- * - `gpt-5`: OpenAI GPT-5 (latest)
131
- * - `gpt-5-mini`: Smaller GPT-5 variant
132
- * - `gpt-5-nano`: Smallest GPT-5 variant for lightweight tasks
133
- * - `gemini-2.5-flash-lite`: Google Gemini lite variant
134
- * - `gemini-2.5-flash`: Google Gemini fast variant
135
- * - `gemini-3-flash-preview`: Google Gemini 3 Flash preview
136
- * - `gemini-3-pro-preview`: Google Gemini 3 Pro preview
137
- * @param req.systemPrompt Optional. The system message that sets the behavior and context for the AI.
138
- * This helps define the AI's role, personality, and constraints.
139
- * @param req.userPrompt The user's message or question to send to the AI.
140
- * @param req.temperature Optional. Controls randomness in the response (0.0 to 1.0).
141
- * Lower values make output more focused and deterministic,
142
- * higher values make it more creative and varied.
143
- * @param req.maxTokens Optional. The maximum number of tokens to generate in the response.
144
- * Controls the length of the AI's output.
145
- * @param req.imageUrls Optional. Image URLs to include with the user prompt (Gemini models only).
146
- * Allows the AI to analyze images and generate structured JSON based on them.
147
- *
148
- * @returns A promise that resolves to the parsed JSON object generated by the AI.
149
- *
150
- * @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.
151
223
  */
152
- generateJson<T extends object>(req: {
153
- 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";
154
- systemPrompt?: string;
155
- userPrompt: string;
156
- temperature?: number;
157
- maxTokens?: number;
158
- /** Image URLs to include with the user prompt (Gemini models only) */
159
- imageUrls?: string[];
160
- }): Promise<T>;
224
+ pollImage(jobId: string, options?: PollOptions<AiJobStatus>): Promise<Upload>;
161
225
  /**
162
- * Modify an image using the AI service.
163
- * @param baseImageUrl The URL of the base image to modify.
164
- * @param prompt The modification prompt to apply to the image.
165
- * @param tags Optional. Tags to associate with the image.
166
- * @returns A promise that resolves to the modified image upload information.
226
+ * Internal polling implementation.
167
227
  */
168
- modifyImage(baseImageUrl: string, prompt: string, tags?: string[], model?: 'gemini-2.5-flash-image' | 'gemini-3-pro-image-preview'): Promise<Upload>;
228
+ private pollJob;
169
229
  }
230
+ export {};