Skip to content

Commit 9786e84

Browse files
committed
Update token counting mechanism to include detailed breakdowns for files, instructions, and formats
- to 'token count' message, add keys 'edit instructions count' 'ask instructions count'. Now TokenCountMessage should re-send whenever instructions change. - remove `apps/editor/src/views/prompt/backend/message-handlers/handle-get-token-count.ts` if redundant - to properly calculate [Fragment], you need to temporaroly replace symbols - TokenCountMessage should also include 'edit format instructions count' - place separator below 'code at cursor' items - [Fragment] should be suffixed with 'token_count', not just 'count' - rename 'token count' to 'selected files token count' └── apps └── editor ├── package.json └── src ├── constants │ └── edit-format-instructions.ts ├── context │ └── context-initialization.ts └── views └── prompt ├── backend │ ├── message-handlers │ │ ├── handle-autofill.ts │ │ └── index.ts │ ├── prompt-view-provider.ts │ └── utils │ └── symbols │ ├── clipboard-paths │ │ └── replace-clipboard-paths-symbol.ts │ ├── fragment │ │ └── replace-fragment-symbol.ts │ ├── git │ │ └── replace-git-symbols.ts │ ├── image │ │ └── replace-image-symbol.ts │ ├── pasted-text │ │ └── replace-pasted-text-symbol.ts │ ├── replace-symbols.ts │ ├── saved-context │ │ └── replace-saved-context-symbol.ts │ ├── selection │ │ └── replace-selection-symbol.ts │ ├── skill │ │ └── replace-skill-symbol.ts │ └── website │ └── replace-website-symbol.ts ├── frontend │ ├── Main │ │ ├── Main.tsx │ │ └── MainView │ │ └── MainView.tsx │ ├── Prompt.tsx │ └── hooks │ └── use-prompt │ └── use-prompt.ts └── types └── messages.ts
1 parent a2f2202 commit 9786e84

9 files changed

Lines changed: 76 additions & 35 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,11 @@
460460
},
461461
{
462462
"command": "codeWebChat.selectReferencingFiles",
463-
"group": "1_action@7"
463+
"group": "2_action@1"
464464
},
465465
{
466466
"command": "codeWebChat.selectDefinitionFile",
467-
"group": "1_action@8"
467+
"group": "2_action@2"
468468
}
469469
],
470470
"commandPalette": [

‎apps/editor/src/views/prompt/backend/message-handlers/handle-get-token-count.ts‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎apps/editor/src/views/prompt/backend/message-handlers/index.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,3 @@ export * from './handle-delete-api-configuration'
6666
export * from './handle-update-last-used-web-configuration'
6767
export * from './handle-request-return-home'
6868
export * from './handle-pick-tasks-workspace'
69-
export * from './handle-get-token-count'

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

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ import {
7979
handle_delete_api_configuration,
8080
handle_update_last_used_web_configuration,
8181
handle_request_return_home,
82-
handle_pick_tasks_workspace,
83-
handle_get_token_count
82+
handle_pick_tasks_workspace
8483
} from './message-handlers'
8584
import { handle_update_api_configuration } from './message-handlers/handle-update-api-configuration'
8685
import { handle_pick_model_provider } from './message-handlers/handle-pick-model-provider'
@@ -114,6 +113,13 @@ import { SharedContextState } from '@/context/shared-context-state'
114113
import { webview_html } from '@/views/shared/utils/webview-html'
115114
import { get_selected_files } from '@/context/helpers/get-selected-files'
116115
import { normalize_path } from '@/utils/normalize-path'
116+
import { replace_symbols } from './utils/symbols/replace-symbols'
117+
import {
118+
EDIT_FORMAT_INSTRUCTIONS_WHOLE,
119+
EDIT_FORMAT_INSTRUCTIONS_TRUNCATED,
120+
EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE,
121+
EDIT_FORMAT_INSTRUCTIONS_DIFF
122+
} from '@/constants/edit-format-instructions'
117123

118124
export class PromptViewProvider implements vscode.WebviewViewProvider {
119125
public readonly extension_uri: vscode.Uri
@@ -158,6 +164,7 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
158164
workspace_name?: string
159165
}[] = []
160166
public api_call_abort_controller: AbortController | null = null
167+
public current_selected_files_token_count: number = 0
161168
public prompt_view_api_calls_manager!: PromptViewApiCallsManager
162169
public response_history: ResponseHistoryItem[] = []
163170
public message_listeners: ((message: BackendMessage) => void)[] = []
@@ -398,12 +405,9 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
398405
)
399406

400407
token_count_emitter.on('token-count-updated', (token_count: number) => {
408+
this.current_selected_files_token_count = token_count
401409
if (this.webview_view) {
402-
this.send_message({
403-
command: 'TOKEN_COUNT_UPDATED',
404-
token_count
405-
})
406-
410+
this.send_token_count()
407411
this.send_selected_files()
408412
}
409413
})
@@ -536,6 +540,50 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
536540
controllers.forEach((item) => item.controller.abort('Preview finished.'))
537541
}
538542

543+
public async send_token_count() {
544+
const edit_instructions_result = await replace_symbols({
545+
instruction: this.current_edit_files_instruction,
546+
extension_context: this.extension_context,
547+
workspace_provider: this.workspace_provider,
548+
remove_images: true
549+
})
550+
const ask_instructions_result = await replace_symbols({
551+
instruction: this.current_ask_about_context_instruction,
552+
extension_context: this.extension_context,
553+
workspace_provider: this.workspace_provider,
554+
remove_images: true
555+
})
556+
557+
let formatted_system_instructions = ''
558+
const edit_format_instructions = {
559+
whole: EDIT_FORMAT_INSTRUCTIONS_WHOLE,
560+
truncated: EDIT_FORMAT_INSTRUCTIONS_TRUNCATED,
561+
'search-replace': EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE,
562+
diff: EDIT_FORMAT_INSTRUCTIONS_DIFF
563+
}[this.edit_format]
564+
if (edit_format_instructions) {
565+
formatted_system_instructions = `# Output formatting\n\n${edit_format_instructions}`
566+
}
567+
568+
this.send_message({
569+
command: 'TOKEN_COUNT_UPDATED',
570+
selected_files_token_count: this.current_selected_files_token_count,
571+
edit_instructions_token_count: Math.ceil(
572+
(edit_instructions_result.instruction.length +
573+
edit_instructions_result.skill_definitions.length) /
574+
4
575+
),
576+
ask_instructions_token_count: Math.ceil(
577+
(ask_instructions_result.instruction.length +
578+
ask_instructions_result.skill_definitions.length) /
579+
4
580+
),
581+
edit_format_instructions_token_count: Math.ceil(
582+
formatted_system_instructions.length / 4
583+
)
584+
})
585+
}
586+
539587
public send_selected_files() {
540588
const file_paths = get_selected_files({
541589
workspace_provider: this.workspace_provider,
@@ -612,6 +660,7 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
612660
handle_get_instructions(this)
613661
} else if (message.command == 'SAVE_INSTRUCTIONS') {
614662
await handle_save_instructions(this, message)
663+
this.send_token_count()
615664
} else if (message.command == 'GET_CONNECTION_STATUS') {
616665
handle_get_connection_status(this)
617666
} else if (message.command == 'GET_WEB_CONFIGURATIONS') {
@@ -650,8 +699,10 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
650699
await handle_delete_web_configuration(message)
651700
} else if (message.command == 'UNDO') {
652701
await handle_undo(this)
702+
this.send_token_count()
653703
} else if (message.command == 'APPLY_RESPONSE_FROM_HISTORY') {
654704
await handle_apply_response_from_history(message)
705+
this.send_token_count()
655706
} else if (message.command == 'EXECUTE_COMMAND') {
656707
await vscode.commands.executeCommand(message.command_id)
657708
} else if (message.command == 'MAKE_API_CALL') {
@@ -800,7 +851,7 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
800851
} else if (message.command == 'PICK_TASKS_WORKSPACE') {
801852
await handle_pick_tasks_workspace(this, message)
802853
} else if (message.command == 'GET_TOKEN_COUNT') {
803-
await handle_get_token_count(this)
854+
this.send_token_count()
804855
}
805856
} catch (error: any) {
806857
Logger.error({
@@ -907,5 +958,6 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
907958
edit_files: this.edit_files_instructions,
908959
caret_position: this.caret_position
909960
})
961+
this.send_token_count()
910962
}
911963
}

‎apps/editor/src/views/prompt/frontend/Main/Main.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type Props = {
6868
on_tabs_reorder: (new_order: number[]) => void
6969
missing_web_configuration?: boolean
7070
voice_input_push_to_talk: boolean
71-
token_count: number
71+
selected_files_token_count: number
7272
bottom_spacer_height?: number
7373
}
7474

@@ -487,7 +487,7 @@ export const Main: React.FC<Props> = (props) => {
487487
on_quick_action_click={handle_quick_action_click}
488488
current_selection={props.current_selection}
489489
chat_history={current_history || []}
490-
token_count={props.token_count}
490+
selected_files_token_count={props.selected_files_token_count}
491491
web_prompt_type={props.web_prompt_type}
492492
api_prompt_type={props.api_prompt_type}
493493
on_web_prompt_type_change={props.on_web_prompt_type_change}

‎apps/editor/src/views/prompt/frontend/Main/MainView/MainView.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type Props = {
5151
currently_open_file_path?: string
5252
current_selection?: SelectionState | null
5353
chat_history: string[]
54-
token_count: number
54+
selected_files_token_count: number
5555
web_prompt_type: WebPromptType
5656
api_prompt_type: ApiPromptType
5757
on_web_prompt_type_change: (prompt_type: WebPromptType) => void
@@ -329,7 +329,7 @@ export const MainView: React.FC<Props> = (props) => {
329329
on_tabs_reorder={props.on_tabs_reorder}
330330
warning={warning}
331331
voice_input_push_to_talk={props.voice_input_push_to_talk}
332-
token_count={props.token_count}
332+
token_count={props.selected_files_token_count}
333333
currently_open_file_path={props.currently_open_file_path}
334334
translations={{
335335
voice_input: t('prompt-field.voice-input'),

‎apps/editor/src/views/prompt/frontend/Prompt.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const Prompt = () => {
7373
handle_tabs_reorder,
7474
voice_input_push_to_talk,
7575
is_modern_ui,
76-
token_count
76+
selected_files_token_count
7777
} = use_panel(vscode)
7878

7979
const {
@@ -349,7 +349,7 @@ export const Prompt = () => {
349349
on_tab_delete={handle_tab_delete}
350350
on_tabs_reorder={handle_tabs_reorder}
351351
voice_input_push_to_talk={voice_input_push_to_talk}
352-
token_count={token_count}
352+
selected_files_token_count={selected_files_token_count}
353353
/>
354354
</div>
355355
<div

‎apps/editor/src/views/prompt/frontend/hooks/use-prompt/use-prompt.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const use_prompt = (vscode: any) => {
2525
const [chat_input_focus_key, set_chat_input_focus_key] = useState(0)
2626
const [chat_input_focus_and_select_key, set_chat_input_focus_and_select_key] =
2727
useState(0)
28-
const [token_count, set_token_count] = useState<number>(0)
28+
const [selected_files_token_count, set_selected_files_token_count] =
29+
useState<number>(0)
2930

3031
const {
3132
ask_about_context_instructions,
@@ -119,7 +120,7 @@ export const use_prompt = (vscode: any) => {
119120
} else if (message.command == 'VOICE_INPUT_PUSH_TO_TALK') {
120121
set_voice_input_push_to_talk(message.enabled)
121122
} else if (message.command == 'TOKEN_COUNT_UPDATED') {
122-
set_token_count(message.token_count)
123+
set_selected_files_token_count(message.selected_files_token_count)
123124
} else if (message.command == 'IS_MODERN_UI') {
124125
set_is_modern_ui(message.is_modern_ui)
125126
}
@@ -236,6 +237,6 @@ export const use_prompt = (vscode: any) => {
236237
handle_tabs_reorder,
237238
voice_input_push_to_talk,
238239
is_modern_ui,
239-
token_count
240+
selected_files_token_count
240241
}
241242
}

‎apps/editor/src/views/prompt/types/messages.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,10 @@ export interface ResponseHistoryMessage extends BaseMessage {
660660

661661
export interface TokenCountMessage extends BaseMessage {
662662
command: 'TOKEN_COUNT_UPDATED'
663-
token_count: number
663+
selected_files_token_count: number
664+
edit_instructions_token_count: number
665+
ask_instructions_token_count: number
666+
edit_format_instructions_token_count: number
664667
}
665668

666669
export interface WebConfigurationUpdatedMessage extends BaseMessage {

0 commit comments

Comments
 (0)