MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / normalizeToolInput

Function normalizeToolInput

source code/utils/api.ts:568–683  ·  view source on GitHub ↗
(
  tool: T,
  input: z.infer<T['inputSchema']>,
  agentId?: AgentId,
)

Source from the content-addressed store, hash-verified

566
567// TODO: Generalize this to all tools
568export function normalizeToolInput<T extends Tool>(
569 tool: T,
570 input: z.infer<T['inputSchema']>,
571 agentId?: AgentId,
572): z.infer<T['inputSchema']> {
573 switch (tool.name) {
574 case EXIT_PLAN_MODE_V2_TOOL_NAME: {
575 // Always inject plan content and file path for ExitPlanModeV2 so hooks/SDK get the plan.
576 // The V2 tool reads plan from file instead of input, but hooks/SDK
577 const plan = getPlan(agentId)
578 const planFilePath = getPlanFilePath(agentId)
579 // Persist file snapshot for CCR sessions so the plan survives pod recycling
580 void persistFileSnapshotIfRemote()
581 return plan !== null ? { ...input, plan, planFilePath } : input
582 }
583 case BashTool.name: {
584 // Validated upstream, won't throw
585 const parsed = BashTool.inputSchema.parse(input)
586 const { command, timeout, description } = parsed
587 const cwd = getCwd()
588 let normalizedCommand = command.replace(`cd ${cwd} && `, '')
589 if (getPlatform() === 'windows') {
590 normalizedCommand = normalizedCommand.replace(
591 `cd ${windowsPathToPosixPath(cwd)} && `,
592 '',
593 )
594 }
595
596 // Replace \\; with \; (commonly needed for find -exec commands)
597 normalizedCommand = normalizedCommand.replace(/\\\\;/g, '\\;')
598
599 // Logging for commands that are only echoing a string. This is to help us understand how often Claude talks via bash
600 if (/^echo\s+["']?[^|&;><]*["']?$/i.test(normalizedCommand.trim())) {
601 logEvent('tengu_bash_tool_simple_echo', {})
602 }
603
604 // Check for run_in_background (may not exist in schema if CLAUDE_CODE_DISABLE_BACKGROUND_TASKS is set)
605 const run_in_background =
606 'run_in_background' in parsed ? parsed.run_in_background : undefined
607
608 // SAFETY: Cast is safe because input was validated by .parse() above.
609 // TypeScript can't narrow the generic T based on switch(tool.name), so it
610 // doesn't know the return type matches T['inputSchema']. This is a fundamental
611 // TS limitation with generics, not bypassable without major refactoring.
612 return {
613 command: normalizedCommand,
614 description,
615 ...(timeout !== undefined && { timeout }),
616 ...(description !== undefined && { description }),
617 ...(run_in_background !== undefined && { run_in_background }),
618 ...('dangerouslyDisableSandbox' in parsed &&
619 parsed.dangerouslyDisableSandbox !== undefined && {
620 dangerouslyDisableSandbox: parsed.dangerouslyDisableSandbox,
621 }),
622 } as z.infer<T['inputSchema']>
623 }
624 case FileEditTool.name: {
625 // Validated upstream, won't throw

Callers 1

normalizeContentFromAPIFunction · 0.85

Calls 8

getPlanFunction · 0.85
getPlanFilePathFunction · 0.85
getCwdFunction · 0.85
getPlatformFunction · 0.85
logEventFunction · 0.85
normalizeFileEditInputFunction · 0.85
stripTrailingWhitespaceFunction · 0.85

Tested by

no test coverage detected