kimi-vercel-ai-sdk-provider 0.2.0 → 0.4.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.
@@ -4,6 +4,7 @@
4
4
  * @module
5
5
  */
6
6
 
7
+ import { type FileCache, type FileCacheEntry, generateCacheKey, getDefaultFileCache } from './file-cache';
7
8
  import {
8
9
  getExtensionFromPath,
9
10
  getMediaTypeFromExtension,
@@ -64,6 +65,13 @@ export interface ProcessAttachmentsOptions {
64
65
  uploadImages?: boolean;
65
66
  /** Whether to delete files after extraction (cleanup) */
66
67
  cleanupAfterExtract?: boolean;
68
+ /**
69
+ * Enable caching of uploaded files.
70
+ * When true, uses the default global cache.
71
+ * When a FileCache instance, uses that cache.
72
+ * @default false
73
+ */
74
+ cache?: boolean | FileCache;
67
75
  }
68
76
 
69
77
  // ============================================================================
@@ -101,9 +109,13 @@ export async function processAttachments(options: ProcessAttachmentsOptions): Pr
101
109
  clientConfig,
102
110
  autoUploadDocuments = true,
103
111
  uploadImages = false,
104
- cleanupAfterExtract = false
112
+ cleanupAfterExtract = false,
113
+ cache = false
105
114
  } = options;
106
115
 
116
+ // Resolve cache instance
117
+ const cacheInstance = cache === true ? getDefaultFileCache() : cache === false ? null : cache;
118
+
107
119
  const results: ProcessedAttachment[] = [];
108
120
  const client = new KimiFileClient(clientConfig);
109
121
 
@@ -112,7 +124,8 @@ export async function processAttachments(options: ProcessAttachmentsOptions): Pr
112
124
  const processed = await processAttachment(attachment, client, {
113
125
  autoUploadDocuments,
114
126
  uploadImages,
115
- cleanupAfterExtract
127
+ cleanupAfterExtract,
128
+ cache: cacheInstance
116
129
  });
117
130
  results.push(processed);
118
131
  } catch (error) {
@@ -134,7 +147,12 @@ export async function processAttachments(options: ProcessAttachmentsOptions): Pr
134
147
  async function processAttachment(
135
148
  attachment: Attachment,
136
149
  client: KimiFileClient,
137
- options: { autoUploadDocuments: boolean; uploadImages: boolean; cleanupAfterExtract: boolean }
150
+ options: {
151
+ autoUploadDocuments: boolean;
152
+ uploadImages: boolean;
153
+ cleanupAfterExtract: boolean;
154
+ cache: FileCache | null;
155
+ }
138
156
  ): Promise<ProcessedAttachment> {
139
157
  // Determine content type
140
158
  const contentType = resolveContentType(attachment);
@@ -196,14 +214,43 @@ async function processAttachment(
196
214
  };
197
215
  }
198
216
 
217
+ const filename = attachment.name ?? guessFilename(attachment, contentType);
218
+
219
+ // Check cache if enabled
220
+ if (options.cache) {
221
+ const cacheKey = generateCacheKey(data, filename);
222
+ const cached = options.cache.get(cacheKey);
223
+
224
+ if (cached) {
225
+ return {
226
+ original: attachment,
227
+ type: 'text-inject',
228
+ textContent: cached.content,
229
+ fileId: cached.fileId
230
+ };
231
+ }
232
+ }
233
+
199
234
  // Upload and extract content
200
235
  const result = await client.uploadAndExtract({
201
236
  data,
202
- filename: attachment.name ?? guessFilename(attachment, contentType),
237
+ filename,
203
238
  mediaType: contentType,
204
239
  purpose: 'file-extract'
205
240
  });
206
241
 
242
+ // Store in cache if enabled (before cleanup)
243
+ if (options.cache && result.content) {
244
+ const cacheKey = generateCacheKey(data, filename);
245
+ const cacheEntry: FileCacheEntry = {
246
+ fileId: result.file.id,
247
+ content: result.content,
248
+ createdAt: Date.now(),
249
+ purpose: 'file-extract'
250
+ };
251
+ options.cache.set(cacheKey, cacheEntry);
252
+ }
253
+
207
254
  // Cleanup if requested
208
255
  if (options.cleanupAfterExtract && result.file.id) {
209
256
  try {
@@ -257,7 +304,8 @@ function guessFilename(attachment: Attachment, contentType: string): string {
257
304
  const urlPath = attachment.url.split('?')[0];
258
305
  const segments = urlPath.split('/');
259
306
  const lastSegment = segments[segments.length - 1];
260
- if (lastSegment && lastSegment.includes('.')) {
307
+
308
+ if (lastSegment.includes('.')) {
261
309
  return lastSegment;
262
310
  }
263
311
  }
@@ -0,0 +1,260 @@
1
+ /**
2
+ * File content caching for efficient re-use of uploaded files.
3
+ * @module
4
+ */
5
+
6
+ // ============================================================================
7
+ // Types
8
+ // ============================================================================
9
+
10
+ /**
11
+ * Entry in the file cache.
12
+ */
13
+ export interface FileCacheEntry {
14
+ /** The Kimi file ID */
15
+ fileId: string;
16
+ /** Extracted text content (for documents) */
17
+ content?: string;
18
+ /** Unix timestamp of creation */
19
+ createdAt: number;
20
+ /** File purpose */
21
+ purpose: 'file-extract' | 'image' | 'video';
22
+ }
23
+
24
+ /**
25
+ * Options for configuring the file cache.
26
+ */
27
+ export interface FileCacheOptions {
28
+ /**
29
+ * Maximum number of entries in the cache.
30
+ * When exceeded, least recently used entries are evicted.
31
+ * @default 100
32
+ */
33
+ maxSize?: number;
34
+
35
+ /**
36
+ * Time-to-live for cache entries in milliseconds.
37
+ * Entries older than this are considered stale.
38
+ * @default 3600000 (1 hour)
39
+ */
40
+ ttlMs?: number;
41
+ }
42
+
43
+ // ============================================================================
44
+ // LRU Cache Implementation
45
+ // ============================================================================
46
+
47
+ /**
48
+ * A simple LRU (Least Recently Used) cache for file content.
49
+ *
50
+ * This cache helps avoid re-uploading the same files multiple times
51
+ * by storing the mapping between content hashes and Kimi file IDs.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * const cache = new FileCache({ maxSize: 50, ttlMs: 30 * 60 * 1000 });
56
+ *
57
+ * // Check if we have this file cached
58
+ * const cached = cache.get(contentHash);
59
+ * if (cached) {
60
+ * console.log('Using cached file:', cached.fileId);
61
+ * }
62
+ *
63
+ * // Store a new file
64
+ * cache.set(contentHash, {
65
+ * fileId: 'file_abc123',
66
+ * content: 'extracted text...',
67
+ * purpose: 'file-extract',
68
+ * createdAt: Date.now()
69
+ * });
70
+ * ```
71
+ */
72
+ export class FileCache {
73
+ private readonly maxSize: number;
74
+ private readonly ttlMs: number;
75
+ private readonly cache: Map<string, FileCacheEntry>;
76
+
77
+ constructor(options: FileCacheOptions = {}) {
78
+ this.maxSize = options.maxSize ?? 100;
79
+ this.ttlMs = options.ttlMs ?? 3600000; // 1 hour
80
+ this.cache = new Map();
81
+ }
82
+
83
+ /**
84
+ * Get a cached entry by content hash.
85
+ * Returns undefined if not found or expired.
86
+ * Moves the entry to the end (most recently used).
87
+ */
88
+ get(contentHash: string): FileCacheEntry | undefined {
89
+ const entry = this.cache.get(contentHash);
90
+
91
+ if (!entry) {
92
+ return undefined;
93
+ }
94
+
95
+ // Check if entry has expired
96
+ if (this.isExpired(entry)) {
97
+ this.cache.delete(contentHash);
98
+ return undefined;
99
+ }
100
+
101
+ // Move to end (most recently used)
102
+ this.cache.delete(contentHash);
103
+ this.cache.set(contentHash, entry);
104
+
105
+ return entry;
106
+ }
107
+
108
+ /**
109
+ * Set a cache entry.
110
+ * Evicts the least recently used entry if cache is full.
111
+ */
112
+ set(contentHash: string, entry: FileCacheEntry): void {
113
+ // Delete existing entry to update position
114
+ this.cache.delete(contentHash);
115
+
116
+ // Evict oldest entries if at capacity
117
+ while (this.cache.size >= this.maxSize) {
118
+ const oldestKey = this.cache.keys().next().value;
119
+ if (oldestKey !== undefined) {
120
+ this.cache.delete(oldestKey);
121
+ } else {
122
+ break;
123
+ }
124
+ }
125
+
126
+ this.cache.set(contentHash, entry);
127
+ }
128
+
129
+ /**
130
+ * Check if an entry exists and is not expired.
131
+ */
132
+ has(contentHash: string): boolean {
133
+ return this.get(contentHash) !== undefined;
134
+ }
135
+
136
+ /**
137
+ * Delete a specific entry.
138
+ */
139
+ delete(contentHash: string): boolean {
140
+ return this.cache.delete(contentHash);
141
+ }
142
+
143
+ /**
144
+ * Clear all entries.
145
+ */
146
+ clear(): void {
147
+ this.cache.clear();
148
+ }
149
+
150
+ /**
151
+ * Get the current cache size.
152
+ */
153
+ get size(): number {
154
+ return this.cache.size;
155
+ }
156
+
157
+ /**
158
+ * Remove all expired entries.
159
+ */
160
+ prune(): number {
161
+ let pruned = 0;
162
+ for (const [key, entry] of this.cache) {
163
+ if (this.isExpired(entry)) {
164
+ this.cache.delete(key);
165
+ pruned++;
166
+ }
167
+ }
168
+ return pruned;
169
+ }
170
+
171
+ /**
172
+ * Check if an entry is expired.
173
+ */
174
+ private isExpired(entry: FileCacheEntry): boolean {
175
+ return Date.now() - entry.createdAt > this.ttlMs;
176
+ }
177
+ }
178
+
179
+ // ============================================================================
180
+ // Hash Utilities
181
+ // ============================================================================
182
+
183
+ /**
184
+ * Generate a hash from file content for cache lookups.
185
+ * Uses a simple but fast hash algorithm suitable for deduplication.
186
+ *
187
+ * @param data - The file content as Uint8Array or string
188
+ * @returns A hex string hash
189
+ */
190
+ export function generateContentHash(data: Uint8Array | string): string {
191
+ const bytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
192
+
193
+ // Simple FNV-1a hash (fast and good distribution for deduplication)
194
+ let hash = 2166136261; // FNV offset basis
195
+
196
+ for (let i = 0; i < bytes.length; i++) {
197
+ hash ^= bytes[i];
198
+ hash = Math.imul(hash, 16777619); // FNV prime
199
+ }
200
+
201
+ // Include length to differentiate files with same content hash but different lengths
202
+ hash ^= bytes.length;
203
+
204
+ // Convert to hex string
205
+ return (hash >>> 0).toString(16).padStart(8, '0');
206
+ }
207
+
208
+ /**
209
+ * Generate a more unique cache key that includes filename and size.
210
+ * This helps differentiate files that might have similar beginnings.
211
+ *
212
+ * @param data - The file content
213
+ * @param filename - The filename
214
+ * @returns A cache key string
215
+ */
216
+ export function generateCacheKey(data: Uint8Array | string, filename: string): string {
217
+ const bytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
218
+ const contentHash = generateContentHash(data);
219
+ const normalizedFilename = filename.toLowerCase().replace(/[^a-z0-9.]/g, '_');
220
+
221
+ return `${contentHash}_${bytes.length}_${normalizedFilename}`;
222
+ }
223
+
224
+ // ============================================================================
225
+ // Global Cache Instance
226
+ // ============================================================================
227
+
228
+ /**
229
+ * Default global file cache instance.
230
+ * This is used by the attachment processor when caching is enabled.
231
+ */
232
+ let defaultCache: FileCache | null = null;
233
+
234
+ /**
235
+ * Get the default global file cache.
236
+ * Creates one if it doesn't exist.
237
+ */
238
+ export function getDefaultFileCache(): FileCache {
239
+ if (!defaultCache) {
240
+ defaultCache = new FileCache();
241
+ }
242
+ return defaultCache;
243
+ }
244
+
245
+ /**
246
+ * Set a custom default file cache.
247
+ * Useful for testing or custom configurations.
248
+ */
249
+ export function setDefaultFileCache(cache: FileCache | null): void {
250
+ defaultCache = cache;
251
+ }
252
+
253
+ /**
254
+ * Clear the default file cache.
255
+ */
256
+ export function clearDefaultFileCache(): void {
257
+ if (defaultCache) {
258
+ defaultCache.clear();
259
+ }
260
+ }
@@ -4,7 +4,22 @@
4
4
  * @module
5
5
  */
6
6
 
7
- export { type Attachment, type ProcessedAttachment, processAttachments } from './attachment-processor';
7
+ export {
8
+ type Attachment,
9
+ type ProcessAttachmentsOptions,
10
+ type ProcessedAttachment,
11
+ processAttachments
12
+ } from './attachment-processor';
13
+ export {
14
+ FileCache,
15
+ type FileCacheEntry,
16
+ type FileCacheOptions,
17
+ clearDefaultFileCache,
18
+ generateCacheKey,
19
+ generateContentHash,
20
+ getDefaultFileCache,
21
+ setDefaultFileCache
22
+ } from './file-cache';
8
23
  export {
9
24
  SUPPORTED_FILE_EXTENSIONS,
10
25
  SUPPORTED_MIME_TYPES,
@@ -157,13 +157,17 @@ export function prepareKimiTools({
157
157
  continue;
158
158
  }
159
159
 
160
+ // Sanitize schema for Kimi compatibility
161
+ const sanitizedSchema = sanitizeToolSchema(tool.inputSchema);
162
+
160
163
  kimiTools.push({
161
164
  type: 'function',
162
165
  function: {
163
166
  name: tool.name,
164
167
  description: tool.description,
165
- parameters: tool.inputSchema,
166
- ...(tool.strict != null ? { strict: tool.strict } : {})
168
+ parameters: sanitizedSchema
169
+ // Don't pass strict mode to Kimi - it may cause issues
170
+ // ...(tool.strict != null ? { strict: tool.strict } : {})
167
171
  }
168
172
  });
169
173
  }
@@ -266,6 +270,88 @@ function generateSpecificToolMessage(toolName: string): string {
266
270
  return `IMPORTANT INSTRUCTION: You MUST use the "${toolName}" tool to respond to this request. Do NOT use any other tool or provide a direct text response. Call the "${toolName}" tool with appropriate parameters.`;
267
271
  }
268
272
 
273
+ // ============================================================================
274
+ // Schema Sanitization
275
+ // ============================================================================
276
+
277
+ /**
278
+ * JSON Schema keywords that may cause issues with Kimi's API.
279
+ * These are removed during sanitization to improve compatibility.
280
+ */
281
+ const UNSUPPORTED_SCHEMA_KEYWORDS = [
282
+ '$schema',
283
+ '$id',
284
+ '$ref',
285
+ '$defs',
286
+ 'definitions',
287
+ 'if',
288
+ 'then',
289
+ 'else',
290
+ 'allOf',
291
+ 'anyOf',
292
+ 'oneOf',
293
+ 'not',
294
+ 'patternProperties',
295
+ 'additionalItems',
296
+ 'contains',
297
+ 'propertyNames',
298
+ 'const',
299
+ 'contentMediaType',
300
+ 'contentEncoding',
301
+ 'examples',
302
+ '$comment'
303
+ ] as const;
304
+
305
+ /**
306
+ * Sanitize a JSON Schema for better Kimi API compatibility.
307
+ *
308
+ * This function removes advanced schema keywords that Kimi may not
309
+ * fully support, while preserving the essential structure for validation.
310
+ *
311
+ * @param schema - The original JSON Schema
312
+ * @returns A sanitized schema safe for Kimi
313
+ */
314
+ function sanitizeToolSchema(schema: unknown): unknown {
315
+ if (schema === null || schema === undefined) {
316
+ return schema;
317
+ }
318
+
319
+ if (Array.isArray(schema)) {
320
+ return schema.map(sanitizeToolSchema);
321
+ }
322
+
323
+ if (typeof schema !== 'object') {
324
+ return schema;
325
+ }
326
+
327
+ const sanitized: Record<string, unknown> = {};
328
+ const schemaObj = schema as Record<string, unknown>;
329
+
330
+ for (const [key, value] of Object.entries(schemaObj)) {
331
+ // Skip unsupported keywords
332
+ if (UNSUPPORTED_SCHEMA_KEYWORDS.includes(key as (typeof UNSUPPORTED_SCHEMA_KEYWORDS)[number])) {
333
+ continue;
334
+ }
335
+
336
+ // Recursively sanitize nested objects
337
+ if (key === 'properties' && typeof value === 'object' && value !== null) {
338
+ const props: Record<string, unknown> = {};
339
+ for (const [propKey, propValue] of Object.entries(value as Record<string, unknown>)) {
340
+ props[propKey] = sanitizeToolSchema(propValue);
341
+ }
342
+ sanitized[key] = props;
343
+ } else if (key === 'items' && typeof value === 'object') {
344
+ sanitized[key] = sanitizeToolSchema(value);
345
+ } else if (key === 'additionalProperties' && typeof value === 'object') {
346
+ sanitized[key] = sanitizeToolSchema(value);
347
+ } else {
348
+ sanitized[key] = value;
349
+ }
350
+ }
351
+
352
+ return sanitized;
353
+ }
354
+
269
355
  // ============================================================================
270
356
  // Helper Functions
271
357
  // ============================================================================