/**
 * agent.json tools 白名单：是否启用 GitTool（含 *；disallowedTools 优先）。
 */

type AgentToolsBinding = {
  tools: string[]
  disallowedTools?: string[]
}

function toolNameFromSpec(spec: string): string {
  const trimmed = spec.trim()
  const paren = trimmed.indexOf('(')
  if (paren === -1) {
    return trimmed
  }
  return trimmed.slice(0, paren).trim()
}

/** agent.json 是否启用 GitTool */
export function isEnabledInAgentConfig(agent: AgentToolsBinding): boolean {
  const disallowed = new Set(
    (agent.disallowedTools ?? []).map(toolNameFromSpec),
  )
  if (disallowed.has('GitTool')) {
    return false
  }
  for (const spec of agent.tools) {
    const toolName = toolNameFromSpec(spec)
    if (toolName === '*' || toolName === 'GitTool') {
      return true
    }
  }
  return false
}
