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 是否启用 CodeTool */
export function isEnabledInAgentConfig(agent: AgentToolsBinding): boolean {
  const disallowed = new Set((agent.disallowedTools ?? []).map(toolNameFromSpec))
  if (disallowed.has('CodeTool')) {
    return false
  }
  for (const spec of agent.tools) {
    const toolName = toolNameFromSpec(spec)
    if (toolName === '*' || toolName === 'CodeTool') {
      return true
    }
  }
  return false
}
