MCPcopy
hub / github.com/claude-code-best/claude-code / query

Function query

src/query.ts:276–391  ·  view source on GitHub ↗
(
  params: QueryParams,
)

Source from the content-addressed store, hash-verified

274}
275
276export async function* query(
277 params: QueryParams,
278): AsyncGenerator<
279 | StreamEvent
280 | RequestStartEvent
281 | Message
282 | TombstoneMessage
283 | ToolUseSummaryMessage,
284 Terminal
285> {
286 const consumedCommandUuids: string[] = []
287 const consumedAutonomyCommands: QueuedCommand[] = []
288
289 // Create Langfuse trace for this query turn (no-op if not configured).
290 // When called as a sub-agent, langfuseTrace is already set by runAgent()
291 // — reuse it instead of creating an independent trace.
292 const ownsTrace = !params.toolUseContext.langfuseTrace
293 logForDebugging(
294 `[query] ownsTrace=${ownsTrace} incoming langfuseTrace=${params.toolUseContext.langfuseTrace ? 'present' : 'null/undefined'} isLangfuseEnabled=${isLangfuseEnabled()}`,
295 )
296 const langfuseTrace =
297 params.toolUseContext.langfuseTrace ??
298 (isLangfuseEnabled()
299 ? createTrace({
300 sessionId: getSessionId(),
301 model: params.toolUseContext.options.mainLoopModel,
302 provider: getAPIProvider(),
303 input: params.messages,
304 querySource: params.querySource,
305 })
306 : null)
307
308 // Attach trace to toolUseContext so tool execution can record observations
309 const paramsWithTrace: QueryParams = langfuseTrace
310 ? {
311 ...params,
312 toolUseContext: { ...params.toolUseContext, langfuseTrace },
313 }
314 : params
315
316 let terminal: Terminal | undefined
317 let didThrow = false
318 let thrownError: unknown
319 try {
320 terminal = yield* queryLoop(
321 paramsWithTrace,
322 consumedCommandUuids,
323 consumedAutonomyCommands,
324 )
325 } catch (error) {
326 didThrow = true
327 thrownError = error
328 throw error
329 } finally {
330 await finalizeAutonomyCommandsForTurn({
331 commands: consumedAutonomyCommands,
332 outcome: getAutonomyTurnOutcome({
333 terminal,

Callers 7

runAgentFunction · 0.85
submitMessageMethod · 0.85
runForkedAgentFunction · 0.85
execAgentHookFunction · 0.85
REPLFunction · 0.85
startBackgroundSessionFunction · 0.85

Calls 13

isLangfuseEnabledFunction · 0.85
createTraceFunction · 0.85
getSessionIdFunction · 0.85
getAPIProviderFunction · 0.85
queryLoopFunction · 0.85
getAutonomyTurnOutcomeFunction · 0.85
endTraceFunction · 0.85
flushLangfuseFunction · 0.85
notifyCommandLifecycleFunction · 0.85
clearMarksMethod · 0.80
logForDebuggingFunction · 0.50

Tested by

no test coverage detected