@@ -3,6 +3,8 @@ import { getWebSocketClient } from '@/lib/ws/connection';
|
||||
import { useQueue } from './domains/session/use-queue';
|
||||
import type { MessageAttachment } from '@/components/task/chat/chat-input-container';
|
||||
import type { ActiveDocument, DocumentComment } from '@/lib/state/slices/ui/types';
|
||||
import type { ContextFile } from '@/lib/state/context-files-store';
|
||||
import type { CustomPrompt } from '@/lib/types/http';
|
||||
|
||||
function buildDocumentContext(activeDocument: ActiveDocument | null, comments?: DocumentComment[]): string {
|
||||
if (!activeDocument) return '';
|
||||
@@ -24,6 +26,35 @@ function buildDocumentContext(activeDocument: ActiveDocument | null, comments?:
|
||||
return `\n\n<kandev-system>\nACTIVE DOCUMENT: The user is editing "${activeDocument.name}" (${activeDocument.path}) side-by-side with this chat.\nRead this file to understand the context before responding.\n</kandev-system>`;
|
||||
}
|
||||
|
||||
function buildContextFilesContext(contextFiles: ContextFile[], prompts: CustomPrompt[]): string {
|
||||
const files = contextFiles.filter((f) => !f.path.startsWith('prompt:') && f.path !== 'plan:context');
|
||||
const promptFiles = contextFiles.filter((f) => f.path.startsWith('prompt:'));
|
||||
|
||||
let context = '';
|
||||
|
||||
if (files.length > 0) {
|
||||
const fileList = files.map((f) => `- ${f.path}`).join('\n');
|
||||
context += `\n\n<kandev-system>\nCONTEXT FILES: The user has attached the following files as context. Read these files to understand what the user is referring to:\n${fileList}\n</kandev-system>`;
|
||||
}
|
||||
|
||||
if (promptFiles.length > 0) {
|
||||
const promptsById = new Map(prompts.map((p) => [p.id, p]));
|
||||
const resolved = promptFiles
|
||||
.map((f) => {
|
||||
const id = f.path.replace('prompt:', '');
|
||||
const prompt = promptsById.get(id);
|
||||
return prompt ? `### ${prompt.name}\n${prompt.content}` : null;
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
if (resolved.length > 0) {
|
||||
context += `\n\n<kandev-system>\nCONTEXT PROMPTS: The user has included the following prompt instructions as context:\n${resolved.join('\n\n')}\n</kandev-system>`;
|
||||
}
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
export function useMessageHandler(
|
||||
resolvedSessionId: string | null,
|
||||
taskId: string | null,
|
||||
@@ -32,12 +63,14 @@ export function useMessageHandler(
|
||||
planMode: boolean = false,
|
||||
isAgentBusy: boolean = false,
|
||||
activeDocument: ActiveDocument | null = null,
|
||||
documentComments: DocumentComment[] = []
|
||||
documentComments: DocumentComment[] = [],
|
||||
contextFiles: ContextFile[] = [],
|
||||
prompts: CustomPrompt[] = []
|
||||
) {
|
||||
const { queue } = useQueue(resolvedSessionId);
|
||||
|
||||
const handleSendMessage = useCallback(
|
||||
async (message: string, attachments?: MessageAttachment[], hasReviewComments?: boolean) => {
|
||||
async (message: string, attachments?: MessageAttachment[], hasReviewComments?: boolean, inlineMentions?: ContextFile[]) => {
|
||||
if (!taskId || !resolvedSessionId) {
|
||||
console.error('No active task session. Start an agent before sending a message.');
|
||||
return;
|
||||
@@ -47,13 +80,23 @@ export function useMessageHandler(
|
||||
|
||||
const trimmedMessage = message.trim();
|
||||
|
||||
// Merge stored context files with inline @mentions from the editor
|
||||
const allContextFiles = [...contextFiles, ...(inlineMentions || [])];
|
||||
|
||||
// Append document context if active
|
||||
const documentContext = buildDocumentContext(activeDocument, documentComments);
|
||||
const finalMessage = documentContext ? trimmedMessage + documentContext : trimmedMessage;
|
||||
const contextFilesContext = buildContextFilesContext(allContextFiles, prompts);
|
||||
const finalMessage = trimmedMessage + documentContext + contextFilesContext;
|
||||
|
||||
// Include active model in the request if it differs from the session model
|
||||
const modelToSend = activeModel && activeModel !== sessionModel ? activeModel : undefined;
|
||||
|
||||
// Build context_files metadata for the WS payload (exclude prompts and plan:context)
|
||||
const realFiles = allContextFiles.filter((f) => !f.path.startsWith('prompt:') && f.path !== 'plan:context');
|
||||
const contextFilesMeta = realFiles.length > 0
|
||||
? realFiles.map((f) => ({ path: f.path, name: f.name }))
|
||||
: undefined;
|
||||
|
||||
// Use longer timeout when sending attachments (images can be large)
|
||||
const hasAttachments = attachments && attachments.length > 0;
|
||||
const timeoutMs = hasAttachments ? 30000 : 10000;
|
||||
@@ -82,11 +125,12 @@ export function useMessageHandler(
|
||||
...(planMode && { plan_mode: true }),
|
||||
...(hasReviewComments && { has_review_comments: true }),
|
||||
...(hasAttachments && { attachments }),
|
||||
...(contextFilesMeta && { context_files: contextFilesMeta }),
|
||||
},
|
||||
timeoutMs
|
||||
);
|
||||
},
|
||||
[resolvedSessionId, taskId, activeModel, sessionModel, planMode, isAgentBusy, queue, activeDocument, documentComments]
|
||||
[resolvedSessionId, taskId, activeModel, sessionModel, planMode, isAgentBusy, queue, activeDocument, documentComments, contextFiles, prompts]
|
||||
);
|
||||
|
||||
return { handleSendMessage };
|
||||
|
||||
Reference in New Issue
Block a user