-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathrun.ts
More file actions
81 lines (73 loc) · 2.55 KB
/
run.ts
File metadata and controls
81 lines (73 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { updateBotRequest } from '@/lib/bot/request-logging';
import { runBotAgent } from '@/lib/bot/agent-runner';
import { extractAndUploadImages } from '@/lib/bot/images';
import type { PlatformIntegration, User } from '@kilocode/db';
import type { Message, Thread } from 'chat';
import { emoji } from 'chat';
import { captureException } from '@sentry/nextjs';
export async function processMessage({
thread,
message,
platformIntegration,
user,
botRequestId,
}: {
thread: Thread;
message: Message;
platformIntegration: PlatformIntegration;
user: User;
botRequestId: string | undefined;
}) {
const startedAt = Date.now();
// Extract and upload any image attachments from the Slack message to R2.
// This runs before the agent loop so the images are ready when a Cloud Agent
// session is spawned. Failures are non-fatal — we log and continue without images.
let images: Awaited<ReturnType<typeof extractAndUploadImages>>;
try {
images = await extractAndUploadImages(message, user.id);
} catch (error) {
console.error('[KiloBot] Failed to extract/upload images, continuing without them:', error);
captureException(error, {
tags: { component: 'kilo-bot', op: 'extract-upload-images' },
});
}
try {
const result = await runBotAgent({
thread,
message,
rawMessage: message,
platformIntegration,
user,
botRequestId,
prompt: message.text,
images,
});
if (botRequestId) {
updateBotRequest(botRequestId, {
...(result.startedCloudAgentSession ? {} : { status: 'completed' }),
steps: [...result.collectedSteps],
responseTimeMs: result.responseTimeMs,
});
}
if (!result.startedCloudAgentSession) {
const received = thread.createSentMessageFromMessage(message);
await thread.post({ markdown: result.finalText });
await Promise.all([received.removeReaction(emoji.eyes), received.addReaction(emoji.check)]);
}
} catch (error) {
const errMsg = error instanceof Error ? error.message : String(error);
if (botRequestId) {
updateBotRequest(botRequestId, {
status: 'error',
errorMessage: errMsg.slice(0, 2000),
responseTimeMs: Date.now() - startedAt,
});
}
console.error(`[KiloBot] Error during bot run:`, errMsg, error);
const received = thread.createSentMessageFromMessage(message);
await Promise.all([
received.removeReaction(emoji.eyes).catch(() => {}),
thread.post(`Sorry, there was an error calling the AI service: ${errMsg.slice(0, 200)}`),
]);
}
}