Skip to content

Commit

Permalink
refactor: added ignore patterns to remove or redact files from context
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodacus committed Nov 14, 2024
1 parent 821ccdd commit d36aa17
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
2 changes: 0 additions & 2 deletions app/components/chat/Chat.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,10 @@ export const ChatImpl = memo(({ initialMessages, storeMessageHistory }: ChatProp
toolCallId: toolCall.toolCallId,
});
logger.info('Tool Call Complete', toolCall.toolName, `${result}`.split('---')[0]);
// addToolResult({ toolCallId: toolCall.toolCallId, result: result });
return result;
} catch (error) {
logger.error('Error calling tool:', toolCall.toolName, error);
toast.error('There was an error processing your request');
// addToolResult({ toolCallId: toolCall.toolCallId, result: 'Error calling tool:' + error });
return 'Error calling tool';
}
},
Expand Down
40 changes: 37 additions & 3 deletions app/lib/stores/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { webcontainer } from '../webcontainer';
import { map, type MapStore } from 'nanostores';
import type { IToolsConfig } from '~/utils/types';
import Cookies from 'js-cookie';
import { matchPatterns } from '~/utils/matchPatterns';

export class ToolStore {
#webcontainer: Promise<WebContainer>;
Expand Down Expand Up @@ -78,7 +79,40 @@ export class ToolStore {
await workbenchStore.updateFile(fullPath, file.content)
await workbenchStore.saveFile(file.path)
}
let templatePromptFile = files.filter(x => x.path.startsWith(".bolt")).filter(x => x.name == 'prompt')

let filteredFiles = files;

// ignoring common unwanted files
// exclude .git
filteredFiles = filteredFiles.filter(x => x.path.startsWith(".git") == false)
// exclude lock files
let comminLockFiles = ["package-lock.json", "yarn.lock", "pnpm-lock.yaml"]
filteredFiles = filteredFiles.filter(x => comminLockFiles.includes(x.name) == false)
// exclude .bolt
filteredFiles = filteredFiles.filter(x => x.path.startsWith(".bolt") == false)


// check for ignore file in .bolt folder
let templateIgnoreFile = files.find(x => x.path.startsWith(".bolt") && x.name == "ignore")
if (templateIgnoreFile) {
// redacting files specified in ignore file
let ignorepatterns = templateIgnoreFile.content.split("\n").map(x => x.trim())
filteredFiles = filteredFiles.filter(x => matchPatterns(x.path, ignorepatterns) == false)
let redactedFiles = filteredFiles.filter(x => matchPatterns(x.path, ignorepatterns))
redactedFiles = redactedFiles.map(x => {
return {
...x,
content: "redacted"
}
})
filteredFiles = [
...filteredFiles,
...redactedFiles
]
}

let templatePromptFile = files.filter(x => x.path.startsWith(".bolt")).find(x => x.name == 'prompt')

return this.generateFormattedResult(`template imported successfully`, `
here is the imported content,
these files are loaded into the bolt. to not write them again, if it don't require changes
Expand All @@ -87,13 +121,13 @@ export class ToolStore {
${templatePromptFile ? `
<User Instruction>
${templatePromptFile[0].content}
${templatePromptFile.content}
<User Instruction>
`: ''
}
<Imported Files>
${JSON.stringify(files.filter(x => !(x.path.endsWith(".svg") || x.path.endsWith(".md"))), null, 2)}
${JSON.stringify(filteredFiles, null, 2)}
<Imported Files>
`)
} catch (error) {
Expand Down
28 changes: 28 additions & 0 deletions app/utils/matchPatterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* A utility to match glob patterns in the browser
* Supports basic glob features like * and **
*/
export function isMatch(path: string, pattern: string): boolean {
// Convert glob pattern to regex
const regex = pattern
// Escape special regex characters except * and /
.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
// Replace ** with special marker
.replace(/\*\*/g, '{{GLOBSTAR}}')
// Replace single * with non-slash matcher
.replace(/\*/g, '[^/]*')
// Replace globstar marker with proper pattern
.replace(/{{GLOBSTAR}}/g, '.*')
// Anchor pattern to full path match
.replace(/^/, '^')
.replace(/$/, '$');

return new RegExp(regex).test(path);
}

/**
* Match multiple patterns against a path
*/
export function matchPatterns(path: string, patterns: string[]): boolean {
return patterns.some(pattern => isMatch(path, pattern));
}

0 comments on commit d36aa17

Please sign in to comment.