Skip to content

Commit 5a82a80

Browse files
committed
Implement symbol caching to optimize prompt generation performance and reduce redundant file operations
1 parent 2220252 commit 5a82a80

15 files changed

Lines changed: 369 additions & 92 deletions

File tree

‎apps/browser/webpack.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const config = (_: any, argv: Record<string, any>): webpack.Configuration => {
7575
},
7676
resolve: {
7777
extensions: ['.ts', '.tsx', '.js', '.jsx'],
78-
plugins: [new TsconfigPathsPlugin()]
78+
plugins: [new TsconfigPathsPlugin() as any]
7979
},
8080
plugins,
8181
stats: 'errors-warnings'

‎apps/editor/src/constants/values.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export const CHECKPOINT_DEFAULT_LIFESPAN = 24
22
export const MAX_FILE_TOKENS_FOR_COMMIT_MESSAGE = 20000
33
export const MAX_PROMPT_CHARS_IN_COMMIT_MESSAGE = 1000
44
export const GIT_LOG_SINCE_DURATION = '6 months ago'
5+
export const SYMBOL_CACHE_DURATION = 5 * 60 * 1000

‎apps/editor/src/views/prompt/backend/prompt-view-provider.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import { webview_html } from '@/views/shared/utils/webview-html'
114114
import { get_selected_files } from '@/context/helpers/get-selected-files'
115115
import { normalize_path } from '@/utils/normalize-path'
116116
import { replace_symbols } from './utils/symbols/replace-symbols'
117+
import { SymbolCacheManager } from './utils/symbols/symbol-cache'
117118

118119
export class PromptViewProvider implements vscode.WebviewViewProvider {
119120
public readonly extension_uri: vscode.Uri
@@ -163,6 +164,7 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
163164
public prompt_view_api_calls_manager!: PromptViewApiCallsManager
164165
public response_history: ResponseHistoryItem[] = []
165166
public message_listeners: ((message: BackendMessage) => void)[] = []
167+
public symbols_cache = new SymbolCacheManager()
166168

167169
// Voice input
168170
public is_recording = false
@@ -202,6 +204,8 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
202204
return state.instructions[state.active_index] || ''
203205
}
204206

207+
private _save_instructions_timeout: NodeJS.Timeout | null = null
208+
205209
public preview_switch_choice_resolver:
206210
| ((choice: 'Switch' | undefined) => void)
207211
| undefined = undefined
@@ -546,13 +550,15 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
546550
instructions: this.current_edit_files_instruction,
547551
extension_context: this.extension_context,
548552
workspace_provider: this.workspace_provider,
549-
remove_images: true
553+
remove_images: true,
554+
symbols_cache: this.symbols_cache
550555
})
551556
const ask_instructions_result = await replace_symbols({
552557
instructions: this.current_ask_about_context_instruction,
553558
extension_context: this.extension_context,
554559
workspace_provider: this.workspace_provider,
555-
remove_images: true
560+
remove_images: true,
561+
symbols_cache: this.symbols_cache
556562
})
557563

558564
this.send_message({
@@ -647,7 +653,20 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
647653
handle_get_instructions(this)
648654
} else if (message.command == 'SAVE_INSTRUCTIONS') {
649655
await handle_save_instructions(this, message)
650-
this.send_token_count()
656+
if (this._save_instructions_timeout) {
657+
clearTimeout(this._save_instructions_timeout)
658+
}
659+
660+
const current_instruction =
661+
message.instruction.instructions[message.instruction.active_index]
662+
663+
if (!current_instruction) {
664+
this.send_token_count()
665+
} else {
666+
this._save_instructions_timeout = setTimeout(() => {
667+
this.send_token_count()
668+
}, 1000)
669+
}
651670
} else if (message.command == 'GET_CONNECTION_STATUS') {
652671
handle_get_connection_status(this)
653672
} else if (message.command == 'GET_WEB_CONFIGURATIONS') {

‎apps/editor/src/views/prompt/backend/utils/symbols/clipboard-paths/replace-clipboard-paths-symbol.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provi
55
import { get_all_workspace_files } from '@/context/helpers/get-all-workspace-files'
66
import { extract_paths_from_text } from '@/utils/extract-paths-from-text'
77
import { normalize_path } from '@/utils/normalize-path'
8+
import { SymbolCacheManager } from '../symbol-cache'
89

910
export const replace_clipboard_paths_symbol = async (params: {
1011
instruction: string
1112
workspace_provider: WorkspaceProvider
13+
symbols_cache?: SymbolCacheManager
1214
}): Promise<{ instruction: string; additional_files_definitions: string }> => {
1315
const regex = /#ClipboardPaths/g
1416
let result_instruction = params.instruction
@@ -24,6 +26,17 @@ export const replace_clipboard_paths_symbol = async (params: {
2426
return { instruction: result_instruction, additional_files_definitions }
2527
}
2628

29+
const cache_key = `#ClipboardPaths:${text}`
30+
31+
if (params.symbols_cache) {
32+
const cached = params.symbols_cache.get(cache_key)
33+
if (cached) {
34+
additional_files_definitions += cached.definitions
35+
result_instruction = result_instruction.replace(regex, cached.replacement)
36+
return { instruction: result_instruction, additional_files_definitions }
37+
}
38+
}
39+
2740
const workspace_roots = params.workspace_provider.get_workspace_roots()
2841
if (workspace_roots.length === 0) {
2942
result_instruction = result_instruction.replace(regex, '')
@@ -103,11 +116,21 @@ export const replace_clipboard_paths_symbol = async (params: {
103116

104117
if (files_markdown) {
105118
additional_files_definitions += `# Additional files\n\n${files_markdown}`
106-
result_instruction = result_instruction.replace(
107-
regex,
108-
display_paths.join(', ')
109-
)
119+
const replacement = display_paths.join(', ')
120+
121+
if (params.symbols_cache) {
122+
params.symbols_cache.set(
123+
cache_key,
124+
replacement,
125+
additional_files_definitions
126+
)
127+
}
128+
129+
result_instruction = result_instruction.replace(regex, replacement)
110130
} else {
131+
if (params.symbols_cache) {
132+
params.symbols_cache.set(cache_key, '', '')
133+
}
111134
result_instruction = result_instruction.replace(regex, '')
112135
}
113136

0 commit comments

Comments
 (0)