@@ -2,7 +2,10 @@ import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
22import { ChatsViewProvider } from '@/views/chats/backend/chats-view-provider'
33import { send_llm_message } from '@/utils/send-llm-message'
44import { randomUUID , createHash } from 'crypto'
5+ import { display_token_count } from '@shared/utils/display-token-count'
56import { Logger } from '@shared/utils/logger'
7+ import * as vscode from 'vscode'
8+ import { t } from '@/i18n'
69
710const CHAIN_RESOLUTION_DELAY_MS = 5000
811
@@ -76,122 +79,155 @@ export class PromptViewApiCallsManager {
7679 } )
7780 }
7881
79- try {
80- const is_queued =
81- previous_waiting && previous_waiting . body_hash == body_hash
82-
83- this . broadcast_message ( {
84- command : 'SHOW_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS' ,
85- id : request_id ,
86- status : is_queued ? 'Queued...' : 'Waiting for server...' ,
87- provider_name : params . provider_name ,
88- model : params . model ,
89- reasoning_effort : params . reasoning_effort
90- } )
91-
92- if ( is_queued ) {
93- if ( abort_controller . signal . aborted ) {
94- throw abort_controller . signal . reason
95- }
96-
97- const abort_promise = new Promise < void > ( ( _ , reject ) => {
98- abort_controller . signal . addEventListener (
99- 'abort' ,
100- ( ) => {
101- reject ( abort_controller . signal . reason )
102- } ,
103- { once : true }
104- )
82+ return await vscode . window . withProgress (
83+ {
84+ location : vscode . ProgressLocation . Notification ,
85+ title : t ( 'service.prompt-view-api-calls-manager.sent-request' ) ,
86+ cancellable : true
87+ } ,
88+ async ( progress , token ) => {
89+ token . onCancellationRequested ( ( ) => {
90+ this . cancel_api_call ( request_id )
10591 } )
10692
107- await Promise . race ( [ previous_waiting . promise , abort_promise ] )
93+ try {
94+ const is_queued =
95+ previous_waiting && previous_waiting . body_hash == body_hash
10896
109- this . broadcast_message ( {
110- command : 'SHOW_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS' ,
111- id : request_id ,
112- status : 'Waiting for server...' ,
113- provider_name : params . provider_name ,
114- model : params . model ,
115- reasoning_effort : params . reasoning_effort
116- } )
117- }
97+ const queued_msg = t ( 'common.progress.queued' )
98+ const waiting_msg = t ( 'common.progress.waiting-for-server' )
99+ const thinking_msg = t ( 'common.progress.thinking' )
100+ const receiving_msg = t ( 'common.progress.receiving' )
101+
102+ progress . report ( { message : is_queued ? queued_msg : waiting_msg } )
118103
119- const result = await send_llm_message ( {
120- base_url : params . base_url ,
121- api_key : params . api_key ,
122- body : params . body ,
123- abort_signal : abort_controller . signal ,
124- on_thinking_chunk : ( ) => {
125- schedule_chain_resolution ( )
126- this . broadcast_message ( {
127- command : 'SHOW_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS' ,
128- id : request_id ,
129- status : 'Thinking...' ,
130- provider_name : params . provider_name ,
131- model : params . model ,
132- reasoning_effort : params . reasoning_effort
133- } )
134- } ,
135- on_chunk : ( tokens_per_second , total_tokens ) => {
136- schedule_chain_resolution ( )
137104 this . broadcast_message ( {
138105 command : 'SHOW_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS' ,
139106 id : request_id ,
140- status : 'Receiving...' ,
141- tokens_per_second,
142- total_tokens,
107+ status : is_queued ? queued_msg : waiting_msg ,
143108 provider_name : params . provider_name ,
144109 model : params . model ,
145110 reasoning_effort : params . reasoning_effort
146111 } )
147- }
148- } )
149-
150- if ( result ) {
151- this . chats_view_provider . add_chat ( {
152- timestamp : Date . now ( ) ,
153- provider_name : params . provider_name ,
154- model : params . model ,
155- reasoning_effort : params . reasoning_effort ,
156- raw_instructions : params . raw_instructions ,
157- response : result . response ,
158- thoughts : result . thoughts
159- } )
160- }
161112
162- return result
163- } catch ( error : any ) {
164- if ( abort_controller . signal . aborted ) {
165- throw error
166- }
113+ if ( is_queued ) {
114+ if ( abort_controller . signal . aborted ) {
115+ throw abort_controller . signal . reason
116+ }
117+
118+ const abort_promise = new Promise < void > ( ( _ , reject ) => {
119+ abort_controller . signal . addEventListener (
120+ 'abort' ,
121+ ( ) => {
122+ reject ( abort_controller . signal . reason )
123+ } ,
124+ { once : true }
125+ )
126+ } )
127+
128+ await Promise . race ( [ previous_waiting . promise , abort_promise ] )
129+
130+ progress . report ( { message : waiting_msg } )
131+
132+ this . broadcast_message ( {
133+ command : 'SHOW_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS' ,
134+ id : request_id ,
135+ status : waiting_msg ,
136+ provider_name : params . provider_name ,
137+ model : params . model ,
138+ reasoning_effort : params . reasoning_effort
139+ } )
140+ }
141+
142+ const result = await send_llm_message ( {
143+ base_url : params . base_url ,
144+ api_key : params . api_key ,
145+ body : params . body ,
146+ abort_signal : abort_controller . signal ,
147+ on_thinking_chunk : ( ) => {
148+ schedule_chain_resolution ( )
149+ progress . report ( { message : thinking_msg } )
150+ this . broadcast_message ( {
151+ command : 'SHOW_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS' ,
152+ id : request_id ,
153+ status : thinking_msg ,
154+ provider_name : params . provider_name ,
155+ model : params . model ,
156+ reasoning_effort : params . reasoning_effort
157+ } )
158+ } ,
159+ on_chunk : ( tokens_per_second , total_tokens ) => {
160+ schedule_chain_resolution ( )
161+
162+ const msg =
163+ tokens_per_second && total_tokens
164+ ? t ( 'common.progress.receiving-stats' , {
165+ total_tokens : display_token_count ( total_tokens ) ,
166+ tokens_per_second : Math . round ( tokens_per_second )
167+ } )
168+ : receiving_msg
169+
170+ progress . report ( { message : msg } )
171+ this . broadcast_message ( {
172+ command : 'SHOW_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS' ,
173+ id : request_id ,
174+ status : msg ,
175+ tokens_per_second,
176+ total_tokens,
177+ provider_name : params . provider_name ,
178+ model : params . model ,
179+ reasoning_effort : params . reasoning_effort
180+ } )
181+ }
182+ } )
167183
168- Logger . error ( {
169- function_name : 'send_llm_message' ,
170- message : 'API call error' ,
171- data : error
172- } )
184+ if ( result ) {
185+ this . chats_view_provider . add_chat ( {
186+ timestamp : Date . now ( ) ,
187+ provider_name : params . provider_name ,
188+ model : params . model ,
189+ reasoning_effort : params . reasoning_effort ,
190+ raw_instructions : params . raw_instructions ,
191+ response : result . response ,
192+ thoughts : result . thoughts
193+ } )
194+ }
195+
196+ return result
197+ } catch ( error : any ) {
198+ if ( abort_controller . signal . aborted ) {
199+ throw error
200+ }
201+
202+ Logger . error ( {
203+ function_name : 'send_llm_message' ,
204+ message : 'API call error' ,
205+ data : error
206+ } )
173207
174- return null
175- } finally {
176- if ( ! is_chain_resolution_scheduled ) {
177- resolve_current ( )
178- }
208+ return null
209+ } finally {
210+ if ( ! is_chain_resolution_scheduled ) {
211+ resolve_current ( )
212+ }
179213
180- this . broadcast_message ( {
181- command : 'HIDE_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS' ,
182- id : request_id
183- } )
184- this . abort_controllers . delete ( request_id )
214+ this . broadcast_message ( {
215+ command : 'HIDE_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS' ,
216+ id : request_id
217+ } )
218+ this . abort_controllers . delete ( request_id )
185219
186- const now = Date . now ( )
187- const wait_until = Math . max ( now , this . next_allowed_finish_time )
188- const delay = wait_until - now
189- this . next_allowed_finish_time = wait_until + 500
220+ const now = Date . now ( )
221+ const wait_until = Math . max ( now , this . next_allowed_finish_time )
222+ const delay = wait_until - now
223+ this . next_allowed_finish_time = wait_until + 500
190224
191- if ( delay > 0 ) {
192- await new Promise ( ( resolve ) => setTimeout ( resolve , delay ) )
225+ if ( delay > 0 ) {
226+ await new Promise ( ( resolve ) => setTimeout ( resolve , delay ) )
227+ }
228+ }
193229 }
194- }
230+ )
195231 }
196232
197233 public cancel_api_call ( request_id : string ) {
0 commit comments