Skip to content

Commit 5e12845

Browse files
committed
Add comprehensive logging for image processing
- Add detailed logs for image cache operations (save/load/clear) - Add logs for image conversion process with file sizes - Add logs for Cursor API payload with image metadata - Add logs for task creation with image attachment details - Add file size and TTL information in cache operations - Better visibility into image processing pipeline
1 parent e87d614 commit 5e12845

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

src/agent.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,21 @@ function createToolsWithContext(context: AgentContext): ToolSet {
137137

138138
// Get images from cache
139139
const images = getImagesFromCache(context.userId, context.chatId);
140+
console.log(`📸 Found ${images.length} images in cache for user ${context.userId} in chat ${context.chatId}`);
141+
142+
if (images.length > 0) {
143+
console.log(`📸 Images details:`, images.map(img => ({
144+
uuid: img.uuid,
145+
dimensions: `${img.dimension.width}x${img.dimension.height}`,
146+
dataSize: `${Math.round(img.data.length / 1024)}KB`
147+
})));
148+
}
140149

141150
// Create fresh CursorApi instance and start the task
142151
const cursorApi = new CursorApi({ cookies });
152+
console.log(`🚀 Starting Cursor task with ${images.length} images attached`);
143153
const result = await cursorApi.startSimpleComposer(repoUrl, taskDescription, branch, model, images.length > 0 ? images : undefined);
154+
console.log(`✅ Cursor task started successfully: ${result.composer.bcId}`);
144155

145156
// Update context with validated instance
146157
context.cursorApi = cursorApi;
@@ -156,6 +167,7 @@ function createToolsWithContext(context: AgentContext): ToolSet {
156167
});
157168

158169
// Clear images from cache after successful task creation
170+
console.log(`🧹 Clearing ${images.length} images from cache after successful task creation`);
159171
clearImagesFromCache(context.userId, context.chatId);
160172

161173
return {

src/bot.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,14 @@ bot.on('message:photo', async (ctx) => {
339339
const photoBuffer = Buffer.from(await response.arrayBuffer());
340340

341341
// Convert to Cursor format
342+
console.log(`📸 Converting Telegram photo ${photo.file_id} to Cursor format`);
342343
const cursorImage = await convertTelegramImageToCursorFormat(photoBuffer, photo.file_id);
344+
console.log(`📸 Converted image: ${cursorImage.dimension.width}x${cursorImage.dimension.height}, ${Math.round(cursorImage.data.length / 1024)}KB`);
343345

344346
// Get existing images from cache and add new one
345347
const existingImages = getImagesFromCache(ctx.from!.id, ctx.chat!.id);
346348
const allImages = [...existingImages, cursorImage];
349+
console.log(`📸 Total images in cache: ${allImages.length} (${existingImages.length} existing + 1 new)`);
347350

348351
// Save to cache
349352
saveImagesToCache(ctx.from!.id, ctx.chat!.id, allImages);

src/cursor-api.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ export class CursorApi {
196196
async startBackgroundComposer(
197197
request: StartBackgroundComposerRequest
198198
): Promise<StartBackgroundComposerResponse> {
199+
// Log payload info (without actual image data to avoid spam)
200+
const payloadInfo = {
201+
bcId: request.bcId,
202+
repoUrl: request.repoUrl,
203+
conversationHistory: request.conversationHistory?.map(msg => ({
204+
text: msg.text.substring(0, 100) + (msg.text.length > 100 ? '...' : ''),
205+
type: msg.type,
206+
hasImages: !!msg.images,
207+
imageCount: msg.images?.length || 0
208+
}))
209+
};
210+
console.log(`📤 Sending request to Cursor API:`, payloadInfo);
211+
199212
return this.request<StartBackgroundComposerResponse>(
200213
'/api/auth/startBackgroundComposerFromSnapshot',
201214
{
@@ -353,8 +366,15 @@ export class CursorApi {
353366
modelName: SupportedModel | string = SupportedModel.CLAUDE_4_SONNET_THINKING,
354367
images?: ConversationImage[]
355368
): Promise<StartBackgroundComposerResponse> {
369+
console.log(`🔧 Creating Cursor config with ${images?.length || 0} images`);
356370
const config = this.createBaseComposerConfig(repoUrl, task, branch, modelName, images);
357371

372+
// Log the conversation history to see if images are included
373+
if (config.conversationHistory && config.conversationHistory.length > 0) {
374+
const firstMessage = config.conversationHistory[0];
375+
console.log(`📝 First message has ${firstMessage.images?.length || 0} images attached`);
376+
}
377+
358378
return this.startBackgroundComposer(config as StartBackgroundComposerRequest);
359379
}
360380

src/image-cache.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ export function saveImagesToCache(userId: number, chatId: number, images: Conver
6161
timestamp: Date.now()
6262
};
6363

64+
const jsonSize = JSON.stringify(cacheData).length;
65+
console.log(`📸 Saving ${images.length} images to cache file ${filePath} (${Math.round(jsonSize / 1024)}KB)`);
66+
6467
fs.writeFileSync(filePath, JSON.stringify(cacheData, null, 2));
65-
console.log(`Saved ${images.length} images to cache for user ${userId} in chat ${chatId}`);
68+
console.log(`📸 Successfully saved ${images.length} images to cache for user ${userId} in chat ${chatId}`);
6669
} catch (error) {
6770
console.error('Error saving images to cache:', error);
6871
}
@@ -75,20 +78,28 @@ export function getImagesFromCache(userId: number, chatId: number): Conversation
7578

7679
try {
7780
if (!fs.existsSync(filePath)) {
81+
console.log(`📸 No cache file found for user ${userId} in chat ${chatId}`);
7882
return [];
7983
}
8084

8185
const stats = fs.statSync(filePath);
8286
const now = Date.now();
87+
const age = now - stats.mtime.getTime();
88+
89+
console.log(`📸 Cache file age: ${Math.round(age / 1000)}s (TTL: ${CACHE_TTL / 1000}s)`);
8390

8491
// Check if expired
85-
if (now - stats.mtime.getTime() > CACHE_TTL) {
92+
if (age > CACHE_TTL) {
93+
console.log(`📸 Cache expired, removing file`);
8694
fs.unlinkSync(filePath);
8795
return [];
8896
}
8997

9098
const cacheData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
91-
return cacheData.images || [];
99+
const images = cacheData.images || [];
100+
console.log(`📸 Successfully loaded ${images.length} images from cache`);
101+
102+
return images;
92103
} catch (error) {
93104
console.error('Error reading images from cache:', error);
94105
return [];
@@ -102,8 +113,14 @@ export function clearImagesFromCache(userId: number, chatId: number) {
102113

103114
try {
104115
if (fs.existsSync(filePath)) {
116+
const stats = fs.statSync(filePath);
117+
const fileSize = Math.round(stats.size / 1024);
118+
console.log(`📸 Clearing cache file ${filePath} (${fileSize}KB)`);
119+
105120
fs.unlinkSync(filePath);
106-
console.log(`Cleared image cache for user ${userId} in chat ${chatId}`);
121+
console.log(`📸 Successfully cleared image cache for user ${userId} in chat ${chatId}`);
122+
} else {
123+
console.log(`📸 No cache file to clear for user ${userId} in chat ${chatId}`);
107124
}
108125
} catch (error) {
109126
console.error('Error clearing images from cache:', error);

src/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,24 @@ export async function convertTelegramImageToCursorFormat(
123123
fileBuffer: Buffer,
124124
telegramFileId: string
125125
): Promise<ConversationImage> {
126+
console.log(`📸 Converting ${fileBuffer.length} bytes image to PNG format`);
127+
126128
// Convert image to PNG and get dimensions
127129
const pngBuffer = await sharp(fileBuffer)
128130
.png()
129131
.toBuffer();
130132

131133
const metadata = await sharp(pngBuffer).metadata();
134+
console.log(`📸 PNG conversion complete: ${metadata.width}x${metadata.height}, ${pngBuffer.length} bytes`);
135+
136+
const uuid = `tg-${telegramFileId}-${Date.now()}`;
132137

133138
return {
134139
data: pngBuffer.toString('base64'),
135140
dimension: {
136141
width: metadata.width || 0,
137142
height: metadata.height || 0,
138143
},
139-
uuid: `tg-${telegramFileId}-${Date.now()}`,
144+
uuid,
140145
};
141146
}

0 commit comments

Comments
 (0)