Skip to content

Commit e930a10

Browse files
committed
Implement reusable WaitingChain utility and integrate it into API calls and CLI invocations
- CLI > Edit/Ask: when invoking the same configuration, they should be queued, just like we queue API calls. It helps with prompt cache. - as soon the agent streams anything (first call of parse_stream_line that yields other notification text than 'waiting for agent'), proceed with the queue (streaming means initial prompt got cached and agent generates response) - `apps/editor/src/utils/agentic-cli-invocation/invoke-agentic-cli.ts` should queue only non-interactive invocations - queue for API calls seems broken (it works great for CLI invocations though). The observed behavior is that once one request finishes, the other is still queued. Expected behavior is that once one enters 'thinking' or completes, the other starts. ├── apps │ └── editor │ └── src │ ├── commands │ │ ├── code-at-cursor-command │ │ │ └── utils │ │ │ └── perform-code-at-cursor.ts │ │ └── generate-commit-message-commands │ │ ├── actions │ │ │ └── run-generate-action.ts │ │ └── utils │ │ └── generate-commit-message-with-api.ts │ ├── constants │ │ └── agents.ts │ ├── extension.ts │ ├── features │ │ ├── agentic-search │ │ │ └── agentic-search.ts │ │ ├── apply-response │ │ │ └── apply-response.ts │ │ └── search-files │ │ ├── search-modes │ │ │ └── perform-intelligent-search-mode.ts │ │ └── utils │ │ └── search-files-by-intelligent.ts │ ├── i18n │ │ └── translations │ │ └── services │ │ └── prompt-view-api-calls-manager.ts │ ├── services │ │ ├── git-discard-file-watcher.ts │ │ ├── model-fetcher.ts │ │ ├── prompt-view-api-calls-manager.ts │ │ ├── providers-manager.ts │ │ ├── websocket-manager.ts │ │ └── websocket-server-process.ts │ ├── types │ │ └── cli-configuration.ts │ ├── utils │ │ ├── agentic-cli-invocation │ │ │ ├── agents │ │ │ │ ├── antigravity.ts │ │ │ │ ├── claude.ts │ │ │ │ ├── codex.ts │ │ │ │ ├── copilot.ts │ │ │ │ ├── cursor.ts │ │ │ │ ├── devin.ts │ │ │ │ ├── grok.ts │ │ │ │ ├── index.ts │ │ │ │ └── opencode.ts │ │ │ ├── index.ts │ │ │ ├── invoke-agentic-cli.ts │ │ │ ├── types.ts │ │ │ └── utils │ │ │ ├── check-command-exists.ts │ │ │ ├── format-duration.ts │ │ │ ├── generate-diff-markdown.ts │ │ │ └── get-progress-dots.ts │ │ ├── cli-configuration-format-converters.ts │ │ ├── send-llm-message.ts │ │ └── waiting-chain.ts │ └── views │ ├── chats │ │ ├── backend │ │ │ └── chats-view-provider.ts │ │ ├── frontend │ │ │ └── Chats.tsx │ │ └── types │ │ └── messages.ts │ └── prompt │ ├── backend │ │ ├── message-handlers │ │ │ ├── handle-invoke-agentic-cli.ts │ │ │ ├── handle-make-api-call.ts │ │ │ ├── handle-patch-repair.ts │ │ │ ├── handle-save-terminal-interactivity.ts │ │ │ ├── handle-select-terminal-interactivity.ts │ │ │ ├── handle-voice-input.ts │ │ │ ├── index.ts │ │ │ └── utils │ │ │ ├── build-cli-prompt.ts │ │ │ └── patch-repair-utils.ts │ │ └── prompt-view-provider.ts │ ├── frontend │ │ └── Main │ │ ├── Main.tsx │ │ └── MainView │ │ └── MainView.tsx │ └── types │ └── messages.ts └── packages ├── shared │ └── src │ └── types │ └── terminal-interactivity.ts └── ui └── src └── components └── editor └── chats-view └── Progress └── Progress.tsx
1 parent 890e70e commit e930a10

4 files changed

Lines changed: 267 additions & 183 deletions

File tree

‎apps/editor/src/services/prompt-view-api-calls-manager.ts‎

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ import { send_llm_message } from '@/utils/send-llm-message'
44
import { randomUUID, createHash } from 'crypto'
55
import { display_token_count } from '@shared/utils/display-token-count'
66
import { Logger } from '@shared/utils/logger'
7+
import { WaitingChain } from '@/utils/waiting-chain'
78
import * as vscode from 'vscode'
89
import { t } from '@/i18n'
910

10-
const CHAIN_RESOLUTION_DELAY_MS = 5000
11-
1211
export class PromptViewApiCallsManager {
1312
private abort_controllers: Map<string, AbortController> = new Map()
1413
private next_allowed_finish_time = 0
15-
private waiting_chain: Map<
16-
string,
17-
{ promise: Promise<void>; resolve: () => void; body_hash: string }
18-
> = new Map()
14+
private waiting_chain = new WaitingChain()
1915

2016
constructor(
2117
private prompt_view_provider: PromptViewProvider,
@@ -53,34 +49,10 @@ export class PromptViewApiCallsManager {
5349
.update(JSON.stringify(body_to_hash))
5450
.digest('hex')
5551

56-
const previous_waiting = this.waiting_chain.get(params.base_url)
57-
58-
let resolve_current: () => void = () => {}
59-
const current_promise = new Promise<void>((resolve) => {
60-
resolve_current = resolve
52+
const gate = this.waiting_chain.enqueue({
53+
key: `${params.base_url}:${body_hash}`
6154
})
6255

63-
let is_chain_resolution_scheduled = false
64-
const schedule_chain_resolution = () => {
65-
if (is_chain_resolution_scheduled) return
66-
67-
const chain_entry = this.waiting_chain.get(params.base_url)
68-
if (chain_entry && chain_entry.resolve === resolve_current) {
69-
is_chain_resolution_scheduled = true
70-
setTimeout(() => {
71-
chain_entry.resolve()
72-
}, CHAIN_RESOLUTION_DELAY_MS)
73-
}
74-
}
75-
76-
if (!previous_waiting || previous_waiting.body_hash !== body_hash) {
77-
this.waiting_chain.set(params.base_url, {
78-
promise: current_promise,
79-
resolve: resolve_current,
80-
body_hash
81-
})
82-
}
83-
8456
return await vscode.window.withProgress(
8557
{
8658
location: vscode.ProgressLocation.Notification,
@@ -93,8 +65,7 @@ export class PromptViewApiCallsManager {
9365
})
9466

9567
try {
96-
const is_queued =
97-
previous_waiting && previous_waiting.body_hash === body_hash
68+
const is_queued = gate.must_wait
9869

9970
const queued_msg = t(
10071
'service.prompt-view-api-calls-manager.progress.queued'
@@ -115,21 +86,7 @@ export class PromptViewApiCallsManager {
11586
})
11687

11788
if (is_queued) {
118-
if (abort_controller.signal.aborted) {
119-
throw abort_controller.signal.reason
120-
}
121-
122-
const abort_promise = new Promise<void>((_, reject) => {
123-
abort_controller.signal.addEventListener(
124-
'abort',
125-
() => {
126-
reject(abort_controller.signal.reason)
127-
},
128-
{ once: true }
129-
)
130-
})
131-
132-
await Promise.race([previous_waiting.promise, abort_promise])
89+
await gate.wait(abort_controller.signal)
13390

13491
progress.report({ message: waiting_msg })
13592

@@ -149,7 +106,7 @@ export class PromptViewApiCallsManager {
149106
body: params.body,
150107
abort_signal: abort_controller.signal,
151108
on_thinking_chunk: () => {
152-
schedule_chain_resolution()
109+
gate.release()
153110
progress.report({ message: thinking_msg })
154111
this.broadcast_message({
155112
command: 'SHOW_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS',
@@ -161,7 +118,7 @@ export class PromptViewApiCallsManager {
161118
})
162119
},
163120
on_chunk: (tokens_per_second, total_tokens) => {
164-
schedule_chain_resolution()
121+
gate.release()
165122

166123
const msg =
167124
tokens_per_second && total_tokens
@@ -214,9 +171,7 @@ export class PromptViewApiCallsManager {
214171

215172
return null
216173
} finally {
217-
if (!is_chain_resolution_scheduled) {
218-
resolve_current()
219-
}
174+
gate.release()
220175

221176
this.broadcast_message({
222177
command: 'HIDE_PROMPT_VIEW_API_CALLS_MANAGER_PROGRESS',

0 commit comments

Comments
 (0)