Skip to content

Commit 168de43

Browse files
committed
Use singleton helper functions to cache and reuse output channels for agent search and reasoning logs
- output channel should focus on showing the agent returned value, not executable etc - should not dispose, so user can see it afterwards. on new search should clean the old state - re-running in the same flow should also clear output channel - log reasoning to output channel - clear reasoning with a new request - use similar convention in `apps/editor/src/features/search-files/search-modes/perform-agent-search-mode/perform-agent-search-mode.ts` [Pasted text]
1 parent f65be7a commit 168de43

2 files changed

Lines changed: 27 additions & 22 deletions

File tree

‎apps/editor/src/features/search-files/search-modes/perform-agent-search-mode/perform-agent-search-mode.ts‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ import { muse_agent } from './agents/muse'
2323
import { CodingAgent } from './types'
2424
import { format_duration } from './utils'
2525

26+
let _output_channel: vscode.OutputChannel | undefined
27+
28+
const get_output_channel = () => {
29+
if (!_output_channel) {
30+
_output_channel = vscode.window.createOutputChannel('CWC Agentic Search')
31+
}
32+
return _output_channel
33+
}
34+
2635
const AGENTS: CodingAgent[] = [
2736
antigravity_agent,
2837
codex_agent,
@@ -60,8 +69,9 @@ export const perform_agent_search_mode = async (params: {
6069
return undefined
6170
}
6271

63-
const output_channel = vscode.window.createOutputChannel('CWC Agentic Search')
64-
try {
72+
const output_channel = get_output_channel()
73+
74+
{
6575
while (true) {
6676
const initial_query =
6777
local_queries[LAST_SEARCH_FILES_AGENT_QUERY_STATE_KEY] !== undefined
@@ -499,6 +509,8 @@ export const perform_agent_search_mode = async (params: {
499509
let is_cancelled = false
500510
const start_time = Date.now()
501511

512+
output_channel.clear()
513+
502514
try {
503515
await vscode.window.withProgress(
504516
{
@@ -518,10 +530,6 @@ export const perform_agent_search_mode = async (params: {
518530
stdio: ['ignore', 'pipe', 'pipe']
519531
})
520532

521-
output_channel.appendLine(
522-
`\n${executable} ${args.join(' ')}\n`
523-
)
524-
525533
child.stdout.on('data', (data) => {
526534
const chunk = data.toString()
527535
output_channel.append(chunk)
@@ -564,10 +572,7 @@ export const perform_agent_search_mode = async (params: {
564572
resolve()
565573
})
566574

567-
child.on('close', (code) => {
568-
output_channel.appendLine(
569-
`\nProcess exited with code ${code}\n`
570-
)
575+
child.on('close', () => {
571576
if (
572577
agent_info.parse_final_output &&
573578
raw_stream_output.trim()
@@ -590,9 +595,6 @@ export const perform_agent_search_mode = async (params: {
590595
})
591596

592597
child.on('error', (err) => {
593-
output_channel.appendLine(
594-
`\nProcess error: ${err.message}\n`
595-
)
596598
Logger.error({
597599
function_name: 'perform_agent_search_mode',
598600
message: 'Agent execution failed',
@@ -755,7 +757,5 @@ export const perform_agent_search_mode = async (params: {
755757
}
756758

757759
return undefined
758-
} finally {
759-
output_channel.dispose()
760760
}
761761
}

‎apps/editor/src/utils/send-llm-message.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ import { Logger } from '@shared/utils/logger'
66
type StreamCallback = (tokens_per_second: number, total_tokens: number) => void
77
type ThinkingStreamCallback = (text: string) => void
88

9-
let reasoning_output_channel: vscode.OutputChannel | undefined
9+
let _reasoning_output_channel: vscode.OutputChannel | undefined
10+
11+
const get_reasoning_output_channel = () => {
12+
if (!_reasoning_output_channel) {
13+
_reasoning_output_channel =
14+
vscode.window.createOutputChannel('CWC Reasoning')
15+
}
16+
return _reasoning_output_channel
17+
}
1018

1119
const DATA_PREFIX = 'data: '
1220
const DONE_TOKEN = '[DONE]'
@@ -87,6 +95,8 @@ export const send_llm_message = async (params: {
8795
on_thinking_chunk?: ThinkingStreamCallback
8896
rethrow_error?: boolean
8997
}): Promise<{ response: string; thoughts?: string } | null> => {
98+
get_reasoning_output_channel().clear()
99+
90100
let stream_start_time = 0
91101
let last_on_chunk_time = 0
92102
let total_tokens = 0
@@ -123,16 +133,11 @@ export const send_llm_message = async (params: {
123133
const handle_thinking_chunk = (chunk: string) => {
124134
if (!chunk) return
125135

126-
if (!reasoning_output_channel) {
127-
reasoning_output_channel =
128-
vscode.window.createOutputChannel('CWC Reasoning')
129-
}
130-
131136
if (is_first_thinking_chunk) {
132137
is_first_thinking_chunk = false
133138
}
134139

135-
reasoning_output_channel.append(chunk)
140+
get_reasoning_output_channel().append(chunk)
136141

137142
if (params.on_thinking_chunk) {
138143
params.on_thinking_chunk(chunk)

0 commit comments

Comments
 (0)