From 7d02bb58fc9239a62540b7dc895cd8cdee618551 Mon Sep 17 00:00:00 2001 From: Brian Hackett Date: Wed, 22 Jan 2025 15:26:40 -0800 Subject: [PATCH] Ignore package-lock.json --- app/components/chat/Chat.client.tsx | 14 +++++++++++++- app/lib/.server/llm/chat-anthropic.ts | 2 ++ app/routes/api.chat.ts | 2 +- app/utils/fileUtils.ts | 5 +++++ app/utils/folderImport.ts | 23 +++++++++++------------ 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/app/components/chat/Chat.client.tsx b/app/components/chat/Chat.client.tsx index 8724ddb9..058ca925 100644 --- a/app/components/chat/Chat.client.tsx +++ b/app/components/chat/Chat.client.tsx @@ -28,6 +28,8 @@ import { getCurrentIFrame } from '../workbench/Preview'; import { getCurrentMouseData } from '../workbench/PointSelector'; import { assert } from '~/lib/replay/ReplayProtocolClient'; import { anthropicNumFreeUsesCookieName, anthropicApiKeyCookieName, MaxFreeUses } from '~/utils/freeUses'; +import type { FileMap } from '~/lib/stores/files'; +import { shouldIncludeFile } from '~/utils/fileUtils'; const toastAnimation = cssTransition({ enter: 'animated fadeInRight', @@ -130,6 +132,16 @@ function handleInjectMessages(baseMessages: Message[], injectedMessages: Injecte return messages; } +function filterFiles(files: FileMap): FileMap { + const rv: FileMap = {}; + for (const [path, file] of Object.entries(files)) { + if (shouldIncludeFile(path)) { + rv[path] = file; + } + } + return rv; +} + export const ChatImpl = memo( ({ description, initialMessages, storeMessageHistory, importChat, exportChat }: ChatProps) => { useShortcuts(); @@ -151,7 +163,7 @@ export const ChatImpl = memo( const { messages: baseMessages, isLoading, input, handleInputChange, setInput, stop, append } = useChat({ api: '/api/chat', body: { - files, + files: filterFiles(files), promptId, }, sendExtraMessageFields: true, diff --git a/app/lib/.server/llm/chat-anthropic.ts b/app/lib/.server/llm/chat-anthropic.ts index c0a15241..8ec495c5 100644 --- a/app/lib/.server/llm/chat-anthropic.ts +++ b/app/lib/.server/llm/chat-anthropic.ts @@ -38,6 +38,8 @@ export async function chatAnthropic(chatController: ChatStreamController, apiKey }); } + console.log("AnthropicMessages", JSON.stringify(messageParams, null, 2)); + const response = await anthropic.messages.create({ model: "claude-3-5-sonnet-20241022", messages: messageParams, diff --git a/app/routes/api.chat.ts b/app/routes/api.chat.ts index 153ed819..8e4f320d 100644 --- a/app/routes/api.chat.ts +++ b/app/routes/api.chat.ts @@ -60,7 +60,7 @@ async function chatAction({ context, request }: ActionFunctionArgs) { await chatAnthropic(chatController, anthropicApiKey, system, coreMessages); } catch (e) { console.error(e); - chatController.writeText("Error chatting with Anthropic."); + chatController.writeText(`Error chatting with Anthropic: ${e}`); } controller.close(); diff --git a/app/utils/fileUtils.ts b/app/utils/fileUtils.ts index fcf2a017..009adbd6 100644 --- a/app/utils/fileUtils.ts +++ b/app/utils/fileUtils.ts @@ -16,6 +16,7 @@ export const IGNORE_PATTERNS = [ '**/npm-debug.log*', '**/yarn-debug.log*', '**/yarn-error.log*', + '**/package-lock.json', ]; export const MAX_FILES = 1000; @@ -39,6 +40,10 @@ export const isBinaryFile = async (file: File): Promise => { }; export const shouldIncludeFile = (path: string): boolean => { + const projectDirectory = "/home/project/"; + if (path.startsWith(projectDirectory)) { + path = path.substring(projectDirectory.length); + } return !ig.ignores(path); }; diff --git a/app/utils/folderImport.ts b/app/utils/folderImport.ts index 27d98a8f..de98d3bc 100644 --- a/app/utils/folderImport.ts +++ b/app/utils/folderImport.ts @@ -1,5 +1,5 @@ import type { Message } from 'ai'; -import { generateId } from './fileUtils'; +import { generateId, shouldIncludeFile } from './fileUtils'; import { detectProjectCommands, createCommandsMessage } from './projectCommands'; export interface FileArtifact { @@ -41,19 +41,18 @@ export const createChatFromFolder = async ( ? `\n\nSkipped ${binaryFiles.length} binary files:\n${binaryFiles.map((f) => `- ${f}`).join('\n')}` : ''; + let filesContent = `I've imported the contents of the "${folderName}" folder.${binaryFilesMessage}`; + filesContent += ``; + for (const file of fileArtifacts) { + if (shouldIncludeFile(file.path)) { + filesContent += `${file.content}\n\n`; + } + } + filesContent += ``; + const filesMessage: Message = { role: 'assistant', - content: `I've imported the contents of the "${folderName}" folder.${binaryFilesMessage} - - -${fileArtifacts - .map( - (file) => ` -${file.content} -`, - ) - .join('\n\n')} -`, + content: filesContent, id: generateId(), createdAt: new Date(), };