MCPcopy Create free account
hub / github.com/WJX20/claude-code / drainCommandQueue

Function drainCommandQueue

src/cli/print.ts:1937–2370  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1935 // ask() call so messages that queued up during a long turn coalesce
1936 // into a single follow-up turn instead of N separate turns.
1937 const drainCommandQueue = async () => {
1938 while ((command = dequeue(isMainThread))) {
1939 if (
1940 command.mode !== 'prompt' &&
1941 command.mode !== 'orphaned-permission' &&
1942 command.mode !== 'task-notification'
1943 ) {
1944 throw new Error(
1945 'only prompt commands are supported in streaming mode',
1946 )
1947 }
1948
1949 // Non-prompt commands (task-notification, orphaned-permission) carry
1950 // side effects or orphanedPermission state, so they process singly.
1951 // Prompt commands greedily collect followers with matching workload.
1952 const batch: QueuedCommand[] = [command]
1953 if (command.mode === 'prompt') {
1954 while (canBatchWith(command, peek(isMainThread))) {
1955 batch.push(dequeue(isMainThread)!)
1956 }
1957 if (batch.length > 1) {
1958 command = {
1959 ...command,
1960 value: joinPromptValues(batch.map(c => c.value)),
1961 uuid: batch.findLast(c => c.uuid)?.uuid ?? command.uuid,
1962 }
1963 }
1964 }
1965 const batchUuids = batch.map(c => c.uuid).filter(u => u !== undefined)
1966
1967 // QueryEngine will emit a replay for command.uuid (the last uuid in
1968 // the batch) via its messagesToAck path. Emit replays here for the
1969 // rest so consumers that track per-uuid delivery (clank's
1970 // asyncMessages footer, CCR) see an ack for every message they sent,
1971 // not just the one that survived the merge.
1972 if (options.replayUserMessages && batch.length > 1) {
1973 for (const c of batch) {
1974 if (c.uuid && c.uuid !== command.uuid) {
1975 output.enqueue({
1976 type: 'user',
1977 content: c.value,
1978 message: { role: 'user', content: c.value },
1979 session_id: getSessionId(),
1980 parent_tool_use_id: null,
1981 uuid: c.uuid as string,
1982 isReplay: true,
1983 } as SDKUserMessageReplay)
1984 }
1985 }
1986 }
1987
1988 // Combine all MCP clients. appState.mcp is populated incrementally
1989 // per-server by main.tsx (mirrors useManageMCPConnections). Reading
1990 // fresh per-command means late-connecting servers are visible on the
1991 // next turn. registerElicitationHandlers is idempotent (tracking set).
1992 const appState = getAppState()
1993 const allMcpClients = [
1994 ...appState.mcp.clients,

Callers 1

runFunction · 0.85

Calls 15

dequeueFunction · 0.85
canBatchWithFunction · 0.85
joinPromptValuesFunction · 0.85
getSessionIdFunction · 0.85
buildAllToolsFunction · 0.85
notifyCommandLifecycleFunction · 0.85
isValidStatusFunction · 0.85
logEventFunction · 0.85
logSuggestionOutcomeFunction · 0.85
createAbortControllerFunction · 0.85

Tested by

no test coverage detected