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

Function drainCommandQueue

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

Source from the content-addressed store, hash-verified

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

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